-
-
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
Use std::filesystem
in canonPath and local store symlink detection
#11871
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -86,43 +86,23 @@ Path canonPath(PathView path, bool resolveSymlinks) | |||||||||||||||||||
if (!isAbsolute(path)) | ||||||||||||||||||||
throw Error("not an absolute path: '%1%'", path); | ||||||||||||||||||||
|
||||||||||||||||||||
// For Windows | ||||||||||||||||||||
auto rootName = fs::path { path }.root_name(); | ||||||||||||||||||||
|
||||||||||||||||||||
/* This just exists because we cannot set the target of `remaining` | ||||||||||||||||||||
(the callback parameter) directly to a newly-constructed string, | ||||||||||||||||||||
since it is `std::string_view`. */ | ||||||||||||||||||||
std::string temp; | ||||||||||||||||||||
|
||||||||||||||||||||
/* Count the number of times we follow a symlink and stop at some | ||||||||||||||||||||
arbitrary (but high) limit to prevent infinite loops. */ | ||||||||||||||||||||
unsigned int followCount = 0, maxFollow = 1024; | ||||||||||||||||||||
|
||||||||||||||||||||
auto ret = canonPathInner<OsPathTrait<char>>( | ||||||||||||||||||||
path, | ||||||||||||||||||||
[&followCount, &temp, maxFollow, resolveSymlinks] | ||||||||||||||||||||
(std::string & result, std::string_view & remaining) { | ||||||||||||||||||||
if (resolveSymlinks && fs::is_symlink(result)) { | ||||||||||||||||||||
if (++followCount >= maxFollow) | ||||||||||||||||||||
throw Error("infinite symlink recursion in path '%0%'", remaining); | ||||||||||||||||||||
remaining = (temp = concatStrings(readLink(result), remaining)); | ||||||||||||||||||||
if (isAbsolute(remaining)) { | ||||||||||||||||||||
/* restart for symlinks pointing to absolute path */ | ||||||||||||||||||||
result.clear(); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
result = dirOf(result); | ||||||||||||||||||||
if (result == "/") { | ||||||||||||||||||||
/* we don’t want trailing slashes here, which `dirOf` | ||||||||||||||||||||
only produces if `result = /` */ | ||||||||||||||||||||
result.clear(); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
}); | ||||||||||||||||||||
// This function used to resolve 1024 symlinks via a custom implementation. | ||||||||||||||||||||
// The standard filesystem library will behave differently. For example, | ||||||||||||||||||||
// libstd++ in GCC will only resolve 40 symlinks. | ||||||||||||||||||||
// I hope that isn't a problem! | ||||||||||||||||||||
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This kind of historical comment is best put in the Git commit message. It is worth noting what happens if the recursion limit is exceeded. Does it throw an exception? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal says
Great, lol |
||||||||||||||||||||
auto result = resolveSymlinks ? fs::weakly_canonical(path) : fs::path { path }.lexically_normal(); | ||||||||||||||||||||
|
||||||||||||||||||||
// Strip trailing slashes | ||||||||||||||||||||
while (!result.has_filename() && result.has_parent_path()) | ||||||||||||||||||||
{ | ||||||||||||||||||||
// The parent of "D:/" is "D:/" so we need to be careful. | ||||||||||||||||||||
fs::path parent = result.parent_path(); | ||||||||||||||||||||
if (parent == result) | ||||||||||||||||||||
break; | ||||||||||||||||||||
result = parent; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+96
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
if (!rootName.empty()) | ||||||||||||||||||||
ret = rootName.string() + std::move(ret); | ||||||||||||||||||||
return ret; | ||||||||||||||||||||
return result.string(); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
|
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.
Would be nice to wrap is_symlink in an try-catch and re-throw a nix error instead the original exception that std::filesystem throws because those actually will have an error trace added when the exception is bubbled up.
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.
The function
rethrowExceptionAsError()
can be used for that. (Should be moved from local-derivation-goal.cc to libutil, probably.)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.
Ok. Just make sure
rethrowExceptionAsError()
properly also logs paths as well this way so that users have a clue what goes wrong.