diff options
| author | Joey Hess <joeyh@joeyh.name> | 2015-12-06 13:30:50 -0400 |
|---|---|---|
| committer | Joey Hess <joeyh@joeyh.name> | 2015-12-06 13:30:50 -0400 |
| commit | 94f91a44810dc3a1eca95c843e3c444cbbe87006 (patch) | |
| tree | 1a4cee76e0b189e7a07fa563ee5401e3130741a4 /src/Propellor/Property.hs | |
| parent | f404f5ed9a79449c620fde5bd669ab41fcb8d0fb (diff) | |
add isNewerThan and use it to avoid unnecessary running of newaliases
Diffstat (limited to 'src/Propellor/Property.hs')
| -rw-r--r-- | src/Propellor/Property.hs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/Propellor/Property.hs b/src/Propellor/Property.hs index c58cc9fe..eacb6004 100644 --- a/src/Propellor/Property.hs +++ b/src/Propellor/Property.hs @@ -29,6 +29,7 @@ module Propellor.Property ( , unchecked , changesFile , changesFileContent + , isNewerThan , checkResult , Checkable , assume @@ -230,6 +231,30 @@ changesFileContent p f = checkResult getmd5 comparemd5 p newmd5 <- getmd5 return $ if oldmd5 == newmd5 then NoChange else MadeChange +-- | Determines if the first file is newer than the second file. +-- +-- This can be used with `check` to only run a command when a file +-- has changed. +-- +-- > check ("/etc/aliases" `isNewerThan` "/etc/aliases.db") +-- > (cmdProperty "newaliases" [] `assume` MadeChange) -- updates aliases.db +-- +-- Or it can be used with `checkResult` to test if a command made a change. +-- +-- > checkResult (return ()) +-- > (\_ -> "/etc/aliases.db" `isNewerThan` "/etc/aliases") +-- > (cmdProperty "newaliases" []) +-- +-- (If one of the files does not exist, the file that does exist is +-- considered to be the newer of the two.) +isNewerThan :: FilePath -> FilePath -> IO Bool +isNewerThan x y = do + mx <- mtime x + my <- mtime y + return (mx > my) + where + mtime f = catchMaybeIO $ modificationTimeHiRes <$> getFileStatus f + -- | Makes a property that is satisfied differently depending on the host's -- operating system. -- |
