1--------------------------------------------------------------------------------
3module Hakyll.Core.Provider.Modified
5 , resourceModificationTime
9--------------------------------------------------------------------------------
10import Control.Applicative ((<$>), (<*>))
11import Control.Monad (when)
12import qualified Crypto.Hash.MD5 as MD5
13import qualified Data.ByteString as B
14import qualified Data.ByteString.Lazy as BL
16import qualified Data.Map as M
17import Data.Time (UTCTime)
18import System.Directory (getModificationTime)
21--------------------------------------------------------------------------------
22#if !MIN_VERSION_directory(1,2,0)
23import Data.Time (readTime)
24import System.Locale (defaultTimeLocale)
25import System.Time (formatCalendarTime,
30--------------------------------------------------------------------------------
31import Hakyll.Core.Identifier
32import Hakyll.Core.Provider.Internal
33import Hakyll.Core.Provider.MetadataCache
34import Hakyll.Core.Store (Store)
35import qualified Hakyll.Core.Store as Store
38--------------------------------------------------------------------------------
39-- | A resource is modified if it or its metadata has changed
40resourceModified :: Provider -> Identifier -> IO Bool
42 | not exists = return False
44 cache <- readIORef cacheRef
45 case M.lookup normalized cache of
48 -- Check if the actual file was modified, and do a recursive
49 -- call to check if the metadata file was modified
51 <$> fileDigestModified store filePath
52 <*> resourceModified p (resourceMetadataResource r)
53 modifyIORef cacheRef (M.insert normalized m)
55 -- Important! (But ugly)
56 when m $ resourceInvalidateMetadataCache p r
60 normalized = setVersion Nothing r
61 exists = resourceExists p r
62 store = providerStore p
63 cacheRef = providerModifiedCache p
64 filePath = resourceFilePath p r
67--------------------------------------------------------------------------------
68-- | Utility: Check if a the digest of a file was modified
69fileDigestModified :: Store -> FilePath -> IO Bool
70fileDigestModified store fp = do
71 -- Get the latest seen digest from the store, and calculate the current
73 lastDigest <- Store.get store key
74 newDigest <- fileDigest fp
75 if Store.Found newDigest == lastDigest
76 -- All is fine, not modified
78 -- Resource modified; store new digest
80 Store.set store key newDigest
83 key = ["Hakyll.Core.Resource.Provider.fileModified", fp]
86--------------------------------------------------------------------------------
87-- | Utility: Retrieve a digest for a given file
88fileDigest :: FilePath -> IO B.ByteString
89fileDigest = fmap MD5.hashlazy . BL.readFile
92--------------------------------------------------------------------------------
93resourceModificationTime :: Provider -> Identifier -> IO UTCTime
94resourceModificationTime p i = do
95#if MIN_VERSION_directory(1,2,0)
96 getModificationTime $ resourceFilePath p i
98 ct <- toCalendarTime =<< getModificationTime (resourceFilePath p i)
99 let str = formatCalendarTime defaultTimeLocale "%s" ct
100 return $ readTime defaultTimeLocale "%s" str
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list