1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-17 07:52:43 +01:00

Add makeParentCanonical()

(cherry picked from commit 69853c067c)
This commit is contained in:
Robert Hensing 2024-12-13 18:37:25 +01:00 committed by Mergify
parent 8abff3cf05
commit 0213f22650
3 changed files with 44 additions and 0 deletions

View file

@ -765,4 +765,19 @@ bool isExecutableFileAmbient(const fs::path & exe) {
) == 0;
}
std::filesystem::path makeParentCanonical(const std::filesystem::path & rawPath)
{
std::filesystem::path path(absPath(rawPath));;
try {
auto parent = path.parent_path();
if (parent == path) {
// `path` is a root directory => trivially canonical
return parent;
}
return std::filesystem::canonical(parent) / path.filename();
} catch (fs::filesystem_error & e) {
throw SysError("canonicalising parent path of '%1%'", path);
}
}
} // namespace nix