3333#include < util/fs.h>
3434#include < util/signalinterrupt.h>
3535#include < util/strencodings.h>
36+ #include < util/syserror.h>
3637#include < util/translation.h>
3738#include < validation.h>
3839
@@ -941,13 +942,13 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
941942 return false ;
942943 }
943944
945+ // Open history file to append
946+ AutoFile file{OpenUndoFile (pos)};
947+ if (file.IsNull ()) {
948+ LogError (" OpenUndoFile failed for %s while writing block undo" , pos.ToString ());
949+ return FatalError (m_opts.notifications , state, _ (" Failed to write undo data." ));
950+ }
944951 {
945- // Open history file to append
946- AutoFile file{OpenUndoFile (pos)};
947- if (file.IsNull ()) {
948- LogError (" OpenUndoFile failed for %s while writing block undo" , pos.ToString ());
949- return FatalError (m_opts.notifications , state, _ (" Failed to write undo data." ));
950- }
951952 BufferedWriter fileout{file};
952953
953954 // Write index header
@@ -960,8 +961,13 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
960961 // Write undo data & checksum
961962 fileout << blockundo << hasher.GetHash ();
962963 }
964+ // BufferedWriter will flush pending data to file when fileout goes out of scope.
965+ }
963966
964- fileout.flush (); // Make sure `AutoFile`/`BufferedWriter` go out of scope before we call `FlushUndoFile`
967+ // Make sure that the file is closed before we call `FlushUndoFile`.
968+ if (file.fclose () != 0 ) {
969+ LogError (" Failed to close block undo file %s: %s" , pos.ToString (), SysErrorString (errno));
970+ return FatalError (m_opts.notifications , state, _ (" Failed to close block undo file." ));
965971 }
966972
967973 // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
@@ -1094,13 +1100,22 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
10941100 m_opts.notifications .fatalError (_ (" Failed to write block." ));
10951101 return FlatFilePos ();
10961102 }
1097- BufferedWriter fileout{file};
1103+ {
1104+ BufferedWriter fileout{file};
1105+
1106+ // Write index header
1107+ fileout << GetParams ().MessageStart () << block_size;
1108+ pos.nPos += STORAGE_HEADER_BYTES;
1109+ // Write block
1110+ fileout << TX_WITH_WITNESS (block);
1111+ }
1112+
1113+ if (file.fclose () != 0 ) {
1114+ LogError (" Failed to close block file %s: %s" , pos.ToString (), SysErrorString (errno));
1115+ m_opts.notifications .fatalError (_ (" Failed to close file when writing block." ));
1116+ return FlatFilePos ();
1117+ }
10981118
1099- // Write index header
1100- fileout << GetParams ().MessageStart () << block_size;
1101- pos.nPos += STORAGE_HEADER_BYTES;
1102- // Write block
1103- fileout << TX_WITH_WITNESS (block);
11041119 return pos;
11051120}
11061121
@@ -1143,6 +1158,11 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
11431158#endif
11441159 )};
11451160 xor_key_file << xor_key;
1161+ if (xor_key_file.fclose () != 0 ) {
1162+ throw std::runtime_error{strprintf (" Error closing XOR key file %s: %s" ,
1163+ fs::PathToString (xor_key_path),
1164+ SysErrorString (errno))};
1165+ }
11461166 }
11471167 // If the user disabled the key, it must be zero.
11481168 if (!opts.use_xor && xor_key != decltype (xor_key){}) {
0 commit comments