···
···
"The given string \"${value}\" contains a `..` component, which is not allowed in subpaths"
41
+
# Split and normalise a relative path string into its components.
42
+
# Error for ".." components and doesn't include "." components
43
+
splitRelPath = path:
45
+
# Split the string into its parts using regex for efficiency. This regex
46
+
# matches patterns like "/", "/./", "/././", with arbitrarily many "/"s
47
+
# together. These are the main special cases:
48
+
# - Leading "./" gets split into a leading "." part
49
+
# - Trailing "/." or "/" get split into a trailing "." or ""
52
+
# These are the only cases where "." and "" parts can occur
53
+
parts = split "/+(\\./+)*" path;
55
+
# `split` creates a list of 2 * k + 1 elements, containing the k +
56
+
# 1 parts, interleaved with k matches where k is the number of
57
+
# (non-overlapping) matches. This calculation here gets the number of parts
58
+
# back from the list length
59
+
# floor( (2 * k + 1) / 2 ) + 1 == floor( k + 1/2 ) + 1 == k + 1
60
+
partCount = length parts / 2 + 1;
62
+
# To assemble the final list of components we want to:
63
+
# - Skip a potential leading ".", normalising "./foo" to "foo"
64
+
# - Skip a potential trailing "." or "", normalising "foo/" and "foo/." to
65
+
# "foo". See ./path.md#trailing-slashes
66
+
skipStart = if head parts == "." then 1 else 0;
67
+
skipEnd = if last parts == "." || last parts == "" then 1 else 0;
69
+
# We can now know the length of the result by removing the number of
70
+
# skipped parts from the total number
71
+
componentCount = partCount - skipEnd - skipStart;
74
+
# Special case of a single "." path component. Such a case leaves a
75
+
# componentCount of -1 due to the skipStart/skipEnd not verifying that
76
+
# they don't refer to the same character
77
+
if path == "." then []
79
+
# Generate the result list directly. This is more efficient than a
80
+
# combination of `filter`, `init` and `tail`, because here we don't
81
+
# allocate any intermediate lists
82
+
else genList (index:
83
+
# To get to the element we need to add the number of parts we skip and
84
+
# multiply by two due to the interleaved layout of `parts`
85
+
elemAt parts ((skipStart + index) * 2)
88
+
# Join relative path components together
89
+
joinRelPath = components:
90
+
# Always return relative paths with `./` as a prefix (./path.md#leading-dots-for-relative-paths)
92
+
# An empty string is not a valid relative path, so we need to return a `.` when we have no components
93
+
(if components == [] then "." else concatStringsSep "/" components);
in /* No rec! Add dependencies on this file at the top. */ {
···
subpathInvalidReason value == null;
140
+
/* Normalise a subpath. Throw an error if the subpath isn't valid, see
141
+
`lib.path.subpath.isValid`
143
+
- Limit repeating `/` to a single one
145
+
- Remove redundant `.` components
147
+
- Remove trailing `/` and `/.`
153
+
- (Idempotency) Normalising multiple times gives the same result:
155
+
subpath.normalise (subpath.normalise p) == subpath.normalise p
157
+
- (Uniqueness) There's only a single normalisation for the paths that lead to the same file system node:
159
+
subpath.normalise p != subpath.normalise q -> $(realpath ${p}) != $(realpath ${q})
161
+
- Don't change the result when appended to a Nix path value:
163
+
base + ("/" + p) == base + ("/" + subpath.normalise p)
165
+
- Don't change the path according to `realpath`:
167
+
$(realpath ${p}) == $(realpath ${subpath.normalise p})
169
+
- Only error on invalid subpaths:
171
+
builtins.tryEval (subpath.normalise p)).success == subpath.isValid p
174
+
subpath.normalise :: String -> String
177
+
# limit repeating `/` to a single one
178
+
subpath.normalise "foo//bar"
181
+
# remove redundant `.` components
182
+
subpath.normalise "foo/./bar"
186
+
subpath.normalise "foo/bar"
189
+
# remove trailing `/`
190
+
subpath.normalise "foo/bar/"
193
+
# remove trailing `/.`
194
+
subpath.normalise "foo/bar/."
197
+
# Return the current directory as `./.`
198
+
subpath.normalise "."
201
+
# error on `..` path components
202
+
subpath.normalise "foo/../bar"
205
+
# error on empty string
206
+
subpath.normalise ""
209
+
# error on absolute path
210
+
subpath.normalise "/foo"
213
+
subpath.normalise = path:
214
+
assert assertMsg (subpathInvalidReason path == null)
215
+
"lib.path.subpath.normalise: Argument is not a valid subpath string: ${subpathInvalidReason path}";
216
+
joinRelPath (splitRelPath path);