mirror of
https://github.com/oguzhaninan/Stacer.git
synced 2026-05-19 05:45:38 -04:00
56 lines
1.1 KiB
C++
56 lines
1.1 KiB
C++
#include "command_util.h"
|
|
|
|
#include <QProcess>
|
|
#include <QTextStream>
|
|
#include <QStandardPaths>
|
|
#include <QDebug>
|
|
|
|
#include <memory>
|
|
|
|
QString CommandUtil::sudoExec(const QString &cmd, QStringList args, QByteArray data)
|
|
{
|
|
args.push_front(cmd);
|
|
|
|
QString result("");
|
|
|
|
try {
|
|
result = CommandUtil::exec("pkexec", args, data);
|
|
} catch (QString &ex) {
|
|
qCritical() << ex;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
QString CommandUtil::exec(const QString &cmd, QStringList args, QByteArray data)
|
|
{
|
|
std::unique_ptr<QProcess> process(new QProcess());
|
|
process->start(cmd, args);
|
|
|
|
if (! data.isEmpty()) {
|
|
process->write(data);
|
|
process->waitForBytesWritten();
|
|
process->closeWriteChannel();
|
|
}
|
|
|
|
// 10 minutes
|
|
process->waitForFinished(600*1000);
|
|
|
|
QTextStream stdOut(process->readAllStandardOutput());
|
|
|
|
QString err = process->errorString();
|
|
|
|
process->kill();
|
|
process->close();
|
|
|
|
if (process->error() != QProcess::UnknownError)
|
|
throw err;
|
|
|
|
return stdOut.readAll().trimmed();
|
|
}
|
|
|
|
bool CommandUtil::isExecutable(const QString &cmd)
|
|
{
|
|
return !QStandardPaths::findExecutable(cmd).isEmpty();
|
|
}
|