-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
transactionalfilesystem.cpp
494 lines (432 loc) · 16.7 KB
/
transactionalfilesystem.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
/*
* LibrePCB - Professional EDA for everyone!
* Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
* https://librepcb.org/
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "transactionalfilesystem.h"
#include "../toolbox.h"
#include "fileutils.h"
#include "sexpression.h"
#include <quazip/quazip.h>
#include <quazip/quazipdir.h>
#include <quazip/quazipfile.h>
/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {
/*******************************************************************************
* Constructors / Destructor
******************************************************************************/
TransactionalFileSystem::TransactionalFileSystem(const FilePath& filepath,
bool writable,
RestoreMode restoreMode,
QObject* parent)
: FileSystem(parent),
mFilePath(filepath),
mIsWritable(writable),
mLock(filepath),
mRestoredFromAutosave(false) {
// Load the backup if there is one (i.e. last save operation has failed).
FilePath backupFile = mFilePath.getPathTo(".backup/backup.lp");
if (backupFile.isExistingFile()) {
qDebug() << "Restoring file system from backup:" << backupFile.toNative();
loadDiff(backupFile); // can throw
}
// Lock directory if the file system is opened in R/W mode.
if (mIsWritable) {
FileUtils::makePath(mFilePath); // can throw
mLock.tryLock(); // can throw
}
// If there is an autosave backup, load it according the restore mode.
FilePath autosaveFile = mFilePath.getPathTo(".autosave/autosave.lp");
if (autosaveFile.isExistingFile()) {
bool restore = (restoreMode == RestoreMode::YES);
if (restoreMode == RestoreMode::ASK) {
QMessageBox::StandardButton btn = QMessageBox::question(
0, tr("Restore autosave backup?"),
tr("It seems that the application was crashed the last time. "
"Do you want to restore the last autosave backup?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Cancel);
switch (btn) {
case QMessageBox::Yes:
restore = true;
break;
case QMessageBox::No:
restore = false;
break;
default:
throw UserCanceled(__FILE__, __LINE__);
}
} else if (restoreMode == RestoreMode::ABORT) {
throw RuntimeError(__FILE__, __LINE__,
QString("Autosave backup detected in directory '%1'.")
.arg(mFilePath.toNative()));
}
if (restore) {
qDebug() << "Restoring file system from autosave backup:"
<< autosaveFile.toNative();
loadDiff(autosaveFile); // can throw
mRestoredFromAutosave = true;
}
}
}
TransactionalFileSystem::~TransactionalFileSystem() noexcept {
// Remove autosave directory as it is not needed in case the file system
// was gracefully closed. We only need it if the application has crashed.
// But if the file system is opened in read-only mode, or if an autosave was
// restored but not saved in the meantime, do NOT remove the autosave
// directory!
if (mIsWritable && (!mRestoredFromAutosave)) {
try {
removeDiff("autosave"); // can throw
} catch (const Exception& e) {
qWarning() << "Could not remove autosave directory:" << e.getMsg();
}
}
}
/*******************************************************************************
* Inherited from FileSystem
******************************************************************************/
FilePath TransactionalFileSystem::getAbsPath(const QString& path) const
noexcept {
return mFilePath.getPathTo(cleanPath(path));
}
QStringList TransactionalFileSystem::getDirs(const QString& path) const
noexcept {
QSet<QString> dirnames;
QString dirpath = cleanPath(path);
if (!dirpath.isEmpty()) dirpath.append("/");
// add directories from file system, if not removed
QDir dir(mFilePath.getPathTo(path).toStr());
foreach (const QString& dirname,
dir.entryList(QDir::Dirs | QDir::Hidden | QDir::NoDotAndDotDot)) {
if (!isRemoved(dirpath % dirname % "/")) {
dirnames.insert(dirname);
}
}
// add directories of new files
foreach (const QString& filepath, mModifiedFiles.keys()) {
if (filepath.startsWith(dirpath)) {
QStringList relpath = filepath.mid(dirpath.length()).split('/');
if (relpath.count() > 1) {
dirnames.insert(relpath.first());
}
}
}
return dirnames.toList();
}
QStringList TransactionalFileSystem::getFiles(const QString& path) const
noexcept {
QSet<QString> filenames;
QString dirpath = cleanPath(path);
if (!dirpath.isEmpty()) dirpath.append("/");
// add files from file system, if not removed
QDir dir(mFilePath.getPathTo(path).toStr());
foreach (const QString& filename, dir.entryList(QDir::Files | QDir::Hidden)) {
if (!isRemoved(dirpath % filename)) {
filenames.insert(filename);
}
}
// add new files
foreach (const QString& filepath, mModifiedFiles.keys()) {
if (filepath.startsWith(dirpath)) {
QStringList relpath = filepath.mid(dirpath.length()).split('/');
if (relpath.count() == 1) {
filenames.insert(relpath.first());
}
}
}
return filenames.toList();
}
bool TransactionalFileSystem::fileExists(const QString& path) const noexcept {
QString cleanedPath = cleanPath(path);
if (mModifiedFiles.contains(cleanedPath)) {
return true;
} else if (isRemoved(cleanedPath)) {
return false;
} else {
return mFilePath.getPathTo(cleanedPath).isExistingFile();
}
}
QByteArray TransactionalFileSystem::read(const QString& path) const {
QString cleanedPath = cleanPath(path);
if (mModifiedFiles.contains(cleanedPath)) {
return mModifiedFiles.value(cleanedPath);
} else if (!isRemoved(cleanedPath)) {
return FileUtils::readFile(mFilePath.getPathTo(cleanedPath)); // can throw
} else {
throw RuntimeError(__FILE__, __LINE__,
QString(tr("File '%1' does not exist."))
.arg(mFilePath.getPathTo(cleanedPath).toNative()));
}
}
void TransactionalFileSystem::write(const QString& path,
const QByteArray& content) {
QString cleanedPath = cleanPath(path);
mModifiedFiles[cleanedPath] = content;
mRemovedFiles.remove(cleanedPath);
}
void TransactionalFileSystem::removeFile(const QString& path) {
QString cleanedPath = cleanPath(path);
mModifiedFiles.remove(cleanedPath);
mRemovedFiles.insert(cleanedPath);
}
void TransactionalFileSystem::removeDirRecursively(const QString& path) {
QString dirpath = cleanPath(path);
if (!dirpath.isEmpty()) dirpath.append("/");
foreach (const QString& fp, mModifiedFiles.keys()) {
if (dirpath.isEmpty() || fp.startsWith(dirpath)) {
mModifiedFiles.remove(fp);
}
}
foreach (const QString& fp, mRemovedFiles) {
if (dirpath.isEmpty() || fp.startsWith(dirpath)) {
mRemovedFiles.remove(fp);
}
}
mRemovedDirs.insert(dirpath);
}
/*******************************************************************************
* General Methods
******************************************************************************/
void TransactionalFileSystem::loadFromZip(const FilePath& fp) {
QuaZip zip(fp.toStr());
if (!zip.open(QuaZip::mdUnzip)) {
throw RuntimeError(
__FILE__, __LINE__,
QString(tr("Failed to open the ZIP file '%1'.")).arg(fp.toNative()));
}
QuaZipFile file(&zip);
for (bool f = zip.goToFirstFile(); f; f = zip.goToNextFile()) {
file.open(QIODevice::ReadOnly);
write(file.getActualFileName(), file.readAll());
file.close();
}
zip.close();
}
void TransactionalFileSystem::exportToZip(const FilePath& fp) const {
QuaZip zip(fp.toStr());
if (!zip.open(QuaZip::mdCreate)) {
throw RuntimeError(
__FILE__, __LINE__,
QString(tr("Failed to create the ZIP file '%1'.")).arg(fp.toNative()));
}
try {
QuaZipFile file(&zip);
exportDirToZip(file, fp, "");
zip.close();
} catch (const Exception& e) {
// Remove ZIP file because it is not complete
QFile(fp.toStr()).remove();
zip.close();
throw;
}
}
void TransactionalFileSystem::discardChanges() noexcept {
mModifiedFiles.clear();
mRemovedFiles.clear();
mRemovedDirs.clear();
}
QStringList TransactionalFileSystem::checkForModifications() const {
QStringList modifications;
// removed directories
foreach (const QString& dir, mRemovedDirs) {
FilePath fp = mFilePath.getPathTo(dir);
if (fp.isExistingDir()) {
modifications.append(dir);
}
}
// removed files
foreach (const QString& filepath, mRemovedFiles) {
FilePath fp = mFilePath.getPathTo(filepath);
if (fp.isExistingFile()) {
modifications.append(filepath);
}
}
// new or modified files
foreach (const QString& filepath, mModifiedFiles.keys()) {
FilePath fp = mFilePath.getPathTo(filepath);
QByteArray content = mModifiedFiles.value(filepath);
if ((!fp.isExistingFile()) ||
(FileUtils::readFile(fp) != content)) { // can throw
modifications.append(filepath);
}
}
return modifications;
}
void TransactionalFileSystem::autosave() {
saveDiff("autosave"); // can throw
}
void TransactionalFileSystem::save() {
// save to backup directory
saveDiff("backup"); // can throw
// modifications are now saved to the backup directory, so there is no risk
// of loosing a restored autosave backup, thus we can reset its flag
mRestoredFromAutosave = false;
// remove autosave directory because it is now older than the backup content
// (the user should not be able to restore the outdated autosave backup)
removeDiff("autosave"); // can throw
// remove directories
foreach (const QString& dir, mRemovedDirs) {
FilePath fp = mFilePath.getPathTo(dir);
if (fp.isExistingDir()) {
FileUtils::removeDirRecursively(fp); // can throw
}
}
// remove files
foreach (const QString& filepath, mRemovedFiles) {
FilePath fp = mFilePath.getPathTo(filepath);
if (fp.isExistingFile()) {
FileUtils::removeFile(fp); // can throw
}
}
// save new or modified files
foreach (const QString& filepath, mModifiedFiles.keys()) {
FileUtils::writeFile(mFilePath.getPathTo(filepath),
mModifiedFiles.value(filepath)); // can throw
}
// remove backup
removeDiff("backup"); // can throw
// clear state
discardChanges();
}
/*******************************************************************************
* Static Methods
******************************************************************************/
QString TransactionalFileSystem::cleanPath(QString path) noexcept {
return path.trimmed()
.replace('\\', '/')
.split('/', QString::SkipEmptyParts)
.join('/')
.trimmed();
}
/*******************************************************************************
* Private Methods
******************************************************************************/
bool TransactionalFileSystem::isRemoved(const QString& path) const noexcept {
if (mRemovedFiles.contains(path)) {
return true;
}
foreach (const QString dir, mRemovedDirs) {
if (path.startsWith(dir)) {
return true;
}
}
return false;
}
void TransactionalFileSystem::exportDirToZip(QuaZipFile& file,
const FilePath& zipFp,
const QString& dir) const {
QString path = dir.isEmpty() ? dir : dir % "/";
// export directories
foreach (const QString& dirname, getDirs(dir)) {
// skip dotdirs, e.g. ".git", ".svn", ".autosave", ".backup"
if (dirname.startsWith('.')) continue;
exportDirToZip(file, zipFp, path % dirname);
}
// export files
foreach (const QString& filename, getFiles(dir)) {
QString filepath = path % filename;
if (filepath == zipFp.toRelative(mFilePath)) {
// In case the exported ZIP file is located inside this file system,
// we have to skip it. Otherwise we would get a ZIP inside the ZIP file.
continue;
}
// skip lock file
if (filename == ".lock") continue;
// read file content and add it to the ZIP archive
const QByteArray& content = read(filepath); // can throw
QuaZipNewInfo newFileInfo(filepath);
newFileInfo.setPermissions(QFileDevice::ReadOwner | QFileDevice::ReadGroup |
QFileDevice::ReadOther |
QFileDevice::WriteOwner);
if (!file.open(QIODevice::WriteOnly, newFileInfo)) {
throw RuntimeError(__FILE__, __LINE__);
}
qint64 bytesWritten = file.write(content);
file.close();
if (bytesWritten != content.length()) {
throw RuntimeError(__FILE__, __LINE__,
QString(tr("Failed to write file '%1' to '%2'."))
.arg(filepath, zipFp.toNative()));
}
}
}
void TransactionalFileSystem::saveDiff(const QString& type) const {
QDateTime dt = QDateTime::currentDateTime();
FilePath dir = mFilePath.getPathTo("." % type);
FilePath filesDir = dir.getPathTo(dt.toString("yyyy-MM-dd_hh-mm-ss-zzz"));
if (!mIsWritable) {
throw RuntimeError(__FILE__, __LINE__, tr("File system is read-only."));
}
SExpression root = SExpression::createList("librepcb_" % type);
root.appendChild("created", dt, true);
root.appendChild("modified_files_directory", filesDir.getFilename(), true);
foreach (const QString& filepath, Toolbox::sorted(mModifiedFiles.keys())) {
root.appendChild("modified_file", filepath, true);
FileUtils::writeFile(filesDir.getPathTo(filepath),
mModifiedFiles.value(filepath)); // can throw
}
foreach (const QString& filepath, Toolbox::sorted(mRemovedFiles.toList())) {
root.appendChild("removed_file", filepath, true);
}
foreach (const QString& filepath, Toolbox::sorted(mRemovedDirs.toList())) {
root.appendChild("removed_directory", filepath, true);
}
// Writing the main file must be the last operation to "mark" this diff as
// complete!
FileUtils::writeFile(dir.getPathTo(type % ".lp"),
root.toByteArray()); // can throw
}
void TransactionalFileSystem::loadDiff(const FilePath& fp) {
discardChanges(); // get a clean state first
SExpression root =
SExpression::parse(FileUtils::readFile(fp), fp); // can throw
QString modifiedFilesDirName =
root.getValueByPath<QString>("modified_files_directory", true);
FilePath modifiedFilesDir = fp.getParentDir().getPathTo(modifiedFilesDirName);
foreach (const SExpression& node, root.getChildren("modified_file")) {
QString relPath = node.getValueOfFirstChild<QString>(true);
FilePath absPath = modifiedFilesDir.getPathTo(relPath);
mModifiedFiles.insert(relPath, FileUtils::readFile(absPath)); // can throw
}
foreach (const SExpression& node, root.getChildren("removed_file")) {
QString relPath = node.getValueOfFirstChild<QString>(true);
mRemovedFiles.insert(relPath);
}
foreach (const SExpression& node, root.getChildren("removed_directory")) {
QString relPath = node.getValueOfFirstChild<QString>(true);
mRemovedDirs.insert(relPath);
}
}
void TransactionalFileSystem::removeDiff(const QString& type) {
FilePath dir = mFilePath.getPathTo("." % type);
FilePath file = dir.getPathTo(type % ".lp");
// remove the index file first to mark the diff directory as incomplete
if (file.isExistingFile()) {
FileUtils::removeFile(file); // can throw
}
// then remove the whole directory
FileUtils::removeDirRecursively(dir); // can throw
}
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace librepcb