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

Add makeParentCanonical()

This commit is contained in:
Robert Hensing 2024-12-13 18:37:25 +01:00 committed by Mic92
parent 6827768807
commit 69853c067c
3 changed files with 44 additions and 0 deletions

View file

@ -766,4 +766,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