{-# LINE 1 "src/System/Posix/IO/ByteString.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# OPTIONS_GHC -fwarn-tabs #-}
module System.Posix.IO.ByteString
(
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 =
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 forall a. Maybe a
Nothing 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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdReadBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_read
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_read
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise =
Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes) 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 forall a. Eq a => a -> a -> Bool
== ByteCount
rc
then forall a. String -> IO a
ioErrorEOF String
_fdRead
else forall (m :: * -> *) a. Monad m => a -> m a
return (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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise = Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
n0) (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 seq :: forall a b. a -> b -> b
`seq` ByteCount
n seq :: forall a b. a -> b -> b
`seq` Ptr Word8
buf seq :: forall a b. a -> b -> b
`seq` Bool
False = 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 forall a. Num a => a -> a -> a
+ ByteCount
rc
case ByteCount
rc of
ByteCount
_ | ByteCount
rc forall a. Eq a => a -> a -> Bool
== ByteCount
0 -> forall a. String -> IO a
ioErrorEOF String
_fdReads
| ByteCount
rc forall a. Eq a => a -> a -> Bool
== ByteCount
n -> forall (m :: * -> *) a. Monad m => a -> m a
return (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 -> forall (m :: * -> *) a. Monad m => a -> m a
return (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 forall a. Num a => a -> a -> a
- ByteCount
rc) (Ptr Word8
buf forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdReadvBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_readv (forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_readv (forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdPreadBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pread
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
Ptr Word8
buf
(forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pread
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
Ptr Word8
buf
(forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise =
Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes) 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 forall a. Eq a => a -> a -> Bool
== ByteCount
rc
then forall a. String -> IO a
ioErrorEOF String
_fdPread
else forall (m :: * -> *) a. Monad m => a -> m a
return (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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
| Bool
otherwise = Int -> (Ptr Word8 -> IO Int) -> IO ByteString
BSI.createAndTrim (forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
n0) (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 seq :: forall a b. a -> b -> b
`seq` ByteCount
n seq :: forall a b. a -> b -> b
`seq` Ptr Word8
buf seq :: forall a b. a -> b -> b
`seq` Bool
False = 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 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
len)
let len' :: ByteCount
len' = ByteCount
len forall a. Num a => a -> a -> a
+ ByteCount
rc
case ByteCount
rc of
ByteCount
_ | ByteCount
rc forall a. Eq a => a -> a -> Bool
== ByteCount
0 -> forall a. String -> IO a
ioErrorEOF String
_fdPreads
| ByteCount
rc forall a. Eq a => a -> a -> Bool
== ByteCount
n -> forall (m :: * -> *) a. Monad m => a -> m a
return (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 -> forall (m :: * -> *) a. Monad m => a -> m a
return (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 forall a. Num a => a -> a -> a
- ByteCount
rc) (Ptr Word8
buf forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdWriteBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_write
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CChar -> ByteCount -> IO CSsize
c_safe_write
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(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 =
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
s forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf,Int
len) -> do
Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdWriteBuf Fd
fd (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
buf) (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 [] = 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ByteCount
rc ->
let acc' :: ByteCount
acc' = ByteCount
accforall a. Num a => a -> a -> a
+ByteCount
rc in ByteCount
acc' seq :: forall a b. a -> b -> b
`seq`
if ByteCount
rc forall a. Eq a => a -> a -> Bool
== 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 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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdWritevBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_writev (forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CIovec -> CInt -> IO CSsize
c_safe_writev (forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) Ptr CIovec
bufs (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 <- forall a b. Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
FMA.withArrayLen (forall a b. (a -> b) -> [a] -> [b]
map ByteString -> CIovec
unsafeByteString2CIovec [ByteString]
cs) forall a b. (a -> b) -> a -> b
$ \Int
len Ptr CIovec
iovs ->
Fd -> Ptr CIovec -> Int -> IO ByteCount
fdWritevBuf Fd
fd Ptr CIovec
iovs (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ByteString -> IO ()
touchByteString [ByteString]
cs
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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ByteCount
0
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1Retry String
_fdPwriteBuf
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pwrite
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(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 forall a. Ord a => a -> a -> Bool
<= ByteCount
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ByteCount
0)
| Bool
otherwise =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either d b) (Either d c)
right forall a b. (Integral a, Num b) => a -> b
fromIntegral)
forall a b. (a -> b) -> a -> b
$ forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1Retry
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Word8 -> ByteCount -> COff -> IO CSsize
c_safe_pwrite
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd)
(forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
buf)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
nbytes)
(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 =
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
s forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf,Int
len) -> do
Fd -> Ptr Word8 -> ByteCount -> COff -> IO ByteCount
fdPwriteBuf Fd
fd (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
buf) (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 761 "src/System/Posix/IO/ByteString.hsc" #-}
mode2Int RelativeSeek = (1)
{-# LINE 762 "src/System/Posix/IO/ByteString.hsc" #-}
mode2Int SeekFromEnd = (2)
{-# LINE 763 "src/System/Posix/IO/ByteString.hsc" #-}
fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffset
fdSeek :: Fd -> SeekMode -> COff -> IO COff
fdSeek Fd
fd SeekMode
mode COff
off =
forall a. (Eq a, Num a) => String -> IO a -> IO a
C.throwErrnoIfMinus1 String
"fdSeek"
forall a b. (a -> b) -> a -> b
$ CInt -> COff -> CInt -> IO COff
Base.c_lseek (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 =
forall a. (Eq a, Num a) => IO a -> IO (Either Errno a)
eitherErrnoIfMinus1
forall a b. (a -> b) -> a -> b
$ CInt -> COff -> CInt -> IO COff
Base.c_lseek (forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd) COff
off (SeekMode -> CInt
mode2Int SeekMode
mode)