From beb5a3db5eb9105ea5a28c814c37be788f5feab4 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 3 Dec 2022 13:33:33 +0100 Subject: [PATCH] enhance: Add AtomicFile::flush method --- src/AtomicFile.cpp | 9 +++++++++ src/AtomicFile.hpp | 1 + 2 files changed, 10 insertions(+) diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp index c9d82c3e2..9c0f0cb3c 100644 --- a/src/AtomicFile.cpp +++ b/src/AtomicFile.cpp @@ -59,6 +59,15 @@ AtomicFile::write(nonstd::span data) } } +void +AtomicFile::flush() +{ + if (fflush(m_stream) != 0) { + throw core::Error( + FMT("failed to flush data to {}: {}", m_path, strerror(errno))); + } +} + void AtomicFile::commit() { diff --git a/src/AtomicFile.hpp b/src/AtomicFile.hpp index 37ac37228..033a3fa3c 100644 --- a/src/AtomicFile.hpp +++ b/src/AtomicFile.hpp @@ -38,6 +38,7 @@ public: void write(std::string_view data); void write(nonstd::span data); + void flush(); // Close the temporary file and rename it to the destination file. Note: The // destructor will not do this automatically to avoid half-written data in the -- 2.47.2