2013-05-03 21:11:00 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@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 "owncloudpropagator.h"
|
2013-10-16 13:59:54 +04:00
|
|
|
#include "syncjournaldb.h"
|
2013-10-28 13:47:10 +04:00
|
|
|
#include "syncjournalfilerecord.h"
|
2014-02-18 14:52:38 +04:00
|
|
|
#include "propagator_qnam.h"
|
|
|
|
#include "propagatorjobs.h"
|
|
|
|
#include "propagator_legacy.h"
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
#include <QStack>
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
namespace Mirall {
|
2013-12-10 20:19:25 +04:00
|
|
|
|
2014-02-12 14:07:34 +04:00
|
|
|
/* The maximum number of active job in parallel */
|
|
|
|
static const int maximumActiveJob = 6;
|
|
|
|
|
2013-11-20 16:44:01 +04:00
|
|
|
void PropagateItemJob::done(SyncFileItem::Status status, const QString &errorString)
|
|
|
|
{
|
|
|
|
_item._errorString = errorString;
|
|
|
|
_item._status = status;
|
|
|
|
|
|
|
|
// Blacklisting
|
|
|
|
int retries = 0;
|
|
|
|
|
2014-02-04 18:01:10 +04:00
|
|
|
if( _item._httpErrorCode == 403 ||_item._httpErrorCode == 413 || _item._httpErrorCode == 415 ) {
|
2013-12-12 14:38:41 +04:00
|
|
|
qDebug() << "Fatal Error condition" << _item._httpErrorCode << ", forbid retry!";
|
2013-11-20 16:44:01 +04:00
|
|
|
retries = -1;
|
|
|
|
} else {
|
|
|
|
retries = 3; // FIXME: good number of allowed retries?
|
|
|
|
}
|
|
|
|
SyncJournalBlacklistRecord record(_item, retries);;
|
|
|
|
|
|
|
|
switch( status ) {
|
2013-11-26 14:31:05 +04:00
|
|
|
case SyncFileItem::SoftError:
|
|
|
|
// do not blacklist in case of soft error.
|
2013-11-26 15:22:28 +04:00
|
|
|
emit progress( Progress::SoftError, _item, 0, 0 );
|
2013-11-26 14:31:05 +04:00
|
|
|
break;
|
2013-11-20 16:44:01 +04:00
|
|
|
case SyncFileItem::FatalError:
|
|
|
|
case SyncFileItem::NormalError:
|
|
|
|
_propagator->_journal->updateBlacklistEntry( record );
|
2013-11-26 15:22:28 +04:00
|
|
|
if( status == SyncFileItem::NormalError ) {
|
|
|
|
emit progress( Progress::NormalError, _item, 0, 0 );
|
|
|
|
}
|
2013-11-20 16:44:01 +04:00
|
|
|
break;
|
|
|
|
case SyncFileItem::Success:
|
|
|
|
if( _item._blacklistedInDb ) {
|
|
|
|
// wipe blacklist entry.
|
|
|
|
_propagator->_journal->wipeBlacklistEntry(_item._file);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SyncFileItem::Conflict:
|
|
|
|
case SyncFileItem::FileIgnored:
|
|
|
|
case SyncFileItem::NoStatus:
|
|
|
|
// nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit completed(_item);
|
|
|
|
emit finished(status);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:01:10 +04:00
|
|
|
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
PropagateItemJob* OwncloudPropagator::createJob(const SyncFileItem& item) {
|
|
|
|
switch(item._instruction) {
|
|
|
|
case CSYNC_INSTRUCTION_REMOVE:
|
|
|
|
if (item._dir == SyncFileItem::Down) return new PropagateLocalRemove(this, item);
|
|
|
|
else return new PropagateRemoteRemove(this, item);
|
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
|
|
|
if (item._isDirectory) {
|
|
|
|
if (item._dir == SyncFileItem::Down) return new PropagateLocalMkdir(this, item);
|
|
|
|
else return new PropagateRemoteMkdir(this, item);
|
|
|
|
} //fall trough
|
|
|
|
case CSYNC_INSTRUCTION_SYNC:
|
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
|
|
|
if (item._isDirectory) {
|
|
|
|
// Should we set the mtime?
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
if (item._dir != SyncFileItem::Up) return new PropagateDownloadFileQNAM(this, item);
|
2014-02-06 14:50:16 +04:00
|
|
|
else return new PropagateUploadFileQNAM(this, item);
|
2013-10-28 13:47:10 +04:00
|
|
|
case CSYNC_INSTRUCTION_RENAME:
|
2013-10-30 20:36:58 +04:00
|
|
|
if (item._dir == SyncFileItem::Up) {
|
|
|
|
return new PropagateRemoteRename(this, item);
|
|
|
|
} else {
|
|
|
|
return new PropagateLocalRename(this, item);
|
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
return new PropagateIgnoreJob(this, item);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-14 21:59:16 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
void OwncloudPropagator::start(const SyncFileItemVector& _syncedItems)
|
|
|
|
{
|
2013-10-28 20:00:27 +04:00
|
|
|
/* This builds all the job needed for the propagation.
|
|
|
|
* Each directories is a PropagateDirectory job, which contains the files in it.
|
|
|
|
* In order to do that we sort the items by destination. and loop over it. When we enter a
|
|
|
|
* directory, we can create the directory job and push it on the stack. */
|
2013-10-28 13:47:10 +04:00
|
|
|
SyncFileItemVector items = _syncedItems;
|
|
|
|
std::sort(items.begin(), items.end());
|
|
|
|
_rootJob.reset(new PropagateDirectory(this));
|
2013-10-28 20:00:27 +04:00
|
|
|
QStack<QPair<QString /* directory name */, PropagateDirectory* /* job */> > directories;
|
2013-10-28 13:47:10 +04:00
|
|
|
directories.push(qMakePair(QString(), _rootJob.data()));
|
2013-10-28 20:00:27 +04:00
|
|
|
QVector<PropagatorJob*> directoriesToRemove;
|
2013-10-28 13:47:10 +04:00
|
|
|
QString removedDirectory;
|
|
|
|
foreach(const SyncFileItem &item, items) {
|
|
|
|
if (item._instruction == CSYNC_INSTRUCTION_REMOVE
|
|
|
|
&& !removedDirectory.isEmpty() && item._file.startsWith(removedDirectory)) {
|
|
|
|
//already taken care of. (by the removal of the parent directory)
|
|
|
|
continue;
|
2013-08-14 21:59:16 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
while (!item._file.startsWith(directories.top().first)) {
|
|
|
|
directories.pop();
|
2013-08-14 21:59:16 +04:00
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
|
|
|
|
if (item._isDirectory) {
|
|
|
|
PropagateDirectory *dir = new PropagateDirectory(this, item);
|
|
|
|
dir->_firstJob.reset(createJob(item));
|
|
|
|
if (item._instruction == CSYNC_INSTRUCTION_REMOVE) {
|
|
|
|
//We do the removal of directories at the end
|
|
|
|
directoriesToRemove.append(dir);
|
|
|
|
removedDirectory = item._file + "/";
|
|
|
|
} else {
|
|
|
|
directories.top().second->append(dir);
|
2013-08-14 21:59:16 +04:00
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
directories.push(qMakePair(item._file + "/" , dir));
|
|
|
|
} else if (PropagateItemJob* current = createJob(item)) {
|
|
|
|
directories.top().second->append(current);
|
2013-08-14 21:59:16 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
|
|
|
|
foreach(PropagatorJob* it, directoriesToRemove) {
|
|
|
|
_rootJob->append(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(_rootJob.data(), SIGNAL(completed(SyncFileItem)), this, SIGNAL(completed(SyncFileItem)));
|
2013-11-25 19:16:33 +04:00
|
|
|
connect(_rootJob.data(), SIGNAL(progress(Progress::Kind,SyncFileItem,quint64,quint64)), this,
|
|
|
|
SIGNAL(progress(Progress::Kind,SyncFileItem,quint64,quint64)));
|
2013-10-28 13:47:10 +04:00
|
|
|
connect(_rootJob.data(), SIGNAL(finished(SyncFileItem::Status)), this, SIGNAL(finished()));
|
2013-11-21 14:13:58 +04:00
|
|
|
|
2014-02-05 23:18:03 +04:00
|
|
|
QMetaObject::invokeMethod(_rootJob.data(), "start");
|
2013-08-14 21:59:16 +04:00
|
|
|
}
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2014-01-07 18:42:21 +04:00
|
|
|
void OwncloudPropagator::overallTransmissionSizeChanged(qint64 change)
|
|
|
|
{
|
|
|
|
emit progressChanged(change);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:01:10 +04:00
|
|
|
bool OwncloudPropagator::isInSharedDirectory(const QString& file)
|
|
|
|
{
|
|
|
|
bool re = false;
|
|
|
|
if( _remoteDir.contains("remote.php/webdav/Shared") ) {
|
|
|
|
// The Shared directory is synced as its own sync connection
|
|
|
|
re = true;
|
|
|
|
} else {
|
|
|
|
if( file.startsWith("Shared/") ) {
|
|
|
|
// The whole ownCloud is synced and Shared is always a top dir
|
|
|
|
re = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2013-11-15 20:42:27 +04:00
|
|
|
void PropagateDirectory::start()
|
|
|
|
{
|
|
|
|
_current = -1;
|
2013-11-28 13:00:12 +04:00
|
|
|
_hasError = SyncFileItem::NoStatus;
|
2013-11-15 20:42:27 +04:00
|
|
|
if (!_firstJob) {
|
2014-02-12 14:07:34 +04:00
|
|
|
slotSubJobReady();
|
2013-11-15 20:42:27 +04:00
|
|
|
} else {
|
|
|
|
startJob(_firstJob.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:11:45 +04:00
|
|
|
void PropagateDirectory::slotSubJobFinished(SyncFileItem::Status status)
|
2013-10-28 13:47:10 +04:00
|
|
|
{
|
|
|
|
if (status == SyncFileItem::FatalError) {
|
2014-02-06 17:52:56 +04:00
|
|
|
abort();
|
2013-10-28 13:47:10 +04:00
|
|
|
emit finished(status);
|
|
|
|
return;
|
2013-11-28 13:00:12 +04:00
|
|
|
} else if (status == SyncFileItem::NormalError || status == SyncFileItem::SoftError) {
|
|
|
|
_hasError = status;
|
2013-10-28 13:47:10 +04:00
|
|
|
}
|
2014-02-12 14:07:34 +04:00
|
|
|
_runningNow--;
|
|
|
|
slotSubJobReady();
|
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
|
2014-02-12 14:07:34 +04:00
|
|
|
void PropagateDirectory::slotSubJobReady()
|
|
|
|
{
|
|
|
|
qDebug() << Q_FUNC_INFO << _runningNow << _propagator->_activeJobs;
|
|
|
|
|
|
|
|
if (_runningNow && _current == -1)
|
|
|
|
return; // Ignore the case when the _fistJob is ready and not yet finished
|
|
|
|
if (_runningNow && _current >= 0 && _current < _subJobs.count()) {
|
|
|
|
// there is a job running and the current one is not ready yet, we can't start new job
|
|
|
|
qDebug() << _subJobs[_current]->_readySent << maximumActiveJob << _subJobs[_current];
|
|
|
|
if (!_subJobs[_current]->_readySent || _propagator->_activeJobs >= maximumActiveJob)
|
|
|
|
return;
|
2014-02-06 15:11:45 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 14:07:34 +04:00
|
|
|
_current++;
|
|
|
|
if (_current < _subJobs.size() && !_propagator->_abortRequested.fetchAndAddRelaxed(0)) {
|
|
|
|
PropagatorJob *next = _subJobs.at(_current);
|
|
|
|
startJob(next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We finished to processing all the jobs
|
|
|
|
emitReady();
|
|
|
|
if (!_runningNow) {
|
2013-11-28 13:00:12 +04:00
|
|
|
if (!_item.isEmpty() && _hasError == SyncFileItem::NoStatus) {
|
2013-11-05 20:50:09 +04:00
|
|
|
if( !_item._renameTarget.isEmpty() ) {
|
|
|
|
_item._file = _item._renameTarget;
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:51:42 +04:00
|
|
|
if (_item._should_update_etag && _item._instruction != CSYNC_INSTRUCTION_REMOVE) {
|
2013-11-15 16:53:18 +04:00
|
|
|
SyncJournalFileRecord record(_item, _propagator->_localDir + _item._file);
|
|
|
|
_propagator->_journal->setFileRecord(record);
|
|
|
|
}
|
2013-10-28 13:47:10 +04:00
|
|
|
}
|
2013-11-28 13:00:12 +04:00
|
|
|
emit finished(_hasError == SyncFileItem::NoStatus ? SyncFileItem::Success : _hasError);
|
2013-10-28 13:47:10 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2014-02-04 18:01:10 +04:00
|
|
|
|
2013-05-04 18:12:51 +04:00
|
|
|
}
|