14#include <boost/container_hash/hash.hpp>
18MakeError(BadCanonPath,
Error);
67 : path(std::move(path))
74 CanonPath(
const std::vector<std::string> & elems);
93 return path.size() <= 1;
96 explicit operator std::string_view()
const
101 const std::string & abs()
const
112 static const std::string epsilon;
113 return isRoot() ? epsilon : path;
116 const char * c_str()
const
121 std::string_view rel()
const
123 return ((std::string_view) path).substr(1);
126 const char * rel_c_str()
const
128 auto cs = path.c_str();
141 std::string_view segment;
144 PointerProxy(std::string_view segment_)
149 const std::string_view * operator->()
const
156 using value_type = std::string_view;
157 using reference_type =
const std::string_view;
158 using pointer_type = PointerProxy;
159 using difference_type = std::ptrdiff_t;
160 using iterator_category = std::forward_iterator_tag;
162 std::string_view remaining;
175 Iterator(std::string_view remaining)
176 : remaining(remaining)
177 , slash(remaining.find(
'/'))
181 bool operator==(
const Iterator & x)
const
183 return remaining.data() == x.remaining.data();
186 reference_type operator*()
const
188 return remaining.substr(0, slash);
191 pointer_type operator->()
const
193 return PointerProxy(**
this);
198 if (slash == remaining.npos)
199 remaining = remaining.substr(remaining.size());
201 remaining = remaining.substr(slash + 1);
202 slash = remaining.find(
'/');
215 static_assert(std::forward_iterator<Iterator>);
224 return Iterator(rel().substr(path.size() - 1));
227 std::optional<CanonPath> parent()
const;
234 std::optional<std::string_view> dirOf()
const
238 return ((std::string_view) path).substr(0, path.rfind(
'/'));
241 std::optional<std::string_view> baseName()
const
245 return ((std::string_view) path).substr(path.rfind(
'/') + 1);
248 bool operator==(
const CanonPath & x)
const
250 return path == x.path;
253 bool operator!=(
const CanonPath & x)
const
255 return path != x.path;
266 auto i = path.begin();
267 auto j = x.path.begin();
268 for (; i != path.end() && j != x.path.end(); ++i, ++j) {
275 unsigned char c_i = *i;
278 unsigned char c_j = *j;
281 if (
auto cmp = c_i <=> c_j; cmp != 0)
284 return (i != path.end()) <=> (j != x.path.end());
308 void push(std::string_view c);
318 bool isAllowed(
const std::set<CanonPath> & allowed)
const;
326 friend std::size_t hash_value(
const CanonPath &);
329static_assert(std::ranges::forward_range<CanonPath>);
331std::ostream & operator<<(std::ostream & stream,
const CanonPath & path);
333inline std::size_t hash_value(
const CanonPath & path)
335 boost::hash<std::string_view> hasher;
336 return hasher(path.path);
342struct std::hash<nix::CanonPath>
344 using is_avalanching = std::true_type;
346 std::size_t operator()(
const nix::CanonPath & path)
const noexcept
348 return nix::hash_value(path);
Definition canon-path.hh:134
Iterator()
Definition canon-path.hh:169
Definition canon-path.hh:50
bool isWithin(const CanonPath &parent) const
Definition canon-path.cc:75
void pop()
Definition canon-path.cc:69
std::string makeRelative(const CanonPath &path) const
Definition canon-path.cc:153
auto operator<=>(const CanonPath &x) const
Definition canon-path.hh:264
const std::string & absOrEmpty() const
Definition canon-path.hh:110
bool isAllowed(const std::set< CanonPath > &allowed) const
Definition canon-path.cc:126
void push(std::string_view c)
Definition canon-path.cc:109
CanonPath(std::string_view raw)
Definition canon-path.cc:38
static CanonPath fromFilename(std::string_view segment)
Definition canon-path.cc:28
void extend(const CanonPath &x)
Definition canon-path.cc:92
This file defines two main structs/classes used in nix error handling.
OsCanonPath operator/(const OsFilename &a, const OsFilename &b)
Definition os-canon-path.cc:93
Definition canon-path.hh:64