ona/source/coral/files.cpp

122 lines
4.0 KiB
C++
Executable File

export module coral.files;
import coral;
export namespace coral {
/**
* [reader] that has a known range of data and may attempt to traverse it freely.
*/
struct file_reader : public reader {
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0;
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0;
};
/**
* Enumerates a file tree recursively, returning the absolute path of each file within.
*
* *Note*: file tree walking order is considered implementation-specific.
*/
struct file_walker : public enumerator<expected<slice<char const>, io_error>> {
virtual ~file_walker() {}
/**
* See [enumerator::enumerate].
*/
virtual bool enumerate() = 0;
/**
* Returns the last-enumerated path as a sequence of `char`s or an [io_error] if the last enumeration did not
* successfully complete.
*/
virtual expected<slice<char const>, io_error> value() = 0;
};
/**
* [writer] that has a known range of data and may attempt to traverse it freely.
*/
struct file_writer : public writer {
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0;
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0;
};
/**
* Platform-generalized file system interface.
*/
struct file_system {
/**
* Errors that may occur during a file system query operation.
*
* [query_error::unsupported] occurs when either part or all of the query operation is not supported on the
* given file system.
*
* [query_error::io_unavailable] is a general catch-all to report that an implementation-specific I/O error has
* occured and the operation could not proceed as a result.
*/
enum class query_error {
unsupported,
io_unavailable,
};
/**
* Various meta-information about a file in a file system.
*/
struct file_info {
u64 size;
};
virtual ~file_system() {};
/**
* Attempts to query a file in the file system located at `path`, returning a [file_info] describing it or a
* [query_error] if the operation failed.
*/
virtual expected<file_info, query_error> query_file(slice<char const> const & path) {
return query_error::unsupported;
}
/**
* Attempts to read a file in the file system located at `path`, calling `then` if it was successfully opened
* for reading and passing the [file_reader] context along.
*
* See [file_reader] for more information on how to read from the file.
*/
virtual void read_file(slice<char const> const & path, closure<void(file_reader &)> const & then) {}
/**
* Attempts to walk the file tree from `path`, calling `then` if it was successfully opened for walking and
* passing the [file_walker] context along.
*
* See [file_walker] for more information on how to traverse the file tree.
*/
virtual void walk_files(slice<char const> const & path, closure<void(file_walker &)> const & then) {}
/**
* Attempts to write a file in the file system located at `path`, calling `then` if it was successfully created
* and / or opened for writing and passing the [file_writer] context along.
*
* See [file_writer] for more information on how to write to the file.
*
* *Note*: Any file already existing at `path` will be overwritten to create a new file for writing.
*/
virtual void write_file(slice<char const> const & path, closure<void(file_writer &)> const & then) {}
};
}