diff --git a/src/Streamly/Coreutils/Chown.hs b/src/Streamly/Coreutils/Chown.hs new file mode 100644 index 0000000..835296a --- /dev/null +++ b/src/Streamly/Coreutils/Chown.hs @@ -0,0 +1,38 @@ +-- | +-- Module : Streamly.Coreutils.Ls +-- Copyright : (c) 2022 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com +-- Stability : experimental +-- Portability : GHC +-- +-- Change file owner and group. + +module Streamly.Coreutils.Chown + ( + chown + + -- * Options + , Chown + , followLinks + ) where + +import qualified System.Posix.Files as Posix +import System.Posix.Types ( CUid (CUid), CGid (CGid) ) +import GHC.Word (Word32) +import Streamly.Coreutils.Common (Switch(..)) + +newtype Chown = Chown{deRef :: Switch} + +defaultConfig :: Chown +defaultConfig = Chown Off + +followLinks :: Switch -> Chown -> Chown +followLinks opt cfg = cfg {deRef = opt} + +chown :: (Chown -> Chown) -> FilePath -> Word32 -> Word32 -> IO () +chown f path uid gid = do + let opt = f defaultConfig + case deRef opt of + Off -> Posix.setSymbolicLinkOwnerAndGroup path (CUid uid) (CGid gid) + On -> Posix.setOwnerAndGroup path (CUid uid) (CGid gid) diff --git a/streamly-coreutils.cabal b/streamly-coreutils.cabal index fa31b39..32c1d9c 100644 --- a/streamly-coreutils.cabal +++ b/streamly-coreutils.cabal @@ -95,6 +95,7 @@ library hs-source-dirs: src exposed-modules: Streamly.Coreutils + , Streamly.Coreutils.Chown , Streamly.Coreutils.Common , Streamly.Coreutils.Cp , Streamly.Coreutils.FileTest