| 1 | /* ---------------------------------- calcDiffThread.cpp --------------------------------------------------------------------------- |
| 2 | Class to calculate the differences between source & snapshot in a seperate thread |
| 3 | |
| 4 | =============================================================================================================================== |
| 5 | =============================================================================================================================== |
| 6 | This file is part of "luckyBackup" project |
| 7 | Copyright 2008-2012, Loukas Avgeriou |
| 8 | luckyBackup is distributed under the terms of the GNU General Public License |
| 9 | luckyBackup is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | luckyBackup is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with luckyBackup. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | project version : Please see "main.cpp" for project version |
| 24 | |
| 25 | developer : luckyb |
| 26 | last modified : 22 May 2016 |
| 27 | =============================================================================================================================== |
| 28 | =============================================================================================================================== |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "calcDiffThread.h" |
| 33 | |
| 34 | #include <QStack> |
| 35 | #include <QTextStream> |
| 36 | |
| 37 | #include "global.h" |
| 38 | #include "operationClass.h" |
| 39 | |
| 40 | // class CalcDiffThread Constructor================================================================================================= |
| 41 | CalcDiffThread::CalcDiffThread(QObject *parent) : QThread(parent) |
| 42 | { |
| 43 | //initialize variables |
| 44 | returnString = ""; |
| 45 | dirToReadString = ""; |
| 46 | dest = ""; |
| 47 | CurrentSnapshotDirectory = ""; |
| 48 | CurrentSnapshotString = ""; |
| 49 | sourceLast = ""; |
| 50 | source = ""; |
| 51 | CurrentSnapshotNo = 0; |
| 52 | currentSnaps = 0; |
| 53 | syncTYPE = false; |
| 54 | abortNOW = false; |
| 55 | } |
| 56 | |
| 57 | // class CalcDiffThread Destructor================================================================================================= |
| 58 | CalcDiffThread::~CalcDiffThread () |
| 59 | { |
| 60 | wait(); |
| 61 | } |
| 62 | |
| 63 | // run the QThread================================================================================================= |
| 64 | void CalcDiffThread::run () |
| 65 | { |
| 66 | returnString = ""; |
| 67 | emit updateViewer(tr("Scanning destination") + "\n\n..." + tr("Please wait"), false); |
| 68 | QFileInfoList returnDirContents = readRecursive(dirToReadString); //this is the destination as a Qfileinfo list |
| 69 | |
| 70 | int countDirEntries=0, countReturnEntries=0; |
| 71 | |
| 72 | // Remove the "luckybackup snapshots root" directory from the returnDirContents list |
| 73 | countReturnEntries = 0; |
| 74 | while ( (countReturnEntries < returnDirContents.size()) && (!syncTYPE) ) |
| 75 | { |
| 76 | QFileInfo localFileInfo = returnDirContents.at(countReturnEntries); |
| 77 | if ( (localFileInfo.filePath()+SLASH).contains((dest + snapDefaultDir)) ) |
| 78 | returnDirContents.removeAt(countReturnEntries); |
| 79 | else |
| 80 | countReturnEntries++; |
| 81 | |
| 82 | if (abortNOW) |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | // Remove files/dirs listed at changes.file from the QFileInfo list (destination) |
| 88 | QStringList removedFiles; |
| 89 | removedFiles.clear(); |
| 90 | count = CurrentSnapshotNo+1; |
| 91 | while ( (count < currentSnaps) && (!syncTYPE) ) |
| 92 | { |
| 93 | snapchangesfilename = snapChangesDir + profileName + "-" + Operation[currentOperation] -> GetName() + "-" + |
| 94 | (Operation[currentOperation] -> GetSnapshotsListItem(count)) + ".changes.log"; |
| 95 | snapfile.setFileName(snapchangesfilename); |
| 96 | snapfile.open(QIODevice::ReadOnly | QIODevice::Text); |
| 97 | QTextStream in(&snapfile); |
| 98 | while (!in.atEnd()) |
| 99 | removedFiles.append(in.readLine()); |
| 100 | |
| 101 | snapfile.close(); |
| 102 | |
| 103 | count++; |
| 104 | } |
| 105 | countReturnEntries = 0; |
| 106 | |
| 107 | while ( (countReturnEntries < returnDirContents.size()) && (!syncTYPE) ) |
| 108 | { |
| 109 | QFileInfo localFileInfo = returnDirContents.at(countReturnEntries); |
| 110 | QString destFile = localFileInfo.filePath(); |
| 111 | destFile.remove(dest, Qt::CaseSensitive); |
| 112 | |
| 113 | countDirEntries = 0; |
| 114 | bool removed = false; |
| 115 | while (countDirEntries < removedFiles.size()) |
| 116 | { |
| 117 | if ( (destFile == removedFiles.at(countDirEntries)) || ((destFile+SLASH) == removedFiles.at(countDirEntries)) ) |
| 118 | { |
| 119 | removed = true; |
| 120 | returnDirContents.removeAt(countReturnEntries); |
| 121 | } |
| 122 | countDirEntries++; |
| 123 | } |
| 124 | if (removed) |
| 125 | removed = false; |
| 126 | else |
| 127 | countReturnEntries++; |
| 128 | } |
| 129 | |
| 130 | // Replace destination directory data with snapshot data - NOT in sync mode |
| 131 | // Do this for every snapshot from the current to the last one going back |
| 132 | count = currentSnaps-2; |
| 133 | while ( (count >= CurrentSnapshotNo) && (!syncTYPE) ) |
| 134 | { |
| 135 | QString SnapshotDirectoryStringToRead = Operation[currentOperation] -> GetSnapshotsListItem (count); |
| 136 | QString SnapshotDirectoryToRead = dest + snapDefaultDir + SnapshotDirectoryStringToRead + SLASH + sourceLast; |
| 137 | |
| 138 | QDir currentsnapdir(SnapshotDirectoryToRead); |
| 139 | if ( (currentsnapdir.exists()) && (!syncTYPE) ) |
| 140 | { |
| 141 | emit updateViewer(tr("Scanning snapshot") + " " + SnapshotDirectoryStringToRead + "\n\n..." + tr("Please wait"), false); |
| 142 | QFileInfoList currentsnapdirContents = readRecursive(SnapshotDirectoryToRead); // this is the snapshots directory currently read as a QFileInfo list |
| 143 | countDirEntries = 0; |
| 144 | |
| 145 | while (countDirEntries < currentsnapdirContents.size()) |
| 146 | { |
| 147 | //strip off beginning of snapshot file path |
| 148 | QFileInfo snapFileInfo = currentsnapdirContents.at(countDirEntries); |
| 149 | QString snapFile = snapFileInfo.filePath(); |
| 150 | snapFile.remove(SnapshotDirectoryToRead, Qt::CaseSensitive); |
| 151 | |
| 152 | countReturnEntries = 0; |
| 153 | bool replaced = false; |
| 154 | while (countReturnEntries < returnDirContents.size()) |
| 155 | { |
| 156 | QFileInfo localFileInfo = returnDirContents.at(countReturnEntries); |
| 157 | QString destFile = localFileInfo.filePath(); |
| 158 | for (int count3=CurrentSnapshotNo; count3 < currentSnaps-1; count3++) // if the destFile belongs to a "later" snapFile |
| 159 | { |
| 160 | QString IlostTrack = Operation[currentOperation] -> GetSnapshotsListItem (count3); |
| 161 | if (destFile.startsWith(dest + snapDefaultDir + IlostTrack) ) |
| 162 | destFile.remove(dest + snapDefaultDir + IlostTrack + SLASH + sourceLast, Qt::CaseSensitive); |
| 163 | } |
| 164 | destFile.remove(dest, Qt::CaseSensitive); |
| 165 | |
| 166 | if (snapFile == destFile) |
| 167 | { |
| 168 | returnDirContents.removeAt(countReturnEntries); |
| 169 | returnDirContents.insert(countReturnEntries,snapFileInfo); |
| 170 | replaced = true; |
| 171 | } |
| 172 | countReturnEntries++; |
| 173 | } |
| 174 | if ( (!replaced) && (!removedFiles.contains(snapFile,Qt::CaseSensitive )) ) |
| 175 | returnDirContents.append(snapFileInfo); |
| 176 | |
| 177 | countDirEntries++; |
| 178 | |
| 179 | if (abortNOW) |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | count--; |
| 184 | } |
| 185 | // Calculate differences between snapshot and source |
| 186 | QFileInfoList changedFiles, existingSource, existingSnap; |
| 187 | changedFiles.clear(); existingSource.clear(); existingSnap.clear(); |
| 188 | QString changedfiles="", existingsource = "", existingsnap = ""; |
| 189 | QList<bool> snapMatchesSource; |
| 190 | snapMatchesSource.clear(); |
| 191 | |
| 192 | emit updateViewer(tr("Scanning source") + "\n\n..." + tr("Please wait"), false); |
| 193 | QFileInfoList sourceDirAsList = readRecursive(source); // This is source as a QFileInfo list |
| 194 | |
| 195 | QString calcInfoText = tr("Calculating differences") + "\n\n..." + tr("Please wait") + "\n\n"; |
| 196 | QString sourceDirAsListSize = countStr.setNum(sourceDirAsList.size()); |
| 197 | |
| 198 | countDirEntries = 0; |
| 199 | while (countDirEntries < sourceDirAsList.size()) |
| 200 | { |
| 201 | bool foundMatchSourceToSnap = false; // If a file with some filename exists at the source and the snapshot |
| 202 | // strip off beginning of source file path |
| 203 | QFileInfo sourceFileInfo = sourceDirAsList.at(countDirEntries); |
| 204 | QString sourceFile = sourceFileInfo.filePath(); |
| 205 | if (sourceFile.startsWith(source)) |
| 206 | sourceFile.remove(source, Qt::CaseSensitive); |
| 207 | |
| 208 | emit updateViewer(calcInfoText + countStr.setNum(countDirEntries) + " / " + sourceDirAsListSize, false); // Give the user sth to watch counting |
| 209 | if (abortNOW) |
| 210 | return; |
| 211 | |
| 212 | // compare with every file in snapshot |
| 213 | countReturnEntries = 0; |
| 214 | while (countReturnEntries < returnDirContents.size()) |
| 215 | { |
| 216 | if (countDirEntries == 0) |
| 217 | snapMatchesSource.append(false); // build a <bool> list |
| 218 | |
| 219 | //strip off beginning of snapshot file path |
| 220 | QFileInfo snapFileInfo = returnDirContents.at(countReturnEntries); |
| 221 | QString snapFile = snapFileInfo.filePath(); |
| 222 | // ********************* TO EDIT *************************************************************************************** |
| 223 | for (count=currentSnaps-2; count >= CurrentSnapshotNo; count--) |
| 224 | { |
| 225 | QString SnapshotDirectoryStringToRead = Operation[currentOperation] -> GetSnapshotsListItem (count); |
| 226 | if (snapFile.startsWith(dest + snapDefaultDir + SnapshotDirectoryStringToRead) ) |
| 227 | snapFile.remove(dest + snapDefaultDir + SnapshotDirectoryStringToRead + SLASH + sourceLast, Qt::CaseSensitive); |
| 228 | //if (snapFile.startsWith(dest + snapDefaultDir + CurrentSnapshotString) ) |
| 229 | // snapFile.remove(dest + snapDefaultDir + CurrentSnapshotString + SLASH + sourceLast, Qt::CaseSensitive); |
| 230 | } |
| 231 | // ********************* TO EDIT *************************************************************************************** |
| 232 | |
| 233 | if (snapFile.startsWith(dest)) |
| 234 | snapFile.remove(dest, Qt::CaseSensitive); |
| 235 | |
| 236 | // 1. Data that have changed at source. |
| 237 | // The snapshot data will replace the relevant source data, at RESTORE |
| 238 | if (snapFile == sourceFile) |
| 239 | { |
| 240 | snapMatchesSource.replace(countReturnEntries, true); |
| 241 | foundMatchSourceToSnap = true; |
| 242 | if ((snapFileInfo.lastModified() != sourceFileInfo.lastModified()) || |
| 243 | (snapFileInfo.size() != sourceFileInfo.size())) |
| 244 | { |
| 245 | changedFiles.append(snapFileInfo); |
| 246 | |
| 247 | // Do not add anything if it's a directory!! |
| 248 | if (snapFileInfo.isDir()) |
| 249 | snapFile.append(SLASH); |
| 250 | else |
| 251 | if (changedfiles.size() < 10000) |
| 252 | changedfiles.append("* <b>" + snapFile + "</b> - " + snapFileInfo.lastModified().toString("yyyy MMM dd - hh:mm:ss") + "<br>"); |
| 253 | |
| 254 | //else |
| 255 | // break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | countReturnEntries++; |
| 260 | } |
| 261 | |
| 262 | // 2. Data that exist at the source but not at the snapshot |
| 263 | // These will be deleted at RESTORE if "delete existing source data that do not exist at the backup" is checked |
| 264 | if (!foundMatchSourceToSnap) |
| 265 | { |
| 266 | existingSource.append(sourceFileInfo); |
| 267 | if (sourceFileInfo.isDir()) |
| 268 | sourceFile.append(SLASH); |
| 269 | if (existingsource.size() < 10000) |
| 270 | existingsource.append("* <b>" + sourceFile + "</b> - " + sourceFileInfo.lastModified().toString("yyyy MMM dd - hh:mm:ss") + "<br>"); |
| 271 | } |
| 272 | |
| 273 | countDirEntries++; |
| 274 | } |
| 275 | |
| 276 | // 3. Data that exist at the snapshot but not at the source. |
| 277 | // These will be transferred over at RESTORE |
| 278 | countReturnEntries = 0; |
| 279 | while (countReturnEntries < snapMatchesSource.size()) |
| 280 | { |
| 281 | if (!snapMatchesSource.at(countReturnEntries)) |
| 282 | { |
| 283 | QFileInfo snapFileInfo = returnDirContents.at(countReturnEntries); |
| 284 | QString snapFile = snapFileInfo.filePath(); |
| 285 | // ********************* TO EDIT *************************************************************************************** |
| 286 | for (count=currentSnaps-2; count >= CurrentSnapshotNo; count--) |
| 287 | { |
| 288 | QString SnapshotDirectoryStringToRead = Operation[currentOperation] -> GetSnapshotsListItem (count); |
| 289 | if (snapFile.startsWith(dest + snapDefaultDir + SnapshotDirectoryStringToRead) ) |
| 290 | snapFile.remove(dest + snapDefaultDir + SnapshotDirectoryStringToRead + SLASH + sourceLast, Qt::CaseSensitive); |
| 291 | } |
| 292 | // ********************* TO EDIT *************************************************************************************** |
| 293 | |
| 294 | if (snapFile.startsWith(dest)) |
| 295 | snapFile.remove(dest, Qt::CaseSensitive); |
| 296 | if (snapFileInfo.isDir()) |
| 297 | snapFile.append(SLASH); |
| 298 | |
| 299 | existingSnap.append(snapFileInfo); |
| 300 | if (existingsnap.size() < 10000) |
| 301 | existingsnap.append("* <b>" + snapFile + "</b> - " + snapFileInfo.lastModified().toString("yyyy MMM dd - hh:mm:ss") + "<br>"); |
| 302 | } |
| 303 | countReturnEntries++; |
| 304 | } |
| 305 | |
| 306 | // Build the text to display to user |
| 307 | returnString.append("<font color=red><p align=\"center\">"); |
| 308 | if (syncTYPE) |
| 309 | returnString.append(" <b>" + tr("Sync dir A & B differences") + "</b><br>"); |
| 310 | else |
| 311 | returnString.append(" <b>" + tr("Snapshot and source differences") + "</b><br>"); |
| 312 | returnString.append("_______________________________________________________</p>"); |
| 313 | if (changedfiles != "") |
| 314 | { |
| 315 | returnString.append("</font><font color=blue><p align=\"center\">"); |
| 316 | if (syncTYPE) |
| 317 | returnString.append(" " + tr("Sync dir B data that also exists at sync dir A and is different") + "</p>"); |
| 318 | else |
| 319 | { |
| 320 | returnString.append(" " + tr("Snapshot data that also exists at the source and is different") + "<br>"); |
| 321 | returnString.append(" " + tr("The snapshot data will replace the source data during RESTORE") + "</p>"); |
| 322 | } |
| 323 | returnString.append(changedfiles); |
| 324 | if (changedfiles.size() > 9900) |
| 325 | returnString.append(" <br>" + tr("Data amount is huge. Displaying only first entries") + "<br>"); |
| 326 | returnString.append("_______________________________________________________<br>"); |
| 327 | } |
| 328 | if (existingsnap != "") |
| 329 | { |
| 330 | returnString.append("</font><font color=green><p align=\"center\">"); |
| 331 | if (syncTYPE) |
| 332 | returnString.append(" " + tr("Sync dir B data that do NOT exist at sync dir A") + "</p>"); |
| 333 | else |
| 334 | { |
| 335 | returnString.append(" " + tr("Snapshot data that do NOT exist at the source") + "<br>"); |
| 336 | returnString.append(" " + tr("These will be transferred over at the source during RESTORE") + "</p>"); |
| 337 | } |
| 338 | returnString.append(existingsnap); |
| 339 | if (existingsnap.size() > 9900) |
| 340 | returnString.append(" <br>" + tr("Data amount is huge. Displaying only first entries") + "<br>"); |
| 341 | returnString.append("_______________________________________________________<br>"); |
| 342 | } |
| 343 | if (existingsource != "") |
| 344 | { |
| 345 | returnString.append("</font><font color=magenta><p align=\"center\">"); |
| 346 | if (syncTYPE) |
| 347 | returnString.append(" " + tr("Sync dir A data that do NOT exist at sync dir B") + "</p>"); |
| 348 | else |
| 349 | { |
| 350 | returnString.append(" " + tr("Source data that do NOT exist at the snapshot") + "<br>"); |
| 351 | returnString.append(" " + tr("These will be deleted during RESTORE only if you enable the option:") + "<br>"); |
| 352 | returnString.append(" \"<b>" + tr("delete existing source data that do not exist at the backup") + "</b>\"</p>"); |
| 353 | } |
| 354 | returnString.append(existingsource); |
| 355 | if (existingsource.size() > 9900) |
| 356 | returnString.append(" <br>" + tr("Data amount is huge. Displaying only first entries") + "<br>"); |
| 357 | returnString.append("_________________________________________________________________<br>"); |
| 358 | } |
| 359 | returnString.append("</font>"); |
| 360 | } |
| 361 | |
| 362 | // set parametres================================================================================================= |
| 363 | void CalcDiffThread::passParams(QString dirtoreadstring, QString desti, QString currentsnapshotdirectory, QString currentsnapshotstring, QString sourcelast, QString sourcei, int currentsnapshotno, int currentsnaps, bool synctype) |
| 364 | { |
| 365 | this -> dirToReadString = dirtoreadstring; |
| 366 | this -> dest = desti; |
| 367 | this -> CurrentSnapshotDirectory = currentsnapshotdirectory; |
| 368 | this -> CurrentSnapshotString = currentsnapshotstring; |
| 369 | this -> sourceLast = sourcelast; |
| 370 | this -> source = sourcei; |
| 371 | this -> CurrentSnapshotNo = currentsnapshotno; |
| 372 | this -> currentSnaps = currentsnaps; |
| 373 | this -> syncTYPE = synctype; |
| 374 | } |
| 375 | |
| 376 | // Return the resulting text================================================================================================= |
| 377 | QString CalcDiffThread::returnResult() |
| 378 | { |
| 379 | return returnString; |
| 380 | } |
| 381 | |
| 382 | // Set the abortNOW =========================================================================================== |
| 383 | void CalcDiffThread::setAbort(bool abortset) |
| 384 | { |
| 385 | abortNOW = abortset; |
| 386 | } |
| 387 | |
| 388 | // // Extract a QFileInfoList from a directory's path =============================================================================================== |
| 389 | QFileInfoList CalcDiffThread::readRecursive(QString dirToReadString) |
| 390 | { |
| 391 | // QFileInfoList readRecursive(QString); |
| 392 | |
| 393 | QDir dirToRead(dirToReadString); |
| 394 | dirToRead.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden); |
| 395 | dirToRead.setSorting(QDir::Name | QDir::DirsFirst | QDir::IgnoreCase | QDir::LocaleAware); |
| 396 | |
| 397 | QFileInfoList DirContentsStart = dirToRead.entryInfoList(); |
| 398 | QFileInfoList returnDirContentsFinal; |
| 399 | |
| 400 | |
| 401 | QStack<QDir> dirStack; |
| 402 | dirStack.push(dirToRead); |
| 403 | |
| 404 | while( !dirStack.empty() ) |
| 405 | { |
| 406 | QDir lastDir = dirStack.last(); |
| 407 | lastDir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden); |
| 408 | lastDir.setSorting(QDir::Name | QDir::DirsFirst | QDir::IgnoreCase | QDir::LocaleAware); |
| 409 | QFileInfoList flist = lastDir.entryInfoList( ); |
| 410 | dirStack.pop(); |
| 411 | |
| 412 | foreach (QFileInfo localFileInfo, flist) |
| 413 | { |
| 414 | returnDirContentsFinal.append(localFileInfo); |
| 415 | if (localFileInfo.isDir()) |
| 416 | { |
| 417 | QDir dirent( localFileInfo.absoluteFilePath() ); |
| 418 | dirStack.push( dirent ); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return returnDirContentsFinal; |
| 424 | } |
| 425 | |
| 426 | // end of calcDiffThread.cpp --------------------------------------------------------------------------- |
| 427 |