diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index 12afb7bc2..420e4374e 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -27,7 +27,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) int openFlagsModes = O_WRONLY | O_CREAT | O_TRUNC; int openFlagsRights = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; - int fd = open(fileName, openFlagsModes, openFlagsRights); + int FF_AUTO_CLOSE_FD fd = open(fileName, openFlagsModes, openFlagsRights); if(fd == -1) { createSubfolders(fileName); @@ -36,11 +36,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) return false; } - bool ret = write(fd, data, dataSize) != -1; - - close(fd); - - return ret; + return write(fd, data, dataSize) > 0; } bool ffAppendFDBuffer(int fd, FFstrbuf* buffer) @@ -77,28 +73,20 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer) ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data) { - int fd = open(fileName, O_RDONLY); + int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY); if(fd == -1) return -1; - ssize_t readed = ffReadFDData(fd, dataSize, data); - - close(fd); - - return readed; + return ffReadFDData(fd, dataSize, data); } bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer) { - int fd = open(fileName, O_RDONLY); + int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY); if(fd == -1) return false; - bool ret = ffAppendFDBuffer(fd, buffer); - - close(fd); - - return ret; + return ffAppendFDBuffer(fd, buffer); } bool ffPathExists(const char* path, FFPathType type) diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index 152c7c9a4..78cdad740 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -2,7 +2,7 @@ static void createSubfolders(const char* fileName) { - FFstrbuf path; + FF_STRBUF_AUTO_DESTROY path; ffStrbufInit(&path); while(*fileName != '\0') @@ -12,13 +12,11 @@ static void createSubfolders(const char* fileName) CreateDirectoryA(path.chars, NULL); ++fileName; } - - ffStrbufDestroy(&path); } bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) { - HANDLE handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(handle == INVALID_HANDLE_VALUE) { createSubfolders(fileName); @@ -28,11 +26,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) } DWORD written; - bool ret = !!WriteFile(handle, data, (DWORD)dataSize, &written, NULL); - - CloseHandle(handle); - - return ret; + return !!WriteFile(handle, data, (DWORD)dataSize, &written, NULL); } bool ffAppendFDBuffer(HANDLE handle, FFstrbuf* buffer) @@ -69,28 +63,20 @@ bool ffAppendFDBuffer(HANDLE handle, FFstrbuf* buffer) ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data) { - HANDLE handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(handle == INVALID_HANDLE_VALUE) return -1; - ssize_t readed = ffReadFDData(handle, dataSize, data); - - CloseHandle(handle); - - return readed; + return ffReadFDData(handle, dataSize, data); } bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer) { - HANDLE handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(handle == INVALID_HANDLE_VALUE) return false; - bool ret = ffAppendFDBuffer(handle, buffer); - - CloseHandle(handle); - - return ret; + return ffAppendFDBuffer(handle, buffer); } bool ffPathExists(const char* path, FFPathType type)