Improve createDirRecursive

This commit is contained in:
Julian Ospald 2016-06-13 01:38:44 +02:00
parent 5b08e14b55
commit 4dec385332
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
1 changed files with 6 additions and 4 deletions

View File

@ -89,6 +89,10 @@ import Control.Monad
, void , void
, when , when
) )
import Control.Monad.IfElse
(
unlessM
)
import Data.ByteString import Data.ByteString
( (
ByteString ByteString
@ -687,12 +691,11 @@ createDir fm dest = createDirectory (fromAbs dest) fm
-- |Create an empty directory at the given directory with the given filename. -- |Create an empty directory at the given directory with the given filename.
--
-- All parent directories are created with the same filemode. This -- All parent directories are created with the same filemode. This
-- basically behaves like: -- basically behaves like:
-- --
-- @ -- @
-- mkdir -p \/destination\/somedir -- mkdir -p \/some\/dir
-- @ -- @
-- --
-- Safety/reliability concerns: -- Safety/reliability concerns:
@ -709,9 +712,8 @@ createDirRecursive :: FileMode -> Path Abs -> IO ()
createDirRecursive fm dest = createDirRecursive fm dest =
catchIOError (createDirectory (fromAbs dest) fm) $ \e -> do catchIOError (createDirectory (fromAbs dest) fm) $ \e -> do
errno <- getErrno errno <- getErrno
isd <- doesDirectoryExist dest
case errno of case errno of
en | en == eEXIST && isd -> return () en | en == eEXIST -> unlessM (doesDirectoryExist dest) (ioError e)
| en == eNOENT -> createDirRecursive fm (dirname dest) | en == eNOENT -> createDirRecursive fm (dirname dest)
>> createDirectory (fromAbs dest) fm >> createDirectory (fromAbs dest) fm
| otherwise -> ioError e | otherwise -> ioError e