Commit 53de5762 authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Refefactoring in progress #5

parent 1575b19a
......@@ -28,7 +28,6 @@ Q_LOGGING_CATEGORY(LOG_CONFIG, "kdeconnect.filesyncapp.daemon.config");
FileSync::Config::Config(const QString &deviceId)
{
Log::instance(deviceId);
m_deviceId = deviceId;
m_confiFilePath = CONFIG_FILE_PATH.arg(
QStandardPaths::writableLocation(QStandardPaths::HomeLocation), deviceId);
......
......@@ -40,13 +40,13 @@ void FileSync::Daemon::requestSync(const QString &deviceId,
QMap<QString, Executor *>::iterator existingExecutor = m_executors.find(
deviceId);
if (existingExecutor != m_executors.end() &&
!existingExecutor.value()->isRunning())
existingExecutor.value()->isExecuting())
{
W(LOG_DAEMON, "An ongoing sync is progress, discarding this sync request");
syncOngoing = true;
}
else if (existingExecutor != m_executors.end() &&
existingExecutor.value()->isRunning())
!existingExecutor.value()->isExecuting())
{
disconnect(existingExecutor.value(),
&FileSync::Executor::notifyDaemon, this,
......
......@@ -30,7 +30,8 @@ FileSync::Executor::Executor(const QString &deviceId,
: m_deviceId(deviceId), m_syncList(syncList),
m_executionResult(QString(), 0, 0, QStringList())
{
Log::instance(m_deviceId);
m_syncId = QDateTime::currentMSecsSinceEpoch();
Log::instance(m_deviceId, m_syncId);
Config::instance(m_deviceId);
Index::instance(m_deviceId);
}
......@@ -43,12 +44,12 @@ FileSync::Executor::~Executor()
void FileSync::Executor::run()
{
LOG_I(LOG_EXECUTOR, QString("Sync starting"));
m_executingMutex.lock();
LOG_D(LOG_EXECUTOR, m_syncId << "Starting the executor");
m_executing = true;
m_executingMutex.unlock();
m_runningMutex.lock();
LOG_D(LOG_EXECUTOR, "Starting the executor");
m_running = true;
m_runningMutex.unlock();
LOG_I(LOG_EXECUTOR, QString("Sync starting"));
LOG_D(LOG_EXECUTOR, "Trying to mount remote device trough SFTP");
m_sftpMounter = new SftpMounter(m_deviceId);
......@@ -59,9 +60,9 @@ void FileSync::Executor::run()
m_sftpMounter->run();
LOG_D(LOG_EXECUTOR, "Waiting for mounter ...");
m_sshfsReadyMutex.lock();
m_sshfsReady.wait(&m_sshfsReadyMutex);
m_sshfsReadyMutex.unlock();
m_sftpReadyMutex.lock();
m_sftpReady.wait(&m_sftpReadyMutex);
m_sftpReadyMutex.unlock();
LOG_D(LOG_EXECUTOR, "Extracting sync entries");
QJsonArray toSync = Config::instance(m_deviceId).data();
......@@ -88,18 +89,19 @@ void FileSync::Executor::run()
m_hashesReady.wait(&m_hashesReadyMutex);
m_hashesReadyMutex.unlock();
SyncResult syncResult = performSync(*fileSyncList, m_hashList);
SyncResult syncResult(performSync(*fileSyncList, m_hashList));
m_runningMutex.lock();
LOG_D(LOG_EXECUTOR, "Stopping the executor");
m_running = false;
m_runningMutex.unlock();
LOG_D(LOG_EXECUTOR, "Sync concluded, notifying daemon");
Q_EMIT notifyDaemon(syncResult);
QString syncCompletedMsg("Sync %1!");
LOG_I(LOG_EXECUTOR, syncCompletedMsg.arg(m_executionResult.syncStatus()));
m_executingMutex.lock();
LOG_D(LOG_EXECUTOR, "Stopping the executor");
m_executing = false;
m_executingMutex.unlock();
}
FileSync::SyncResult FileSync::Executor::performSync(
......@@ -128,8 +130,8 @@ FileSync::SyncResult FileSync::Executor::performSync(
}
// Determine what sync operation we need to performee
SyncOperation operation(createSyncOperation(localPath, remotePath,
direction, remoteContentHash));
SyncOperation operation = createSyncOperation(localPath, remotePath,
direction, remoteContentHash);
// Perform the syn operation
bool errorOccured = performSyncOperation(operation);
......@@ -153,12 +155,10 @@ FileSync::SyncResult FileSync::Executor::performSync(
bool FileSync::Executor::isExecuting()
{
// m_runningMutex.lock();
bool running = m_running;
LOG_D(LOG_EXECUTOR, "Running");
LOG_D(LOG_EXECUTOR, QString(running));
// m_runningMutex.unlock();
return running;
m_executingMutex.lock();
bool executing = m_executing;
m_executingMutex.unlock();
return executing;
}
void FileSync::Executor::abortDueToSftpError(const QString &error)
......@@ -166,9 +166,9 @@ void FileSync::Executor::abortDueToSftpError(const QString &error)
LOG_E(LOG_EXECUTOR, "Sftp failed, aborting executor!");
delete m_sftpMounter;
m_sftpMounter = nullptr;
m_sshfsReadyMutex.lock();
m_sshfsReady.wakeAll();
m_sshfsReadyMutex.unlock();
m_sftpReadyMutex.lock();
m_sftpReady.wakeAll();
m_sftpReadyMutex.unlock();
}
void FileSync::Executor::resumeAfterWaitForSftp(
......@@ -177,10 +177,10 @@ void FileSync::Executor::resumeAfterWaitForSftp(
m_remoteSystemMountPoint = remoteSystemMountPoint;
delete m_sftpMounter;
m_sftpMounter = nullptr;
m_sshfsReadyMutex.lock();
m_sshfsReady.wakeAll();
m_sshfsReadyMutex.unlock();
LOG_D(LOG_EXECUTOR, "Sftp ready, waking up executor thread");
m_sftpReadyMutex.lock();
m_sftpReady.wakeAll();
m_sftpReadyMutex.unlock();
}
void FileSync::Executor::hashesReady(const QJsonArray &hashList)
......@@ -229,11 +229,9 @@ FileSync::Executor::extractFileSyncEntries(QJsonArray *syncFileList,
root.mkpath(remotePathInfo.dir().absolutePath());
}
}
LOG_D(LOG_EXECUTOR, "Pass" << remotePath);
if (localPathInfo.exists() && remotePathInfo.exists())
{
LOG_D(LOG_EXECUTOR, "Pass1" << remotePath);
if (localPathInfo.isDir() && !remotePathInfo.isDir())
{
LOG_W(LOG_EXECUTOR, FileSync::INCONSISTENT_PATHS_ERROR.arg(localPath,
......@@ -282,7 +280,6 @@ FileSync::Executor::extractFileSyncEntries(QJsonArray *syncFileList,
{
LOG_W(LOG_EXECUTOR,
FileSync::WRONG_PATHS_ERROR.arg(localPath, remotePath));
// errorOccured = true;
}
}
......
......@@ -41,14 +41,15 @@ namespace FileSync
{
Q_OBJECT
private:
qint64 m_syncId;
QString m_deviceId;
QStringList m_syncList;
QWaitCondition m_hashesReady;
QMutex m_hashesReadyMutex;
QWaitCondition m_sshfsReady;
QMutex m_sshfsReadyMutex;
bool m_running;
QMutex m_runningMutex;
QWaitCondition m_sftpReady;
QMutex m_sftpReadyMutex;
bool m_executing;
QMutex m_executingMutex;
SyncResult m_executionResult;
QString m_remoteSystemMountPoint;
SftpMounter *m_sftpMounter;
......
......@@ -19,17 +19,15 @@
*/
#include <QtCore/QFile>
#include <QtCore/QException>
#include "log.h"
#include "common.h"
FileSync::Log::Log(const QString &deviceId,
FileSync::Log::Log(const QString &deviceId, const qint64 &syncId,
FileSync::Log::LogLevel publicLogLevel)
{
m_publicLogLevel = publicLogLevel;
m_logFilePath = LOG_FILE_PATH.arg(deviceId, QString::number(
QDateTime::currentMSecsSinceEpoch()));
m_logFilePath = LOG_FILE_PATH.arg(deviceId, QString::number(syncId));
m_logFile = new QFile(m_logFilePath);
if (m_logFile->open(QIODevice::ReadWrite))
{
......
......@@ -42,7 +42,8 @@ namespace FileSync
DEBUG, INFO, WARN, ERROR
};
Log(const QString &deviceId, LogLevel publicLogLevel = LogLevel::INFO);
Log(const QString &deviceId, const qint64 &syncId,
LogLevel publicLogLevel = LogLevel::INFO);
~Log();
......@@ -64,7 +65,8 @@ namespace FileSync
static void throwError(const QString message)
{
throw std::runtime_error(message.toLocal8Bit().data()); \
}
}
private:
void logMessage(const QString &message, LogLevel level);
......@@ -80,7 +82,6 @@ namespace FileSync
return QString(message) \
}
#define D(logCategory, message) { \
qCDebug(logCategory) << message; \
}
......
......@@ -141,7 +141,7 @@ bool FileSync::SftpMounter::waitForRemoteRoot(QString deviceId, int timeout)
void FileSync::SftpMounter::mountOperationComplete()
{
I(LOG_SFTP_MOUNTER, "Device SSHFS mount completed");
LOG_I(LOG_SFTP_MOUNTER, "Device SSHFS mount completed");
// Wait till remote root becomes accessible (or not)
if (waitForRemoteRoot(m_deviceId, FileSync::REMOTE_ROOT_TIMEOUT))
......
......@@ -59,6 +59,31 @@ namespace FileSync
return *instance;
}
static T &instance(const QString &instanceId, const int &arg1)
{
T *instance = NULL;
if (s_instances == NULL)
{
s_instances = new QMap<QString, T *>();
instance = new T(instanceId, arg1);
s_instances->insert(instanceId, instance);
}
else
{
auto it = s_instances->find(instanceId);
if (it != s_instances->end())
{
instance = it.value();
}
else
{
instance = new T(instanceId, arg1);
s_instances->insert(instanceId, instance);
}
}
return *instance;
}
static void destroyInstance(const QString &instanceId)
{
auto it = s_instances->find(instanceId);
......
......@@ -108,13 +108,13 @@ void FileSync::SyncOperation::calcFromMTime(
{
if (localMTime < remoteMTime)
{
m_type = SyncOperation::FROM_REMOTE;
m_indexValue = remoteContentHash;
setTypeAndIndexValueIfAllowed(direction, SyncOperation::FROM_REMOTE,
remoteContentHash);
}
else if (localMTime > remoteMTime)
{
m_type = SyncOperation::FROM_LOCAL;
m_indexValue = localContentHash;
setTypeAndIndexValueIfAllowed(direction, SyncOperation::FROM_LOCAL,
localContentHash);
}
else if (localMTime == remoteMTime)
{
......@@ -136,14 +136,14 @@ void FileSync::SyncOperation::calcFromContentHash(
else if ((currentIndexValue == remoteContentHash) &&
(currentIndexValue != localContentHash))
{
m_type = SyncOperation::FROM_LOCAL;
m_indexValue = localContentHash;
setTypeAndIndexValueIfAllowed(direction, SyncOperation::FROM_LOCAL,
localContentHash);
}
else if ((currentIndexValue != remoteContentHash) &&
(currentIndexValue == localContentHash))
{
m_type = SyncOperation::FROM_REMOTE;
m_indexValue = remoteContentHash;
setTypeAndIndexValueIfAllowed(direction, SyncOperation::FROM_REMOTE,
remoteContentHash);
}
else if ((currentIndexValue != remoteContentHash) &&
(currentIndexValue != localContentHash))
......@@ -214,4 +214,34 @@ void FileSync::SyncOperation::calcOperation()
}
}
void FileSync::SyncOperation::setTypeAndIndexValueIfAllowed(
const FileSync::SyncOperation::SyncDirection &allowedDirection,
const FileSync::SyncOperation::SyncOperationType &desiredOperationType,
const QString &desiredIndexValue)
{
bool allowed = true;
if ((desiredOperationType == FileSync::SyncOperation::FROM_REMOTE) &&
(allowedDirection == FileSync::SyncOperation::ONLY_UPLOAD))
{
allowed = false;
LOG_D(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath,
m_remotePath)); // TODO direction not allowed
}
else if ((desiredOperationType == FileSync::SyncOperation::FROM_LOCAL) &&
(allowedDirection == FileSync::SyncOperation::ONLY_DOWNLOAD))
{
allowed = false;
LOG_D(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath,
m_remotePath)); // TODO direction not allowed
}
if (allowed)
{
m_type = desiredOperationType;
m_indexValue = desiredIndexValue;
}
}
#include "sync_operation.moc"
......@@ -88,6 +88,11 @@ namespace FileSync
const QString &currentIndexValue,
const QString &localContentHash,
const QString &remoteContentHash);
void setTypeAndIndexValueIfAllowed(
const FileSync::SyncOperation::SyncDirection &allowedDirection,
const FileSync::SyncOperation::SyncOperationType &desiredOperationType,
const QString &desiredIndexValue);
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment