mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-06 03:02:37 +02:00
Explicitly close all AutoFiles that have been written
There is no way to report a close error from `AutoFile` destructor. Such an error could be serious if the file has been written to because it may mean the file is now corrupted (same as if write fails). So, change all users of `AutoFile` that use it to write data to explicitly close the file and handle a possible error.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <node/blockstorage.h>
|
||||
#include <undo.h>
|
||||
#include <util/fs_helpers.h>
|
||||
#include <util/syserror.h>
|
||||
|
||||
/* The index database stores three items for each block: the disk location of the encoded filter,
|
||||
* its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by
|
||||
@@ -159,6 +160,11 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
|
||||
}
|
||||
if (!file.Commit()) {
|
||||
LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
|
||||
(void)file.fclose();
|
||||
return false;
|
||||
}
|
||||
if (file.fclose() != 0) {
|
||||
LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -213,6 +219,11 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
|
||||
}
|
||||
if (!last_file.Commit()) {
|
||||
LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
|
||||
(void)last_file.fclose();
|
||||
return 0;
|
||||
}
|
||||
if (last_file.fclose() != 0) {
|
||||
LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -235,6 +246,12 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
|
||||
}
|
||||
|
||||
fileout << filter.GetBlockHash() << filter.GetEncodedFilter();
|
||||
|
||||
if (fileout.fclose() != 0) {
|
||||
LogError("Failed to close filter file %d: %s", pos.nFile, SysErrorString(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return data_size;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user