From 11b1b975677e034857c867d250bd70744c3c81cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 18 Dec 2023 21:00:19 +0800 Subject: [PATCH] IO: only retry when open() error is ENOENT / ERROR_PATH_NOT_FOUND Close: #663 --- src/common/io/io.h | 1 + src/common/io/io_unix.c | 13 +++++++++---- src/common/io/io_windows.c | 13 +++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/common/io/io.h b/src/common/io/io.h index f499d1332..257e9ce83 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -10,6 +10,7 @@ #else #include #include + #include typedef int FFNativeFD; // procfs's file can be changed between read calls such as /proc/meminfo and /proc/uptime. // one safe way to read correct data is reading the whole file in a single read syscall diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index fcfe05718..546231198 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -3,10 +3,10 @@ #include "util/unused.h" #include -#include #include #include #include +#include #if __has_include() #include @@ -33,9 +33,14 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) int FF_AUTO_CLOSE_FD fd = open(fileName, openFlagsModes, openFlagsRights); if(fd == -1) { - createSubfolders(fileName); - fd = open(fileName, openFlagsModes, openFlagsRights); - if(fd == -1) + if (errno == ENOENT) + { + createSubfolders(fileName); + fd = open(fileName, openFlagsModes, openFlagsRights); + if(fd == -1) + return false; + } + else return false; } diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index 3a520f098..e53a1cee6 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -18,11 +18,16 @@ static void createSubfolders(const char* fileName) bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) { 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) + if (handle == INVALID_HANDLE_VALUE) { - createSubfolders(fileName); - handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if(handle == INVALID_HANDLE_VALUE) + if (GetLastError() == ERROR_PATH_NOT_FOUND) + { + createSubfolders(fileName); + handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (handle == INVALID_HANDLE_VALUE) + return false; + } + else return false; }