-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (42 loc) · 1.77 KB
/
main.cpp
File metadata and controls
53 lines (42 loc) · 1.77 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
#include <QApplication>
#include <QMessageBox>
#include <QFile>
#include <QRegularExpression>
#include <QProcess>
#include <QSharedMemory>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Prevent multiple instances
QSharedMemory sharedMemory;
sharedMemory.setKey("SnapshotRestorerInstance");
if (!sharedMemory.create(1)) {
// Another instance is already running
return 0;
}
// Check if booted into a snapshot
QFile cmdlineFile("/proc/cmdline");
if (cmdlineFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString cmdline = cmdlineFile.readAll();
QRegularExpression re(R"(/\.snapshots/(\d+)/snapshot)");
QRegularExpressionMatch match = re.match(cmdline);
if (match.hasMatch()) {
QString snapshotId = match.captured(1);
QMessageBox::StandardButton reply;
reply = QMessageBox::question(nullptr, "Snapshot Detected",
"You are booted into snapshot #" + snapshotId +
". Do you want to restore it?",
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
QProcess process;
process.start("pkexec", {"snapper", "rollback", snapshotId});
process.waitForFinished();
// Display output from the snapper rollback command
QString output = process.readAllStandardOutput();
QString errorOutput = process.readAllStandardError();
QMessageBox::information(nullptr, "Snapshot Restored",
output.isEmpty() ? errorOutput : output);
}
}
}
return 0;
}