-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkbackupthread.cpp
executable file
·319 lines (287 loc) · 10.8 KB
/
linkbackupthread.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
#include "linkbackupthread.h"
#include "dbfileentries.h"
#include "linkbackupglobals.h"
#include <QDir>
#include <QCryptographicHash>
#include <QMessageBox>
#include <QRegularExpression>
LinkBackupThread::LinkBackupThread(QObject *parent) : QThread(parent), m_cancelRequested(false), m_currentEntries(nullptr), m_oldEntries(nullptr)
{
}
LinkBackupThread::LinkBackupThread(const BackupSet& backupSet, QObject *parent) : QThread(parent), m_cancelRequested(false), m_currentEntries(nullptr), m_oldEntries(nullptr)
{
setBackupSet(backupSet);
}
LinkBackupThread::~LinkBackupThread()
{
DBFileEntries* entries = nullptr;
setOldEntries(entries);
setCurrentEntries(entries);
}
void LinkBackupThread::setOldEntries(DBFileEntries* entries)
{
if (m_oldEntries != nullptr) {
delete m_oldEntries;
}
m_oldEntries = entries;
}
void LinkBackupThread::setCurrentEntries(DBFileEntries* entries)
{
if (m_currentEntries != nullptr)
{
delete m_currentEntries;
}
m_currentEntries = entries;
}
void LinkBackupThread::setBackupSet(const BackupSet& backupSet)
{
m_backupSet = backupSet;
}
void LinkBackupThread::run()
{
if (!::getCopyLinkUtil().setHashType(m_backupSet.getHashMethod()))
{
ERROR_MSG(QString(tr("Failed to setup hash generator.")), 0);
return;
}
::getCopyLinkUtil().resetStats();
setOldEntries(nullptr);
setCurrentEntries(new DBFileEntries());
m_cancelRequested = false;
m_previousDirRoot = newestBackDirectory(m_backupSet.getToPath());
if (m_previousDirRoot.length() > 0) {
INFO_MSG(QString(tr("Found previous backup in %1")).arg(m_previousDirRoot), 1);
QString previousDBFileEntries = findHashFileCaseInsensitive(m_previousDirRoot, m_backupSet.getHashMethod());
if (previousDBFileEntries.isEmpty())
{
WARN_MSG(QString(tr("Although a previous backup was found in %1, file entries were not found for hash method %2.")).arg(m_previousDirRoot, m_backupSet.getHashMethod()), 1);
}
else
{
setOldEntries(DBFileEntries::read(previousDBFileEntries));
INFO_MSG(QString(tr("Found %1 entries in previous backup.")).arg(numOldEntries()), 1);
}
} else {
WARN_MSG(QString(tr("No previous backup found.")), 1);
}
if (m_oldEntries == nullptr)
{
m_oldEntries = new DBFileEntries();
}
m_toDirRoot = createBackDirectory(m_backupSet.getToPath());
QDir topFromDir(m_backupSet.getFromPath());
// Name of the directory that is backed up without the path.
QString topFromDirName = topFromDir.dirName();
if (!topFromDir.exists() || topFromDirName.length() == 0) {
WARN_MSG(QString(tr("Diretcory does not exist, or no directory name in %1, aborting backup.")).arg(m_backupSet.getFromPath()), 1);
return;
}
QString canonicalPath = topFromDir.canonicalPath();
m_fromDirWithoutTopDirName = canonicalPath.left(canonicalPath.length() - topFromDirName.length());
QDir toDirLocation(m_toDirRoot);
toDirLocation.mkdir(topFromDirName);
toDirLocation.cd(topFromDirName);
DEBUG_MSG(QString(tr("toDirRoot:%1 topFromDirName:%2 m_fromDir:%3")).arg(m_toDirRoot, topFromDirName, canonicalPath), 1);
INFO_MSG(QString(tr("toDirRoot:%1 topFromDirName:%2 m_fromDir:%3")).arg(m_toDirRoot, topFromDirName, canonicalPath), 0);
processDir(topFromDir, toDirLocation);
TRACE_MSG(QString("Ready to write final hash summary %1").arg(m_toDirRoot + "/" + m_backupSet.getHashMethod() + ".txt"), 1);
m_currentEntries->write(m_toDirRoot + "/" + m_backupSet.getHashMethod() + ".txt");
INFO_MSG(QString(tr("Backup finished.")), 0);
INFO_MSG(::getCopyLinkUtil().getStats(), 0);
}
void LinkBackupThread::processDir(QDir& currentFromDir, QDir& currentToDir)
{
long numErrors = getLogger().errorCount();
if (numErrors > 1000)
{
QString errorMessage = QString("%1 errors found, quit?");
QMessageBox::StandardButton reply = QMessageBox::question(nullptr, "Test", errorMessage, QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
requestCancel();
} else {
getLogger().clearErrorCount();
}
}
TRACE_MSG(QString("Processing directory %1").arg(currentFromDir.canonicalPath()), 1);
// Process directories, then process files.
QList<QFileInfo> list = currentFromDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden | QDir::Readable);
QFileInfo info;
foreach (info, list) {
if (isCancelRequested()) {
return;
}
TRACE_MSG(QString("Found Dir to processes %1").arg(info.canonicalFilePath()), 2);
if (passes(info))
{
DEBUG_MSG(QString("Dir Passes: %1").arg(info.canonicalFilePath()), 2);
if (!currentToDir.mkdir(info.fileName())) {
ERROR_MSG(QString("Failed to create directory %1/%2").arg(currentToDir.canonicalPath(), info.fileName()), 1);
} else if (!currentToDir.cd(info.fileName())) {
ERROR_MSG(QString("Failed to cd to directory %1/%2").arg(currentToDir.canonicalPath(), info.fileName()), 1);
} else if (!currentFromDir.cd(info.fileName())) {
ERROR_MSG(QString("Failed to cd to directory %1/%2").arg(currentFromDir.canonicalPath(), info.fileName()), 1);
if (!currentToDir.cdUp()) {
ERROR_MSG("Failed to cd up a directory for the to directory", 1);
requestCancel();
return;
}
} else {
processDir(currentFromDir, currentToDir);
if (!currentToDir.cdUp()) {
ERROR_MSG("Failed to cd up a directory for the to directory", 1);
requestCancel();
return;
}
if (!currentFromDir.cdUp()) {
ERROR_MSG("Failed to cd up a directory for the from directory", 1);
requestCancel();
return;
}
}
}
else
{
DEBUG_MSG(QString("Skipping Dir: %1").arg(info.canonicalFilePath()), 2);
}
}
// Now handle files
list = currentFromDir.entryInfoList(QDir::Files | QDir::NoSymLinks | QDir::Hidden | QDir::Readable);
foreach (info, list) {
if (isCancelRequested()) {
return;
}
QString fullPathFileToRead = info.canonicalFilePath();
//TRACE_MSG(QString("Found File to test %1").arg(fullPathFileToRead), 2);
if (passes(info))
{
//TRACE_MSG(QString("File passes %1").arg(fullPathFileToRead), 2);
QFile fileToRead(fullPathFileToRead);
// Just in case someone deleted the file.
if (fileToRead.exists())
{
DBFileEntry* currentEntry = new DBFileEntry(info, m_fromDirWithoutTopDirName);
const DBFileEntry* linkEntry = m_oldEntries->findEntry(m_backupSet.getCriteria(), currentEntry, m_fromDirWithoutTopDirName);
QString pathToLinkFile = m_previousDirRoot;
// If not in the old backup, search the current backup.
if (linkEntry == nullptr)
{
linkEntry = m_currentEntries->findEntry(m_backupSet.getCriteria(), currentEntry, m_fromDirWithoutTopDirName);
pathToLinkFile = m_toDirRoot;
}
if (linkEntry == nullptr)
{
bool failedToCopy = false;
QString fullFileNameToWrite = m_toDirRoot + "/" + currentEntry->getPath();
bool needHash = (currentEntry->getHash().length() == 0);
if (needHash)
{
failedToCopy = !::getCopyLinkUtil().copyFileGenerateHash(fullPathFileToRead, fullFileNameToWrite);
if (!failedToCopy)
{
currentEntry->setHash(getCopyLinkUtil().getLastHash());
}
}
else if (!getCopyLinkUtil().copyFile(fullPathFileToRead, fullFileNameToWrite))
{
failedToCopy = true;
}
if (failedToCopy)
{
ERROR_MSG(QString(tr("EC %1")).arg(currentEntry->getPath()), 1);
}
else
{
INFO_MSG(QString(tr("C %1")).arg(currentEntry->getPath()), 1);
currentEntry->setLinkTypeCopy();
m_currentEntries->addEntry(currentEntry);
currentEntry = nullptr;
}
}
else
{
if (getCopyLinkUtil().linkFile(pathToLinkFile + "/" + linkEntry->getPath(), m_toDirRoot + "/" + currentEntry->getPath()))
{
currentEntry->setLinkTypeLink();
currentEntry->setHash(linkEntry->getHash());
m_currentEntries->addEntry(currentEntry);
INFO_MSG(QString(tr("L %1")).arg(currentEntry->getPath()), 1);
currentEntry = nullptr;
}
else
{
ERROR_MSG(QString(tr("EL %1")).arg(currentEntry->getPath()), 1);
ERROR_MSG(QString(tr("(%1)(%2)(%3)")).arg(pathToLinkFile, linkEntry->getPath(), m_toDirRoot), 1);
}
}
if (currentEntry != nullptr)
{
delete currentEntry;
currentEntry= nullptr;
}
}
}
}
TRACE_MSG(QString("Finished with directory %1").arg(currentFromDir.canonicalPath()), 1);
}
bool LinkBackupThread::passes(const QFileInfo& info) const
{
return m_backupSet.passes(info);
}
QString LinkBackupThread::newestBackDirectory(const QString& parentPath)
{
QDir dir(parentPath);
if (!dir.exists()) {
ERROR_MSG("Directory does not exist in newest Back Directory", 1);
return "";
}
// File names were created so that sorting the names places them in chronoligical order.
dir.setFilter(QDir::Dirs);
dir.setSorting(QDir::Name | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
QRegularExpression dirNameRegExp("^\\d{8}-\\d{6}$");
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if (dirNameRegExp.match(fileInfo.fileName()).hasMatch()) {
return fileInfo.canonicalFilePath();
}
}
return "";
}
QString LinkBackupThread::findHashFileCaseInsensitive(const QString& parentPath, const QString& hashName)
{
QDir dir(parentPath);
if (!dir.exists()) {
ERROR_MSG("Directory does not exist in newest Back Directory", 1);
return "";
}
dir.setFilter(QDir::Files);
QFileInfoList list = dir.entryInfoList();
QRegularExpression dirNameRegExp(QString("^%1\\.txt$").arg(hashName), QRegularExpression::PatternOptions(QRegularExpression::CaseInsensitiveOption));
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if (dirNameRegExp.match(fileInfo.fileName()).hasMatch()) {
return fileInfo.canonicalFilePath();
}
}
return "";
}
QString LinkBackupThread::createBackDirectory(const QString& parentPath)
{
QDir dir(parentPath);
if (!dir.exists()) {
ERROR_MSG("Directory does not exist in createBackDirectory", 1);
return "";
}
QString dirName = QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss");
if (dir.mkdir(dirName) && dir.cd(dirName)) {
return dir.canonicalPath();
}
return "";
}
int LinkBackupThread::numOldEntries() const {
return (m_oldEntries != nullptr) ? m_oldEntries->count() : 0;
}
void LinkBackupThread::requestCancel() {
::getCopyLinkUtil().setCancelRequested(true);
m_cancelRequested = true;
}