diff options
-rw-r--r-- | src/flatfile.cpp | 24 | ||||
-rw-r--r-- | src/flatfile.h | 3 | ||||
-rw-r--r-- | src/validation.cpp | 27 |
3 files changed, 29 insertions, 25 deletions
diff --git a/src/flatfile.cpp b/src/flatfile.cpp index c9ca9aa869..535f4eda91 100644 --- a/src/flatfile.cpp +++ b/src/flatfile.cpp @@ -5,6 +5,7 @@ #include <stdexcept> #include <flatfile.h> +#include <logging.h> #include <tinyformat.h> FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) : @@ -21,3 +22,26 @@ fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const { return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile); } + +FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly) +{ + if (pos.IsNull()) + return nullptr; + fs::path path = FileName(pos); + fs::create_directories(path.parent_path()); + FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+"); + if (!file && !fReadOnly) + file = fsbridge::fopen(path, "wb+"); + if (!file) { + LogPrintf("Unable to open file %s\n", path.string()); + return nullptr; + } + if (pos.nPos) { + if (fseek(file, pos.nPos, SEEK_SET)) { + LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string()); + fclose(file); + return nullptr; + } + } + return file; +} diff --git a/src/flatfile.h b/src/flatfile.h index 9c7131d201..b1eea93140 100644 --- a/src/flatfile.h +++ b/src/flatfile.h @@ -31,6 +31,9 @@ public: /** Get the name of the file at the given position. */ fs::path FileName(const CDiskBlockPos& pos) const; + + /** Open a handle to the file at the given position. */ + FILE* Open(const CDiskBlockPos& pos, bool fReadOnly = false); }; #endif // BITCOIN_FLATFILE_H diff --git a/src/validation.cpp b/src/validation.cpp index 3abe8b2fa1..4135063505 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3769,29 +3769,6 @@ static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfte nLastBlockWeCanPrune, count); } -static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) -{ - if (pos.IsNull()) - return nullptr; - fs::path path = GetBlockPosFilename(pos, prefix); - fs::create_directories(path.parent_path()); - FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+"); - if (!file && !fReadOnly) - file = fsbridge::fopen(path, "wb+"); - if (!file) { - LogPrintf("Unable to open file %s\n", path.string()); - return nullptr; - } - if (pos.nPos) { - if (fseek(file, pos.nPos, SEEK_SET)) { - LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string()); - fclose(file); - return nullptr; - } - } - return file; -} - static FlatFileSeq BlockFileSeq() { return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE); @@ -3803,12 +3780,12 @@ static FlatFileSeq UndoFileSeq() } FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) { - return OpenDiskFile(pos, "blk", fReadOnly); + return BlockFileSeq().Open(pos, fReadOnly); } /** Open an undo file (rev?????.dat) */ static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) { - return OpenDiskFile(pos, "rev", fReadOnly); + return UndoFileSeq().Open(pos, fReadOnly); } fs::path GetBlockPosFilename(const CDiskBlockPos &pos) |