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 "propagateupload.h"
|
|
|
|
#include "owncloudpropagator_p.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-11-23 14:09:25 +03:00
|
|
|
#include "checksums.h"
|
2015-11-20 17:14:22 +03:00
|
|
|
#include "syncengine.h"
|
2016-01-06 12:01:22 +03:00
|
|
|
#include "propagateremotedelete.h"
|
2015-05-12 17:36:40 +03:00
|
|
|
|
2014-07-25 15:30:48 +04: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>
|
2014-02-13 17:02:05 +04:00
|
|
|
#include <cmath>
|
2015-01-14 14:48:38 +03:00
|
|
|
#include <cstring>
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2015-04-27 16:04:44 +03:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 4, 2)
|
|
|
|
namespace {
|
|
|
|
const char owncloudShouldSoftCancelPropertyName[] = "owncloud-should-soft-cancel";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 16:20:13 +03:00
|
|
|
namespace OCC {
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-09-17 15:35:54 +04:00
|
|
|
/**
|
2015-02-26 13:00:06 +03:00
|
|
|
* We do not want to upload files that are currently being modified.
|
|
|
|
* To avoid that, we don't upload files that have a modification time
|
|
|
|
* that is too close to the current time.
|
2014-09-17 15:35:54 +04:00
|
|
|
*
|
2015-02-26 13:00:06 +03:00
|
|
|
* This interacts with the msBetweenRequestAndSync delay in the folder
|
|
|
|
* manager. If that delay between file-change notification and sync
|
|
|
|
* has passed, we should accept the file for upload here.
|
2014-09-17 15:35:54 +04:00
|
|
|
*/
|
2015-02-26 13:00:06 +03:00
|
|
|
static bool fileIsStillChanging(const SyncFileItem & item)
|
|
|
|
{
|
|
|
|
const QDateTime modtime = Utility::qDateTimeFromTime_t(item._modtime);
|
|
|
|
const qint64 msSinceMod = modtime.msecsTo(QDateTime::currentDateTime());
|
|
|
|
|
2015-11-20 17:14:22 +03:00
|
|
|
return msSinceMod < SyncEngine::minimumFileAgeForUpload
|
2015-02-26 13:00:06 +03:00
|
|
|
// if the mtime is too much in the future we *do* upload the file
|
|
|
|
&& msSinceMod > -10000;
|
|
|
|
}
|
2014-09-17 15:35:54 +04:00
|
|
|
|
2015-03-23 18:13:52 +03:00
|
|
|
PUTFileJob::~PUTFileJob()
|
|
|
|
{
|
|
|
|
// Make sure that we destroy the QNetworkReply before our _device of which it keeps an internal pointer.
|
|
|
|
setReply(0);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:48:56 +03:00
|
|
|
setReply(_url.isValid() ? davRequest("PUT", _url, req, _device.data())
|
|
|
|
: davRequest("PUT", path(), req, _device.data()));
|
2014-02-06 14:50:16 +04:00
|
|
|
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-12-18 14:09:48 +03:00
|
|
|
connect(this, SIGNAL(networkActivity()), account().data(), SIGNAL(propagatorNetworkActivity()));
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2015-04-27 16:04:44 +03:00
|
|
|
// For Qt versions not including https://codereview.qt-project.org/110150
|
|
|
|
// Also do the runtime check if compiled with an old Qt but running with fixed one.
|
2015-05-22 15:19:41 +03:00
|
|
|
// (workaround disabled on windows and mac because the binaries we ship have patched qt)
|
2015-04-27 16:04:44 +03:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(4, 8, 7)
|
|
|
|
if (QLatin1String(qVersion()) < QLatin1String("4.8.7"))
|
|
|
|
connect(_device.data(), SIGNAL(wasReset()), this, SLOT(slotSoftAbort()));
|
2015-05-22 15:19:41 +03:00
|
|
|
#elif QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(5, 4, 2) && !defined Q_OS_WIN && !defined Q_OS_MAC
|
2015-04-27 16:04:44 +03:00
|
|
|
if (QLatin1String(qVersion()) < QLatin1String("5.4.2"))
|
|
|
|
connect(_device.data(), SIGNAL(wasReset()), this, SLOT(slotSoftAbort()));
|
|
|
|
#endif
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2014-04-30 19:54:14 +04:00
|
|
|
void PUTFileJob::slotTimeout() {
|
2016-06-15 15:32:25 +03:00
|
|
|
qDebug() << "Timeout" << (reply() ? reply()->request().url() : path());
|
|
|
|
if (!reply())
|
|
|
|
return;
|
2014-04-30 19:54:14 +04:00
|
|
|
_errorString = tr("Connection Timeout");
|
|
|
|
reply()->abort();
|
2014-03-28 14:11:02 +04:00
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2015-04-27 16:04:44 +03:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 4, 2)
|
|
|
|
void PUTFileJob::slotSoftAbort() {
|
|
|
|
reply()->setProperty(owncloudShouldSoftCancelPropertyName, true);
|
|
|
|
reply()->abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-25 15:30:48 +04:00
|
|
|
void PollJob::start()
|
|
|
|
{
|
2014-08-28 13:27:08 +04:00
|
|
|
setTimeout(120 * 1000);
|
2014-07-29 21:51:26 +04:00
|
|
|
QUrl accountUrl = account()->url();
|
|
|
|
QUrl finalUrl = QUrl::fromUserInput(accountUrl.scheme() + QLatin1String("://") + accountUrl.authority()
|
2014-11-11 12:10:46 +03:00
|
|
|
+ (path().startsWith('/') ? QLatin1String("") : QLatin1String("/")) + path());
|
2014-07-29 21:51:26 +04:00
|
|
|
setReply(getRequest(finalUrl));
|
|
|
|
setupConnections(reply());
|
2014-07-25 15:30:48 +04:00
|
|
|
connect(reply(), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(resetTimeout()));
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2014-07-28 14:12:52 +04:00
|
|
|
bool PollJob::finished()
|
|
|
|
{
|
|
|
|
QNetworkReply::NetworkError err = reply()->error();
|
|
|
|
if (err != QNetworkReply::NoError) {
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_httpErrorCode = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
_item->_status = classifyError(err, _item->_httpErrorCode);
|
|
|
|
_item->_errorString = reply()->errorString();
|
2015-03-04 14:00:25 +03:00
|
|
|
|
|
|
|
if (reply()->hasRawHeader("OC-ErrorString")) {
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_errorString = reply()->rawHeader("OC-ErrorString");
|
2015-03-04 14:00:25 +03:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_status == SyncFileItem::FatalError || _item->_httpErrorCode >= 400) {
|
|
|
|
if (_item->_status != SyncFileItem::FatalError
|
|
|
|
&& _item->_httpErrorCode != 503) {
|
2014-07-29 21:51:26 +04:00
|
|
|
SyncJournalDb::PollInfo info;
|
2015-04-15 16:19:11 +03:00
|
|
|
info._file = _item->_file;
|
2014-07-29 21:51:26 +04:00
|
|
|
// no info._url removes it from the database
|
|
|
|
_journal->setPollInfo(info);
|
2014-09-11 14:05:35 +04:00
|
|
|
_journal->commit("remove poll info");
|
2014-07-29 21:51:26 +04:00
|
|
|
|
|
|
|
}
|
2014-07-29 17:51:22 +04:00
|
|
|
emit finishedSignal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
start();
|
2014-07-28 14:12:52 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = false;
|
2014-08-29 15:58:33 +04:00
|
|
|
QByteArray jsonData = reply()->readAll().trimmed();
|
2014-10-07 19:42:50 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << ">" << jsonData << "<" << reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2014-08-29 15:58:33 +04:00
|
|
|
QVariantMap status = QtJson::parse(QString::fromUtf8(jsonData), ok).toMap();
|
2014-07-28 14:12:52 +04:00
|
|
|
if (!ok || status.isEmpty()) {
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_errorString = tr("Invalid JSON reply from the poll URL");
|
|
|
|
_item->_status = SyncFileItem::NormalError;
|
2014-07-28 14:12:52 +04:00
|
|
|
emit finishedSignal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-29 17:51:22 +04:00
|
|
|
if (status["unfinished"].isValid()) {
|
|
|
|
start();
|
|
|
|
return false;
|
2014-07-28 14:12:52 +04:00
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_errorString = status["error"].toString();
|
|
|
|
_item->_status = _item->_errorString.isEmpty() ? SyncFileItem::Success : SyncFileItem::NormalError;
|
|
|
|
_item->_fileId = status["fileid"].toByteArray();
|
|
|
|
_item->_etag = status["etag"].toByteArray();
|
|
|
|
_item->_responseTimeStamp = responseTimestamp();
|
2014-07-28 14:12:52 +04:00
|
|
|
|
|
|
|
SyncJournalDb::PollInfo info;
|
2015-04-15 16:19:11 +03:00
|
|
|
info._file = _item->_file;
|
2014-07-28 14:12:52 +04:00
|
|
|
// no info._url removes it from the database
|
|
|
|
_journal->setPollInfo(info);
|
2014-09-11 14:05:35 +04:00
|
|
|
_journal->commit("remove poll info");
|
2014-07-28 14:12:52 +04:00
|
|
|
|
|
|
|
emit finishedSignal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::setDeleteExisting(bool enabled)
|
|
|
|
{
|
|
|
|
_deleteExisting = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PropagateUploadFileCommon::start()
|
2014-02-10 16:00:22 +04:00
|
|
|
{
|
2017-01-17 16:29:12 +03:00
|
|
|
if (propagator()->_abortRequested.fetchAndAddRelaxed(0)) {
|
2014-02-13 17:02:05 +04:00
|
|
|
return;
|
2015-02-27 17:27:49 +03:00
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_activeJobList.append(this);
|
2016-01-06 12:01:22 +03:00
|
|
|
|
|
|
|
if (!_deleteExisting) {
|
|
|
|
return slotComputeContentChecksum();
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
auto job = new DeleteJob(propagator()->account(),
|
|
|
|
propagator()->_remoteFolder + _item->_file,
|
2016-01-06 12:01:22 +03:00
|
|
|
this);
|
|
|
|
_jobs.append(job);
|
|
|
|
connect(job, SIGNAL(finishedSignal()), SLOT(slotComputeContentChecksum()));
|
|
|
|
connect(job, SIGNAL(destroyed(QObject*)), SLOT(slotJobDestroyed(QObject*)));
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::slotComputeContentChecksum()
|
2016-01-06 12:01:22 +03:00
|
|
|
{
|
2017-01-17 16:29:12 +03:00
|
|
|
if (propagator()->_abortRequested.fetchAndAddRelaxed(0)) {
|
2016-01-06 12:01:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
const QString filePath = propagator()->getFilePath(_item->_file);
|
2015-05-15 16:39:26 +03:00
|
|
|
|
|
|
|
// remember the modtime before checksumming to be able to detect a file
|
|
|
|
// change during the checksum calculation
|
2015-05-26 13:33:19 +03:00
|
|
|
_item->_modtime = FileSystem::getModTime(filePath);
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2017-01-26 15:40:10 +03:00
|
|
|
#ifdef WITH_TESTING
|
2015-05-12 17:36:40 +03:00
|
|
|
_stopWatch.start();
|
2017-01-26 15:40:10 +03:00
|
|
|
#endif
|
2015-05-12 17:36:40 +03:00
|
|
|
|
2016-03-02 15:52:14 +03:00
|
|
|
QByteArray checksumType = contentChecksumType();
|
2015-11-23 15:44:49 +03:00
|
|
|
|
|
|
|
// Maybe the discovery already computed the checksum?
|
2016-03-02 15:52:14 +03:00
|
|
|
if (_item->_contentChecksumType == checksumType
|
2015-11-23 15:44:49 +03:00
|
|
|
&& !_item->_contentChecksum.isEmpty()) {
|
2016-03-02 15:52:14 +03:00
|
|
|
slotComputeTransmissionChecksum(checksumType, _item->_contentChecksum);
|
2015-11-23 15:44:49 +03:00
|
|
|
return;
|
2015-11-23 13:53:06 +03:00
|
|
|
}
|
|
|
|
|
2015-11-23 15:44:49 +03:00
|
|
|
// Compute the content checksum.
|
|
|
|
auto computeChecksum = new ComputeChecksum(this);
|
2016-03-02 15:52:14 +03:00
|
|
|
computeChecksum->setChecksumType(checksumType);
|
2015-11-23 15:44:49 +03:00
|
|
|
|
2015-11-23 13:53:06 +03:00
|
|
|
connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
|
|
|
|
SLOT(slotComputeTransmissionChecksum(QByteArray,QByteArray)));
|
2016-06-17 20:08:04 +03:00
|
|
|
connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
|
|
|
|
computeChecksum, SLOT(deleteLater()));
|
2015-11-23 13:53:06 +03:00
|
|
|
computeChecksum->start(filePath);
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::slotComputeTransmissionChecksum(const QByteArray& contentChecksumType, const QByteArray& contentChecksum)
|
2015-11-23 13:53:06 +03:00
|
|
|
{
|
|
|
|
_item->_contentChecksum = contentChecksum;
|
|
|
|
_item->_contentChecksumType = contentChecksumType;
|
|
|
|
|
2017-01-26 15:40:10 +03:00
|
|
|
#ifdef WITH_TESTING
|
2015-11-23 13:53:06 +03:00
|
|
|
_stopWatch.addLapTime(QLatin1String("ContentChecksum"));
|
|
|
|
_stopWatch.start();
|
2017-01-26 15:40:10 +03:00
|
|
|
#endif
|
2015-11-23 13:53:06 +03:00
|
|
|
|
|
|
|
// Reuse the content checksum as the transmission checksum if possible
|
|
|
|
const auto supportedTransmissionChecksums =
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->account()->capabilities().supportedChecksumTypes();
|
2015-11-23 13:53:06 +03:00
|
|
|
if (supportedTransmissionChecksums.contains(contentChecksumType)) {
|
|
|
|
slotStartUpload(contentChecksumType, contentChecksum);
|
|
|
|
return;
|
2015-10-01 16:00:33 +03:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:53:06 +03:00
|
|
|
// Compute the transmission checksum.
|
2015-10-14 16:03:40 +03:00
|
|
|
auto computeChecksum = new ComputeChecksum(this);
|
2015-10-15 10:54:01 +03:00
|
|
|
if (uploadChecksumEnabled()) {
|
2017-01-17 16:29:12 +03:00
|
|
|
computeChecksum->setChecksumType(propagator()->account()->capabilities().uploadChecksumType());
|
2015-10-15 10:54:01 +03:00
|
|
|
} else {
|
2015-10-15 10:39:49 +03:00
|
|
|
computeChecksum->setChecksumType(QByteArray());
|
|
|
|
}
|
2015-10-01 16:00:33 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
|
|
|
|
SLOT(slotStartUpload(QByteArray,QByteArray)));
|
2016-06-17 20:08:04 +03:00
|
|
|
connect(computeChecksum, SIGNAL(done(QByteArray,QByteArray)),
|
|
|
|
computeChecksum, SLOT(deleteLater()));
|
2017-01-17 16:29:12 +03:00
|
|
|
const QString filePath = propagator()->getFilePath(_item->_file);
|
2015-10-14 16:03:40 +03:00
|
|
|
computeChecksum->start(filePath);
|
2015-05-12 17:36:40 +03:00
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::slotStartUpload(const QByteArray& transmissionChecksumType, const QByteArray& transmissionChecksum)
|
2015-05-12 17:36:40 +03:00
|
|
|
{
|
2016-05-20 16:06:07 +03:00
|
|
|
// Remove ourselfs from the list of active job, before any posible call to done()
|
|
|
|
// When we start chunks, we will add it again, once for every chunks.
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_activeJobList.removeOne(this);
|
2016-05-20 16:06:07 +03:00
|
|
|
|
2015-11-23 13:53:06 +03:00
|
|
|
_transmissionChecksum = transmissionChecksum;
|
|
|
|
_transmissionChecksumType = transmissionChecksumType;
|
2015-10-14 16:45:44 +03:00
|
|
|
|
2016-02-25 19:17:14 +03:00
|
|
|
if (_item->_contentChecksum.isEmpty() && _item->_contentChecksumType.isEmpty()) {
|
|
|
|
// If the _contentChecksum was not set, reuse the transmission checksum as the content checksum.
|
|
|
|
_item->_contentChecksum = transmissionChecksum;
|
|
|
|
_item->_contentChecksumType = transmissionChecksumType;
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
const QString fullFilePath = propagator()->getFilePath(_item->_file);
|
2015-02-27 17:27:49 +03:00
|
|
|
|
|
|
|
if (!FileSystem::fileExists(fullFilePath)) {
|
2015-01-14 14:48:38 +03:00
|
|
|
done(SyncFileItem::SoftError, tr("File Removed"));
|
2014-02-13 17:02:05 +04:00
|
|
|
return;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
2017-01-26 15:40:10 +03:00
|
|
|
#ifdef WITH_TESTING
|
2015-11-23 13:53:06 +03:00
|
|
|
_stopWatch.addLapTime(QLatin1String("TransmissionChecksum"));
|
2017-01-26 15:40:10 +03:00
|
|
|
#endif
|
2015-05-12 17:36:40 +03:00
|
|
|
|
2016-05-18 17:42:55 +03:00
|
|
|
time_t prevModtime = _item->_modtime; // the _item value was set in PropagateUploadFile::start()
|
2015-05-12 17:36:40 +03:00
|
|
|
// but a potential checksum calculation could have taken some time during which the file could
|
|
|
|
// have been changed again, so better check again here.
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_modtime = FileSystem::getModTime(fullFilePath);
|
2015-05-26 13:33:19 +03:00
|
|
|
if( prevModtime != _item->_modtime ) {
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2015-05-20 23:44:12 +03:00
|
|
|
done(SyncFileItem::SoftError, tr("Local file changed during syncing. It will be resumed."));
|
2015-05-12 17:36:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-27 17:27:49 +03:00
|
|
|
quint64 fileSize = FileSystem::getSize(fullFilePath);
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_size = fileSize;
|
2014-09-17 15:35:54 +04:00
|
|
|
|
|
|
|
// 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.
|
2015-04-15 16:19:11 +03:00
|
|
|
if (fileIsStillChanging(*_item)) {
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_anotherSyncNeeded = true;
|
2014-09-17 15:35:54 +04:00
|
|
|
done(SyncFileItem::SoftError, tr("Local file changed during sync."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
doStartUpload();
|
2014-02-13 17:02:05 +04:00
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2015-01-14 17:14:17 +03:00
|
|
|
UploadDevice::UploadDevice(BandwidthManager *bwm)
|
|
|
|
: _read(0),
|
2014-09-29 12:30:39 +04:00
|
|
|
_bandwidthManager(bwm),
|
|
|
|
_bandwidthQuota(0),
|
|
|
|
_readWithProgress(0),
|
|
|
|
_bandwidthLimited(false), _choked(false)
|
|
|
|
{
|
|
|
|
_bandwidthManager->registerUploadDevice(this);
|
|
|
|
}
|
2014-02-10 16:00:22 +04:00
|
|
|
|
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
UploadDevice::~UploadDevice() {
|
2015-01-14 16:27:24 +03:00
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterUploadDevice(this);
|
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
2014-02-13 17:02:05 +04:00
|
|
|
|
2015-01-14 17:14:17 +03:00
|
|
|
bool UploadDevice::prepareAndOpen(const QString& fileName, qint64 start, qint64 size)
|
2015-01-14 14:48:38 +03:00
|
|
|
{
|
|
|
|
_data.clear();
|
2015-01-14 17:14:17 +03:00
|
|
|
_read = 0;
|
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
QString openError;
|
2015-03-12 15:18:08 +03:00
|
|
|
if (!FileSystem::openAndSeekFileSharedRead(&file, &openError, start)) {
|
2015-01-14 17:14:17 +03:00
|
|
|
setErrorString(openError);
|
2015-01-14 14:48:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-14 17:14:17 +03:00
|
|
|
|
2015-03-12 15:18:08 +03:00
|
|
|
size = qBound(0ll, size, FileSystem::getSize(fileName) - start);
|
2015-01-14 17:14:17 +03:00
|
|
|
_data.resize(size);
|
|
|
|
auto read = file.read(_data.data(), size);
|
|
|
|
if (read != size) {
|
|
|
|
setErrorString(file.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QIODevice::open(QIODevice::ReadOnly);
|
2015-01-14 14:48:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
qint64 UploadDevice::writeData(const char* , qint64 ) {
|
|
|
|
Q_ASSERT(!"write to read only device");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-09-15 19:55:55 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
qint64 UploadDevice::readData(char* data, qint64 maxlen) {
|
2014-10-07 19:42:50 +04:00
|
|
|
//qDebug() << Q_FUNC_INFO << maxlen << _read << _size << _bandwidthQuota;
|
2015-01-14 17:14:17 +03:00
|
|
|
if (_data.size() - _read <= 0) {
|
2014-09-29 12:30:39 +04:00
|
|
|
// at end
|
2015-02-06 13:19:41 +03:00
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterUploadDevice(this);
|
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-14 17:14:17 +03:00
|
|
|
maxlen = qMin(maxlen, _data.size() - _read);
|
2014-09-29 12:30:39 +04:00
|
|
|
if (maxlen == 0) {
|
2014-02-13 17:02:05 +04:00
|
|
|
return 0;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
if (isChoked()) {
|
|
|
|
return 0;
|
2014-02-10 16:00:22 +04:00
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
if (isBandwidthLimited()) {
|
|
|
|
maxlen = qMin(maxlen, _bandwidthQuota);
|
|
|
|
if (maxlen <= 0) { // no quota
|
2016-05-18 17:17:52 +03:00
|
|
|
//qDebug() << "no quota";
|
2014-09-29 12:30:39 +04:00
|
|
|
return 0;
|
2014-07-14 21:53:42 +04:00
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
_bandwidthQuota -= maxlen;
|
2014-02-13 17:02:05 +04:00
|
|
|
}
|
2015-01-14 14:48:38 +03:00
|
|
|
std::memcpy(data, _data.data()+_read, maxlen);
|
|
|
|
_read += maxlen;
|
|
|
|
return maxlen;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadDevice::slotJobUploadProgress(qint64 sent, qint64 t)
|
2014-11-11 14:16:14 +03:00
|
|
|
{
|
2014-10-07 19:42:50 +04:00
|
|
|
//qDebug() << Q_FUNC_INFO << sent << _read << t << _size << _bandwidthQuota;
|
2014-09-29 12:30:39 +04:00
|
|
|
if (sent == 0 || t == 0) {
|
|
|
|
return;
|
2014-04-04 17:41:35 +04:00
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
_readWithProgress = sent;
|
|
|
|
}
|
2014-04-04 17:41:35 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
bool UploadDevice::atEnd() const {
|
2015-01-14 17:14:17 +03:00
|
|
|
return _read >= _data.size();
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
2014-04-05 16:25:41 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
qint64 UploadDevice::size() const{
|
2014-10-07 19:42:50 +04:00
|
|
|
// qDebug() << this << Q_FUNC_INFO << _size;
|
2015-01-14 17:14:17 +03:00
|
|
|
return _data.size();
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 UploadDevice::bytesAvailable() const
|
|
|
|
{
|
2014-10-07 19:42:50 +04:00
|
|
|
// qDebug() << this << Q_FUNC_INFO << _size << _read << QIODevice::bytesAvailable()
|
|
|
|
// << _size - _read + QIODevice::bytesAvailable();
|
2015-01-14 17:14:17 +03:00
|
|
|
return _data.size() - _read + QIODevice::bytesAvailable();
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
2014-04-05 16:25:41 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
// random access, we can seek
|
|
|
|
bool UploadDevice::isSequential() const{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UploadDevice::seek ( qint64 pos ) {
|
2015-01-14 17:14:17 +03:00
|
|
|
if (! QIODevice::seek(pos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pos < 0 || pos > _data.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
_read = pos;
|
2015-01-14 17:07:41 +03:00
|
|
|
return true;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
2014-04-04 17:41:35 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
void UploadDevice::giveBandwidthQuota(qint64 bwq) {
|
|
|
|
if (!atEnd()) {
|
|
|
|
_bandwidthQuota = bwq;
|
|
|
|
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); // tell QNAM that we have quota
|
2014-04-04 17:41:35 +04:00
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadDevice::setBandwidthLimited(bool b) {
|
|
|
|
_bandwidthLimited = b;
|
|
|
|
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadDevice::setChoked(bool b) {
|
|
|
|
_choked = b;
|
|
|
|
if (!_choked) {
|
|
|
|
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::startPollJob(const QString& path)
|
2014-07-25 15:30:48 +04:00
|
|
|
{
|
2017-01-17 16:29:12 +03:00
|
|
|
PollJob* job = new PollJob(propagator()->account(), path, _item,
|
|
|
|
propagator()->_journal, propagator()->_localDir, this);
|
2014-07-28 14:12:52 +04:00
|
|
|
connect(job, SIGNAL(finishedSignal()), SLOT(slotPollFinished()));
|
|
|
|
SyncJournalDb::PollInfo info;
|
2015-04-15 16:19:11 +03:00
|
|
|
info._file = _item->_file;
|
2014-07-28 14:12:52 +04:00
|
|
|
info._url = path;
|
2015-04-15 16:19:11 +03:00
|
|
|
info._modtime = _item->_modtime;
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setPollInfo(info);
|
|
|
|
propagator()->_journal->commit("add poll info");
|
|
|
|
propagator()->_activeJobList.append(this);
|
2014-07-29 21:51:26 +04:00
|
|
|
job->start();
|
2014-07-25 15:30:48 +04:00
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::slotPollFinished()
|
2014-07-25 15:30:48 +04:00
|
|
|
{
|
|
|
|
PollJob *job = qobject_cast<PollJob *>(sender());
|
|
|
|
Q_ASSERT(job);
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_activeJobList.removeOne(this);
|
2014-11-24 15:33:13 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (job->_item->_status != SyncFileItem::Success) {
|
2014-11-24 13:58:51 +03:00
|
|
|
_finished = true;
|
2015-04-15 16:19:11 +03:00
|
|
|
done(job->_item->_status, job->_item->_errorString);
|
2014-07-25 15:30:48 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
finalize();
|
2014-07-25 15:30:48 +04:00
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2017-01-13 16:44:45 +03:00
|
|
|
void PropagateUploadFileCommon::checkResettingErrors()
|
|
|
|
{
|
|
|
|
if (_item->_httpErrorCode == 412
|
2017-01-17 16:29:12 +03:00
|
|
|
|| propagator()->account()->capabilities().httpErrorCodesThatResetFailingChunkedUploads()
|
2017-01-13 16:44:45 +03:00
|
|
|
.contains(_item->_httpErrorCode)) {
|
2017-01-17 16:29:12 +03:00
|
|
|
auto uploadInfo = propagator()->_journal->getUploadInfo(_item->_file);
|
2017-01-13 16:44:45 +03:00
|
|
|
uploadInfo._errorCount += 1;
|
|
|
|
if (uploadInfo._errorCount > 3) {
|
|
|
|
qDebug() << "Reset transfer of" << _item->_file
|
|
|
|
<< "due to repeated error" << _item->_httpErrorCode;
|
|
|
|
uploadInfo = SyncJournalDb::UploadInfo();
|
|
|
|
} else {
|
|
|
|
qDebug() << "Error count for maybe-reset error" << _item->_httpErrorCode
|
|
|
|
<< "on file" << _item->_file
|
|
|
|
<< "is" << uploadInfo._errorCount;
|
|
|
|
}
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setUploadInfo(_item->_file, uploadInfo);
|
|
|
|
propagator()->_journal->commit("Upload info");
|
2017-01-13 16:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::slotJobDestroyed(QObject* job)
|
2014-09-15 19:55:55 +04:00
|
|
|
{
|
|
|
|
_jobs.erase(std::remove(_jobs.begin(), _jobs.end(), job) , _jobs.end());
|
|
|
|
}
|
2014-03-14 16:03:16 +04:00
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::abort()
|
2014-02-06 17:52:56 +04:00
|
|
|
{
|
2014-09-15 19:55:55 +04:00
|
|
|
foreach(auto *job, _jobs) {
|
|
|
|
if (job->reply()) {
|
2015-04-15 16:19:11 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << job << this->_item->_file;
|
2014-09-15 19:55:55 +04:00
|
|
|
job->reply()->abort();
|
|
|
|
}
|
2014-04-05 16:25:41 +04:00
|
|
|
}
|
2014-02-06 17:52:56 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 14:48:38 +03:00
|
|
|
// This function is used whenever there is an error occuring and jobs might be in progress
|
2016-05-20 11:26:02 +03:00
|
|
|
void PropagateUploadFileCommon::abortWithError(SyncFileItem::Status status, const QString &error)
|
2015-01-14 14:48:38 +03:00
|
|
|
{
|
|
|
|
_finished = true;
|
|
|
|
abort();
|
|
|
|
done(status, error);
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:26:02 +03:00
|
|
|
QMap<QByteArray, QByteArray> PropagateUploadFileCommon::headers()
|
|
|
|
{
|
|
|
|
QMap<QByteArray, QByteArray> headers;
|
|
|
|
headers["OC-Async"] = "1";
|
|
|
|
headers["Content-Type"] = "application/octet-stream";
|
|
|
|
headers["X-OC-Mtime"] = QByteArray::number(qint64(_item->_modtime));
|
|
|
|
|
|
|
|
if(_item->_file.contains(".sys.admin#recall#")) {
|
|
|
|
// This is a file recall triggered by the admin. Note: the
|
|
|
|
// recall list file created by the admin and downloaded by the
|
|
|
|
// client (.sys.admin#recall#) also falls into this category
|
|
|
|
// (albeit users are not supposed to mess up with it)
|
|
|
|
|
|
|
|
// We use a special tag header so that the server may decide to store this file away in some admin stage area
|
|
|
|
// And not directly in the user's area (which would trigger redownloads etc).
|
|
|
|
headers["OC-Tag"] = ".sys.admin#recall#";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_item->_etag.isEmpty() && _item->_etag != "empty_etag"
|
|
|
|
&& _item->_instruction != CSYNC_INSTRUCTION_NEW // On new files never send a If-Match
|
|
|
|
&& _item->_instruction != CSYNC_INSTRUCTION_TYPE_CHANGE
|
|
|
|
&& !_deleteExisting
|
|
|
|
) {
|
|
|
|
// We add quotes because the owncloud server always adds quotes around the etag, and
|
|
|
|
// csync_owncloud.c's owncloud_file_id always strips the quotes.
|
|
|
|
headers["If-Match"] = '"' + _item->_etag + '"';
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadFileCommon::finalize()
|
|
|
|
{
|
|
|
|
_finished = true;
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
if (!propagator()->_journal->setFileRecord(SyncJournalFileRecord(*_item, propagator()->getFilePath(_item->_file)))) {
|
2016-05-20 11:26:02 +03:00
|
|
|
done(SyncFileItem::FatalError, tr("Error writing metadata to the database"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Remove from the progress database:
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->setUploadInfo(_item->_file, SyncJournalDb::UploadInfo());
|
|
|
|
propagator()->_journal->commit("upload file start");
|
2016-05-20 11:26:02 +03:00
|
|
|
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:48:38 +03:00
|
|
|
|
2014-12-10 15:01:36 +03:00
|
|
|
}
|