From d71731141c23dac6474970de61ce32d77a4363b3 Mon Sep 17 00:00:00 2001 From: Caio Carvalho Date: Tue, 9 Jan 2018 01:35:59 -0300 Subject: [PATCH] - Changed m_Quirk variable in SmartAttributeParsedData to SmartQuirk value instead of reference - Changed smartctl call to use ExternalCommand class instead of QProcess - Changed Q_FOREACH to C++11 ranged based for - Changed quint64 json parsing --- src/core/smartattributeparseddata.cpp | 56 +++++++++++++-------------- src/core/smartattributeparseddata.h | 2 +- src/core/smartdiskinformation.cpp | 4 +- src/core/smartparser.cpp | 24 +++++------- src/core/smartstatus.cpp | 2 +- 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/core/smartattributeparseddata.cpp b/src/core/smartattributeparseddata.cpp index 8960c92..3b21ccd 100644 --- a/src/core/smartattributeparseddata.cpp +++ b/src/core/smartattributeparseddata.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #define MKELVIN_VALID_MIN ((qint64) ((-15LL*1000LL) + 273150LL)) #define MKELVIN_VALID_MAX ((qint64) ((100LL*1000LL) + 273150LL)) @@ -30,7 +31,7 @@ #define MSECOND_VALID_LONG_MAX (30ULL * 365ULL * 24ULL * 60ULL * 60ULL * 1000ULL) static const QMap tableUnit(); -static SmartAttributeParsedData::SmartQuirk *getQuirk(QString model, QString firmware); +static SmartAttributeParsedData::SmartQuirk getQuirk(QString model, QString firmware); SmartAttributeParsedData::SmartAttributeParsedData(SmartDiskInformation *disk, QJsonObject jsonAttribute) : @@ -52,7 +53,7 @@ SmartAttributeParsedData::SmartAttributeParsedData(SmartDiskInformation *disk, m_Warn(false), m_PrettyUnit(SMART_ATTRIBUTE_UNIT_UNKNOWN), m_Disk(disk), - m_Quirk(nullptr) + m_Quirk((SmartAttributeParsedData::SmartQuirk) 0) { if (disk) m_Quirk = getQuirk(disk->model(), disk->firmware()); @@ -63,7 +64,6 @@ SmartAttributeParsedData::SmartAttributeParsedData(SmartDiskInformation *disk, QString worst = QString::fromLocal8Bit("worst"); QString thresh = QString::fromLocal8Bit("thresh"); QString raw = QString::fromLocal8Bit("raw"); - QString str = QString::fromLocal8Bit("string"); QString flags = QString::fromLocal8Bit("flags"); QString prefailure = QString::fromLocal8Bit("prefailure"); QString online = QString::fromLocal8Bit("updated_online"); @@ -75,7 +75,7 @@ SmartAttributeParsedData::SmartAttributeParsedData(SmartDiskInformation *disk, QJsonObject rawObj = jsonAttribute[raw].toObject(); - m_Raw = rawObj[str].toString().toULongLong(); + m_Raw = rawObj[value].toVariant().toULongLong(); QJsonObject flagsObj = jsonAttribute[flags].toObject(); @@ -264,119 +264,119 @@ bool SmartAttributeParsedData::updateUnit() if (m_Quirk) { switch (id()) { case 3: - if (*m_Quirk & SMART_QUIRK_3_UNUSED) { + if (m_Quirk & SMART_QUIRK_3_UNUSED) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_UNKNOWN; return true; } break; case 4: - if (*m_Quirk & SMART_QUIRK_4_UNUSED) { + if (m_Quirk & SMART_QUIRK_4_UNUSED) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_UNKNOWN; return true; } break; case 5: - if (*m_Quirk & SMART_QUIRK_5_UNKNOWN) + if (m_Quirk & SMART_QUIRK_5_UNKNOWN) return false; break; case 9: - if (*m_Quirk & SMART_QUIRK_9_POWERONMINUTES) { + if (m_Quirk & SMART_QUIRK_9_POWERONMINUTES) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MSECONDS; return true; - } else if (*m_Quirk & SMART_QUIRK_9_POWERONSECONDS) { + } else if (m_Quirk & SMART_QUIRK_9_POWERONSECONDS) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MSECONDS; return true; - } else if (*m_Quirk & SMART_QUIRK_9_POWERONHALFMINUTES) { + } else if (m_Quirk & SMART_QUIRK_9_POWERONHALFMINUTES) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MSECONDS; return true; - } else if (*m_Quirk & SMART_QUIRK_9_UNKNOWN) + } else if (m_Quirk & SMART_QUIRK_9_UNKNOWN) return false; break; case 190: - if (*m_Quirk & SMART_QUIRK_190_UNKNOWN) + if (m_Quirk & SMART_QUIRK_190_UNKNOWN) return false; break; case 192: - if (*m_Quirk & SMART_QUIRK_192_EMERGENCYRETRACTCYCLECT) { + if (m_Quirk & SMART_QUIRK_192_EMERGENCYRETRACTCYCLECT) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_NONE; return true; } break; case 194: - if (*m_Quirk & SMART_QUIRK_194_10XCELSIUS) { + if (m_Quirk & SMART_QUIRK_194_10XCELSIUS) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MKELVIN; return true; - } else if (*m_Quirk & SMART_QUIRK_194_UNKNOWN) + } else if (m_Quirk & SMART_QUIRK_194_UNKNOWN) return false; break; case 197: - if (*m_Quirk & SMART_QUIRK_197_UNKNOWN) + if (m_Quirk & SMART_QUIRK_197_UNKNOWN) return false; break; case 198: - if (*m_Quirk & SMART_QUIRK_198_UNKNOWN) + if (m_Quirk & SMART_QUIRK_198_UNKNOWN) return false; break; case 200: - if (*m_Quirk & SMART_QUIRK_200_WRITEERRORCOUNT) { + if (m_Quirk & SMART_QUIRK_200_WRITEERRORCOUNT) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_NONE; return true; } break; case 201: - if (*m_Quirk & SMART_QUIRK_201_DETECTEDTACOUNT) { + if (m_Quirk & SMART_QUIRK_201_DETECTEDTACOUNT) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_NONE; return true; } break; case 225: - if (*m_Quirk & SMART_QUIRK_225_TOTALLBASWRITTEN) { + if (m_Quirk & SMART_QUIRK_225_TOTALLBASWRITTEN) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MB; return true; } break; case 226: - if (*m_Quirk & SMART_QUIRK_226_TIMEWORKLOADMEDIAWEAR) { + if (m_Quirk & SMART_QUIRK_226_TIMEWORKLOADMEDIAWEAR) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_SMALL_PERCENT; return true; } break; case 227: - if (*m_Quirk & SMART_QUIRK_227_TIMEWORKLOADHOSTREADS) { + if (m_Quirk & SMART_QUIRK_227_TIMEWORKLOADHOSTREADS) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_SMALL_PERCENT; return true; } break; case 228: - if (*m_Quirk & SMART_QUIRK_228_WORKLOADTIMER) { + if (m_Quirk & SMART_QUIRK_228_WORKLOADTIMER) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_MSECONDS; return true; } break; case 232: - if (*m_Quirk & SMART_QUIRK_232_AVAILABLERESERVEDSPACE) { + if (m_Quirk & SMART_QUIRK_232_AVAILABLERESERVEDSPACE) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_PERCENT; return true; } break; case 233: - if (*m_Quirk & SMART_QUIRK_233_MEDIAWEAROUTINDICATOR) { + if (m_Quirk & SMART_QUIRK_233_MEDIAWEAROUTINDICATOR) { m_PrettyUnit = SMART_ATTRIBUTE_UNIT_PERCENT; return true; } @@ -628,7 +628,7 @@ static const SmartAttributeParsedData::SmartQuirkDataBase *quirkDatabase() return quirkDb; } -static SmartAttributeParsedData::SmartQuirk *getQuirk(QString model, QString firmware) +static SmartAttributeParsedData::SmartQuirk getQuirk(QString model, QString firmware) { const SmartAttributeParsedData::SmartQuirkDataBase *db; @@ -646,8 +646,8 @@ static SmartAttributeParsedData::SmartQuirk *getQuirk(QString model, QString fir if (!firmwareRegex.exactMatch(firmware)) continue; } - return (SmartAttributeParsedData::SmartQuirk *)&db->quirk; + return db->quirk; } - return nullptr; + return (SmartAttributeParsedData::SmartQuirk) 0; } diff --git a/src/core/smartattributeparseddata.h b/src/core/smartattributeparseddata.h index 641b5ea..2868fb7 100644 --- a/src/core/smartattributeparseddata.h +++ b/src/core/smartattributeparseddata.h @@ -201,7 +201,7 @@ private: bool m_Warn; SmartAttributeUnit m_PrettyUnit; SmartDiskInformation *m_Disk; - SmartQuirk *m_Quirk; + SmartQuirk m_Quirk; }; #endif // SMARTATTRIBUTEPARSEDDATA_H diff --git a/src/core/smartdiskinformation.cpp b/src/core/smartdiskinformation.cpp index cf701ef..b220279 100644 --- a/src/core/smartdiskinformation.cpp +++ b/src/core/smartdiskinformation.cpp @@ -154,7 +154,7 @@ bool SmartDiskInformation::updatePowerCycle() void SmartDiskInformation::validateBadAttributes() { - foreach (SmartAttributeParsedData attribute, m_Attributes) { + for (const SmartAttributeParsedData &attribute : qAsConst(m_Attributes)) { if (attribute.prefailure()) { if (attribute.goodNowValid() && !attribute.goodNow()) m_BadAttributeNow = true; @@ -167,7 +167,7 @@ void SmartDiskInformation::validateBadAttributes() SmartAttributeParsedData *SmartDiskInformation::findAttribute(quint32 id) { SmartAttributeParsedData *attr = nullptr; - foreach (SmartAttributeParsedData attribute, m_Attributes) { + for (const SmartAttributeParsedData &attribute : qAsConst(m_Attributes)) { if (id == attribute.id()) { attr = new SmartAttributeParsedData(attribute); break; diff --git a/src/core/smartparser.cpp b/src/core/smartparser.cpp index 81a8214..51b9350 100644 --- a/src/core/smartparser.cpp +++ b/src/core/smartparser.cpp @@ -20,11 +20,12 @@ #include "core/smartattributeparseddata.h" #include "core/smartdiskinformation.h" +#include "util/externalcommand.h" + #include #include #include #include -#include #include SmartParser::SmartParser(const QString &device_path) : @@ -80,7 +81,7 @@ bool SmartParser::init() m_DiskInformation->setModel(smartJson[model_name].toString()); m_DiskInformation->setFirmware(smartJson[firmware].toString()); m_DiskInformation->setSerial(smartJson[serial_number].toString()); - m_DiskInformation->setSize(smartJson[user_capacity].toString().toULongLong()); + m_DiskInformation->setSize(smartJson[user_capacity].toVariant().toULongLong()); QJsonObject selfTest = smartJson[self_test].toObject(); QJsonObject selfTestStatus = selfTest[status].toObject(); @@ -114,20 +115,15 @@ void SmartParser::loadSmartOutput() args.append(QString::fromLocal8Bit("-j")); args.append(devicePath()); - QProcess smartctl; - smartctl.start(QString::fromLocal8Bit("smartctl"), args); + ExternalCommand smartctl(QString::fromLocal8Bit("smartctl"), args); - bool success = smartctl.waitForFinished(); + if (smartctl.run() && smartctl.exitCode() == 0) { + QByteArray output = smartctl.rawOutput(); - if (!success || smartctl.exitStatus() == QProcess::CrashExit) { - qDebug() << "smartctl initialization failed for " << devicePath() << ": " << strerror(errno); - return; + m_SmartOutput = QJsonDocument::fromJson(output); } - - QByteArray output = smartctl.readAllStandardOutput(); - smartctl.close(); - - m_SmartOutput = QJsonDocument::fromJson(output); + else + qDebug() << "smartctl initialization failed for " << devicePath() << ": " << strerror(errno); } } @@ -152,7 +148,7 @@ void SmartParser::loadAttributes() return; } - foreach (QJsonValue value, attributeArray) { + for (const QJsonValue &value : qAsConst(attributeArray)) { SmartAttributeParsedData parsedObject(m_DiskInformation, value.toObject()); m_DiskInformation->addAttribute(parsedObject); } diff --git a/src/core/smartstatus.cpp b/src/core/smartstatus.cpp index 60c8d6e..3c8a35a 100644 --- a/src/core/smartstatus.cpp +++ b/src/core/smartstatus.cpp @@ -230,7 +230,7 @@ void SmartStatus::addAttributes(QList attr) { m_Attributes.clear(); - foreach (SmartAttributeParsedData at, attr) { + for (const SmartAttributeParsedData &at : qAsConst(attr)) { SmartAttribute sm(at); m_Attributes.append(sm); }