Skip to content

Commit 01b0f3f

Browse files
committed
fix: Fix fail to get device info
- Refactor command execution code to use a unified API with separated program name and parameter list. - Update .gitignore to include additional files and directories. - Remove unused watcher class and related code to streamline the service. Log: Fix fail to get device info Bug: https://pms.uniontech.com/bug-view-303363.html
1 parent 8f6b2c5 commit 01b0f3f

11 files changed

Lines changed: 215 additions & 264 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
.build
33
.qmake.stash
44
*.qm
5+
.vscode
56

67
# AI
78
.cursor/
89
.cursorindexingignore
910
.specstory/
11+
12+
# debian output
13+
debian/.debhelper/
14+
debian/debhelper-build-stamp
15+
debian/deepin-diskmanager.substvars
16+
debian/deepin-diskmanager/
17+
debian/files
18+
obj-x86_64-linux-gnu/

application/main.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ int executCmd(const QString &strCmd, QString &outPut, QString &error)
7777
{
7878
qDebug() << "executCmd called with command:" << strCmd;
7979
QProcess proc;
80-
#if QT_VERSION_MAJOR > 5
81-
qDebug() << "QT_VERSION_MAJOR > 5, using split command start.";
82-
QStringList list = strCmd.split(" ");
83-
proc.start(list.at(0), QStringList() << list.at(1));
84-
#else
85-
qDebug() << "QT_VERSION_MAJOR <= 5, using direct command start.";
86-
proc.start(strCmd);
87-
#endif
80+
QStringList args = strCmd.split(" ");
81+
if (args.isEmpty()) {
82+
error = "args is empty";
83+
return -1;
84+
}
85+
const QString prog = args.takeFirst();
86+
proc.start(prog, args);
87+
8888
proc.waitForFinished(-1);
8989
outPut = proc.readAllStandardOutput();
9090
error = proc.readAllStandardError();
@@ -150,7 +150,9 @@ int main(int argc, char *argv[])
150150

151151
if (!executCmd(cmd, oldPid, error)) {
152152
qDebug() << "deepin-diskmanager-service found, sending Quit signal.";
153-
proc.startDetached("/usr/bin/dbus-send --system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
153+
QString args = QString("--system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
154+
QStringList argList = args.split(" ");
155+
proc.startDetached("/usr/bin/dbus-send", argList);
154156
}
155157

156158
QStringList argList;

application/widgets/mainwindow.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
9393
m_central->HandleQuit();
9494
// m_handler->Quit();
9595
QProcess proc;
96-
proc.startDetached("/usr/bin/dbus-send --system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
96+
QString args = QString("--system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
97+
QStringList argList = args.split(" ");
98+
proc.startDetached("/usr/bin/dbus-send", argList);
9799

98100
DMainWindow::closeEvent(event);
99101
qDebug() << "MainWindow::closeEvent completed.";
@@ -172,7 +174,9 @@ void MainWindow::onHandleQuitAction()
172174
qDebug() << __FUNCTION__;
173175

174176
QProcess proc;
175-
proc.startDetached("/usr/bin/dbus-send --system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
177+
QString args = QString("--system --type=method_call --dest=com.deepin.diskmanager /com/deepin/diskmanager com.deepin.diskmanager.Quit");
178+
QStringList argList = args.split(" ");
179+
proc.startDetached("/usr/bin/dbus-send", argList);
176180

177181
}
178182

basestruct/utils.cpp

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,46 @@ Utils::Utils()
2222
qDebug() << "Utils constructor";
2323
}
2424

25+
static QStringList parseCombinedArgString(const QString &program)
26+
{
27+
QStringList args;
28+
QString tmp;
29+
int quoteCount = 0;
30+
bool inQuote = false;
31+
32+
// handle quoting. tokens can be surrounded by double quotes
33+
// "hello world". three consecutive double quotes represent
34+
// the quote character itself.
35+
for (int i = 0; i < program.size(); ++i) {
36+
if (program.at(i) == QLatin1Char('"')) {
37+
++quoteCount;
38+
if (quoteCount == 3) {
39+
// third consecutive quote
40+
quoteCount = 0;
41+
tmp += program.at(i);
42+
}
43+
continue;
44+
}
45+
if (quoteCount) {
46+
if (quoteCount == 1)
47+
inQuote = !inQuote;
48+
quoteCount = 0;
49+
}
50+
if (!inQuote && program.at(i).isSpace()) {
51+
if (!tmp.isEmpty()) {
52+
args += tmp;
53+
tmp.clear();
54+
}
55+
} else {
56+
tmp += program.at(i);
57+
}
58+
}
59+
if (!tmp.isEmpty())
60+
args += tmp;
61+
62+
return args;
63+
}
64+
2565
QString Utils::findProgramInPath(const QString &proName)
2666
{
2767
qDebug() << "Utils::findProgramInPath----" << proName;
@@ -58,34 +98,15 @@ int Utils::executeCmdWithArtList(const QString &strCmd, const QStringList &strAr
5898

5999
int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error)
60100
{
61-
qDebug() << "Utils::executCmd*******--------" << strCmd;
62-
63-
// qDebug() << "Utils::executCmd*******--------" << strCmd;
64-
// QProcess proc;
65-
// // proc.open(QIODevice::ReadWrite);
66-
// proc.start(strCmd);
67-
// proc.waitForFinished(-1);
68-
// outPut = proc.readAllStandardOutput();
69-
// QString stderror = proc.readAllStandardError();
70-
// error = proc.errorString();
71-
// int exitcode = proc.exitCode();
72-
// // qDebug()<<output<<error<<stderror;
73-
// //mkfs.ext4 -V have no output,stderror include useful info
74-
// if (outPut.isEmpty() && !stderror.isEmpty()) {
75-
// outPut = stderror;
76-
// }
77-
// proc.close();
78-
79-
QStringList cmdList = strCmd.trimmed().split(" ");
80-
81-
QStringList argList;
82-
for (int i = 1; i < cmdList.size(); i++) {
83-
if (!cmdList[i].isEmpty()) {
84-
qDebug() << "Appending argument:" << cmdList[i];
85-
argList.append(cmdList[i]);
86-
}
101+
qDebug() << "Utils::executCmd" << strCmd;
102+
103+
QStringList args = parseCombinedArgString(strCmd);
104+
if (args.isEmpty()) {
105+
error = "args is empty";
106+
return -1;
87107
}
88-
int exitcode = executeCmdWithArtList(cmdList[0], argList, outPut, error);
108+
const QString prog = args.takeFirst();
109+
int exitcode = executeCmdWithArtList(prog, args, outPut, error);
89110

90111
qDebug() << "Utils::executCmd exitcode: " << exitcode;
91112
return exitcode;
@@ -97,11 +118,13 @@ int Utils::executWithInputOutputCmd(const QString &strCmdArg, const QString *inP
97118
QProcess proc;
98119
int exitCode;
99120

100-
#if QT_VERSION_MAJOR > 5
101-
proc.start(strCmdArg);
102-
#else
103-
proc.start(strCmdArg, QIODevice::ReadWrite);
104-
#endif
121+
QStringList args = parseCombinedArgString(strCmdArg);
122+
if (args.isEmpty()) {
123+
error = "args is empty";
124+
return -1;
125+
}
126+
const QString prog = args.takeFirst();
127+
proc.start(prog, args);
105128

106129
if (inPut) {
107130
qDebug() << "Writing input to process:" << *inPut;
@@ -812,12 +835,6 @@ QString Utils::readContent(const QString &filename)
812835

813836
int Utils::executCmd(const QString &strCmd)
814837
{
815-
qDebug() << "Utils::executCmd - Executing:" << strCmd;
816-
QProcess proc;
817-
proc.setProgram(strCmd);
818-
proc.start(QIODevice::ReadWrite);
819-
proc.waitForFinished(-1);
820-
int exitcode = proc.exitCode();
821-
proc.close();
822-
return exitcode;
838+
QString outPut, error;
839+
return executCmd(strCmd, outPut, error);
823840
}

debian/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
deepin-diskmanager (6.0.10) unstable; urgency=medium
2+
3+
* fix: Fix fail to get device info
4+
5+
-- wangrong <wangrong@uniontech.com> Tue, 29 Jul 2025 19:10:31 +0800
6+
17
deepin-diskmanager (6.0.9) unstable; urgency=medium
28

39
* chore: Update deepin-manual resources (#157)

service/diskoperation/DeviceStorage.cpp

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,12 @@ bool DeviceStorage::getDiskInfoFromHwinfo(const QString &devicePath)
353353
{
354354
qDebug() << "Getting disk info from hwinfo for device:" << devicePath;
355355
QString cmd = QString("hwinfo --disk --only %1").arg(devicePath);
356-
QProcess proc;
357-
proc.start(cmd);
358-
proc.waitForFinished(-1);
359-
QString outPut = proc.readAllStandardOutput();
356+
QString outPut;
357+
QString error;
358+
if (Utils::executCmd(cmd, outPut, error) != 0) {
359+
qDebug() << "Failed to execute hwinfo command, error:" << error;
360+
return false;
361+
}
360362

361363
QMap<QString, QString> mapInfo;
362364

@@ -440,10 +442,11 @@ void DeviceStorage::getMapInfoFromInput(const QString &info, QMap<QString, QStri
440442
bool DeviceStorage::getDiskInfoFromLshw(const QString &devicePath)
441443
{
442444
qDebug() << "DeviceStorage::getDiskInfoFromLshw BEGIN";
443-
QProcess proc;
444-
proc.start("sudo lshw -C disk");
445-
proc.waitForFinished(-1);
446-
QString outPut = proc.readAllStandardOutput();
445+
QString outPut, error;
446+
if (Utils::executCmd("lshw -C disk", outPut, error) != 0) {
447+
qDebug() << "Failed to execute lshw command, error:" << error;
448+
return false;
449+
}
447450

448451
QStringList list = outPut.split("*-disk\n");
449452

@@ -523,12 +526,13 @@ bool DeviceStorage::getDiskInfoFromLsblk(const QString &devicePath)
523526
{
524527
qDebug() << "DeviceStorage::getDiskInfoFromLsblk BEGIN";
525528
QString cmd = QString("lsblk -d -o name,rota %1").arg(devicePath);
526-
QProcess proc;
527-
proc.start(cmd);
528-
proc.waitForFinished(-1);
529+
QString outPut, error;
530+
if (Utils::executCmd(cmd, outPut, error) != 0) {
531+
qDebug() << "Failed to execute lsblk command, error:" << error;
532+
return false;
533+
}
529534

530535
QMap<QString, QString> mapInfo;
531-
QString outPut = proc.readAllStandardOutput();
532536

533537
loadLsblkInfo(outPut, mapInfo);
534538

@@ -568,18 +572,18 @@ bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath)
568572
{
569573
qDebug() << "DeviceStorage::getDiskInfoFromSmartCtl BEGIN";
570574
QString cmd = QString("smartctl --all %1").arg(devicePath);
571-
QProcess proc;
572-
proc.start(cmd);
573-
proc.waitForFinished(-1);
574-
QString outPut = proc.readAllStandardOutput();
575+
QString outPut, error;
576+
int exitcode = Utils::executCmd(cmd, outPut, error);
575577

576578
if (outPut.contains("Please specify device type with the -d option")) {
577579
qDebug() << "need to specify device type";
578-
QString cmd = QString("smartctl --all -d sat %1").arg(devicePath);
579-
QProcess proc;
580-
proc.start(cmd);
581-
proc.waitForFinished(-1);
582-
outPut = proc.readAllStandardOutput();
580+
cmd = QString("smartctl --all -d sat %1").arg(devicePath);
581+
exitcode = Utils::executCmd(cmd, outPut, error);
582+
}
583+
584+
if (exitcode != 0) {
585+
qDebug() << "Failed to execute smartctl command, error:" << error;
586+
return false;
583587
}
584588

585589
QMap<QString, QString> mapInfo;
@@ -595,18 +599,19 @@ bool DeviceStorage::getDiskInfoFromSmartCtl(const QString &devicePath)
595599
void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model)
596600
{
597601
qDebug() << "DeviceStorage::getDiskInfoModel BEGIN";
598-
QProcess proc;
599602
QString cmd = QString("smartctl --all %1").arg(devicePath);
600-
proc.start(cmd);
601-
proc.waitForFinished(-1);
602-
QString outPut = proc.readAllStandardOutput();
603+
QString outPut, error;
604+
int exitcode = Utils::executCmd(cmd, outPut, error);
603605

604606
if (outPut.contains("Please specify device type with the -d option")) {
605607
qDebug() << "need to specify device type";
606608
cmd = QString("smartctl --all -d sat %1").arg(devicePath);
607-
proc.start(cmd);
608-
proc.waitForFinished(-1);
609-
outPut = proc.readAllStandardOutput();
609+
exitcode = Utils::executCmd(cmd, outPut, error);
610+
}
611+
612+
if (exitcode != 0) {
613+
qDebug() << "Failed to execute smartctl command, error:" << error;
614+
return;
610615
}
611616

612617
QStringList infoList = outPut.split("\n");
@@ -620,9 +625,11 @@ void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model)
620625
}
621626

622627
cmd = "lshw -C disk";
623-
proc.start(cmd);
624-
proc.waitForFinished(-1);
625-
outPut = proc.readAllStandardOutput();
628+
exitcode = Utils::executCmd(cmd, outPut, error);
629+
if (exitcode != 0) {
630+
qDebug() << "Failed to execute lshw command, error:" << error;
631+
return;
632+
}
626633

627634
infoList = outPut.split("*-disk\n");
628635
for (int i =0; i < infoList.size(); i++) {
@@ -650,37 +657,36 @@ QString DeviceStorage::getDiskInfoMediaType(const QString &devicePath)
650657
{
651658
qDebug() << "DeviceStorage::getDiskInfoMediaType BEGIN";
652659
QStringList deviceList = devicePath.split("/");
660+
if (deviceList.size() < 2) {
661+
qDebug() << "deviceList.size() < 2, return UnKnow";
662+
return "UnKnow";
663+
}
653664
QString device = deviceList[deviceList.size()-1];
654-
QString value;
655-
QString cmd = QString("cat /sys/block/%1/queue/rotational").arg(device);
656-
QProcess proc;
657-
proc.start(cmd);
658-
proc.waitForFinished(-1);
659-
QString outPut = proc.readAllStandardOutput().trimmed();
660-
value = outPut;
665+
QString rotational_file = QString("/sys/block/%1/queue/rotational").arg(device);
666+
QString value = Utils::readContent(rotational_file).trimmed();
661667

662668
if ("1" == value) {
663669
qDebug() << "value is 1";
664-
cmd = QString("smartctl -i %1").arg(devicePath);
665-
proc.start(cmd);
666-
proc.waitForFinished(-1);
667-
outPut = proc.readAllStandardOutput();
670+
QString cmd = QString("smartctl -i %1").arg(devicePath);
671+
QString outPut, error;
672+
int exitcode = Utils::executCmd(cmd, outPut, error);
668673
if (outPut.contains("Solid State Device")) {
669674
qDebug() << "is Solid State Device";
670675
value = "0";
671676
} else if (outPut.contains("Please specify device type with the -d option")) {
672677
qDebug() << "need to specify device type";
673678
//FIXME: Hard type scsi for USB disk
674679
cmd = QString("smartctl -i -d scsi %1").arg(devicePath);
675-
proc.start(cmd);
676-
proc.waitForFinished(-1);
677-
outPut = proc.readAllStandardOutput();
680+
exitcode = Utils::executCmd(cmd, outPut, error);
678681
if (!outPut.contains("Rotation Rate:")) {
679682
qDebug() << "not contains Rotation Rate";
680683
value = "0";
681684
}
682685
}
683-
686+
if (exitcode != 0) {
687+
qDebug() << "Failed to execute smartctl command, error:" << error;
688+
return "UnKnow";
689+
}
684690
}
685691
if (QString("0") == value) {
686692
qDebug() << "return SSD";
@@ -734,10 +740,13 @@ void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &int
734740
if (interface.isEmpty()) {
735741
qDebug() << "interface is empty";
736742
QString cmd = QString("hwinfo --disk --only %1").arg(devicePath);
737-
QProcess proc;
738-
proc.start(cmd);
739-
proc.waitForFinished(-1);
740-
QString outPut = proc.readAllStandardOutput().trimmed();
743+
QString outPut, error;
744+
int exitcode = Utils::executCmd(cmd, outPut, error);
745+
if (exitcode != 0) {
746+
interface = "UnKnow";
747+
qDebug() << "Failed to execute hwinfo command, error:" << error;
748+
return;
749+
}
741750
QStringList outPutList = outPut.split("(");
742751
interface = outPutList[outPutList.size() - 1].split(" ")[0];
743752
}

0 commit comments

Comments
 (0)