![]() |
Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
|
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 |
File descriptor operations for almost arbitrary file descriptors.
More specialized file-system-specific operations are in file-system-at.hh.
| using nix::Descriptor |
Operating System capability
| void nix::unix::closeExtraFDs | ( | ) |
Close all file descriptors except stdio fds (ie 0, 1, 2). Good practice in child processes.
| 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.
| 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.
| 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.
| tryCoW | Used as a hint to use optimised file copying like copy_file_range. |
| SystemError | if fd is not seekable or any operation fails |
| Interrupted | if the operation was interrupted |
| EndOfFile | if an EOF was reached before reading nbytes |
| std::string nix::drainFD | ( | Descriptor | fd, |
| DrainFdOpts | opts = {} ) |
Read a file descriptor until EOF occurs.
| fd | The file descriptor to drain |
| opts | Options for the drain operation |
| void nix::drainFD | ( | Descriptor | fd, |
| Sink & | sink, | ||
| DrainFdSinkOpts | opts = {} ) |
Read a file descriptor until EOF occurs, writing to a sink.
| fd | The file descriptor to drain |
| sink | The sink to write data to |
| opts | Options for the drain operation |
| 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.
| std::make_unsigned_t< off_t > nix::getFileSize | ( | Descriptor | fd | ) |
Get the size of a file.
Thin wrapper around fstat (Unix) or GetFileSizeEx (Windows).
| fd | The file descriptor |
| SystemError | on failure |
|
inline |
Get Standard Error
|
inline |
Get Standard Input
|
inline |
Get Standard Output
| AutoCloseFD nix::openNullFD | ( | ) |
Open a /dev/null file descriptor as read/write.
| SystemError | on error. |
| 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.
| fd | The file descriptor to read from |
| buffer | The buffer to read into |
| SystemError | on failure |
| std::string nix::readFile | ( | Descriptor | fd | ) |
Read the contents of a resource into a string.
| void nix::readFull | ( | Descriptor | fd, |
| char * | buf, | ||
| size_t | count ) |
Wrappers around read()/write() that read/write exactly the requested number of bytes.
| 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.
| fd | The file descriptor to read from |
| eofOk | If true, return an unterminated line if EOF is reached. (e.g. the empty string) |
| terminator | The chartacter that ends the line |
| 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.
| fd | The file descriptor to read from (must be seekable) |
| offset | The offset to read from |
| buffer | The buffer to read into |
| SystemError | on failure |
| Result< off_t, NativeError > nix::seek | ( | Descriptor | fd, |
| off_t | offset, | ||
| int | whence ) |
Like POSIX lseek, on every platform.
| whence | Whence [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. |
| void nix::syncDescriptor | ( | Descriptor | fd | ) |
Perform a blocking fsync operation on a file descriptor.
| 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.
| fd | The file descriptor to write to |
| buffer | The buffer to write from |
| SystemError | on failure |
| void nix::writeLine | ( | Descriptor | fd, |
| std::string | s ) |
Write a line to a file descriptor.
| const Descriptor nix::INVALID_DESCRIPTOR |