CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
vfs.h
Go to the documentation of this file.
1
3
30
31#pragma once
32
33#include <cx/fs/file.h>
34#include <cx/fs/fs.h>
35#include <cx/time/time.h>
36#include <cx/fs/vfsfile.h>
37#include <cx/fs/vfsobj.h>
38#include <cx/fs/vfsprovider.h>
39
42typedef struct VFS VFS;
43
44CX_C_BEGIN
45
56_At_(*pvfs, _Pre_maybenull_ _Post_null_) void vfsDestroy(VFS** pvfs);
57
67bool vfsUnmount(_Inout_ VFS* vfs, _In_opt_ strref path);
68
69bool _vfsMountProvider(_Inout_ VFS* vfs, _Inout_ ObjInst* provider, _In_opt_ strref path,
70 flags_t flags);
71
90#define vfsMountProvider(vfs, provider, path, ...) \
91 _vfsMountProvider(vfs, objInstBase(provider), path, opt_flags(__VA_ARGS__))
92
93bool _vfsMountFS(_Inout_ VFS* vfs, _In_opt_ strref path, _In_opt_ strref fsroot, flags_t flags);
94
115#define vfsMountFS(vfs, path, fsroot, ...) _vfsMountFS(vfs, path, fsroot, opt_flags(__VA_ARGS__))
116
117bool _vfsMountVFS(_Inout_ VFS* vfs, _In_opt_ strref path, _Inout_ VFS* vfs2,
118 _In_opt_ strref vfs2root, flags_t flags);
119
142#define vfsMountVFS(vfs, path, vfs2, vfs2root, ...) \
143 _vfsMountVFS(vfs, path, vfs2, vfs2root, opt_flags(__VA_ARGS__))
144
152void vfsCurDir(_Inout_ VFS* vfs, _Inout_ string* out);
153
162bool vfsSetCurDir(_Inout_ VFS* vfs, _In_opt_ strref cur);
163
165#define VFS_CACHE_MAXDIRS 4096
167#define VFS_CACHE_TTL timeS(300)
168
187void vfsSetCacheLimits(_Inout_ VFS* vfs, uint32 maxdirs, int64 ttl);
188
196void vfsPruneCache(_Inout_ VFS* vfs);
197
206void vfsAbsolutePath(_Inout_ VFS* vfs, _Inout_ string* out, _In_opt_ strref path);
207
208#define _vfsStatAnno _Success_(return != FS_Nonexistent)
209
219_vfsStatAnno FSPathStat vfsStat(_Inout_ VFS* vfs, _In_opt_ strref path, _Out_opt_ FSStat* stat);
220
226_meta_inline bool vfsExist(_Inout_ VFS* vfs, _In_opt_ strref path)
227{
228 return vfsStat(vfs, path, NULL) != FS_Nonexistent;
229}
230
236_meta_inline bool vfsIsDir(_Inout_ VFS* vfs, _In_opt_ strref path)
237{
238 return vfsStat(vfs, path, NULL) == FS_Directory;
239}
240
246_meta_inline bool vfsIsFile(_Inout_ VFS* vfs, _In_opt_ strref path)
247{
248 return vfsStat(vfs, path, NULL) == FS_File;
249}
250
261bool vfsSetTimes(_Inout_ VFS* vfs, _In_opt_ strref path, int64 modified, int64 accessed);
262
271bool vfsCreateDir(_Inout_ VFS* vfs, _In_opt_ strref path);
272
281bool vfsCreateAll(_Inout_ VFS* vfs, _In_opt_ strref path);
282
290bool vfsRemoveDir(_Inout_ VFS* vfs, _In_opt_ strref path);
291
300bool vfsDelete(_Inout_ VFS* vfs, _In_opt_ strref path);
301
311bool vfsCopy(_Inout_ VFS* vfs, _In_opt_ strref from, _In_opt_ strref to);
312
323bool vfsRename(_Inout_ VFS* vfs, _In_opt_ strref from, _In_opt_ strref to);
324
339bool vfsGetFSPath(_Inout_ string* out, _Inout_ VFS* vfs, _In_opt_ strref path);
340
341typedef struct FSSearchIter FSSearchIter;
342
359bool vfsSearchInit(_Out_ FSSearchIter* iter, _Inout_ VFS* vfs, _In_opt_ strref path,
360 _In_opt_ strref pattern, int typefilter, bool stat);
361
366bool vfsSearchNext(_Inout_ FSSearchIter* iter);
367
374void vfsSearchFinish(_Inout_ FSSearchIter* iter);
375
380_meta_inline bool vfsSearchValid(_In_ FSSearchIter* iter)
381{
382 return iter->_search;
383}
384
397_Ret_opt_valid_ VFSFile* vfsOpen(_Inout_ VFS* vfs, _In_opt_ strref path, flags_t flags);
398
409_meta_inline bool vfsRead(_Inout_ VFSFile* file, _Out_writes_bytes_to_(sz, *bytesread) void* buf,
410 size_t sz, _Out_ _Deref_out_range_(0, sz) size_t* bytesread)
411{
412 if (!file) {
413 *bytesread = 0;
414 return false;
415 }
416 return fileRead(file, buf, sz, bytesread);
417}
418
429_meta_inline bool vfsWrite(_Inout_ VFSFile* file, _In_reads_bytes_(sz) const void* buf, size_t sz,
430 _Out_opt_ _Deref_out_range_(0, sz) size_t* byteswritten)
431{
432 if (!file) {
433 if (byteswritten)
434 *byteswritten = 0;
435 return false;
436 }
437 return fileWrite(file, buf, sz, byteswritten);
438}
439
449_meta_inline bool vfsWriteString(_Inout_ VFSFile* file, _In_opt_ strref str,
450 _Out_opt_ size_t* byteswritten)
451{
452 if (!file) {
453 if (byteswritten)
454 *byteswritten = 0;
455 return false;
456 }
457 return fileWriteString(file, str, byteswritten);
458}
459
464_meta_inline int64 vfsTell(_Inout_ VFSFile* file)
465{
466 if (!file)
467 return -1;
468 return fileTell(file);
469}
470
479_meta_inline int64 vfsSeek(_Inout_ VFSFile* file, int64 off, FSSeekType seektype)
480{
481 if (!file)
482 return -1;
483 return fileSeek(file, off, seektype);
484}
485
492_meta_inline bool vfsFlush(_Inout_ VFSFile* file)
493{
494 if (!file)
495 return false;
496 return fileFlush(file);
497}
498
503 // Flags valid for both VFS creation and provider mounting:
504
505 VFS_ReadOnly = 0x00000001,
508
509 VFS_CaseSensitive = 0x00000002,
513
514 VFS_NoCache = 0x00000004,
518
519 // Flags valid only for provider mounting:
520
521 VFS_NewFiles = 0x00000008,
523
524 VFS_AlwaysCOW = 0x00000010,
527
528 VFS_Opaque = 0x00000020,
530};
531
533
534CX_C_END
Low-level file I/O operations.
Platform-specific filesystem operations.
#define fileRead(self, buf, sz, bytesread)
Definition fileobj.h:116
#define fileTell(self)
Definition fileobj.h:135
#define fileSeek(self, off, seektype)
Definition fileobj.h:143
#define fileWrite(self, buf, sz, byteswritten)
Definition fileobj.h:129
#define fileFlush(self)
Definition fileobj.h:152
#define fileWriteString(self, str, byteswritten)
Definition fileobj.h:95
enum FSSeekTypeEnum FSSeekType
enum FSPathStatEnum FSPathStat
@ FS_Directory
Path is a directory.
Definition fs.h:95
@ FS_File
Path is a regular file.
Definition fs.h:96
@ FS_Nonexistent
Path does not exist.
Definition fs.h:94
bool vfsSearchValid(FSSearchIter *iter)
Definition vfs.h:380
void vfsCurDir(VFS *vfs, string *out)
bool vfsSearchInit(FSSearchIter *iter, VFS *vfs, strref path, strref pattern, int typefilter, bool stat)
bool vfsCreateAll(VFS *vfs, strref path)
void vfsSearchFinish(FSSearchIter *iter)
bool vfsDelete(VFS *vfs, strref path)
VFSFlags
Definition vfs.h:502
FSPathStat vfsStat(VFS *vfs, strref path, FSStat *stat)
bool vfsIsDir(VFS *vfs, strref path)
Definition vfs.h:236
bool vfsSearchNext(FSSearchIter *iter)
bool vfsFlush(VFSFile *file)
Definition vfs.h:492
bool vfsRemoveDir(VFS *vfs, strref path)
void vfsDestroy(VFS **pvfs)
VFSFile * vfsOpen(VFS *vfs, strref path, flags_t flags)
bool vfsUnmount(VFS *vfs, strref path)
void vfsSetCacheLimits(VFS *vfs, uint32 maxdirs, int64 ttl)
bool vfsWriteString(VFSFile *file, strref str, size_t *byteswritten)
Definition vfs.h:449
bool vfsExist(VFS *vfs, strref path)
Definition vfs.h:226
void vfsAbsolutePath(VFS *vfs, string *out, strref path)
int64 vfsSeek(VFSFile *file, int64 off, FSSeekType seektype)
Definition vfs.h:479
bool vfsCopy(VFS *vfs, strref from, strref to)
bool vfsCreateDir(VFS *vfs, strref path)
bool vfsRead(VFSFile *file, void *buf, size_t sz, size_t *bytesread)
Definition vfs.h:409
bool vfsGetFSPath(string *out, VFS *vfs, strref path)
bool vfsSetTimes(VFS *vfs, strref path, int64 modified, int64 accessed)
void vfsPruneCache(VFS *vfs)
bool vfsSetCurDir(VFS *vfs, strref cur)
bool vfsRename(VFS *vfs, strref from, strref to)
bool vfsWrite(VFSFile *file, const void *buf, size_t sz, size_t *byteswritten)
Definition vfs.h:429
bool vfsIsFile(VFS *vfs, strref path)
Definition vfs.h:246
int64 vfsTell(VFSFile *file)
Definition vfs.h:464
@ VFS_CaseSensitive
Definition vfs.h:509
@ VFS_NoCache
Definition vfs.h:514
@ VFS_NewFiles
Definition vfs.h:521
@ VFS_AlwaysCOW
Definition vfs.h:524
@ VFS_Opaque
Definition vfs.h:528
@ VFS_ReadOnly
Definition vfs.h:505
FSStat stat
File metadata (only valid if stat=true in fsSearchInit)
Definition fs.h:239
Definition fs.h:103
VFS Object.
Definition vfsobj.h:51
Time manipulation and conversion functions.
VFS object definitions.