{-# LINE 1 "libraries/unix/System/Posix/IO/ByteString.hsc" #-}
{-# LANGUAGE Trustworthy #-}
module System.Posix.IO.ByteString (
    
    
    stdInput, stdOutput, stdError,
    
    OpenMode(..),
    OpenFileFlags(..), defaultFileFlags,
    openFd, openFdAt, createFile, createFileAt,
    closeFd,
    
    
    
    fdRead, fdWrite,
    fdReadBuf, fdWriteBuf,
    
    fdSeek,
    
    FdOption(..),
    queryFdOption,
    setFdOption,
    
    FileLock,
    LockRequest(..),
    getLock,  setLock,
    waitToSetLock,
    
    createPipe,
    
    dup, dupTo,
    
    handleToFd,
    fdToHandle,
  ) where
import Data.ByteString ( ByteString, empty )
import qualified Data.ByteString.Internal as BI
import qualified Data.ByteString.Unsafe as BU
import Foreign ( castPtr )
import GHC.IO.Exception ( IOErrorType(EOF) )
import System.IO.Error ( ioeSetErrorString, mkIOError )
import System.Posix.Types
import System.Posix.IO.Common
import System.Posix.ByteString.FilePath
openFd :: RawFilePath
       -> OpenMode
       -> OpenFileFlags
       -> IO Fd
openFd :: RawFilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFd = Maybe Fd -> RawFilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFdAt Maybe Fd
forall a. Maybe a
Nothing
openFdAt :: Maybe Fd 
         -> RawFilePath 
         -> OpenMode 
         -> OpenFileFlags 
         -> IO Fd
openFdAt :: Maybe Fd -> RawFilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFdAt Maybe Fd
fdMay RawFilePath
name OpenMode
how OpenFileFlags
flags =
  RawFilePath -> (CString -> IO Fd) -> IO Fd
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name ((CString -> IO Fd) -> IO Fd) -> (CString -> IO Fd) -> IO Fd
forall a b. (a -> b) -> a -> b
$ \CString
str ->
    String -> RawFilePath -> IO Fd -> IO Fd
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO a
throwErrnoPathIfMinus1Retry String
"openFdAt" RawFilePath
name (IO Fd -> IO Fd) -> IO Fd -> IO Fd
forall a b. (a -> b) -> a -> b
$
      Maybe Fd -> CString -> OpenMode -> OpenFileFlags -> IO Fd
openat_ Maybe Fd
fdMay CString
str OpenMode
how OpenFileFlags
flags
createFile :: RawFilePath -> FileMode -> IO Fd
createFile :: RawFilePath -> FileMode -> IO Fd
createFile = Maybe Fd -> RawFilePath -> FileMode -> IO Fd
createFileAt Maybe Fd
forall a. Maybe a
Nothing
createFileAt :: Maybe Fd 
             -> RawFilePath 
             -> FileMode 
             -> IO Fd
createFileAt :: Maybe Fd -> RawFilePath -> FileMode -> IO Fd
createFileAt Maybe Fd
fdMay RawFilePath
name FileMode
mode
  = Maybe Fd -> RawFilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFdAt Maybe Fd
fdMay RawFilePath
name OpenMode
WriteOnly OpenFileFlags
defaultFileFlags{ trunc=True, creat=(Just mode) }
fdRead :: Fd
       -> ByteCount 
       -> IO ByteString 
fdRead :: Fd -> ByteCount -> IO RawFilePath
fdRead Fd
_fd ByteCount
0 = RawFilePath -> IO RawFilePath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return RawFilePath
empty
fdRead Fd
fd ByteCount
nbytes =
  Int -> (Ptr Word8 -> IO Int) -> IO RawFilePath
BI.createUptoN (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes) ((Ptr Word8 -> IO Int) -> IO RawFilePath)
-> (Ptr Word8 -> IO Int) -> IO RawFilePath
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
buf -> do
  ByteCount
rc <- Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdReadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes
  case ByteCount
rc of
    ByteCount
0 -> IOError -> IO Int
forall a. IOError -> IO a
ioError (IOError -> String -> IOError
ioeSetErrorString (IOErrorType -> String -> Maybe Handle -> Maybe String -> IOError
mkIOError IOErrorType
EOF String
"fdRead" Maybe Handle
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing) String
"EOF")
    ByteCount
n -> Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
n)
fdWrite :: Fd -> ByteString -> IO ByteCount
fdWrite :: Fd -> RawFilePath -> IO ByteCount
fdWrite Fd
fd RawFilePath
bs =
  RawFilePath -> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a. RawFilePath -> (CStringLen -> IO a) -> IO a
BU.unsafeUseAsCStringLen RawFilePath
bs ((CStringLen -> IO ByteCount) -> IO ByteCount)
-> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ \ (CString
buf,Int
len) ->
    Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdWriteBuf Fd
fd (CString -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr CString
buf) (Int -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)