Skip to content

fix: improve desktop file path resolution in content command#1615

Merged
dengbo11 merged 2 commits into
OpenAtom-Linyaps:masterfrom
dengbo11:fix-content
Mar 17, 2026
Merged

fix: improve desktop file path resolution in content command#1615
dengbo11 merged 2 commits into
OpenAtom-Linyaps:masterfrom
dengbo11:fix-content

Conversation

@dengbo11

Copy link
Copy Markdown
Collaborator

Enhanced the content command to properly resolve desktop file paths by checking both default and overlay shared directories. Previously, desktop files were always treated as regular files without considering the actual exported locations.

Key changes:

  1. Added resolveDesktopFileExportPath method to OSTreeRepo that checks both default and overlay directories
  2. Modified content command to use this method for desktop files while keeping regular files unchanged
  3. Improved content filtering logic to only show existing exported files
  4. Added comprehensive unit tests covering both default and overlay directory scenarios

The fix ensures that desktop files are correctly resolved to their actual exported locations, preventing issues where desktop files might not be found if they exist in overlay directories but not in default directories.

Influence:

  1. Test content command with applications that have desktop files in default shared directory
  2. Test content command with applications that have desktop files only in overlay directory
  3. Verify both cases work correctly and show proper paths
  4. Test with applications that have no desktop files to ensure regular file handling remains unchanged
  5. Verify the output only includes existing files after path resolution

@dengbo11 dengbo11 requested review from ComixHe and myml March 16, 2026 09:11
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue in the content command where desktop files were not always correctly resolved, particularly when they resided in overlay directories. The changes introduce a robust mechanism to locate these files by searching both default and overlay shared directories, ensuring that the content command accurately reflects the exported paths of desktop files and improves the overall reliability of file listing.

Highlights

  • Desktop File Path Resolution: Added a new method, resolveDesktopFileExportPath, to OSTreeRepo to correctly identify desktop files by checking both default and overlay shared directories.
  • Content Command Enhancement: Modified the content command to utilize the new resolution method for desktop files, ensuring they are found in their actual exported locations.
  • Improved Content Filtering: Refined the logic for filtering content to ensure that only existing and exported files are displayed.
  • Comprehensive Unit Tests: Included new unit tests to cover scenarios where desktop files are located in default and overlay directories, validating the improved path resolution.
Changelog
  • libs/linglong/src/linglong/cli/cli.cpp
    • Modified the content command to conditionally resolve desktop file paths using a new repository method.
    • Refactored the content filtering loop to collect existing files into a new list, improving clarity and efficiency.
  • libs/linglong/src/linglong/repo/ostree_repo.cpp
    • Implemented the resolveDesktopFileExportPath method to determine the correct path for desktop files by prioritizing overlay directories if they exist.
  • libs/linglong/src/linglong/repo/ostree_repo.h
    • Declared the new resolveDesktopFileExportPath method within the OSTreeRepo class interface.
  • libs/linglong/tests/ll-tests/src/linglong/cli/cli_test.cpp
    • Included <filesystem> and <fstream> headers for file system operations in tests.
    • Added UnorderedElementsAre to the testing namespace for more flexible test assertions.
    • Introduced defaultSharedDir() and overlaySharedDir() helper methods to MockRepo for test setup.
    • Added mock methods for clearReference, getLayerItem, and printContent to facilitate testing.
    • Created two new test cases, contentPreferDesktopFromDefaultSharedDir and contentFallbackDesktopToOverlaySharedDir, to validate the desktop file path resolution logic.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request significantly improves the content command by correctly resolving desktop file paths, considering both default and overlay shared directories. The introduction of the resolveDesktopFileExportPath method in OSTreeRepo and its integration into the cli.cpp ensures that desktop files are found in their actual exported locations. Additionally, the filtering logic for exported content has been refactored for better efficiency. Comprehensive unit tests have been added to cover various scenarios, ensuring the robustness of the new functionality.

Comment thread libs/linglong/src/linglong/cli/cli.cpp Outdated
contents.append(entryPath);
}

// replace $LINGLONG_ROOT/layers/appid/verison/arch/module/entries to ${LINGLONG_ROOT}/entires

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

The comment here describes a general replacement, but the replaceInStrings call specifically targets entriesDir.absolutePath(). While the current implementation works, updating the comment to reflect the exact paths being replaced would improve clarity for future maintainers.

return defaultDesktopPath;
}

return overlayDesktopPath;

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

The resolveDesktopFileExportPath method returns overlayDesktopPath even if neither the overlay nor the default desktop file exists. While the calling code in cli.cpp filters out non-existent paths, it might be more explicit for this method to return an empty QString or an std::optional<QString> if no existing file is found. This would clearly communicate that no valid, existing path was resolved, making the method's contract more robust for future uses.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the content command to properly resolve desktop file paths by checking both the default and overlay shared directories. Previously, desktop files from the layer entries were treated as regular files without considering their actual exported location. The PR also improves the filtering loop to avoid an index-shifting bug in the original removeAt approach.

Changes:

  • Added resolveDesktopFileExportPath method to OSTreeRepo that checks both default and overlay shared directories for desktop files
  • Modified the content command in Cli to use this new method for .desktop files, and refactored the export filtering to use a separate list instead of in-place removal
  • Added unit tests for both default-preferred and overlay-fallback desktop file resolution scenarios

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
libs/linglong/src/linglong/repo/ostree_repo.h Declared the new resolveDesktopFileExportPath public method
libs/linglong/src/linglong/repo/ostree_repo.cpp Implemented resolveDesktopFileExportPath to resolve desktop files from default or overlay shared dirs
libs/linglong/src/linglong/cli/cli.cpp Updated content() to resolve desktop file paths and improved export filtering logic
libs/linglong/tests/ll-tests/src/linglong/cli/cli_test.cpp Added tests for default-preferred and overlay-fallback desktop file resolution, plus new mock methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
Comment on lines +3061 to +3068
if (overlayDesktopPath != defaultDesktopPath && QFileInfo::exists(overlayDesktopPath)) {
return overlayDesktopPath;
}

if (QFileInfo::exists(defaultDesktopPath)) {
return defaultDesktopPath;
}

@dengbo11 dengbo11 marked this pull request as draft March 17, 2026 01:43
myml
myml previously approved these changes Mar 17, 2026
@dengbo11 dengbo11 force-pushed the fix-content branch 2 times, most recently from 902350f to bf74fe1 Compare March 17, 2026 07:58
@dengbo11

Copy link
Copy Markdown
Collaborator Author

pre-commit.ci autofix

@codecov

codecov Bot commented Mar 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.63636% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
libs/linglong/src/linglong/cli/cli.cpp 35.71% 0 Missing and 9 partials ⚠️
libs/linglong/src/linglong/repo/ostree_repo.cpp 79.31% 2 Missing and 4 partials ⚠️
apps/ll-builder/src/main.cpp 0.00% 1 Missing ⚠️
Files with missing lines Coverage Δ
libs/linglong/src/linglong/repo/ostree_repo.h 9.09% <ø> (ø)
apps/ll-builder/src/main.cpp 0.00% <0.00%> (ø)
libs/linglong/src/linglong/repo/ostree_repo.cpp 12.63% <79.31%> (+1.84%) ⬆️
libs/linglong/src/linglong/cli/cli.cpp 1.79% <35.71%> (+0.78%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dengbo11 dengbo11 marked this pull request as ready for review March 17, 2026 08:20
myml
myml previously approved these changes Mar 17, 2026
@dengbo11 dengbo11 requested a review from myml March 17, 2026 08:21
@dengbo11 dengbo11 force-pushed the fix-content branch 2 times, most recently from bd19f3a to 3185f8f Compare March 17, 2026 08:46
Enhanced the content command to properly resolve desktop file paths
by checking both default and overlay shared directories. Previously,
desktop files were always treated as regular files without considering
the actual exported locations.

Key changes:
1. Added resolveDesktopFileExportPath method to OSTreeRepo that checks
both default and overlay directories
2. Modified content command to use this method for desktop files while
keeping regular files unchanged
3. Improved content filtering logic to only show existing exported files
4. Added comprehensive unit tests covering both default and overlay
directory scenarios

The fix ensures that desktop files are correctly resolved to their
actual exported locations, preventing issues where desktop files might
not be found if they exist in overlay directories but not in default
directories.

Influence:
1. Test content command with applications that have desktop files in
default shared directory
2. Test content command with applications that have desktop files only
in overlay directory
3. Verify both cases work correctly and show proper paths
4. Test with applications that have no desktop files to ensure regular
file handling remains unchanged
5. Verify the output only includes existing files after path resolution
@dengbo11

Copy link
Copy Markdown
Collaborator Author

pre-commit.ci autofix

@dengbo11 dengbo11 force-pushed the fix-content branch 2 times, most recently from 00ff6f5 to 9eecaa1 Compare March 17, 2026 12:10
@dengbo11

Copy link
Copy Markdown
Collaborator Author

pre-commit.ci autofix

Replace all uses of mkdtemp() with the TempDir utility class across
multiple test files. This change improves test reliability by ensuring
proper cleanup of temporary directories and provides better error
handling. The TempDir class automatically handles directory creation and
cleanup, reducing the risk of leftover test artifacts and making test
code more maintainable.

Key changes:
1. Replace mkdtemp calls with std::make_unique<TempDir> in application
singleton test
2. Update builder mock to use TempDir for OSTree repository
3. Refactor layer packager test to use TempDir for layer directories
4. Improve UAB file test with proper TempDir management
5. Update file utility tests to use TempDir for source and destination
6. Enhance package info handler test with TempDir for temporary files

Influence:
1. Verify all tests still pass with the new TempDir implementation
2. Check that temporary directories are properly cleaned up after tests
3. Test directory creation and validation in each test case
4. Verify no leftover temporary directories after test execution
5. Confirm error handling works correctly when directory creation fails
@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

deepin pr auto review

这段代码主要涉及对应用层导出路径(entries)的处理逻辑,特别是针对 .desktop 文件和 systemd 用户单元文件的路径解析。此外,还包括对测试代码中临时目录管理方式的改进。

以下是对代码的详细审查和改进意见:

1. 语法逻辑与代码质量

文件:libs/linglong/src/linglong/cli/cli.cpp

  • 逻辑改进:路径处理
    Cli::content 方法中,代码逻辑从简单的字符串替换(replaceInStrings)改为基于 std::filesystem 的路径解析和映射。这是一个很好的改进,提高了代码的健壮性。

    • 观察QDir entriesDir((layer->path() / "entries").c_str()); 这里将 entries 目录作为遍历起点。
    • 建议:确认 resolveEntryExportPath 的逻辑是否覆盖了所有必要的导出场景。目前的逻辑似乎专注于 share/applicationssystemd/user,如果未来有其他类型的文件(如图标、mime info等)需要导出,需要确保该函数或其调用逻辑能够处理。
  • 逻辑改进:过滤目录

    if (!info.exists() || info.isDir()) {
        continue;
    }

    这段代码显式过滤掉了目录,只保留文件。这与之前的逻辑(仅检查 exists)不同。

    • 建议:确认业务需求。如果 ll-cli content 的目的是列出"应用提供的导出项",通常列出文件是合理的。但如果某些导出项本身是目录结构(例如特定的配置目录),则可能需要保留。

文件:libs/linglong/src/linglong/repo/ostree_repo.cpp

  • 新增函数:resolveEntryExportPath
    该函数负责将层内的相对路径映射到最终的导出路径。

    • 逻辑:处理了 .desktop 文件的委托、systemd/user 的新旧路径映射。
    • 建议
      • 类型安全auto relative = relativePath.generic_string();path 转为 string 进行字符串匹配。虽然可行,但尽量使用 std::filesystem 的路径操作(如 lexically_relative, begin()/end() 迭代器)会更符合 C++17 的风格,也更安全。例如,检查前缀可以使用 relativePath.begin() 迭代器。
      • 常量定义shareSystemdUserlibSystemdUser 定义为 constexpr std::string_view 是好的做法。
  • 新增函数:resolveDesktopFileExportPath

    • 逻辑:优先检查 overlay 目录,如果不存在则回退到 default 目录。
    • 建议:逻辑清晰,但要注意 std::filesystem::exists 可能会受到符号链接的影响。如果业务逻辑需要区分符号链接和真实文件,需额外处理。

文件:libs/linglong/src/linglong/repo/ostree_repo.h

  • 声明:新增了两个 noexcept 方法。
    • 建议:方法声明使用了 [[nodiscard]],这很好,强制调用者检查返回值。

2. 代码性能

  • 文件系统访问
    resolveDesktopFileExportPathCli::content 中使用了 std::filesystem::exists
    • 分析:在循环中(如 Cli::contentwhile (it.hasNext()))频繁调用 exists 可能会导致性能问题,特别是如果文件系统是网络存储或较慢的磁盘。
    • 建议
      • Cli::content 中,QDirIterator 已经在遍历文件系统。resolveEntryExportPath 内部又可能触发 exists(间接通过 resolveDesktopFileExportPath)。
      • 优化方案:如果 resolveEntryExportPath 仅仅是做路径映射计算而不检查文件存在性,性能会更好。文件存在性的检查(如 exportedContents 的过滤)应该统一在遍历阶段或最后阶段进行,避免重复 I/O。
      • 对于 resolveDesktopFileExportPath,如果 overlay 和 default 目录结构相对固定,可以考虑缓存目录的存在性状态,或者由上层调用者保证只传入需要检查的路径。

3. 代码安全

  • 路径遍历安全

    • 分析:代码使用了 std::filesystem::path,这比纯字符串拼接更安全,能自动处理分隔符。
    • 建议:在 resolveEntryExportPath 中,虽然使用了 lexically_relative,但仍需确保 relativePath 是合法的相对路径,不包含 .. 跳出层目录的攻击向量。虽然输入通常来自受控的层文件,但作为防御性编程,可以考虑添加断言或检查。
  • 临时目录清理

    • 分析:测试代码中大量使用了 TempDir 类(RAII 风格)替代原来的 mkdtemp + 手动 remove_all
    • 评价:这是一个非常好的改进。使用 RAII 管理资源可以防止测试异常退出时临时文件残留,避免了“磁盘空间满”或“权限冲突”等问题,提高了测试的安全性和稳定性。

4. 测试代码改进

文件:libs/linglong/tests/ll-tests/src/linglong/cli/cli_test.cpp

  • Mock 增强:增加了 clearReference, getLayerItem 等方法的 Mock,使得单元测试更加独立,不依赖真实的仓库操作。
  • 测试覆盖:新增了针对 content 命令的四个测试用例,覆盖了默认路径、Overlay 路径、旧版 Systemd 路径映射以及优先级逻辑。
    • 建议:测试用例非常完善。确保在 CI/CD 中运行这些测试。

文件:libs/linglong/tests/ll-tests/src/.../..._test.cpp (多个文件)

  • 重构:将 char tempPath[] = "...XXXXXX"; mkdtemp(tempPath); 替换为 TempDir 类。
    • 优点:代码更简洁,异常安全。
    • 注意:确保 TempDir 类的实现是线程安全的(如果在多线程测试中使用),并且其析构函数能够正确处理清理失败的情况(例如打印警告但不抛异常)。

总结

这段代码的主要改动是对应用内容导出逻辑的重构和测试代码的资源管理优化。

  1. 核心逻辑:从字符串操作升级为路径对象操作,逻辑更清晰,支持了更复杂的路径映射需求(如 systemd user unit 的迁移)。
  2. 性能:需注意文件系统 I/O 操作(exists)在循环中的调用频率。
  3. 安全:路径处理相对安全,测试代码的 RAII 改进显著提升了测试环境的安全性。
  4. 测试:单元测试覆盖率高,Mock 设计合理。

具体修改建议:
ostree_repo.cppresolveEntryExportPath 中,如果性能是瓶颈,可以尝试减少字符串转换和 exists 检查。目前的逻辑对于功能正确性是很好的。

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengbo11, myml

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dengbo11 dengbo11 merged commit 5b1db05 into OpenAtom-Linyaps:master Mar 17, 2026
17 checks passed
@dengbo11 dengbo11 deleted the fix-content branch July 1, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants