From 2cda9d0a0fcdd1cc2aebc066ef19282fbe36e898 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 30 Jun 2011 00:42:09 -0400 Subject: generalized safeWriteFile to viaTmp --- Command/Init.hs | 2 +- Command/Unused.hs | 2 +- Upgrade/V2.hs | 2 +- Utility.hs | 11 ++++++----- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Command/Init.hs b/Command/Init.hs index 7f5773117..8cde8bb9f 100644 --- a/Command/Init.hs +++ b/Command/Init.hs @@ -55,7 +55,7 @@ gitPreCommitHookWrite repo = do if exists then warning $ "pre-commit hook (" ++ hook ++ ") already exists, not configuring" else liftIO $ do - safeWriteFile hook preCommitScript + viaTmp writeFile hook preCommitScript p <- getPermissions hook setPermissions hook $ p {executable = True} where diff --git a/Command/Unused.hs b/Command/Unused.hs index c0a347179..a9d5f90a1 100644 --- a/Command/Unused.hs +++ b/Command/Unused.hs @@ -86,7 +86,7 @@ checkRemoteUnused' r = do writeUnusedFile :: FilePath -> [(Int, Key)] -> Annex () writeUnusedFile prefix l = do g <- Annex.gitRepo - liftIO $ safeWriteFile (gitAnnexUnusedLog prefix g) $ + liftIO $ viaTmp writeFile (gitAnnexUnusedLog prefix g) $ unlines $ map (\(n, k) -> show n ++ " " ++ show k) l table :: [(Int, Key)] -> [String] diff --git a/Upgrade/V2.hs b/Upgrade/V2.hs index 8537a5022..ea68e78c9 100644 --- a/Upgrade/V2.hs +++ b/Upgrade/V2.hs @@ -128,7 +128,7 @@ gitAttributesUnWrite repo = do let attributes = Git.attributes repo whenM (doesFileExist attributes) $ do c <- readFileStrict attributes - liftIO $ safeWriteFile attributes $ unlines $ + liftIO $ viaTmp writeFile attributes $ unlines $ filter (\l -> not $ l `elem` attrLines) $ lines c Git.run repo "add" [File attributes] diff --git a/Utility.hs b/Utility.hs index 47d10ed75..7831a4ab4 100644 --- a/Utility.hs +++ b/Utility.hs @@ -22,7 +22,7 @@ module Utility ( shellUnEscape, unsetFileMode, readMaybe, - safeWriteFile, + viaTmp, dirContains, dirContents, myHomeDir, @@ -243,13 +243,14 @@ readMaybe s = case reads s of ((x,_):_) -> Just x _ -> Nothing -{- Writes a file using a temp file that is renamed atomically into place. -} -safeWriteFile :: FilePath -> String -> IO () -safeWriteFile file content = do +{- Runs an action like writeFile, writing to a tmp file first and + - then moving it into place. -} +viaTmp :: (FilePath -> String -> IO ()) -> FilePath -> String -> IO () +viaTmp a file content = do pid <- getProcessID let tmpfile = file ++ ".tmp" ++ show pid createDirectoryIfMissing True (parentDir file) - writeFile tmpfile content + a tmpfile content renameFile tmpfile file {- Lists the contents of a directory. -- cgit v1.2.3