From fc2635d0ed10be94aceab22bad9c4fe2889353d4 Mon Sep 17 00:00:00 2001 From: chenyuanbo Date: Mon, 29 Jun 2026 14:25:40 +0800 Subject: [PATCH] fix: improve temporary background file creation in theme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit 72bacd97433a00de16921d933ae424dcc59f3fb1) --- grub2/theme_ifc.go | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/grub2/theme_ifc.go b/grub2/theme_ifc.go index e1aef3ee1..206a44818 100644 --- a/grub2/theme_ifc.go +++ b/grub2/theme_ifc.go @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -8,13 +8,11 @@ import ( "os" "os/exec" "path" - "path/filepath" "strings" "github.com/godbus/dbus/v5" "github.com/linuxdeepin/go-lib/dbusutil" "github.com/linuxdeepin/go-lib/utils" - dutils "github.com/linuxdeepin/go-lib/utils" ) const ( @@ -79,19 +77,35 @@ func (theme *Theme) GetBackground(sender dbus.Sender) (background string, busErr if len(background) == 0 { return "", nil - } else { + } - tmpBackground := "dde-grub-background" + path.Ext(defaultGrubBackground) - backGroundTmpPath := filepath.Join("/tmp", tmpBackground) - //copy file to /tmp - err := dutils.CopyFile(background, backGroundTmpPath) - if err != nil { - return "", dbusutil.ToError(err) - } - logger.Debugf("Copy file %s to %s", background, backGroundTmpPath) + ext := path.Ext(background) + backGroundTmpFile, err := os.CreateTemp("/tmp", "dde-grub-background-*"+ext) + if err != nil { + return "", dbusutil.ToError(err) + } - return backGroundTmpPath, nil + backGroundTmpPath := backGroundTmpFile.Name() + backGroundTmpFile.Close() + + err = utils.CopyFile(background, backGroundTmpPath) + if err != nil { + logger.Warningf("GetBackground: copy %q to %q failed: %v", background, backGroundTmpPath, err) + _ = os.Remove(backGroundTmpPath) + return "", dbusutil.ToError(err) + } + + err = os.Chmod(backGroundTmpPath, 0644) + if err != nil { + logger.Warningf("GetBackground: chmod %q 0644 failed: %v", backGroundTmpPath, err) + _ = os.Remove(backGroundTmpPath) + return "", dbusutil.ToError(err) } + logger.Debugf("Copy file %s to %s", background, backGroundTmpPath) + + // 已知问题:返回的临时文件由调用方使用,本服务不清理。每次调用产生一个 + // 唯一命名的新文件,重复调用会在 /tmp 中累积(依赖重启清空 tmpfs)。 + return backGroundTmpPath, nil } func (theme *Theme) emitSignalBackgroundChanged() {