A straightforward wrapping of unix APIs for Haskell.
I wrote this package because state of posix API support in Haskell is somewhat unsatisfactory in a few ways:
- There are many syscalls not covered by the
unixpackage - The
unixpackage needlessly renames syscalls, making them harder to find for people familiar with the posix API. For example,lstathas been renamed togetSymbolicLinkStatus - There are a large number of packages that fill in various holes, but you have to scrape them together.
The goal of this package then, is to provide a one-stop-shop for unix API bindings, with a straightforward mapping to the underlying C API. At the time of writing there are many syscalls missing, but in theory most things belong here. Patches welcome; see the section below re: how to add a syscall.
- Function names are the same as the underlying system calls, so there is no guesswork.
- Wrappers automatically retry on
EINTRwhere appropriate (this is safe for most system calls, but not all, e.g.closeon Linux). - The basic versions of the syscalls return
IO (Either Errno a), rather than raising an exception.- Each system call also has a variant suffixed with
Exnthat throws exceptions. - We define a type alias
type EIO a = IO (Either Errno a)for convenience.
- Each system call also has a variant suffixed with
- For flags, we add newtype wrappers, and they can be combined with
their
SemiGroupinstances, e.g.open "hello.txt" (o_CREAT <> o_RDWR) 0o600
- Flags are named the same as the C constant, but with the first character lower-cased.
- For functions that take a buffer, we instead accept a
ByteStringwhen the buffer is read, or return one when it is written, e.g.read :: Fd -> Int -> EIO BS.ByteStringwrite :: Fd -> BS.ByteString -> EIO CSsize
- We also provide variants suffixed with
Buf, that take a pointer and size:readBuf :: Fd -> Ptr Word8 -> CSize -> EIO CSsize
- For functions that take an array of
struct iovecas an input, we provide several variants, e.g. forwritevwe provide:writev :: Fd -> [BS.ByteString] -> EIO CSizewritevBuf :: Fd -> Ptr CIOVec -> CInt -> EIO CSsizewritevVec :: Fd -> SMV.IOVector CIOVec -> EIO CSize(How to support readv is still an open design question).
- We provide a
CStringtype for functions which accept strings as arguments. This type is an instance ofIsString, so you can use string literals if you enableOverloadedStrings, or use thefromStringfunction to convert. The conversion uses utf-8 encoding. - For some calls we also add obvious convenience helpers, e.g.
readFullandwriteFull, which wrapreadandwriteand handle short reads and writes for the caller.
To add a new system call:
- Add the appropriate declaration to
Unix.C, following conventions established by the examples in that file. - Add a wrapper following the general conventions in
Unix.