Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
file-descriptor.hh File Reference

File descriptor operations for almost arbitrary file descriptors. More...

#include "nix/util/error.hh"
#include "nix/util/result.hh"
#include "nix/util/system-error.hh"
#include <sys/types.h>
#include <span>
#include <cstddef>
#include <type_traits>

Go to the source code of this file.

Classes

struct  nix::DrainFdSinkOpts
struct  nix::DrainFdOpts
class  nix::AutoCloseFD
class  nix::Pipe
struct  nix::unix::SelfPipe

Typedefs

using nix::Descriptor

Functions

std::string nix::readFile (Descriptor fd)
size_t nix::read (Descriptor fd, std::span< std::byte > buffer)
size_t nix::write (Descriptor fd, std::span< const std::byte > buffer, bool allowInterrupts)
std::make_unsigned_t< off_t > nix::getFileSize (Descriptor fd)
size_t nix::readOffset (Descriptor fd, off_t offset, std::span< std::byte > buffer)
void nix::copyFdRange (Descriptor fd, off_t offset, size_t nbytes, Sink &sink, bool tryCoW)
void nix::readFull (Descriptor fd, char *buf, size_t count)
void nix::writeFull (Descriptor fd, std::string_view s, bool allowInterrupts)
std::string nix::readLine (Descriptor fd, bool eofOk, char terminator)
void nix::writeLine (Descriptor fd, std::string s)
void nix::syncDescriptor (Descriptor fd)
std::string nix::drainFD (Descriptor fd, DrainFdOpts opts)
void nix::drainFD (Descriptor fd, Sink &sink, DrainFdSinkOpts opts)
Descriptor nix::getStandardInput ()
Descriptor nix::getStandardOutput ()
Descriptor nix::getStandardError ()
AutoCloseFD nix::dupDescriptor (Descriptor fd)
AutoCloseFD nix::openNullFD ()
void nix::closeOnExec (Descriptor fd)
void nix::unix::closeExtraFDs ()
void nix::unix::closeExtraFDs (std::span< const Descriptor > keepExtra)
 nix::MakeError (EndOfFile, Error)
Result< off_t, NativeError > nix::seek (Descriptor fd, off_t offset, int whence)

Variables

const Descriptor nix::INVALID_DESCRIPTOR

Detailed Description

File descriptor operations for almost arbitrary file descriptors.

More specialized file-system-specific operations are in file-system-at.hh.

Typedef Documentation

◆ Descriptor

Initial value:
int

Operating System capability

Function Documentation

◆ closeExtraFDs() [1/2]

void nix::unix::closeExtraFDs ( )

Close all file descriptors except stdio fds (ie 0, 1, 2). Good practice in child processes.

◆ closeExtraFDs() [2/2]

void nix::unix::closeExtraFDs ( std::span< const Descriptor > keepExtra)

Close all file descriptors except stdio fds (ie 0, 1, 2) and keepExtra. Good practice in child processes.

◆ closeOnExec()

void nix::closeOnExec ( Descriptor fd)

Prevent the given descriptor from being passed on to child processes.

On Unix this sets FD_CLOEXEC, so the descriptor is closed on exec. On Windows there is no exec; inheritance is a per-handle property, so this clears HANDLE_FLAG_INHERIT instead. A Windows handle is not inheritable unless it was created that way, so the Windows call is usually redundant — it exists so that callers do not have to know which platform they are on.

◆ copyFdRange()

void nix::copyFdRange ( Descriptor fd,
off_t offset,
size_t nbytes,
Sink & sink,
bool tryCoW = false )

Read nbytes starting at offset from a seekable file into a sink.

Parameters
tryCoWUsed as a hint to use optimised file copying like copy_file_range.
Exceptions
SystemErrorif fd is not seekable or any operation fails
Interruptedif the operation was interrupted
EndOfFileif an EOF was reached before reading nbytes

◆ drainFD() [1/2]

std::string nix::drainFD ( Descriptor fd,
DrainFdOpts opts = {} )

Read a file descriptor until EOF occurs.

Parameters
fdThe file descriptor to drain
optsOptions for the drain operation

◆ drainFD() [2/2]

void nix::drainFD ( Descriptor fd,
Sink & sink,
DrainFdSinkOpts opts = {} )

Read a file descriptor until EOF occurs, writing to a sink.

Parameters
fdThe file descriptor to drain
sinkThe sink to write data to
optsOptions for the drain operation

◆ dupDescriptor()

AutoCloseFD nix::dupDescriptor ( Descriptor fd)

Duplicate a file descriptor.

Returns a new file descriptor that refers to the same open file description as the original.

◆ getFileSize()

std::make_unsigned_t< off_t > nix::getFileSize ( Descriptor fd)

Get the size of a file.

Thin wrapper around fstat (Unix) or GetFileSizeEx (Windows).

Parameters
fdThe file descriptor
Returns
The file size
Exceptions
SystemErroron failure

◆ getStandardError()

Descriptor nix::getStandardError ( )
inline

◆ getStandardInput()

Descriptor nix::getStandardInput ( )
inline

◆ getStandardOutput()

Descriptor nix::getStandardOutput ( )
inline

◆ openNullFD()

AutoCloseFD nix::openNullFD ( )

Open a /dev/null file descriptor as read/write.

Exceptions
SystemErroron error.

◆ read()

size_t nix::read ( Descriptor fd,
std::span< std::byte > buffer )

Platform-specific read into a buffer.

Thin wrapper around ::read (Unix) or ReadFile (Windows). Handles EINTR on Unix. Treats ERROR_BROKEN_PIPE as EOF on Windows.

Parameters
fdThe file descriptor to read from
bufferThe buffer to read into
Returns
The number of bytes actually read (0 indicates EOF)
Exceptions
SystemErroron failure

◆ readFile()

std::string nix::readFile ( Descriptor fd)

Read the contents of a resource into a string.

◆ readFull()

void nix::readFull ( Descriptor fd,
char * buf,
size_t count )

Wrappers around read()/write() that read/write exactly the requested number of bytes.

◆ readLine()

std::string nix::readLine ( Descriptor fd,
bool eofOk = false,
char terminator = '\n' )

Read a line from an unbuffered file descriptor. See BufferedSource::readLine for a buffered variant.

Parameters
fdThe file descriptor to read from
eofOkIf true, return an unterminated line if EOF is reached. (e.g. the empty string)
terminatorThe chartacter that ends the line
Returns
A line of text ending in \n, or a string without \n if eofOk is true and EOF is reached.

◆ readOffset()

size_t nix::readOffset ( Descriptor fd,
off_t offset,
std::span< std::byte > buffer )

Platform-specific positioned read into a buffer.

Thin wrapper around pread (Unix) or ReadFile with OVERLAPPED (Windows). Does NOT handle EINTR on Unix - caller must catch and retry if needed.

Parameters
fdThe file descriptor to read from (must be seekable)
offsetThe offset to read from
bufferThe buffer to read into
Returns
The number of bytes actually read (0 indicates EOF)
Exceptions
SystemErroron failure

◆ seek()

Result< off_t, NativeError > nix::seek ( Descriptor fd,
off_t offset,
int whence )

Like POSIX lseek, on every platform.

Parameters
whenceWhence [from where] to apply the offset. This position plus the offset is where to seek in the file. Must be one of SEEK_SET, SEEK_CUR or SEEK_END.
Returns
The new offset, or on failure the native error code, which NativeSysError can be constructed from.

◆ syncDescriptor()

void nix::syncDescriptor ( Descriptor fd)

Perform a blocking fsync operation on a file descriptor.

◆ write()

size_t nix::write ( Descriptor fd,
std::span< const std::byte > buffer,
bool allowInterrupts )

Platform-specific write from a buffer.

Thin wrapper around ::write (Unix) or WriteFile (Windows). Handles EINTR on Unix.

Parameters
fdThe file descriptor to write to
bufferThe buffer to write from
Returns
The number of bytes actually written
Exceptions
SystemErroron failure

◆ writeLine()

void nix::writeLine ( Descriptor fd,
std::string s )

Write a line to a file descriptor.

Variable Documentation

◆ INVALID_DESCRIPTOR

const Descriptor nix::INVALID_DESCRIPTOR
Initial value:
=
-1