1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41: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:
Sergei Zimmerman 2025-03-05 02:43:11 +03:00
parent c99edc840c
commit 82f337de10
4 changed files with 57 additions and 4 deletions

View file

@ -275,4 +275,30 @@ TEST(makeParentCanonical, root)
{
ASSERT_EQ(makeParentCanonical("/"), "/");
}
/* ----------------------------------------------------------------------------
* chmodIfNeeded
* --------------------------------------------------------------------------*/
TEST(chmodIfNeeded, works)
{
auto [autoClose_, tmpFile] = nix::createTempFile();
auto deleteTmpFile = AutoDelete(tmpFile);
auto modes = std::vector<mode_t>{0755, 0644, 0422, 0600, 0777};
for (mode_t oldMode : modes) {
for (mode_t newMode : modes) {
chmod(tmpFile.c_str(), oldMode);
bool permissionsChanged = false;
ASSERT_NO_THROW(permissionsChanged = chmodIfNeeded(tmpFile, newMode));
ASSERT_EQ(permissionsChanged, oldMode != newMode);
}
}
}
TEST(chmodIfNeeded, nonexistent)
{
ASSERT_THROW(chmodIfNeeded("/schnitzel/darmstadt/pommes", 0755), SysError);
}
}