Skip to content

feat: Add the upgrade clean command to clear incremental update cache…#240

Merged
zhaohuiw42 merged 1 commit into
masterfrom
opt_upgrade_clean
Oct 23, 2025
Merged

feat: Add the upgrade clean command to clear incremental update cache…#240
zhaohuiw42 merged 1 commit into
masterfrom
opt_upgrade_clean

Conversation

@Fire-dtx
Copy link
Copy Markdown
Contributor

feat: Add the upgrade clean command to clear incremental update cache files.

Description: Add the upgrade clean command to clear incremental update cache files.

Log: Add the upgrade clean command to clear incremental update cache files.

Task: https://pms.uniontech.com/task-view-382931.html

Influence: Clearing of incremental update cache files

… files.

Description: Add the upgrade clean command to clear incremental update cache files.

Log: Add the upgrade clean command to clear incremental update cache files.

Task: https://pms.uniontech.com/task-view-382931.html

Influence: Clearing of incremental update cache files
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

我来对这段代码进行审查:

  1. 语法逻辑:
  • 代码语法正确,没有明显的语法错误。
  • 增量更新的检查逻辑清晰,使用配置文件判断是否需要执行清理操作。
  1. 代码质量改进建议:
  • 配置文件路径硬编码为"/var/lib/lastore",建议将其提取为常量。
  • 错误处理可以更加详细,目前只是打印警告日志,可以增加错误类型判断。
  • 建议对config.IncrementalUpdate的判断添加日志,方便调试和追踪。
  1. 代码性能改进:
  • NewConfig函数每次都会读取配置文件,如果这是频繁操作,建议考虑缓存配置。
  • exec.Command创建的命令没有设置超时,可能会无限等待,建议添加超时控制。
  1. 代码安全改进:
  • 执行外部命令时没有验证deepin-immutable-ctl命令的真实性,建议检查命令路径和权限。
  • 配置文件路径使用path.Join拼接,但没有验证路径的合法性,可能存在路径遍历风险。

改进后的代码建议:

const (
    configDir = "/var/lib/lastore"
    configFile = "config.json"
    cleanCommand = "deepin-immutable-ctl"
    cleanTimeout = 30 * time.Second
)

func main() {
    // ... 其他代码 ...

    // 如果是增量更新,则调用deepin-immutable-ctl upgrade cleanup命令清理immutable系统的缓存deb包和ostree包分支
    configPath := path.Join(configDir, configFile)
    if err := validatePath(configPath); err != nil {
        logger.Errorf("Invalid config path: %v", err)
        return
    }
    
    config := NewConfig(configPath)
    logger.Infof("IncrementalUpdate config: %v", config.IncrementalUpdate)
    
    if config.IncrementalUpdate {
        cmdPath, err := exec.LookPath(cleanCommand)
        if err != nil {
            logger.Errorf("Failed to find %s command: %v", cleanCommand, err)
            return
        }
        
        ctx, cancel := context.WithTimeout(context.Background(), cleanTimeout)
        defer cancel()
        
        cmd := exec.CommandContext(ctx, cmdPath, "upgrade", "clean")
        if err := cmd.Run(); err != nil {
            if ctx.Err() == context.DeadlineExceeded {
                logger.Errorf("Cleanup command timed out after %v", cleanTimeout)
            } else {
                logger.Warnf("Failed to clean upgrade cache: %v", err)
            }
        }
    }

    // ... 其他代码 ...
}

func validatePath(p string) error {
    cleanPath := filepath.Clean(p)
    if !strings.HasPrefix(cleanPath, configDir) {
        return fmt.Errorf("invalid path: %s", p)
    }
    return nil
}

主要改进点:

  1. 提取常量,提高代码可维护性
  2. 添加路径验证,增加安全性
  3. 添加命令超时控制,避免无限等待
  4. 增加更详细的日志记录
  5. 添加命令存在性检查
  6. 改进错误处理机制

这些改进可以提高代码的安全性、可靠性和可维护性。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Fire-dtx, zhaohuiw42

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

@zhaohuiw42 zhaohuiw42 merged commit 794d66b into master Oct 23, 2025
24 of 28 checks passed
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.

3 participants