Skip to content

Commit e038093

Browse files
committed
validation: Refactor file flush logic into FlatFileSeq.
1 parent 992404b commit e038093

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/flatfile.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ size_t FlatFileSeq::Allocate(const CDiskBlockPos& pos, size_t add_size, bool& ou
7272
}
7373
return 0;
7474
}
75+
76+
bool FlatFileSeq::Flush(const CDiskBlockPos& pos, bool finalize)
77+
{
78+
FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
79+
if (!file) {
80+
return error("%s: failed to open file %d", __func__, pos.nFile);
81+
}
82+
if (finalize && !TruncateFile(file, pos.nPos)) {
83+
fclose(file);
84+
return error("%s: failed to truncate file %d", __func__, pos.nFile);
85+
}
86+
if (!FileCommit(file)) {
87+
fclose(file);
88+
return error("%s: failed to commit file %d", __func__, pos.nFile);
89+
}
90+
91+
fclose(file);
92+
return true;
93+
}

src/flatfile.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class FlatFileSeq
4545
* @return The number of bytes successfully allocated.
4646
*/
4747
size_t Allocate(const CDiskBlockPos& pos, size_t add_size, bool& out_of_space);
48+
49+
/**
50+
* Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
51+
*
52+
* @param[in] pos The first unwritten position in the file to be flushed.
53+
* @param[in] finalize True if no more data will be written to this file.
54+
* @return true on success, false on failure.
55+
*/
56+
bool Flush(const CDiskBlockPos& pos, bool finalize = false);
4857
};
4958

5059
#endif // BITCOIN_FLATFILE_H

src/validation.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,25 +1630,12 @@ void static FlushBlockFile(bool fFinalize = false)
16301630
{
16311631
LOCK(cs_LastBlockFile);
16321632

1633-
CDiskBlockPos posOld(nLastBlockFile, 0);
1634-
bool status = true;
1635-
1636-
FILE *fileOld = OpenBlockFile(posOld);
1637-
if (fileOld) {
1638-
if (fFinalize)
1639-
status &= TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
1640-
status &= FileCommit(fileOld);
1641-
fclose(fileOld);
1642-
}
1643-
1644-
fileOld = OpenUndoFile(posOld);
1645-
if (fileOld) {
1646-
if (fFinalize)
1647-
status &= TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
1648-
status &= FileCommit(fileOld);
1649-
fclose(fileOld);
1650-
}
1633+
CDiskBlockPos block_pos_old(nLastBlockFile, vinfoBlockFile[nLastBlockFile].nSize);
1634+
CDiskBlockPos undo_pos_old(nLastBlockFile, vinfoBlockFile[nLastBlockFile].nUndoSize);
16511635

1636+
bool status = true;
1637+
status &= BlockFileSeq().Flush(block_pos_old, fFinalize);
1638+
status &= UndoFileSeq().Flush(undo_pos_old, fFinalize);
16521639
if (!status) {
16531640
AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.");
16541641
}

0 commit comments

Comments
 (0)