diff options
author | Joey Hess <id@joeyh.name> | 2013-05-12 15:38:00 -0500 |
---|---|---|
committer | Joey Hess <id@joeyh.name> | 2013-05-12 15:38:00 -0500 |
commit | a3c1d8927e4e754fed80787a001899998e758915 (patch) | |
tree | fdf8a9749796af599b3324a43180cebd034f0c64 /Utility | |
parent | f1a0b394f63cd598c91cffab1c956dd813cc5701 (diff) |
make work on windows
Diffstat (limited to 'Utility')
-rwxr-xr-x[-rw-r--r--] | Utility/TempFile.hs | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/Utility/TempFile.hs b/Utility/TempFile.hs index ba7449ba2..58d07c3a2 100644..100755 --- a/Utility/TempFile.hs +++ b/Utility/TempFile.hs @@ -1,70 +1,71 @@ {- temp file functions - - - Copyright 2010-2011 Joey Hess <joey@kitenet.net> + - Copyright 2010-2013 Joey Hess <joey@kitenet.net> - - Licensed under the GNU GPL version 3 or higher. -} -{-# LANGUAGE CPP #-} - module Utility.TempFile where import Control.Exception (bracket) import System.IO -#ifndef mingw32_HOST_OS -import System.Posix.Process -#endif import System.Directory +import Control.Monad.IfElse import Utility.Exception -import Utility.Path import System.FilePath +type Template = String + {- Runs an action like writeFile, writing to a temp file first and - 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 () -#ifndef mingw32_HOST_OS viaTmp a file content = do - pid <- getProcessID - let tmpfile = file ++ ".tmp" ++ show pid - createDirectoryIfMissing True (parentDir file) + let (dir, base) = splitFileName file + createDirectoryIfMissing True dir + (tmpfile, handle) <- openTempFile dir (base ++ ".tmp") + hClose handle a tmpfile content renameFile tmpfile file -#else -viaTmp = error "viaTMP TODO" -#endif - -type Template = String -{- Runs an action with a temp file, then removes the 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. -} withTempFile :: Template -> (FilePath -> Handle -> IO a) -> IO a -withTempFile template a = bracket create remove use +withTempFile template a = do + tmpdir <- catchDefaultIO "." getTemporaryDirectory + withTempFileIn tmpdir template a + +{- Runs an action with a tmp file located in the specified directory, + - then removes the file. -} +withTempFileIn :: FilePath -> Template -> (FilePath -> Handle -> IO a) -> IO a +withTempFileIn tmpdir template a = bracket create remove use where - create = do - tmpdir <- catchDefaultIO "." getTemporaryDirectory - openTempFile tmpdir template + create = openTempFile tmpdir template remove (name, handle) = do hClose handle catchBoolIO (removeFile name >> return True) use (name, handle) = a name handle -{- Runs an action with a temp directory, then removes the directory and - - all its contents. -} +{- Runs an action with a tmp directory located within the system's tmp + - directory (or within "." if there is none), then removes the tmp + - directory and all its contents. -} withTempDir :: Template -> (FilePath -> IO a) -> IO a -#ifndef mingw32_HOST_OS -withTempDir template = bracket create remove +withTempDir template a = do + tmpdir <- catchDefaultIO "." getTemporaryDirectory + withTempDirIn tmpdir template a + +{- Runs an action with a tmp directory located within a specified directory, + - then removes the tmp directory and all its contents. -} +withTempDirIn :: FilePath -> Template -> (FilePath -> IO a) -> IO a +withTempDirIn tmpdir template = bracket create remove where - remove = removeDirectoryRecursive + remove d = whenM (doesDirectoryExist d) $ + removeDirectoryRecursive d create = do - tmpdir <- catchDefaultIO "." getTemporaryDirectory createDirectoryIfMissing True tmpdir - pid <- getProcessID - makedir tmpdir (template ++ show pid) (0 :: Int) - makedir tmpdir t n = do - let dir = tmpdir </> t ++ "." ++ show n - r <- tryIO $ createDirectory dir - either (const $ makedir tmpdir t $ n + 1) (const $ return dir) r -#else -withTempDir = error "withTempDir TODO" -#endif + makenewdir (tmpdir </> template) (0 :: Int) + makenewdir t n = do + let dir = t ++ "." ++ show n + either (const $ makenewdir t $ n + 1) (const $ return dir) + =<< tryIO (createDirectory dir) |