diff options
author | Pieter Wuille <pieter@wuille.net> | 2024-09-13 21:59:29 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2024-09-13 21:59:29 -0400 |
commit | a240e150e837b5a95ed19765a2e8b7c5b6013f35 (patch) | |
tree | 86ece1ede351f9bc090623a78325dcea8c7ec410 | |
parent | e624a9bef16b6335fd119c10698352b59bf2930a (diff) |
streams: remove AutoFile::Get() entirely
Co-Authored-By: David Gumberg <davidzgumberg@gmail.com>
-rw-r--r-- | src/addrdb.cpp | 2 | ||||
-rw-r--r-- | src/index/blockfilterindex.cpp | 6 | ||||
-rw-r--r-- | src/node/mempool_persist.cpp | 4 | ||||
-rw-r--r-- | src/node/utxo_snapshot.cpp | 2 | ||||
-rw-r--r-- | src/streams.cpp | 16 | ||||
-rw-r--r-- | src/streams.h | 10 | ||||
-rw-r--r-- | src/test/fuzz/autofile.cpp | 1 |
7 files changed, 27 insertions, 14 deletions
diff --git a/src/addrdb.cpp b/src/addrdb.cpp index e9838d7222..b89141c88e 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -73,7 +73,7 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data remove(pathTmp); return false; } - if (!FileCommit(fileout.Get())) { + if (!fileout.Commit()) { fileout.fclose(); remove(pathTmp); LogError("%s: Failed to flush file %s\n", __func__, fs::PathToString(pathTmp)); diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index 41bdca9df5..26de7eee32 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -151,7 +151,7 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch) LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile); return false; } - if (!FileCommit(file.Get())) { + if (!file.Commit()) { LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile); return false; } @@ -201,11 +201,11 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile); return 0; } - if (!TruncateFile(last_file.Get(), pos.nPos)) { + if (!last_file.Truncate(pos.nPos)) { LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile); return 0; } - if (!FileCommit(last_file.Get())) { + if (!last_file.Commit()) { LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile); return 0; } diff --git a/src/node/mempool_persist.cpp b/src/node/mempool_persist.cpp index a265c2e12d..ff7de8c64a 100644 --- a/src/node/mempool_persist.cpp +++ b/src/node/mempool_persist.cpp @@ -199,8 +199,8 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock LogInfo("Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size()); file << unbroadcast_txids; - if (!skip_file_commit && !FileCommit(file.Get())) - throw std::runtime_error("FileCommit failed"); + if (!skip_file_commit && !file.Commit()) + throw std::runtime_error("Commit failed"); file.fclose(); if (!RenameOver(dump_path + ".new", dump_path)) { throw std::runtime_error("Rename failed"); diff --git a/src/node/utxo_snapshot.cpp b/src/node/utxo_snapshot.cpp index dbb93a15b5..7d589c886b 100644 --- a/src/node/utxo_snapshot.cpp +++ b/src/node/utxo_snapshot.cpp @@ -77,7 +77,7 @@ std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir) afile.seek(0, SEEK_END); if (position != afile.tell()) { LogPrintf("[snapshot] warning: unexpected trailing data in %s\n", read_from_str); - } else if (std::ferror(afile.Get())) { + } else if (afile.IsError()) { LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str); } return base_blockhash; diff --git a/src/streams.cpp b/src/streams.cpp index 1ba260f57c..5f7baf92b9 100644 --- a/src/streams.cpp +++ b/src/streams.cpp @@ -4,6 +4,7 @@ #include <span.h> #include <streams.h> +#include <util/fs_helpers.h> #include <array> @@ -99,3 +100,18 @@ void AutoFile::write(Span<const std::byte> src) } } } + +bool AutoFile::Commit() +{ + return ::FileCommit(m_file); +} + +bool AutoFile::IsError() +{ + return ferror(m_file); +} + +bool AutoFile::Truncate(unsigned size) +{ + return ::TruncateFile(m_file, size); +} diff --git a/src/streams.h b/src/streams.h index 2fcb83826e..431a4d77c6 100644 --- a/src/streams.h +++ b/src/streams.h @@ -420,12 +420,6 @@ public: return ret; } - /** Get wrapped FILE* without transfer of ownership. - * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the - * AutoFile outlives use of the passed pointer. - */ - std::FILE* Get() const { return m_file; } - /** Return true if the wrapped FILE* is nullptr, false otherwise. */ bool IsNull() const { return m_file == nullptr; } @@ -459,6 +453,10 @@ public: ::Unserialize(*this, obj); return *this; } + + bool Commit(); + bool IsError(); + bool Truncate(unsigned size); }; /** Wrapper around an AutoFile& that implements a ring buffer to diff --git a/src/test/fuzz/autofile.cpp b/src/test/fuzz/autofile.cpp index 45316b6b21..81761c7bf9 100644 --- a/src/test/fuzz/autofile.cpp +++ b/src/test/fuzz/autofile.cpp @@ -56,7 +56,6 @@ FUZZ_TARGET(autofile) WriteToStream(fuzzed_data_provider, auto_file); }); } - (void)auto_file.Get(); (void)auto_file.IsNull(); if (fuzzed_data_provider.ConsumeBool()) { FILE* f = auto_file.release(); |