diff options
author | Joey Hess <joey@kitenet.net> | 2014-07-21 16:35:23 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2014-07-21 16:35:23 -0400 |
commit | 619c8bd261a4fee0b0d40b664e55c51782e062f7 (patch) | |
tree | ba39cd283d4c0fecb61c7a1bbbb803cc9459a222 /Utility/Tmp.hs | |
parent | 7882653e7856df87e598464379951ec89775cd1a (diff) | |
download | git-repair-619c8bd261a4fee0b0d40b664e55c51782e062f7.tar.gz |
Merge from git-annex.
Diffstat (limited to 'Utility/Tmp.hs')
-rw-r--r-- | Utility/Tmp.hs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/Utility/Tmp.hs b/Utility/Tmp.hs index f46e1a5..bed30bb 100644 --- a/Utility/Tmp.hs +++ b/Utility/Tmp.hs @@ -2,7 +2,7 @@ - - Copyright 2010-2013 Joey Hess <joey@kitenet.net> - - - Licensed under the GNU GPL version 3 or higher. + - License: BSD-2-clause -} {-# LANGUAGE CPP #-} @@ -25,13 +25,20 @@ type Template = String - then moving it into place. The temp file is stored in the same - directory as the final file to avoid cross-device renames. -} viaTmp :: (FilePath -> String -> IO ()) -> FilePath -> String -> IO () -viaTmp a file content = do - let (dir, base) = splitFileName file - createDirectoryIfMissing True dir - (tmpfile, handle) <- openTempFile dir (base ++ ".tmp") - hClose handle - a tmpfile content - rename tmpfile file +viaTmp a file content = bracket setup cleanup use + where + (dir, base) = splitFileName file + template = base ++ ".tmp" + setup = do + createDirectoryIfMissing True dir + openTempFile dir template + cleanup (tmpfile, handle) = do + _ <- tryIO $ hClose handle + tryIO $ removeFile tmpfile + use (tmpfile, handle) = do + hClose handle + a tmpfile content + rename tmpfile file {- Runs an action with a tmp file located in the system's tmp directory - (or in "." if there is none) then removes the file. -} |