1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 23:11:16 +02:00

isInDir and isDirOrInDir: Clean up with std::filesystem

The behavior *does* change, per the tests, but I think the new behavior
is less buggy.
This commit is contained in:
John Ericson 2025-03-17 17:54:09 -04:00
parent 3286728e40
commit d3de22b2be
3 changed files with 14 additions and 16 deletions

View file

@ -149,16 +149,18 @@ std::string_view baseNameOf(std::string_view path)
}
bool isInDir(std::string_view path, std::string_view dir)
bool isInDir(const fs::path & path, const fs::path & dir)
{
return path.substr(0, 1) == "/"
&& path.substr(0, dir.size()) == dir
&& path.size() >= dir.size() + 2
&& path[dir.size()] == '/';
/* Note that while the standard doesn't guarantee this, the
`lexically_*` functions should do no IO and not throw. */
auto rel = path.lexically_relative(dir);
/* Method from
https://stackoverflow.com/questions/62503197/check-if-path-contains-another-in-c++ */
return !rel.empty() && rel.native()[0] != OS_STR('.');
}
bool isDirOrInDir(std::string_view path, std::string_view dir)
bool isDirOrInDir(const fs::path & path, const fs::path & dir)
{
return path == dir || isInDir(path, dir);
}