Skip to content

Commit

Permalink
fix: [cmd] Remove command line calls through shell parsing
Browse files Browse the repository at this point in the history
    Remove command line calls through shell parsing

Log: Remove command line calls through shell parsing
Task: https://pms.uniontech.com/task-view-355359.html
  • Loading branch information
starhcq committed Sep 6, 2024
1 parent 29dc2d7 commit 8bf1d5f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 67 deletions.
7 changes: 5 additions & 2 deletions src/app/dman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ int main(int argc, char **argv)
if (Utils::judgeLoongson()) {
//add by wujian 20200907 for 解决龙芯平台,QWebEngine因字体库字体太多,造成启动失败的问题
QString strHomePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QString strExeShell = QString("rm -fr %1/.cache/fontconfig").arg(strHomePath);
shellObj::execSystem(strExeShell);
QString strCmd = QString("rm");
QStringList args;
args << "-rf";
args << QString("%1/.cache/fontconfig").arg(strHomePath);
shellObj::execSystem(strCmd, args);
}

//设置窗口属性
Expand Down
39 changes: 0 additions & 39 deletions src/base/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,6 @@

namespace dman {

bool RunScriptFile(const QStringList &args)
{
Q_ASSERT(!args.isEmpty());
if (args.isEmpty()) {
qCritical() << "RunScriptFile() args is empty!";
return false;
}

// Change working directory.
const QString current_dir(QFileInfo(args.at(0)).absolutePath());
if (!QDir::setCurrent(current_dir)) {
qCritical() << "Failed to change working directory:" << current_dir;
return false;
}

// NOTE(xushaohua): If args[0] is not a script file, bash may raise
// error message.
return SpawnCmd("/bin/bash", args);
}

bool RunScriptFile(const QStringList &args, QString &output, QString &err)
{
Q_ASSERT(!args.isEmpty());
if (args.isEmpty()) {
qCritical() << "RunScriptFile() arg is empty!";
return false;
}

// Change working directory.
const QString current_dir(QFileInfo(args.at(0)).absolutePath());
if (!QDir::setCurrent(current_dir)) {
qCritical() << "Failed to change working directory:" << current_dir;
return false;
}

// TODO(xushaohua): Remove bash
return SpawnCmd("/bin/bash", args, output, err);
}

bool SpawnCmd(const QString &cmd, const QStringList &args)
{
QProcess process;
Expand Down
9 changes: 0 additions & 9 deletions src/base/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

namespace dman {

// Run a script file in bash, no matter it is executable or not.
// First argument in |args| is the path to script file.
// Current working directory is changed to folder of |args[0]|.
// Returns true if |args[0]| executed and exited with 0.
// |output| and |err| are content of stdout and stderr.

bool RunScriptFile(const QStringList &args);
bool RunScriptFile(const QStringList &args, QString &output, QString &err);

// Run |cmd| with |args| in background and returns its result.
bool SpawnCmd(const QString &cmd, const QStringList &args);
bool SpawnCmd(const QString &cmd, const QStringList &args, QString &output);
Expand Down
17 changes: 10 additions & 7 deletions src/controller/shellobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ shellObj::~shellObj()
thread = nullptr;
}

shellObj &shellObj::execSystem(const QString &cmds)
shellObj &shellObj::execSystem(const QString &cmds, const QStringList &args)
{
shellObj *shell = new shellObj();
qDebug() << "shell exec:" << cmds;
//执行shell命令
shell->startSystemThread(cmds);
shell->startSystemThread(cmds, args);
return *shell;
}

int shellObj::startSystemThread(const QString &cmd)
int shellObj::startSystemThread(const QString &cmd, const QStringList &args)
{
this->cmd = cmd;
m_cmd = cmd;
m_args = args;
thread = new QThread();
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
moveToThread(thread);
Expand All @@ -45,8 +46,10 @@ int shellObj::startSystemThread(const QString &cmd)

void shellObj::runSystem()
{
system(cmd.toStdString().data());
qDebug() << "shell " << cmd << endl;
// emit onCompleted(iRst < 0 ? "error" : "");
QProcess process;
process.start(m_cmd, m_args);
process.waitForFinished(-1);

qDebug() << "shell cmd:" << m_cmd << "args:" << m_args;
delete this;
}
11 changes: 8 additions & 3 deletions src/controller/shellobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class shellObj : public QObject
* @param cmds 命令内容
* @return 新建的DMailShell对象
*/
static shellObj &execSystem(const QString &cmds);
static shellObj &execSystem(const QString &cmds, const QStringList &m_args = QStringList());


private:
int startSystemThread(const QString &cmd);
int startSystemThread(const QString &m_cmd, const QStringList &m_args);
/**
* @brief runSystem 采用system来执行shell命令
*/
Expand All @@ -38,7 +38,12 @@ class shellObj : public QObject
/**
* @brief cmd 保存shell命令
*/
QString cmd;
QString m_cmd;

/**
* @brief args 命令参数
*/
QStringList m_args;

/**
* @brief thread 线程对象
Expand Down
22 changes: 16 additions & 6 deletions src/dbus/dmanwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ DManWatcher::DManWatcher():m_Timer(new QTimer (this))
void DManWatcher::onTimeOut()
{
QString cmd, outPut;
QStringList args;
//判断dman客户端是否存在,如果不存在退出服务。
cmd = QString("ps aux | grep -w dman$");
outPut= executCmd(cmd);
int ret = outPut.length();
if (!ret)
cmd = "ps";
args << "aux";
outPut= executCmd(cmd, args);
bool bHasDman = false;
QStringList rows = outPut.split('\n');
for (auto line : rows) {
QStringList items = line.split(' ');
if (items.contains("dman")) {
bHasDman = true;
break;
}
}
if (!bHasDman)
QCoreApplication::exit(0);
}

/**
* @brief 执行外部命令
* @param strCmd:外部命令字符串
*/
QString DManWatcher::executCmd(const QString &strCmd)
QString DManWatcher::executCmd(const QString &strCmd, const QStringList &args)
{
QProcess proc;
proc.start("bash", QStringList() << "-c" << strCmd);
proc.start(strCmd, args);
proc.waitForFinished(-1);
return proc.readAllStandardOutput();
}
2 changes: 1 addition & 1 deletion src/dbus/dmanwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DManWatcher :public QObject
public Q_SLOTS:
void onTimeOut();
private:
QString executCmd(const QString &strCmd);
QString executCmd(const QString &strCmd, const QStringList &args = QStringList());
private:
QTimer *m_Timer=nullptr;
};
Expand Down

0 comments on commit 8bf1d5f

Please sign in to comment.