Skip to content

Commit 410ff24

Browse files
committed
Add Path.IO.copyDirectoryRecursive (#143)
1 parent 2a564ec commit 410ff24

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Path/IO.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module Path.IO
1414
,removeTree
1515
,removeTreeIfExists
1616
,fileExists
17-
,dirExists)
17+
,dirExists
18+
,copyDirectoryRecursive)
1819
where
1920

2021
import Control.Exception hiding (catch)
@@ -150,3 +151,24 @@ fileExists =
150151
dirExists :: MonadIO m => Path b Dir -> m Bool
151152
dirExists =
152153
liftIO . doesFileExist . toFilePath
154+
155+
-- | Copy a directory recursively. This just uses 'copyFile', so it is not smart about symbolic
156+
-- links or other special files.
157+
copyDirectoryRecursive :: (MonadIO m,MonadThrow m)
158+
=> Path Abs Dir -- ^ Source directory
159+
-> Path Abs Dir -- ^ Destination directory
160+
-> m ()
161+
copyDirectoryRecursive srcDir destDir =
162+
do liftIO (createDirectoryIfMissing False (toFilePath destDir))
163+
(srcSubDirs,srcFiles) <- listDirectory srcDir
164+
forM_ srcFiles
165+
(\srcFile ->
166+
case stripDir srcDir srcFile of
167+
Nothing -> return ()
168+
Just relFile -> liftIO (copyFile (toFilePath srcFile)
169+
(toFilePath (destDir </> relFile))))
170+
forM_ srcSubDirs
171+
(\srcSubDir ->
172+
case stripDir srcDir srcSubDir of
173+
Nothing -> return ()
174+
Just relSubDir -> copyDirectoryRecursive srcSubDir (destDir </> relSubDir))

0 commit comments

Comments
 (0)