Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
file-system.hh File Reference
#include "lix/libutil/async-io.hh"
#include "lix/libutil/box_ptr.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/file-descriptor.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <functional>
#include <optional>

Go to the source code of this file.

Classes

struct  nix::DirEntry
struct  nix::CopyFileFlags
class  nix::AutoDelete
struct  nix::DIRDeleter

Macros

#define DT_UNKNOWN   0
#define DT_REG   1
#define DT_LNK   2
#define DT_DIR   3

Typedefs

typedef std::vector< DirEntrynix::DirEntries
typedef std::unique_ptr< DIR, DIRDeleternix::AutoCloseDir
typedef std::function< bool(const Path &path)> nix::PathFilter

Functions

Path nix::getCwd ()
Path nix::absPath (Path path, std::optional< PathView > dir, bool resolveSymlinks)
Path nix::canonPath (PathView path, bool resolveSymlinks)
Path nix::realPath (Path const &path)
Path nix::tildePath (Path const &path, const std::optional< Path > &home)
void nix::chmodPath (const Path &path, mode_t mode)
Path nix::dirOf (const PathView path)
std::string_view nix::baseNameOf (std::string_view path)
std::string nix::expandTilde (std::string_view path)
bool nix::isInDir (std::string_view path, std::string_view dir)
bool nix::isDirOrInDir (std::string_view path, std::string_view dir)
struct stat nix::stat (const Path &path)
struct stat nix::lstat (const Path &path)
std::optional< struct stat > nix::maybeStat (const Path &path)
std::optional< struct stat > nix::maybeLstat (const Path &path)
bool nix::pathExists (const Path &path)
bool nix::pathAccessible (const Path &path, bool resolveSymlinks)
Path nix::readLink (const Path &path)
bool nix::isLink (const Path &path)
DirEntries nix::readDirectory (const Path &path)
unsigned char nix::getFileType (const Path &path)
std::string nix::readFile (const Path &path)
Generator< Bytes > nix::readFileSource (const Path &path)
void nix::writeFile (const Path &path, std::string_view s, mode_t mode)
void nix::writeFile (const Path &path, Source &source, mode_t mode)
void nix::writeFile (AutoCloseFD &fd, std::string_view s, mode_t mode)
kj::Promise< Result< void > > nix::writeFile (const Path &path, AsyncInputStream &source, mode_t mode)
void nix::writeFileAndSync (const Path &path, std::string_view s, mode_t mode)
void nix::syncParent (const Path &path)
void nix::deletePath (const Path &path)
void nix::deletePathUninterruptible (const Path &path)
void nix::deletePath (const Path &path, uint64_t &bytesFreed)
Paths nix::createDirs (const Path &path)
Paths nix::createDirs (PathView path)
void nix::createSymlink (const Path &target, const Path &link)
void nix::replaceSymlink (const Path &target, const Path &link)
void nix::renameFile (const Path &oldName, const Path &newName)
void nix::moveFile (const Path &oldName, const Path &newName)
void nix::copyFile (const Path &oldPath, const Path &newPath, CopyFileFlags flags)
Path nix::createTempSubdir (const Path &parent, const Path &prefix, bool includePid, bool useGlobalCounter, mode_t mode)
Path nix::makeTempPath (const Path &root, const Path &suffix)

Detailed Description

Utiltities for working with the file sytem and file paths.

Typedef Documentation

◆ PathFilter

typedef std::function<bool(const Path & path)> nix::PathFilter

Used in various places.

Function Documentation

◆ absPath()

Path nix::absPath ( Path path,
std::optional< PathView > dir = {},
bool resolveSymlinks = false )
Returns
An absolutized path, resolving paths relative to the specified directory, or the current directory otherwise. The path is also canonicalised.

◆ baseNameOf()

std::string_view nix::baseNameOf ( std::string_view path)
Returns
the base name of the given canonical path, i.e., everything following the final / (trailing slashes are removed).

◆ canonPath()

Path nix::canonPath ( PathView path,
bool resolveSymlinks = false )

Canonicalise a path by removing all . or .. components and double or trailing slashes. Optionally resolves all symlink components such that each component of the resulting path is not a symbolic link.

◆ chmodPath()

void nix::chmodPath ( const Path & path,
mode_t mode )

Change the permissions of a path Not called chmod as it shadows and could be confused with int chmod(char *, mode_t), which does not handle errors

◆ copyFile()

void nix::copyFile ( const Path & oldPath,
const Path & newPath,
CopyFileFlags flags )

Recursively copy the content of oldPath to newPath. If andDelete is true, then also remove oldPath (making this equivalent to moveFile, but with the guaranty that the destination will be “fresh”, with no stale inode or file descriptor pointing to it).

◆ createDirs()

Paths nix::createDirs ( const Path & path)

Create a directory and all its parents, if necessary. Returns the list of created directories, in order of creation.

◆ createSymlink()

void nix::createSymlink ( const Path & target,
const Path & link )

Create a symlink. Throws if the symlink exists.

◆ createTempSubdir()

Path nix::createTempSubdir ( const Path & parent,
const Path & prefix = "nix",
bool includePid = true,
bool useGlobalCounter = true,
mode_t mode = 0755 )

Create a temporary directory in a given parent directory.

◆ deletePath()

void nix::deletePath ( const Path & path)

Delete a path; i.e., in the case of a directory, it is deleted recursively. It's not an error if the path does not exist. The second variant returns the number of bytes and blocks freed.

◆ dirOf()

Path nix::dirOf ( const PathView path)
Returns
The directory part of the given canonical path, i.e., everything before the final /. If the path is the root or an immediate child thereof (e.g., /foo), this means / is returned.

◆ expandTilde()

std::string nix::expandTilde ( std::string_view path)

Perform tilde expansion on a path.

◆ getCwd()

Path nix::getCwd ( )

Get the current working directory.

Throw an error if the current directory cannot get got.

◆ isDirOrInDir()

bool nix::isDirOrInDir ( std::string_view path,
std::string_view dir )

Check whether 'path' is equal to 'dir' or a descendant of 'dir'. Both paths must be canonicalized.

◆ isInDir()

bool nix::isInDir ( std::string_view path,
std::string_view dir )

Check whether 'path' is a descendant of 'dir'. Both paths must be canonicalized.

◆ makeTempPath()

Path nix::makeTempPath ( const Path & root,
const Path & suffix = ".tmp" )

Return temporary path constructed by appending a suffix to a root path.

The constructed path looks like <root><suffix>-<pid>-<unique>. To create a path nested in a directory, provide a suffix starting with /.

◆ maybeLstat()

std::optional< struct stat > nix::maybeLstat ( const Path & path)

lstat the given path if it exists.

Returns
std::nullopt if the path doesn't exist, or an optional containing the result of lstat otherwise

◆ maybeStat()

std::optional< struct stat > nix::maybeStat ( const Path & path)

stat the given path if it exists.

Returns
std::nullopt if the path doesn't exist, or an optional containing the result of stat otherwise

◆ moveFile()

void nix::moveFile ( const Path & src,
const Path & dst )

Similar to 'renameFile', but fallback to a copy+remove if src and dst are on a different filesystem.

Beware that this might not be atomic because of the copy that happens behind the scenes

◆ pathAccessible()

bool nix::pathAccessible ( const Path & path,
bool resolveSymlinks = false )

A version of pathExists that returns false on a permission error. Useful for inferring default paths across directories that might not be readable. Optionally resolves symlinks to determine if the real path exists.

Returns
true iff the given path can be accessed and exists

◆ pathExists()

bool nix::pathExists ( const Path & path)
Returns
true iff the given path exists.

◆ readFile()

std::string nix::readFile ( const Path & path)

Read the contents of a file into a string.

◆ readLink()

Path nix::readLink ( const Path & path)

Read the contents (target) of a symbolic link. The result is not in any way canonicalised.

◆ realPath()

Path nix::realPath ( Path const & path)

Resolves a file path to a fully absolute path with no symbolic links.

Parameters
pathThe path to resolve. If it is relative, it will be resolved relative to the process's current directory.
Note
This is not a pure function; it performs this resolution by querying the filesystem.
path sadly must be (a reference to) an owned string, as std::string_view are not valid C strings...
Returns
The fully resolved path.

◆ replaceSymlink()

void nix::replaceSymlink ( const Path & target,
const Path & link )

Atomically create or replace a symlink.

◆ stat()

struct stat nix::stat ( const Path & path)

Get status of path.

◆ syncParent()

void nix::syncParent ( const Path & path)

Flush a file's parent directory to disk

◆ tildePath()

Path nix::tildePath ( Path const & path,
const std::optional< Path > & home = std::nullopt )

Resolve a tilde path like ~/puppy.nix into an absolute path.

If home is given, it's substituted for ~/ at the start of the input path. Otherwise, an error is thrown.

If the path starts with ~ but not ~/, an error is thrown.

◆ writeFile()

void nix::writeFile ( const Path & path,
std::string_view s,
mode_t mode = 0666 )

Write a string to a file.

◆ writeFileAndSync()

void nix::writeFileAndSync ( const Path & path,
std::string_view s,
mode_t mode = 0666 )

Write a string to a file and flush the file and its parents direcotry to disk.