on f104f91 help sign in
67 lines 2.6 kB history raw
1-- | A module containing various file utility functions
2--
3module Hakyll.Core.Util.File
4 ( makeDirectories
5 , getRecursiveContents
6 , isFileObsolete
7 ) where
9import System.FilePath (normalise, takeDirectory, (</>))
10import System.Time (ClockTime)
11import Control.Monad (forM, filterM)
12import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
13 , doesFileExist, getModificationTime
14 , getDirectoryContents
15 )
17-- | Given a path to a file, try to make the path writable by making
18-- all directories on the path.
19--
20makeDirectories :: FilePath -> IO ()
21makeDirectories = createDirectoryIfMissing True . takeDirectory
23-- | Get all contents of a directory. Note that files starting with a dot (.)
24-- will be ignored.
25--
26getRecursiveContents :: FilePath -> IO [FilePath]
27getRecursiveContents topdir = do
28 topdirExists <- doesDirectoryExist topdir
29 if topdirExists
30 then do names <- getDirectoryContents topdir
31 let properNames = filter isProper names
32 paths <- forM properNames $ \name -> do
33 let path = topdir </> name
34 isDirectory <- doesDirectoryExist path
35 if isDirectory
36 then getRecursiveContents path
37 else return [normalise path]
38 return (concat paths)
39 else return []
40 where
41 isProper = not . (== ".") . take 1
43-- | Check if a timestamp is obsolete compared to the timestamps of a number of
44-- files. When they are no files, it is never obsolete.
45--
46isObsolete :: ClockTime -- ^ The time to check.
47 -> [FilePath] -- ^ Dependencies of the cached file.
48 -> IO Bool
49isObsolete _ [] = return False
50isObsolete timeStamp depends = do
51 depends' <- filterM doesFileExist depends
52 dependsModified <- mapM getModificationTime depends'
53 return (timeStamp < maximum dependsModified)
55-- | Check if a file is obsolete, given it's dependencies. When the file does
56-- not exist, it is always obsolete. Other wise, it is obsolete if any of it's
57-- dependencies has a more recent modification time than the file.
58--
59isFileObsolete :: FilePath -- ^ The cached file
60 -> [FilePath] -- ^ Dependencies of the cached file
61 -> IO Bool
62isFileObsolete file depends = do
63 exists <- doesFileExist file
64 if not exists
65 then return True
66 else do timeStamp <- getModificationTime file
67 isObsolete timeStamp depends