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.FilePath
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(..) )
35 #if defined(mingw32_HOST_OS)
36 import GHC.IOBase ( IOErrorType(..) )
39 import System.IO ( try )
41 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
45 import System.Directory(doesFileExist, doesDirectoryExist, getPermissions, setPermissions, createDirectory)
47 getAppUserDataDirectory :: String -> IO FilePath
48 getAppUserDataDirectory appName = do
49 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
50 allocaBytes long_path_size $ \pPath -> do
51 r <- c_SHGetFolderPath nullPtr csidl_APPDATA nullPtr 0 pPath
52 when (r<0) (raiseUnsupported "Compat.Directory.getAppUserDataDirectory")
53 s <- peekCString pPath
54 return (s++'\\':appName)
57 return (path++'/':'.':appName)
60 #if __GLASGOW_HASKELL__ && defined(mingw32_HOST_OS)
61 foreign import ccall unsafe "directory.h __hscore_getFolderPath"
62 c_SHGetFolderPath :: Ptr ()
69 -- __compat_long_path_size defined in cbits/directory.c
70 foreign import ccall unsafe "directory.h __compat_long_path_size"
73 foreign import ccall unsafe "directory.h __hscore_CSIDL_APPDATA" csidl_APPDATA :: CInt
75 raiseUnsupported loc =
76 ioError (IOError Nothing UnsupportedOperation loc "unsupported operation" Nothing)
80 copyFile :: FilePath -> FilePath -> IO ()
81 copyFile fromFPath toFPath =
82 #if (!(defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ > 600))
83 do readFile fromFPath >>= writeFile toFPath
84 try (getPermissions fromFPath >>= setPermissions toFPath)
87 (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom ->
88 bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo ->
89 allocaBytes bufferSize $ \buffer -> do
90 copyContents hFrom hTo buffer
91 try (getPermissions fromFPath >>= setPermissions toFPath)
92 return ()) `catch` (ioError . changeFunName)
96 changeFunName (IOError h iot fun str mb_fp) = IOError h iot "copyFile" str mb_fp
98 copyContents hFrom hTo buffer = do
99 count <- hGetBuf hFrom buffer bufferSize
100 when (count > 0) $ do
101 hPutBuf hTo buffer count
102 copyContents hFrom hTo buffer
105 -- | Given an executable file name, searches for such file
106 -- in the directories listed in system PATH. The returned value
107 -- is the path to the found executable or Nothing if there isn't
108 -- such executable. For example (findExecutable \"ghc\")
109 -- gives you the path to GHC.
110 findExecutable :: String -> IO (Maybe FilePath)
111 findExecutable binary =
112 #if defined(mingw32_HOST_OS)
113 withCString binary $ \c_binary ->
114 withCString ('.':exeExtension) $ \c_ext ->
115 allocaBytes long_path_size $ \pOutPath ->
116 alloca $ \ppFilePart -> do
117 res <- c_SearchPath nullPtr c_binary c_ext (fromIntegral long_path_size) pOutPath ppFilePart
118 if res > 0 && res < fromIntegral long_path_size
119 then do fpath <- peekCString pOutPath
123 foreign import stdcall unsafe "SearchPathA"
124 c_SearchPath :: CString
133 path <- getEnv "PATH"
134 search (splitSearchPath path)
136 fileName = binary <.> exeExtension
138 search :: [FilePath] -> IO (Maybe FilePath)
139 search [] = return Nothing
141 let path = d </> fileName
142 b <- doesFileExist path
143 if b then return (Just path)
147 -- ToDo: This should be determined via autoconf (AC_EXEEXT)
148 -- | Extension for executable files
149 -- (typically @\"\"@ on Unix and @\"exe\"@ on Windows or OS\/2)
150 exeExtension :: String
151 #ifdef mingw32_HOST_OS
157 -- | @'createDirectoryIfMissing' parents dir@ creates a new directory
158 -- @dir@ if it doesn\'t exist. If the first argument is 'True'
159 -- the function will also create all parent directories if they are missing.
160 createDirectoryIfMissing :: Bool -- ^ Create its parents too?
161 -> FilePath -- ^ The path to the directory you want to make
163 createDirectoryIfMissing parents file = do
164 b <- doesDirectoryExist file
165 case (b,parents, file) of
166 (_, _, "") -> return ()
167 (True, _, _) -> return ()
168 (_, True, _) -> mapM_ (createDirectoryIfMissing False) $ mkParents file
169 (_, False, _) -> createDirectory file
170 where mkParents = scanl1 (</>) . splitDirectories . normalise