validation: Refactor file flush logic into FlatFileSeq.

This commit is contained in:
Jim Posen
2019-01-06 10:14:35 -08:00
parent 992404b31e
commit e0380933e3
3 changed files with 33 additions and 18 deletions

View File

@@ -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;
}