Skip to content

Commit a240e15

Browse files
sipadavidgumberg
andcommitted
streams: remove AutoFile::Get() entirely
Co-Authored-By: David Gumberg <davidzgumberg@gmail.com>
1 parent e624a9b commit a240e15

File tree

7 files changed

+27
-14
lines changed

7 files changed

+27
-14
lines changed

src/addrdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
7373
remove(pathTmp);
7474
return false;
7575
}
76-
if (!FileCommit(fileout.Get())) {
76+
if (!fileout.Commit()) {
7777
fileout.fclose();
7878
remove(pathTmp);
7979
LogError("%s: Failed to flush file %s\n", __func__, fs::PathToString(pathTmp));

src/index/blockfilterindex.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
151151
LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
152152
return false;
153153
}
154-
if (!FileCommit(file.Get())) {
154+
if (!file.Commit()) {
155155
LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
156156
return false;
157157
}
@@ -201,11 +201,11 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
201201
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
202202
return 0;
203203
}
204-
if (!TruncateFile(last_file.Get(), pos.nPos)) {
204+
if (!last_file.Truncate(pos.nPos)) {
205205
LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
206206
return 0;
207207
}
208-
if (!FileCommit(last_file.Get())) {
208+
if (!last_file.Commit()) {
209209
LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
210210
return 0;
211211
}

src/node/mempool_persist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
199199
LogInfo("Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size());
200200
file << unbroadcast_txids;
201201

202-
if (!skip_file_commit && !FileCommit(file.Get()))
203-
throw std::runtime_error("FileCommit failed");
202+
if (!skip_file_commit && !file.Commit())
203+
throw std::runtime_error("Commit failed");
204204
file.fclose();
205205
if (!RenameOver(dump_path + ".new", dump_path)) {
206206
throw std::runtime_error("Rename failed");

src/node/utxo_snapshot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
7777
afile.seek(0, SEEK_END);
7878
if (position != afile.tell()) {
7979
LogPrintf("[snapshot] warning: unexpected trailing data in %s\n", read_from_str);
80-
} else if (std::ferror(afile.Get())) {
80+
} else if (afile.IsError()) {
8181
LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str);
8282
}
8383
return base_blockhash;

src/streams.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <span.h>
66
#include <streams.h>
7+
#include <util/fs_helpers.h>
78

89
#include <array>
910

@@ -99,3 +100,18 @@ void AutoFile::write(Span<const std::byte> src)
99100
}
100101
}
101102
}
103+
104+
bool AutoFile::Commit()
105+
{
106+
return ::FileCommit(m_file);
107+
}
108+
109+
bool AutoFile::IsError()
110+
{
111+
return ferror(m_file);
112+
}
113+
114+
bool AutoFile::Truncate(unsigned size)
115+
{
116+
return ::TruncateFile(m_file, size);
117+
}

src/streams.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,6 @@ class AutoFile
420420
return ret;
421421
}
422422

423-
/** Get wrapped FILE* without transfer of ownership.
424-
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
425-
* AutoFile outlives use of the passed pointer.
426-
*/
427-
std::FILE* Get() const { return m_file; }
428-
429423
/** Return true if the wrapped FILE* is nullptr, false otherwise.
430424
*/
431425
bool IsNull() const { return m_file == nullptr; }
@@ -459,6 +453,10 @@ class AutoFile
459453
::Unserialize(*this, obj);
460454
return *this;
461455
}
456+
457+
bool Commit();
458+
bool IsError();
459+
bool Truncate(unsigned size);
462460
};
463461

464462
/** Wrapper around an AutoFile& that implements a ring buffer to

src/test/fuzz/autofile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ FUZZ_TARGET(autofile)
5656
WriteToStream(fuzzed_data_provider, auto_file);
5757
});
5858
}
59-
(void)auto_file.Get();
6059
(void)auto_file.IsNull();
6160
if (fuzzed_data_provider.ConsumeBool()) {
6261
FILE* f = auto_file.release();

0 commit comments

Comments
 (0)