on fbcd643 help sign in
Idris-dev/src/Util/System.hs
41 lines 1.5 kB history raw
1module Util.System(tempfile,withTempdir,rmFile,catchIO) where
3-- System helper functions.
4import Control.Monad (when)
5import System.Directory (getTemporaryDirectory
6 , removeFile
7 , removeDirectoryRecursive
8 , createDirectoryIfMissing
9 )
10import System.FilePath ((</>), normalise)
11import System.IO
12import System.IO.Error
13import Control.Exception as CE
15catchIO :: IO a -> (IOError -> IO a) -> IO a
16catchIO = CE.catch
18throwIO :: IOError -> IO a
19throwIO = CE.throw
21tempfile :: IO (FilePath, Handle)
22tempfile = do dir <- getTemporaryDirectory
23 openTempFile (normalise dir) "idris"
25withTempdir :: String -> (FilePath -> IO a) -> IO a
26withTempdir subdir callback
27 = do dir <- getTemporaryDirectory
28 let tmpDir = (normalise dir) </> subdir
29 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
30 (\ ioError -> if isAlreadyExistsError ioError then return False
31 else throw ioError
32 )
33 result <- callback tmpDir
34 when removeLater $ removeDirectoryRecursive tmpDir
35 return result
37rmFile :: FilePath -> IO ()
38rmFile f = do putStrLn $ "Removing " ++ f
39 catchIO (removeFile f)
40 (\ioerr -> putStrLn $ "WARNING: Cannot remove file "
41 ++ f ++ ", Error msg:" ++ show ioerr)