propagatedownload.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. * Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "config.h"
  15. #include "owncloudpropagator_p.h"
  16. #include "propagatedownload.h"
  17. #include "networkjobs.h"
  18. #include "account.h"
  19. #include "common/syncjournaldb.h"
  20. #include "common/syncjournalfilerecord.h"
  21. #include "common/utility.h"
  22. #include "filesystem.h"
  23. #include "propagatorjobs.h"
  24. #include "common/checksums.h"
  25. #include "common/asserts.h"
  26. #include "clientsideencryptionjobs.h"
  27. #include "propagatedownloadencrypted.h"
  28. #include "common/vfs.h"
  29. #include <QLoggingCategory>
  30. #include <QNetworkAccessManager>
  31. #include <QFileInfo>
  32. #include <QDir>
  33. #include <cmath>
  34. #ifdef Q_OS_UNIX
  35. #include <unistd.h>
  36. #endif
  37. namespace OCC {
  38. Q_LOGGING_CATEGORY(lcGetJob, "nextcloud.sync.networkjob.get", QtInfoMsg)
  39. Q_LOGGING_CATEGORY(lcPropagateDownload, "nextcloud.sync.propagator.download", QtInfoMsg)
  40. // Always coming in with forward slashes.
  41. // In csync_excluded_no_ctx we ignore all files with longer than 254 chars
  42. // This function also adds a dot at the beginning of the filename to hide the file on OS X and Linux
  43. QString OWNCLOUDSYNC_EXPORT createDownloadTmpFileName(const QString &previous)
  44. {
  45. QString tmpFileName;
  46. QString tmpPath;
  47. int slashPos = previous.lastIndexOf('/');
  48. // work with both pathed filenames and only filenames
  49. if (slashPos == -1) {
  50. tmpFileName = previous;
  51. tmpPath = QString();
  52. } else {
  53. tmpFileName = previous.mid(slashPos + 1);
  54. tmpPath = previous.left(slashPos);
  55. }
  56. int overhead = 1 + 1 + 2 + 8; // slash dot dot-tilde ffffffff"
  57. int spaceForFileName = qMin(254, tmpFileName.length() + overhead) - overhead;
  58. if (tmpPath.length() > 0) {
  59. return tmpPath + '/' + '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
  60. } else {
  61. return '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
  62. }
  63. }
  64. // DOES NOT take ownership of the device.
  65. GETFileJob::GETFileJob(AccountPtr account, const QString &path, QIODevice *device,
  66. const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
  67. qint64 resumeStart, QObject *parent)
  68. : AbstractNetworkJob(account, path, parent)
  69. , _device(device)
  70. , _headers(headers)
  71. , _expectedEtagForResume(expectedEtagForResume)
  72. , _expectedContentLength(-1)
  73. , _contentLength(-1)
  74. , _resumeStart(resumeStart)
  75. , _errorStatus(SyncFileItem::NoStatus)
  76. , _bandwidthLimited(false)
  77. , _bandwidthChoked(false)
  78. , _bandwidthQuota(0)
  79. , _bandwidthManager(nullptr)
  80. , _hasEmittedFinishedSignal(false)
  81. , _lastModified()
  82. {
  83. }
  84. GETFileJob::GETFileJob(AccountPtr account, const QUrl &url, QIODevice *device,
  85. const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
  86. qint64 resumeStart, QObject *parent)
  87. : AbstractNetworkJob(account, url.toEncoded(), parent)
  88. , _device(device)
  89. , _headers(headers)
  90. , _expectedEtagForResume(expectedEtagForResume)
  91. , _expectedContentLength(-1)
  92. , _contentLength(-1)
  93. , _resumeStart(resumeStart)
  94. , _errorStatus(SyncFileItem::NoStatus)
  95. , _directDownloadUrl(url)
  96. , _bandwidthLimited(false)
  97. , _bandwidthChoked(false)
  98. , _bandwidthQuota(0)
  99. , _bandwidthManager(nullptr)
  100. , _hasEmittedFinishedSignal(false)
  101. , _lastModified()
  102. {
  103. }
  104. void GETFileJob::start()
  105. {
  106. if (_resumeStart > 0) {
  107. _headers["Range"] = "bytes=" + QByteArray::number(_resumeStart) + '-';
  108. _headers["Accept-Ranges"] = "bytes";
  109. qCDebug(lcGetJob) << "Retry with range " << _headers["Range"];
  110. }
  111. QNetworkRequest req;
  112. for (QMap<QByteArray, QByteArray>::const_iterator it = _headers.begin(); it != _headers.end(); ++it) {
  113. req.setRawHeader(it.key(), it.value());
  114. }
  115. req.setPriority(QNetworkRequest::LowPriority); // Long downloads must not block non-propagation jobs.
  116. if (_directDownloadUrl.isEmpty()) {
  117. sendRequest("GET", makeDavUrl(path()), req);
  118. } else {
  119. // Use direct URL
  120. sendRequest("GET", _directDownloadUrl, req);
  121. }
  122. qCDebug(lcGetJob) << _bandwidthManager << _bandwidthChoked << _bandwidthLimited;
  123. if (_bandwidthManager) {
  124. _bandwidthManager->registerDownloadJob(this);
  125. }
  126. connect(this, &AbstractNetworkJob::networkActivity, account().data(), &Account::propagatorNetworkActivity);
  127. AbstractNetworkJob::start();
  128. }
  129. void GETFileJob::newReplyHook(QNetworkReply *reply)
  130. {
  131. reply->setReadBufferSize(16 * 1024); // keep low so we can easier limit the bandwidth
  132. connect(reply, &QNetworkReply::metaDataChanged, this, &GETFileJob::slotMetaDataChanged);
  133. connect(reply, &QIODevice::readyRead, this, &GETFileJob::slotReadyRead);
  134. connect(reply, &QNetworkReply::finished, this, &GETFileJob::slotReadyRead);
  135. connect(reply, &QNetworkReply::downloadProgress, this, &GETFileJob::downloadProgress);
  136. }
  137. void GETFileJob::slotMetaDataChanged()
  138. {
  139. // For some reason setting the read buffer in GETFileJob::start doesn't seem to go
  140. // through the HTTP layer thread(?)
  141. reply()->setReadBufferSize(16 * 1024);
  142. int httpStatus = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  143. if (httpStatus == 301 || httpStatus == 302 || httpStatus == 303 || httpStatus == 307
  144. || httpStatus == 308 || httpStatus == 401) {
  145. // Redirects and auth failures (oauth token renew) are handled by AbstractNetworkJob and
  146. // will end up restarting the job. We do not want to process further data from the initial
  147. // request. newReplyHook() will reestablish signal connections for the follow-up request.
  148. bool ok = disconnect(reply(), &QNetworkReply::finished, this, &GETFileJob::slotReadyRead)
  149. && disconnect(reply(), &QNetworkReply::readyRead, this, &GETFileJob::slotReadyRead);
  150. ASSERT(ok);
  151. return;
  152. }
  153. // If the status code isn't 2xx, don't write the reply body to the file.
  154. // For any error: handle it when the job is finished, not here.
  155. if (httpStatus / 100 != 2) {
  156. // Disable the buffer limit, as we don't limit the bandwidth for error messages.
  157. // (We are only going to do a readAll() at the end.)
  158. reply()->setReadBufferSize(0);
  159. return;
  160. }
  161. if (reply()->error() != QNetworkReply::NoError) {
  162. return;
  163. }
  164. _etag = getEtagFromReply(reply());
  165. if (!_directDownloadUrl.isEmpty() && !_etag.isEmpty()) {
  166. qCInfo(lcGetJob) << "Direct download used, ignoring server ETag" << _etag;
  167. _etag = QByteArray(); // reset received ETag
  168. } else if (!_directDownloadUrl.isEmpty()) {
  169. // All fine, ETag empty and directDownloadUrl used
  170. } else if (_etag.isEmpty()) {
  171. qCWarning(lcGetJob) << "No E-Tag reply by server, considering it invalid";
  172. _errorString = tr("No E-Tag received from server, check Proxy/Gateway");
  173. _errorStatus = SyncFileItem::NormalError;
  174. reply()->abort();
  175. return;
  176. } else if (!_expectedEtagForResume.isEmpty() && _expectedEtagForResume != _etag) {
  177. qCWarning(lcGetJob) << "We received a different E-Tag for resuming!"
  178. << _expectedEtagForResume << "vs" << _etag;
  179. _errorString = tr("We received a different E-Tag for resuming. Retrying next time.");
  180. _errorStatus = SyncFileItem::NormalError;
  181. reply()->abort();
  182. return;
  183. }
  184. bool ok = false;
  185. _contentLength = reply()->header(QNetworkRequest::ContentLengthHeader).toLongLong(&ok);
  186. if (ok && _expectedContentLength != -1 && _contentLength != _expectedContentLength) {
  187. qCWarning(lcGetJob) << "We received a different content length than expected!"
  188. << _expectedContentLength << "vs" << _contentLength;
  189. _errorString = tr("We received an unexpected download Content-Length.");
  190. _errorStatus = SyncFileItem::NormalError;
  191. reply()->abort();
  192. return;
  193. }
  194. qint64 start = 0;
  195. QByteArray ranges = reply()->rawHeader("Content-Range");
  196. if (!ranges.isEmpty()) {
  197. QRegExp rx("bytes (\\d+)-");
  198. if (rx.indexIn(ranges) >= 0) {
  199. start = rx.cap(1).toLongLong();
  200. }
  201. }
  202. if (start != _resumeStart) {
  203. qCWarning(lcGetJob) << "Wrong content-range: " << ranges << " while expecting start was" << _resumeStart;
  204. if (ranges.isEmpty()) {
  205. // device doesn't support range, just try again from scratch
  206. _device->close();
  207. if (!_device->open(QIODevice::WriteOnly)) {
  208. _errorString = _device->errorString();
  209. _errorStatus = SyncFileItem::NormalError;
  210. reply()->abort();
  211. return;
  212. }
  213. _resumeStart = 0;
  214. } else {
  215. _errorString = tr("Server returned wrong content-range");
  216. _errorStatus = SyncFileItem::NormalError;
  217. reply()->abort();
  218. return;
  219. }
  220. }
  221. auto lastModified = reply()->header(QNetworkRequest::LastModifiedHeader);
  222. if (!lastModified.isNull()) {
  223. _lastModified = Utility::qDateTimeToTime_t(lastModified.toDateTime());
  224. }
  225. _saveBodyToFile = true;
  226. }
  227. void GETFileJob::setBandwidthManager(BandwidthManager *bwm)
  228. {
  229. _bandwidthManager = bwm;
  230. }
  231. void GETFileJob::setChoked(bool c)
  232. {
  233. _bandwidthChoked = c;
  234. QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
  235. }
  236. void GETFileJob::setBandwidthLimited(bool b)
  237. {
  238. _bandwidthLimited = b;
  239. QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
  240. }
  241. void GETFileJob::giveBandwidthQuota(qint64 q)
  242. {
  243. _bandwidthQuota = q;
  244. qCDebug(lcGetJob) << "Got" << q << "bytes";
  245. QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
  246. }
  247. qint64 GETFileJob::currentDownloadPosition()
  248. {
  249. if (_device && _device->pos() > 0 && _device->pos() > qint64(_resumeStart)) {
  250. return _device->pos();
  251. }
  252. return _resumeStart;
  253. }
  254. void GETFileJob::slotReadyRead()
  255. {
  256. if (!reply())
  257. return;
  258. int bufferSize = qMin(1024 * 8ll, reply()->bytesAvailable());
  259. QByteArray buffer(bufferSize, Qt::Uninitialized);
  260. while (reply()->bytesAvailable() > 0 && _saveBodyToFile) {
  261. if (_bandwidthChoked) {
  262. qCWarning(lcGetJob) << "Download choked";
  263. break;
  264. }
  265. qint64 toRead = bufferSize;
  266. if (_bandwidthLimited) {
  267. toRead = qMin(qint64(bufferSize), _bandwidthQuota);
  268. if (toRead == 0) {
  269. qCWarning(lcGetJob) << "Out of quota";
  270. break;
  271. }
  272. _bandwidthQuota -= toRead;
  273. }
  274. qint64 r = reply()->read(buffer.data(), toRead);
  275. if (r < 0) {
  276. _errorString = networkReplyErrorString(*reply());
  277. _errorStatus = SyncFileItem::NormalError;
  278. qCWarning(lcGetJob) << "Error while reading from device: " << _errorString;
  279. reply()->abort();
  280. return;
  281. }
  282. qint64 w = _device->write(buffer.constData(), r);
  283. if (w != r) {
  284. _errorString = _device->errorString();
  285. _errorStatus = SyncFileItem::NormalError;
  286. qCWarning(lcGetJob) << "Error while writing to file" << w << r << _errorString;
  287. reply()->abort();
  288. return;
  289. }
  290. }
  291. if (reply()->isFinished() && (reply()->bytesAvailable() == 0 || !_saveBodyToFile)) {
  292. qCDebug(lcGetJob) << "Actually finished!";
  293. if (_bandwidthManager) {
  294. _bandwidthManager->unregisterDownloadJob(this);
  295. }
  296. if (!_hasEmittedFinishedSignal) {
  297. qCInfo(lcGetJob) << "GET of" << reply()->request().url().toString() << "FINISHED WITH STATUS"
  298. << replyStatusString()
  299. << reply()->rawHeader("Content-Range") << reply()->rawHeader("Content-Length");
  300. emit finishedSignal();
  301. }
  302. _hasEmittedFinishedSignal = true;
  303. deleteLater();
  304. }
  305. }
  306. void GETFileJob::cancel()
  307. {
  308. if (reply()->isRunning()) {
  309. reply()->abort();
  310. }
  311. emit canceled();
  312. }
  313. void GETFileJob::onTimedOut()
  314. {
  315. qCWarning(lcGetJob) << "Timeout" << (reply() ? reply()->request().url() : path());
  316. if (!reply())
  317. return;
  318. _errorString = tr("Connection Timeout");
  319. _errorStatus = SyncFileItem::FatalError;
  320. reply()->abort();
  321. }
  322. QString GETFileJob::errorString() const
  323. {
  324. if (!_errorString.isEmpty()) {
  325. return _errorString;
  326. }
  327. return AbstractNetworkJob::errorString();
  328. }
  329. void PropagateDownloadFile::start()
  330. {
  331. if (propagator()->_abortRequested)
  332. return;
  333. _isEncrypted = false;
  334. qCDebug(lcPropagateDownload) << _item->_file << propagator()->_activeJobList.count();
  335. const auto path = _item->_file;
  336. const auto slashPosition = path.lastIndexOf('/');
  337. const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString();
  338. SyncJournalFileRecord parentRec;
  339. propagator()->_journal->getFileRecord(parentPath, &parentRec);
  340. const auto account = propagator()->account();
  341. if (!account->capabilities().clientSideEncryptionAvailable() ||
  342. !parentRec.isValid() ||
  343. !parentRec._isE2eEncrypted) {
  344. startAfterIsEncryptedIsChecked();
  345. } else {
  346. _downloadEncryptedHelper = new PropagateDownloadEncrypted(propagator(), parentPath, _item, this);
  347. connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::fileMetadataFound, [this] {
  348. _isEncrypted = true;
  349. startAfterIsEncryptedIsChecked();
  350. });
  351. connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::failed, [this] {
  352. done(SyncFileItem::NormalError,
  353. tr("File %1 cannot be downloaded because encryption information is missing.").arg(QDir::toNativeSeparators(_item->_file)));
  354. });
  355. _downloadEncryptedHelper->start();
  356. }
  357. }
  358. void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
  359. {
  360. _stopwatch.start();
  361. auto &syncOptions = propagator()->syncOptions();
  362. auto &vfs = syncOptions._vfs;
  363. // For virtual files just dehydrate or create the file and be done
  364. if (_item->_type == ItemTypeVirtualFileDehydration) {
  365. QString fsPath = propagator()->fullLocalPath(_item->_file);
  366. if (!FileSystem::verifyFileUnchanged(fsPath, _item->_previousSize, _item->_previousModtime)) {
  367. propagator()->_anotherSyncNeeded = true;
  368. done(SyncFileItem::SoftError, tr("File has changed since discovery"));
  369. return;
  370. }
  371. qCDebug(lcPropagateDownload) << "dehydrating file" << _item->_file;
  372. auto r = vfs->dehydratePlaceholder(*_item);
  373. if (!r) {
  374. done(SyncFileItem::NormalError, r.error());
  375. return;
  376. }
  377. propagator()->_journal->deleteFileRecord(_item->_originalFile);
  378. updateMetadata(false);
  379. if (!_item->_remotePerm.isNull() && !_item->_remotePerm.hasPermission(RemotePermissions::CanWrite)) {
  380. // make sure ReadOnly flag is preserved for placeholder, similarly to regular files
  381. FileSystem::setFileReadOnly(propagator()->fullLocalPath(_item->_file), true);
  382. }
  383. return;
  384. }
  385. if (vfs->mode() == Vfs::Off && _item->_type == ItemTypeVirtualFile) {
  386. qCWarning(lcPropagateDownload) << "ignored virtual file type of" << _item->_file;
  387. _item->_type = ItemTypeFile;
  388. }
  389. if (_item->_type == ItemTypeVirtualFile) {
  390. if (propagator()->localFileNameClash(_item->_file)) {
  391. done(SyncFileItem::NormalError, tr("File %1 cannot be downloaded because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
  392. return;
  393. }
  394. qCDebug(lcPropagateDownload) << "creating virtual file" << _item->_file;
  395. auto r = vfs->createPlaceholder(*_item);
  396. if (!r) {
  397. done(SyncFileItem::NormalError, r.error());
  398. return;
  399. }
  400. updateMetadata(false);
  401. if (!_item->_remotePerm.isNull() && !_item->_remotePerm.hasPermission(RemotePermissions::CanWrite)) {
  402. // make sure ReadOnly flag is preserved for placeholder, similarly to regular files
  403. FileSystem::setFileReadOnly(propagator()->fullLocalPath(_item->_file), true);
  404. }
  405. return;
  406. }
  407. if (_deleteExisting) {
  408. deleteExistingFolder();
  409. // check for error with deletion
  410. if (_state == Finished) {
  411. return;
  412. }
  413. }
  414. // If we have a conflict where size of the file is unchanged,
  415. // compare the remote checksum to the local one.
  416. // Maybe it's not a real conflict and no download is necessary!
  417. // If the hashes are collision safe and identical, we assume the content is too.
  418. // For weak checksums, we only do that if the mtimes are also identical.
  419. const auto csync_is_collision_safe_hash = [](const QByteArray &checksum_header)
  420. {
  421. return checksum_header.startsWith("SHA")
  422. || checksum_header.startsWith("MD5:");
  423. };
  424. if (_item->_instruction == CSYNC_INSTRUCTION_CONFLICT
  425. && _item->_size == _item->_previousSize
  426. && !_item->_checksumHeader.isEmpty()
  427. && (csync_is_collision_safe_hash(_item->_checksumHeader)
  428. || _item->_modtime == _item->_previousModtime)) {
  429. qCDebug(lcPropagateDownload) << _item->_file << "may not need download, computing checksum";
  430. auto computeChecksum = new ComputeChecksum(this);
  431. computeChecksum->setChecksumType(parseChecksumHeaderType(_item->_checksumHeader));
  432. connect(computeChecksum, &ComputeChecksum::done,
  433. this, &PropagateDownloadFile::conflictChecksumComputed);
  434. propagator()->_activeJobList.append(this);
  435. computeChecksum->start(propagator()->fullLocalPath(_item->_file));
  436. return;
  437. }
  438. startDownload();
  439. }
  440. void PropagateDownloadFile::conflictChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
  441. {
  442. propagator()->_activeJobList.removeOne(this);
  443. if (makeChecksumHeader(checksumType, checksum) == _item->_checksumHeader) {
  444. // No download necessary, just update fs and journal metadata
  445. qCDebug(lcPropagateDownload) << _item->_file << "remote and local checksum match";
  446. // Apply the server mtime locally if necessary, ensuring the journal
  447. // and local mtimes end up identical
  448. auto fn = propagator()->fullLocalPath(_item->_file);
  449. if (_item->_modtime != _item->_previousModtime) {
  450. FileSystem::setModTime(fn, _item->_modtime);
  451. emit propagator()->touchedFile(fn);
  452. }
  453. _item->_modtime = FileSystem::getModTime(fn);
  454. updateMetadata(/*isConflict=*/false);
  455. return;
  456. }
  457. startDownload();
  458. }
  459. void PropagateDownloadFile::startDownload()
  460. {
  461. if (propagator()->_abortRequested)
  462. return;
  463. // do a klaas' case clash check.
  464. if (propagator()->localFileNameClash(_item->_file)) {
  465. done(SyncFileItem::NormalError, tr("File %1 cannot be downloaded because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
  466. return;
  467. }
  468. propagator()->reportProgress(*_item, 0);
  469. QString tmpFileName;
  470. QByteArray expectedEtagForResume;
  471. const SyncJournalDb::DownloadInfo progressInfo = propagator()->_journal->getDownloadInfo(_item->_file);
  472. if (progressInfo._valid) {
  473. // if the etag has changed meanwhile, remove the already downloaded part.
  474. if (progressInfo._etag != _item->_etag) {
  475. FileSystem::remove(propagator()->fullLocalPath(progressInfo._tmpfile));
  476. propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
  477. } else {
  478. tmpFileName = progressInfo._tmpfile;
  479. expectedEtagForResume = progressInfo._etag;
  480. }
  481. }
  482. if (tmpFileName.isEmpty()) {
  483. tmpFileName = createDownloadTmpFileName(_item->_file);
  484. }
  485. _tmpFile.setFileName(propagator()->fullLocalPath(tmpFileName));
  486. _resumeStart = _tmpFile.size();
  487. if (_resumeStart > 0 && _resumeStart == _item->_size) {
  488. qCInfo(lcPropagateDownload) << "File is already complete, no need to download";
  489. downloadFinished();
  490. return;
  491. }
  492. // Can't open(Append) read-only files, make sure to make
  493. // file writable if it exists.
  494. if (_tmpFile.exists())
  495. FileSystem::setFileReadOnly(_tmpFile.fileName(), false);
  496. if (!_tmpFile.open(QIODevice::Append | QIODevice::Unbuffered)) {
  497. qCWarning(lcPropagateDownload) << "could not open temporary file" << _tmpFile.fileName();
  498. done(SyncFileItem::NormalError, _tmpFile.errorString());
  499. return;
  500. }
  501. // Hide temporary after creation
  502. FileSystem::setFileHidden(_tmpFile.fileName(), true);
  503. // If there's not enough space to fully download this file, stop.
  504. const auto diskSpaceResult = propagator()->diskSpaceCheck();
  505. if (diskSpaceResult != OwncloudPropagator::DiskSpaceOk) {
  506. if (diskSpaceResult == OwncloudPropagator::DiskSpaceFailure) {
  507. // Using DetailError here will make the error not pop up in the account
  508. // tab: instead we'll generate a general "disk space low" message and show
  509. // these detail errors only in the error view.
  510. done(SyncFileItem::DetailError,
  511. tr("The download would reduce free local disk space below the limit"));
  512. emit propagator()->insufficientLocalStorage();
  513. } else if (diskSpaceResult == OwncloudPropagator::DiskSpaceCritical) {
  514. done(SyncFileItem::FatalError,
  515. tr("Free space on disk is less than %1").arg(Utility::octetsToString(criticalFreeSpaceLimit())));
  516. }
  517. // Remove the temporary, if empty.
  518. if (_resumeStart == 0) {
  519. _tmpFile.remove();
  520. }
  521. return;
  522. }
  523. {
  524. SyncJournalDb::DownloadInfo pi;
  525. pi._etag = _item->_etag;
  526. pi._tmpfile = tmpFileName;
  527. pi._valid = true;
  528. propagator()->_journal->setDownloadInfo(_item->_file, pi);
  529. propagator()->_journal->commit("download file start");
  530. }
  531. QMap<QByteArray, QByteArray> headers;
  532. if (_item->_directDownloadUrl.isEmpty()) {
  533. // Normal job, download from oC instance
  534. _job = new GETFileJob(propagator()->account(),
  535. propagator()->fullRemotePath(_isEncrypted ? _item->_encryptedFileName : _item->_file),
  536. &_tmpFile, headers, expectedEtagForResume, _resumeStart, this);
  537. } else {
  538. // We were provided a direct URL, use that one
  539. qCInfo(lcPropagateDownload) << "directDownloadUrl given for " << _item->_file << _item->_directDownloadUrl;
  540. if (!_item->_directDownloadCookies.isEmpty()) {
  541. headers["Cookie"] = _item->_directDownloadCookies.toUtf8();
  542. }
  543. QUrl url = QUrl::fromUserInput(_item->_directDownloadUrl);
  544. _job = new GETFileJob(propagator()->account(),
  545. url,
  546. &_tmpFile, headers, expectedEtagForResume, _resumeStart, this);
  547. }
  548. _job->setBandwidthManager(&propagator()->_bandwidthManager);
  549. connect(_job.data(), &GETFileJob::finishedSignal, this, &PropagateDownloadFile::slotGetFinished);
  550. connect(_job.data(), &GETFileJob::downloadProgress, this, &PropagateDownloadFile::slotDownloadProgress);
  551. propagator()->_activeJobList.append(this);
  552. _job->start();
  553. }
  554. qint64 PropagateDownloadFile::committedDiskSpace() const
  555. {
  556. if (_state == Running) {
  557. return qBound(0LL, _item->_size - _resumeStart - _downloadProgress, _item->_size);
  558. }
  559. return 0;
  560. }
  561. void PropagateDownloadFile::setDeleteExistingFolder(bool enabled)
  562. {
  563. _deleteExisting = enabled;
  564. }
  565. const char owncloudCustomSoftErrorStringC[] = "owncloud-custom-soft-error-string";
  566. void PropagateDownloadFile::slotGetFinished()
  567. {
  568. propagator()->_activeJobList.removeOne(this);
  569. GETFileJob *job = _job;
  570. ASSERT(job);
  571. _item->_httpErrorCode = job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  572. _item->_requestId = job->requestId();
  573. QNetworkReply::NetworkError err = job->reply()->error();
  574. if (err != QNetworkReply::NoError) {
  575. // If we sent a 'Range' header and get 416 back, we want to retry
  576. // without the header.
  577. const bool badRangeHeader = job->resumeStart() > 0 && _item->_httpErrorCode == 416;
  578. if (badRangeHeader) {
  579. qCWarning(lcPropagateDownload) << "server replied 416 to our range request, trying again without";
  580. propagator()->_anotherSyncNeeded = true;
  581. }
  582. // Getting a 404 probably means that the file was deleted on the server.
  583. const bool fileNotFound = _item->_httpErrorCode == 404;
  584. if (fileNotFound) {
  585. qCWarning(lcPropagateDownload) << "server replied 404, assuming file was deleted";
  586. }
  587. // Getting a 423 means that the file is locked
  588. const bool fileLocked = _item->_httpErrorCode == 423;
  589. if (fileLocked) {
  590. qCWarning(lcPropagateDownload) << "server replied 423, file is Locked";
  591. }
  592. // Don't keep the temporary file if it is empty or we
  593. // used a bad range header or the file's not on the server anymore.
  594. if (_tmpFile.exists() && (_tmpFile.size() == 0 || badRangeHeader || fileNotFound)) {
  595. _tmpFile.close();
  596. FileSystem::remove(_tmpFile.fileName());
  597. propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
  598. }
  599. if (!_item->_directDownloadUrl.isEmpty() && err != QNetworkReply::OperationCanceledError) {
  600. // If this was with a direct download, retry without direct download
  601. qCWarning(lcPropagateDownload) << "Direct download of" << _item->_directDownloadUrl << "failed. Retrying through owncloud.";
  602. _item->_directDownloadUrl.clear();
  603. start();
  604. return;
  605. }
  606. // This gives a custom QNAM (by the user of libowncloudsync) to abort() a QNetworkReply in its metaDataChanged() slot and
  607. // set a custom error string to make this a soft error. In contrast to the default hard error this won't bring down
  608. // the whole sync and allows for a custom error message.
  609. QNetworkReply *reply = job->reply();
  610. if (err == QNetworkReply::OperationCanceledError && reply->property(owncloudCustomSoftErrorStringC).isValid()) {
  611. job->setErrorString(reply->property(owncloudCustomSoftErrorStringC).toString());
  612. job->setErrorStatus(SyncFileItem::SoftError);
  613. } else if (badRangeHeader) {
  614. // Can't do this in classifyError() because 416 without a
  615. // Range header should result in NormalError.
  616. job->setErrorStatus(SyncFileItem::SoftError);
  617. } else if (fileNotFound) {
  618. job->setErrorString(tr("File was deleted from server"));
  619. job->setErrorStatus(SyncFileItem::SoftError);
  620. // As a precaution against bugs that cause our database and the
  621. // reality on the server to diverge, rediscover this folder on the
  622. // next sync run.
  623. propagator()->_journal->schedulePathForRemoteDiscovery(_item->_file);
  624. }
  625. QByteArray errorBody;
  626. QString errorString = _item->_httpErrorCode >= 400 ? job->errorStringParsingBody(&errorBody)
  627. : job->errorString();
  628. SyncFileItem::Status status = job->errorStatus();
  629. if (status == SyncFileItem::NoStatus) {
  630. status = classifyError(err, _item->_httpErrorCode,
  631. &propagator()->_anotherSyncNeeded, errorBody);
  632. }
  633. done(status, errorString);
  634. return;
  635. }
  636. _item->_responseTimeStamp = job->responseTimestamp();
  637. if (!job->etag().isEmpty()) {
  638. // The etag will be empty if we used a direct download URL.
  639. // (If it was really empty by the server, the GETFileJob will have errored
  640. _item->_etag = parseEtag(job->etag());
  641. }
  642. if (job->lastModified()) {
  643. // It is possible that the file was modified on the server since we did the discovery phase
  644. // so make sure we have the up-to-date time
  645. _item->_modtime = job->lastModified();
  646. }
  647. _tmpFile.close();
  648. _tmpFile.flush();
  649. /* Check that the size of the GET reply matches the file size. There have been cases
  650. * reported that if a server breaks behind a proxy, the GET is still a 200 but is
  651. * truncated, as described here: https://github.com/owncloud/mirall/issues/2528
  652. */
  653. const QByteArray sizeHeader("Content-Length");
  654. qint64 bodySize = job->reply()->rawHeader(sizeHeader).toLongLong();
  655. bool hasSizeHeader = !job->reply()->rawHeader(sizeHeader).isEmpty();
  656. // Qt removes the content-length header for transparently decompressed HTTP1 replies
  657. // but not for HTTP2 or SPDY replies. For these it remains and contains the size
  658. // of the compressed data. See QTBUG-73364.
  659. const auto contentEncoding = job->reply()->rawHeader("content-encoding").toLower();
  660. if ((contentEncoding == "gzip" || contentEncoding == "deflate")
  661. && (job->reply()->attribute(QNetworkRequest::HTTP2WasUsedAttribute).toBool()
  662. || job->reply()->attribute(QNetworkRequest::SpdyWasUsedAttribute).toBool())) {
  663. bodySize = 0;
  664. hasSizeHeader = false;
  665. }
  666. if (hasSizeHeader && _tmpFile.size() > 0 && bodySize == 0) {
  667. // Strange bug with broken webserver or webfirewall https://github.com/owncloud/client/issues/3373#issuecomment-122672322
  668. // This happened when trying to resume a file. The Content-Range header was files, Content-Length was == 0
  669. qCDebug(lcPropagateDownload) << bodySize << _item->_size << _tmpFile.size() << job->resumeStart();
  670. FileSystem::remove(_tmpFile.fileName());
  671. done(SyncFileItem::SoftError, QLatin1String("Broken webserver returning empty content length for non-empty file on resume"));
  672. return;
  673. }
  674. if (bodySize > 0 && bodySize != _tmpFile.size() - job->resumeStart()) {
  675. qCDebug(lcPropagateDownload) << bodySize << _tmpFile.size() << job->resumeStart();
  676. propagator()->_anotherSyncNeeded = true;
  677. done(SyncFileItem::SoftError, tr("The file could not be downloaded completely."));
  678. return;
  679. }
  680. if (_tmpFile.size() == 0 && _item->_size > 0) {
  681. FileSystem::remove(_tmpFile.fileName());
  682. done(SyncFileItem::NormalError,
  683. tr("The downloaded file is empty, but the server said it should have been %1.")
  684. .arg(Utility::octetsToString(_item->_size)));
  685. return;
  686. }
  687. // Did the file come with conflict headers? If so, store them now!
  688. // If we download conflict files but the server doesn't send conflict
  689. // headers, the record will be established by SyncEngine::conflictRecordMaintenance.
  690. // (we can't reliably determine the file id of the base file here,
  691. // it might still be downloaded in a parallel job and not exist in
  692. // the database yet!)
  693. if (job->reply()->rawHeader("OC-Conflict") == "1") {
  694. _conflictRecord.path = _item->_file.toUtf8();
  695. _conflictRecord.initialBasePath = job->reply()->rawHeader("OC-ConflictInitialBasePath");
  696. _conflictRecord.baseFileId = job->reply()->rawHeader("OC-ConflictBaseFileId");
  697. _conflictRecord.baseEtag = job->reply()->rawHeader("OC-ConflictBaseEtag");
  698. auto mtimeHeader = job->reply()->rawHeader("OC-ConflictBaseMtime");
  699. if (!mtimeHeader.isEmpty())
  700. _conflictRecord.baseModtime = mtimeHeader.toLongLong();
  701. // We don't set it yet. That will only be done when the download finished
  702. // successfully, much further down. Here we just grab the headers because the
  703. // job will be deleted later.
  704. }
  705. // Do checksum validation for the download. If there is no checksum header, the validator
  706. // will also emit the validated() signal to continue the flow in slot transmissionChecksumValidated()
  707. // as this is (still) also correct.
  708. auto *validator = new ValidateChecksumHeader(this);
  709. connect(validator, &ValidateChecksumHeader::validated,
  710. this, &PropagateDownloadFile::transmissionChecksumValidated);
  711. connect(validator, &ValidateChecksumHeader::validationFailed,
  712. this, &PropagateDownloadFile::slotChecksumFail);
  713. auto checksumHeader = findBestChecksum(job->reply()->rawHeader(checkSumHeaderC));
  714. auto contentMd5Header = job->reply()->rawHeader(contentMd5HeaderC);
  715. if (checksumHeader.isEmpty() && !contentMd5Header.isEmpty())
  716. checksumHeader = "MD5:" + contentMd5Header;
  717. validator->start(_tmpFile.fileName(), checksumHeader);
  718. }
  719. void PropagateDownloadFile::slotChecksumFail(const QString &errMsg)
  720. {
  721. FileSystem::remove(_tmpFile.fileName());
  722. propagator()->_anotherSyncNeeded = true;
  723. done(SyncFileItem::SoftError, errMsg); // tr("The file downloaded with a broken checksum, will be redownloaded."));
  724. }
  725. void PropagateDownloadFile::deleteExistingFolder()
  726. {
  727. QString existingDir = propagator()->fullLocalPath(_item->_file);
  728. if (!QFileInfo(existingDir).isDir()) {
  729. return;
  730. }
  731. // Delete the directory if it is empty!
  732. QDir dir(existingDir);
  733. if (dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries).count() == 0) {
  734. if (dir.rmdir(existingDir)) {
  735. return;
  736. }
  737. // on error, just try to move it away...
  738. }
  739. QString error;
  740. if (!propagator()->createConflict(_item, _associatedComposite, &error)) {
  741. done(SyncFileItem::NormalError, error);
  742. }
  743. }
  744. namespace { // Anonymous namespace for the recall feature
  745. static QString makeRecallFileName(const QString &fn)
  746. {
  747. QString recallFileName(fn);
  748. // Add _recall-XXXX before the extension.
  749. int dotLocation = recallFileName.lastIndexOf('.');
  750. // If no extension, add it at the end (take care of cases like foo/.hidden or foo.bar/file)
  751. if (dotLocation <= recallFileName.lastIndexOf('/') + 1) {
  752. dotLocation = recallFileName.size();
  753. }
  754. QString timeString = QDateTime::currentDateTimeUtc().toString("yyyyMMdd-hhmmss");
  755. recallFileName.insert(dotLocation, "_.sys.admin#recall#-" + timeString);
  756. return recallFileName;
  757. }
  758. void handleRecallFile(const QString &filePath, const QString &folderPath, SyncJournalDb &journal)
  759. {
  760. qCDebug(lcPropagateDownload) << "handleRecallFile: " << filePath;
  761. FileSystem::setFileHidden(filePath, true);
  762. QFile file(filePath);
  763. if (!file.open(QIODevice::ReadOnly)) {
  764. qCWarning(lcPropagateDownload) << "Could not open recall file" << file.errorString();
  765. return;
  766. }
  767. QFileInfo existingFile(filePath);
  768. QDir baseDir = existingFile.dir();
  769. while (!file.atEnd()) {
  770. QByteArray line = file.readLine();
  771. line.chop(1); // remove trailing \n
  772. QString recalledFile = QDir::cleanPath(baseDir.filePath(line));
  773. if (!recalledFile.startsWith(folderPath) || !recalledFile.startsWith(baseDir.path())) {
  774. qCWarning(lcPropagateDownload) << "Ignoring recall of " << recalledFile;
  775. continue;
  776. }
  777. // Path of the recalled file in the local folder
  778. QString localRecalledFile = recalledFile.mid(folderPath.size());
  779. SyncJournalFileRecord record;
  780. if (!journal.getFileRecord(localRecalledFile, &record) || !record.isValid()) {
  781. qCWarning(lcPropagateDownload) << "No db entry for recall of" << localRecalledFile;
  782. continue;
  783. }
  784. qCInfo(lcPropagateDownload) << "Recalling" << localRecalledFile << "Checksum:" << record._checksumHeader;
  785. QString targetPath = makeRecallFileName(recalledFile);
  786. qCDebug(lcPropagateDownload) << "Copy recall file: " << recalledFile << " -> " << targetPath;
  787. // Remove the target first, QFile::copy will not overwrite it.
  788. FileSystem::remove(targetPath);
  789. QFile::copy(recalledFile, targetPath);
  790. }
  791. }
  792. static void preserveGroupOwnership(const QString &fileName, const QFileInfo &fi)
  793. {
  794. #ifdef Q_OS_UNIX
  795. int chownErr = chown(fileName.toLocal8Bit().constData(), -1, fi.groupId());
  796. if (chownErr) {
  797. // TODO: Consider further error handling!
  798. qCWarning(lcPropagateDownload) << QString("preserveGroupOwnership: chown error %1: setting group %2 failed on file %3").arg(chownErr).arg(fi.groupId()).arg(fileName);
  799. }
  800. #else
  801. Q_UNUSED(fileName);
  802. Q_UNUSED(fi);
  803. #endif
  804. }
  805. } // end namespace
  806. void PropagateDownloadFile::transmissionChecksumValidated(const QByteArray &checksumType, const QByteArray &checksum)
  807. {
  808. const QByteArray theContentChecksumType = propagator()->account()->capabilities().preferredUploadChecksumType();
  809. // Reuse transmission checksum as content checksum.
  810. //
  811. // We could do this more aggressively and accept both MD5 and SHA1
  812. // instead of insisting on the exactly correct checksum type.
  813. if (theContentChecksumType == checksumType || theContentChecksumType.isEmpty()) {
  814. return contentChecksumComputed(checksumType, checksum);
  815. }
  816. // Compute the content checksum.
  817. auto computeChecksum = new ComputeChecksum(this);
  818. computeChecksum->setChecksumType(theContentChecksumType);
  819. connect(computeChecksum, &ComputeChecksum::done,
  820. this, &PropagateDownloadFile::contentChecksumComputed);
  821. computeChecksum->start(_tmpFile.fileName());
  822. }
  823. void PropagateDownloadFile::contentChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
  824. {
  825. _item->_checksumHeader = makeChecksumHeader(checksumType, checksum);
  826. if (_isEncrypted) {
  827. if (_downloadEncryptedHelper->decryptFile(_tmpFile)) {
  828. downloadFinished();
  829. } else {
  830. done(SyncFileItem::NormalError, _downloadEncryptedHelper->errorString());
  831. }
  832. } else {
  833. downloadFinished();
  834. }
  835. }
  836. void PropagateDownloadFile::downloadFinished()
  837. {
  838. ASSERT(!_tmpFile.isOpen());
  839. QString fn = propagator()->fullLocalPath(_item->_file);
  840. // In case of file name clash, report an error
  841. // This can happen if another parallel download saved a clashing file.
  842. if (propagator()->localFileNameClash(_item->_file)) {
  843. done(SyncFileItem::NormalError, tr("File %1 cannot be saved because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
  844. return;
  845. }
  846. FileSystem::setModTime(_tmpFile.fileName(), _item->_modtime);
  847. // We need to fetch the time again because some file systems such as FAT have worse than a second
  848. // Accuracy, and we really need the time from the file system. (#3103)
  849. _item->_modtime = FileSystem::getModTime(_tmpFile.fileName());
  850. bool previousFileExists = FileSystem::fileExists(fn);
  851. if (previousFileExists) {
  852. // Preserve the existing file permissions.
  853. QFileInfo existingFile(fn);
  854. if (existingFile.permissions() != _tmpFile.permissions()) {
  855. _tmpFile.setPermissions(existingFile.permissions());
  856. }
  857. preserveGroupOwnership(_tmpFile.fileName(), existingFile);
  858. // Make the file a hydrated placeholder if possible
  859. const auto result = propagator()->syncOptions()._vfs->convertToPlaceholder(_tmpFile.fileName(), *_item, fn);
  860. if (!result) {
  861. done(SyncFileItem::NormalError, result.error());
  862. return;
  863. }
  864. }
  865. // Apply the remote permissions
  866. FileSystem::setFileReadOnlyWeak(_tmpFile.fileName(), !_item->_remotePerm.isNull() && !_item->_remotePerm.hasPermission(RemotePermissions::CanWrite));
  867. bool isConflict = _item->_instruction == CSYNC_INSTRUCTION_CONFLICT
  868. && (QFileInfo(fn).isDir() || !FileSystem::fileEquals(fn, _tmpFile.fileName()));
  869. if (isConflict) {
  870. QString error;
  871. if (!propagator()->createConflict(_item, _associatedComposite, &error)) {
  872. done(SyncFileItem::SoftError, error);
  873. return;
  874. }
  875. previousFileExists = false;
  876. }
  877. const auto vfs = propagator()->syncOptions()._vfs;
  878. // In the case of an hydration, this size is likely to change for placeholders
  879. // (except with the cfapi backend)
  880. const auto isVirtualDownload = _item->_type == ItemTypeVirtualFileDownload;
  881. const auto isCfApiVfs = vfs && vfs->mode() == Vfs::WindowsCfApi;
  882. if (previousFileExists && (isCfApiVfs || !isVirtualDownload)) {
  883. // Check whether the existing file has changed since the discovery
  884. // phase by comparing size and mtime to the previous values. This
  885. // is necessary to avoid overwriting user changes that happened between
  886. // the discovery phase and now.
  887. const qint64 expectedSize = _item->_previousSize;
  888. const time_t expectedMtime = _item->_previousModtime;
  889. if (!FileSystem::verifyFileUnchanged(fn, expectedSize, expectedMtime)) {
  890. propagator()->_anotherSyncNeeded = true;
  891. done(SyncFileItem::SoftError, tr("File has changed since discovery"));
  892. return;
  893. }
  894. }
  895. QString error;
  896. emit propagator()->touchedFile(fn);
  897. // The fileChanged() check is done above to generate better error messages.
  898. if (!FileSystem::uncheckedRenameReplace(_tmpFile.fileName(), fn, &error)) {
  899. qCWarning(lcPropagateDownload) << QString("Rename failed: %1 => %2").arg(_tmpFile.fileName()).arg(fn);
  900. // If the file is locked, we want to retry this sync when it
  901. // becomes available again, otherwise try again directly
  902. if (FileSystem::isFileLocked(fn)) {
  903. emit propagator()->seenLockedFile(fn);
  904. } else {
  905. propagator()->_anotherSyncNeeded = true;
  906. }
  907. done(SyncFileItem::SoftError, error);
  908. return;
  909. }
  910. FileSystem::setFileHidden(fn, false);
  911. // Maybe we downloaded a newer version of the file than we thought we would...
  912. // Get up to date information for the journal.
  913. _item->_size = FileSystem::getSize(fn);
  914. // Maybe what we downloaded was a conflict file? If so, set a conflict record.
  915. // (the data was prepared in slotGetFinished above)
  916. if (_conflictRecord.isValid())
  917. propagator()->_journal->setConflictRecord(_conflictRecord);
  918. if (vfs && vfs->mode() == Vfs::WithSuffix) {
  919. // If the virtual file used to have a different name and db
  920. // entry, remove it transfer its old pin state.
  921. if (_item->_type == ItemTypeVirtualFileDownload) {
  922. QString virtualFile = _item->_file + vfs->fileSuffix();
  923. auto fn = propagator()->fullLocalPath(virtualFile);
  924. qCDebug(lcPropagateDownload) << "Download of previous virtual file finished" << fn;
  925. QFile::remove(fn);
  926. propagator()->_journal->deleteFileRecord(virtualFile);
  927. // Move the pin state to the new location
  928. auto pin = propagator()->_journal->internalPinStates().rawForPath(virtualFile.toUtf8());
  929. if (pin && *pin != PinState::Inherited) {
  930. vfs->setPinState(_item->_file, *pin);
  931. vfs->setPinState(virtualFile, PinState::Inherited);
  932. }
  933. }
  934. // Ensure the pin state isn't contradictory
  935. auto pin = vfs->pinState(_item->_file);
  936. if (pin && *pin == PinState::OnlineOnly)
  937. vfs->setPinState(_item->_file, PinState::Unspecified);
  938. }
  939. updateMetadata(isConflict);
  940. }
  941. void PropagateDownloadFile::updateMetadata(bool isConflict)
  942. {
  943. QString fn = propagator()->fullLocalPath(_item->_file);
  944. if (!propagator()->updateMetadata(*_item)) {
  945. done(SyncFileItem::FatalError, tr("Error writing metadata to the database"));
  946. return;
  947. }
  948. if (_isEncrypted) {
  949. propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
  950. } else {
  951. propagator()->_journal->setDownloadInfo(_item->_encryptedFileName, SyncJournalDb::DownloadInfo());
  952. }
  953. propagator()->_journal->commit("download file start2");
  954. done(isConflict ? SyncFileItem::Conflict : SyncFileItem::Success);
  955. // handle the special recall file
  956. if (!_item->_remotePerm.hasPermission(RemotePermissions::IsShared)
  957. && (_item->_file == QLatin1String(".sys.admin#recall#")
  958. || _item->_file.endsWith(QLatin1String("/.sys.admin#recall#")))) {
  959. handleRecallFile(fn, propagator()->localPath(), *propagator()->_journal);
  960. }
  961. qint64 duration = _stopwatch.elapsed();
  962. if (isLikelyFinishedQuickly() && duration > 5 * 1000) {
  963. qCWarning(lcPropagateDownload) << "WARNING: Unexpectedly slow connection, took" << duration << "msec for" << _item->_size - _resumeStart << "bytes for" << _item->_file;
  964. }
  965. }
  966. void PropagateDownloadFile::slotDownloadProgress(qint64 received, qint64)
  967. {
  968. if (!_job)
  969. return;
  970. _downloadProgress = received;
  971. propagator()->reportProgress(*_item, _resumeStart + received);
  972. }
  973. void PropagateDownloadFile::abort(PropagatorJob::AbortType abortType)
  974. {
  975. if (_job && _job->reply())
  976. _job->reply()->abort();
  977. if (abortType == AbortType::Asynchronous) {
  978. emit abortFinished();
  979. }
  980. }
  981. }