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
34 import System.Directory(doesFileExist, getPermissions, setPermissions)
35 #if defined(__GLASGOW_HASKELL__)
36 import GHC.IOBase ( IOException(..) )
39 getAppUserDataDirectory :: String -> IO FilePath
40 getAppUserDataDirectory appName = do
41 #if __GLASGOW_HASKELL__ && defined(mingw32_TARGET_OS)
42 allocaBytes long_path_size $ \pPath -> do
43 r <- c_SHGetFolderPath nullPtr csidl_APPDATA nullPtr 0 pPath
44 s <- peekCString pPath
45 return (s++'\\':appName)
48 return (path++'/':'.':appName)
51 #if __GLASGOW_HASKELL__ && defined(mingw32_TARGET_OS)
52 foreign import stdcall unsafe "SHGetFolderPathA"
53 c_SHGetFolderPath :: Ptr ()
60 foreign import ccall unsafe "__hscore_long_path_size"
63 foreign import ccall unsafe "__hscore_CSIDL_APPDATA" csidl_APPDATA :: CInt
67 copyFile :: FilePath -> FilePath -> IO ()
68 copyFile fromFPath toFPath =
69 #if (!(defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ > 600))
70 do readFile fromFPath >>= writeFile toFPath
71 try (getPermissions fromFPath >>= setPermissions toFPath)
74 (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom ->
75 bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo ->
76 allocaBytes bufferSize $ \buffer -> do
77 copyContents hFrom hTo buffer
78 try (getPermissions fromFPath >>= setPermissions toFPath)
79 return ()) `catch` (ioError . changeFunName)
83 changeFunName (IOError h iot fun str mb_fp) = IOError h iot "copyFile" str mb_fp
85 copyContents hFrom hTo buffer = do
86 count <- hGetBuf hFrom buffer bufferSize
88 hPutBuf hTo buffer count
89 copyContents hFrom hTo buffer
93 findExecutable :: String -> IO (Maybe FilePath)
94 findExecutable binary = do
96 search (parseSearchPath path)
98 #ifdef mingw32_TARGET_OS
99 fileName = binary `joinFileExt` "exe"
104 search :: [FilePath] -> IO (Maybe FilePath)
105 search [] = return Nothing
107 let path = d `joinFileName` fileName
108 b <- doesFileExist path
109 if b then return (Just path)