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 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
|
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "syncengine.h"
#include "account.h"
#include "common/filesystembase.h"
#include "owncloudpropagator.h"
#include "common/syncjournaldb.h"
#include "common/syncjournalfilerecord.h"
#include "discoveryphase.h"
#include "creds/abstractcredentials.h"
#include "common/syncfilestatus.h"
#include "csync_exclude.h"
#include "filesystem.h"
#include "deletejob.h"
#include "propagatedownload.h"
#include "common/asserts.h"
#include "configfile.h"
#include "discovery.h"
#include "common/vfs.h"
#include "clientsideencryption.h"
#include "clientsideencryptionjobs.h"
#ifdef Q_OS_WIN
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <climits>
#include <cassert>
#include <chrono>
#include <QCoreApplication>
#include <QSslSocket>
#include <QDir>
#include <QLoggingCategory>
#include <QMutexLocker>
#include <QThread>
#include <QStringList>
#include <QTextStream>
#include <QTime>
#include <QUrl>
#include <QSslCertificate>
#include <QProcess>
#include <QElapsedTimer>
#include <QFileInfo>
#include <qtextcodec.h>
namespace OCC {
Q_LOGGING_CATEGORY(lcEngine, "nextcloud.sync.engine", QtInfoMsg)
bool SyncEngine::s_anySyncRunning = false;
/** When the client touches a file, block change notifications for this duration (ms)
*
* On Linux and Windows the file watcher can't distinguish a change that originates
* from the client (like a download during a sync operation) and an external change.
* To work around that, all files the client touches are recorded and file change
* notifications for these are blocked for some time. This value controls for how
* long.
*
* Reasons this delay can't be very small:
* - it takes time for the change notification to arrive and to be processed by the client
* - some time could pass between the client recording that a file will be touched
* and its filesystem operation finishing, triggering the notification
*/
static const std::chrono::milliseconds s_touchedFilesMaxAgeMs(3 * 1000);
// doc in header
std::chrono::milliseconds SyncEngine::minimumFileAgeForUpload(2000);
SyncEngine::SyncEngine(AccountPtr account,
const QString &localPath,
const SyncOptions &syncOptions,
const QString &remotePath,
OCC::SyncJournalDb *journal)
: _account(account)
, _localPath(localPath)
, _remotePath(remotePath)
, _journal(journal)
, _progressInfo(new ProgressInfo)
, _syncOptions(syncOptions)
{
qRegisterMetaType<SyncFileItem>("SyncFileItem");
qRegisterMetaType<SyncFileItemPtr>("SyncFileItemPtr");
qRegisterMetaType<SyncFileItem::Status>("SyncFileItem::Status");
qRegisterMetaType<SyncFileStatus>("SyncFileStatus");
qRegisterMetaType<SyncFileItemVector>("SyncFileItemVector");
qRegisterMetaType<SyncFileItem::Direction>("SyncFileItem::Direction");
// Everything in the SyncEngine expects a trailing slash for the localPath.
ASSERT(localPath.endsWith(QLatin1Char('/')));
_excludedFiles.reset(new ExcludedFiles(localPath));
_syncFileStatusTracker.reset(new SyncFileStatusTracker(this));
_clearTouchedFilesTimer.setSingleShot(true);
_clearTouchedFilesTimer.setInterval(30 * 1000);
connect(&_clearTouchedFilesTimer, &QTimer::timeout, this, &SyncEngine::slotClearTouchedFiles);
connect(this, &SyncEngine::finished, [this](bool /* finished */) {
_journal->keyValueStoreSet("last_sync", QDateTime::currentSecsSinceEpoch());
});
}
SyncEngine::~SyncEngine()
{
abort();
_excludedFiles.reset();
}
bool SyncEngine::SingleItemDiscoveryOptions::isValid() const
{
return !filePathRelative.isEmpty() && !discoveryPath.isEmpty()
&& ((discoveryDirItem && !discoveryDirItem->isEmpty()) || discoveryPath == QStringLiteral("/"));
}
/**
* Check if the item is in the blacklist.
* If it should not be sync'ed because of the blacklist, update the item with the error instruction
* and proper error message, and return true.
* If the item is not in the blacklist, or the blacklist is stale, return false.
*/
bool SyncEngine::checkErrorBlacklisting(SyncFileItem &item)
{
if (!_journal) {
qCCritical(lcEngine) << "Journal is undefined!";
return false;
}
SyncJournalErrorBlacklistRecord entry = _journal->errorBlacklistEntry(item._file);
item._hasBlacklistEntry = false;
if (!entry.isValid()) {
return false;
}
item._hasBlacklistEntry = true;
// If duration has expired, it's not blacklisted anymore
time_t now = Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc());
if (now >= entry._lastTryTime + entry._ignoreDuration) {
qCInfo(lcEngine) << "blacklist entry for " << item._file << " has expired!";
return false;
}
// If the file has changed locally or on the server, the blacklist
// entry no longer applies
if (item._direction == SyncFileItem::Up) { // check the modtime
if (item._modtime == 0 || entry._lastTryModtime == 0) {
return false;
} else if (item._modtime != entry._lastTryModtime) {
qCInfo(lcEngine) << item._file << " is blacklisted, but has changed mtime!";
return false;
} else if (item._renameTarget != entry._renameTarget) {
qCInfo(lcEngine) << item._file << " is blacklisted, but rename target changed from" << entry._renameTarget;
return false;
}
} else if (item._direction == SyncFileItem::Down) {
// download, check the etag.
if (item._etag.isEmpty() || entry._lastTryEtag.isEmpty()) {
qCInfo(lcEngine) << item._file << "one ETag is empty, no blacklisting";
return false;
} else if (item._etag != entry._lastTryEtag) {
qCInfo(lcEngine) << item._file << " is blacklisted, but has changed etag!";
return false;
}
}
qint64 waitSeconds = entry._lastTryTime + entry._ignoreDuration - now;
qCInfo(lcEngine) << "Item is on blacklist: " << entry._file
<< "retries:" << entry._retryCount
<< "for another" << waitSeconds << "s";
// We need to indicate that we skip this file due to blacklisting
// for reporting and for making sure we don't update the blacklist
// entry yet.
// Classification is this _instruction and _status
item._instruction = CSYNC_INSTRUCTION_IGNORE;
item._status = SyncFileItem::BlacklistedError;
auto waitSecondsStr = Utility::durationToDescriptiveString1(1000 * waitSeconds);
item._errorString = tr("%1 (skipped due to earlier error, trying again in %2)").arg(entry._errorString, waitSecondsStr);
if (entry._errorCategory == SyncJournalErrorBlacklistRecord::InsufficientRemoteStorage) {
slotInsufficientRemoteStorage();
}
return true;
}
static bool isFileTransferInstruction(SyncInstructions instruction)
{
return instruction == CSYNC_INSTRUCTION_CONFLICT
|| instruction == CSYNC_INSTRUCTION_NEW
|| instruction == CSYNC_INSTRUCTION_SYNC
|| instruction == CSYNC_INSTRUCTION_TYPE_CHANGE;
}
void SyncEngine::deleteStaleDownloadInfos(const SyncFileItemVector &syncItems)
{
// Find all downloadinfo paths that we want to preserve.
QSet<QString> download_file_paths;
for (const SyncFileItemPtr &it : syncItems) {
if (it->_direction == SyncFileItem::Down
&& it->_type == ItemTypeFile
&& isFileTransferInstruction(it->_instruction)) {
download_file_paths.insert(it->_file);
}
}
// Delete from journal and from filesystem.
const QVector<SyncJournalDb::DownloadInfo> deleted_infos =
_journal->getAndDeleteStaleDownloadInfos(download_file_paths);
for (const SyncJournalDb::DownloadInfo &deleted_info : deleted_infos) {
const QString tmppath = _propagator->fullLocalPath(deleted_info._tmpfile);
FileSystem::remove(tmppath);
}
}
void SyncEngine::deleteStaleUploadInfos(const SyncFileItemVector &syncItems)
{
// Find all blacklisted paths that we want to preserve.
QSet<QString> upload_file_paths;
for (const SyncFileItemPtr &it: syncItems) {
if (it->_direction == SyncFileItem::Up
&& it->_type == ItemTypeFile
&& isFileTransferInstruction(it->_instruction)) {
upload_file_paths.insert(it->_file);
}
}
// Delete from journal.
auto ids = _journal->deleteStaleUploadInfos(upload_file_paths);
// Delete the stales chunk on the server.
if (account()->capabilities().chunkingNg()) {
for (uint transferId : std::as_const(ids)) {
if (!transferId)
continue; // Was not a chunked upload
QUrl url = Utility::concatUrlPath(account()->url(), QLatin1String("remote.php/dav/uploads/") + account()->davUser() + QLatin1Char('/') + QString::number(transferId));
(new DeleteJob(account(), url, {}, this))->start();
}
}
}
void SyncEngine::deleteStaleErrorBlacklistEntries(const SyncFileItemVector &syncItems)
{
// Find all blacklisted paths that we want to preserve.
QSet<QString> blacklist_file_paths;
for (const SyncFileItemPtr &it : syncItems) {
if (it->_hasBlacklistEntry)
blacklist_file_paths.insert(it->_file);
}
// Delete from journal.
if (!_journal->deleteStaleErrorBlacklistEntries(blacklist_file_paths)) {
qCWarning(lcEngine) << "Could not delete StaleErrorBlacklistEntries from DB";
}
}
void SyncEngine::conflictRecordMaintenance()
{
// Remove stale conflict entries from the database
// by checking which files still exist and removing the
// missing ones.
const auto conflictRecordPaths = _journal->conflictRecordPaths();
for (const auto &path : conflictRecordPaths) {
auto fsPath = _propagator->fullLocalPath(QString::fromUtf8(path));
if (!FileSystem::fileExists(fsPath)) {
_journal->deleteConflictRecord(path);
}
}
// Did the sync see any conflict files that don't yet have records?
// If so, add them now.
//
// This happens when the conflicts table is new or when conflict files
// are downloaded but the server doesn't send conflict headers.
for (const auto &path : std::as_const(_seenConflictFiles)) {
ASSERT(Utility::isConflictFile(path));
auto bapath = path.toUtf8();
if (!conflictRecordPaths.contains(bapath)) {
ConflictRecord record;
record.path = bapath;
auto basePath = Utility::conflictFileBaseNameFromPattern(bapath);
record.initialBasePath = basePath;
// Determine fileid of target file
SyncJournalFileRecord baseRecord;
if (_journal->getFileRecord(basePath, &baseRecord) && baseRecord.isValid()) {
record.baseFileId = baseRecord._fileId;
}
_journal->setConflictRecord(record);
}
}
}
void SyncEngine::caseClashConflictRecordMaintenance()
{
// Remove stale conflict entries from the database
// by checking which files still exist and removing the
// missing ones.
const auto conflictRecordPaths = _journal->caseClashConflictRecordPaths();
for (const auto &path : conflictRecordPaths) {
const auto fsPath = _propagator->fullLocalPath(QString::fromUtf8(path));
if (!FileSystem::fileExists(fsPath)) {
_journal->deleteCaseClashConflictByPathRecord(path);
}
}
}
void OCC::SyncEngine::slotItemDiscovered(const OCC::SyncFileItemPtr &item)
{
emit itemDiscovered(item);
if (Utility::isConflictFile(item->_file))
_seenConflictFiles.insert(item->_file);
if (item->_instruction == CSYNC_INSTRUCTION_UPDATE_METADATA && !item->isDirectory()) {
// For directories, metadata-only updates will be done after all their files are propagated.
// Update the database now already: New remote fileid or Etag or RemotePerm
// Or for files that were detected as "resolved conflict".
// Or a local inode/mtime change
// In case of "resolved conflict": there should have been a conflict because they
// both were new, or both had their local mtime or remote etag modified, but the
// size and mtime is the same on the server. This typically happens when the
// database is removed. Nothing will be done for those files, but we still need
// to update the database.
// This metadata update *could* be a propagation job of its own, but since it's
// quick to do and we don't want to create a potentially large number of
// mini-jobs later on, we just update metadata right now.
if (item->_direction == SyncFileItem::Down) {
auto modificationHappened = false; // Decides whether or not we modify file metadata
QString filePath = _localPath + item->_file;
// If the 'W' remote permission changed, update the local filesystem
SyncJournalFileRecord prev;
if (_journal->getFileRecord(item->_file, &prev)
&& prev.isValid()
&& prev._remotePerm.hasPermission(RemotePermissions::CanWrite) != item->_remotePerm.hasPermission(RemotePermissions::CanWrite)) {
const bool isReadOnly = !item->_remotePerm.isNull() && !item->_remotePerm.hasPermission(RemotePermissions::CanWrite);
modificationHappened = FileSystem::setFileReadOnlyWeak(filePath, isReadOnly);
}
modificationHappened |= item->_size != prev._fileSize;
auto rec = item->toSyncJournalFileRecordWithInode(filePath);
if (rec._checksumHeader.isEmpty())
rec._checksumHeader = prev._checksumHeader;
rec._serverHasIgnoredFiles |= prev._serverHasIgnoredFiles;
// Ensure it's a placeholder file on disk
if (item->_type == ItemTypeFile && _syncOptions._vfs->mode() != Vfs::Off) {
const auto result = _syncOptions._vfs->convertToPlaceholder(filePath, *item, {}, Vfs::UpdateMetadataType::DatabaseMetadata);
if (!result) {
item->_status = SyncFileItem::Status::NormalError;
item->_instruction = CSYNC_INSTRUCTION_ERROR;
item->_errorString = tr("Could not update file: %1").arg(result.error());
emit itemCompleted(item, ErrorCategory::GenericError);
return;
}
modificationHappened = true;
}
if (item->_type == CSyncEnums::ItemTypeVirtualFile) {
const auto lockOwnerTypeToSkipReadonly = _account->capabilities().filesLockTypeAvailable()
? SyncFileItem::LockOwnerType::TokenLock
: SyncFileItem::LockOwnerType::UserLock;
if (item->_locked == SyncFileItem::LockStatus::LockedItem
&& (item->_lockOwnerType != lockOwnerTypeToSkipReadonly || item->_lockOwnerId != account()->davUser())) {
qCDebug(lcEngine()) << filePath << "file is locked: making it read only";
FileSystem::setFileReadOnly(filePath, true);
} else {
qCDebug(lcEngine()) << filePath << "file is not locked: making it"
<< ((!item->_remotePerm.isNull() && !item->_remotePerm.hasPermission(RemotePermissions::CanWrite)) ? "read only"
: "read write");
FileSystem::setFileReadOnlyWeak(filePath, (!item->_remotePerm.isNull() && !item->_remotePerm.hasPermission(RemotePermissions::CanWrite)));
}
}
// Update on-disk virtual file metadata
if (modificationHappened && item->_type == ItemTypeVirtualFile) {
auto r = _syncOptions._vfs->updateMetadata(*item, filePath, {});
if (!r) {
item->_status = SyncFileItem::Status::NormalError;
item->_instruction = CSYNC_INSTRUCTION_ERROR;
item->_errorString = tr("Could not update virtual file metadata: %1").arg(r.error());
emit itemCompleted(item, ErrorCategory::GenericError);
return;
}
} else if (prev._modtime != item->_modtime) {
if (!FileSystem::setModTime(filePath, item->_modtime)) {
item->_instruction = CSYNC_INSTRUCTION_ERROR;
item->_errorString = tr("Could not update file metadata: %1").arg(filePath);
emit itemCompleted(item, ErrorCategory::GenericError);
return;
}
}
// Updating the db happens on success
if (!_journal->setFileRecord(rec)) {
item->_status = SyncFileItem::Status::NormalError;
item->_instruction = CSYNC_INSTRUCTION_ERROR;
item->_errorString = tr("Could not set file record to local DB: %1").arg(rec.path());
qCWarning(lcEngine) << "Could not set file record to local DB" << rec.path();
}
// This might have changed the shared flag, so we must notify SyncFileStatusTracker for example
emit itemCompleted(item, ErrorCategory::NoError);
} else {
// Update only outdated data from the disk.
SyncJournalFileLockInfo lockInfo;
lockInfo._locked = item->_locked == SyncFileItem::LockStatus::LockedItem;
lockInfo._lockTime = item->_lockTime;
lockInfo._lockTimeout = item->_lockTimeout;
lockInfo._lockOwnerId = item->_lockOwnerId;
lockInfo._lockOwnerType = static_cast<qint64>(item->_lockOwnerType);
lockInfo._lockOwnerDisplayName = item->_lockOwnerDisplayName;
lockInfo._lockEditorApp = item->_lockOwnerDisplayName;
lockInfo._lockToken = item->_lockToken;
if (!_journal->updateLocalMetadata(item->_file, item->_modtime, item->_size, item->_inode, lockInfo)) {
qCWarning(lcEngine) << "Could not update local metadata for file" << item->_file;
}
}
_hasNoneFiles = true;
return;
} else if (item->_instruction == CSYNC_INSTRUCTION_NONE) {
_hasNoneFiles = true;
_syncFileStatusTracker->slotCheckAndRemoveSilentlyExcluded(item->_file);
if (_account->capabilities().uploadConflictFiles() && Utility::isConflictFile(item->_file)) {
// For uploaded conflict files, files with no action performed on them should
// be displayed: but we mustn't overwrite the instruction if something happens
// to the file!
item->_errorString = tr("Unresolved conflict.");
item->_instruction = CSYNC_INSTRUCTION_IGNORE;
item->_status = SyncFileItem::Conflict;
}
return;
} else if (item->_instruction == CSYNC_INSTRUCTION_REMOVE && !item->_isSelectiveSync) {
_hasRemoveFile = true;
} else if (item->_instruction == CSYNC_INSTRUCTION_RENAME) {
_hasNoneFiles = true; // If a file (or every file) has been renamed, it means not al files where deleted
} else if (item->_instruction == CSYNC_INSTRUCTION_TYPE_CHANGE
|| item->_instruction == CSYNC_INSTRUCTION_SYNC) {
if (item->_direction == SyncFileItem::Up) {
// An upload of an existing file means that the file was left unchanged on the server
// This counts as a NONE for detecting if all the files on the server were changed
_hasNoneFiles = true;
}
}
// check for blacklisting of this item.
// if the item is on blacklist, the instruction was set to ERROR
checkErrorBlacklisting(*item);
_needsUpdate = true;
// Insert sorted
auto it = std::lower_bound( _syncItems.begin(), _syncItems.end(), item ); // the _syncItems is sorted
_syncItems.insert( it, item );
slotNewItem(item);
if (item->isDirectory()) {
slotFolderDiscovered(item->_etag.isEmpty(), item->_file);
}
}
void SyncEngine::startSync()
{
if (_journal->exists()) {
QVector<SyncJournalDb::PollInfo> pollInfos = _journal->getPollInfos();
if (!pollInfos.isEmpty()) {
qCInfo(lcEngine) << "Finish Poll jobs before starting a sync";
auto *job = new CleanupPollsJob(_account,
_journal, _localPath, _syncOptions._vfs, this);
connect(job, &CleanupPollsJob::finished, this, &SyncEngine::startSync);
connect(job, &CleanupPollsJob::aborted, this, &SyncEngine::slotCleanPollsJobAborted);
job->start();
return;
}
const auto e2EeLockedFolders = _journal->e2EeLockedFolders();
if (!e2EeLockedFolders.isEmpty()) {
for (const auto &e2EeLockedFolder : e2EeLockedFolders) {
const auto folderId = e2EeLockedFolder.first;
qCInfo(lcEngine()) << "start unlock job for folderId:" << folderId;
const auto folderToken = EncryptionHelper::decryptStringAsymmetric(_account->e2e()->getCertificateInformation(), _account->e2e()->paddingMode(), *_account->e2e(), e2EeLockedFolder.second);
if (!folderToken) {
qCWarning(lcEngine()) << "decrypt failed";
return;
}
// TODO: We need to rollback changes done to metadata in case we have an active lock, this needs to be implemented on the server first
const auto unlockJob = new OCC::UnlockEncryptFolderApiJob(_account, folderId, *folderToken, _journal, this);
unlockJob->setShouldRollbackMetadataChanges(true);
unlockJob->start();
}
}
}
if (s_anySyncRunning || _syncRunning) {
return;
}
const auto currentEncryptionStatus = EncryptionStatusEnums::toDbEncryptionStatus(EncryptionStatusEnums::fromEndToEndEncryptionApiVersion(_account->capabilities().clientSideEncryptionVersion()));
[[maybe_unused]] const auto result = _journal->listAllE2eeFoldersWithEncryptionStatusLessThan(static_cast<int>(currentEncryptionStatus), [this](const SyncJournalFileRecord &record) {
_journal->schedulePathForRemoteDiscovery(record.path());
});
s_anySyncRunning = true;
_syncRunning = true;
_anotherSyncNeeded = NoFollowUpSync;
_clearTouchedFilesTimer.stop();
_hasNoneFiles = false;
_hasRemoveFile = false;
_seenConflictFiles.clear();
_progressInfo->reset();
if (!QFileInfo::exists(_localPath)) {
_anotherSyncNeeded = DelayedFollowUp;
// No _tr, it should only occur in non-mirall
Q_EMIT syncError(QStringLiteral("Unable to find local sync folder."), ErrorCategory::GenericError);
finalize(false);
return;
}
// Check free size on disk first.
const qint64 minFree = criticalFreeSpaceLimit();
const qint64 freeBytes = Utility::freeDiskSpace(_localPath);
if (freeBytes >= 0) {
if (freeBytes < minFree) {
qCWarning(lcEngine()) << "Too little space available at" << _localPath << ". Have"
<< freeBytes << "bytes and require at least" << minFree << "bytes";
_anotherSyncNeeded = DelayedFollowUp;
Q_EMIT syncError(tr("Only %1 are available, need at least %2 to start",
"Placeholders are postfixed with file sizes using Utility::octetsToString()")
.arg(
Utility::octetsToString(freeBytes),
Utility::octetsToString(minFree)), ErrorCategory::GenericError);
finalize(false);
return;
} else {
qCInfo(lcEngine) << "There are" << freeBytes << "bytes available at" << _localPath;
}
} else {
qCWarning(lcEngine) << "Could not determine free space available at" << _localPath;
}
_syncItems.clear();
_needsUpdate = false;
if (!_journal->exists()) {
qCInfo(lcEngine) << "New sync (no sync journal exists)";
} else {
qCInfo(lcEngine) << "Sync with existing sync journal";
}
QString verStr("Using Qt ");
verStr.append(qVersion());
verStr.append(" SSL library ").append(QSslSocket::sslLibraryVersionString().toUtf8().data());
verStr.append(" on ").append(Utility::platformName());
qCInfo(lcEngine) << verStr;
// This creates the DB if it does not exist yet.
if (!_journal->open()) {
qCWarning(lcEngine) << "No way to create a sync journal!";
Q_EMIT syncError(tr("Unable to open or create the local sync database. Make sure you have write access in the sync folder."), ErrorCategory::GenericError);
finalize(false);
return;
// database creation error!
}
// Functionality like selective sync might have set up etag storage
// filtering via schedulePathForRemoteDiscovery(). This *is* the next sync, so
// undo the filter to allow this sync to retrieve and store the correct etags.
_journal->clearEtagStorageFilter();
_excludedFiles->setExcludeConflictFiles(!_account->capabilities().uploadConflictFiles());
_lastLocalDiscoveryStyle = _localDiscoveryStyle;
if (_syncOptions._vfs->mode() == Vfs::WithSuffix && _syncOptions._vfs->fileSuffix().isEmpty()) {
Q_EMIT syncError(tr("Using virtual files with suffix, but suffix is not set"), ErrorCategory::GenericError);
finalize(false);
return;
}
bool ok = false;
auto selectiveSyncBlackList = _journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok);
if (ok) {
bool usingSelectiveSync = (!selectiveSyncBlackList.isEmpty());
qCInfo(lcEngine) << (usingSelectiveSync ? "Using Selective Sync" : "NOT Using Selective Sync");
} else {
qCWarning(lcEngine) << "Could not retrieve selective sync list from DB";
Q_EMIT syncError(tr("Unable to read the blacklist from the local database"), ErrorCategory::GenericError);
finalize(false);
return;
}
processCaseClashConflictsBeforeDiscovery();
_stopWatch.start();
_progressInfo->_status = ProgressInfo::Starting;
emit transmissionProgress(*_progressInfo);
qCInfo(lcEngine) << "#### Discovery start ####################################################";
qCInfo(lcEngine) << "Server" << account()->serverVersion()
<< (account()->isHttp2Supported() ? "Using HTTP/2" : "");
_progressInfo->_status = ProgressInfo::Discovery;
emit transmissionProgress(*_progressInfo);
_remnantReadOnlyFolders.clear();
_discoveryPhase = std::make_unique<DiscoveryPhase>();
_discoveryPhase->_leadingAndTrailingSpacesFilesAllowed = _leadingAndTrailingSpacesFilesAllowed;
_discoveryPhase->_account = _account;
_discoveryPhase->_excludes = _excludedFiles.data();
const QString excludeFilePath = _localPath + QStringLiteral(".sync-exclude.lst");
if (FileSystem::fileExists(excludeFilePath)) {
_discoveryPhase->_excludes->addExcludeFilePath(excludeFilePath);
_discoveryPhase->_excludes->reloadExcludeFiles();
}
_discoveryPhase->_statedb = _journal;
_discoveryPhase->_localDir = Utility::trailingSlashPath(_localPath);
_discoveryPhase->_remoteFolder = Utility::trailingSlashPath(_remotePath);
_discoveryPhase->_syncOptions = _syncOptions;
_discoveryPhase->_shouldDiscoverLocaly = [this](const QString &path) {
const auto result = shouldDiscoverLocally(path);
return result;
};
_discoveryPhase->setSelectiveSyncBlackList(selectiveSyncBlackList);
_discoveryPhase->setSelectiveSyncWhiteList(_journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok));
if (!ok) {
qCWarning(lcEngine) << "Unable to read selective sync list, aborting.";
Q_EMIT syncError(tr("Unable to read from the sync journal."), ErrorCategory::GenericError);
finalize(false);
return;
}
const auto accountCaps = _account->capabilities();
const auto forbiddenFilenames = accountCaps.forbiddenFilenames();
const auto forbiddenBasenames = accountCaps.forbiddenFilenameBasenames();
const auto forbiddenExtensions = accountCaps.forbiddenFilenameExtensions();
const auto forbiddenChars = accountCaps.forbiddenFilenameCharacters();
_discoveryPhase->_forbiddenFilenames = forbiddenFilenames;
_discoveryPhase->_forbiddenBasenames = forbiddenBasenames;
_discoveryPhase->_forbiddenExtensions = forbiddenExtensions;
_discoveryPhase->_forbiddenChars = forbiddenChars;
if (!forbiddenFilenames.isEmpty() &&
!forbiddenBasenames.isEmpty() &&
!forbiddenExtensions.isEmpty() &&
!forbiddenChars.isEmpty()) {
_shouldEnforceWindowsFileNameCompatibility = true;
_discoveryPhase->_shouldEnforceWindowsFileNameCompatibility = _shouldEnforceWindowsFileNameCompatibility;
}
#if defined Q_OS_WINDOWS
_shouldEnforceWindowsFileNameCompatibility = true;
_discoveryPhase->_shouldEnforceWindowsFileNameCompatibility = _shouldEnforceWindowsFileNameCompatibility;
#endif
// Check for invalid character in old server version
QString invalidFilenamePattern = _account->capabilities().invalidFilenameRegex();
if (invalidFilenamePattern.isNull()
&& _account->serverVersionInt() < Account::makeServerVersion(8, 1, 0)) {
// Server versions older than 8.1 don't support some characters in filenames.
// If the capability is not set, default to a pattern that avoids uploading
// files with names that contain these.
// It's important to respect the capability also for older servers -- the
// version check doesn't make sense for custom servers.
invalidFilenamePattern = R"([\\:?*"<>|])";
}
if (!invalidFilenamePattern.isEmpty())
_discoveryPhase->_invalidFilenameRx = QRegularExpression(invalidFilenamePattern);
_discoveryPhase->_serverBlacklistedFiles = _account->capabilities().blacklistedFiles();
_discoveryPhase->_ignoreHiddenFiles = ignoreHiddenFiles();
connect(_discoveryPhase.get(), &DiscoveryPhase::itemDiscovered, this, &SyncEngine::slotItemDiscovered);
connect(_discoveryPhase.get(), &DiscoveryPhase::newBigFolder, this, &SyncEngine::newBigFolder);
connect(_discoveryPhase.get(), &DiscoveryPhase::existingFolderNowBig, this, &SyncEngine::existingFolderNowBig);
connect(_discoveryPhase.get(), &DiscoveryPhase::fatalError, this, [this](const QString &errorString, ErrorCategory errorCategory) {
Q_EMIT syncError(errorString, errorCategory);
finalize(false);
});
connect(_discoveryPhase.get(), &DiscoveryPhase::finished, this, &SyncEngine::slotDiscoveryFinished);
connect(_discoveryPhase.get(), &DiscoveryPhase::silentlyExcluded,
_syncFileStatusTracker.data(), &SyncFileStatusTracker::slotAddSilentlyExcluded);
connect(_discoveryPhase.get(), &DiscoveryPhase::remnantReadOnlyFolderDiscovered, this, &SyncEngine::remnantReadOnlyFolderDiscovered);
ProcessDirectoryJob *discoveryJob = nullptr;
if (singleItemDiscoveryOptions().isValid()) {
_discoveryPhase->_listExclusiveFiles.clear();
_discoveryPhase->_listExclusiveFiles.push_back(singleItemDiscoveryOptions().filePathRelative);
}
if (singleItemDiscoveryOptions().isValid() && singleItemDiscoveryOptions().discoveryDirItem) {
const auto databaseFingerprint = _journal->dataFingerprint();
_discoveryPhase->_dataFingerprint = databaseFingerprint;
ProcessDirectoryJob::PathTuple path = {};
path._local = path._original = path._server = path._target = singleItemDiscoveryOptions().discoveryPath;
SyncJournalFileRecord rec;
const auto localQueryMode = _journal->getFileRecord(singleItemDiscoveryOptions().discoveryDirItem->_file, &rec) && rec.isValid()
? ProcessDirectoryJob::NormalQuery
: ProcessDirectoryJob::ParentDontExist;
const auto pinState = [this, &rec]() {
if (!_syncOptions._vfs || _syncOptions._vfs->mode() == Vfs::Off) {
return PinState::AlwaysLocal;
}
if (!rec.isValid()) {
return PinState::OnlineOnly;
}
const auto pinStateInDb = _journal->internalPinStates().rawForPath(singleItemDiscoveryOptions().discoveryDirItem->_file.toUtf8());
if (pinStateInDb) {
return *pinStateInDb;
}
return PinState::Unspecified;
}();
discoveryJob = new ProcessDirectoryJob(
_discoveryPhase.get(),
pinState,
path,
singleItemDiscoveryOptions().discoveryDirItem,
{},
localQueryMode,
_journal->keyValueStoreGetInt("last_sync", 0),
_discoveryPhase.get()
);
} else {
discoveryJob = new ProcessDirectoryJob(
_discoveryPhase.get(),
PinState::AlwaysLocal,
_journal->keyValueStoreGetInt("last_sync", 0),
_discoveryPhase.get()
);
}
_discoveryPhase->startJob(discoveryJob);
connect(discoveryJob, &ProcessDirectoryJob::etag, this, &SyncEngine::slotRootEtagReceived);
connect(discoveryJob, &ProcessDirectoryJob::updatedRootFolderQuota, account().data(), &Account::rootFolderQuotaChanged);
connect(_discoveryPhase.get(), &DiscoveryPhase::addErrorToGui, this, &SyncEngine::addErrorToGui);
}
void SyncEngine::slotFolderDiscovered(bool local, const QString &folder)
{
// Don't wanna overload the UI
if (!_lastUpdateProgressCallbackCall.isValid() || _lastUpdateProgressCallbackCall.elapsed() >= 200) {
_lastUpdateProgressCallbackCall.start(); // first call or enough elapsed time
} else {
return;
}
if (local) {
_progressInfo->_currentDiscoveredLocalFolder = folder;
_progressInfo->_currentDiscoveredRemoteFolder.clear();
} else {
_progressInfo->_currentDiscoveredRemoteFolder = folder;
_progressInfo->_currentDiscoveredLocalFolder.clear();
}
emit transmissionProgress(*_progressInfo);
}
void SyncEngine::slotRootEtagReceived(const QByteArray &e, const QDateTime &time)
{
if (_remoteRootEtag.isEmpty()) {
qCDebug(lcEngine) << "Root etag:" << e;
_remoteRootEtag = e;
emit rootEtag(_remoteRootEtag, time);
}
}
void SyncEngine::slotNewItem(const SyncFileItemPtr &item)
{
_progressInfo->adjustTotalsForFile(*item);
}
void SyncEngine::slotDiscoveryFinished()
{
if (!_discoveryPhase) {
// There was an error that was already taken care of
return;
}
qCInfo(lcEngine) << "#### Discovery end #################################################### " << _stopWatch.addLapTime(QLatin1String("Discovery Finished")) << "ms";
// Sanity check
if (!_journal->open()) {
qCWarning(lcEngine) << "Bailing out, DB failure";
Q_EMIT syncError(tr("Cannot open the sync journal"), ErrorCategory::GenericError);
finalize(false);
return;
} else {
// Commits a possibly existing (should not though) transaction and starts a new one for the propagate phase
_journal->commitIfNeededAndStartNewTransaction("Post discovery");
}
_progressInfo->_currentDiscoveredRemoteFolder.clear();
_progressInfo->_currentDiscoveredLocalFolder.clear();
_progressInfo->_status = ProgressInfo::Reconcile;
emit transmissionProgress(*_progressInfo);
if (handleMassDeletion()) {
return;
}
if (!_remnantReadOnlyFolders.isEmpty()) {
handleRemnantReadOnlyFolders();
}
finishSync();
}
void SyncEngine::slotCleanPollsJobAborted(const QString &error, const ErrorCategory errorCategory)
{
emit syncError(error, errorCategory);
finalize(false);
}
void SyncEngine::detectFileLock(const SyncFileItemPtr &item)
{
const auto isNewlyUploadedFile = !item->isDirectory() &&
item->_instruction == CSYNC_INSTRUCTION_NEW &&
item->_direction == SyncFileItem::Up && item->_status == SyncFileItem::Success;
if (isNewlyUploadedFile && item->_locked != SyncFileItem::LockStatus::LockedItem && _account->capabilities().filesLockAvailable() &&
FileSystem::isMatchingOfficeFileExtension(item->_file)) {
{
SyncJournalFileRecord rec;
if (!_journal->getFileRecord(item->_file, &rec) || !rec.isValid()) {
qCWarning(lcEngine) << "Newly-created office file just uploaded but not in sync journal. Not going to lock it." << item->_file;
return;
}
}
const auto localFilePath = _propagator->fullLocalPath(item->_file);
const auto allMatchingLockFiles = FileSystem::findAllLockFilesInDir(QFileInfo(localFilePath).absolutePath());
for (const auto &lockFilePath : allMatchingLockFiles) {
const auto checkResult = FileSystem::lockFileTargetFilePath(lockFilePath, FileSystem::filePathLockFilePatternMatch(lockFilePath));
if (checkResult.type == FileSystem::FileLockingInfo::Type::Locked && checkResult.path == localFilePath) {
qCInfo(lcEngine) << "Newly-created office file lock detected. Let FolderWatcher take it from here..." << item->_file;
emit lockFileDetected(lockFilePath);
}
}
}
}
void SyncEngine::setNetworkLimits(int upload, int download)
{
_uploadLimit = upload;
_downloadLimit = download;
if (!_propagator)
return;
_propagator->_uploadLimit = upload;
_propagator->_downloadLimit = download;
if (upload != 0 || download != 0) {
qCInfo(lcEngine) << "Network Limits (down/up) " << upload << download;
}
}
void SyncEngine::slotItemCompleted(const SyncFileItemPtr &item, const ErrorCategory category)
{
_progressInfo->setProgressComplete(*item);
emit transmissionProgress(*_progressInfo);
emit itemCompleted(item, category);
detectFileLock(item);
}
void SyncEngine::slotPropagationFinished(OCC::SyncFileItem::Status status)
{
if (_propagator->_anotherSyncNeeded && _anotherSyncNeeded == NoFollowUpSync) {
_anotherSyncNeeded = ImmediateFollowUp;
}
if ((status == SyncFileItem::Success || status == SyncFileItem::BlacklistedError) && _discoveryPhase) {
_journal->setDataFingerprint(_discoveryPhase->_dataFingerprint);
}
conflictRecordMaintenance();
caseClashConflictRecordMaintenance();
_journal->deleteStaleFlagsEntries();
_journal->commit("All Finished.", false);
// Send final progress information even if no
// files needed propagation, but clear the lastCompletedItem
// so we don't count this twice (like Recent Files)
_progressInfo->_lastCompletedItem = SyncFileItem();
_progressInfo->_status = ProgressInfo::Done;
emit transmissionProgress(*_progressInfo);
finalize(status == SyncFileItem::Success);
}
void SyncEngine::finalize(bool success)
{
setSingleItemDiscoveryOptions({});
qCInfo(lcEngine) << "Sync run took " << _stopWatch.addLapTime(QLatin1String("Sync Finished")) << "ms";
_stopWatch.stop();
if (_discoveryPhase) {
_discoveryPhase.release()->deleteLater();
}
s_anySyncRunning = false;
_syncRunning = false;
emit finished(success);
if (_account->shouldSkipE2eeMetadataChecksumValidation()) {
qCDebug(lcEngine) << "shouldSkipE2eeMetadataChecksumValidation was set. Sync is finished, so resetting it...";
_account->resetShouldSkipE2eeMetadataChecksumValidation();
}
// Delete the propagator only after emitting the signal.
_propagator.clear();
_seenConflictFiles.clear();
_uniqueErrors.clear();
_localDiscoveryPaths.clear();
_localDiscoveryStyle = LocalDiscoveryStyle::FilesystemOnly;
_clearTouchedFilesTimer.start();
_leadingAndTrailingSpacesFilesAllowed.clear();
}
void SyncEngine::processCaseClashConflictsBeforeDiscovery()
{
QSet<QByteArray> pathsToAppend;
const auto caseClashConflictPaths = _journal->caseClashConflictRecordPaths();
for (const auto &caseClashConflictPath : caseClashConflictPaths) {
auto caseClashPathSplit = caseClashConflictPath.split('/');
if (caseClashPathSplit.size() > 1) {
caseClashPathSplit.removeLast();
pathsToAppend.insert(caseClashPathSplit.join('/'));
}
}
for (const auto &pathToAppend : pathsToAppend) {
_journal->schedulePathForRemoteDiscovery(pathToAppend);
}
}
void SyncEngine::slotProgress(const SyncFileItem &item, qint64 current)
{
_progressInfo->setProgressItem(item, current);
emit transmissionProgress(*_progressInfo);
}
void SyncEngine::restoreOldFiles(SyncFileItemVector &syncItems)
{
/* When the server is trying to send us lots of file in the past, this means that a backup
was restored in the server. In that case, we should not simply overwrite the newer file
on the file system with the older file from the backup on the server. Instead, we will
upload the client file. But we still downloaded the old file in a conflict file just in case
*/
for (const auto &syncItem : std::as_const(syncItems)) {
if (syncItem->_direction != SyncFileItem::Down || syncItem->_isSelectiveSync) {
continue;
}
switch (syncItem->_instruction) {
case CSYNC_INSTRUCTION_SYNC:
qCWarning(lcEngine) << "restoreOldFiles: RESTORING" << syncItem->_file;
syncItem->_instruction = CSYNC_INSTRUCTION_CONFLICT;
break;
case CSYNC_INSTRUCTION_REMOVE:
if (syncItem->_type != CSyncEnums::ItemTypeVirtualFile && syncItem->_type != CSyncEnums::ItemTypeVirtualFileDownload) {
qCWarning(lcEngine) << "restoreOldFiles: RESTORING" << syncItem->_file;
syncItem->_instruction = CSYNC_INSTRUCTION_NEW;
syncItem->_direction = SyncFileItem::Up;
}
break;
case CSYNC_INSTRUCTION_RENAME:
case CSYNC_INSTRUCTION_NEW:
// Ideally we should try to revert the rename or remove, but this would be dangerous
// without re-doing the reconcile phase. So just let it happen.
default:
break;
}
}
}
void SyncEngine::cancelSyncOrContinue(bool cancel)
{
if (cancel) {
qCInfo(lcEngine) << "User aborted sync";
finalize(false);
} else {
finishSync();
}
}
void SyncEngine::finishSync()
{
auto databaseFingerprint = _journal->dataFingerprint();
// If databaseFingerprint is empty, this means that there was no information in the database
// (for example, upgrading from a previous version, or first sync, or server not supporting fingerprint)
if (!databaseFingerprint.isEmpty() && _discoveryPhase
&& _discoveryPhase->_dataFingerprint != databaseFingerprint) {
qCInfo(lcEngine) << "data fingerprint changed, assume restore from backup" << databaseFingerprint << _discoveryPhase->_dataFingerprint;
restoreOldFiles(_syncItems);
}
if (_discoveryPhase && _discoveryPhase->_anotherSyncNeeded && !_discoveryPhase->_filesNeedingScheduledSync.empty()) {
slotScheduleFilesDelayedSync();
} else if (_discoveryPhase && _discoveryPhase->_anotherSyncNeeded && _anotherSyncNeeded == NoFollowUpSync) {
_anotherSyncNeeded = ImmediateFollowUp;
}
if (_discoveryPhase && !_discoveryPhase->_filesUnscheduleSync.empty()) {
slotUnscheduleFilesDelayedSync();
}
if (_discoveryPhase && _discoveryPhase->_hasDownloadRemovedItems && _discoveryPhase->_hasUploadErrorItems) {
for (const auto &item : std::as_const(_syncItems)) {
if (item->_instruction == CSYNC_INSTRUCTION_ERROR && item->_direction == SyncFileItem::Up) {
// item->_instruction = CSYNC_INSTRUCTION_IGNORE;
}
}
_anotherSyncNeeded = ImmediateFollowUp;
}
Q_ASSERT(std::is_sorted(_syncItems.begin(), _syncItems.end()));
qCInfo(lcEngine) << "#### Reconcile (aboutToPropagate) #################################################### " << _stopWatch.addLapTime(QStringLiteral("Reconcile (aboutToPropagate)")) << "ms";
_localDiscoveryPaths.clear();
// To announce the beginning of the sync
emit aboutToPropagate(_syncItems);
qCInfo(lcEngine) << "#### Reconcile (aboutToPropagate OK) #################################################### "<< _stopWatch.addLapTime(QStringLiteral("Reconcile (aboutToPropagate OK)")) << "ms";
// it's important to do this before ProgressInfo::start(), to announce start of new sync
_progressInfo->_status = ProgressInfo::Propagation;
emit transmissionProgress(*_progressInfo);
_progressInfo->startEstimateUpdates();
// post update phase script: allow to tweak stuff by a custom script in debug mode.
if (!qEnvironmentVariableIsEmpty("OWNCLOUD_POST_UPDATE_SCRIPT")) {
#ifndef NDEBUG
const QString script = qEnvironmentVariable("OWNCLOUD_POST_UPDATE_SCRIPT");
qCDebug(lcEngine) << "Post Update Script: " << script;
auto scriptArgs = script.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
if (scriptArgs.size() > 0) {
const auto scriptExecutable = scriptArgs.takeFirst();
QProcess::execute(scriptExecutable, scriptArgs);
}
#else
qCWarning(lcEngine) << "**** Attention: POST_UPDATE_SCRIPT installed, but not executed because compiled with NDEBUG";
#endif
}
// do a database commit
_journal->commit(QStringLiteral("post treewalk"));
_propagator = QSharedPointer<OwncloudPropagator>(
new OwncloudPropagator(_account, _localPath, _remotePath, _journal, _bulkUploadBlackList));
_propagator->setSyncOptions(_syncOptions);
connect(_propagator.data(), &OwncloudPropagator::itemCompleted,
this, &SyncEngine::slotItemCompleted);
connect(_propagator.data(), &OwncloudPropagator::progress,
this, &SyncEngine::slotProgress);
connect(_propagator.data(), &OwncloudPropagator::finished, this, &SyncEngine::slotPropagationFinished, Qt::QueuedConnection);
connect(_propagator.data(), &OwncloudPropagator::seenLockedFile, this, &SyncEngine::seenLockedFile);
connect(_propagator.data(), &OwncloudPropagator::touchedFile, this, &SyncEngine::slotAddTouchedFile);
connect(_propagator.data(), &OwncloudPropagator::insufficientLocalStorage, this, &SyncEngine::slotInsufficientLocalStorage);
connect(_propagator.data(), &OwncloudPropagator::insufficientRemoteStorage, this, &SyncEngine::slotInsufficientRemoteStorage);
connect(_propagator.data(), &OwncloudPropagator::newItem, this, &SyncEngine::slotNewItem);
// apply the network limits to the propagator
setNetworkLimits(_uploadLimit, _downloadLimit);
deleteStaleDownloadInfos(_syncItems);
deleteStaleUploadInfos(_syncItems);
deleteStaleErrorBlacklistEntries(_syncItems);
_journal->commit(QStringLiteral("post stale entry removal"));
// Emit the started signal only after the propagator has been set up.
if (_needsUpdate)
Q_EMIT started();
_propagator->start(std::move(_syncItems));
qCInfo(lcEngine) << "#### Post-Reconcile end #################################################### " << _stopWatch.addLapTime(QStringLiteral("Post-Reconcile Finished")) << "ms";
}
bool SyncEngine::handleMassDeletion()
{
const auto displayDialog = ConfigFile().promptDeleteFiles() && !_syncOptions.isCmd();
const auto allFilesDeleted = !_hasNoneFiles && _hasRemoveFile;
auto deletionCounter = 0;
for (const auto &oneItem : std::as_const(_syncItems)) {
if (oneItem->_instruction == CSYNC_INSTRUCTION_REMOVE) {
if (oneItem->isDirectory()) {
const auto result = _journal->listFilesInPath(oneItem->_file.toUtf8(), [&deletionCounter] (const auto &oneRecord) {
if (oneRecord.isFile()) {
++deletionCounter;
}
});
if (!result) {
qCDebug(lcEngine()) << "unable to find the number of files within a deleted folder:" << oneItem->_file;
}
} else {
++deletionCounter;
}
}
}
const auto filesDeletedThresholdExceeded = deletionCounter > ConfigFile().deleteFilesThreshold();
if ((allFilesDeleted || filesDeletedThresholdExceeded) && displayDialog) {
qCWarning(lcEngine) << "Many files are going to be deleted, asking the user";
int side = 0; // > 0 means more deleted on the server. < 0 means more deleted on the client
for (const auto &it : std::as_const(_syncItems)) {
if (it->_instruction == CSYNC_INSTRUCTION_REMOVE) {
side += it->_direction == SyncFileItem::Down ? 1 : -1;
}
}
promptUserBeforePropagation([this, side](auto &&callback){
emit aboutToRemoveAllFiles(side >= 0 ? SyncFileItem::Down : SyncFileItem::Up, callback);
});
return true;
}
return false;
}
void SyncEngine::handleRemnantReadOnlyFolders()
{
auto listOfFolders = QStringList{};
for (const auto &oneFolder : std::as_const(_remnantReadOnlyFolders)) {
listOfFolders.push_back(oneFolder->_file);
}
qCInfo(lcEngine()) << "will delete invalid read-only folders:" << listOfFolders.join(", ");
for(const auto &oneFolder : std::as_const(_remnantReadOnlyFolders)) {
const auto fileInfo = QFileInfo{_localPath + oneFolder->_file};
const auto parentFolderPath = fileInfo.dir().absolutePath();
slotAddTouchedFile(parentFolderPath);
const auto parentPermissionsHandler = FileSystem::FilePermissionsRestore{parentFolderPath, FileSystem::FolderPermissions::ReadWrite};
slotAddTouchedFile(_localPath + oneFolder->_file);
if (oneFolder->_type == ItemType::ItemTypeDirectory) {
const auto deletionCallback = [this] (const QString &deleteItem, bool) {
slotAddTouchedFile(deleteItem);
};
FileSystem::removeRecursively(_localPath + oneFolder->_file, deletionCallback, nullptr, deletionCallback);
} else {
FileSystem::remove(_localPath + oneFolder->_file);
}
}
}
template <typename T>
void SyncEngine::promptUserBeforePropagation(T &&lambda)
{
QPointer<QObject> guard = new QObject();
QPointer<QObject> self = this;
auto callback = [this, self, guard](bool cancel) -> void {
// use a guard to ensure its only called once...
// qpointer to self to ensure we still exist
if (!guard || !self) {
return;
}
guard->deleteLater();
cancelSyncOrContinue(cancel);
};
lambda(callback);
}
void SyncEngine::slotAddTouchedFile(const QString &fn)
{
QElapsedTimer now;
now.start();
QString file = QDir::cleanPath(fn);
// Iterate from the oldest and remove anything older than 15 seconds.
while (true) {
auto first = _touchedFiles.begin();
if (first == _touchedFiles.end())
break;
// Compare to our new QElapsedTimer instead of using elapsed().
// This avoids querying the current time from the OS for every loop.
auto elapsed = std::chrono::milliseconds(now.msecsSinceReference() - first.key().msecsSinceReference());
if (elapsed <= s_touchedFilesMaxAgeMs) {
// We found the first path younger than the maximum age, keep the rest.
break;
}
_touchedFiles.erase(first);
}
// This should be the largest QElapsedTimer yet, use constEnd() as hint.
_touchedFiles.insert(_touchedFiles.constEnd(), now, file);
}
void SyncEngine::slotClearTouchedFiles()
{
_touchedFiles.clear();
}
void SyncEngine::addAcceptedInvalidFileName(const QString& filePath)
{
_leadingAndTrailingSpacesFilesAllowed.append(filePath);
}
void SyncEngine::setLocalDiscoveryEnforceWindowsFileNameCompatibility(bool value)
{
_shouldEnforceWindowsFileNameCompatibility = value;
}
bool SyncEngine::wasFileTouched(const QString &fn) const
{
// Start from the end (most recent) and look for our path. Check the time just in case.
auto begin = _touchedFiles.constBegin();
for (auto it = _touchedFiles.constEnd(); it != begin; --it) {
if (const auto prevIt = std::prev(it); prevIt.value() == fn)
return std::chrono::milliseconds(prevIt.key().elapsed()) <= s_touchedFilesMaxAgeMs;
}
return false;
}
void SyncEngine::setLocalDiscoveryOptions(LocalDiscoveryStyle style, std::set<QString> paths)
{
_localDiscoveryStyle = style;
_localDiscoveryPaths = std::move(paths);
if (lcEngine().isDebugEnabled() && !_localDiscoveryPaths.empty()) {
// only execute if logging is enabled
auto debug = qDebug(lcEngine);
debug << "paths to discover locally";
for (auto path : _localDiscoveryPaths) {
debug << path;
}
}
// Normalize to make sure that no path is a contained in another.
// Note: for simplicity, this code consider anything less than '/' as a path separator, so for
// example, this will remove "foo.bar" if "foo" is in the list. This will mean we might have
// some false positive, but that's Ok.
// This invariant is used in SyncEngine::shouldDiscoverLocally
QString prev;
auto it = _localDiscoveryPaths.begin();
while(it != _localDiscoveryPaths.end()) {
if (!prev.isNull() && it->startsWith(prev) && (prev.endsWith('/') || *it == prev || it->at(prev.size()) <= '/')) {
it = _localDiscoveryPaths.erase(it);
} else {
prev = *it;
++it;
}
}
}
void SyncEngine::setSingleItemDiscoveryOptions(const SingleItemDiscoveryOptions &singleItemDiscoveryOptions)
{
_singleItemDiscoveryOptions = singleItemDiscoveryOptions;
}
const SyncEngine::SingleItemDiscoveryOptions &SyncEngine::singleItemDiscoveryOptions() const
{
return _singleItemDiscoveryOptions;
}
bool SyncEngine::shouldDiscoverLocally(const QString &path) const
{
auto result = false;
if (_localDiscoveryStyle == LocalDiscoveryStyle::FilesystemOnly) {
result = true;
return result;
}
// The intention is that if "A/X" is in _localDiscoveryPaths:
// - parent folders like "/", "A" will be discovered (to make sure the discovery reaches the
// point where something new happened)
// - the folder itself "A/X" will be discovered
// - subfolders like "A/X/Y" will be discovered (so data inside a new or renamed folder will be
// discovered in full)
// Check out TestLocalDiscovery::testLocalDiscoveryDecision()
auto it = _localDiscoveryPaths.lower_bound(path);
if (it == _localDiscoveryPaths.end() || !it->startsWith(path)) {
// Maybe a subfolder of something in the list?
if (it != _localDiscoveryPaths.begin() && path.startsWith(*(--it))) {
result = it->endsWith('/') || (path.size() > it->size() && path.at(it->size()) <= '/');
if (!result) {
qCDebug(lcEngine()) << path << "no local discovery needed";
}
return result;
}
result = false;
qCDebug(lcEngine()) << path << "no local discovery needed";
return result;
}
// maybe an exact match or an empty path?
if (it->size() == path.size() || path.isEmpty()) {
return true;
}
// Maybe a parent folder of something in the list?
// check for a prefix + / match
forever {
if (it->size() > path.size() && it->at(path.size()) == '/') {
result = true;
return result;
}
++it;
if (it == _localDiscoveryPaths.end() || !it->startsWith(path)) {
result = false;
qCDebug(lcEngine()) << path << "no local discovery needed";
return result;
}
}
qCDebug(lcEngine()) << path << "no local discovery needed";
return result;
}
void SyncEngine::wipeVirtualFiles(const QString &localPath, SyncJournalDb &journal, Vfs &vfs)
{
qCInfo(lcEngine) << "Wiping virtual files inside" << localPath;
const auto resGetFilesBelowPath = journal.getFilesBelowPath(QByteArray(), [&](const SyncJournalFileRecord &rec) {
if (rec._type != ItemTypeVirtualFile && rec._type != ItemTypeVirtualFileDownload)
return;
qCDebug(lcEngine) << "Removing db record for" << rec.path();
if (!journal.deleteFileRecord(rec._path)) {
qCWarning(lcEngine) << "Could not update delete file record" << rec._path;
}
// If the local file is a dehydrated placeholder, wipe it too.
// Otherwise leave it to allow the next sync to have a new-new conflict.
QString localFile = localPath + rec._path;
if (FileSystem::fileExists(localFile) && vfs.isDehydratedPlaceholder(localFile)) {
qCDebug(lcEngine) << "Removing local dehydrated placeholder" << rec.path();
QFile::remove(localFile);
}
});
if (!resGetFilesBelowPath) {
qCWarning(lcEngine) << "Failed to get files below path" << localPath;
}
journal.forceRemoteDiscoveryNextSync();
// Postcondition: No ItemTypeVirtualFile / ItemTypeVirtualFileDownload left in the db.
// But hydrated placeholders may still be around.
}
void SyncEngine::switchToVirtualFiles(const QString &localPath, SyncJournalDb &journal, Vfs &vfs)
{
qCInfo(lcEngine) << "Convert to virtual files inside" << localPath;
const auto res = journal.getFilesBelowPath({}, [&](const SyncJournalFileRecord &rec) {
const auto path = rec.path();
const auto fileName = QFileInfo(path).fileName();
if (FileSystem::isExcludeFile(fileName)) {
return;
}
SyncFileItem item;
QString localFile = localPath + path;
const auto result = vfs.convertToPlaceholder(localFile, item, localFile);
if (!result.isValid()) {
qCWarning(lcEngine) << "Could not convert file to placeholder" << result.error();
}
});
if (!res) {
qCWarning(lcEngine) << "Failed to get files below path" << localPath;
}
}
void SyncEngine::abort()
{
if (_propagator) {
// If we're already in the propagation phase, aborting that is sufficient
qCInfo(lcEngine) << "Aborting sync in propagator...";
_propagator->abort();
} else if (_discoveryPhase) {
// Delete the discovery and all child jobs after ensuring
// it can't finish and start the propagator
disconnect(_discoveryPhase.get(), nullptr, this, nullptr);
_discoveryPhase.release()->deleteLater();
qCInfo(lcEngine) << "Aborting sync in discovery...";
finalize(false);
}
}
void SyncEngine::slotSummaryError(const QString &message)
{
if (_uniqueErrors.contains(message))
return;
_uniqueErrors.insert(message);
emit syncError(message, ErrorCategory::GenericError);
}
void SyncEngine::slotInsufficientLocalStorage()
{
slotSummaryError(
tr("Disk space is low: Downloads that would reduce free space "
"below %1 were skipped.")
.arg(Utility::octetsToString(freeSpaceLimit())));
}
void SyncEngine::slotInsufficientRemoteStorage()
{
auto msg = tr("There is insufficient space available on the server for some uploads.");
if (_uniqueErrors.contains(msg)) {
return;
}
_uniqueErrors.insert(msg);
emit syncError(msg, ErrorCategory::InsufficientRemoteStorage);
}
void SyncEngine::slotScheduleFilesDelayedSync()
{
if (!_discoveryPhase || _discoveryPhase->_filesNeedingScheduledSync.empty()) {
return;
}
// The latest sync of the interval bucket is the one that goes through and is used in the timer.
// By running the sync run as late as possible in the selected interval, we try to strike a
// balance between updating the needed file in a timely manner while also syncing late enough
// to cover all the files in the interval bucket.
static constexpr qint64 intervalSecs = 60;
const auto scheduledSyncBuckets = groupNeededScheduledSyncRuns(intervalSecs);
qCDebug(lcEngine) << "Active scheduled sync run timers:" << _scheduledSyncTimers.count();
for (const auto &[scheduledSyncTimerSecs, filesAffected] : scheduledSyncBuckets) {
const auto currentSecsSinceEpoch = QDateTime::currentSecsSinceEpoch();
const auto scheduledSyncTimerTime = QDateTime::fromSecsSinceEpoch(currentSecsSinceEpoch + scheduledSyncTimerSecs);
const auto scheduledSyncTimerMsecs = std::chrono::milliseconds(scheduledSyncTimerSecs * 1000);
const auto addFilesToTimerAndScheduledHash = [this, &files = filesAffected] (const QSharedPointer<ScheduledSyncTimer> &timer) {
for (const auto &file : files) {
timer->files.insert(file);
_filesScheduledForLaterSync.insert(file, timer);
}
};
// We want to make sure that this bucket won't schedule a sync near a pre-existing sync run,
// as we often get, for example, locked file notifications one by one as the user interacts
// through the web.
const auto nearbyTimer = nearbyScheduledSyncTimer(scheduledSyncTimerSecs, intervalSecs);
if (nearbyTimer) {
addFilesToTimerAndScheduledHash(nearbyTimer);
qCInfo(lcEngine) << "Using a nearby scheduled sync run at:" << scheduledSyncTimerTime
<< "for files:" << filesAffected
<< "this timer is now resoponsible for files:" << nearbyTimer->files;
continue;
}
qCInfo(lcEngine) << "Will have a new sync run in" << scheduledSyncTimerSecs
<< "seconds, at" << scheduledSyncTimerTime
<< "for files:" << filesAffected;
QSharedPointer<ScheduledSyncTimer> newTimer(new ScheduledSyncTimer);
newTimer->setSingleShot(true);
newTimer->callOnTimeout(this, [this, newTimer] {
qCInfo(lcEngine) << "Rescanning now that delayed sync run is scheduled for:" << newTimer->files;
for (const auto &file : std::as_const(newTimer->files)) {
this->_filesScheduledForLaterSync.remove(file);
}
this->startSync();
this->slotCleanupScheduledSyncTimers();
});
addFilesToTimerAndScheduledHash(newTimer);
newTimer->start(scheduledSyncTimerMsecs);
_scheduledSyncTimers.append(newTimer);
}
}
QHash<qint64, SyncEngine::ScheduledSyncBucket> SyncEngine::groupNeededScheduledSyncRuns(const qint64 interval) const
{
if (!_discoveryPhase || _discoveryPhase->_filesNeedingScheduledSync.empty()) {
return {};
}
QHash<qint64, ScheduledSyncBucket> intervalSyncBuckets;
for (auto it = _discoveryPhase->_filesNeedingScheduledSync.cbegin();
it != _discoveryPhase->_filesNeedingScheduledSync.cend();
++it) {
const auto file = it.key();
const auto syncScheduledSecs = it.value();
// We don't want to schedule syncs again for files we have already discovered needing a
// scheduled sync, unless the files have been re-locked or had their lock expire time
// extended. So we check the time-out of the already set timer with the time-out we
// receive from the server entry
//
// Since the division here is both of ints, we receive a "floor" of the division, so we
// are safe from a possible situation where the timer's interval is lower than we need
// for the file we are possibly scheduling a sync run for
if (_filesScheduledForLaterSync.contains(file) &&
_filesScheduledForLaterSync.value(file)->interval() / 1000 >= syncScheduledSecs) {
continue;
}
// Both qint64 so division results in floor-ed result
const auto intervalBucketKey = syncScheduledSecs / interval;
if (!intervalSyncBuckets.contains(intervalBucketKey)) {
intervalSyncBuckets.insert(intervalBucketKey, {syncScheduledSecs, {file}});
continue;
}
auto bucketValue = intervalSyncBuckets.value(intervalBucketKey);
bucketValue.scheduledSyncTimerSecs = qMax(bucketValue.scheduledSyncTimerSecs, syncScheduledSecs);
bucketValue.files.append(file);
intervalSyncBuckets.insert(intervalBucketKey, bucketValue);
}
return intervalSyncBuckets;
}
QSharedPointer<SyncEngine::ScheduledSyncTimer> SyncEngine::nearbyScheduledSyncTimer(const qint64 scheduledSyncTimerSecs,
const qint64 intervalSecs) const
{
const auto scheduledSyncTimerMsecs = scheduledSyncTimerSecs * 1000;
const auto halfIntervalMsecs = (intervalSecs * 1000) / 2;
for (const auto &scheduledTimer : _scheduledSyncTimers) {
const auto timerRemainingMsecs = scheduledTimer->remainingTime();
const auto differenceMsecs = timerRemainingMsecs - scheduledSyncTimerMsecs;
const auto nearbyScheduledSync = differenceMsecs > -halfIntervalMsecs &&
differenceMsecs < halfIntervalMsecs;
// Iterated timer is going to fire slightly before we need it to for the parameter timer, delay it.
if (differenceMsecs > -halfIntervalMsecs && differenceMsecs < 0) {
const auto scheduledSyncTimerTimeoutMsecs = std::chrono::milliseconds(scheduledSyncTimerMsecs);
scheduledTimer->start(scheduledSyncTimerTimeoutMsecs);
qCDebug(lcEngine) << "Delayed sync timer with remaining time" << timerRemainingMsecs / 1000
<< "by" << (differenceMsecs * -1) / 1000
<< "seconds due to nearby new sync run needed.";
}
if(nearbyScheduledSync) {
return scheduledTimer;
}
}
return {};
}
void SyncEngine::slotCleanupScheduledSyncTimers()
{
qCDebug(lcEngine) << "Beginning scheduled sync timer cleanup.";
auto it = _scheduledSyncTimers.begin();
while(it != _scheduledSyncTimers.end()) {
const auto &timer = *it;
auto eraseTimer = false;
if(timer && (timer->files.empty() || !timer->isActive())) {
qCInfo(lcEngine) << "Stopping and erasing an expired/empty scheduled sync run timer.";
timer->stop();
eraseTimer = true;
} else if (!timer) {
qCInfo(lcEngine) << "Erasing a null scheduled sync run timer.";
eraseTimer = true;
}
if(eraseTimer) {
it = _scheduledSyncTimers.erase(it);
} else {
++it;
}
}
}
void SyncEngine::remnantReadOnlyFolderDiscovered(const SyncFileItemPtr &item)
{
_remnantReadOnlyFolders.push_back(item);
}
void SyncEngine::slotUnscheduleFilesDelayedSync()
{
if (!_discoveryPhase || _discoveryPhase->_filesUnscheduleSync.empty()) {
return;
}
for (const auto &file : std::as_const(_discoveryPhase->_filesUnscheduleSync)) {
const auto fileSyncRunTimer = _filesScheduledForLaterSync.value(file);
if (fileSyncRunTimer) {
fileSyncRunTimer->files.remove(file);
// Below is only needed for logging
const auto currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
const auto scheduledSyncTimerMSecs = fileSyncRunTimer->remainingTime();
const auto timerExpireDate = QDateTime::fromMSecsSinceEpoch(currentMSecsSinceEpoch + scheduledSyncTimerMSecs);
qCInfo(lcEngine) << "Removed" << file << "from sync run timer elapsing at" << timerExpireDate
<< "this timer is still running for files:" << fileSyncRunTimer->files;
}
}
slotCleanupScheduledSyncTimers();
}
} // namespace OCC
|