io/FileOutputStream: add method Sync()

This commit is contained in:
Max Kellermann 2022-05-12 16:49:15 +02:00
parent 47d103e8a1
commit 270a74e53b
2 changed files with 27 additions and 0 deletions

View File

@ -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()
{

View File

@ -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.