merge generalization of writeFileProtected from propellor

This commit is contained in:
Joey Hess 2015-04-28 14:58:29 -04:00
parent 986e245ac1
commit d40dea58e3

View file

@ -11,6 +11,7 @@ module Utility.FileMode where
import System.IO
import Control.Monad
import Control.Exception (bracket)
import System.PosixCompat.Types
import Utility.PosixFiles
#ifndef mingw32_HOST_OS
@ -124,7 +125,7 @@ withUmask _ a = a
#endif
combineModes :: [FileMode] -> FileMode
combineModes [] = 0
combineModes [] = undefined
combineModes [m] = m
combineModes (m:ms) = foldl unionFileModes m ms
@ -151,7 +152,11 @@ setSticky f = modifyFileMode f $ addModes [stickyMode]
- as writeFile.
-}
writeFileProtected :: FilePath -> String -> IO ()
writeFileProtected file content = withUmask 0o0077 $
writeFileProtected file content = writeFileProtected' file
(\h -> hPutStr h content)
writeFileProtected' :: FilePath -> (Handle -> IO ()) -> IO ()
writeFileProtected' file writer = withUmask 0o0077 $
withFile file WriteMode $ \h -> do
void $ tryIO $ modifyFileMode file $ removeModes otherGroupModes
hPutStr h content
writer h