git-annex/Utility/FileMode.hs

103 lines
3.1 KiB
Haskell
Raw Normal View History

2011-09-23 22:13:24 +00:00
{- File mode utilities.
-
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
2011-09-23 22:13:24 +00:00
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.FileMode where
import Common
import Control.Exception (bracket)
2011-09-23 22:13:24 +00:00
import System.Posix.Types
import Foreign (complement)
{- Applies a conversion function to a file's mode. -}
modifyFileMode :: FilePath -> (FileMode -> FileMode) -> IO ()
modifyFileMode f convert = void $ modifyFileMode' f convert
modifyFileMode' :: FilePath -> (FileMode -> FileMode) -> IO FileMode
modifyFileMode' f convert = do
s <- getFileStatus f
let old = fileMode s
let new = convert old
when (new /= old) $
setFileMode f new
return old
2012-04-21 20:01:56 +00:00
{- 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
where
setup = modifyFileMode' file convert
cleanup oldmode = modifyFileMode file (const oldmode)
go _ = a
2012-04-21 20:01:56 +00:00
writeModes :: [FileMode]
writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode]
readModes :: [FileMode]
readModes = [ownerReadMode, groupReadMode, otherReadMode]
2011-09-23 22:13:24 +00:00
{- Removes the write bits from a file. -}
preventWrite :: FilePath -> IO ()
2012-04-21 20:01:56 +00:00
preventWrite f = modifyFileMode f $ removeModes writeModes
2011-09-23 22:13:24 +00:00
2012-04-21 20:01:56 +00:00
{- Turns a file's owner write bit back on. -}
2011-09-23 22:13:24 +00:00
allowWrite :: FilePath -> IO ()
2012-04-21 20:01:56 +00:00
allowWrite f = modifyFileMode f $ addModes [ownerWriteMode]
{- Allows owner and group to read and write to a file. -}
groupWriteRead :: FilePath -> IO ()
2012-04-21 20:01:56 +00:00
groupWriteRead f = modifyFileMode f $ addModes
[ ownerWriteMode, groupWriteMode
, ownerReadMode, groupReadMode
]
checkMode :: FileMode -> FileMode -> Bool
checkMode checkfor mode = checkfor `intersectFileModes` mode == checkfor
{- Checks if a file mode indicates it's a symlink. -}
isSymLink :: FileMode -> Bool
isSymLink = checkMode symbolicLinkMode
2012-03-14 16:17:38 +00:00
{- Checks if a file has any executable bits set. -}
isExecutable :: FileMode -> Bool
isExecutable mode = combineModes ebits `intersectFileModes` mode /= 0
2012-03-14 16:17:38 +00:00
where
ebits = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
2012-04-21 20:01:56 +00:00
{- Runs an action without that pesky umask influencing it, unless the
- passed FileMode is the standard one. -}
noUmask :: FileMode -> IO a -> IO a
noUmask mode a
| mode == stdFileMode = a
| otherwise = bracket setup cleanup go
where
setup = setFileCreationMask nullFileMode
cleanup = setFileCreationMask
go _ = a
2012-04-21 20:01:56 +00:00
combineModes :: [FileMode] -> FileMode
combineModes [] = undefined
combineModes [m] = m
combineModes (m:ms) = foldl unionFileModes m ms
stickyMode :: FileMode
stickyMode = 512
isSticky :: FileMode -> Bool
isSticky = checkMode stickyMode
setSticky :: FilePath -> IO ()
setSticky f = modifyFileMode f $ addModes [stickyMode]