|
5 | 5 | // 项目自身文件 |
6 | 6 | #include "DeviceStorage.h" |
7 | 7 | #include "commonfunction.h" |
| 8 | +#include <cmath> |
8 | 9 |
|
9 | 10 | // Qt库文件 |
10 | 11 | #include <QDir> |
11 | 12 | #include<QDebug> |
12 | 13 |
|
| 14 | +#define DISK_SCALE_1024 1024 |
| 15 | +#define DISK_SCALE_1000 1000 |
| 16 | + |
13 | 17 | DeviceStorage::DeviceStorage() |
14 | 18 | : DeviceBaseInfo() |
15 | 19 | , m_Model("") |
16 | 20 | // , m_Vendor("") |
17 | 21 | , m_MediaType("") |
18 | 22 | , m_Size("") |
| 23 | + , m_SizeBytes(0) |
19 | 24 | , m_RotationRate("") |
20 | 25 | , m_Interface("") |
21 | 26 | , m_SerialNumber("") |
@@ -71,6 +76,48 @@ static QString decimalkilos(quint64 value) |
71 | 76 | return valueStr; |
72 | 77 | } |
73 | 78 |
|
| 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 | + |
74 | 121 | bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo) |
75 | 122 | { |
76 | 123 | // 龙芯机器中 hwinfo --disk会列出所有的分区信息 |
@@ -109,11 +156,14 @@ bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo) |
109 | 156 | quint64 bytesSize = reSize.cap(1).trimmed().toULongLong(&ok); |
110 | 157 | if (ok) { |
111 | 158 | m_Size = decimalkilos(bytesSize); |
| 159 | + m_SizeBytes = bytesSize; |
112 | 160 | } else { |
113 | 161 | m_Size.replace(QRegExp("\\(.*\\)"), "").replace(" ", ""); |
| 162 | + m_SizeBytes = convertToBytes(m_Size,DISK_SCALE_1024); |
114 | 163 | } |
115 | 164 | } else { |
116 | 165 | m_Size.replace(QRegExp("\\(.*\\)"), "").replace(" ", ""); |
| 166 | + m_SizeBytes = convertToBytes(m_Size,DISK_SCALE_1024); |
117 | 167 | } |
118 | 168 |
|
119 | 169 | // 如果既没有capacity也没有序列号则认为该磁盘无效,否则都属于有效磁盘 |
@@ -363,7 +413,7 @@ bool DeviceStorage::setKLUMediaType(const QString &name, const QString &value) |
363 | 413 | bool DeviceStorage::isValid() |
364 | 414 | { |
365 | 415 | // 若是m_Size为空则 该设备无效 |
366 | | - if (m_Size.isEmpty()) |
| 416 | + if (m_Size.isEmpty() && m_SizeBytes == 0) |
367 | 417 | return false; |
368 | 418 |
|
369 | 419 | return true; |
@@ -410,79 +460,40 @@ void DeviceStorage::appendDisk(DeviceStorage *device) |
410 | 460 | for (int i = 0; i < allAttribs.size(); ++i) { |
411 | 461 | allAttribMaps.insert(allAttribs[i].first, allAttribs[i].second); |
412 | 462 | } |
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 | | - } |
448 | 463 |
|
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; |
456 | 469 |
|
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 | + } |
462 | 475 |
|
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 | + } |
469 | 482 |
|
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); |
482 | 494 | } |
483 | | - |
484 | | - loadOtherDeviceInfo(); |
485 | 495 | } |
| 496 | + loadOtherDeviceInfo(); |
486 | 497 | } |
487 | 498 | } |
488 | 499 |
|
@@ -644,11 +655,21 @@ void DeviceStorage::getInfoFromLshw(const QMap<QString, QString> &mapInfo) |
644 | 655 | setAttribute(mapInfo, "serial", m_SerialNumber, false); |
645 | 656 | setAttribute(mapInfo, "product", m_Name); |
646 | 657 | 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) |
649 | 662 | 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 | + |
652 | 673 |
|
653 | 674 | if (m_SerialNumber.compare("0",Qt::CaseInsensitive) == 0) |
654 | 675 | m_SerialNumber = ""; |
@@ -691,6 +712,17 @@ void DeviceStorage::getInfoFromsmartctl(const QMap<QString, QString> &mapInfo) |
691 | 712 | QRegExp reg(".*\\[(.*)\\]$"); |
692 | 713 | if (reg.exactMatch(capacity)) |
693 | 714 | 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 | + } |
694 | 726 | } |
695 | 727 |
|
696 | 728 | // 通过不断适配,当厂商有在固件中提供时,硬盘型号从smartctl中获取更加合理 |
|
0 commit comments