Skip to content
Merged
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
15 changes: 12 additions & 3 deletions libs/linglong/src/linglong/builder/linglong_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@
namespace detail {
void mergeOutput(const std::vector<std::filesystem::path> &src,
const std::filesystem::path &dest,
const std::vector<std::string> &targets)
const std::vector<std::string> &targets,
const std::vector<std::string> &excludes)
{
for (const auto &dir : src) {
LogD("merge {} to {}", dir, dest);
auto matcher = [&dir, &targets](const std::filesystem::path &path) {
auto matcher = [&dir, &targets, &excludes](const std::filesystem::path &path) {
if (common::strings::starts_with(path.filename().string(), ".wh.")) {
return false;
}
Expand All @@ -171,7 +172,12 @@
if (st.st_size == 0 && st.st_rdev == 0 && st.st_mode == S_IFCHR) {
return false;
}

Check warning on line 175 in libs/linglong/src/linglong/builder/linglong_builder.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::any_of algorithm instead of a raw loop.
for (const auto &exclude : excludes) {
if (common::strings::starts_with(path.string(), exclude)) {
return false;
}
Comment on lines +177 to +179

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

当前的路径排除逻辑 common::strings::starts_with(path.string(), exclude) 存在两个潜在问题:

  1. 精确性不足:它会错误地匹配前缀相同的路径。例如,若排除规则为 "lib/foo",路径 "lib/foobar/some.file" 也会被错误地排除。
  2. 平台依赖path.string() 在不同平台(如 Windows)上可能返回带反斜杠的路径,导致与使用正斜杠的 exclude 规则匹配失败。

建议改用 path.generic_string() 并检查完整的目录匹配,以提高健壮性和平台兼容性。

if (auto path_str = path.generic_string(); path_str == exclude || common::strings::starts_with(path_str, exclude + "/")) {
    return false;
}

}
for (const auto &target : targets) {
if (common::strings::starts_with(path.string(), target)) {
return true;
Expand Down Expand Up @@ -959,7 +965,10 @@
src.push_back(runtimeOverlay->upperDirPath());
}
}
detail::mergeOutput(src, buildOutput, { "bin/", "sbin/", "lib/" });
detail::mergeOutput(src,
buildOutput,
{ "bin/", "sbin/", "lib/" },
{ "lib/systemd", "share/systemd" });

return LINGLONG_OK;
}
Expand Down
3 changes: 2 additions & 1 deletion libs/linglong/src/linglong/builder/linglong_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ utils::error::Result<void> cmdRemoveApp(repo::OSTreeRepo &repo,
namespace detail {
void mergeOutput(const std::vector<std::filesystem::path> &src,
const std::filesystem::path &dest,
const std::vector<std::string> &targets);
const std::vector<std::string> &targets,
const std::vector<std::string> &excludes);
}

class Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <gtest/gtest.h>

Check warning on line 4 in libs/linglong/tests/ll-tests/src/linglong/builder/linglong_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "../../common/tempdir.h"
#include "../mocks/linglong_builder_mock.h"
Expand Down Expand Up @@ -111,10 +111,12 @@
const auto &srcPath1 = srcDir1.path();
std::filesystem::create_directories(srcPath1 / "bin");
std::filesystem::create_directories(srcPath1 / "lib");
std::filesystem::create_directories(srcPath1 / "lib/systemd");
std::filesystem::create_directories(srcPath1 / "share");

std::ofstream(srcPath1 / "bin/tool1");
std::ofstream(srcPath1 / "lib/libfoo.so");
std::ofstream(srcPath1 / "lib/systemd/dbus.service");
std::ofstream(srcPath1 / "share/data1");
std::ofstream(srcPath1 / ".wh.somefile");

Expand All @@ -130,7 +132,10 @@
// Define merge targets
std::vector<std::string> targets = { "bin/", "share/data1" };

linglong::builder::detail::mergeOutput({ srcPath1, srcPath2 }, destDir.path(), targets);
linglong::builder::detail::mergeOutput({ srcPath1, srcPath2 },
destDir.path(),
targets,
{ "lib/systemd" });

const auto &destPath = destDir.path();
// Check files are merged
Expand All @@ -143,6 +148,7 @@
EXPECT_FALSE(std::filesystem::exists(destPath / "lib/libbar.so"));
EXPECT_FALSE(std::filesystem::exists(destPath / "share/data2"));
EXPECT_FALSE(std::filesystem::exists(destPath / ".wh.somefile"));
EXPECT_FALSE(std::filesystem::exists(destPath / "lib/systemd/dbus.service"));
}

TEST(LinglongBuilder, UabExportFilename)
Expand Down
Loading