Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
#include "../../common/tempdir.h"
#include "linglong/api/types/v1/Generators.hpp"
#include "linglong/package/fuzzy_reference.h"
#include "linglong/package/layer_dir.h"

Check warning on line 11 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/package/layer_dir.h" not found.
#include "linglong/package/reference.h"

Check warning on line 12 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/package/reference.h" not found.
#include "linglong/repo/ostree_repo.h"

Check warning on line 13 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/repo/ostree_repo.h" not found.
#include "linglong/utils/file.h"
#include "linglong/utils/finally/finally.h"

Check warning on line 15 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <filesystem> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <fstream> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <filesystem>

Check warning on line 17 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <memory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream>

Check warning on line 18 in libs/linglong/tests/ll-tests/src/linglong/repo/ostree_repo_real_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <optional> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <memory>
#include <optional>

#include <unistd.h>

namespace linglong::repo::test {

namespace fs = std::filesystem;
Expand Down Expand Up @@ -75,7 +79,8 @@
const std::string &version,
const std::string &kind,
const std::string &module,
const std::vector<std::string> &relFiles)
const std::vector<std::string> &relFiles,
const std::vector<std::string> &readOnlyDirs = {})
{
std::error_code ec;
auto layerDir = repoRoot / ("layersrc-" + id);
Expand All @@ -101,6 +106,18 @@
nlohmann::json j = info;
std::ofstream{ layerDir / "info.json" } << j.dump();

for (const auto &rel : readOnlyDirs) {
fs::permissions(layerDir / rel,
fs::perms::owner_write | fs::perms::group_write
| fs::perms::others_write,
fs::perm_options::remove,
ec);
if (ec) {
ADD_FAILURE() << "set directory permissions failed: " << ec.message();
return std::nullopt;
}
}

LayerDir ld(layerDir);
if (!ld.valid()) {
ADD_FAILURE() << "layer dir should be valid";
Expand Down Expand Up @@ -353,6 +370,50 @@
fs::remove_all(tmp->path(), ec);
}

TEST_F(RealRepoTest, TempMergedModuleDirCleansReadOnlyPackageDirectories)
{
if (::geteuid() == 0) {
GTEST_SKIP() << "root bypasses the directory permissions under test";
}

// Restore fixture directories even when the original cleanup leaves files behind.
auto cleanup = utils::finally::finally([&] {
std::ignore = utils::makeDirectoryTreeRemovable(repoRoot);
});

auto develop = importLayer("com.example.readonly",
"1.0.0",
"app",
"develop",
{ "files/usr/include/readonly.h" });
ASSERT_TRUE(develop.has_value());
const std::string readOnlyDir = "files/usr/share/readonly";
auto binary = importLayer("com.example.readonly",
"1.0.0",
"app",
"binary",
{ readOnlyDir + "/data" },
{ readOnlyDir });
ASSERT_TRUE(binary.has_value());
auto originalLayer = repo->getLayerDir(*binary, "binary");
ASSERT_TRUE(originalLayer.has_value());
const auto originalFile = originalLayer->path() / readOnlyDir / "data";
const auto originalPermissions = fs::status(originalFile).permissions();

fs::path temporaryPath;
{
auto merged = repo->createTempMergedModuleDir(*binary, { "binary", "develop" });
ASSERT_TRUE(merged.has_value()) << merged.error().message();
temporaryPath = merged->path();
ASSERT_TRUE(fs::exists(temporaryPath / readOnlyDir / "data"));
ASSERT_EQ(fs::status(temporaryPath / readOnlyDir).permissions() & fs::perms::owner_write,
fs::perms::none);
}

EXPECT_FALSE(fs::exists(temporaryPath));
EXPECT_EQ(fs::status(originalFile).permissions(), originalPermissions);
}

TEST_F(RealRepoTest, ListLocalByQuery)
{
auto all = repo->listLocalBy(repoCacheQuery{ .id = "com.example.app" });
Expand Down
17 changes: 17 additions & 0 deletions libs/utils/src/linglong/utils/temporary_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include "temporary_directory.h"

#include "linglong/utils/file.h"

Check warning on line 6 in libs/utils/src/linglong/utils/temporary_directory.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cerrno> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "linglong/utils/log/log.h"

Check warning on line 7 in libs/utils/src/linglong/utils/temporary_directory.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstring> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 8 in libs/utils/src/linglong/utils/temporary_directory.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <string> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cerrno>
#include <cstring>
#include <string>
Expand Down Expand Up @@ -83,6 +86,20 @@

std::error_code ec;
std::filesystem::remove_all(directory, ec);
if (ec) {
auto removable = makeDirectoryTreeRemovable(directory);
if (removable) {
ec.clear();
std::filesystem::remove_all(directory, ec);
} else {
LogW("failed to make temporary directory {} removable: {}",
directory,
removable.error());
}
if (ec) {
LogW("failed to remove temporary directory {}: {}", directory, ec.message());
}
}
directory.clear();
}

Expand Down
Loading