{-# LINE 1 "libraries/unix/System/Posix/Files/Common.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Trustworthy #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Files.Common
-- Copyright   :  (c) The University of Glasgow 2002
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  non-portable (requires POSIX)
--
-- Functions defined by the POSIX standards for manipulating and querying the
-- file system. Names of underlying POSIX functions are indicated whenever
-- possible. A more complete documentation of the POSIX functions together
-- with a more detailed description of different error conditions are usually
-- available in the system's manual pages or from
-- <http://www.unix.org/version3/online.html> (free registration required).
--
-- When a function that calls an underlying POSIX function fails, the errno
-- code is converted to an 'IOError' using 'Foreign.C.Error.errnoToIOError'.
-- For a list of which errno codes may be generated, consult the POSIX
-- documentation for the underlying function.
--
-----------------------------------------------------------------------------



module System.Posix.Files.Common (
    -- * File modes
    -- FileMode exported by System.Posix.Types
    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,

    -- ** Setting file modes
    setFdMode, setFileCreationMask,

    -- * File status
    FileStatus(..),
    -- ** Obtaining file status
    getFdStatus,
    -- ** Querying file status
    deviceID, fileID, fileMode, linkCount, fileOwner, fileGroup,
    specialDeviceID, fileSize, accessTime, modificationTime,
    statusChangeTime,
    accessTimeHiRes, modificationTimeHiRes, statusChangeTimeHiRes,
    setFdTimesHiRes, touchFd,
    isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
    isDirectory, isSymbolicLink, isSocket,

    fileBlockSize,
    fileBlocks,

    -- * Setting file sizes
    setFdSize,

    -- * Changing file ownership
    setFdOwnerAndGroup,

    -- * Find system-specific limits for a file
    PathVar(..), getFdPathVar, pathVarConst,

    -- * Low level types and functions

{-# LINE 74 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    CTimeSpec(..),
    toCTimeSpec,
    c_utimensat,

{-# LINE 78 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 83 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    CTimeVal(..),
    toCTimeVal,
    c_utimes,

{-# LINE 87 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    c_lutimes,

{-# LINE 89 "libraries/unix/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 101 "libraries/unix/System/Posix/Files/Common.hsc" #-}
import Foreign.Marshal (withArray)

{-# LINE 103 "libraries/unix/System/Posix/Files/Common.hsc" #-}
import Foreign.Ptr
import Foreign.Storable


{-# LINE 110 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- -----------------------------------------------------------------------------
-- POSIX file modes

-- The abstract type 'FileMode', constants and operators for
-- manipulating the file modes defined by POSIX.

-- | No permissions.
nullFileMode :: FileMode
nullFileMode :: FileMode
nullFileMode = FileMode
0

-- | Owner has read permission.
ownerReadMode :: FileMode
ownerReadMode :: FileMode
ownerReadMode = (FileMode
256)
{-# LINE 124 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Owner has write permission.
ownerWriteMode :: FileMode
ownerWriteMode :: FileMode
ownerWriteMode = (FileMode
128)
{-# LINE 128 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Owner has execute permission.
ownerExecuteMode :: FileMode
ownerExecuteMode :: FileMode
ownerExecuteMode = (FileMode
64)
{-# LINE 132 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Group has read permission.
groupReadMode :: FileMode
groupReadMode :: FileMode
groupReadMode = (FileMode
32)
{-# LINE 136 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Group has write permission.
groupWriteMode :: FileMode
groupWriteMode :: FileMode
groupWriteMode = (FileMode
16)
{-# LINE 140 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Group has execute permission.
groupExecuteMode :: FileMode
groupExecuteMode :: FileMode
groupExecuteMode = (FileMode
8)
{-# LINE 144 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Others have read permission.
otherReadMode :: FileMode
otherReadMode :: FileMode
otherReadMode = (FileMode
4)
{-# LINE 148 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Others have write permission.
otherWriteMode :: FileMode
otherWriteMode :: FileMode
otherWriteMode = (FileMode
2)
{-# LINE 152 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Others have execute permission.
otherExecuteMode :: FileMode
otherExecuteMode :: FileMode
otherExecuteMode = (FileMode
1)
{-# LINE 156 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Set user ID on execution.
setUserIDMode :: FileMode
setUserIDMode :: FileMode
setUserIDMode = (FileMode
2048)
{-# LINE 160 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Set group ID on execution.
setGroupIDMode :: FileMode
setGroupIDMode :: FileMode
setGroupIDMode = (FileMode
1024)
{-# LINE 164 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Owner, group and others have read and write permission.
stdFileMode :: FileMode
stdFileMode :: FileMode
stdFileMode = FileMode
ownerReadMode  FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
ownerWriteMode FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|.
              FileMode
groupReadMode  FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
groupWriteMode FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|.
              FileMode
otherReadMode  FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
otherWriteMode

-- | Owner has read, write and execute permission.
ownerModes :: FileMode
ownerModes :: FileMode
ownerModes = (FileMode
448)
{-# LINE 174 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Group has read, write and execute permission.
groupModes :: FileMode
groupModes :: FileMode
groupModes = (FileMode
56)
{-# LINE 178 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Others have read, write and execute permission.
otherModes :: FileMode
otherModes :: FileMode
otherModes = (FileMode
7)
{-# LINE 182 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Owner, group and others have read, write and execute permission.
accessModes :: FileMode
accessModes :: FileMode
accessModes = FileMode
ownerModes FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
groupModes FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
otherModes

-- | Combines the two file modes into one that contains modes that appear in
-- either.
unionFileModes :: FileMode -> FileMode -> FileMode
unionFileModes :: FileMode -> FileMode -> FileMode
unionFileModes FileMode
m1 FileMode
m2 = FileMode
m1 FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.|. FileMode
m2

-- | Combines two file modes into one that only contains modes that appear in
-- both.
intersectFileModes :: FileMode -> FileMode -> FileMode
intersectFileModes :: FileMode -> FileMode -> FileMode
intersectFileModes FileMode
m1 FileMode
m2 = FileMode
m1 FileMode -> FileMode -> FileMode
forall a. Bits a => a -> a -> a
.&. FileMode
m2

fileTypeModes :: FileMode
fileTypeModes :: FileMode
fileTypeModes = (FileMode
61440)
{-# LINE 199 "libraries/unix/System/Posix/Files/Common.hsc" #-}

blockSpecialMode :: FileMode
blockSpecialMode :: FileMode
blockSpecialMode = (FileMode
24576)
{-# LINE 202 "libraries/unix/System/Posix/Files/Common.hsc" #-}

characterSpecialMode :: FileMode
characterSpecialMode :: FileMode
characterSpecialMode = (FileMode
8192)
{-# LINE 205 "libraries/unix/System/Posix/Files/Common.hsc" #-}

namedPipeMode :: FileMode
namedPipeMode :: FileMode
namedPipeMode = (FileMode
4096)
{-# LINE 208 "libraries/unix/System/Posix/Files/Common.hsc" #-}

regularFileMode :: FileMode
regularFileMode :: FileMode
regularFileMode = (FileMode
32768)
{-# LINE 211 "libraries/unix/System/Posix/Files/Common.hsc" #-}

directoryMode :: FileMode
directoryMode :: FileMode
directoryMode = (FileMode
16384)
{-# LINE 214 "libraries/unix/System/Posix/Files/Common.hsc" #-}

symbolicLinkMode :: FileMode
symbolicLinkMode :: FileMode
symbolicLinkMode = (FileMode
40960)
{-# LINE 217 "libraries/unix/System/Posix/Files/Common.hsc" #-}

socketMode :: FileMode
socketMode :: FileMode
socketMode = (FileMode
49152)
{-# LINE 220 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 222 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | @setFdMode fd mode@ acts like 'setFileMode' but uses a file descriptor
-- @fd@ instead of a 'FilePath'.
--
-- Note: calls @fchmod@.
setFdMode :: Fd -> FileMode -> IO ()
setFdMode :: Fd -> FileMode -> IO ()
setFdMode (Fd CInt
fd) FileMode
m =
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"setFdMode" (CInt -> FileMode -> IO CInt
c_fchmod CInt
fd FileMode
m)

foreign import ccall unsafe "fchmod"
  c_fchmod :: CInt -> CMode -> IO CInt


{-# LINE 241 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | @setFileCreationMask mode@ sets the file mode creation mask to @mode@.
-- Modes set by this operation are subtracted from files and directories upon
-- creation. The previous file creation mask is returned.
--
-- Note: calls @umask@.
setFileCreationMask :: FileMode -> IO FileMode
setFileCreationMask :: FileMode -> IO FileMode
setFileCreationMask FileMode
mask = FileMode -> IO FileMode
c_umask FileMode
mask

-- -----------------------------------------------------------------------------
-- stat() support

-- | POSIX defines operations to get information, such as owner, permissions,
-- size and access times, about a file. This information is represented by the
-- 'FileStatus' type.
--
-- Note: see @chmod@.
--
-- Limitations: Support for high resolution timestamps is filesystem dependent:
--
-- - HFS+ volumes on OS X only support whole-second times.
--
newtype FileStatus = FileStatus (ForeignPtr CStat) -- ^ The constructor is considered internal and may change.

-- | ID of the device on which this file resides.
deviceID         :: FileStatus -> DeviceID
-- | inode number
fileID           :: FileStatus -> FileID
-- | File mode (such as permissions).
fileMode         :: FileStatus -> FileMode
-- | Number of hard links to this file.
linkCount        :: FileStatus -> LinkCount
-- | ID of owner.
fileOwner        :: FileStatus -> UserID
-- | ID of group.
fileGroup        :: FileStatus -> GroupID
-- | Describes the device that this file represents.
specialDeviceID  :: FileStatus -> DeviceID
-- | Size of the file in bytes. If this file is a symbolic link the size is
-- the length of the pathname it contains.
fileSize         :: FileStatus -> FileOffset
-- | Number of blocks allocated for this file, in units of
-- 512-bytes. Returns @Nothing@ if @st_blocks@ is not supported on this
-- platform.
fileBlocks       :: FileStatus -> Maybe CBlkCnt
-- | Gives the preferred block size for efficient filesystem I/O in
-- bytes. Returns @Nothing@ if @st_blocksize@ is not supported on this
-- platform.
fileBlockSize    :: FileStatus -> Maybe CBlkSize
-- | Time of last access.
accessTime       :: FileStatus -> EpochTime
-- | Time of last access in sub-second resolution. Depends on the timestamp resolution of the
-- underlying filesystem.
accessTimeHiRes  :: FileStatus -> POSIXTime
-- | Time of last modification.
modificationTime :: FileStatus -> EpochTime
-- | Time of last modification in sub-second resolution. Depends on the timestamp resolution of the
-- underlying filesystem.
modificationTimeHiRes :: FileStatus -> POSIXTime
-- | Time of last status change (i.e. owner, group, link count, mode, etc.).
statusChangeTime :: FileStatus -> EpochTime
-- | Time of last status change (i.e. owner, group, link count, mode, etc.) in sub-second resolution.
-- Depends on the timestamp resolution of the underlying filesystem.
statusChangeTimeHiRes :: FileStatus -> POSIXTime

deviceID :: FileStatus -> DeviceID
deviceID (FileStatus ForeignPtr CStat
stat) =
  IO DeviceID -> DeviceID
forall a. IO a -> a
unsafePerformIO (IO DeviceID -> DeviceID) -> IO DeviceID -> DeviceID
forall a b. (a -> b) -> a -> b
$ ForeignPtr CStat -> (Ptr CStat -> IO DeviceID) -> IO DeviceID
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CStat
stat ((Ptr CStat -> IO DeviceID) -> IO DeviceID)
-> (Ptr CStat -> IO DeviceID) -> IO DeviceID
forall a b. (a -> b) -> a -> b
$ ((\Ptr CStat
hsc_ptr -> Ptr CStat -> Int -> IO DeviceID
forall b. Ptr b -> Int -> IO DeviceID
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CStat
hsc_ptr Int
0))
{-# LINE 308 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileID (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 8))
fileMode :: FileStatus -> FileMode
{-# LINE 310 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileMode (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 24))
{-# LINE 312 "libraries/unix/System/Posix/Files/Common.hsc" #-}
linkCount (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 16))
{-# LINE 314 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileOwner (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 28))
{-# LINE 316 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileGroup (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 32))
{-# LINE 318 "libraries/unix/System/Posix/Files/Common.hsc" #-}
specialDeviceID (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 40))
fileSize :: FileStatus -> FileOffset
{-# LINE 320 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileSize (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 48))
{-# LINE 322 "libraries/unix/System/Posix/Files/Common.hsc" #-}
accessTime (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 72))
{-# LINE 324 "libraries/unix/System/Posix/Files/Common.hsc" #-}
modificationTime (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 88))
{-# LINE 326 "libraries/unix/System/Posix/Files/Common.hsc" #-}
statusChangeTime (FileStatus stat) =
  unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 104))
{-# LINE 328 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 330 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileBlocks (FileStatus stat) =
  Just $ unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 64))
{-# LINE 332 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 335 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 336 "libraries/unix/System/Posix/Files/Common.hsc" #-}
fileBlockSize (FileStatus stat) =
  Just $ unsafePerformIO $ withForeignPtr stat $ ((\hsc_ptr -> peekByteOff hsc_ptr 56))
{-# LINE 338 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 341 "libraries/unix/System/Posix/Files/Common.hsc" #-}

accessTimeHiRes :: FileStatus -> POSIXTime
accessTimeHiRes (FileStatus ForeignPtr CStat
stat) =
  IO POSIXTime -> POSIXTime
forall a. IO a -> a
unsafePerformIO (IO POSIXTime -> POSIXTime) -> IO POSIXTime -> POSIXTime
forall a b. (a -> b) -> a -> b
$ ForeignPtr CStat -> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CStat
stat ((Ptr CStat -> IO POSIXTime) -> IO POSIXTime)
-> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ \Ptr CStat
stat_ptr -> do
    EpochTime
sec  <- ((\Ptr CStat
hsc_ptr -> Ptr CStat -> Int -> IO EpochTime
forall b. Ptr b -> Int -> IO EpochTime
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CStat
hsc_ptr Int
72)) Ptr CStat
stat_ptr :: IO EpochTime
{-# LINE 345 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 346 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 80)) stat_ptr :: IO (Int64)
{-# LINE 347 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    let frac = toInteger nsec % 10^(9::Int)

{-# LINE 363 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    POSIXTime -> IO POSIXTime
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (POSIXTime -> IO POSIXTime) -> POSIXTime -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ Rational -> POSIXTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> POSIXTime) -> Rational -> POSIXTime
forall a b. (a -> b) -> a -> b
$ EpochTime -> Rational
forall a. Real a => a -> Rational
toRational EpochTime
sec Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
frac

modificationTimeHiRes :: FileStatus -> POSIXTime
modificationTimeHiRes (FileStatus ForeignPtr CStat
stat) =
  IO POSIXTime -> POSIXTime
forall a. IO a -> a
unsafePerformIO (IO POSIXTime -> POSIXTime) -> IO POSIXTime -> POSIXTime
forall a b. (a -> b) -> a -> b
$ ForeignPtr CStat -> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CStat
stat ((Ptr CStat -> IO POSIXTime) -> IO POSIXTime)
-> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ \Ptr CStat
stat_ptr -> do
    EpochTime
sec  <- ((\Ptr CStat
hsc_ptr -> Ptr CStat -> Int -> IO EpochTime
forall b. Ptr b -> Int -> IO EpochTime
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CStat
hsc_ptr Int
88)) Ptr CStat
stat_ptr :: IO EpochTime
{-# LINE 368 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 369 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 96)) stat_ptr :: IO (Int64)
{-# LINE 370 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    let frac = toInteger nsec % 10^(9::Int)

{-# LINE 386 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    POSIXTime -> IO POSIXTime
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (POSIXTime -> IO POSIXTime) -> POSIXTime -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ Rational -> POSIXTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> POSIXTime) -> Rational -> POSIXTime
forall a b. (a -> b) -> a -> b
$ EpochTime -> Rational
forall a. Real a => a -> Rational
toRational EpochTime
sec Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
frac

statusChangeTimeHiRes :: FileStatus -> POSIXTime
statusChangeTimeHiRes (FileStatus ForeignPtr CStat
stat) =
  IO POSIXTime -> POSIXTime
forall a. IO a -> a
unsafePerformIO (IO POSIXTime -> POSIXTime) -> IO POSIXTime -> POSIXTime
forall a b. (a -> b) -> a -> b
$ ForeignPtr CStat -> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CStat
stat ((Ptr CStat -> IO POSIXTime) -> IO POSIXTime)
-> (Ptr CStat -> IO POSIXTime) -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ \Ptr CStat
stat_ptr -> do
    EpochTime
sec  <- ((\Ptr CStat
hsc_ptr -> Ptr CStat -> Int -> IO EpochTime
forall b. Ptr b -> Int -> IO EpochTime
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CStat
hsc_ptr Int
104)) Ptr CStat
stat_ptr :: IO EpochTime
{-# LINE 391 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 392 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 112)) stat_ptr :: IO (Int64)
{-# LINE 393 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    let frac = toInteger nsec % 10^(9::Int)

{-# LINE 409 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    POSIXTime -> IO POSIXTime
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (POSIXTime -> IO POSIXTime) -> POSIXTime -> IO POSIXTime
forall a b. (a -> b) -> a -> b
$ Rational -> POSIXTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> POSIXTime) -> Rational -> POSIXTime
forall a b. (a -> b) -> a -> b
$ EpochTime -> Rational
forall a. Real a => a -> Rational
toRational EpochTime
sec Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
frac

-- | Checks if this file is a block device.
isBlockDevice     :: FileStatus -> Bool
-- | Checks if this file is a character device.
isCharacterDevice :: FileStatus -> Bool
-- | Checks if this file is a named pipe device.
isNamedPipe       :: FileStatus -> Bool
-- | Checks if this file is a regular file device.
isRegularFile     :: FileStatus -> Bool
-- | Checks if this file is a directory device.
isDirectory       :: FileStatus -> Bool
-- | Checks if this file is a symbolic link device.
isSymbolicLink    :: FileStatus -> Bool
-- | Checks if this file is a socket device.
isSocket          :: FileStatus -> Bool

isBlockDevice :: FileStatus -> Bool
isBlockDevice FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
blockSpecialMode
isCharacterDevice :: FileStatus -> Bool
isCharacterDevice FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
characterSpecialMode
isNamedPipe :: FileStatus -> Bool
isNamedPipe FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
namedPipeMode
isRegularFile :: FileStatus -> Bool
isRegularFile FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
regularFileMode
isDirectory :: FileStatus -> Bool
isDirectory FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
directoryMode
isSymbolicLink :: FileStatus -> Bool
isSymbolicLink FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
symbolicLinkMode
isSocket :: FileStatus -> Bool
isSocket FileStatus
stat =
  (FileStatus -> FileMode
fileMode FileStatus
stat FileMode -> FileMode -> FileMode
`intersectFileModes` FileMode
fileTypeModes) FileMode -> FileMode -> Bool
forall a. Eq a => a -> a -> Bool
== FileMode
socketMode

-- | @getFdStatus fd@ acts as 'getFileStatus' but uses a file descriptor @fd@.
--
-- Note: calls @fstat@.
getFdStatus :: Fd -> IO FileStatus
getFdStatus :: Fd -> IO FileStatus
getFdStatus (Fd CInt
fd) = do
  ForeignPtr CStat
fp <- Int -> IO (ForeignPtr CStat)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
144)
{-# LINE 447 "libraries/unix/System/Posix/Files/Common.hsc" #-}
  withForeignPtr fp $ \p ->
    throwErrnoIfMinus1_ "getFdStatus" (c_fstat fd p)
  FileStatus -> IO FileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr CStat -> FileStatus
FileStatus ForeignPtr CStat
fp)

-- -----------------------------------------------------------------------------
-- Setting file times


{-# LINE 455 "libraries/unix/System/Posix/Files/Common.hsc" #-}
data CTimeSpec = CTimeSpec EpochTime CLong

instance Storable CTimeSpec where
    sizeOf :: CTimeSpec -> Int
sizeOf    CTimeSpec
_ = (Int
16)
{-# LINE 459 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    alignment _ = alignment (undefined :: CInt)
    poke :: Ptr CTimeSpec -> CTimeSpec -> IO ()
poke Ptr CTimeSpec
p (CTimeSpec EpochTime
sec CLong
nsec) = do
        ((\Ptr CTimeSpec
hsc_ptr -> Ptr CTimeSpec -> Int -> EpochTime -> IO ()
forall b. Ptr b -> Int -> EpochTime -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimeSpec
hsc_ptr Int
0)) Ptr CTimeSpec
p EpochTime
sec
{-# LINE 462 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        ((\Ptr CTimeSpec
hsc_ptr -> Ptr CTimeSpec -> Int -> CLong -> IO ()
forall b. Ptr b -> Int -> CLong -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimeSpec
hsc_ptr Int
8)) Ptr CTimeSpec
p CLong
nsec
{-# LINE 463 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    peek p = do
        sec  <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 465 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        nsec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 466 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        return $ CTimeSpec sec nsec

toCTimeSpec :: POSIXTime -> CTimeSpec
toCTimeSpec :: POSIXTime -> CTimeSpec
toCTimeSpec POSIXTime
t = EpochTime -> CLong -> CTimeSpec
CTimeSpec (Int64 -> EpochTime
CTime Int64
sec) (Rational -> CLong
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Rational -> CLong) -> Rational -> CLong
forall a b. (a -> b) -> a -> b
$ Rational
10Rational -> Int -> Rational
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
9::Int) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
frac)
  where
    (Int64
sec, Rational
frac) = if (Rational
frac' Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< Rational
0) then (Int64
sec' Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
1, Rational
frac' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
1) else (Int64
sec', Rational
frac')
    (Int64
sec', Rational
frac') = Rational -> (Int64, Rational)
forall b. Integral b => Rational -> (b, Rational)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction (Rational -> (Int64, Rational)) -> Rational -> (Int64, Rational)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Rational
forall a. Real a => a -> Rational
toRational POSIXTime
t

{-# LINE 474 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 476 "libraries/unix/System/Posix/Files/Common.hsc" #-}
foreign import capi unsafe "sys/stat.h utimensat"
    c_utimensat :: CInt -> CString -> Ptr CTimeSpec -> CInt -> IO CInt

{-# LINE 479 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 481 "libraries/unix/System/Posix/Files/Common.hsc" #-}
foreign import capi unsafe "sys/stat.h futimens"
    c_futimens :: CInt -> Ptr CTimeSpec -> IO CInt

{-# LINE 484 "libraries/unix/System/Posix/Files/Common.hsc" #-}

data CTimeVal = CTimeVal CLong CLong

instance Storable CTimeVal where
    sizeOf :: CTimeVal -> Int
sizeOf    CTimeVal
_ = (Int
16)
{-# LINE 489 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    alignment _ = alignment (undefined :: CInt)
    poke :: Ptr CTimeVal -> CTimeVal -> IO ()
poke Ptr CTimeVal
p (CTimeVal CLong
sec CLong
usec) = do
        ((\Ptr CTimeVal
hsc_ptr -> Ptr CTimeVal -> Int -> CLong -> IO ()
forall b. Ptr b -> Int -> CLong -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimeVal
hsc_ptr Int
0)) Ptr CTimeVal
p CLong
sec
{-# LINE 492 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        ((\Ptr CTimeVal
hsc_ptr -> Ptr CTimeVal -> Int -> CLong -> IO ()
forall b. Ptr b -> Int -> CLong -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimeVal
hsc_ptr Int
8)) Ptr CTimeVal
p CLong
usec
{-# LINE 493 "libraries/unix/System/Posix/Files/Common.hsc" #-}
    peek p = do
        sec  <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 495 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        usec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 496 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        return $ CTimeVal sec usec

toCTimeVal :: POSIXTime -> CTimeVal
toCTimeVal :: POSIXTime -> CTimeVal
toCTimeVal POSIXTime
t = CLong -> CLong -> CTimeVal
CTimeVal CLong
sec (Rational -> CLong
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Rational -> CLong) -> Rational -> CLong
forall a b. (a -> b) -> a -> b
$ Rational
10Rational -> Int -> Rational
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
6::Int) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
frac)
  where
    (CLong
sec, Rational
frac) = if (Rational
frac' Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< Rational
0) then (CLong
sec' CLong -> CLong -> CLong
forall a. Num a => a -> a -> a
- CLong
1, Rational
frac' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
1) else (CLong
sec', Rational
frac')
    (CLong
sec', Rational
frac') = Rational -> (CLong, Rational)
forall b. Integral b => Rational -> (b, Rational)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction (Rational -> (CLong, Rational)) -> Rational -> (CLong, Rational)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Rational
forall a. Real a => a -> Rational
toRational POSIXTime
t

foreign import capi unsafe "sys/time.h utimes"
    c_utimes :: CString -> Ptr CTimeVal -> IO CInt


{-# LINE 508 "libraries/unix/System/Posix/Files/Common.hsc" #-}
foreign import capi unsafe "sys/time.h lutimes"
    c_lutimes :: CString -> Ptr CTimeVal -> IO CInt

{-# LINE 511 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 513 "libraries/unix/System/Posix/Files/Common.hsc" #-}
foreign import capi unsafe "sys/time.h futimes"
    c_futimes :: CInt -> Ptr CTimeVal -> IO CInt

{-# LINE 516 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 525 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Like 'setFileTimesHiRes' but uses a file descriptor instead of a path.
-- This operation is not supported on all platforms. On these platforms,
-- this function will raise an exception.
--
-- Note: calls @futimens@ or @futimes@. Support for high resolution timestamps
--   is filesystem dependent with the following limitations:
--
-- - HFS+ volumes on OS X truncate the sub-second part of the timestamp.
--
--
-- @since 2.7.0.0
setFdTimesHiRes :: Fd -> POSIXTime -> POSIXTime -> IO ()

{-# LINE 542 "libraries/unix/System/Posix/Files/Common.hsc" #-}
setFdTimesHiRes :: Fd -> POSIXTime -> POSIXTime -> IO ()
setFdTimesHiRes (Fd CInt
fd) POSIXTime
atime POSIXTime
mtime =
  [CTimeSpec] -> (Ptr CTimeSpec -> IO ()) -> IO ()
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [POSIXTime -> CTimeSpec
toCTimeSpec POSIXTime
atime, POSIXTime -> CTimeSpec
toCTimeSpec POSIXTime
mtime] ((Ptr CTimeSpec -> IO ()) -> IO ())
-> (Ptr CTimeSpec -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CTimeSpec
times ->
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"setFdTimesHiRes" (CInt -> Ptr CTimeSpec -> IO CInt
c_futimens CInt
fd Ptr CTimeSpec
times)

{-# LINE 554 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Like 'touchFile' but uses a file descriptor instead of a path.
-- This operation is not supported on all platforms. On these platforms,
-- this function will raise an exception.
--
-- Note: calls @futimes@.
--
-- @since 2.7.0.0
touchFd :: Fd -> IO ()

{-# LINE 568 "libraries/unix/System/Posix/Files/Common.hsc" #-}
touchFd :: Fd -> IO ()
touchFd (Fd CInt
fd) =
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"touchFd" (CInt -> Ptr CTimeVal -> IO CInt
c_futimes CInt
fd Ptr CTimeVal
forall a. Ptr a
nullPtr)

{-# LINE 575 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- -----------------------------------------------------------------------------
-- fchown()


{-# LINE 580 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | Acts as 'setOwnerAndGroup' but uses a file descriptor instead of a
-- 'FilePath'.
--
-- Note: calls @fchown@.
setFdOwnerAndGroup :: Fd -> UserID -> GroupID -> IO ()
setFdOwnerAndGroup :: Fd -> UserID -> GroupID -> IO ()
setFdOwnerAndGroup (Fd CInt
fd) UserID
uid GroupID
gid =
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"setFdOwnerAndGroup" (CInt -> UserID -> GroupID -> IO CInt
c_fchown CInt
fd UserID
uid GroupID
gid)

foreign import ccall unsafe "fchown"
  c_fchown :: CInt -> CUid -> CGid -> IO CInt


{-# LINE 599 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- -----------------------------------------------------------------------------
-- ftruncate()

-- | Acts as 'setFileSize' but uses a file descriptor instead of a 'FilePath'.
--
-- Note: calls @ftruncate@.
setFdSize :: Fd -> FileOffset -> IO ()
setFdSize :: Fd -> FileOffset -> IO ()
setFdSize (Fd CInt
fd) FileOffset
off =
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"setFdSize" (CInt -> FileOffset -> IO CInt
c_ftruncate CInt
fd FileOffset
off)

-- -----------------------------------------------------------------------------
-- pathconf()/fpathconf() support

data PathVar
  = FileSizeBits                  {- _PC_FILESIZEBITS     -}
  | LinkLimit                     {- _PC_LINK_MAX         -}
  | InputLineLimit                {- _PC_MAX_CANON        -}
  | InputQueueLimit               {- _PC_MAX_INPUT        -}
  | FileNameLimit                 {- _PC_NAME_MAX         -}
  | PathNameLimit                 {- _PC_PATH_MAX         -}
  | PipeBufferLimit               {- _PC_PIPE_BUF         -}
                                  -- These are described as optional in POSIX:
                                  {- _PC_ALLOC_SIZE_MIN     -}
                                  {- _PC_REC_INCR_XFER_SIZE -}
                                  {- _PC_REC_MAX_XFER_SIZE  -}
                                  {- _PC_REC_MIN_XFER_SIZE  -}
                                  {- _PC_REC_XFER_ALIGN     -}
  | SymbolicLinkLimit             {- _PC_SYMLINK_MAX      -}
  | SetOwnerAndGroupIsRestricted  {- _PC_CHOWN_RESTRICTED -}
  | FileNamesAreNotTruncated      {- _PC_NO_TRUNC         -}
  | VDisableChar                  {- _PC_VDISABLE         -}
  | AsyncIOAvailable              {- _PC_ASYNC_IO         -}
  | PrioIOAvailable               {- _PC_PRIO_IO          -}
  | SyncIOAvailable               {- _PC_SYNC_IO          -}

pathVarConst :: PathVar -> CInt
pathVarConst :: PathVar -> CInt
pathVarConst PathVar
v = case PathVar
v of
        PathVar
LinkLimit                       -> (CInt
0)
{-# LINE 638 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
InputLineLimit                  -> (CInt
1)
{-# LINE 639 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
InputQueueLimit                 -> (CInt
2)
{-# LINE 640 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
FileNameLimit                   -> (CInt
3)
{-# LINE 641 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
PathNameLimit                   -> (CInt
4)
{-# LINE 642 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
PipeBufferLimit                 -> (CInt
5)
{-# LINE 643 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
SetOwnerAndGroupIsRestricted    -> (CInt
6)
{-# LINE 644 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
FileNamesAreNotTruncated        -> (CInt
7)
{-# LINE 645 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
VDisableChar                    -> (CInt
8)
{-# LINE 646 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 648 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
SyncIOAvailable         -> (CInt
9)
{-# LINE 649 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 652 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 654 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
AsyncIOAvailable        -> (CInt
10)
{-# LINE 655 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 658 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 660 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
PrioIOAvailable         -> (CInt
11)
{-# LINE 661 "libraries/unix/System/Posix/Files/Common.hsc" #-}

{-# LINE 664 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 668 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
FileSizeBits            -> String -> CInt
forall a. HasCallStack => String -> a
error String
"_PC_FILESIZEBITS not available"

{-# LINE 670 "libraries/unix/System/Posix/Files/Common.hsc" #-}


{-# LINE 674 "libraries/unix/System/Posix/Files/Common.hsc" #-}
        PathVar
SymbolicLinkLimit       -> String -> CInt
forall a. HasCallStack => String -> a
error String
"_PC_SYMLINK_MAX not available"

{-# LINE 676 "libraries/unix/System/Posix/Files/Common.hsc" #-}

-- | @getFdPathVar var fd@ obtains the dynamic value of the requested
-- configurable file limit or option associated with the file or directory
-- attached to the open channel @fd@. For defined file limits, @getFdPathVar@
-- returns the associated value.  For defined file options, the result of
-- @getFdPathVar@ is undefined, but not failure.
--
-- Note: calls @fpathconf@.
getFdPathVar :: Fd -> PathVar -> IO Limit
getFdPathVar :: Fd -> PathVar -> IO CLong
getFdPathVar (Fd CInt
fd) PathVar
v =
    String -> IO CLong -> IO CLong
forall a. (Eq a, Num a) => String -> IO a -> IO a
throwErrnoIfMinus1 String
"getFdPathVar" (IO CLong -> IO CLong) -> IO CLong -> IO CLong
forall a b. (a -> b) -> a -> b
$
      CInt -> CInt -> IO CLong
c_fpathconf CInt
fd (PathVar -> CInt
pathVarConst PathVar
v)

foreign import ccall unsafe "fpathconf"
  c_fpathconf :: CInt -> CInt -> IO CLong