Skip to content

fix: improve temporary background file creation in theme#1159

Open
52cyb wants to merge 1 commit into
linuxdeepin:masterfrom
52cyb:tmppath
Open

fix: improve temporary background file creation in theme#1159
52cyb wants to merge 1 commit into
linuxdeepin:masterfrom
52cyb:tmppath

Conversation

@52cyb

@52cyb 52cyb commented Jun 29, 2026

Copy link
Copy Markdown
Contributor
  1. Removed unused imports (filepath, utils) and cleaned up import references
  2. Changed temporary file creation from hardcoded path to using os.CreateTemp with a safe filename pattern
  3. Simplified the if-else logic by removing unnecessary else block and using early return
  4. Used path.Ext for extension extraction instead of filepath.Ext

This refactoring ensures that temporary background files are created using Go's standard safe method, which provides random naming to avoid collisions and potential race conditions. The previous approach used a hardcoded filename which could cause conflicts if multiple instances run simultaneously.

Influence:

  1. Verify that setting a background source file works correctly via DBus
  2. Test getting the background returns a valid temporary file path
  3. Verify the temporary file has the correct extension matching the source
  4. Check that temporary files are properly cleaned up
  5. Test concurrent operations to ensure no file name collisions
  6. Verify that the temporary file is readable and contains valid image data

refactor: 改进主题中的临时背景文件创建

  1. 移除未使用的导入(filepath, utils)并清理导入引用
  2. 将临时文件创建从硬编码路径改为使用 os.CreateTemp 并使用安全的文件名 模式
  3. 通过移除不必要的 else 块和使用提前返回来简化 if-else 逻辑
  4. 使用 path.Ext 替代 filepath.Ext 进行扩展名提取

这次重构确保使用 Go 标准的安全方法创建临时背景文件,通过随机命名避免冲突
和潜在的竞争条件。之前的方法使用硬编码文件名,如果多个实例同时运行可能导
致冲突。

Influence:

  1. 验证通过 DBus 设置背景源文件功能正常
  2. 测试获取背景返回有效的临时文件路径
  3. 验证临时文件具有与源文件匹配的正确扩展名
  4. 检查临时文件是否被正确清理
  5. 测试并发操作以确保文件名无冲突
  6. 验证临时文件可读且包含有效的图像数据

PMS: BUG-367561

Summary by Sourcery

Improve temporary background file handling in the GRUB theme interface.

Bug Fixes:

  • Prevent background temporary file name collisions by using os.CreateTemp with randomized filenames.

Enhancements:

  • Simplify background retrieval logic with early return and safer extension handling.
  • Clean up imports and URI decoding usage in the theme background APIs.

@sourcery-ai

sourcery-ai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors temporary background file handling in the GRUB theme interface to use Go’s safe temporary file creation, cleans up imports, and simplifies control flow for background retrieval and setting via DBus.

Sequence diagram for GetBackground temporary file creation

sequenceDiagram
  actor DBusClient
  participant Theme
  participant os
  participant dutils

  DBusClient->>Theme: GetBackground(sender)
  alt background is empty
    Theme-->>DBusClient: "" (nil error)
  else background is non-empty
    Theme->>Theme: path.Ext(background)
    Theme->>os: CreateTemp("/tmp", "dde-grub-background-*" + ext)
    os-->>Theme: backGroundTmpFile
    Theme->>Theme: backGroundTmpFile.Name()
    Theme->>Theme: backGroundTmpFile.Close()
    Theme->>dutils: CopyFile(background, backGroundTmpPath)
    dutils-->>Theme: (error or nil)
    Theme-->>DBusClient: backGroundTmpPath (wrapped error if any)
  end
Loading

File-Level Changes

Change Details Files
Use os.CreateTemp with randomized filenames to create temporary background files instead of a hardcoded path.
  • Replaced construction of a fixed temporary filename with extension-based pattern extraction using path.Ext on the background path.
  • Added os.CreateTemp call targeting /tmp with a "dde-grub-background-*" prefix plus the original file extension.
  • Closed the created temporary file handle immediately after acquiring its name.
  • Used the generated temporary path as the destination for copying the background image via dutils.CopyFile.
  • Kept logging of the copy operation but updated it to reference the new temporary path.
grub2/theme_ifc.go
Simplify GetBackground control flow with early return when no background is set.
  • Changed the GetBackground function to return immediately when background is empty instead of wrapping the remainder of the logic in an else block.
  • Adjusted subsequent code layout to be top-level after the empty-check, improving readability.
grub2/theme_ifc.go
Clean up imports and fix DecodeURI usage to use the correct utils alias.
  • Removed unused imports for path/filepath and github.com/linuxdeepin/go-lib/utils.
  • Ensured DecodeURI is called via the existing dutils alias instead of the removed utils import in SetBackgroundSourceFile.
  • Relied on path.Ext from the standard path package for extension handling now that filepath is removed.
grub2/theme_ifc.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've left some high level feedback:

  • The switch to os.CreateTemp is good, but since you immediately close the file and then call dutils.CopyFile, double‑check that CopyFile behaves correctly when the destination file already exists; if it assumes a non‑existent target you may want to generate the name with CreateTemp then remove the file before copying.
  • Consider using os.TempDir() instead of the hardcoded "/tmp" to remain consistent with Go’s temp directory conventions, unless there is a deliberate requirement for using /tmp specifically in this environment.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The switch to os.CreateTemp is good, but since you immediately close the file and then call dutils.CopyFile, double‑check that CopyFile behaves correctly when the destination file already exists; if it assumes a non‑existent target you may want to generate the name with CreateTemp then remove the file before copying.
- Consider using os.TempDir() instead of the hardcoded "/tmp" to remain consistent with Go’s temp directory conventions, unless there is a deliberate requirement for using /tmp specifically in this environment.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@52cyb 52cyb force-pushed the tmppath branch 2 times, most recently from 90e921f to b651c59 Compare June 29, 2026 06:38
@deepin-ci-robot

Copy link
Copy Markdown

Hi @52cyb. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 52cyb

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

1 similar comment
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 52cyb

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

@52cyb 52cyb force-pushed the tmppath branch 2 times, most recently from 0c1f187 to 5fe8b4d Compare July 2, 2026 02:44
1. Removed unused imports (filepath, utils) and cleaned up import
references
2. Changed temporary file creation from hardcoded path to using
os.CreateTemp with a safe filename pattern
3. Simplified the if-else logic by removing unnecessary else block and
using early return
4. Used path.Ext for extension extraction instead of filepath.Ext

This refactoring ensures that temporary background files are created
using Go's standard safe method, which provides random naming to avoid
collisions and potential race conditions. The previous approach used
a hardcoded filename which could cause conflicts if multiple instances
run simultaneously.

Influence:
1. Verify that setting a background source file works correctly via DBus
2. Test getting the background returns a valid temporary file path
3. Verify the temporary file has the correct extension matching the
source
4. Check that temporary files are properly cleaned up
5. Test concurrent operations to ensure no file name collisions
6. Verify that the temporary file is readable and contains valid image
data

refactor: 改进主题中的临时背景文件创建

1. 移除未使用的导入(filepath, utils)并清理导入引用
2. 将临时文件创建从硬编码路径改为使用 os.CreateTemp 并使用安全的文件名
模式
3. 通过移除不必要的 else 块和使用提前返回来简化 if-else 逻辑
4. 使用 path.Ext 替代 filepath.Ext 进行扩展名提取

这次重构确保使用 Go 标准的安全方法创建临时背景文件,通过随机命名避免冲突
和潜在的竞争条件。之前的方法使用硬编码文件名,如果多个实例同时运行可能导
致冲突。

Influence:
1. 验证通过 DBus 设置背景源文件功能正常
2. 测试获取背景返回有效的临时文件路径
3. 验证临时文件具有与源文件匹配的正确扩展名
4. 检查临时文件是否被正确清理
5. 测试并发操作以确保文件名无冲突
6. 验证临时文件可读且包含有效的图像数据

PMS: BUG-367561
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants