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"
|
|
|
|
#include "syncjournaldb.h"
|
|
|
|
#include "syncjournalfilerecord.h"
|
2014-02-13 17:02:05 +04:00
|
|
|
#include "utility.h"
|
2014-02-18 15:54:40 +04:00
|
|
|
#include "filesystem.h"
|
2014-04-29 18:47:07 +04:00
|
|
|
#include "propagatorjobs.h"
|
2015-05-15 16:39:26 +03:00
|
|
|
#include "transmissionchecksumvalidator.h"
|
|
|
|
|
2014-11-11 14:16:14 +03:00
|
|
|
#include <json.h>
|
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>
|
2015-03-30 09:41:37 +03:00
|
|
|
#include <QDebug>
|
2014-02-13 17:02:05 +04:00
|
|
|
#include <cmath>
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-02-06 14:50:16 +04: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
|
|
|
|
// This function also adds a dot at the begining 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
// DOES NOT take owncership of the device.
|
2014-12-18 14:09:48 +03:00
|
|
|
GETFileJob::GETFileJob(AccountPtr account, const QString& path, QFile *device,
|
2014-12-18 14:13:12 +03:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
|
|
|
|
quint64 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)
|
|
|
|
, _resumeStart(resumeStart) , _errorStatus(SyncFileItem::NoStatus)
|
2014-11-11 14:16:14 +03:00
|
|
|
, _bandwidthLimited(false), _bandwidthChoked(false), _bandwidthQuota(0), _bandwidthManager(0)
|
2015-02-06 12:20:10 +03:00
|
|
|
, _hasEmittedFinishedSignal(false), _lastModified()
|
2014-06-03 13:50:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
GETFileJob::GETFileJob(AccountPtr account, const QUrl& url, QFile *device,
|
2015-01-08 17:33:39 +03:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, const QByteArray &expectedEtagForResume,
|
2014-12-18 14:13:12 +03:00
|
|
|
quint64 resumeStart, QObject* parent)
|
2015-01-08 17:33:39 +03:00
|
|
|
|
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)
|
|
|
|
, _resumeStart(resumeStart), _errorStatus(SyncFileItem::NoStatus), _directDownloadUrl(url)
|
2014-11-11 14:16:14 +03:00
|
|
|
, _bandwidthLimited(false), _bandwidthChoked(false), _bandwidthQuota(0), _bandwidthManager(0)
|
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";
|
|
|
|
qDebug() << "Retry with range " << _headers["Range"];
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (_directDownloadUrl.isEmpty()) {
|
|
|
|
setReply(davRequest("GET", path(), req));
|
|
|
|
} else {
|
|
|
|
// Use direct URL
|
|
|
|
setReply(davRequest("GET", _directDownloadUrl, req));
|
2014-12-16 13:51:25 +03:00
|
|
|
_followRedirects = true; // (follow redirections for the direct download)
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
setupConnections(reply());
|
2014-11-11 14:16:14 +03:00
|
|
|
|
|
|
|
reply()->setReadBufferSize(16 * 1024); // keep low so we can easier limit the bandwidth
|
|
|
|
qDebug() << Q_FUNC_INFO << _bandwidthManager << _bandwidthChoked << _bandwidthLimited;
|
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->registerDownloadJob(this);
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
if( reply()->error() != QNetworkReply::NoError ) {
|
2014-03-06 23:33:17 +04:00
|
|
|
qWarning() << Q_FUNC_INFO << " Network error: " << reply()->errorString();
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-03-20 16:26:40 +04:00
|
|
|
connect(reply(), SIGNAL(metaDataChanged()), this, SLOT(slotMetaDataChanged()));
|
2014-02-17 16:48:56 +04:00
|
|
|
connect(reply(), SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
|
2014-03-14 16:03:16 +04:00
|
|
|
connect(reply(), SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
|
2014-12-18 14:09:48 +03:00
|
|
|
connect(this, SIGNAL(networkActivity()), account().data(), SIGNAL(propagatorNetworkActivity()));
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
_device->close();
|
|
|
|
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()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Direct download used, ignoring server ETag" << _etag;
|
|
|
|
_etag = QByteArray(); // reset received ETag
|
|
|
|
} else if (!_directDownloadUrl.isEmpty()) {
|
|
|
|
// All fine, ETag empty and directDownloadUrl used
|
|
|
|
} else if (_etag.isEmpty()) {
|
2014-03-20 16:26:40 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "No E-Tag reply by server, considering it invalid";
|
|
|
|
_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) {
|
2014-03-20 16:26:40 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "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
|
|
|
|
|
|
|
quint64 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) {
|
|
|
|
start = rx.cap(1).toULongLong();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start != _resumeStart) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Wrong content-range: "<< ranges << " while expecting start was" << _resumeStart;
|
2015-01-15 19:58:43 +03:00
|
|
|
if (ranges.isEmpty()) {
|
2014-07-18 14:02:57 +04:00
|
|
|
// device don't support range, just stry again from scratch
|
|
|
|
_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());
|
|
|
|
}
|
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;
|
|
|
|
qDebug() << Q_FUNC_INFO << "Got" << q << "bytes";
|
|
|
|
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()
|
|
|
|
{
|
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);
|
|
|
|
|
2014-11-11 14:16:14 +03:00
|
|
|
//qDebug() << Q_FUNC_INFO << reply()->bytesAvailable() << reply()->isOpen() << reply()->isFinished();
|
|
|
|
|
2014-03-19 18:19:09 +04:00
|
|
|
while(reply()->bytesAvailable() > 0) {
|
2014-11-11 14:16:14 +03:00
|
|
|
if (_bandwidthChoked) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Download choked";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
qint64 toRead = bufferSize;
|
|
|
|
if (_bandwidthLimited) {
|
|
|
|
toRead = qMin(qint64(bufferSize), _bandwidthQuota);
|
|
|
|
if (toRead == 0) {
|
2015-06-29 18:43:25 +03:00
|
|
|
//qDebug() << Q_FUNC_INFO << "Out of quota";
|
2014-11-11 14:16:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_bandwidthQuota -= toRead;
|
|
|
|
//qDebug() << Q_FUNC_INFO << "Reading" << toRead << "remaining" << _bandwidthQuota;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 r = reply()->read(buffer.data(), toRead);
|
2014-03-19 18:19:09 +04:00
|
|
|
if (r < 0) {
|
|
|
|
_errorString = reply()->errorString();
|
2014-04-22 14:34:03 +04:00
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
2014-03-19 18:19:09 +04:00
|
|
|
qDebug() << "Error while reading from device: " << _errorString;
|
|
|
|
reply()->abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:57 +04:00
|
|
|
if (_device->isOpen()) {
|
|
|
|
qint64 w = _device->write(buffer.constData(), r);
|
|
|
|
if (w != r) {
|
|
|
|
_errorString = _device->errorString();
|
|
|
|
_errorStatus = SyncFileItem::NormalError;
|
|
|
|
qDebug() << "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
|
|
|
|
|
|
|
//qDebug() << Q_FUNC_INFO << "END" << reply()->isFinished() << reply()->bytesAvailable() << _hasEmittedFinishedSignal;
|
|
|
|
if (reply()->isFinished() && reply()->bytesAvailable() == 0) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Actually finished!";
|
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterDownloadJob(this);
|
|
|
|
}
|
|
|
|
if (!_hasEmittedFinishedSignal) {
|
|
|
|
emit finishedSignal();
|
|
|
|
}
|
|
|
|
_hasEmittedFinishedSignal = true;
|
|
|
|
deleteLater();
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
void GETFileJob::slotTimeout()
|
|
|
|
{
|
2015-07-30 15:27:54 +03:00
|
|
|
qDebug() << "Timeout" << reply()->request().url();
|
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;
|
|
|
|
} else if (reply()->hasRawHeader("OC-ErrorString")) {
|
|
|
|
return reply()->rawHeader("OC-ErrorString");
|
|
|
|
} else {
|
|
|
|
return reply()->errorString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << _item->_file << _propagator->_activeJobs;
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
// do a klaas' case clash check.
|
2015-04-15 16:19:11 +03:00
|
|
|
if( _propagator->localFileNameClash(_item->_file) ) {
|
2014-05-23 20:55:44 +04:00
|
|
|
done( SyncFileItem::NormalError, tr("File %1 can not be downloaded because of a local file name clash!")
|
2015-04-15 16:19:11 +03:00
|
|
|
.arg(QDir::toNativeSeparators(_item->_file)) );
|
2014-05-23 20:55:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
emit progress(*_item, 0);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
QString tmpFileName;
|
2014-03-20 16:26:40 +04:00
|
|
|
QByteArray expectedEtagForResume;
|
2015-04-15 16:19:11 +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) {
|
2014-09-03 14:11:03 +04:00
|
|
|
QFile::remove(_propagator->getFilePath(progressInfo._tmpfile));
|
2015-04-15 16:19:11 +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
|
|
|
}
|
|
|
|
|
2014-09-03 14:11:03 +04:00
|
|
|
_tmpFile.setFileName(_propagator->getFilePath(tmpFileName));
|
2014-02-17 16:48:56 +04:00
|
|
|
if (!_tmpFile.open(QIODevice::Append | QIODevice::Unbuffered)) {
|
|
|
|
done(SyncFileItem::NormalError, _tmpFile.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
_resumeStart = _tmpFile.size();
|
|
|
|
if (_resumeStart > 0) {
|
|
|
|
if (_resumeStart == _item->_size) {
|
|
|
|
qDebug() << "File is already complete, no need to download";
|
|
|
|
_tmpFile.close();
|
|
|
|
downloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's not enough space to fully download this file, stop.
|
|
|
|
const auto diskSpaceResult = _propagator->diskSpaceCheck();
|
|
|
|
if (diskSpaceResult == OwncloudPropagator::DiskSpaceFailure) {
|
|
|
|
done(SyncFileItem::NormalError,
|
|
|
|
tr("The download would reduce free disk space below %1").arg(
|
|
|
|
Utility::octetsToString(freeSpaceLimit())));
|
|
|
|
return;
|
|
|
|
} else if (diskSpaceResult == OwncloudPropagator::DiskSpaceCritical) {
|
|
|
|
done(SyncFileItem::FatalError,
|
|
|
|
tr("Free space on disk is less than %1").arg(
|
|
|
|
Utility::octetsToString(criticalFreeSpaceLimit())));
|
|
|
|
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;
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_journal->setDownloadInfo(_item->_file, pi);
|
2014-02-17 16:48:56 +04:00
|
|
|
_propagator->_journal->commit("download file start");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-12-18 14:09:48 +03:00
|
|
|
_job = new GETFileJob(_propagator->account(),
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_remoteFolder + _item->_file,
|
2015-10-01 12:39:09 +03:00
|
|
|
&_tmpFile, headers, expectedEtagForResume, _resumeStart);
|
2014-06-03 13:50:13 +04:00
|
|
|
} else {
|
|
|
|
// We were provided a direct URL, use that one
|
2015-04-15 16:19:11 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << "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);
|
2014-12-18 14:09:48 +03:00
|
|
|
_job = new GETFileJob(_propagator->account(),
|
2014-06-03 13:50:13 +04:00
|
|
|
url,
|
2015-10-01 12:39:09 +03:00
|
|
|
&_tmpFile, headers, expectedEtagForResume, _resumeStart);
|
2014-06-03 13:50:13 +04:00
|
|
|
}
|
2014-11-11 14:16:14 +03:00
|
|
|
_job->setBandwidthManager(&_propagator->_bandwidthManager);
|
2014-02-17 16:48:56 +04:00
|
|
|
connect(_job, SIGNAL(finishedSignal()), this, SLOT(slotGetFinished()));
|
2014-03-14 16:03:16 +04:00
|
|
|
connect(_job, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(slotDownloadProgress(qint64,qint64)));
|
2014-02-17 16:48:56 +04:00
|
|
|
_propagator->_activeJobs ++;
|
|
|
|
_job->start();
|
|
|
|
}
|
|
|
|
|
2015-10-01 12:39:09 +03:00
|
|
|
qint64 PropagateDownloadFileQNAM::committedDiskSpace() const
|
|
|
|
{
|
|
|
|
if (_state == Running) {
|
|
|
|
return qBound(0ULL, _item->_size - _resumeStart - _downloadProgress, _item->_size);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:31:30 +03:00
|
|
|
const char owncloudCustomSoftErrorStringC[] = "owncloud-custom-soft-error-string";
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::slotGetFinished()
|
|
|
|
{
|
|
|
|
_propagator->_activeJobs--;
|
|
|
|
|
|
|
|
GETFileJob *job = qobject_cast<GETFileJob *>(sender());
|
|
|
|
Q_ASSERT(job);
|
|
|
|
|
2014-04-30 13:36:16 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << job->reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< job->reply()->error()
|
2015-07-30 15:27:54 +03:00
|
|
|
<< (job->reply()->error() == QNetworkReply::NoError ? QLatin1String("") : job->reply()->errorString())
|
|
|
|
<< _item->_httpErrorCode
|
|
|
|
<< _tmpFile.size() << _item->_size << job->resumeStart()
|
|
|
|
<< job->reply()->rawHeader("Content-Range") << job->reply()->rawHeader("Content-Length");
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
QNetworkReply::NetworkError err = job->reply()->error();
|
|
|
|
if (err != QNetworkReply::NoError) {
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_httpErrorCode = job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
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) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "server replied 416 to our range request, trying again without";
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "server replied 404, assuming file was deleted";
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if (_tmpFile.size() == 0 || badRangeHeader || fileNotFound) {
|
2014-02-17 16:48:56 +04:00
|
|
|
_tmpFile.close();
|
|
|
|
_tmpFile.remove();
|
2015-04-15 16:19:11 +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
|
2015-04-15 16:19:11 +03:00
|
|
|
qWarning() << "Direct download of" << _item->_directDownloadUrl << "failed. Retrying through owncloud.";
|
|
|
|
_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);
|
2015-03-18 01:31:30 +03:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
&_propagator->_anotherSyncNeeded);
|
2014-04-22 14:34:03 +04:00
|
|
|
}
|
2015-06-05 12:12:21 +03:00
|
|
|
|
2014-04-22 14:34:03 +04:00
|
|
|
done(status, job->errorString());
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_requestDuration = job->duration();
|
|
|
|
_item->_responseTimeStamp = job->responseTimestamp();
|
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");
|
2014-11-27 16:50:49 +03:00
|
|
|
quint64 bodySize = job->reply()->rawHeader(sizeHeader).toULongLong();
|
2014-11-20 20:49:34 +03:00
|
|
|
|
2015-07-30 15:32:33 +03:00
|
|
|
if (!job->reply()->rawHeader(sizeHeader).isEmpty() && _tmpFile.size() > 0 && bodySize == 0) {
|
|
|
|
// 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
|
|
|
|
qDebug() << bodySize << _item->_size << _tmpFile.size() << job->resumeStart();
|
|
|
|
_tmpFile.remove();
|
|
|
|
done(SyncFileItem::NormalError, QLatin1String("Broken webserver returning empty content length for non-empty file on resume"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:50:49 +03:00
|
|
|
if(bodySize > 0 && bodySize != _tmpFile.size() - job->resumeStart() ) {
|
|
|
|
qDebug() << bodySize << _tmpFile.size() << job->resumeStart();
|
2014-11-21 13:03:14 +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;
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:44:12 +03:00
|
|
|
// Do checksum validation for the download. If there is no checksum header, the validator
|
|
|
|
// will also emit the validated() signal to continue the flow in slot downloadFinished()
|
|
|
|
// as this is (still) also correct.
|
2015-05-20 17:54:04 +03:00
|
|
|
TransmissionChecksumValidator *validator = new TransmissionChecksumValidator(_tmpFile.fileName(), this);
|
2015-05-21 16:51:48 +03:00
|
|
|
connect(validator, SIGNAL(validated(QByteArray)), this, SLOT(downloadFinished()));
|
2015-05-20 17:54:04 +03:00
|
|
|
connect(validator, SIGNAL(validationFailed(QString)), this, SLOT(slotChecksumFail(QString)));
|
|
|
|
validator->downloadValidation(job->reply()->rawHeader(checkSumHeaderC));
|
2015-05-15 11:50:55 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:39:26 +03:00
|
|
|
void PropagateDownloadFileQNAM::slotChecksumFail( const QString& errMsg )
|
2015-05-15 11:50:55 +03:00
|
|
|
{
|
2015-05-15 16:39:26 +03:00
|
|
|
_tmpFile.remove();
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, errMsg ); // tr("The file downloaded with a broken checksum, will be redownloaded."));
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 21:52:09 +04:00
|
|
|
QString makeConflictFileName(const QString &fn, const QDateTime &dt)
|
2014-04-22 18:07:01 +04:00
|
|
|
{
|
|
|
|
QString conflictFileName(fn);
|
|
|
|
// Add _conflict-XXXX before the extention.
|
|
|
|
int dotLocation = conflictFileName.lastIndexOf('.');
|
|
|
|
// If no extention, add it at the end (take care of cases like foo/.hidden or foo.bar/file)
|
|
|
|
if (dotLocation <= conflictFileName.lastIndexOf('/') + 1) {
|
|
|
|
dotLocation = conflictFileName.size();
|
|
|
|
}
|
|
|
|
QString timeString = dt.toString("yyyyMMdd-hhmmss");
|
2014-04-22 18:17:39 +04:00
|
|
|
|
|
|
|
// Additional marker
|
|
|
|
QByteArray conflictFileUserName = qgetenv("CSYNC_CONFLICT_FILE_USERNAME");
|
|
|
|
if (conflictFileUserName.isEmpty())
|
|
|
|
conflictFileName.insert(dotLocation, "_conflict-" + timeString);
|
|
|
|
else
|
|
|
|
conflictFileName.insert(dotLocation, "_conflict_" + QString::fromUtf8(conflictFileUserName) + "-" + timeString);
|
|
|
|
|
2014-04-22 18:07:01 +04:00
|
|
|
return conflictFileName;
|
|
|
|
}
|
|
|
|
|
2015-04-09 16:25:34 +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);
|
|
|
|
// Add _recall-XXXX before the extention.
|
|
|
|
int dotLocation = recallFileName.lastIndexOf('.');
|
|
|
|
// If no extention, add it at the end (take care of cases like foo/.hidden or foo.bar/file)
|
|
|
|
if (dotLocation <= recallFileName.lastIndexOf('/') + 1) {
|
|
|
|
dotLocation = recallFileName.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString timeString = QDateTime::currentDateTime().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;
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:23:54 +03:00
|
|
|
static void handleRecallFile(const QString &fn)
|
|
|
|
{
|
|
|
|
qDebug() << "handleRecallFile: " << fn;
|
|
|
|
|
|
|
|
FileSystem::setFileHidden(fn, true);
|
|
|
|
|
|
|
|
QFile file(fn);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qWarning() << "Could not open recall file" << file.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFileInfo existingFile(fn);
|
|
|
|
QDir thisDir = existingFile.dir();
|
|
|
|
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
line.chop(1); // remove trailing \n
|
|
|
|
QString fpath = thisDir.filePath(line);
|
|
|
|
QString rpath = makeRecallFileName(fpath);
|
|
|
|
|
|
|
|
// if previously recalled file exists then remove it (copy will not overwrite it)
|
|
|
|
QFile(rpath).remove();
|
|
|
|
qDebug() << "Copy recall file: " << fpath << " -> " << rpath;
|
|
|
|
QFile::copy(fpath,rpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end namespace
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::downloadFinished()
|
|
|
|
{
|
2015-04-15 16:19:11 +03:00
|
|
|
QString fn = _propagator->getFilePath(_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.
|
2015-04-15 16:19:11 +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!")
|
2015-04-15 16:19:11 +03:00
|
|
|
.arg(QDir::toNativeSeparators(_item->_file)) );
|
2014-10-29 13:23:44 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2014-10-29 13:23:44 +03:00
|
|
|
// In case of conflict, make a backup of the old file
|
|
|
|
// Ignore conflicts where both files are binary equal
|
2015-04-15 16:19:11 +03:00
|
|
|
bool isConflict = _item->_instruction == CSYNC_INSTRUCTION_CONFLICT
|
2014-10-29 13:23:44 +03:00
|
|
|
&& !FileSystem::fileEquals(fn, _tmpFile.fileName());
|
2014-02-17 16:48:56 +04:00
|
|
|
if (isConflict) {
|
2015-03-11 12:51:36 +03:00
|
|
|
QString renameError;
|
2015-04-15 16:19:11 +03:00
|
|
|
QString conflictFileName = makeConflictFileName(fn, Utility::qDateTimeFromTime_t(_item->_modtime));
|
2015-03-11 12:51:36 +03:00
|
|
|
if (!FileSystem::rename(fn, conflictFileName, &renameError)) {
|
2014-02-17 16:48:56 +04:00
|
|
|
//If the rename fails, don't replace it.
|
2015-03-11 12:51:36 +03:00
|
|
|
done(SyncFileItem::SoftError, renameError);
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-05-12 18:10:53 +03:00
|
|
|
qDebug() << "Created conflict file" << fn << "->" << conflictFileName;
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2015-05-12 11:35:28 +03:00
|
|
|
FileSystem::setModTime(_tmpFile.fileName(), _item->_modtime);
|
2015-04-15 16:31:47 +03:00
|
|
|
// We need to fetch the time again because some file system such as FAT have a less than a second
|
|
|
|
// 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
|
|
|
|
2015-05-07 12:41:29 +03:00
|
|
|
if (FileSystem::fileExists(fn)) {
|
|
|
|
// Preserve the existing file permissions.
|
|
|
|
QFileInfo existingFile(fn);
|
|
|
|
if (existingFile.permissions() != _tmpFile.permissions()) {
|
|
|
|
_tmpFile.setPermissions(existingFile.permissions());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-05-12 11:35:28 +03:00
|
|
|
const qint64 expectedSize = _item->log._other_size;
|
|
|
|
const time_t expectedMtime = _item->log._other_modtime;
|
2015-05-07 12:41:29 +03:00
|
|
|
if (! FileSystem::verifyFileUnchanged(fn, expectedSize, expectedMtime)) {
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, tr("File has changed since discovery"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:05:29 +04:00
|
|
|
QString error;
|
2014-11-07 13:41:21 +03:00
|
|
|
_propagator->addTouchedFile(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)) {
|
2015-01-15 22:49:52 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << QString("Rename failed: %1 => %2").arg(_tmpFile.fileName()).arg(fn);
|
2015-05-07 12:41:29 +03:00
|
|
|
|
2014-11-05 13:00:46 +03:00
|
|
|
// If we moved away the original file due to a conflict but can't
|
|
|
|
// put the downloaded file in its place, we are in a bad spot:
|
|
|
|
// If we do nothing the next sync run will assume the user deleted
|
|
|
|
// the file!
|
|
|
|
// To avoid that, the file is removed from the metadata table entirely
|
|
|
|
// which makes it look like we're just about to initially download
|
|
|
|
// it.
|
|
|
|
if (isConflict) {
|
|
|
|
_propagator->_journal->deleteFileRecord(fn);
|
|
|
|
_propagator->_journal->commit("download finished");
|
|
|
|
}
|
2015-05-07 12:41:29 +03:00
|
|
|
|
2015-01-15 22:49:52 +03:00
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, error);
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_journal->setFileRecord(SyncJournalFileRecord(*_item, fn));
|
|
|
|
_propagator->_journal->setDownloadInfo(_item->_file, SyncJournalDb::DownloadInfo());
|
2014-02-17 16:48:56 +04:00
|
|
|
_propagator->_journal->commit("download file start2");
|
|
|
|
done(isConflict ? SyncFileItem::Conflict : SyncFileItem::Success);
|
2015-04-09 16:25:34 +03:00
|
|
|
|
|
|
|
// handle the special recall file
|
2015-05-26 13:33:19 +03:00
|
|
|
if(_item->_file == QLatin1String(".sys.admin#recall#") || _item->_file.endsWith("/.sys.admin#recall#")) {
|
2015-05-21 17:23:54 +03:00
|
|
|
handleRecallFile(fn);
|
2015-04-09 16:25:34 +03:00
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
void PropagateDownloadFileQNAM::slotDownloadProgress(qint64 received, qint64)
|
|
|
|
{
|
2014-08-29 21:23:08 +04:00
|
|
|
if (!_job) return;
|
2015-10-01 12:39:09 +03:00
|
|
|
_downloadProgress = received;
|
|
|
|
emit progress(*_item, _resumeStart + received);
|
2014-03-14 16:03:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::abort()
|
|
|
|
{
|
|
|
|
if (_job && _job->reply())
|
|
|
|
_job->reply()->abort();
|
|
|
|
}
|
|
|
|
|
2014-11-11 14:16:14 +03:00
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|