Skip to content
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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,15 @@ LocalStore::LocalStore(

/* Ensure that the store and its parents are not symlinks. */
if (!settings.allowSymlinkedStore) {
Path path = realStoreDir;
struct stat st;
while (path != "/") {
st = lstat(path);
if (S_ISLNK(st.st_mode))
std::filesystem::path path = realStoreDir.get();
std::filesystem::path root = path.root_path();
while (path != root) {
if (std::filesystem::is_symlink(path))
Copy link
Member

@Mic92 Mic92 Nov 19, 2024

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.

Copy link
Member

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.)

Copy link
Member

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.

throw Error(
"the path '%1%' is a symlink; "
"this is not allowed for the Nix store and its parent directories",
path);
path = dirOf(path);
path = path.parent_path();
}
}

Expand Down
52 changes: 16 additions & 36 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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!
// 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!

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal says

Exceptions

May throw implementation-defined exceptions.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a lexically_normal path can have at most one trailing separator, so you probably don't need a while loop here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}
if (result.has_parent_path()) result = result.parent_path();


if (!rootName.empty())
ret = rootName.string() + std::move(ret);
return ret;
return result.string();
}


Expand Down
Loading