2 -----------------------------------------------------------------------------
4 -- Module : Compat.Directory
5 -- Copyright : (c) The University of Glasgow 2001-2004
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Functions from System.Directory that aren't present in older versions
15 -----------------------------------------------------------------------------
17 module Compat.Directory (
18 getAppUserDataDirectory,
21 createDirectoryIfMissing
24 #include "../../includes/ghcconfig.h"
26 import System.Environment (getEnv)
27 import System.Directory.Internals
28 #if __GLASGOW_HASKELL__ > 600
29 import Control.Exception ( bracket )
30 import Control.Monad ( when )
31 import Foreign.Marshal.Alloc ( allocaBytes )
32 import System.IO (IOMode(..), openBinaryFile, hGetBuf, hPutBuf, hClose)
33 import System.IO.Error ( try )
34 import GHC.IOBase ( IOException(..) )
36 import System.IO ( try )
38 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
42 import System.Directory(doesFileExist, doesDirectoryExist, getPermissions, setPermissions, createDirectory)
44 getAppUserDataDirectory :: String -> IO FilePath
45 getAppUserDataDirectory appName = do
46 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
47 allocaBytes long_path_size $ \pPath -> do
48 r <- c_SHGetFolderPath nullPtr csidl_APPDATA nullPtr 0 pPath
49 s <- peekCString pPath
50 return (s++'\\':appName)
53 return (path++'/':'.':appName)
56 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
57 foreign import stdcall unsafe "SHGetFolderPathA"
58 c_SHGetFolderPath :: Ptr ()
65 -- __compat_long_path_size defined in cbits/directory.c
66 foreign import ccall unsafe "__compat_long_path_size"
69 foreign import ccall unsafe "__hscore_CSIDL_APPDATA" csidl_APPDATA :: CInt
73 copyFile :: FilePath -> FilePath -> IO ()
74 copyFile fromFPath toFPath =
75 #if (!(defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ > 600))
76 do readFile fromFPath >>= writeFile toFPath
77 try (getPermissions fromFPath >>= setPermissions toFPath)
80 (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom ->
81 bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo ->
82 allocaBytes bufferSize $ \buffer -> do
83 copyContents hFrom hTo buffer
84 try (getPermissions fromFPath >>= setPermissions toFPath)
85 return ()) `catch` (ioError . changeFunName)
89 changeFunName (IOError h iot fun str mb_fp) = IOError h iot "copyFile" str mb_fp
91 copyContents hFrom hTo buffer = do
92 count <- hGetBuf hFrom buffer bufferSize
94 hPutBuf hTo buffer count
95 copyContents hFrom hTo buffer
99 findExecutable :: String -> IO (Maybe FilePath)
100 findExecutable binary = do
101 path <- getEnv "PATH"
102 search (parseSearchPath path)
104 #ifdef mingw32_HOST_OS
105 fileName = binary `joinFileExt` "exe"
110 search :: [FilePath] -> IO (Maybe FilePath)
111 search [] = return Nothing
113 let path = d `joinFileName` fileName
114 b <- doesFileExist path
115 if b then return (Just path)
118 createDirectoryIfMissing :: Bool -- ^ Create its parents too?
119 -> FilePath -- ^ The path to the directory you want to make
121 createDirectoryIfMissing parents file = do
122 b <- doesDirectoryExist file
123 case (b,parents, file) of
124 (_, _, "") -> return ()
125 (True, _, _) -> return ()
126 (_, True, _) -> mapM_ (createDirectoryIfMissing False) (tail (pathParents file))
127 (_, False, _) -> createDirectory file