-
Notifications
You must be signed in to change notification settings - Fork 0
/
maincli.cpp
176 lines (155 loc) · 7.15 KB
/
maincli.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
/**********************************************************************
* main.cpp
**********************************************************************
* Copyright (C) 2015 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 <QCoreApplication>
#include <QCommandLineParser>
#include <QDateTime>
#include <QDebug>
#include <QIcon>
#include <QLibraryInfo>
#include <QLocale>
#include <QScopedPointer>
#include <QTranslator>
#include <signal.h>
#include <unistd.h>
#include "version.h"
#include "batchprocessing.h"
static QScopedPointer<QFile> logFile;
void checkSquashfs();
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
void runApp(QCommandLineParser parser);
void setLog();
void setTranslation();
void signalHandler(int sig);
int main(int argc, char *argv[])
{
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
signal(SIGHUP, signalHandler);
//signal(SIGQUIT, signalHandler); // allow SIGQUIT CTRL-\?
QCommandLineParser parser;
parser.setApplicationDescription(QObject::tr("Tool used for creating a live-CD from the running system"));
parser.addHelpOption();
parser.addVersionOption();
parser.addOption({{"d", "directory"}, QObject::tr("Output directory"), QObject::tr("path")});
parser.addOption({{"f", "file"}, QObject::tr("Output filename"), QObject::tr("name")});
parser.addOption({{"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..."), QObject::tr("version, or path")});
parser.addOption({{"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")});
parser.addOption({{"n", "no-checksums"}, QObject::tr("Don't calculate checksums for resulting ISO file")});
parser.addOption({{"p", "preempt"}, QObject::tr("Option to fix issue with calculating checksums on preempt_rt kernels")});
parser.addOption({{"r", "reset"}, QObject::tr("Resetting accounts (for distribution to others)")});
parser.addOption({{"s", "checksums"}, QObject::tr("Calculate checksums for resulting ISO file")});
parser.addOption({{"x", "exclude"}, QObject::tr("Exclude main folders, valid choices: ") + "Desktop, Documents, Downloads, Music, Networks, Pictures, Videos." + " " +
QObject::tr("Use the option one time for each item you want to exclude"), QObject::tr("one item")});
parser.addOption({{"z", "compression"}, QObject::tr("Compression format, valid choices: ") + "lz4, lzo, gzip, xz", QObject::tr("format")});
QStringList opts;
for (int i = 0; i < argc; ++i)
opts << QString(argv[i]);
parser.parse(opts);
QStringList allowed_comp {"lz4", "lzo", "gzip", "xz"};
if (!parser.value("compression").isEmpty())
if (!allowed_comp.contains(parser.value("compression"))) {
qDebug() << "Wrong compression format";
return EXIT_FAILURE;
}
QCoreApplication app(argc, argv);
app.setApplicationVersion(VERSION);
parser.process(app);
setTranslation();
checkSquashfs();
// root guard
if (system("logname |grep -q ^root$") == 0) {
qDebug() << QObject::tr("You seem to be logged in as root, please log out and log in as normal user to use this program.");
exit(EXIT_FAILURE);
}
if (getuid() == 0) {
setLog();
qDebug().noquote() << qApp->applicationName() << QObject::tr("version:") << qApp->applicationVersion();
if (argc > 1) qDebug().noquote() << "Args:" << qApp->arguments();
Batchprocessing batch(parser);
QTimer::singleShot(0, &app, &QCoreApplication::quit);
return app.exec();
} else {
qDebug().noquote() << QObject::tr("You must run this program as root.");
return EXIT_FAILURE;
}
}
// The implementation of the handler
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QTextStream term_out(stdout);
msg.contains("\r") ? term_out << msg << flush: term_out << msg << "\n" << flush;
if (msg.startsWith("\033[2KProcessing")) return;
QTextStream out(logFile.data());
out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz ");
switch (type)
{
case QtInfoMsg: out << QStringLiteral("INF "); break;
case QtDebugMsg: out << QStringLiteral("DBG "); break;
case QtWarningMsg: out << QStringLiteral("WRN "); break;
case QtCriticalMsg: out << QStringLiteral("CRT "); break;
case QtFatalMsg: out << QStringLiteral("FTL "); break;
}
out << context.category << QStringLiteral(": ") << msg << endl;
}
void setTranslation()
{
QTranslator qtTran;
if (qtTran.load(QLocale::system(), "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
qApp->installTranslator(&qtTran);
QTranslator qtBaseTran;
if (qtBaseTran.load("qtbase_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
qApp->installTranslator(&qtBaseTran);
QTranslator appTran;
if (appTran.load(qApp->applicationName() + "_" + QLocale::system().name(), "/usr/share/" + qApp->applicationName() + "/locale"))
qApp->installTranslator(&appTran);
}
// Check if SQUASHFS is available
void checkSquashfs()
{
if (system("[ -f /boot/config-$(uname -r) ]") == 0 && system("grep -q ^CONFIG_SQUASHFS=[ym] /boot/config-$(uname -r)") != 0) {
qDebug() << QObject::tr("Current kernel doesn't support Squashfs, cannot continue.");
exit(EXIT_FAILURE);
}
}
void setLog()
{
QString log_name= "/var/log/" + qApp->applicationName() + ".log";
system("[ -f " + log_name.toUtf8() + " ] && mv " + log_name.toUtf8() + " " + log_name.toUtf8() + ".old");
logFile.reset(new QFile(log_name));
logFile.data()->open(QFile::Append | QFile::Text);
qInstallMessageHandler(messageHandler);
}
void signalHandler(int sig)
{
switch (sig)
{
case 1: qDebug() << "\nSIGHUP"; break;
case 2: qDebug() << "\nSIGINT"; break;
case 3: qDebug() << "\nSIGQUIT"; break;
case 15: qDebug() << "\nSIGTERM"; break;
}
qApp->quit(); // quit app anyway in case a subprocess was killed, but at least this calls aboutToQuit
}