1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
module System.EasyFile.Directory (
module System.EasyFile.Directory
, module System.Directory
) where
import System.Directory (
createDirectory
, createDirectoryIfMissing
, removeDirectory
, removeDirectoryRecursive
, renameDirectory
, getDirectoryContents
, setCurrentDirectory
, removeFile
, renameFile
, copyFile
, canonicalizePath
, doesFileExist
, doesDirectoryExist
, Permissions(..)
, getPermissions
, setPermissions
)
import qualified System.Directory as D (
getCurrentDirectory
, getHomeDirectory
, getAppUserDataDirectory
, getUserDocumentsDirectory
, getTemporaryDirectory
, copyPermissions
)
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
import qualified Control.Exception as E
import System.Environment
----------------------------------------------------------------
{- |If the operating system has a notion of current directories,
'getCurrentDirectory' returns an absolute path to the
current directory of the calling process.
The operation may fail with:
* 'HardwareFault'
A physical I\/O error has occurred.
@[EIO]@
* 'isDoesNotExistError' \/ 'NoSuchThing'
There is no path referring to the current directory.
@[EPERM, ENOENT, ESTALE...]@
* 'isPermissionError' \/ 'PermissionDenied'
The process has insufficient privileges to perform the operation.
@[EACCES]@
* 'ResourceExhausted'
Insufficient resources are available to perform the operation.
* 'UnsupportedOperation'
The operating system has no notion of current directory.
-}
getCurrentDirectory :: IO FilePath
getCurrentDirectory = fixPath <$> D.getCurrentDirectory
{- | Returns the current user's home directory.
The directory returned is expected to be writable by the current user,
but note that it isn't generally considered good practice to store
application-specific data here; use 'getAppUserDataDirectory'
instead.
On Unix, 'getHomeDirectory' returns the value of the @HOME@
environment variable. On Windows, the system is queried for a
suitable path; a typical path might be
@C:/Documents And Settings/user@.
The operation may fail with:
* 'UnsupportedOperation'
The operating system has no notion of home directory.
* 'isDoesNotExistError'
The home directory for the current user does not exist, or
cannot be found.
-}
getHomeDirectory :: IO FilePath
getHomeDirectory = fixPath <$> D.getHomeDirectory
{- | Returns the current user's home directory from
the @HOME@ environment variable.
-}
getHomeDirectory2 :: IO (Maybe FilePath)
getHomeDirectory2 = (Just . fixPath <$> getEnv "HOME") `E.catch`
\(_ :: E.IOException) -> return Nothing
{- | Returns the pathname of a directory in which application-specific
data for the current user can be stored. The result of
'getAppUserDataDirectory' for a given application is specific to
the current user.
The argument should be the name of the application, which will be used
to construct the pathname (so avoid using unusual characters that
might result in an invalid pathname).
Note: the directory may not actually exist, and may need to be created
first. It is expected that the parent directory exists and is
writable.
On Unix, this function returns @$HOME\/.appName@. On Windows, a
typical path might be
> C:/Documents And Settings/user/Application Data/appName
The operation may fail with:
* 'UnsupportedOperation'
The operating system has no notion of application-specific data directory.
* 'isDoesNotExistError'
The home directory for the current user does not exist, or
cannot be found.
-}
getAppUserDataDirectory :: String -> IO FilePath
getAppUserDataDirectory x = fixPath <$> D.getAppUserDataDirectory x
{- | Returns the current user's document directory.
The directory returned is expected to be writable by the current user,
but note that it isn't generally considered good practice to store
application-specific data here; use 'getAppUserDataDirectory'
instead.
On Unix, 'getUserDocumentsDirectory' returns the value of the @HOME@
environment variable. On Windows, the system is queried for a
suitable path; a typical path might be
@C:\/Documents and Settings\/user\/My Documents@.
The operation may fail with:
* 'UnsupportedOperation'
The operating system has no notion of document directory.
* 'isDoesNotExistError'
The document directory for the current user does not exist, or
cannot be found.
-}
getUserDocumentsDirectory :: IO FilePath
getUserDocumentsDirectory = fixPath <$> D.getUserDocumentsDirectory
{- | Returns the current directory for temporary files.
On Unix, 'getTemporaryDirectory' returns the value of the @TMPDIR@
environment variable or \"\/tmp\" if the variable isn\'t defined.
On Windows, the function checks for the existence of environment variables in
the following order and uses the first path found:
* TMP environment variable.
* TEMP environment variable.
* USERPROFILE environment variable.
* The Windows directory
The operation may fail with:
* 'UnsupportedOperation'
The operating system has no notion of temporary directory.
The function doesn\'t verify whether the path exists.
-}
getTemporaryDirectory :: IO FilePath
getTemporaryDirectory = fixPath <$> D.getTemporaryDirectory
----------------------------------------------------------------
fixPath :: FilePath -> FilePath
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
fixPath [] = []
fixPath (c:cs)
| c == '\\' = '/' : fixPath cs
| otherwise = c : fixPath cs
#else
fixPath = id
#endif
----------------------------------------------------------------
-- Just adding documentation.
{-|
This function copy the permission of the first file to the second.
-}
copyPermissions :: FilePath -> FilePath -> IO ()
copyPermissions = D.copyPermissions
|