Simplify csyncthread with keeping csync ctx intact

This commit is contained in:
Klaas Freitag 2013-04-20 13:15:27 +03:00
parent cd7d2a0778
commit 9f0348979b
5 changed files with 189 additions and 198 deletions

View file

@ -62,7 +62,7 @@ void CSyncFolder::startSync(const QStringList &pathList)
emit syncStateChange();
_thread = new QThread(this);
_csync = new CSyncThread( path(), secondPath() );
// _csync = new CSyncThread( _csync );
connect(_csync, SIGNAL(started()), SLOT(slotCSyncStarted()), Qt::QueuedConnection);
connect(_csync, SIGNAL(finished()), SLOT(slotCSyncFinished()), Qt::QueuedConnection);
connect(_csync, SIGNAL(csyncError(QString)), SLOT(slotCSyncError(QString)), Qt::QueuedConnection);

View file

@ -17,7 +17,6 @@
#include "mirall/mirallconfigfile.h"
#include "mirall/theme.h"
#include "mirall/logger.h"
#include "mirall/utility.h"
#include "mirall/owncloudinfo.h"
#ifdef Q_OS_WIN
@ -43,31 +42,16 @@
namespace Mirall {
/* static variables to hold the credentials */
QString CSyncThread::_user;
QString CSyncThread::_passwd;
QNetworkProxy CSyncThread::_proxy;
QString CSyncThread::_csyncConfigDir; // to be able to remove the lock file.
QMutex CSyncThread::_mutex;
QMutex CSyncThread::_syncMutex;
void csyncLogCatcher(CSYNC *ctx,
int verbosity,
const char *function,
const char *buffer,
void *userdata)
{
Logger::instance()->csyncLog( QString::fromUtf8(buffer) );
}
CSyncThread::CSyncThread(const QString &source, const QString &target)
: _source(source)
, _target(target)
CSyncThread::CSyncThread(CSYNC *csync)
{
_mutex.lock();
if( ! _source.endsWith(QLatin1Char('/'))) _source.append(QLatin1Char('/'));
_csync_ctx = csync;
_mutex.unlock();
}
@ -123,7 +107,7 @@ QString CSyncThread::csyncErrorToString( CSYNC_ERROR_CODE err, const char *errSt
errStr = tr("CSync processing step propagate failed.");
break;
case CSYNC_ERR_ACCESS_FAILED:
errStr = tr("<p>The target directory %1 does not exist.</p><p>Please check the sync setup.</p>").arg(_target);
errStr = tr("<p>The target directory does not exist.</p><p>Please check the sync setup.</p>");
// this is critical. The database has to be removed.
emit wipeDb();
break;
@ -182,26 +166,6 @@ QString CSyncThread::csyncErrorToString( CSYNC_ERROR_CODE err, const char *errSt
}
const char* CSyncThread::proxyTypeToCStr(QNetworkProxy::ProxyType type)
{
switch (type) {
case QNetworkProxy::NoProxy:
return "NoProxy";
case QNetworkProxy::DefaultProxy:
return "DefaultProxy";
case QNetworkProxy::Socks5Proxy:
return "Socks5Proxy";
case QNetworkProxy::HttpProxy:
return "HttpProxy";
case QNetworkProxy::HttpCachingProxy:
return "HttpCachingProxy";
case QNetworkProxy::FtpCachingProxy:
return "FtpCachingProxy";
default:
return "NoProxy";
}
}
int CSyncThread::treewalkLocal( TREE_WALK_FILE* file, void *data )
{
return static_cast<CSyncThread*>(data)->treewalkFile( file, false );
@ -294,21 +258,21 @@ int CSyncThread::treewalkError(TREE_WALK_FILE* file)
}
struct CSyncRunScopeHelper {
CSyncRunScopeHelper(CSYNC *_ctx, CSyncThread *_parent)
: ctx(_ctx), parent(_parent)
CSyncRunScopeHelper(CSYNC *ctx, CSyncThread *parent)
: _ctx(ctx), _parent(parent)
{
t.start();
_t.start();
}
~CSyncRunScopeHelper() {
csync_destroy(ctx);
csync_commit(_ctx);
qDebug() << "CSync run took " << t.elapsed() << " Milliseconds";
emit(parent->finished());
parent->_syncMutex.unlock();
qDebug() << "CSync run took " << _t.elapsed() << " Milliseconds";
emit(_parent->finished());
_parent->_syncMutex.unlock();
}
CSYNC *ctx;
QTime t;
CSyncThread *parent;
CSYNC *_ctx;
QTime _t;
CSyncThread *_parent;
};
void CSyncThread::handleSyncError(CSYNC *ctx, const char *state) {
@ -333,93 +297,63 @@ void CSyncThread::startSync()
return;
}
if( ! _csync_ctx ) {
qDebug() << "XXXXXXXXXXXXXXXX FAIL: do not have csync_ctx!";
}
qDebug() << Q_FUNC_INFO << "Sync started";
qDebug() << "starting to sync " << qApp->thread() << QThread::currentThread();
CSYNC *csync;
int proxyPort = _proxy.port();
_mutex.lock();
_syncedItems.clear();
_needsUpdate = false;
_mutex.unlock();
if( csync_create(&csync,
_source.toUtf8().data(),
_target.toUtf8().data()) < 0 ) {
emit csyncError( tr("CSync create failed.") );
}
csync_set_log_verbosity(csync, 11);
MirallConfigFile cfg;
csync_set_config_dir( csync, cfg.configPath().toUtf8() );
_mutex.lock();
_csyncConfigDir = cfg.configPath();
_mutex.unlock();
csync_enable_conflictcopys(csync);
QString excludeList = cfg.excludeFile();
if( !excludeList.isEmpty() ) {
qDebug() << "==== added CSync exclude List: " << excludeList.toUtf8();
csync_add_exclude_list( csync, excludeList.toUtf8() );
}
// cleans up behind us and emits finished() to ease error handling
CSyncRunScopeHelper helper(csync, this);
CSyncRunScopeHelper helper(_csync_ctx, this);
csync_set_userdata(csync, this);
csync_set_userdata(_csync_ctx, this);
csync_set_log_callback( csync, csyncLogCatcher );
csync_set_auth_callback( csync, getauth );
csync_set_progress_callback( csync, progress );
if( csync_init(csync) < 0 ) {
handleSyncError(csync, "csync_init");
return;
}
// set module properties, mainly the proxy information.
// do not use QLatin1String here because that has to be real const char* for C.
csync_set_module_property(csync, "csync_context", csync);
csync_set_module_property(csync, "proxy_type", (char*) proxyTypeToCStr(_proxy.type()) );
csync_set_module_property(csync, "proxy_host", _proxy.hostName().toUtf8().data() );
csync_set_module_property(csync, "proxy_port", &proxyPort );
csync_set_module_property(csync, "proxy_user", _proxy.user().toUtf8().data() );
csync_set_module_property(csync, "proxy_pwd" , _proxy.password().toUtf8().data() );
// csync_set_auth_callback( _csync_ctx, getauth );
csync_set_progress_callback( _csync_ctx, progress );
qDebug() << "#### Update start #################################################### >>";
if( csync_update(csync) < 0 ) {
handleSyncError(csync, "csync_update");
if( csync_update(_csync_ctx) < 0 ) {
handleSyncError(_csync_ctx, "csync_update");
return;
}
qDebug() << "<<#### Update end ###########################################################";
if( csync_reconcile(csync) < 0 ) {
handleSyncError(csync, "cysnc_reconcile");
if( csync_reconcile(_csync_ctx) < 0 ) {
handleSyncError(_csync_ctx, "cysnc_reconcile");
return;
}
bool walkOk = true;
if( csync_walk_local_tree(csync, &treewalkLocal, 0) < 0 ) {
if( csync_walk_local_tree(_csync_ctx, &treewalkLocal, 0) < 0 ) {
qDebug() << "Error in local treewalk.";
walkOk = false;
}
if( walkOk && csync_walk_remote_tree(csync, &treewalkRemote, 0) < 0 ) {
if( walkOk && csync_walk_remote_tree(_csync_ctx, &treewalkRemote, 0) < 0 ) {
qDebug() << "Error in remote treewalk.";
}
if (_needsUpdate)
emit(started());
if( csync_propagate(csync) < 0 ) {
handleSyncError(csync, "cysnc_reconcile");
if( csync_propagate(_csync_ctx) < 0 ) {
handleSyncError(_csync_ctx, "cysnc_reconcile");
return;
}
if( walkOk ) {
if( csync_walk_local_tree(csync, &walkFinalize, 0) < 0 ||
csync_walk_remote_tree( csync, &walkFinalize, 0 ) < 0 ) {
if( csync_walk_local_tree(_csync_ctx, &walkFinalize, 0) < 0 ||
csync_walk_remote_tree( _csync_ctx, &walkFinalize, 0 ) < 0 ) {
qDebug() << "Error in finalize treewalk.";
} else {
// emit the treewalk results.
@ -429,83 +363,11 @@ void CSyncThread::startSync()
qDebug() << Q_FUNC_INFO << "Sync finished";
}
void CSyncThread::setConnectionDetails( const QString &user, const QString &passwd, const QNetworkProxy &proxy )
{
_mutex.lock();
_user = user;
_passwd = passwd;
_proxy = proxy;
_mutex.unlock();
}
QString CSyncThread::csyncConfigDir()
{
return _csyncConfigDir;
}
int CSyncThread::getauth(const char *prompt,
char *buf,
size_t len,
int echo,
int verify,
void *userdata
)
{
int re = 0;
QString qPrompt = QString::fromLatin1( prompt ).trimmed();
if( qPrompt == QLatin1String("Enter your username:") ) {
// qDebug() << "OOO Username requested!";
QMutexLocker locker( &_mutex );
qstrncpy( buf, _user.toUtf8().constData(), len );
} else if( qPrompt == QLatin1String("Enter your password:") ) {
QMutexLocker locker( &_mutex );
// qDebug() << "OOO Password requested!";
qstrncpy( buf, _passwd.toUtf8().constData(), len );
} else {
if( qPrompt.startsWith( QLatin1String("There are problems with the SSL certificate:"))) {
// SSL is requested. If the program came here, the SSL check was done by mirall
// It needs to be checked if the chain is still equal to the one which
// was verified by the user.
QRegExp regexp("fingerprint: ([\\w\\d:]+)");
bool certOk = false;
int pos = 0;
// This is the set of certificates which QNAM accepted, so we should accept
// them as well
QList<QSslCertificate> certs = ownCloudInfo::instance()->certificateChain();
while (!certOk && (pos = regexp.indexIn(qPrompt, 1+pos)) != -1) {
QString neon_fingerprint = regexp.cap(1);
foreach( const QSslCertificate& c, certs ) {
QString verified_shasum = Utility::formatFingerprint(c.digest(QCryptographicHash::Sha1).toHex());
qDebug() << "SSL Fingerprint from neon: " << neon_fingerprint << " compared to verified: " << verified_shasum;
if( verified_shasum == neon_fingerprint ) {
certOk = true;
break;
}
}
}
// certOk = false; DEBUG setting, keep disabled!
if( !certOk ) { // Problem!
qstrcpy( buf, "no" );
re = -1;
} else {
qstrcpy( buf, "yes" ); // Certificate is fine!
}
} else {
qDebug() << "Unknown prompt: <" << prompt << ">";
re = -1;
}
}
return re;
}
void CSyncThread::progress(const char *remote_url, enum csync_notify_type_e kind,
long long o1, long long o2, void *userdata)
{

View file

@ -35,13 +35,11 @@ class CSyncThread : public QObject
{
Q_OBJECT
public:
CSyncThread(const QString &source, const QString &target);
CSyncThread(CSYNC *);
~CSyncThread();
static void setConnectionDetails( const QString&, const QString&, const QNetworkProxy& );
static QString csyncConfigDir();
const char* proxyTypeToCStr(QNetworkProxy::ProxyType);
QString csyncErrorToString( CSYNC_ERROR_CODE, const char * );
Q_INVOKABLE void startSync();
@ -74,26 +72,16 @@ private:
static int walkFinalize(TREE_WALK_FILE*, void* );
static int getauth(const char *prompt,
char *buf,
size_t len,
int echo,
int verify,
void *userdata
);
static QMutex _mutex;
static QMutex _syncMutex;
static QString _user;
static QString _passwd;
static QNetworkProxy _proxy;
static QString _csyncConfigDir;
SyncFileItemVector _syncedItems;
QString _source;
QString _target;
CSYNC *_csync_ctx;
bool _needsUpdate;
friend class CSyncRunScopeHelper;

View file

@ -18,6 +18,7 @@
#include "mirall/owncloudinfo.h"
#include "mirall/credentialstore.h"
#include "mirall/logger.h"
#include "mirall/utility.h"
#include <csync.h>
@ -35,6 +36,14 @@
namespace Mirall {
void csyncLogCatcher(CSYNC *ctx,
int verbosity,
const char *function,
const char *buffer,
void *userdata)
{
Logger::instance()->csyncLog( QString::fromUtf8(buffer) );
}
static QString replaceScheme(const QString &urlStr)
{
@ -50,10 +59,10 @@ static QString replaceScheme(const QString &urlStr)
}
ownCloudFolder::ownCloudFolder(const QString &alias,
const QString &path,
const QString &mpath,
const QString &secondPath,
QObject *parent)
: Folder(alias, path, secondPath, parent)
: Folder(alias, mpath, secondPath, parent)
, _secondPath(secondPath)
, _thread(0)
, _csync(0)
@ -66,13 +75,143 @@ ownCloudFolder::ownCloudFolder(const QString &alias,
connect(this, SIGNAL(syncFinished(SyncResult)), notifier, SLOT(slotSyncFinished(SyncResult)));
qDebug() << "****** ownCloud folder using watcher *******";
// The folder interval is set in the folder parent class.
QString url = replaceScheme(_secondPath);
QString localpath = path();
if( csync_create( &_csync_ctx, localpath.toUtf8().data(), url.toUtf8().data() ) < 0 ) {
qDebug() << "Unable to create csync-context!";
_csync_ctx = 0;
} else {
csync_set_log_callback( _csync_ctx, csyncLogCatcher );
csync_set_log_verbosity(_csync_ctx, 11);
MirallConfigFile cfgFile;
csync_set_config_dir( _csync_ctx, cfgFile.configPath().toUtf8() );
csync_enable_conflictcopys(_csync_ctx);
QString excludeList = cfgFile.excludeFile();
if( !excludeList.isEmpty() ) {
qDebug() << "==== added CSync exclude List: " << excludeList.toUtf8();
csync_add_exclude_list( _csync_ctx, excludeList.toUtf8() );
}
csync_set_auth_callback( _csync_ctx, getauth );
if( csync_init( _csync_ctx ) < 0 ) {
qDebug() << "Could not initialize csync!";
_csync_ctx = 0;
}
if( _csync_ctx ) {
/* Store proxy */
QList<QNetworkProxy> proxies = QNetworkProxyFactory::proxyForQuery(QUrl(cfgFile.ownCloudUrl()));
// We set at least one in Application
Q_ASSERT(proxies.count() > 0);
QNetworkProxy proxy = proxies.first();
int proxyPort = proxy.port();
csync_set_module_property(_csync_ctx, "proxy_type", (char*) proxyTypeToCStr(proxy.type()) );
csync_set_module_property(_csync_ctx, "proxy_host", proxy.hostName().toUtf8().data() );
csync_set_module_property(_csync_ctx, "proxy_port", &proxyPort );
csync_set_module_property(_csync_ctx, "proxy_user", proxy.user().toUtf8().data() );
csync_set_module_property(_csync_ctx, "proxy_pwd" , proxy.password().toUtf8().data() );
csync_set_module_property(_csync_ctx, "csync_context", _csync_ctx);
}
}
}
ownCloudFolder::~ownCloudFolder()
{
// Destroy csync here.
csync_destroy(_csync_ctx);
}
const char* ownCloudFolder::proxyTypeToCStr(QNetworkProxy::ProxyType type)
{
switch (type) {
case QNetworkProxy::NoProxy:
return "NoProxy";
case QNetworkProxy::DefaultProxy:
return "DefaultProxy";
case QNetworkProxy::Socks5Proxy:
return "Socks5Proxy";
case QNetworkProxy::HttpProxy:
return "HttpProxy";
case QNetworkProxy::HttpCachingProxy:
return "HttpCachingProxy";
case QNetworkProxy::FtpCachingProxy:
return "FtpCachingProxy";
default:
return "NoProxy";
}
}
int ownCloudFolder::getauth(const char *prompt,
char *buf,
size_t len,
int echo,
int verify,
void *userdata
)
{
int re = 0;
QMutex mutex;
QString qPrompt = QString::fromLatin1( prompt ).trimmed();
QString user = CredentialStore::instance()->user();
QString pwd = CredentialStore::instance()->password();
if( qPrompt == QLatin1String("Enter your username:") ) {
// qDebug() << "OOO Username requested!";
QMutexLocker locker( &mutex );
qstrncpy( buf, user.toUtf8().constData(), len );
} else if( qPrompt == QLatin1String("Enter your password:") ) {
QMutexLocker locker( &mutex );
// qDebug() << "OOO Password requested!";
qstrncpy( buf, pwd.toUtf8().constData(), len );
} else {
if( qPrompt.startsWith( QLatin1String("There are problems with the SSL certificate:"))) {
// SSL is requested. If the program came here, the SSL check was done by mirall
// It needs to be checked if the chain is still equal to the one which
// was verified by the user.
QRegExp regexp("fingerprint: ([\\w\\d:]+)");
bool certOk = false;
int pos = 0;
// This is the set of certificates which QNAM accepted, so we should accept
// them as well
QList<QSslCertificate> certs = ownCloudInfo::instance()->certificateChain();
while (!certOk && (pos = regexp.indexIn(qPrompt, 1+pos)) != -1) {
QString neon_fingerprint = regexp.cap(1);
foreach( const QSslCertificate& c, certs ) {
QString verified_shasum = Utility::formatFingerprint(c.digest(QCryptographicHash::Sha1).toHex());
qDebug() << "SSL Fingerprint from neon: " << neon_fingerprint << " compared to verified: " << verified_shasum;
if( verified_shasum == neon_fingerprint ) {
certOk = true;
break;
}
}
}
// certOk = false; DEBUG setting, keep disabled!
if( !certOk ) { // Problem!
qstrcpy( buf, "no" );
re = -1;
} else {
qstrcpy( buf, "yes" ); // Certificate is fine!
}
} else {
qDebug() << "Unknown prompt: <" << prompt << ">";
re = -1;
}
}
return re;
}
bool ownCloudFolder::isBusy() const
{
return ( _thread && _thread->isRunning() );
@ -127,21 +266,12 @@ void ownCloudFolder::startSync(const QStringList &pathList)
_syncResult.setStatus( SyncResult::SyncPrepare );
emit syncStateChange();
QString url = replaceScheme(_secondPath);
qDebug() << "*** Start syncing url to ownCloud: " << url;
qDebug() << "*** Start syncing";
_thread = new QThread(this);
_csync = new CSyncThread( path(), url);
_csync = new CSyncThread( _csync_ctx );
_csync->moveToThread(_thread);
QList<QNetworkProxy> proxies = QNetworkProxyFactory::proxyForQuery(QUrl(cfgFile.ownCloudUrl()));
// We set at least one in Application
Q_ASSERT(proxies.count() > 0);
QNetworkProxy proxy = proxies.first();
_csync->setConnectionDetails( CredentialStore::instance()->user(),
CredentialStore::instance()->password(),
proxy );
qRegisterMetaType<SyncFileItemVector>("SyncFileItemVector");
connect( _csync, SIGNAL(treeWalkResult(const SyncFileItemVector&)),
this, SLOT(slotThreadTreeWalkResult(const SyncFileItemVector&)), Qt::QueuedConnection);

View file

@ -88,6 +88,15 @@ private slots:
void slotCSyncFinished();
private:
static int getauth(const char *prompt,
char *buf,
size_t len,
int echo,
int verify,
void *userdata
);
const char* proxyTypeToCStr(QNetworkProxy::ProxyType type);
QString _secondPath;
QThread *_thread;
CSyncThread *_csync;
@ -96,6 +105,8 @@ private:
bool _csyncUnavail;
bool _wipeDb;
SyncFileItemVector _items;
CSYNC *_csync_ctx;
};
}