diff options
author | Joey Hess <joey@kitenet.net> | 2012-04-21 16:01:56 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-04-21 16:01:56 -0400 |
commit | 7e45712d194aa2b231083c3ccee3668f053e5717 (patch) | |
tree | 3fbeafd9271bc936495dc61a40e515ef8914d47c /Utility/FileMode.hs | |
parent | b4a5e39ee62020380fc0dcf7aecaaf593d44dba5 (diff) |
better file mode setting code
Diffstat (limited to 'Utility/FileMode.hs')
-rw-r--r-- | Utility/FileMode.hs | 57 |
1 files changed, 24 insertions, 33 deletions
diff --git a/Utility/FileMode.hs b/Utility/FileMode.hs index 98c7124c2..f3db70923 100644 --- a/Utility/FileMode.hs +++ b/Utility/FileMode.hs @@ -13,11 +13,6 @@ import Control.Exception (bracket) import System.Posix.Types import Foreign (complement) -combineModes :: [FileMode] -> FileMode -combineModes [] = undefined -combineModes [m] = m -combineModes (m:ms) = foldl unionFileModes m ms - {- Applies a conversion function to a file's mode. -} modifyFileMode :: FilePath -> (FileMode -> FileMode) -> IO () modifyFileMode f convert = do @@ -32,6 +27,15 @@ modifyFileMode' f convert = do setFileMode f new return old +{- Adds the specified FileModes to the input mode, leaving the rest + - unchanged. -} +addModes :: [FileMode] -> FileMode -> FileMode +addModes ms m = combineModes (m:ms) + +{- Removes the specified FileModes from the input mode. -} +removeModes :: [FileMode] -> FileMode -> FileMode +removeModes ms m = m `intersectFileModes` complement (combineModes ms) + {- Runs an action after changing a file's mode, then restores the old mode. -} withModifiedFileMode :: FilePath -> (FileMode -> FileMode) -> IO a -> IO a withModifiedFileMode file convert a = bracket setup cleanup go @@ -40,45 +44,27 @@ withModifiedFileMode file convert a = bracket setup cleanup go cleanup oldmode = modifyFileMode file (const oldmode) go _ = a -{- Removes a FileMode from a file. - - For example, call with otherWriteMode to chmod o-w -} -unsetFileMode :: FilePath -> FileMode -> IO () -unsetFileMode f m = modifyFileMode f $ - \cur -> cur `intersectFileModes` complement m +writeModes :: [FileMode] +writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode] + +readModes :: [FileMode] +readModes = [ownerReadMode, groupReadMode, otherReadMode] {- Removes the write bits from a file. -} preventWrite :: FilePath -> IO () -preventWrite f = unsetFileMode f $ combineModes writebits - where - writebits = [ownerWriteMode, groupWriteMode, otherWriteMode] +preventWrite f = modifyFileMode f $ removeModes writeModes -{- Turns a file's write bit back on. -} +{- Turns a file's owner write bit back on. -} allowWrite :: FilePath -> IO () -allowWrite f = modifyFileMode f $ - \cur -> cur `unionFileModes` ownerWriteMode +allowWrite f = modifyFileMode f $ addModes [ownerWriteMode] {- Allows owner and group to read and write to a file. -} groupWriteRead :: FilePath -> IO () -groupWriteRead f = modifyFileMode f $ \cur -> combineModes - [ cur - , ownerWriteMode, groupWriteMode +groupWriteRead f = modifyFileMode f $ addModes + [ ownerWriteMode, groupWriteMode , ownerReadMode, groupReadMode ] -{- Allows group to read a file. -} -groupRead :: FilePath -> IO () -groupRead f = modifyFileMode f $ \cur -> combineModes - [ cur - , ownerReadMode, groupReadMode - ] - -{- Allows all to read a file. -} -allRead :: FilePath -> IO () -allRead f = modifyFileMode f $ \cur -> combineModes - [ cur - , ownerReadMode, groupReadMode, otherReadMode - ] - {- Checks if a file mode indicates it's a symlink. -} isSymLink :: FileMode -> Bool isSymLink mode = symbolicLinkMode `intersectFileModes` mode == symbolicLinkMode @@ -88,3 +74,8 @@ isExecutable :: FileMode -> Bool isExecutable mode = combineModes ebits `intersectFileModes` mode /= 0 where ebits = [ownerExecuteMode, groupExecuteMode, otherExecuteMode] + +combineModes :: [FileMode] -> FileMode +combineModes [] = undefined +combineModes [m] = m +combineModes (m:ms) = foldl unionFileModes m ms |