2013-05-03 21:11:00 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OWNCLOUDPROPAGATOR_H
|
|
|
|
#define OWNCLOUDPROPAGATOR_H
|
|
|
|
|
2013-05-15 17:22:20 +04:00
|
|
|
#include <QHash>
|
2013-05-16 15:54:22 +04:00
|
|
|
#include <QObject>
|
2014-09-29 12:30:39 +04:00
|
|
|
#include <QMap>
|
|
|
|
#include <QLinkedList>
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QPointer>
|
|
|
|
#include <QIODevice>
|
2014-11-07 13:41:21 +03:00
|
|
|
#include <QMutex>
|
2013-05-05 13:41:31 +04:00
|
|
|
|
2013-05-03 21:11:00 +04:00
|
|
|
#include "syncfileitem.h"
|
2014-07-28 14:12:52 +04:00
|
|
|
#include "syncjournaldb.h"
|
2014-09-29 12:30:39 +04:00
|
|
|
#include "bandwidthmanager.h"
|
2014-12-18 14:09:48 +03:00
|
|
|
#include "accountfwd.h"
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2013-10-16 18:47:24 +04:00
|
|
|
struct hbf_transfer_s;
|
2013-05-03 21:11:00 +04:00
|
|
|
struct ne_session_s;
|
|
|
|
struct ne_decompress_s;
|
2014-06-12 15:45:25 +04:00
|
|
|
typedef struct ne_prop_result_set_s ne_prop_result_set;
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2013-10-16 13:59:54 +04:00
|
|
|
class SyncJournalDb;
|
2013-10-28 13:47:10 +04:00
|
|
|
class OwncloudPropagator;
|
2013-05-06 18:59:11 +04:00
|
|
|
|
2014-11-18 19:54:53 +03:00
|
|
|
/**
|
|
|
|
* @class PropagatorJob
|
|
|
|
* @brief the base class of propagator jobs
|
|
|
|
*
|
|
|
|
* This can either be a job, or a container for jobs.
|
|
|
|
* If it is a composite jobs, it then inherits from PropagateDirectory
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
class PropagatorJob : public QObject {
|
2013-05-16 15:54:22 +04:00
|
|
|
Q_OBJECT
|
2013-10-28 13:47:10 +04:00
|
|
|
protected:
|
|
|
|
OwncloudPropagator *_propagator;
|
2014-11-18 19:35:31 +03:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
public:
|
2014-11-18 19:54:53 +03:00
|
|
|
explicit PropagatorJob(OwncloudPropagator* propagator) : _propagator(propagator), _state(NotYetStarted) {}
|
|
|
|
|
2014-11-18 19:35:31 +03:00
|
|
|
enum JobState {
|
|
|
|
NotYetStarted,
|
|
|
|
Running,
|
|
|
|
Finished
|
|
|
|
};
|
2014-11-18 19:54:53 +03:00
|
|
|
JobState _state;
|
2014-11-18 19:35:31 +03:00
|
|
|
|
|
|
|
enum JobParallelism {
|
2014-11-18 19:54:53 +03:00
|
|
|
|
|
|
|
/** Jobs can be run in parallel to this job */
|
2014-11-18 19:35:31 +03:00
|
|
|
FullParallelism,
|
2014-11-18 19:54:53 +03:00
|
|
|
/** This job do not support parallelism, and no other job shall
|
|
|
|
be started until this one has finished */
|
2014-11-18 19:35:31 +03:00
|
|
|
WaitForFinished,
|
2014-11-18 19:54:53 +03:00
|
|
|
|
|
|
|
/** This job support paralelism with other jobs in the same directory, but it should
|
|
|
|
not be paralelized with jobs in other directories (typically a move operation) */
|
2014-11-18 19:35:31 +03:00
|
|
|
WaitForFinishedInParentDirectory
|
|
|
|
};
|
|
|
|
|
2014-11-18 19:54:53 +03:00
|
|
|
virtual JobParallelism parallelism() { return FullParallelism; }
|
2014-01-07 18:42:21 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
public slots:
|
2014-02-06 17:52:56 +04:00
|
|
|
virtual void abort() {}
|
2014-11-18 19:54:53 +03:00
|
|
|
|
|
|
|
/** Starts this job, or a new subjob
|
|
|
|
* returns true if a job was started.
|
|
|
|
*/
|
2014-11-18 19:35:31 +03:00
|
|
|
virtual bool scheduleNextJob() = 0;
|
2013-10-28 13:47:10 +04:00
|
|
|
signals:
|
2014-02-12 14:07:34 +04:00
|
|
|
/**
|
|
|
|
* Emitted when the job is fully finished
|
|
|
|
*/
|
2013-10-28 13:47:10 +04:00
|
|
|
void finished(SyncFileItem::Status);
|
2014-02-12 14:07:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emitted when one item has been completed within a job.
|
|
|
|
*/
|
2013-10-28 13:47:10 +04:00
|
|
|
void completed(const SyncFileItem &);
|
2014-02-12 14:07:34 +04:00
|
|
|
|
|
|
|
/**
|
2014-11-18 19:54:53 +03:00
|
|
|
* Emitted when all the sub-jobs have been finished and
|
|
|
|
* more jobs might be started (so scheduleNextJob can/must be called again)
|
2014-02-12 14:07:34 +04:00
|
|
|
*/
|
|
|
|
void ready();
|
|
|
|
|
2014-03-14 16:03:16 +04:00
|
|
|
void progress(const SyncFileItem& item, quint64 bytes);
|
2014-02-12 14:07:34 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
};
|
2013-05-16 15:54:22 +04:00
|
|
|
|
2014-12-06 14:27:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Abstract class to propagate a single item
|
|
|
|
* (Only used for neon job)
|
|
|
|
*/
|
|
|
|
class PropagateItemJob : public PropagatorJob {
|
|
|
|
Q_OBJECT
|
|
|
|
protected:
|
|
|
|
void done(SyncFileItem::Status status, const QString &errorString = QString());
|
|
|
|
|
|
|
|
bool checkForProblemsWithShared(int httpStatusCode, const QString& msg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set a custom restore job message that is used if the restore job succeeded.
|
|
|
|
* It is displayed in the activity view.
|
|
|
|
*/
|
|
|
|
QString restoreJobMsg() const {
|
|
|
|
return _item._isRestoration ? _item._errorString : QString();
|
|
|
|
}
|
|
|
|
void setRestoreJobMsg( const QString& msg = QString() ) {
|
|
|
|
_item._isRestoration = true;
|
|
|
|
_item._errorString = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected slots:
|
|
|
|
void slotRestoreJobCompleted(const SyncFileItem& );
|
|
|
|
|
|
|
|
private:
|
|
|
|
QScopedPointer<PropagateItemJob> _restoreJob;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PropagateItemJob(OwncloudPropagator* propagator, const SyncFileItem &item)
|
|
|
|
: PropagatorJob(propagator), _item(item) {}
|
|
|
|
|
2014-12-10 15:01:36 +03:00
|
|
|
bool scheduleNextJob() Q_DECL_OVERRIDE {
|
|
|
|
if (_state != NotYetStarted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_state = Running;
|
|
|
|
QMetaObject::invokeMethod(this, "start"); // We could be in a different thread (neon jobs)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-06 14:27:50 +03:00
|
|
|
SyncFileItem _item;
|
2014-12-10 15:01:36 +03:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
virtual void start() = 0;
|
2014-12-06 14:27:50 +03:00
|
|
|
};
|
|
|
|
|
2014-12-10 15:01:36 +03:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
/*
|
|
|
|
* Propagate a directory, and all its sub entries.
|
|
|
|
*/
|
|
|
|
class PropagateDirectory : public PropagatorJob {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
// e.g: create the directory
|
2014-12-06 14:27:50 +03:00
|
|
|
QScopedPointer<PropagateItemJob>_firstJob;
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
// all the sub files or sub directories.
|
|
|
|
QVector<PropagatorJob *> _subJobs;
|
2013-05-16 16:50:36 +04:00
|
|
|
|
2013-11-14 16:47:43 +04:00
|
|
|
SyncFileItem _item;
|
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
int _current; // index of the current running job
|
2014-02-12 14:07:34 +04:00
|
|
|
int _runningNow; // number of subJob running now
|
2013-11-28 13:00:12 +04:00
|
|
|
SyncFileItem::Status _hasError; // NoStatus, or NormalError / SoftError if there was an error
|
2013-05-03 21:11:00 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
explicit PropagateDirectory(OwncloudPropagator *propagator, const SyncFileItem &item = SyncFileItem())
|
|
|
|
: PropagatorJob(propagator)
|
2014-12-06 14:27:50 +03:00
|
|
|
, _firstJob(0), _item(item), _current(-1), _runningNow(0), _hasError(SyncFileItem::NoStatus)
|
|
|
|
{ }
|
2013-10-28 13:47:10 +04:00
|
|
|
|
|
|
|
virtual ~PropagateDirectory() {
|
|
|
|
qDeleteAll(_subJobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(PropagatorJob *subJob) {
|
|
|
|
_subJobs.append(subJob);
|
|
|
|
}
|
|
|
|
|
2014-11-18 19:35:31 +03:00
|
|
|
virtual bool scheduleNextJob() Q_DECL_OVERRIDE;
|
|
|
|
virtual JobParallelism parallelism() Q_DECL_OVERRIDE;
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual void abort() Q_DECL_OVERRIDE {
|
2014-02-06 17:52:56 +04:00
|
|
|
if (_firstJob)
|
|
|
|
_firstJob->abort();
|
|
|
|
foreach (PropagatorJob *j, _subJobs)
|
|
|
|
j->abort();
|
|
|
|
}
|
2013-05-04 17:32:11 +04:00
|
|
|
|
2014-12-06 14:27:50 +03:00
|
|
|
void increaseAffectedCount() {
|
|
|
|
_firstJob->_item._affectedItems++;
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:54:30 +03:00
|
|
|
void finalize();
|
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
private slots:
|
2014-11-18 19:35:31 +03:00
|
|
|
bool possiblyRunNextJob(PropagatorJob *next) {
|
|
|
|
if (next->_state == NotYetStarted) {
|
|
|
|
connect(next, SIGNAL(finished(SyncFileItem::Status)), this, SLOT(slotSubJobFinished(SyncFileItem::Status)), Qt::QueuedConnection);
|
|
|
|
connect(next, SIGNAL(completed(SyncFileItem)), this, SIGNAL(completed(SyncFileItem)));
|
|
|
|
connect(next, SIGNAL(progress(SyncFileItem,quint64)), this, SIGNAL(progress(SyncFileItem,quint64)));
|
|
|
|
connect(next, SIGNAL(ready()), this, SIGNAL(ready()));
|
|
|
|
_runningNow++;
|
|
|
|
}
|
|
|
|
return next->scheduleNextJob();
|
2013-10-28 13:47:10 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:11:45 +04:00
|
|
|
void slotSubJobFinished(SyncFileItem::Status status);
|
2013-10-28 13:47:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Dummy job that just mark it as completed and ignored.
|
|
|
|
class PropagateIgnoreJob : public PropagateItemJob {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
PropagateIgnoreJob(OwncloudPropagator* propagator,const SyncFileItem& item)
|
|
|
|
: PropagateItemJob(propagator, item) {}
|
2014-07-10 01:22:28 +04:00
|
|
|
void start() Q_DECL_OVERRIDE {
|
2014-06-13 13:19:31 +04:00
|
|
|
SyncFileItem::Status status = _item._status;
|
|
|
|
done(status == SyncFileItem::NoStatus ? SyncFileItem::FileIgnored : status, _item._errorString);
|
2013-10-28 13:47:10 +04:00
|
|
|
}
|
|
|
|
};
|
2013-08-14 21:59:16 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
class OwncloudPropagator : public QObject {
|
|
|
|
Q_OBJECT
|
2013-08-14 21:59:16 +04:00
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
PropagateItemJob *createJob(const SyncFileItem& item);
|
|
|
|
QScopedPointer<PropagateDirectory> _rootJob;
|
2014-02-18 15:24:35 +04:00
|
|
|
bool useLegacyJobs();
|
2013-10-28 13:47:10 +04:00
|
|
|
|
|
|
|
public:
|
2014-02-17 20:31:03 +04:00
|
|
|
/* 'const' because they are accessed by the thread */
|
|
|
|
|
2014-02-05 23:18:03 +04:00
|
|
|
QThread* _neonThread;
|
2014-02-17 20:31:03 +04:00
|
|
|
ne_session_s * const _session;
|
|
|
|
|
2014-02-05 23:18:03 +04:00
|
|
|
const QString _localDir; // absolute path to the local directory. ends with '/'
|
2014-02-17 20:31:03 +04:00
|
|
|
const QString _remoteDir; // path to the root of the remote. ends with '/' (include remote.php/webdav)
|
|
|
|
const QString _remoteFolder; // folder. (same as remoteDir but without remote.php/webdav)
|
|
|
|
|
|
|
|
SyncJournalDb * const _journal;
|
2014-05-29 14:15:13 +04:00
|
|
|
bool _finishedEmited; // used to ensure that finished is only emit once
|
2013-10-16 18:47:24 +04:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
|
2013-05-03 21:11:00 +04:00
|
|
|
public:
|
2014-12-18 14:09:48 +03:00
|
|
|
OwncloudPropagator(AccountPtr account, ne_session_s *session, const QString &localDir,
|
|
|
|
const QString &remoteDir, const QString &remoteFolder,
|
2014-02-06 17:52:56 +04:00
|
|
|
SyncJournalDb *progressDb, QThread *neonThread)
|
2014-02-05 23:18:03 +04:00
|
|
|
: _neonThread(neonThread)
|
|
|
|
, _session(session)
|
2014-02-17 20:31:03 +04:00
|
|
|
, _localDir((localDir.endsWith(QChar('/'))) ? localDir : localDir+'/' )
|
|
|
|
, _remoteDir((remoteDir.endsWith(QChar('/'))) ? remoteDir : remoteDir+'/' )
|
|
|
|
, _remoteFolder((remoteFolder.endsWith(QChar('/'))) ? remoteFolder : remoteFolder+'/' )
|
2013-10-16 13:59:54 +04:00
|
|
|
, _journal(progressDb)
|
2014-05-29 14:15:13 +04:00
|
|
|
, _finishedEmited(false)
|
2014-09-29 12:30:39 +04:00
|
|
|
, _bandwidthManager(this)
|
2014-02-12 14:07:34 +04:00
|
|
|
, _activeJobs(0)
|
2014-09-10 19:25:13 +04:00
|
|
|
, _anotherSyncNeeded(false)
|
2014-12-18 14:09:48 +03:00
|
|
|
, _account(account)
|
2014-02-05 23:18:03 +04:00
|
|
|
{ }
|
2013-10-28 13:47:10 +04:00
|
|
|
|
2015-03-27 13:11:44 +03:00
|
|
|
~OwncloudPropagator();
|
|
|
|
|
2013-10-28 13:47:10 +04:00
|
|
|
void start(const SyncFileItemVector &_syncedItems);
|
2013-05-16 15:54:22 +04:00
|
|
|
|
2014-02-05 23:18:03 +04:00
|
|
|
QAtomicInt _downloadLimit;
|
|
|
|
QAtomicInt _uploadLimit;
|
2014-11-21 18:55:46 +03:00
|
|
|
BandwidthManager _bandwidthManager;
|
2013-08-14 21:59:16 +04:00
|
|
|
|
2014-02-06 17:52:56 +04:00
|
|
|
QAtomicInt _abortRequested; // boolean set by the main thread to abort.
|
2013-10-02 21:41:17 +04:00
|
|
|
|
2014-02-12 14:07:34 +04:00
|
|
|
/* The number of currently active jobs */
|
|
|
|
int _activeJobs;
|
|
|
|
|
2014-09-10 19:25:13 +04:00
|
|
|
/** We detected that another sync is required after this one */
|
|
|
|
bool _anotherSyncNeeded;
|
|
|
|
|
2014-09-15 19:55:55 +04:00
|
|
|
/* The maximum number of active job in parallel */
|
|
|
|
int maximumActiveJob();
|
|
|
|
|
2014-02-04 18:01:10 +04:00
|
|
|
bool isInSharedDirectory(const QString& file);
|
2014-05-23 20:54:35 +04:00
|
|
|
bool localFileNameClash(const QString& relfile);
|
2014-09-03 14:11:03 +04:00
|
|
|
QString getFilePath(const QString& tmp_file_name) const;
|
2014-02-12 14:07:34 +04:00
|
|
|
|
2014-02-06 17:52:56 +04:00
|
|
|
void abort() {
|
|
|
|
_abortRequested.fetchAndStoreOrdered(true);
|
2014-05-13 15:39:00 +04:00
|
|
|
if (_rootJob) {
|
2014-02-06 17:52:56 +04:00
|
|
|
_rootJob->abort();
|
2014-05-13 15:39:00 +04:00
|
|
|
}
|
2014-05-29 14:15:13 +04:00
|
|
|
emitFinished();
|
2014-02-06 17:52:56 +04:00
|
|
|
}
|
2014-02-17 20:31:03 +04:00
|
|
|
|
2014-05-02 15:04:53 +04:00
|
|
|
// timeout in seconds
|
|
|
|
static int httpTimeout();
|
|
|
|
|
2014-11-07 13:41:21 +03:00
|
|
|
/** Records that a file was touched by a job.
|
|
|
|
*
|
|
|
|
* Thread-safe.
|
|
|
|
*/
|
|
|
|
void addTouchedFile(const QString& fn);
|
|
|
|
|
|
|
|
/** Get the ms since a file was touched, or -1 if it wasn't.
|
|
|
|
*
|
|
|
|
* Thread-safe.
|
|
|
|
*/
|
|
|
|
qint64 timeSinceFileTouched(const QString& fn) const;
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
AccountPtr account() const;
|
|
|
|
|
2014-11-07 13:41:21 +03:00
|
|
|
|
2014-05-29 14:15:13 +04:00
|
|
|
private slots:
|
|
|
|
|
|
|
|
/** Emit the finished signal and make sure it is only emit once */
|
|
|
|
void emitFinished() {
|
|
|
|
if (!_finishedEmited)
|
|
|
|
emit finished();
|
|
|
|
_finishedEmited = true;
|
|
|
|
}
|
|
|
|
|
2014-11-18 19:35:31 +03:00
|
|
|
void scheduleNextJob();
|
|
|
|
|
2013-05-16 15:54:22 +04:00
|
|
|
signals:
|
2013-10-04 17:13:36 +04:00
|
|
|
void completed(const SyncFileItem &);
|
2014-03-14 16:03:16 +04:00
|
|
|
void progress(const SyncFileItem&, quint64 bytes);
|
2013-10-28 13:47:10 +04:00
|
|
|
void finished();
|
2014-03-14 16:03:16 +04:00
|
|
|
/**
|
|
|
|
* Called when we detect that the total number of bytes changes (because a download or upload
|
|
|
|
* turns out to be bigger or smaller than what was initially computed in the update phase
|
|
|
|
*/
|
|
|
|
void adjustTotalTransmissionSize( qint64 adjust );
|
2013-11-25 01:26:50 +04:00
|
|
|
|
2014-11-07 13:41:21 +03:00
|
|
|
private:
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
AccountPtr _account;
|
|
|
|
|
2014-11-07 13:41:21 +03:00
|
|
|
/** Stores the time since a job touched a file. */
|
|
|
|
QHash<QString, QElapsedTimer> _touchedFiles;
|
|
|
|
mutable QMutex _touchedFilesMutex;
|
2013-05-03 21:11:00 +04:00
|
|
|
};
|
|
|
|
|
2014-07-28 14:12:52 +04:00
|
|
|
// Job that wait for all the poll jobs to be completed
|
|
|
|
class CleanupPollsJob : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
QVector< SyncJournalDb::PollInfo > _pollInfos;
|
2014-12-18 14:09:48 +03:00
|
|
|
AccountPtr _account;
|
2014-07-28 14:12:52 +04:00
|
|
|
SyncJournalDb *_journal;
|
|
|
|
QString _localPath;
|
|
|
|
public:
|
2014-12-18 14:09:48 +03:00
|
|
|
explicit CleanupPollsJob(const QVector< SyncJournalDb::PollInfo > &pollInfos, AccountPtr account,
|
2014-07-28 14:12:52 +04:00
|
|
|
SyncJournalDb *journal, const QString &localPath, QObject* parent = 0)
|
|
|
|
: QObject(parent), _pollInfos(pollInfos), _account(account), _journal(journal), _localPath(localPath) {}
|
|
|
|
|
2015-03-27 13:11:44 +03:00
|
|
|
~CleanupPollsJob();
|
|
|
|
|
2014-07-28 14:12:52 +04:00
|
|
|
void start();
|
|
|
|
signals:
|
|
|
|
void finished();
|
2014-07-29 17:51:22 +04:00
|
|
|
void aborted(const QString &error);
|
2014-07-28 14:12:52 +04:00
|
|
|
private slots:
|
|
|
|
void slotPollFinished();
|
|
|
|
};
|
|
|
|
|
2013-05-03 21:11:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|