use unix-compat, removed a lot of stubs in Utility.FileMode

This commit is contained in:
Joey Hess 2013-05-11 11:16:47 -05:00
parent 763cbda14f
commit 3a7eb68c1a
2 changed files with 11 additions and 56 deletions

View file

@ -13,13 +13,13 @@ import Common
import Control.Exception (bracket) import Control.Exception (bracket)
import Utility.Exception import Utility.Exception
import System.Posix.Types import System.PosixCompat.Types
import System.PosixCompat.Files
import Foreign (complement) import Foreign (complement)
{- 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 = void $ modifyFileMode' f convert modifyFileMode f convert = void $ modifyFileMode' f convert
#ifndef __WINDOWS__
modifyFileMode' :: FilePath -> (FileMode -> FileMode) -> IO FileMode modifyFileMode' :: FilePath -> (FileMode -> FileMode) -> IO FileMode
modifyFileMode' f convert = do modifyFileMode' f convert = do
s <- getFileStatus f s <- getFileStatus f
@ -28,9 +28,6 @@ modifyFileMode' f convert = do
when (new /= old) $ when (new /= old) $
setFileMode f new setFileMode f new
return old return old
#else
modifyFileMode' = error "modifyFileMode' TODO"
#endif
{- Adds the specified FileModes to the input mode, leaving the rest {- Adds the specified FileModes to the input mode, leaving the rest
- unchanged. -} - unchanged. -}
@ -39,11 +36,7 @@ addModes ms m = combineModes (m:ms)
{- Removes the specified FileModes from the input mode. -} {- Removes the specified FileModes from the input mode. -}
removeModes :: [FileMode] -> FileMode -> FileMode removeModes :: [FileMode] -> FileMode -> FileMode
#ifndef __WINDOWS__
removeModes ms m = m `intersectFileModes` complement (combineModes ms) removeModes ms m = m `intersectFileModes` complement (combineModes ms)
#else
removeModes = error "removeModes TODO"
#endif
{- 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
@ -54,73 +47,43 @@ withModifiedFileMode file convert a = bracket setup cleanup go
go _ = a go _ = a
writeModes :: [FileMode] writeModes :: [FileMode]
#ifndef __WINDOWS__
writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode] writeModes = [ownerWriteMode, groupWriteMode, otherWriteMode]
#else
writeModes = []
#endif
readModes :: [FileMode] readModes :: [FileMode]
#ifndef __WINDOWS__
readModes = [ownerReadMode, groupReadMode, otherReadMode] readModes = [ownerReadMode, groupReadMode, otherReadMode]
#else
readModes = []
#endif
executeModes :: [FileMode] executeModes :: [FileMode]
#ifndef __WINDOWS__
executeModes = [ownerExecuteMode, groupExecuteMode, otherExecuteMode] executeModes = [ownerExecuteMode, groupExecuteMode, otherExecuteMode]
#else
executeModes = []
#endif
{- Removes the write bits from a file. -} {- Removes the write bits from a file. -}
preventWrite :: FilePath -> IO () preventWrite :: FilePath -> IO ()
#ifndef __WINDOWS__
preventWrite f = modifyFileMode f $ removeModes writeModes preventWrite f = modifyFileMode f $ removeModes writeModes
#else
preventWrite _ = return ()
#endif
{- Turns a file's owner write bit back on. -} {- Turns a file's owner write bit back on. -}
allowWrite :: FilePath -> IO () allowWrite :: FilePath -> IO ()
#ifndef __WINDOWS__
allowWrite f = modifyFileMode f $ addModes [ownerWriteMode] allowWrite f = modifyFileMode f $ addModes [ownerWriteMode]
#else
allowWrite _ = return ()
#endif
{- 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 ()
#ifndef __WINDOWS__
groupWriteRead f = modifyFileMode f $ addModes groupWriteRead f = modifyFileMode f $ addModes
[ ownerWriteMode, groupWriteMode [ ownerWriteMode, groupWriteMode
, ownerReadMode, groupReadMode , ownerReadMode, groupReadMode
] ]
#else
groupWriteRead _ = return ()
#endif
#ifndef __WINDOWS__
checkMode :: FileMode -> FileMode -> Bool checkMode :: FileMode -> FileMode -> Bool
checkMode checkfor mode = checkfor `intersectFileModes` mode == checkfor checkMode checkfor mode = checkfor `intersectFileModes` mode == checkfor
#endif
{- 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
#ifndef __WINDOWS__ #ifdef __WINDOWS__
isSymLink = checkMode symbolicLinkMode
#else
isSymLink _ = False isSymLink _ = False
#else
isSymLink = checkMode symbolicLinkMode
#endif #endif
{- Checks if a file has any executable bits set. -} {- Checks if a file has any executable bits set. -}
isExecutable :: FileMode -> Bool isExecutable :: FileMode -> Bool
#ifndef __WINDOWS__
isExecutable mode = combineModes executeModes `intersectFileModes` mode /= 0 isExecutable mode = combineModes executeModes `intersectFileModes` mode /= 0
#else
isExecutable _ = False
#endif
{- Runs an action without that pesky umask influencing it, unless the {- Runs an action without that pesky umask influencing it, unless the
- passed FileMode is the standard one. -} - passed FileMode is the standard one. -}
@ -138,22 +101,18 @@ noUmask _ a = a
#endif #endif
combineModes :: [FileMode] -> FileMode combineModes :: [FileMode] -> FileMode
#ifndef __WINDOWS__
combineModes [] = undefined combineModes [] = undefined
combineModes [m] = m combineModes [m] = m
combineModes (m:ms) = foldl unionFileModes m ms combineModes (m:ms) = foldl unionFileModes m ms
isSticky :: FileMode -> Bool
#ifdef __WINDOWS__
isSticky _ = False
#else #else
combineModes _ = error "combineModes TODO" isSticky = checkMode stickyMode
#endif
stickyMode :: FileMode stickyMode :: FileMode
stickyMode = 512 stickyMode = 512
isSticky :: FileMode -> Bool
#ifndef __WINDOWS__
isSticky = checkMode stickyMode
#else
isSticky _ = False
#endif #endif
setSticky :: FilePath -> IO () setSticky :: FilePath -> IO ()
@ -166,7 +125,6 @@ setSticky f = modifyFileMode f $ addModes [stickyMode]
- as writeFile. - as writeFile.
-} -}
writeFileProtected :: FilePath -> String -> IO () writeFileProtected :: FilePath -> String -> IO ()
#ifndef __WINDOWS__
writeFileProtected file content = do writeFileProtected file content = do
h <- openFile file WriteMode h <- openFile file WriteMode
void $ tryIO $ void $ tryIO $
@ -174,6 +132,3 @@ writeFileProtected file content = do
removeModes [groupReadMode, otherReadMode] removeModes [groupReadMode, otherReadMode]
hPutStr h content hPutStr h content
hClose h hClose h
#else
writeFileProtected = writeFile
#endif

View file

@ -73,7 +73,7 @@ Executable git-annex
extensible-exceptions, dataenc, SHA, process, json, extensible-exceptions, dataenc, SHA, process, json,
base (>= 4.5 && < 4.8), monad-control, transformers-base, lifted-base, base (>= 4.5 && < 4.8), monad-control, transformers-base, lifted-base,
IfElse, text, QuickCheck >= 2.1, bloomfilter, edit-distance, process, IfElse, text, QuickCheck >= 2.1, bloomfilter, edit-distance, process,
SafeSemaphore, uuid, random, dlist SafeSemaphore, uuid, random, dlist, unix-compat
-- Need to list these because they're generated from .hsc files. -- Need to list these because they're generated from .hsc files.
Other-Modules: Utility.Touch Utility.Mounts Other-Modules: Utility.Touch Utility.Mounts
Include-Dirs: Utility Include-Dirs: Utility