Read data from an Fd and convert it to a ByteString.
Throws an exception if this is an invalid descriptor, or EOF has
been reached. This is essentially equivalent to fdReadBuf; the
differences are that we allocate a byte buffer for the ByteString,
and that we detect EOF and throw an IOError.
Moduleunix-bytestring-0.4.0.1Haskell2010
System.Posix.IO.ByteString.Ext
Provides a strict-ByteString file-descriptor based I/O API wrapping the standard C implementations of the functions specified by the ISO/IEC 9945-1:1990 (``POSIX.1'') and X/Open Portability Guide Issue 4, Version 2 (``XPG4.2'') specifications.
The API was originally designed loosely after the String based
API in System.Posix.IO, but significantly extending that API.
The `unix-2.8.0.0` package added a subset of our original API
in System.Posix.IO.ByteString, so as of version 0.4.0 this
module has been renamed in order to avoid conflicts.
- 22 values
- Packageunix-bytestring-0.4.0.1
- Exports22
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceExt.hsc
I/O with file descriptors
0 declarationsReading
The POSIX.1 read(2) syscall
fdReadBuf Read data from an Fd into memory. This is exactly equivalent
to the POSIX.1 read(2) system call, except that we return 0
bytes read if the ByteCount argument is less than or equal to
zero (instead of throwing an errno exception).
N.B., this behavior is different from the version in unix-2.4.2.0
which only checks for equality to zero. If there are any errors,
then they are thrown as IOError exceptions.
Since: 0.3.0
tryFdReadBuf fdReads Read data from an Fd and convert it to a ByteString. Throws an exception if this is an invalid descriptor, or EOF has been reached.
This version takes a kind of stateful predicate for whether and
how long to keep retrying. Assume the function is called as
fdReads f z0 fd n0. We will attempt to read n0 bytes from
fd. If we fall short, then we will call f len z where len
is the total number of bytes read so far and z is the current
state (initially z0). If it returns Nothing then we will
give up and return the current buffer; otherwise we will retry
with the new state, continuing from where we left off.
For example, to define a function that tries up to n times,
we can use:
fdReadUptoNTimes :: Int -> Fd -> ByteCount -> IO ByteString
fdReadUptoNTimes n0 = fdReads retry n0
where
retry _ 0 = Nothing
retry _ n = Just $! n-1The benefit of doing this instead of the naive approach of calling fdRead repeatedly is that we only need to allocate one byte buffer, and trim it once at the end--- whereas the naive approach would allocate a buffer, trim it to the number of bytes read, and then concatenate with the previous one (another allocation, plus copying everything over) for each time around the loop.
Since: 0.2.1
The XPG4.2 readv(2) syscall
fdReadvBuf Read data from an Fd and scatter it into memory. This is
exactly equivalent to the XPG4.2 readv(2) system call, except
that we return 0 bytes read if the Int argument is less than
or equal to zero (instead of throwing an eINVAL exception).
If there are any errors, then they are thrown as IOError
exceptions.
TODO: better documentation.
Since: 0.3.0
tryFdReadvBuf Read data from an Fd and scatter it into memory. This is a variation of fdReadvBuf which returns errors with an Either instead of throwing exceptions.
Since: 0.3.3
The XPG4.2 pread(2) syscall
fdPread :: Fd-> ByteCountHow many bytes to try to read.
-> FileOffsetWhere to read the data from.
-> IO ByteStringThe bytes read.
Read data from a specified position in the Fd and convert
it to a ByteString, without altering the position stored
in the Fd. Throws an exception if this is an invalid descriptor,
or EOF has been reached. This is essentially equivalent to
fdPreadBuf; the differences are that we allocate a byte buffer
for the ByteString, and that we detect EOF and throw an
IOError.
Since: 0.3.0
fdPreadBuf Read data from a specified position in the Fd into memory,
without altering the position stored in the Fd. This is exactly
equivalent to the XPG4.2 pread(2) system call, except that we
return 0 bytes read if the Int argument is less than or equal
to zero (instead of throwing an errno exception). If there are
any errors, then they are thrown as IOError exceptions.
Since: 0.3.0
tryFdPreadBuf Read data from a specified position in the Fd into memory,
without altering the position stored in the Fd. This is a
variation of fdPreadBuf which returns errors with an Either
instead of throwing exceptions.
Since: 0.3.3
fdPreads :: (ByteCount -> a -> Maybe a)A stateful predicate for retrying.
-> aAn initial state for the predicate.
-> Fd-> ByteCountHow many bytes to try to read.
-> FileOffsetWhere to read the data from.
-> IO ByteStringThe bytes read.
Read data from a specified position in the Fd and convert
it to a ByteString, without altering the position stored
in the Fd. Throws an exception if this is an invalid descriptor,
or EOF has been reached. This is a fdPreadBuf based version
of fdReads; see those functions for more details.
Since: 0.3.1
Writing
The POSIX.1 write(2) syscall
fdWrite :: Fd-> ByteStringThe string to write.
-> IO ByteCountHow many bytes were actually written.
Write a ByteString to an Fd. The return value is the
total number of bytes actually written. This is exactly equivalent
to fdWriteBuf; we just convert the ByteString into its
underlying Ptr Word8 and ByteCount components for passing
to fdWriteBuf.
fdWriteBuf Write data from memory to an Fd. This is exactly equivalent
to the POSIX.1 write(2) system call, except that we return 0
bytes written if the ByteCount argument is less than or equal
to zero (instead of throwing an errno exception). N.B., this
behavior is different from the version in unix-2.4.2.0 which
doesn't check the byte count. If there are any errors, then they
are thrown as IOError exceptions.
Since: 0.3.0
tryFdWriteBuf Write data from memory to an Fd. This is a variation of fdWriteBuf which returns errors with an Either instead of throwing exceptions.
Since: 0.3.3
fdWrites :: Fd-> [ByteString]The strings to write.
-> IO (ByteCount, ByteCount, [ByteString])The total number of bytes written, the number of bytes written from the first of the remaining strings, the remaining (unwritten) strings.
Write a sequence of ByteStrings to an Fd. The return value is a triple of: the total number of bytes written, the number of bytes written from the first of the remaining strings, and the remaining (unwritten) strings. We return this triple instead of a pair adjusting the head of the remaining strings (i.e., removing the bytes already written) in case there is some semantic significance to the way the input is split into chunks.
This version consumes the list lazily and will call fdWrite
once for each ByteString, thus making O(n) system calls.
This laziness allows the early parts of the list to be garbage
collected and prevents needing to hold the whole list of
ByteStrings in memory at once. Compare against fdWritev.
The XPG4.2 writev(2) syscall
fdWritev :: Fd-> [ByteString]The strings to write.
-> IO ByteCountHow many bytes were actually written.
Write a sequence of ByteStrings to an Fd. The return
value is the total number of bytes written. Unfortunately the
writev(2) system call does not provide enough information to
return the triple that fdWrites does.
This version will force the spine of the list, converting each
ByteString into an iovec (see CIovec), and then call
fdWritevBuf. This means we only make one system call, which
reduces the overhead of performing context switches. But it also
means that we must store the whole list of ByteStrings in
memory at once, and that we must perform some allocation and
conversion. Compare against fdWrites.
fdWritevBuf Write data from memory to an Fd. This is exactly equivalent
to the XPG4.2 writev(2) system call, except that we return 0
bytes written if the Int argument is less than or equal to
zero (instead of throwing an eINVAL exception). If there are
any errors, then they are thrown as IOError exceptions.
TODO: better documentation.
Since: 0.3.0
tryFdWritevBuf Write data from memory to an Fd. This is a variation of fdWritevBuf which returns errors with an Either instead of throwing exceptions.
Since: 0.3.3
The XPG4.2 pwrite(2) syscall
fdPwrite :: Fd-> ByteStringThe string to write.
-> FileOffsetWhere to write the data to.
-> IO ByteCountHow many bytes were actually written.
Write data from memory to a specified position in the Fd,
but without altering the position stored in the Fd. This is
exactly equivalent to fdPwriteBuf; we just convert the
ByteString into its underlying Ptr Word8 and ByteCount
components for passing to fdPwriteBuf.
Since: 0.3.0
fdPwriteBuf Write data from memory to a specified position in the Fd,
but without altering the position stored in the Fd. This is
exactly equivalent to the XPG4.2 pwrite(2) system call, except
that we return 0 bytes written if the ByteCount argument is
less than or equal to zero (instead of throwing an errno exception).
If there are any errors, then they are thrown as IOError
exceptions.
Since: 0.3.0
tryFdPwriteBuf Write data from memory to a specified position in the Fd,
but without altering the position stored in the Fd. This is a
variation of fdPwriteBuf which returns errors with an Either
instead of throwing exceptions.
Since: 0.3.3
Seeking
These functions are not ByteString related, but are provided here for API completeness.
The POSIX.1 lseek(2) syscall
Repositions the offset of the file descriptor according to the
offset and the seeking mode. This is exactly equivalent to the
POSIX.1 lseek(2) system call. If there are any errors, then
they are thrown as IOError exceptions.
This is the same as System.Posix.IO.fdSeek in unix-2.6.0.1,
but provided here for consistency.
Since: 0.3.5
Repositions the offset of the file descriptor according to the
offset and the seeking mode. This is a variation of fdSeek
which returns errors with an Either instead of throwing
exceptions.
Since: 0.3.5