1 -----------------------------------------------------------------------------
2 -- $Id: TmpFiles.hs,v 1.11 2000/12/07 08:20:46 simonpj Exp $
4 -- Temporary file management
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
12 initTempFileStorage, -- :: IO ()
13 cleanTempFiles, -- :: IO ()
14 newTempName, -- :: Suffix -> IO FilePath
15 addFilesToClean, -- :: [FilePath] -> IO ()
33 #include "HsVersions.h"
35 GLOBAL_VAR(v_FilesToClean, [], [String] )
36 GLOBAL_VAR(v_TmpDir, cDEFAULT_TMPDIR, String )
39 initTempFileStorage = do
40 -- check whether TMPDIR is set in the environment
41 IO.try (do dir <- getEnv "TMPDIR" -- fails if not set
42 #ifndef mingw32_TARGET_OS
43 writeIORef v_TmpDir dir
48 cleanTempFiles :: Bool -> IO ()
49 cleanTempFiles verbose = do
50 fs <- readIORef v_FilesToClean
53 (do when verbose (hPutStrLn stderr ("Removing: " ++ f))
54 if '*' `elem` f then system ("rm -f " ++ f) >> return ()
57 (\_ -> when verbose (hPutStrLn stderr
58 ("Warning: can't remove tmp file " ++ f)))
63 -- find a temporary name that doesn't already exist.
64 newTempName :: Suffix -> IO FilePath
67 tmp_dir <- readIORef v_TmpDir
68 findTempName tmp_dir x
69 where findTempName tmp_dir x = do
70 let filename = tmp_dir ++ "/ghc" ++ show x ++ '.':extn
71 b <- doesFileExist filename
72 if b then findTempName tmp_dir (x+1)
73 else do add v_FilesToClean filename -- clean it up later
76 addFilesToClean :: [FilePath] -> IO ()
77 addFilesToClean files = mapM_ (add v_FilesToClean) files
79 add :: IORef [a] -> a -> IO ()