This stops them being confused with 'real' filenames by both users and code if the daemon crashes before the temporary file is removed.
Signed-off-by: Brian Foley <[email protected]> --- src/Ganeti/Utils/Atomic.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Ganeti/Utils/Atomic.hs b/src/Ganeti/Utils/Atomic.hs index 7f4d2df..7bb35b2 100644 --- a/src/Ganeti/Utils/Atomic.hs +++ b/src/Ganeti/Utils/Atomic.hs @@ -80,8 +80,14 @@ fsyncFileChecked path = atomicUpdateFile :: (MonadBaseControl IO m) => FilePath -> (FilePath -> Handle -> m a) -> m a atomicUpdateFile path action = do + -- Put a separator on the filename pattern to produce temporary filesnames + -- such as job-1234-NNNNNN.tmp instead of job-1234NNNNNN. The latter can cause + -- problems (as well as user confusion) because temporary filenames have the + -- same format as real filenames, and anything that scans a directory won't be + -- able to tell them apart. + let filenameTemplate = takeBaseName path ++ "-.tmp" (tmppath, tmphandle) <- liftBase $ openBinaryTempFile (takeDirectory path) - (takeBaseName path) + filenameTemplate r <- L.finally (action tmppath tmphandle) (liftBase (hClose tmphandle >> fsyncFileChecked tmppath)) -- if all went well, rename the file -- 2.8.0.rc3.226.g39d4020
