MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::io::SyncFileSystem Struct Reference

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>

Collaboration diagram for mtcore::io::SyncFileSystem:

Public Member Functions

Result< void, FileErrorwrite_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, FileErrorwrite_file_direct (const char *path, Slice< const u8 > data) const
 Overwrites a file's contents directly Will sync file to disk.
 
Result< Slice< u8 >, FileErrorread_file_into (const char *path, Slice< u8 > out) const
 Reads a file into a static buffer.
 
Result< Slice< char >, FileErrorread_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, FileErrorread_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, FileErrorread_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 >, FileErrorread_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 >, FileErrorread_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, FileErrorwrite_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, FileErrorwrite_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 *, FileErroropen (const char *path, const char *mode) const
 Opens a file handle On failure, returns NULL.
 
Result< void, FileErrorsync (FILE *file) const
 Syncs a file to disk.
 
Result< void, FileErrorclose (FILE *file) const
 Closes a file handle.
 
Result< void, FileErrorrename (const char *oldPath, const char *newPath) const
 Renames a file.
 
Result< void, FileErrorremove (const char *path) const
 Removes a file.
 

Public Attributes

const SyncFileSystemVTablevtable = nullptr
 Underlying vtable for file operations.
 
void * state = nullptr
 

Detailed Description

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.

Member Function Documentation

◆ close()

Result< void, FileError > mtcore::io::SyncFileSystem::close ( FILE * file) const
nodiscard

Closes a file handle.

Parameters
fileFile pointer to close
Returns
On failure returns error

◆ open()

Result< FILE *, FileError > mtcore::io::SyncFileSystem::open ( const char * path,
const char * mode ) const
nodiscard

Opens a file handle On failure, returns NULL.

Parameters
pathFile path to open
modeFile mode to use (e.g. "rb")
Returns
File pointer on success, nullptr on failure

◆ read_chunk_into()

Result< Slice< u8 >, FileError > mtcore::io::SyncFileSystem::read_chunk_into ( FILE * file,
const Slice< u8 > out ) const
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.

Parameters
fileFile pointer to read from
outBuffer to read into. Will read at most out.len characters
Returns
Sub-Slice of characters read. Empty if at end or unable to read

◆ read_chunk_str_into()

Result< Slice< char >, FileError > mtcore::io::SyncFileSystem::read_chunk_str_into ( FILE * file,
const Slice< char > out ) const
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.

Parameters
fileFile pointer to read from
outBuffer to read into. Will read at most out.len characters
Returns
Sub-Slice of characters read. Empty if at end or unable to read

◆ read_file()

Result< void, FileError > mtcore::io::SyncFileSystem::read_file ( Allocator & alloc,
const char * path,
Slice< u8 > & out ) const
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)

Parameters
allocAllocator to use to allocate buffer for file contents
pathFile path to read from
outReference to Slice that will hold the allocated file contents
Returns
On failure returns error

◆ read_file_into()

Result< Slice< u8 >, FileError > mtcore::io::SyncFileSystem::read_file_into ( const char * path,
Slice< u8 > out ) const
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

Parameters
pathFile path to read from
outOutput buffer to write to. Will not allocate
Returns
Sub-Slice containing file contents (in case full Slice was too large) or an error

◆ read_file_str()

Result< void, FileError > mtcore::io::SyncFileSystem::read_file_str ( Allocator & alloc,
const char * path,
Slice< char > & out ) const
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)

Parameters
allocAllocator to use to allocate buffer for file contents
pathFile path to read from
outReference to Slice that will hold the allocated file contents
Returns
On failure returns error

◆ read_file_str_into()

Result< Slice< char >, FileError > mtcore::io::SyncFileSystem::read_file_str_into ( const char * path,
const Slice< char > out ) const
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

Parameters
pathFile path to read from
outOutput buffer to write to. Will not allocate
Returns
Sub-Slice containing file contents (in case full Slice was too large) or an error

◆ remove()

Result< void, FileError > mtcore::io::SyncFileSystem::remove ( const char * path) const
nodiscard

Removes a file.

Parameters
pathTarget file name
Returns
On failure returns error

◆ rename()

Result< void, FileError > mtcore::io::SyncFileSystem::rename ( const char * oldPath,
const char * newPath ) const
nodiscard

Renames a file.

Parameters
oldPathCurrent file name
newPathTarget file name
Returns
On failure returns error

◆ sync()

Result< void, FileError > mtcore::io::SyncFileSystem::sync ( FILE * file) const
nodiscard

Syncs a file to disk.

Parameters
fileFile pointer to sync
Returns
On failure returns error

◆ write_chunk()

Result< void, FileError > mtcore::io::SyncFileSystem::write_chunk ( FILE * file,
const Slice< const u8 > buff ) const
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.

Parameters
fileFile pointer to write to
buffData to write to file
Returns
On failure returns error

◆ write_chunk_str()

Result< void, FileError > mtcore::io::SyncFileSystem::write_chunk_str ( FILE * file,
const Slice< const char > buff ) const
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.

Parameters
fileFile pointer to write to
buffData to write to file
Returns
On failure returns error

◆ write_file_direct()

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.

Parameters
pathFinal destination path
dataData two write to disk
Returns
success or error code otherwise

◆ write_file_safe()

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.

Parameters
tmpAllocAllocator for temporary, internal allocations (allocates temporary file name)
pathFinal destination path
dataData two write to disk
prngPRNG to use to create temporary file name
Returns
success or error code otherwise

Member Data Documentation

◆ state

void* mtcore::io::SyncFileSystem::state = nullptr

Definition at line 144 of file file_sys.hpp.

◆ vtable

const SyncFileSystemVTable* mtcore::io::SyncFileSystem::vtable = nullptr

Underlying vtable for file operations.

Definition at line 143 of file file_sys.hpp.


The documentation for this struct was generated from the following file: