cubos::core::data::FileSystem class final

Singleton which represents the virtual file system of the engine.

All filesystem operations in the engine should be done through this class or through a File obtained here.

Public static functions

static auto root() -> File::Handle
Gets the root file of the virtual file system.
static auto mount(std::string_view path, std::unique_ptr<Archive> archive) -> bool
Mounts an archive to an absolute path.
static auto unmount(std::string_view path) -> bool
Unmounts an archive from an absolute path.
static auto find(std::string_view path) -> File::Handle
Gets a handle to a file.
static auto create(std::string_view path, bool directory = false) -> File::Handle
Creates a new file at the specified absolute path.
static auto destroy(std::string_view path) -> bool
Marks a file for destruction.
static auto open(std::string_view path, File::OpenMode mode) -> std::unique_ptr<memory::Stream>
Opens a file in the virtual file system.
static auto copy(std::string_view sourcePath, std::string_view destinationPath) -> bool
Copies a file from the source path to the destination path.

Function documentation

static File::Handle cubos::core::data::FileSystem::root()

Gets the root file of the virtual file system.

Returns File handle.

static bool cubos::core::data::FileSystem::mount(std::string_view path, std::unique_ptr<Archive> archive)

Mounts an archive to an absolute path.

Parameters
path Absolute path to mount the archive on.
archive Archive to mount.
Returns Whether the archive was successfully mounted.

Creates any parent directories that may be necessary, but the mount point itself must not already exist.

This method fails on the following conditions:

  • path is relative or invalid.
  • a parent file in the path exists and is not a directory.
  • a parent directory in the path belongs to an archive.
  • a file already exists at the mount point.

static bool cubos::core::data::FileSystem::unmount(std::string_view path)

Unmounts an archive from an absolute path.

Parameters
path Path to unmount the archive from.
Returns Whether the archive was successfully unmounted.

Removes all of the archive's files from the virtual file system.

This method fails on the following conditions:

  • path is relative or invalid.
  • any of the files in the path do not exist.
  • the target path is not the mount point of an archive.

static File::Handle cubos::core::data::FileSystem::find(std::string_view path)

Gets a handle to a file.

Parameters
path Absolute path to the file.
Returns File handle or nullptr on failure.

Fails if the file does not exist or the path is relative or invalid.

static File::Handle cubos::core::data::FileSystem::create(std::string_view path, bool directory = false)

Creates a new file at the specified absolute path.

Parameters
path Absolute path to the file.
directory Whether the new file should be a directory.
Returns File or nullptr on failure.

The destination location must be writeable - that is, it must be under a mounted writeable archive.

If a file at the specified path already exists, it is returned instead. Any parent directories that may be necessary are created.

This method fails on the following conditions:

  • path is relative or invalid.
  • a parent directory in the path does not exist and cannot be created.
  • a parent directory in the path already exists as a regular file.
  • a file of a different type already exists at the destination.

static bool cubos::core::data::FileSystem::destroy(std::string_view path)

Marks a file for destruction.

Parameters
path Absolute path of the file to destroy.
Returns Whether the file was successfully marked for destruction.

Although The file will only be deleted when no more references to it exist, it is immediately removed from the virtual file system.

All children of the file are also marked for destruction.

This method fails on the following conditions:

  • path is relative or invalid.
  • the file does not exist.
  • the file is the mount point of an archive.
  • the file does not belong to an archive.
  • the file belongs to a read-only archive.

static std::unique_ptr<memory::Stream> cubos::core::data::FileSystem::open(std::string_view path, File::OpenMode mode)

Opens a file in the virtual file system.

Parameters
path Absolute path of the file.
mode Mode to open the file in.
Returns File stream, or nullptr if an error occurred.

If the file is being written to, blocks until the other threads are done with the file.

This method fails on the following conditions:

  • FileSystem::find(path) fails.
  • file->open(mode) fails.

static bool cubos::core::data::FileSystem::copy(std::string_view sourcePath, std::string_view destinationPath)

Copies a file from the source path to the destination path.

Parameters
sourcePath Absolute path of the source file.
destinationPath Absolute path of the destination file.
Returns Whether the file was successfully copied.

This method opens the source file in binary mode, reads its contents, and writes them to the destination file. If the destination file already exists, it will be overwritten.