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 @@ -10,9 +10,10 @@
#include "linglong/utils/log/log.h"
#include "linglong/utils/runtime_config.h"

#include <filesystem>

Check warning on line 13 in libs/linglong/tests/ll-tests/src/linglong/utils/runtime_config_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.
#include <fstream>

Check warning on line 14 in libs/linglong/tests/ll-tests/src/linglong/utils/runtime_config_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 <string>

Check warning on line 15 in libs/linglong/tests/ll-tests/src/linglong/utils/runtime_config_test.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 <system_error>

namespace fs = std::filesystem;
using namespace linglong::api::types::v1;
Expand Down Expand Up @@ -443,3 +444,54 @@
EXPECT_EQ(loadedConfig.devices->at(1), "vendor.com/device=10");
EXPECT_EQ(loadedConfig.devices->at(2), "vendor.com/device=20");
}

TEST(RuntimeConfigTest, LoadRuntimeConfigWithSymlinkLoop)
{
TempDir tempDir;

// A config.json pointing at itself makes the exists() probe fail with
// ELOOP, which must be reported as an error.
fs::path configDir = tempDir.path() / "linglong" / "apps" / "test-app";
fs::create_directories(configDir);

fs::path configFile = configDir / "config.json";
std::error_code ec;
fs::create_symlink(configFile, configFile, ec);
ASSERT_EQ(ec.value(), 0);

std::vector<std::filesystem::path> configDirs = { tempDir.path() / "linglong" };
auto result = linglong::utils::loadRuntimeConfig(configDirs, "test-app", "");
EXPECT_FALSE(result);
}

TEST(RuntimeConfigTest, LoadRuntimeConfigWithoutConfig)
{
TempDir tempDir;

fs::path configDir = tempDir.path() / "linglong";
fs::create_directories(configDir);

std::vector<std::filesystem::path> configDirs = { configDir };
auto result = linglong::utils::loadRuntimeConfig(configDirs, "test-app", "");
ASSERT_TRUE(result.has_value());
EXPECT_FALSE(result->has_value());
}

TEST(RuntimeConfigTest, LoadRuntimeConfigWithConfigDSymlinkLoop)
{
TempDir tempDir;

// A config.d pointing at itself makes the is_directory() probe fail with
// ELOOP, which must be reported as an error.
fs::path configDir = tempDir.path() / "linglong";
fs::create_directories(configDir);

fs::path configDPath = configDir / "config.d";
std::error_code ec;
fs::create_symlink(configDPath, configDPath, ec);
ASSERT_EQ(ec.value(), 0);

std::vector<std::filesystem::path> configDirs = { configDir };
auto result = linglong::utils::loadRuntimeConfig(configDirs, "", "");
EXPECT_FALSE(result);
}
19 changes: 17 additions & 2 deletions libs/utils/src/linglong/utils/runtime_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include "runtime_config.h"

#include "linglong/common/dir.h"
#include "linglong/utils/error/error.h"

Check warning on line 10 in libs/utils/src/linglong/utils/runtime_config.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/error/error.h" not found.
#include "linglong/utils/serialize/json.h"

Check warning on line 11 in libs/utils/src/linglong/utils/runtime_config.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/serialize/json.h" not found.

#include <fmt/format.h>

Check warning on line 13 in libs/utils/src/linglong/utils/runtime_config.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 14 in libs/utils/src/linglong/utils/runtime_config.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.
#include <algorithm>

Check warning on line 15 in libs/utils/src/linglong/utils/runtime_config.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>
#include <fstream>

Expand Down Expand Up @@ -38,7 +40,14 @@
std::vector<RuntimeConfigure> configs;
auto configPath = configBaseDir / "config.json";
std::error_code ec;
if (std::filesystem::exists(configPath, ec)) {
// file_type::none means the status itself could not be determined (e.g. ELOOP/EACCES);
// not_found with a set error_code is the regular "absent" case on POSIX.
auto configStatus = std::filesystem::status(configPath, ec);
if (configStatus.type() == std::filesystem::file_type::none) {
return LINGLONG_ERR(
fmt::format("failed to check runtime config {}: {}", configPath.string(), ec.message()));
}
if (std::filesystem::exists(configStatus)) {
auto config = linglong::utils::loadRuntimeConfig(configPath);
if (!config) {
return LINGLONG_ERR(config);
Expand All @@ -47,7 +56,13 @@
}

auto configDPath = configBaseDir / "config.d";
if (std::filesystem::is_directory(configDPath, ec)) {
auto configDStatus = std::filesystem::status(configDPath, ec);
if (configDStatus.type() == std::filesystem::file_type::none) {
return LINGLONG_ERR(fmt::format("failed to check runtime config drop-in dir {}: {}",
configDPath.string(),
ec.message()));
}
if (std::filesystem::is_directory(configDStatus)) {
std::vector<std::filesystem::path> paths;
for (const auto &entry : std::filesystem::directory_iterator(
configDPath,
Expand Down
Loading