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