summaryrefslogtreecommitdiffstats
path: root/src/common/io.h
blob: f7aec0b42feb39e0b158eb94f209e8c978a4b5e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#pragma once

#include "common/FFstrbuf.h"
#include "common/FFlist.h"

#ifdef _WIN32
    #include <fileapi.h>
    #include <handleapi.h>
    #include <io.h>
    #include "common/windows/nt.h"
typedef HANDLE FFNativeFD;
    #define FF_INVALID_FD INVALID_HANDLE_VALUE
#else
    #include <unistd.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <errno.h>
    #include <limits.h>
    #include <fcntl.h>
typedef int FFNativeFD;
    #define FF_INVALID_FD (-1)
    // 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
    #define PROC_FILE_BUFFSIZ (32 * 1024)
#endif

#ifdef _WIN32
    #ifndef O_CLOEXEC
        #define O_CLOEXEC 0
    #endif
    #ifndef O_RDONLY
        #define O_RDONLY 0
    #endif
    #ifndef O_DIRECTORY
        #define O_DIRECTORY 0200000
    #endif

// Only O_RDONLY is supported
HANDLE openat(HANDLE dfd, const char* fileName, int oflag);
HANDLE openatW(HANDLE dfd, const wchar_t* fileName, uint16_t fileNameLen, bool directory);
#endif

static inline bool ffIsValidNativeFD(FFNativeFD fd) {
#ifndef _WIN32
    return fd >= 0;
#else
    // https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
    return fd != INVALID_HANDLE_VALUE && fd != NULL;
#endif
}

FF_A_NONNULL(1) static inline bool wrapClose(FFNativeFD* pfd) {
    assert(pfd);

    if (!ffIsValidNativeFD(*pfd)) {
        return false;
    }

#ifndef _WIN32
    close(*pfd);
#else
    NtClose(*pfd);
#endif

    return true;
}
#define FF_AUTO_CLOSE_FD FF_A_CLEANUP(wrapClose)

static inline FFNativeFD FFUnixFD2NativeFD(int unixfd) {
#ifndef _WIN32
    return unixfd;
#else
    return (FFNativeFD) _get_osfhandle(unixfd);
#endif
}

FF_A_NONNULL(3) static inline bool ffWriteFDData(FFNativeFD fd, size_t dataSize, const void* data) {
#ifndef _WIN32
    return write(fd, data, dataSize) != -1;
#else
    DWORD written;
    return WriteFile(fd, data, (DWORD) dataSize, &written, NULL) && written == dataSize;
#endif
}

FF_A_NONNULL(2) static inline bool ffWriteFDBuffer(FFNativeFD fd, const FFstrbuf* content) {
    return ffWriteFDData(fd, content->length, content->chars);
}

FF_A_NONNULL(1, 3) bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data);

FF_A_NONNULL(1, 2) static inline bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer) {
    return ffWriteFileData(fileName, buffer->length, buffer->chars);
}

FF_A_NONNULL(3) static inline ssize_t ffReadFDData(FFNativeFD fd, size_t dataSize, void* data) {
#ifndef _WIN32
    return read(fd, data, dataSize);
#else
    DWORD bytesRead;
    if (!ReadFile(fd, data, (DWORD) dataSize, &bytesRead, NULL)) {
        return -1;
    }

    return (ssize_t) bytesRead;
#endif
}

FF_A_NONNULL(2) bool ffAppendFDBuffer(FFNativeFD fd, FFstrbuf* buffer);

FF_A_NONNULL(1, 3) static inline ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data) {
    FFNativeFD FF_AUTO_CLOSE_FD fd =
#ifndef _WIN32
        open(fileName, O_RDONLY | O_CLOEXEC);
#else
        CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif

    if (!ffIsValidNativeFD(fd)) {
        return -1;
    }

    return ffReadFDData(fd, dataSize, data);
}

FF_A_NONNULL(2, 4) static inline ssize_t ffReadFileDataRelative(FFNativeFD dfd, const char* fileName, size_t dataSize, void* data) {
    FFNativeFD FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
    if (!ffIsValidNativeFD(fd)) {
        return -1;
    }

    return ffReadFDData(fd, dataSize, data);
}

FF_A_NONNULL(1, 2) static inline bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer) {
    FFNativeFD FF_AUTO_CLOSE_FD fd =
#ifndef _WIN32
        open(fileName, O_RDONLY | O_CLOEXEC);
#else
        CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif

    if (!ffIsValidNativeFD(fd)) {
        return false;
    }

    return ffAppendFDBuffer(fd, buffer);
}

FF_A_NONNULL(2, 3) static inline bool ffAppendFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer) {
    FFNativeFD FF_AUTO_CLOSE_FD fd = openat(dfd, fileName, O_RDONLY | O_CLOEXEC);
    if (!ffIsValidNativeFD(fd)) {
        return false;
    }

    return ffAppendFDBuffer(fd, buffer);
}

FF_A_NONNULL(2) static inline bool ffReadFDBuffer(FFNativeFD fd, FFstrbuf* buffer) {
    ffStrbufClear(buffer);
    return ffAppendFDBuffer(fd, buffer);
}

FF_A_NONNULL(1, 2) static inline bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer) {
    ffStrbufClear(buffer);
    return ffAppendFileBuffer(fileName, buffer);
}

FF_A_NONNULL(2, 3) static inline bool ffReadFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer) {
    ffStrbufClear(buffer);
    return ffAppendFileBufferRelative(dfd, fileName, buffer);
}

typedef enum FF_A_PACKED FFPathType {
    FF_PATHTYPE_FILE = 1 << 0,
    FF_PATHTYPE_DIRECTORY = 1 << 1,
    FF_PATHTYPE_ANY = FF_PATHTYPE_FILE | FF_PATHTYPE_DIRECTORY,
    FF_PATHTYPE_FORCE_UNSIGNED = UINT8_MAX,
} FFPathType;

FF_A_NONNULL(1) static inline bool ffPathExists(const char* path, FFPathType pathType) {
#ifdef _WIN32

    wchar_t wPath[MAX_PATH];
    if (!NT_SUCCESS(RtlUTF8ToUnicodeN(wPath, (ULONG) sizeof(wPath), NULL, path, (ULONG) strlen(path) + 1))) {
        return false;
    }

    DWORD attr = GetFileAttributesW(wPath);

    if (attr == INVALID_FILE_ATTRIBUTES) {
        return false;
    }

    if (pathType & FF_PATHTYPE_FILE && !(attr & FILE_ATTRIBUTE_DIRECTORY)) {
        return true;
    }

    if (pathType & FF_PATHTYPE_DIRECTORY && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
        return true;
    }

#else

    if (pathType == FF_PATHTYPE_ANY) {
        // Zero overhead
        return access(path, F_OK) == 0;
    } else {
        struct stat fileStat;
        if (stat(path, &fileStat) != 0) {
            return false;
        }

        unsigned int mode = fileStat.st_mode & S_IFMT;

        if (pathType & FF_PATHTYPE_FILE && mode != S_IFDIR) {
            return true;
        }

        if (pathType & FF_PATHTYPE_DIRECTORY && mode == S_IFDIR) {
            return true;
        }
    }

#endif

    return false;
}

FF_A_NONNULL(1, 2) bool ffPathExpandEnv(const char* in, FFstrbuf* out);

#define FF_IO_TERM_RESP_WAIT_MS 100 // #554

FF_A_SCANF(3, 4)
FF_A_NONNULL(1, 3)
const char* ffGetTerminalResponse(const char* request, int nParams, const char* format, ...);

// Not thread safe!
bool ffSuppressIO(bool suppress);

static inline void ffUnsuppressIO(bool* suppressed) {
    if (!*suppressed) {
        return;
    }
    ffSuppressIO(false);
    *suppressed = false;
}

#define FF_SUPPRESS_IO() bool FF_A_CLEANUP(ffUnsuppressIO) FF_A_UNUSED io_suppressed__ = ffSuppressIO(true)

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

FF_A_NONNULL(1) static inline bool wrapFclose(FILE** pfile) {
    assert(pfile);
    if (!*pfile) {
        return false;
    }
    fclose(*pfile);
    return true;
}
#define FF_AUTO_CLOSE_FILE FF_A_CLEANUP(wrapFclose)

FF_A_NONNULL(1)
#ifndef _WIN32
static inline bool wrapClosedir(DIR** pdir) {
    assert(pdir);
    if (!*pdir) {
        return false;
    }
    closedir(*pdir);
    return true;
}
#else
static inline bool wrapClosedir(HANDLE* pdir) {
    assert(pdir);
    if (!*pdir) {
        return false;
    }
    FindClose(*pdir);
    return true;
}
#endif
#define FF_AUTO_CLOSE_DIR FF_A_CLEANUP(wrapClosedir)

FF_A_NONNULL(1, 2, 3) static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char* fileSubpath, FFstrbuf* result) {
    // configDirs is a list of FFstrbufs include the trailing slash
    FF_LIST_FOR_EACH (FFstrbuf, dir, *configDirs) {
        ffStrbufClear(result);
        ffStrbufAppend(result, dir);
        ffStrbufAppendS(result, fileSubpath);
        if (ffPathExists(result->chars, FF_PATHTYPE_FILE)) {
            return true;
        }
    }

    return false;
}

FFNativeFD ffGetNullFD(void);
bool ffRemoveFile(const char* fileName);