Skip to content

Commit 0b8aa67

Browse files
add tryRemove to ignore errors while removing filesystem trees
1 parent 12ef61f commit 0b8aa67

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/System/File/Tree.hs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module System.File.Tree
3636
, moveTo, moveTo_
3737
, mergeInto, mergeInto_
3838
-- ***removing
39-
, remove, tryRemoveWith, removeEmptyDirectories
39+
, remove, tryRemove, tryRemoveWith, removeEmptyDirectories
4040
)where
4141

4242
import System.IO.Unsafe (unsafeInterleaveIO)
@@ -55,11 +55,11 @@ import Data.DList as DL (DList(..), cons, append, toList, empty, concat, snoc)
5555
import Control.Exception (throwIO, catch, IOException)
5656
import Control.Monad (forM, liftM, liftM2, void)
5757
import Control.Monad.Identity (runIdentity)
58-
import Control.Applicative ((<$>), (<*))
58+
import Control.Applicative ((<$>), (<*>), (<*))
5959
import Control.Arrow (second)
6060
import Data.Foldable (foldrM)
6161
import qualified Data.Traversable as T (mapM)
62-
import Data.Maybe (mapMaybe)
62+
import Data.Maybe (mapMaybe, catMaybes)
6363
import Data.Function (on)
6464
import Data.Lens.Common (Lens, lens, getL, setL, modL)
6565
import Control.Cond (ifM, (<&&>), notM, whenM)
@@ -285,16 +285,19 @@ mergeInto_ :: FilePath -> FSTree -> IO ()
285285
mergeInto_ = (void .) . mergeInto
286286

287287
remove :: FSTree -> IO ()
288-
remove = tryRemoveWith throwIO
289-
290-
tryRemoveWith :: (IOException -> IO ()) -> FSTree -> IO ()
291-
tryRemoveWith handler = remove' . prependPaths
292-
where remove' (Node p cs) = do
293-
P.mapM_ remove' cs
294-
ifM (doesDirectoryExist p)
295-
(removeDirectory p)
296-
(removeFile p)
297-
`catch` handler
288+
remove = void . tryRemoveWith throwIO
289+
290+
tryRemove :: FSTree -> IO [IOException]
291+
tryRemove = tryRemoveWith return
292+
293+
tryRemoveWith :: (IOException -> IO a) -> FSTree -> IO [a]
294+
tryRemoveWith handler = fmap (catMaybes . DL.toList) . remove' . prependPaths
295+
where remove' (Node p cs) =
296+
DL.snoc <$> (fmap DL.concat . P.mapM remove' $ cs)
297+
<*> ifM (doesDirectoryExist p)
298+
(removeDirectory p >> return Nothing)
299+
(removeFile p >> return Nothing)
300+
`catch` (fmap Just . handler)
298301

299302

300303
removeEmptyDirectories :: FSTree -> IO ()

0 commit comments

Comments
 (0)