Skip to content

Commit c570c5b

Browse files
committed
IO: use FF_AUTO_CLOSE_FD
1 parent 52c304e commit c570c5b

File tree

2 files changed

+13
-39
lines changed

2 files changed

+13
-39
lines changed

src/common/io/io_unix.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
2727
int openFlagsModes = O_WRONLY | O_CREAT | O_TRUNC;
2828
int openFlagsRights = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2929

30-
int fd = open(fileName, openFlagsModes, openFlagsRights);
30+
int FF_AUTO_CLOSE_FD fd = open(fileName, openFlagsModes, openFlagsRights);
3131
if(fd == -1)
3232
{
3333
createSubfolders(fileName);
@@ -36,11 +36,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
3636
return false;
3737
}
3838

39-
bool ret = write(fd, data, dataSize) != -1;
40-
41-
close(fd);
42-
43-
return ret;
39+
return write(fd, data, dataSize) > 0;
4440
}
4541

4642
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
@@ -77,28 +73,20 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
7773

7874
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
7975
{
80-
int fd = open(fileName, O_RDONLY);
76+
int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY);
8177
if(fd == -1)
8278
return -1;
8379

84-
ssize_t readed = ffReadFDData(fd, dataSize, data);
85-
86-
close(fd);
87-
88-
return readed;
80+
return ffReadFDData(fd, dataSize, data);
8981
}
9082

9183
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
9284
{
93-
int fd = open(fileName, O_RDONLY);
85+
int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY);
9486
if(fd == -1)
9587
return false;
9688

97-
bool ret = ffAppendFDBuffer(fd, buffer);
98-
99-
close(fd);
100-
101-
return ret;
89+
return ffAppendFDBuffer(fd, buffer);
10290
}
10391

10492
bool ffPathExists(const char* path, FFPathType type)

src/common/io/io_windows.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
static void createSubfolders(const char* fileName)
44
{
5-
FFstrbuf path;
5+
FF_STRBUF_AUTO_DESTROY path;
66
ffStrbufInit(&path);
77

88
while(*fileName != '\0')
@@ -12,13 +12,11 @@ static void createSubfolders(const char* fileName)
1212
CreateDirectoryA(path.chars, NULL);
1313
++fileName;
1414
}
15-
16-
ffStrbufDestroy(&path);
1715
}
1816

1917
bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
2018
{
21-
HANDLE handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
19+
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2220
if(handle == INVALID_HANDLE_VALUE)
2321
{
2422
createSubfolders(fileName);
@@ -28,11 +26,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
2826
}
2927

3028
DWORD written;
31-
bool ret = !!WriteFile(handle, data, (DWORD)dataSize, &written, NULL);
32-
33-
CloseHandle(handle);
34-
35-
return ret;
29+
return !!WriteFile(handle, data, (DWORD)dataSize, &written, NULL);
3630
}
3731

3832
bool ffAppendFDBuffer(HANDLE handle, FFstrbuf* buffer)
@@ -69,28 +63,20 @@ bool ffAppendFDBuffer(HANDLE handle, FFstrbuf* buffer)
6963

7064
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
7165
{
72-
HANDLE handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
66+
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
7367
if(handle == INVALID_HANDLE_VALUE)
7468
return -1;
7569

76-
ssize_t readed = ffReadFDData(handle, dataSize, data);
77-
78-
CloseHandle(handle);
79-
80-
return readed;
70+
return ffReadFDData(handle, dataSize, data);
8171
}
8272

8373
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
8474
{
85-
HANDLE handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
75+
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
8676
if(handle == INVALID_HANDLE_VALUE)
8777
return false;
8878

89-
bool ret = ffAppendFDBuffer(handle, buffer);
90-
91-
CloseHandle(handle);
92-
93-
return ret;
79+
return ffAppendFDBuffer(handle, buffer);
9480
}
9581

9682
bool ffPathExists(const char* path, FFPathType type)

0 commit comments

Comments
 (0)