From d3cc43c80088ecf6cb1bee9297514e9d32538b69 Mon Sep 17 00:00:00 2001 From: wangrong Date: Fri, 19 Sep 2025 15:39:59 +0800 Subject: [PATCH] fix: Fix fail to verify bad sectors In the previous version, the standard output of badblocks was mistakenly used for judgment. The new version uses the standard error output. Log: Fix fail to verify bad sectors Bug: https://pms.uniontech.com/bug-view-315835.html --- basestruct/utils.cpp | 32 ++++++++++++++++++++++++++------ basestruct/utils.h | 10 ++++++++++ service/diskoperation/thread.cpp | 18 +++++++++--------- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/basestruct/utils.cpp b/basestruct/utils.cpp index 30c0802..4c9854f 100644 --- a/basestruct/utils.cpp +++ b/basestruct/utils.cpp @@ -97,6 +97,17 @@ int Utils::executeCmdWithArtList(const QString &strCmd, const QStringList &strAr } int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error) +{ + return executCmd(strCmd, &outPut, nullptr, error); +} + +int Utils::executCmd(const QString &strCmd) +{ + QString error; + return executCmd(strCmd, nullptr, nullptr, error); +} + +int Utils::executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error) { qDebug() << "Utils::executCmd" << strCmd; @@ -106,10 +117,24 @@ int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error) return -1; } const QString prog = args.takeFirst(); - int exitcode = executeCmdWithArtList(prog, args, outPut, error); + + QProcess proc; + proc.setProgram(prog); + proc.setArguments(args); + proc.start(QIODevice::ReadWrite); + + proc.waitForFinished(-1); + if (stdOut) + *stdOut = QString::fromLocal8Bit(proc.readAllStandardOutput()); + if (stdErr) + *stdErr = QString::fromLocal8Bit(proc.readAllStandardError()); + error = proc.errorString(); + int exitcode = proc.exitCode(); + proc.close(); qDebug() << "Utils::executCmd exitcode: " << exitcode; return exitcode; + } int Utils::executWithInputOutputCmd(const QString &strCmdArg, const QString *inPut, QString &outPut, QString &error) @@ -833,8 +858,3 @@ QString Utils::readContent(const QString &filename) return ret; } -int Utils::executCmd(const QString &strCmd) -{ - QString outPut, error; - return executCmd(strCmd, outPut, error); -} diff --git a/basestruct/utils.h b/basestruct/utils.h index f1d4491..3fba7a7 100644 --- a/basestruct/utils.h +++ b/basestruct/utils.h @@ -54,6 +54,16 @@ class Utils static int executCmd(const QString &strCmd); + /** + * @brief 执行外部命令 + * @param strCmd:命令 + * @param stdOut:命令标准输出 + * @param stdErr:命令标准错误输出 + * @param error:错误信息 + * @return 非0失败 + */ + static int executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error); + /** * @brief 执行外部命令,并提供输入输出 * @param strCmd:命令 diff --git a/service/diskoperation/thread.cpp b/service/diskoperation/thread.cpp index 06acba5..1e2a917 100644 --- a/service/diskoperation/thread.cpp +++ b/service/diskoperation/thread.cpp @@ -68,15 +68,15 @@ void WorkThread::runCount() QString cmd = QString("badblocks -sv -c %1 -b %2 %3 %4 %5").arg(m_checkConut).arg(DEFAULT_BLOCK_SIZE).arg(m_devicePath).arg(j).arg(i); QDateTime ctime = QDateTime::currentDateTime(); - QString output, error; - int exitcode = Utils::executCmd(cmd, output, error); + QString stdErr, error; + int exitcode = Utils::executCmd(cmd, nullptr, &stdErr, error); if (exitcode != 0) { qDebug() << "Failed to execute badblocks command, error:" << error; return; } QDateTime ctime1 = QDateTime::currentDateTime(); - if (output.indexOf("0/0/0") != -1) { + if (stdErr.indexOf("0/0/0") != -1) { // qDebug() << "Block" << i << "is good"; QString cylinderNumber = QString("%1").arg(i); QString cylinderTimeConsuming = QString("%1").arg(ctime.msecsTo(ctime1)); @@ -116,8 +116,8 @@ void WorkThread::runTime() QString cmd = QString("badblocks -sv -b %1 %2 %3 %4").arg(DEFAULT_BLOCK_SIZE).arg(m_devicePath).arg(j).arg(i); QDateTime ctime = QDateTime::currentDateTime(); - QString output, error; - int exitcode = Utils::executCmd(cmd, output, error); + QString stdErr, error; + int exitcode = Utils::executCmd(cmd, nullptr, &stdErr, error); if (exitcode != 0) { qDebug() << "Failed to execute badblocks command, error:" << error; return; @@ -133,7 +133,7 @@ void WorkThread::runTime() emit checkBadBlocksInfo(cylinderNumber, cylinderTimeConsuming, cylinderStatus, cylinderErrorInfo); } else { - if (output.indexOf("0/0/0") != -1) { + if (stdErr.indexOf("0/0/0") != -1) { // qDebug() << "Block" << i << "is good"; QString cylinderNumber = QString("%1").arg(i); QString cylinderTimeConsuming = QString("%1").arg(ctime.msecsTo(ctime1)); @@ -188,15 +188,15 @@ void FixThread::runFix() QString cmd = QString("badblocks -sv -b %1 -w %2 %3 %4").arg(m_checkSize).arg(m_devicePath).arg(k).arg(j); QDateTime ctime = QDateTime::currentDateTime(); - QString output, error; - int exitcode = Utils::executCmd(cmd, output, error); + QString stdErr, error; + int exitcode = Utils::executCmd(cmd, nullptr, &stdErr, error); if (exitcode != 0) { qDebug() << "Failed to execute badblocks command, error:" << error; return; } QDateTime ctime1 = QDateTime::currentDateTime(); - if (output.indexOf("0/0/0") != -1) { + if (stdErr.indexOf("0/0/0") != -1) { // qDebug() << "Fixed block" << j << "successfully"; QString cylinderNumber = QString("%1").arg(j); QString cylinderStatus = "good";