Thursday 2 June 2016

Haskell: Work with file permissions

Haskell System.Directory module Provides number of functions to work with file/directory permissions. Following table summarizes the functions.

Function
Signature
Description
emptyPermissions
emptyPermissions :: Permissions
Return Permissions instance by clearing all.

Prelude System.Directory> emptyPermissions
Permissions {readable = False, writable = False, executable = False, searchable = False}
readable
readable :: Permissions -> Bool
Return True, if the Permissions instance has the property readable = True, else False.
writable
writable :: Permissions -> Bool
Return True, if the Permissions instance has the property writable = True, else False.
executable
executable :: Permissions -> Bool
Return True, if the Permissions instance has the property executable = True, else False.
searchable
searchable :: Permissions -> Bool
Return True, if the Permissions instance has the property searchable = True, else False
setOwnerReadable
setOwnerReadable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the readable permission to the owner, else unset.
setOwnerWritable
setOwnerWritable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the writable permission to the owner, else unset.
setOwnerExecutable
setOwnerExecutable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the executable permission to the owner, else unset.
setOwnerSearchable
setOwnerSearchable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the searchable permission to the owner, else unset.
getPermissions
getPermissions :: FilePath -> IO Permissions
Get the permissions of a file (or) directory.
setPermissions
setPermissions :: FilePath -> Permissions -> IO ()
Set permissins to given file.
copyPermissions
copyPermissions :: FilePath -> FilePath -> IO ()
Copy permissions of one file to other.

PermissionUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter directory (or) file"
    file <- getLine

    permissions <- getPermissions file
    let isReadable = readable permissions
    let isWritable = writable permissions
    let isExecutable = executable permissions
    let isSearchable = searchable permissions

    putStrLn $ "permissions : " ++ (show permissions)
    putStrLn $ "isReadable : " ++ (show isReadable)
    putStrLn $ "isWritable : " ++ (show isWritable)
    putStrLn $ "isExecutable : " ++ (show isExecutable)
    putStrLn $ "isSearchable : " ++ (show isSearchable)

$ runghc PermissionUtil.hs 
Enter directory (or) file
/Users/harikrishna_gurram
permissions : Permissions {readable = True, writable = True, executable = False, searchable = True}
isReadable : True
isWritable : True
isExecutable : False
isSearchable : True
$ 
$ runghc PermissionUtil.hs 
Enter directory (or) file
/usr/bin/java
permissions : Permissions {readable = True, writable = False, executable = True, searchable = False}
isReadable : True
isWritable : False
isExecutable : True
isSearchable : False

Note
getPermissions, setPermissions functions may fail with following errors.

isPermissionError if the user is not permitted to access the permissions; or
isDoesNotExistError if the file or directory does not exist.




Previous                                                 Next                                                 Home

No comments:

Post a Comment