Skip to content

Commit 8fdaa98

Browse files
committed
IO: inlines more helper functions
1 parent 1580409 commit 8fdaa98

3 files changed

Lines changed: 97 additions & 114 deletions

File tree

src/common/impl/io_unix.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,6 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
9797
return buffer->length > 0;
9898
}
9999

100-
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
101-
{
102-
int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY | O_CLOEXEC);
103-
if(fd == -1)
104-
return -1;
105-
106-
return ffReadFDData(fd, dataSize, data);
107-
}
108-
109-
ssize_t ffReadFileDataRelative(int dfd, const char* fileName, size_t dataSize, void* data)
110-
{
111-
int FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
112-
if(fd == -1)
113-
return -1;
114-
115-
return ffReadFDData(fd, dataSize, data);
116-
}
117-
118-
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
119-
{
120-
int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY | O_CLOEXEC);
121-
if(fd == -1)
122-
return false;
123-
124-
return ffAppendFDBuffer(fd, buffer);
125-
}
126-
127-
bool ffAppendFileBufferRelative(int dfd, const char* fileName, FFstrbuf* buffer)
128-
{
129-
int FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
130-
if(fd == -1)
131-
return false;
132-
133-
return ffAppendFDBuffer(fd, buffer);
134-
}
135-
136100
bool ffPathExpandEnv(const char* in, FFstrbuf* out)
137101
{
138102
bool result = false;

src/common/impl/io_windows.c

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -225,24 +225,6 @@ bool ffAppendFDBuffer(HANDLE handle, FFstrbuf* buffer)
225225
return buffer->length > 0;
226226
}
227227

228-
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
229-
{
230-
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
231-
if(handle == INVALID_HANDLE_VALUE)
232-
return -1;
233-
234-
return ffReadFDData(handle, dataSize, data);
235-
}
236-
237-
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
238-
{
239-
HANDLE FF_AUTO_CLOSE_FD handle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
240-
if(handle == INVALID_HANDLE_VALUE)
241-
return false;
242-
243-
return ffAppendFDBuffer(handle, buffer);
244-
}
245-
246228
HANDLE openatW(HANDLE dfd, const wchar_t* fileName, uint16_t fileNameLen, bool directory)
247229
{
248230
assert(fileNameLen <= 0x7FFF);
@@ -269,7 +251,7 @@ HANDLE openatW(HANDLE dfd, const wchar_t* fileName, uint16_t fileNameLen, bool d
269251
return hFile;
270252
}
271253

272-
HANDLE openat(HANDLE dfd, const char* fileName, bool directory)
254+
HANDLE openat(HANDLE dfd, const char* fileName, int oflag)
273255
{
274256
wchar_t fileNameW[MAX_PATH];
275257
ULONG len;
@@ -284,25 +266,7 @@ HANDLE openat(HANDLE dfd, const char* fileName, bool directory)
284266
fileNameW[i] = L'\\';
285267
}
286268

287-
return openatW(dfd, fileNameW, (uint16_t)(len - 1), directory);
288-
}
289-
290-
bool ffAppendFileBufferRelative(HANDLE dfd, const char* fileName, FFstrbuf* buffer)
291-
{
292-
HANDLE FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, false);
293-
if(fd == INVALID_HANDLE_VALUE)
294-
return false;
295-
296-
return ffAppendFDBuffer(fd, buffer);
297-
}
298-
299-
ssize_t ffReadFileDataRelative(HANDLE dfd, const char* fileName, size_t dataSize, void* data)
300-
{
301-
HANDLE FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, false);
302-
if(fd == INVALID_HANDLE_VALUE)
303-
return -1;
304-
305-
return ffReadFDData(fd, dataSize, data);
269+
return openatW(dfd, fileNameW, (uint16_t)(len - 1), !!(oflag & O_DIRECTORY));
306270
}
307271

308272
bool ffPathExpandEnv(const char* in, FFstrbuf* out)

src/common/io.h

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,59 @@
1616
#include <sys/stat.h>
1717
#include <errno.h>
1818
#include <limits.h>
19+
#include <fcntl.h>
1920
typedef int FFNativeFD;
2021
#define FF_INVALID_FD (-1)
2122
// procfs's file can be changed between read calls such as /proc/meminfo and /proc/uptime.
2223
// one safe way to read correct data is reading the whole file in a single read syscall
2324
#define PROC_FILE_BUFFSIZ (32 * 1024)
2425
#endif
2526

27+
#ifdef _WIN32
28+
#ifndef O_CLOEXEC
29+
#define O_CLOEXEC 0
30+
#endif
31+
#ifndef O_RDONLY
32+
#define O_RDONLY 0
33+
#endif
34+
#ifndef O_DIRECTORY
35+
#define O_DIRECTORY 0200000
36+
#endif
37+
38+
// Only O_RDONLY is supported
39+
HANDLE openat(HANDLE dfd, const char* fileName, int oflag);
40+
HANDLE openatW(HANDLE dfd, const wchar_t* fileName, uint16_t fileNameLen, bool directory);
41+
#endif
42+
43+
44+
static inline bool ffIsValidNativeFD(FFNativeFD fd)
45+
{
46+
#ifndef _WIN32
47+
return fd >= 0;
48+
#else
49+
// https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
50+
return fd != INVALID_HANDLE_VALUE && fd != NULL;
51+
#endif
52+
}
53+
54+
FF_C_NONNULL(1)
55+
static inline bool wrapClose(FFNativeFD* pfd)
56+
{
57+
assert(pfd);
58+
59+
if (!ffIsValidNativeFD(*pfd))
60+
return false;
61+
62+
#ifndef _WIN32
63+
close(*pfd);
64+
#else
65+
NtClose(*pfd);
66+
#endif
67+
68+
return true;
69+
}
70+
#define FF_AUTO_CLOSE_FD __attribute__((__cleanup__(wrapClose)))
71+
2672
static inline FFNativeFD FFUnixFD2NativeFD(int unixfd)
2773
{
2874
#ifndef _WIN32
@@ -72,17 +118,60 @@ static inline ssize_t ffReadFDData(FFNativeFD fd, size_t dataSize, void* data)
72118
#endif
73119
}
74120

121+
FF_C_NONNULL(2)
122+
bool ffAppendFDBuffer(FFNativeFD fd, FFstrbuf* buffer);
123+
75124
FF_C_NONNULL(1, 3)
76-
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data);
125+
static inline ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
126+
{
127+
FFNativeFD FF_AUTO_CLOSE_FD fd =
128+
#ifndef _WIN32
129+
open(fileName, O_RDONLY | O_CLOEXEC);
130+
#else
131+
CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
132+
#endif
133+
134+
if (!ffIsValidNativeFD(fd))
135+
return -1;
136+
137+
return ffReadFDData(fd, dataSize, data);
138+
}
139+
77140
FF_C_NONNULL(2, 4)
78-
ssize_t ffReadFileDataRelative(FFNativeFD dfd, const char* fileName, size_t dataSize, void* data);
141+
static inline ssize_t ffReadFileDataRelative(FFNativeFD dfd, const char* fileName, size_t dataSize, void* data)
142+
{
143+
FFNativeFD FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
144+
if (!ffIsValidNativeFD(fd))
145+
return -1;
146+
147+
return ffReadFDData(fd, dataSize, data);
148+
}
79149

80-
FF_C_NONNULL(2)
81-
bool ffAppendFDBuffer(FFNativeFD fd, FFstrbuf* buffer);
82150
FF_C_NONNULL(1, 2)
83-
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer);
151+
static inline bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
152+
{
153+
FFNativeFD FF_AUTO_CLOSE_FD fd =
154+
#ifndef _WIN32
155+
open(fileName, O_RDONLY | O_CLOEXEC);
156+
#else
157+
CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
158+
#endif
159+
160+
if (!ffIsValidNativeFD(fd))
161+
return false;
162+
163+
return ffAppendFDBuffer(fd, buffer);
164+
}
165+
84166
FF_C_NONNULL(2, 3)
85-
bool ffAppendFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer);
167+
static inline bool ffAppendFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer)
168+
{
169+
FFNativeFD FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
170+
if (!ffIsValidNativeFD(fd))
171+
return false;
172+
173+
return ffAppendFDBuffer(fd, buffer);
174+
}
86175

87176
FF_C_NONNULL(2)
88177
static inline bool ffReadFDBuffer(FFNativeFD fd, FFstrbuf* buffer)
@@ -183,34 +272,6 @@ static inline void ffUnsuppressIO(bool* suppressed)
183272

184273
void ffListFilesRecursively(const char* path, bool pretty);
185274

186-
static inline bool ffIsValidNativeFD(FFNativeFD fd)
187-
{
188-
#ifndef _WIN32
189-
return fd >= 0;
190-
#else
191-
// https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
192-
return fd != INVALID_HANDLE_VALUE && fd != NULL;
193-
#endif
194-
}
195-
196-
FF_C_NONNULL(1)
197-
static inline bool wrapClose(FFNativeFD* pfd)
198-
{
199-
assert(pfd);
200-
201-
if (!ffIsValidNativeFD(*pfd))
202-
return false;
203-
204-
#ifndef _WIN32
205-
close(*pfd);
206-
#else
207-
NtClose(*pfd);
208-
#endif
209-
210-
return true;
211-
}
212-
#define FF_AUTO_CLOSE_FD __attribute__((__cleanup__(wrapClose)))
213-
214275
FF_C_NONNULL(1)
215276
static inline bool wrapFclose(FILE** pfile)
216277
{
@@ -262,9 +323,3 @@ static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char*
262323

263324
FFNativeFD ffGetNullFD(void);
264325
bool ffRemoveFile(const char* fileName);
265-
266-
#ifdef _WIN32
267-
// Only O_RDONLY is supported
268-
HANDLE openat(HANDLE dfd, const char* fileName, bool directory);
269-
HANDLE openatW(HANDLE dfd, const wchar_t* fileName, uint16_t fileNameLen, bool directory);
270-
#endif

0 commit comments

Comments
 (0)