summaryrefslogtreecommitdiff
path: root/Utility.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-08-16 20:45:58 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-08-16 20:45:58 -0400
commit354c5f349bcd8c43b45191983dce4a6c7489e9ed (patch)
tree2b33682374e88eb3f025b807522bf6bfd94baeb6 /Utility.hs
parentcfcd7805b441c48e404826903480113a82cff9cf (diff)
add withTempFile
This is essentially the same as withSystemTempFile from System.IO.Temp, but that library is not packaged for Debian, and may not be widely used. I see various other withTempFile implementations here and there, none canonical. Sigh.
Diffstat (limited to 'Utility.hs')
-rw-r--r--Utility.hs14
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]