{-# LINE 1 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
{-# LINE 2 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 6 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
module System.Posix.Files.ByteString (
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 82 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setSymbolicLinkOwnerAndGroup,
{-# LINE 84 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setFileTimes, setFileTimesHiRes,
setFdTimesHiRes, setSymbolicLinkTimesHiRes,
touchFile, touchFd, touchSymbolicLink,
setFileSize, setFdSize,
PathVar(..), getPathVar, getFdPathVar,
) where
import System.Posix.Types
import System.Posix.Internals hiding (withFilePath, peekFilePathLen)
import Foreign
import Foreign.C hiding (
throwErrnoPath,
throwErrnoPathIf,
throwErrnoPathIf_,
throwErrnoPathIfNull,
throwErrnoPathIfMinus1,
throwErrnoPathIfMinus1_ )
import System.Posix.Files.Common
import System.Posix.ByteString.FilePath
import Data.Time.Clock.POSIX (POSIXTime)
setFileMode :: RawFilePath -> FileMode -> IO ()
setFileMode :: RawFilePath -> FileMode -> IO ()
setFileMode RawFilePath
name FileMode
m =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s -> do
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setFileMode" RawFilePath
name (CString -> FileMode -> IO CInt
c_chmod CString
s FileMode
m)
fileAccess :: RawFilePath -> Bool -> Bool -> Bool -> IO Bool
fileAccess :: RawFilePath -> Bool -> Bool -> Bool -> IO Bool
fileAccess RawFilePath
name Bool
readOK Bool
writeOK Bool
execOK = RawFilePath -> FileMode -> IO Bool
access RawFilePath
name FileMode
flags
where
flags :: FileMode
flags = FileMode
read_f FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
write_f FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
exec_f
read_f :: FileMode
read_f = if Bool
readOK then (FileMode
4) else FileMode
0
{-# LINE 141 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
write_f = if writeOK then (2) else 0
{-# LINE 142 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
exec_f = if execOK then (1) else 0
{-# LINE 143 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
fileExist :: RawFilePath -> IO Bool
fileExist :: RawFilePath -> IO Bool
fileExist RawFilePath
name =
RawFilePath -> (CString -> IO Bool) -> IO Bool
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO Bool) -> IO Bool)
-> (CString -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \CString
s -> do
CInt
r <- CString -> CInt -> IO CInt
c_access CString
s (CInt
0)
{-# LINE 151 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
if (r == 0)
then return True
else do err <- getErrno
if (err == eNOENT)
then return False
else throwErrnoPath "fileExist" name
access :: RawFilePath -> CMode -> IO Bool
access :: RawFilePath -> FileMode -> IO Bool
access RawFilePath
name FileMode
flags =
RawFilePath -> (CString -> IO Bool) -> IO Bool
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO Bool) -> IO Bool)
-> (CString -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \CString
s -> do
CInt
r <- CString -> CInt -> IO CInt
c_access CString
s (FileMode -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral FileMode
flags)
if (CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0)
then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else do Errno
err <- IO Errno
getErrno
if (Errno
err Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eACCES Bool -> Bool -> Bool
|| Errno
err Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eROFS Bool -> Bool -> Bool
|| Errno
err Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eTXTBSY Bool -> Bool -> Bool
||
Errno
err Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
ePERM)
then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else String -> RawFilePath -> IO Bool
forall a. String -> RawFilePath -> IO a
throwErrnoPath String
"fileAccess" RawFilePath
name
getFileStatus :: RawFilePath -> IO FileStatus
getFileStatus :: RawFilePath -> IO FileStatus
getFileStatus RawFilePath
path = do
ForeignPtr CStat
fp <- Int -> IO (ForeignPtr CStat)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
144)
{-# LINE 178 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
withForeignPtr fp $ \p ->
withFilePath path $ \s ->
throwErrnoPathIfMinus1Retry_ "getFileStatus" path (c_stat s p)
FileStatus -> IO FileStatus
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr CStat -> FileStatus
FileStatus ForeignPtr CStat
fp)
getSymbolicLinkStatus :: RawFilePath -> IO FileStatus
getSymbolicLinkStatus :: RawFilePath -> IO FileStatus
getSymbolicLinkStatus RawFilePath
path = do
ForeignPtr CStat
fp <- Int -> IO (ForeignPtr CStat)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
144)
{-# LINE 191 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
withForeignPtr fp $ \p ->
withFilePath path $ \s ->
throwErrnoPathIfMinus1_ "getSymbolicLinkStatus" path (c_lstat s p)
FileStatus -> IO FileStatus
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr CStat -> FileStatus
FileStatus ForeignPtr CStat
fp)
foreign import capi unsafe "HsUnix.h lstat"
c_lstat :: CString -> Ptr CStat -> IO CInt
createNamedPipe :: RawFilePath -> FileMode -> IO ()
createNamedPipe :: RawFilePath -> FileMode -> IO ()
createNamedPipe RawFilePath
name FileMode
mode = do
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createNamedPipe" RawFilePath
name (CString -> FileMode -> IO CInt
c_mkfifo CString
s FileMode
mode)
createDevice :: RawFilePath -> FileMode -> DeviceID -> IO ()
createDevice :: RawFilePath -> FileMode -> DeviceID -> IO ()
createDevice RawFilePath
path FileMode
mode DeviceID
dev =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
path ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createDevice" RawFilePath
path (CString -> FileMode -> DeviceID -> IO CInt
c_mknod CString
s FileMode
mode DeviceID
dev)
foreign import capi unsafe "HsUnix.h mknod"
c_mknod :: CString -> CMode -> CDev -> IO CInt
createLink :: RawFilePath -> RawFilePath -> IO ()
createLink :: RawFilePath -> RawFilePath -> IO ()
createLink RawFilePath
name1 RawFilePath
name2 =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name1 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name2 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createLink" RawFilePath
name1 (CString -> CString -> IO CInt
c_link CString
s1 CString
s2)
removeLink :: RawFilePath -> IO ()
removeLink :: RawFilePath -> IO ()
removeLink RawFilePath
name =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"removeLink" RawFilePath
name (CString -> IO CInt
c_unlink CString
s)
createSymbolicLink :: RawFilePath -> RawFilePath -> IO ()
createSymbolicLink :: RawFilePath -> RawFilePath -> IO ()
createSymbolicLink RawFilePath
file1 RawFilePath
file2 =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
file1 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
file2 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"createSymbolicLink" RawFilePath
file2 (CString -> CString -> IO CInt
c_symlink CString
s1 CString
s2)
foreign import ccall unsafe "symlink"
c_symlink :: CString -> CString -> IO CInt
{-# LINE 275 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
readSymbolicLink :: RawFilePath -> IO RawFilePath
readSymbolicLink :: RawFilePath -> IO RawFilePath
readSymbolicLink RawFilePath
file =
Int -> (CString -> IO RawFilePath) -> IO RawFilePath
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray0 (Int
4096) ((CString -> IO RawFilePath) -> IO RawFilePath)
-> (CString -> IO RawFilePath) -> IO RawFilePath
forall a b. (a -> b) -> a -> b
$ \CString
buf -> do
{-# LINE 282 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
withFilePath file $ \s -> do
len <- throwErrnoPathIfMinus1 "readSymbolicLink" file $
c_readlink s buf (4096)
{-# LINE 285 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
peekFilePathLen (buf,fromIntegral len)
foreign import ccall unsafe "readlink"
c_readlink :: CString -> CString -> CSize -> IO CInt
rename :: RawFilePath -> RawFilePath -> IO ()
rename :: RawFilePath -> RawFilePath -> IO ()
rename RawFilePath
name1 RawFilePath
name2 =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name1 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s1 ->
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name2 ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s2 ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"rename" RawFilePath
name1 (CString -> CString -> IO CInt
c_rename CString
s1 CString
s2)
foreign import ccall unsafe "rename"
c_rename :: CString -> CString -> IO CInt
setOwnerAndGroup :: RawFilePath -> UserID -> GroupID -> IO ()
setOwnerAndGroup :: RawFilePath -> UserID -> GroupID -> IO ()
setOwnerAndGroup RawFilePath
name UserID
uid GroupID
gid = do
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setOwnerAndGroup" RawFilePath
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 323 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setSymbolicLinkOwnerAndGroup :: RawFilePath -> UserID -> GroupID -> IO ()
setSymbolicLinkOwnerAndGroup :: RawFilePath -> UserID -> GroupID -> IO ()
setSymbolicLinkOwnerAndGroup RawFilePath
name UserID
uid GroupID
gid = do
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setSymbolicLinkOwnerAndGroup" RawFilePath
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 336 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setFileTimes :: RawFilePath -> EpochTime -> EpochTime -> IO ()
setFileTimes :: RawFilePath -> EpochTime -> EpochTime -> IO ()
setFileTimes RawFilePath
name EpochTime
atime EpochTime
mtime = do
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
Int -> (Ptr CUtimbuf -> IO ()) -> IO ()
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (Int
16) ((Ptr CUtimbuf -> IO ()) -> IO ())
-> (Ptr CUtimbuf -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CUtimbuf
p -> do
{-# LINE 348 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p atime
{-# LINE 349 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p mtime
{-# LINE 350 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
throwErrnoPathIfMinus1_ "setFileTimes" name (c_utime s p)
setFileTimesHiRes :: RawFilePath -> POSIXTime -> POSIXTime -> IO ()
{-# LINE 357 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setFileTimesHiRes name atime mtime =
withFilePath name $ \s ->
withArray [toCTimeSpec atime, toCTimeSpec mtime] $ \times ->
throwErrnoPathIfMinus1_ "setFileTimesHiRes" name $
c_utimensat (-100) s times 0
{-# LINE 362 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
{-# LINE 368 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setSymbolicLinkTimesHiRes :: RawFilePath -> POSIXTime -> POSIXTime -> IO ()
{-# LINE 376 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setSymbolicLinkTimesHiRes name atime mtime =
withFilePath name $ \s ->
withArray [toCTimeSpec atime, toCTimeSpec mtime] $ \times ->
throwErrnoPathIfMinus1_ "setSymbolicLinkTimesHiRes" name $
c_utimensat (-100) s times (256)
{-# LINE 381 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
{-# LINE 391 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
touchFile :: RawFilePath -> IO ()
touchFile :: RawFilePath -> IO ()
touchFile RawFilePath
name = do
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"touchFile" RawFilePath
name (CString -> Ptr CUtimbuf -> IO CInt
c_utime CString
s Ptr CUtimbuf
forall a. Ptr a
nullPtr)
touchSymbolicLink :: RawFilePath -> IO ()
{-# LINE 408 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
touchSymbolicLink name =
withFilePath name $ \s ->
throwErrnoPathIfMinus1_ "touchSymbolicLink" name (c_lutimes s nullPtr)
{-# LINE 415 "libraries/unix/System/Posix/Files/ByteString.hsc" #-}
setFileSize :: RawFilePath -> FileOffset -> IO ()
setFileSize :: RawFilePath -> FileOffset -> IO ()
setFileSize RawFilePath
file FileOffset
off =
RawFilePath -> (CString -> IO ()) -> IO ()
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
file ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> RawFilePath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1_ String
"setFileSize" RawFilePath
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 :: RawFilePath -> PathVar -> IO Limit
getPathVar :: RawFilePath -> PathVar -> IO Limit
getPathVar RawFilePath
name PathVar
v = do
RawFilePath -> (CString -> IO Limit) -> IO Limit
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO Limit) -> IO Limit)
-> (CString -> IO Limit) -> IO Limit
forall a b. (a -> b) -> a -> b
$ \ CString
nameP ->
String -> RawFilePath -> IO Limit -> IO Limit
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO a
throwErrnoPathIfMinus1 String
"getPathVar" RawFilePath
name (IO Limit -> IO Limit) -> IO Limit -> IO Limit
forall a b. (a -> b) -> a -> b
$
CString -> CInt -> IO Limit
c_pathconf CString
nameP (PathVar -> CInt
pathVarConst PathVar
v)
foreign import ccall unsafe "pathconf"
c_pathconf :: CString -> CInt -> IO CLong