Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
canon-path.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/error.hh"
5
6#include <iosfwd>
7#include <string>
8#include <optional>
9#include <cassert>
10#include <set>
11#include <vector>
12#include <ranges>
13
14#include <boost/container_hash/hash.hpp>
15
16namespace nix {
17
18MakeError(BadCanonPath, Error);
19
50{
51 std::string path;
52
53public:
54
59 CanonPath(std::string_view raw);
60
61 explicit CanonPath(const char * raw);
62
64 {};
65
66 constexpr CanonPath(unchecked_t _, std::string path)
67 : path(std::move(path))
68 {
69 }
70
74 CanonPath(const std::vector<std::string> & elems);
75
76 static const CanonPath root;
77
82 static CanonPath fromFilename(std::string_view segment);
83
89 CanonPath(std::string_view raw, const CanonPath & root);
90
91 bool isRoot() const
92 {
93 return path.size() <= 1;
94 }
95
96 explicit operator std::string_view() const
97 {
98 return path;
99 }
100
101 const std::string & abs() const
102 {
103 return path;
104 }
105
110 const std::string & absOrEmpty() const
111 {
112 static const std::string epsilon;
113 return isRoot() ? epsilon : path;
114 }
115
116 const char * c_str() const
117 {
118 return path.c_str();
119 }
120
121 std::string_view rel() const
122 {
123 return ((std::string_view) path).substr(1);
124 }
125
126 const char * rel_c_str() const
127 {
128 auto cs = path.c_str();
129 assert(cs[0]); // for safety if invariant is broken
130 return &cs[1];
131 }
132
134 {
139 class PointerProxy
140 {
141 std::string_view segment;
142
143 public:
144 PointerProxy(std::string_view segment_)
145 : segment(segment_)
146 {
147 }
148
149 const std::string_view * operator->() const
150 {
151 return &segment;
152 }
153 };
154
155 public:
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;
161
162 std::string_view remaining;
163 size_t slash;
164
170 : remaining()
171 , slash(0)
172 {
173 }
174
175 Iterator(std::string_view remaining)
176 : remaining(remaining)
177 , slash(remaining.find('/'))
178 {
179 }
180
181 bool operator==(const Iterator & x) const
182 {
183 return remaining.data() == x.remaining.data();
184 }
185
186 reference_type operator*() const
187 {
188 return remaining.substr(0, slash);
189 }
190
191 pointer_type operator->() const
192 {
193 return PointerProxy(**this);
194 }
195
196 Iterator & operator++()
197 {
198 if (slash == remaining.npos)
199 remaining = remaining.substr(remaining.size());
200 else {
201 remaining = remaining.substr(slash + 1);
202 slash = remaining.find('/');
203 }
204 return *this;
205 }
206
207 Iterator operator++(int)
208 {
209 auto tmp = *this;
210 ++*this;
211 return tmp;
212 }
213 };
214
215 static_assert(std::forward_iterator<Iterator>);
216
217 Iterator begin() const
218 {
219 return Iterator(rel());
220 }
221
222 Iterator end() const
223 {
224 return Iterator(rel().substr(path.size() - 1));
225 }
226
227 std::optional<CanonPath> parent() const;
228
232 void pop();
233
234 std::optional<std::string_view> dirOf() const
235 {
236 if (isRoot())
237 return std::nullopt;
238 return ((std::string_view) path).substr(0, path.rfind('/'));
239 }
240
241 std::optional<std::string_view> baseName() const
242 {
243 if (isRoot())
244 return std::nullopt;
245 return ((std::string_view) path).substr(path.rfind('/') + 1);
246 }
247
248 bool operator==(const CanonPath & x) const
249 {
250 return path == x.path;
251 }
252
253 bool operator!=(const CanonPath & x) const
254 {
255 return path != x.path;
256 }
257
264 auto operator<=>(const CanonPath & x) const
265 {
266 auto i = path.begin();
267 auto j = x.path.begin();
268 for (; i != path.end() && j != x.path.end(); ++i, ++j) {
269 /* Unsigned, so that `/` (mapped to 0) sorts before every
270 other byte, including non-ASCII ones. Otherwise we
271 violate the spec written above.
272
273 Null bytes are already disallowed so we don't need to worry
274 about '\0' and '/' comparisons. */
275 unsigned char c_i = *i;
276 if (c_i == '/')
277 c_i = 0;
278 unsigned char c_j = *j;
279 if (c_j == '/')
280 c_j = 0;
281 if (auto cmp = c_i <=> c_j; cmp != 0)
282 return cmp;
283 }
284 return (i != path.end()) <=> (j != x.path.end());
285 }
286
291 bool isWithin(const CanonPath & parent) const;
292
293 CanonPath removePrefix(const CanonPath & prefix) const;
294
298 void extend(const CanonPath & x);
299
303 CanonPath operator/(const CanonPath & x) const;
304
308 void push(std::string_view c);
309
310 CanonPath operator/(std::string_view c) const;
311
318 bool isAllowed(const std::set<CanonPath> & allowed) const;
319
324 std::string makeRelative(const CanonPath & path) const;
325
326 friend std::size_t hash_value(const CanonPath &);
327};
328
329static_assert(std::ranges::forward_range<CanonPath>);
330
331std::ostream & operator<<(std::ostream & stream, const CanonPath & path);
332
333inline std::size_t hash_value(const CanonPath & path)
334{
335 boost::hash<std::string_view> hasher;
336 return hasher(path.path);
337}
338
339} // namespace nix
340
341template<>
342struct std::hash<nix::CanonPath>
343{
344 using is_avalanching = std::true_type;
345
346 std::size_t operator()(const nix::CanonPath & path) const noexcept
347 {
348 return nix::hash_value(path);
349 }
350};
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
Definition error.hh:310
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