-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.h
730 lines (667 loc) · 17.2 KB
/
fs.h
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#ifndef LIBFS__h
#define LIBFS__h
#ifdef __cplusplus
extern "C"
{
#endif
/** Major version of libfs. */
#define LIBFS_VERSION_MAJOR 0
/** Minor version of libfs. */
#define LIBFS_VERSION_MINOR 2
/** Patch version of libfs. */
#define LIBFS_VERSION_PATCH 3
/* Define to 1 if you have the <dirent.h> header file. */
#ifndef HAVE_DIRENT_H
#define HAVE_DIRENT_H 1
#endif
/* Define to 1 if you have the <stddef.h> header file. */
#ifndef HAVE_STDDEF_H
#define HAVE_STDDEF_H 1
#endif
/* Define to 1 if you have the <stdio.h> header file. */
#ifndef HAVE_STDIO_H
#define HAVE_STDIO_H 1
#endif
/* Define to 1 if you have the <stdlib.h> header file. */
#ifndef HAVE_STDLIB_H
#define HAVE_STDLIB_H 1
#endif
/* Define to 1 if you have the <sys/types.h> header file. */
#ifndef HAVE_SYS_TYPES_H
#define HAVE_SYS_TYPES_H 1
#endif
/* Define to 1 if you have the <sys/stat.h> header file. */
#ifndef HAVE_SYS_STAT_H
#define HAVE_SYS_STAT_H 1
#endif
/* Define to 1 if you have the <sys/sendfile.h> header file. */
#ifndef HAVE_SYS_SENDFILE_H
#define HAVE_SYS_SENDFILE_H 1
#endif
/* Define to 1 if you have the <string.h> header file. */
#ifndef HAVE_STRING_H
#define HAVE_STRING_H 1
#endif
/* Define to 1 if you have the <unistd.h> header file. */
#ifndef HAVE_UNISTD_H
#define HAVE_UNISTD_H 1
#endif
/* Define to 1 if you have the <windows.h> header file. */
#ifndef HAVE_WINDOWS_H
/* #undef HAVE_WINDOWS_H */
#endif
/* Define to 1 if you have the `free' function. */
#ifndef HAVE_FREE
#define HAVE_FREE 1
#endif
/* Define to 1 if you have the `malloc' function. */
#ifndef HAVE_MALLOC
#define HAVE_MALLOC 1
#endif
/* Define to 1 if you have the `memset' function. */
#ifndef HAVE_MEMSET
#define HAVE_MEMSET 1
#endif
/* Define to 1 if you have the `memcpy' function. */
#ifndef HAVE_MEMCPY
#define HAVE_MEMCPY 1
#endif
/* Define to 1 if you have the `_snprintf_s' function. */
#ifndef HAVE__SNPRINTF_S
/* #undef HAVE__SNPRINTF_S */
#endif
/* Define to 1 if you have the `_snprintf' function. */
#ifndef HAVE__SNPRINTF
/* #undef HAVE__SNPRINTF */
#endif
/* Define to 1 if you have the `snprintf' function. */
#ifndef HAVE_SNPRINTF
#define HAVE_SNPRINTF 1
#endif
/* Define to 1 if you have the `vsnprintf' function. */
#ifndef HAVE_VSNPRINTF
#define HAVE_VSNPRINTF 1
#endif
/* Define to 1 if you build with Doxygen. */
#ifndef LIBFS_DOXYGEN
/* #undef LIBFS_DOXYGEN */
#endif
#ifdef HAVE_STDDEF_H
/* Required for size_t */
#include <stddef.h>
#endif
#ifndef LIBFS_MALLOC
#ifdef HAVE_MALLOC
/**
* Defines the malloc function used by libfs at compile time.
*
* @code
* void* my_malloc(size_t size)
* {
* // do something
* }
*
* #define LIBFS_MALLOC my_malloc
* @endcode
*/
#define LIBFS_MALLOC malloc
#else
#define LIBFS_MALLOC(size) NULL
#endif
#endif
#ifndef LIBFS_FREE
#ifdef HAVE_FREE
/**
* Defines the free function used by libfs at compile time.
*
* @code
* void my_free(void* ptr)
* {
* // do something
* }
*
* #define LIBFS_FREE my_free
* @endcode
*/
#define LIBFS_FREE free
#else
#define LIBFS_FREE(ptr)
#endif
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
#define LIBFS_CDECL __cdecl
#define LIBFS_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(LIBFS_HIDE_SYMBOLS) && !defined(LIBFS_IMPORT_SYMBOLS) && !defined(LIBFS_EXPORT_SYMBOLS)
#define LIBFS_EXPORT_SYMBOLS
#endif
#if defined(LIBFS_HIDE_SYMBOLS)
#define LIBFS_PUBLIC(type) type LIBFS_STDCALL
#elif defined(LIBFS_EXPORT_SYMBOLS)
#define LIBFS_PUBLIC(type) __declspec(dllexport) type LIBFS_STDCALL
#elif defined(LIBFS_IMPORT_SYMBOLS)
#define LIBFS_PUBLIC(type) __declspec(dllimport) type LIBFS_STDCALL
#endif
#else /* !__WINDOWS__ */
#define LIBFS_CDECL
#define LIBFS_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define LIBFS_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define LIBFS_PUBLIC(type) type
#endif
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
/** Struct for custom hooks configuration. */
struct fs_hooks
{
/** Custom malloc function. */
void *(LIBFS_CDECL *malloc_fn)(size_t size);
/** Custom free function. */
void(LIBFS_CDECL *free_fn)(void *ptr);
};
/**
* Register custom hooks.
*
* @code{.c}
* struct fs_hooks hooks = { malloc, free };
* fs_init_hooks(&hooks);
* @endcode
*
* @param[in] hooks Hooks configuration
*/
LIBFS_PUBLIC(void)
fs_init_hooks(struct fs_hooks *hooks);
/**
* Composes an absolute path.
*
* @code{.c}
* char buf[MAX_PATH];
* if (!fs_absolute("./relative", buf, MAX_PATH))
* {
* print("fs_absolute failed");
* }
* else
* {
* printf("%s", buf);
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @param[out] buf Buffer for storing the result path
* @param[in] size Buffer size
* @return A pointer to buf if there is no error, NULL otherwise.
*/
LIBFS_PUBLIC(char *)
fs_absolute(const char *path, char *buf, size_t size);
/**
* Gets a pointer to the rightmost path separator.
*
* @code{.c}
* const char* c = fs_rsplit("./path/to/foo.txt");
* @endcode
*
* @param[in] path Some null-terminated path
* @return A pointer to the rightmost separator.
*/
LIBFS_PUBLIC(const char*)
fs_rsplit(const char* path);
/**
* Gets the directory name of path.
*
* @code{.c}
* char dirname[256];
* fs_dirname("./foo.txt", dirname, 256);
* @endcode
*
* @param[in] path Some null-terminated path
* @param[out] buf Buffer for storing the result path
* @param[in] size Buffer size
* @return The number of bytes that would have been written if
* buf was large enough (excluding the null-terminating character).
*/
LIBFS_PUBLIC(size_t)
fs_dirname(const char* path, char *buf, size_t size);
/**
* Gets the base name of path.
*
* @code{.c}
* const char* basename = fs_basename("./foo.txt");
* @endcode
*
* @param[in] path Some null-terminated path
* @return A pointer to the base name.
*/
LIBFS_PUBLIC(const char*)
fs_basename(const char* path);
/**
* Copies files or directories.
*
* @code{.c}
* fs_copy("foo.txt", "bar.txt");
* @endcode
*
* @param[in] from Some null-terminated path to the source file, directory, or symlink
* @param[in] to Some null-terminated path to the destination file, directory, or symlink
*/
LIBFS_PUBLIC(void)
fs_copy(const char *from, const char *to);
/**
* Copies file contents.
*
* @code{.c}
* fs_copy_file("foo.txt", "bar.txt");
* @endcode
*
* @param[in] from Some null-terminated path to the source file
* @param[in] to Some null-terminated path to the destination file
*/
LIBFS_PUBLIC(void)
fs_copy_file(const char *from, const char *to);
/**
* Get the current working directory.
*
* @code{.c}
* char buf[MAX_PATH];
* if (!fs_current_dir(buf, MAX_PATH))
* {
* print("fs_current_dir failed");
* }
* else
* {
* printf("%s", buf);
* }
* @endcode
*
* @param[out] buf Buffer for storing the result path
* @param[in] size Buffer size
* @return A pointer to buf if there is no error, NULL otherwise.
*/
LIBFS_PUBLIC(char *)
fs_current_dir(char *buf, size_t size);
/**
* Concatenates two paths together with the platform specific separator.
*
* @code{.c}
* char cwd[MAX_PATH];
* if (!fs_current_dir(cwd, MAX_PATH))
* {
* print("fs_current_dir failed");
* return;
* }
*
* char buf[MAX_PATH];
* fs_join_path(buf, MAX_PATH, cwd, "foo.txt");
* printf("%s", buf);
* @endcode
*
* @param[out] buf Buffer for storing the result path
* @param[in] size Buffer size
* @param[in] left Left part null-terminated path
* @param[in] right Right part null-terminated path
* @return The number of bytes written to buf.
*/
LIBFS_PUBLIC(size_t)
fs_join_path(char *buf, size_t size, const char *left, const char *right);
/**
* Checks if a path corresponds to an existing file or directory.
*
* @code{.c}
* if (!fs_exist("./foo.txt"))
* {
* print("foo.txt not found");
* }
* else
* {
* printf("foo.txt found");
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @return If the file or directory exists.
*/
LIBFS_PUBLIC(int)
fs_exist(const char *path);
/**
* Gets the size of an existing file.
*
* @code{.c}
* off_t size = fs_file_size("foo.txt")
* printf("file size: %d", size);
* @endcode
*
* @param[in] path Some null-terminated path
* @return The size of the file, in bytes
*/
LIBFS_PUBLIC(off_t)
fs_file_size(const char *path);
/**
* Checks if a path corresponds to a directory.
*
* @code{.c}
* if (!fs_is_directory("./somedirectory"))
* {
* print("path is not a directory");
* }
* else
* {
* print("path is a directory");
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @return If path points to an existing directory.
*/
LIBFS_PUBLIC(int)
fs_is_directory(const char *path);
/**
* Checks if a path corresponds to a file.
*
* @code{.c}
* if (!fs_is_file("./foo.txt"))
* {
* print("path is not a file");
* }
* else
* {
* print("path is a file");
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @return If path points to an existing file.
*/
LIBFS_PUBLIC(int)
fs_is_file(const char *path);
/**
* Checks if a path corresponds to a symbolic link.
*
* @code{.c}
* if (!fs_is_symlink("./somesymlink"))
* {
* print("path is not a symbolic link");
* }
* else
* {
* print("path is a symbolic link");
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @return If path points to an existing symbolic link.
*/
LIBFS_PUBLIC(int)
fs_is_symlink(const char *path);
/**
* Writes file content to buffer.
*
* @code{.c}
* void buf[1024];
* fs_read_file("foo.txt", buf, 1024);
* @endcode
*
* @param[in] path Some null-terminated path to existing file
* @param[in] buf Some memory buffer
* @param[in] size Buffer size
* @return The number of bytes that would have been readen if
* buf was large enough (excluding the null-terminating character).
*/
LIBFS_PUBLIC(size_t)
fs_read_file_buffer(const char *path, void *buf, size_t size);
/**
* Reads a whole file content.
*
* @code{.c}
* int size;
* void* buf;
* if (!(buf = fs_read_file("foo.txt", &size)))
* {
* printf("fs_read_file failed");
* }
* else
* {
* printf("file size: %d", size);
* }
* @endcode
*
* @param[in] path Some null-terminated path to existing file
* @param[out] size Number of bytes read
* @return A pointer to read bytes if there is no error, NULL otherwise.
*/
LIBFS_PUBLIC(void *)
fs_read_file(const char *path, size_t *size);
/**
* Writes content to file.
*
* @code{.c}
* const char* buf = "hello";
* if (!fs_write_file("foo.txt", buf, 5))
* {
* printf("fs_write_file failed");
* }
* @endcode
*
* @param[in] path Some null-terminated path
* @param[in] buf Some memory buffer
* @param[in] size Buffer size
* @return If the file was written.
*/
LIBFS_PUBLIC(int)
fs_write_file(const char *path, const void *buf, size_t size);
/**
* @struct fs_file_iterator
* Struct used to iterate over a file.
*
* @code{.c}
* struct fs_file_iterator* it = fs_iter_file("foo.txt");
* char c;
*
* while(fs_next_char(it, &c))
* {
* printf("%c", c);
* }
*
* fs_close_file(it);
* @endcode
*/
struct fs_file_iterator;
/**
* Opens a file to iterate over its content.
*
* @code{.c}
* struct fs_file_iterator* it = fs_iter_file("foo.txt");
*
* // iterate file
*
* fs_close_file(it);
* @endcode
*
* @param[in] path Some null-terminated path
* @return A pointer for iterating over the file if there is no error,
* NULL otherwise.
*/
LIBFS_PUBLIC(struct fs_file_iterator *)
fs_iter_file(const char *path);
/**
* Iterates over the next char of a file.
*
* @code{.c}
* char c;
* while(fs_next_char(it, &c))
* {
* printf("%c", c);
* }
* @endcode
*
* @param[in] it Some opened file iterator
* @param[out] c Character read
* @return The same it pointer or NULL if an error occurred or there
* is no more entry to iterate over.
*/
LIBFS_PUBLIC(struct fs_file_iterator *)
fs_next_char(struct fs_file_iterator *it, char *c);
/**
* Closes and frees an opened file iterator.
*
* @code{.c}
* struct fs_file_iterator* it = fs_iter_file("foo.txt");
*
* // iterate file
*
* fs_close_file(it);
* @endcode
*
* @param[in] it Some opened file iterator
*/
LIBFS_PUBLIC(void)
fs_close_file(struct fs_file_iterator *it);
/**
* Gets the absolute path to the platform specific temporary directory.
*
* @code{.c}
* char buf[MAX_PATH];
* if (!fs_temp_dir(buf, MAX_PATH))
* {
* printf("fs_temp_dir failed");
* }
* else
* {
* printf("%s", buf);
* }
* @endcode
*
* @param[in] buf Buffer for storing the result path
* @param[in] size Buffer size
* @return A pointer to buf if there is no error, NULL otherwise.
*/
LIBFS_PUBLIC(char *)
fs_temp_dir(char *buf, size_t size);
/**
* Deletes an empty directory if it exists.
*
* @code
* if (!fs_delete_dir("foo"))
* {
* printf("fs_delete_dir failed");
* }
* @endcode
* @param[in] path Some null-terminated path
* @return If the directory was deleted.
*/
LIBFS_PUBLIC(int)
fs_delete_dir(const char *path);
/**
* Deletes a file if it exists.
*
* @code
* if (!fs_delete_file("foo.txt"))
* {
* printf("fs_delete_file failed");
* }
* @endcode
* @param[in] path Some null-terminated path
* @return If the file was deleted.
*/
LIBFS_PUBLIC(int)
fs_delete_file(const char *path);
/**
* Creates a directory if it doesn't exist.
*
* The parent directory must exist as it will not
* be created recursively.
*
* @code
* if (!fs_make_dir("foo"))
* {
* printf("fs_make_dir failed");
* }
* @endcode
* @param[in] path Some null-terminated path
* @return If the directory was created.
*/
LIBFS_PUBLIC(int)
fs_make_dir(const char *path);
/**
* Struct used to iterate over a directory.
*
* @code{.c}
* struct fs_directory_iterator* it = fs_open_dir("./somedir");
*
* while(fs_read_dir(it))
* {
* printf("%s", it->path);
* }
*
* fs_close_dir(it);
* @endcode
*/
struct fs_directory_iterator
{
/** Path to file. */
const char *path;
};
/**
* Gets an iterator over entries of a directory.
*
* @code{.c}
* struct fs_directory_iterator* it = fs_open_dir("./somedir");
*
* while(fs_read_dir(it))
* {
* printf("%s", it->path);
* }
*
* fs_close_dir(it);
* @endcode
*
* @param[in] path Some null-terminated path
* @return A pointer for iterating over the directory if there is no error, NULL otherwise.
*/
LIBFS_PUBLIC(struct fs_directory_iterator *)
fs_open_dir(const char *path);
/**
* Iterates over the next entry of a directory.
*
* @code{.c}
* struct fs_directory_iterator* it = fs_open_dir("./somedir");
*
* while(fs_read_dir(it))
* {
* printf("%s", it->path);
* }
*
* fs_close_dir(it);
* @endcode
*
* @param[in] it Some opened directory iterator
* @return The same it pointer or NULL if an error occurred or there is no more entry to iterate over.
*/
LIBFS_PUBLIC(struct fs_directory_iterator *)
fs_read_dir(struct fs_directory_iterator *it);
/**
* Closes and frees an opened directory iterator.
*
* @code{.c}
* struct fs_directory_iterator* it = fs_open_dir("./somedir");
*
* while(fs_read_dir(it))
* {
* printf("%s", it->path);
* }
*
* fs_close_dir(it);
* @endcode
*
* @param[in] it Some opened directory iterator
*/
LIBFS_PUBLIC(void)
fs_close_dir(struct fs_directory_iterator *it);
#ifdef __cplusplus
}
#endif
#endif