diff options
author | Joey Hess <joeyh@joeyh.name> | 2015-04-28 14:58:29 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2015-04-28 14:58:29 -0400 |
commit | 62d9d780208c69b38d90ce160478e29472e5a24d (patch) | |
tree | 015d66a93bd92ea518bbe3e3c5f3498df034ce15 /Utility | |
parent | bad1f3faa36fd29c6baa66f0cfd57b1c8b6fdb22 (diff) |
merge generalization of writeFileProtected from propellor
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/FileMode.hs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Utility/FileMode.hs b/Utility/FileMode.hs index f98e1bc87..fd0fe07b5 100644 --- a/Utility/FileMode.hs +++ b/Utility/FileMode.hs @@ -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 |