2014-02-06 14:50:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2015-05-15 11:50:55 +03:00
|
|
|
#include "config.h"
|
2014-11-11 14:16:14 +03:00
|
|
|
#include "owncloudpropagator_p.h"
|
|
|
|
#include "propagatedownload.h"
|
2014-02-06 14:50:16 +04:00
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "account.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/syncjournaldb.h"
|
|
|
|
#include "common/syncjournalfilerecord.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2014-02-18 15:54:40 +04:00
|
|
|
#include "filesystem.h"
|
2014-04-29 18:47:07 +04:00
|
|
|
#include "propagatorjobs.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/checksums.h"
|
|
|
|
#include "common/asserts.h"
|
2018-01-05 23:37:31 +03:00
|
|
|
#include "clientsideencryptionjobs.h"
|
2018-01-21 23:25:06 +03:00
|
|
|
#include "propagatedownloadencrypted.h"
|
2018-08-15 11:46:16 +03:00
|
|
|
#include "common/vfs.h"
|
2015-05-15 16:39:26 +03:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2014-02-06 14:50:16 +04:00
|
|
|
#include <QNetworkAccessManager>
|
2014-02-17 16:48:56 +04:00
|
|
|
#include <QFileInfo>
|
2014-05-23 20:55:44 +04:00
|
|
|
#include <QDir>
|
2014-02-13 17:02:05 +04:00
|
|
|
#include <cmath>
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2016-09-14 16:31:05 +03:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcGetJob, "nextcloud.sync.networkjob.get", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcPropagateDownload, "nextcloud.sync.propagator.download", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2015-05-11 16:41:56 +03:00
|
|
|
// Always coming in with forward slashes.
|
|
|
|
// In csync_excluded_no_ctx we ignore all files with longer than 254 chars
|
2015-10-05 06:20:09 +03:00
|
|
|
// This function also adds a dot at the beginning of the filename to hide the file on OS X and Linux
|
2015-05-22 10:17:24 +03:00
|
|
|
QString OWNCLOUDSYNC_EXPORT createDownloadTmpFileName(const QString &previous)
|
|
|
|
{
|
2015-05-11 16:41:56 +03:00
|
|
|
QString tmpFileName;
|
|
|
|
QString tmpPath;
|
|
|
|
int slashPos = previous.lastIndexOf('/');
|
|
|
|
// work with both pathed filenames and only filenames
|
|
|
|
if (slashPos == -1) {
|
|
|
|
tmpFileName = previous;
|
|
|
|
tmpPath = QString();
|
|
|
|
} else {
|
|
|
|
tmpFileName = previous.mid(slashPos + 1);
|
|
|
|
tmpPath = previous.left(slashPos);
|
|
|
|
}
|
|
|
|
int overhead = 1 + 1 + 2 + 8; // slash dot dot-tilde ffffffff"
|
|
|
|
int spaceForFileName = qMin(254, tmpFileName.length() + overhead) - overhead;
|
|
|
|
if (tmpPath.length() > 0) {
|
|
|
|
return tmpPath + '/' + '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
|
|
|
|
} else {
|
|
|
|
return '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
|
2015-05-08 17:41:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 06:20:09 +03:00
|
|
|
// DOES NOT take ownership of the device.
|
2018-08-15 11:46:16 +03:00
|
|
|
GETFileJob::GETFileJob(AccountPtr account, const QString &path, QIODevice *device,
|
2014-12-18 14:13:12 +03:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
|
2019-02-13 12:15:33 +03:00
|
|
|
qint64 resumeStart, QObject *parent)
|
2014-06-03 13:50:13 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2014-12-18 14:13:12 +03:00
|
|
|
, _device(device)
|
|
|
|
, _headers(headers)
|
|
|
|
, _expectedEtagForResume(expectedEtagForResume)
|
2018-08-15 11:46:16 +03:00
|
|
|
, _expectedContentLength(-1)
|
2018-11-09 14:17:24 +03:00
|
|
|
, _contentLength(-1)
|
2014-12-18 14:13:12 +03:00
|
|
|
, _resumeStart(resumeStart)
|
|
|
|
, _errorStatus(SyncFileItem::NoStatus)
|
2014-11-11 14:16:14 +03:00
|
|
|
, _bandwidthLimited(false)
|
|
|
|
, _bandwidthChoked(false)
|
|
|
|
, _bandwidthQuota(0)
|
2018-11-12 20:46:39 +03:00
|
|
|
, _bandwidthManager(nullptr)
|
2015-02-06 12:20:10 +03:00
|
|
|
, _hasEmittedFinishedSignal(false)
|
|
|
|
, _lastModified()
|
2014-06-03 13:50:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:46:16 +03:00
|
|
|
GETFileJob::GETFileJob(AccountPtr account, const QUrl &url, QIODevice *device,
|
2015-01-08 17:33:39 +03:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
|
2019-02-13 12:15:33 +03:00
|
|
|
qint64 resumeStart, QObject *parent)
|
2014-06-03 13:50:13 +04:00
|
|
|
: AbstractNetworkJob(account, url.toEncoded(), parent)
|
2014-12-18 14:13:12 +03:00
|
|
|
, _device(device)
|
|
|
|
, _headers(headers)
|
|
|
|
, _expectedEtagForResume(expectedEtagForResume)
|
2018-08-15 11:46:16 +03:00
|
|
|
, _expectedContentLength(-1)
|
2018-11-09 14:17:24 +03:00
|
|
|
, _contentLength(-1)
|
2014-12-18 14:13:12 +03:00
|
|
|
, _resumeStart(resumeStart)
|
|
|
|
, _errorStatus(SyncFileItem::NoStatus)
|
|
|
|
, _directDownloadUrl(url)
|
2014-11-11 14:16:14 +03:00
|
|
|
, _bandwidthLimited(false)
|
|
|
|
, _bandwidthChoked(false)
|
|
|
|
, _bandwidthQuota(0)
|
2018-11-12 20:46:39 +03:00
|
|
|
, _bandwidthManager(nullptr)
|
2015-02-06 12:20:10 +03:00
|
|
|
, _hasEmittedFinishedSignal(false)
|
|
|
|
, _lastModified()
|
2014-06-03 13:50:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void GETFileJob::start()
|
|
|
|
{
|
2014-10-08 14:04:17 +04:00
|
|
|
if (_resumeStart > 0) {
|
|
|
|
_headers["Range"] = "bytes=" + QByteArray::number(_resumeStart) + '-';
|
|
|
|
_headers["Accept-Ranges"] = "bytes";
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcGetJob) << "Retry with range " << _headers["Range"];
|
2014-10-08 14:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
QNetworkRequest req;
|
|
|
|
for (QMap<QByteArray, QByteArray>::const_iterator it = _headers.begin(); it != _headers.end(); ++it) {
|
|
|
|
req.setRawHeader(it.key(), it.value());
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:41:06 +03:00
|
|
|
req.setPriority(QNetworkRequest::LowPriority); // Long downloads must not block non-propagation jobs.
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (_directDownloadUrl.isEmpty()) {
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("GET", makeDavUrl(path()), req);
|
2014-06-03 13:50:13 +04:00
|
|
|
} else {
|
|
|
|
// Use direct URL
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("GET", _directDownloadUrl, req);
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2014-11-11 14:16:14 +03:00
|
|
|
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcGetJob) << _bandwidthManager << _bandwidthChoked << _bandwidthLimited;
|
2014-11-11 14:16:14 +03:00
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->registerDownloadJob(this);
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(this, &AbstractNetworkJob::networkActivity, account().data(), &Account::propagatorNetworkActivity);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2017-11-14 13:44:02 +03:00
|
|
|
void GETFileJob::newReplyHook(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
reply->setReadBufferSize(16 * 1024); // keep low so we can easier limit the bandwidth
|
|
|
|
|
|
|
|
connect(reply, &QNetworkReply::metaDataChanged, this, &GETFileJob::slotMetaDataChanged);
|
|
|
|
connect(reply, &QIODevice::readyRead, this, &GETFileJob::slotReadyRead);
|
2018-06-13 15:20:21 +03:00
|
|
|
connect(reply, &QNetworkReply::finished, this, &GETFileJob::slotReadyRead);
|
2017-11-14 13:44:02 +03:00
|
|
|
connect(reply, &QNetworkReply::downloadProgress, this, &GETFileJob::downloadProgress);
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:26:40 +04:00
|
|
|
void GETFileJob::slotMetaDataChanged()
|
|
|
|
{
|
2014-11-11 14:16:14 +03:00
|
|
|
// For some reason setting the read buffer in GETFileJob::start doesn't seem to go
|
|
|
|
// through the HTTP layer thread(?)
|
|
|
|
reply()->setReadBufferSize(16 * 1024);
|
|
|
|
|
2014-10-08 16:09:57 +04:00
|
|
|
int httpStatus = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
2018-10-17 15:49:00 +03:00
|
|
|
if (httpStatus == 301 || httpStatus == 302 || httpStatus == 303 || httpStatus == 307
|
|
|
|
|| httpStatus == 308 || httpStatus == 401) {
|
|
|
|
// Redirects and auth failures (oauth token renew) are handled by AbstractNetworkJob and
|
|
|
|
// will end up restarting the job. We do not want to process further data from the initial
|
|
|
|
// request. newReplyHook() will reestablish signal connections for the follow-up request.
|
|
|
|
bool ok = disconnect(reply(), &QNetworkReply::finished, this, &GETFileJob::slotReadyRead)
|
|
|
|
&& disconnect(reply(), &QNetworkReply::readyRead, this, &GETFileJob::slotReadyRead);
|
|
|
|
ASSERT(ok);
|
2017-11-14 13:44:02 +03:00
|
|
|
return;
|
2018-10-17 15:49:00 +03:00
|
|
|
}
|
2017-11-14 13:44:02 +03:00
|
|
|
|
2014-10-08 16:09:57 +04:00
|
|
|
// If the status code isn't 2xx, don't write the reply body to the file.
|
|
|
|
// For any error: handle it when the job is finished, not here.
|
|
|
|
if (httpStatus / 100 != 2) {
|
2018-05-30 17:29:29 +03:00
|
|
|
// Disable the buffer limit, as we don't limit the bandwidth for error messages.
|
|
|
|
// (We are only going to do a readAll() at the end.)
|
|
|
|
reply()->setReadBufferSize(0);
|
2014-10-08 16:09:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply()->error() != QNetworkReply::NoError) {
|
2014-04-22 14:34:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-11 14:16:14 +03:00
|
|
|
_etag = getEtagFromReply(reply());
|
2014-04-22 14:34:03 +04:00
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (!_directDownloadUrl.isEmpty() && !_etag.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcGetJob) << "Direct download used, ignoring server ETag" << _etag;
|
2014-06-03 13:50:13 +04:00
|
|
|
_etag = QByteArray(); // reset received ETag
|
|
|
|
} else if (!_directDownloadUrl.isEmpty()) {
|
|
|
|
// All fine, ETag empty and directDownloadUrl used
|
|
|
|
} else if (_etag.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "No E-Tag reply by server, considering it invalid";
|
2014-03-20 16:26:40 +04:00
|
|
|
_errorString = tr("No E-Tag received from server, check Proxy/Gateway");
|
2014-04-22 14:34:03 +04:00
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
2014-03-20 16:26:40 +04:00
|
|
|
reply()->abort();
|
|
|
|
return;
|
2014-06-03 13:50:13 +04:00
|
|
|
} else if (!_expectedEtagForResume.isEmpty() && _expectedEtagForResume != _etag) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "We received a different E-Tag for resuming!"
|
2014-06-03 13:50:13 +04:00
|
|
|
<< _expectedEtagForResume << "vs" << _etag;
|
2014-03-20 16:26:40 +04:00
|
|
|
_errorString = tr("We received a different E-Tag for resuming. Retrying next time.");
|
2014-04-22 14:34:03 +04:00
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
2014-03-20 16:26:40 +04:00
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-18 14:02:57 +04:00
|
|
|
|
2020-12-10 21:52:58 +03:00
|
|
|
bool ok = false;
|
2020-12-01 18:58:19 +03:00
|
|
|
_contentLength = reply()->header(QNetworkRequest::ContentLengthHeader).toLongLong(&ok);
|
|
|
|
if (ok && _expectedContentLength != -1 && _contentLength != _expectedContentLength) {
|
2018-08-15 11:46:16 +03:00
|
|
|
qCWarning(lcGetJob) << "We received a different content length than expected!"
|
|
|
|
<< _expectedContentLength << "vs" << _contentLength;
|
|
|
|
_errorString = tr("We received an unexpected download Content-Length.");
|
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:15:33 +03:00
|
|
|
qint64 start = 0;
|
2014-08-29 21:23:08 +04:00
|
|
|
QByteArray ranges = reply()->rawHeader("Content-Range");
|
2014-07-18 14:02:57 +04:00
|
|
|
if (!ranges.isEmpty()) {
|
|
|
|
QRegExp rx("bytes (\\d+)-");
|
|
|
|
if (rx.indexIn(ranges) >= 0) {
|
2019-02-13 12:15:33 +03:00
|
|
|
start = rx.cap(1).toLongLong();
|
2014-07-18 14:02:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start != _resumeStart) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "Wrong content-range: " << ranges << " while expecting start was" << _resumeStart;
|
2015-01-15 19:58:43 +03:00
|
|
|
if (ranges.isEmpty()) {
|
2015-10-05 06:20:09 +03:00
|
|
|
// device doesn't support range, just try again from scratch
|
2014-07-18 14:02:57 +04:00
|
|
|
_device->close();
|
|
|
|
if (!_device->open(QIODevice::WriteOnly)) {
|
|
|
|
_errorString = _device->errorString();
|
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-08-29 21:23:08 +04:00
|
|
|
_resumeStart = 0;
|
2014-07-18 14:02:57 +04:00
|
|
|
} else {
|
|
|
|
_errorString = tr("Server returned wrong content-range");
|
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 12:20:10 +03:00
|
|
|
auto lastModified = reply()->header(QNetworkRequest::LastModifiedHeader);
|
|
|
|
if (!lastModified.isNull()) {
|
|
|
|
_lastModified = Utility::qDateTimeToTime_t(lastModified.toDateTime());
|
|
|
|
}
|
2017-11-14 13:44:02 +03:00
|
|
|
|
|
|
|
_saveBodyToFile = true;
|
2014-03-20 16:26:40 +04:00
|
|
|
}
|
|
|
|
|
2014-11-11 14:16:14 +03:00
|
|
|
void GETFileJob::setBandwidthManager(BandwidthManager *bwm)
|
|
|
|
{
|
|
|
|
_bandwidthManager = bwm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GETFileJob::setChoked(bool c)
|
|
|
|
{
|
|
|
|
_bandwidthChoked = c;
|
|
|
|
QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GETFileJob::setBandwidthLimited(bool b)
|
|
|
|
{
|
|
|
|
_bandwidthLimited = b;
|
|
|
|
QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GETFileJob::giveBandwidthQuota(qint64 q)
|
|
|
|
{
|
|
|
|
_bandwidthQuota = q;
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcGetJob) << "Got" << q << "bytes";
|
2014-11-11 14:16:14 +03:00
|
|
|
QMetaObject::invokeMethod(this, "slotReadyRead", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 GETFileJob::currentDownloadPosition()
|
|
|
|
{
|
|
|
|
if (_device && _device->pos() > 0 && _device->pos() > qint64(_resumeStart)) {
|
|
|
|
return _device->pos();
|
|
|
|
}
|
|
|
|
return _resumeStart;
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void GETFileJob::slotReadyRead()
|
|
|
|
{
|
PropagateDownload: fix possible crash
Backtrace looks like this:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0x0
File "propagatedownload.cpp", line 234, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownl_CA5CFSHZDTX34X.cpp", line 86, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 495, in QMetaCallEvent::placeMetaCall
File "qobject.cpp", line 1256, in QObject::event
File "qapplication.cpp", line 3804, in QApplicationPrivate::notify_helper
GETFileJob::slotReadyRead can be called with a QueuedConnection when the
bendwith manager is involved. In that case, if the QNAM was reset
in between, the reply might have been destroyed.
(This is only speculation based on the backtrace)
2017-03-28 16:21:59 +03:00
|
|
|
if (!reply())
|
|
|
|
return;
|
2014-07-08 17:30:53 +04:00
|
|
|
int bufferSize = qMin(1024 * 8ll, reply()->bytesAvailable());
|
2014-03-19 18:19:09 +04:00
|
|
|
QByteArray buffer(bufferSize, Qt::Uninitialized);
|
|
|
|
|
2018-05-30 17:29:29 +03:00
|
|
|
while (reply()->bytesAvailable() > 0 && _saveBodyToFile) {
|
2014-11-11 14:16:14 +03:00
|
|
|
if (_bandwidthChoked) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "Download choked";
|
2014-11-11 14:16:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
qint64 toRead = bufferSize;
|
|
|
|
if (_bandwidthLimited) {
|
|
|
|
toRead = qMin(qint64(bufferSize), _bandwidthQuota);
|
|
|
|
if (toRead == 0) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "Out of quota";
|
2014-11-11 14:16:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_bandwidthQuota -= toRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 r = reply()->read(buffer.data(), toRead);
|
2014-03-19 18:19:09 +04:00
|
|
|
if (r < 0) {
|
2017-03-23 17:53:22 +03:00
|
|
|
_errorString = networkReplyErrorString(*reply());
|
2014-04-22 14:34:03 +04:00
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "Error while reading from device: " << _errorString;
|
2014-03-19 18:19:09 +04:00
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:24:57 +03:00
|
|
|
qint64 w = _device->write(buffer.constData(), r);
|
|
|
|
if (w != r) {
|
|
|
|
_errorString = _device->errorString();
|
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
|
|
|
qCWarning(lcGetJob) << "Error while writing to file" << w << r << _errorString;
|
|
|
|
reply()->abort();
|
|
|
|
return;
|
2014-03-19 18:19:09 +04:00
|
|
|
}
|
|
|
|
}
|
2014-11-11 14:16:14 +03:00
|
|
|
|
2018-05-30 17:29:29 +03:00
|
|
|
if (reply()->isFinished() && (reply()->bytesAvailable() == 0 || !_saveBodyToFile)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcGetJob) << "Actually finished!";
|
2014-11-11 14:16:14 +03:00
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterDownloadJob(this);
|
|
|
|
}
|
|
|
|
if (!_hasEmittedFinishedSignal) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcGetJob) << "GET of" << reply()->request().url().toString() << "FINISHED WITH STATUS"
|
2018-04-17 21:21:49 +03:00
|
|
|
<< replyStatusString()
|
2017-03-30 14:46:20 +03:00
|
|
|
<< reply()->rawHeader("Content-Range") << reply()->rawHeader("Content-Length");
|
|
|
|
|
2014-11-11 14:16:14 +03:00
|
|
|
emit finishedSignal();
|
|
|
|
}
|
|
|
|
_hasEmittedFinishedSignal = true;
|
|
|
|
deleteLater();
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2021-03-16 15:31:12 +03:00
|
|
|
void GETFileJob::cancel()
|
|
|
|
{
|
|
|
|
if (reply()->isRunning()) {
|
|
|
|
reply()->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit canceled();
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:53:22 +03:00
|
|
|
void GETFileJob::onTimedOut()
|
2014-06-03 13:50:13 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcGetJob) << "Timeout" << (reply() ? reply()->request().url() : path());
|
2016-02-24 21:52:14 +03:00
|
|
|
if (!reply())
|
|
|
|
return;
|
2014-06-03 13:50:13 +04:00
|
|
|
_errorString = tr("Connection Timeout");
|
|
|
|
_errorStatus = SyncFileItem::FatalError;
|
|
|
|
reply()->abort();
|
|
|
|
}
|
|
|
|
|
2015-02-26 17:52:07 +03:00
|
|
|
QString GETFileJob::errorString() const
|
|
|
|
{
|
|
|
|
if (!_errorString.isEmpty()) {
|
|
|
|
return _errorString;
|
|
|
|
}
|
2017-03-23 17:53:22 +03:00
|
|
|
return AbstractNetworkJob::errorString();
|
2015-02-26 17:52:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::start()
|
2014-02-17 16:48:56 +04:00
|
|
|
{
|
2020-02-05 14:57:09 +03:00
|
|
|
if (propagator()->_abortRequested)
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
2018-01-21 23:25:06 +03:00
|
|
|
_isEncrypted = false;
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateDownload) << _item->_file << propagator()->_activeJobList.count();
|
2018-01-21 23:25:06 +03:00
|
|
|
|
2020-12-07 20:42:24 +03:00
|
|
|
const auto path = _item->_file;
|
|
|
|
const auto slashPosition = path.lastIndexOf('/');
|
|
|
|
const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString();
|
|
|
|
|
|
|
|
SyncJournalFileRecord parentRec;
|
|
|
|
propagator()->_journal->getFileRecord(parentPath, &parentRec);
|
2020-06-26 10:43:06 +03:00
|
|
|
|
|
|
|
const auto account = propagator()->account();
|
|
|
|
if (!account->capabilities().clientSideEncryptionAvailable() ||
|
2020-12-07 20:42:24 +03:00
|
|
|
!parentRec.isValid() ||
|
|
|
|
!parentRec._isE2eEncrypted) {
|
2020-06-26 10:43:06 +03:00
|
|
|
startAfterIsEncryptedIsChecked();
|
|
|
|
} else {
|
|
|
|
_downloadEncryptedHelper = new PropagateDownloadEncrypted(propagator(), parentPath, _item, this);
|
2020-12-08 18:20:29 +03:00
|
|
|
connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::fileMetadataFound, [this] {
|
2018-01-21 23:25:06 +03:00
|
|
|
_isEncrypted = true;
|
|
|
|
startAfterIsEncryptedIsChecked();
|
2018-01-05 23:37:31 +03:00
|
|
|
});
|
2018-03-29 00:12:48 +03:00
|
|
|
connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::failed, [this] {
|
|
|
|
done(SyncFileItem::NormalError,
|
2021-04-12 13:38:15 +03:00
|
|
|
tr("File %1 cannot be downloaded because encryption information is missing.").arg(QDir::toNativeSeparators(_item->_file)));
|
2018-03-29 00:12:48 +03:00
|
|
|
});
|
2018-01-22 16:08:32 +03:00
|
|
|
_downloadEncryptedHelper->start();
|
2018-01-05 23:37:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
|
|
|
|
{
|
2016-02-25 19:40:24 +03:00
|
|
|
_stopwatch.start();
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2018-08-15 11:46:16 +03:00
|
|
|
auto &syncOptions = propagator()->syncOptions();
|
2018-11-21 14:23:08 +03:00
|
|
|
auto &vfs = syncOptions._vfs;
|
2018-08-15 11:46:16 +03:00
|
|
|
|
2019-02-01 13:23:00 +03:00
|
|
|
// For virtual files just dehydrate or create the file and be done
|
2018-08-15 11:46:16 +03:00
|
|
|
if (_item->_type == ItemTypeVirtualFileDehydration) {
|
2020-09-22 12:47:40 +03:00
|
|
|
QString fsPath = propagator()->fullLocalPath(_item->_file);
|
2019-02-01 13:23:00 +03:00
|
|
|
if (!FileSystem::verifyFileUnchanged(fsPath, _item->_previousSize, _item->_previousModtime)) {
|
|
|
|
propagator()->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, tr("File has changed since discovery"));
|
|
|
|
return;
|
2018-08-15 11:46:16 +03:00
|
|
|
}
|
2019-02-01 13:23:00 +03:00
|
|
|
|
|
|
|
qCDebug(lcPropagateDownload) << "dehydrating file" << _item->_file;
|
2019-10-30 15:16:32 +03:00
|
|
|
auto r = vfs->dehydratePlaceholder(*_item);
|
|
|
|
if (!r) {
|
|
|
|
done(SyncFileItem::NormalError, r.error());
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 17:07:54 +03:00
|
|
|
propagator()->_journal->deleteFileRecord(_item->_originalFile);
|
2019-02-01 13:23:00 +03:00
|
|
|
updateMetadata(false);
|
|
|
|
return;
|
2018-08-15 11:46:16 +03:00
|
|
|
}
|
2018-11-21 14:23:08 +03:00
|
|
|
if (vfs->mode() == Vfs::Off && _item->_type == ItemTypeVirtualFile) {
|
|
|
|
qCWarning(lcPropagateDownload) << "ignored virtual file type of" << _item->_file;
|
|
|
|
_item->_type = ItemTypeFile;
|
|
|
|
}
|
2018-05-18 09:29:40 +03:00
|
|
|
if (_item->_type == ItemTypeVirtualFile) {
|
2019-02-01 13:23:00 +03:00
|
|
|
qCDebug(lcPropagateDownload) << "creating virtual file" << _item->_file;
|
2019-10-30 15:16:32 +03:00
|
|
|
auto r = vfs->createPlaceholder(*_item);
|
|
|
|
if (!r) {
|
|
|
|
done(SyncFileItem::NormalError, r.error());
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 20:04:58 +03:00
|
|
|
updateMetadata(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-06 12:01:22 +03:00
|
|
|
if (_deleteExisting) {
|
|
|
|
deleteExistingFolder();
|
|
|
|
|
|
|
|
// check for error with deletion
|
|
|
|
if (_state == Finished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:13:21 +03:00
|
|
|
// If we have a conflict where size of the file is unchanged,
|
2017-06-14 13:14:46 +03:00
|
|
|
// compare the remote checksum to the local one.
|
|
|
|
// Maybe it's not a real conflict and no download is necessary!
|
2017-11-16 12:13:21 +03:00
|
|
|
// If the hashes are collision safe and identical, we assume the content is too.
|
|
|
|
// For weak checksums, we only do that if the mtimes are also identical.
|
2020-10-19 15:47:58 +03:00
|
|
|
|
|
|
|
const auto csync_is_collision_safe_hash = [](const QByteArray &checksum_header)
|
|
|
|
{
|
|
|
|
return checksum_header.startsWith("SHA")
|
|
|
|
|| checksum_header.startsWith("MD5:");
|
|
|
|
};
|
2017-06-14 13:14:46 +03:00
|
|
|
if (_item->_instruction == CSYNC_INSTRUCTION_CONFLICT
|
2017-08-24 18:07:22 +03:00
|
|
|
&& _item->_size == _item->_previousSize
|
2017-11-16 12:13:21 +03:00
|
|
|
&& !_item->_checksumHeader.isEmpty()
|
|
|
|
&& (csync_is_collision_safe_hash(_item->_checksumHeader)
|
|
|
|
|| _item->_modtime == _item->_previousModtime)) {
|
2017-06-14 13:14:46 +03:00
|
|
|
qCDebug(lcPropagateDownload) << _item->_file << "may not need download, computing checksum";
|
|
|
|
auto computeChecksum = new ComputeChecksum(this);
|
|
|
|
computeChecksum->setChecksumType(parseChecksumHeaderType(_item->_checksumHeader));
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(computeChecksum, &ComputeChecksum::done,
|
|
|
|
this, &PropagateDownloadFile::conflictChecksumComputed);
|
2019-08-12 09:23:40 +03:00
|
|
|
propagator()->_activeJobList.append(this);
|
2020-09-22 12:47:40 +03:00
|
|
|
computeChecksum->start(propagator()->fullLocalPath(_item->_file));
|
2017-06-14 13:14:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startDownload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateDownloadFile::conflictChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
|
|
|
|
{
|
2019-08-12 09:23:40 +03:00
|
|
|
propagator()->_activeJobList.removeOne(this);
|
2017-06-14 13:14:46 +03:00
|
|
|
if (makeChecksumHeader(checksumType, checksum) == _item->_checksumHeader) {
|
2017-11-16 12:13:21 +03:00
|
|
|
// No download necessary, just update fs and journal metadata
|
2017-06-14 13:14:46 +03:00
|
|
|
qCDebug(lcPropagateDownload) << _item->_file << "remote and local checksum match";
|
2017-11-16 12:13:21 +03:00
|
|
|
|
|
|
|
// Apply the server mtime locally if necessary, ensuring the journal
|
|
|
|
// and local mtimes end up identical
|
2020-09-22 12:47:40 +03:00
|
|
|
auto fn = propagator()->fullLocalPath(_item->_file);
|
2017-11-16 12:13:21 +03:00
|
|
|
if (_item->_modtime != _item->_previousModtime) {
|
|
|
|
FileSystem::setModTime(fn, _item->_modtime);
|
|
|
|
emit propagator()->touchedFile(fn);
|
|
|
|
}
|
|
|
|
_item->_modtime = FileSystem::getModTime(fn);
|
2017-06-14 13:14:46 +03:00
|
|
|
updateMetadata(/*isConflict=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startDownload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateDownloadFile::startDownload()
|
|
|
|
{
|
2020-02-05 14:57:09 +03:00
|
|
|
if (propagator()->_abortRequested)
|
2017-06-14 13:14:46 +03:00
|
|
|
return;
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
// do a klaas' case clash check.
|
2017-01-17 16:29:12 +03:00
|
|
|
if (propagator()->localFileNameClash(_item->_file)) {
|
2021-04-12 13:38:15 +03:00
|
|
|
done(SyncFileItem::NormalError, tr("File %1 cannot be downloaded because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
|
2014-05-23 20:55:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:46:44 +03:00
|
|
|
propagator()->reportProgress(*_item, 0);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
QString tmpFileName;
|
2014-03-20 16:26:40 +04:00
|
|
|
QByteArray expectedEtagForResume;
|
2017-01-17 16:29:12 +03:00
|
|
|
const SyncJournalDb::DownloadInfo progressInfo = propagator()->_journal->getDownloadInfo(_item->_file);
|
2014-02-17 16:48:56 +04:00
|
|
|
if (progressInfo._valid) {
|
|
|
|
// if the etag has changed meanwhile, remove the already downloaded part.
|
2015-04-15 16:19:11 +03:00
|
|
|
if (progressInfo._etag != _item->_etag) {
|
2020-09-22 12:47:40 +03:00
|
|
|
FileSystem::remove(propagator()->fullLocalPath(progressInfo._tmpfile));
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
|
2014-02-17 16:48:56 +04:00
|
|
|
} else {
|
|
|
|
tmpFileName = progressInfo._tmpfile;
|
2014-03-20 16:26:40 +04:00
|
|
|
expectedEtagForResume = progressInfo._etag;
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpFileName.isEmpty()) {
|
2015-05-12 11:35:28 +03:00
|
|
|
tmpFileName = createDownloadTmpFileName(_item->_file);
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
2020-09-22 12:47:40 +03:00
|
|
|
_tmpFile.setFileName(propagator()->fullLocalPath(tmpFileName));
|
2019-08-23 15:49:24 +03:00
|
|
|
|
|
|
|
_resumeStart = _tmpFile.size();
|
|
|
|
if (_resumeStart > 0 && _resumeStart == _item->_size) {
|
|
|
|
qCInfo(lcPropagateDownload) << "File is already complete, no need to download";
|
|
|
|
downloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't open(Append) read-only files, make sure to make
|
|
|
|
// file writable if it exists.
|
|
|
|
if (_tmpFile.exists())
|
|
|
|
FileSystem::setFileReadOnly(_tmpFile.fileName(), false);
|
2014-02-17 16:48:56 +04:00
|
|
|
if (!_tmpFile.open(QIODevice::Append | QIODevice::Unbuffered)) {
|
2019-08-23 15:49:24 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "could not open temporary file" << _tmpFile.fileName();
|
2014-02-17 16:48:56 +04:00
|
|
|
done(SyncFileItem::NormalError, _tmpFile.errorString());
|
|
|
|
return;
|
|
|
|
}
|
2019-08-23 15:49:24 +03:00
|
|
|
// Hide temporary after creation
|
2014-02-18 15:54:40 +04:00
|
|
|
FileSystem::setFileHidden(_tmpFile.fileName(), true);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2015-10-01 12:39:09 +03:00
|
|
|
// If there's not enough space to fully download this file, stop.
|
2017-01-17 16:29:12 +03:00
|
|
|
const auto diskSpaceResult = propagator()->diskSpaceCheck();
|
2017-05-04 16:12:14 +03:00
|
|
|
if (diskSpaceResult != OwncloudPropagator::DiskSpaceOk) {
|
|
|
|
if (diskSpaceResult == OwncloudPropagator::DiskSpaceFailure) {
|
2017-07-12 13:38:53 +03:00
|
|
|
// Using DetailError here will make the error not pop up in the account
|
2017-06-28 13:45:54 +03:00
|
|
|
// tab: instead we'll generate a general "disk space low" message and show
|
|
|
|
// these detail errors only in the error view.
|
2017-07-12 13:38:53 +03:00
|
|
|
done(SyncFileItem::DetailError,
|
2017-06-28 13:45:54 +03:00
|
|
|
tr("The download would reduce free local disk space below the limit"));
|
|
|
|
emit propagator()->insufficientLocalStorage();
|
2017-05-04 16:12:14 +03:00
|
|
|
} else if (diskSpaceResult == OwncloudPropagator::DiskSpaceCritical) {
|
|
|
|
done(SyncFileItem::FatalError,
|
|
|
|
tr("Free space on disk is less than %1").arg(Utility::octetsToString(criticalFreeSpaceLimit())));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the temporary, if empty.
|
|
|
|
if (_resumeStart == 0) {
|
|
|
|
_tmpFile.remove();
|
|
|
|
}
|
|
|
|
|
2015-10-01 12:39:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
{
|
|
|
|
SyncJournalDb::DownloadInfo pi;
|
2015-04-15 16:19:11 +03:00
|
|
|
pi._etag = _item->_etag;
|
2014-02-17 16:48:56 +04:00
|
|
|
pi._tmpfile = tmpFileName;
|
|
|
|
pi._valid = true;
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setDownloadInfo(_item->_file, pi);
|
|
|
|
propagator()->_journal->commit("download file start");
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QByteArray, QByteArray> headers;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_directDownloadUrl.isEmpty()) {
|
2014-06-03 13:50:13 +04:00
|
|
|
// Normal job, download from oC instance
|
2017-01-17 16:29:12 +03:00
|
|
|
_job = new GETFileJob(propagator()->account(),
|
2020-09-22 12:47:40 +03:00
|
|
|
propagator()->fullRemotePath(_isEncrypted ? _item->_encryptedFileName : _item->_file),
|
Propagator: make sure every network job has a parent
This could make sure that the network job gets deleted if the parent job gets
deleted, and would avoid crashes like:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0xffffffff8b008a04
File "qiodevice.cpp", line 1617, in QIODevice::errorString
File "propagatedownload.cpp", line 264, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownload.cpp", line 85, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 3716, in QMetaObject::activate
File "moc_qiodevice.cpp", line 154, in QIODevice::readyRead
File "qnetworkreplyhttpimpl.cpp", line 1045, in QNetworkReplyHttpImplPrivate::replyDownloadData
(#5329)
2016-11-29 18:20:19 +03:00
|
|
|
&_tmpFile, headers, expectedEtagForResume, _resumeStart, this);
|
2014-06-03 13:50:13 +04:00
|
|
|
} else {
|
|
|
|
// We were provided a direct URL, use that one
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcPropagateDownload) << "directDownloadUrl given for " << _item->_file << _item->_directDownloadUrl;
|
2014-10-08 14:04:17 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (!_item->_directDownloadCookies.isEmpty()) {
|
|
|
|
headers["Cookie"] = _item->_directDownloadCookies.toUtf8();
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2014-10-08 14:04:17 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
QUrl url = QUrl::fromUserInput(_item->_directDownloadUrl);
|
2017-01-17 16:29:12 +03:00
|
|
|
_job = new GETFileJob(propagator()->account(),
|
2014-06-03 13:50:13 +04:00
|
|
|
url,
|
Propagator: make sure every network job has a parent
This could make sure that the network job gets deleted if the parent job gets
deleted, and would avoid crashes like:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0xffffffff8b008a04
File "qiodevice.cpp", line 1617, in QIODevice::errorString
File "propagatedownload.cpp", line 264, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownload.cpp", line 85, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 3716, in QMetaObject::activate
File "moc_qiodevice.cpp", line 154, in QIODevice::readyRead
File "qnetworkreplyhttpimpl.cpp", line 1045, in QNetworkReplyHttpImplPrivate::replyDownloadData
(#5329)
2016-11-29 18:20:19 +03:00
|
|
|
&_tmpFile, headers, expectedEtagForResume, _resumeStart, this);
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2017-01-17 16:29:12 +03:00
|
|
|
_job->setBandwidthManager(&propagator()->_bandwidthManager);
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(_job.data(), &GETFileJob::finishedSignal, this, &PropagateDownloadFile::slotGetFinished);
|
|
|
|
connect(_job.data(), &GETFileJob::downloadProgress, this, &PropagateDownloadFile::slotDownloadProgress);
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_activeJobList.append(this);
|
2014-02-17 16:48:56 +04:00
|
|
|
_job->start();
|
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
qint64 PropagateDownloadFile::committedDiskSpace() const
|
2015-10-01 12:39:09 +03:00
|
|
|
{
|
|
|
|
if (_state == Running) {
|
2019-02-13 12:15:33 +03:00
|
|
|
return qBound(0LL, _item->_size - _resumeStart - _downloadProgress, _item->_size);
|
2015-10-01 12:39:09 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::setDeleteExistingFolder(bool enabled)
|
2016-01-06 12:01:22 +03:00
|
|
|
{
|
|
|
|
_deleteExisting = enabled;
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:31:30 +03:00
|
|
|
const char owncloudCustomSoftErrorStringC[] = "owncloud-custom-soft-error-string";
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::slotGetFinished()
|
2014-02-17 16:48:56 +04:00
|
|
|
{
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_activeJobList.removeOne(this);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2017-12-02 13:40:43 +03:00
|
|
|
GETFileJob *job = _job;
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(job);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2018-04-04 17:27:08 +03:00
|
|
|
_item->_httpErrorCode = job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
_item->_requestId = job->requestId();
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
QNetworkReply::NetworkError err = job->reply()->error();
|
|
|
|
if (err != QNetworkReply::NoError) {
|
2014-10-08 14:04:17 +04:00
|
|
|
// If we sent a 'Range' header and get 416 back, we want to retry
|
|
|
|
// without the header.
|
2015-06-05 12:12:21 +03:00
|
|
|
const bool badRangeHeader = job->resumeStart() > 0 && _item->_httpErrorCode == 416;
|
2014-10-08 14:04:17 +04:00
|
|
|
if (badRangeHeader) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "server replied 416 to our range request, trying again without";
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2014-10-08 14:04:17 +04:00
|
|
|
}
|
|
|
|
|
2015-06-05 12:12:21 +03:00
|
|
|
// Getting a 404 probably means that the file was deleted on the server.
|
|
|
|
const bool fileNotFound = _item->_httpErrorCode == 404;
|
|
|
|
if (fileNotFound) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "server replied 404, assuming file was deleted";
|
2015-06-05 12:12:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 18:50:16 +03:00
|
|
|
// Getting a 423 means that the file is locked
|
|
|
|
const bool fileLocked = _item->_httpErrorCode == 423;
|
|
|
|
if (fileLocked) {
|
|
|
|
qCWarning(lcPropagateDownload) << "server replied 423, file is Locked";
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:04:17 +04:00
|
|
|
// Don't keep the temporary file if it is empty or we
|
2015-06-05 12:12:21 +03:00
|
|
|
// used a bad range header or the file's not on the server anymore.
|
2018-08-15 11:46:16 +03:00
|
|
|
if (_tmpFile.exists() && (_tmpFile.size() == 0 || badRangeHeader || fileNotFound)) {
|
2014-02-17 16:48:56 +04:00
|
|
|
_tmpFile.close();
|
2016-01-05 13:58:18 +03:00
|
|
|
FileSystem::remove(_tmpFile.fileName());
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
2014-10-08 14:04:17 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (!_item->_directDownloadUrl.isEmpty() && err != QNetworkReply::OperationCanceledError) {
|
2014-12-17 16:28:13 +03:00
|
|
|
// If this was with a direct download, retry without direct download
|
2017-05-09 15:24:11 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "Direct download of" << _item->_directDownloadUrl << "failed. Retrying through owncloud.";
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_directDownloadUrl.clear();
|
2014-12-17 16:28:13 +03:00
|
|
|
start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:31:30 +03:00
|
|
|
// This gives a custom QNAM (by the user of libowncloudsync) to abort() a QNetworkReply in its metaDataChanged() slot and
|
|
|
|
// set a custom error string to make this a soft error. In contrast to the default hard error this won't bring down
|
|
|
|
// the whole sync and allows for a custom error message.
|
|
|
|
QNetworkReply *reply = job->reply();
|
|
|
|
if (err == QNetworkReply::OperationCanceledError && reply->property(owncloudCustomSoftErrorStringC).isValid()) {
|
|
|
|
job->setErrorString(reply->property(owncloudCustomSoftErrorStringC).toString());
|
|
|
|
job->setErrorStatus(SyncFileItem::SoftError);
|
2015-06-05 12:12:21 +03:00
|
|
|
} else if (badRangeHeader) {
|
|
|
|
// Can't do this in classifyError() because 416 without a
|
|
|
|
// Range header should result in NormalError.
|
|
|
|
job->setErrorStatus(SyncFileItem::SoftError);
|
|
|
|
} else if (fileNotFound) {
|
|
|
|
job->setErrorString(tr("File was deleted from server"));
|
|
|
|
job->setErrorStatus(SyncFileItem::SoftError);
|
2017-05-29 13:05:22 +03:00
|
|
|
|
|
|
|
// As a precaution against bugs that cause our database and the
|
|
|
|
// reality on the server to diverge, rediscover this folder on the
|
|
|
|
// next sync run.
|
2019-02-13 16:18:54 +03:00
|
|
|
propagator()->_journal->schedulePathForRemoteDiscovery(_item->_file);
|
2015-03-18 01:31:30 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 16:46:58 +03:00
|
|
|
QByteArray errorBody;
|
|
|
|
QString errorString = _item->_httpErrorCode >= 400 ? job->errorStringParsingBody(&errorBody)
|
|
|
|
: job->errorString();
|
2014-04-22 14:34:03 +04:00
|
|
|
SyncFileItem::Status status = job->errorStatus();
|
|
|
|
if (status == SyncFileItem::NoStatus) {
|
2015-09-30 16:34:50 +03:00
|
|
|
status = classifyError(err, _item->_httpErrorCode,
|
2018-06-20 16:46:58 +03:00
|
|
|
&propagator()->_anotherSyncNeeded, errorBody);
|
2014-04-22 14:34:03 +04:00
|
|
|
}
|
2015-06-05 12:12:21 +03:00
|
|
|
|
2018-06-20 16:46:58 +03:00
|
|
|
done(status, errorString);
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:38:36 +03:00
|
|
|
_item->_responseTimeStamp = job->responseTimestamp();
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (!job->etag().isEmpty()) {
|
|
|
|
// The etag will be empty if we used a direct download URL.
|
|
|
|
// (If it was really empty by the server, the GETFileJob will have errored
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_etag = parseEtag(job->etag());
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2015-02-06 12:20:10 +03:00
|
|
|
if (job->lastModified()) {
|
|
|
|
// It is possible that the file was modified on the server since we did the discovery phase
|
|
|
|
// so make sure we have the up-to-date time
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_modtime = job->lastModified();
|
2015-02-06 12:20:10 +03:00
|
|
|
}
|
2014-03-26 20:58:32 +04:00
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
_tmpFile.close();
|
|
|
|
_tmpFile.flush();
|
2014-11-20 20:49:34 +03:00
|
|
|
|
|
|
|
/* Check that the size of the GET reply matches the file size. There have been cases
|
|
|
|
* reported that if a server breaks behind a proxy, the GET is still a 200 but is
|
|
|
|
* truncated, as described here: https://github.com/owncloud/mirall/issues/2528
|
|
|
|
*/
|
|
|
|
const QByteArray sizeHeader("Content-Length");
|
2019-02-13 12:15:33 +03:00
|
|
|
qint64 bodySize = job->reply()->rawHeader(sizeHeader).toLongLong();
|
2019-01-30 07:43:08 +03:00
|
|
|
bool hasSizeHeader = !job->reply()->rawHeader(sizeHeader).isEmpty();
|
2014-11-20 20:49:34 +03:00
|
|
|
|
2019-01-30 07:43:08 +03:00
|
|
|
// Qt removes the content-length header for transparently decompressed HTTP1 replies
|
|
|
|
// but not for HTTP2 or SPDY replies. For these it remains and contains the size
|
|
|
|
// of the compressed data. See QTBUG-73364.
|
|
|
|
const auto contentEncoding = job->reply()->rawHeader("content-encoding").toLower();
|
|
|
|
if ((contentEncoding == "gzip" || contentEncoding == "deflate")
|
|
|
|
&& (job->reply()->attribute(QNetworkRequest::HTTP2WasUsedAttribute).toBool()
|
|
|
|
|| job->reply()->attribute(QNetworkRequest::SpdyWasUsedAttribute).toBool())) {
|
|
|
|
bodySize = 0;
|
|
|
|
hasSizeHeader = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasSizeHeader && _tmpFile.size() > 0 && bodySize == 0) {
|
2015-07-30 15:32:33 +03:00
|
|
|
// Strange bug with broken webserver or webfirewall https://github.com/owncloud/client/issues/3373#issuecomment-122672322
|
|
|
|
// This happened when trying to resume a file. The Content-Range header was files, Content-Length was == 0
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateDownload) << bodySize << _item->_size << _tmpFile.size() << job->resumeStart();
|
2016-01-05 13:58:18 +03:00
|
|
|
FileSystem::remove(_tmpFile.fileName());
|
2015-10-26 14:31:07 +03:00
|
|
|
done(SyncFileItem::SoftError, QLatin1String("Broken webserver returning empty content length for non-empty file on resume"));
|
2015-07-30 15:32:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:50:49 +03:00
|
|
|
if (bodySize > 0 && bodySize != _tmpFile.size() - job->resumeStart()) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateDownload) << bodySize << _tmpFile.size() << job->resumeStart();
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2014-11-20 20:49:34 +03:00
|
|
|
done(SyncFileItem::SoftError, tr("The file could not be downloaded completely."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:49:27 +03:00
|
|
|
if (_tmpFile.size() == 0 && _item->_size > 0) {
|
|
|
|
FileSystem::remove(_tmpFile.fileName());
|
|
|
|
done(SyncFileItem::NormalError,
|
2019-03-07 08:10:55 +03:00
|
|
|
tr("The downloaded file is empty despite that the server announced it should have been %1.")
|
2016-04-29 09:49:27 +03:00
|
|
|
.arg(Utility::octetsToString(_item->_size)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:40:43 +03:00
|
|
|
// Did the file come with conflict headers? If so, store them now!
|
|
|
|
// If we download conflict files but the server doesn't send conflict
|
|
|
|
// headers, the record will be established by SyncEngine::conflictRecordMaintenance.
|
|
|
|
// (we can't reliably determine the file id of the base file here,
|
|
|
|
// it might still be downloaded in a parallel job and not exist in
|
|
|
|
// the database yet!)
|
|
|
|
if (job->reply()->rawHeader("OC-Conflict") == "1") {
|
|
|
|
_conflictRecord.path = _item->_file.toUtf8();
|
2018-08-21 12:24:45 +03:00
|
|
|
_conflictRecord.initialBasePath = job->reply()->rawHeader("OC-ConflictInitialBasePath");
|
2017-12-02 13:40:43 +03:00
|
|
|
_conflictRecord.baseFileId = job->reply()->rawHeader("OC-ConflictBaseFileId");
|
2018-08-14 14:35:04 +03:00
|
|
|
_conflictRecord.baseEtag = job->reply()->rawHeader("OC-ConflictBaseEtag");
|
2017-12-02 13:40:43 +03:00
|
|
|
|
2018-08-14 14:35:04 +03:00
|
|
|
auto mtimeHeader = job->reply()->rawHeader("OC-ConflictBaseMtime");
|
2017-12-02 13:40:43 +03:00
|
|
|
if (!mtimeHeader.isEmpty())
|
|
|
|
_conflictRecord.baseModtime = mtimeHeader.toLongLong();
|
|
|
|
|
|
|
|
// We don't set it yet. That will only be done when the download finished
|
|
|
|
// successfully, much further down. Here we just grab the headers because the
|
|
|
|
// job will be deleted later.
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:44:12 +03:00
|
|
|
// Do checksum validation for the download. If there is no checksum header, the validator
|
2016-03-02 16:20:36 +03:00
|
|
|
// will also emit the validated() signal to continue the flow in slot transmissionChecksumValidated()
|
2015-05-20 23:44:12 +03:00
|
|
|
// as this is (still) also correct.
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *validator = new ValidateChecksumHeader(this);
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(validator, &ValidateChecksumHeader::validated,
|
|
|
|
this, &PropagateDownloadFile::transmissionChecksumValidated);
|
|
|
|
connect(validator, &ValidateChecksumHeader::validationFailed,
|
|
|
|
this, &PropagateDownloadFile::slotChecksumFail);
|
2017-11-20 10:18:52 +03:00
|
|
|
auto checksumHeader = findBestChecksum(job->reply()->rawHeader(checkSumHeaderC));
|
2017-10-10 14:24:41 +03:00
|
|
|
auto contentMd5Header = job->reply()->rawHeader(contentMd5HeaderC);
|
|
|
|
if (checksumHeader.isEmpty() && !contentMd5Header.isEmpty())
|
|
|
|
checksumHeader = "MD5:" + contentMd5Header;
|
2015-10-15 10:39:49 +03:00
|
|
|
validator->start(_tmpFile.fileName(), checksumHeader);
|
2015-05-15 11:50:55 +03:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::slotChecksumFail(const QString &errMsg)
|
2015-05-15 11:50:55 +03:00
|
|
|
{
|
2016-01-05 13:58:18 +03:00
|
|
|
FileSystem::remove(_tmpFile.fileName());
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2015-05-15 16:39:26 +03:00
|
|
|
done(SyncFileItem::SoftError, errMsg); // tr("The file downloaded with a broken checksum, will be redownloaded."));
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::deleteExistingFolder()
|
2016-01-06 12:01:22 +03:00
|
|
|
{
|
2020-09-22 12:47:40 +03:00
|
|
|
QString existingDir = propagator()->fullLocalPath(_item->_file);
|
2016-01-06 12:01:22 +03:00
|
|
|
if (!QFileInfo(existingDir).isDir()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the directory if it is empty!
|
|
|
|
QDir dir(existingDir);
|
|
|
|
if (dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries).count() == 0) {
|
|
|
|
if (dir.rmdir(existingDir)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// on error, just try to move it away...
|
|
|
|
}
|
|
|
|
|
2018-01-17 12:59:47 +03:00
|
|
|
QString error;
|
|
|
|
if (!propagator()->createConflict(_item, _associatedComposite, &error)) {
|
|
|
|
done(SyncFileItem::NormalError, error);
|
2016-01-06 12:01:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:23:54 +03:00
|
|
|
namespace { // Anonymous namespace for the recall feature
|
2015-05-15 16:45:50 +03:00
|
|
|
static QString makeRecallFileName(const QString &fn)
|
2015-04-09 16:25:34 +03:00
|
|
|
{
|
|
|
|
QString recallFileName(fn);
|
2015-10-05 06:20:09 +03:00
|
|
|
// Add _recall-XXXX before the extension.
|
2015-04-09 16:25:34 +03:00
|
|
|
int dotLocation = recallFileName.lastIndexOf('.');
|
2015-10-05 06:20:09 +03:00
|
|
|
// If no extension, add it at the end (take care of cases like foo/.hidden or foo.bar/file)
|
2015-04-09 16:25:34 +03:00
|
|
|
if (dotLocation <= recallFileName.lastIndexOf('/') + 1) {
|
|
|
|
dotLocation = recallFileName.size();
|
|
|
|
}
|
|
|
|
|
2017-09-25 12:49:11 +03:00
|
|
|
QString timeString = QDateTime::currentDateTimeUtc().toString("yyyyMMdd-hhmmss");
|
2015-05-15 16:45:50 +03:00
|
|
|
recallFileName.insert(dotLocation, "_.sys.admin#recall#-" + timeString);
|
2015-04-09 16:25:34 +03:00
|
|
|
|
|
|
|
return recallFileName;
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:42:19 +03:00
|
|
|
void handleRecallFile(const QString &filePath, const QString &folderPath, SyncJournalDb &journal)
|
2015-05-21 17:23:54 +03:00
|
|
|
{
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateDownload) << "handleRecallFile: " << filePath;
|
2015-05-21 17:23:54 +03:00
|
|
|
|
2016-09-14 16:42:19 +03:00
|
|
|
FileSystem::setFileHidden(filePath, true);
|
2015-05-21 17:23:54 +03:00
|
|
|
|
2016-09-14 16:42:19 +03:00
|
|
|
QFile file(filePath);
|
2015-05-21 17:23:54 +03:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "Could not open recall file" << file.errorString();
|
2015-05-21 17:23:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-09-14 16:42:19 +03:00
|
|
|
QFileInfo existingFile(filePath);
|
|
|
|
QDir baseDir = existingFile.dir();
|
2015-05-21 17:23:54 +03:00
|
|
|
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
line.chop(1); // remove trailing \n
|
|
|
|
|
2016-09-14 16:42:19 +03:00
|
|
|
QString recalledFile = QDir::cleanPath(baseDir.filePath(line));
|
|
|
|
if (!recalledFile.startsWith(folderPath) || !recalledFile.startsWith(baseDir.path())) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "Ignoring recall of " << recalledFile;
|
2016-09-14 16:42:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path of the recalled file in the local folder
|
|
|
|
QString localRecalledFile = recalledFile.mid(folderPath.size());
|
|
|
|
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord record;
|
|
|
|
if (!journal.getFileRecord(localRecalledFile, &record) || !record.isValid()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "No db entry for recall of" << localRecalledFile;
|
2016-09-14 16:42:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:14:46 +03:00
|
|
|
qCInfo(lcPropagateDownload) << "Recalling" << localRecalledFile << "Checksum:" << record._checksumHeader;
|
2016-09-14 16:42:19 +03:00
|
|
|
|
|
|
|
QString targetPath = makeRecallFileName(recalledFile);
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateDownload) << "Copy recall file: " << recalledFile << " -> " << targetPath;
|
2016-09-06 11:42:59 +03:00
|
|
|
// Remove the target first, QFile::copy will not overwrite it.
|
2016-09-14 16:42:19 +03:00
|
|
|
FileSystem::remove(targetPath);
|
|
|
|
QFile::copy(recalledFile, targetPath);
|
2017-05-17 11:55:42 +03:00
|
|
|
}
|
2015-05-21 17:23:54 +03:00
|
|
|
}
|
2016-03-03 13:50:06 +03:00
|
|
|
|
|
|
|
static void preserveGroupOwnership(const QString &fileName, const QFileInfo &fi)
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_UNIX
|
2019-09-09 19:06:29 +03:00
|
|
|
int chownErr = chown(fileName.toLocal8Bit().constData(), -1, fi.groupId());
|
|
|
|
if (chownErr) {
|
2019-09-16 21:26:18 +03:00
|
|
|
// TODO: Consider further error handling!
|
|
|
|
qCWarning(lcPropagateDownload) << QString("preserveGroupOwnership: chown error %1: setting group %2 failed on file %3").arg(chownErr).arg(fi.groupId()).arg(fileName);
|
2019-09-09 19:06:29 +03:00
|
|
|
}
|
2016-03-03 13:50:06 +03:00
|
|
|
#else
|
|
|
|
Q_UNUSED(fileName);
|
|
|
|
Q_UNUSED(fi);
|
|
|
|
#endif
|
|
|
|
}
|
2015-05-21 17:23:54 +03:00
|
|
|
} // end namespace
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::transmissionChecksumValidated(const QByteArray &checksumType, const QByteArray &checksum)
|
2014-02-17 16:48:56 +04:00
|
|
|
{
|
2020-07-24 18:16:28 +03:00
|
|
|
const QByteArray theContentChecksumType = propagator()->account()->capabilities().preferredUploadChecksumType();
|
2016-03-02 16:20:36 +03:00
|
|
|
|
|
|
|
// Reuse transmission checksum as content checksum.
|
|
|
|
//
|
|
|
|
// We could do this more aggressively and accept both MD5 and SHA1
|
|
|
|
// instead of insisting on the exactly correct checksum type.
|
|
|
|
if (theContentChecksumType == checksumType || theContentChecksumType.isEmpty()) {
|
|
|
|
return contentChecksumComputed(checksumType, checksum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the content checksum.
|
|
|
|
auto computeChecksum = new ComputeChecksum(this);
|
|
|
|
computeChecksum->setChecksumType(theContentChecksumType);
|
|
|
|
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(computeChecksum, &ComputeChecksum::done,
|
|
|
|
this, &PropagateDownloadFile::contentChecksumComputed);
|
2016-03-02 16:20:36 +03:00
|
|
|
computeChecksum->start(_tmpFile.fileName());
|
|
|
|
}
|
2016-02-25 19:17:14 +03:00
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::contentChecksumComputed(const QByteArray &checksumType, const QByteArray &checksum)
|
2016-03-02 16:20:36 +03:00
|
|
|
{
|
2017-06-14 13:14:46 +03:00
|
|
|
_item->_checksumHeader = makeChecksumHeader(checksumType, checksum);
|
2016-03-02 16:20:36 +03:00
|
|
|
|
2018-01-05 23:37:31 +03:00
|
|
|
if (_isEncrypted) {
|
2018-01-21 23:25:06 +03:00
|
|
|
if (_downloadEncryptedHelper->decryptFile(_tmpFile)) {
|
|
|
|
downloadFinished();
|
|
|
|
} else {
|
|
|
|
done(SyncFileItem::NormalError, _downloadEncryptedHelper->errorString());
|
|
|
|
}
|
2018-01-05 23:37:31 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
downloadFinished();
|
|
|
|
}
|
2016-03-02 16:20:36 +03:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::downloadFinished()
|
2016-03-02 16:20:36 +03:00
|
|
|
{
|
2020-09-22 12:47:40 +03:00
|
|
|
ASSERT(!_tmpFile.isOpen());
|
|
|
|
QString fn = propagator()->fullLocalPath(_item->_file);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2014-10-29 13:23:44 +03:00
|
|
|
// In case of file name clash, report an error
|
|
|
|
// This can happen if another parallel download saved a clashing file.
|
2017-01-17 16:29:12 +03:00
|
|
|
if (propagator()->localFileNameClash(_item->_file)) {
|
2014-10-29 13:23:44 +03:00
|
|
|
done(SyncFileItem::NormalError, tr("File %1 cannot be saved because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
|
|
|
|
return;
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2015-05-12 11:35:28 +03:00
|
|
|
FileSystem::setModTime(_tmpFile.fileName(), _item->_modtime);
|
2015-10-05 06:20:09 +03:00
|
|
|
// We need to fetch the time again because some file systems such as FAT have worse than a second
|
2015-04-15 16:31:47 +03:00
|
|
|
// Accuracy, and we really need the time from the file system. (#3103)
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_modtime = FileSystem::getModTime(_tmpFile.fileName());
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2019-02-06 17:12:57 +03:00
|
|
|
bool previousFileExists = FileSystem::fileExists(fn);
|
|
|
|
if (previousFileExists) {
|
2015-05-07 12:41:29 +03:00
|
|
|
// Preserve the existing file permissions.
|
|
|
|
QFileInfo existingFile(fn);
|
|
|
|
if (existingFile.permissions() != _tmpFile.permissions()) {
|
|
|
|
_tmpFile.setPermissions(existingFile.permissions());
|
|
|
|
}
|
2016-03-03 13:50:06 +03:00
|
|
|
preserveGroupOwnership(_tmpFile.fileName(), existingFile);
|
2015-05-07 12:41:29 +03:00
|
|
|
|
2019-02-05 13:42:15 +03:00
|
|
|
// Make the file a hydrated placeholder if possible
|
2020-12-30 14:53:03 +03:00
|
|
|
const auto result = propagator()->syncOptions()._vfs->convertToPlaceholder(_tmpFile.fileName(), *_item, fn);
|
|
|
|
if (!result) {
|
|
|
|
done(SyncFileItem::NormalError, result.error());
|
|
|
|
return;
|
|
|
|
}
|
2015-05-07 12:41:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 16:10:11 +03:00
|
|
|
// Apply the remote permissions
|
2017-09-19 11:53:51 +03:00
|
|
|
FileSystem::setFileReadOnlyWeak(_tmpFile.fileName(), !_item->_remotePerm.isNull() && !_item->_remotePerm.hasPermission(RemotePermissions::CanWrite));
|
2015-10-29 16:10:11 +03:00
|
|
|
|
2019-02-05 13:42:15 +03:00
|
|
|
bool isConflict = _item->_instruction == CSYNC_INSTRUCTION_CONFLICT
|
|
|
|
&& (QFileInfo(fn).isDir() || !FileSystem::fileEquals(fn, _tmpFile.fileName()));
|
|
|
|
if (isConflict) {
|
|
|
|
QString error;
|
|
|
|
if (!propagator()->createConflict(_item, _associatedComposite, &error)) {
|
|
|
|
done(SyncFileItem::SoftError, error);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 17:12:57 +03:00
|
|
|
previousFileExists = false;
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:23:47 +03:00
|
|
|
const auto vfs = propagator()->syncOptions()._vfs;
|
|
|
|
|
|
|
|
// In the case of an hydration, this size is likely to change for placeholders
|
|
|
|
// (except with the cfapi backend)
|
|
|
|
const auto isVirtualDownload = _item->_type == ItemTypeVirtualFileDownload;
|
|
|
|
const auto isCfApiVfs = vfs && vfs->mode() == Vfs::WindowsCfApi;
|
|
|
|
if (previousFileExists && (isCfApiVfs || !isVirtualDownload)) {
|
2019-02-06 17:12:57 +03:00
|
|
|
// Check whether the existing file has changed since the discovery
|
|
|
|
// phase by comparing size and mtime to the previous values. This
|
|
|
|
// is necessary to avoid overwriting user changes that happened between
|
|
|
|
// the discovery phase and now.
|
|
|
|
const qint64 expectedSize = _item->_previousSize;
|
|
|
|
const time_t expectedMtime = _item->_previousModtime;
|
|
|
|
if (!FileSystem::verifyFileUnchanged(fn, expectedSize, expectedMtime)) {
|
|
|
|
propagator()->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, tr("File has changed since discovery"));
|
|
|
|
return;
|
|
|
|
}
|
2019-02-05 13:42:15 +03:00
|
|
|
}
|
2019-01-25 13:11:28 +03:00
|
|
|
|
2014-02-18 17:05:29 +04:00
|
|
|
QString error;
|
2017-01-17 16:29:12 +03:00
|
|
|
emit propagator()->touchedFile(fn);
|
2015-05-07 12:41:29 +03:00
|
|
|
// The fileChanged() check is done above to generate better error messages.
|
|
|
|
if (!FileSystem::uncheckedRenameReplace(_tmpFile.fileName(), fn, &error)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << QString("Rename failed: %1 => %2").arg(_tmpFile.fileName()).arg(fn);
|
2016-04-29 17:14:18 +03:00
|
|
|
// If the file is locked, we want to retry this sync when it
|
|
|
|
// becomes available again, otherwise try again directly
|
|
|
|
if (FileSystem::isFileLocked(fn)) {
|
2017-01-17 16:29:12 +03:00
|
|
|
emit propagator()->seenLockedFile(fn);
|
2016-04-29 17:14:18 +03:00
|
|
|
} else {
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2016-04-29 17:14:18 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 22:49:52 +03:00
|
|
|
done(SyncFileItem::SoftError, error);
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-08-15 11:46:16 +03:00
|
|
|
|
2015-05-07 12:41:29 +03:00
|
|
|
FileSystem::setFileHidden(fn, false);
|
2014-02-18 17:05:29 +04:00
|
|
|
|
2014-09-05 16:01:26 +04:00
|
|
|
// Maybe we downloaded a newer version of the file than we thought we would...
|
|
|
|
// Get up to date information for the journal.
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_size = FileSystem::getSize(fn);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2017-12-02 13:40:43 +03:00
|
|
|
// Maybe what we downloaded was a conflict file? If so, set a conflict record.
|
|
|
|
// (the data was prepared in slotGetFinished above)
|
|
|
|
if (_conflictRecord.isValid())
|
|
|
|
propagator()->_journal->setConflictRecord(_conflictRecord);
|
|
|
|
|
2019-07-24 16:25:02 +03:00
|
|
|
if (vfs && vfs->mode() == Vfs::WithSuffix) {
|
2018-08-15 11:46:16 +03:00
|
|
|
// If the virtual file used to have a different name and db
|
2019-07-24 16:25:02 +03:00
|
|
|
// entry, remove it transfer its old pin state.
|
|
|
|
if (_item->_type == ItemTypeVirtualFileDownload) {
|
2018-08-15 11:46:16 +03:00
|
|
|
QString virtualFile = _item->_file + vfs->fileSuffix();
|
2020-09-22 12:47:40 +03:00
|
|
|
auto fn = propagator()->fullLocalPath(virtualFile);
|
2018-08-15 11:46:16 +03:00
|
|
|
qCDebug(lcPropagateDownload) << "Download of previous virtual file finished" << fn;
|
|
|
|
QFile::remove(fn);
|
|
|
|
propagator()->_journal->deleteFileRecord(virtualFile);
|
2019-06-27 16:47:04 +03:00
|
|
|
|
|
|
|
// Move the pin state to the new location
|
2019-07-18 16:30:40 +03:00
|
|
|
auto pin = propagator()->_journal->internalPinStates().rawForPath(virtualFile.toUtf8());
|
2019-06-27 16:47:04 +03:00
|
|
|
if (pin && *pin != PinState::Inherited) {
|
2019-07-18 16:30:40 +03:00
|
|
|
vfs->setPinState(_item->_file, *pin);
|
|
|
|
vfs->setPinState(virtualFile, PinState::Inherited);
|
2019-06-27 16:47:04 +03:00
|
|
|
}
|
2018-08-15 11:46:16 +03:00
|
|
|
}
|
2019-07-24 16:25:02 +03:00
|
|
|
|
|
|
|
// Ensure the pin state isn't contradictory
|
|
|
|
auto pin = vfs->pinState(_item->_file);
|
|
|
|
if (pin && *pin == PinState::OnlineOnly)
|
|
|
|
vfs->setPinState(_item->_file, PinState::Unspecified);
|
2018-05-30 11:33:32 +03:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:14:46 +03:00
|
|
|
updateMetadata(isConflict);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateDownloadFile::updateMetadata(bool isConflict)
|
|
|
|
{
|
2020-09-22 12:47:40 +03:00
|
|
|
QString fn = propagator()->fullLocalPath(_item->_file);
|
2017-06-14 13:14:46 +03:00
|
|
|
|
2019-01-21 13:19:45 +03:00
|
|
|
if (!propagator()->updateMetadata(*_item)) {
|
2016-04-07 12:47:04 +03:00
|
|
|
done(SyncFileItem::FatalError, tr("Error writing metadata to the database"));
|
|
|
|
return;
|
|
|
|
}
|
2018-02-12 14:50:51 +03:00
|
|
|
|
2018-01-29 01:50:12 +03:00
|
|
|
if (_isEncrypted) {
|
2018-02-12 14:50:51 +03:00
|
|
|
propagator()->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
|
|
|
|
} else {
|
|
|
|
propagator()->_journal->setDownloadInfo(_item->_encryptedFileName, SyncJournalDb::DownloadInfo());
|
2018-01-29 01:50:12 +03:00
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->commit("download file start2");
|
2019-01-21 13:19:45 +03:00
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
done(isConflict ? SyncFileItem::Conflict : SyncFileItem::Success);
|
2015-04-09 16:25:34 +03:00
|
|
|
|
|
|
|
// handle the special recall file
|
2017-09-19 11:53:51 +03:00
|
|
|
if (!_item->_remotePerm.hasPermission(RemotePermissions::IsShared)
|
2016-09-14 16:42:19 +03:00
|
|
|
&& (_item->_file == QLatin1String(".sys.admin#recall#")
|
2020-09-22 12:47:40 +03:00
|
|
|
|| _item->_file.endsWith(QLatin1String("/.sys.admin#recall#")))) {
|
|
|
|
handleRecallFile(fn, propagator()->localPath(), *propagator()->_journal);
|
2015-04-09 16:25:34 +03:00
|
|
|
}
|
2016-02-25 19:40:24 +03:00
|
|
|
|
|
|
|
qint64 duration = _stopwatch.elapsed();
|
|
|
|
if (isLikelyFinishedQuickly() && duration > 5 * 1000) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropagateDownload) << "WARNING: Unexpectedly slow connection, took" << duration << "msec for" << _item->_size - _resumeStart << "bytes for" << _item->_file;
|
2016-02-25 19:40:24 +03:00
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
void PropagateDownloadFile::slotDownloadProgress(qint64 received, qint64)
|
2014-03-14 16:03:16 +04:00
|
|
|
{
|
2014-08-29 21:23:08 +04:00
|
|
|
if (!_job)
|
|
|
|
return;
|
2015-10-01 12:39:09 +03:00
|
|
|
_downloadProgress = received;
|
2017-02-14 14:46:44 +03:00
|
|
|
propagator()->reportProgress(*_item, _resumeStart + received);
|
2014-03-14 16:03:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-11 01:03:03 +03:00
|
|
|
void PropagateDownloadFile::abort(PropagatorJob::AbortType abortType)
|
2014-02-17 16:48:56 +04:00
|
|
|
{
|
|
|
|
if (_job && _job->reply())
|
|
|
|
_job->reply()->abort();
|
2017-08-11 01:03:03 +03:00
|
|
|
|
|
|
|
if (abortType == AbortType::Asynchronous) {
|
|
|
|
emit abortFinished();
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|