on fa5a047 help sign in
Idris-dev/src/Util/System.hs
45 lines 1.6 kB history raw
1module Util.System(tempfile,withTempdir,rmFile,catchIO, isWindows) 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.Info
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
22isWindows :: Bool
23isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
25tempfile :: IO (FilePath, Handle)
26tempfile = do dir <- getTemporaryDirectory
27 openTempFile (normalise dir) "idris"
29withTempdir :: String -> (FilePath -> IO a) -> IO a
30withTempdir subdir callback
31 = do dir <- getTemporaryDirectory
32 let tmpDir = (normalise dir) </> subdir
33 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
34 (\ ioError -> if isAlreadyExistsError ioError then return False
35 else throw ioError
36 )
37 result <- callback tmpDir
38 when removeLater $ removeDirectoryRecursive tmpDir
39 return result
41rmFile :: FilePath -> IO ()
42rmFile f = do putStrLn $ "Removing " ++ f
43 catchIO (removeFile f)
44 (\ioerr -> putStrLn $ "WARNING: Cannot remove file "
45 ++ f ++ ", Error msg:" ++ show ioerr)