-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.cpp
More file actions
116 lines (100 loc) · 3.42 KB
/
cmd.cpp
File metadata and controls
116 lines (100 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "cmd.h"
#include <QApplication>
#include <QDebug>
#include <QEventLoop>
#include <QFile>
#include <QTemporaryFile>
#include <unistd.h>
Cmd::Cmd(QObject *parent)
: QProcess(parent),
elevate {QFile::exists("/usr/bin/pkexec") ? "/usr/bin/pkexec" : "/usr/bin/gksu"},
helper {"/usr/lib/" + QApplication::applicationName() + "/helper"}
{
}
QString Cmd::getOut(const QString &cmd, QuietMode quiet)
{
QString output;
run(cmd, quiet);
output = QString::fromUtf8(readAllStandardOutput()).trimmed();
return output;
}
QString Cmd::getOutAsRoot(const QString &program, const QStringList &args, QuietMode quiet, StderrMode stderrMode)
{
QString output;
procAsRoot(program, args, &output, nullptr, quiet, stderrMode);
return output;
}
bool Cmd::proc(const QString &program, const QStringList &args, QString *output, const QByteArray *input,
QuietMode quiet)
{
if (state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << QProcess::program() << arguments();
return false;
}
if (quiet == QuietMode::No) {
qDebug() << program << args;
}
QEventLoop loop;
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
start(program, args);
if (input && !input->isEmpty()) {
write(*input);
}
closeWriteChannel();
loop.exec();
if (output) {
*output = QString::fromUtf8(readAllStandardOutput()).trimmed();
}
emit done();
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}
bool Cmd::helperProc(const QStringList &helperArgs, QString *output, const QByteArray *input, QuietMode quiet)
{
const bool isRoot = (getuid() == 0);
if (!isRoot && elevate.isEmpty()) {
qWarning() << "No elevation command available";
return false;
}
QStringList programArgs = helperArgs;
const QString program = isRoot ? helper : elevate;
if (!isRoot) {
programArgs.prepend(helper);
}
return proc(program, programArgs, output, input, quiet);
}
bool Cmd::procAsRoot(const QString &program, const QStringList &args, QString *output, const QByteArray *input,
QuietMode quiet, StderrMode stderrMode)
{
QStringList helperArgs {"exec", program};
if (stderrMode == StderrMode::Suppress) {
helperArgs << "--quiet-stderr";
}
helperArgs += args;
return helperProc(helperArgs, output, input, quiet);
}
bool Cmd::run(const QString &cmd, QuietMode quiet)
{
return proc("/bin/bash", {"-c", cmd}, nullptr, nullptr, quiet);
}
bool Cmd::runAsRoot(const QString &program, const QStringList &args, QuietMode quiet, StderrMode stderrMode)
{
return procAsRoot(program, args, nullptr, nullptr, quiet, stderrMode);
}
bool Cmd::appendLineAsRoot(const QString &path, const QString &line, QuietMode quiet)
{
return helperProc({"append-line", path, line}, nullptr, nullptr, quiet);
}
bool Cmd::writeFileAsRoot(const QString &path, const QByteArray &content, QuietMode quiet)
{
QTemporaryFile tempFile;
if (!tempFile.open()) {
qWarning() << "Unable to create temporary file for root write";
return false;
}
if (tempFile.write(content) != content.size()) {
qWarning() << "Unable to stage content for root write";
return false;
}
tempFile.flush();
return helperProc({"write-file-from", path, tempFile.fileName()}, nullptr, nullptr, quiet);
}