summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/File.hs
diff options
context:
space:
mode:
authorSean Whitton2017-10-28 13:13:36 -0700
committerSean Whitton2017-10-28 13:13:36 -0700
commit18dfecfa1e39842365b5b8d2bd99dfb6dc8bd510 (patch)
tree190264d3c3becf51042063dae4c6a586130214d4 /src/Propellor/Property/File.hs
parent5a182b90922464cd507218054e0071eb105472e1 (diff)
File.isSymlinkedTo now revertable
Diffstat (limited to 'src/Propellor/Property/File.hs')
-rw-r--r--src/Propellor/Property/File.hs22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs
index 3293599a..340a6d02 100644
--- a/src/Propellor/Property/File.hs
+++ b/src/Propellor/Property/File.hs
@@ -126,18 +126,30 @@ newtype LinkTarget = LinkTarget FilePath
-- | Creates or atomically updates a symbolic link.
--
+-- Revert to ensure the symlink is not present.
+--
-- Does not overwrite regular files or directories.
-isSymlinkedTo :: FilePath -> LinkTarget -> Property UnixLike
-link `isSymlinkedTo` (LinkTarget target) = property desc $
- go =<< (liftIO $ tryIO $ getSymbolicLinkStatus link)
+isSymlinkedTo :: FilePath -> LinkTarget -> RevertableProperty UnixLike UnixLike
+link `isSymlinkedTo` (LinkTarget target) = linked <!> notLinked
where
- desc = link ++ " is symlinked to " ++ target
+ linked = property (link ++ " is symlinked to " ++ target) $
+ go =<< getLinkStatus
+
go (Right stat) =
if isSymbolicLink stat
then checkLink
else nonSymlinkExists
go (Left _) = makeChange $ createSymbolicLink target link
+ notLinked = property (link ++ "does not exist as a symlink") $
+ stop =<< getLinkStatus
+
+ stop (Right stat) =
+ if isSymbolicLink stat
+ then makeChange $ nukeFile link
+ else nonSymlinkExists
+ stop (Left _) = noChange
+
nonSymlinkExists = do
warningMessage $ link ++ " exists and is not a symlink"
return FailedChange
@@ -148,6 +160,8 @@ link `isSymlinkedTo` (LinkTarget target) = property desc $
else makeChange updateLink
updateLink = createSymbolicLink target `viaStableTmp` link
+ getLinkStatus = liftIO $ tryIO $ getSymbolicLinkStatus link
+
-- | Ensures that a file is a copy of another (regular) file.
isCopyOf :: FilePath -> FilePath -> Property UnixLike
f `isCopyOf` src = property desc $ go =<< (liftIO $ tryIO $ getFileStatus src)