2011-09-23 22:13:24 +00:00
|
|
|
{- File mode utilities.
|
|
|
|
-
|
2012-04-21 18:06:36 +00:00
|
|
|
- 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.
|
|
|
|
-}
|
|
|
|
|
2013-05-10 21:29:59 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2011-09-23 22:13:24 +00:00
|
|
|
module Utility.FileMode where
|
|
|
|
|
2012-04-21 15:57:17 +00:00
|
|
|
import Common
|
|
|
|
|
2012-04-21 18:06:36 +00:00
|
|
|
import Control.Exception (bracket)
|
2013-05-11 16:16:47 +00:00
|
|
|
import System.PosixCompat.Types
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-11 22:23:41 +00:00
|
|
|
import System.Posix.Files
|
|
|
|
#endif
|
2011-09-23 22:13:24 +00:00
|
|
|
import Foreign (complement)
|
|
|
|
|
2012-04-21 18:06:36 +00:00
|
|
|
{- Applies a conversion function to a file's mode. -}
|
2012-04-21 15:57:17 +00:00
|
|
|
modifyFileMode :: FilePath -> (FileMode -> FileMode) -> IO ()
|
2012-04-22 03:04:59 +00:00
|
|
|
modifyFileMode f convert = void $ modifyFileMode' f convert
|
2012-04-21 18:06:36 +00:00
|
|
|
modifyFileMode' :: FilePath -> (FileMode -> FileMode) -> IO FileMode
|
|
|
|
modifyFileMode' f convert = do
|
2012-04-21 15:57:17 +00:00
|
|
|
s <- getFileStatus f
|
2012-04-21 18:06:36 +00:00
|
|
|
let old = fileMode s
|
|
|
|
let new = convert old
|
|
|
|
when (new /= old) $
|
2012-04-21 15:57:17 +00:00
|
|
|
setFileMode f new
|
2012-04-21 18:06:36 +00:00
|
|
|
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)
|
|
|
|
|
2012-04-21 18:06:36 +00:00
|
|
|
{- 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
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
setup = modifyFileMode' file convert
|
|
|
|
cleanup oldmode = modifyFileMode file (const oldmode)
|
|
|
|
go _ = a
|
2012-04-21 15:57:17 +00:00
|
|
|
|
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
|
|
|
|
2012-11-09 16:51:54 +00:00
|
|
|
executeModes :: [FileMode]
|
|
|
|
executeModes = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
|
|
|
|
|
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]
|
2011-09-28 20:43:10 +00:00
|
|
|
|
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]
|
|
|
|
|
2012-04-21 18:06:36 +00:00
|
|
|
{- Allows owner and group to read and write to a file. -}
|
2013-11-18 22:05:37 +00:00
|
|
|
groupSharedModes :: [FileMode]
|
|
|
|
groupSharedModes =
|
2012-04-21 20:01:56 +00:00
|
|
|
[ ownerWriteMode, groupWriteMode
|
2012-04-21 18:06:36 +00:00
|
|
|
, ownerReadMode, groupReadMode
|
|
|
|
]
|
|
|
|
|
2013-11-18 22:05:37 +00:00
|
|
|
groupWriteRead :: FilePath -> IO ()
|
|
|
|
groupWriteRead f = modifyFileMode f $ addModes groupSharedModes
|
|
|
|
|
2012-09-25 17:30:32 +00:00
|
|
|
checkMode :: FileMode -> FileMode -> Bool
|
|
|
|
checkMode checkfor mode = checkfor `intersectFileModes` mode == checkfor
|
|
|
|
|
2011-09-28 20:43:10 +00:00
|
|
|
{- Checks if a file mode indicates it's a symlink. -}
|
|
|
|
isSymLink :: FileMode -> Bool
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-05-10 21:29:59 +00:00
|
|
|
isSymLink _ = False
|
2013-05-11 16:16:47 +00:00
|
|
|
#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
|
2012-11-09 16:51:54 +00:00
|
|
|
isExecutable mode = combineModes executeModes `intersectFileModes` mode /= 0
|
2012-04-21 20:01:56 +00:00
|
|
|
|
2012-04-21 20:59:49 +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
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-04-21 20:59:49 +00:00
|
|
|
noUmask mode a
|
|
|
|
| mode == stdFileMode = a
|
|
|
|
| otherwise = bracket setup cleanup go
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
setup = setFileCreationMask nullFileMode
|
|
|
|
cleanup = setFileCreationMask
|
|
|
|
go _ = a
|
2013-05-10 21:29:59 +00:00
|
|
|
#else
|
|
|
|
noUmask _ a = a
|
|
|
|
#endif
|
2012-04-21 20:59:49 +00:00
|
|
|
|
2012-04-21 20:01:56 +00:00
|
|
|
combineModes :: [FileMode] -> FileMode
|
|
|
|
combineModes [] = undefined
|
|
|
|
combineModes [m] = m
|
|
|
|
combineModes (m:ms) = foldl unionFileModes m ms
|
2013-05-11 16:16:47 +00:00
|
|
|
|
|
|
|
isSticky :: FileMode -> Bool
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-05-11 16:16:47 +00:00
|
|
|
isSticky _ = False
|
2013-05-10 21:29:59 +00:00
|
|
|
#else
|
2013-05-11 16:16:47 +00:00
|
|
|
isSticky = checkMode stickyMode
|
2012-09-25 17:30:32 +00:00
|
|
|
|
|
|
|
stickyMode :: FileMode
|
|
|
|
stickyMode = 512
|
|
|
|
|
|
|
|
setSticky :: FilePath -> IO ()
|
|
|
|
setSticky f = modifyFileMode f $ addModes [stickyMode]
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2013-01-03 22:50:30 +00:00
|
|
|
|
|
|
|
{- Writes a file, ensuring that its modes do not allow it to be read
|
2013-05-03 03:34:58 +00:00
|
|
|
- by anyone other than the current user, before any content is written.
|
|
|
|
-
|
|
|
|
- On a filesystem that does not support file permissions, this is the same
|
|
|
|
- as writeFile.
|
|
|
|
-}
|
2013-01-03 22:50:30 +00:00
|
|
|
writeFileProtected :: FilePath -> String -> IO ()
|
|
|
|
writeFileProtected file content = do
|
|
|
|
h <- openFile file WriteMode
|
2013-05-03 03:34:58 +00:00
|
|
|
void $ tryIO $
|
|
|
|
modifyFileMode file $
|
|
|
|
removeModes [groupReadMode, otherReadMode]
|
2013-01-03 22:50:30 +00:00
|
|
|
hPutStr h content
|
|
|
|
hClose h
|