Changeset View
Changeset View
Standalone View
Standalone View
src/flatfile.cpp
Show All 19 Lines | |||||
std::string FlatFilePos::ToString() const { | std::string FlatFilePos::ToString() const { | ||||
return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos); | return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos); | ||||
} | } | ||||
fs::path FlatFileSeq::FileName(const FlatFilePos &pos) const { | fs::path FlatFileSeq::FileName(const FlatFilePos &pos) const { | ||||
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile); | return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile); | ||||
} | } | ||||
FILE *FlatFileSeq::Open(const FlatFilePos &pos, bool fReadOnly) { | FILE *FlatFileSeq::Open(const FlatFilePos &pos, bool read_only) { | ||||
if (pos.IsNull()) { | if (pos.IsNull()) { | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
fs::path path = FileName(pos); | fs::path path = FileName(pos); | ||||
fs::create_directories(path.parent_path()); | fs::create_directories(path.parent_path()); | ||||
FILE *file = fsbridge::fopen(path, fReadOnly ? "rb" : "rb+"); | FILE *file = fsbridge::fopen(path, read_only ? "rb" : "rb+"); | ||||
if (!file && !fReadOnly) { | if (!file && !read_only) { | ||||
file = fsbridge::fopen(path, "wb+"); | file = fsbridge::fopen(path, "wb+"); | ||||
} | } | ||||
if (!file) { | if (!file) { | ||||
LogPrintf("Unable to open file %s\n", path.string()); | LogPrintf("Unable to open file %s\n", path.string()); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
if (pos.nPos) { | if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) { | ||||
if (fseek(file, pos.nPos, SEEK_SET)) { | |||||
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, | LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, | ||||
path.string()); | path.string()); | ||||
fclose(file); | fclose(file); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | |||||
return file; | return file; | ||||
} | } | ||||
size_t FlatFileSeq::Allocate(const FlatFilePos &pos, size_t add_size, | size_t FlatFileSeq::Allocate(const FlatFilePos &pos, size_t add_size, | ||||
bool &out_of_space) { | bool &out_of_space) { | ||||
out_of_space = false; | out_of_space = false; | ||||
unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size; | unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size; | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |