From 61ed50460724aa7149f52cf473b90579e8c6dd5b Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Fri, 1 Jul 2022 09:48:00 +0530 Subject: [PATCH] Add Chmod module --- src/Streamly/Coreutils/Chmod.hs | 69 +++++++++++++++++++++++++++++++++ streamly-coreutils.cabal | 1 + 2 files changed, 70 insertions(+) create mode 100644 src/Streamly/Coreutils/Chmod.hs diff --git a/src/Streamly/Coreutils/Chmod.hs b/src/Streamly/Coreutils/Chmod.hs new file mode 100644 index 00000000..509b56c3 --- /dev/null +++ b/src/Streamly/Coreutils/Chmod.hs @@ -0,0 +1,69 @@ +-- | +-- Module : Streamly.Coreutils.Chmod +-- Copyright : (c) 2022 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com +-- Stability : experimental +-- Portability : GHC +-- +-- change file mode bits. + +module Streamly.Coreutils.Chmod + ( chmod + ) +where + +import Data.Bits ((.|.), Bits ((.&.), complement)) +import Data.Default.Class (Default(..)) + +import qualified System.Posix as Posix + +data UserType = Owner | Group | Others deriving (Eq, Ord, Read, Show) + +data Permissions = Permissions + { readable :: Bool + , writable :: Bool + , executable :: Bool + } deriving (Eq, Ord, Read, Show) + +instance Default Permissions where + def = Permissions + { readable = False + , writable = False + , executable = False + } + +modifyBit :: Bool -> Posix.FileMode -> Posix.FileMode -> Posix.FileMode +modifyBit False b m = m .&. complement b +modifyBit True b m = m .|. b + +chmod :: UserType -> Permissions -> FilePath -> IO () +chmod utype (Permissions r w e) path = do + case utype of + Owner -> do + stat <- Posix.getFileStatus path + Posix.setFileMode + path + ( modifyBit e Posix.ownerExecuteMode + . modifyBit w Posix.ownerWriteMode + . modifyBit r Posix.ownerReadMode + . Posix.fileMode $ stat + ) + Group -> do + stat <- Posix.getFileStatus path + Posix.setFileMode + path + ( modifyBit e Posix.groupExecuteMode + . modifyBit w Posix.groupWriteMode + . modifyBit r Posix.groupReadMode + . Posix.fileMode $ stat + ) + Others -> do + stat <- Posix.getFileStatus path + Posix.setFileMode + path + ( modifyBit e Posix.otherExecuteMode + . modifyBit w Posix.otherWriteMode + . modifyBit r Posix.otherReadMode + . Posix.fileMode $ stat + ) diff --git a/streamly-coreutils.cabal b/streamly-coreutils.cabal index 2df63439..540afe60 100644 --- a/streamly-coreutils.cabal +++ b/streamly-coreutils.cabal @@ -108,6 +108,7 @@ library hs-source-dirs: src exposed-modules: Streamly.Coreutils + , Streamly.Coreutils.Chmod , Streamly.Coreutils.Common , Streamly.Coreutils.Cp , Streamly.Coreutils.Directory