mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
util: check that a file has been closed before ~AutoFile() is called
If an `AutoFile` has been written to, then expect callers to have closed it explicitly via the `AutoFile::fclose()` method. This is because if the destructor calls `std::fclose()` and encounters an error, then it is too late to indicate this to the caller in a meaningful way.
This commit is contained in:
@@ -85,6 +85,7 @@ void AutoFile::write(std::span<const std::byte> src)
|
||||
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
|
||||
throw std::ios_base::failure("AutoFile::write: write failed");
|
||||
}
|
||||
m_was_written = true;
|
||||
if (m_position.has_value()) *m_position += src.size();
|
||||
} else {
|
||||
std::array<std::byte, 4096> buf;
|
||||
@@ -107,6 +108,7 @@ void AutoFile::write_buffer(std::span<std::byte> src)
|
||||
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
|
||||
throw std::ios_base::failure("AutoFile::write_buffer: write failed");
|
||||
}
|
||||
m_was_written = true;
|
||||
if (m_position) *m_position += src.size();
|
||||
}
|
||||
|
||||
@@ -117,6 +119,7 @@ bool AutoFile::Commit()
|
||||
|
||||
bool AutoFile::Truncate(unsigned size)
|
||||
{
|
||||
m_was_written = true;
|
||||
return ::TruncateFile(m_file, size);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user