Skip to content

feat: Add lastore-upgrade-query tool#273

Merged
qiuzhiqian merged 1 commit into
masterfrom
speed-up-apt-query
Dec 29, 2025
Merged

feat: Add lastore-upgrade-query tool#273
qiuzhiqian merged 1 commit into
masterfrom
speed-up-apt-query

Conversation

@qiuzhiqian
Copy link
Copy Markdown
Contributor

  • using libapt-pkg to implement upgrade package detailed information query

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 29, 2025

CLA Assistant Lite bot:
提交邮箱中包含我们的合作伙伴,但您似乎并非合作伙伴的成员或对接人,请联系相关对接人将您添加至组织之中,或由其重新发起 Pull Request。
The commit email domain belongs to one of our partners, but it seems you are not yet a member of the current organization, please contact the contact person to add you to the organization or let them submit the Pull Request.

xml seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request

#include "upgrade_query.h"
#include <iostream>
#include <iomanip>
#include <string>
Copy link
Copy Markdown
Contributor

@zhaohuiw42 zhaohuiw42 Dec 29, 2025

Choose a reason for hiding this comment

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

引入头文件 #include <sstream>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

不需要吧,本身就能够正常编译了

Comment thread src/lastore-upgrade-query/upgrade_query.h Outdated
Comment thread src/lastore-upgrade-query/upgrade_query.cc Outdated
Comment thread src/lastore-upgrade-query/main.cc
Comment thread src/lastore-upgrade-query/upgrade_query.cc Outdated
Comment thread src/lastore-upgrade-query/upgrade_query.h Outdated
Comment thread src/lastore-upgrade-query/upgrade_query.cc Outdated
Comment thread src/lastore-upgrade-query/upgrade_query.cc
Comment thread src/lastore-upgrade-query/main.cc Outdated
* using libapt-pkg to implement upgrade package detailed information query

Task: https://pms.uniontech.com/task-view-384369.html
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

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

  1. 代码结构和组织:
  • 代码结构清晰,将功能合理地分成了头文件、实现文件和主程序文件。
  • 使用了良好的命名约定,类名和函数名都采用大驼峰命名法。
  1. 依赖管理:
  • 在 debian/control 中正确添加了新的依赖 libapt-pkg-dev 和 nlohmann-json3-dev。
  • makefile 中正确配置了编译选项和链接库。
  1. 安全性建议:
  • GetUpgradePackage 函数中对文件路径和版本信息的处理需要增加更多的验证:
// 建议添加路径验证
if (!result.Filename.empty() && result.Filename.find("..") != std::string::npos) {
    // 处理潜在的路径遍历攻击
    continue;
}
  1. 错误处理:
  • 当前代码的错误处理基本完善,但可以改进:
// 在 GetUpgradePackages 中添加更多错误检查
if (!pkg.end() && !pkg.VersionList().end()) {
    // 验证包的完整性
    if (!pkg.VersionList()->VerStr()) {
        std::cerr << "Invalid version for package: " << pkg.Name() << std::endl;
        continue;
    }
}
  1. 性能优化:
  • 使用了 result.reserve() 来预分配内存,这是好的实践。
  • 建议在处理大量包时添加进度显示:
// 在遍历包时添加进度显示
size_t total = Cache->Head().PackageCount;
size_t current = 0;
for (pkgCache::PkgIterator pkg = Cache->PkgBegin(); !pkg.end(); ++pkg) {
    current++;
    if (current % 100 == 0) {
        std::cerr << "\rProcessing: " << current << "/" << total;
    }
    // ... 处理逻辑
}
  1. 代码健壮性:
  • UpgradePackage::Valid() 函数的验证逻辑很好,但建议增加对版本格式的验证:
bool UpgradePackage::Valid() const {
    // 现有验证...
    
    // 验证版本格式
    if (!CandidateVersion.empty() && !std::regex_match(CandidateVersion, std::regex("[0-9]+.*"))) {
        return false;
    }
    
    return true;
}
  1. 内存管理:
  • 代码中没有明显的内存泄漏风险,apt-pkg 库使用了 RAII 模式管理资源。
  1. 其他改进建议:
  • 添加日志记录功能:
#include <fstream>
void logError(const std::string& message) {
    std::ofstream log("/var/log/lastore-upgrade-query.log", std::ios::app);
    if (log.is_open()) {
        log << "[" << time(nullptr) << "] " << message << std::endl;
    }
}
  • 考虑添加配置文件支持:
struct Config {
    std::string defaultSourceList;
    std::string defaultSourceParts;
    bool defaultAllowDowngrades;
    // 其他配置项
};
  1. 代码文档:
  • 建议添加更详细的函数注释,特别是对 GetUpgradePackages 函数:
/**
 * @brief 获取可升级的软件包列表
 * @param sourcelist 自定义的 sources.list 文件路径
 * @param sourceparts 自定义的 sources.list.d 目录路径
 * @param allow_downgrades 是否允许降级安装
 * @return std::vector<UpgradePackage> 可升级软件包列表
 * @note 需要 root 权限执行
 */
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, 
                                             const std::string &sourceparts, 
                                             bool allow_downgrades = false);
  1. 测试建议:
  • 建议添加单元测试:
// test_upgrade_query.cc
TEST(TestUpgradePackage, ValidPackage) {
    UpgradePackage pkg;
    pkg.Name = "test";
    pkg.CandidateVersion = "1.0.0";
    // ... 设置其他字段
    EXPECT_TRUE(pkg.Valid());
}

总体来说,这是一个结构良好的实现,主要需要改进的是错误处理、安全性和可维护性方面的细节。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: qiuzhiqian, 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

@qiuzhiqian qiuzhiqian merged commit 23b70ba into master Dec 29, 2025
25 of 28 checks passed
@qiuzhiqian qiuzhiqian deleted the speed-up-apt-query branch December 29, 2025 07:54
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