on e71bbd9 help sign in
57 lines 2.3 kB history raw
1--------------------------------------------------------------------------------
2-- | A module containing various file utility functions
3module Hakyll.Core.Util.File
4 ( makeDirectories
5 , getRecursiveContents
6 , removeDirectory
7 ) where
10--------------------------------------------------------------------------------
11import Control.Applicative ((<$>))
12import Control.Monad (filterM, forM, when)
13import System.Directory (createDirectoryIfMissing,
14 doesDirectoryExist, getDirectoryContents,
15 removeDirectoryRecursive)
16import System.FilePath (takeDirectory, (</>))
19--------------------------------------------------------------------------------
20-- | Given a path to a file, try to make the path writable by making
21-- all directories on the path.
22makeDirectories :: FilePath -> IO ()
23makeDirectories = createDirectoryIfMissing True . takeDirectory
26--------------------------------------------------------------------------------
27-- | Get all contents of a directory.
28getRecursiveContents :: (FilePath -> IO Bool) -- ^ Ignore this file/directory
29 -> FilePath -- ^ Directory to search
30 -> IO [FilePath] -- ^ List of files found
31getRecursiveContents ignore top = go ""
32 where
33 isProper x
34 | x `elem` [".", ".."] = return False
35 | otherwise = not <$> ignore x
37 go dir = do
38 dirExists <- doesDirectoryExist (top </> dir)
39 if not dirExists
40 then return []
41 else do
42 names <- filterM isProper =<< getDirectoryContents (top </> dir)
43 paths <- forM names $ \name -> do
44 let rel = dir </> name
45 isDirectory <- doesDirectoryExist (top </> rel)
46 if isDirectory
47 then go rel
48 else return [rel]
50 return $ concat paths
53--------------------------------------------------------------------------------
54removeDirectory :: FilePath -> IO ()
55removeDirectory fp = do
56 e <- doesDirectoryExist fp
57 when e $ removeDirectoryRecursive fp