diff --git a/service/diskoperation/DeviceStorage.cpp b/service/diskoperation/DeviceStorage.cpp index 2f04c89..87135d2 100755 --- a/service/diskoperation/DeviceStorage.cpp +++ b/service/diskoperation/DeviceStorage.cpp @@ -352,15 +352,19 @@ void DeviceStorage::setAttribute(const QMap &mapInfo, const QS } } -bool DeviceStorage::getDiskInfoFromHwinfo(const QString &devicePath) +bool DeviceStorage::getDiskInfoFromHwinfo(const QString &devicePath, const QString &cachedOutput) { qDebug() << "Getting disk info from hwinfo for device:" << devicePath; - QString cmd = QString("hwinfo --disk --only %1").arg(devicePath); QString outPut; - QString error; - if (Utils::executCmd(cmd, outPut, error) != 0) { - qDebug() << "Failed to execute hwinfo command, error:" << error; - return false; + if (cachedOutput.isEmpty()) { + QString cmd = QString("hwinfo --disk --only %1").arg(devicePath); + QString error; + if (Utils::executCmd(cmd, outPut, error) != 0) { + qDebug() << "Failed to execute hwinfo command, error:" << error; + return false; + } + } else { + outPut = cachedOutput; } QMap mapInfo; @@ -442,13 +446,18 @@ void DeviceStorage::getMapInfoFromInput(const QString &info, QMap &m qDebug() << "DeviceStorage::loadLsblkInfo END"; } -bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath) +bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath, const QString &cachedOutput, int cachedExitCode) { qDebug() << "DeviceStorage::getDiskInfoFromSmartCtl BEGIN"; - QString cmd = QString("smartctl --all %1").arg(devicePath); - QString outPut, error; - int exitcode = Utils::executCmd(cmd, outPut, error); - - if (outPut.contains("Please specify device type with the -d option")) { - qDebug() << "need to specify device type"; - cmd = QString("smartctl --all -d sat %1").arg(devicePath); - exitcode = Utils::executCmd(cmd, outPut, error); + QString outPut; + int exitcode; + if (cachedOutput.isEmpty()) { + exitcode = runSmartctlAll(devicePath, outPut); + } else { + outPut = cachedOutput; + exitcode = cachedExitCode; } if (exitcode != 0) { - qDebug() << "Failed to execute smartctl command, error:" << error; + qDebug() << "Failed to execute smartctl command"; return false; } @@ -598,22 +606,20 @@ bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath) return true; } -void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model) +void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model, const QString &cachedSmartctlOutput, const QString &cachedLshwOutput) { qDebug() << "DeviceStorage::getDiskInfoModel BEGIN"; - QString cmd = QString("smartctl --all %1").arg(devicePath); QString outPut, error; - int exitcode = Utils::executCmd(cmd, outPut, error); - - if (outPut.contains("Please specify device type with the -d option")) { - qDebug() << "need to specify device type"; - cmd = QString("smartctl --all -d sat %1").arg(devicePath); - exitcode = Utils::executCmd(cmd, outPut, error); - } - - if (exitcode != 0) { - qDebug() << "Failed to execute smartctl command, error:" << error; - return; + int exitcode = 0; + QString cmd; + if (cachedSmartctlOutput.isEmpty()) { + exitcode = runSmartctlAll(devicePath, outPut); + if (exitcode != 0) { + qDebug() << "Failed to execute smartctl command, error:" << error; + return; + } + } else { + outPut = cachedSmartctlOutput; } QStringList infoList = outPut.split("\n"); @@ -626,11 +632,14 @@ void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model) } } - cmd = "lshw -C disk"; - exitcode = Utils::executCmd(cmd, outPut, error); - if (exitcode != 0) { - qDebug() << "Failed to execute lshw command, error:" << error; - return; + if (cachedLshwOutput.isEmpty()) { + exitcode = Utils::executCmd("lshw -C disk", outPut, error); + if (exitcode != 0) { + qDebug() << "Failed to execute lshw command, error:" << error; + return; + } + } else { + outPut = cachedLshwOutput; } infoList = outPut.split("*-disk\n"); @@ -736,7 +745,7 @@ static bool isPGUX() return 1 == isPGUX; } -void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model) +void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model, const QString &cachedHwinfoOutput) { qDebug() << "DeviceStorage::getDiskInfoInterface BEGIN"; QString bootDevicePath("/proc/bootdevice/product_name"); @@ -759,13 +768,18 @@ void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &int if (interface.isEmpty()) { qDebug() << "interface is empty"; - QString cmd = QString("hwinfo --disk --only %1").arg(devicePath); - QString outPut, error; - int exitcode = Utils::executCmd(cmd, outPut, error); - if (exitcode != 0) { - interface = "UnKnow"; - qDebug() << "Failed to execute hwinfo command, error:" << error; - return; + QString outPut; + if (cachedHwinfoOutput.isEmpty()) { + QString cmd = QString("hwinfo --disk --only %1").arg(devicePath); + QString error; + int exitcode = Utils::executCmd(cmd, outPut, error); + if (exitcode != 0) { + interface = "UnKnow"; + qDebug() << "Failed to execute hwinfo command, error:" << error; + return; + } + } else { + outPut = cachedHwinfoOutput; } QStringList outPutList = outPut.split("("); interface = outPutList[outPutList.size() - 1].split(" ")[0]; @@ -1041,4 +1055,19 @@ void DeviceStorage::getMapInfoFromSmartctl(QMap &mapInfo, cons qDebug() << "DeviceStorage::getMapInfoFromSmartctl END"; } +int DeviceStorage::runSmartctlAll(const QString &devicePath, QString &outPut) +{ + QString cmd = QString("smartctl --all %1").arg(devicePath); + QString error; + int exitcode = Utils::executCmd(cmd, outPut, error); + + if (outPut.contains("Please specify device type with the -d option")) { + qDebug() << "need to specify device type"; + cmd = QString("smartctl --all -d sat %1").arg(devicePath); + exitcode = Utils::executCmd(cmd, outPut, error); + } + + return exitcode; +} + } diff --git a/service/diskoperation/DeviceStorage.h b/service/diskoperation/DeviceStorage.h index e5e8b40..7995dad 100755 --- a/service/diskoperation/DeviceStorage.h +++ b/service/diskoperation/DeviceStorage.h @@ -14,25 +14,25 @@ class DeviceStorage DeviceStorage(); /**@brief:添加从hwinfo中获取的信息*/ - bool getDiskInfoFromHwinfo(const QString &devicePath); + bool getDiskInfoFromHwinfo(const QString &devicePath, const QString &cachedOutput = QString()); /**@brief:添加从lshw中获取的信息*/ - bool getDiskInfoFromLshw(const QString &devicePath); + bool getDiskInfoFromLshw(const QString &devicePath, const QString &cachedOutput = QString()); /**@brief:添加lsblk获取到的介质信息*/ bool getDiskInfoFromLsblk(const QString &devicePath); /**@brief:添加smartctl中获取到的信息*/ - bool getDiskInfoFromSmartCtl(const QString &devicePath); + bool getDiskInfoFromSmartCtl(const QString &devicePath, const QString &cachedOutput = QString(), int cachedExitCode = 0); /**@brief:获取当前磁盘model信息*/ - void getDiskInfoModel(const QString &devicePath, QString &model); + void getDiskInfoModel(const QString &devicePath, QString &model, const QString &cachedSmartctlOutput = QString(), const QString &cachedLshwOutput = QString()); /**@brief:获取当前磁盘介质信息*/ QString getDiskInfoMediaType(const QString &devicePath); /**@brief:获取当前磁盘接口信息*/ - void getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model); + void getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model, const QString &cachedHwinfoOutput = QString()); /**@brief:为HW设备更新信息*/ void updateForHWDevice(const QString &devicePath); @@ -49,6 +49,9 @@ class DeviceStorage void getMapInfoFromSmartctl(QMap &mapInfo, const QString &info, const QString &ch = QString(": ")); + /**@brief:运行 smartctl --all 并处理 -d sat 重试,返回退出码*/ + static int runSmartctlAll(const QString &devicePath, QString &outPut); + /**@brief:解析从 sudo hwinfo --disk 中获取的消息*/ bool setHwinfoInfo(const QMap &mapInfo); /**@brief:解析从 sudo hwinfo --disk 中获取的消息,KLU专用*/ diff --git a/service/diskoperation/partedcore.cpp b/service/diskoperation/partedcore.cpp index 94f99af..f7cd27b 100644 --- a/service/diskoperation/partedcore.cpp +++ b/service/diskoperation/partedcore.cpp @@ -136,19 +136,37 @@ HardDiskInfo PartedCore::getDeviceHardInfo(const QString &devicepath) } DeviceStorage device; - device.getDiskInfoFromHwinfo(devicepath); - device.getDiskInfoFromLshw(devicepath); + // Pre-execute and cache command outputs to eliminate redundant calls + QString smartctlOutput; + int smartctlExitCode = DeviceStorage::runSmartctlAll(devicepath, smartctlOutput); + + QString hwinfoOutput; + { + QString cmd = QString("hwinfo --disk --only %1").arg(devicepath); + QString error; + Utils::executCmd(cmd, hwinfoOutput, error); + } + + QString lshwOutput; + { + QString error; + Utils::executCmd("lshw -C disk", lshwOutput, error); + } + + device.getDiskInfoFromHwinfo(devicepath, hwinfoOutput); + + device.getDiskInfoFromLshw(devicepath, lshwOutput); device.getDiskInfoFromLsblk(devicepath); - device.getDiskInfoFromSmartCtl(devicepath); + device.getDiskInfoFromSmartCtl(devicepath, smartctlOutput, smartctlExitCode); device.m_mediaType = device.getDiskInfoMediaType(devicepath); - device.getDiskInfoModel(devicepath, device.m_model); + device.getDiskInfoModel(devicepath, device.m_model, smartctlOutput, lshwOutput); - device.getDiskInfoInterface(devicepath, device.m_interface, device.m_model); + device.getDiskInfoInterface(devicepath, device.m_interface, device.m_model, hwinfoOutput); device.updateForHWDevice(devicepath);