validation: Refactor file flush logic into FlatFileSeq.
This commit is contained in:
parent
992404b31e
commit
e0380933e3
3 changed files with 33 additions and 18 deletions
|
@ -72,3 +72,22 @@ size_t FlatFileSeq::Allocate(const CDiskBlockPos& pos, size_t add_size, bool& ou
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool FlatFileSeq::Flush(const CDiskBlockPos& pos, bool finalize)
|
||||
{
|
||||
FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
|
||||
if (!file) {
|
||||
return error("%s: failed to open file %d", __func__, pos.nFile);
|
||||
}
|
||||
if (finalize && !TruncateFile(file, pos.nPos)) {
|
||||
fclose(file);
|
||||
return error("%s: failed to truncate file %d", __func__, pos.nFile);
|
||||
}
|
||||
if (!FileCommit(file)) {
|
||||
fclose(file);
|
||||
return error("%s: failed to commit file %d", __func__, pos.nFile);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -45,6 +45,15 @@ public:
|
|||
* @return The number of bytes successfully allocated.
|
||||
*/
|
||||
size_t Allocate(const CDiskBlockPos& pos, size_t add_size, bool& out_of_space);
|
||||
|
||||
/**
|
||||
* Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
|
||||
*
|
||||
* @param[in] pos The first unwritten position in the file to be flushed.
|
||||
* @param[in] finalize True if no more data will be written to this file.
|
||||
* @return true on success, false on failure.
|
||||
*/
|
||||
bool Flush(const CDiskBlockPos& pos, bool finalize = false);
|
||||
};
|
||||
|
||||
#endif // BITCOIN_FLATFILE_H
|
||||
|
|
|
@ -1630,25 +1630,12 @@ void static FlushBlockFile(bool fFinalize = false)
|
|||
{
|
||||
LOCK(cs_LastBlockFile);
|
||||
|
||||
CDiskBlockPos posOld(nLastBlockFile, 0);
|
||||
CDiskBlockPos block_pos_old(nLastBlockFile, vinfoBlockFile[nLastBlockFile].nSize);
|
||||
CDiskBlockPos undo_pos_old(nLastBlockFile, vinfoBlockFile[nLastBlockFile].nUndoSize);
|
||||
|
||||
bool status = true;
|
||||
|
||||
FILE *fileOld = OpenBlockFile(posOld);
|
||||
if (fileOld) {
|
||||
if (fFinalize)
|
||||
status &= TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
|
||||
status &= FileCommit(fileOld);
|
||||
fclose(fileOld);
|
||||
}
|
||||
|
||||
fileOld = OpenUndoFile(posOld);
|
||||
if (fileOld) {
|
||||
if (fFinalize)
|
||||
status &= TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
|
||||
status &= FileCommit(fileOld);
|
||||
fclose(fileOld);
|
||||
}
|
||||
|
||||
status &= BlockFileSeq().Flush(block_pos_old, fFinalize);
|
||||
status &= UndoFileSeq().Flush(undo_pos_old, fFinalize);
|
||||
if (!status) {
|
||||
AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue