mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-03 09:23:01 +01:00
validation: Refactor OpenDiskFile into method on FlatFileSeq.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user