@@ -10,15 +10,22 @@ module Path.IO
1010 ,resolveDirMaybe
1111 ,resolveFileMaybe
1212 ,ResolveException (.. )
13+ ,removeFile
1314 ,removeFileIfExists
1415 ,removeTree
1516 ,removeTreeIfExists
16- ,fileExists
17+ ,renameFile
1718 ,renameFileIfExists
19+ ,renameDir
1820 ,renameDirIfExists
21+ ,moveFile
1922 ,moveFileIfExists
23+ ,moveDir
2024 ,moveDirIfExists
25+ ,fileExists
2126 ,dirExists
27+ ,copyFile
28+ ,copyFileIfExists
2229 ,copyDirectoryRecursive
2330 ,createTree )
2431 where
@@ -31,7 +38,7 @@ import Data.Either
3138import Data.Maybe
3239import Data.Typeable
3340import Path
34- import System.Directory
41+ import qualified System.Directory as D
3542import qualified System.FilePath as FP
3643import System.IO.Error
3744
@@ -47,7 +54,7 @@ instance Show ResolveException where
4754
4855-- | Get the current working directory.
4956getWorkingDir :: (MonadIO m ) => m (Path Abs Dir )
50- getWorkingDir = liftIO (canonicalizePath " ." >>= parseAbsDir)
57+ getWorkingDir = liftIO (D. canonicalizePath " ." >>= parseAbsDir)
5158
5259-- | Appends a stringly-typed relative path to an absolute path, and then
5360-- canonicalizes it.
@@ -84,7 +91,7 @@ resolveCheckParse check parse x y = do
8491 exists <- liftIO $ check fp
8592 if exists
8693 then do
87- canonic <- liftIO $ canonicalizePath fp
94+ canonic <- liftIO $ D. canonicalizePath fp
8895 liftM Just (parse canonic)
8996 else return Nothing
9097
@@ -93,23 +100,23 @@ resolveCheckParse check parse x y = do
93100-- be canonicalized, 'Nothing' is returned).
94101resolveDirMaybe :: (MonadIO m ,MonadThrow m )
95102 => Path Abs Dir -> FilePath -> m (Maybe (Path Abs Dir ))
96- resolveDirMaybe = resolveCheckParse doesDirectoryExist parseAbsDir
103+ resolveDirMaybe = resolveCheckParse D. doesDirectoryExist parseAbsDir
97104
98105-- | Appends a stringly-typed relative path to an absolute path, and then
99106-- canonicalizes it. If the path doesn't exist (and therefore cannot
100107-- be canonicalized, 'Nothing' is returned).
101108resolveFileMaybe :: (MonadIO m ,MonadThrow m )
102109 => Path Abs Dir -> FilePath -> m (Maybe (Path Abs File ))
103- resolveFileMaybe = resolveCheckParse doesFileExist parseAbsFile
110+ resolveFileMaybe = resolveCheckParse D. doesFileExist parseAbsFile
104111
105112-- | List objects in a directory, excluding "@.@" and "@..@". Entries are not sorted.
106113listDirectory :: (MonadIO m ,MonadThrow m ) => Path Abs Dir -> m ([Path Abs Dir ],[Path Abs File ])
107114listDirectory dir =
108- do entriesFP <- liftIO (getDirectoryContents dirFP)
115+ do entriesFP <- liftIO (D. getDirectoryContents dirFP)
109116 maybeEntries <-
110117 forM (map (dirFP ++ ) entriesFP)
111118 (\ entryFP ->
112- do isDir <- liftIO (doesDirectoryExist entryFP)
119+ do isDir <- liftIO (D. doesDirectoryExist entryFP)
113120 if isDir
114121 then case parseAbsDir entryFP of
115122 Nothing -> return Nothing
@@ -124,97 +131,78 @@ listDirectory dir =
124131 return (lefts entries,rights entries)
125132 where dirFP = toFilePath dir
126133
127- -- | Remove the given file. Optimistically assumes it exists. If it
128- -- doesn't, doesn't complain.
134+ -- | Remove a file. Bails out if it doesn't exist.
135+ removeFile :: MonadIO m => Path b File -> m ()
136+ removeFile = liftIO . D. removeFile . toFilePath
137+
138+ -- | Remove a file. Optimistically assumes it exists. If it doesn't,
139+ -- doesn't complain.
129140removeFileIfExists :: MonadIO m => Path b File -> m ()
130- removeFileIfExists fp =
131- liftIO (catch
132- (removeFile
133- (toFilePath fp))
134- (\ e ->
135- if isDoesNotExistError e
136- then return ()
137- else throwIO e))
138-
139- -- | Move the given file. Optimistically assumes it exists. If it
140- -- doesn't, doesn't complain.
141- renameFileIfExists :: MonadIO m => Path b File -> Path b File -> m ()
142- renameFileIfExists from to =
143- liftIO
144- (catch
145- (renameFile (toFilePath from)
146- (toFilePath to))
147- (\ e ->
148- if isDoesNotExistError e
149- then return ()
150- else throwIO e))
151-
152- -- | Rename the directory. Optimistically assumes it exists. If it
141+ removeFileIfExists = ignoreDoesNotExist . removeFile
142+
143+ -- | Rename a file. Bails out if it doesn't exist.
144+ renameFile :: MonadIO m => Path b1 File -> Path b2 File -> m ()
145+ renameFile from to = liftIO (D. renameFile (toFilePath from) (toFilePath to))
146+
147+ -- | Rename a file. Optimistically assumes it exists. If it doesn't,
148+ -- doesn't complain.
149+ renameFileIfExists :: MonadIO m => Path b1 File -> Path b2 File -> m ()
150+ renameFileIfExists from to = ignoreDoesNotExist (renameFile from to)
151+
152+ renameDir :: MonadIO m => Path b1 Dir -> Path b2 Dir -> m ()
153+ renameDir from to = liftIO (D. renameDirectory (toFilePath from) (toFilePath to))
154+
155+ -- | Rename a directory. Optimistically assumes it exists. If it
153156-- doesn't, doesn't complain.
154- renameDirIfExists :: MonadIO m => Path b Dir -> Path b Dir -> m ()
155- renameDirIfExists from to =
156- liftIO
157- (catch
158- (renameDirectory (toFilePath from)
159- (toFilePath to))
160- (\ e ->
161- if isDoesNotExistError e
162- then return ()
163- else throwIO e))
157+ renameDirIfExists :: MonadIO m => Path b1 Dir -> Path b2 Dir -> m ()
158+ renameDirIfExists from to = ignoreDoesNotExist (renameDir from to)
164159
165160-- | Make a directory tree, creating parents if needed.
166161createTree :: MonadIO m => Path b Dir -> m ()
167- createTree = liftIO . createDirectoryIfMissing True . toFilePath
162+ createTree = liftIO . D. createDirectoryIfMissing True . toFilePath
168163
169- -- | Move the given file. Optimistically assumes it exists. If it
170- -- doesn't, doesn't complain.
171- moveFileIfExists :: MonadIO m => Path b File -> Path b Dir -> m ()
172- moveFileIfExists from to =
173- liftIO
174- (catch
175- (renameFile (toFilePath from)
176- (toFilePath (to </> filename from)))
177- (\ e ->
178- if isDoesNotExistError e
179- then return ()
180- else throwIO e))
181-
182- -- | Move the given dir. Optimistically assumes it exists. If it
183- -- doesn't, doesn't complain.
184- moveDirIfExists :: MonadIO m => Path b Dir -> Path b Dir -> m ()
185- moveDirIfExists from to =
186- liftIO
187- (catch
188- (renameDirectory
189- (toFilePath from)
190- (toFilePath (to </> dirname from)))
191- (\ e ->
192- if isDoesNotExistError e
193- then return ()
194- else throwIO e))
195-
196- -- | Remove the given tree. Bails out if the directory doesn't exist.
164+ -- | Move a file. Bails out if it doesn't exist.
165+ moveFile :: MonadIO m => Path b1 File -> Path b2 Dir -> m ()
166+ moveFile from to = renameFile from (to </> filename from)
167+
168+ -- | Move a file. Optimistically assumes it exists. If it doesn't,
169+ -- doesn't complain.
170+ moveFileIfExists :: MonadIO m => Path b1 File -> Path b2 Dir -> m ()
171+ moveFileIfExists from to = ignoreDoesNotExist (moveFile from to)
172+
173+ -- | Move a dir. Bails out if it doesn't exist.
174+ moveDir :: MonadIO m => Path b1 Dir -> Path b2 Dir -> m ()
175+ moveDir from to = renameDir from (to </> dirname from)
176+
177+ -- | Move a dir. Optimistically assumes it exists. If it doesn't,
178+ -- doesn't complain.
179+ moveDirIfExists :: MonadIO m => Path b1 Dir -> Path b2 Dir -> m ()
180+ moveDirIfExists from to = ignoreDoesNotExist (moveDir from to)
181+
182+ -- | Remove a tree. Bails out if it doesn't exist.
197183removeTree :: MonadIO m => Path b Dir -> m ()
198- removeTree =
199- liftIO . removeDirectoryRecursive . toFilePath
184+ removeTree = liftIO . D. removeDirectoryRecursive . toFilePath
200185
201186-- | Remove tree, don't complain about non-existent directories.
202187removeTreeIfExists :: MonadIO m => Path b Dir -> m ()
203- removeTreeIfExists fp = do
204- liftIO (catch (removeTree fp)
205- (\ e -> if isDoesNotExistError e
206- then return ()
207- else throwIO e))
188+ removeTreeIfExists = ignoreDoesNotExist . removeTree
208189
209- -- | Does the given file exist?
190+ -- | Does the file exist?
210191fileExists :: MonadIO m => Path b File -> m Bool
211- fileExists =
212- liftIO . doesFileExist . toFilePath
192+ fileExists = liftIO . D. doesFileExist . toFilePath
213193
214- -- | Does the given directory exist?
194+ -- | Does the directory exist?
215195dirExists :: MonadIO m => Path b Dir -> m Bool
216- dirExists =
217- liftIO . doesDirectoryExist . toFilePath
196+ dirExists = liftIO . D. doesDirectoryExist . toFilePath
197+
198+ -- | Copies a file to another path. Bails out if it doesn't exist.
199+ copyFile :: MonadIO m => Path b1 File -> Path b2 File -> m ()
200+ copyFile from to = liftIO (D. copyFile (toFilePath from) (toFilePath to))
201+
202+ -- | Copies a file to another path. Optimistically assumes it exists. If
203+ -- it doesn't, doesn't complain.
204+ copyFileIfExists :: MonadIO m => Path b1 File -> Path b2 File -> m ()
205+ copyFileIfExists from to = ignoreDoesNotExist (copyFile from to)
218206
219207-- | Copy a directory recursively. This just uses 'copyFile', so it is not smart about symbolic
220208-- links or other special files.
@@ -223,16 +211,20 @@ copyDirectoryRecursive :: (MonadIO m,MonadThrow m)
223211 -> Path Abs Dir -- ^ Destination directory
224212 -> m ()
225213copyDirectoryRecursive srcDir destDir =
226- do liftIO (createDirectoryIfMissing False (toFilePath destDir))
214+ do liftIO (D. createDirectoryIfMissing False (toFilePath destDir))
227215 (srcSubDirs,srcFiles) <- listDirectory srcDir
228216 forM_ srcFiles
229217 (\ srcFile ->
230218 case stripDir srcDir srcFile of
231219 Nothing -> return ()
232- Just relFile -> liftIO (copyFile (toFilePath srcFile)
233- (toFilePath (destDir </> relFile))))
220+ Just relFile -> copyFile srcFile (destDir </> relFile))
234221 forM_ srcSubDirs
235222 (\ srcSubDir ->
236223 case stripDir srcDir srcSubDir of
237224 Nothing -> return ()
238225 Just relSubDir -> copyDirectoryRecursive srcSubDir (destDir </> relSubDir))
226+
227+ -- Utility function for a common pattern of ignoring does-not-exist errors.
228+ ignoreDoesNotExist :: MonadIO m => IO () -> m ()
229+ ignoreDoesNotExist f =
230+ liftIO $ catch f $ \ e -> unless (isDoesNotExistError e) (throwIO e)
0 commit comments