module System.Directory.Recursive ( getDirRecursive
, getSubdirsRecursive
, getFilesRecursive
, getDirFiltered
) where
import Control.Applicative (pure, (<$>))
import Control.Monad (filterM)
import Data.Foldable (fold)
import Data.Traversable (traverse)
import System.Directory (doesDirectoryExist, doesFileExist,
listDirectory)
import System.FilePath ((</>))
import System.IO.Unsafe (unsafeInterleaveIO)
getSubdirsRecursive :: FilePath -> IO [FilePath]
getSubdirsRecursive = getDirFiltered doesDirectoryExist
getDirRecursive :: FilePath -> IO [FilePath]
getDirRecursive = getDirFiltered (const $ pure True)
getFilesRecursive :: FilePath -> IO [FilePath]
getFilesRecursive fp = getDirRecursive fp >>= filterM doesFileExist
{-# INLINE getDirFiltered #-}
getDirFiltered :: (FilePath -> IO Bool)
-> FilePath
-> IO [FilePath]
getDirFiltered p fp = do
all' <- listDirectory fp
all'' <- filterM p (mkRel <$> all')
dirs <- filterM doesDirectoryExist all''
case dirs of
[] -> pure all''
ds -> do
next <- unsafeInterleaveIO $ foldMapA (getDirFiltered p) ds
pure $ all'' ++ next
where mkRel = (fp </>)
foldMapA = (fmap fold .) . traverse