-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Purify CanonPath
#9881
Merged
Merged
Purify CanonPath
#9881
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,81 @@ | ||
#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 callback which is passed two arguments, | ||
* references to | ||
* | ||
* 1. the result so far | ||
* | ||
* 2. the remaining path to resolve | ||
* | ||
* This is a chance to modify those two paths in arbitrary way, e.g. if | ||
* "result" points to a symlink. | ||
*/ | ||
typename std::string canonPathInner( | ||
std::string_view remaining, | ||
auto && hookComponent) | ||
{ | ||
assert(remaining != ""); | ||
|
||
std::string result; | ||
result.reserve(256); | ||
|
||
while (true) { | ||
|
||
/* Skip slashes. */ | ||
while (!remaining.empty() && remaining[0] == '/') | ||
remaining.remove_prefix(1); | ||
|
||
if (remaining.empty()) break; | ||
|
||
auto nextComp = ({ | ||
auto nextPathSep = remaining.find('/'); | ||
nextPathSep == remaining.npos ? remaining : remaining.substr(0, nextPathSep); | ||
}); | ||
|
||
/* Ignore `.'. */ | ||
if (nextComp == ".") | ||
remaining.remove_prefix(1); | ||
|
||
/* If `..', delete the last component. */ | ||
else if (nextComp == "..") | ||
{ | ||
if (!result.empty()) result.erase(result.rfind('/')); | ||
remaining.remove_prefix(2); | ||
} | ||
|
||
/* Normal component; copy it. */ | ||
else { | ||
result += '/'; | ||
if (const auto slash = remaining.find('/'); slash == result.npos) { | ||
result += remaining; | ||
remaining = {}; | ||
} else { | ||
result += remaining.substr(0, slash); | ||
remaining = remaining.substr(slash); | ||
} | ||
|
||
hookComponent(result, remaining); | ||
} | ||
} | ||
|
||
if (result.empty()) | ||
result = "/"; | ||
|
||
return result; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be made a
std::function<void(std::string &, std::string_view &)>
to avoid the need for a template?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I'd rather make this a flag to follow or not symlinks (unless there's a reason to generalize it). Higher-order functions are nice but add some non-trivial complexity (a consumer would have a pretty hard time re-implementing the
followSymlinks
lambda used here) so I don't think it's a great tradeoff hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a flag to follow symlinks before. The point of this PR is that following the flags was non-obvious. It's very important that
CanonPath
stuff not secretly use the host file system unless one opts-in with aPosixSourceAccesor
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To quote the PR description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also fwiw in #9767 this code needs template regardless; I didn't just do it to try to make the function inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My interpretation of #9767 (comment) and https://discourse.nixos.org/t/2024-02-05-nix-team-meeting-minutes-121/39340 is that we do want to do that PR, and so this the stepping-stone.