Debian Codegraph @

external/fs/fs.h

raw symbols SCIP
1 /*
2 File system library. Choice of public domain or MIT-0. See license statements at the end of this file.
3 fs - v1.0.0 - Release Date TBD
4
5 David Reid - [email protected]
6
7 GitHub: https://github.com/mackron/fs
8 */
9
10 /*
11 1. Introduction
12 ===============
13 This library is used to abstract access to the regular file system and archives such as ZIP files.
14
15 1.1. Basic Usage
16 ----------------
17 The main object in the library is the `fs` object. Below is the most basic way to initialize a `fs`
18 object:
19
20 ```c
21 fs_result result;
22 fs* pFS;
23
24 result = fs_init(NULL, &pFS);
25 if (result != FS_SUCCESS) {
26 // Failed to initialize.
27 }
28 ```
29
30 The above code will initialize a `fs` object representing the system's regular file system. It uses
31 stdio under the hood. Once this is set up you can load files:
32
33 ```c
34 fs_file* pFile;
35
36 result = fs_file_open(pFS, "file.txt", FS_READ, &pFile);
37 if (result != FS_SUCCESS) {
38 // Failed to open file.
39 }
40 ```
41
42 If you don't need any of the advanced features of the library, you can just pass in NULL for the
43 `fs` object which will just use the native file system like normal:
44
45 ```c
46 fs_file_open(NULL, "file.txt", FS_READ, &pFile);
47 ```
48
49 From here on out, examples will use an `fs` object for the sake of consistency, but all basic IO
50 APIs that do not use things like mounting and archive registration will work with NULL.
51
52 To close a file, use `fs_file_close()`:
53
54 ```c
55 fs_file_close(pFile);
56 ```
57
58 Reading content from the file is very standard:
59
60 ```c
61 size_t bytesRead;
62
63 result = fs_file_read(pFile, pBuffer, bytesToRead, &bytesRead);
64 if (result != FS_SUCCESS) {
65 // Failed to read file. You can use FS_AT_END to check if reading failed due to being at EOF.
66 }
67 ```
68
69 In the code above, the number of bytes actually read is output to a variable. You can use this to
70 determine if you've reached the end of the file. You can also check if the result is FS_AT_END. You
71 can pass in null for the last parameter of `fs_file_read()` in which an error will be returned if
72 the exact number of bytes requested could not be read.
73
74 Writing works the same way as reading:
75
76 ```c
77 fs_file* pFile;
78
79 result = fs_file_open(pFS, "file.txt", FS_WRITE, &pFile);
80 if (result != FS_SUCCESS) {
81 // Failed to open file.
82 }
83
84 result = fs_file_write(pFile, pBuffer, bytesToWrite, &bytesWritten);
85 ```
86
87 Formatted writing is also supported:
88
89 ```c
90 result = fs_file_writef(pFile, "Hello %s!\n", "World");
91 if (result != FS_SUCCESS) {
92 // Failed to write file.
93 }
94
95 va_list args;
96 va_start(args, format);
97 result = fs_file_writefv(pFile, "Hello %s!\n", args);
98 va_end(args);
99 ```
100
101 The `FS_WRITE` option will default to overwrite mode. You can use `FS_TRUNCATE` if you want to
102 truncate the file instead of overwriting it.
103
104 ```c
105 fs_file_open(pFS, "file.txt", FS_WRITE | FS_TRUNCATE, &pFile);
106 ```
107
108 You can also open a file in append mode with `FS_APPEND`:
109
110 ```c
111 fs_file_open(pFS, "file.txt", FS_WRITE | FS_APPEND, &pFile);
112 ```
113
114 When using `FS_APPEND` mode, the file will always append to the end of the file and can never
115 overwrite existing content. This follows POSIX semantics. In this mode, it is not possible to
116 create sparse files.
117
118 To open a file in write mode, but fail if the file already exists, you can use `FS_EXCLUSIVE`:
119
120 ```c
121 fs_file_open(pFS, "file.txt", FS_WRITE | FS_EXCLUSIVE, &pFile);
122 ```
123
124 Files can be opened for both reading and writing by simply combining the two:
125
126 ```c
127 fs_file_open(pFS, "file.txt", FS_READ | FS_WRITE, &pFile);
128 ```
129
130 Seeking and telling is very standard as well:
131
132 ```c
133 fs_file_seek(pFile, 0, FS_SEEK_END);
134
135 fs_int64 cursorPos;
136 fs_file_tell(pFile, &cursorPos);
137 ```
138
139 When seeking, you can seek beyond the end of the file. Attempting to read from beyond the end of
140 the file will return `FS_AT_END`. Attempting to write beyond the end of the file will create a
141 hole if supported by the file system, or fill the space with data (the filled data can be left
142 undefined). When seeking from the end of the file with a negative offset, it will seek backwards
143 from the end. Seeking to before the start of the file is not allowed and will return an error.
144
145 Retrieving information about a file is done with `fs_file_get_info()`:
146
147 ```c
148 fs_file_info info;
149 fs_file_get_info(pFile, &info);
150 ```
151
152 If you want to get information about a file without opening it, you can use `fs_info()`:
153
154 ```c
155 fs_file_info info;
156 fs_info(pFS, "file.txt", FS_READ, &info); // FS_READ tells it to check read-only mounts (explained later)
157 ```
158
159 A file handle can be duplicated with `fs_file_duplicate()`:
160
161 ```c
162 fs_file* pFileDup;
163 fs_file_duplicate(pFile, &pFileDup);
164 ```
165
166 Note that this will only duplicate the file handle. It does not make a copy of the file on the file
167 system itself. The duplicated file handle will be entirely independent of the original handle,
168 including having its own separate read/write cursor position. The initial position of the cursor of
169 the new file handle is undefined and you should explicitly seek to the appropriate location.
170
171 Important: `fs_file_duplicate()` can fail or work incorrectly if you use relative paths for mounts
172 and something changes the working directory. The reason being that it reopens the file based on the
173 original path to do the duplication.
174
175 To delete a file, use `fs_remove()`:
176
177 ```c
178 fs_remove(pFS, "file.txt");
179 ```
180
181 Note that files are deleted permanently. There is no recycle bin or trash functionality.
182
183 Files can be renamed and moved with `fs_rename()`:
184
185 ```c
186 fs_rename(pFS, "file.txt", "new-file.txt");
187 ```
188
189 To create a directory, use `fs_mkdir()`:
190
191 ```c
192 fs_mkdir(pFS, "new-directory", 0);
193 ```
194
195 By default, `fs_mkdir()` will create the directory hierarchy for you. If you want to disable this
196 so it fails if the directory hierarchy doesn't exist, you can use `FS_NO_CREATE_DIRS`:
197
198 ```c
199 fs_mkdir(pFS, "new-directory", FS_NO_CREATE_DIRS);
200 ```
201
202 1.2. Archives
203 -------------
204 To enable support for archives, you need an `fs` object, and it must be initialized with a config:
205
206 ```c
207 #include "extras/backends/zip/fs_zip.h" // <-- This is where FS_ZIP is declared.
208 #include "extras/backends/pak/fs_pak.h" // <-- This is where FS_PAK is declared.
209
210 ...
211
212 fs_archive_type pArchiveTypes[] =
213 {
214 {FS_ZIP, "zip"},
215 {FS_PAK, "pak"}
216 };
217
218 fs_config fsConfig = fs_config_init_default();
219 fsConfig.pArchiveTypes = pArchiveTypes;
220 fsConfig.archiveTypeCount = sizeof(pArchiveTypes) / sizeof(pArchiveTypes[0]);
221
222 fs* pFS;
223 fs_init(&fsConfig, &pFS);
224 ```
225
226 In the code above we are registering support for ZIP archives (`FS_ZIP`) and Quake PAK archives
227 (`FS_PAK`). Whenever a file with a "zip" or "pak" extension is found, the library will be able to
228 access the archive. The library will determine whether or not a file is an archive based on its
229 extension. You can use whatever extension you would like for a backend, and you can associate
230 multiple extensions to the same backend. You can also associate different backends to the same
231 extension, in which case the library will use the first one that works. If the extension of a file
232 does not match with one of the registered archive types it'll assume it's not an archive and will
233 skip it. Below is an example of one way you can read from an archive:
234
235 ```c
236 result = fs_file_open(pFS, "archive.zip/file-inside-archive.txt", FS_READ, &pFile);
237 if (result != FS_SUCCESS) {
238 // Failed to open file.
239 }
240 ```
241
242 In the example above, we've explicitly specified the name of the archive in the file path. The
243 library also supports the ability to handle archives transparently, meaning you don't need to
244 explicitly specify the archive. The code below will also work:
245
246 ```c
247 fs_file_open(pFS, "file-inside-archive.txt", FS_READ, &pFile);
248 ```
249
250 Transparently handling archives like this has overhead because the library needs to scan the file
251 system and check every archive it finds. To avoid this, you can explicitly disable this feature:
252
253 ```c
254 fs_file_open(pFS, "archive.zip/file-inside-archive.txt", FS_READ | FS_VERBOSE, &pFile);
255 ```
256
257 In the code above, the `FS_VERBOSE` flag will require you to pass in a verbose file path, meaning
258 you need to explicitly specify the archive in the path. You can take this one step further by
259 disabling access to archives in this manner altogether via `FS_OPAQUE`:
260
261 ```c
262 result = fs_file_open(pFS, "archive.zip/file-inside-archive.txt", FS_READ | FS_OPAQUE, &pFile);
263 if (result != FS_SUCCESS) {
264 // This example will always fail.
265 }
266 ```
267
268 In the example above, opening the file will fail because `FS_OPAQUE` is telling the library to
269 treat archives as if they're totally opaque which means the files within cannot be accessed.
270
271 Up to this point the handling of archives has been done automatically via `fs_file_open()`, however
272 the library allows you to manage archives manually. To do this you just initialize a `fs` object to
273 represent the archive:
274
275 ```c
276 // Open the archive file itself first.
277 fs_file* pArchiveFile;
278
279 result = fs_file_open(pFS, "archive.zip", FS_READ, &pArchiveFile);
280 if (result != FS_SUCCESS) {
281 // Failed to open archive file.
282 }
283
284
285 // Once we have the archive file we can create the `fs` object representing the archive.
286 fs* pArchive;
287 fs_config archiveConfig;
288
289 archiveConfig = fs_config_init(FS_ZIP, NULL, fs_file_get_stream(pArchiveFile));
290
291 result = fs_init(&archiveConfig, &pArchive);
292 if (result != FS_SUCCESS) {
293 // Failed to initialize archive.
294 }
295
296 ...
297
298 // During teardown, make sure the archive `fs` object is uninitialized before the stream.
299 fs_uninit(pArchive);
300 fs_file_close(pArchiveFile);
301 ```
302
303 To initialize an `fs` object for an archive you need a stream to provide the raw archive data to
304 the backend. Conveniently, the `fs_file` object itself is a stream. In the example above we're just
305 opening a file from a different `fs` object (usually one representing the default file system) to
306 gain access to a stream. The stream does not need to be a `fs_file`. You can implement your own
307 `fs_stream` object, and a `fs_memory_stream` is included as stock with the library for when you
308 want to store the contents of an archive in-memory. Once you have the `fs` object for the archive
309 you can use it just like any other:
310
311 ```c
312 result = fs_file_open(pArchive, "file-inside-archive.txt", FS_READ, &pFile);
313 if (result != FS_SUCCESS) {
314 // Failed to open file.
315 }
316 ```
317
318 In addition to the above, you can use `fs_open_archive()` to open an archive from a file:
319
320 ```c
321 fs* pArchive;
322 fs_open_archive(pFS, "archive.zip", FS_READ, &pArchive);
323
324 ...
325
326 // When tearing down, do *not* use `fs_uninit()`. Use `fs_close_archive()` instead.
327 fs_close_archive(pArchive);
328 ```
329
330 Note that you need to use `fs_close_archive()` when opening an archive like this. The reason for
331 this is that there's some internal reference counting and memory management happening under the
332 hood. You should only call `fs_close_archive()` if `fs_open_archive()` succeeds.
333
334 When opening an archive with `fs_open_archive()`, it will inherit the archive types from the parent
335 `fs` object and will therefore support archives within archives. Use caution when doing this
336 because if both archives are compressed you will get a big performance hit. Only the inner-most
337 archive should be compressed.
338
339
340 1.3. Mounting
341 -------------
342 There is no ability to change the working directory in this library. Instead you can mount a
343 physical directory to a virtual path, similar in concept to Unix operating systems. The difference,
344 however, is that you can mount multiple directories to the same virtual path in which case a
345 prioritization system will be used (only for reading - in write mode only a single mount is used).
346 There are separate mount points for reading and writing. Below is an example of mounting for
347 reading:
348
349 ```c
350 fs_mount(pFS, "/some/actual/path", NULL, FS_READ);
351 ```
352
353 To unmount, you need to specify the actual path, not the virtual path:
354
355 ```c
356 fs_unmount(pFS, "/some/actual/path", FS_READ);
357 ```
358
359 In the example above, using `NULL` for the virtual path is equivalent to an empty path. If, for
360 example, you have a file with the path "/some/actual/path/file.txt", you can open it like the
361 following:
362
363 ```c
364 fs_file_open(pFS, "file.txt", FS_READ, &pFile);
365 ```
366
367 You don't need to specify the "/some/actual/path" part because it's handled by the mount. If you
368 specify a virtual path, you can do something like the following:
369
370 ```c
371 fs_mount(pFS, "/some/actual/path", "assets", FS_READ);
372 ```
373
374 In this case, loading files that are physically located in "/some/actual/path" would need to be
375 prefixed with "assets":
376
377 ```c
378 fs_file_open(pFS, "assets/file.txt", FS_READ, &pFile);
379 ```
380
381 You can mount multiple paths to the same virtual path in which case a prioritization system will be
382 used:
383
384 ```c
385 fs_mount(pFS, "/usr/share/mygame/gamedata/base", "gamedata", FS_READ); // Base game. Lowest priority.
386 fs_mount(pFS, "/home/user/.local/share/mygame/gamedata/mod1", "gamedata", FS_READ); // Mod #1. Middle priority.
387 fs_mount(pFS, "/home/user/.local/share/mygame/gamedata/mod2", "gamedata", FS_READ); // Mod #2. Highest priority.
388 ```
389
390 The example above shows a basic system for setting up some kind of modding support in a game. In
391 this case, attempting to load a file from the "gamedata" mount point will first check the "mod2"
392 directory, and if it cannot be opened from there, it will check "mod1", and finally it'll fall back
393 to the base game data.
394
395 Internally there are a separate set of mounts for reading and writing. To set up a mount point for
396 opening files in write mode, you need to specify the `FS_WRITE` option:
397
398 ```c
399 fs_mount(pFS, "/home/user/.config/mygame", "config", FS_WRITE);
400 fs_mount(pFS, "/home/user/.local/share/mygame/saves", "saves", FS_WRITE);
401 ```
402
403 To open a file for writing, you need only prefix the path with the mount's virtual path, exactly
404 like read mode:
405
406 ```c
407 fs_file_open(pFS, "config/game.cfg", FS_WRITE, &pFile); // Prefixed with "config", so will use the "config" mount point.
408 fs_file_open(pFS, "saves/save0.sav", FS_WRITE, &pFile); // Prefixed with "saves", so will use the "saves" mount point.
409 ```
410
411 If you want to mount a directory for reading and writing, you can use both `FS_READ` and
412 `FS_WRITE` together:
413
414 ```c
415 fs_mount(pFS, "/home/user/.config/mygame", "config", FS_READ | FS_WRITE);
416 ```
417
418 You can set up read and write mount points to the same virtual path:
419
420 ```c
421 fs_mount(pFS, "/usr/share/mygame/config", "config", FS_READ);
422 fs_mount(pFS, "/home/user/.local/share/mygame/config", "config", FS_READ | FS_WRITE);
423 ```
424
425 When opening a file for reading, it'll first try searching the second mount point, and if it's not
426 found will fall back to the first. When opening in write mode, it will only ever use the second
427 mount point as the output directory because that's the only one set up with `FS_WRITE`. With this
428 setup, the first mount point is essentially protected from modification.
429
430 When mounting a directory for writing, the library will create the directory hierarchy for you. If
431 you want to disable this functionality, you can use the `FS_NO_CREATE_DIRS` flag:
432
433 ```c
434 fs_mount(pFS, "/home/user/.config/mygame", "config", FS_WRITE | FS_NO_CREATE_DIRS);
435 ```
436
437
438 By default, you can move outside the mount point with ".." segments. If you want to disable this
439 functionality, you can use the `FS_NO_ABOVE_ROOT_NAVIGATION` flag when opening the file:
440
441 ```c
442 fs_file_open(pFS, "../file.txt", FS_READ | FS_NO_ABOVE_ROOT_NAVIGATION, &pFile);
443 ```
444
445 In addition, any mount points that start with a "/" will be considered absolute and will not allow
446 any above-root navigation:
447
448 ```c
449 fs_mount(pFS, "/usr/share/mygame/gamedata/base", "/gamedata", FS_READ);
450 ```
451
452 In the example above, the "/gamedata" mount point starts with a "/", so it will not allow any
453 above-root navigation which means you cannot navigate above "/usr/share/mygame/gamedata/base". When
454 opening a file with this kind of mount point, you would need to specify the leading slash:
455
456 ```c
457 fs_file_open(pFS, "/gamedata/file.txt", FS_READ, &pFile); // Note how the path starts with "/".
458 ```
459
460 Important: When using mount points that start with "/", if the file cannot be opened from the mount,
461 it will fall back to trying the actual absolute path. To prevent this and ensure files are only
462 loaded from the mount point, use the `FS_ONLY_MOUNTS` flag when opening files. Alternatively,
463 simply avoid using "/" prefixed mounts and instead use `FS_NO_ABOVE_ROOT_NAVIGATION` for security.
464
465
466 You can also mount an archive to a virtual path:
467
468 ```c
469 fs_mount(pFS, "/usr/share/mygame/gamedata.zip", "gamedata", FS_READ);
470 ```
471
472 In order to do this, the `fs` object must have been configured with support for the given archive
473 type. Note that writing directly into an archive is not supported by this API. To write into an
474 archive, the backend itself must support writing, and you will need to manually initialize a `fs`
475 object for the archive and write into it directly.
476
477
478 The examples above have been hard coding paths, but you can use `fs_mount_sysdir()` to mount a
479 system directory to a virtual path. This is just a convenience helper function, and you need not
480 use it if you'd rather deal with system directories yourself:
481
482 ```c
483 fs_mount_sysdir(pFS, FS_SYSDIR_CONFIG, "myapp", "/config", FS_READ | FS_WRITE);
484 ```
485
486 This function requires that you specify a sub-directory of the system directory to mount. The reason
487 for this is to encourage the application to use good practice to avoid cluttering the file system.
488
489 Use `fs_unmount_sysdir()` to unmount a system directory. When using this you must specify the
490 sub-directory you used when mounting it:
491
492 ```c
493 fs_unmount_sysdir(pFS, FS_SYSDIR_CONFIG, "myapp", FS_READ | FS_WRITE);
494 ```
495
496
497 Mounting a `fs` object to a virtual path is also supported.
498
499 ```c
500 fs* pSomeOtherFS; // <-- This would have been initialized earlier.
501
502 fs_mount_fs(pFS, pSomeOtherFS, "assets.zip", FS_READ);
503
504 ...
505
506 fs_unmount_fs(pFS, pSomeOtherFS, FS_READ);
507 ```
508
509
510 If the file cannot be opened from any mounts it will attempt to open the file from the backend's
511 default search path. Mounts always take priority. When opening in transparent mode with
512 `FS_TRANSPARENT` (default), it will first try opening the file as if it were not in an archive. If
513 that fails, it will look inside archives.
514
515 When opening a file, if you pass in NULL for the `pFS` parameter it will open the file like normal
516 using the standard file system. That is, it'll work exactly as if you were using stdio `fopen()`,
517 and you will not have access to mount points. Keep in mind that there is no notion of a "current
518 directory" in this library so you'll be stuck with the initial working directory.
519
520 You can also skip mount points when opening a file by using the `FS_IGNORE_MOUNTS` flag:
521
522 ```c
523 fs_file_open(pFS, "/absolute/path/to/file.txt", FS_READ | FS_IGNORE_MOUNTS, &pFile);
524 ```
525
526 This can be useful when you want to access a file directly without going through the mount system,
527 such as when working with temporary files.
528
529
530
531 1.4. Enumeration
532 ----------------
533 You can enumerate over the contents of a directory like the following:
534
535 ```c
536 for (fs_iterator* pIterator = fs_first(pFS, "directory/to/enumerate", FS_NULL_TERMINATED, 0); pIterator != NULL; pIterator = fs_next(pIterator)) {
537 printf("Name: %s\n", pIterator->pName);
538 printf("Size: %llu\n", pIterator->info.size);
539 }
540 ```
541
542 If you want to terminate iteration early, use `fs_free_iterator()` to free the iterator object.
543 `fs_next()` will free the iterator for you when it reaches the end.
544
545 Like when opening a file, you can specify `FS_OPAQUE`, `FS_VERBOSE` or `FS_TRANSPARENT` (default)
546 in `fs_first()` to control which files are enumerated. Enumerated files will be consistent with
547 what would be opened when using the same option with `fs_file_open()`.
548
549 Internally, `fs_first()` will gather all of the enumerated files. This means you should expect
550 `fs_first()` to be slow compared to `fs_next()`.
551
552 Enumerated entries will be sorted by name in terms of `strcmp()`.
553
554 Enumeration is not recursive. If you want to enumerate recursively you will need to do it manually.
555 You can inspect the `directory` member of the `info` member in `fs_iterator` to determine if the
556 entry is a directory.
557
558
559 1.5. System Directories
560 -----------------------
561 It can often be useful to know the exact paths of known standard system directories, such as the
562 home directory. You can use the `fs_sysdir()` function for this:
563
564 ```c
565 char pPath[256];
566 size_t pathLen = fs_sysdir(FS_SYSDIR_HOME, pPath, sizeof(pPath));
567 if (pathLen > 0) {
568 if (pathLen < sizeof(pPath)) {
569 // Success!
570 } else {
571 // The buffer was too small. Expand the buffer to at least `pathLen + 1` and try again.
572 }
573 } else {
574 // An error occurred.
575 }
576 ```
577
578 `fs_sysdir()` will return the length of the path written to `pPath`, or 0 if an error occurred. If
579 the buffer is too small, it will return the required size, not including the null terminator.
580
581 Recognized system directories include the following:
582
583 - FS_SYSDIR_HOME
584 - FS_SYSDIR_TEMP
585 - FS_SYSDIR_CONFIG
586 - FS_SYSDIR_DATA
587 - FS_SYSDIR_CACHE
588
589
590
591 1.6. Temporary Files
592 --------------------
593 You can create a temporary file or folder with `fs_mktmp()`. To create a temporary folder, use the
594 `FS_MKTMP_DIR` option:
595
596 ```c
597 char pTmpPath[256];
598 fs_result result = fs_mktmp("prefix", pTmpPath, sizeof(pTmpPath), FS_MKTMP_DIR);
599 if (result != FS_SUCCESS) {
600 // Failed to create temporary file.
601 }
602 ```
603
604 Similarly, to create a temporary file, use the `FS_MKTMP_FILE` option:
605
606 ```c
607 char pTmpPath[256];
608 fs_result result = fs_mktmp("prefix", pTmpPath, sizeof(pTmpPath), FS_MKTMP_FILE);
609 if (result != FS_SUCCESS) {
610 // Failed to create temporary file.
611 }
612 ```
613
614 `fs_mktmp()` will create a temporary file or folder with a unique name based on the provided
615 prefix and will return the full path to the created file or folder in `pTmpPath`. To open the
616 temporary file, you can pass in the path to `fs_file_open()`, making sure to ignore mount points
617 with `FS_IGNORE_MOUNTS`:
618
619 ```c
620 fs_file* pFile;
621 result = fs_file_open(pFS, pTmpPath, FS_WRITE | FS_IGNORE_MOUNTS, &pFile);
622 if (result != FS_SUCCESS) {
623 // Failed to open temporary file.
624 }
625 ```
626
627 The prefix can include subdirectories, such as "myapp/subdir". In this case the library will create
628 the directory hierarchy for you, unless you pass in `FS_NO_CREATE_DIRS`. Note that not all
629 platforms treat the name portion of the prefix the same. In particular, Windows will only use up to
630 the first 3 characters of the name portion of the prefix.
631
632 If you don't like the behavior of `fs_mktmp()`, you can consider using `fs_sysdir()` with
633 `FS_SYSDIR_TEMP` and create the temporary file yourself.
634
635
636
637 2. Thread Safety
638 ================
639 The following points apply regarding thread safety.
640
641 - Opening files across multiple threads is safe. Backends are responsible for ensuring thread
642 safety when opening files.
643
644 - An individual `fs_file` object is not thread safe. If you want to use a specific `fs_file`
645 object across multiple threads, you will need to synchronize access to it yourself. Using
646 different `fs_file` objects across multiple threads is safe.
647
648 - Mounting and unmounting is not thread safe. You must use your own synchronization if you
649 want to do this across multiple threads.
650
651 - Opening a file on one thread while simultaneously mounting or unmounting on another thread is
652 not safe. Again, you must use your own synchronization if you need to do this. The recommended
653 usage is to set up your mount points once during initialization before opening any files.
654
655
656
657 3. Platform Considerations
658 ============================
659
660 3.1. Windows
661 --------------
662 On Windows, Unicode support is determined by the `UNICODE` preprocessor define. When `UNICODE` is
663 defined, the library will use the wide character versions of Windows APIs. When not defined, it
664 will use the ANSI versions.
665
666 3.2. POSIX
667 ------------
668 On POSIX platforms, `ftruncate()` is unavailable with `-std=c89` unless `_XOPEN_SOURCE` is defined
669 to >= 500. This may affect the availability of file truncation functionality when using strict C89
670 compilation.
671
672
673
674 4. Backends
675 ===========
676 You can implement custom backends to support different file systems and archive formats. A POSIX
677 or Win32 backend is the default backend depending on the platform, and is built into the library.
678 A backend implements the functions in the `fs_backend` structure.
679
680 A ZIP backend is included in the "extras" folder of this library's repository. Refer to this for
681 a complete example for how to implement a backend (not including write support, but I'm sure
682 you'll figure it out!). A PAK backend is also included in the "extras" folder, and is simpler than
683 the ZIP backend which might also be a good place to start.
684
685 The backend abstraction is designed to relieve backends from having to worry about the
686 implementation details of the main library. Backends should only concern themselves with their
687 own local content and not worry about things like mount points, archives, etc. Those details will
688 be handled at a higher level in the library.
689
690 Instances of a `fs` object can be configured with backend-specific configuration data. This is
691 passed to the backend as a void pointer to the necessary functions. This data will point to a
692 backend-defined structure that the backend will know how to use.
693
694 In order for the library to know how much memory to allocate for the `fs` object, the backend
695 needs to implement the `alloc_size` function. This function should return the total size of the
696 backend-specific data to associate with the `fs` object. Internally, this memory will be stored
697 at the end of the `fs` object. The backend can access this data via `fs_get_backend_data()`:
698
699 ```c
700 typedef struct my_fs_data
701 {
702 int someData;
703 } my_fs_data;
704
705 ...
706
707 my_fs_data* pBackendData = (my_fs_data*)fs_get_backend_data(pFS);
708 assert(pBackendData != NULL);
709
710 do_something(pBackendData->someData);
711 ```
712
713 This pattern will be a central part of how backends are implemented. If you don't have any
714 backend-specific data, you can just return 0 from `alloc_size()` and simply not reference the
715 backend data pointer.
716
717
718 4.1. Backend Functions
719 ----------------------
720 alloc_size
721 This function should return the size of the backend-specific data to associate with the `fs`
722 object. If no additional data is required, return 0.
723
724 The main library will allocate the `fs` object, including any additional space specified by the
725 `alloc_size` function.
726
727 init
728 This function is called after `alloc_size()` and after the `fs` object has been allocated. This
729 is where you should initialize the backend.
730
731 This function will take a pointer to the `fs` object, the backend-specific configuration data,
732 and a stream object. The stream is used to provide the backend with the raw data of an archive,
733 which will be required for archive backends like ZIP. If your backend requires this, you should
734 check for if the stream is null, and if so, return an error. See section "5. Streams" for more
735 details on how to use streams. You need not take a copy of the stream pointer for use outside
736 of `init`. Instead you can just use `fs_get_stream()` to get the stream object when you need it.
737 You should not ever close or otherwise take ownership of the stream - that will be handled
738 at a higher level.
739
740 uninit
741 This is where you should do any cleanup. Do not close the stream here.
742
743 remove
744 This function is used to delete a file or directory. This is not recursive. If the path is
745 a directory, the backend should return an error if it is not empty. Backends do not need to
746 implement this function in which case they can leave the callback pointer as `NULL`, or have
747 it return `FS_NOT_IMPLEMENTED`.
748
749 rename
750 This function is used to rename a file. This will act as a move if the source and destination
751 are in different directories. If the destination already exists, it should be overwritten. This
752 function is optional and can be left as `NULL` or return `FS_NOT_IMPLEMENTED`.
753
754 mkdir
755 This function is used to create a directory. This is not recursive. If the directory already
756 exists, the backend should return `FS_ALREADY_EXISTS`. If a parent directory does not exist,
757 the backend should return `FS_DOES_NOT_EXIST`. This function is optional and can be left as
758 `NULL` or return `FS_NOT_IMPLEMENTED`.
759
760 info
761 This function is used to get information about a file. If the backend does not have the notion
762 of the last modified or access time, it can set those values to 0. Set `directory` to 1 (or
763 `FS_TRUE`) if it's a directory. Likewise, set `symlink` to 1 if it's a symbolic link. It is
764 important that this function return the info of the exact file that would be opened with
765 `file_open()`. This function is mandatory.
766
767 file_alloc_size
768 Like when initializing a `fs` object, the library needs to know how much backend-specific data
769 to allocate for the `fs_file` object. This is done with the `file_alloc_size` function. This
770 function is basically the same as `alloc_size` for the `fs` object, but for `fs_file`. If the
771 backend does not need any additional data, it can return 0. The backend can access this data
772 via `fs_file_get_backend_data()`.
773
774 file_open
775 The `file_open` function is where the backend should open the file.
776
777 If the `fs` object that owns the file was initialized with a stream, the stream will be
778 non-null. If your backends requires a stream, you should check that the stream is null, and if
779 so, return an error. The reason you need to check for this is that the application itself may
780 erroneously attempt to initialize a `fs` object for your backend without a stream, and since
781 the library cannot know whether or not a backend requires a stream, it cannot check this on
782 your behalf.
783
784 If the backend requires a stream, it should take a copy of only the pointer and store it for
785 later use. Do *not* make a duplicate of the stream with `fs_stream_duplicate()`.
786
787 Backends need only handle the following open mode flags:
788
789 FS_READ
790 FS_WRITE
791 FS_TRUNCATE
792 FS_APPEND
793 FS_EXCLUSIVE
794
795 All other flags are for use at a higher level and should be ignored.
796
797 When opening in write mode (`FS_WRITE`), the backend should default to overwrite mode. If
798 `FS_TRUNCATE` is specified, the file should be truncated to 0 length. If `FS_APPEND` is
799 specified, all writes should happen at the end of the file regardless of the position of the
800 write cursor. If `FS_EXCLUSIVE` is specified, opening should fail if the file already exists.
801 In all write modes, the file should be created if it does not already exist.
802
803 If any flags cannot be supported, the backend should return an error.
804
805 When opening in read mode, if the file does not exist, `FS_DOES_NOT_EXIST` should be returned.
806 Similarly, if the file is a directory, `FS_IS_DIRECTORY` should be returned.
807
808 Before calling into this function, the library will normalize all paths to use forward slashes.
809 Therefore, backends must support forward slashes ("/") as path separators.
810
811 file_close
812 This function is where the file should be closed. This is where the backend should release any
813 resources associated with the file. Do not uninitialize the stream here - it'll be cleaned up
814 at a higher level.
815
816 file_read
817 This is used to read data from the file. The backend should return `FS_AT_END` when the end of
818 the file is reached, but only if the number of bytes read is 0.
819
820 file_write
821 This is used to write data to the file. If the file is opened in append mode, the backend
822 should always ensure writes are appended to the end, regardless of the position of the write
823 cursor. This is optional and need only be specified if the backend supports writing.
824
825 file_seek
826 The `file_seek` function is used to seek the read/write cursor. The backend should allow
827 seeking beyond the end of the file. If the file is opened in write mode and data is written
828 beyond the end of the file, the file should be extended, and if possible made into a sparse
829 file. If sparse files are not supported, the backend should fill the gap with data, preferably
830 with zeros if possible.
831
832 Attempting to seek to before the start of the file should return `FS_BAD_SEEK`.
833
834 file_tell
835 The `file_tell` function is used to get the current cursor position. There is only one cursor,
836 even when the file is opened in read and write mode.
837
838 file_flush
839 The `file_flush` function is used to flush any buffered data to the file. This is optional and
840 can be left as `NULL` or return `FS_NOT_IMPLEMENTED`.
841
842 file_truncate
843 The `file_truncate` function is used to truncate a file to the current cursor position. This is
844 only useful for write mode, so is therefore optional and can be left as `NULL` or return
845 `FS_NOT_IMPLEMENTED`.
846
847 file_info
848 The `file_info` function is used to get information about an opened file. It returns the same
849 information as `info` but for an opened file. This is mandatory.
850
851 file_duplicate
852 The `file_duplicate` function is used to duplicate a file. The destination file will be a new
853 file and already allocated. The backend need only copy the necessary backend-specific data to
854 the new file. The backend must ensure that the duplicated file is totally independent of the
855 original file and has its own independent read/write pointer. If the backend is unable to
856 support duplicated files having their own independent read/write pointer, it must return an
857 error.
858
859 If a backend cannot support duplication, it can leave this as `NULL` or return
860 `FS_NOT_IMPLEMENTED`. However, if this is not implemented, the backend will not be able to open
861 files within archives.
862
863 first, next, free_iterator
864 The `first`, `next` and `free_iterator` functions are used to enumerate the contents of a
865 directory. If the directory is empty, or an error occurs, `fs_first` should return `NULL`. The
866 `next` function should return `NULL` when there are no more entries. When `next` returns
867 `NULL`, the backend needs to free the iterator object. The `free_iterator` function is used to
868 free the iterator object explicitly. The backend is responsible for any memory management of
869 the name string. A typical way to deal with this is to allocate additional space for the name
870 immediately after the `fs_iterator` allocation.
871
872
873 4.2. Thread Safety
874 ------------------
875 Backends are responsible for guaranteeing thread-safety of different files across different
876 threads. This should typically be quite easy since most system backends, such as stdio, are already
877 thread-safe, and archive backends are typically read-only which should make thread-safety trivial
878 on that front as well. You need not worry about thread-safety of a single individual file handle.
879 But when you have two different file handles, they must be able to be used on two different threads
880 at the same time.
881
882
883 5. Streams
884 ==========
885 Streams are the data delivery mechanism for archive backends. You can implement custom streams, but
886 this should be uncommon because `fs_file` itself is a stream, and a memory stream is included in
887 the library called `fs_memory_stream`. Between these two the majority of use cases should be
888 covered. You can retrieve the stream associated with a `fs_file` using `fs_file_get_stream()`.
889
890 A stream is initialized using a specialized initialization function depending on the stream type.
891 For `fs_file`, simply opening the file is enough. For `fs_memory_stream`, you need to call
892 `fs_memory_stream_init_readonly()` for a standard read-only stream, or
893 `fs_memory_stream_init_write()` for a stream with write (and read) support. If you want to
894 implement your own stream type you would need to implement a similar initialization function.
895
896 Use `fs_stream_read()` and `fs_stream_write()` to read and write data from a stream. If the stream
897 does not support reading or writing, the respective function will return `FS_NOT_IMPLEMENTED`.
898
899 The cursor can be set and retrieved with `fs_stream_seek()` and `fs_stream_tell()`. There is only
900 a single cursor which is shared between reading and writing.
901
902 Streams can be duplicated. A duplicated stream is a fully independent stream. This functionality
903 is used heavily internally by the library so if you build a custom stream you should support it
904 if you can. Without duplication support, you will not be able to open files within archives. To
905 duplicate a stream, use `fs_stream_duplicate()`. To delete a duplicated stream, use
906 `fs_stream_delete_duplicate()`. Do not use implementation-specific uninitialization routines to
907 uninitialize a duplicated stream - `fs_stream_delete_duplicate()` will deal with that for you.
908
909 Streams are not thread safe. If you want to use a stream across multiple threads, you will need to
910 synchronize access to it yourself. Using different stream objects across multiple threads is safe.
911 A duplicated stream is entirely independent of the original stream and can be used on a different
912 thread to the original stream.
913
914 The `fs_stream` object is a base class. If you want to implement your own stream, you should make
915 the first member of your stream object a `fs_stream` object. This will allow you to cast between
916 `fs_stream*` and your custom stream type.
917
918 See `fs_stream_vtable` for a list of functions that need to be implemented for a custom stream. If
919 the stream does not support writing, the `write` callback can be left as `NULL` or return
920 `FS_NOT_IMPLEMENTED`.
921
922 See `fs_memory_stream` for an example of how to implement a custom stream.
923 */
924
925 /*
926 This library has been designed to be amalgamated into other libraries of mine. You will probably
927 see some random tags and stuff in this file. These are just used for doing a dumb amalgamation.
928 */
929 #ifndef fs_h
930 #define fs_h
931
932 #if defined(__cplusplus)
933 extern "C" {
934 #endif
935
936 /* BEG fs_platform_detection.c */
937 #if defined(_WIN32)
938 #define FS_WIN32
939 #else
940 #define FS_POSIX
941 #endif
942 /* END fs_platform_detection.c */
943
944 /* BEG fs_compiler_compat.h */
945 #include <stddef.h> /* For size_t. */
946 #include <stdarg.h> /* For va_list. */
947
948 #if defined(SIZE_MAX)
949 #define FS_SIZE_MAX SIZE_MAX
950 #else
951 #define FS_SIZE_MAX 0xFFFFFFFF /* When SIZE_MAX is not defined by the standard library just default to the maximum 32-bit unsigned integer. */
952 #endif
953
954 #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__powerpc64__)
955 #define FS_SIZEOF_PTR 8
956 #else
957 #define FS_SIZEOF_PTR 4
958 #endif
959
960 #if FS_SIZEOF_PTR == 8
961 #define FS_64BIT
962 #else
963 #define FS_32BIT
964 #endif
965
966 #if defined(FS_USE_STDINT)
967 #include <stdint.h>
968 typedef int8_t fs_int8;
969 typedef uint8_t fs_uint8;
970 typedef int16_t fs_int16;
971 typedef uint16_t fs_uint16;
972 typedef int32_t fs_int32;
973 typedef uint32_t fs_uint32;
974 typedef int64_t fs_int64;
975 typedef uint64_t fs_uint64;
976 #else
977 typedef signed char fs_int8;
978 typedef unsigned char fs_uint8;
979 typedef signed short fs_int16;
980 typedef unsigned short fs_uint16;
981 typedef signed int fs_int32;
982 typedef unsigned int fs_uint32;
983 #if defined(_MSC_VER) && !defined(__clang__)
984 typedef signed __int64 fs_int64;
985 typedef unsigned __int64 fs_uint64;
986 #else
987 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
988 #pragma GCC diagnostic push
989 #pragma GCC diagnostic ignored "-Wlong-long"
990 #if defined(__clang__)
991 #pragma GCC diagnostic ignored "-Wc++11-long-long"
992 #endif
993 #endif
994 typedef signed long long fs_int64;
995 typedef unsigned long long fs_uint64;
996 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
997 #pragma GCC diagnostic pop
998 #endif
999 #endif
1000 #endif /* FS_USE_STDINT */
1001
1002 #if FS_SIZEOF_PTR == 8
1003 typedef fs_uint64 fs_uintptr;
1004 typedef fs_int64 fs_intptr;
1005 #else
1006 typedef fs_uint32 fs_uintptr;
1007 typedef fs_int32 fs_intptr;
1008 #endif
1009
1010 typedef unsigned char fs_bool8;
1011 typedef unsigned int fs_bool32;
1012 #define FS_TRUE 1
1013 #define FS_FALSE 0
1014
1015
1016 #define FS_INT64_MAX ((fs_int64)(((fs_uint64)0x7FFFFFFF << 32) | 0xFFFFFFFF))
1017
1018
1019 #ifndef FS_API
1020 #define FS_API
1021 #endif
1022
1023 #ifdef _MSC_VER
1024 #define FS_INLINE __forceinline
1025 #elif defined(__GNUC__)
1026 /*
1027 I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
1028 the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
1029 case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
1030 command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
1031 I am using "__inline__" only when we're compiling in strict ANSI mode.
1032 */
1033 #if defined(__STRICT_ANSI__)
1034 #define FS_GNUC_INLINE_HINT __inline__
1035 #else
1036 #define FS_GNUC_INLINE_HINT inline
1037 #endif
1038
1039 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)) || defined(__clang__)
1040 #define FS_INLINE FS_GNUC_INLINE_HINT __attribute__((always_inline))
1041 #else
1042 #define FS_INLINE FS_GNUC_INLINE_HINT
1043 #endif
1044 #elif defined(__WATCOMC__)
1045 #define FS_INLINE __inline
1046 #else
1047 #define FS_INLINE
1048 #endif
1049
1050
1051 #if defined(__has_attribute)
1052 #if __has_attribute(format)
1053 #define FS_ATTRIBUTE_FORMAT(fmt, va) __attribute__((format(printf, fmt, va)))
1054 #endif
1055 #endif
1056 #ifndef FS_ATTRIBUTE_FORMAT
1057 #define FS_ATTRIBUTE_FORMAT(fmt, va)
1058 #endif
1059
1060
1061 #define FS_NULL_TERMINATED ((size_t)-1)
1062 /* END fs_compiler_compat.h */
1063
1064
1065 /* BEG fs_thread_basic_types.h */
1066 #if defined(FS_POSIX)
1067 #ifndef FS_USE_PTHREAD
1068 #define FS_USE_PTHREAD
1069 #endif
1070
1071 #ifndef FS_NO_PTHREAD_IN_HEADER
1072 #include <pthread.h>
1073 typedef pthread_t fs_pthread_t;
1074 typedef pthread_mutex_t fs_pthread_mutex_t;
1075 #else
1076 typedef fs_uintptr fs_pthread_t;
1077 typedef union fs_pthread_mutex_t { char __data[40]; fs_uint64 __alignment; } fs_pthread_mutex_t;
1078 #endif
1079 #endif
1080 /* END fs_thread_basic_types.h */
1081
1082
1083 /* BEG fs_thread_mtx.h */
1084 #if defined(FS_WIN32)
1085 typedef struct
1086 {
1087 void* handle; /* HANDLE, CreateMutex(), CreateEvent() */
1088 int type;
1089 } fs_mtx;
1090 #else
1091 /*
1092 We may need to force the use of a manual recursive mutex which will happen when compiling
1093 on very old compilers, or with `-std=c89`.
1094 */
1095
1096 /* If __STDC_VERSION__ is not defined it means we're compiling in C89 mode. */
1097 #if !defined(FS_USE_MANUAL_RECURSIVE_MUTEX) && !defined(__STDC_VERSION__)
1098 #define FS_USE_MANUAL_RECURSIVE_MUTEX
1099 #endif
1100
1101 /* This is for checking if PTHREAD_MUTEX_RECURSIVE is available. */
1102 #if !defined(FS_USE_MANUAL_RECURSIVE_MUTEX) && (!defined(__USE_UNIX98) && !defined(__USE_XOPEN2K8))
1103 #define FS_USE_MANUAL_RECURSIVE_MUTEX
1104 #endif
1105
1106 #ifdef FS_USE_MANUAL_RECURSIVE_MUTEX
1107 typedef struct
1108 {
1109 fs_pthread_mutex_t mutex; /* The underlying pthread mutex. */
1110 fs_pthread_mutex_t guard; /* Guard for metadata (owner and recursionCount). */
1111 fs_pthread_t owner;
1112 int recursionCount;
1113 int type;
1114 } fs_mtx;
1115 #else
1116 typedef fs_pthread_mutex_t fs_mtx;
1117 #endif
1118 #endif
1119
1120 enum
1121 {
1122 fs_mtx_plain = 0x00000000,
1123 fs_mtx_timed = 0x00000001,
1124 fs_mtx_recursive = 0x00000002
1125 };
1126
1127 FS_API int fs_mtx_init(fs_mtx* mutex, int type);
1128 FS_API void fs_mtx_destroy(fs_mtx* mutex);
1129 FS_API int fs_mtx_lock(fs_mtx* mutex);
1130 FS_API int fs_mtx_unlock(fs_mtx* mutex);
1131 /* END fs_thread_mtx.h */
1132
1133
1134 /* BEG fs_result.h */
1135 typedef enum
1136 {
1137 FS_SUCCESS = 0,
1138 FS_ERROR = -1, /* Generic, unknown error. */
1139 FS_INVALID_ARGS = -2,
1140 FS_INVALID_OPERATION = -3,
1141 FS_OUT_OF_MEMORY = -4,
1142 FS_OUT_OF_RANGE = -5,
1143 FS_ACCESS_DENIED = -6,
1144 FS_DOES_NOT_EXIST = -7,
1145 FS_ALREADY_EXISTS = -8,
1146 FS_TOO_MANY_OPEN_FILES = -9,
1147 FS_INVALID_FILE = -10,
1148 FS_TOO_BIG = -11,
1149 FS_PATH_TOO_LONG = -12,
1150 FS_NAME_TOO_LONG = -13,
1151 FS_NOT_DIRECTORY = -14,
1152 FS_IS_DIRECTORY = -15,
1153 FS_DIRECTORY_NOT_EMPTY = -16,
1154 FS_AT_END = -17,
1155 FS_NO_SPACE = -18,
1156 FS_BUSY = -19,
1157 FS_IO_ERROR = -20,
1158 FS_INTERRUPT = -21,
1159 FS_UNAVAILABLE = -22,
1160 FS_ALREADY_IN_USE = -23,
1161 FS_BAD_ADDRESS = -24,
1162 FS_BAD_SEEK = -25,
1163 FS_BAD_PIPE = -26,
1164 FS_DEADLOCK = -27,
1165 FS_TOO_MANY_LINKS = -28,
1166 FS_NOT_IMPLEMENTED = -29,
1167 FS_NO_MESSAGE = -30,
1168 FS_BAD_MESSAGE = -31,
1169 FS_NO_DATA_AVAILABLE = -32,
1170 FS_INVALID_DATA = -33,
1171 FS_TIMEOUT = -34,
1172 FS_NO_NETWORK = -35,
1173 FS_NOT_UNIQUE = -36,
1174 FS_NOT_SOCKET = -37,
1175 FS_NO_ADDRESS = -38,
1176 FS_BAD_PROTOCOL = -39,
1177 FS_PROTOCOL_UNAVAILABLE = -40,
1178 FS_PROTOCOL_NOT_SUPPORTED = -41,
1179 FS_PROTOCOL_FAMILY_NOT_SUPPORTED = -42,
1180 FS_ADDRESS_FAMILY_NOT_SUPPORTED = -43,
1181 FS_SOCKET_NOT_SUPPORTED = -44,
1182 FS_CONNECTION_RESET = -45,
1183 FS_ALREADY_CONNECTED = -46,
1184 FS_NOT_CONNECTED = -47,
1185 FS_CONNECTION_REFUSED = -48,
1186 FS_NO_HOST = -49,
1187 FS_IN_PROGRESS = -50,
1188 FS_CANCELLED = -51,
1189 FS_MEMORY_ALREADY_MAPPED = -52,
1190 FS_DIFFERENT_DEVICE = -53,
1191 FS_CHECKSUM_MISMATCH = -100,
1192 FS_NO_BACKEND = -101,
1193
1194 /* Non-Error Result Codes. */
1195 FS_NEEDS_MORE_INPUT = 100, /* Some stream needs more input data before it can be processed. */
1196 FS_HAS_MORE_OUTPUT = 102 /* Some stream has more output data to be read, but there's not enough room in the output buffer. */
1197 } fs_result;
1198
1199 FS_API const char* fs_result_description(fs_result result);
1200 /* END fs_result.h */
1201
1202
1203 /* BEG fs_allocation_callbacks.h */
1204 typedef struct fs_allocation_callbacks
1205 {
1206 void* pUserData;
1207 void* (* onMalloc )(size_t sz, void* pUserData);
1208 void* (* onRealloc)(void* p, size_t sz, void* pUserData);
1209 void (* onFree )(void* p, void* pUserData);
1210 } fs_allocation_callbacks;
1211
1212 FS_API void* fs_malloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks);
1213 FS_API void* fs_calloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks);
1214 FS_API void* fs_realloc(void* p, size_t sz, const fs_allocation_callbacks* pAllocationCallbacks);
1215 FS_API void fs_free(void* p, const fs_allocation_callbacks* pAllocationCallbacks);
1216 /* END fs_allocation_callbacks.h */
1217
1218
1219
1220 /* BEG fs_stream.h */
1221 /*
1222 Streams.
1223
1224 The feeding of input and output data is done via a stream.
1225
1226 To implement a custom stream, such as a memory stream, or a file stream, you need to extend from
1227 `fs_stream` and implement `fs_stream_vtable`. You can access your custom data by casting the
1228 `fs_stream` to your custom type.
1229
1230 The stream vtable can support both reading and writing, but it doesn't need to support both at
1231 the same time. If one is not supported, simply leave the relevant `read` or `write` callback as
1232 `NULL`, or have them return FS_NOT_IMPLEMENTED.
1233 */
1234
1235 typedef enum fs_seek_origin
1236 {
1237 FS_SEEK_SET = 0,
1238 FS_SEEK_CUR = 1,
1239 FS_SEEK_END = 2
1240 } fs_seek_origin;
1241
1242 typedef struct fs_stream_vtable fs_stream_vtable;
1243 typedef struct fs_stream fs_stream;
1244
1245 struct fs_stream_vtable
1246 {
1247 fs_result (* read )(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead);
1248 fs_result (* write )(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten);
1249 fs_result (* seek )(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin);
1250 fs_result (* tell )(fs_stream* pStream, fs_int64* pCursor);
1251 /* BEG fs_stream_vtable_duplicate */
1252 size_t (* duplicate_alloc_size)(fs_stream* pStream); /* Optional. Returns the allocation size of the stream. When not defined, duplicating is disabled. */
1253 fs_result (* duplicate )(fs_stream* pStream, fs_stream* pDuplicatedStream); /* Optional. Duplicate the stream. */
1254 void (* uninit )(fs_stream* pStream); /* Optional. Uninitialize the stream. */
1255 /* END fs_stream_vtable_duplicate */
1256 };
1257
1258 struct fs_stream
1259 {
1260 const fs_stream_vtable* pVTable;
1261 };
1262
1263 FS_API fs_result fs_stream_init(const fs_stream_vtable* pVTable, fs_stream* pStream);
1264 FS_API fs_result fs_stream_read(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead);
1265 FS_API fs_result fs_stream_write(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten);
1266 FS_API fs_result fs_stream_seek(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin);
1267 FS_API fs_result fs_stream_tell(fs_stream* pStream, fs_int64* pCursor);
1268
1269 /* BEG fs_stream_writef.h */
1270 FS_API fs_result fs_stream_writef(fs_stream* pStream, const char* fmt, ...) FS_ATTRIBUTE_FORMAT(2, 3);
1271 FS_API fs_result fs_stream_writef_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, ...) FS_ATTRIBUTE_FORMAT(3, 4);
1272 FS_API fs_result fs_stream_writefv(fs_stream* pStream, const char* fmt, va_list args);
1273 FS_API fs_result fs_stream_writefv_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, va_list args);
1274 /* END fs_stream_writef.h */
1275
1276 /* BEG fs_stream_duplicate.h */
1277 /*
1278 Duplicates a stream.
1279
1280 This will allocate the new stream on the heap. The caller is responsible for freeing the stream
1281 with `fs_stream_delete_duplicate()` when it's no longer needed.
1282 */
1283 FS_API fs_result fs_stream_duplicate(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, fs_stream** ppDuplicatedStream);
1284
1285 /*
1286 Deletes a duplicated stream.
1287
1288 Do not use this for a stream that was not duplicated with `fs_stream_duplicate()`.
1289 */
1290 FS_API void fs_stream_delete_duplicate(fs_stream* pDuplicatedStream, const fs_allocation_callbacks* pAllocationCallbacks);
1291 /* END fs_stream_duplicate.h */
1292
1293 /* BEG fs_stream_helpers.h */
1294 /*
1295 Helper functions for reading the entire contents of a stream, starting from the current cursor position. Free
1296 the returned pointer with fs_free().
1297
1298 The format (FS_FORMAT_TEXT or FS_FORMAT_BINARY) is used to determine whether or not a null terminator should be
1299 appended to the end of the data.
1300
1301 For flexiblity in case the backend does not support cursor retrieval or positioning, the data will be read
1302 in fixed sized chunks.
1303 */
1304
1305 typedef enum fs_format
1306 {
1307 FS_FORMAT_TEXT,
1308 FS_FORMAT_BINARY
1309 } fs_format;
1310
1311 FS_API fs_result fs_stream_read_to_end(fs_stream* pStream, fs_format format, const fs_allocation_callbacks* pAllocationCallbacks, void** ppData, size_t* pDataSize);
1312 /* END fs_stream_helpers.h */
1313 /* END fs_stream.h */
1314
1315
1316 /* BEG fs_sysdir.h */
1317 typedef enum fs_sysdir_type
1318 {
1319 FS_SYSDIR_HOME,
1320 FS_SYSDIR_TEMP,
1321 FS_SYSDIR_CONFIG,
1322 FS_SYSDIR_DATA,
1323 FS_SYSDIR_CACHE
1324 } fs_sysdir_type;
1325
1326 /*
1327 Get the path of a known system directory.
1328
1329 The returned path will be null-terminated. If the output buffer is too small, the required size
1330 will be returned, not including the null terminator.
1331
1332
1333 Parameters
1334 ----------
1335 type : (in)
1336 The type of system directory to query. See `fs_sysdir_type` for recognized values.
1337
1338 pDst : (out, optional)
1339 A pointer to a buffer that will receive the path. If NULL, the function will return the
1340 required length of the buffer, not including the null terminator.
1341
1342 dstCap : (in)
1343 The capacity of the output buffer, in bytes. This is ignored if `pDst` is NULL.
1344
1345
1346 Return Value
1347 ------------
1348 Returns the length of the string, not including the null terminator. Returns 0 on failure. If the
1349 return value is >= to `dstCap` it means the output buffer was too small. Use the returned value to
1350 know how big to make the buffer.
1351
1352
1353 Example 1 - Querying the Home Directory
1354 ---------------------------------------
1355 ```c
1356 size_t len = fs_sysdir(FS_SYSDIR_HOME, NULL, 0);
1357 if (len == 0) {
1358 // Failed to query the length of the home directory path.
1359 }
1360
1361 char* pPath = (char*)malloc(len + 1); // +1 for null terminator.
1362 if (pPath == NULL) {
1363 // Out of memory.
1364 }
1365
1366 len = fs_sysdir(FS_SYSDIR_HOME, pPath, len + 1);
1367 if (len == 0) {
1368 // Failed to get the home directory path.
1369 }
1370
1371 printf("Home directory: %s\n", pPath);
1372 free(pPath);
1373 ```
1374
1375
1376 See Also
1377 --------
1378 fs_sysdir_type
1379 fs_mktmp()
1380 */
1381 FS_API size_t fs_sysdir(fs_sysdir_type type, char* pDst, size_t dstCap);
1382 /* END fs_sysdir.h */
1383
1384
1385 /* BEG fs_mktmp.h */
1386 /*
1387 Create a temporary file or directory.
1388
1389 This function creates a temporary file or directory with a unique name based on the provided
1390 prefix. The full path to the created file or directory is returned in `pTmpPath`.
1391
1392 Use the option flag `FS_MKTMP_FILE` to create a temporary file, or `FS_MKTMP_DIR` to create a
1393 temporary directory.
1394
1395
1396 Parameters
1397 ----------
1398 pPrefix : (in)
1399 A prefix for the temporary file or directory name. This should not include the system's base
1400 temp directory path. Do not include paths like "/tmp" in the prefix. The output path will
1401 include the system's base temp directory and the prefix.
1402
1403 The prefix can include subdirectories, such as "myapp/subdir". In this case the library will
1404 create the directory hierarchy for you, unless you pass in `FS_NO_CREATE_DIRS`. Note that not
1405 all platforms treat the name portion of the prefix the same. In particular, Windows will only
1406 use up to the first 3 characters of the name portion of the prefix.
1407
1408 pTmpPath : (out)
1409 A pointer to a buffer that will receive the full path of the created temporary file or
1410 directory. This will be null-terminated.
1411
1412 tmpPathCap : (in)
1413 The capacity of the output buffer, in bytes.
1414
1415 options : (in)
1416 Options for creating the temporary file or directory. Can be a combination of the following:
1417 FS_MKTMP_FILE
1418 Creates a temporary file. Cannot be used with FS_MKTMP_DIR.
1419
1420 FS_MKTMP_DIR
1421 Creates a temporary directory. Cannot be used with FS_MKTMP_FILE.
1422
1423 FS_NO_CREATE_DIRS
1424 Do not create parent directories if they do not exist. If this flag is not set,
1425 parent directories will be created as needed.
1426
1427
1428 Return Value
1429 ------------
1430 Returns `FS_SUCCESS` on success; any other error code on failure. Will return `FS_PATH_TOO_LONG` if
1431 the output buffer is too small.
1432 */
1433 FS_API fs_result fs_mktmp(const char* pPrefix, char* pTmpPath, size_t tmpPathCap, int options); /* Returns FS_PATH_TOO_LONG if the output buffer is too small. Use FS_MKTMP_FILE to create a file and FS_MKTMP_DIR to create a directory. pPrefix should not include the name of the system's base temp directory. Do not include paths like "/tmp" in the prefix. The output path will include the system's base temp directory and the prefix. */
1434 /* END fs_mktmp.h */
1435
1436
1437 /* BEG fs.h */
1438 /**************************************************************************************************
1439
1440 Open Mode Flags
1441
1442 **************************************************************************************************/
1443 #define FS_READ 0x0001 /* Used by: fs_file_open(), fs_info(), fs_first(), fs_open_archive*(), fs_mount*() */
1444 #define FS_WRITE 0x0002 /* Used by: fs_file_open(), fs_info(), fs_first(), fs_open_archive*(), fs_mount*() */
1445 #define FS_TRUNCATE 0x0004 /* Used by: fs_file_open() */
1446 #define FS_APPEND 0x0008 /* Used by: fs_file_open() */
1447 #define FS_EXCLUSIVE 0x0010 /* Used by: fs_file_open() */
1448
1449 #define FS_TRANSPARENT 0x0000 /* Used by: fs_file_open(), fs_info(), fs_first() */
1450 #define FS_OPAQUE 0x0020 /* Used by: fs_file_open(), fs_info(), fs_first() */
1451 #define FS_VERBOSE 0x0040 /* Used by: fs_file_open(), fs_info(), fs_first() */
1452
1453 #define FS_NO_CREATE_DIRS 0x0080 /* Used by: fs_file_open(), fs_info(), fs_mount(), fs_mkdir(), fs_mktmp() */
1454 #define FS_IGNORE_MOUNTS 0x0100 /* Used by: fs_file_open(), fs_info(), fs_first() */
1455 #define FS_ONLY_MOUNTS 0x0200 /* Used by: fs_file_open(), fs_info(), fs_first() */
1456 #define FS_NO_SPECIAL_DIRS 0x0400 /* Used by: fs_file_open(), fs_info(), fs_first() */
1457 #define FS_NO_ABOVE_ROOT_NAVIGATION 0x0800 /* Used by: fs_file_open(), fs_info(), fs_first() */
1458
1459 #define FS_LOWEST_PRIORITY 0x1000 /* Used by: fs_mount*() */
1460
1461 #define FS_MKTMP_DIR 0x2000 /* Used by: fs_mktmp() */
1462 #define FS_MKTMP_FILE 0x4000 /* Used by: fs_mktmp() */
1463
1464 #define FS_NO_INCREMENT_REFCOUNT 0x8000 /* Do not use. Internal use only. Used with fs_open_archive_ex() internally. */
1465
1466
1467 /* Garbage collection policies.*/
1468 #define FS_GC_POLICY_THRESHOLD 0x0001 /* Only garbage collect unreferenced opened archives until the count is below the configured threshold. */
1469 #define FS_GC_POLICY_FULL 0x0002 /* Garbage collect every unreferenced opened archive, regardless of how many are open.*/
1470
1471
1472 typedef struct fs_config fs_config;
1473 typedef struct fs fs;
1474 typedef struct fs_file fs_file;
1475 typedef struct fs_file_info fs_file_info;
1476 typedef struct fs_iterator fs_iterator;
1477 typedef struct fs_backend fs_backend;
1478
1479
1480 /* File paths for stdin, stdout and stderr. Use these with fs_file_open(). */
1481 extern const char* FS_STDIN;
1482 extern const char* FS_STDOUT;
1483 extern const char* FS_STDERR;
1484
1485
1486 /*
1487 This callback is fired when the reference count of a fs object changes. This is useful if you want
1488 to do some kind of advanced memory management, such as garbage collection. If the new reference count
1489 is 1, it means no other objects are referencing the fs object.
1490 */
1491 typedef void (* fs_on_refcount_changed_proc)(void* pUserData, fs* pFS, fs_uint32 newRefCount, fs_uint32 oldRefCount);
1492
1493 typedef struct fs_archive_type
1494 {
1495 const fs_backend* pBackend;
1496 const char* pExtension;
1497 } fs_archive_type;
1498
1499 FS_API fs_archive_type fs_archive_type_init(const fs_backend* pBackend, const char* pExtension);
1500
1501
1502 struct fs_file_info
1503 {
1504 fs_uint64 size;
1505 fs_uint64 lastModifiedTime;
1506 fs_uint64 lastAccessTime;
1507 int directory;
1508 int symlink;
1509 };
1510
1511 struct fs_iterator
1512 {
1513 fs* pFS;
1514 const char* pName; /* Must be null terminated. The FS implementation is responsible for manageing the memory allocation. */
1515 size_t nameLen;
1516 fs_file_info info;
1517 };
1518
1519
1520 /*
1521 Configuration structure for fs objects.
1522
1523 Members
1524 -------
1525 pBackend
1526 The backend to use. If NULL, the standard file system backend will be used.
1527
1528 pBackendConfig
1529 A pointer to a backend-specific configuration structure. This is passed directly to the
1530 backend's init function. The documentation for your backend will tell you how to use this. Most
1531 backends will allow you to set this to NULL.
1532
1533 pStream
1534 A stream to use for archive backends. This is required for any file-backed backend, such as ZIP
1535 archives. If the backend does not need a stream, this can be NULL.
1536
1537 pArchiveTypes
1538 An array of archive types to register. This can be NULL if no archive types are to be
1539 registered. Archive types are mapped to file extensions. See `fs_archive_type` for more
1540 information.
1541
1542 archiveTypeCount
1543 The number of archive types in the `pArchiveTypes` array. Set this to 0 if `pArchiveTypes` is
1544 NULL.
1545
1546 onRefCountChanged
1547 A callback that is fired when the reference count of a fs object changes.
1548
1549 pRefCountChangedUserData
1550 User data that is passed to the `onRefCountChanged` callback.
1551
1552 pAllocationCallbacks
1553 Custom allocation callbacks. If NULL, the standard malloc/realloc/free functions will be used.
1554 */
1555 struct fs_config
1556 {
1557 const fs_backend* pBackend;
1558 const void* pBackendConfig;
1559 fs_stream* pStream;
1560 const fs_archive_type* pArchiveTypes;
1561 size_t archiveTypeCount;
1562 fs_on_refcount_changed_proc onRefCountChanged;
1563 void* pRefCountChangedUserData;
1564 const fs_allocation_callbacks* pAllocationCallbacks;
1565 };
1566
1567 FS_API fs_config fs_config_init_default(void);
1568 FS_API fs_config fs_config_init(const fs_backend* pBackend, const void* pBackendConfig, fs_stream* pStream);
1569
1570
1571 struct fs_backend
1572 {
1573 size_t (* alloc_size )(const void* pBackendConfig);
1574 fs_result (* init )(fs* pFS, const void* pBackendConfig, fs_stream* pStream); /* Return 0 on success or an errno result code on error. pBackendConfig is a pointer to a backend-specific struct. The documentation for your backend will tell you how to use this. You can usually pass in NULL for this. */
1575 void (* uninit )(fs* pFS);
1576 fs_result (* remove )(fs* pFS, const char* pFilePath);
1577 fs_result (* rename )(fs* pFS, const char* pOldPath, const char* pNewPath); /* Return FS_DIFFERENT_DEVICE if the old and new paths are on different devices and would require a copy. */
1578 fs_result (* mkdir )(fs* pFS, const char* pPath); /* This is not recursive. Return FS_ALREADY_EXISTS if directory already exists. Return FS_DOES_NOT_EXIST if a parent directory does not exist. */
1579 fs_result (* info )(fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo); /* openMode flags can be ignored by most backends. It's primarily used by passthrough style backends. */
1580 size_t (* file_alloc_size )(fs* pFS);
1581 fs_result (* file_open )(fs* pFS, fs_stream* pStream, const char* pFilePath, int openMode, fs_file* pFile); /* Return 0 on success or an errno result code on error. Return FS_DOES_NOT_EXIST if the file does not exist. pStream will be null if the backend does not need a stream (the `pFS` object was not initialized with one). */
1582 void (* file_close )(fs_file* pFile);
1583 fs_result (* file_read )(fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead); /* Return 0 on success, or FS_AT_END on end of file. Only return FS_AT_END if *pBytesRead is 0. Return an errno code on error. Implementations must support reading when already at EOF, in which case FS_AT_END should be returned and *pBytesRead should be 0. */
1584 fs_result (* file_write )(fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten);
1585 fs_result (* file_seek )(fs_file* pFile, fs_int64 offset, fs_seek_origin origin);
1586 fs_result (* file_tell )(fs_file* pFile, fs_int64* pCursor);
1587 fs_result (* file_flush )(fs_file* pFile);
1588 fs_result (* file_truncate )(fs_file* pFile);
1589 fs_result (* file_info )(fs_file* pFile, fs_file_info* pInfo);
1590 fs_result (* file_duplicate )(fs_file* pFile, fs_file* pDuplicate); /* Duplicate the file handle. */
1591 fs_iterator* (* first )(fs* pFS, const char* pDirectoryPath, size_t directoryPathLen);
1592 fs_iterator* (* next )(fs_iterator* pIterator); /* <-- Must return null when there are no more files. In this case, free_iterator must be called internally. */
1593 void (* free_iterator )(fs_iterator* pIterator); /* <-- Free the `fs_iterator` object here since `first` and `next` were the ones who allocated it. Also do any uninitialization routines. */
1594 };
1595
1596 /*
1597 Initializes a file system object.
1598
1599 This is the main object that you will use to open files. There are different types of file system
1600 backends, such as the standard file system, ZIP archives, etc. which you can configure via the
1601 config.
1602
1603 The config is used to select which backend to use and to register archive types against known
1604 file extensions. If you just want to use the regular file system and don't care about archives,
1605 you can just pass in NULL for the config.
1606
1607 By registering archive types, you'll be able to open files from within them straight from a file
1608 path without without needing to do any manual management. For example, if you register ZIP archives
1609 to the ".zip" extension, you can open a file from a path like this:
1610
1611 somefolder/archive.zip/somefile.txt
1612
1613 These can also be handled transparently, so the above path can be opened with this:
1614
1615 somefolder/somefile.txt
1616
1617 Note that the `archive.zip` part is not needed. If you want this functionality, you must register
1618 the archive types with the config.
1619
1620 Most of the time you will use a `fs` object that represents the normal file system, which is the
1621 default backend if you don't pass in a config, but sometimes you may want to have a `fs` object
1622 that represents an archive, such as a ZIP archive. To do this, you need to provide a stream that
1623 reads the actual data of the archive. Most of the time you will just use the stream provided by
1624 a `fs_file` object you opened earlier from the regular file system, but if you would rather source
1625 your data from elsewhere, like a memory buffer, you can pass in your own stream. You also need to
1626 specify the backend to use, such as `FS_ZIP` in the case of ZIP archives. See examples below for
1627 more information.
1628
1629 If you want to use custom allocation callbacks, you can do so by passing in a pointer to a
1630 `fs_allocation_callbacks` struct into the config. If you pass in NULL, the default allocation
1631 callbacks which use malloc/realloc/free will be used. If you pass in non-NULL, this function will
1632 make a copy of the struct, so you can free or modify the struct after this function returns.
1633
1634
1635 Parameters
1636 ----------
1637 pConfig : (in, optional)
1638 A pointer to a configuration struct. Can be NULL, in which case the regular file system will be
1639 used, and archives will not be supported unless explicitly mounted later with `fs_mount_fs()`.
1640
1641 ppFS : (out)
1642 A pointer to a pointer which will receive the initialized file system object. The object must
1643 be uninitialized with `fs_uninit()` when no longer needed.
1644
1645
1646 Return Value
1647 ------------
1648 Returns FS_SUCCESS on success; any other result code otherwise.
1649
1650
1651 Example 1 - Basic Usage
1652 -----------------------
1653 The example below shows how to initialize a `fs` object which uses the regular file system and does
1654 not support archives. This is the most basic usage of the `fs` object.
1655
1656 ```c
1657 #include "fs.h"
1658
1659 ...
1660
1661 fs* pFS;
1662 fs_result result = fs_init(NULL, &pFS);
1663 if (result != FS_SUCCESS) {
1664 // Handle error.
1665 }
1666
1667 ...
1668
1669 fs_uninit(pFS);
1670 ```
1671
1672
1673 Example 2 - Supporting Archives
1674 -------------------------------
1675 The example below shows how to initialize a `fs` object which uses the regular file system and
1676 supports ZIP archives. Error checking has been omitted for clarity.
1677
1678 ```c
1679 #include "fs.h"
1680 #include "extras/backends/zip/fs_zip.h" // For FS_ZIP backend.
1681
1682 ...
1683
1684 fs* pFS;
1685 fs_config fsConfig;
1686
1687 // Archive types are supported by mapping a backend (`FS_ZIP` in this case) to a file extension.
1688 fs_archive_type pArchiveTypes[] =
1689 {
1690 {FS_ZIP, "zip"}
1691 };
1692
1693 // The archive types are registered via the config.
1694 fsConfig = fs_config_init_default();
1695 fsConfig.pArchiveTypes = pArchiveTypes;
1696 fsConfig.archiveTypeCount = sizeof(pArchiveTypes) / sizeof(pArchiveTypes[0]);
1697
1698 // Once the config is ready, initialize the fs object.
1699 fs_init(&fsConfig, &pFS);
1700
1701 // Now you can open files from within ZIP archives from a file path.
1702 fs_file* pFileInArchive;
1703 fs_file_open(pFS, "somefolder/archive.zip/somefile.txt", FS_READ, &pFileInArchive);
1704 ```
1705
1706
1707 Example 3 - Archive Backends
1708 ----------------------------
1709 This example shows how you can open an archive file directly, and then create a new `fs` object
1710 which uses the archive as its backend. This is useful if, for example, you want to use a ZIP file
1711 as a virtual file system.
1712
1713 ```c
1714 #include "fs.h"
1715 #include "extras/backends/zip/fs_zip.h" // For FS_ZIP backend.
1716
1717 ...
1718
1719 fs* pFS; // Main file system object.
1720 fs* pArchiveFS; // File system object for the archive.
1721 fs_config archiveConfig;
1722 fs_file* pArchiveFile; // The actual archive file.
1723
1724 // Open the archive file itself first, usually from the regular file system.
1725 fs_file_open(pFS, "somefolder/archive.zip", FS_READ, &pArchiveFile);
1726
1727 ...
1728
1729 // Setup the config for the archive `fs` object such that it uses the ZIP backend (FS_ZIP), and
1730 // reads from the stream of the actual archive file (pArchiveFile) which was opened earlier.
1731 archiveConfig = fs_config_init(FS_ZIP, NULL, fs_file_get_stream(pArchiveFile));
1732
1733 // With the config ready we can now initialize the `fs` object for the archive.
1734 fs_init(&archiveConfig, &pArchiveFS);
1735
1736 ...
1737
1738 // Now that we have a `fs` object representing the archive, we can open files from within it like
1739 // normal.
1740 fs_file* pFileInArchive;
1741 fs_file_open(pArchiveFS, "fileinsidearchive.txt", FS_READ, &pFileInArchive);
1742 ```
1743
1744
1745 See Also
1746 --------
1747 fs_uninit()
1748 */
1749 FS_API fs_result fs_init(const fs_config* pConfig, fs** ppFS);
1750
1751
1752 /*
1753 Uninitializes a file system object.
1754
1755 This does not do any file closing for you. You must close any opened files yourself before calling
1756 this function.
1757
1758
1759 Parameters
1760 ----------
1761 pFS : (in)
1762 A pointer to the file system object to uninitialize. Must not be NULL.
1763
1764
1765 See Also
1766 --------
1767 fs_init()
1768 */
1769 FS_API void fs_uninit(fs* pFS);
1770
1771
1772 /*
1773 Removes a file or empty directory.
1774
1775 This function will delete a file or an empty directory from the file system. It will consider write
1776 mount points unless the FS_IGNORE_MOUNTS flag is specified in the options parameter in which case
1777 the path will be treated as a real path.
1778
1779 See fs_file_open() for information about the options flags.
1780
1781
1782 Parameters
1783 ----------
1784 pFS : (in, optional)
1785 A pointer to the file system object. Can be NULL to use the native file system.
1786
1787 pFilePath : (in)
1788 The path to the file or directory to remove. Must not be NULL.
1789
1790 options : (in)
1791 Options for the operation. Can be 0 or a combination of the following flags:
1792 FS_IGNORE_MOUNTS
1793 FS_NO_SPECIAL_DIRS
1794 FS_NO_ABOVE_ROOT_NAVIGATION
1795
1796
1797 Return Value
1798 ------------
1799 Returns FS_SUCCESS on success; any other result code otherwise. Returns FS_DOES_NOT_EXIST if the
1800 file or directory does not exist. Returns FS_DIRECTORY_NOT_EMPTY if attempting to remove a
1801 non-empty directory.
1802
1803
1804 See Also
1805 --------
1806 fs_file_open()
1807 */
1808 FS_API fs_result fs_remove(fs* pFS, const char* pFilePath, int options);
1809
1810
1811 /*
1812 Renames or moves a file or directory.
1813
1814 This function will rename or move a file or directory from one location to another. It will
1815 consider write mount points unless the FS_IGNORE_MOUNTS flag is specified in the options parameter
1816 in which case the paths will be treated as real paths.
1817
1818 This will fail with FS_DIFFERENT_DEVICE if the source and destination are on different devices.
1819
1820 See fs_file_open() for information about the options flags.
1821
1822
1823 Parameters
1824 ----------
1825 pFS : (in, optional)
1826 A pointer to the file system object. Can be NULL to use the native file system.
1827
1828 pOldPath : (in)
1829 The current path of the file or directory to rename/move. Must not be NULL.
1830
1831 pNewPath : (in)
1832 The new path for the file or directory. Must not be NULL.
1833
1834 options : (in)
1835 Options for the operation. Can be 0 or a combination of the following flags:
1836 FS_IGNORE_MOUNTS
1837 FS_NO_SPECIAL_DIRS
1838 FS_NO_ABOVE_ROOT_NAVIGATION
1839
1840
1841 Return Value
1842 ------------
1843 Returns FS_SUCCESS on success; any other result code otherwise. Returns FS_DOES_NOT_EXIST if the
1844 source file or directory does not exist. Returns FS_ALREADY_EXISTS if the destination path already
1845 exists.
1846
1847
1848 See Also
1849 --------
1850 fs_file_open()
1851 */
1852 FS_API fs_result fs_rename(fs* pFS, const char* pOldPath, const char* pNewPath, int options);
1853
1854
1855 /*
1856 Creates a directory.
1857
1858 This function creates a directory at the specified path. By default, it will create the entire
1859 directory hierarchy if parent directories do not exist. It will consider write mount points unless
1860 the FS_IGNORE_MOUNTS flag is specified in the options parameter in which case the path will be
1861 treated as a real path.
1862
1863 See fs_file_open() for information about the options flags.
1864
1865
1866 Parameters
1867 ----------
1868 pFS : (in, optional)
1869 A pointer to the file system object. Can be NULL to use the native file system.
1870
1871 pPath : (in)
1872 The path of the directory to create. Must not be NULL.
1873
1874 options : (in)
1875 Options for the operation. Can be 0 or a combination of the following flags:
1876 FS_IGNORE_MOUNTS
1877 FS_NO_CREATE_DIRS
1878
1879
1880 Return Value
1881 ------------
1882 Returns FS_SUCCESS on success; any other result code otherwise. Returns FS_ALREADY_EXISTS if the
1883 directory already exists. Returns FS_DOES_NOT_EXIST if FS_NO_CREATE_DIRS is specified and a
1884 parent directory does not exist.
1885
1886
1887 See Also
1888 --------
1889 fs_file_open()
1890 */
1891 FS_API fs_result fs_mkdir(fs* pFS, const char* pPath, int options);
1892
1893
1894 /*
1895 Retrieves information about a file or directory without opening it.
1896
1897 This function gets information about a file or directory such as its size, modification time,
1898 and whether it is a directory or symbolic link. The openMode parameter accepts the same flags as
1899 fs_file_open() but FS_READ, FS_WRITE, FS_TRUNCATE, FS_APPEND, and FS_EXCLUSIVE are ignored.
1900
1901
1902 Parameters
1903 ----------
1904 pFS : (in, optional)
1905 A pointer to the file system object. Can be NULL to use the native file system.
1906
1907 pPath : (in)
1908 The path to the file or directory to get information about. Must not be NULL.
1909
1910 openMode : (in)
1911 Open mode flags that may affect how the file is accessed. See fs_file_open() for details.
1912
1913 pInfo : (out)
1914 A pointer to a fs_file_info structure that will receive the file information. Must not be NULL.
1915
1916
1917 Return Value
1918 ------------
1919 Returns FS_SUCCESS on success; any other result code otherwise. Returns FS_DOES_NOT_EXIST if the
1920 file or directory does not exist.
1921
1922
1923 See Also
1924 --------
1925 fs_file_get_info()
1926 fs_file_open()
1927 */
1928 FS_API fs_result fs_info(fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo);
1929
1930
1931 /*
1932 Retrieves a pointer to the stream used by the file system object.
1933
1934 This is only relevant if the file system will initialized with a stream (such as when opening an
1935 archive). If the file system was not initialized with a stream, this will return NULL.
1936
1937
1938 Parameters
1939 ----------
1940 pFS : (in)
1941 A pointer to the file system object. Must not be NULL.
1942
1943
1944 Return Value
1945 ------------
1946 Returns a pointer to the stream used by the file system object, or NULL if no stream was provided
1947 at initialization time.
1948 */
1949 FS_API fs_stream* fs_get_stream(fs* pFS);
1950
1951
1952 /*
1953 Retrieves a pointer to the allocation callbacks used by the file system object.
1954
1955 Note that this will *not* return the same pointer that was specified in the config at initialization
1956 time. This function returns a pointer to the internal copy of the struct.
1957
1958
1959 Parameters
1960 ----------
1961 pFS : (in)
1962 A pointer to the file system object. Must not be NULL.
1963
1964
1965 Return Value
1966 ------------
1967 Returns a pointer to the allocation callbacks used by the file system object. If `pFS` is NULL, this
1968 will return NULL.
1969 */
1970 FS_API const fs_allocation_callbacks* fs_get_allocation_callbacks(fs* pFS);
1971
1972
1973 /*
1974 For use only by backend implementations. Retrieves a pointer to the backend-specific data
1975 associated with the file system object.
1976
1977 You should never call this function unless you are implementing a custom backend. The size of the
1978 data can be retrieved with `fs_get_backend_data_size()`.
1979
1980
1981 Parameters
1982 ----------
1983 pFS : (in)
1984 A pointer to the file system object. Must not be NULL.
1985
1986
1987 Return Value
1988 ------------
1989 Returns a pointer to the backend-specific data associated with the file system object, or NULL if no
1990 backend data is available.
1991
1992
1993 See Also
1994 --------
1995 fs_get_backend_data_size()
1996 */
1997 FS_API void* fs_get_backend_data(fs* pFS);
1998
1999
2000 /*
2001 For use only by backend implementations. Retrieves the size of the backend-specific data
2002 associated with the file system object.
2003
2004 You should never call this function unless you are implementing a custom backend. The data can be
2005 accessed with `fs_get_backend_data()`.
2006
2007
2008 Parameters
2009 ----------
2010 pFS : (in)
2011 A pointer to the file system object. Must not be NULL.
2012
2013
2014 Return Value
2015 ------------
2016 Returns the size of the backend-specific data associated with the file system object, or 0 if no
2017 backend data is available.
2018
2019
2020 See Also
2021 --------
2022 fs_get_backend_data()
2023 */
2024 FS_API size_t fs_get_backend_data_size(fs* pFS);
2025
2026
2027 /*
2028 Increments the reference count of the file system object.
2029
2030 This function would be used to prevent garbage collection of opened archives. It should be rare to
2031 ever need to call this function directly.
2032
2033
2034 Parameters
2035 ----------
2036 pFS : (in)
2037 A pointer to the file system object. Must not be NULL.
2038
2039
2040 Return Value
2041 ------------
2042 Returns `pFS`.
2043
2044
2045 See Also
2046 --------
2047 fs_unref()
2048 fs_refcount()
2049 */
2050 FS_API fs* fs_ref(fs* pFS);
2051
2052 /*
2053 Decrements the reference count of the file system object.
2054
2055 This does not uninitialize the object once the reference count hits zero.
2056
2057
2058 Parameters
2059 ----------
2060 pFS : (in)
2061 A pointer to the file system object. Must not be NULL.
2062
2063
2064 Return Value
2065 ------------
2066 Returns the new reference count.
2067
2068
2069 See Also
2070 --------
2071 fs_ref()
2072 fs_refcount()
2073 */
2074 FS_API fs_uint32 fs_unref(fs* pFS);
2075
2076 /*
2077 Retrieves the current reference count of the file system object.
2078
2079
2080 Parameters
2081 ----------
2082 pFS : (in)
2083 A pointer to the file system object. Must not be NULL.
2084
2085
2086 Return Value
2087 ------------
2088 Returns the current reference count of the file system object.
2089
2090
2091 See Also
2092 --------
2093 fs_ref()
2094 fs_unref()
2095 */
2096 FS_API fs_uint32 fs_refcount(fs* pFS);
2097
2098
2099 /*
2100 Opens a file.
2101
2102 If the file path is prefixed with the virtual path of a mount point, this function will first try
2103 opening the file from that mount. If that fails, it will fall back to the native file system and
2104 treat the path as a real path. If the FS_ONLY_MOUNTS flag is specified in the openMode parameter,
2105 the last step of falling back to the native file system will be skipped.
2106
2107 By default, opening a file will transparently look inside archives of known types (registered at
2108 initialization time of the `fs` object). This can slow, and if you would rather not have this
2109 behavior, consider using the `FS_OPAQUE` option (see below).
2110
2111 This function opens a file for reading and/or writing. The openMode parameter specifies how the
2112 file should be opened. It can be a combination of the following flags:
2113
2114 FS_READ
2115 Open the file for reading. If used with `FS_WRITE`, the file will be opened in read/write mode.
2116 When opening in read-only mode, the file must exist.
2117
2118 FS_WRITE
2119 Open the file for writing. If used with `FS_READ`, the file will be opened in read/write mode.
2120 When opening in write-only mode, the file will be created if it does not exist. By default, the
2121 file will be opened in overwrite mode. To change this, combine this with either one of
2122 the `FS_TRUNCATE` or `FS_APPEND` flags.
2123
2124 FS_TRUNCATE
2125 Only valid when used with `FS_WRITE`. If the file already exists, it will be truncated to zero
2126 length when opened. If the file does not exist, it will be created. Not compatible with
2127 `FS_APPEND`.
2128
2129 FS_APPEND
2130 Only valid when used with `FS_WRITE`. All writes will occur at the end of the file, regardless
2131 of the current cursor position. If the file does not exist, it will be created. Not compatible
2132 with `FS_TRUNCATE`.
2133
2134 FS_EXCLUSIVE
2135 Only valid when used with `FS_WRITE`. The file will be created, but if it already exists, the
2136 open will fail with FS_ALREADY_EXISTS.
2137
2138 FS_TRANSPARENT
2139 This is the default behavior. When used, files inside archives can be opened transparently. For
2140 example, "somefolder/archive.zip/file.txt" can be opened with "somefolder/file.txt" (the
2141 "archive.zip" part need not be specified). This assumes the `fs` object has been initialized
2142 with support for the relevant archive types.
2143
2144 Transparent mode is the slowest mode since it requires searching through the file system for
2145 archives, and then open those archives, and then searching through the archive for the file. If
2146 this is prohibitive, consider using `FS_OPAQUE` (fastest) or `FS_VERBOSE` modes instead.
2147 Furthermore, you can consider having a rule in your application that instead of opening files
2148 inside archives from a transparent path, that you instead mount the archive, and then open all
2149 files with `FS_OPAQUE`, but with a virtual path that points to the archive. For example:
2150
2151 fs_mount(pFS, "somefolder/archive.zip", "assets", FS_READ);
2152 fs_file_open(pFS, "assets/file.txt", FS_READ | FS_OPAQUE, &pFile);
2153
2154 Here the archive is mounted to the virtual path "assets". Because the path "assets/file.txt"
2155 is prefixed with "assets", the file system knows to look inside the mounted archive without
2156 having to search for it.
2157
2158 FS_OPAQUE
2159 When used, archives will be treated as opaque, meaning attempting to open a file from an
2160 unmounted archive will fail. For example, "somefolder/archive.zip/file.txt" will fail because
2161 it is inside an archive. This is the fastest mode, but you will not be able to open files from
2162 inside archives unless it is sourced from a mount.
2163
2164 FS_VERBOSE
2165 When used, files inside archives can be opened, but the name of the archive must be specified
2166 explicitly in the path, such as "somefolder/archive.zip/file.txt". This is faster than
2167 `FS_TRANSPARENT` mode since it does not require searching for archives.
2168
2169 FS_NO_CREATE_DIRS
2170 When opening a file in write mode, the default behavior is to create the directory structure
2171 automatically if required. When this options is used, directories will *not* be created
2172 automatically. If the necessary parent directories do not exist, the open will fail with
2173 FS_DOES_NOT_EXIST.
2174
2175 FS_IGNORE_MOUNTS
2176 When used, mounted directories and archives will be ignored when opening files. The path will
2177 be treated as a real path.
2178
2179 FS_ONLY_MOUNTS
2180 When used, only mounted directories and archives will be considered when opening files. When a
2181 file is opened, it will first search through mounts, and if the file is not found in any of
2182 those it will fall back to the native file system and try treating the path as a real path.
2183 When this flag is set, that last step of falling back to the native file system is skipped.
2184
2185 FS_NO_SPECIAL_DIRS
2186 When used, the presence of special directories like "." and ".." in the path will result in an
2187 error. When using this option, you need not specify FS_NO_ABOVE_ROOT_NAVIGATION since it is
2188 implied.
2189
2190 FS_NO_ABOVE_ROOT_NAVIGATION
2191 When used, navigating above the mount point with leading ".." segments will result in an error.
2192 This option is implied when using FS_NO_SPECIAL_DIRS.
2193
2194
2195 Close the file with `fs_file_close()` when no longer needed. The file will not be closed
2196 automatically when the `fs` object is uninitialized.
2197
2198
2199 Parameters
2200 ----------
2201 pFS : (in, optional)
2202 A pointer to the file system object. Can be NULL to use the native file system. Note that when
2203 this is NULL, archives and mounts will not be supported.
2204
2205 pFilePath : (in)
2206 The path to the file to open. Must not be NULL.
2207
2208 openMode : (in)
2209 The mode to open the file with. A combination of the flags described above.
2210
2211 ppFile : (out)
2212 A pointer to a pointer which will receive the opened file object. Must not be NULL.
2213
2214
2215 Return Value
2216 ------------
2217 Returns FS_SUCCESS on success; any other result code otherwise. Returns FS_DOES_NOT_EXIST if the
2218 file does not exist when opening for reading. Returns FS_ALREADY_EXISTS if the file already exists
2219 when opening with FS_EXCLUSIVE. Returns FS_IS_DIRECTORY if the path refers to a directory.
2220
2221
2222 Example 1 - Basic Usage
2223 -----------------------
2224 The example below shows how to open a file for reading from the regular file system.
2225
2226 ```c
2227 fs_result result;
2228 fs_file* pFile;
2229
2230 result = fs_file_open(pFS, "somefolder/somefile.txt", FS_READ, &pFile);
2231 if (result != FS_SUCCESS) {
2232 // Handle error.
2233 }
2234
2235 // Use the file...
2236
2237 // Close the file when no longer needed.
2238 fs_file_close(pFile);
2239 ```
2240
2241 Example 2 - Opening from a Mount
2242 --------------------------------
2243 The example below shows how to mount a directory and then open a file from it. Error checking has
2244 been omitted for clarity.
2245
2246 ```c
2247 // "assets" is the virtual path. FS_READ indicates this is a mount for use when opening a file in
2248 // read-only mode (write mounts would use FS_WRITE).
2249 fs_mount(pFS, "some_actual_path", "assets", FS_READ);
2250
2251 ...
2252
2253 // The file path is prefixed with the virtual path "assets" so the file system will look inside the
2254 // mounted directory first. Since the "assets" virtual path points to "some_actual_path", the file
2255 // that will actually be opened is "some_actual_path/file.txt".
2256 fs_file_open(pFS, "assets/file.txt", FS_READ, &pFile);
2257 ```
2258
2259 Example 3 - Opening from an Archive
2260 -----------------------------------
2261 The example below shows how to open a file from within a ZIP archive. This assumes the `fs` object
2262 was initialized with support for ZIP archives (see fs_init() documentation for more information).
2263
2264 ```c
2265 // Opening the file directly from the archive by specifying the archive name in the path.
2266 fs_file_open(pFS, "somefolder/archive.zip/somefile.txt", FS_READ, &pFile);
2267
2268 // Same as above. The "archive.zip" part is not needed because transparent mode is used by default.
2269 fs_file_open(pFS, "somefolder/somefile.txt", FS_READ, &pFile);
2270
2271 // Opening a file in verbose mode. The archive name must be specified in the path.
2272 fs_file_open(pFS, "somefolder/archive.zip/somefile.txt", FS_READ | FS_VERBOSE, &pFile); // This is a valid path in verbose mode.
2273
2274 // This will fail because the archive name is not specified in the path.
2275 fs_file_open(pFS, "somefolder/somefile.txt", FS_READ | FS_VERBOSE, &pFile); // This will fail because verbose mode requires explicit archive names.
2276
2277 // This will fail because opaque mode treats archives as opaque.
2278 fs_file_open(pFS, "somefolder/archive.zip/somefile.txt", FS_READ | FS_OPAQUE, &pFile);
2279 ```
2280
2281 Example 4 - Opening from a Mounted Archive
2282 ------------------------------------------
2283 It is OK to use opaque mode when opening files from a mounted archive. This is the only way to open
2284 files from an archive when using opaque mode.
2285
2286 ```c
2287 // Mount the archive to the virtual path "assets".
2288 fs_mount(pFS, "somefolder/archive.zip", "assets", FS_READ);
2289
2290 // Now you can open files from the archive in opaque mode. Note how the path is prefixed with the
2291 // virtual path "assets" which is how the mapping back to "somefolder/archive.zip" is made.
2292 fs_file_open(pFS, "assets/somefile.txt", FS_READ | FS_OPAQUE, &pFile);
2293 ```
2294
2295
2296 See Also
2297 --------
2298 fs_file_close()
2299 fs_file_read()
2300 fs_file_write()
2301 fs_file_seek()
2302 fs_file_tell()
2303 fs_file_flush()
2304 fs_file_truncate()
2305 fs_file_get_info()
2306 fs_file_duplicate()
2307 */
2308 FS_API fs_result fs_file_open(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile);
2309
2310
2311 /*
2312 Closes a file.
2313
2314 You must close any opened files with this function when they are no longer needed. The owner `fs`
2315 object will not close files automatically when it is uninitialized with `fs_uninit()`.
2316
2317
2318 Parameters
2319 ----------
2320 pFile : (in)
2321 A pointer to the file to close. Must not be NULL.
2322
2323
2324 See Also
2325 --------
2326 fs_file_open()
2327 */
2328 FS_API void fs_file_close(fs_file* pFile);
2329
2330
2331 /*
2332 Reads data from a file.
2333
2334 This function reads up to `bytesToRead` bytes from the file into the buffer pointed to by `pDst`.
2335 The number of bytes actually read will be stored in the variable pointed to by `pBytesRead`.
2336
2337 If the end of the file is reached before any bytes are read, this function will return `FS_AT_END`
2338 and `*pBytesRead` will be set to 0. `FS_AT_END` will only be returned if `*pBytesRead` is 0.
2339
2340
2341 Parameters
2342 ----------
2343 pFile : (in)
2344 A pointer to the file to read from. Must not be NULL.
2345
2346 pDst : (out)
2347 A pointer to the buffer that will receive the read data. Must not be NULL.
2348
2349 bytesToRead : (in)
2350 The maximum number of bytes to read from the file.
2351
2352 pBytesRead : (out, optional)
2353 A pointer to a variable that will receive the number of bytes actually read. Can be NULL if you
2354 do not care about this information. If NULL, the function will return an error if not all
2355 requested bytes could be read.
2356
2357
2358 Return Value
2359 ------------
2360 Returns `FS_SUCCESS` on success, `FS_AT_END` on end of file, or an error code otherwise. Will only
2361 return `FS_AT_END` if `*pBytesRead` is 0.
2362
2363 If `pBytesRead` is NULL, the function will return an error if not all requested bytes could be
2364 read. Otherwise, if `pBytesRead` is not NULL, the function will return `FS_SUCCESS` even if fewer
2365 than `bytesToRead` bytes were read.
2366
2367
2368 See Also
2369 --------
2370 fs_file_open()
2371 fs_file_write()
2372 fs_file_seek()
2373 fs_file_tell()
2374 */
2375 FS_API fs_result fs_file_read(fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead); /* Returns 0 on success, FS_AT_END on end of file, or an errno result code on error. Will only return FS_AT_END if *pBytesRead is 0. */
2376
2377
2378 /*
2379 Writes data to a file.
2380
2381 This function writes up to `bytesToWrite` bytes from the buffer pointed to by `pSrc` to the file.
2382 The number of bytes actually written will be stored in the variable pointed to by `pBytesWritten`.
2383
2384
2385 Parameters
2386 ----------
2387 pFile : (in)
2388 A pointer to the file to write to. Must not be NULL.
2389
2390 pSrc : (in)
2391 A pointer to the buffer containing the data to write. Must not be NULL.
2392
2393 bytesToWrite : (in)
2394 The number of bytes to write to the file.
2395
2396 pBytesWritten : (out, optional)
2397 A pointer to a variable that will receive the number of bytes actually written. Can be NULL if
2398 you do not care about this information. If NULL, the function will return an error if not all
2399 requested bytes could be written.
2400
2401
2402 Return Value
2403 ------------
2404 Returns `FS_SUCCESS` on success, or an error code otherwise.
2405
2406 If `pBytesWritten` is NULL, the function will return an error if not all requested bytes could be
2407 written. Otherwise, if `pBytesWritten` is not NULL, the function will return `FS_SUCCESS` even if
2408 fewer than `bytesToWrite` bytes were written.
2409
2410
2411 See Also
2412 --------
2413 fs_file_open()
2414 fs_file_read()
2415 fs_file_seek()
2416 fs_file_tell()
2417 fs_file_flush()
2418 fs_file_truncate()
2419 */
2420 FS_API fs_result fs_file_write(fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten);
2421
2422 /*
2423 A helper for writing formatted data to a file.
2424
2425
2426 Parameters
2427 ----------
2428 pFile : (in)
2429 A pointer to the file to write to. Must not be NULL.
2430
2431 fmt : (in)
2432 A printf-style format string. Must not be NULL.
2433
2434 ... : (in)
2435 Additional arguments as required by the format string.
2436
2437
2438 Return Value
2439 ------------
2440 Same as `fs_file_write()`.
2441
2442
2443 See Also
2444 --------
2445 fs_file_write()
2446 fs_file_writefv()
2447 */
2448 FS_API fs_result fs_file_writef(fs_file* pFile, const char* fmt, ...) FS_ATTRIBUTE_FORMAT(2, 3);
2449
2450 /*
2451 A helper for writing formatted data to a file.
2452
2453
2454 Parameters
2455 ----------
2456 pFile : (in)
2457 A pointer to the file to write to. Must not be NULL.
2458
2459 fmt : (in)
2460 A printf-style format string. Must not be NULL.
2461
2462 args : (in)
2463 Additional arguments as required by the format string.
2464
2465
2466 Return Value
2467 ------------
2468 Same as `fs_file_write()`.
2469
2470
2471 See Also
2472 --------
2473 fs_file_write()
2474 fs_file_writef()
2475 */
2476 FS_API fs_result fs_file_writefv(fs_file* pFile, const char* fmt, va_list args);
2477
2478 /*
2479 Moves the read/write cursor of a file.
2480
2481 You can seek relative to the start of the file, the current cursor position, or the end of the file.
2482 A negative offset seeks backwards.
2483
2484 It is not an error to seek beyond the end of the file. If you seek beyond the end of the file and
2485 then write, the exact behavior depends on the backend. On POSIX systems, it will most likely result
2486 in a sparse file. In read mode, attempting to read beyond the end of the file will simply result
2487 in zero bytes being read, and `FS_AT_END` being returned by `fs_file_read()`.
2488
2489 It is an error to try seeking to before the start of the file.
2490
2491
2492 Parameters
2493 ----------
2494 pFile : (in)
2495 A pointer to the file to seek. Must not be NULL.
2496
2497 offset : (in)
2498 The offset to seek to, relative to the position specified by `origin`. A negative value seeks
2499 backwards.
2500
2501 origin : (in)
2502 The origin from which to seek. One of the following values:
2503 FS_SEEK_SET
2504 Seek from the start of the file.
2505
2506 FS_SEEK_CUR
2507 Seek from the current cursor position.
2508
2509 FS_SEEK_END
2510 Seek from the end of the file.
2511
2512
2513 Return Value
2514 ------------
2515 Returns FS_SUCCESS on success; any other result code otherwise.
2516
2517
2518 See Also
2519 --------
2520 fs_file_tell()
2521 fs_file_read()
2522 fs_file_write()
2523 */
2524 FS_API fs_result fs_file_seek(fs_file* pFile, fs_int64 offset, fs_seek_origin origin);
2525
2526 /*
2527 Retrieves the current position of the read/write cursor in a file.
2528
2529
2530 Parameters
2531 ----------
2532 pFile : (in)
2533 A pointer to the file to query. Must not be NULL.
2534
2535 pCursor : (out)
2536 A pointer to a variable that will receive the current cursor position. Must not be NULL.
2537
2538
2539 Return Value
2540 ------------
2541 Returns FS_SUCCESS on success; any other result code otherwise.
2542
2543
2544 See Also
2545 --------
2546 fs_file_seek()
2547 fs_file_read()
2548 fs_file_write()
2549 */
2550 FS_API fs_result fs_file_tell(fs_file* pFile, fs_int64* pCursor);
2551
2552
2553 /*
2554 Flushes any buffered data to disk.
2555
2556
2557 Parameters
2558 ----------
2559 pFile : (in)
2560 A pointer to the file to flush. Must not be NULL.
2561
2562
2563 Return Value
2564 ------------
2565 Returns FS_SUCCESS on success; any other result code otherwise.
2566 */
2567 FS_API fs_result fs_file_flush(fs_file* pFile);
2568
2569
2570 /*
2571 Truncates a file to the current cursor position.
2572
2573 It is possible for a backend to not support truncation, in which case this function will return
2574 `FS_NOT_IMPLEMENTED`.
2575
2576
2577 Parameters
2578 ----------
2579 pFile : (in)
2580 A pointer to the file to truncate. Must not be NULL.
2581
2582
2583 Return Value
2584 ------------
2585 Returns FS_SUCCESS on success; any other result code otherwise. Will return `FS_NOT_IMPLEMENTED` if
2586 the backend does not support truncation.
2587 */
2588 FS_API fs_result fs_file_truncate(fs_file* pFile);
2589
2590
2591 /*
2592 Retrieves information about an opened file.
2593
2594
2595 Parameters
2596 ----------
2597 pFile : (in)
2598 A pointer to the file to query. Must not be NULL.
2599
2600 pInfo : (out)
2601 A pointer to a fs_file_info structure that will receive the file information. Must not be NULL.
2602
2603
2604 Return Value
2605 ------------
2606 Returns FS_SUCCESS on success; any other result code otherwise.
2607 */
2608 FS_API fs_result fs_file_get_info(fs_file* pFile, fs_file_info* pInfo);
2609
2610
2611 /*
2612 Duplicates a file handle.
2613
2614 This creates a new file handle that refers to the same underlying file as the original. The new
2615 file handle will have its own independent cursor position. The initial position of the new file's
2616 cursor will be undefined. You should call `fs_file_seek()` to set it to a known position before
2617 using it.
2618
2619 Note that this does not duplicate the actual file on the file system itself. It just creates a
2620 new `fs_file` object that refers to the same file.
2621
2622
2623 Parameters
2624 ----------
2625 pFile : (in)
2626 A pointer to the file to duplicate. Must not be NULL.
2627
2628 ppDuplicate : (out)
2629 A pointer to a pointer which will receive the duplicated file handle. Must not be NULL.
2630
2631
2632 Return Value
2633 ------------
2634 Returns FS_SUCCESS on success; any other result code otherwise.
2635 */
2636 FS_API fs_result fs_file_duplicate(fs_file* pFile, fs_file** ppDuplicate);
2637
2638 /*
2639 Retrieves the backend-specific data associated with a file.
2640
2641 You should never call this function unless you are implementing a custom backend. The size of the
2642 data can be retrieved with `fs_file_get_backend_data_size()`.
2643
2644
2645 Parameters
2646 ----------
2647 pFile : (in)
2648 A pointer to the file to query. Must not be NULL.
2649
2650
2651 Return Value
2652 ------------
2653 Returns a pointer to the backend-specific data associated with the file, or NULL if there is no
2654 such data.
2655
2656
2657 See Also
2658 --------
2659 fs_file_get_backend_data_size()
2660 */
2661 FS_API void* fs_file_get_backend_data(fs_file* pFile);
2662
2663 /*
2664 Retrieves the size of the backend-specific data associated with a file.
2665
2666 You should never call this function unless you are implementing a custom backend. The data can be
2667 accessed with `fs_file_get_backend_data()`.
2668
2669
2670 Parameters
2671 ----------
2672 pFile : (in)
2673 A pointer to the file to query. Must not be NULL.
2674
2675
2676 Return Value
2677 ------------
2678 Returns the size of the backend-specific data associated with the file, or 0 if there is no such
2679 data.
2680
2681
2682 See Also
2683 --------
2684 fs_file_get_backend_data()
2685 */
2686 FS_API size_t fs_file_get_backend_data_size(fs_file* pFile);
2687
2688 /*
2689 Files are streams. This function returns a pointer to the `fs_stream` interface of the file.
2690
2691
2692 Parameters
2693 ----------
2694 pFile : (in)
2695 A pointer to the file whose stream pointer is being retrieved. Must not be NULL.
2696
2697
2698 Return Value
2699 ------------
2700 Returns a pointer to the `fs_stream` interface of the file, or NULL if `pFile` is NULL.
2701
2702
2703 See Also
2704 --------
2705 fs_file_get_fs()
2706 */
2707 FS_API fs_stream* fs_file_get_stream(fs_file* pFile);
2708
2709 /*
2710 Retrieves the file system that owns a file.
2711
2712
2713 Parameters
2714 ----------
2715 pFile : (in)
2716 A pointer to the file whose file system pointer is being retrieved. Must not be NULL.
2717
2718
2719 Return Value
2720 ------------
2721 Returns a pointer to the `fs` interface of the file's file system, or NULL if `pFile` is NULL.
2722
2723
2724 See Also
2725 --------
2726 fs_file_get_stream()
2727 */
2728 FS_API fs* fs_file_get_fs(fs_file* pFile);
2729
2730
2731 /*
2732 The same as `fs_first()`, but with the length of the directory path specified explicitly.
2733
2734
2735 Parameters
2736 ----------
2737 pFS : (in, optional)
2738 A pointer to the file system object. This can be NULL in which case the native file system will
2739 be used.
2740
2741 pDirectoryPath : (in)
2742 The path to the directory to iterate. Must not be NULL.
2743
2744 directoryPathLen : (in)
2745 The length of the directory path. Can be set to `FS_NULL_TERMINATED` if the path is
2746 null-terminated.
2747
2748 mode : (in)
2749 Options for the iterator. See `fs_file_open()` for a description of the available flags.
2750
2751
2752 Return Value
2753 ------------
2754 Same as `fs_first()`.
2755 */
2756 FS_API fs_iterator* fs_first_ex(fs* pFS, const char* pDirectoryPath, size_t directoryPathLen, int mode);
2757
2758 /*
2759 Creates an iterator for the first entry in a directory.
2760
2761 This function creates an iterator that can be used to iterate over the entries in a directory. This
2762 will be the first function called when iterating over the files inside a directory.
2763
2764 To get the next entry in the directory, call `fs_next()`. When `fs_next()` returns NULL, there are
2765 no more entries in the directory. If you want to end iteration early, use `fs_free_iterator()` to
2766 free the iterator.
2767
2768 See `fs_file_open()` for a description of the available flags that can be used in the `mode`
2769 parameter. When `FS_WRITE` is specified, it will look at write mounts. Otherwise, it will look at
2770 read mounts.
2771
2772
2773 Parameter
2774 ---------
2775 pFS : (in, optional)
2776 A pointer to the file system object. This can be NULL in which case the native file system will
2777 be used.
2778
2779 pDirectoryPath : (in)
2780 The path to the directory to iterate. Must not be NULL.
2781
2782 mode : (in)
2783 Options for the iterator. See `fs_file_open()` for a description of the available flags.
2784
2785
2786 Return Value
2787 ------------
2788 Returns a pointer to an iterator object on success; NULL on failure or if the directory is empty.
2789
2790
2791 Example
2792 -------
2793 The example below shows how to iterate over all entries in a directory. Error checking has been
2794 omitted for clarity.
2795
2796 ```c
2797 fs_iterator* pIterator = fs_first(pFS, "somefolder", FS_READ);
2798 while (pIterator != NULL) {
2799 // Use pIterator->name and pIterator->info here...
2800 printf("Found entry: %.*s\n", (int)pIterator->nameLength, pIterator->pName);
2801 pIterator = fs_next(pIterator);
2802 }
2803
2804 fs_free_iterator(pIterator); // This is only needed if you want to terminate iteration early. If `fs_next()` returns NULL, you need not call this.
2805 ```
2806
2807 See Also
2808 --------
2809 fs_next()
2810 fs_free_iterator()
2811 fs_first_ex()
2812 */
2813 FS_API fs_iterator* fs_first(fs* pFS, const char* pDirectoryPath, int mode);
2814
2815 /*
2816 Gets the next entry in a directory iteration.
2817
2818 This function is used to get the next entry in a directory iteration. It should be called after
2819 `fs_first()` or `fs_first_ex()` to retrieve the first entry, and then subsequently called to
2820 retrieve each following entry.
2821
2822 If there are no more entries in the directory, this function will return NULL, and an explicit call
2823 to `fs_free_iterator()` is not needed.
2824
2825
2826 Parameters
2827 ----------
2828 pIterator : (in)
2829 A pointer to the iterator object. Must not be NULL.
2830
2831
2832 Return Value
2833 ------------
2834 Returns a pointer to the next iterator object on success; NULL if there are no more entries. If
2835 NULL is returned, you need not call `fs_free_iterator()`. If you want to terminate iteration early,
2836 you must call `fs_free_iterator()` to free the iterator.
2837
2838 You cannot assume that the returned pointer is the same as the input pointer. It may need to be
2839 reallocated internally to hold the data of the next entry.
2840
2841
2842 See Also
2843 --------
2844 fs_first()
2845 fs_first_ex()
2846 fs_free_iterator()
2847 */
2848 FS_API fs_iterator* fs_next(fs_iterator* pIterator);
2849
2850 /*
2851 Frees an iterator object.
2852
2853 This function frees an iterator object that was created by `fs_first()` or `fs_first_ex()`. You
2854 need not call this if `fs_next()` returned NULL from an earlier iteration. However, if you want to
2855 terminate iteration early, you must call this function to free the iterator.
2856
2857 It is safe to call this function with a NULL pointer, in which case it will do nothing.
2858
2859
2860 Parameters
2861 ----------
2862 pIterator : (in, optional)
2863 A pointer to the iterator object. Can be NULL.
2864
2865
2866 See Also
2867 --------
2868 fs_first()
2869 fs_first_ex()
2870 fs_next()
2871 */
2872 FS_API void fs_free_iterator(fs_iterator* pIterator);
2873
2874
2875 /*
2876 The same as `fs_open_archive()`, but with the ability to explicitly specify the backend to use.
2877
2878
2879 Parameters
2880 ----------
2881 pFS : (in)
2882 A pointer to the file system object. Must not be NULL.
2883
2884 pBackend : (in)
2885 A pointer to the backend to use for opening the archive. Must not be NULL.
2886
2887 pBackendConfig : (in, optional)
2888 A pointer to backend-specific configuration data. Can be NULL if the backend does not require
2889 any configuration.
2890
2891 pArchivePath : (in)
2892 The path to the archive to open. Must not be NULL.
2893
2894 archivePathLen : (in)
2895 The length of the archive path. Can be set to `FS_NULL_TERMINATED` if the path is null-terminated.
2896
2897 openMode : (in)
2898 The mode to open the archive with.
2899
2900 ppArchive : (out)
2901 A pointer to a pointer which will receive the opened archive file system object. Must not be
2902 NULL.
2903
2904
2905 Return Value
2906 ------------
2907 Returns FS_SUCCESS on success; any other result code otherwise.
2908
2909
2910 See Also
2911 --------
2912 fs_open_archive()
2913 fs_close_archive()
2914 */
2915 FS_API fs_result fs_open_archive_ex(fs* pFS, const fs_backend* pBackend, const void* pBackendConfig, const char* pArchivePath, size_t archivePathLen, int openMode, fs** ppArchive);
2916
2917 /*
2918 Helper function for initializing a file system object for an archive, such as a ZIP file.
2919
2920 To uninitialize the archive, you must use `fs_close_archive()`. Do not use `fs_uninit()` to
2921 uninitialize an archive. The reason for this is that archives opened in this way are garbage
2922 collected, and there are reference counting implications.
2923
2924 Note that opening the archive in write mode (`FS_WRITE`) does not automatically mean you will be
2925 able to write to it. None of the stock backends support writing to archives at this time.
2926
2927
2928 Parameters
2929 ----------
2930 pFS : (in)
2931 A pointer to the file system object. Must not be NULL.
2932
2933 pArchivePath : (in)
2934 The path to the archive to open. Must not be NULL.
2935
2936 openMode : (in)
2937 The open mode flags to open the archive with. See `fs_file_open()` for a description of the
2938 available flags.
2939
2940 ppArchive : (out)
2941 A pointer to a pointer which will receive the opened archive file system object. Must not be
2942 NULL.
2943
2944
2945 Return Value
2946 ------------
2947 Returns FS_SUCCESS on success; any other result code otherwise.
2948
2949
2950 See Also
2951 --------
2952 fs_close_archive()
2953 fs_open_archive_ex()
2954 */
2955 FS_API fs_result fs_open_archive(fs* pFS, const char* pArchivePath, int openMode, fs** ppArchive);
2956
2957
2958 /*
2959 Closes an archive that was previously opened with `fs_open_archive()`.
2960
2961 You must use this function to close an archive opened with `fs_open_archive()`. Do not use
2962 `fs_uninit()` to uninitialize an archive.
2963
2964 Note that when an archive is closed, it does not necessarily mean that the underlying file is
2965 closed immediately. This is because archives are reference counted and garbage collected. You can
2966 force garbage collection of unused archives with `fs_gc_archives()`.
2967
2968
2969 Parameters
2970 ----------
2971 pArchive : (in)
2972 A pointer to the archive file system object to close. Must not be NULL.
2973
2974
2975 See Also
2976 --------
2977 fs_open_archive()
2978 fs_open_archive_ex()
2979 */
2980 FS_API void fs_close_archive(fs* pArchive);
2981
2982
2983 /*
2984 Garbage collects unused archives.
2985
2986 This function will close any opened archives that are no longer in use depending on the specified
2987 policy.
2988
2989 You should rarely need to call this function directly. Archives will automatically be garbage collected
2990 when the `fs` object is uninitialized with `fs_uninit()`.
2991
2992
2993 Parameters
2994 ----------
2995 pFS : (in)
2996 A pointer to the file system object. Must not be NULL.
2997
2998 policy : (in)
2999 The garbage collection policy to use. Set this to FS_GC_POLICY_THRESHOLD to only collect archives
3000 if the number of opened archives exceeds the threshold set with `fs_set_archive_gc_threshold()`
3001 which defaults to 10. Set this to FS_GC_POLICY_ALL to collect all unused archives regardless of the
3002 threshold.
3003
3004
3005 See Also
3006 --------
3007 fs_open_archive()
3008 fs_close_archive()
3009 fs_set_archive_gc_threshold()
3010 */
3011 FS_API void fs_gc_archives(fs* pFS, int policy);
3012
3013 /*
3014 Sets the threshold for garbage collecting unused archives.
3015
3016 When an archive is no longer in use (its reference count drops to zero), it will not be closed
3017 immediately. Instead, it will be kept open in case it is needed again soon. The threshold is what
3018 determines how many unused archives will be kept open before they are garbage collected. The
3019 default threshold is 10.
3020
3021
3022 Parameters
3023 ----------
3024 pFS : (in)
3025 A pointer to the file system object. Must not be NULL.
3026
3027 threshold : (in)
3028 The threshold for garbage collecting unused archives.
3029
3030
3031 See Also
3032 --------
3033 fs_gc_archives()
3034 */
3035 FS_API void fs_set_archive_gc_threshold(fs* pFS, size_t threshold);
3036
3037 /*
3038 Retrieves the threshold for garbage collecting unused archives.
3039
3040
3041 Parameters
3042 ----------
3043 pFS : (in)
3044 A pointer to the file system object. Must not be NULL.
3045
3046
3047 Return Value
3048 ------------
3049 Returns the threshold for garbage collecting unused archives.
3050 */
3051 FS_API size_t fs_get_archive_gc_threshold(fs* pFS);
3052
3053 /*
3054 A helper function for checking if a path looks like it could be an archive.
3055
3056 This only checks the path string itself. It does not actually attempt to open and validate the
3057 archive itself.
3058
3059
3060 Parameters
3061 ----------
3062 pFS : (in)
3063 A pointer to the file system object. Must not be NULL.
3064
3065 pPath : (in)
3066 The path to check. Must not be NULL.
3067
3068 pathLen : (in)
3069 The length of the path string. Can be set to `FS_NULL_TERMINATED` if the path is null-terminated.
3070
3071 Return Value
3072 ------------
3073 Returns FS_TRUE if the path looks like an archive, FS_FALSE otherwise.
3074 */
3075 FS_API fs_bool32 fs_path_looks_like_archive(fs* pFS, const char* pPath, size_t pathLen); /* Does not validate that it's an actual valid archive. */
3076
3077
3078 /*
3079 Mounts a real directory or archive to a virtual path.
3080
3081 You must specify the actual path to the directory or archive on the file system referred to by
3082 `pFS`. The virtual path can be NULL, in which case it will be treated as an empty string.
3083
3084 The virtual path is the path prefix that will be used when opening files. For example, if you mount
3085 the actual path "somefolder" to the virtual path "assets", then when you open a file with the path
3086 "assets/somefile.txt", it will actually open "somefolder/somefile.txt".
3087
3088 There are two groups of mounts - read-only and write. Read-only mounts are used when opening a file
3089 in read-only mode (i.e. without the `FS_WRITE` flag). Write mounts are used when opening a file in
3090 write mode (i.e. with the `FS_WRITE` flag). To control this, set the appropriate flag in the
3091 `options` parameter.
3092
3093 The following flags are supported in the `options` parameter:
3094
3095 FS_READ
3096 This is a read-only mount. It will be used when opening files without the `FS_WRITE` flag.
3097
3098 FS_WRITE
3099 This is a write mount. It will be used when opening files with the `FS_WRITE
3100
3101 FS_LOWEST_PRIORITY
3102 By default, mounts are searched in the reverse order that they were added. This means that
3103 the most recently added mount has the highest priority. When this flag is specified, the
3104 mount will have the lowest priority instead.
3105
3106 For a read-only mount, you can have multiple mounts with the same virtual path in which case they
3107 will be searched in order or priority when opening a file.
3108
3109 For write mounts, you can have multiple mounts with the same virtual path, but when opening a file
3110 for writing, only the first matching mount will be used. You can have multiple write mounts where
3111 the virtual path is a sub-path of another write mount. For example, you could have one write
3112 mount with the virtual path "assets" and another with the virtual path "assets/images". When
3113 opening a file for writing, if the path starts with "assets/images", that mount will be used
3114 because it is a more specific match. Otherwise, if the path starts with "assets" but not
3115 "assets/images", the other mount will be used.
3116
3117 You can specify both `FS_READ` and `FS_WRITE` in the `options` parameter to create one read-only
3118 mount, and one write mount in a single call. This is equivalent to calling `fs_mount()` twice -
3119 once with `FS_READ`, and again with `FS_WRITE`.
3120
3121 Unmounting a directory or archive is done with `fs_unmount()`. You must specify the actual path
3122 when unmounting.
3123
3124
3125 Parameters
3126 ----------
3127 pFS : (in)
3128 A pointer to the file system object. Must not be NULL.
3129
3130 pActualPath : (in)
3131 The actual path to the directory or archive to mount. Must not be NULL.
3132
3133 pVirtualPath : (in, optional)
3134 The virtual path to mount the directory or archive to. Can be NULL in which case it will be
3135 treated as an empty string.
3136
3137 options : (in)
3138 Options for the mount. A combination of the flags described above.
3139
3140
3141 Return Value
3142 ------------
3143 Returns `FS_SUCCESS` on success; any other result code otherwise. If an identical mount already exists,
3144 `FS_SUCCESS` will be returned.
3145
3146
3147 Example 1 - Basic Usage
3148 -----------------------
3149 ```c
3150 // Mount two directories to the same virtual path.
3151 fs_mount(pFS, "some/actual/path", "assets", FS_READ); // Lowest priority.
3152 fs_mount(pFS, "some/other/path", "assets", FS_READ);
3153
3154 // Mount a directory for writing.
3155 fs_mount(pFS, "some/write/path", "assets", FS_WRITE);
3156 fs_mount(pFS, "some/more/write/path", "assets/images", FS_WRITE); // More specific write mount.
3157
3158 // Mount a read-only mount, and a write mount in a single call.
3159 fs_mount(pFS, "some/actual/path", "assets", FS_READ | FS_WRITE);
3160 ```
3161
3162
3163 Example 2 - Mounting an Archive
3164 -------------------------------
3165 ```c
3166 // Mount a ZIP archive to the virtual path "assets".
3167 fs_mount(pFS, "some/actual/path/archive.zip", "assets", FS_READ);
3168 ```
3169
3170
3171 See Also
3172 --------
3173 fs_unmount()
3174 fs_mount_sysdir()
3175 fs_mount_fs()
3176 */
3177 FS_API fs_result fs_mount(fs* pFS, const char* pActualPath, const char* pVirtualPath, int options);
3178
3179 /*
3180 Unmounts a directory or archive that was previously mounted with `fs_mount()`.
3181
3182 You must specify the actual path to the directory or archive that was used when mounting. The
3183 virtual path is not needed.
3184
3185 The only options that matter here are `FS_READ` and `FS_WRITE`. If you want to unmount a read-only
3186 mount, you must specify `FS_READ`. If you want to unmount a write mount, you must specify
3187 `FS_WRITE`. If you want to unmount both a read-only mount, and a write mount in a single call, you
3188 can specify both flags. Using both flags is the same as calling `fs_unmount()` twice - once for the
3189 read-only mount, and once for the write mount.
3190
3191
3192 Parameters
3193 ----------
3194 pFS : (in)
3195 A pointer to the file system object. Must not be NULL.
3196
3197 pActualPath : (in)
3198 The actual path to the directory or archive to unmount. Must not be NULL.
3199
3200 options : (in)
3201 Either `FS_READ`, `FS_WRITE`, or both to unmount the corresponding mounts.
3202
3203
3204 Return Value
3205 ------------
3206 Returns `FS_SUCCESS` on success; any other result code otherwise. If no matching mount could be
3207 found, `FS_SUCCESS` will be returned (it will just be a no-op).
3208 */
3209 FS_API fs_result fs_unmount(fs* pFS, const char* pActualPath, int options);
3210
3211 /*
3212 A helper function for mounting a standard system directory to a virtual path.
3213
3214 When calling this function you specify the type of system directory you want to mount. The actual
3215 path of the system directory will often be generic, like "/home/yourname/" which is not useful for
3216 a real program. For this reason, this function forces you to specify a sub-directory that will be
3217 used with the system directory. This would often be something like the name of your application,
3218 such as "myapp". It can also include sub-directories, such as "mycompany/myapp".
3219
3220 Otherwise, this function behaves exactly like `fs_mount()`.
3221
3222 Unmount the directory with `fs_unmount_sysdir()`. You must specify the same type and sub-directory
3223 that was used when mounting.
3224
3225
3226 Parameters
3227 ----------
3228 pFS : (in)
3229 A pointer to the file system object. Must not be NULL.
3230
3231 type : (in)
3232 The type of system directory to mount.
3233
3234 pSubDir : (in)
3235 The sub-directory to use with the system directory. Must not be NULL nor an empty string.
3236
3237 pVirtualPath : (in, optional)
3238 The virtual path to mount the system directory to. Can be NULL in which case it will be treated
3239 as an empty string.
3240
3241 options : (in)
3242 Options for the mount. A combination of the flags described in `fs_mount()`.
3243
3244
3245 Return Value
3246 ------------
3247 Returns `FS_SUCCESS` on success; any other result code otherwise. If an identical mount already
3248 exists, `FS_SUCCESS` will be returned.
3249
3250
3251 See Also
3252 --------
3253 fs_mount()
3254 fs_unmount_sysdir()
3255 */
3256 FS_API fs_result fs_mount_sysdir(fs* pFS, fs_sysdir_type type, const char* pSubDir, const char* pVirtualPath, int options);
3257
3258 /*
3259 Unmounts a system directory that was previously mounted with `fs_mount_sysdir()`.
3260
3261 This is the same as `fs_unmount()`, but follows the "type" and sub-directory semantics of
3262 `fs_mount_sysdir()`.
3263
3264
3265 Parameters
3266 ----------
3267 pFS : (in)
3268 A pointer to the file system object. Must not be NULL.
3269
3270 type : (in)
3271 The type of system directory to unmount.
3272
3273 pSubDir : (in)
3274 The sub-directory that was used with the system directory when mounting. Must not be NULL nor
3275 an empty string.
3276
3277 options : (in)
3278 Either `FS_READ`, `FS_WRITE`, or both to unmount the corresponding mounts.
3279
3280
3281 Return Value
3282 ------------
3283 Returns `FS_SUCCESS` on success; any other result code otherwise. If no matching mount could be
3284 found, `FS_SUCCESS` will be returned (it will just be a no-op).
3285 */
3286 FS_API fs_result fs_unmount_sysdir(fs* pFS, fs_sysdir_type type, const char* pSubDir, int options);
3287
3288 /*
3289 Mounts another `fs` object to a virtual path.
3290
3291 This is the same as `fs_mount()`, but instead of specifying an actual path to a directory or
3292 archive, you specify another `fs` object.
3293
3294 Use `fs_unmount_fs()` to unmount the file system.
3295
3296
3297 Parameters
3298 ----------
3299 pFS : (in)
3300 A pointer to the file system object. Must not be NULL.
3301
3302 pOtherFS : (in)
3303 A pointer to the other file system object to mount. Must not be NULL.
3304
3305 pVirtualPath : (in, optional)
3306 The virtual path to mount the other file system to. Can be NULL in which case it will be treated
3307 as an empty string.
3308
3309 options : (in)
3310 Options for the mount. A combination of the flags described in `fs_mount()`.
3311
3312
3313 Return Value
3314 ------------
3315 Returns `FS_SUCCESS` on success; any other result code otherwise. If an identical mount already
3316 exists, `FS_SUCCESS` will be returned.
3317
3318
3319 See Also
3320 --------
3321 fs_mount()
3322 fs_unmount_fs()
3323 */
3324 FS_API fs_result fs_mount_fs(fs* pFS, fs* pOtherFS, const char* pVirtualPath, int options);
3325
3326 /*
3327 Unmounts a file system that was previously mounted with `fs_mount_fs()`.
3328
3329
3330 Parameters
3331 ----------
3332 pFS : (in)
3333 A pointer to the file system object. Must not be NULL.
3334
3335 pOtherFS : (in)
3336 A pointer to the other file system object to unmount. Must not be NULL.
3337
3338 options : (in)
3339 Options for the unmount. A combination of the flags described in `fs_unmount()`.
3340
3341
3342 Return Value
3343 ------------
3344 Returns `FS_SUCCESS` on success; any other result code otherwise. If no matching mount could be
3345 found, `FS_SUCCESS` will be returned (it will just be a no-op).
3346
3347
3348 See Also
3349 --------
3350 fs_unmount()
3351 fs_mount_fs()
3352 */
3353 FS_API fs_result fs_unmount_fs(fs* pFS, fs* pOtherFS, int options);
3354
3355
3356 /*
3357 Helper functions for reading the entire contents of a file, starting from the current cursor position. Free
3358 the returned pointer with fs_free(), using the same allocation callbacks as the fs object. You can use
3359 fs_get_allocation_callbacks() if necessary, like so:
3360
3361 fs_free(pFileData, fs_get_allocation_callbacks(pFS));
3362
3363 The format (FS_FORMAT_TEXT or FS_FORMAT_BINARY) is used to determine whether or not a null terminator should be
3364 appended to the end of the data.
3365
3366 For flexiblity in case the backend does not support cursor retrieval or positioning, the data will be read
3367 in fixed sized chunks.
3368 */
3369 FS_API fs_result fs_file_read_to_end(fs_file* pFile, fs_format format, void** ppData, size_t* pDataSize);
3370 FS_API fs_result fs_file_open_and_read(fs* pFS, const char* pFilePath, fs_format format, void** ppData, size_t* pDataSize);
3371 FS_API fs_result fs_file_open_and_write(fs* pFS, const char* pFilePath, const void* pData, size_t dataSize);
3372
3373
3374 /*
3375 Serializes a file system subdirectory to a stream.
3376
3377 This function recursively serializes all files and directories within the specified directory
3378 to a binary stream. The serialized data can later be restored using `fs_deserialize()`.
3379
3380 The directory parameter specifies which directory to serialize. This function is built on top of
3381 standard file iteration functions, i.e. `fs_first()`, `fs_next()`. The directory and options all
3382 work the same way as they do for `fs_first()`.
3383
3384 It is possible for serialization to fail partway through, in which case the stream will contain
3385 partial data. It is the caller's responsibility to handle this case appropriately.
3386
3387 When calculating offsets, it will use `fs_stream_tell()` to get the current position in the stream.
3388 Keep this in mind if you are appending this to the end of an existing stream. This may or may not
3389 be useful to you depending on your use case.
3390
3391 The format is designed to be appendable to existing payloads using tools like `cat`. Offsets are
3392 stored relative to the end of the archive to support this use case. The Base Offset field in the
3393 tail specifies where file data begins relative to the end of the stream, and will always be a
3394 negative number. Each file's offset is then relative to this base offset. To seek to a file's data,
3395 you would add the file's offset to the base offset and then seek by that amount relative to the end
3396 of the stream.
3397
3398 Below is the format:
3399
3400 |: MAIN STRUCTURE :|
3401 |------------------------------------------------|
3402 | n | File Data |
3403 |------|-----------------------------------------|
3404 | n | TOC Entries |
3405 |------|-----------------------------------------|
3406 | 8 | 'FSSRLZ1\0' |
3407 | 8 | Base Offset (negative, relative to end) |
3408 | 8 | TOC Offset (relative to Base Offset) |
3409 | 4 | TOC Entry Count |
3410 | 4 | Reserved (set to zero) |
3411
3412
3413 |: TOC ENTRY :|
3414 |------------------------------------------------|
3415 | 4 | File Flags |
3416 | 4 | File Path Length |
3417 | n | File Path |
3418 | 1 | '\0' (Null Terminator) |
3419 | n | Padding to 8-byte alignment |
3420 | 8 | File Size |
3421 | 8 | File Offset (local, relative) |
3422
3423
3424 |: FILE FLAGS :|
3425 |------------------------------------------------|
3426 | 0x1 | Directory |
3427
3428
3429 Notes:
3430 - The signature is located at the end of the stream. To parse, seek to the last 32 bytes, read
3431 the signature, base offset, TOC offset and entry count, then calculate the absolute TOC offset
3432 as Base Offset + TOC Offset and seek to that position, relative to the end, to read the TOC.
3433 The base offset is always negative.
3434 - Directories should be explicitly listed in the TOC, and also have the "Directory" flag set.
3435 - Directories must be listed before any files that are contained within them.
3436 - File Offset and TOC Offset are both local offsets relative to the Base Offset position. Sum
3437 the offsets with the base offset, and then seek relative to the end.
3438 - The Base Offset is relative to the end of the archive (negative value).
3439 - File data is aligned to 8-byte boundaries.
3440 - All multi-byte values are little-endian.
3441 - File paths are stored using UTF-8 encoding.
3442 - To calculate the padding between the null terminator and the next 8-byte aligned offset, round
3443 the length of the file path plus one (for the null terminator) up to the next multiple of 8,
3444 then subtract the length of the file path plus one.
3445
3446
3447 Parameters
3448 ----------
3449 pFS : (in)
3450 A pointer to the file system object. Must not be NULL.
3451
3452 pDirectoryPath : (in, optional)
3453 The path to the directory to serialize.
3454
3455 options : (in)
3456 Options for the serialization operation. These are passed in directly to `fs_first()` and
3457 therefore have the same meaning. See `fs_first()` for details. These will also be passed into
3458 `fs_file_open()` when opening files, combined with `FS_READ`.
3459
3460 pOutputStream : (in)
3461 A pointer to the output stream where the serialized data will be written. Must not be NULL.
3462
3463
3464 Return Value
3465 ------------
3466 Returns FS_SUCCESS on success; any other result code otherwise.
3467
3468
3469 See Also
3470 --------
3471 fs_deserialize()
3472 fs_first()
3473 */
3474 FS_API fs_result fs_serialize(fs* pFS, const char* pDirectoryPath, int options, fs_stream* pOutputStream);
3475
3476 /*
3477 Deserializes file system data from a stream.
3478
3479 This function reads serialized file system data from a stream and recreates the files and
3480 directories in the specified subdirectory. The format of the data must match that produced by
3481 `fs_serialize()`.
3482
3483 The subdirectory parameter specifies where to restore the serialized data. If NULL or empty, the
3484 data is restored to the file system root. The path is relative to the file system root.
3485
3486 Existing files with the same name will be overwritten during deserialization. If the output
3487 directory already exists, it will not emptied before restoring the data.
3488
3489 This function will not attempt to clean up any partially restored data if an error occurs during
3490 the process.
3491
3492
3493 Parameters
3494 ----------
3495 pFS : (in)
3496 A pointer to the file system object. Must not be NULL.
3497
3498 pSubdirectoryPath : (in, optional)
3499 The path to the subdirectory where the data should be restored. Can be NULL or empty to
3500 restore to the filesystem root. The path should use forward slashes ('/') as separators.
3501
3502 options : (in)
3503 Options for the deserialization operation. These will be passed into `fs_file_open()` when
3504 creating files. See `fs_file_open()` for details.
3505
3506 pInputStream : (in)
3507 A pointer to the input stream containing the serialized data. Must not be NULL.
3508
3509
3510 Return Value
3511 ------------
3512 Returns FS_SUCCESS on success; any other result code otherwise.
3513
3514
3515 See Also
3516 --------
3517 fs_serialize()
3518 */
3519 FS_API fs_result fs_deserialize(fs* pFS, const char* pDirectoryPath, int options, fs_stream* pInputStream);
3520
3521
3522 /* BEG fs_backend_posix.h */
3523 extern const fs_backend* FS_BACKEND_POSIX;
3524 /* END fs_backend_posix.h */
3525
3526 /* BEG fs_backend_win32.h */
3527 extern const fs_backend* FS_BACKEND_WIN32;
3528 /* END fs_backend_win32.h */
3529 /* END fs.h */
3530
3531
3532 /* BEG fs_errno.h */
3533 FS_API fs_result fs_result_from_errno(int error);
3534 /* END fs_errno.h */
3535
3536
3537 /* BEG fs_path.h */
3538 /*
3539 These functions are low-level functions for working with paths. The most important part of this API
3540 is probably the iteration functions. These functions are used for iterating over each of the
3541 segments of a path. This library will recognize both '\' and '/'. If you want to use a different
3542 separator, you'll need to use a different library. Likewise, this library will treat paths as case
3543 sensitive. Again, you'll need to use a different library if this is not suitable for you.
3544
3545 Iteration will always return both sides of a separator. For example, if you iterate "abc/def",
3546 you will get two items: "abc" and "def". Where this is of particular importance and where you must
3547 be careful, is the handling of the root directory. If you iterate "/", it will also return two
3548 items. The first will be length 0 with an offset of zero which represents the left side of the "/"
3549 and the second will be length 0 with an offset of 1 which represents the right side. The reason for
3550 this design is that it makes iteration unambiguous and makes it easier to reconstruct a path.
3551
3552 The path API does not do any kind of validation to check if it represents an actual path on the
3553 file system. Likewise, it does not do any validation to check if the path contains invalid
3554 characters. All it cares about is "/" and "\".
3555 */
3556 typedef struct
3557 {
3558 const char* pFullPath;
3559 size_t fullPathLength;
3560 size_t segmentOffset;
3561 size_t segmentLength;
3562 } fs_path_iterator;
3563
3564 FS_API fs_result fs_path_first(const char* pPath, size_t pathLen, fs_path_iterator* pIterator);
3565 FS_API fs_result fs_path_last(const char* pPath, size_t pathLen, fs_path_iterator* pIterator);
3566 FS_API fs_result fs_path_next(fs_path_iterator* pIterator);
3567 FS_API fs_result fs_path_prev(fs_path_iterator* pIterator);
3568 FS_API fs_bool32 fs_path_is_first(const fs_path_iterator* pIterator);
3569 FS_API fs_bool32 fs_path_is_last(const fs_path_iterator* pIterator);
3570 FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB);
3571 FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen);
3572 FS_API const char* fs_path_file_name(const char* pPath, size_t pathLen); /* Does *not* include the null terminator. Returns an offset of pPath. Will only be null terminated if pPath is. Returns null if the path ends with a slash. */
3573 FS_API int fs_path_directory(char* pDst, size_t dstCap, const char* pPath, size_t pathLen); /* Returns the length, or < 0 on error. pDst can be null in which case the required length will be returned. Will not include a trailing slash. */
3574 FS_API const char* fs_path_extension(const char* pPath, size_t pathLen); /* Does *not* include the null terminator. Returns an offset of pPath. Will only be null terminated if pPath is. Returns null if the extension cannot be found. */
3575 FS_API fs_bool32 fs_path_extension_equal(const char* pPath, size_t pathLen, const char* pExtension, size_t extensionLen); /* Returns true if the extension is equal to the given extension. Case insensitive. */
3576 FS_API const char* fs_path_trim_base(const char* pPath, size_t pathLen, const char* pBasePath, size_t basePathLen);
3577 FS_API fs_bool32 fs_path_begins_with(const char* pPath, size_t pathLen, const char* pBasePath, size_t basePathLen);
3578 FS_API int fs_path_append(char* pDst, size_t dstCap, const char* pBasePath, size_t basePathLen, const char* pPathToAppend, size_t pathToAppendLen); /* pDst can be equal to pBasePath in which case it will be appended in-place. pDst can be null in which case the function will return the required length. */
3579 FS_API int fs_path_normalize(char* pDst, size_t dstCap, const char* pPath, size_t pathLen, unsigned int options); /* The only root component recognized is "/". The path cannot start with "C:", "//<address>", etc. This is not intended to be a general cross-platform path normalization routine. If the path starts with "/", this will fail with a negative result code if normalization would result in the path going above the root directory. Will convert all separators to "/". Will remove trailing slash. pDst can be null in which case the required length will be returned. */
3580 /* END fs_path.h */
3581
3582
3583 /* BEG fs_memory_stream.h */
3584 /*
3585 Memory streams support both reading and writing within the same stream. To only support read-only
3586 mode, use fs_memory_stream_init_readonly(). With this you can pass in a standard data/size pair.
3587
3588 If you need writing support, use fs_memory_stream_init_write(). When writing data, the stream will
3589 output to a buffer that is owned by the stream. When you need to access the data, do so by
3590 inspecting the pointer directly with `stream.write.pData` and `stream.write.dataSize`. This mode
3591 also supports reading.
3592
3593 You can overwrite data by seeking to the required location and then just writing like normal. To
3594 append data, just seek to the end:
3595
3596 fs_memory_stream_seek(pStream, 0, FS_SEEK_END);
3597
3598 The memory stream need not be uninitialized in read-only mode. In write mode you can use
3599 `fs_memory_stream_uninit()` to free the data. Alternatively you can just take ownership of the
3600 buffer and free it yourself with `fs_free()`.
3601
3602 Below is an example for write mode.
3603
3604 ```c
3605 fs_memory_stream stream;
3606 fs_memory_stream_init_write(NULL, &stream);
3607
3608 // Write some data to the stream...
3609 fs_memory_stream_write(&stream, pSomeData, someDataSize, NULL);
3610
3611 // Do something with the data.
3612 do_something_with_my_data(stream.write.pData, stream.write.dataSize);
3613 ```
3614
3615 To free the data, you can use `fs_memory_stream_uninit()`, or you can take ownership of the data
3616 and free it yourself with `fs_free()`:
3617
3618 ```c
3619 fs_memory_stream_uninit(&stream);
3620 ```
3621
3622 Or to take ownership:
3623
3624 ```c
3625 size_t dataSize;
3626 void* pData = fs_memory_stream_take_ownership(&stream, &dataSize);
3627 ```
3628
3629 With the above, `pData` will be the pointer to the data and `dataSize` will be the size of the data
3630 and you will be responsible for deleting the buffer with `fs_free()`.
3631
3632
3633 Read mode is simpler:
3634
3635 ```c
3636 fs_memory_stream stream;
3637 fs_memory_stream_init_readonly(pData, dataSize, &stream);
3638
3639 // Read some data.
3640 fs_memory_stream_read(&stream, &myBuffer, bytesToRead, NULL);
3641 ```
3642
3643 There is only one cursor. As you read and write the cursor will move forward. If you need to
3644 read and write from different locations from the same fs_memory_stream object, you need to
3645 seek before doing your read or write. You cannot read and write at the same time across
3646 multiple threads for the same fs_memory_stream object.
3647 */
3648
3649 typedef struct fs_memory_stream fs_memory_stream;
3650
3651 struct fs_memory_stream
3652 {
3653 fs_stream base;
3654 void** ppData; /* Will be set to &readonly.pData in readonly mode. */
3655 size_t* pDataSize; /* Will be set to &readonly.dataSize in readonly mode. */
3656 struct
3657 {
3658 const void* pData;
3659 size_t dataSize;
3660 } readonly;
3661 struct
3662 {
3663 void* pData; /* Will only be set in write mode. */
3664 size_t dataSize;
3665 size_t dataCap;
3666 } write;
3667 size_t cursor;
3668 fs_allocation_callbacks allocationCallbacks; /* This is copied from the allocation callbacks passed in from e_memory_stream_init(). Only used in write mode. */
3669 };
3670
3671 FS_API fs_result fs_memory_stream_init_write(const fs_allocation_callbacks* pAllocationCallbacks, fs_memory_stream* pStream);
3672 FS_API fs_result fs_memory_stream_init_readonly(const void* pData, size_t dataSize, fs_memory_stream* pStream);
3673 FS_API void fs_memory_stream_uninit(fs_memory_stream* pStream); /* Only needed for write mode. This will free the internal pointer so make sure you've done what you need to do with it. */
3674 FS_API fs_result fs_memory_stream_read(fs_memory_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead);
3675 FS_API fs_result fs_memory_stream_write(fs_memory_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten);
3676 FS_API fs_result fs_memory_stream_seek(fs_memory_stream* pStream, fs_int64 offset, int origin);
3677 FS_API fs_result fs_memory_stream_tell(fs_memory_stream* pStream, size_t* pCursor);
3678 FS_API fs_result fs_memory_stream_remove(fs_memory_stream* pStream, size_t offset, size_t size);
3679 FS_API fs_result fs_memory_stream_truncate(fs_memory_stream* pStream);
3680 FS_API void* fs_memory_stream_take_ownership(fs_memory_stream* pStream, size_t* pSize); /* Takes ownership of the buffer. The caller is responsible for freeing the buffer with fs_free(). Only valid in write mode. */
3681 /* END fs_memory_stream.h */
3682
3683
3684 /* BEG fs_utils.h */
3685 FS_API void fs_sort(void* pBase, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData);
3686 FS_API void* fs_binary_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData);
3687 FS_API void* fs_linear_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData);
3688 FS_API void* fs_sorted_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData);
3689
3690 FS_API int fs_strncmp(const char* str1, const char* str2, size_t maxLen);
3691 FS_API int fs_strnicmp(const char* str1, const char* str2, size_t count);
3692 FS_API int fs_strncat_s(char* dst, size_t dstCap, const char* src, size_t count);
3693 /* END fs_utils.h */
3694
3695
3696 /* BEG fs_snprintf.h */
3697 FS_API int fs_vsprintf(char* buf, char const* fmt, va_list va);
3698 FS_API int fs_vsnprintf(char* buf, size_t count, char const* fmt, va_list va);
3699 FS_API int fs_sprintf(char* buf, char const* fmt, ...) FS_ATTRIBUTE_FORMAT(2, 3);
3700 FS_API int fs_snprintf(char* buf, size_t count, char const* fmt, ...) FS_ATTRIBUTE_FORMAT(3, 4);
3701 /* END fs_snprintf.h */
3702
3703
3704 #if defined(__cplusplus)
3705 }
3706 #endif
3707 #endif /* fs_h */
3708
3709 /*
3710 This software is available as a choice of the following licenses. Choose
3711 whichever you prefer.
3712
3713 ===============================================================================
3714 ALTERNATIVE 1 - Public Domain (www.unlicense.org)
3715 ===============================================================================
3716 This is free and unencumbered software released into the public domain.
3717
3718 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
3719 software, either in source code form or as a compiled binary, for any purpose,
3720 commercial or non-commercial, and by any means.
3721
3722 In jurisdictions that recognize copyright laws, the author or authors of this
3723 software dedicate any and all copyright interest in the software to the public
3724 domain. We make this dedication for the benefit of the public at large and to
3725 the detriment of our heirs and successors. We intend this dedication to be an
3726 overt act of relinquishment in perpetuity of all present and future rights to
3727 this software under copyright law.
3728
3729 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3730 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3731 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3732 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3733 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3734 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3735
3736 For more information, please refer to <http://unlicense.org/>
3737
3738 ===============================================================================
3739 ALTERNATIVE 2 - MIT No Attribution
3740 ===============================================================================
3741 Copyright 2025 David Reid
3742
3743 Permission is hereby granted, free of charge, to any person obtaining a copy of
3744 this software and associated documentation files (the "Software"), to deal in
3745 the Software without restriction, including without limitation the rights to
3746 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
3747 of the Software, and to permit persons to whom the Software is furnished to do
3748 so.
3749
3750 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3751 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3752 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3753 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3754 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3755 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3756 SOFTWARE.
3757 */