Skip to content

fix: derive P2P update support from delivery repo prefix#378

Merged
electricface merged 1 commit intolinuxdeepin:develop/intranet-updatefrom
electricface:swt/setP2PSupport
Apr 18, 2026
Merged

fix: derive P2P update support from delivery repo prefix#378
electricface merged 1 commit intolinuxdeepin:develop/intranet-updatefrom
electricface:swt/setP2PSupport

Conversation

@electricface
Copy link
Copy Markdown
Member

Set P2PUpdateSupport based on whether the update repository uses the
delivery:// prefix returned by the update platform. Preserve the server
repository protocol when generating platform sources, and refresh P2P
support after updating platform repository data.

Bug: https://pms.uniontech.com/bug-view-356709.html
Change-Id: If6f425b8e750349acb1b7724e1316e6c7e440bac

@electricface electricface force-pushed the swt/setP2PSupport branch 2 times, most recently from e1c4d49 to 7b51f53 Compare April 18, 2026 05:31
Set P2PUpdateSupport based on whether the update repository uses the
delivery:// prefix returned by the update platform. Preserve the server
repository protocol when generating platform sources, and refresh P2P
support after updating platform repository data.

Bug: https://pms.uniontech.com/bug-view-356709.html
Change-Id: If6f425b8e750349acb1b7724e1316e6c7e440bac
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: electricface, qiuzhiqian

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

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

代码审查报告

整体评价

这次代码重构主要针对平台源列表生成逻辑进行了改进,将功能从方法中解耦为独立函数,并添加了单元测试。同时增强了升级传递服务的状态管理逻辑。整体代码质量较好,但仍有改进空间。

详细审查意见

1. 语法与逻辑

message_report.go

优点:

  • genDepositoryFromPlatform 重命名为更准确的 genRepositoryFromPlatform,命名更规范
  • 将仓库生成逻辑提取为独立函数 genPlatformReposFromRepoInfos,提高了可测试性和可维护性

改进建议:

  • genRepositoryFromPlatform 方法中的错误处理可以更完善:
    err := os.WriteFile(system.PlatFormSourceFile, []byte(strings.Join(repos, "\n")), 0644)
    if err != nil {
        logger.Warning("update source list file err:", err)
        // 建议返回错误而不是静默处理
        return err
    }

updater.go

优点:

  • refreshUpgradeDeliveryService 方法逻辑清晰,注释完善
  • 新增 sourceFileHasDeliveryProtocol 函数用于检查源文件中是否包含 delivery 协议

改进建议:

  • sourceFileHasDeliveryProtocol 中,对于大文件可以优化读取方式:
    // 使用 bufio.Scanner 逐行读取,避免一次性加载大文件到内存
    file, err := os.Open(sourcePath)
    if err != nil {
        return false
    }
    defer file.Close()
    
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        line := strings.TrimSpace(scanner.Text())
        if line == "" || strings.HasPrefix(line, "#") {
            continue
        }
        if strings.Contains(line, "delivery://") {
            return true
        }
    }
    return false

2. 代码质量

优点:

  • 添加了全面的单元测试,覆盖了多种场景
  • 函数职责单一,符合单一职责原则
  • 变量命名清晰,如 upgradeDeliveryObjectobject 更具描述性

改进建议:

  • genPlatformReposFromRepoInfos 函数参数较多(4个),可以考虑使用结构体封装:
    type RepoConfig struct {
        RepoInfos            []repoInfo
        PlatformRepoComponents string
        UpgradeDeliveryEnabled bool
        IntranetUpdate         bool
    }
    
    func genPlatformReposFromRepoInfos(config RepoConfig) []string {
        // ...
    }

3. 代码性能

改进建议:

  • sourceFileHasDeliveryProtocol 中,对于大文件可以使用缓冲读取,如上所述
  • genPlatformReposFromRepoInfos 中,可以考虑预分配切片容量:
    repos := make([]string, 0, len(repoInfos))

4. 代码安全

优点:

  • 文件操作使用了适当的权限(0644)
  • 添加了文件存在性检查

改进建议:

  • sourceFileHasDeliveryProtocol 中,应验证文件路径是否合法,防止路径遍历攻击:

    // 添加路径验证
    if !filepath.IsAbs(sourcePath) {
        return false
    }
    
    // 或者使用 filepath.Clean 和 filepath.Join 确保路径安全
    cleanPath := filepath.Clean(sourcePath)
    if !strings.HasPrefix(cleanPath, "/var/lib/lastore/") { // 假设源文件应在特定目录下
        return false
    }
  • genRepositoryFromPlatform 中,写入文件前应验证目录是否存在:

    dir := filepath.Dir(system.PlatFormSourceFile)
    if _, err := os.Stat(dir); os.IsNotExist(err) {
        if err := os.MkdirAll(dir, 0755); err != nil {
            logger.Warning("failed to create directory:", err)
            return err
        }
    }

5. 其他建议

  1. 测试覆盖率:

    • 建议添加对 genRepositoryFromPlatform 方法的测试,特别是错误处理路径
    • refreshUpgradeDeliveryService 添加测试,可能需要 mock DBus 调用
  2. 日志记录:

    • 在关键操作处添加更多日志,便于调试和问题追踪
    • 统一日志格式,确保包含足够的上下文信息
  3. 常量定义:

    • 考虑将字符串常量(如 "delivery://", "deb" 等)定义为包级常量,提高可维护性

总结

这次代码重构在功能解耦和测试覆盖方面做得很好,主要改进点集中在错误处理、性能优化和安全加固上。建议在后续版本中逐步实施上述改进建议,以提高代码的健壮性和可维护性。

@electricface electricface merged commit 4aa4c7a into linuxdeepin:develop/intranet-update Apr 18, 2026
13 of 17 checks passed
@electricface electricface deleted the swt/setP2PSupport branch April 18, 2026 05:38
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