diff options
Diffstat (limited to 'Utility.hs')
-rw-r--r-- | Utility.hs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Utility.hs b/Utility.hs index dab3b4d0e..511350898 100644 --- a/Utility.hs +++ b/Utility.hs @@ -23,6 +23,7 @@ module Utility ( unsetFileMode, readMaybe, viaTmp, + withTempFile, dirContains, dirContents, myHomeDir, @@ -38,6 +39,7 @@ module Utility ( prop_relPathDirToFile_basics ) where +import IO (bracket) import System.IO import System.Exit import qualified System.Posix.Process @@ -253,6 +255,18 @@ viaTmp a file content = do a tmpfile content renameFile tmpfile file +{- Runs an action with a temp file, then removes the file. -} +withTempFile :: String -> (FilePath -> Handle -> IO a) -> IO a +withTempFile template action = bracket create remove use + where + create = do + tmpdir <- catch getTemporaryDirectory (const $ return ".") + openTempFile tmpdir template + remove (name, handle) = do + hClose handle + catchBool (removeFile name >> return True) + use (name, handle) = action name handle + {- Lists the contents of a directory. - Unlike getDirectoryContents, paths are not relative to the directory. -} dirContents :: FilePath -> IO [FilePath] |