Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions basestruct/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions basestruct/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:命令
Expand Down
18 changes: 9 additions & 9 deletions service/diskoperation/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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";
Expand Down