Skip to content

Commit 73bea3f

Browse files
committed
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
1 parent d1b6ad9 commit 73bea3f

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

basestruct/utils.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error)
112112
return exitcode;
113113
}
114114

115+
int Utils::executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error)
116+
{
117+
qDebug() << "Utils::executCmd" << strCmd;
118+
119+
QStringList args = parseCombinedArgString(strCmd);
120+
if (args.isEmpty()) {
121+
error = "args is empty";
122+
return -1;
123+
}
124+
const QString prog = args.takeFirst();
125+
126+
QProcess proc;
127+
proc.setProgram(prog);
128+
proc.setArguments(args);
129+
proc.start(QIODevice::ReadWrite);
130+
131+
proc.waitForFinished(-1);
132+
if (stdOut)
133+
*stdOut = proc.readAllStandardOutput().data();
134+
if (stdErr)
135+
*stdErr = proc.readAllStandardError().data();
136+
error = proc.errorString();
137+
int exitcode = proc.exitCode();
138+
proc.close();
139+
140+
qDebug() << "Utils::executCmd exitcode: " << exitcode;
141+
return exitcode;
142+
143+
}
144+
115145
int Utils::executWithInputOutputCmd(const QString &strCmdArg, const QString *inPut, QString &outPut, QString &error)
116146
{
117147
qDebug() << "Entering Utils::executWithInputOutputCmd with command:" << strCmdArg;

basestruct/utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class Utils
5454

5555
static int executCmd(const QString &strCmd);
5656

57+
/**
58+
* @brief 执行外部命令
59+
* @param strCmd:命令
60+
* @param stdOut:命令标准输出
61+
* @param stdErr:命令标准错误输出
62+
* @param error:错误信息
63+
* @return 非0失败
64+
*/
65+
static int executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error);
66+
5767
/**
5868
* @brief 执行外部命令,并提供输入输出
5969
* @param strCmd:命令

service/diskoperation/thread.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ void WorkThread::runCount()
6868
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);
6969

7070
QDateTime ctime = QDateTime::currentDateTime();
71-
QString output, error;
72-
int exitcode = Utils::executCmd(cmd, output, error);
71+
QString stdErr, error;
72+
int exitcode = Utils::executCmd(cmd, NULL, &stdErr, error);
7373
if (exitcode != 0) {
7474
qDebug() << "Failed to execute badblocks command, error:" << error;
7575
return;
7676
}
7777
QDateTime ctime1 = QDateTime::currentDateTime();
7878

79-
if (output.indexOf("0/0/0") != -1) {
79+
if (stdErr.indexOf("0/0/0") != -1) {
8080
// qDebug() << "Block" << i << "is good";
8181
QString cylinderNumber = QString("%1").arg(i);
8282
QString cylinderTimeConsuming = QString("%1").arg(ctime.msecsTo(ctime1));
@@ -116,8 +116,8 @@ void WorkThread::runTime()
116116
QString cmd = QString("badblocks -sv -b %1 %2 %3 %4").arg(DEFAULT_BLOCK_SIZE).arg(m_devicePath).arg(j).arg(i);
117117

118118
QDateTime ctime = QDateTime::currentDateTime();
119-
QString output, error;
120-
int exitcode = Utils::executCmd(cmd, output, error);
119+
QString stdErr, error;
120+
int exitcode = Utils::executCmd(cmd, NULL, &stdErr, error);
121121
if (exitcode != 0) {
122122
qDebug() << "Failed to execute badblocks command, error:" << error;
123123
return;
@@ -133,7 +133,7 @@ void WorkThread::runTime()
133133

134134
emit checkBadBlocksInfo(cylinderNumber, cylinderTimeConsuming, cylinderStatus, cylinderErrorInfo);
135135
} else {
136-
if (output.indexOf("0/0/0") != -1) {
136+
if (stdErr.indexOf("0/0/0") != -1) {
137137
// qDebug() << "Block" << i << "is good";
138138
QString cylinderNumber = QString("%1").arg(i);
139139
QString cylinderTimeConsuming = QString("%1").arg(ctime.msecsTo(ctime1));
@@ -188,15 +188,15 @@ void FixThread::runFix()
188188
QString cmd = QString("badblocks -sv -b %1 -w %2 %3 %4").arg(m_checkSize).arg(m_devicePath).arg(k).arg(j);
189189

190190
QDateTime ctime = QDateTime::currentDateTime();
191-
QString output, error;
192-
int exitcode = Utils::executCmd(cmd, output, error);
191+
QString stdErr, error;
192+
int exitcode = Utils::executCmd(cmd, NULL, &stdErr, error);
193193
if (exitcode != 0) {
194194
qDebug() << "Failed to execute badblocks command, error:" << error;
195195
return;
196196
}
197197
QDateTime ctime1 = QDateTime::currentDateTime();
198198

199-
if (output.indexOf("0/0/0") != -1) {
199+
if (stdErr.indexOf("0/0/0") != -1) {
200200
// qDebug() << "Fixed block" << j << "successfully";
201201
QString cylinderNumber = QString("%1").arg(j);
202202
QString cylinderStatus = "good";

0 commit comments

Comments
 (0)