{-# LINE 1 "src/System/Posix/IO/ByteString/Ext.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# OPTIONS_GHC -fwarn-tabs #-}
module System.Posix.IO.ByteString.Ext
(
fdRead
, fdReadBuf
, tryFdReadBuf
, fdReads
, fdReadvBuf
, tryFdReadvBuf
, fdPread
, fdPreadBuf
, tryFdPreadBuf
, fdPreads
, fdWrite
, fdWriteBuf
, tryFdWriteBuf
, fdWrites
, fdWritev
, fdWritevBuf
, tryFdWritevBuf
, fdPwrite
, fdPwriteBuf
, tryFdPwriteBuf
, fdSeek
, tryFdSeek
) where
import Data.Word (Word8)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BSI
import qualified Data.ByteString.Unsafe as BSU
import System.IO (SeekMode(..))
import qualified System.IO.Error as IOE
import qualified System.Posix.Internals as Base
import System.Posix.Types.Iovec
import System.Posix.Types ( Fd, ByteCount, FileOffset
, CSsize(..), COff(..))
import Foreign.C.Types (CInt(..), CSize(..), CChar(..))
import qualified Foreign.C.Error as C
import Foreign.C.Error.Safe
import Foreign.Ptr (Ptr, castPtr, plusPtr)
import qualified Foreign.Marshal.Array as FMA (withArrayLen)
import Control.Arrow (ArrowChoice(..))
ioErrorEOF :: String -> IO a
ioErrorEOF :: forall a. String -> IO a
ioErrorEOF String
fun =
IOError -> IO a
forall a. IOError -> IO a
IOE.ioError
(IOError -> String -> IOError
IOE.ioeSetErrorString
(IOErrorType -> String -> Maybe Handle -> Maybe String -> IOError
IOE.mkIOError IOErrorType
IOE.eofErrorType String
fun Maybe Handle
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing)
String
"EOF")
foreign import ccall safe "read"
c_safe_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
fdReadBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> IO ByteCount
fdReadBuf :: Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdReadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdReadBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_read
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
_fdReadBuf :: String
_fdReadBuf :: String
_fdReadBuf = String
"System.Posix.IO.ByteString.fdReadBuf"
{-# NOINLINE _fdReadBuf #-}
tryFdReadBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> IO (Either C.Errno ByteCount)
tryFdReadBuf :: Fd -> Ptr Word8 -> ByteCount -> IO (Either Errno ByteCount)
tryFdReadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_read
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
fdRead
:: Fd
-> ByteCount
-> IO BS.ByteString
fdRead :: Fd -> ByteCount -> IO ByteString
fdRead Fd
fd ByteCount
nbytes
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise =
Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes) ((Ptr Word8 -> IO Int) -> IO ByteString)
-> (Ptr Word8 -> IO Int) -> IO ByteString
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
if ByteCount
0 ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
rc
then String -> IO Int
forall a. String -> IO a
ioErrorEOF String
_fdRead
else 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
rc)
_fdRead :: String
_fdRead :: String
_fdRead = String
"System.Posix.IO.ByteString.fdRead"
{-# NOINLINE _fdRead #-}
fdReads
:: (ByteCount -> a -> Maybe a)
-> a
-> Fd
-> ByteCount
-> IO BS.ByteString
fdReads :: forall a.
(ByteCount -> a -> Maybe a)
-> a -> Fd -> ByteCount -> IO ByteString
fdReads ByteCount -> a -> Maybe a
f a
z0 Fd
fd ByteCount
n0
| ByteCount
n0 ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise = Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
n0) (a -> ByteCount -> ByteCount -> Ptr Word8 -> IO Int
forall {b}.
Num b =>
a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
z0 ByteCount
0 ByteCount
n0)
where
go :: a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
_ ByteCount
len ByteCount
n Ptr Word8
buf | ByteCount
len ByteCount -> Bool -> Bool
forall a b. a -> b -> b
`seq` ByteCount
n ByteCount -> Bool -> Bool
forall a b. a -> b -> b
`seq` Ptr Word8
buf Ptr Word8 -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = IO b
forall a. HasCallStack => a
undefined
go a
z ByteCount
len ByteCount
n Ptr Word8
buf = do
ByteCount
rc <- Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdReadBuf Fd
fd Ptr Word8
buf ByteCount
n
let len' :: ByteCount
len' = ByteCount
len ByteCount -> ByteCount -> ByteCount
forall a. Num a => a -> a -> a
+ ByteCount
rc
case ByteCount
rc of
ByteCount
_ | ByteCount
rc ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
0 -> String -> IO b
forall a. String -> IO a
ioErrorEOF String
_fdReads
| ByteCount
rc ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
n -> b -> IO b
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len')
| Bool
otherwise ->
case ByteCount -> a -> Maybe a
f ByteCount
len' a
z of
Maybe a
Nothing -> b -> IO b
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len')
Just a
z' ->
a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
z' ByteCount
len' (ByteCount
n ByteCount -> ByteCount -> ByteCount
forall a. Num a => a -> a -> a
- ByteCount
rc) (Ptr Word8
buf Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
rc)
_fdReads :: String
_fdReads :: String
_fdReads = String
"System.Posix.IO.ByteString.fdReads"
{-# NOINLINE _fdReads #-}
foreign import ccall safe "readv"
c_safe_readv :: CInt -> Ptr CIovec -> CInt -> IO CSsize
fdReadvBuf
:: Fd
-> Ptr CIovec
-> Int
-> IO ByteCount
fdReadvBuf :: Fd -> Ptr CIovec -> Int -> IO ByteCount
fdReadvBuf Fd
fd Ptr CIovec
bufs Int
len
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdReadvBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_readv (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
_fdReadvBuf :: String
_fdReadvBuf :: String
_fdReadvBuf = String
"System.Posix.IO.ByteString.fdReadvBuf"
{-# NOINLINE _fdReadvBuf #-}
tryFdReadvBuf
:: Fd
-> Ptr CIovec
-> Int
-> IO (Either C.Errno ByteCount)
tryFdReadvBuf :: Fd -> Ptr CIovec -> Int -> IO (Either Errno ByteCount)
tryFdReadvBuf Fd
fd Ptr CIovec
bufs Int
len
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_readv (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
foreign import ccall safe "pread"
c_safe_pread :: CInt -> Ptr Word8 -> CSize -> COff -> IO CSsize
fdPreadBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> FileOffset
-> IO ByteCount
fdPreadBuf :: Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPreadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes COff
offset
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdPreadBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pread
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
Ptr Word8
buf
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(COff -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
offset)
_fdPreadBuf :: String
_fdPreadBuf :: String
_fdPreadBuf = String
"System.Posix.IO.ByteString.fdPreadBuf"
{-# NOINLINE _fdPreadBuf #-}
tryFdPreadBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> FileOffset
-> IO (Either C.Errno ByteCount)
tryFdPreadBuf :: Fd -> Ptr Word8 -> ByteCount -> COff -> IO (Either Errno ByteCount)
tryFdPreadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes COff
offset
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pread
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
Ptr Word8
buf
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(COff -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
offset)
fdPread
:: Fd
-> ByteCount
-> FileOffset
-> IO BS.ByteString
fdPread :: Fd -> ByteCount -> COff -> IO ByteString
fdPread Fd
fd ByteCount
nbytes COff
offset
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise =
Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes) ((Ptr Word8 -> IO Int) -> IO ByteString)
-> (Ptr Word8 -> IO Int) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
buf -> do
ByteCount
rc <- Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPreadBuf Fd
fd Ptr Word8
buf ByteCount
nbytes COff
offset
if ByteCount
0 ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
rc
then String -> IO Int
forall a. String -> IO a
ioErrorEOF String
_fdPread
else 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
rc)
_fdPread :: String
_fdPread :: String
_fdPread = String
"System.Posix.IO.ByteString.fdPread"
{-# NOINLINE _fdPread #-}
fdPreads
:: (ByteCount -> a -> Maybe a)
-> a
-> Fd
-> ByteCount
-> FileOffset
-> IO BS.ByteString
fdPreads :: forall a.
(ByteCount -> a -> Maybe a)
-> a -> Fd -> ByteCount -> COff -> IO ByteString
fdPreads ByteCount -> a -> Maybe a
f a
z0 Fd
fd ByteCount
n0 COff
offset
| ByteCount
n0 ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise = Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
n0) (a -> ByteCount -> ByteCount -> Ptr Word8 -> IO Int
forall {b}.
Num b =>
a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
z0 ByteCount
0 ByteCount
n0)
where
go :: a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
_ ByteCount
len ByteCount
n Ptr Word8
buf | ByteCount
len ByteCount -> Bool -> Bool
forall a b. a -> b -> b
`seq` ByteCount
n ByteCount -> Bool -> Bool
forall a b. a -> b -> b
`seq` Ptr Word8
buf Ptr Word8 -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = IO b
forall a. HasCallStack => a
undefined
go a
z ByteCount
len ByteCount
n Ptr Word8
buf = do
ByteCount
rc <- Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPreadBuf Fd
fd Ptr Word8
buf ByteCount
n (COff
offset COff -> COff -> COff
forall a. Num a => a -> a -> a
+ ByteCount -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len)
let len' :: ByteCount
len' = ByteCount
len ByteCount -> ByteCount -> ByteCount
forall a. Num a => a -> a -> a
+ ByteCount
rc
case ByteCount
rc of
ByteCount
_ | ByteCount
rc ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
0 -> String -> IO b
forall a. String -> IO a
ioErrorEOF String
_fdPreads
| ByteCount
rc ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== ByteCount
n -> b -> IO b
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len')
| Bool
otherwise ->
case ByteCount -> a -> Maybe a
f ByteCount
len' a
z of
Maybe a
Nothing -> b -> IO b
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len')
Just a
z' ->
a -> ByteCount -> ByteCount -> Ptr Word8 -> IO b
go a
z' ByteCount
len' (ByteCount
n ByteCount -> ByteCount -> ByteCount
forall a. Num a => a -> a -> a
- ByteCount
rc) (Ptr Word8
buf Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
rc)
_fdPreads :: String
_fdPreads :: String
_fdPreads = String
"System.Posix.IO.ByteString.fdPreads"
{-# NOINLINE _fdPreads #-}
foreign import ccall safe "write"
c_safe_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
fdWriteBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> IO ByteCount
fdWriteBuf :: Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdWriteBuf Fd
fd Ptr Word8
buf ByteCount
nbytes
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdWriteBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_write
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
_fdWriteBuf :: String
_fdWriteBuf :: String
_fdWriteBuf = String
"System.Posix.IO.ByteString.fdWriteBuf"
{-# NOINLINE _fdWriteBuf #-}
tryFdWriteBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> IO (Either C.Errno ByteCount)
tryFdWriteBuf :: Fd -> Ptr Word8 -> ByteCount -> IO (Either Errno ByteCount)
tryFdWriteBuf Fd
fd Ptr Word8
buf ByteCount
nbytes
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_write
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
fdWrite
:: Fd
-> BS.ByteString
-> IO ByteCount
fdWrite :: Fd -> ByteString -> IO ByteCount
fdWrite Fd
fd ByteString
s =
ByteString -> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
s ((CStringLen -> IO ByteCount) -> IO ByteCount)
-> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf,Int
len) -> do
Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdWriteBuf Fd
fd (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
buf) (Int -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
fdWrites
:: Fd
-> [BS.ByteString]
-> IO (ByteCount, ByteCount, [BS.ByteString])
fdWrites :: Fd -> [ByteString] -> IO (ByteCount, ByteCount, [ByteString])
fdWrites Fd
fd = ByteCount
-> [ByteString] -> IO (ByteCount, ByteCount, [ByteString])
go ByteCount
0
where
go :: ByteCount
-> [ByteString] -> IO (ByteCount, ByteCount, [ByteString])
go ByteCount
acc [] = (ByteCount, ByteCount, [ByteString])
-> IO (ByteCount, ByteCount, [ByteString])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount
acc, ByteCount
0, [])
go ByteCount
acc ccs :: [ByteString]
ccs@(ByteString
c:[ByteString]
cs) =
Fd -> ByteString -> IO ByteCount
fdWrite Fd
fd ByteString
c IO ByteCount
-> (ByteCount -> IO (ByteCount, ByteCount, [ByteString]))
-> IO (ByteCount, ByteCount, [ByteString])
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ByteCount
rc ->
let acc' :: ByteCount
acc' = ByteCount
accByteCount -> ByteCount -> ByteCount
forall a. Num a => a -> a -> a
+ByteCount
rc in ByteCount
acc' ByteCount
-> IO (ByteCount, ByteCount, [ByteString])
-> IO (ByteCount, ByteCount, [ByteString])
forall a b. a -> b -> b
`seq`
if ByteCount
rc ByteCount -> ByteCount -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
c)
then ByteCount
-> [ByteString] -> IO (ByteCount, ByteCount, [ByteString])
go ByteCount
acc' [ByteString]
cs
else (ByteCount, ByteCount, [ByteString])
-> IO (ByteCount, ByteCount, [ByteString])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount
acc', ByteCount
rc, [ByteString]
ccs)
foreign import ccall safe "writev"
c_safe_writev :: CInt -> Ptr CIovec -> CInt -> IO CSsize
fdWritevBuf
:: Fd
-> Ptr CIovec
-> Int
-> IO ByteCount
fdWritevBuf :: Fd -> Ptr CIovec -> Int -> IO ByteCount
fdWritevBuf Fd
fd Ptr CIovec
bufs Int
len
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdWritevBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_writev (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
_fdWritevBuf :: String
_fdWritevBuf :: String
_fdWritevBuf = String
"System.Posix.IO.ByteString.fdWritevBuf"
{-# NOINLINE _fdWritevBuf #-}
tryFdWritevBuf
:: Fd
-> Ptr CIovec
-> Int
-> IO (Either C.Errno ByteCount)
tryFdWritevBuf :: Fd -> Ptr CIovec -> Int -> IO (Either Errno ByteCount)
tryFdWritevBuf Fd
fd Ptr CIovec
bufs Int
len
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_writev (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
fdWritev
:: Fd
-> [BS.ByteString]
-> IO ByteCount
fdWritev :: Fd -> [ByteString] -> IO ByteCount
fdWritev Fd
fd [ByteString]
cs = do
ByteCount
rc <- [CIovec] -> (Int -> Ptr CIovec -> IO ByteCount) -> IO ByteCount
forall a b. Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
FMA.withArrayLen ((ByteString -> CIovec) -> [ByteString] -> [CIovec]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> CIovec
unsafeByteString2CIovec [ByteString]
cs) ((Int -> Ptr CIovec -> IO ByteCount) -> IO ByteCount)
-> (Int -> Ptr CIovec -> IO ByteCount) -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ \Int
len Ptr CIovec
iovs ->
Fd -> Ptr CIovec -> Int -> IO ByteCount
fdWritevBuf Fd
fd Ptr CIovec
iovs (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
(ByteString -> IO ()) -> [ByteString] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ByteString -> IO ()
touchByteString [ByteString]
cs
ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
rc
foreign import ccall safe "pwrite"
c_safe_pwrite :: CInt -> Ptr Word8 -> CSize -> COff -> IO CSsize
fdPwriteBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> FileOffset
-> IO ByteCount
fdPwriteBuf :: Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPwriteBuf Fd
fd Ptr Word8
buf ByteCount
nbytes COff
offset
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = ByteCount -> IO ByteCount
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
(CSsize -> ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral
(IO CSsize -> IO ByteCount) -> IO CSsize -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ String -> IO CSsize -> IO CSsize
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdPwriteBuf
(IO CSsize -> IO CSsize) -> IO CSsize -> IO CSsize
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pwrite
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(COff -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
offset)
_fdPwriteBuf :: String
_fdPwriteBuf :: String
_fdPwriteBuf = String
"System.Posix.IO.ByteString.fdPwriteBuf"
{-# NOINLINE _fdPwriteBuf #-}
tryFdPwriteBuf
:: Fd
-> Ptr Word8
-> ByteCount
-> FileOffset
-> IO (Either C.Errno ByteCount)
tryFdPwriteBuf :: Fd -> Ptr Word8 -> ByteCount -> COff -> IO (Either Errno ByteCount)
tryFdPwriteBuf Fd
fd Ptr Word8
buf ByteCount
nbytes COff
offset
| ByteCount
nbytes ByteCount -> ByteCount -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = Either Errno ByteCount -> IO (Either Errno ByteCount)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteCount -> Either Errno ByteCount
forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
(Either Errno CSsize -> Either Errno ByteCount)
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CSsize -> ByteCount)
-> Either Errno CSsize -> Either Errno ByteCount
forall b c d. (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right CSsize -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
(IO (Either Errno CSsize) -> IO (Either Errno ByteCount))
-> IO (Either Errno CSsize) -> IO (Either Errno ByteCount)
forall a b. (a -> b) -> a -> b
$ IO CSsize -> IO (Either Errno CSsize)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
(IO CSsize -> IO (Either Errno CSsize))
-> IO CSsize -> IO (Either Errno CSsize)
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pwrite
(Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(Ptr Word8 -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(ByteCount -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(COff -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
offset)
fdPwrite
:: Fd
-> BS.ByteString
-> FileOffset
-> IO ByteCount
fdPwrite :: Fd -> ByteString -> COff -> IO ByteCount
fdPwrite Fd
fd ByteString
s COff
offset =
ByteString -> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
s ((CStringLen -> IO ByteCount) -> IO ByteCount)
-> (CStringLen -> IO ByteCount) -> IO ByteCount
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf,Int
len) -> do
Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPwriteBuf Fd
fd (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
buf) (Int -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) COff
offset
mode2Int :: SeekMode -> CInt
mode2Int :: SeekMode -> CInt
mode2Int SeekMode
AbsoluteSeek = (CInt
0)
{-# LINE 782 "src/System/Posix/IO/ByteString/Ext.hsc" #-}
mode2Int RelativeSeek = (1)
{-# LINE 783 "src/System/Posix/IO/ByteString/Ext.hsc" #-}
mode2Int SeekFromEnd = (2)
{-# LINE 784 "src/System/Posix/IO/ByteString/Ext.hsc" #-}
fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffset
fdSeek :: Fd -> SeekMode -> COff -> IO COff
fdSeek Fd
fd SeekMode
mode COff
off =
String -> IO COff -> IO COff
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1 String
"fdSeek"
(IO COff -> IO COff) -> IO COff -> IO COff
forall a b. (a -> b) -> a -> b
$ CInt -> COff -> CInt -> IO COff
Base.c_lseek (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) COff
off (SeekMode -> CInt
mode2Int SeekMode
mode)
tryFdSeek :: Fd -> SeekMode -> FileOffset -> IO (Either C.Errno FileOffset)
tryFdSeek :: Fd -> SeekMode -> COff -> IO (Either Errno COff)
tryFdSeek Fd
fd SeekMode
mode COff
off =
IO COff -> IO (Either Errno COff)
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1
(IO COff -> IO (Either Errno COff))
-> IO COff -> IO (Either Errno COff)
forall a b. (a -> b) -> a -> b
$ CInt -> COff -> CInt -> IO COff
Base.c_lseek (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) COff
off (SeekMode -> CInt
mode2Int SeekMode
mode)