-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsutil.h
More file actions
369 lines (324 loc) · 9.36 KB
/
fsutil.h
File metadata and controls
369 lines (324 loc) · 9.36 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
`fsutil.h` - filesystem utilities (platform: Windows, Linux)
NOTES:
- Unsafe
- Filepath must be POSIX path (including on Windows i.e. "/c/Program Files/Windows/")
- Filepath only support ASCII character
Windows Only Notes:
- Root filepath in Windows must be the drive or the partition name i.e. "/c" or "/d"
other than that it will be assumed as invalid file path.
- It doesn't handle UNC
*/
#ifndef FSUTIL_H_
#define FSUTIL_H_
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#ifndef FS_ASSERT
#include <assert.h>
#define FS_ASSERT assert
#endif
#ifndef FS_MALLOC
#include <stdlib.h>
#define FS_MALLOC malloc
#define FS_FREE free
#endif
/////////////////////////////////////////
///
/// Utilties
///
/// This is temporary allocator that usually a function needs
typedef struct FsTemp {
uint8_t *buffer;
uint32_t reserved;
uint32_t capacity;
} FsTemp;
void *fs_temp_alloc(FsTemp *tmp, size_t size);
void fs_temp_reset(FsTemp *tmp);
void *fs_temp_strndup(FsTemp *tmp, const char *cstr, size_t size);
void *fs_temp_strdup(FsTemp *tmp, const char *cstr);
typedef enum {
FS_OK = 0,
FS_RESULT_ALREADY_EXISTS,
FS_RESULT_MKDIR_FAILED,
FS_RESULT_MALLOC_FAILED,
FS_RESULT_TEMP_ALLOC_FAILED,
FS_RESULT_NOT_A_DIR,
FS_RESULT_NOT_A_FILE,
FS_RESULT_COULDNT_OPEN_DIR_ENTRY,
FS_RESULT_COULDNT_READ_NEXT_DIR_ENTRY,
_COUNT_FS_RESULTS,
} FsResult;
const char *fs_result_to_cstr(FsResult result);
/////////////////////////////////////////
///
/// File System API
///
typedef enum {
FS_FILE_NOT_EXIST = 0,
FS_FILE_REGULER,
FS_FILE_DIRECTORY,
FS_FILE_SYMLINK,
FS_FILE_OTHER,
} FsFileType;
FsResult fs_mkdir(const char *path);
FsResult fs_rmdir(const char *path);
FsResult fs_delete_file(const char *path);
FsFileType fs_get_file_type(const char *path);
const char *fs_path_win32_to_posix(FsTemp *temp, const char *path);
const char *fs_path_posix_to_win32(FsTemp *temp, const char *path);
const char *fs_get_cwd(FsTemp *temp);
const char *fs_get_home_dir(FsTemp *temp);
// This doesn't support the case where b = "./test" or b ="../test"
const char *fs_path_join(FsTemp *temp, const char *a, const char *b);
const char *fs_getext(FsTemp *temp, const char *path);
const char *fs_dirname(FsTemp *temp, const char *path);
typedef struct FsDirEntry {
char *name;
void *private_fields;
FsResult result;
} FsDirEntry;
FsResult fs_dir_entry_open(const char *dir, FsDirEntry *entry);
bool fs_dir_entry_next(FsDirEntry *entry);
void fs_dir_entry_close(FsDirEntry *dir);
#endif // FSUTIL_H_
#ifdef FSUTIL_IMPLEMENTATION
#undef FSUTIL_IMPLEMENTATION
const char *fs_result_to_cstr(FsResult result)
{
static const char *result_to_cstr[_COUNT_FS_RESULTS] = {
[FS_OK] = "ok",
[FS_RESULT_ALREADY_EXISTS] = "already exists",
[FS_RESULT_MKDIR_FAILED] = "failed to create directory",
[FS_RESULT_MALLOC_FAILED] = "failed to allocate memory",
[FS_RESULT_NOT_A_DIR] = "not a directory",
[FS_RESULT_NOT_A_FILE] = "not a regular file",
[FS_RESULT_COULDNT_OPEN_DIR_ENTRY] = "couldn't open dir entry",
[FS_RESULT_COULDNT_READ_NEXT_DIR_ENTRY] = "couldn't read next dir entry",
};
if(result >= 0 && result < _COUNT_FS_RESULTS)
return result_to_cstr[result];
else
return NULL;
}
void *fs_temp_alloc(FsTemp *tmp, size_t size)
{
if(!tmp) return NULL;
if(!tmp->buffer) return NULL;
if(tmp->reserved + size < tmp->capacity) {
void *buf = &tmp->buffer[tmp->reserved];
tmp->reserved += size;
return buf;
}
return NULL;
}
void *fs_temp_strdup(FsTemp *temp, const char *cstr)
{
size_t n = 0;
for(; cstr[n] != 0; ++n);
char *result = fs_temp_alloc(temp, n + 1);
size_t i = 0;
for(; i < n; ++i)
result[i] = cstr[i];
result[i++] = 0;
return result;
}
void *fs_temp_strndup(FsTemp *tmp, const char *cstr, size_t size)
{
size_t n = 0;
for(; n < size && cstr[n] != 0; ++n);
if(n < size) n = size;
char *result = fs_temp_alloc(tmp, n + 1);
size_t i = 0;
for(; i < n; ++i)
result[i] = cstr[i];
result[i++] = 0;
return result;
}
void fs_temp_reset(FsTemp *tmp)
{
tmp->reserved = 0;
}
const char *fs_path_posix_to_win32(FsTemp *temp, const char *path)
{
size_t len = strlen(path);
size_t cap = len + 1;
char *result = fs_temp_alloc(temp, cap);
if(!result) return NULL;
for(size_t i = 0; i < len; ++i) {
if(path[i] == '/') {
result[i] = '\\';
} else {
result[i] = path[i];
}
}
result[cap-1] = 0;
return result;
}
const char *fs_path_win32_to_posix(FsTemp *temp, const char *path)
{
size_t len = strlen(path);
size_t cap = len + 1;
char *result = fs_temp_alloc(temp, cap);
if(!result) return NULL;
for(size_t i = 0; i < len; ++i) {
if(path[i] == '\\') {
result[i] = '/';
} else {
result[i] = path[i];
}
}
result[cap-1] = 0;
return result;
}
#define FS_PATH_SEP '/'
const char *fs_path_join(FsTemp *temp, const char *path_a, const char *path_b)
{
FS_ASSERT(path_a != NULL);
FS_ASSERT(path_b != NULL);
size_t len_a = strlen(path_a);
size_t len_b = strlen(path_b);
int need_separator = path_a[len_a - 1] != FS_PATH_SEP;
size_t total_length = len_a + len_b + (need_separator ? 1 : 0) + 1;
char *result = fs_temp_alloc(temp, total_length);
if(!result) return NULL;
size_t i;
for(i = 0; i < len_a; ++i) result[i] = path_a[i];
if(need_separator) result[i++] = FS_PATH_SEP;
for(size_t j = 0; j < len_b; ++j) result[i + j] = path_b[j];
result[total_length-1] = 0;
return result;
}
#include <stdlib.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
typedef struct FsDirEntryWin32PrivateFields {
WIN32_FIND_DATA win32_data;
HANDLE win32_hFind;
bool win32_init;
} FsDirEntryWin32PrivateFields;
#else
// #include <sys/stat.h>
int mkdir(const char *path, unsigned int stuff);
#endif
const char *fs_get_cwd(FsTemp *temp)
{
#ifdef _WIN32
DWORD cap = GetCurrentDirectory(0, NULL);
char *data = fs_temp_alloc(temp, cap + 1);
if(!data) return NULL;
DWORD length = GetCurrentDirectory(cap, data);
data[cap] = 0;
return fs_path_win32_to_posix(temp, data);
#endif
}
const char *fs_get_home_dir(FsTemp *temp)
{
#ifdef _WIN32
const char *home_path = getenv("HOMEPATH");
return fs_path_win32_to_posix(temp, home_path);
#else
#error "Not implemented for this platform"
#endif
}
FsResult fs_mkdir(const char *path)
{
#ifdef _WIN32
int result = _mkdir(path);
#else
int result = mkdir(path, 0755);
#endif
if(result < 0) {
if(errno == EEXIST) return FS_RESULT_ALREADY_EXISTS;
return FS_RESULT_MKDIR_FAILED;
}
return FS_OK;
}
FsFileType fs_get_file_type(const char *path)
{
#ifdef _WIN32
DWORD attr = GetFileAttributesA(path);
if(attr == INVALID_FILE_ATTRIBUTES) {
return FS_FILE_NOT_EXIST;
}
if((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
return FS_FILE_DIRECTORY;
}
return FS_FILE_REGULER;
#else
#error "Not implemented for this platform"
#endif
}
FsResult fs_dir_entry_open(const char *path, FsDirEntry *dir)
{
memset(dir, 0, sizeof(*dir));
#ifdef _WIN32
uint8_t buf[1024];
FsTemp tmp = {0};
tmp.buffer = buf;
tmp.capacity = sizeof(buf);
const char *win32_path = fs_path_posix_to_win32(&tmp, path);
if(!win32_path) return FS_RESULT_TEMP_ALLOC_FAILED;
int length = strlen(win32_path);
char *win32_path_ast = fs_temp_strndup(&tmp, win32_path, length + 3);
if(!win32_path_ast) return FS_RESULT_TEMP_ALLOC_FAILED;
if(win32_path[length - 1] != '\\') {
win32_path_ast[length++] = '\\';
}
win32_path_ast[length++] = '*';
win32_path_ast[length++] = 0;
WIN32_FIND_DATA find_data = {0};
HANDLE hFind = FindFirstFile(win32_path_ast, &find_data);
if (hFind == INVALID_HANDLE_VALUE) {
return FS_RESULT_COULDNT_OPEN_DIR_ENTRY;
}
FsDirEntryWin32PrivateFields *private_fields = FS_MALLOC(sizeof(*private_fields));
if(private_fields == NULL) {
FindClose(hFind);
return FS_RESULT_MALLOC_FAILED;
}
private_fields->win32_hFind = hFind;
private_fields->win32_data = find_data;
private_fields->win32_init = false;
dir->private_fields = private_fields;
#else
#error "Not implemented for this platform"
#endif
return FS_OK;
}
bool fs_dir_entry_next(FsDirEntry *dir)
{
#ifdef _WIN32
FS_ASSERT(dir);
FS_ASSERT(dir->private_fields);
FsDirEntryWin32PrivateFields *private = dir->private_fields;
if (!private->win32_init) {
private->win32_init = true;
dir->name = private->win32_data.cFileName;
return true;
}
if (!FindNextFile(private->win32_hFind, &private->win32_data)) {
if (GetLastError() == ERROR_NO_MORE_FILES) return false;
dir->result = FS_RESULT_COULDNT_READ_NEXT_DIR_ENTRY;
return false;
}
dir->name = private->win32_data.cFileName;
return true;
#else
#error "Not implemented for this platform"
#endif
}
void fs_dir_entry_close(FsDirEntry *dir)
{
#ifdef _WIN32
FsDirEntryWin32PrivateFields *private = dir->private_fields;
FindClose(private->win32_hFind);
FS_FREE(private);
#else
#error "Not implemented for this platform"
#endif
}
#endif // FSUTIL_IMPLEMENTATION