MT Core (C++)
Core library for replacing C++ standard in project usage
|
Synchronous File System interface for handling I/O calls This is encapsulated as a struct of file pointers so we can do deterministic testing This allows us to also dynamically swap things out for different file systems, or even do remote "file systems" where we're reading and writing to an object store (e.g. More...
#include <file_sys.hpp>
Public Member Functions | |
Result< void, FileError > | write_file_safe (Allocator &tmpAlloc, const char *path, Slice< const u8 > data, Prng &prng) const |
Overwrites a file's contents by first creating a temporary file and then doing a rename Will sync file to disk If there is a system failure, the worst case is that a corrupted temporary file exists on the disk. | |
Result< void, FileError > | write_file_direct (const char *path, Slice< const u8 > data) const |
Overwrites a file's contents directly Will sync file to disk. | |
Result< Slice< u8 >, FileError > | read_file_into (const char *path, Slice< u8 > out) const |
Reads a file into a static buffer. | |
Result< Slice< char >, FileError > | read_file_str_into (const char *path, const Slice< char > out) const |
Reads a file into a static buffer of chars (for text data). | |
Result< void, FileError > | read_file (Allocator &alloc, const char *path, Slice< u8 > &out) const |
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer. | |
Result< void, FileError > | read_file_str (Allocator &alloc, const char *path, Slice< char > &out) const |
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer The buffer will be a buffer of chars for text data On success, returns 0, otherwise returns -1 NOTE: the caller MUST free the allocated memory by calling a_free(<allocator>, out.head) | |
Result< Slice< u8 >, FileError > | read_chunk_into (FILE *file, const Slice< u8 > out) const |
Reads a chunk of characters from a file handle (without closing) into a buffer On success, returns a buffer that's a substring into the buffer On failure, returns a buffer that has a start of NULL and length of 0. | |
Result< Slice< char >, FileError > | read_chunk_str_into (FILE *file, const Slice< char > out) const |
Reads a chunk of characters from a file handle (without closing) into a buffer Uses a buffer of chars for text data On success, returns a buffer that's a substring into the buffer On failure, returns a buffer that has a start of NULL and length of 0. | |
Result< void, FileError > | write_chunk (FILE *file, const Slice< const u8 > buff) const |
Writes a chunk of data into a file handle (without closing) from a buffer If the entire buffer could not be written, will return -1. | |
Result< void, FileError > | write_chunk_str (FILE *file, const Slice< const char > buff) const |
Writes a chunk of data into a file handle (without closing) from a buffer of chars (text) If the entire buffer could not be written, will return -1. | |
Result< FILE *, FileError > | open (const char *path, const char *mode) const |
Opens a file handle On failure, returns NULL. | |
Result< void, FileError > | sync (FILE *file) const |
Syncs a file to disk. | |
Result< void, FileError > | close (FILE *file) const |
Closes a file handle. | |
Result< void, FileError > | rename (const char *oldPath, const char *newPath) const |
Renames a file. | |
Result< void, FileError > | remove (const char *path) const |
Removes a file. | |
Public Attributes | |
const SyncFileSystemVTable * | vtable = nullptr |
Underlying vtable for file operations. | |
void * | state = nullptr |
Synchronous File System interface for handling I/O calls This is encapsulated as a struct of file pointers so we can do deterministic testing This allows us to also dynamically swap things out for different file systems, or even do remote "file systems" where we're reading and writing to an object store (e.g.
S3, file share, SFTP, etc) Also, we could extend it to have more locks behind the scenes or whatever Note: This does NOT perform asynchronous I/O, only synchronous I/O
Only need to define SyncFileSystemVTable to create a simulated file system, or to change underlying system calls
Definition at line 138 of file file_sys.hpp.
Closes a file handle.
file | File pointer to close |
|
nodiscard |
Opens a file handle On failure, returns NULL.
path | File path to open |
mode | File mode to use (e.g. "rb") |
|
nodiscard |
Reads a chunk of characters from a file handle (without closing) into a buffer On success, returns a buffer that's a substring into the buffer On failure, returns a buffer that has a start of NULL and length of 0.
file | File pointer to read from |
out | Buffer to read into. Will read at most out.len characters |
|
nodiscard |
Reads a chunk of characters from a file handle (without closing) into a buffer Uses a buffer of chars for text data On success, returns a buffer that's a substring into the buffer On failure, returns a buffer that has a start of NULL and length of 0.
file | File pointer to read from |
out | Buffer to read into. Will read at most out.len characters |
|
nodiscard |
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer.
On failure returns error NOTE: the caller MUST free the allocated memory by calling a_free(<allocator>, out.head)
alloc | Allocator to use to allocate buffer for file contents |
path | File path to read from |
out | Reference to Slice that will hold the allocated file contents |
|
nodiscard |
Reads a file into a static buffer.
On success, will return a buffer that's a subset of the original buffer with written contents On failure, will return a buffer that's set to NULL with length 0
path | File path to read from |
out | Output buffer to write to. Will not allocate |
|
nodiscard |
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer The buffer will be a buffer of chars for text data On success, returns 0, otherwise returns -1 NOTE: the caller MUST free the allocated memory by calling a_free(<allocator>, out.head)
alloc | Allocator to use to allocate buffer for file contents |
path | File path to read from |
out | Reference to Slice that will hold the allocated file contents |
|
nodiscard |
Reads a file into a static buffer of chars (for text data).
On success, will return a buffer that's a subset of the original buffer with written contents On failure, will return a buffer that's set to NULL with length 0
path | File path to read from |
out | Output buffer to write to. Will not allocate |
Removes a file.
path | Target file name |
|
nodiscard |
Renames a file.
oldPath | Current file name |
newPath | Target file name |
Syncs a file to disk.
file | File pointer to sync |
|
nodiscard |
Writes a chunk of data into a file handle (without closing) from a buffer If the entire buffer could not be written, will return -1.
file | File pointer to write to |
buff | Data to write to file |
|
nodiscard |
Writes a chunk of data into a file handle (without closing) from a buffer of chars (text) If the entire buffer could not be written, will return -1.
file | File pointer to write to |
buff | Data to write to file |
Result< void, FileError > mtcore::io::SyncFileSystem::write_file_direct | ( | const char * | path, |
Slice< const u8 > | data ) const |
Overwrites a file's contents directly Will sync file to disk.
path | Final destination path |
data | Data two write to disk |
Result< void, FileError > mtcore::io::SyncFileSystem::write_file_safe | ( | Allocator & | tmpAlloc, |
const char * | path, | ||
Slice< const u8 > | data, | ||
Prng & | prng ) const |
Overwrites a file's contents by first creating a temporary file and then doing a rename Will sync file to disk If there is a system failure, the worst case is that a corrupted temporary file exists on the disk.
tmpAlloc | Allocator for temporary, internal allocations (allocates temporary file name) |
path | Final destination path |
data | Data two write to disk |
prng | PRNG to use to create temporary file name |
void* mtcore::io::SyncFileSystem::state = nullptr |
Definition at line 144 of file file_sys.hpp.
const SyncFileSystemVTable* mtcore::io::SyncFileSystem::vtable = nullptr |
Underlying vtable for file operations.
Definition at line 143 of file file_sys.hpp.