Skip to content

Commit e2d2abb

Browse files
committed
validation: Refactor OpenDiskFile into method on FlatFileSeq.
1 parent 9183d6e commit e2d2abb

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/flatfile.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdexcept>
66

77
#include <flatfile.h>
8+
#include <logging.h>
89
#include <tinyformat.h>
910

1011
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
@@ -21,3 +22,26 @@ fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const
2122
{
2223
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
2324
}
25+
26+
FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly)
27+
{
28+
if (pos.IsNull())
29+
return nullptr;
30+
fs::path path = FileName(pos);
31+
fs::create_directories(path.parent_path());
32+
FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+");
33+
if (!file && !fReadOnly)
34+
file = fsbridge::fopen(path, "wb+");
35+
if (!file) {
36+
LogPrintf("Unable to open file %s\n", path.string());
37+
return nullptr;
38+
}
39+
if (pos.nPos) {
40+
if (fseek(file, pos.nPos, SEEK_SET)) {
41+
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
42+
fclose(file);
43+
return nullptr;
44+
}
45+
}
46+
return file;
47+
}

src/flatfile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class FlatFileSeq
3131

3232
/** Get the name of the file at the given position. */
3333
fs::path FileName(const CDiskBlockPos& pos) const;
34+
35+
/** Open a handle to the file at the given position. */
36+
FILE* Open(const CDiskBlockPos& pos, bool fReadOnly = false);
3437
};
3538

3639
#endif // BITCOIN_FLATFILE_H

src/validation.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3769,29 +3769,6 @@ static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfte
37693769
nLastBlockWeCanPrune, count);
37703770
}
37713771

3772-
static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
3773-
{
3774-
if (pos.IsNull())
3775-
return nullptr;
3776-
fs::path path = GetBlockPosFilename(pos, prefix);
3777-
fs::create_directories(path.parent_path());
3778-
FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+");
3779-
if (!file && !fReadOnly)
3780-
file = fsbridge::fopen(path, "wb+");
3781-
if (!file) {
3782-
LogPrintf("Unable to open file %s\n", path.string());
3783-
return nullptr;
3784-
}
3785-
if (pos.nPos) {
3786-
if (fseek(file, pos.nPos, SEEK_SET)) {
3787-
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3788-
fclose(file);
3789-
return nullptr;
3790-
}
3791-
}
3792-
return file;
3793-
}
3794-
37953772
static FlatFileSeq BlockFileSeq()
37963773
{
37973774
return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE);
@@ -3803,12 +3780,12 @@ static FlatFileSeq UndoFileSeq()
38033780
}
38043781

38053782
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3806-
return OpenDiskFile(pos, "blk", fReadOnly);
3783+
return BlockFileSeq().Open(pos, fReadOnly);
38073784
}
38083785

38093786
/** Open an undo file (rev?????.dat) */
38103787
static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3811-
return OpenDiskFile(pos, "rev", fReadOnly);
3788+
return UndoFileSeq().Open(pos, fReadOnly);
38123789
}
38133790

38143791
fs::path GetBlockPosFilename(const CDiskBlockPos &pos)

0 commit comments

Comments
 (0)