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");
21 return strprintf(
"FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);
33 fs::create_directories(path.parent_path());
35 if (!file && !read_only) {
39 LogPrintf(
"Unable to open file %s\n", path.string());
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);
66 LogPrintf(
"Pre-allocating up to position 0x%x in %s%05u.dat\n",
83 return error(
"%s: failed to open file %d", __func__, pos.
nFile);
87 return error(
"%s: failed to truncate file %d", __func__, pos.
nFile);
91 return error(
"%s: failed to commit file %d", __func__, pos.
nFile);
bool FileCommit(FILE *file)
FILE * fopen(const fs::path &p, const char *mode)
static void LogPrintf(const char *fmt, const Args &... args)
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool Flush(const FlatFilePos &pos, bool finalize=false)
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
const char *const m_prefix
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...
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.
std::string ToString() const
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
bool TruncateFile(FILE *file, unsigned int length)
const size_t m_chunk_size
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
bool error(const char *fmt, const Args &... args)