git-annex/Utility/FileMode.hs

188 lines
5.1 KiB
Haskell
Raw Normal View History

2011-09-23 22:13:24 +00:00
{- File mode utilities.
-
- Copyright 2010-2017 Joey Hess <id@joeyh.name>
2011-09-23 22:13:24 +00:00
-
- License: BSD-2-clause
2011-09-23 22:13:24 +00:00
-}
2013-05-10 21:29:59 +00:00
{-# LANGUAGE CPP #-}
2015-10-08 18:26:21 +00:00
module Utility.FileMode (
2015-10-12 19:08:17 +00:00
module Utility.FileMode,
2015-10-08 18:26:21 +00:00
FileMode,
) where
2011-09-23 22:13:24 +00:00
import System.IO
import Control.Monad
import System.PosixCompat.Types
import System.PosixCompat.Files
#ifndef mingw32_HOST_OS
import System.Posix.Files (symbolicLinkMode)
2015-12-28 17:05:08 +00:00
import Control.Monad.IO.Class (liftIO)
2013-05-11 22:23:41 +00:00
#endif
2015-12-28 17:05:08 +00:00
import Control.Monad.IO.Class (MonadIO)
2011-09-23 22:13:24 +00:00
import Foreign (complement)
2015-11-12 22:03:49 +00:00
import Control.Monad.Catch
2011-09-23 22:13:24 +00:00
import Utility.Exception
{- 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
{- 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
{- 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)
writeModes :: [FileMode]
writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode]
readModes :: [FileMode]
readModes = [ownerReadMode, groupReadMode, otherReadMode]
2011-09-23 22:13:24 +00:00
executeModes :: [FileMode]
executeModes = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
2014-04-01 00:15:16 +00:00
otherGroupModes :: [FileMode]
otherGroupModes =
[ groupReadMode, otherReadMode
, groupWriteMode, otherWriteMode
, groupExecuteMode, otherExecuteMode
2014-04-01 00:15:16 +00:00
]
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]
2013-11-20 17:42:13 +00:00
{- Turns a file's owner read bit back on. -}
allowRead :: FilePath -> IO ()
allowRead f = modifyFileMode f $ addModes [ownerReadMode]
{- Allows owner and group to read and write to a file. -}
groupSharedModes :: [FileMode]
groupSharedModes =
2012-04-21 20:01:56 +00:00
[ ownerWriteMode, groupWriteMode
, ownerReadMode, groupReadMode
]
groupWriteRead :: FilePath -> IO ()
groupWriteRead f = modifyFileMode f $ addModes groupSharedModes
checkMode :: FileMode -> FileMode -> Bool
checkMode checkfor mode = checkfor `intersectFileModes` mode == checkfor
{- Checks if a file mode indicates it's a symlink. -}
isSymLink :: FileMode -> Bool
#ifdef mingw32_HOST_OS
2013-05-10 21:29:59 +00:00
isSymLink _ = False
#else
isSymLink = checkMode symbolicLinkMode
2013-05-10 21:29:59 +00:00
#endif
2012-03-14 16:17:38 +00:00
{- Checks if a file has any executable bits set. -}
isExecutable :: FileMode -> Bool
isExecutable mode = combineModes executeModes `intersectFileModes` mode /= 0
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. -}
2015-11-12 22:03:49 +00:00
noUmask :: (MonadIO m, MonadMask m) => FileMode -> m a -> m a
#ifndef mingw32_HOST_OS
noUmask mode a
| mode == stdFileMode = a
| otherwise = withUmask nullFileMode a
#else
noUmask _ a = a
#endif
2015-11-12 22:03:49 +00:00
withUmask :: (MonadIO m, MonadMask m) => FileMode -> m a -> m a
#ifndef mingw32_HOST_OS
withUmask umask a = bracket setup cleanup go
2012-12-13 04:24:19 +00:00
where
2015-11-12 22:03:49 +00:00
setup = liftIO $ setFileCreationMask umask
cleanup = liftIO . setFileCreationMask
2012-12-13 04:24:19 +00:00
go _ = a
2013-05-10 21:29:59 +00:00
#else
withUmask _ a = a
2013-05-10 21:29:59 +00:00
#endif
getUmask :: IO FileMode
#ifndef mingw32_HOST_OS
getUmask = bracket setup cleanup return
where
setup = setFileCreationMask nullFileMode
cleanup = setFileCreationMask
#else
getUmask = return nullFileMode
#endif
defaultFileMode :: IO FileMode
defaultFileMode = do
umask <- getUmask
return $ intersectFileModes (complement umask) stdFileMode
2012-04-21 20:01:56 +00:00
combineModes :: [FileMode] -> FileMode
2015-04-29 18:15:08 +00:00
combineModes [] = 0
2012-04-21 20:01:56 +00:00
combineModes [m] = m
combineModes (m:ms) = foldl unionFileModes m ms
isSticky :: FileMode -> Bool
#ifdef mingw32_HOST_OS
isSticky _ = False
2013-05-10 21:29:59 +00:00
#else
isSticky = checkMode stickyMode
stickyMode :: FileMode
stickyMode = 512
setSticky :: FilePath -> IO ()
setSticky f = modifyFileMode f $ addModes [stickyMode]
#endif
{- Writes a file, ensuring that its modes do not allow it to be read
- or written by anyone other than the current user,
- before any content is written.
-
- When possible, this is done using the umask.
-
- On a filesystem that does not support file permissions, this is the same
- as writeFile.
-}
writeFileProtected :: FilePath -> String -> IO ()
writeFileProtected file content = writeFileProtected' file
(\h -> hPutStr h content)
writeFileProtected' :: FilePath -> (Handle -> IO ()) -> IO ()
writeFileProtected' file writer = protectedOutput $
withFile file WriteMode $ \h -> do
2014-04-01 00:15:16 +00:00
void $ tryIO $ modifyFileMode file $ removeModes otherGroupModes
writer h
protectedOutput :: IO a -> IO a
protectedOutput = withUmask 0o0077