Skip to content

Commit 11b1b97

Browse files
committed
IO: only retry when open() error is ENOENT / ERROR_PATH_NOT_FOUND
Close: #663
1 parent 4ae0c43 commit 11b1b97

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/common/io/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#else
1111
#include <unistd.h>
1212
#include <dirent.h>
13+
#include <sys/stat.h>
1314
typedef int FFNativeFD;
1415
// procfs's file can be changed between read calls such as /proc/meminfo and /proc/uptime.
1516
// one safe way to read correct data is reading the whole file in a single read syscall

src/common/io/io_unix.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include "util/unused.h"
44

55
#include <fcntl.h>
6-
#include <sys/stat.h>
76
#include <termios.h>
87
#include <poll.h>
98
#include <dirent.h>
9+
#include <errno.h>
1010

1111
#if __has_include(<wordexp.h>)
1212
#include <wordexp.h>
@@ -33,9 +33,14 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
3333
int FF_AUTO_CLOSE_FD fd = open(fileName, openFlagsModes, openFlagsRights);
3434
if(fd == -1)
3535
{
36-
createSubfolders(fileName);
37-
fd = open(fileName, openFlagsModes, openFlagsRights);
38-
if(fd == -1)
36+
if (errno == ENOENT)
37+
{
38+
createSubfolders(fileName);
39+
fd = open(fileName, openFlagsModes, openFlagsRights);
40+
if(fd == -1)
41+
return false;
42+
}
43+
else
3944
return false;
4045
}
4146

src/common/io/io_windows.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ static void createSubfolders(const char* fileName)
1818
bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
1919
{
2020
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
21-
if(handle == INVALID_HANDLE_VALUE)
21+
if (handle == INVALID_HANDLE_VALUE)
2222
{
23-
createSubfolders(fileName);
24-
handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
25-
if(handle == INVALID_HANDLE_VALUE)
23+
if (GetLastError() == ERROR_PATH_NOT_FOUND)
24+
{
25+
createSubfolders(fileName);
26+
handle = CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
27+
if (handle == INVALID_HANDLE_VALUE)
28+
return false;
29+
}
30+
else
2631
return false;
2732
}
2833

0 commit comments

Comments
 (0)