Diff-minimise

This commit is contained in:
Luke Dashjr 2024-06-22 02:49:54 +00:00
parent d2ea6ab60b
commit eddc01a795
6 changed files with 10 additions and 15 deletions

View File

@ -63,18 +63,19 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
FILE *file = fsbridge::fopen(pathTmp, "wb"); FILE *file = fsbridge::fopen(pathTmp, "wb");
AutoFile fileout{file}; AutoFile fileout{file};
if (fileout.IsNull()) { if (fileout.IsNull()) {
fileout.fclose();
remove(pathTmp); remove(pathTmp);
return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp)); return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp));
} }
// Serialize // Serialize
if (!SerializeDB(fileout, data)) { if (!SerializeDB(fileout, data)) {
(void)fileout.fclose(); fileout.fclose();
remove(pathTmp); remove(pathTmp);
return false; return false;
} }
if (!FileCommit(fileout.Get())) { if (!FileCommit(fileout.Get())) {
(void)fileout.fclose(); fileout.fclose();
remove(pathTmp); remove(pathTmp);
return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp)); return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp));
} }

View File

@ -28,7 +28,7 @@ static void FindByte(benchmark::Bench& bench)
}); });
// Cleanup // Cleanup
(void)file.fclose(); file.fclose();
fs::remove("streams_tmp"); fs::remove("streams_tmp");
} }

View File

@ -169,8 +169,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
auto mid = SteadyClock::now(); auto mid = SteadyClock::now();
const fs::path file_fspath{dump_path + ".new"}; AutoFile file{mockable_fopen_function(dump_path + ".new", "wb")};
AutoFile file{mockable_fopen_function(file_fspath, "wb")};
if (file.IsNull()) { if (file.IsNull()) {
return false; return false;
} }
@ -202,6 +201,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
if (!skip_file_commit && !FileCommit(file.Get())) if (!skip_file_commit && !FileCommit(file.Get()))
throw std::runtime_error("FileCommit failed"); throw std::runtime_error("FileCommit failed");
if (file.fclose() != 0) { if (file.fclose() != 0) {
const fs::path file_fspath{dump_path + ".new"};
throw std::runtime_error( throw std::runtime_error(
strprintf("Error closing %s: %s", fs::PathToString(file_fspath), SysErrorString(errno))); strprintf("Error closing %s: %s", fs::PathToString(file_fspath), SysErrorString(errno)));
} }

View File

@ -384,13 +384,7 @@ public:
* *
* Will automatically close the file when it goes out of scope if not null. * Will automatically close the file when it goes out of scope if not null.
* If you're returning the file pointer, return file.release(). * If you're returning the file pointer, return file.release().
* If you need to close the file early, use autofile.fclose() instead of fclose(underlying_FILE). * If you need to close the file early, use file.fclose() instead of fclose(file).
*
* @note If the file has been written to, then the caller must close it
* explicitly with the `fclose()` method, check if it returns an error and treat
* such an error as if the `write()` method failed. The OS's `fclose(3)` may
* fail to flush to disk data that has been previously written, rendering the
* file corrupt.
*/ */
class AutoFile class AutoFile
{ {
@ -422,7 +416,7 @@ public:
bool feof() const { return std::feof(m_file); } bool feof() const { return std::feof(m_file); }
[[nodiscard]] int fclose() int fclose()
{ {
if (auto rel{release()}) return std::fclose(rel); if (auto rel{release()}) return std::fclose(rel);
return 0; return 0;

View File

@ -47,7 +47,7 @@ FUZZ_TARGET(autofile)
} }
}, },
[&] { [&] {
(void)auto_file.fclose(); auto_file.fclose();
}, },
[&] { [&] {
ReadFromStream(fuzzed_data_provider, auto_file); ReadFromStream(fuzzed_data_provider, auto_file);

View File

@ -48,7 +48,7 @@ CreateAndActivateUTXOSnapshot(
AutoFile auto_outfile{outfile}; AutoFile auto_outfile{outfile};
UniValue result = CreateUTXOSnapshot( UniValue result = CreateUTXOSnapshot(
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path); // Will close auto_outfile. node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
LogPrintf( LogPrintf(
"Wrote UTXO snapshot to %s: %s\n", fs::PathToString(snapshot_path.make_preferred()), result.write()); "Wrote UTXO snapshot to %s: %s\n", fs::PathToString(snapshot_path.make_preferred()), result.write());