@@ -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
440442bool 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)
595599void 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