better file mode setting code
This commit is contained in:
parent
b4a5e39ee6
commit
7e45712d19
3 changed files with 30 additions and 40 deletions
|
@ -337,12 +337,12 @@ preseedTmp key file = go =<< inAnnex key
|
||||||
freezeContent :: FilePath -> Annex ()
|
freezeContent :: FilePath -> Annex ()
|
||||||
freezeContent file = liftIO . go =<< fromRepo getSharedRepository
|
freezeContent file = liftIO . go =<< fromRepo getSharedRepository
|
||||||
where
|
where
|
||||||
go GroupShared = do
|
go GroupShared = modifyFileMode file $
|
||||||
preventWrite file
|
removeModes writeModes .
|
||||||
groupRead file
|
addModes [ownerReadMode, groupReadMode]
|
||||||
go AllShared = do
|
go AllShared = modifyFileMode file $
|
||||||
preventWrite file
|
removeModes writeModes .
|
||||||
allRead file
|
addModes readModes
|
||||||
go _ = preventWrite file
|
go _ = preventWrite file
|
||||||
|
|
||||||
{- Allows writing to an annexed file that freezeContent was called on
|
{- Allows writing to an annexed file that freezeContent was called on
|
||||||
|
|
|
@ -11,7 +11,6 @@ import Common.Annex
|
||||||
import Command
|
import Command
|
||||||
import Annex.Content
|
import Annex.Content
|
||||||
import Utility.CopyFile
|
import Utility.CopyFile
|
||||||
import Utility.FileMode
|
|
||||||
|
|
||||||
def :: [Command]
|
def :: [Command]
|
||||||
def =
|
def =
|
||||||
|
|
|
@ -13,11 +13,6 @@ import Control.Exception (bracket)
|
||||||
import System.Posix.Types
|
import System.Posix.Types
|
||||||
import Foreign (complement)
|
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. -}
|
{- Applies a conversion function to a file's mode. -}
|
||||||
modifyFileMode :: FilePath -> (FileMode -> FileMode) -> IO ()
|
modifyFileMode :: FilePath -> (FileMode -> FileMode) -> IO ()
|
||||||
modifyFileMode f convert = do
|
modifyFileMode f convert = do
|
||||||
|
@ -32,6 +27,15 @@ modifyFileMode' f convert = do
|
||||||
setFileMode f new
|
setFileMode f new
|
||||||
return old
|
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. -}
|
{- Runs an action after changing a file's mode, then restores the old mode. -}
|
||||||
withModifiedFileMode :: FilePath -> (FileMode -> FileMode) -> IO a -> IO a
|
withModifiedFileMode :: FilePath -> (FileMode -> FileMode) -> IO a -> IO a
|
||||||
withModifiedFileMode file convert a = bracket setup cleanup go
|
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)
|
cleanup oldmode = modifyFileMode file (const oldmode)
|
||||||
go _ = a
|
go _ = a
|
||||||
|
|
||||||
{- Removes a FileMode from a file.
|
writeModes :: [FileMode]
|
||||||
- For example, call with otherWriteMode to chmod o-w -}
|
writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode]
|
||||||
unsetFileMode :: FilePath -> FileMode -> IO ()
|
|
||||||
unsetFileMode f m = modifyFileMode f $
|
readModes :: [FileMode]
|
||||||
\cur -> cur `intersectFileModes` complement m
|
readModes = [ownerReadMode, groupReadMode, otherReadMode]
|
||||||
|
|
||||||
{- Removes the write bits from a file. -}
|
{- Removes the write bits from a file. -}
|
||||||
preventWrite :: FilePath -> IO ()
|
preventWrite :: FilePath -> IO ()
|
||||||
preventWrite f = unsetFileMode f $ combineModes writebits
|
preventWrite f = modifyFileMode f $ removeModes writeModes
|
||||||
where
|
|
||||||
writebits = [ownerWriteMode, groupWriteMode, otherWriteMode]
|
|
||||||
|
|
||||||
{- Turns a file's write bit back on. -}
|
{- Turns a file's owner write bit back on. -}
|
||||||
allowWrite :: FilePath -> IO ()
|
allowWrite :: FilePath -> IO ()
|
||||||
allowWrite f = modifyFileMode f $
|
allowWrite f = modifyFileMode f $ addModes [ownerWriteMode]
|
||||||
\cur -> cur `unionFileModes` ownerWriteMode
|
|
||||||
|
|
||||||
{- Allows owner and group to read and write to a file. -}
|
{- Allows owner and group to read and write to a file. -}
|
||||||
groupWriteRead :: FilePath -> IO ()
|
groupWriteRead :: FilePath -> IO ()
|
||||||
groupWriteRead f = modifyFileMode f $ \cur -> combineModes
|
groupWriteRead f = modifyFileMode f $ addModes
|
||||||
[ cur
|
[ ownerWriteMode, groupWriteMode
|
||||||
, ownerWriteMode, groupWriteMode
|
|
||||||
, ownerReadMode, groupReadMode
|
, 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. -}
|
{- Checks if a file mode indicates it's a symlink. -}
|
||||||
isSymLink :: FileMode -> Bool
|
isSymLink :: FileMode -> Bool
|
||||||
isSymLink mode = symbolicLinkMode `intersectFileModes` mode == symbolicLinkMode
|
isSymLink mode = symbolicLinkMode `intersectFileModes` mode == symbolicLinkMode
|
||||||
|
@ -88,3 +74,8 @@ isExecutable :: FileMode -> Bool
|
||||||
isExecutable mode = combineModes ebits `intersectFileModes` mode /= 0
|
isExecutable mode = combineModes ebits `intersectFileModes` mode /= 0
|
||||||
where
|
where
|
||||||
ebits = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
|
ebits = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
|
||||||
|
|
||||||
|
combineModes :: [FileMode] -> FileMode
|
||||||
|
combineModes [] = undefined
|
||||||
|
combineModes [m] = m
|
||||||
|
combineModes (m:ms) = foldl unionFileModes m ms
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue