In this way, we ensure that the temporary file is fully written
to disk before the rename makes it take over the position of the
old file.

Signed-off-by: Klaus Aehlig <[email protected]>
---
 src/Ganeti/Utils/Atomic.hs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/Ganeti/Utils/Atomic.hs b/src/Ganeti/Utils/Atomic.hs
index eb9acab..84b9834 100644
--- a/src/Ganeti/Utils/Atomic.hs
+++ b/src/Ganeti/Utils/Atomic.hs
@@ -45,6 +45,7 @@ import System.Posix.Types
 import Ganeti.BasicTypes
 import Ganeti.Errors
 import Ganeti.Utils
+import Ganeti.Utils.UniStd (hCloseAndFsync)
 
 -- | Atomically write a file, by first writing the contents into a temporary
 -- file and then renaming it to the old position.
@@ -62,7 +63,8 @@ atomicUpdateFile :: (MonadBaseControl IO m)
 atomicUpdateFile path action = do
   (tmppath, tmphandle) <- liftBase $ openTempFile (takeDirectory path)
                                                   (takeBaseName path)
-  r <- L.finally (action tmppath tmphandle) (liftBase $ hClose tmphandle)
+  r <- L.finally (action tmppath tmphandle)
+                 (liftBase $ hCloseAndFsync tmphandle)
   -- if all went well, rename the file
   liftBase $ renameFile tmppath path
   return r
-- 
2.0.0.526.g5318336

Reply via email to