Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
nix::CanonPath Class Reference

#include <canon-path.hh>

Classes

struct  Iterator
 
struct  unchecked_t
 

Public Member Functions

 CanonPath (std::string_view raw)
 
 CanonPath (const char *raw)
 
 CanonPath (unchecked_t _, std::string path)
 
 CanonPath (const std::vector< std::string > &elems)
 
 CanonPath (std::string_view raw, const CanonPath &root)
 
bool isRoot () const
 
 operator std::string_view () const
 
const std::string & abs () const
 
const std::string & absOrEmpty () const
 
const charc_str () const
 
std::string_view rel () const
 
const charrel_c_str () const
 
Iterator begin () const
 
Iterator end () const
 
std::optional< CanonPathparent () const
 
void pop ()
 
std::optional< std::string_view > dirOf () const
 
std::optional< std::string_view > baseName () const
 
bool operator== (const CanonPath &x) const
 
bool operator!= (const CanonPath &x) const
 
auto operator<=> (const CanonPath &x) const
 
bool isWithin (const CanonPath &parent) const
 
CanonPath removePrefix (const CanonPath &prefix) const
 
void extend (const CanonPath &x)
 
CanonPath operator/ (const CanonPath &x) const
 
void push (std::string_view c)
 
CanonPath operator/ (std::string_view c) const
 
bool isAllowed (const std::set< CanonPath > &allowed) const
 
std::string makeRelative (const CanonPath &path) const
 

Static Public Attributes

static CanonPath root = CanonPath("/")
 

Friends

class std::hash< CanonPath >
 

Detailed Description

A canonical representation of a path. It ensures the following:

  • It always starts with a slash.
  • It never ends with a slash, except if the path is "/".
  • A slash is never followed by a slash (i.e. no empty components).
  • There are no components equal to '.' or '..'.

CanonPath are "virtual" Nix paths for abstract file system objects; they are always Unix-style paths, regardless of what OS Nix is running on. The / root doesn't denote the ambient host file system root, but some virtual FS root.

Note
It might be useful to compare openat(some_fd, "foo/bar") on Unix. "foo/bar" is a relative path because an absolute path would "override" the some_fd directory file descriptor and escape to the "system root". Conversely, Nix's abstract file operations never escape the designated virtual file system (i.e. SourceAccessor or ParseSink), so CanonPath does not need an absolute/relative distinction.
The path does not need to correspond to an actually existing path, and the path may or may not have unresolved symlinks.

Constructor & Destructor Documentation

◆ CanonPath() [1/3]

nix::CanonPath::CanonPath ( std::string_view raw)

Construct a canon path from a non-canonical path. Any '.', '..' or empty components are removed.

◆ CanonPath() [2/3]

nix::CanonPath::CanonPath ( const std::vector< std::string > & elems)

Construct a canon path from a vector of elements.

◆ CanonPath() [3/3]

nix::CanonPath::CanonPath ( std::string_view raw,
const CanonPath & root )

If raw starts with a slash, return CanonPath(raw). Otherwise return a CanonPath representing root + "/" + raw.

Member Function Documentation

◆ absOrEmpty()

const std::string & nix::CanonPath::absOrEmpty ( ) const
inline

Like abs(), but return an empty string if this path is '/'. Thus the returned string never ends in a slash.

◆ extend()

void nix::CanonPath::extend ( const CanonPath & x)

Append another path to this one.

◆ isAllowed()

bool nix::CanonPath::isAllowed ( const std::set< CanonPath > & allowed) const

Check whether access to this path is allowed, which is the case if 1) this is within any of the allowed paths; or 2) any of the allowed paths are within this. (The latter condition ensures access to the parents of allowed paths.)

◆ isWithin()

bool nix::CanonPath::isWithin ( const CanonPath & parent) const

Return true if this is equal to parent or a child of parent.

◆ makeRelative()

std::string nix::CanonPath::makeRelative ( const CanonPath & path) const

Return a representation x of path relative to this, i.e. CanonPath(this.makeRelative(x), this) == path.

◆ operator/()

CanonPath nix::CanonPath::operator/ ( const CanonPath & x) const

Concatenate two paths.

◆ operator<=>()

auto nix::CanonPath::operator<=> ( const CanonPath & x) const
inline

Compare paths lexicographically except that path separators are sorted before any other character. That is, in the sorted order a directory is always followed directly by its children. For instance, 'foo' < 'foo/bar' < 'foo!'.

◆ pop()

void nix::CanonPath::pop ( )

Remove the last component. Panics if this path is the root.

◆ push()

void nix::CanonPath::push ( std::string_view c)

Add a path component to this one. It must not contain any slashes.


The documentation for this class was generated from the following files: