1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-11 13:06:01 +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 Jörg Thalheim
parent 2c89c38fa1
commit fa3fd41063
3 changed files with 44 additions and 0 deletions

View file

@ -685,4 +685,19 @@ void moveFile(const Path & oldName, const Path & newName)
//////////////////////////////////////////////////////////////////////
std::filesystem::path makeParentCanonical(const std::filesystem::path & rawPath)
{
std::filesystem::path path(absPath(rawPath.string()));;
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