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