module Control.Shell.Posix
(
User, Group
, getCurrentUser, Control.Shell.Posix.getCurrentGroups
, getOwner, getGroup, getOwnership
, setOwner, setGroup, setOwnership
, CMode
, unionFileModes, intersectFileModes
, nullFileMode
, ownerReadMode, ownerWriteMode, ownerExecuteMode, ownerModes
, groupReadMode, groupWriteMode, groupExecuteMode, groupModes
, otherReadMode, otherWriteMode, otherExecuteMode, otherModes
, stdFileMode, accessModes
, Control.Shell.Posix.setFileMode, getFileMode
, Control.Shell.Posix.setFileCreationMask
, daemonize
) where
import System.Posix as Posix
import Control.Shell
import Control.Shell.Daemon
type User = String
type Group = String
setFileMode :: FilePath -> CMode -> Shell ()
setFileMode f = liftIO . Posix.setFileMode f
getFileMode :: FilePath -> Shell CMode
getFileMode = liftIO . fmap fileMode . getFileStatus
getCurrentUser :: Shell String
getCurrentUser = liftIO getEffectiveUserName
getCurrentGroups :: Shell [String]
getCurrentGroups = liftIO $ do
gids <- Posix.getGroups
mapM (fmap groupName . getGroupEntryForID) gids
setOwner :: User -> FilePath -> Shell ()
setOwner name file = liftIO $ do
uid <- userID <$> getUserEntryForName name
setOwnerAndGroup file uid (-1)
setGroup :: Group -> FilePath -> Shell ()
setGroup group file = liftIO $ do
gid <- groupID <$> getGroupEntryForName group
setOwnerAndGroup file (-1) gid
setOwnership :: User -> Group -> FilePath -> Shell ()
setOwnership name group file = liftIO $ do
uid <- userID <$> getUserEntryForName name
gid <- groupID <$> getGroupEntryForName group
setOwnerAndGroup file uid gid
getOwner :: FilePath -> Shell User
getOwner file = liftIO $ do
uid <- fileOwner <$> getFileStatus file
userName <$> getUserEntryForID uid
getGroup :: FilePath -> Shell Group
getGroup file = liftIO $ do
gid <- fileGroup <$> getFileStatus file
groupName <$> getGroupEntryForID gid
getOwnership :: FilePath -> Shell (User, Group)
getOwnership file = liftIO $ do
st <- getFileStatus file
user <- userName <$> getUserEntryForID (fileOwner st)
group <- groupName <$> getGroupEntryForID (fileGroup st)
return (user, group)
setFileCreationMask :: CMode -> Shell CMode
setFileCreationMask = unsafeLiftIO . Posix.setFileCreationMask