diff options
author | Jim Posen <jim.posen@gmail.com> | 2019-01-06 10:14:35 -0800 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2019-02-22 17:38:45 -0800 |
commit | e0380933e3745214331d358bda8c5e79299c84d2 (patch) | |
tree | b3dc6b1ad203e1c19b7beceae88e9b3131e4025d /src/flatfile.cpp | |
parent | 992404b31ed2f8cabeed59d074552f0ae10fda94 (diff) |
validation: Refactor file flush logic into FlatFileSeq.
Diffstat (limited to 'src/flatfile.cpp')
-rw-r--r-- | src/flatfile.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/flatfile.cpp b/src/flatfile.cpp index d9fd4041b7..1cdead6bf5 100644 --- a/src/flatfile.cpp +++ b/src/flatfile.cpp @@ -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; +} |