Skip to content

Commit d3dd370

Browse files
wangrong1069deepin-bot[bot]
authored andcommitted
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 d3dd370

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

basestruct/utils.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ int Utils::executeCmdWithArtList(const QString &strCmd, const QStringList &strAr
9797
}
9898

9999
int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error)
100+
{
101+
return executCmd(strCmd, &outPut, nullptr, error);
102+
}
103+
104+
int Utils::executCmd(const QString &strCmd)
105+
{
106+
QString error;
107+
return executCmd(strCmd, nullptr, nullptr, error);
108+
}
109+
110+
int Utils::executCmd(const QString &strCmd, QString *stdOut, QString *stdErr, QString &error)
100111
{
101112
qDebug() << "Utils::executCmd" << strCmd;
102113

@@ -106,10 +117,24 @@ int Utils::executCmd(const QString &strCmd, QString &outPut, QString &error)
106117
return -1;
107118
}
108119
const QString prog = args.takeFirst();
109-
int exitcode = executeCmdWithArtList(prog, args, outPut, error);
120+
121+
QProcess proc;
122+
proc.setProgram(prog);
123+
proc.setArguments(args);
124+
proc.start(QIODevice::ReadWrite);
125+
126+
proc.waitForFinished(-1);
127+
if (stdOut)
128+
*stdOut = QString::fromLocal8Bit(proc.readAllStandardOutput());
129+
if (stdErr)
130+
*stdErr = QString::fromLocal8Bit(proc.readAllStandardError());
131+
error = proc.errorString();
132+
int exitcode = proc.exitCode();
133+
proc.close();
110134

111135
qDebug() << "Utils::executCmd exitcode: " << exitcode;
112136
return exitcode;
137+
113138
}
114139

115140
int Utils::executWithInputOutputCmd(const QString &strCmdArg, const QString *inPut, QString &outPut, QString &error)
@@ -833,8 +858,3 @@ QString Utils::readContent(const QString &filename)
833858
return ret;
834859
}
835860

836-
int Utils::executCmd(const QString &strCmd)
837-
{
838-
QString outPut, error;
839-
return executCmd(strCmd, outPut, error);
840-
}

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, nullptr, &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, nullptr, &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, nullptr, &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)