-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The core `CanonPath` constructors were using `absPath`, but `absPath` in some situations does IO which is not appropriate. It turns out that these constructors avoided those situations, and thus were pure, but it was far from obvious this was the case. To remedy the situation, abstract the core algorithm from `canonPath` to use separately in `CanonPath` without any IO. No we know by-construction that those constructors are pure. That leaves `CanonPath::fromCWD` as the only operation which uses IO / is impure. Add docs on it, and `CanonPath` as a whole, explaining the situation. This is also necessary to support Windows paths on windows without messing up `CanonPath`. But, I think it is good even without that. Co-authored-by: Eelco Dolstra <[email protected]>
- Loading branch information
1 parent
c291d2d
commit f425a50
Showing
5 changed files
with
141 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#pragma once | ||
/** | ||
* @file | ||
* | ||
* Pure (no IO) infrastructure just for defining other path types; | ||
* should not be used directly outside of utilities. | ||
*/ | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace nix { | ||
|
||
/** | ||
* Core pure path canonicalization algorithm. | ||
* | ||
* @param hookComponent A chance to modify `s` and `path` in an | ||
* arbitrary way, e.g. if it points to a symlink. Does nothing by | ||
* default. | ||
*/ | ||
typename std::string canonPathInner( | ||
std::string_view path, | ||
auto && hookComponent) | ||
{ | ||
assert(path != ""); | ||
|
||
typename std::string s; | ||
s.reserve(256); | ||
|
||
while (true) { | ||
|
||
/* Skip slashes. */ | ||
while (!path.empty() && path[0] == '/') | ||
path.remove_prefix(1); | ||
|
||
if (path.empty()) break; | ||
|
||
auto nextComp = ({ | ||
auto nextPathSep = path.find('/'); | ||
nextPathSep == path.npos ? path : path.substr(0, nextPathSep); | ||
}); | ||
|
||
/* Ignore `.'. */ | ||
if (nextComp == ".") | ||
path.remove_prefix(1); | ||
|
||
/* If `..', delete the last component. */ | ||
else if (nextComp == "..") | ||
{ | ||
if (!s.empty()) s.erase(s.rfind('/')); | ||
path.remove_prefix(2); | ||
} | ||
|
||
/* Normal component; copy it. */ | ||
else { | ||
s += '/'; | ||
if (const auto slash = path.find('/'); slash == s.npos) { | ||
s += path; | ||
path = {}; | ||
} else { | ||
s += path.substr(0, slash); | ||
path = path.substr(slash); | ||
} | ||
|
||
hookComponent(s, path); | ||
} | ||
} | ||
|
||
if (s.empty()) | ||
s = "/"; | ||
|
||
return s; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters