2012-02-15 12:30:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTextStream>
|
2012-02-28 19:49:13 +04:00
|
|
|
#include <QTime>
|
2012-03-21 21:03:49 +04:00
|
|
|
#include <QDebug>
|
2012-02-28 19:49:13 +04:00
|
|
|
|
2012-02-15 12:30:37 +04:00
|
|
|
#include "mirall/csyncthread.h"
|
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
2012-03-13 20:37:43 +04:00
|
|
|
/* static variables to hold the credentials */
|
|
|
|
QString CSyncThread::_user;
|
|
|
|
QString CSyncThread::_passwd;
|
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
|
|
|
|
int CSyncThread::checkPermissions( TREE_WALK_FILE* file, void *data )
|
|
|
|
{
|
|
|
|
WalkStats *wStats = static_cast<WalkStats*>(data);
|
|
|
|
|
|
|
|
if( !wStats ) {
|
|
|
|
qDebug() << "WalkStats is zero - must not be!";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wStats->seenFiles++;
|
|
|
|
|
|
|
|
switch(file->instruction) {
|
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
|
|
|
wStats->eval++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_REMOVE:
|
|
|
|
wStats->removed++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_RENAME:
|
|
|
|
wStats->renamed++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
|
|
|
wStats->newFiles++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
|
|
|
wStats->conflicts++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
wStats->ignores++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_SYNC:
|
|
|
|
wStats->sync++;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_STAT_ERROR:
|
|
|
|
case CSYNC_INSTRUCTION_ERROR:
|
|
|
|
/* instructions for the propagator */
|
|
|
|
case CSYNC_INSTRUCTION_DELETED:
|
|
|
|
case CSYNC_INSTRUCTION_UPDATED:
|
|
|
|
wStats->error++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wStats->error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug() << "XXXX " << cur->type << " uid: " << cur->uid;
|
|
|
|
qDebug() << wStats->seenFiles << ". Path: " << file->path << ": uid= " << file->uid << " - type: " << file->type;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-28 19:49:13 +04:00
|
|
|
CSyncThread::CSyncThread(const QString &source, const QString &target, bool localCheckOnly)
|
2012-03-13 20:37:43 +04:00
|
|
|
|
2012-02-15 12:30:37 +04:00
|
|
|
: _source(source)
|
|
|
|
, _target(target)
|
2012-02-28 19:49:13 +04:00
|
|
|
, _localCheckOnly( localCheckOnly )
|
2012-02-15 12:30:37 +04:00
|
|
|
, _error(0)
|
2012-03-21 21:03:49 +04:00
|
|
|
|
2012-02-15 12:30:37 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CSyncThread::~CSyncThread()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSyncThread::error() const
|
|
|
|
{
|
|
|
|
return _error != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutex CSyncThread::_mutex;
|
|
|
|
|
|
|
|
void CSyncThread::run()
|
|
|
|
{
|
|
|
|
CSYNC *csync;
|
2012-03-21 21:03:49 +04:00
|
|
|
WalkStats *wStats = new WalkStats;
|
|
|
|
memset(wStats, 0, sizeof(WalkStats));
|
2012-02-15 12:30:37 +04:00
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.lock();
|
2012-02-15 12:30:37 +04:00
|
|
|
_error = csync_create(&csync,
|
|
|
|
_source.toLocal8Bit().data(),
|
|
|
|
_target.toLocal8Bit().data());
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.unlock();
|
|
|
|
|
2012-02-15 12:30:37 +04:00
|
|
|
if (error())
|
|
|
|
return;
|
2012-02-28 19:49:13 +04:00
|
|
|
qDebug() << "## CSync Thread local only: " << _localCheckOnly;
|
2012-03-13 20:37:43 +04:00
|
|
|
csync_set_auth_callback( csync, getauth );
|
2012-02-15 12:30:37 +04:00
|
|
|
csync_enable_conflictcopys(csync);
|
|
|
|
|
2012-02-28 19:49:13 +04:00
|
|
|
QTime t;
|
|
|
|
t.start();
|
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.lock();
|
2012-02-28 19:49:13 +04:00
|
|
|
if( _localCheckOnly ) {
|
|
|
|
_error = csync_set_local_only( csync, true );
|
2012-03-21 21:03:49 +04:00
|
|
|
if(error()) {
|
|
|
|
_mutex.unlock();
|
2012-02-28 19:49:13 +04:00
|
|
|
goto cleanup;
|
2012-03-21 21:03:49 +04:00
|
|
|
}
|
2012-02-28 19:49:13 +04:00
|
|
|
}
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.unlock();
|
2012-02-15 12:30:37 +04:00
|
|
|
|
2012-02-28 19:49:13 +04:00
|
|
|
_error = csync_init(csync);
|
2012-02-15 12:30:37 +04:00
|
|
|
if (error())
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-02-28 19:49:13 +04:00
|
|
|
qDebug() << "############################################################### >>";
|
2012-02-15 12:30:37 +04:00
|
|
|
_error = csync_update(csync);
|
2012-02-28 19:49:13 +04:00
|
|
|
qDebug() << "<<###############################################################";
|
2012-02-15 12:30:37 +04:00
|
|
|
if (error())
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
csync_set_userdata(csync, wStats);
|
2012-02-15 12:30:37 +04:00
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
csync_walk_local_tree(csync, &checkPermissions, 0);
|
2012-02-28 19:49:13 +04:00
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
qDebug() << "New files: " << wStats->newFiles;
|
|
|
|
qDebug() << "Updated files: " << wStats->eval;
|
|
|
|
qDebug() << "Walked files: " << wStats->seenFiles;
|
|
|
|
emit treeWalkResult(wStats);
|
2012-02-28 19:49:13 +04:00
|
|
|
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.lock();
|
2012-02-28 19:49:13 +04:00
|
|
|
if( _localCheckOnly ) {
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.unlock();
|
|
|
|
/* check if there are happend changes in the file system */
|
|
|
|
if( (wStats->newFiles + wStats->eval + wStats->removed + wStats->renamed) > 0 ) {
|
|
|
|
qDebug() << "OO there are local changes!";
|
2012-02-28 19:49:13 +04:00
|
|
|
}
|
|
|
|
// we have to go out here as its local check only.
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
2012-03-21 21:03:49 +04:00
|
|
|
_mutex.unlock();
|
|
|
|
// check if we can write all over.
|
|
|
|
|
2012-02-28 19:49:13 +04:00
|
|
|
_error = csync_reconcile(csync);
|
|
|
|
if (error())
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
_error = csync_propagate(csync);
|
|
|
|
}
|
2012-02-15 12:30:37 +04:00
|
|
|
cleanup:
|
|
|
|
csync_destroy(csync);
|
2012-03-21 21:03:49 +04:00
|
|
|
delete wStats;
|
2012-02-28 19:49:13 +04:00
|
|
|
qDebug() << "CSync run took " << t.elapsed() << " Milliseconds";
|
|
|
|
}
|
|
|
|
|
2012-03-13 20:37:43 +04:00
|
|
|
|
|
|
|
void CSyncThread::setUserPwd( const QString& user, const QString& passwd )
|
|
|
|
{
|
|
|
|
_user = user;
|
|
|
|
_passwd = passwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSyncThread::getauth(const char *prompt,
|
|
|
|
char *buf,
|
|
|
|
size_t len,
|
|
|
|
int echo,
|
|
|
|
int verify,
|
|
|
|
void *userdata
|
|
|
|
)
|
|
|
|
{
|
|
|
|
QString qPrompt = QString::fromLocal8Bit( prompt ).trimmed();
|
|
|
|
|
|
|
|
if( qPrompt == QString::fromLocal8Bit("Enter your username:") ) {
|
|
|
|
qDebug() << "OOO Username requested!";
|
|
|
|
strncpy( buf, _user.toLocal8Bit().constData(), len );
|
|
|
|
} else if( qPrompt == QString::fromLocal8Bit("Enter your password:") ) {
|
|
|
|
qDebug() << "OOO Password requested!";
|
|
|
|
strncpy( buf, _passwd.toLocal8Bit().constData(), len );
|
|
|
|
} else {
|
|
|
|
qDebug() << "Unknown prompt: <" << prompt << ">";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 12:30:37 +04:00
|
|
|
}
|