Skip to content

Commit

Permalink
Add Path.IO.copyDirectoryRecursive (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
borsboom committed Jun 22, 2015
1 parent 2a564ec commit 410ff24
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Path/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Path.IO
,removeTree
,removeTreeIfExists
,fileExists
,dirExists)
,dirExists
,copyDirectoryRecursive)
where

import Control.Exception hiding (catch)
Expand Down Expand Up @@ -150,3 +151,24 @@ fileExists =
dirExists :: MonadIO m => Path b Dir -> m Bool
dirExists =
liftIO . doesFileExist . toFilePath

-- | Copy a directory recursively. This just uses 'copyFile', so it is not smart about symbolic
-- links or other special files.
copyDirectoryRecursive :: (MonadIO m,MonadThrow m)
=> Path Abs Dir -- ^ Source directory
-> Path Abs Dir -- ^ Destination directory
-> m ()
copyDirectoryRecursive srcDir destDir =
do liftIO (createDirectoryIfMissing False (toFilePath destDir))
(srcSubDirs,srcFiles) <- listDirectory srcDir
forM_ srcFiles
(\srcFile ->
case stripDir srcDir srcFile of
Nothing -> return ()
Just relFile -> liftIO (copyFile (toFilePath srcFile)
(toFilePath (destDir </> relFile))))
forM_ srcSubDirs
(\srcSubDir ->
case stripDir srcDir srcSubDir of
Nothing -> return ()
Just relSubDir -> copyDirectoryRecursive srcSubDir (destDir </> relSubDir))

0 comments on commit 410ff24

Please sign in to comment.