/
vault.cpp
442 lines (386 loc) · 12.9 KB
/
vault.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <qtaround/os.hpp>
#include <qtaround/debug.hpp>
#include <vault/vault.hpp>
#include <vault/config.hpp>
#include <transfer.hpp>
#include <QJSValue>
#include "vault.hpp"
namespace os = qtaround::os;
namespace error = qtaround::error;
namespace debug = qtaround::debug;
Q_DECLARE_METATYPE(Vault::Operation)
static const int _vault_operation_ __attribute__((unused))
= qRegisterMetaType<Vault::Operation>();
Q_DECLARE_METATYPE(Vault::ImportExportAction)
static const int _vault_importexportaction_ __attribute__((unused))
= qRegisterMetaType<Vault::ImportExportAction>();
Q_DECLARE_METATYPE(Vault::DataType)
static const int _vault_datatype__ __attribute__((unused))
= qRegisterMetaType<Vault::DataType>();
class Worker : public QObject
{
Q_OBJECT
public:
Worker()
: QObject()
, m_vault(nullptr)
, m_transfer(nullptr)
{
}
~Worker()
{
delete m_vault;
delete m_transfer;
}
Q_INVOKABLE void init(const QString &root)
{
m_vault = new vault::Vault(root);
QVariantMap options = {{ "user.name", "Some Sailor"}, {"user.email", "sailor@jolla.com"}};
if (!m_vault->init(options)) {
error::raise({{"msg", "Can't init vault"}, {"path", root}});
}
syncConfig();
}
void syncConfig()
{
vault::config::Config *global = vault::config::global();
m_vault->config().update(global->units());
}
Q_INVOKABLE void restore(const QString &home, const QStringList &units, const QString &snapshot)
{
debug::debug("Restore: home", home);
m_vault->restore(snapshot, home, units, [this](const QString unit, const QString &status) {
emit progress(Vault::Restore, {{"unit", unit}, {"status", status}});
});
emit done(Vault::Restore, QVariantMap());
}
Q_INVOKABLE void reset()
{
debug::debug("Reset storage master to the last snapshot");
m_vault->resetLastSnapshot();
emit done(Vault::Maintenance, {{"operation", "reset"}});
}
Q_INVOKABLE void backupUnit(const QString &home, const QString &unit)
{
debug::debug(home, "- backup unit", unit);
auto res = m_vault->backupUnit(home, unit);
emit progress(Vault::Backup, {{"unit", unit}, {"status", std::get<2>(res)}});
}
Q_INVOKABLE void restoreUnit(const QString &snapshot
, const QString &home
, const QString &unit)
{
debug::debug(home, "- restore unit", unit, "snapshot", snapshot);
auto res = m_vault->restoreUnit(snapshot, home, unit);
emit progress(Vault::Restore, {{"unit", unit}, {"status", std::get<2>(res)}});
}
Q_INVOKABLE void tagSnapshot(const QString &message)
{
debug::debug("Tag snapshot, message:", message);
auto tag = m_vault->tagSnapshot(message);
emit done(Vault::Backup, {{"tag", tag}});
}
Q_INVOKABLE void backup(const QString &home, const QStringList &units, const QString &message)
{
debug::debug("Backup: home", home);
auto res = m_vault->backup(home, units, message, [this](const QString unit, const QString &status) {
emit progress(Vault::Backup, {{"unit", unit}, {"status", status}});
});
if (res.failedUnits.size() == 0) {
emit done(Vault::Backup, QVariantMap());
} else {
QVariantMap info{{"msg", "Backup of some units is failed"}
, {"reason", "Backup"}
, {"succeeded", res.succededUnits}
, {"failed", res.failedUnits}};
emit error(Vault::Backup, info);
}
}
QStringList snapshots() const
{
auto snapshots = m_vault->snapshots();
QStringList snaps;
for (const vault::Snapshot &ss: snapshots) {
snaps << ss.name();
}
return snaps;
}
Q_INVOKABLE bool destroy()
{
debug::debug("Destroy vault");
QVariantMap msg = {{"destroy", true}, {"ignore_snapshots", true}};
return m_vault->clear(msg);
}
Q_INVOKABLE void eiPrepare(Vault::ImportExportAction action, const QString &path)
{
if (!m_transfer) {
m_transfer = new CardTransfer;
}
try {
CardTransfer::Action ac = action == Vault::Import ? CardTransfer::Import : CardTransfer::Export;
m_transfer->init(m_vault, ac, path);
QVariantMap data;
data["action"] = action ==Vault:: Import ? "import" : "export";
data["src"] = m_transfer->getSrc();
emit done(Vault::ExportImportPrepare, data);
} catch (error::Error e) {
emit error(Vault::ExportImportPrepare, e.m);
}
}
Q_INVOKABLE void eiExecute()
{
if (!m_transfer) {
emit error(Vault::ExportImportExecute
, map({{"message", "exportImportPrepare was not called"}
, {"reason", "Logic"}}));
return;
}
try {
m_transfer->execute([this](QVariantMap &&map) {
emit progress(Vault::ExportImportExecute, map);
});
emit done(Vault::ExportImportExecute, QVariantMap());
} catch (error::Error e) {
emit error(Vault::ExportImportExecute, e.m);
}
}
Q_INVOKABLE void rmSnapshot(const QString &name)
{
debug::debug("Requesting snapshot removal:", name);
for (vault::Snapshot ss: m_vault->snapshots()) {
if (ss.name() == name) {
ss.remove();
break;
}
}
if (snapshots().size() == 0) {
// TODO This action should be done only until proper
// garbage collection will be introduced
debug::warning("Last snapshot is removed, destroy storage to conserve space");
QString root = m_vault->root();
destroy();
try {
init(root);
} catch (error::Error e) {
debug::error("Error reconnecting", e.what());
emit error(Vault::RemoveSnapshot, e.m);
}
} else {
debug::debug("There are some snapshots, continue");
}
emit done(Vault::RemoveSnapshot, QVariantMap());
}
QVariantMap units()
{
QVariantMap res;
for (auto &u: m_vault->config().units())
res.insert(u.name(), u.data());
return res;
}
Q_INVOKABLE void requestData(Vault::DataType dataType
, QVariantMap const &context)
{
try {
if (dataType == Vault::SnapshotUnits) {
auto snapshotName = str(context["snapshot"]);
auto reply = context;
auto unitNames = m_vault->units(snapshotName).toSet();
auto allUnits = units();
for (auto it = allUnits.begin(); it != allUnits.end(); ++it) {
auto info = it.value().toMap();
info["snapshot"] = unitNames.contains(it.key()) ? snapshotName : "";
it.value() = info;
}
reply["units"] = allUnits;
emit data(dataType, reply);
}
} catch (error::Error const &e) {
emit error(Vault::Data, e.m);
} catch (...) {
emit error(Vault::Data, map({{"Exception", "unknown"}}));
}
}
Q_INVOKABLE void exportSnapshot(const QString &snapshot, const QString &dstDir)
{
auto res = m_vault->exportSnapshot(snapshot, dstDir);
int rc = std::get<0>(res);
auto data = map({{"snapshot", snapshot}, {"dst", dstDir}
, {"rc", std::get<0>(res)}, {"stdout", std::get<1>(res)}
, {"stderr", std::get<2>(res)}});
if (!rc)
emit done(Vault::ExportSnapshot, data);
else
emit error(Vault::ExportSnapshot, data);
}
signals:
void progress(Vault::Operation op, const QVariantMap &map);
void error(Vault::Operation op, const QVariantMap &error);
void done(Vault::Operation op, const QVariantMap &);
void data(Vault::DataType id, const QVariantMap &data);
public:
vault::Vault *m_vault;
CardTransfer *m_transfer;
};
Vault::Vault(QObject *p)
: QObject(p)
, m_worker(nullptr)
, m_home(os::home())
, m_root(os::path::join(m_home, ".vault"))
{
}
Vault::~Vault()
{
m_workerThread.quit();
m_workerThread.wait();
delete m_worker;
}
QString Vault::root() const
{
return m_root;
}
QString Vault::backupHome() const
{
return m_home;
}
void Vault::setRoot(const QString &root)
{
if (m_root != root) {
m_root = root;
emit rootChanged();
}
}
void Vault::setBackupHome(const QString &home)
{
if (m_home != home) {
m_home = home;
emit backupHomeChanged();
}
}
void Vault::initWorker(bool reload)
{
debug::info((reload ? "re" : ""), "connect to vault");
debug::debug("Vault in", m_root, ", Home is", m_home);
debug::info("Init vault actor, exist", m_worker, "reload=", reload);
if (!m_worker) {
m_worker = new Worker;
m_worker->moveToThread(&m_workerThread);
m_workerThread.start();
reload = false;
connect(m_worker, &Worker::progress, this, &Vault::progress);
connect(m_worker, &Worker::error, this, &Vault::error);
connect(m_worker, &Worker::done, this, &Vault::done);
connect(m_worker, &Worker::data, this, &Vault::data);
}
try {
m_worker->init(m_root);
emit done(Vault::Connect, QVariantMap());
} catch (error::Error e) {
emit error(Vault::Connect, e.m);
}
}
void Vault::connectVault(bool reconnect)
{
if (m_worker && !reconnect) {
emit done(Vault::Connect, QVariantMap());
return;
}
initWorker(reconnect);
}
void Vault::startRestore(const QString &snapshot, const QStringList &units)
{
if (units.isEmpty()) {
debug::info("Nothing to restore");
emit done(Vault::Restore, QVariantMap());
return;
}
QMetaObject::invokeMethod(m_worker, "restore", Q_ARG(QString, m_home), Q_ARG(QStringList, units), Q_ARG(QString, snapshot));
}
void Vault::startBackup(const QString &message, const QStringList &units)
{
if (units.isEmpty()) {
debug::info("Nothing to backup");
emit done(Vault::Backup, QVariantMap());
return;
}
QMetaObject::invokeMethod(m_worker, "backup", Q_ARG(QString, m_home), Q_ARG(QStringList, units), Q_ARG(QString, message));
}
QStringList Vault::snapshots() const
{
return m_worker->snapshots();
}
QVariantMap Vault::units() const
{
return m_worker->units();
}
void Vault::resetHead()
{
m_worker->m_vault->reset();
}
void Vault::removeSnapshot(const QString &name)
{
QMetaObject::invokeMethod(m_worker, "rmSnapshot", Q_ARG(QString, name));
}
void Vault::exportImportPrepare(ImportExportAction action, const QString &path)
{
QMetaObject::invokeMethod(m_worker, "eiPrepare", Q_ARG(Vault::ImportExportAction, action), Q_ARG(QString, path));
}
void Vault::exportImportExecute()
{
QMetaObject::invokeMethod(m_worker, "eiExecute");
}
QString Vault::notes(const QString &snapshot) const
{
return m_worker->m_vault->notes(">" + snapshot);
}
void Vault::registerUnit(const QJSValue &unit, bool global)
{
QVariantMap map = unit.toVariant().toMap();
if (global) {
vault::config::global()->set(map);
} else {
if (!m_worker) {
initWorker(false);
}
m_worker->m_vault->registerConfig(map);
}
}
void Vault::startGc()
{
if (os::system("systemctl", {"--user", "start", "vault-gc.service"}) != 0)
debug::error("Can't start vault-gc.service");
}
void Vault::requestData(DataType dataType, QVariantMap const &context)
{
QMetaObject::invokeMethod(m_worker, "requestData"
, Q_ARG(Vault::DataType, dataType)
, Q_ARG(QVariantMap, context));
}
/// reset storage to the master head, cleanup tree
void Vault::reset()
{
QMetaObject::invokeMethod(m_worker, "reset");
}
Q_INVOKABLE void Vault::backupUnit(const QString &unit)
{
QMetaObject::invokeMethod(m_worker, "backupUnit"
, Q_ARG(QString, m_home)
, Q_ARG(QString, unit));
}
Q_INVOKABLE void Vault::tagSnapshot(const QString &message)
{
QMetaObject::invokeMethod(m_worker, "tagSnapshot", Q_ARG(QString, message));
}
Q_INVOKABLE void Vault::restoreUnit(const QString &snapshot, const QString &unit)
{
QMetaObject::invokeMethod(m_worker, "restoreUnit"
, Q_ARG(QString, snapshot)
, Q_ARG(QString, m_home)
, Q_ARG(QString, unit));
}
void Vault::exportSnapshot(const QString &snapshot, const QString &dstDir)
{
QMetaObject::invokeMethod(m_worker, "exportSnapshot"
, Q_ARG(QString, snapshot)
, Q_ARG(QString, dstDir));
}
#include "vault.moc"