2013-10-03 19:04:55 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Dominik Schmidt <dev@dominik-schmidt.de>
|
2014-07-14 17:28:26 +04:00
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
2013-10-03 19:04:55 +04:00
|
|
|
*
|
|
|
|
* 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-07-11 02:31:24 +04:00
|
|
|
#include "socketapi.h"
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-11-10 01:25:57 +03:00
|
|
|
#include "configfile.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "folderman.h"
|
|
|
|
#include "folder.h"
|
|
|
|
#include "utility.h"
|
|
|
|
#include "theme.h"
|
|
|
|
#include "syncjournalfilerecord.h"
|
|
|
|
#include "syncfileitem.h"
|
2014-09-18 19:08:53 +04:00
|
|
|
#include "filesystem.h"
|
2014-08-27 14:02:47 +04:00
|
|
|
#include "version.h"
|
2013-10-03 19:04:55 +04:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QMetaObject>
|
|
|
|
#include <QStringList>
|
2014-06-02 14:08:06 +04:00
|
|
|
#include <QScopedPointer>
|
2013-10-03 19:04:55 +04:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QApplication>
|
2014-09-29 14:19:33 +04:00
|
|
|
#include <QLocalSocket>
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-10-13 16:14:43 +04:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
|
2014-09-29 15:54:13 +04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#endif
|
|
|
|
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-08-27 14:02:47 +04:00
|
|
|
// This is the version that is returned when the client asks for the VERSION.
|
|
|
|
// The first number should be changed if there is an incompatible change that breaks old clients.
|
|
|
|
// The second number should be changed when there are new features.
|
|
|
|
#define MIRALL_SOCKET_API_VERSION "1.0"
|
|
|
|
|
2014-06-19 16:08:30 +04:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
enum csync_exclude_type_e {
|
|
|
|
CSYNC_NOT_EXCLUDED = 0,
|
|
|
|
CSYNC_FILE_SILENTLY_EXCLUDED,
|
|
|
|
CSYNC_FILE_EXCLUDE_AND_REMOVE,
|
|
|
|
CSYNC_FILE_EXCLUDE_LIST,
|
|
|
|
CSYNC_FILE_EXCLUDE_INVALID_CHAR
|
|
|
|
};
|
|
|
|
typedef enum csync_exclude_type_e CSYNC_EXCLUDE_TYPE;
|
|
|
|
|
2014-08-21 14:43:04 +04:00
|
|
|
CSYNC_EXCLUDE_TYPE csync_excluded_no_ctx(c_strlist_t *excludes, const char *path, int filetype);
|
|
|
|
int csync_exclude_load(const char *fname, c_strlist_t **list);
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-10-03 19:04:55 +04:00
|
|
|
|
|
|
|
#define DEBUG qDebug() << "SocketApi: "
|
|
|
|
|
2014-07-17 17:00:21 +04:00
|
|
|
SocketApi::SocketApi(QObject* parent)
|
2013-10-03 19:04:55 +04:00
|
|
|
: QObject(parent)
|
2014-08-21 14:43:04 +04:00
|
|
|
, _excludes(0)
|
2014-06-19 16:08:30 +04:00
|
|
|
{
|
2014-09-29 14:19:33 +04:00
|
|
|
QString socketPath;
|
2014-09-29 15:54:13 +04:00
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
if (Utility::isWindows()) {
|
|
|
|
socketPath = QLatin1String("\\\\.\\pipe\\")
|
2014-10-24 01:46:17 +04:00
|
|
|
+ QLatin1String("ownCloud");
|
|
|
|
// TODO: once the windows extension supports multiple
|
|
|
|
// client connections, switch back to the theme name
|
|
|
|
// See issue #2388
|
|
|
|
// + Theme::instance()->appName();
|
2014-09-30 09:36:20 +04:00
|
|
|
} else if (Utility::isMac()) {
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
// Always using Qt5 on OS X
|
|
|
|
QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
|
|
|
|
socketPath = runtimeDir + "/SyncStateHelper/" + Theme::instance()->appName() + ".socket";
|
|
|
|
// We use the generic SyncStateHelper name on OS X since the different branded clients
|
|
|
|
// should unfortunately not mention that they are ownCloud :-)
|
|
|
|
#endif
|
2014-09-30 13:15:27 +04:00
|
|
|
} else if( Utility::isLinux() ) {
|
2014-09-29 15:54:13 +04:00
|
|
|
QString runtimeDir;
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
|
|
|
|
#else
|
|
|
|
runtimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
|
2014-12-01 14:05:14 +03:00
|
|
|
if (runtimeDir.isEmpty()) {
|
|
|
|
runtimeDir = QDir::tempPath() + QLatin1String("/runtime-")
|
|
|
|
+ QString::fromLocal8Bit(qgetenv("USER"));
|
|
|
|
QDir().mkdir(runtimeDir);
|
|
|
|
}
|
2014-09-29 15:54:13 +04:00
|
|
|
#endif
|
|
|
|
socketPath = runtimeDir + "/" + Theme::instance()->appName() + "/socket";
|
2014-09-30 13:15:27 +04:00
|
|
|
} else {
|
|
|
|
DEBUG << "An unexpected system detected";
|
2014-06-19 17:35:29 +04:00
|
|
|
}
|
2014-06-19 16:08:30 +04:00
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
QLocalServer::removeServer(socketPath);
|
2014-09-29 15:54:13 +04:00
|
|
|
QFileInfo info(socketPath);
|
|
|
|
if (!info.dir().exists()) {
|
|
|
|
bool result = info.dir().mkpath(".");
|
|
|
|
DEBUG << "creating" << info.dir().path() << result;
|
2014-09-30 13:16:49 +04:00
|
|
|
if( result ) {
|
|
|
|
QFile::setPermissions(socketPath,
|
|
|
|
QFile::Permissions(QFile::ReadOwner+QFile::WriteOwner+QFile::ExeOwner));
|
|
|
|
}
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
2014-09-29 14:19:33 +04:00
|
|
|
if(!_localServer.listen(socketPath)) {
|
|
|
|
DEBUG << "can't start server" << socketPath;
|
2014-06-19 16:08:30 +04:00
|
|
|
} else {
|
2014-09-29 14:19:33 +04:00
|
|
|
DEBUG << "server started, listening at " << socketPath;
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
2014-06-19 17:35:29 +04:00
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
connect(&_localServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
|
2013-10-03 19:04:55 +04:00
|
|
|
|
|
|
|
// folder watcher
|
2014-07-15 15:34:32 +04:00
|
|
|
connect(FolderMan::instance(), SIGNAL(folderSyncStateChange(QString)), this, SLOT(slotUpdateFolderView(QString)));
|
|
|
|
connect(ProgressDispatcher::instance(), SIGNAL(jobCompleted(QString,SyncFileItem)),
|
|
|
|
SLOT(slotJobCompleted(QString,SyncFileItem)));
|
2014-08-27 21:05:26 +04:00
|
|
|
connect(ProgressDispatcher::instance(), SIGNAL(syncItemDiscovered(QString,SyncFileItem)),
|
|
|
|
this, SLOT(slotSyncItemDiscovered(QString,SyncFileItem)));
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketApi::~SocketApi()
|
|
|
|
{
|
|
|
|
DEBUG << "dtor";
|
2014-09-29 14:19:33 +04:00
|
|
|
_localServer.close();
|
2014-08-21 14:43:04 +04:00
|
|
|
slotClearExcludesList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::slotClearExcludesList()
|
|
|
|
{
|
|
|
|
c_strlist_clear(_excludes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::slotReadExcludes()
|
|
|
|
{
|
2014-11-10 00:30:29 +03:00
|
|
|
ConfigFile cfgFile;
|
2014-08-21 14:43:04 +04:00
|
|
|
slotClearExcludesList();
|
2014-11-10 00:30:29 +03:00
|
|
|
QString excludeList = cfgFile.excludeFile( ConfigFile::SystemScope );
|
2014-08-21 14:43:04 +04:00
|
|
|
if( !excludeList.isEmpty() ) {
|
|
|
|
qDebug() << "==== added system ignore list to socketapi:" << excludeList.toUtf8();
|
|
|
|
csync_exclude_load(excludeList.toUtf8(), &_excludes);
|
|
|
|
}
|
2014-11-10 00:30:29 +03:00
|
|
|
excludeList = cfgFile.excludeFile( ConfigFile::UserScope );
|
2014-08-21 14:43:04 +04:00
|
|
|
if( !excludeList.isEmpty() ) {
|
|
|
|
qDebug() << "==== added user defined ignore list to csync:" << excludeList.toUtf8();
|
|
|
|
csync_exclude_load(excludeList.toUtf8(), &_excludes);
|
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
void SocketApi::slotNewConnection()
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-09-29 14:19:33 +04:00
|
|
|
SocketType* socket = _localServer.nextPendingConnection();
|
2014-07-10 17:50:24 +04:00
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
if( ! socket ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-17 17:00:21 +04:00
|
|
|
DEBUG << "New connection" << socket;
|
2014-06-02 14:08:06 +04:00
|
|
|
connect(socket, SIGNAL(readyRead()), this, SLOT(slotReadSocket()));
|
2013-10-03 19:04:55 +04:00
|
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection()));
|
|
|
|
Q_ASSERT(socket->readAll().isEmpty());
|
|
|
|
|
|
|
|
_listeners.append(socket);
|
2014-07-15 19:55:55 +04:00
|
|
|
|
2014-09-12 17:06:51 +04:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// We want to tell our location so it can load the icons
|
|
|
|
// e.g. "/Users/guruz/woboq/owncloud/client/buildmirall/owncloud.app/Contents/MacOS/"
|
|
|
|
QString iconPath = qApp->applicationDirPath() + "/../Resources/icons/";
|
|
|
|
if (!QDir(iconPath).exists()) {
|
|
|
|
DEBUG << "Icon path " << iconPath << " does not exist, did you forget make install?";
|
|
|
|
}
|
|
|
|
broadcastMessage(QLatin1String("ICON_PATH"), iconPath );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-07-15 19:55:55 +04:00
|
|
|
foreach( QString alias, FolderMan::instance()->map().keys() ) {
|
2014-07-25 14:10:45 +04:00
|
|
|
slotRegisterPath(alias);
|
2014-07-15 19:55:55 +04:00
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::onLostConnection()
|
|
|
|
{
|
|
|
|
DEBUG << "Lost connection " << sender();
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
SocketType* socket = qobject_cast<SocketType*>(sender());
|
2013-10-03 19:04:55 +04:00
|
|
|
_listeners.removeAll(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
void SocketApi::slotReadSocket()
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-09-29 14:19:33 +04:00
|
|
|
SocketType* socket = qobject_cast<SocketType*>(sender());
|
2013-10-03 19:04:55 +04:00
|
|
|
Q_ASSERT(socket);
|
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
while(socket->canReadLine()) {
|
2013-10-03 19:04:55 +04:00
|
|
|
QString line = QString::fromUtf8(socket->readLine()).trimmed();
|
|
|
|
QString command = line.split(":").first();
|
|
|
|
QString function = QString(QLatin1String("command_")).append(command);
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
QString functionWithArguments = function + QLatin1String("(QString,SocketType*)");
|
2013-10-03 19:04:55 +04:00
|
|
|
int indexOfMethod = this->metaObject()->indexOfMethod(functionWithArguments.toAscii());
|
|
|
|
|
|
|
|
QString argument = line.remove(0, command.length()+1).trimmed();
|
2014-06-02 14:08:06 +04:00
|
|
|
if(indexOfMethod != -1) {
|
2014-09-29 14:19:33 +04:00
|
|
|
QMetaObject::invokeMethod(this, function.toAscii(), Q_ARG(QString, argument), Q_ARG(SocketType*, socket));
|
2014-06-02 14:08:06 +04:00
|
|
|
} else {
|
2013-10-03 19:04:55 +04:00
|
|
|
DEBUG << "The command is not supported by this version of the client:" << command << "with argument:" << argument;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
void SocketApi::slotRegisterPath( const QString& alias )
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-07-25 14:10:45 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(alias);
|
|
|
|
if (f) {
|
|
|
|
broadcastMessage(QLatin1String("REGISTER_PATH"), f->path() );
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 13:30:47 +04:00
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
void SocketApi::slotUnregisterPath( const QString& alias )
|
|
|
|
{
|
2014-07-11 13:30:47 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(alias);
|
|
|
|
if (f) {
|
2014-08-20 18:24:06 +04:00
|
|
|
broadcastMessage(QLatin1String("UNREGISTER_PATH"), f->path(), QString::null, true );
|
2014-10-13 19:31:40 +04:00
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
if( _dbQueries.contains(f)) {
|
|
|
|
SqlQuery *h = _dbQueries[f];
|
|
|
|
if( h ) {
|
|
|
|
h->finish();
|
2014-10-20 12:50:57 +04:00
|
|
|
}
|
2014-10-20 18:54:51 +04:00
|
|
|
_dbQueries.remove(f);
|
2014-10-13 19:31:40 +04:00
|
|
|
}
|
2014-10-20 19:31:00 +04:00
|
|
|
if( _openDbs.contains(f) ) {
|
|
|
|
SqlDatabase *db = _openDbs[f];
|
|
|
|
if( db ) {
|
|
|
|
db->close();
|
|
|
|
}
|
|
|
|
_openDbs.remove(f);
|
|
|
|
}
|
2014-07-11 13:30:47 +04:00
|
|
|
}
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
2014-07-11 13:30:47 +04:00
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
void SocketApi::slotUpdateFolderView(const QString& alias)
|
|
|
|
{
|
2014-10-21 17:26:51 +04:00
|
|
|
if (_listeners.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(alias);
|
|
|
|
if (f) {
|
|
|
|
// do only send UPDATE_VIEW for a couple of status
|
|
|
|
if( f->syncResult().status() == SyncResult::SyncPrepare ||
|
|
|
|
f->syncResult().status() == SyncResult::Success ||
|
2014-08-07 16:07:08 +04:00
|
|
|
f->syncResult().status() == SyncResult::Paused ||
|
2014-07-25 14:10:45 +04:00
|
|
|
f->syncResult().status() == SyncResult::Problem ||
|
|
|
|
f->syncResult().status() == SyncResult::Error ||
|
|
|
|
f->syncResult().status() == SyncResult::SetupError ) {
|
2014-10-21 17:26:51 +04:00
|
|
|
|
|
|
|
broadcastMessage(QLatin1String("STATUS"), f->path() ,
|
|
|
|
this->fileStatus(f, "", _excludes).toSocketAPIString());
|
|
|
|
|
2014-10-21 16:57:50 +04:00
|
|
|
broadcastMessage(QLatin1String("UPDATE_VIEW"), f->path() );
|
2014-10-13 19:23:42 +04:00
|
|
|
} else {
|
|
|
|
qDebug() << "Not sending UPDATE_VIEW for" << alias << "because status() is" << f->syncResult().status();
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 17:52:55 +04:00
|
|
|
void SocketApi::slotJobCompleted(const QString &folder, const SyncFileItem &item)
|
|
|
|
{
|
2014-10-21 17:26:51 +04:00
|
|
|
if (_listeners.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-06 17:52:55 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(folder);
|
2014-08-27 21:05:26 +04:00
|
|
|
if (!f) {
|
2014-06-06 17:52:55 +04:00
|
|
|
return;
|
2014-08-27 21:05:26 +04:00
|
|
|
}
|
2014-06-06 17:52:55 +04:00
|
|
|
|
|
|
|
const QString path = f->path() + item.destination();
|
|
|
|
|
|
|
|
QString command = QLatin1String("OK");
|
|
|
|
if (Progress::isWarningKind(item._status)) {
|
|
|
|
command = QLatin1String("ERROR");
|
|
|
|
}
|
2014-08-27 21:05:26 +04:00
|
|
|
broadcastMessage(QLatin1String("STATUS"), path, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::slotSyncItemDiscovered(const QString &folder, const SyncFileItem &item)
|
|
|
|
{
|
2014-10-21 17:26:51 +04:00
|
|
|
if (_listeners.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-27 21:05:26 +04:00
|
|
|
if (item._instruction == CSYNC_INSTRUCTION_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Folder *f = FolderMan::instance()->folder(folder);
|
|
|
|
if (!f) {
|
|
|
|
return;
|
2014-08-04 13:08:44 +04:00
|
|
|
}
|
2014-08-27 21:05:26 +04:00
|
|
|
|
|
|
|
const QString path = f->path() + item.destination();
|
|
|
|
|
|
|
|
const QString command = QLatin1String("SYNC");
|
|
|
|
broadcastMessage(QLatin1String("STATUS"), path, command);
|
2014-06-06 17:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
void SocketApi::sendMessage(SocketType *socket, const QString& message, bool doWait)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
|
|
|
DEBUG << "Sending message: " << message;
|
|
|
|
QString localMessage = message;
|
2014-07-25 14:10:45 +04:00
|
|
|
if( ! localMessage.endsWith(QLatin1Char('\n'))) {
|
|
|
|
localMessage.append(QLatin1Char('\n'));
|
|
|
|
}
|
2014-10-10 18:20:57 +04:00
|
|
|
|
|
|
|
QByteArray bytesToSend = localMessage.toUtf8();
|
|
|
|
qint64 sent = socket->write(bytesToSend);
|
2014-08-20 18:24:06 +04:00
|
|
|
if( doWait ) {
|
|
|
|
socket->waitForBytesWritten(1000);
|
|
|
|
}
|
2014-10-10 18:20:57 +04:00
|
|
|
if( sent != bytesToSend.length() ) {
|
2014-07-25 14:10:45 +04:00
|
|
|
qDebug() << "WARN: Could not send all data on socket for " << localMessage;
|
|
|
|
}
|
|
|
|
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2014-08-20 18:24:06 +04:00
|
|
|
void SocketApi::broadcastMessage( const QString& verb, const QString& path, const QString& status, bool doWait )
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-07-25 14:10:45 +04:00
|
|
|
QString msg(verb);
|
|
|
|
|
|
|
|
if( !status.isEmpty() ) {
|
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
msg.append(status);
|
|
|
|
}
|
|
|
|
if( !path.isEmpty() ) {
|
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
msg.append(QDir::toNativeSeparators(path));
|
|
|
|
}
|
|
|
|
|
2014-10-13 19:23:42 +04:00
|
|
|
// sendMessage already has a debug output
|
|
|
|
//DEBUG << "Broadcasting to" << _listeners.count() << "listeners: " << msg;
|
2014-09-29 14:19:33 +04:00
|
|
|
foreach(SocketType *socket, _listeners) {
|
2014-08-20 18:24:06 +04:00
|
|
|
sendMessage(socket, msg, doWait);
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
void SocketApi::command_RETRIEVE_FOLDER_STATUS(const QString& argument, SocketType* socket)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-06-19 17:02:27 +04:00
|
|
|
// This command is the same as RETRIEVE_FILE_STATUS
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-10-22 16:43:23 +04:00
|
|
|
//qDebug() << Q_FUNC_INFO << argument;
|
2014-06-19 17:02:27 +04:00
|
|
|
command_RETRIEVE_FILE_STATUS(argument, socket);
|
2014-06-06 17:37:04 +04:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
void SocketApi::command_RETRIEVE_FILE_STATUS(const QString& argument, SocketType* socket)
|
2014-06-06 17:37:04 +04:00
|
|
|
{
|
|
|
|
if( !socket ) {
|
|
|
|
qDebug() << "No valid socket object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << argument;
|
|
|
|
|
|
|
|
QString statusString;
|
|
|
|
|
2014-07-10 16:27:52 +04:00
|
|
|
Folder* syncFolder = FolderMan::instance()->folderForPath( argument );
|
|
|
|
if (!syncFolder) {
|
|
|
|
// this can happen in offline mode e.g.: nothing to worry about
|
2014-06-06 17:37:04 +04:00
|
|
|
DEBUG << "folder offline or not watched:" << argument;
|
|
|
|
statusString = QLatin1String("NOP");
|
2014-07-10 16:27:52 +04:00
|
|
|
} else {
|
2014-10-22 17:33:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
const QString file = QDir::cleanPath(argument).mid(QDir::cleanPath(syncFolder->path()).length()+1);
|
2014-10-17 15:25:15 +04:00
|
|
|
SyncFileStatus fileStatus = this->fileStatus(syncFolder, file, _excludes);
|
2014-06-06 17:37:04 +04:00
|
|
|
|
2014-07-10 16:27:52 +04:00
|
|
|
statusString = fileStatus.toSocketAPIString();
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
2014-06-06 17:37:04 +04:00
|
|
|
|
2014-07-17 19:41:20 +04:00
|
|
|
QString message = QLatin1String("STATUS:")+statusString+QLatin1Char(':')
|
|
|
|
+QDir::toNativeSeparators(argument);
|
2014-06-06 17:37:04 +04:00
|
|
|
sendMessage(socket, message);
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
void SocketApi::command_VERSION(const QString&, SocketType* socket)
|
2014-08-27 14:02:47 +04:00
|
|
|
{
|
2014-08-29 19:28:36 +04:00
|
|
|
sendMessage(socket, QLatin1String("VERSION:" MIRALL_VERSION_STRING ":" MIRALL_SOCKET_API_VERSION));
|
2014-08-27 14:02:47 +04:00
|
|
|
}
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
SqlQuery* SocketApi::getSqlQuery( Folder *folder )
|
2014-10-13 17:38:57 +04:00
|
|
|
{
|
2014-10-20 18:54:51 +04:00
|
|
|
if( !folder ) {
|
|
|
|
return 0;
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
if( _dbQueries.contains(folder) ) {
|
|
|
|
return _dbQueries[folder];
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
2014-10-20 18:54:51 +04:00
|
|
|
|
|
|
|
/* No valid sql query object yet for this folder */
|
2014-10-13 17:38:57 +04:00
|
|
|
int rc;
|
2014-10-20 18:54:51 +04:00
|
|
|
const QString sql("SELECT inode, mode, modtime, type, md5, fileid, remotePerm FROM "
|
|
|
|
"metadata WHERE phash=?1");
|
2014-10-13 19:31:40 +04:00
|
|
|
QString dbFileName = folder->journalDb()->databaseFilePath();
|
2014-10-13 17:38:57 +04:00
|
|
|
|
2014-10-20 19:05:14 +04:00
|
|
|
QFileInfo fi(dbFileName);
|
2014-10-20 19:10:34 +04:00
|
|
|
if( fi.exists() ) {
|
2014-10-20 18:54:51 +04:00
|
|
|
SqlDatabase *db = new SqlDatabase;
|
|
|
|
|
2014-10-20 19:31:00 +04:00
|
|
|
if( db && db->open(dbFileName) ) {
|
|
|
|
_openDbs.insert(folder, db);
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
SqlQuery *query = new SqlQuery(*db);
|
|
|
|
rc = query->prepare(sql);
|
2014-10-13 17:38:57 +04:00
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
if( rc != SQLITE_OK ) {
|
|
|
|
delete query;
|
|
|
|
qDebug() << "Unable to prepare the query statement:" << rc;
|
|
|
|
return 0; // do not insert into hash
|
|
|
|
}
|
|
|
|
_dbQueries.insert( folder, query);
|
|
|
|
return query;
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
2014-10-20 19:10:34 +04:00
|
|
|
} else {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Journal to query does not yet exist.";
|
2014-10-13 19:31:40 +04:00
|
|
|
}
|
2014-10-20 18:54:51 +04:00
|
|
|
return 0;
|
2014-10-13 19:31:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncJournalFileRecord SocketApi::dbFileRecord_capi( Folder *folder, QString fileName )
|
|
|
|
{
|
|
|
|
if( !(folder && folder->journalDb()) ) {
|
|
|
|
return SyncJournalFileRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fileName.startsWith( folder->path() )) {
|
|
|
|
fileName.remove(0, folder->path().length());
|
|
|
|
}
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
SqlQuery *query = getSqlQuery(folder);
|
2014-10-13 19:31:40 +04:00
|
|
|
SyncJournalFileRecord rec;
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
if( query ) {
|
2014-10-13 17:38:57 +04:00
|
|
|
qlonglong phash = SyncJournalDb::getPHash( fileName );
|
2014-10-20 18:54:51 +04:00
|
|
|
query->bindValue(1, phash);
|
2014-10-13 17:38:57 +04:00
|
|
|
// int column_count = sqlite3_column_count(stmt);
|
|
|
|
|
2014-10-20 18:54:51 +04:00
|
|
|
if (query->next()) {
|
|
|
|
rec._path = fileName;
|
|
|
|
rec._inode = query->int64Value(0);
|
|
|
|
rec._mode = query->intValue(1);
|
|
|
|
rec._modtime = Utility::qDateTimeFromTime_t( query->int64Value(2));
|
|
|
|
rec._type = query->intValue(3);
|
|
|
|
rec._etag = query->baValue(4);
|
|
|
|
rec._fileId = query->baValue(5);
|
|
|
|
rec._remotePerm = query->baValue(6);
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
2014-10-20 18:54:51 +04:00
|
|
|
query->reset();
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get status about a single file.
|
|
|
|
*/
|
|
|
|
SyncFileStatus SocketApi::fileStatus(Folder *folder, const QString& systemFileName, c_strlist_t *excludes )
|
|
|
|
{
|
|
|
|
QString file = folder->path();
|
|
|
|
QString fileName = systemFileName.normalized(QString::NormalizationForm_C);
|
2014-10-22 15:29:59 +04:00
|
|
|
QString fileNameSlash = fileName;
|
2014-10-13 17:38:57 +04:00
|
|
|
|
|
|
|
if( fileName != QLatin1String("/") && !fileName.isEmpty() ) {
|
|
|
|
file = folder->path() + fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fileName.endsWith(QLatin1Char('/')) ) {
|
|
|
|
fileName.truncate(fileName.length()-1);
|
|
|
|
qDebug() << "Removed trailing slash: " << fileName;
|
2014-10-22 15:29:59 +04:00
|
|
|
} else {
|
|
|
|
fileNameSlash += QLatin1Char('/');
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo fi(file);
|
|
|
|
|
|
|
|
if( !fi.exists() ) {
|
|
|
|
qDebug() << "OO File " << file << " is not existing";
|
|
|
|
return SyncFileStatus(SyncFileStatus::STATUS_STAT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// file is ignored?
|
|
|
|
if( fi.isSymLink() ) {
|
|
|
|
return SyncFileStatus(SyncFileStatus::STATUS_IGNORE);
|
|
|
|
}
|
2014-10-14 13:43:10 +04:00
|
|
|
|
|
|
|
csync_ftw_type_e type = CSYNC_FTW_TYPE_FILE;
|
2014-10-13 17:38:57 +04:00
|
|
|
if( fi.isDir() ) {
|
|
|
|
type = CSYNC_FTW_TYPE_DIR;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:43:23 +04:00
|
|
|
// Is it excluded?
|
2014-10-22 17:33:35 +04:00
|
|
|
CSYNC_EXCLUDE_TYPE excl = csync_excluded_no_ctx(excludes, fileName.toUtf8(), type);
|
2014-10-13 17:38:57 +04:00
|
|
|
if( excl != CSYNC_NOT_EXCLUDED ) {
|
|
|
|
return SyncFileStatus(SyncFileStatus::STATUS_IGNORE);
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:29:59 +04:00
|
|
|
// Error if it is in the selective sync blacklistr
|
|
|
|
foreach(const auto &s, folder->selectiveSyncBlackList()) {
|
|
|
|
if (fileNameSlash.startsWith(s)) {
|
|
|
|
return SyncFileStatus(SyncFileStatus::STATUS_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 13:43:10 +04:00
|
|
|
|
|
|
|
SyncFileStatus status(SyncFileStatus::STATUS_NONE);
|
2014-10-22 17:33:35 +04:00
|
|
|
SyncJournalFileRecord rec = dbFileRecord_capi(folder, fileName );
|
2014-10-22 17:05:37 +04:00
|
|
|
|
|
|
|
if (folder->estimateState(fileName, type, &status)) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Folder estimated status for" << fileName << "to" << status.toSocketAPIString();
|
|
|
|
} else if (fileName == "") {
|
|
|
|
// sync folder itself
|
|
|
|
switch (folder->syncResult().status()) {
|
|
|
|
case SyncResult::Undefined:
|
|
|
|
case SyncResult::NotYetStarted:
|
|
|
|
case SyncResult::SyncPrepare:
|
|
|
|
case SyncResult::SyncRunning:
|
|
|
|
status.set(SyncFileStatus::STATUS_EVAL);
|
2014-10-14 13:43:10 +04:00
|
|
|
return status;
|
2014-10-22 14:17:44 +04:00
|
|
|
|
2014-10-22 17:05:37 +04:00
|
|
|
case SyncResult::Success:
|
|
|
|
case SyncResult::Problem:
|
|
|
|
status.set(SyncFileStatus::STATUS_SYNC);
|
|
|
|
return status;
|
2014-10-22 14:17:44 +04:00
|
|
|
|
2014-10-22 17:05:37 +04:00
|
|
|
case SyncResult::Error:
|
|
|
|
case SyncResult::SetupError:
|
|
|
|
case SyncResult::SyncAbortRequested:
|
|
|
|
status.set(SyncFileStatus::STATUS_ERROR);
|
|
|
|
return status;
|
2014-10-22 14:17:44 +04:00
|
|
|
|
2014-10-22 17:05:37 +04:00
|
|
|
case SyncResult::Paused:
|
|
|
|
status.set(SyncFileStatus::STATUS_IGNORE);
|
|
|
|
return status;
|
2014-10-14 13:43:10 +04:00
|
|
|
}
|
2014-10-22 17:05:37 +04:00
|
|
|
} else if (type == CSYNC_FTW_TYPE_DIR) {
|
2014-10-14 13:43:10 +04:00
|
|
|
if (rec.isValid()) {
|
|
|
|
status.set(SyncFileStatus::STATUS_SYNC);
|
|
|
|
} else {
|
2014-10-22 17:05:37 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "Could not determine state for folder" << fileName << "will set STATUS_NEW";
|
|
|
|
status.set(SyncFileStatus::STATUS_NEW);
|
2014-10-14 13:43:10 +04:00
|
|
|
}
|
|
|
|
} else if (type == CSYNC_FTW_TYPE_FILE) {
|
|
|
|
if (rec.isValid()) {
|
|
|
|
if( FileSystem::getModTime(fi.absoluteFilePath()) == Utility::qDateTimeToTime_t(rec._modtime) ) {
|
|
|
|
status.set(SyncFileStatus::STATUS_SYNC);
|
2014-10-13 17:38:57 +04:00
|
|
|
} else {
|
2014-10-22 19:13:34 +04:00
|
|
|
if (rec._remotePerm.isNull() || rec._remotePerm.contains("W") ) {
|
|
|
|
status.set(SyncFileStatus::STATUS_EVAL);
|
|
|
|
} else {
|
|
|
|
status.set(SyncFileStatus::STATUS_ERROR);
|
|
|
|
}
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
2014-10-22 17:05:37 +04:00
|
|
|
} else {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Could not determine state for file" << fileName << "will set STATUS_NEW";
|
|
|
|
status.set(SyncFileStatus::STATUS_NEW);
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:37 +04:00
|
|
|
if (rec.isValid()) {
|
|
|
|
if (rec._remotePerm.isNull()) {
|
|
|
|
// probably owncloud 6, that does not have permissions flag yet.
|
|
|
|
QString url = folder->remoteUrl().toString() + fileName;
|
|
|
|
if (url.contains(QLatin1String("/remote.php/webdav/Shared/"))) {
|
|
|
|
status.setSharedWithMe(true);
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
2014-10-22 17:05:37 +04:00
|
|
|
} else if (rec._remotePerm.contains("S")) {
|
|
|
|
status.setSharedWithMe(true);
|
2014-10-13 17:38:57 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 19:13:34 +04:00
|
|
|
if (status.tag() == SyncFileStatus::STATUS_NEW) {
|
|
|
|
// check the parent folder if it is shared and if it is allowed to create a file/dir within
|
|
|
|
QDir d( fi.path() );
|
|
|
|
auto parentPath = d.path();
|
|
|
|
auto dirRec = dbFileRecord_capi(folder, parentPath);
|
|
|
|
bool isDir = type == CSYNC_FTW_TYPE_DIR;
|
|
|
|
while( !d.isRoot() && !(d.exists() && dirRec.isValid()) ) {
|
|
|
|
d.cdUp(); // returns true if the dir exists.
|
2014-10-13 17:38:57 +04:00
|
|
|
|
2014-10-22 19:13:34 +04:00
|
|
|
parentPath = d.path();
|
|
|
|
// cut the folder path
|
|
|
|
dirRec = dbFileRecord_capi(folder, parentPath);
|
|
|
|
|
|
|
|
isDir = true;
|
|
|
|
}
|
|
|
|
if( dirRec.isValid() && !dirRec._remotePerm.isNull()) {
|
|
|
|
if( (isDir && !dirRec._remotePerm.contains("K"))
|
|
|
|
|| (!isDir && !dirRec._remotePerm.contains("C")) ) {
|
|
|
|
status.set(SyncFileStatus::STATUS_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 17:38:57 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-08-27 14:02:47 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|
2014-09-25 23:49:38 +04:00
|
|
|
|