From 270a74e53bd536b019a7afcf71feaa672fcf6d81 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 12 May 2022 16:49:15 +0200 Subject: [PATCH] io/FileOutputStream: add method Sync() --- src/io/FileOutputStream.cxx | 18 ++++++++++++++++++ src/io/FileOutputStream.hxx | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/src/io/FileOutputStream.cxx b/src/io/FileOutputStream.cxx index 0cbd54284..cc0284658 100644 --- a/src/io/FileOutputStream.cxx +++ b/src/io/FileOutputStream.cxx @@ -138,6 +138,15 @@ FileOutputStream::Write(const void *data, size_t size) GetPath().c_str()); } +void +FileOutputStream::Sync() +{ + assert(IsDefined()); + + if (!FlushFileBuffers(handle)) + throw FormatLastError("Failed to sync %s", GetPath().c_str()); +} + void FileOutputStream::Commit() { @@ -240,6 +249,15 @@ FileOutputStream::Write(const void *data, size_t size) GetPath().c_str()); } +void +FileOutputStream::Sync() +{ + assert(IsDefined()); + + if (fdatasync(fd.Get()) < 0) + throw FormatErrno("Failed to sync %s", GetPath().c_str()); +} + void FileOutputStream::Commit() { diff --git a/src/io/FileOutputStream.hxx b/src/io/FileOutputStream.hxx index 78fcefc06..9b36b9ee4 100644 --- a/src/io/FileOutputStream.hxx +++ b/src/io/FileOutputStream.hxx @@ -148,6 +148,15 @@ public: /* virtual methods from class OutputStream */ void Write(const void *data, size_t size) override; + /** + * Flush all data written to this object to disk (but does not + * commit to the final path). This method blocks until this + * flush is complete. It can be called repeatedly. + * + * Throws on error. + */ + void Sync(); + /** * Commit all data written to the file and make the file * visible on the specified path.