Merge branch 'AutoFile_error_check-26' into mining_priority

This commit is contained in:
Luke Dashjr 2024-02-23 15:29:53 +00:00
commit e2f55d92f9
4 changed files with 21 additions and 5 deletions

View File

@ -22,6 +22,7 @@
#include <univalue.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/syserror.h>
#include <util/translation.h>
namespace {
@ -74,7 +75,10 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
remove(pathTmp);
return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp));
}
fileout.fclose();
if (fileout.fclose() != 0) {
remove(pathTmp);
return error("%s: Failed to close file %s: %s", __func__, fs::PathToString(pathTmp), SysErrorString(errno));
}
// replace existing file, if any, with new file
if (!RenameOver(pathTmp, path)) {

View File

@ -16,6 +16,7 @@
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/signalinterrupt.h>
#include <util/syserror.h>
#include <util/time.h>
#include <validation.h>
@ -187,7 +188,11 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
if (!skip_file_commit && !FileCommit(file.Get()))
throw std::runtime_error("FileCommit failed");
file.fclose();
if (file.fclose() != 0) {
const fs::path file_fspath{dump_path + ".new"};
throw std::runtime_error(
strprintf("Error closing %s: %s", fs::PathToString(file_fspath), SysErrorString(errno)));
}
if (!RenameOver(dump_path + ".new", dump_path)) {
throw std::runtime_error("Rename failed");
}

View File

@ -42,6 +42,7 @@
#include <util/check.h>
#include <util/fs.h>
#include <util/strencodings.h>
#include <util/syserror.h>
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
@ -2686,7 +2687,10 @@ UniValue CreateUTXOSnapshot(
pcursor->Next();
}
afile.fclose();
if (afile.fclose() != 0) {
throw std::ios_base::failure(
strprintf("Error closing %s: %s", fs::PathToString(temppath), SysErrorString(errno)));
}
UniValue result(UniValue::VOBJ);
result.pushKV("coins_written", maybe_stats->coins_count);

View File

@ -4,6 +4,8 @@
#include <span.h>
#include <streams.h>
#include <tinyformat.h>
#include <util/syserror.h>
#include <array>
@ -23,8 +25,9 @@ std::size_t AutoFile::detail_fread(Span<std::byte> dst)
void AutoFile::read(Span<std::byte> dst)
{
if (detail_fread(dst) != dst.size()) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
if (detail_fread(dst) != dst.size() || ferror(m_file)) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" :
strprintf("AutoFile::read: error: %s", SysErrorString(errno)));
}
}