{-# LINE 1 "System/Posix/Files/PosixString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
module System.Posix.Files.PosixString (
unionFileModes, intersectFileModes,
nullFileMode,
ownerReadMode, ownerWriteMode, ownerExecuteMode, ownerModes,
groupReadMode, groupWriteMode, groupExecuteMode, groupModes,
otherReadMode, otherWriteMode, otherExecuteMode, otherModes,
setUserIDMode, setGroupIDMode,
stdFileMode, accessModes,
fileTypeModes,
blockSpecialMode, characterSpecialMode, namedPipeMode, regularFileMode,
directoryMode, symbolicLinkMode, socketMode,
setFileMode, setFdMode, setFileCreationMask,
fileAccess, fileExist,
FileStatus,
getFileStatus, getFdStatus, getSymbolicLinkStatus,
deviceID, fileID, fileMode, linkCount, fileOwner, fileGroup,
specialDeviceID, fileSize, accessTime, modificationTime,
statusChangeTime,
accessTimeHiRes, modificationTimeHiRes, statusChangeTimeHiRes,
isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
isDirectory, isSymbolicLink, isSocket,
createNamedPipe,
createDevice,
createLink, removeLink,
createSymbolicLink, readSymbolicLink,
rename,
setOwnerAndGroup, setFdOwnerAndGroup,
{-# LINE 77 "System/Posix/Files/PosixString.hsc" #-}
setSymbolicLinkOwnerAndGroup,
{-# LINE 79 "System/Posix/Files/PosixString.hsc" #-}
setFileTimes, setFileTimesHiRes,
setSymbolicLinkTimesHiRes,
touchFile, touchFd, touchSymbolicLink,
setFileSize, setFdSize,
PathVar(..), getPathVar, getFdPathVar,
) where
import System.Posix.Types
import System.Posix.Internals hiding (withFilePath, peekFilePathLen)
import qualified System.Posix.Files.Common as Common
import Foreign
import Foreign.C hiding (
throwErrnoPath,
throwErrnoPathIf,
throwErrnoPathIf_,
throwErrnoPathIfNull,
throwErrnoPathIfMinus1,
throwErrnoPathIfMinus1_ )
import System.OsPath.Types
import System.Posix.Files hiding (getFileStatus, getSymbolicLinkStatus, createNamedPipe, createDevice, createLink, removeLink, createSymbolicLink, readSymbolicLink, rename, setOwnerAndGroup, setSymbolicLinkOwnerAndGroup, setFileTimes, setSymbolicLinkTimesHiRes, touchFile, touchSymbolicLink, setFileSize, getPathVar, setFileMode, fileAccess, fileExist, setFdTimesHiRes, setFileTimesHiRes)
import System.Posix.PosixPath.FilePath
import Data.Time.Clock.POSIX (POSIXTime)
{-# LINE 114 "System/Posix/Files/PosixString.hsc" #-}
setFileMode :: PosixPath -> FileMode -> IO ()
setFileMode :: PosixPath -> CMode -> IO ()
setFileMode PosixPath
name CMode
m =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s -> do
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setFileMode" PosixPath
name (CString -> CMode -> IO CInt
c_chmod CString
s CMode
m)
fileAccess :: PosixPath -> Bool -> Bool -> Bool -> IO Bool
fileAccess :: PosixPath -> Bool -> Bool -> Bool -> IO Bool
fileAccess PosixPath
name Bool
readOK Bool
writeOK Bool
execOK = PosixPath -> CMode -> IO Bool
access PosixPath
name CMode
flags
where
flags :: CMode
flags = CMode
read_f forall a. Bits a => a -> a -> a
.|. CMode
write_f forall a. Bits a => a -> a -> a
.|. CMode
exec_f
read_f :: CMode
read_f = if Bool
readOK then (CMode
4) else CMode
0
{-# LINE 143 "System/Posix/Files/PosixString.hsc" #-}
write_f = if writeOK then (2) else 0
{-# LINE 144 "System/Posix/Files/PosixString.hsc" #-}
exec_f = if execOK then (1) else 0
{-# LINE 145 "System/Posix/Files/PosixString.hsc" #-}
fileExist :: PosixPath -> IO Bool
fileExist :: PosixPath -> IO Bool
fileExist PosixPath
name =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s -> do
CInt
r <- CString -> CInt -> IO CInt
c_access CString
s (CInt
0)
{-# LINE 153 "System/Posix/Files/PosixString.hsc" #-}
if (r == 0)
then return True
else do err <- getErrno
if (err == eNOENT)
then return False
else throwErrnoPath "fileExist" name
access :: PosixPath -> CMode -> IO Bool
access :: PosixPath -> CMode -> IO Bool
access PosixPath
name CMode
flags =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s -> do
CInt
r <- CString -> CInt -> IO CInt
c_access CString
s (forall a b. (Integral a, Num b) => a -> b
fromIntegral CMode
flags)
if (CInt
r forall a. Eq a => a -> a -> Bool
== CInt
0)
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else do Errno
err <- IO Errno
getErrno
if (Errno
err forall a. Eq a => a -> a -> Bool
== Errno
eACCES Bool -> Bool -> Bool
|| Errno
err forall a. Eq a => a -> a -> Bool
== Errno
eROFS Bool -> Bool -> Bool
|| Errno
err forall a. Eq a => a -> a -> Bool
== Errno
eTXTBSY Bool -> Bool -> Bool
||
Errno
err forall a. Eq a => a -> a -> Bool
== Errno
ePERM)
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else forall a. String -> PosixPath -> IO a
throwErrnoPath String
"fileAccess" PosixPath
name
getFileStatus :: PosixPath -> IO FileStatus
getFileStatus :: PosixPath -> IO FileStatus
getFileStatus PosixPath
path = do
ForeignPtr CStat
fp <- forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
144)
{-# LINE 180 "System/Posix/Files/PosixString.hsc" #-}
withForeignPtr fp $ \p ->
withFilePath path $ \s ->
throwErrnoPathIfMinus1Retry_ "getFileStatus" path (c_stat s p)
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr CStat -> FileStatus
Common.FileStatus ForeignPtr CStat
fp)
getSymbolicLinkStatus :: PosixPath -> IO FileStatus
getSymbolicLinkStatus :: PosixPath -> IO FileStatus
getSymbolicLinkStatus PosixPath
path = do
ForeignPtr CStat
fp <- forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
144)
{-# LINE 193 "System/Posix/Files/PosixString.hsc" #-}
withForeignPtr fp $ \p ->
withFilePath path $ \s ->
throwErrnoPathIfMinus1_ "getSymbolicLinkStatus" path (c_lstat s p)
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr CStat -> FileStatus
Common.FileStatus ForeignPtr CStat
fp)
foreign import capi unsafe "HsUnix.h lstat"
c_lstat :: CString -> Ptr CStat -> IO CInt
createNamedPipe :: PosixPath -> FileMode -> IO ()
createNamedPipe :: PosixPath -> CMode -> IO ()
createNamedPipe PosixPath
name CMode
mode = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createNamedPipe" PosixPath
name (CString -> CMode -> IO CInt
c_mkfifo CString
s CMode
mode)
{-# LINE 220 "System/Posix/Files/PosixString.hsc" #-}
createDevice :: PosixPath -> FileMode -> DeviceID -> IO ()
createDevice :: PosixPath -> CMode -> DeviceID -> IO ()
createDevice PosixPath
path CMode
mode DeviceID
dev =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
path forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createDevice" PosixPath
path (CString -> CMode -> DeviceID -> IO CInt
c_mknod CString
s CMode
mode DeviceID
dev)
foreign import capi unsafe "HsUnix.h mknod"
c_mknod :: CString -> CMode -> CDev -> IO CInt
{-# LINE 237 "System/Posix/Files/PosixString.hsc" #-}
createLink :: PosixPath -> PosixPath -> IO ()
createLink :: PosixPath -> PosixPath -> IO ()
createLink PosixPath
name1 PosixPath
name2 =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name1 forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name2 forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
forall a.
(Eq a, Num a) =>
String -> PosixPath -> PosixPath -> IO a -> IO ()
throwErrnoTwoPathsIfMinus1_ String
"createLink" PosixPath
name1 PosixPath
name2 (CString -> CString -> IO CInt
c_link CString
s1 CString
s2)
removeLink :: PosixPath -> IO ()
removeLink :: PosixPath -> IO ()
removeLink PosixPath
name =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"removeLink" PosixPath
name (CString -> IO CInt
c_unlink CString
s)
createSymbolicLink :: PosixPath -> PosixPath -> IO ()
createSymbolicLink :: PosixPath -> PosixPath -> IO ()
createSymbolicLink PosixPath
name1 PosixPath
name2 =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name1 forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name2 forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
forall a.
(Eq a, Num a) =>
String -> PosixPath -> PosixPath -> IO a -> IO ()
throwErrnoTwoPathsIfMinus1_ String
"createSymbolicLink" PosixPath
name1 PosixPath
name2 (CString -> CString -> IO CInt
c_symlink CString
s1 CString
s2)
foreign import ccall unsafe "symlink"
c_symlink :: CString -> CString -> IO CInt
{-# LINE 286 "System/Posix/Files/PosixString.hsc" #-}
readSymbolicLink :: PosixPath -> IO PosixPath
readSymbolicLink :: PosixPath -> IO PosixPath
readSymbolicLink PosixPath
file =
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray0 (Int
4096) forall a b. (a -> b) -> a -> b
$ \CString
buf -> do
{-# LINE 293 "System/Posix/Files/PosixString.hsc" #-}
withFilePath file $ \s -> do
len <- throwErrnoPathIfMinus1 "readSymbolicLink" file $
c_readlink s buf (4096)
{-# LINE 296 "System/Posix/Files/PosixString.hsc" #-}
peekFilePathLen (buf,fromIntegral len)
foreign import ccall unsafe "readlink"
c_readlink :: CString -> CString -> CSize -> IO CInt
rename :: PosixPath -> PosixPath -> IO ()
rename :: PosixPath -> PosixPath -> IO ()
rename PosixPath
name1 PosixPath
name2 =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name1 forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name2 forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
forall a.
(Eq a, Num a) =>
String -> PosixPath -> PosixPath -> IO a -> IO ()
throwErrnoTwoPathsIfMinus1_ String
"rename" PosixPath
name1 PosixPath
name2 (CString -> CString -> IO CInt
c_rename CString
s1 CString
s2)
foreign import ccall unsafe "rename"
c_rename :: CString -> CString -> IO CInt
{-# LINE 320 "System/Posix/Files/PosixString.hsc" #-}
setOwnerAndGroup :: PosixPath -> UserID -> GroupID -> IO ()
setOwnerAndGroup :: PosixPath -> UserID -> GroupID -> IO ()
setOwnerAndGroup PosixPath
name UserID
uid GroupID
gid = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setOwnerAndGroup" PosixPath
name (CString -> UserID -> GroupID -> IO CInt
c_chown CString
s UserID
uid GroupID
gid)
foreign import ccall unsafe "chown"
c_chown :: CString -> CUid -> CGid -> IO CInt
{-# LINE 342 "System/Posix/Files/PosixString.hsc" #-}
{-# LINE 344 "System/Posix/Files/PosixString.hsc" #-}
setSymbolicLinkOwnerAndGroup :: PosixPath -> UserID -> GroupID -> IO ()
setSymbolicLinkOwnerAndGroup :: PosixPath -> UserID -> GroupID -> IO ()
setSymbolicLinkOwnerAndGroup PosixPath
name UserID
uid GroupID
gid = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setSymbolicLinkOwnerAndGroup" PosixPath
name
(CString -> UserID -> GroupID -> IO CInt
c_lchown CString
s UserID
uid GroupID
gid)
foreign import ccall unsafe "lchown"
c_lchown :: CString -> CUid -> CGid -> IO CInt
{-# LINE 357 "System/Posix/Files/PosixString.hsc" #-}
setFileTimes :: PosixPath -> EpochTime -> EpochTime -> IO ()
setFileTimes :: PosixPath -> EpochTime -> EpochTime -> IO ()
setFileTimes PosixPath
name EpochTime
atime EpochTime
mtime = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (Int
16) forall a b. (a -> b) -> a -> b
$ \Ptr CUtimbuf
p -> do
{-# LINE 369 "System/Posix/Files/PosixString.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p atime
{-# LINE 370 "System/Posix/Files/PosixString.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p mtime
{-# LINE 371 "System/Posix/Files/PosixString.hsc" #-}
throwErrnoPathIfMinus1_ "setFileTimes" name (c_utime s p)
setFileTimesHiRes :: PosixPath -> POSIXTime -> POSIXTime -> IO ()
{-# LINE 387 "System/Posix/Files/PosixString.hsc" #-}
setFileTimesHiRes :: PosixPath -> POSIXTime -> POSIXTime -> IO ()
setFileTimesHiRes PosixPath
name POSIXTime
atime POSIXTime
mtime =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [POSIXTime -> CTimeSpec
Common.toCTimeSpec POSIXTime
atime, POSIXTime -> CTimeSpec
Common.toCTimeSpec POSIXTime
mtime] forall a b. (a -> b) -> a -> b
$ \Ptr CTimeSpec
times ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setFileTimesHiRes" PosixPath
name forall a b. (a -> b) -> a -> b
$
CInt -> CString -> Ptr CTimeSpec -> CInt -> IO CInt
Common.c_utimensat (-CInt
100) CString
s Ptr CTimeSpec
times CInt
0
{-# LINE 392 "System/Posix/Files/PosixString.hsc" #-}
{-# LINE 398 "System/Posix/Files/PosixString.hsc" #-}
setSymbolicLinkTimesHiRes :: PosixPath -> POSIXTime -> POSIXTime -> IO ()
{-# LINE 410 "System/Posix/Files/PosixString.hsc" #-}
setSymbolicLinkTimesHiRes name atime mtime =
withFilePath name $ \s ->
withArray [Common.toCTimeSpec atime, Common.toCTimeSpec mtime] $ \times ->
throwErrnoPathIfMinus1_ "setSymbolicLinkTimesHiRes" name $
Common.c_utimensat (-100) s times (256)
{-# LINE 415 "System/Posix/Files/PosixString.hsc" #-}
{-# LINE 425 "System/Posix/Files/PosixString.hsc" #-}
touchFile :: PosixPath -> IO ()
touchFile :: PosixPath -> IO ()
touchFile PosixPath
name = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"touchFile" PosixPath
name (CString -> Ptr CUtimbuf -> IO CInt
c_utime CString
s forall a. Ptr a
nullPtr)
touchSymbolicLink :: PosixPath -> IO ()
{-# LINE 442 "System/Posix/Files/PosixString.hsc" #-}
touchSymbolicLink name =
withFilePath name $ \s ->
throwErrnoPathIfMinus1_ "touchSymbolicLink" name (Common.c_lutimes s nullPtr)
{-# LINE 449 "System/Posix/Files/PosixString.hsc" #-}
setFileSize :: PosixPath -> FileOffset -> IO ()
setFileSize :: PosixPath -> FileOffset -> IO ()
setFileSize PosixPath
file FileOffset
off =
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
file forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setFileSize" PosixPath
file (CString -> FileOffset -> IO CInt
c_truncate CString
s FileOffset
off)
foreign import capi unsafe "HsUnix.h truncate"
c_truncate :: CString -> COff -> IO CInt
getPathVar :: PosixPath -> PathVar -> IO Limit
getPathVar :: PosixPath -> PathVar -> IO CLong
getPathVar PosixPath
name PathVar
v = do
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name forall a b. (a -> b) -> a -> b
$ \ CString
nameP ->
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO a
throwErrnoPathIfMinus1 String
"getPathVar" PosixPath
name forall a b. (a -> b) -> a -> b
$
CString -> CInt -> IO CLong
c_pathconf CString
nameP (PathVar -> CInt
Common.pathVarConst PathVar
v)
foreign import ccall unsafe "pathconf"
c_pathconf :: CString -> CInt -> IO CLong