unix-2.4.0.1: POSIX functionalitySource codeContentsIndex
System.Posix.IO
Portabilitynon-portable (requires POSIX)
Stabilityprovisional
Maintainerlibraries@haskell.org
Contents
Input / Output
Standard file descriptors
Opening and closing files
Reading/writing data
Seeking
File options
Locking
Pipes
Duplicating file descriptors
Converting file descriptors to/from Handles
Description
POSIX IO support. These types and functions correspond to the unix functions open(2), close(2), etc. For more portable functions which are more like fopen(3) and friends from stdio.h, see System.IO.
Synopsis
stdInput :: Fd
stdOutput :: Fd
stdError :: Fd
data OpenMode
= ReadOnly
| WriteOnly
| ReadWrite
data OpenFileFlags = OpenFileFlags {
append :: Bool
exclusive :: Bool
noctty :: Bool
nonBlock :: Bool
trunc :: Bool
}
defaultFileFlags :: OpenFileFlags
openFd :: FilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
createFile :: FilePath -> FileMode -> IO Fd
closeFd :: Fd -> IO ()
fdRead :: Fd -> ByteCount -> IO (String, ByteCount)
fdWrite :: Fd -> String -> IO ByteCount
fdReadBuf :: Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdWriteBuf :: Fd -> Ptr Word8 -> ByteCount -> IO ByteCount
fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffset
data FdOption
= AppendOnWrite
| CloseOnExec
| NonBlockingRead
| SynchronousWrites
queryFdOption :: Fd -> FdOption -> IO Bool
setFdOption :: Fd -> FdOption -> Bool -> IO ()
type FileLock = (LockRequest, SeekMode, FileOffset, FileOffset)
data LockRequest
= ReadLock
| WriteLock
| Unlock
getLock :: Fd -> FileLock -> IO (Maybe (ProcessID, FileLock))
setLock :: Fd -> FileLock -> IO ()
waitToSetLock :: Fd -> FileLock -> IO ()
createPipe :: IO (Fd, Fd)
dup :: Fd -> IO Fd
dupTo :: Fd -> Fd -> IO Fd
handleToFd :: Handle -> IO Fd
fdToHandle :: Fd -> IO Handle
Input / Output
Standard file descriptors
stdInput :: FdSource
stdOutput :: FdSource
stdError :: FdSource
Opening and closing files
data OpenMode Source
Constructors
ReadOnly
WriteOnly
ReadWrite
data OpenFileFlags Source
Correspond to some of the int flags from C's fcntl.h.
Constructors
OpenFileFlags
append :: BoolO_APPEND
exclusive :: BoolO_EXCL
noctty :: BoolO_NOCTTY
nonBlock :: BoolO_NONBLOCK
trunc :: BoolO_TRUNC
defaultFileFlags :: OpenFileFlagsSource
Default values for the OpenFileFlags type. False for each of append, exclusive, noctty, nonBlock, and trunc.
openFdSource
:: FilePath
-> OpenMode
-> Maybe FileModeJust x => creates the file with the given modes, Nothing => the file must exist.
-> OpenFileFlags
-> IO Fd
Open and optionally create this file. See System.Posix.Files for information on how to use the FileMode type.
createFile :: FilePath -> FileMode -> IO FdSource
Create and open this file in WriteOnly mode. A special case of openFd. See System.Posix.Files for information on how to use the FileMode type.
closeFd :: Fd -> IO ()Source
Close this file descriptor. May throw an exception if this is an invalid descriptor.
Reading/writing data
Programmers using the fdRead and fdWrite API should be aware that EAGAIN exceptions may occur for non-blocking IO!
fdReadSource
:: Fd
-> ByteCountHow many bytes to read
-> IO (String, ByteCount)The bytes read, how many bytes were read.
Read data from an Fd and convert it to a String. Throws an exception if this is an invalid descriptor, or EOF has been reached.
fdWrite :: Fd -> String -> IO ByteCountSource
Write a String to an Fd (no character conversion is done, the least-significant 8 bits of each character are written).
fdReadBufSource
:: Fd
-> Ptr Word8Memory in which to put the data
-> ByteCountMaximum number of bytes to read
-> IO ByteCountNumber of bytes read (zero for EOF)
Read data from an Fd into memory. This is exactly equivalent to the POSIX read function.
fdWriteBufSource
:: Fd
-> Ptr Word8Memory containing the data to write
-> ByteCountMaximum number of bytes to write
-> IO ByteCountNumber of bytes written
Write data from memory to an Fd. This is exactly equivalent to the POSIX write function.
Seeking
fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffsetSource
May throw an exception if this is an invalid descriptor.
File options
data FdOption Source
Constructors
AppendOnWriteO_APPEND
CloseOnExecFD_CLOEXEC
NonBlockingReadO_NONBLOCK
SynchronousWritesO_SYNC
queryFdOption :: Fd -> FdOption -> IO BoolSource
May throw an exception if this is an invalid descriptor.
setFdOption :: Fd -> FdOption -> Bool -> IO ()Source
May throw an exception if this is an invalid descriptor.
Locking
type FileLock = (LockRequest, SeekMode, FileOffset, FileOffset)Source
data LockRequest Source
Constructors
ReadLock
WriteLock
Unlock
getLock :: Fd -> FileLock -> IO (Maybe (ProcessID, FileLock))Source
May throw an exception if this is an invalid descriptor.
setLock :: Fd -> FileLock -> IO ()Source
May throw an exception if this is an invalid descriptor.
waitToSetLock :: Fd -> FileLock -> IO ()Source
May throw an exception if this is an invalid descriptor.
Pipes
createPipe :: IO (Fd, Fd)Source
The createPipe function creates a pair of connected file descriptors. The first component is the fd to read from, the second is the write end. Although pipes may be bidirectional, this behaviour is not portable and programmers should use two separate pipes for this purpose. May throw an exception if this is an invalid descriptor.
Duplicating file descriptors
dup :: Fd -> IO FdSource
May throw an exception if this is an invalid descriptor.
dupTo :: Fd -> Fd -> IO FdSource
May throw an exception if this is an invalid descriptor.
Converting file descriptors to/from Handles
handleToFd :: Handle -> IO FdSource
Extracts the Fd from a Handle. This function has the side effect of closing the Handle and flushing its write buffer, if necessary.
fdToHandle :: Fd -> IO HandleSource

Converts an Fd into a Handle that can be used with the standard Haskell IO library (see System.IO).

GHC only: this function has the side effect of putting the Fd into non-blocking mode (O_NONBLOCK) due to the way the standard IO library implements multithreaded I/O.

Produced by Haddock version 2.6.1