#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 char * | c_str () const |
std::string_view | rel () const |
const char * | rel_c_str () const |
Iterator | begin () const |
Iterator | end () const |
std::optional< CanonPath > | parent () 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 > |
A canonical representation of a path. It ensures the following:
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.
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.nix::CanonPath::CanonPath | ( | std::string_view | raw | ) |
Construct a canon path from a non-canonical path. Any '.', '..' or empty components are removed.
nix::CanonPath::CanonPath | ( | const std::vector< std::string > & | elems | ) |
Construct a canon path from a vector of elements.
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
.
|
inline |
Like abs(), but return an empty string if this path is '/'. Thus the returned string never ends in a slash.
void nix::CanonPath::extend | ( | const CanonPath & | x | ) |
Append another path to this one.
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.)
bool nix::CanonPath::isWithin | ( | const CanonPath & | parent | ) | const |
Return true if this
is equal to parent
or a child of parent
.
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
.
|
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!'.
void nix::CanonPath::pop | ( | ) |
Remove the last component. Panics if this path is the root.
void nix::CanonPath::push | ( | std::string_view | c | ) |
Add a path component to this one. It must not contain any slashes.