Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
file-system.hh File Reference
#include "types.hh"
#include "error.hh"
#include "logging.hh"
#include "file-descriptor.hh"
#include "file-path.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <atomic>
#include <functional>
#include <map>
#include <sstream>
#include <optional>

Go to the source code of this file.

Classes

class  nix::AutoDelete
 
struct  nix::DIRDeleter
 

Macros

#define S_ISLNK(m)
 

Typedefs

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

Functions

bool nix::isAbsolute (PathView path)
 
Path nix::absPath (PathView path, std::optional< PathView > dir, bool resolveSymlinks)
 
Path nix::absPath (const Path &path, std::optional< PathView > dir={}, bool resolveSymlinks=false)
 
std::filesystem::path nix::absPath (const std::filesystem::path &path, bool resolveSymlinks)
 
Path nix::canonPath (PathView path, bool resolveSymlinks)
 
Path nix::dirOf (const PathView path)
 
std::string_view nix::baseNameOf (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::maybeLstat (const Path &path)
 
bool nix::pathExists (const Path &path)
 
bool nix::fs::symlink_exists (const std::filesystem::path &path)
 
std::filesystem::path nix::makeParentCanonical (const std::filesystem::path &rawPath)
 
bool nix::pathAccessible (const std::filesystem::path &path)
 
Path nix::readLink (const Path &path)
 
Descriptor nix::openDirectory (const std::filesystem::path &path)
 
std::string nix::readFile (const Path &path)
 
std::string nix::readFile (const std::filesystem::path &path)
 
void nix::readFile (const Path &path, Sink &sink)
 
void nix::writeFile (const Path &path, std::string_view s, mode_t mode, bool sync)
 
void nix::writeFile (const Path &path, Source &source, mode_t mode, bool sync)
 
void nix::syncParent (const Path &path)
 
void nix::recursiveSync (const Path &path)
 
void nix::deletePath (const std::filesystem::path &path)
 
void nix::deletePath (const std::filesystem::path &path, uint64_t &bytesFreed)
 
void nix::createDirs (const Path &path)
 
void nix::createDirs (PathView path)
 
void nix::createDir (const Path &path, mode_t mode)
 
void nix::setWriteTime (const std::filesystem::path &path, time_t accessedTime, time_t modificationTime, std::optional< bool > isSymlink=std::nullopt)
 
void nix::setWriteTime (const std::filesystem::path &path, const struct stat &st)
 
void nix::createSymlink (const Path &target, const Path &link)
 
void nix::replaceSymlink (const std::filesystem::path &target, const std::filesystem::path &link)
 
void nix::replaceSymlink (const Path &target, const Path &link)
 
void nix::moveFile (const Path &oldName, const Path &newName)
 
void nix::copyFile (const std::filesystem::path &from, const std::filesystem::path &to, bool andDelete)
 
Path nix::createTempDir (const Path &tmpRoot, const Path &prefix, bool includePid, bool useGlobalCounter, mode_t mode)
 
std::pair< AutoCloseFD, Pathnix::createTempFile (const Path &prefix)
 
std::string nix::defaultTempDir ()
 
bool nix::isExecutableFileAmbient (const std::filesystem::path &exe)
 

Detailed Description

Utiltities for working with the file sytem and file paths.

Macro Definition Documentation

◆ S_ISLNK

#define S_ISLNK ( m)
Value:
false

Polyfill for MinGW

Windows does in fact support symlinks, but the C runtime interfaces predate this.

Todo
get rid of this, and stop using stat when we want lstat too.

Typedef Documentation

◆ PathFilter

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

Used in various places.

Function Documentation

◆ absPath()

Path nix::absPath ( PathView 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.

In the process of being deprecated for std::filesystem::absolute.

◆ 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).

In the process of being deprecated for std::filesystem::path::filename.

◆ 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.

In the process of being deprecated for std::filesystem::path::lexically_normal (for the resolveSymlinks = false case), and std::filesystem::weakly_canonical (for the resolveSymlinks = true case).

◆ copyFile()

void nix::copyFile ( const std::filesystem::path & from,
const std::filesystem::path & to,
bool andDelete )

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).

◆ createDir()

void nix::createDir ( const Path & path,
mode_t mode = 0755 )

Create a single directory.

◆ createDirs()

void nix::createDirs ( const Path & path)

Create a directory and all its parents, if necessary.

In the process of being deprecated for std::filesystem::create_directories.

◆ createSymlink()

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

Create a symlink.

◆ createTempDir()

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

Create a temporary directory.

◆ createTempFile()

std::pair< AutoCloseFD, Path > nix::createTempFile ( const Path & prefix = "nix")

Create a temporary file, returning a file handle and its path.

◆ defaultTempDir()

Path nix::defaultTempDir ( )

Return TMPDIR, or the default temporary directory if unset or empty.

◆ deletePath()

void nix::deletePath ( const std::filesystem::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.

In the process of being deprecated for std::filesystem::path::parent_path.

◆ isAbsolute()

bool nix::isAbsolute ( PathView path)

Return whether the path denotes an absolute path.

◆ 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.

◆ isExecutableFileAmbient()

bool nix::isExecutableFileAmbient ( const std::filesystem::path & exe)

Interpret exe as a location in the ambient file system and return whether it resolves to a file that is executable.

◆ 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.

◆ makeParentCanonical()

std::filesystem::path nix::makeParentCanonical ( const std::filesystem::path & path)

Canonicalize a path except for the last component.

This is useful for getting the canonical location of a symlink.

Consider the case where foo/l is a symlink. canonical("foo/l") will resolve the symlink l to its target. makeParentCanonical("foo/l") will not resolve the symlink l to its target, but does ensure that the returned parent part of the path, foo is resolved to canonical("foo"), and can therefore be retrieved without traversing any symlinks.

If a relative path is passed, it will be made absolute, so that the parent can always be canonicalized.

◆ 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

◆ 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

◆ openDirectory()

Descriptor nix::openDirectory ( const std::filesystem::path & path)

Open a Descriptor with read-only access to the given directory.

◆ pathAccessible()

bool nix::pathAccessible ( const std::filesystem::path & path)

A version of pathExists that returns false on a permission error. Useful for inferring default paths across directories that might not be readable.

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.

In the process of being deprecated for fs::symlink_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.

In the process of being deprecated for std::filesystem::read_symlink.

◆ recursiveSync()

void nix::recursiveSync ( const Path & path)

Flush a file or entire directory tree to disk.

◆ replaceSymlink()

void nix::replaceSymlink ( const std::filesystem::path & target,
const std::filesystem::path & link )

Atomically create or replace a symlink.

◆ setWriteTime() [1/2]

void nix::setWriteTime ( const std::filesystem::path & path,
const struct stat & st )

Convenience wrapper that takes all arguments from the struct stat.

◆ setWriteTime() [2/2]

void nix::setWriteTime ( const std::filesystem::path & path,
time_t accessedTime,
time_t modificationTime,
std::optional< bool > isSymlink = std::nullopt )

Set the access and modification times of the given path, not following symlinks.

Parameters
accessedTimeSpecified in seconds.
modificationTimeSpecified in seconds.
isSymlinkWhether the file in question is a symlink. Used for fallback code where we don't have lutimes or similar. if std::optional is passed, the information will be recomputed if it is needed. Race conditions are possible so be careful!

◆ stat()

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

Get status of path.

◆ symlink_exists()

bool nix::fs::symlink_exists ( const std::filesystem::path & path)
inline
symlink_exists(p) = std::filesystem::exists(std::filesystem::symlink_status(p))
std::shared_ptr< T > p
Definition lexer.l:1269

Missing convenience analogous to

std::filesystem::exists(p) = std::filesystem::exists(std::filesystem::status(p))

◆ syncParent()

void nix::syncParent ( const Path & path)

Flush a path's parent directory to disk.

◆ writeFile()

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

Write a string to a file.