{-# LINE 1 "System/Posix/Files/Common.hsc" #-}
{-# LANGUAGE Trustworthy #-}
module System.Posix.Files.Common (
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,
setFdMode, setFileCreationMask,
FileStatus(..),
getFdStatus,
deviceID, fileID, fileMode, linkCount, fileOwner, fileGroup,
specialDeviceID, fileSize, accessTime, modificationTime,
statusChangeTime,
accessTimeHiRes, modificationTimeHiRes, statusChangeTimeHiRes,
setFdTimesHiRes, touchFd,
isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
isDirectory, isSymbolicLink, isSocket,
setFdSize,
setFdOwnerAndGroup,
PathVar(..), getFdPathVar, pathVarConst,
{-# LINE 70 "System/Posix/Files/Common.hsc" #-}
CTimeSpec(..),
toCTimeSpec,
c_utimensat,
{-# LINE 74 "System/Posix/Files/Common.hsc" #-}
CTimeVal(..),
toCTimeVal,
c_utimes,
{-# LINE 78 "System/Posix/Files/Common.hsc" #-}
c_lutimes,
{-# LINE 80 "System/Posix/Files/Common.hsc" #-}
) where
import System.Posix.Types
import System.IO.Unsafe
import Data.Bits
import Data.Int
import Data.Ratio
import Data.Time.Clock.POSIX (POSIXTime)
import System.Posix.Internals
import Foreign.C
import Foreign.ForeignPtr
{-# LINE 92 "System/Posix/Files/Common.hsc" #-}
import Foreign.Marshal (withArray)
{-# LINE 94 "System/Posix/Files/Common.hsc" #-}
import Foreign.Ptr
import Foreign.Storable
nullFileMode :: FileMode
nullFileMode = 0
ownerReadMode :: FileMode
ownerReadMode = (256)
{-# LINE 110 "System/Posix/Files/Common.hsc" #-}
ownerWriteMode :: FileMode
ownerWriteMode = (128)
{-# LINE 114 "System/Posix/Files/Common.hsc" #-}
ownerExecuteMode :: FileMode
ownerExecuteMode = (64)
{-# LINE 118 "System/Posix/Files/Common.hsc" #-}
groupReadMode :: FileMode
groupReadMode = (32)
{-# LINE 122 "System/Posix/Files/Common.hsc" #-}
groupWriteMode :: FileMode
groupWriteMode = (16)
{-# LINE 126 "System/Posix/Files/Common.hsc" #-}
groupExecuteMode :: FileMode
groupExecuteMode = (8)
{-# LINE 130 "System/Posix/Files/Common.hsc" #-}
otherReadMode :: FileMode
otherReadMode = (4)
{-# LINE 134 "System/Posix/Files/Common.hsc" #-}
otherWriteMode :: FileMode
otherWriteMode = (2)
{-# LINE 138 "System/Posix/Files/Common.hsc" #-}
otherExecuteMode :: FileMode
otherExecuteMode = (1)
{-# LINE 142 "System/Posix/Files/Common.hsc" #-}
setUserIDMode :: FileMode
setUserIDMode = (2048)
{-# LINE 146 "System/Posix/Files/Common.hsc" #-}
setGroupIDMode :: FileMode
setGroupIDMode = (1024)
{-# LINE 150 "System/Posix/Files/Common.hsc" #-}
stdFileMode :: FileMode
stdFileMode = ownerReadMode .|. ownerWriteMode .|.
groupReadMode .|. groupWriteMode .|.
otherReadMode .|. otherWriteMode
ownerModes :: FileMode
ownerModes = (448)
{-# LINE 160 "System/Posix/Files/Common.hsc" #-}
groupModes :: FileMode
groupModes = (56)
{-# LINE 164 "System/Posix/Files/Common.hsc" #-}
otherModes :: FileMode
otherModes = (7)
{-# LINE 168 "System/Posix/Files/Common.hsc" #-}
accessModes :: FileMode
accessModes = ownerModes .|. groupModes .|. otherModes
unionFileModes :: FileMode -> FileMode -> FileMode
unionFileModes m1 m2 = m1 .|. m2
intersectFileModes :: FileMode -> FileMode -> FileMode
intersectFileModes m1 m2 = m1 .&. m2
fileTypeModes :: FileMode
fileTypeModes = (61440)
{-# LINE 185 "System/Posix/Files/Common.hsc" #-}
blockSpecialMode :: FileMode
blockSpecialMode = (24576)
{-# LINE 188 "System/Posix/Files/Common.hsc" #-}
characterSpecialMode :: FileMode
characterSpecialMode = (8192)
{-# LINE 191 "System/Posix/Files/Common.hsc" #-}
namedPipeMode :: FileMode
namedPipeMode = (4096)
{-# LINE 194 "System/Posix/Files/Common.hsc" #-}
regularFileMode :: FileMode
regularFileMode = (32768)
{-# LINE 197 "System/Posix/Files/Common.hsc" #-}
directoryMode :: FileMode
directoryMode = (16384)
{-# LINE 200 "System/Posix/Files/Common.hsc" #-}
symbolicLinkMode :: FileMode
symbolicLinkMode = (40960)
{-# LINE 203 "System/Posix/Files/Common.hsc" #-}
socketMode :: FileMode
socketMode = (49152)
{-# LINE 206 "System/Posix/Files/Common.hsc" #-}
setFdMode :: Fd -> FileMode -> IO ()
setFdMode (Fd fd) m =
throwErrnoIfMinus1_ "setFdMode" (c_fchmod fd m)
foreign import ccall unsafe "fchmod"
c_fchmod :: CInt -> CMode -> IO CInt
setFileCreationMask :: FileMode -> IO FileMode
setFileCreationMask mask = c_umask mask
newtype FileStatus = FileStatus (ForeignPtr CStat)
deviceID :: FileStatus -> DeviceID
fileID :: FileStatus -> FileID
fileMode :: FileStatus -> FileMode
linkCount :: FileStatus -> LinkCount
fileOwner :: FileStatus -> UserID
fileGroup :: FileStatus -> GroupID
specialDeviceID :: FileStatus -> DeviceID
fileSize :: FileStatus -> FileOffset
accessTime :: FileStatus -> EpochTime
accessTimeHiRes :: FileStatus -> POSIXTime
modificationTime :: FileStatus -> EpochTime
modificationTimeHiRes :: FileStatus -> POSIXTime
statusChangeTime :: FileStatus -> EpochTime
statusChangeTimeHiRes :: FileStatus -> POSIXTime
deviceID (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 0))
{-# LINE 268 "System/Posix/Files/Common.hsc" #-}
fileID (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 8))
{-# LINE 270 "System/Posix/Files/Common.hsc" #-}
fileMode (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 24))
{-# LINE 272 "System/Posix/Files/Common.hsc" #-}
linkCount (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 16))
{-# LINE 274 "System/Posix/Files/Common.hsc" #-}
fileOwner (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 28))
{-# LINE 276 "System/Posix/Files/Common.hsc" #-}
fileGroup (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 32))
{-# LINE 278 "System/Posix/Files/Common.hsc" #-}
specialDeviceID (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 40))
{-# LINE 280 "System/Posix/Files/Common.hsc" #-}
fileSize (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 48))
{-# LINE 282 "System/Posix/Files/Common.hsc" #-}
accessTime (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 72))
{-# LINE 284 "System/Posix/Files/Common.hsc" #-}
modificationTime (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 88))
{-# LINE 286 "System/Posix/Files/Common.hsc" #-}
statusChangeTime (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 104))
{-# LINE 288 "System/Posix/Files/Common.hsc" #-}
accessTimeHiRes (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ \stat_ptr -> do
sec <- ((\hsc_ptr -> peekByteOff hsc_ptr 72)) stat_ptr :: IO EpochTime
{-# LINE 292 "System/Posix/Files/Common.hsc" #-}
{-# LINE 293 "System/Posix/Files/Common.hsc" #-}
nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 80)) stat_ptr :: IO (Int64)
{-# LINE 294 "System/Posix/Files/Common.hsc" #-}
let frac = toInteger nsec % 10^(9::Int)
{-# LINE 310 "System/Posix/Files/Common.hsc" #-}
return $ fromRational $ toRational sec + frac
modificationTimeHiRes (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ \stat_ptr -> do
sec <- ((\hsc_ptr -> peekByteOff hsc_ptr 88)) stat_ptr :: IO EpochTime
{-# LINE 315 "System/Posix/Files/Common.hsc" #-}
{-# LINE 316 "System/Posix/Files/Common.hsc" #-}
nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 96)) stat_ptr :: IO (Int64)
{-# LINE 317 "System/Posix/Files/Common.hsc" #-}
let frac = toInteger nsec % 10^(9::Int)
{-# LINE 333 "System/Posix/Files/Common.hsc" #-}
return $ fromRational $ toRational sec + frac
statusChangeTimeHiRes (FileStatus stat) =
unsafePerformIO $ withForeignPtr stat $ \stat_ptr -> do
sec <- ((\hsc_ptr -> peekByteOff hsc_ptr 104)) stat_ptr :: IO EpochTime
{-# LINE 338 "System/Posix/Files/Common.hsc" #-}
{-# LINE 339 "System/Posix/Files/Common.hsc" #-}
nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 112)) stat_ptr :: IO (Int64)
{-# LINE 340 "System/Posix/Files/Common.hsc" #-}
let frac = toInteger nsec % 10^(9::Int)
{-# LINE 356 "System/Posix/Files/Common.hsc" #-}
return $ fromRational $ toRational sec + frac
isBlockDevice :: FileStatus -> Bool
isCharacterDevice :: FileStatus -> Bool
isNamedPipe :: FileStatus -> Bool
isRegularFile :: FileStatus -> Bool
isDirectory :: FileStatus -> Bool
isSymbolicLink :: FileStatus -> Bool
isSocket :: FileStatus -> Bool
isBlockDevice stat =
(fileMode stat `intersectFileModes` fileTypeModes) == blockSpecialMode
isCharacterDevice stat =
(fileMode stat `intersectFileModes` fileTypeModes) == characterSpecialMode
isNamedPipe stat =
(fileMode stat `intersectFileModes` fileTypeModes) == namedPipeMode
isRegularFile stat =
(fileMode stat `intersectFileModes` fileTypeModes) == regularFileMode
isDirectory stat =
(fileMode stat `intersectFileModes` fileTypeModes) == directoryMode
isSymbolicLink stat =
(fileMode stat `intersectFileModes` fileTypeModes) == symbolicLinkMode
isSocket stat =
(fileMode stat `intersectFileModes` fileTypeModes) == socketMode
getFdStatus :: Fd -> IO FileStatus
getFdStatus (Fd fd) = do
fp <- mallocForeignPtrBytes (144)
{-# LINE 394 "System/Posix/Files/Common.hsc" #-}
withForeignPtr fp $ \p ->
throwErrnoIfMinus1_ "getFdStatus" (c_fstat fd p)
return (FileStatus fp)
{-# LINE 402 "System/Posix/Files/Common.hsc" #-}
data CTimeSpec = CTimeSpec EpochTime CLong
instance Storable CTimeSpec where
sizeOf _ = (16)
{-# LINE 406 "System/Posix/Files/Common.hsc" #-}
alignment _ = alignment (undefined :: CInt)
poke p (CTimeSpec sec nsec) = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p sec
{-# LINE 409 "System/Posix/Files/Common.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p nsec
{-# LINE 410 "System/Posix/Files/Common.hsc" #-}
peek p = do
sec <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 412 "System/Posix/Files/Common.hsc" #-}
nsec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 413 "System/Posix/Files/Common.hsc" #-}
return $ CTimeSpec sec nsec
toCTimeSpec :: POSIXTime -> CTimeSpec
toCTimeSpec t = CTimeSpec (CTime sec) (truncate $ 10^(9::Int) * frac)
where
(sec, frac) = if (frac' < 0) then (sec' - 1, frac' + 1) else (sec', frac')
(sec', frac') = properFraction $ toRational t
{-# LINE 421 "System/Posix/Files/Common.hsc" #-}
{-# LINE 423 "System/Posix/Files/Common.hsc" #-}
foreign import ccall unsafe "utimensat"
c_utimensat :: CInt -> CString -> Ptr CTimeSpec -> CInt -> IO CInt
{-# LINE 426 "System/Posix/Files/Common.hsc" #-}
{-# LINE 428 "System/Posix/Files/Common.hsc" #-}
foreign import ccall unsafe "futimens"
c_futimens :: CInt -> Ptr CTimeSpec -> IO CInt
{-# LINE 431 "System/Posix/Files/Common.hsc" #-}
data CTimeVal = CTimeVal CLong CLong
instance Storable CTimeVal where
sizeOf _ = (16)
{-# LINE 436 "System/Posix/Files/Common.hsc" #-}
alignment _ = alignment (undefined :: CInt)
poke p (CTimeVal sec usec) = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p sec
{-# LINE 439 "System/Posix/Files/Common.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p usec
{-# LINE 440 "System/Posix/Files/Common.hsc" #-}
peek p = do
sec <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 442 "System/Posix/Files/Common.hsc" #-}
usec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 443 "System/Posix/Files/Common.hsc" #-}
return $ CTimeVal sec usec
toCTimeVal :: POSIXTime -> CTimeVal
toCTimeVal t = CTimeVal sec (truncate $ 10^(6::Int) * frac)
where
(sec, frac) = if (frac' < 0) then (sec' - 1, frac' + 1) else (sec', frac')
(sec', frac') = properFraction $ toRational t
foreign import ccall unsafe "utimes"
c_utimes :: CString -> Ptr CTimeVal -> IO CInt
{-# LINE 455 "System/Posix/Files/Common.hsc" #-}
foreign import ccall unsafe "lutimes"
c_lutimes :: CString -> Ptr CTimeVal -> IO CInt
{-# LINE 458 "System/Posix/Files/Common.hsc" #-}
{-# LINE 460 "System/Posix/Files/Common.hsc" #-}
foreign import ccall unsafe "futimes"
c_futimes :: CInt -> Ptr CTimeVal -> IO CInt
{-# LINE 463 "System/Posix/Files/Common.hsc" #-}
setFdTimesHiRes :: Fd -> POSIXTime -> POSIXTime -> IO ()
{-# LINE 473 "System/Posix/Files/Common.hsc" #-}
setFdTimesHiRes (Fd fd) atime mtime =
withArray [toCTimeSpec atime, toCTimeSpec mtime] $ \times ->
throwErrnoIfMinus1_ "setFdTimesHiRes" (c_futimens fd times)
{-# LINE 484 "System/Posix/Files/Common.hsc" #-}
touchFd :: Fd -> IO ()
{-# LINE 494 "System/Posix/Files/Common.hsc" #-}
touchFd (Fd fd) =
throwErrnoIfMinus1_ "touchFd" (c_futimes fd nullPtr)
{-# LINE 500 "System/Posix/Files/Common.hsc" #-}
setFdOwnerAndGroup :: Fd -> UserID -> GroupID -> IO ()
setFdOwnerAndGroup (Fd fd) uid gid =
throwErrnoIfMinus1_ "setFdOwnerAndGroup" (c_fchown fd uid gid)
foreign import ccall unsafe "fchown"
c_fchown :: CInt -> CUid -> CGid -> IO CInt
setFdSize :: Fd -> FileOffset -> IO ()
setFdSize (Fd fd) off =
throwErrnoIfMinus1_ "setFdSize" (c_ftruncate fd off)
data PathVar
= FileSizeBits
| LinkLimit
| InputLineLimit
| InputQueueLimit
| FileNameLimit
| PathNameLimit
| PipeBufferLimit
| SymbolicLinkLimit
| SetOwnerAndGroupIsRestricted
| FileNamesAreNotTruncated
| VDisableChar
| AsyncIOAvailable
| PrioIOAvailable
| SyncIOAvailable
pathVarConst :: PathVar -> CInt
pathVarConst v = case v of
LinkLimit -> (0)
{-# LINE 553 "System/Posix/Files/Common.hsc" #-}
InputLineLimit -> (1)
{-# LINE 554 "System/Posix/Files/Common.hsc" #-}
InputQueueLimit -> (2)
{-# LINE 555 "System/Posix/Files/Common.hsc" #-}
FileNameLimit -> (3)
{-# LINE 556 "System/Posix/Files/Common.hsc" #-}
PathNameLimit -> (4)
{-# LINE 557 "System/Posix/Files/Common.hsc" #-}
PipeBufferLimit -> (5)
{-# LINE 558 "System/Posix/Files/Common.hsc" #-}
SetOwnerAndGroupIsRestricted -> (6)
{-# LINE 559 "System/Posix/Files/Common.hsc" #-}
FileNamesAreNotTruncated -> (7)
{-# LINE 560 "System/Posix/Files/Common.hsc" #-}
VDisableChar -> (8)
{-# LINE 561 "System/Posix/Files/Common.hsc" #-}
{-# LINE 563 "System/Posix/Files/Common.hsc" #-}
SyncIOAvailable -> (9)
{-# LINE 564 "System/Posix/Files/Common.hsc" #-}
{-# LINE 567 "System/Posix/Files/Common.hsc" #-}
{-# LINE 569 "System/Posix/Files/Common.hsc" #-}
AsyncIOAvailable -> (10)
{-# LINE 570 "System/Posix/Files/Common.hsc" #-}
{-# LINE 573 "System/Posix/Files/Common.hsc" #-}
{-# LINE 575 "System/Posix/Files/Common.hsc" #-}
PrioIOAvailable -> (11)
{-# LINE 576 "System/Posix/Files/Common.hsc" #-}
{-# LINE 579 "System/Posix/Files/Common.hsc" #-}
{-# LINE 583 "System/Posix/Files/Common.hsc" #-}
FileSizeBits -> error "_PC_FILESIZEBITS not available"
{-# LINE 585 "System/Posix/Files/Common.hsc" #-}
{-# LINE 589 "System/Posix/Files/Common.hsc" #-}
SymbolicLinkLimit -> error "_PC_SYMLINK_MAX not available"
{-# LINE 591 "System/Posix/Files/Common.hsc" #-}
getFdPathVar :: Fd -> PathVar -> IO Limit
getFdPathVar (Fd fd) v =
throwErrnoIfMinus1 "getFdPathVar" $
c_fpathconf fd (pathVarConst v)
foreign import ccall unsafe "fpathconf"
c_fpathconf :: CInt -> CInt -> IO CLong