/
unit.cpp
580 lines (503 loc) · 18.5 KB
/
unit.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/**
* @file unit.hpp
* @brief Vault unit option parsing and file export/import support
* @author Denis Zalevskiy <denis.zalevskiy@jolla.com>
* @copyright (C) 2014 Jolla Ltd.
* @par License: LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
*/
#include "unit.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <memory>
#include <functional>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QString>
#include <QProcess>
#include <QDebug>
#include <QLoggingCategory>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDateTime>
Q_LOGGING_CATEGORY(lcBackup, "org.sailfishos.backup", QtWarningMsg)
namespace vault { namespace unit {
static const unsigned current_version = 1;
static const QString config_prefix = ".f8b52b7481393a3e6ade051ecfb549fa";
namespace {
const QList <QCommandLineOption> options_info =
{QCommandLineOption({"d", "dir"}, "dir", "undef"),
QCommandLineOption({"b", "bin-dir"}, "bin-dir", "undef"),
QCommandLineOption({"H", "home-dir"}, "home", "undef"),
QCommandLineOption({"n", "name"}, "appname", "undef"),
QCommandLineOption({"a", "action"}, "action", "undef")};
// QCommandLineParser can't handle whitespaces, thus do preprocessing.
QStringList parseArgs()
{
auto args = QCoreApplication::arguments();
QStringList argsWithSpace = {args[0]};
QString withSpace;
QString argname;
for (int i = 1; i < args.size(); ++i) {
QString arg = args[i];
if (arg.at(0) == '-') {
if (!withSpace.isEmpty()) {
argsWithSpace << argname << withSpace.remove(withSpace.size()-1, 1);
withSpace.clear();
}
argname = arg;
} else if (i == args.size()-1) {
withSpace += arg;
argsWithSpace << argname << withSpace;
} else {
withSpace += arg + " ";
}
}
return argsWithSpace;
}
// cp -L -p -f
void copy(QString const &src, QString dst)
{
bool failure = false;
if (QFileInfo(dst).isDir())
dst += QDir::separator() + QFileInfo(src).fileName();
if (QFile(dst).exists())
failure |= !QFile::remove(dst);
struct stat statbuf;
failure |= stat(src.toStdString().c_str(), &statbuf);
const struct utimbuf timestamp = {statbuf.st_atime, statbuf.st_mtime};
if (QFile::copy(src, dst)) {
QFile(dst).setPermissions(QFile(src).permissions());
failure |= chown(dst.toStdString().c_str(), statbuf.st_uid, statbuf.st_gid);
failure |= utime(dst.toStdString().c_str(), ×tamp);
} else {
failure = true;
}
if (failure)
qCWarning(lcBackup) << "Copy failed:" << src << dst;
}
void cptree(QString const &src, QString dst, bool update=false, bool first=true)
{
if (first && QFileInfo(src).baseName() != QFileInfo(dst).baseName())
dst += "/" + QFileInfo(src).baseName();
QDir dir(src);
if (!dir.exists())
return;
QDir().mkpath(dst);
for (auto &d : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
QString dir = QDir::separator() + d;
cptree(src+dir, dst+dir, update, false);
}
for (auto &f : dir.entryList(QDir::Files)) {
QString file = QDir::separator() + f;
if ((!update || !QFileInfo(dst+file).exists()) ||
(QFileInfo(src+file).lastModified() >= QFileInfo(dst+file).lastModified())) {
copy(src+file, dst+file);
}
}
}
void update_tree(QString const &src, QString const &dst)
{
cptree(src, dst, true);
}
typedef QVariantMap map_type;
typedef QList<QVariantMap> list_type;
class Version {
public:
Version(QString const &root)
: fname(QFileInfo(root + "/" + config_prefix + ".unit.version").absoluteFilePath())
{}
unsigned get()
{
QFile f(fname);
if (!f.open(QIODevice::ReadOnly))
qCWarning(lcBackup) << "Can't open file" << fname;
return f.exists()
? QString(f.readAll()).toInt()
: 0;
}
void save()
{
QFile f(fname);
if (f.open(QIODevice::WriteOnly))
f.write(std::to_string(current_version).c_str());
else
qCWarning(lcBackup) << "Can't open file" << fname;
}
private:
QString fname;
};
class Operation
{
public:
Operation(map_type const &c) : context(c)
{
parser.addOptions(options_info);
parser.process(parseArgs());
vault_dir = {{"bin", parser.value("bin-dir")}, {"data", parser.value("dir")}};
home = QFileInfo(parser.value("home-dir")).canonicalFilePath();
}
void execute();
private:
typedef std::function<void (QString const &, list_type &
, map_type const &)> action_type;
class Links
{
public:
Links(map_type &&d, QString const &root)
: data(std::move(d))
, root_dir(root)
{}
void add(map_type const &info) {
map_type value = {{ "target", info["target"]}
, {"target_path", info["target_path"]}};
data.insert(info["path"].toString(), std::move(value));
}
void save() {
if (!data.empty())
write_links(data, root_dir);
}
map_type get(map_type const &info) {
return data[info["path"].toString()].toMap();
}
map_type data;
QString root_dir;
};
void to_vault(QString const &data_type
, list_type &paths
, map_type const &location);
void from_vault(QString const &data_type
, list_type &paths
, map_type const &location);
static map_type read_links(QString const &root_dir) {
QFile f(get_link_info_fname(root_dir));
if (!f.open(QFile::ReadOnly)) {
qCWarning(lcBackup) << "Unable to open" << f.fileName();
return map_type();
}
auto data = f.readAll();
return QJsonDocument::fromJson(data).object().toVariantMap();
}
static size_t write_links(map_type const &links, QString const &root_dir)
{
QFile f(get_link_info_fname(root_dir));
if (!f.open(QFile::WriteOnly)) {
qCWarning(lcBackup) << "Can't write to" << f.fileName();
return 0;
}
return f.write(QJsonDocument::fromVariant(links).toJson());
}
static QString get_link_info_fname(QString const &root)
{
return QFileInfo(root + "/" + config_prefix + ".links").absoluteFilePath();
}
Version version(QString const &root)
{
return Version(root);
}
QString get_root_vault_dir(QString const &data_type);
QCommandLineParser parser;
map_type const &context;
map_type vault_dir;
QString home;
};
void create_dst_dirs(map_type const &item)
{
QString path = item["full_path"].toString();
if (!QFileInfo(item["src"].toString()).isDir())
path = QFileInfo(path).dir().absolutePath();
if (!QFileInfo(path).isDir()) {
if (!QDir().mkpath(path) && item["required"].toBool())
throw std::runtime_error("Can't create path " + path.toStdString());
}
}
QString Operation::get_root_vault_dir(QString const &data_type)
{
QString res = vault_dir[data_type].toString();
if (!QFileInfo(res).isDir())
throw std::runtime_error("Vault dir doesn't exist: " + res.toStdString()
+ " or unknown datatype: " + data_type.toStdString());
return res;
}
void Operation::to_vault(QString const &data_type
, list_type &paths
, map_type const &location)
{
qCDebug(lcBackup) << "To vault" << data_type << "Paths"
<< paths << "Location" << location;
QString dst_root = get_root_vault_dir(data_type);
QString link_info_path = get_link_info_fname(dst_root);
Links links(read_links(dst_root), dst_root);
auto copy_entry = [dst_root](map_type const &info) {
qCDebug(lcBackup) << "COPY" << info;
QString dst = QFileInfo(dst_root + "/" + info["path"].toString()).absolutePath();
QString src = info["full_path"].toString();
if (!QFileInfo(dst).isDir() && !QDir().mkpath(dst)) {
throw std::runtime_error("Can't create destination in vault: "
+ dst.toStdString());
}
if (QFileInfo(src).isDir()) {
update_tree(src, dst);
} else if (QFileInfo(src).isFile()) {
copy(src, dst);
} else {
throw std::runtime_error("No handler for this entry type"
+ src.toStdString());
}
};
auto process_symlink = [this, &links](map_type &path) {
QString full_path = QFileInfo(path["full_path"].toString()).absoluteFilePath();
if (!QFileInfo(full_path).isSymLink())
return;
QString tgt = QFileInfo(full_path).symLinkTarget();
full_path = QFileInfo(full_path).canonicalFilePath();
QString root_path = QFileInfo(path["root_path"].toString()).canonicalFilePath();
QString tgt_path = QDir(home).relativeFilePath(full_path);
qCDebug(lcBackup) << "Process symlink" << path;
if (!full_path.contains(QRegExp("^" + root_path))) {
if (path["required"].toBool())
throw std::runtime_error("Required path does not belong to its root dir: "
+ full_path.toStdString());
path["skip"] = true;
return;
}
map_type link_info(path);
link_info.unite(path);
link_info["target"] = tgt;
link_info["target_path"] = tgt_path;
qCDebug(lcBackup) << "Symlink info" << link_info;
links.add(link_info);
path["full_path"] = full_path;
path["path"] = tgt_path;
};
auto is_src_exists = [](map_type const &info) {
auto res = true;
auto full_path = info["full_path"].toString();
if (info["skip"].toBool()) {
res = false;
} else if (!QFileInfo(full_path).exists()) {
if (info["required"].toBool())
throw std::runtime_error("Required path does not exist: " +
full_path.toStdString());
res = false;
}
if (!res)
qCDebug(lcBackup) << "Does not exist/skip" << info;
return res;
};
std::for_each(paths.begin(), paths.end(), process_symlink);
list_type existing_paths;
for (auto const &path : paths) {
if (is_src_exists(path))
existing_paths.push_back(path);
}
std::for_each(existing_paths.begin(), existing_paths.end(), copy_entry);
links.save();
version(dst_root).save();
}
void Operation::from_vault(QString const &data_type
, list_type &items
, map_type const &location)
{
qCDebug(lcBackup) << "From vault" << data_type << "Paths" << items << "Location" << location;
QString src_root(get_root_vault_dir(data_type));
bool overwrite_default;
{
auto v = location["options"].toMap()["overwrite"];
if (!v.isValid())
v = context["options"].toMap()["overwrite"];
overwrite_default = v.toBool();
}
Links links(read_links(src_root), src_root);
auto fallback_v0 = [src_root, &items]() {
qCDebug(lcBackup) << "Restoring from old unit version";
if (items.empty())
throw std::runtime_error("There should be at least 1 item");
// during migration from initial format old (and single)
// item is going first
QString dst = QFileInfo(items[0]["full_path"].toString()).absoluteFilePath();
if (!QFileInfo(dst).isDir() && !QDir().mkpath(dst))
throw std::runtime_error("Can't create directory" + dst.toStdString());
update_tree(src_root, dst);
};
auto process_absent_and_links = [src_root, &links](map_type &item) {
QString item_path = item["path"].toString();
QString src = QFileInfo(src_root + "/" + item_path).absoluteFilePath();
if (QFileInfo(src).exists()) {
item["src"] = src;
return map_type();
}
map_type link = links.get(item);
item["skip"] = true;
auto create_link = [](map_type const &link, map_type const &item) {
create_dst_dirs(item);
QFile::link(link["target"].toString(), item["full_path"].toString());
};
if (link.empty()) {
qCDebug(lcBackup) << "No symlink for" << item_path;
if (item["required"].toBool()) {
qCCritical(lcBackup) << "No source item:" << src << link["path"];
throw std::runtime_error("No required source item");
}
return map_type();
}
qCDebug(lcBackup) << "There is a symlink for" << item_path;
QVariantMap linked(item);
QString target_path = link["target_path"].toString();
linked["path"] = target_path;
linked["full_path"] = QFileInfo(item["root_path"].toString() + "/" + target_path).absoluteFilePath();
src = QFileInfo(src_root + "/" + target_path).absoluteFilePath();
if (QFileInfo(src).exists()) {
linked["src"] = src;
linked["skip"] = false;
create_link(link, item);
qCDebug(lcBackup) << "Symlink target path is" << linked;
return linked;
} else if (item["required"].toBool()) {
qCCritical(lcBackup) << "No linked source item:" << "path" << src
<< "link" << link["path"] << "target" << linked["path"];
throw std::runtime_error("No linked source item");
}
return map_type();
};
unsigned v = version(src_root).get();
if (v > current_version) {
throw std::runtime_error("Can't restore from newer unit version. Expected:"
+ std::to_string(current_version) + " got: " + std::to_string(v));
} else if (v < current_version) {
return fallback_v0();
}
list_type linked_items;
for (auto &item : items) {
map_type linked = process_absent_and_links(item);
if (!linked.empty())
linked_items.push_back(std::move(linked));
}
items.append(linked_items);
qCDebug(lcBackup) << "LINKED+" << items;
for (auto const &item : items) {
if (item["skip"].toBool()) {
qCDebug(lcBackup) << "Skipping" << item["path"].toString();
continue;
}
bool overwrite;
{
auto v = item["overwrite"];
overwrite = v.isValid() ? v.toBool() : overwrite_default;
}
// TODO process correctly self dir (copy with dir itself)
create_dst_dirs(item);
QString src = item["src"].toString();
QString dst = item["full_path"].toString();
QString dst_dir = QFileInfo(dst).path();
if (QFileInfo(src).isDir()) {
src = QFileInfo(src).canonicalFilePath();
cptree(src, dst_dir);
} else if (QFileInfo(src).isFile()) {
if (overwrite)
unlink(dst.toStdString().c_str());
copy(src, dst_dir);
} else {
qCWarning(lcBackup) << "No operation done or file found:" << src;
}
}
}
void Operation::execute()
{
qCDebug(lcBackup) << "Unit execute. Context:" << context;
action_type action;
if (!QFileInfo(home).isDir())
throw std::runtime_error("Home dir doesn't exist" + home.toStdString());
QString action_name = parser.value("action");
using namespace std::placeholders;
if (action_name == "export") {
action = std::bind(&Operation::to_vault, this, _1, _2, _3);
} else if (action_name == "import") {
action = std::bind(&Operation::from_vault, this, _1, _2, _3);
} else {
throw std::runtime_error("Unknown action" + action_name.toStdString());
}
auto get_home_path = [this](QVariant const &item) {
map_type res;
if (static_cast<QMetaType::Type>(item.type()) == QMetaType::QString) {
res["path"] = item.toString();
} else if (static_cast<QMetaType::Type>(item.type()) == QMetaType::QVariantMap) {
res.unite(item.toMap());
} else {
throw std::runtime_error("Unexpected path type" +
item.toString().toStdString());
}
QString path = res["path"].toString();
if (path.isEmpty())
throw std::runtime_error("Invalid data(path)" +
item.toString().toStdString());
res["full_path"] = QFileInfo(home + "/" + path).absoluteFilePath();
res["root_path"] = home;
return res;
};
auto process_home_path = [&action, get_home_path](map_type const &location) {
auto create_paths_list = [get_home_path](auto const &items) {
list_type l;
for (auto const &i : items)
l.push_back(get_home_path(i));
return l;
};
for (auto it = location.begin(); it != location.end(); ++it) {
auto const &name = it.key();
auto const &items = it.value();
if (name == "options")
continue; // skip options
auto data_type = name;
list_type paths = create_paths_list(items.toList());
action(data_type, paths, location);
};
};
for (auto it = context.begin(); it != context.end(); ++it) {
auto const &name = it.key();
auto const &value = it.value();
if (name == "options")
continue; // skip options
if (name == "home") {
process_home_path(value.toMap());
} else {
throw std::runtime_error("Unknown context item" + name.toStdString());
}
};
}
} // namespace
int execute(QVariantMap const &info)
{
try {
Operation op(info);
op.execute();
} catch (std::exception const &e) {
qCCritical(lcBackup) << e.what();
return 1;
}
return 0;
}
int runProcess(const QString &program, const QStringList &args)
{
QProcess ps;
ps.start(program, args);
ps.waitForFinished(-1);
if (ps.exitStatus() == QProcess::CrashExit || ps.exitCode() != 0) {
qCWarning(lcBackup) << ps.program() << args << "failed";
qCWarning(lcBackup) << ps.readAllStandardError();
qCWarning(lcBackup) << ps.readAllStandardOutput();
return 1;
}
return 0;
}
QString optValue(const QString &arg)
{
QCommandLineParser p;
p.addOptions(options_info);
p.process(parseArgs());
return p.value(arg);
}
}} // namespace vault::unit