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,
23 #if __GLASGOW_HASKELL__ < 603
27 import Control.Exception ( bracket )
28 import Control.Monad ( when )
29 import System.Environment (getEnv)
30 import System.FilePath
32 #if defined(mingw32_TARGET_OS)
36 import System.Directory(doesFileExist, getPermissions, setPermissions)
37 #if defined(__GLASGOW_HASKELL__)
38 import GHC.IOBase ( IOException(..) )
41 getAppUserDataDirectory :: String -> IO FilePath
42 getAppUserDataDirectory appName = do
43 #if __GLASGOW_HASKELL__ && defined(mingw32_TARGET_OS)
44 allocaBytes long_path_size $ \pPath -> do
45 r <- c_SHGetFolderPath nullPtr csidl_APPDATA nullPtr 0 pPath
46 s <- peekCString pPath
47 return (s++'\\':appName)
50 return (path++'/':'.':appName)
53 #if __GLASGOW_HASKELL__ && defined(mingw32_TARGET_OS)
54 foreign import stdcall unsafe "SHGetFolderPathA"
55 c_SHGetFolderPath :: Ptr ()
62 foreign import ccall unsafe "__hscore_long_path_size"
65 foreign import ccall unsafe "__hscore_CSIDL_APPDATA" csidl_APPDATA :: CInt
69 copyFile :: FilePath -> FilePath -> IO ()
70 copyFile fromFPath toFPath =
71 #if (!(defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ > 600))
72 do readFile fromFPath >>= writeFile toFPath
73 try (getPermissions fromFPath >>= setPermissions toFPath)
76 (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom ->
77 bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo ->
78 allocaBytes bufferSize $ \buffer -> do
79 copyContents hFrom hTo buffer
80 try (getPermissions fromFPath >>= setPermissions toFPath)
81 return ()) `catch` (ioError . changeFunName)
85 changeFunName (IOError h iot fun str mb_fp) = IOError h iot "copyFile" str mb_fp
87 copyContents hFrom hTo buffer = do
88 count <- hGetBuf hFrom buffer bufferSize
90 hPutBuf hTo buffer count
91 copyContents hFrom hTo buffer
95 findExecutable :: String -> IO (Maybe FilePath)
96 findExecutable binary = do
98 search (parseSearchPath path)
100 #ifdef mingw32_TARGET_OS
101 fileName = binary `joinFileExt` "exe"
106 search :: [FilePath] -> IO (Maybe FilePath)
107 search [] = return Nothing
109 let path = d `joinFileName` fileName
110 b <- doesFileExist path
111 if b then return (Just path)