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

#include <os-canon-path.hh>

Classes

class  Iterator

Public Member Functions

 OsCanonPath ()=default
 OsCanonPath (OsFilename name)
 OsCanonPath (const CanonPath &canonPath)
CanonPath toPortable () const
const std::filesystem::path & path () const noexcept
const std::filesystem::path::value_type * c_str () const noexcept
bool empty () const noexcept
void pop ()
std::optional< OsCanonPath > parent () const
bool isWithin (const OsCanonPath &prefix) const
OsCanonPath removePrefix (const OsCanonPath &prefix) const
Iterator begin () const
Iterator end () const
auto operator<=> (const OsCanonPath &) const =default
OsCanonPath operator/ (const OsCanonPath &other) const
OsCanonPath operator/ (const OsFilename &name) const

Static Public Member Functions

static OsCanonPath fromPathAsserting (std::filesystem::path p)

Friends

OsCanonPath operator/ (const OsFilename &name, const OsCanonPath &path)
OsCanonPath operator/ (const OsFilename &a, const OsFilename &b)

Detailed Description

A newtype around std::filesystem::path that asserts at construction time that the path is a canonical relative path suitable for use with openat and Windows NT equivalents.

A valid OsCanonPath:

  • Has no root path or root name (no drive letter on Windows, no leading /)
  • Has no . or .. components
  • Never ends with a directory separator (except the empty path)
  • Has no empty components (no //)
  • The empty path "" is valid and represents the identity

Unlike CanonPath, which normalizes its input, OsCanonPath asserts that the input is already canonical. Unlike CanonPath which is a virtual Unix-style path wrapping std::string, OsCanonPath wraps std::filesystem::path and respects OS-native directory separators.

Constructor & Destructor Documentation

◆ OsCanonPath() [1/3]

nix::OsCanonPath::OsCanonPath ( )
default

Construct the empty OsCanonPath, representing the identity path "".

◆ OsCanonPath() [2/3]

nix::OsCanonPath::OsCanonPath ( OsFilename name)
inline

Construct an OsCanonPath from an OsFilename.

This is always valid since a single filename component is trivially a canonical relative path.

◆ OsCanonPath() [3/3]

nix::OsCanonPath::OsCanonPath ( const CanonPath & canonPath)

Construct an OsCanonPath from a CanonPath.

Builds the path component-by-component using std::filesystem::path::operator/, which produces the correct OS-native directory separator.

Member Function Documentation

◆ empty()

bool nix::OsCanonPath::empty ( ) const
inlinenoexcept

Whether this is the empty path.

◆ fromPathAsserting()

OsCanonPath nix::OsCanonPath::fromPathAsserting ( std::filesystem::path p)
inlinestatic

Construct an OsCanonPath from a path, asserting that it is already in canonical relative form. Fails with an assertion otherwise.

◆ isWithin()

bool nix::OsCanonPath::isWithin ( const OsCanonPath & prefix) const

Whether prefix is this path or an ancestor of it.

◆ operator/() [1/2]

OsCanonPath nix::OsCanonPath::operator/ ( const OsCanonPath & other) const

Concatenate two OsCanonPaths.

◆ operator/() [2/2]

OsCanonPath nix::OsCanonPath::operator/ ( const OsFilename & name) const

Append an OsFilename to an OsCanonPath.

◆ parent()

std::optional< OsCanonPath > nix::OsCanonPath::parent ( ) const

Everything but the last component, or std::nullopt if this path is empty.

◆ pop()

void nix::OsCanonPath::pop ( )

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

◆ removePrefix()

OsCanonPath nix::OsCanonPath::removePrefix ( const OsCanonPath & prefix) const

Reinterpret this path as relative to prefix rather than to whatever it was relative to before.

Precondition
isWithin(prefix) is true.

◆ toPortable()

CanonPath nix::OsCanonPath::toPortable ( ) const

Convert back to the portable CanonPath, the inverse of the constructor above.

Structurally this round-trips: OsCanonPath invariants mean every component is a plain filename, which is exactly what CanonPath accepts, so no component is dropped or renormalized.

Todo
The encoding does not round-trip on Windows, where this narrows each wchar_t component. Names that are not valid Unicode (e.g. unpaired surrogates) will not survive until we represent CanonPath as WTF-8.

◆ operator/ [1/2]

OsCanonPath operator/ ( const OsFilename & a,
const OsFilename & b )
friend

Concatenate two OsFilenames into an OsCanonPath.

This explicit overload is needed because OsFilename / OsFilename would otherwise be ambiguous between converting the LHS or RHS.

◆ operator/ [2/2]

OsCanonPath operator/ ( const OsFilename & name,
const OsCanonPath & path )
friend

Prepend: OsFilename / OsCanonPath.


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