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.
|
|
|
|
*/
|
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
#include "propagator_qnam.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"
|
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
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
2014-09-17 15:35:54 +04:00
|
|
|
/**
|
|
|
|
* The mtime of a file must be at least this many milliseconds in
|
|
|
|
* the past for an upload to be started. Otherwise the propagator will
|
|
|
|
* assume it's still being changed and skip it.
|
|
|
|
*
|
|
|
|
* This value must be smaller than the msBetweenRequestAndSync in
|
|
|
|
* the folder manager.
|
|
|
|
*
|
|
|
|
* Two seconds has shown to be a good value in tests.
|
|
|
|
*/
|
|
|
|
static int minFileAgeForUpload = 2000;
|
|
|
|
|
2014-08-26 14:30:00 +04:00
|
|
|
static qint64 chunkSize() {
|
2014-04-30 19:54:14 +04:00
|
|
|
static uint chunkSize;
|
|
|
|
if (!chunkSize) {
|
|
|
|
chunkSize = qgetenv("OWNCLOUD_CHUNK_SIZE").toUInt();
|
|
|
|
if (chunkSize == 0) {
|
2014-08-27 12:35:58 +04:00
|
|
|
chunkSize = 20*1024*1024; // default to 20 MiB
|
2014-04-30 19:54:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return chunkSize;
|
|
|
|
}
|
|
|
|
|
2014-08-29 18:23:44 +04:00
|
|
|
static QByteArray get_etag_from_reply(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
QByteArray ret = parseEtag(reply->rawHeader("OC-ETag"));
|
|
|
|
if (ret.isEmpty()) {
|
|
|
|
ret = parseEtag(reply->rawHeader("ETag"));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-06 23:33:17 +04:00
|
|
|
/**
|
|
|
|
* Fiven an error from the network, map to a SyncFileItem::Status error
|
|
|
|
*/
|
|
|
|
static SyncFileItem::Status classifyError(QNetworkReply::NetworkError nerror, int httpCode) {
|
|
|
|
Q_ASSERT (nerror != QNetworkReply::NoError); // we should only be called when there is an error
|
|
|
|
|
|
|
|
if (nerror > QNetworkReply::NoError && nerror <= QNetworkReply::UnknownProxyError) {
|
|
|
|
// network error or proxy error -> fatal
|
|
|
|
return SyncFileItem::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (httpCode == 412) {
|
|
|
|
// "Precondition Failed"
|
|
|
|
// Happens when the e-tag has changed
|
|
|
|
return SyncFileItem::SoftError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SyncFileItem::NormalError;
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
void PUTFileJob::start() {
|
|
|
|
QNetworkRequest req;
|
|
|
|
for(QMap<QByteArray, QByteArray>::const_iterator it = _headers.begin(); it != _headers.end(); ++it) {
|
|
|
|
req.setRawHeader(it.key(), it.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
setReply(davRequest("PUT", path(), req, _device));
|
|
|
|
_device->setParent(reply());
|
|
|
|
setupConnections(reply());
|
|
|
|
|
|
|
|
if( reply()->error() != QNetworkReply::NoError ) {
|
2014-03-06 23:33:17 +04:00
|
|
|
qWarning() << Q_FUNC_INFO << " Network error: " << reply()->errorString();
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
|
|
|
|
connect(reply(), SIGNAL(uploadProgress(qint64,qint64)), this, SIGNAL(uploadProgress(qint64,qint64)));
|
2014-09-18 16:00:51 +04:00
|
|
|
connect(this, SIGNAL(networkActivity()), account(), SIGNAL(propagatorNetworkActivity()));
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2014-04-30 19:54:14 +04:00
|
|
|
void PUTFileJob::slotTimeout() {
|
|
|
|
_errorString = tr("Connection Timeout");
|
|
|
|
reply()->abort();
|
2014-03-28 14:11:02 +04:00
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
void PropagateUploadFileQNAM::start()
|
2014-02-10 16:00:22 +04:00
|
|
|
{
|
2014-02-13 17:02:05 +04:00
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-09-03 14:11:03 +04:00
|
|
|
_file = new QFile(_propagator->getFilePath(_item._file), this);
|
2014-02-13 17:02:05 +04:00
|
|
|
if (!_file->open(QIODevice::ReadOnly)) {
|
|
|
|
done(SyncFileItem::NormalError, _file->errorString());
|
|
|
|
delete _file;
|
|
|
|
return;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 15:35:54 +04:00
|
|
|
// Update the mtime and size, it might have changed since discovery.
|
|
|
|
_item._modtime = FileSystem::getModTime(_file->fileName());
|
2014-02-13 17:02:05 +04:00
|
|
|
quint64 fileSize = _file->size();
|
2014-09-17 15:35:54 +04:00
|
|
|
_item._size = fileSize;
|
|
|
|
|
|
|
|
// But skip the file if the mtime is too close to 'now'!
|
|
|
|
// That usually indicates a file that is still being changed
|
|
|
|
// or not yet fully copied to the destination.
|
|
|
|
QDateTime modtime = Utility::qDateTimeFromTime_t(_item._modtime);
|
|
|
|
if (modtime.msecsTo(QDateTime::currentDateTime()) < minFileAgeForUpload) {
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
done(SyncFileItem::SoftError, tr("Local file changed during sync."));
|
|
|
|
delete _file;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-28 14:11:02 +04:00
|
|
|
_chunkCount = std::ceil(fileSize/double(chunkSize()));
|
2014-02-13 17:02:05 +04:00
|
|
|
_startChunk = 0;
|
|
|
|
_transferId = qrand() ^ _item._modtime ^ (_item._size << 16);
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
const SyncJournalDb::UploadInfo progressInfo = _propagator->_journal->getUploadInfo(_item._file);
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
if (progressInfo._valid && Utility::qDateTimeToTime_t(progressInfo._modtime) == _item._modtime ) {
|
|
|
|
_startChunk = progressInfo._chunk;
|
|
|
|
_transferId = progressInfo._transferid;
|
|
|
|
qDebug() << Q_FUNC_INFO << _item._file << ": Resuming from chunk " << _startChunk;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
_currentChunk = 0;
|
2014-03-26 20:58:32 +04:00
|
|
|
_duration.start();
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
_propagator->_activeJobs++;
|
2014-03-14 16:03:16 +04:00
|
|
|
emit progress(_item, 0);
|
2014-02-13 17:02:05 +04:00
|
|
|
emitReady();
|
|
|
|
this->startNextChunk();
|
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
struct ChunkDevice : QIODevice {
|
2014-04-04 17:41:35 +04:00
|
|
|
public:
|
2014-07-14 21:53:42 +04:00
|
|
|
QPointer<QIODevice> _file;
|
2014-02-13 17:02:05 +04:00
|
|
|
qint64 _read;
|
2014-04-04 17:41:35 +04:00
|
|
|
qint64 _size;
|
|
|
|
qint64 _start;
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-04-04 17:41:35 +04:00
|
|
|
ChunkDevice(QIODevice *file, qint64 start, qint64 size)
|
|
|
|
: QIODevice(file), _file(file), _read(0), _size(size), _start(start) {
|
2014-07-14 21:53:42 +04:00
|
|
|
_file = QPointer<QIODevice>(file);
|
|
|
|
_file.data()->seek(start);
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual qint64 writeData(const char* , qint64 ) Q_DECL_OVERRIDE {
|
2014-02-13 17:02:05 +04:00
|
|
|
Q_ASSERT(!"write to read only device");
|
|
|
|
return 0;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
2014-02-13 17:02:05 +04:00
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual qint64 readData(char* data, qint64 maxlen) Q_DECL_OVERRIDE {
|
2014-07-14 21:53:42 +04:00
|
|
|
if (_file.isNull()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Upload file object deleted during upload";
|
|
|
|
close();
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-28 14:11:02 +04:00
|
|
|
maxlen = qMin(maxlen, chunkSize() - _read);
|
2014-02-13 17:02:05 +04:00
|
|
|
if (maxlen == 0)
|
|
|
|
return 0;
|
2014-07-14 21:53:42 +04:00
|
|
|
qint64 ret = _file.data()->read(data, maxlen);
|
2014-04-05 16:25:41 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
2014-02-13 17:02:05 +04:00
|
|
|
_read += ret;
|
|
|
|
return ret;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual bool atEnd() const Q_DECL_OVERRIDE {
|
2014-07-14 21:53:42 +04:00
|
|
|
if (_file.isNull()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Upload file object deleted during upload";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return _read >= chunkSize() || _file.data()->atEnd();
|
2014-02-13 17:02:05 +04:00
|
|
|
}
|
2014-04-04 17:41:35 +04:00
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual qint64 size() const Q_DECL_OVERRIDE{
|
2014-04-04 17:41:35 +04:00
|
|
|
return _size;
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
qint64 bytesAvailable() const Q_DECL_OVERRIDE
|
2014-04-05 16:25:41 +04:00
|
|
|
{
|
|
|
|
return _size - _read + QIODevice::bytesAvailable();
|
|
|
|
}
|
|
|
|
|
2014-04-04 17:41:35 +04:00
|
|
|
// random access, we can seek
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual bool isSequential() const Q_DECL_OVERRIDE{
|
2014-04-04 17:41:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual bool seek ( qint64 pos ) Q_DECL_OVERRIDE {
|
2014-07-14 21:53:42 +04:00
|
|
|
if (_file.isNull()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Upload file object deleted during upload";
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-05 16:25:41 +04:00
|
|
|
_read = pos;
|
2014-07-14 21:53:42 +04:00
|
|
|
return _file.data()->seek(pos + _start);
|
2014-04-04 17:41:35 +04:00
|
|
|
}
|
2014-02-13 17:02:05 +04:00
|
|
|
};
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
void PropagateUploadFileQNAM::startNextChunk()
|
2014-02-06 14:50:16 +04:00
|
|
|
{
|
2014-02-17 16:48:56 +04:00
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
quint64 fileSize = _item._size;
|
|
|
|
QMap<QByteArray, QByteArray> headers;
|
|
|
|
headers["OC-Total-Length"] = QByteArray::number(fileSize);
|
|
|
|
headers["Content-Type"] = "application/octet-stream";
|
|
|
|
headers["X-OC-Mtime"] = QByteArray::number(qint64(_item._modtime));
|
2014-07-15 15:33:13 +04:00
|
|
|
if (!_item._etag.isEmpty() && _item._etag != "empty_etag" &&
|
|
|
|
_item._instruction != CSYNC_INSTRUCTION_NEW // On new files never send a If-Match
|
|
|
|
) {
|
2014-02-13 17:02:05 +04:00
|
|
|
// We add quotes because the owncloud server always add quotes around the etag, and
|
|
|
|
// csync_owncloud.c's owncloud_file_id always strip the quotes.
|
|
|
|
headers["If-Match"] = '"' + _item._etag + '"';
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
QString path = _item._file;
|
2014-06-16 15:35:50 +04:00
|
|
|
QIODevice *device = 0;
|
2014-02-13 17:02:05 +04:00
|
|
|
if (_chunkCount > 1) {
|
|
|
|
int sendingChunk = (_currentChunk + _startChunk) % _chunkCount;
|
2014-03-28 14:11:02 +04:00
|
|
|
// XOR with chunk size to make sure everything goes well if chunk size change between runs
|
|
|
|
uint transid = _transferId ^ chunkSize();
|
|
|
|
path += QString("-chunking-%1-%2-%3").arg(transid).arg(_chunkCount).arg(sendingChunk);
|
2014-02-13 17:02:05 +04:00
|
|
|
headers["OC-Chunked"] = "1";
|
2014-04-04 17:41:35 +04:00
|
|
|
int currentChunkSize = chunkSize();
|
2014-04-07 18:29:06 +04:00
|
|
|
if (sendingChunk == _chunkCount - 1) { // last chunk
|
2014-04-04 17:41:35 +04:00
|
|
|
currentChunkSize = (fileSize % chunkSize());
|
2014-04-07 18:29:06 +04:00
|
|
|
if( currentChunkSize == 0 ) { // if the last chunk pretents to be 0, its actually the full chunk size.
|
|
|
|
currentChunkSize = chunkSize();
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 14:30:00 +04:00
|
|
|
device = new ChunkDevice(_file, chunkSize() * quint64(sendingChunk), currentChunkSize);
|
2014-02-10 16:00:22 +04:00
|
|
|
} else {
|
2014-02-13 17:02:05 +04:00
|
|
|
device = _file;
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
|
|
|
|
2014-06-16 15:35:50 +04:00
|
|
|
bool isOpen = true;
|
|
|
|
if (!device->isOpen()) {
|
|
|
|
isOpen = device->open(QIODevice::ReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( isOpen ) {
|
|
|
|
_job = new PUTFileJob(AccountManager::instance()->account(), _propagator->_remoteFolder + path, device, headers);
|
|
|
|
_job->setTimeout(_propagator->httpTimeout() * 1000);
|
|
|
|
connect(_job, SIGNAL(finishedSignal()), this, SLOT(slotPutFinished()));
|
|
|
|
connect(_job, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(slotUploadProgress(qint64,qint64)));
|
|
|
|
_job->start();
|
|
|
|
} else {
|
|
|
|
qDebug() << "ERR: Could not open upload file: " << device->errorString();
|
|
|
|
done( SyncFileItem::NormalError, device->errorString() );
|
2014-06-20 12:34:07 +04:00
|
|
|
delete device;
|
2014-06-16 15:35:50 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadFileQNAM::slotPutFinished()
|
|
|
|
{
|
|
|
|
PUTFileJob *job = qobject_cast<PUTFileJob *>(sender());
|
|
|
|
Q_ASSERT(job);
|
|
|
|
|
2014-04-04 17:41:35 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << job->reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< job->reply()->error()
|
|
|
|
<< (job->reply()->error() == QNetworkReply::NoError ? QLatin1String("") : job->reply()->errorString())
|
|
|
|
<< job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute)
|
|
|
|
<< job->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute);
|
2014-02-06 17:52:56 +04:00
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
QNetworkReply::NetworkError err = job->reply()->error();
|
|
|
|
if (err != QNetworkReply::NoError) {
|
|
|
|
_item._httpErrorCode = job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2014-02-13 17:02:05 +04:00
|
|
|
_propagator->_activeJobs--;
|
2014-02-27 15:02:22 +04:00
|
|
|
if(checkForProblemsWithShared(_item._httpErrorCode,
|
|
|
|
tr("The file was edited locally but is part of a read only share. "
|
|
|
|
"It is restored and your edit is in the conflict file."))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-30 19:54:14 +04:00
|
|
|
QString errorString = job->errorString();
|
2014-02-27 15:02:22 +04:00
|
|
|
|
2014-03-26 15:02:22 +04:00
|
|
|
QByteArray replyContent = job->reply()->readAll();
|
|
|
|
qDebug() << replyContent; // display the XML error in the debug
|
|
|
|
QRegExp rx("<s:message>(.*)</s:message>"); // Issue #1366: display server exception
|
|
|
|
if (rx.indexIn(QString::fromUtf8(replyContent)) != -1) {
|
|
|
|
errorString += QLatin1String(" (") + rx.cap(1) + QLatin1Char(')');
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:22:40 +04:00
|
|
|
if (_item._httpErrorCode == 412) {
|
|
|
|
// Precondition Failed: Maybe the bad etag is in the database, we need to clear the
|
|
|
|
// parent folder etag so we won't read from DB next sync.
|
|
|
|
_propagator->_journal->avoidReadFromDbOnNextSync(_item._file);
|
2014-10-11 17:39:35 +04:00
|
|
|
_propagator->_anotherSyncNeeded = true;
|
2014-06-03 19:22:40 +04:00
|
|
|
}
|
|
|
|
|
2014-03-26 15:02:22 +04:00
|
|
|
done(classifyError(err, _item._httpErrorCode), errorString);
|
2014-02-06 14:50:16 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-29 18:23:44 +04:00
|
|
|
bool finished = job->reply()->hasRawHeader("ETag")
|
|
|
|
|| job->reply()->hasRawHeader("OC-ETag");
|
2014-03-06 19:04:32 +04:00
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
if (!finished) {
|
2014-09-03 14:11:03 +04:00
|
|
|
QFileInfo fi(_propagator->getFilePath(_item._file));
|
2014-06-16 15:34:59 +04:00
|
|
|
if( !fi.exists() ) {
|
|
|
|
_propagator->_activeJobs--;
|
|
|
|
done(SyncFileItem::SoftError, tr("The local file was removed during sync."));
|
|
|
|
return;
|
|
|
|
}
|
2014-03-06 19:04:32 +04:00
|
|
|
|
2014-09-04 13:20:41 +04:00
|
|
|
const time_t new_mtime = FileSystem::getModTime(fi.absoluteFilePath());
|
2014-09-05 16:01:26 +04:00
|
|
|
const quint64 new_size = static_cast<quint64>(fi.size());
|
|
|
|
if (new_mtime != _item._modtime || new_size != _item._size) {
|
|
|
|
qDebug() << "The local file has changed during upload:"
|
|
|
|
<< "mtime: " << _item._modtime << "<->" << new_mtime
|
|
|
|
<< ", size: " << _item._size << "<->" << new_size
|
2014-09-04 13:20:41 +04:00
|
|
|
<< ", QFileInfo: " << Utility::qDateTimeToTime_t(fi.lastModified()) << fi.lastModified();
|
2014-03-06 19:04:32 +04:00
|
|
|
_propagator->_activeJobs--;
|
2014-09-10 19:25:13 +04:00
|
|
|
_propagator->_anotherSyncNeeded = true;
|
2014-03-06 19:04:32 +04:00
|
|
|
done(SyncFileItem::SoftError, tr("Local file changed during sync."));
|
2014-03-06 23:33:17 +04:00
|
|
|
// FIXME: the legacy code was retrying for a few seconds.
|
|
|
|
// and also checking that after the last chunk, and removed the file in case of INSTRUCTION_NEW
|
2014-03-06 19:04:32 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:02:05 +04:00
|
|
|
// Proceed to next chunk.
|
|
|
|
_currentChunk++;
|
|
|
|
if (_currentChunk >= _chunkCount) {
|
|
|
|
_propagator->_activeJobs--;
|
2014-05-24 17:04:42 +04:00
|
|
|
done(SyncFileItem::NormalError, tr("The server did not acknowledge the last chunk. (No e-tag were present)"));
|
2014-02-13 17:02:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncJournalDb::UploadInfo pi;
|
|
|
|
pi._valid = true;
|
2014-03-14 16:03:16 +04:00
|
|
|
pi._chunk = (_currentChunk + _startChunk) % _chunkCount; // next chunk to start with
|
2014-02-13 17:02:05 +04:00
|
|
|
pi._transferid = _transferId;
|
|
|
|
pi._modtime = Utility::qDateTimeFromTime_t(_item._modtime);
|
|
|
|
_propagator->_journal->setUploadInfo(_item._file, pi);
|
|
|
|
_propagator->_journal->commit("Upload info");
|
|
|
|
startNextChunk();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-06 11:30:36 +04:00
|
|
|
// the following code only happens after all chunks were uploaded.
|
|
|
|
//
|
2014-02-06 14:50:16 +04:00
|
|
|
// the file id should only be empty for new files up- or downloaded
|
2014-03-24 15:21:44 +04:00
|
|
|
QByteArray fid = job->reply()->rawHeader("OC-FileID");
|
2014-02-06 14:50:16 +04:00
|
|
|
if( !fid.isEmpty() ) {
|
|
|
|
if( !_item._fileId.isEmpty() && _item._fileId != fid ) {
|
|
|
|
qDebug() << "WARN: File ID changed!" << _item._fileId << fid;
|
|
|
|
}
|
|
|
|
_item._fileId = fid;
|
|
|
|
}
|
|
|
|
|
2014-08-29 18:23:44 +04:00
|
|
|
QByteArray etag = get_etag_from_reply(job->reply());
|
|
|
|
_item._etag = etag;
|
|
|
|
|
2014-04-29 18:47:07 +04:00
|
|
|
_item._responseTimeStamp = job->responseTimestamp();
|
2014-02-06 14:50:16 +04:00
|
|
|
|
|
|
|
if (job->reply()->rawHeader("X-OC-MTime") != "accepted") {
|
2014-04-29 18:47:07 +04:00
|
|
|
// X-OC-MTime is supported since owncloud 5.0. But not when chunking.
|
|
|
|
// Normaly Owncloud 6 always put X-OC-MTime
|
|
|
|
qDebug() << "Server do not support X-OC-MTime";
|
|
|
|
PropagatorJob *newJob = new UpdateMTimeAndETagJob(_propagator, _item);
|
2014-04-30 12:10:32 +04:00
|
|
|
QObject::connect(newJob, SIGNAL(completed(SyncFileItem)), this, SLOT(finalize(SyncFileItem)));
|
2014-04-29 18:47:07 +04:00
|
|
|
QMetaObject::invokeMethod(newJob, "start");
|
2014-02-06 14:50:16 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-30 12:10:32 +04:00
|
|
|
finalize(_item);
|
2014-04-29 18:47:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-30 12:10:32 +04:00
|
|
|
void PropagateUploadFileQNAM::finalize(const SyncFileItem ©)
|
2014-04-29 18:47:07 +04:00
|
|
|
{
|
2014-04-30 12:10:32 +04:00
|
|
|
// Normally, copy == _item, but when it comes from the UpdateMTimeAndETagJob, we need to do
|
|
|
|
// some updates
|
|
|
|
_item._etag = copy._etag;
|
|
|
|
_item._fileId = copy._fileId;
|
|
|
|
|
|
|
|
_propagator->_activeJobs--;
|
|
|
|
|
2014-03-26 20:58:32 +04:00
|
|
|
_item._requestDuration = _duration.elapsed();
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-09-03 14:11:03 +04:00
|
|
|
_propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, _propagator->getFilePath(_item._file)));
|
2014-02-06 14:50:16 +04:00
|
|
|
// Remove from the progress database:
|
|
|
|
_propagator->_journal->setUploadInfo(_item._file, SyncJournalDb::UploadInfo());
|
|
|
|
_propagator->_journal->commit("upload file start");
|
|
|
|
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
void PropagateUploadFileQNAM::slotUploadProgress(qint64 sent, qint64)
|
|
|
|
{
|
|
|
|
int progressChunk = _currentChunk + _startChunk;
|
|
|
|
if (progressChunk >= _chunkCount)
|
|
|
|
progressChunk = _currentChunk;
|
2014-08-29 18:06:56 +04:00
|
|
|
emit progress(_item, sent + progressChunk * chunkSize());
|
2014-03-14 16:03:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-06 17:52:56 +04:00
|
|
|
void PropagateUploadFileQNAM::abort()
|
|
|
|
{
|
2014-04-05 16:25:41 +04:00
|
|
|
if (_job && _job->reply()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << this->_item._file;
|
2014-02-06 17:52:56 +04:00
|
|
|
_job->reply()->abort();
|
2014-04-05 16:25:41 +04:00
|
|
|
}
|
2014-02-06 17:52:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
// DOES NOT take owncership of the device.
|
2014-07-18 18:59:29 +04:00
|
|
|
GETFileJob::GETFileJob(Account* account, const QString& path, QFile *device,
|
2014-06-03 13:50:13 +04:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, QByteArray expectedEtagForResume,
|
2014-07-18 18:59:29 +04:00
|
|
|
quint64 _resumeStart, QObject* parent)
|
2014-06-03 13:50:13 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent),
|
|
|
|
_device(device), _headers(headers), _expectedEtagForResume(expectedEtagForResume),
|
2014-07-18 18:59:29 +04:00
|
|
|
_resumeStart(_resumeStart) , _errorStatus(SyncFileItem::NoStatus)
|
2014-06-03 13:50:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:59:29 +04:00
|
|
|
GETFileJob::GETFileJob(Account* account, const QUrl& url, QFile *device,
|
2014-06-03 13:50:13 +04:00
|
|
|
const QMap<QByteArray, QByteArray> &headers,
|
|
|
|
QObject* parent)
|
|
|
|
: AbstractNetworkJob(account, url.toEncoded(), parent),
|
2014-07-18 18:59:29 +04:00
|
|
|
_device(device), _headers(headers), _resumeStart(0),
|
2014-06-03 13:50:13 +04:00
|
|
|
_errorStatus(SyncFileItem::NoStatus), _directDownloadUrl(url)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-02-17 16:48:56 +04:00
|
|
|
setupConnections(reply());
|
2014-07-24 17:41:09 +04:00
|
|
|
reply()->setReadBufferSize(128 * 1024);
|
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-09-18 16:00:51 +04:00
|
|
|
connect(this, SIGNAL(networkActivity()), account(), SIGNAL(propagatorNetworkActivity()));
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:26:40 +04:00
|
|
|
void GETFileJob::slotMetaDataChanged()
|
|
|
|
{
|
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-08-29 18:23:44 +04:00
|
|
|
_etag = get_etag_from_reply(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;
|
|
|
|
if (start == 0) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:26:40 +04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
while(reply()->bytesAvailable() > 0) {
|
|
|
|
qint64 r = reply()->read(buffer.data(), bufferSize);
|
|
|
|
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-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
void GETFileJob::slotTimeout()
|
|
|
|
{
|
|
|
|
_errorString = tr("Connection Timeout");
|
|
|
|
_errorStatus = SyncFileItem::FatalError;
|
|
|
|
reply()->abort();
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << _item._file << _propagator->_activeJobs;
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
// do a klaas' case clash check.
|
2014-05-26 20:18:32 +04: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!")
|
|
|
|
.arg(QDir::toNativeSeparators(_item._file)) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:03:16 +04: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;
|
2014-02-17 16:48:56 +04:00
|
|
|
const SyncJournalDb::DownloadInfo progressInfo = _propagator->_journal->getDownloadInfo(_item._file);
|
|
|
|
if (progressInfo._valid) {
|
|
|
|
// if the etag has changed meanwhile, remove the already downloaded part.
|
|
|
|
if (progressInfo._etag != _item._etag) {
|
2014-09-03 14:11:03 +04:00
|
|
|
QFile::remove(_propagator->getFilePath(progressInfo._tmpfile));
|
2014-02-17 16:48:56 +04:00
|
|
|
_propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());
|
|
|
|
} 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()) {
|
|
|
|
tmpFileName = _item._file;
|
|
|
|
//add a dot at the begining of the filename to hide the file.
|
|
|
|
int slashPos = tmpFileName.lastIndexOf('/');
|
|
|
|
tmpFileName.insert(slashPos+1, '.');
|
|
|
|
//add the suffix
|
|
|
|
tmpFileName += ".~" + QString::number(uint(qrand()), 16);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
{
|
|
|
|
SyncJournalDb::DownloadInfo pi;
|
|
|
|
pi._etag = _item._etag;
|
|
|
|
pi._tmpfile = tmpFileName;
|
|
|
|
pi._valid = true;
|
|
|
|
_propagator->_journal->setDownloadInfo(_item._file, pi);
|
|
|
|
_propagator->_journal->commit("download file start");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QMap<QByteArray, QByteArray> headers;
|
|
|
|
|
2014-10-08 14:04:17 +04:00
|
|
|
quint64 startSize = _tmpFile.size();
|
|
|
|
if (startSize > 0) {
|
|
|
|
if (startSize == _item._size) {
|
2014-02-17 16:48:56 +04:00
|
|
|
qDebug() << "File is already complete, no need to download";
|
|
|
|
downloadFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (_item._directDownloadUrl.isEmpty()) {
|
|
|
|
// Normal job, download from oC instance
|
|
|
|
_job = new GETFileJob(AccountManager::instance()->account(),
|
2014-07-18 18:59:29 +04:00
|
|
|
_propagator->_remoteFolder + _item._file,
|
2014-08-29 21:23:08 +04:00
|
|
|
&_tmpFile, headers, expectedEtagForResume, startSize);
|
2014-06-03 13:50:13 +04:00
|
|
|
} else {
|
|
|
|
// We were provided a direct URL, use that one
|
2014-10-08 14:04:17 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "directDownloadUrl given for " << _item._file << _item._directDownloadUrl;
|
|
|
|
|
|
|
|
// Direct URLs don't support resuming, so clear an existing tmp file
|
|
|
|
if (startSize > 0) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "resuming not supported for directDownloadUrl, deleting temporary";
|
|
|
|
_tmpFile.close();
|
|
|
|
if (!_tmpFile.open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
|
|
|
done(SyncFileItem::NormalError, _tmpFile.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startSize = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (!_item._directDownloadCookies.isEmpty()) {
|
|
|
|
headers["Cookie"] = _item._directDownloadCookies.toUtf8();
|
|
|
|
}
|
2014-10-08 14:04:17 +04:00
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
QUrl url = QUrl::fromUserInput(_item._directDownloadUrl);
|
|
|
|
_job = new GETFileJob(AccountManager::instance()->account(),
|
|
|
|
url,
|
|
|
|
&_tmpFile, headers);
|
|
|
|
}
|
2014-05-02 15:04:53 +04:00
|
|
|
_job->setTimeout(_propagator->httpTimeout() * 1000);
|
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();
|
|
|
|
emitReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
<< (job->reply()->error() == QNetworkReply::NoError ? QLatin1String("") : job->reply()->errorString());
|
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
|
|
|
_item._httpErrorCode = job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
// If we sent a 'Range' header and get 416 back, we want to retry
|
|
|
|
// without the header.
|
|
|
|
bool badRangeHeader = job->resumeStart() > 0 && _item._httpErrorCode == 416;
|
|
|
|
if (badRangeHeader) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "server replied 416 to our range request, trying again without";
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't keep the temporary file if it is empty or we
|
|
|
|
// used a bad range header.
|
|
|
|
if (_tmpFile.size() == 0 || badRangeHeader) {
|
2014-02-17 16:48:56 +04:00
|
|
|
_tmpFile.close();
|
|
|
|
_tmpFile.remove();
|
|
|
|
_propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());
|
|
|
|
}
|
2014-10-08 14:04:17 +04:00
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
_propagator->_activeJobs--;
|
2014-04-22 14:34:03 +04:00
|
|
|
SyncFileItem::Status status = job->errorStatus();
|
|
|
|
if (status == SyncFileItem::NoStatus) {
|
|
|
|
status = classifyError(err, _item._httpErrorCode);
|
|
|
|
}
|
2014-10-08 14:04:17 +04:00
|
|
|
if (badRangeHeader) {
|
|
|
|
// Can't do this in classifyError() because 416 without a
|
|
|
|
// Range header should result in NormalError.
|
|
|
|
status = SyncFileItem::SoftError;
|
|
|
|
}
|
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
|
|
|
|
_item._etag = parseEtag(job->etag());
|
|
|
|
}
|
2014-03-26 20:58:32 +04:00
|
|
|
_item._requestDuration = job->duration();
|
|
|
|
_item._responseTimeStamp = job->responseTimestamp();
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
_tmpFile.close();
|
|
|
|
_tmpFile.flush();
|
|
|
|
downloadFinished();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
void PropagateDownloadFileQNAM::downloadFinished()
|
|
|
|
{
|
|
|
|
|
2014-09-03 14:11:03 +04: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.
|
|
|
|
if (_propagator->localFileNameClash(_item._file)) {
|
|
|
|
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
|
|
|
|
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
|
2014-02-17 16:48:56 +04: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) {
|
|
|
|
QFile f(fn);
|
2014-04-22 18:07:01 +04:00
|
|
|
QString conflictFileName = makeConflictFileName(fn, Utility::qDateTimeFromTime_t(_item._modtime));
|
2014-02-17 16:48:56 +04:00
|
|
|
if (!f.rename(conflictFileName)) {
|
|
|
|
//If the rename fails, don't replace it.
|
|
|
|
done(SyncFileItem::NormalError, f.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo existingFile(fn);
|
|
|
|
if(existingFile.exists() && existingFile.permissions() != _tmpFile.permissions()) {
|
|
|
|
_tmpFile.setPermissions(existingFile.permissions());
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:54:40 +04:00
|
|
|
FileSystem::setFileHidden(_tmpFile.fileName(), false);
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2014-02-18 17:05:29 +04:00
|
|
|
QString error;
|
|
|
|
if (!FileSystem::renameReplace(_tmpFile.fileName(), fn, &error)) {
|
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");
|
|
|
|
_propagator->_anotherSyncNeeded = true;
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:05:29 +04:00
|
|
|
done(SyncFileItem::NormalError, error);
|
2014-02-17 16:48:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-18 17:05:29 +04:00
|
|
|
|
2014-09-24 23:06:59 +04:00
|
|
|
existingFile.refresh();
|
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.
|
2014-02-18 15:54:40 +04:00
|
|
|
FileSystem::setModTime(fn, _item._modtime);
|
2014-09-05 16:01:26 +04:00
|
|
|
_item._size = existingFile.size();
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
_propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));
|
|
|
|
_propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());
|
|
|
|
_propagator->_journal->commit("download file start2");
|
|
|
|
done(isConflict ? SyncFileItem::Conflict : SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
emit progress(_item, received + _job->resumeStart());
|
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-02-06 14:50:16 +04:00
|
|
|
}
|