2014-03-31 03:37:54 +00:00
|
|
|
|
module Propellor.Property.File where
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
2014-03-31 03:55:59 +00:00
|
|
|
|
import Propellor
|
2014-04-13 15:58:22 +00:00
|
|
|
|
import Utility.FileMode
|
2014-03-30 19:14:36 +00:00
|
|
|
|
|
2014-04-04 04:08:30 +00:00
|
|
|
|
import System.Posix.Files
|
2014-04-13 15:58:22 +00:00
|
|
|
|
import System.PosixCompat.Types
|
2014-04-04 04:08:30 +00:00
|
|
|
|
|
2014-03-30 19:31:57 +00:00
|
|
|
|
type Line = String
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
2014-03-31 04:43:28 +00:00
|
|
|
|
-- | Replaces all the content of a file.
|
2014-03-30 17:12:33 +00:00
|
|
|
|
hasContent :: FilePath -> [Line] -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
|
f `hasContent` newcontent = fileProperty ("replace " ++ f)
|
|
|
|
|
(\_oldcontent -> newcontent) f
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
2014-04-08 20:58:11 +00:00
|
|
|
|
-- | Ensures a file has contents that comes from PrivData.
|
2014-04-13 15:58:22 +00:00
|
|
|
|
--
|
|
|
|
|
-- The file's permissions are preserved if the file already existed.
|
|
|
|
|
-- Otherwise, they're set to 600.
|
2014-07-06 19:56:56 +00:00
|
|
|
|
hasPrivContent :: FilePath -> Context -> Property
|
2014-07-18 02:35:59 +00:00
|
|
|
|
hasPrivContent = hasPrivContent' writeFileProtected
|
2014-04-08 20:58:11 +00:00
|
|
|
|
|
2014-07-18 02:35:59 +00:00
|
|
|
|
-- | Leaves the file at its default or current mode,
|
|
|
|
|
-- allowing "private" data to be read.
|
|
|
|
|
--
|
|
|
|
|
-- Use with caution!
|
2014-07-06 19:56:56 +00:00
|
|
|
|
hasPrivContentExposed :: FilePath -> Context -> Property
|
2014-07-18 02:35:59 +00:00
|
|
|
|
hasPrivContentExposed = hasPrivContent' writeFile
|
|
|
|
|
|
|
|
|
|
hasPrivContent' :: (String -> FilePath -> IO ()) -> FilePath -> Context -> Property
|
|
|
|
|
hasPrivContent' writer f context =
|
|
|
|
|
withPrivData (PrivFile f) context $ \getcontent ->
|
|
|
|
|
property desc $ getcontent $ \privcontent ->
|
|
|
|
|
ensureProperty $ fileProperty' writer desc
|
|
|
|
|
(\_oldcontent -> lines privcontent) f
|
|
|
|
|
where
|
|
|
|
|
desc = "privcontent " ++ f
|
2014-04-13 16:21:43 +00:00
|
|
|
|
|
2014-03-31 04:43:28 +00:00
|
|
|
|
-- | Ensures that a line is present in a file, adding it to the end if not.
|
2014-03-30 17:12:33 +00:00
|
|
|
|
containsLine :: FilePath -> Line -> Property
|
2014-04-13 18:01:30 +00:00
|
|
|
|
f `containsLine` l = f `containsLines` [l]
|
|
|
|
|
|
|
|
|
|
containsLines :: FilePath -> [Line] -> Property
|
2014-07-18 03:32:29 +00:00
|
|
|
|
f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f
|
2014-03-30 17:12:33 +00:00
|
|
|
|
where
|
2014-07-18 03:32:29 +00:00
|
|
|
|
go content = content ++ filter (`notElem` content) ls
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
2014-03-31 04:43:28 +00:00
|
|
|
|
-- | Ensures that a line is not present in a file.
|
|
|
|
|
-- Note that the file is ensured to exist, so if it doesn't, an empty
|
2014-04-03 06:27:17 +00:00
|
|
|
|
-- file will be written.
|
2014-03-30 17:12:33 +00:00
|
|
|
|
lacksLine :: FilePath -> Line -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
|
f `lacksLine` l = fileProperty (f ++ " remove: " ++ l) (filter (/= l)) f
|
2014-03-30 19:14:36 +00:00
|
|
|
|
|
2014-03-31 04:43:28 +00:00
|
|
|
|
-- | Removes a file. Does not remove symlinks or non-plain-files.
|
2014-03-30 19:14:36 +00:00
|
|
|
|
notPresent :: FilePath -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
|
notPresent f = check (doesFileExist f) $ property (f ++ " not present") $
|
2014-03-30 19:14:36 +00:00
|
|
|
|
makeChange $ nukeFile f
|
2014-03-30 19:31:57 +00:00
|
|
|
|
|
|
|
|
|
fileProperty :: Desc -> ([Line] -> [Line]) -> FilePath -> Property
|
2014-04-13 15:58:22 +00:00
|
|
|
|
fileProperty = fileProperty' writeFile
|
|
|
|
|
fileProperty' :: (FilePath -> String -> IO ()) -> Desc -> ([Line] -> [Line]) -> FilePath -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
|
fileProperty' writer desc a f = property desc $ go =<< liftIO (doesFileExist f)
|
2014-03-30 19:31:57 +00:00
|
|
|
|
where
|
|
|
|
|
go True = do
|
2014-04-10 21:22:32 +00:00
|
|
|
|
ls <- liftIO $ lines <$> readFile f
|
2014-03-30 19:31:57 +00:00
|
|
|
|
let ls' = a ls
|
|
|
|
|
if ls' == ls
|
|
|
|
|
then noChange
|
2014-04-04 04:08:30 +00:00
|
|
|
|
else makeChange $ viaTmp updatefile f (unlines ls')
|
2014-04-13 15:58:22 +00:00
|
|
|
|
go False = makeChange $ writer f (unlines $ a [])
|
2014-04-01 05:12:05 +00:00
|
|
|
|
|
2014-04-04 04:08:30 +00:00
|
|
|
|
-- viaTmp makes the temp file mode 600.
|
2014-04-13 15:58:22 +00:00
|
|
|
|
-- Replicate the original file's owner and mode.
|
2014-04-04 04:08:30 +00:00
|
|
|
|
updatefile f' content = do
|
2014-04-13 15:58:22 +00:00
|
|
|
|
writer f' content
|
|
|
|
|
s <- getFileStatus f
|
|
|
|
|
setFileMode f' (fileMode s)
|
|
|
|
|
setOwnerAndGroup f' (fileOwner s) (fileGroup s)
|
2014-04-04 04:08:30 +00:00
|
|
|
|
|
2014-04-01 05:12:05 +00:00
|
|
|
|
-- | Ensures a directory exists.
|
|
|
|
|
dirExists :: FilePath -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
|
dirExists d = check (not <$> doesDirectoryExist d) $ property (d ++ " exists") $
|
2014-04-01 05:12:05 +00:00
|
|
|
|
makeChange $ createDirectoryIfMissing True d
|
2014-04-10 07:06:35 +00:00
|
|
|
|
|
|
|
|
|
-- | Ensures that a file/dir has the specified owner and group.
|
|
|
|
|
ownerGroup :: FilePath -> UserName -> GroupName -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
|
ownerGroup f owner group = property (f ++ " owner " ++ og) $ do
|
2014-04-10 07:06:35 +00:00
|
|
|
|
r <- ensureProperty $ cmdProperty "chown" [og, f]
|
|
|
|
|
if r == FailedChange
|
|
|
|
|
then return r
|
|
|
|
|
else noChange
|
|
|
|
|
where
|
|
|
|
|
og = owner ++ ":" ++ group
|
2014-04-13 15:58:22 +00:00
|
|
|
|
|
|
|
|
|
-- | Ensures that a file/dir has the specfied mode.
|
|
|
|
|
mode :: FilePath -> FileMode -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
|
mode f v = property (f ++ " mode " ++ show v) $ do
|
2014-04-13 15:58:22 +00:00
|
|
|
|
liftIO $ modifyFileMode f (\_old -> v)
|
|
|
|
|
noChange
|