mirror of
https://github.com/NixOS/nix
synced 2025-06-25 23:11:16 +02:00
{libutil,libstore}: Factor out chmodIfNeeded
Using std::filesystem::path directly because we need .c_str() anyway to interact with chmod. Path/string views don't have to be null-terminated.
This commit is contained in:
parent
c99edc840c
commit
82f337de10
4 changed files with 57 additions and 4 deletions
|
@ -776,4 +776,18 @@ std::filesystem::path makeParentCanonical(const std::filesystem::path & rawPath)
|
|||
}
|
||||
}
|
||||
|
||||
bool chmodIfNeeded(const fs::path & path, mode_t mode, mode_t mask)
|
||||
{
|
||||
auto pathString = path.string();
|
||||
auto prevMode = lstat(pathString).st_mode;
|
||||
|
||||
if (((prevMode ^ mode) & mask) == 0)
|
||||
return false;
|
||||
|
||||
if (chmod(pathString.c_str(), mode) != 0)
|
||||
throw SysError("could not set permissions on '%s' to %o", pathString, mode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue