From a98b6c8f0759232fbb19ca611f954943f3f0b7af Mon Sep 17 00:00:00 2001
From: Morph <39850852+Morph1984@users.noreply.github.com>
Date: Sun, 13 Jun 2021 07:47:57 -0400
Subject: [PATCH] common: fs: file: Flush the file to the disk when Flush() is
 called

std::fflush does not guarantee that file buffers are flushed to the disk.

Use _commit on Windows and fsync on all other OSes to ensure that the file is flushed to the disk.
---
 src/common/fs/file.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/common/fs/file.cpp b/src/common/fs/file.cpp
index 9f3de1cb0..c84f31f3e 100644
--- a/src/common/fs/file.cpp
+++ b/src/common/fs/file.cpp
@@ -309,7 +309,11 @@ bool IOFile::Flush() const {
 
     errno = 0;
 
-    const auto flush_result = std::fflush(file) == 0;
+#ifdef _WIN32
+    const auto flush_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0;
+#else
+    const auto flush_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0;
+#endif
 
     if (!flush_result) {
         const auto ec = std::error_code{errno, std::generic_category()};