summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-09-23 18:13:24 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-09-23 18:13:24 -0400
commit4bf1a5ef59026a095abf751ea60b586c299aa0b9 (patch)
tree067cd0813f3f315475b3b958762ed569ae95e8de /Utility
parentd75da353b9905bb5757df08520e63607fbfd2073 (diff)
refactor
Diffstat (limited to 'Utility')
-rw-r--r--Utility/FileMode.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/Utility/FileMode.hs b/Utility/FileMode.hs
new file mode 100644
index 000000000..f5b018c84
--- /dev/null
+++ b/Utility/FileMode.hs
@@ -0,0 +1,32 @@
+{- File mode utilities.
+ -
+ - Copyright 2010 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Utility.FileMode where
+
+import System.Posix.Files
+import System.Posix.Types
+import Foreign (complement)
+
+{- Removes a FileMode from a file.
+ - For example, call with otherWriteMode to chmod o-w -}
+unsetFileMode :: FilePath -> FileMode -> IO ()
+unsetFileMode f m = do
+ s <- getFileStatus f
+ setFileMode f $ fileMode s `intersectFileModes` complement m
+
+{- Removes the write bits from a file. -}
+preventWrite :: FilePath -> IO ()
+preventWrite f = unsetFileMode f writebits
+ where
+ writebits = foldl unionFileModes ownerWriteMode
+ [groupWriteMode, otherWriteMode]
+
+{- Turns a file's write bit back on. -}
+allowWrite :: FilePath -> IO ()
+allowWrite f = do
+ s <- getFileStatus f
+ setFileMode f $ fileMode s `unionFileModes` ownerWriteMode