summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoey Hess2015-12-06 13:30:50 -0400
committerJoey Hess2015-12-06 13:30:50 -0400
commit94f91a44810dc3a1eca95c843e3c444cbbe87006 (patch)
tree1a4cee76e0b189e7a07fa563ee5401e3130741a4 /src
parentf404f5ed9a79449c620fde5bd669ab41fcb8d0fb (diff)
add isNewerThan and use it to avoid unnecessary running of newaliases
Diffstat (limited to 'src')
-rw-r--r--src/Propellor/Property.hs25
-rw-r--r--src/Propellor/Property/Postfix.hs5
2 files changed, 28 insertions, 2 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.
--
diff --git a/src/Propellor/Property/Postfix.hs b/src/Propellor/Property/Postfix.hs
index bcb9fb30..e9fdfc38 100644
--- a/src/Propellor/Property/Postfix.hs
+++ b/src/Propellor/Property/Postfix.hs
@@ -60,8 +60,9 @@ mappedFile f setup = setup f
-- | Run newaliases command, which should be done after changing
-- @/etc/aliases@.
newaliases :: Property NoInfo
-newaliases = cmdProperty "newaliases" []
- `assume` MadeChange
+newaliases = check ("/etc/aliases" `isNewerThan` "/etc/aliases.db") $
+ cmdProperty "newaliases" []
+ `assume` MadeChange
-- | The main config file for postfix.
mainCfFile :: FilePath