-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.cpp
233 lines (208 loc) · 8.58 KB
/
main.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**********************************************************************
* main.cpp
**********************************************************************
* Copyright (C) 2015-2024 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
*
* This file is part of MX Snapshot.
*
* MX Snapshot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MX Snapshot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MX Snapshot. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QLibraryInfo>
#include <QLocale>
#include <QTranslator>
#include <csignal>
#include <unistd.h>
#ifndef CLI_BUILD
#include "mainwindow.h"
#include <QApplication>
#endif
#include "batchprocessing.h"
#include "common.h"
#include "log.h"
#include "version.h"
static QTranslator qtTran, qtBaseTran, appTran;
inline QString current_kernel {};
void checkSquashfs();
void setTranslation();
void signalHandler(int signal);
int main(int argc, char *argv[])
{
if (getuid() == 0) {
qputenv("XDG_RUNTIME_DIR", "/run/user/0");
qunsetenv("SESSION_MANAGER");
qputenv("HOME", "/root");
}
const std::initializer_list<int> signalList {SIGINT, SIGTERM, SIGHUP}; // allow SIGQUIT CTRL-\?
for (auto signalName : signalList) {
signal(signalName, signalHandler);
}
QProcess proc;
proc.start("logname", {}, QIODevice::ReadOnly);
proc.waitForFinished();
const QString logname = QString::fromLatin1(proc.readAllStandardOutput().trimmed());
QCommandLineParser parser;
parser.setApplicationDescription(QObject::tr("Tool used for creating a live-CD from the running system"));
parser.addHelpOption();
parser.addVersionOption();
#ifndef CLI_BUILD
parser.addOption({{"c", "cli"}, QObject::tr("Use CLI only")});
#endif
const QVector<QCommandLineOption> options {
{"cores", QObject::tr("Number of CPU cores to be used."), "number"},
{{"d", "directory"}, QObject::tr("Output directory"), "path"},
{{"f", "file"}, QObject::tr("Output filename"), "name"},
{{"k", "kernel"},
QObject::tr("Name a different kernel to use other than the default running kernel, use format returned by "
"'uname -r'")
+ " " + QObject::tr("Or the full path: %1").arg("/boot/vmlinuz-x.xx.x..."),
"version, or path"},
{{"l", "compression-level"},
QObject::tr("Compression level options.") + " "
+ QObject::tr("Use quotes: \"-Xcompression-level <level>\", or \"-Xalgorithm <algorithm>\", or \"-Xhc\", "
"see mksquashfs man page"),
"\"option\""},
{{"m", "month"},
QObject::tr("Create a monthly snapshot, add 'Month' name in the ISO name, skip used space calculation") + " "
+ QObject::tr("This option sets reset-accounts and compression to defaults, arguments changing those "
"items will be ignored"),
""},
{{"n", "no-checksums"}, QObject::tr("Don't calculate checksums for resulting ISO file"), ""},
{{"o", "override-size"}, QObject::tr("Skip calculating free space to see if the resulting ISO will fit"), ""},
{{"p", "preempt"}, QObject::tr("Option to fix issue with calculating checksums on preempt_rt kernels"), ""},
{{"r", "reset"}, QObject::tr("Resetting accounts (for distribution to others)"), ""},
{{"s", "checksums"}, QObject::tr("Calculate checksums for resulting ISO file"), ""},
{{"t", "throttle"},
QObject::tr("Throttle the I/O input rate by the given percentage. This can be used to reduce the I/O and CPU "
"consumption of Mksquashfs."),
"number"},
{{"w", "workdir"}, QObject::tr("Work directory"), "path"},
{{"x", "exclude"},
QObject::tr("Exclude main folders, valid choices: ")
+ "Desktop, Documents, Downloads, Flatpaks, Music, Networks, Pictures, Steam, Videos, VirtualBox. "
+ QObject::tr("Use the option one time for each item you want to exclude"),
"one item"},
{{"z", "compression"},
QObject::tr("Compression format, valid choices: ") + "lz4, lzo, gzip, xz, zstd",
"format"},
{"shutdown", QObject::tr("Shutdown computer when done.")}};
for (const auto &option : options) {
parser.addOption(option);
}
QCoreApplication::setApplicationVersion(VERSION);
QCoreApplication::setOrganizationName("MX-Linux");
parser.process(QCoreApplication(argc, argv));
const QString compressionValue = parser.value("compression");
const QStringList allowedComp {"lz4", "lzo", "gzip", "xz", "zstd"};
if (!compressionValue.isEmpty() && !allowedComp.contains(compressionValue)) {
qDebug() << "Unsupported compression format:" << compressionValue;
return EXIT_FAILURE;
}
QCoreApplication *app;
#ifdef CLI_BUILD
app = new QCoreApplication(argc, argv);
#else
if (parser.isSet("cli") || parser.isSet("help")) {
app = new QCoreApplication(argc, argv);
} else {
app = new QApplication(argc, argv);
QApplication::setApplicationDisplayName(QObject::tr("MX Snapshot"));
}
#endif
if (logname == "root") {
const QString message = QObject::tr(
"You seem to be logged in as root, please log out and log in as normal user to use this program.");
#ifndef CLI_BUILD
if (QCoreApplication::instance()->inherits("QApplication")) {
QMessageBox::critical(nullptr, QObject::tr("Error"), message);
} else
#endif
{
qDebug() << message;
return EXIT_FAILURE;
}
}
setTranslation();
checkSquashfs();
const bool isGuiApp = QCoreApplication::instance()->inherits("QApplication");
const bool hasAuthTools = QFile::exists("/usr/bin/pkexec") || QFile::exists("/usr/bin/gksu");
if (getuid() != 0 && (!isGuiApp || !hasAuthTools)) {
qDebug().noquote() << QObject::tr("You must run this program with sudo or pkexec.");
return EXIT_FAILURE;
}
const Log setLog("/tmp/" + app->applicationName() + ".log");
qDebug().noquote() << app->applicationName() << QObject::tr("version:") << app->applicationVersion();
if (argc > 1) {
qDebug().noquote() << "Args:" << app->arguments();
}
if (!isGuiApp) {
Batchprocessing batch(parser);
QTimer::singleShot(0, app, &QCoreApplication::quit);
app->exec();
}
#ifndef CLI_BUILD
else {
MainWindow w(parser);
w.show();
app->exec();
}
#endif
}
void setTranslation()
{
const QString localeName = QLocale().name();
const QString translationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
const QString appName = QCoreApplication::applicationName();
if (qtTran.load("qt_" + localeName, translationsPath)) {
QCoreApplication::installTranslator(&qtTran);
}
if (qtBaseTran.load("qtbase_" + localeName, translationsPath)) {
QCoreApplication::installTranslator(&qtBaseTran);
}
if (appTran.load("mx-snapshot_" + localeName, "/usr/share/" + appName + "/locale")) {
QCoreApplication::installTranslator(&appTran);
}
}
void checkSquashfs()
{
QProcess proc;
proc.start("uname", {"-r"});
proc.waitForFinished();
current_kernel = proc.readAllStandardOutput().trimmed();
const QString configPath = "/boot/config-" + current_kernel;
if (QFile::exists(configPath) && QProcess::execute("grep", {"-q", "^CONFIG_SQUASHFS=[ym]", configPath}) != 0) {
const QString message = QObject::tr("Current kernel doesn't support Squashfs, cannot continue.");
#ifndef CLI_BUILD
if (QCoreApplication::instance()->inherits("QApplication")) {
QMessageBox::critical(nullptr, QObject::tr("Error"), message);
} else
#endif
{
qDebug() << message;
}
exit(EXIT_FAILURE);
}
}
void signalHandler(int signal)
{
const auto signame = strsignal(signal);
qDebug() << "\nReceived signal:" << (signame ? signame : "Unknown signal");
QCoreApplication::quit();
}