Commit 9d211edd authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Refefactoring in progress #3

- Executor
parent 7c9be989
......@@ -43,12 +43,14 @@ FileSync::Executor::~Executor()
void FileSync::Executor::run()
{
LOG_I(LOG_EXECUTOR, QString("Sync starting"));
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);
connect(m_sftpMounter, &SftpMounter::sftpReady, this,
&Executor::resumeAfterWaitForSftp);
......@@ -56,62 +58,46 @@ void FileSync::Executor::run()
&Executor::abortDueToSftpError);
m_sftpMounter->run();
LOG_D(LOG_EXECUTOR, "Waiting for mounter ...");
m_sshfsReadyMutex.lock();
m_sshfsReady.wait(&m_sshfsReadyMutex);
m_sshfsReadyMutex.unlock();
if (m_errorOccured)
{
exit(1);
}
LOG_D(LOG_EXECUTOR, "Extracting sync entries");
QJsonArray toSync = Config::instance(m_deviceId).data();
// TODO fileter for only provided m_syncList
// TODO filter for only items that are provided in m_syncList
QJsonArray *fileSyncList = new QJsonArray();
for (const QJsonValue &value: toSync)
{
QJsonObject syncItem = value.toObject();
synchronizeEntry(fileSyncList, syncItem, true);
extractFileSyncEntries(fileSyncList, syncItem, true);
}
D(LOG_EXECUTOR, "File check completeed ");
LOG_D(LOG_EXECUTOR, "Preparing list of files for hash request");
QStringList hashRequestList;
for (const QJsonValue &value: toSync)
{
hashRequestList.append(value.toObject().value("remotePath").toString());
}
D(LOG_EXECUTOR, "Sending hash requestd ");
DeviceFilesyncDbusInterface *deviceFilesyncDbusInterface = new DeviceFilesyncDbusInterface(
m_deviceId, parent());
deviceFilesyncDbusInterface->requestHashes(hashRequestList);
D(LOG_EXECUTOR, "Thread waiting ");
// m_hashRequestWaitLoop.exec();
LOG_D(LOG_EXECUTOR, "Waiting for plugin (hash request) ...");
m_hashesReadyMutex.lock();
m_hashesReady.wait(&m_hashesReadyMutex);
m_hashesReadyMutex.unlock();
D(LOG_EXECUTOR, "Thread Continue ");
// This will continue only after wake up signal
for (const QJsonValue &hash: m_hashList)
{
LOG_I(LOG_EXECUTOR, hash.toObject().value("filePath").toString() + " " +
hash.toObject().value("hash").toString());
}
SyncResult syncResult(
performSync(*fileSyncList, m_hashList));
D(LOG_EXECUTOR, "Signal emitted ...");
Q_EMIT notifyDaemon(syncResult);
D(LOG_EXECUTOR, "Signal emitted.");
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()));
}
......@@ -119,8 +105,10 @@ void FileSync::Executor::run()
FileSync::SyncResult FileSync::Executor::performSync(
const QJsonArray &fileSyncList, const QJsonArray &fileHashList)
{
LOG_D(LOG_EXECUTOR, "Performing synchronization");
SyncResult syncResult(QString(), 0, 0, QStringList());
bool errorOccured = false;
for (const QJsonValue &value: fileSyncList)
{
QString localPath(value.toObject().value("localPath").toString());
......@@ -139,12 +127,14 @@ FileSync::SyncResult FileSync::Executor::performSync(
}
}
// Determine what sync operation we need to perform
SyncOperation operation = createSyncOperation(localPath, remotePath,
direction, remoteContentHash);
// Determine what sync operation we need to performee
SyncOperation operation(createSyncOperation(localPath, remotePath,
direction, remoteContentHash));
errorOccured = performSyncOperation(operation);
// Perform the syn operation
bool errorOccured = performSyncOperation(operation);
// If there was no error try to execute tye post install script
if (!errorOccured)
{
errorOccured = executePostSyncScript(operation, postSyncScript);
......@@ -203,9 +193,9 @@ void FileSync::Executor::hashesReady(const QJsonArray &hashList)
}
void
FileSync::Executor::synchronizeEntry(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative)
FileSync::Executor::extractFileSyncEntries(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative)
{
QString localPath = entry.value("localPath").toString();
QString remotePath = entry.value("remotePath").toString();
......@@ -262,7 +252,7 @@ FileSync::Executor::synchronizeEntry(QJsonArray *syncFileList,
for (QJsonObject dirEntry : dirEntries)
{
LOG_D(LOG_EXECUTOR, "Synchronizing nested entry" << dirEntry);
synchronizeEntry(syncFileList, dirEntry, false);
extractFileSyncEntries(syncFileList, dirEntry, false);
}
}
else
......@@ -377,7 +367,6 @@ bool FileSync::Executor::copyFile(QString sourceFilePath, QString destFilePath)
copyResult = false;
}
// TODO it seems that it is not working (not by using shell nor utime)
QString command = FileSync::MTIME_MOD_CMD.arg(
sourceFileInfo.lastModified().toString(
FileSync::MTIME_DATETIME_FORMAT), destFilePath);
......
......@@ -85,9 +85,9 @@ namespace FileSync
bool executePostSyncScript(SyncOperation &operation, QString &postSyncScript);
void synchronizeEntry(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative);
void extractFileSyncEntries(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative);
bool
performSyncOperation(const SyncOperation &operation);
......
......@@ -37,7 +37,7 @@ void FileSync::SftpMounter::run()
QEventLoop loop;
if (!isRemoteSystemRootAvailable(m_deviceId))
{
W(LOG_SFTP_MOUNTER, "Remote root not accessible, trying to re-mount");
LOG_W(LOG_SFTP_MOUNTER, "Remote root not accessible, trying to re-mount");
QDBusConnection::sessionBus().connect("org.kde.kdeconnect",
FileSync::SFTP_PLUGIN_DBUS_OBJ_PATH.arg(
m_deviceId),
......@@ -92,13 +92,13 @@ QString FileSync::SftpMounter::findMountFolderForDevice(const QString &deviceId)
if (mountTarget.contains(deviceId))
{
retValue = mountTarget;
I(LOG_SFTP_MOUNTER, "Found mount point for device "
LOG_I(LOG_SFTP_MOUNTER, "Found mount point for device "
<< deviceId << "(" << mountTarget
<< ")");
}
else
{
I(LOG_SFTP_MOUNTER, "Mount point for device "
LOG_I(LOG_SFTP_MOUNTER, "Mount point for device "
<< deviceId << " not found");
}
}
......@@ -121,7 +121,7 @@ bool FileSync::SftpMounter::waitForRemoteRoot(QString deviceId, int timeout)
{
if (remoteSystemRoot.exists())
{
I(LOG_SFTP_MOUNTER, "SSHFS mount ready and remote root"
LOG_I(LOG_SFTP_MOUNTER, "SSHFS mount ready and remote root"
<< remoteSystemRoot.absolutePath()
<< "accessible");
break;
......@@ -129,9 +129,9 @@ bool FileSync::SftpMounter::waitForRemoteRoot(QString deviceId, int timeout)
else
{
QThread::sleep(1);
I(LOG_SFTP_MOUNTER,
"Waiting for remote root" << remoteSystemRoot.absolutePath()
<< "to become available ...");
LOG_I(LOG_SFTP_MOUNTER,
"Waiting for remote root" << remoteSystemRoot.absolutePath()
<< "to become available ...");
}
}
}
......@@ -153,7 +153,7 @@ void FileSync::SftpMounter::mountOperationComplete()
{
QString error("Sync can not start, remote root unavailable!");
Q_EMIT sftpError(error);
E(LOG_SFTP_MOUNTER, error);
LOG_E(LOG_SFTP_MOUNTER, error);
}
}
......
......@@ -67,6 +67,18 @@ FileSync::SyncOperation::SyncOperation(
calcOperation();
}
FileSync::SyncOperation &
FileSync::SyncOperation::operator=(const FileSync::SyncOperation &other)
{
m_direction = other.m_direction;
m_indexKey = other.m_indexKey;
m_indexValue = other.m_indexValue;
m_localPath = other.m_localPath;
m_remotePath = other.m_remotePath;
m_remoteContentHash = other.m_remoteContentHash;
return *this;
}
FileSync::SyncOperation::SyncOperationType
FileSync::SyncOperation::SyncOperation::type() const
{
......
......@@ -53,6 +53,8 @@ namespace FileSync
const QString &indexValue, const QString &localPath,
const QString &remotePath, const QString &remoteContentHash);
SyncOperation &operator=(const SyncOperation &other);
SyncOperationType type() const;
QString localPath() const;
......
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