Skip to content

Engine → Core

native_file_system.h

Declarations for native_file_system.h.

Classes

File Description
vfs::native_file_system A directory on disk exposed as a file_system. Every path is resolved under the root; anything that would escape it ("../x", "/../x", "C:/x", "//server/x") is PERMISSION_DENIED. Stateless apart from the root, so one instance may be used from any number of threads. Files are read through handles that share deletion, so an open reader never makes a write_file_atomic, rename or remove of the same file fail. write_file_atomic is durable: the new contents are flushed to the storage device (FlushFileBuffers, fsync) before they replace the old ones, at the cost of one device flush per call. Two limits: a volume whose driver has no flush at all still takes the write, without this guarantee, and logs one warning per process; on macOS the flush is fsync, which hands the data to the drive without waiting for the drive's own write cache, so the guarantee there is only as strong as that cache.

Functions

Public

vfs::is_private_file

[[nodiscard]] CONJURE_API result<bool> is_private_file(const std::filesystem::path &path);

Whether the entry at the on-disk path path is a regular file that only the user the process runs as may open, by the rule write_private_file establishes. POSIX: a regular file (a symbolic link is not followed and is never private) owned by the effective user with no permission bit for its group or for others. Windows: a file that is not a reparse point, on a volume that keeps access control lists, owned by the process user, whose list is protected from inheritance and holds nothing but entries allowing that user. The limits of write_private_file apply (privileged accounts, access control lists on POSIX). NOT_FOUND when nothing is at path, IO_FAILURE when the entry cannot be inspected. A file another user made private is IO_FAILURE on Windows, where it cannot be opened to look, and false on POSIX.

vfs::write_native_file_atomic

[[nodiscard]] CONJURE_API result<void> write_native_file_atomic( const std::filesystem::path &path, std::span<const std::byte> data );

Replaces the file at the on-disk path path with data in one step, the way native_file_system::write_file_atomic does inside its root: the data goes into a new file beside path ("<name>.tmp-<uuid>") that is flushed to the storage device (FlushFileBuffers, fsync) and then renamed over path. After a failure, a crash or a power cut the path holds the old contents (none, for a file that did not exist) or the new ones, never a part. The cost is one device flush per call. The limits of write_file_atomic's flush apply: a volume whose driver has no flush at all takes the write without the guarantee and logs one warning per process, and on macOS fsync does not wait for the drive's own write cache. Missing parent directories are created. A relative path is resolved against the working directory, and a bare file name is written there. No VFS rule applies: the path is not normalised, confined to a root or checked for portable names; the caller owns it. INVALID_ARGUMENT, with nothing written, for an empty path, a path that ends in a separator and a directory at the path (a symbolic link to a directory counts as one); IO_FAILURE for everything else (a parent that is a file, a refused open, write, flush or rename). Every message names the file: the path as given, except a refused rename's, which names it in the form handed to the system ("./save.json" for a bare name; on Windows the extended-length form, "//?/C:/...", where the path needed or already had it) beside the temporary. A failure removes the new file again; should that removal fail as well, it stays beside path. Replacing is not writing through. POSIX: the file at path afterwards is a new one with mode 0666 less the umask; a read-only file in a writable folder is replaced, and the old file's custom mode, access control lists, extended attributes and hard links are not kept; a symbolic link to a file (or a dangling one) at path is replaced by a regular file and the file it named is not written. Windows: a file with the read-only attribute, or one that another program holds open without sharing deletion, is not replaced (IO_FAILURE); the new file takes the access control list the folder hands down, not the old file's.

vfs::write_private_file

[[nodiscard]] CONJURE_API result<void> write_private_file( const std::filesystem::path &path, std::span<const std::byte> data );

Writes data to the file at the on-disk path path so that only the user the process runs as may open it: the place for a secret that other accounts of the machine must not read. The data goes into a new file beside path that is created with the restriction already in place (POSIX: mode 0600; Windows: an access control list that names that user alone, inherits nothing from the folder and makes the user the owner), and that file then takes the place of path in one step. A reader therefore sees the old contents or the new ones, never a part, and what was at path before (a file with wider permissions, a symbolic link) is replaced, not written through. The folder must exist. path must name a file: an empty path, one that ends in a separator and one that is a directory are INVALID_ARGUMENT, and nothing is created. Limits. UNSUPPORTED, with nothing written, when the volume cannot restrict a file to its owner: FAT and exFAT, and POSIX mounts whose owner and mode are fixed by mount options. Accounts the operating system lets pass any permission still read the file (root; on Windows administrators and backup operators). Access control lists beyond the mode bits are not examined on POSIX (macOS). The folder is the caller's business: an account that may write it can replace or delete the file, though not read it. The data is not flushed to the device, so a power cut may lose it. Every other failure is IO_FAILURE with the operating system's text. The data is written only once the restriction was checked on the new file, and a failure removes that file again; should the removal fail as well, it stays beside path as "<name>.tmp-<uuid>", either holding none of the data or restricted to its owner.