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

File system operations relative to directory file descriptors. More...

#include "nix/util/error.hh"
#include "nix/util/file-descriptor.hh"
#include "nix/util/file-system.hh"
#include "nix/util/os-canon-path.hh"
#include "nix/util/result.hh"
#include "nix/util/source-accessor.hh"
#include "nix/util/system-error.hh"
#include <optional>
#include <system_error>

Go to the source code of this file.

Macros

#define NIX_ERR_OPEN_SYMLINK   EMLINK

Functions

OsString nix::readLinkAt (Descriptor dirFd, const OsCanonPath &path)
void nix::createFileSymlinkAt (Descriptor dirFd, const OsCanonPath &path, const OsString &target)
void nix::createDirectorySymlinkAt (Descriptor dirFd, const OsCanonPath &path, const OsString &target)
void nix::createUnknownSymlinkAt (Descriptor dirFd, const OsCanonPath &path, const OsString &target)
Result< AutoCloseFD, NativeError > nix::openDirectoryAt (Descriptor dirFd, const OsCanonPath &path)
Result< AutoCloseFD, NativeError > nix::reopenDirectory (Descriptor dirFd)
Result< AutoCloseFD, NativeError > nix::createDirectoryAt (Descriptor dirFd, const OsCanonPath &path, mode_t mode=0777)
PosixStat nix::fstat (Descriptor fd)
std::optional< PosixStat > nix::maybeFstatat (Descriptor dirFd, const OsCanonPath &path)
PosixStat nix::fstatat (Descriptor dirFd, const OsCanonPath &path)
Result< AutoCloseFD, NativeError > nix::openFileEnsureBeneathNoSymlinks (Descriptor dirFd, const OsCanonPath &path, int flags, mode_t mode=0, std::function< void(AutoCloseFD dirFd, OsCanonPath relPath)> dirFdCallback=nullptr)
std::optional< Result< AutoCloseFD, NativeError > > nix::linux::openat2 (Descriptor dirFd, const char *path, uint64_t flags, uint64_t mode, uint64_t resolve)
void nix::unix::fchmodatTryNoFollow (Descriptor dirFd, const OsCanonPath &path, mode_t mode)

Detailed Description

File system operations relative to directory file descriptors.

This header provides cross-platform wrappers for POSIX *at functions (e.g., symlinkat, mkdirat, readlinkat) that operate relative to a directory file descriptor.

Prefer this to file-system.hh because file descriptor-based file system operations are necessary to avoid TOCTOU issues.

Function Documentation

◆ createDirectoryAt()

Result< AutoCloseFD, NativeError > nix::createDirectoryAt ( Descriptor dirFd,
const OsCanonPath & path,
mode_t mode = 0777 )

Create a directory relative to a directory file descriptor, and (best effort, see below) open it.

Parameters
dirFdDirectory file descriptor
pathRelative path to the new directory
modeFile mode for the new directory. Unix only.
Returns
On success, the file descriptor for the new directory if gotten atomically, otherwise null (see below). On failure, an error code.
Postcondition

A file descriptor is only returned if it was opened atomically with the creation of the directory it refers to. Opening the new directory is best effort: if we cannot guarantee atomicity, we just return null, and a caller that needs the directory opened has to open it itself, knowing that something else could have replaced the directory in the meantime.

Currently, atomically creating and opening is only possible on Windows. Some Unixes, including (it seems) upcoming versions of Linux, could also do this, with O_CREAT | O_DIRECTORY | O_EXCL. For Linux, see https://lore.kernel.org/all/20261024213539.1307600-1-jkoolstra@xs4all.nl/

◆ createDirectorySymlinkAt()

void nix::createDirectorySymlinkAt ( Descriptor dirFd,
const OsCanonPath & path,
const OsString & target )

Create a symlink to a directory relative to a directory file descriptor.

On Windows, creates a directory symlink. On Unix, equivalent to symlinkat.

Parameters
dirFdDirectory file descriptor
pathRelative path for the new symlink
targetThe symlink target (what it points to)
Exceptions
SystemErroron any I/O errors.

◆ createFileSymlinkAt()

void nix::createFileSymlinkAt ( Descriptor dirFd,
const OsCanonPath & path,
const OsString & target )

Create a symlink to a file relative to a directory file descriptor.

On Windows, creates a file symlink. On Unix, equivalent to symlinkat.

Parameters
dirFdDirectory file descriptor
pathRelative path for the new symlink
targetThe symlink target (what it points to)
Exceptions
SystemErroron any I/O errors.

◆ createUnknownSymlinkAt()

void nix::createUnknownSymlinkAt ( Descriptor dirFd,
const OsCanonPath & path,
const OsString & target )

Create a symlink relative to a directory file descriptor, detecting target type.

On Windows, stats the target to determine whether to create a file or directory symlink. Falls back to file symlink if the target does not exist. On Unix, equivalent to symlinkat.

Parameters
dirFdDirectory file descriptor
pathRelative path for the new symlink
targetThe symlink target (what it points to)
Exceptions
SystemErroron any I/O errors.

◆ fchmodatTryNoFollow()

void nix::unix::fchmodatTryNoFollow ( Descriptor dirFd,
const OsCanonPath & path,
mode_t mode )

Try to change the mode of file named by path relative to the parent directory denoted by dirFd.

Note
When on linux without fchmodat2 support and without procfs mounted falls back to fchmodat without AT_SYMLINK_NOFOLLOW, since it's the best we can do without failing.
Precondition
path.empty() is false
Exceptions
SysErrorif any operation fails

◆ fstat()

PosixStat nix::fstat ( Descriptor fd)

Get status of an open file/directory handle.

Parameters
fdFile descriptor/handle
Exceptions
SystemErroron I/O errors.

◆ fstatat()

PosixStat nix::fstatat ( Descriptor dirFd,
const OsCanonPath & path )

Get status of a file relative to a directory file descriptor.

Parameters
dirFdDirectory file descriptor
pathRelative path to stat
Exceptions
SystemErrorif the path does not exist or on other I/O errors.
Precondition
path must be relative (not absolute) and non-empty.

◆ maybeFstatat()

std::optional< PosixStat > nix::maybeFstatat ( Descriptor dirFd,
const OsCanonPath & path )

Get status of a file relative to a directory file descriptor.

Parameters
dirFdDirectory file descriptor
pathRelative path to stat
Returns
nullopt if the path does not exist.
Exceptions
SystemErroron other I/O errors.
Precondition
path must be relative (not absolute) and non-empty.

◆ openat2()

std::optional< Result< AutoCloseFD, NativeError > > nix::linux::openat2 ( Descriptor dirFd,
const char * path,
uint64_t flags,
uint64_t mode,
uint64_t resolve )

Wrapper around Linux's openat2 syscall introduced in Linux 5.6.

See also
https://man7.org/linux/man-pages/man2/openat2.2.html
https://man7.org/linux/man-pages/man2/open_how.2type.html
Parameters
flagsO_* flags
modeMode for O_{CREAT,TMPFILE}
resolveRESOLVE_* flags
Returns
nullopt if openat2 is not supported by the kernel, otherwise the descriptor or the native error code.

◆ openDirectoryAt()

Result< AutoCloseFD, NativeError > nix::openDirectoryAt ( Descriptor dirFd,
const OsCanonPath & path )

Open a directory relative to a directory file descriptor.

Parameters
dirFdDirectory file descriptor
pathRelative path to the directory
Returns
On success, the file descriptor for the directory. On failure, an error code.
Note
Does not follow symlinks - if path is a symlink, will fail to open.

◆ openFileEnsureBeneathNoSymlinks()

Result< AutoCloseFD, NativeError > nix::openFileEnsureBeneathNoSymlinks ( Descriptor dirFd,
const OsCanonPath & path,
int flags,
mode_t mode = 0,
std::function< void(AutoCloseFD dirFd, OsCanonPath relPath)> dirFdCallback = nullptr )

Open a file relative to dirFd, ensuring the path stays beneath dirFd and that no path component is a symlink (with the exception that O_PATH (without O_DIRECTORY) on Unix permits a trailing symlink).

Callers must not pass O_NOFOLLOW on Unix (enforced by assert); this function owns symlink policy and handles the flag internally.

Parameters
dirFdCallbackCallback invoked that gets the ownership of an intermediate directory fd.
Precondition
path.empty() is false
Exceptions
SymlinkNotAllowedif an interior path component is a symlink, or if the final component is a symlink and O_PATH (without O_DIRECTORY) was not passed. With O_PATH (without O_DIRECTORY) on Unix, a trailing symlink is permitted and the caller receives a "path fd" to the symlink itself.
Note
With O_CREAT | O_EXCL, a pre-existing symlink at the final component causes the OS to return EEXIST rather than ELOOP, so SymlinkNotAllowed is not thrown — the caller sees a failure with EEXIST.
Returns
The descriptor, or on non-symlink errors the native error code (e.g. ENOENT, ENOTDIR, EACCES on Unix).

The error will never be ELOOP because that case is translated to a SymlinkNotAllowed throw instead.

Parameters
pathDirectory handle to open relative to
flagspath Relative path (with no .. or . components)
modeO_* flags (must not include O_NOFOLLOW)
dirFdCallbackMode for O_{CREAT,TMPFILE}

◆ readLinkAt()

OsString nix::readLinkAt ( Descriptor dirFd,
const OsCanonPath & path )

Read a symlink relative to a directory file descriptor. On Linux, this also supports reading from O_PATH descriptors with CanonPath::root.

Exceptions
SystemErroron any I/O errors.
Interruptedif interrupted.

◆ reopenDirectory()

Result< AutoCloseFD, NativeError > nix::reopenDirectory ( Descriptor dirFd)

Open a fresh handle to the directory dirFd refers to.

Unlike dupDescriptor, the new handle has its own read position, so it can be read from without disturbing anyone else using dirFd.

Returns
On success, the new handle. On failure, an error code.