Skip to content

Commit c10e382

Browse files
committed
flatfile: check whether the file has been closed successfully
1 parent 4bb5dd7 commit c10e382

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/flatfile.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
4646
}
4747
if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
4848
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, fs::PathToString(path));
49-
fclose(file);
49+
if (fclose(file) != 0) {
50+
LogError("Unable to close file %s", fs::PathToString(path));
51+
}
5052
return nullptr;
5153
}
5254
return file;
@@ -68,7 +70,10 @@ size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_
6870
if (file) {
6971
LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
7072
AllocateFileRange(file, pos.nPos, inc_size);
71-
fclose(file);
73+
if (fclose(file) != 0) {
74+
LogError("Cannot close file %s%05u.dat after extending it with %u bytes", m_prefix, pos.nFile, new_size);
75+
return 0;
76+
}
7277
return inc_size;
7378
}
7479
} else {
@@ -86,17 +91,24 @@ bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
8691
return false;
8792
}
8893
if (finalize && !TruncateFile(file, pos.nPos)) {
89-
fclose(file);
9094
LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);
95+
if (fclose(file) != 0) {
96+
LogError("Failed to close file %d", pos.nFile);
97+
}
9198
return false;
9299
}
93100
if (!FileCommit(file)) {
94-
fclose(file);
95101
LogError("%s: failed to commit file %d\n", __func__, pos.nFile);
102+
if (fclose(file) != 0) {
103+
LogError("Failed to close file %d", pos.nFile);
104+
}
96105
return false;
97106
}
98107
DirectoryCommit(m_dir);
99108

100-
fclose(file);
109+
if (fclose(file) != 0) {
110+
LogError("Failed to close file %d after flush", pos.nFile);
111+
return false;
112+
}
101113
return true;
102114
}

0 commit comments

Comments
 (0)