-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle.cpp
More file actions
362 lines (312 loc) · 11.7 KB
/
Copy pathhandle.cpp
File metadata and controls
362 lines (312 loc) · 11.7 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
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
// Copyright blackgiulia.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
#include "handle.hpp"
handle::handle(QObject *parent)
: QObject(parent), statusMsg(""), targetDir(""), sourceDir(""),
encryptKey(""), signKey(""),
// TODO: add more backends
backend("pydrive+gdocs://developer.gserviceaccount.com/"), passphrase(""),
signPassphrase(""), p_duplicity("/") {}
void handle::updateHandleFromQML(const QString &targetDir_,
const QString &sourceDir_,
const QString &encryptKey_,
const QString &signKey_,
const QString &passphrase_,
const QString &signPassphrase_,
const QString &dir_) {
if (dir_ == QString("Select directory:")) { // Update from QML
targetDir = targetDir_.toStdString();
// Convert /path/to/dir to path/to/dir
if (targetDir[0] == '/') {
targetDir = targetDir.substr(1);
}
sourceDir = sourceDir_.toStdString().substr(7);
encryptKey = encryptKey_.toStdString();
signKey = signKey_.toStdString();
passphrase = passphrase_.toStdString();
signPassphrase = signPassphrase_.toStdString();
} else { // Update from handle.json
std::string d = dir_.toStdString();
auto config_path = boost::filesystem::current_path();
config_path += boost::filesystem::path("/handle.json");
if (boost::filesystem::exists(config_path)) {
auto root = readFromJson(config_path);
for (auto &i : root.get_child("pydrive")) {
if (i.second.get<std::string>("sourceDir") == d) {
targetDir = i.second.get<std::string>("targetDir");
sourceDir = i.second.get<std::string>("sourceDir");
encryptKey = i.second.get<std::string>("encryptKey");
signKey = i.second.get<std::string>("signKey");
passphrase = decode64(i.second.get<std::string>("passphrase"));
signPassphrase =
decode64(i.second.get<std::string>("signPassphrase"));
break;
}
}
}
}
return;
}
void handle::performRestore() const {
// Do restore
try {
boost::process::child c(
p_duplicity, "restore", "--encrypt-key", encryptKey, "--sign-key",
signKey, backend + targetDir, sourceDir,
boost::process::std_out > boost::process::null,
boost::process::env["PASSPHRASE"] = passphrase,
boost::process::env["SIGN_PASSPHRASE"] = signPassphrase);
c.wait();
} catch (std::system_error &err) {
updateStatusText(getTime() + " - restore failed. Make sure duplicity, "
"PyDrive and GPG are installed");
}
updateStatusText(getTime() + " - " + QString::fromStdString(targetDir) +
" has been restored to " +
QString::fromStdString(sourceDir));
return;
}
// TODO: Asynchronous backup process for different folders feature
void handle::performBackup(const bool &isFull) const {
std::string argv;
if (isFull) {
argv = "full";
} else {
argv = "incr";
}
boost::process::ipstream is;
// Max 1,677,216 TB
uint64_t backupSize = 0;
// Do backup
try {
boost::process::child c1(
p_duplicity, argv, "--encrypt-key", encryptKey, "--sign-key", signKey,
"--gpg-options", "--cipher-algo=AES256", "--allow-source-mismatch",
sourceDir, backend + targetDir, boost::process::std_out > is,
boost::process::env["PASSPHRASE"] = passphrase,
boost::process::env["SIGN_PASSPHRASE"] = signPassphrase);
c1.wait();
} catch (std::system_error &err) {
updateStatusText(
getTime() +
" - backup failed. Make sure duplicity, PyDrive and GPG are installed");
}
std::string line;
while (is && std::getline(is, line)) {
if (line.empty()) {
continue;
}
std::vector<std::string> tokens;
std::stringstream ss(line);
std::string s;
while (getline(ss, s, ' ')) {
tokens.push_back(s);
}
if (tokens[0] == "TotalDestinationSizeChange") {
std::stringstream sz(tokens[1]);
sz >> backupSize;
break;
}
}
updateStatusText(getTime() + " - " + QString("new ") +
QString::fromStdString(isFull ? "full" : "incremental") +
QString(" backup size is ") +
QString::fromStdString(std::to_string(backupSize)) +
" bytes");
// In case backup files are brocken
boost::process::child c2(
p_duplicity, "cleanup", "--force", backend + targetDir,
boost::process::std_out > boost::process::null,
boost::process::env["PASSPHRASE"] = passphrase,
boost::process::env["SIGN_PASSPHRASE"] = signPassphrase);
c2.wait();
// Remove previous backup
if (argv == "full") {
boost::process::child c3(p_duplicity, "remove-all-but-n-full", "1",
"--force", backend + targetDir,
boost::process::std_out > boost::process::null);
c3.wait();
updateStatusText(getTime() + " - " +
QString("previous full backup is deleted"));
}
boost::posix_time::ptime t(boost::posix_time::second_clock::local_time());
auto s_time = boost::posix_time::to_iso_extended_string(t);
auto config_path = boost::filesystem::current_path();
config_path += boost::filesystem::path("/handle.json");
if (boost::filesystem::exists(config_path)) {
if (!isFull) { // Update incremental backup status to handle.json
auto root = readFromJson(config_path);
for (auto &i : root.get_child("pydrive")) {
if (i.second.get<std::string>("targetDir") == targetDir) {
i.second.put("lastIncrDate", s_time);
auto last_incr_size = i.second.get<uint64_t>("totalIncrSize");
i.second.put("totalIncrSize", backupSize + last_incr_size);
break;
}
}
writeToJson(root, config_path);
return;
} else { // Update full backup status to handle.json
auto root = readFromJson(config_path);
for (auto &i : root.get_child("pydrive")) {
// Modify corresponding status
if (i.second.get<std::string>("targetDir") == targetDir) {
i.second.put("lastFullDate", s_time);
i.second.put("lastFullSize", backupSize);
i.second.put("lastIncrDate", "none");
i.second.put("totalIncrSize", 0);
writeToJson(root, config_path);
return;
}
}
// Add a new status
auto r = writeToPT();
r.put("lastFullDate", s_time);
r.put("lastFullSize", backupSize);
r.put("lastIncrDate", "none");
r.put("totalIncrSize", 0);
r.put("passphrase", encode64(r.get<std::string>("passphrase")));
r.put("signPassphrase", encode64(r.get<std::string>("signPassphrase")));
root.get_child("pydrive").push_back(make_pair("", r));
writeToJson(root, config_path);
return;
}
} else { // Create new handle.json
auto r = writeToPT();
r.put("lastFullDate", s_time);
r.put("lastFullSize", backupSize);
r.put("lastIncrDate", "none");
r.put("totalIncrSize", 0);
r.put("passphrase", encode64(r.get<std::string>("passphrase")));
r.put("signPassphrase", encode64(r.get<std::string>("signPassphrase")));
boost::property_tree::ptree root;
root.put("pydrive", "");
root.get_child("pydrive").push_back(make_pair("", r));
writeToJson(root, config_path);
return;
}
return;
}
void handle::showLastStatus(const QString sourceDir_) const {
std::string d = sourceDir_.toStdString();
auto config_path = boost::filesystem::current_path();
config_path += boost::filesystem::path("/handle.json");
if (boost::filesystem::exists(config_path)) {
auto root = readFromJson(config_path);
for (auto &i : root.get_child("pydrive")) {
if (i.second.get<std::string>("sourceDir") == d) {
updateStatusText(
QString(" last full backup date is ") +
QString::fromStdString(i.second.get<std::string>("lastFullDate")));
updateStatusText(
QString(" last full backup size is ") +
QString::fromStdString(i.second.get<std::string>("lastFullSize")) +
QString(" bytes"));
updateStatusText(
QString(" last incremental backup date is ") +
QString::fromStdString(i.second.get<std::string>("lastIncrDate")));
updateStatusText(
QString(" total incremental backup size is ") +
QString::fromStdString(i.second.get<std::string>("totalIncrSize")) +
QString(" bytes"));
updateStatusText(sourceDir_ + " last status:");
break;
}
}
}
return;
}
void handle::getDup() {
p_duplicity = boost::process::search_path("duplicity");
return;
}
std::vector<std::pair<std::string, std::string>> handle::get_keys() const {
std::vector<std::pair<std::string, std::string>> keys;
auto p_gpg = boost::process::search_path("gpg");
boost::process::ipstream is;
try {
boost::process::child c1(p_gpg, "--list-keys",
boost::process::std_out > is);
c1.wait();
} catch (std::system_error &err) {
updateStatusText(getTime() + " - GPG keys are not detected");
}
std::string line, uid_, key_;
while (is && std::getline(is, line)) {
if (line.empty()) {
continue;
}
std::vector<std::string> tokens;
std::stringstream ss(line);
std::string s;
while (getline(ss, s, ' ')) {
if (!s.empty()) {
tokens.push_back(s);
}
}
// Get GPG keys, show only uid to UI
if (tokens.size() == 1) {
key_ = tokens[0];
}
if (tokens[0] == "uid") {
uid_ = "uid";
for_each(tokens.begin() + 1, tokens.end(),
[&](auto &i) { uid_ += (" " + i); });
keys.emplace_back(uid_, key_);
}
}
return keys;
}
boost::property_tree::ptree handle::writeToPT() const {
boost::property_tree::ptree root;
root.put("targetDir", targetDir);
root.put("sourceDir", sourceDir);
root.put("encryptKey", encryptKey);
root.put("signKey", signKey);
root.put("backend", backend);
root.put("passphrase", passphrase);
root.put("signPassphrase", signPassphrase);
return root;
}
boost::property_tree::ptree
readFromJson(const boost::filesystem::path &config_path) {
boost::property_tree::ptree root;
boost::filesystem::ifstream file;
file.open(config_path, boost::filesystem::ifstream::in);
boost::property_tree::read_json(file, root);
file.close();
return root;
}
void writeToJson(const boost::property_tree::ptree &root,
const boost::filesystem::path &config_path) {
boost::filesystem::ofstream file;
if (!boost::filesystem::exists(config_path)) {
file.open(config_path, boost::filesystem::ofstream::out);
} else {
file.open(config_path, boost::filesystem::ofstream::out |
boost::filesystem::ofstream::trunc);
}
boost::property_tree::write_json(file, root);
file.close();
return;
}
QString getTime() noexcept {
boost::posix_time::ptime t(boost::posix_time::second_clock::local_time());
auto s = boost::posix_time::to_iso_extended_string(t).substr(11, 8);
return QString::fromStdString(s);
}
std::string encode64(const std::string &pass) noexcept {
std::string res;
QByteArray ba(pass.c_str(), pass.size());
res = ba.toBase64(QByteArray::Base64UrlEncoding).toStdString();
return res;
}
std::string decode64(const std::string &encoded) noexcept {
std::string decoded;
QByteArray ba(encoded.c_str(), encoded.size());
decoded =
QByteArray::fromBase64(ba, QByteArray::Base64UrlEncoding).toStdString();
return decoded;
}