aboutsummaryrefslogtreecommitdiff
path: root/src/flatfile.cpp
diff options
context:
space:
mode:
authorJim Posen <jim.posen@gmail.com>2019-01-06 11:14:22 -0800
committerJim Posen <jim.posen@gmail.com>2019-02-22 17:38:45 -0800
commite2d2abb99fe353ffc2ff3bc1ff578fad31065335 (patch)
treed2085909eecec7d13d78cb111bc466fa4f072480 /src/flatfile.cpp
parent9183d6ef656c8f3ed406821b99827f9b5f047665 (diff)
validation: Refactor OpenDiskFile into method on FlatFileSeq.
Diffstat (limited to 'src/flatfile.cpp')
-rw-r--r--src/flatfile.cpp24
1 files changed, 24 insertions, 0 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;
+}