-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filesystem.hpp
552 lines (513 loc) · 19.6 KB
/
Filesystem.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#ifndef DONUT_FILESYSTEM_HPP
#define DONUT_FILESYSTEM_HPP
#include <donut/File.hpp>
#include <span> // std::span
#include <string> // std::string
#include <vector> // std::vector
namespace donut {
/**
* Mount priority for a newly mounted archive to a virtual Filesystem, relative
* to all previously mounted archives.
*/
enum class FilesystemMountPriority {
/**
* Mount the archive at a lower priority than any previously mounted
* archive, meaning files in already mounted archives will be preferred
* when choosing which host file to map a virtual filepath to.
*/
LOWER,
/**
* Mount the archive at a higher priority than any previously mounted
* archive, meaning files in the new archive will be preferred when
* choosing which host file to map a virtual filepath to.
*/
HIGHER,
};
/**
* Configuration options for a virtual Filesystem.
*/
struct FilesystemOptions {
/**
* Non-owning pointer to a null-terminated UTF-8 string that commonly
* identifies the publisher of the application, such as an organization
* name, alias or internet domain.
*
* When set, this is used to determine part of the filesystem's initial
* output directory, into which files such as configuration or save data can
* then be written by the application. Namely, it is used for the name of
* the organization folder in the user/platform-specific preferences
* directory on platforms where it is applicable, which will be created if
* it doesn't already exist.
*
* If set to nullptr, no output directory will be created nor mounted, and
* the application will be unable to write files unless an output directory
* is set manually using Filesystem::setOutputDirectory().
*
* \sa applicationName
* \sa mountOutputDirectory
* \sa outputDirectoryMountPriority
* \sa dataDirectory
*/
const char* organizationName = nullptr;
/**
* Non-owning pointer to a null-terminated UTF-8 string that uniquely
* identifies the application among all other applications released by the
* same organization.
*
* When set, this is used to determine part of the filesystem's initial
* write directory, into which files such as configuration or save data can
* then be written by the application. Namely, it is used for the name of
* the application folder under the organization folder in the
* user/platform-specific preferences directory on platforms where it is
* applicable, which will be created if it doesn't already exist.
*
* If set to nullptr, no output directory will be created nor mounted, and
* the application will be unable to write files unless an output directory
* is set manually using Filesystem::setOutputDirectory().
*
* \sa organizationName
* \sa mountOutputDirectory
* \sa outputDirectoryMountPriority
* \sa dataDirectory
*/
const char* applicationName = nullptr;
/**
* Non-owning pointer to a null-terminated UTF-8 string of the host filepath
* to the main data directory which will be mounted for reading.
*
* If set to nullptr, no main data directory will be mounted, and the
* application will be unable to read any files unless the output directory
* or additional archives are mounted, or if an archive is mounted manually
* using Filesystem::mountArchive().
*
* \sa organizationName
* \sa applicationName
*/
const char* dataDirectory = ".";
/**
* Non-owning pointer to a null-terminated UTF-8 string of the virtual
* filepath to a directory in which to search for additional initial
* archives to mount.
*
* If set to nullptr, no additional archives will be mounted.
*
* \sa archiveSearchFileExtension
* \sa archiveSearchMountPriority
*/
const char* archiveSearchPath = nullptr;
/**
* Non-owning pointer to a null-terminated UTF-8 string of the filename
* extension of initial archives to search for.
*
* If set to nullptr, all found archives will be mounted regardless of
* extension.
*
* \note This option is only applicable when #archiveSearchPath is not
* nullptr.
*
* \sa archiveSearchPath
* \sa archiveSearchMountPriority
*/
const char* archiveSearchFileExtension = nullptr;
/**
* Mount priority of the main data directory relative to the initial write
* directory.
*
* \note This option is only applicable when #mountOutputDirectory
* is set to true, and neither #dataDirectory, #organizationName nor
* #applicationName are nullptr.
*
* \sa organizationName
* \sa applicationName
* \sa mountOutputDirectory
*/
FilesystemMountPriority mountPriorityOfDataDirectoryRelativeToOutputDirectory = FilesystemMountPriority::LOWER;
/**
* Mount priority of the additional initial archives relative to the initial
* write directory.
*
* \note This option is only applicable when #archiveSearchPath is not
* nullptr.
*
* \sa archiveSearchPath
* \sa archiveSearchFileExtension
*/
FilesystemMountPriority mountPriorityOfArchiveSearchRelativeToOutputDirectory = FilesystemMountPriority::LOWER;
/**
* Mount priority of the additional initial archives relative to the main
* data directory.
*
* \note This option is only applicable when #archiveSearchPath is not
* nullptr.
*
* \sa archiveSearchPath
* \sa archiveSearchFileExtension
*/
FilesystemMountPriority mountPriorityOfArchiveSearchRelativeToDataDirectory = FilesystemMountPriority::HIGHER;
/**
* Mount the initial output directory for reading in addition to writing.
*
* \note This option is only applicable when neither #organizationName nor
* #applicationName are nullptr.
*
* \sa organizationName
* \sa applicationName
* \sa outputDirectoryMountPriority
* \sa dataDirectory
*/
bool mountOutputDirectory = true;
};
/**
* Persistent system for managing the virtual filesystem.
*
* This system allows for mounting multiple host filesystem directories to the
* same virtual mount point, mapping each contained file path to the
* corresponding host file with the highest mount priority for the purposes of
* reading. For writing, the filesystem defines a specific centralized folder
* known as the output directory, where any output files will be written. See
* FilesystemOptions for more information.
*/
class Filesystem {
public:
/**
* Initialize the virtual filesystem.
*
* \param programFilepath the first string in the argument vector passed to
* the main function of the program, i.e. argv[0].
* \param options initial configuration of the virtual filesystem, see
* FilesystemOptions.
*
* \throws File::Error if initialization failed.
* \throws std::bad_alloc on allocation failure.
*
* \warning The behavior of passing programFilepath a value other than the
* argv[0] string received from main is undefined.
* \warning There can only be one active virtual filesystem in a program at
* a time.
*
* \sa setOutputDirectory()
* \sa mountArchives()
* \sa mountArchive()
*/
explicit Filesystem(const char* programFilepath, const FilesystemOptions& options = {});
/** Destructor. */
~Filesystem();
/** Copying a filesystem is not allowed, since it manages global state. */
Filesystem(const Filesystem&) = delete;
/** Moving a filesystem is not allowed, since it manages global state. */
Filesystem(Filesystem&&) = delete;
/** Copying a filesystem is not allowed, since it manages global state. */
Filesystem& operator=(const Filesystem&) = delete;
/** Moving a filesystem is not allowed, since it manages global state. */
Filesystem& operator=(Filesystem&&) = delete;
/**
* Get a suitable output directory for configuration files and other save
* data on the host platform, which is usually located somwhere within the
* user's home directory in a sub-folder tailored to this specific
* application.
*
* \param organizationName non-owning pointer to a null-terminated UTF-8
* string that commonly identifies the publisher of the application,
* such as an organization name, alias or internet domain. This will
* be used for the name of the organization folder in the
* user/platform-specific preferences directory on platforms where it
* is applicable. Must not be nullptr.
* \param applicationName non-owning pointer to a null-terminated UTF-8
* string that uniquely identifies the application among all other
* applications released by the same organization. This will be used
* for the name of the application folder under the organization
* folder in the user/platform-specific preferences directory on
* platforms where it is applicable. Must not be nullptr.
*
* \return a directory that can be passed to setOutputDirectory(), or an
* empty string if a suitable output directory could not be
* determined.
*
* \throws std::bad_alloc on allocation failure.
*
* \sa setOutputDirectory()
* \sa getOutputDirectory()
*/
[[nodiscard]] std::string getStandardOutputDirectory(const char* organizationName, const char* applicationName) const;
/**
* Get the current output directory of the virtual filesystem.
*
* \return the host filepath corresponding to the current output directory,
* or an empty string if no output directory is currently set.
*
* \sa getStandardOutputDirectory()
* \sa setOutputDirectory()
*/
[[nodiscard]] std::string getOutputDirectory() const;
/**
* Set the output directory of the virtual filesystem, where all output
* files will be written.
*
* The specified directory will be created if it doesn't already exist.
*
* \param path host filepath of the new output directory to set.
*
* \throws File::Error on failure to set the output directory to the given
* path.
* \throws std::bad_alloc on allocation failure.
*
* \note To be able to read files from the output directory, it must also be
* mounted using mountArchive().
*
* \sa getStandardOutputDirectory()
* \sa getOutputDirectory()
*/
void setOutputDirectory(std::string path);
/**
* Mount a directory or archive on the host filesystem to the root directory
* of the virtual filesystem.
*
* \param path host filepath of the directory or archive to mount. Must not
* be nullptr.
* \param priority mount priority of the archive to be mounted, see
* FilesystemMountPriority.
*
* \throws File::Error on failure to mount the given filepath.
*
* \note If the given path is already mounted, no change will occur.
*
* \sa mountArchives()
* \sa unmountArchive()
*/
void mountArchive(const char* path, FilesystemMountPriority priority = FilesystemMountPriority::HIGHER);
/**
* Unmount a previously mounted directory or archive on the host filesystem
* from the virtual filesystem.
*
* \param path host filepath of the directory or archive to unmount, which
* was previously mounted by mountArchive() or mountArchives(). Must
* not be nullptr.
*
* \throws File::Error on failure to unmount the given filepath.
*
* \sa mountArchive()
*/
void unmountArchive(const char* path);
/**
* Mount all archives in a given directory on the host filesystem to the
* root directory of the virtual filesystem. This is useful for allowing
* users to easily create and share modifications or plugins that add or
* override application resources by simply adding the mod archive to the
* given directory.
*
* The newly mounted archives will have a higher priority than any
* previously mounted archives when choosing which host file to map a
* virtual filepath to, meaning more recently mounted files are preferred.
*
* \param filepath virtual filepath of the mounted directory to search for
* archives in. Must not be nullptr.
* \param archiveFileExtension non-owning pointer to a null-terminated UTF-8
* string of the filename extension of the archive files to mount. If
* set to nullptr, all found archives will be mounted regardless of
* extension.
* \param priority mount priority of the archives to be mounted, see
* FilesystemMountPriority.
*
* \return an input-iterable sequence containing the host filepaths of the
* archives that were mounted, in no specific order.
*
* \throws File::Error on failure to search for archives or mount an
* archive.
* \throws std::bad_alloc on allocation failure.
*
* \note The found archives are mounted in no particular order.
*
* \sa unmountArchives()
* \sa mountArchive()
* \sa unmountArchive()
*/
std::vector<std::string> mountArchives(const char* filepath, const char* archiveFileExtension, FilesystemMountPriority priority = FilesystemMountPriority::HIGHER);
/**
* Unmount multiple archives.
*
* Equivalent to calling unmountArchive() on each given path.
*
* \param paths the host filepaths of the archives to unmount, which
* were previously mounted by mountArchive() or mountArchives().
*
* \throws File::Error on failure to unmount a given filepath.
*
* \sa mountArchives()
* \sa mountArchive()
* \sa unmountArchives()
*/
void unmountArchives(std::span<const std::string> paths);
/**
* Get the host filepath of the mounted archive on the host filesystem that
* contains a given virtual file.
*
* \param filepath virtual filepath of the file to get the archive of. Must
* not be nullptr.
*
* \return the host filepath of the directory or archive that was previously
* passed to mountArchive(), in which the corresponding virtual file
* was found, or nullptr if the virtual file was not found at any
* active mount point.
*
* \warning This function may return nullptr.
*
* \sa mountArchive()
*/
[[nodiscard]] const char* findArchiveOfFile(const char* filepath) const;
/**
* Create a new host directory in the output directory.
*
* \param filepath virtual filepath, relative to the current output
* directory, of the new directory to be created. Must not be
* nullptr.
*
* \throws Error on failure to create the given directory.
* \throws std::bad_alloc on allocation failure.
*/
void createDirectory(const char* filepath);
/**
* Delete a host file or directory in the output directory.
*
* \param filepath virtual filepath, relative to the current output
* directory, of the file or directory to delete. Must not be
* nullptr.
*
* \throws Error on failure to delete the given file or directory.
* \throws std::bad_alloc on allocation failure.
*
* \warning If successful, this will delete the actual file that
* corresponds to the given virtual filepath on the host
* filesystem; not just the virtual file entry.
* \warning Although deleting a file will prevent it from being read again
* through conventional means, the physical data that was contained
* in the file may or may not remain untouched on disk, meaning
* that this function cannot be relied upon to securly erase
* sensitive data.
*
* \note Directories must be empty before they can be successfully deleted
* using this function.
*/
void deleteFile(const char* filepath);
/**
* Check if a given virtual filepath has a corresponding host file mounted.
*
* \param filepath virtual filepath to check for a mounted file. Must not be
* nullptr.
*
* \return true if a file is mounted at the given filepath, false otherwise.
*
* \sa getFileMetadata()
* \sa getFilenamesInDirectory()
*/
[[nodiscard]] bool fileExists(const char* filepath) const;
/**
* Get the metadata of a file that is mounted at a given virtual filepath.
*
* \param filepath virtual filepath of the file to get the metadata of. Must
* not be nullptr.
*
* \return the file metadata, see File::Metadata.
*
* \throws File::Error on failure to get the metadata, such as if no file is
* mounted at the given filepath.
* \throws std::bad_alloc on allocation failure.
*/
[[nodiscard]] File::Metadata getFileMetadata(const char* filepath) const;
/**
* Get a list of the filenames of all readable virtual filepaths that are
* direct children of a given directory.
*
* \param filepath virtual filepath of the directory to enumerate. Must not
* be nullptr.
*
* \return an input-iterable sequence containing the filenames, in no
* specific order.
*
* \throws Error on failure to enumerate the directory.
* \throws std::bad_alloc on allocation failure.
*
* \note This function is not recursive, and only returns the filename
* component of the direct descendants of the given directory, without
* the leading directory path. The full virtual filepath of each
* result can be formatted as `"{filepath}/{filename}"`, where
* `{filepath}` is the directory filepath that was passed to the
* function, and `{filename}` is one of the results in the returned
* sequence. Note that this path may refer to any kind of file,
* including a subdirectory. Use getFileMetadata() to find out which
* kind of file it refers to.
*
* \sa fileExists()
* \sa getFileMetadata()
*/
[[nodiscard]] std::vector<std::string> getFilenamesInDirectory(const char* filepath) const;
/**
* Open a file in the virtual filesystem for reading.
*
* \param filepath virtual filepath of the file to open. Must not be
* nullptr.
*
* \return a new virtual file handle with an input stream set up to read the
* opened file starting at file position 0.
*
* \throws File::Error on failure to open the file for reading.
* \throws std::bad_alloc on allocation failure.
*
* \sa createFile()
* \sa appendFile()
*/
[[nodiscard]] File openFile(const char* filepath) const;
/**
* Create a file in the output directory and open it for writing,
* overwriting any existing file at the same filepath.
*
* \param filepath virtual filepath of the new file to be created, relative
* to the current output directory. Must not be nullptr.
*
* \return a new virtual file handle with an output stream set up to write
* to the new empty file that was opened.
*
* \throws File::Error on failure to delete the existing file, create the
* new file, or open the new file for writing.
* \throws std::bad_alloc on allocation failure.
*
* \warning If successful, any existing host file that corresponds to the
* given virtual filepath in the output directory on the host
* filesystem will be deleted on the host filesystem; not just its
* virtual file entry. All modifications made through the virtual
* file handle will also be reflected in the physical file on the
* host file system.
*
* \sa openFile()
* \sa appendFile()
*/
[[nodiscard]] File createFile(const char* filepath);
/**
* Open a file in the output directory specified by the virtual filesystem
* for appended writing.
*
* \param filepath virtual filepath of the file to be opened, relative to
* the current output directory. Must not be nullptr.
*
* \return a new virtual file handle with an output stream set up to write
* to the end of the opened file.
*
* \throws File::Error on failure to open the file for writing.
* \throws std::bad_alloc on allocation failure.
*
* \warning If successful, the existing host file that corresponds to the
* given virtual filepath in the output directory on the host
* filesystem will receive all modifications made through the
* virtual file handle.
*
* \note A new, empty file will be created if the specified file doesn't
* already exist.
*
* \sa openFile()
* \sa createFile()
*/
[[nodiscard]] File appendFile(const char* filepath);
private:
std::string outputDirectory{};
};
} // namespace donut
#endif