Repository navigation
fix(storage): read UFS spec versions from DConfig instead of hardcoding #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "magic": "dsg.config.meta", | ||
| "version": "1.0", | ||
| "contents": { | ||
| "ufsSpecVersions": { | ||
| "value": ["300", "310", "400", "410"], | ||
| "serial": 0, | ||
| "flags": ["global"], | ||
| "name": "UFS Spec Versions", | ||
| "name[zh_CN]": "UFS规范版本", | ||
| "description": "Substrings of the UFS spec version read from /sys/block/<dev>/device/spec_version that should be recognized as the UFS interface. Append new version strings here instead of modifying source code.", | ||
| "description[zh_CN]": "从 /sys/block/<dev>/device/spec_version 读取的UFS规范版本子串列表,匹配任一项即识别为UFS接口。新增UFS版本时只需在此配置中追加,无需修改源码。", | ||
| "permissions": "readonly", | ||
| "visibility": "private" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| // SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd. | ||
| // SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-only | ||
|
|
||
| #include "ddlog.h" | ||
| #include "DeviceStorage.h" | ||
| #include <QDebug> | ||
| #include <QFile> | ||
| #include <QRegularExpression> | ||
| #include <QScopedPointer> | ||
| #include <QVariant> | ||
| #include <DConfig> | ||
| #include "utils.h" | ||
|
|
||
| namespace DiskManager { | ||
|
|
||
|
|
@@ -737,7 +740,22 @@ | |
| return 1 == isPGUX; | ||
| } | ||
|
|
||
| // 从 DConfig 读取需要识别为 UFS 接口的 spec_version 子串列表,新增 UFS 版本时 | ||
| // 只需在 com.deepin.diskmanager.storage 配置中追加,无需修改源码。 | ||
| static QStringList ufsSpecVersions() | ||
| { | ||
| const QStringList fallback{"300", "310", "400", "410"}; | ||
| QScopedPointer<Dtk::Core::DConfig> cfg( | ||
| Dtk::Core::DConfig::create("com.deepin.diskmanager", "com.deepin.diskmanager.storage")); | ||
| if (cfg && cfg->isValid()) { | ||
| QStringList versions = cfg->value("ufsSpecVersions", QVariant::fromValue(fallback)).toStringList(); | ||
| if (!versions.isEmpty()) | ||
| return versions; | ||
| } | ||
| return fallback; | ||
| } | ||
|
|
||
| void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model) | ||
| { | ||
| qCDebug(serviceLog) << "DeviceStorage::getDiskInfoInterface BEGIN"; | ||
| QString bootDevicePath("/proc/bootdevice/product_name"); | ||
|
|
@@ -750,8 +768,11 @@ | |
| QString spec_version = Utils::readContent("/sys/block/sdd/device/spec_version").trimmed(); | ||
| if (!spec_version.isEmpty()) { | ||
| qCDebug(serviceLog) << "spec_version is not empty"; | ||
| if (spec_version.contains("300") || spec_version.contains("310") || spec_version.contains("400")) { | ||
| interface = "UFS"; | ||
| foreach (const QString &version, ufsSpecVersions()) { | ||
| if (spec_version.contains(version)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Non-UFS boot disks appear as UFS When the configured Skip empty entries in the version list before matching them against Prompt for AI agentsIn `service/diskoperation/DeviceStorage.cpp` at line 772:
**Non-UFS boot disks appear as UFS**
When the configured `ufsSpecVersions()` list contains an empty string and a boot device has a nonempty spec version, `getDiskInfoInterface` passes the empty version to `QString::contains`, which matches every nonempty `spec_version` and sets `interface` to UFS, so callers see non-UFS boot storage reported as UFS.
Skip empty entries in the version list before matching them against `spec_version`. |
||
| interface = "UFS"; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -768,7 +789,9 @@ | |
| qCDebug(serviceLog) << "Failed to execute hwinfo command, error:" << error; | ||
| return; | ||
| } | ||
| QStringList outPutList = outPut.split("("); | ||
| QMap<QString, QString> mapInfo; | ||
| getMapInfoFromInput(outPut, mapInfo); | ||
| QStringList outPutList = mapInfo.value("Attached to").split("("); | ||
| interface = outPutList[outPutList.size() - 1].split(" ")[0]; | ||
| } | ||
| qCDebug(serviceLog) << "DeviceStorage::getDiskInfoInterface END"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-only | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "../../service/diskoperation/DeviceStorage.h" | ||
|
|
||
| #include <QFile> | ||
| #include <QTemporaryDir> | ||
|
|
||
| using namespace DiskManager; | ||
|
|
||
| namespace { | ||
|
|
||
| class PathGuard | ||
| { | ||
| public: | ||
| explicit PathGuard(const QString &path) | ||
| : m_oldPath(qgetenv("PATH")) | ||
| { | ||
| qputenv("PATH", QString("%1:%2").arg(path, QString::fromLocal8Bit(m_oldPath)).toLocal8Bit()); | ||
| } | ||
|
|
||
| ~PathGuard() | ||
| { | ||
| qputenv("PATH", m_oldPath); | ||
| } | ||
|
|
||
| private: | ||
| QByteArray m_oldPath; | ||
| }; | ||
|
|
||
| void writeExecutable(const QString &filePath, const QByteArray &content) | ||
| { | ||
| QFile file(filePath); | ||
| ASSERT_TRUE(file.open(QIODevice::WriteOnly | QIODevice::Truncate)); | ||
| ASSERT_EQ(file.write(content), content.size()); | ||
| file.close(); | ||
| ASSERT_TRUE(file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| class ut_devicestorage : public ::testing::Test | ||
| { | ||
| }; | ||
|
|
||
| TEST_F(ut_devicestorage, getDiskInfoInterface_usesAttachedToWhenCapacityFollows) | ||
| { | ||
| QTemporaryDir dir; | ||
| ASSERT_TRUE(dir.isValid()); | ||
| writeExecutable(dir.filePath("hwinfo"), | ||
| "#!/bin/sh\n" | ||
| "printf '%s\\n' '10: None 00.0: 10600 Disk' " | ||
| "' Attached to: #1 (UFS 3.1 Controller)' " | ||
| "' Capacity: 1 TB (1024626524160 bytes)'\n"); | ||
| PathGuard guard(dir.path()); | ||
|
|
||
| DeviceStorage storage; | ||
| QString interface; | ||
| QString model("__ut_nonexistent_model__"); | ||
|
|
||
| storage.getDiskInfoInterface("/dev/__ut_nonexistent_pms372777_ufs__", interface, model); | ||
|
|
||
| EXPECT_EQ(interface, "UFS"); | ||
| } | ||
|
|
||
| TEST_F(ut_devicestorage, getDiskInfoInterface_ignoresCapacityBytesWhenInterfaceMissing) | ||
| { | ||
| QTemporaryDir dir; | ||
| ASSERT_TRUE(dir.isValid()); | ||
| writeExecutable(dir.filePath("hwinfo"), | ||
| "#!/bin/sh\n" | ||
| "printf '%s\\n' ' Capacity: 1 TB (1024626524160 bytes)'\n"); | ||
| PathGuard guard(dir.path()); | ||
|
|
||
| DeviceStorage storage; | ||
| QString interface; | ||
| QString model("__ut_nonexistent_model__"); | ||
|
|
||
| storage.getDiskInfoInterface("/dev/__ut_nonexistent_pms372777_capacity__", interface, model); | ||
|
|
||
| EXPECT_TRUE(interface.isEmpty()); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Configured versions silently ignored
When DConfig is unavailable or invalid and a device uses a spec-version substring added only through DConfig,
ufsSpecVersions()returns the hardcoded fallback without logging that DConfig is invalid, so versions added only through DConfig are not matched and affected devices can be reported with the wrong interface.Log a warning when DConfig cannot be loaded or validated before using the fallback list.
Prompt for AI agents