streams: remove AutoFile::Get() entirely

Co-Authored-By: David Gumberg <davidzgumberg@gmail.com>
This commit is contained in:
Pieter Wuille 2024-09-13 21:59:29 -04:00
parent e624a9bef1
commit a240e150e8
7 changed files with 27 additions and 14 deletions

View file

@ -73,7 +73,7 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
remove(pathTmp); remove(pathTmp);
return false; return false;
} }
if (!FileCommit(fileout.Get())) { if (!fileout.Commit()) {
fileout.fclose(); fileout.fclose();
remove(pathTmp); remove(pathTmp);
LogError("%s: Failed to flush file %s\n", __func__, fs::PathToString(pathTmp)); LogError("%s: Failed to flush file %s\n", __func__, fs::PathToString(pathTmp));

View file

@ -151,7 +151,7 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile); LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
return false; return false;
} }
if (!FileCommit(file.Get())) { if (!file.Commit()) {
LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile); LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
return false; 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); LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
return 0; 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); LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
return 0; return 0;
} }
if (!FileCommit(last_file.Get())) { if (!last_file.Commit()) {
LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile); LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
return 0; return 0;
} }

View file

@ -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()); LogInfo("Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size());
file << unbroadcast_txids; file << unbroadcast_txids;
if (!skip_file_commit && !FileCommit(file.Get())) if (!skip_file_commit && !file.Commit())
throw std::runtime_error("FileCommit failed"); throw std::runtime_error("Commit failed");
file.fclose(); file.fclose();
if (!RenameOver(dump_path + ".new", dump_path)) { if (!RenameOver(dump_path + ".new", dump_path)) {
throw std::runtime_error("Rename failed"); throw std::runtime_error("Rename failed");

View file

@ -77,7 +77,7 @@ std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
afile.seek(0, SEEK_END); afile.seek(0, SEEK_END);
if (position != afile.tell()) { if (position != afile.tell()) {
LogPrintf("[snapshot] warning: unexpected trailing data in %s\n", read_from_str); 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); LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str);
} }
return base_blockhash; return base_blockhash;

View file

@ -4,6 +4,7 @@
#include <span.h> #include <span.h>
#include <streams.h> #include <streams.h>
#include <util/fs_helpers.h>
#include <array> #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);
}

View file

@ -420,12 +420,6 @@ public:
return ret; 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. /** Return true if the wrapped FILE* is nullptr, false otherwise.
*/ */
bool IsNull() const { return m_file == nullptr; } bool IsNull() const { return m_file == nullptr; }
@ -459,6 +453,10 @@ public:
::Unserialize(*this, obj); ::Unserialize(*this, obj);
return *this; return *this;
} }
bool Commit();
bool IsError();
bool Truncate(unsigned size);
}; };
/** Wrapper around an AutoFile& that implements a ring buffer to /** Wrapper around an AutoFile& that implements a ring buffer to

View file

@ -56,7 +56,6 @@ FUZZ_TARGET(autofile)
WriteToStream(fuzzed_data_provider, auto_file); WriteToStream(fuzzed_data_provider, auto_file);
}); });
} }
(void)auto_file.Get();
(void)auto_file.IsNull(); (void)auto_file.IsNull();
if (fuzzed_data_provider.ConsumeBool()) { if (fuzzed_data_provider.ConsumeBool()) {
FILE* f = auto_file.release(); FILE* f = auto_file.release();