Skip to content

Commit fa389d9

Browse files
author
MarcoFalke
committed
refactor: Drop unused fclose() from BufferedFile
This was only explicitly used in the tests, where it can be replaced by wrapping the original raw file pointer into a CAutoFile on creation and then calling CAutoFile::fclose(). Also, it was used in LoadExternalBlockFile(), where it can also be replaced by the (implicit call to the) CAutoFile destructor after wrapping the original raw file pointer in a CAutoFile.
1 parent f608a40 commit fa389d9

File tree

8 files changed

+42
-57
lines changed

8 files changed

+42
-57
lines changed

src/bench/load_external.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bench/bench.h>
66
#include <bench/data.h>
77
#include <chainparams.h>
8+
#include <clientversion.h>
89
#include <test/util/setup_common.h>
910
#include <util/chaintype.h>
1011
#include <validation.h>
@@ -54,8 +55,8 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
5455
bench.run([&] {
5556
// "rb" is "binary, O_RDONLY", positioned to the start of the file.
5657
// The file will be closed by LoadExternalBlockFile().
57-
FILE* file{fsbridge::fopen(blkfile, "rb")};
58-
testing_setup->m_node.chainman->LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
58+
CAutoFile file{fsbridge::fopen(blkfile, "rb"), CLIENT_VERSION};
59+
testing_setup->m_node.chainman->LoadExternalBlockFile(file.Get(), &pos, &blocks_with_unknown_parent);
5960
});
6061
fs::remove(blkfile);
6162
}

src/bench/streams_findbyte.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@
44

55
#include <bench/bench.h>
66

7-
#include <util/fs.h>
87
#include <streams.h>
8+
#include <util/fs.h>
9+
10+
#include <cstddef>
11+
#include <cstdint>
12+
#include <cstdio>
913

1014
static void FindByte(benchmark::Bench& bench)
1115
{
1216
// Setup
13-
FILE* file = fsbridge::fopen("streams_tmp", "w+b");
17+
CAutoFile file{fsbridge::fopen("streams_tmp", "w+b"), 0};
1418
const size_t file_size = 200;
1519
uint8_t data[file_size] = {0};
1620
data[file_size-1] = 1;
17-
fwrite(&data, sizeof(uint8_t), file_size, file);
18-
rewind(file);
19-
BufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};
21+
file << data;
22+
std::rewind(file.Get());
23+
BufferedFile bf{file.Get(), /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};
2024

2125
bench.run([&] {
2226
bf.SetPos(0);
2327
bf.FindByte(std::byte(1));
2428
});
2529

2630
// Cleanup
27-
bf.fclose();
31+
file.fclose();
2832
fs::remove("streams_tmp");
2933
}
3034

src/node/blockstorage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,12 @@ 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-
FILE* file = chainman.m_blockman.OpenBlockFile(pos, true);
1019-
if (!file) {
1018+
CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true), CLIENT_VERSION};
1019+
if (file.IsNull()) {
10201020
break; // This error is logged in OpenBlockFile
10211021
}
10221022
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
1023-
chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
1023+
chainman.LoadExternalBlockFile(file.Get(), &pos, &blocks_with_unknown_parent);
10241024
if (chainman.m_interrupt) {
10251025
LogPrintf("Interrupt requested. Exit %s\n", __func__);
10261026
return;
@@ -1036,10 +1036,10 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
10361036

10371037
// -loadblock=
10381038
for (const fs::path& path : vImportFiles) {
1039-
FILE* file = fsbridge::fopen(path, "rb");
1040-
if (file) {
1039+
CAutoFile file{fsbridge::fopen(path, "rb"), CLIENT_VERSION};
1040+
if (!file.IsNull()) {
10411041
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
1042-
chainman.LoadExternalBlockFile(file);
1042+
chainman.LoadExternalBlockFile(file.Get());
10431043
if (chainman.m_interrupt) {
10441044
LogPrintf("Interrupt requested. Exit %s\n", __func__);
10451045
return;

src/streams.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -637,25 +637,8 @@ class BufferedFile
637637
src = fileIn;
638638
}
639639

640-
~BufferedFile()
641-
{
642-
fclose();
643-
}
644-
645-
// Disallow copies
646-
BufferedFile(const BufferedFile&) = delete;
647-
BufferedFile& operator=(const BufferedFile&) = delete;
648-
649640
int GetVersion() const { return nVersion; }
650641

651-
void fclose()
652-
{
653-
if (src) {
654-
::fclose(src);
655-
src = nullptr;
656-
}
657-
}
658-
659642
//! check whether we're at the end of the source file
660643
bool eof() const {
661644
return m_read_pos == nSrcPos && feof(src);

src/test/fuzz/buffered_file.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ FUZZ_TARGET(buffered_file)
1919
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2020
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
2121
std::optional<BufferedFile> opt_buffered_file;
22-
FILE* fuzzed_file = fuzzed_file_provider.open();
22+
CAutoFile fuzzed_file{fuzzed_file_provider.open(), 0};
2323
try {
24-
opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>());
24+
opt_buffered_file.emplace(fuzzed_file.Get(), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>());
2525
} catch (const std::ios_base::failure&) {
26-
if (fuzzed_file != nullptr) {
27-
fclose(fuzzed_file);
28-
}
2926
}
30-
if (opt_buffered_file && fuzzed_file != nullptr) {
27+
if (opt_buffered_file && !fuzzed_file.IsNull()) {
3128
bool setpos_fail = false;
3229
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
3330
CallOneOf(

src/test/fuzz/load_external_block_file.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <chainparams.h>
6+
#include <clientversion.h>
67
#include <flatfile.h>
78
#include <test/fuzz/FuzzedDataProvider.h>
89
#include <test/fuzz/fuzz.h>
@@ -27,17 +28,17 @@ FUZZ_TARGET(load_external_block_file, .init = initialize_load_external_block_fil
2728
{
2829
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2930
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
30-
FILE* fuzzed_block_file = fuzzed_file_provider.open();
31-
if (fuzzed_block_file == nullptr) {
31+
CAutoFile fuzzed_block_file{fuzzed_file_provider.open(), CLIENT_VERSION};
32+
if (fuzzed_block_file.IsNull()) {
3233
return;
3334
}
3435
if (fuzzed_data_provider.ConsumeBool()) {
3536
// Corresponds to the -reindex case (track orphan blocks across files).
3637
FlatFilePos flat_file_pos;
3738
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
38-
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file, &flat_file_pos, &blocks_with_unknown_parent);
39+
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file.Get(), &flat_file_pos, &blocks_with_unknown_parent);
3940
} else {
4041
// Corresponds to the -loadblock= case (orphan blocks aren't tracked across files).
41-
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file);
42+
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file.Get());
4243
}
4344
}

src/test/streams_tests.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,26 +249,26 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
249249
BOOST_AUTO_TEST_CASE(streams_buffered_file)
250250
{
251251
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
252-
FILE* file = fsbridge::fopen(streams_test_filename, "w+b");
252+
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
253253

254254
// The value at each offset is the offset.
255255
for (uint8_t j = 0; j < 40; ++j) {
256-
fwrite(&j, 1, 1, file);
256+
file << j;
257257
}
258-
rewind(file);
258+
std::rewind(file.Get());
259259

260260
// The buffer size (second arg) must be greater than the rewind
261261
// amount (third arg).
262262
try {
263-
BufferedFile bfbad{file, 25, 25, 333};
263+
BufferedFile bfbad{file.Get(), 25, 25, 333};
264264
BOOST_CHECK(false);
265265
} catch (const std::exception& e) {
266266
BOOST_CHECK(strstr(e.what(),
267267
"Rewind limit must be less than buffer size") != nullptr);
268268
}
269269

270270
// The buffer is 25 bytes, allow rewinding 10 bytes.
271-
BufferedFile bf{file, 25, 10, 333};
271+
BufferedFile bf{file.Get(), 25, 10, 333};
272272
BOOST_CHECK(!bf.eof());
273273

274274
// This member has no functional effect.
@@ -375,23 +375,23 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
375375
BOOST_CHECK(bf.GetPos() <= 30U);
376376

377377
// We can explicitly close the file, or the destructor will do it.
378-
bf.fclose();
378+
file.fclose();
379379

380380
fs::remove(streams_test_filename);
381381
}
382382

383383
BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
384384
{
385385
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
386-
FILE* file = fsbridge::fopen(streams_test_filename, "w+b");
386+
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
387387
// The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
388388
for (uint8_t j = 0; j < 40; ++j) {
389-
fwrite(&j, 1, 1, file);
389+
file << j;
390390
}
391-
rewind(file);
391+
std::rewind(file.Get());
392392

393393
// The buffer is 25 bytes, allow rewinding 10 bytes.
394-
BufferedFile bf{file, 25, 10, 333};
394+
BufferedFile bf{file.Get(), 25, 10, 333};
395395

396396
uint8_t i;
397397
// This is like bf >> (7-byte-variable), in that it will cause data
@@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
425425
bf.SkipTo(13);
426426
BOOST_CHECK_EQUAL(bf.GetPos(), 13U);
427427

428-
bf.fclose();
428+
file.fclose();
429429
fs::remove(streams_test_filename);
430430
}
431431

@@ -436,16 +436,16 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
436436

437437
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
438438
for (int rep = 0; rep < 50; ++rep) {
439-
FILE* file = fsbridge::fopen(streams_test_filename, "w+b");
439+
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
440440
size_t fileSize = InsecureRandRange(256);
441441
for (uint8_t i = 0; i < fileSize; ++i) {
442-
fwrite(&i, 1, 1, file);
442+
file << i;
443443
}
444-
rewind(file);
444+
std::rewind(file.Get());
445445

446446
size_t bufSize = InsecureRandRange(300) + 1;
447447
size_t rewindSize = InsecureRandRange(bufSize);
448-
BufferedFile bf{file, bufSize, rewindSize, 333};
448+
BufferedFile bf{file.Get(), bufSize, rewindSize, 333};
449449
size_t currentPos = 0;
450450
size_t maxPos = 0;
451451
for (int step = 0; step < 100; ++step) {

src/validation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4589,7 +4589,6 @@ void ChainstateManager::LoadExternalBlockFile(
45894589

45904590
int nLoaded = 0;
45914591
try {
4592-
// This takes over fileIn and calls fclose() on it in the BufferedFile destructor
45934592
BufferedFile blkdat{fileIn, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8, CLIENT_VERSION};
45944593
// nRewind indicates where to resume scanning in case something goes wrong,
45954594
// such as a block fails to deserialize.

0 commit comments

Comments
 (0)