Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 76 additions & 47 deletions service/diskoperation/DeviceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,19 @@
}
}

bool DeviceStorage::getDiskInfoFromHwinfo(const QString &devicePath)
bool DeviceStorage::getDiskInfoFromHwinfo(const QString &devicePath, const QString &cachedOutput)

Check warning on line 355 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getDiskInfoFromHwinfo' is never used.
{
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<QString, QString> mapInfo;
Expand Down Expand Up @@ -442,13 +446,18 @@
qDebug() << "DeviceStorage::getMapInfoFromInput END";
}

bool DeviceStorage::getDiskInfoFromLshw(const QString &devicePath)
bool DeviceStorage::getDiskInfoFromLshw(const QString &devicePath, const QString &cachedOutput)

Check warning on line 449 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getDiskInfoFromLshw' is never used.
{
qDebug() << "DeviceStorage::getDiskInfoFromLshw BEGIN";
QString outPut, error;
if (Utils::executCmd("lshw -C disk", outPut, error) != 0) {
qDebug() << "Failed to execute lshw command, error:" << error;
return false;
QString outPut;
if (cachedOutput.isEmpty()) {
QString error;
if (Utils::executCmd("lshw -C disk", outPut, error) != 0) {
qDebug() << "Failed to execute lshw command, error:" << error;
return false;
}
} else {
outPut = cachedOutput;
}

QString diskInfo;
Expand Down Expand Up @@ -570,21 +579,20 @@
qDebug() << "DeviceStorage::loadLsblkInfo END";
}

bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath)
bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath, const QString &cachedOutput, int cachedExitCode)

Check warning on line 582 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getDiskInfoFromSmartCtl' is never used.
{
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;
}

Expand All @@ -598,22 +606,20 @@
return true;
}

void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model)
void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model, const QString &cachedSmartctlOutput, const QString &cachedLshwOutput)

Check warning on line 609 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getDiskInfoModel' is never used.
{
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");
Expand All @@ -626,11 +632,14 @@
}
}

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");
Expand Down Expand Up @@ -736,7 +745,7 @@
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)

Check warning on line 748 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'getDiskInfoInterface' is never used.

Check warning on line 748 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'model' can be declared as reference to const
{
qDebug() << "DeviceStorage::getDiskInfoInterface BEGIN";
QString bootDevicePath("/proc/bootdevice/product_name");
Expand All @@ -759,13 +768,18 @@

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];
Expand Down Expand Up @@ -1041,4 +1055,19 @@
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;
}

}
13 changes: 8 additions & 5 deletions service/diskoperation/DeviceStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,6 +49,9 @@ class DeviceStorage

void getMapInfoFromSmartctl(QMap<QString, QString> &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<QString, QString> &mapInfo);
/**@brief:解析从 sudo hwinfo --disk 中获取的消息,KLU专用*/
Expand Down
28 changes: 23 additions & 5 deletions service/diskoperation/partedcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading