Skip to content

Commit

Permalink
Add Chmod module
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed Jul 27, 2022
1 parent 55ae2ff commit 61ed504
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Streamly/Coreutils/Chmod.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- |
-- Module : Streamly.Coreutils.Chmod
-- Copyright : (c) 2022 Composewell Technologies
-- License : BSD-3-Clause
-- Maintainer : [email protected]
-- 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
)
1 change: 1 addition & 0 deletions streamly-coreutils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61ed504

Please sign in to comment.