Skip to content

fix: [Stroage-interface] support UFS 4.1 spec_version 410 - #214

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/1071from
tianming-1996:release/1071
Aug 6, 2026
Merged

fix: [Stroage-interface] support UFS 4.1 spec_version 410#214
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/1071from
tianming-1996:release/1071

Conversation

@tianming-1996

@tianming-1996 tianming-1996 commented Aug 6, 2026

Copy link
Copy Markdown

Add spec_version "410" to UFS detection and use getMapInfoFromInput to parse "Attached to" field instead of raw output split.

补充UFS 4.1 (spec_version 410) 的识别,修正hwinfo输出解析逻辑。

Log: 支持UFS 4.1接口识别
Bug: https://pms.uniontech.com/bug-view-372777.html
Influence: PGUY机型UFS 4.1存储设备接口可正确识别为UFS。

Summary by Sourcery

Extend disk interface detection to correctly recognize UFS devices and improve robustness of hwinfo output parsing.

New Features:

  • Recognize UFS 4.1 storage devices by handling spec_version 410 in interface detection.

Bug Fixes:

  • Parse the hwinfo "Attached to" field via structured map extraction to avoid misidentifying interfaces from capacity lines.

Tests:

  • Add unit tests for getDiskInfoInterface covering correct UFS detection and ignoring capacity-only hwinfo output.

@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.

Sorry @tianming-1996, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Reviewer's Guide

Add support for detecting UFS 4.1 (spec_version 410) and harden hwinfo output parsing by using a key-value map, with unit tests to validate the new parsing behavior around the "Attached to" and "Capacity" lines.

Sequence diagram for updated getDiskInfoInterface hwinfo parsing

sequenceDiagram
    participant DeviceStorage
    participant HwinfoProcess
    participant getMapInfoFromInput

    DeviceStorage->>HwinfoProcess: start(cmd)
    DeviceStorage->>HwinfoProcess: waitForFinished(-1)
    DeviceStorage->>HwinfoProcess: readAllStandardOutput()
    HwinfoProcess-->>DeviceStorage: outPut
    DeviceStorage->>getMapInfoFromInput: getMapInfoFromInput(outPut, mapInfo)
    getMapInfoFromInput-->>DeviceStorage: mapInfo
    DeviceStorage->>DeviceStorage: mapInfo.value(Attached to)
    DeviceStorage->>DeviceStorage: split("(") and extract interface
Loading

Flow diagram for UFS spec_version detection including 410

flowchart TD
    A[read spec_version] --> B{spec_version contains 300
    or 310
    or 400
    or 410}
    B -->|yes| C[interface = UFS]
    B -->|no| D[interface unchanged]
Loading

File-Level Changes

Change Details Files
Extend UFS interface detection to recognize spec_version 410.
  • Read spec_version from sysfs and treat value containing 410 as UFS in addition to existing 300, 310, and 400 handling
service/diskoperation/DeviceStorage.cpp
Refactor hwinfo output parsing in getDiskInfoInterface to use structured map parsing for the "Attached to" field.
  • Replace raw string splitting on '(' with getMapInfoFromInput to extract the "Attached to" field
  • Derive interface value from the parsed "Attached to" entry instead of the entire raw hwinfo output
service/diskoperation/DeviceStorage.cpp
Add unit tests covering hwinfo parsing edge cases around "Attached to" and "Capacity" lines.
  • Introduce PathGuard and helper to write a temporary hwinfo executable used by tests
  • Add tests ensuring UFS interface is set when "Attached to" contains UFS 3.1 and capacity follows
  • Add tests ensuring interface remains empty when only a capacity line is present without interface information
test/ut_diskoperation/ut_devicestorage.cpp

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

max-lvs
max-lvs previously approved these changes Aug 6, 2026
@max-lvs

max-lvs commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

-2, 硬代码,重新搞,基于d-config 搞

max-lvs
max-lvs previously approved these changes Aug 6, 2026
Read UFS spec_version substrings from DConfig so new UFS versions can
be added via configuration without source code changes.

从 DConfig 读取 UFS spec_version 子串列表,新增 UFS 版本时只需
在配置中追加,无需修改源码。

Log: UFS版本识别改为DConfig配置驱动
Bug: https://pms.uniontech.com/bug-view-372777.html
Influence: UFS接口识别逻辑改为配置驱动,新增UFS版本无需改源码,提升可维护性。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码成功修复了hwinfo输出解析错误并重构了UFS版本识别逻辑,质量优秀且无安全风险
逻辑完全正确且引入了单元测试与动态配置,符合高质量标准仅因微小风格习惯未给满分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修复前使用outPut.split("(")解析hwinfo输出,当输出包含Capacity: 1 TB (1024626524160 bytes)时,会错误提取到bytes字段。修复后通过getMapInfoFromInput精确提取"Attached to"字段的值再进行分割,彻底解决了括号干扰导致的接口类型识别失败问题。UFS版本匹配逻辑从硬编码改为遍历动态列表,逻辑严密,边界处理正确。
建议:将foreach循环替换为C++11的范围for循环,以保持与项目中其他现代C++代码的风格一致

  • 2.代码质量(良好)✓

引入DConfig替代硬编码,新增UFS版本时无需修改源码,极大提升了可维护性。新增的单元测试覆盖了容量括号干扰和接口缺失两种边界场景,通过伪造hwinfo命令和环境变量劫持实现了安全的隔离测试。DConfig配置文件中包含了完善的中英文描述和权限控制声明。
建议:ufsSpecVersions函数每次调用都会创建DConfig对象,虽然DConfig内部有缓存且当前调用频率极低,但若未来被高频调用可考虑将其改为单例或静态局部变量

  • 3.代码性能(无性能问题)✓

getMapInfoFromInput替代全局split操作,减少了不必要的字符串分割和内存分配。DConfig的读取操作仅在特定设备路径匹配时触发,且读取后立即释放,不会对主流程造成性能瓶颈。
建议:无需优化

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码通过引入DConfig动态读取UFS版本列表,并修复了字符串分割逻辑,未引入新的安全风险。对hwinfo输出的解析采用了先提取特定键值再分割的策略,有效避免了因格式变化导致的越界或错误解析问题。DConfig配置文件设置了readonly权限,防止了未授权的运行时篡改。单元测试中通过环境变量劫持PATH的方式安全可控,PathGuard利用RAII机制确保了环境变量恢复,无信息泄露或权限提升风险。
建议:继续保持对外部命令输出解析的防御性编程习惯

■ 【改进建议代码示例】

// service/diskoperation/DeviceStorage.cpp
void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model)
{
    QString bootDevicePath("/proc/bootdevice/product_name");
    // ... 省略前置 UFS spec_version 读取与匹配逻辑 ...
    if (!spec_version.isEmpty()) {
        // 建议:使用范围 for 循环替代 Qt 的 foreach 宏,提升现代 C++ 代码风格一致性
        for (const QString &version : ufsSpecVersions()) {
            if (spec_version.contains(version)) {
                interface = "UFS";
                break;
            }
        }
    }
    // ... 省略中间逻辑 ...
    
    // 【BUG 修复核心点】增强防御性编程
    QMap<QString, QString> mapInfo;
    getMapInfoFromInput(outPut, mapInfo);
    const QString attachedTo = mapInfo.value("Attached to");
    
    // 建议增加非空校验,使逻辑意图更明确,避免潜在的未来修改引入越界风险
    if (!attachedTo.isEmpty()) {
        QStringList outPutList = attachedTo.split("(");
        if (!outPutList.isEmpty()) {
            // 使用 last() 和 value(0) 替代直接下标访问,更加安全
            interface = outPutList.last().split(" ").value(0);
        }
    }
    return;
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, tianming-1996

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

@tianming-1996

Copy link
Copy Markdown
Author

/merge

@deepin-bot
deepin-bot Bot merged commit e4a72be into linuxdeepin:release/1071 Aug 6, 2026
20 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