Skip to content

Commit fa56c42

Browse files
author
MarcoFalke
committed
Return CAutoFile from BlockManager::Open*File()
This is a refactor.
1 parent 9999b89 commit fa56c42

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
7979
return false;
8080
}
8181

82-
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true), CLIENT_VERSION};
82+
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
8383
if (file.IsNull()) {
8484
return error("%s: OpenBlockFile failed", __func__);
8585
}

src/node/blockstorage.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ bool BlockManager::LoadBlockIndexDB()
459459
}
460460
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
461461
FlatFilePos pos(*it, 0);
462-
if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) {
462+
if (OpenBlockFile(pos, true).IsNull()) {
463463
return false;
464464
}
465465
}
@@ -592,7 +592,7 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
592592
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const
593593
{
594594
// Open history file to append
595-
AutoFile fileout{OpenUndoFile(pos)};
595+
CAutoFile fileout{OpenUndoFile(pos)};
596596
if (fileout.IsNull()) {
597597
return error("%s: OpenUndoFile failed", __func__);
598598
}
@@ -627,7 +627,7 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
627627
}
628628

629629
// Open history file to read
630-
AutoFile filein{OpenUndoFile(pos, true)};
630+
CAutoFile filein{OpenUndoFile(pos, true)};
631631
if (filein.IsNull()) {
632632
return error("%s: OpenUndoFile failed", __func__);
633633
}
@@ -715,15 +715,15 @@ FlatFileSeq BlockManager::UndoFileSeq() const
715715
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
716716
}
717717

718-
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
718+
CAutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
719719
{
720-
return BlockFileSeq().Open(pos, fReadOnly);
720+
return CAutoFile{BlockFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
721721
}
722722

723723
/** Open an undo file (rev?????.dat) */
724-
FILE* BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
724+
CAutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
725725
{
726-
return UndoFileSeq().Open(pos, fReadOnly);
726+
return CAutoFile{UndoFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
727727
}
728728

729729
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
@@ -824,7 +824,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
824824
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
825825
{
826826
// Open history file to append
827-
CAutoFile fileout{OpenBlockFile(pos), CLIENT_VERSION};
827+
CAutoFile fileout{OpenBlockFile(pos)};
828828
if (fileout.IsNull()) {
829829
return error("WriteBlockToDisk: OpenBlockFile failed");
830830
}
@@ -880,7 +880,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
880880
block.SetNull();
881881

882882
// Open history file to read
883-
CAutoFile filein{OpenBlockFile(pos, true), CLIENT_VERSION};
883+
CAutoFile filein{OpenBlockFile(pos, true)};
884884
if (filein.IsNull()) {
885885
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
886886
}
@@ -923,7 +923,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
923923
{
924924
FlatFilePos hpos = pos;
925925
hpos.nPos -= 8; // Seek back 8 bytes for meta header
926-
AutoFile filein{OpenBlockFile(hpos, true)};
926+
CAutoFile filein{OpenBlockFile(hpos, true)};
927927
if (filein.IsNull()) {
928928
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
929929
}
@@ -1015,7 +1015,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
10151015
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
10161016
break; // No block files left to reindex
10171017
}
1018-
CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true), CLIENT_VERSION};
1018+
CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
10191019
if (file.IsNull()) {
10201020
break; // This error is logged in OpenBlockFile
10211021
}

src/node/blockstorage.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <vector>
3030

3131
class BlockValidationState;
32+
class CAutoFile;
3233
class CBlock;
3334
class CBlockFileInfo;
3435
class CBlockUndo;
@@ -126,7 +127,7 @@ class BlockManager
126127
FlatFileSeq BlockFileSeq() const;
127128
FlatFileSeq UndoFileSeq() const;
128129

129-
FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
130+
CAutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
130131

131132
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
132133
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
@@ -278,7 +279,7 @@ class BlockManager
278279
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
279280

280281
/** Open a block file (blk?????.dat) */
281-
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
282+
CAutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
282283

283284
/** Translation to a filesystem path */
284285
fs::path GetBlockPosFilename(const FlatFilePos& pos) const;

src/test/blockmanager_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain
7474
// Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
7575
// if m_have_pruned is not yet set
7676
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
77-
BOOST_CHECK(!AutoFile(blockman.OpenBlockFile(pos, true)).IsNull());
77+
BOOST_CHECK(!blockman.OpenBlockFile(pos, true).IsNull());
7878

7979
// Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
8080
// once m_have_pruned is set
8181
blockman.m_have_pruned = true;
8282
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
83-
BOOST_CHECK(AutoFile(blockman.OpenBlockFile(pos, true)).IsNull());
83+
BOOST_CHECK(blockman.OpenBlockFile(pos, true).IsNull());
8484

8585
// Check that calling with already pruned files doesn't cause an error
8686
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
@@ -90,7 +90,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain
9090
BOOST_CHECK_NE(old_tip, new_tip);
9191
const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)};
9292
const FlatFilePos new_pos(new_file_number, 0);
93-
BOOST_CHECK(!AutoFile(blockman.OpenBlockFile(new_pos, true)).IsNull());
93+
BOOST_CHECK(!blockman.OpenBlockFile(new_pos, true).IsNull());
9494
}
9595

9696
BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)

0 commit comments

Comments
 (0)