LIB: make deleteDirRecursive more robust

We now try 'deleteDir' first and only start recursive removal
if that fails.
This commit is contained in:
Julian Ospald 2016-05-03 11:54:25 +02:00
parent 4a86b4d2cf
commit 6ec455b515
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28

View File

@ -71,8 +71,10 @@ import Data.Word
)
import Foreign.C.Error
(
eINVAL
eEXIST
, eINVAL
, eNOSYS
, eNOTEMPTY
, eXDEV
)
import Foreign.C.Types
@ -416,17 +418,20 @@ deleteDir p = P.withAbsPath p removeDirectory
-- * not atomic
-- * examines filetypes explicitly
deleteDirRecursive :: Path Abs -> IO ()
deleteDirRecursive p = do
files <- getDirsFiles p
for_ files $ \file -> do
ftype <- getFileType file
case ftype of
SymbolicLink -> deleteFile file
Directory -> deleteDirRecursive file
RegularFile -> deleteFile file
_ -> ioError $ userError $ "No idea what to do with the" ++
"given filetype: " ++ show ftype
removeDirectory . P.toFilePath $ p
deleteDirRecursive p =
catchErrno [eNOTEMPTY, eEXIST]
(deleteDir p)
$ do
files <- getDirsFiles p
for_ files $ \file -> do
ftype <- getFileType file
case ftype of
SymbolicLink -> deleteFile file
Directory -> deleteDirRecursive file
RegularFile -> deleteFile file
_ -> ioError $ userError $ "No idea what to do with the" ++
"given filetype: " ++ show ftype
removeDirectory . P.toFilePath $ p
-- |Deletes a file, directory or symlink, whatever it may be.