14 : m_dir(
std::move(dir)), m_prefix(
prefix), m_chunk_size(chunk_size) {
15 if (chunk_size == 0) {
16 throw std::invalid_argument(
"chunk_size must be positive");
35 if (!file && !read_only) {
42 if (pos.
nPos && fseek(file, pos.
nPos, SEEK_SET)) {
56 unsigned int n_new_chunks =
58 if (n_new_chunks > n_old_chunks) {
59 size_t old_size = pos.
nPos;
61 size_t inc_size = new_size - old_size;
64 FILE *file =
Open(pos);
67 "Pre-allocating up to position 0x%x in %s%05u.dat\n",
84 return error(
"%s: failed to open file %d", __func__, pos.
nFile);
88 return error(
"%s: failed to truncate file %d", __func__, pos.
nFile);
92 return error(
"%s: failed to commit file %d", __func__, pos.
nFile);
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
const size_t m_chunk_size
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space)
Allocate additional space in a file after the given starting position.
bool Flush(const FlatFilePos &pos, bool finalize=false)
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
const char *const m_prefix
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
This function tries to make a particular range of a file allocated (corresponding to disk space) it i...
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool TruncateFile(FILE *file, unsigned int length)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
bool error(const char *fmt, const Args &...args)
#define LogPrint(category,...)
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
std::string ToString() const