Skip to content

Commit 6545f0b

Browse files
shuaijiedeepin-bot[bot]
authored andcommitted
fix: 修正部分disk容量信息
部分disk容量信息255GB 511GB 1.5TB Log: 修正部分disk容量信息为正确值 Bug: https://pms.uniontech.com/bug-view-251369.html
1 parent 652ae46 commit 6545f0b

3 files changed

Lines changed: 119 additions & 72 deletions

File tree

deepin-devicemanager/src/DeviceManager/DeviceManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ void DeviceManager::mergeDisk()
750750
delete curDevice;
751751
}
752752
}
753+
for (int i = 0; i < m_ListDeviceStorage.size(); ++i) {
754+
DeviceStorage *device = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[i]);
755+
device->unitConvertByDecimal();
756+
}
753757
}
754758

755759
void DeviceManager::checkDiskSize()

deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp

Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
// 项目自身文件
66
#include "DeviceStorage.h"
77
#include "commonfunction.h"
8+
#include <cmath>
89

910
// Qt库文件
1011
#include <QDir>
1112
#include<QDebug>
1213

14+
#define DISK_SCALE_1024 1024
15+
#define DISK_SCALE_1000 1000
16+
1317
DeviceStorage::DeviceStorage()
1418
: DeviceBaseInfo()
1519
, m_Model("")
1620
// , m_Vendor("")
1721
, m_MediaType("")
1822
, m_Size("")
23+
, m_SizeBytes(0)
1924
, m_RotationRate("")
2025
, m_Interface("")
2126
, m_SerialNumber("")
@@ -71,6 +76,48 @@ static QString decimalkilos(quint64 value)
7176
return valueStr;
7277
}
7378

79+
static quint64 convertToBytes(const QString& size, double scale)
80+
{
81+
/*convert "KB MB GB TB PB EB ZB YB " to bytes */
82+
const QString prefixes("KMGTPEZY");
83+
quint64 diskBytesSize = 0;
84+
double multiplier = 1;
85+
double diskSizeFloat = 0;
86+
87+
QRegExp reg("([0-9]*\\.?[0-9]*)([A-Za-z]*)");
88+
if(reg.indexIn(size) == -1)
89+
return diskBytesSize;
90+
QString sizeValue1 = reg.cap(1);
91+
if(sizeValue1.isEmpty())
92+
return diskBytesSize;
93+
QString diskUnits = size.right(size.length() - sizeValue1.size()).trimmed().toUpper();
94+
if(diskUnits.isEmpty())
95+
return diskBytesSize;
96+
diskUnits.replace("B","").replace("I","");
97+
QChar lastChar = diskUnits.at(diskUnits.length() - 1);
98+
99+
if (prefixes.contains(diskUnits)) {
100+
diskSizeFloat = sizeValue1.toDouble();
101+
102+
int i = 0;
103+
while (i < prefixes.size()) {
104+
QChar iChar = prefixes.at(i++);
105+
if(iChar == lastChar) {
106+
multiplier = std::pow(scale,i);
107+
break;
108+
}
109+
}
110+
}
111+
diskBytesSize = static_cast<quint64>(diskSizeFloat * multiplier);
112+
return diskBytesSize;
113+
}
114+
115+
void DeviceStorage::unitConvertByDecimal()
116+
{
117+
if(m_SizeBytes > 0)
118+
m_Size = decimalkilos(m_SizeBytes);
119+
}
120+
74121
bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo)
75122
{
76123
// 龙芯机器中 hwinfo --disk会列出所有的分区信息
@@ -109,11 +156,14 @@ bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo)
109156
quint64 bytesSize = reSize.cap(1).trimmed().toULongLong(&ok);
110157
if (ok) {
111158
m_Size = decimalkilos(bytesSize);
159+
m_SizeBytes = bytesSize;
112160
} else {
113161
m_Size.replace(QRegExp("\\(.*\\)"), "").replace(" ", "");
162+
m_SizeBytes = convertToBytes(m_Size,DISK_SCALE_1024);
114163
}
115164
} else {
116165
m_Size.replace(QRegExp("\\(.*\\)"), "").replace(" ", "");
166+
m_SizeBytes = convertToBytes(m_Size,DISK_SCALE_1024);
117167
}
118168

119169
// 如果既没有capacity也没有序列号则认为该磁盘无效,否则都属于有效磁盘
@@ -363,7 +413,7 @@ bool DeviceStorage::setKLUMediaType(const QString &name, const QString &value)
363413
bool DeviceStorage::isValid()
364414
{
365415
// 若是m_Size为空则 该设备无效
366-
if (m_Size.isEmpty())
416+
if (m_Size.isEmpty() && m_SizeBytes == 0)
367417
return false;
368418

369419
return true;
@@ -410,79 +460,40 @@ void DeviceStorage::appendDisk(DeviceStorage *device)
410460
for (int i = 0; i < allAttribs.size(); ++i) {
411461
allAttribMaps.insert(allAttribs[i].first, allAttribs[i].second);
412462
}
413-
QString size = allAttribMaps[tr("Size")];
414-
415-
if (size.isEmpty()) return;
416-
// 直接设置大小
417-
if (m_Size.isEmpty())
418-
m_Size = size;
419-
else {
420-
QRegExp reg("[0-9]*.?[0-9]*");
421-
int index = reg.indexIn(m_Size);
422-
double num1 = 0;
423-
QString type1;
424-
// index>0时,对于"32GB"(数字开头的字符串,index=0)无法获取正确的数据32
425-
// 所以要改为index >= 0
426-
if (index >= 0) {
427-
num1 = reg.cap(0).toDouble();
428-
type1 = m_Size.right(m_Size.length() - reg.cap(0).size()).trimmed();
429-
}
430-
431-
index = reg.indexIn(size);
432-
double num2 = 0;
433-
QString type2;
434-
if (index >= 0) {
435-
num2 = reg.cap(0).toDouble();
436-
type2 = size.right(size.length() - reg.cap(0).size()).trimmed();
437-
}
438-
// 匹配大小
439-
if (type1 != type2) {
440-
if (type1 == "TB" && type2 != "TB") {
441-
num2 /= 1000.0;
442-
type2 = "TB";
443-
} else if (type1 != "TB" && type2 == "TB") {
444-
num1 /= 1000.0;
445-
type1 = "TB";
446-
}
447-
}
448463

449-
if (type1 == type2) {
450-
num1 = num1 + num2;
451-
if (num1 > int(num1))
452-
m_Size = QString("%1 %2").arg(QString::number(num1, 'f', 2)).arg(type1);
453-
else {
454-
m_Size = QString("%1 %2").arg(num1).arg(type1);
455-
}
464+
quint64 size2 = device->getDiskSizeByte();
465+
if(m_SizeBytes == 0)
466+
m_SizeBytes = size2;
467+
else if (size2 > 0) {
468+
m_SizeBytes += size2;
456469

457-
QList<QPair<QString, QString> > allOtherAttribs = device->getOtherAttribs();
458-
QMap<QString, QString> allOtherAttribMaps;
459-
for (int i = 0; i < allOtherAttribs.size(); ++i) {
460-
allAttribMaps.insert(allOtherAttribs[i].first, allOtherAttribs[i].second);
461-
}
470+
QList<QPair<QString, QString> > allOtherAttribs = device->getOtherAttribs();
471+
QMap<QString, QString> allOtherAttribMaps;
472+
for (int i = 0; i < allOtherAttribs.size(); ++i) {
473+
allAttribMaps.insert(allOtherAttribs[i].first, allOtherAttribs[i].second);
474+
}
462475

463-
loadOtherDeviceInfo();
464-
//合并
465-
QMap<QString, QString> curAllOtherAttribMaps;
466-
for (int i = 0; i < m_LstOtherInfo.size(); ++i) {
467-
curAllOtherAttribMaps.insert(m_LstOtherInfo[i].first, m_LstOtherInfo[i].second);
468-
}
476+
loadOtherDeviceInfo();
477+
//合并
478+
QMap<QString, QString> curAllOtherAttribMaps;
479+
for (int i = 0; i < m_LstOtherInfo.size(); ++i) {
480+
curAllOtherAttribMaps.insert(m_LstOtherInfo[i].first, m_LstOtherInfo[i].second);
481+
}
469482

470-
QStringList keyList;
471-
keyList.append(QObject::tr("bus info"));
472-
keyList.append(QObject::tr("Device File"));
473-
// keyList.append(QObject::tr("physical id"));
474-
keyList.append(QObject::tr("Device Number"));
475-
keyList.append(QObject::tr("logical name"));
476-
for (QString keyStr : keyList) {
477-
QString curBusInfo = curAllOtherAttribMaps[keyStr];
478-
QString busInfo = allAttribMaps[keyStr];
479-
if (!curBusInfo.isEmpty() && !busInfo.isEmpty() && curBusInfo != busInfo) {
480-
setOtherDeviceInfo(keyStr, curBusInfo + "," + busInfo);
481-
}
483+
QStringList keyList;
484+
keyList.append(QObject::tr("bus info"));
485+
keyList.append(QObject::tr("Device File"));
486+
// keyList.append(QObject::tr("physical id"));
487+
keyList.append(QObject::tr("Device Number"));
488+
keyList.append(QObject::tr("logical name"));
489+
for (QString keyStr : keyList) {
490+
QString curBusInfo = curAllOtherAttribMaps[keyStr];
491+
QString busInfo = allAttribMaps[keyStr];
492+
if (!curBusInfo.isEmpty() && !busInfo.isEmpty() && curBusInfo != busInfo) {
493+
setOtherDeviceInfo(keyStr, curBusInfo + "," + busInfo);
482494
}
483-
484-
loadOtherDeviceInfo();
485495
}
496+
loadOtherDeviceInfo();
486497
}
487498
}
488499

@@ -644,11 +655,21 @@ void DeviceStorage::getInfoFromLshw(const QMap<QString, QString> &mapInfo)
644655
setAttribute(mapInfo, "serial", m_SerialNumber, false);
645656
setAttribute(mapInfo, "product", m_Name);
646657
setAttribute(mapInfo, "description", m_Description);
647-
setAttribute(mapInfo, "size", m_Size, false);
648-
// 223GiB (240GB)
658+
QString sizeFromlshw = QString();
659+
quint64 sizeByte = 0;
660+
setAttribute(mapInfo, "size", sizeFromlshw, true);
661+
// 样式数据 223GiB (240GB) size: 57GiB (61GB) size: 931GiB (1TB)
649662
QRegExp re(".*\\((.*)\\)$");
650-
if (re.exactMatch(m_Size))
651-
m_Size = re.cap(1);
663+
if (re.exactMatch(sizeFromlshw)) {
664+
sizeFromlshw = re.cap(1);
665+
sizeByte = convertToBytes(sizeFromlshw,DISK_SCALE_1000);
666+
}
667+
if(m_SizeBytes == 0)
668+
m_SizeBytes = sizeByte;
669+
670+
if(m_Size.isEmpty() )
671+
m_Size = sizeFromlshw;
672+
652673

653674
if (m_SerialNumber.compare("0",Qt::CaseInsensitive) == 0)
654675
m_SerialNumber = "";
@@ -691,6 +712,17 @@ void DeviceStorage::getInfoFromsmartctl(const QMap<QString, QString> &mapInfo)
691712
QRegExp reg(".*\\[(.*)\\]$");
692713
if (reg.exactMatch(capacity))
693714
m_Size = reg.cap(1);
715+
716+
capacity.replace(",","").replace(" ","");
717+
QRegExp re("(\\d+)bytes*"); //取值格式如: User Capacity: 1,000,204,886,016 bytes [1.00 TB] Total NVM Capacity: 256,060,514,304 [256 GB]
718+
int pos = re.indexIn(capacity);
719+
if (pos != -1) {
720+
QString byteSize = re.cap(1);
721+
bool isValue = false;
722+
quint64 value = byteSize.trimmed().toULongLong(&isValue);
723+
if(value > 0 && isValue)
724+
m_SizeBytes = value;
725+
}
694726
}
695727

696728
// 通过不断适配,当厂商有在固件中提供时,硬盘型号从smartctl中获取更加合理

deepin-devicemanager/src/DeviceManager/DeviceStorage.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class DeviceStorage: public DeviceBaseInfo
1919
public:
2020
DeviceStorage();
2121

22+
/**
23+
* @brief unitConvertByDecimal:将m_Size的byte单位转换为合理单位:GB、TB之类
24+
*/
25+
void unitConvertByDecimal();
26+
2227
/**
2328
* @brief setInfoFromTomlOneByOne:设置从toml里面获取的信息
2429
* @param mapInfo:由toml获取的信息map
@@ -114,6 +119,11 @@ class DeviceStorage: public DeviceBaseInfo
114119
*/
115120
void appendDisk(DeviceStorage *device);
116121

122+
/**
123+
* @brief getDiskSizeByte:读取磁盘大小,单位Byte
124+
*/
125+
virtual quint64 getDiskSizeByte() { return m_SizeBytes; }
126+
117127
/**
118128
* @brief checkDiskSize:规范磁盘大小
119129
*/
@@ -197,6 +207,7 @@ class DeviceStorage: public DeviceBaseInfo
197207
// QString m_Vendor; //<! 【制造商】2
198208
QString m_MediaType; //<! 【介质类型】3
199209
QString m_Size; //<! 【大小】4
210+
quint64 m_SizeBytes; //<! 【大小单位byte】4 //对于有合并的时候 用此代替m_Size进行合并更准确。
200211
QString m_RotationRate; //<! 【转速】
201212
QString m_Interface; //<! 【接口】6
202213
QString m_SerialNumber; //<! 【序列号】7

0 commit comments

Comments
 (0)