Commit 7c9be989 authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Refefactoring in progress #2

- Sync operation
parent fc6e5457
......@@ -87,6 +87,8 @@ namespace FileSync
static const QString UTIL_NOT_A_DIRECTORY = "Can not scan, path: %1; not a directory!";
static const QString DUMMY_MESSAGE = "DUMMY_MESSAGE"; // TODO redo to normal
class Util
{
public:
......
This diff is collapsed.
......@@ -55,8 +55,8 @@ namespace FileSync
bool m_errorOccured;
QJsonArray m_hashList;
SyncResult determineAndPerformSyncOperation(const QJsonArray &fileSyncList,
const QJsonArray &fileHashList);
SyncResult performSync(const QJsonArray &fileSyncList,
const QJsonArray &fileHashList);
public:
Executor(const QString &deviceId, const QStringList &syncList);
......@@ -83,29 +83,24 @@ namespace FileSync
const QString &remotePath,
const QString &postSyncScript);
bool executePostSyncScript(QString postSyncScript,
int operationType,
QString localFilePath,
QString remoteFilePath);
bool executePostSyncScript(SyncOperation &operation, QString &postSyncScript);
void synchronizeEntry(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative);
bool
performSyncOperation(const SyncOperation &operation,
const QString &localFilePath,
const QString &remoteFilePath,
const QString &postSyncScript);
performSyncOperation(const SyncOperation &operation);
bool
copyFile(QString sourceFilePath, QString destFilePath);
SyncOperation
calculateSyncOperation(
createSyncOperation(
const QString &localPath,
const QString &remotePath,
const QString &direction);
const SyncOperation::SyncDirection &direction,
const QString &remoteContentHash);
Q_SIGNALS:
......
......@@ -18,38 +18,188 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QFileInfo>
#include "sync_operation.h"
#include "log.h"
#include "common.h"
Q_LOGGING_CATEGORY(LOG_SYNC_OPERATION,
"kdeconnect.filesyncapp.daemon.sync_operation");
FileSync::SyncOperation::SyncOperation() : m_type(
FileSync::SyncOperation::UNKNOWN)
{
}
FileSync::SyncOperation::SyncOperation(const SyncOperation &other) : m_type(
FileSync::SyncOperation::UNKNOWN),
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)
{
calcOperation();
}
FileSync::SyncOperation::SyncOperation(
const SyncDirection &direction,
const QString &indexKey,
const QString &indexValue,
const QString &localPath,
const QString &remotePath,
const QString &remoteContentHash) : m_type(
FileSync::SyncOperation::UNKNOWN),
m_direction(direction),
m_indexKey(indexKey),
m_indexValue(indexValue),
m_localPath(localPath),
m_remotePath(remotePath),
m_remoteContentHash(remoteContentHash)
{
calcOperation();
}
FileSync::SyncOperation::SyncOperationType
FileSync::SyncOperation::SyncOperation::getType() const
FileSync::SyncOperation::SyncOperation::type() const
{
return m_type;
}
void
FileSync::SyncOperation::setType(SyncOperation::SyncOperationType newType)
QString FileSync::SyncOperation::localPath() const
{
m_type = newType;
return m_localPath;
}
QPair<QString, QString> FileSync::SyncOperation::getHashRecord() const
QString FileSync::SyncOperation::remotePath() const
{
return QPair<QString, QString>(m_indexRecordKey, m_indexRecordValue);
return m_remotePath;
}
void FileSync::SyncOperation::updateRecord(const QString &newIndexRecordKey,
const QString &newIndexRecordValue)
QPair<QString, QString> FileSync::SyncOperation::indexRecord() const
{
m_indexRecordKey = newIndexRecordKey;
m_indexRecordValue = newIndexRecordValue;
return QPair<QString, QString>(m_indexKey, m_indexValue);
}
FileSync::SyncOperation::SyncOperation(const FileSync::SyncOperation &other)
: m_type(other.getType())
void FileSync::SyncOperation::calcFromMTime(
const FileSync::SyncOperation::SyncDirection &direction,
const qint64 localMTime, const qint64 remoteMTime,
const QString &currentIndexKey, const QString &localContentHash,
const QString &remoteContentHash)
{
if (localMTime < remoteMTime)
{
m_type = SyncOperation::FROM_REMOTE;
m_indexValue = remoteContentHash;
}
else if (localMTime > remoteMTime)
{
m_type = SyncOperation::FROM_LOCAL;
m_indexValue = localContentHash;
}
else if (localMTime == remoteMTime)
{
m_type = SyncOperation::NONE;
// Here would not matter which content hash is used
m_indexValue = localContentHash;
}
}
void FileSync::SyncOperation::calcFromContentHash(
const FileSync::SyncOperation::SyncDirection &direction,
const QString &currentIndexKey, const QString &currentIndexValue,
const QString &localContentHash, const QString &remoteContentHash)
{
if (remoteContentHash == localContentHash)
{
m_type = SyncOperation::NONE;
}
else if ((currentIndexValue == remoteContentHash) &&
(currentIndexValue != localContentHash))
{
m_type = SyncOperation::FROM_LOCAL;
m_indexValue = localContentHash;
}
else if ((currentIndexValue != remoteContentHash) &&
(currentIndexValue == localContentHash))
{
m_type = SyncOperation::FROM_REMOTE;
m_indexValue = remoteContentHash;
}
else if ((currentIndexValue != remoteContentHash) &&
(currentIndexValue != localContentHash))
{
m_type = SyncOperation::CONFLICT;
}
}
void FileSync::SyncOperation::calcOperation()
{
m_localContentHash = Util::convertToHash(
Util::getFileContent(m_localPath));
QFileInfo localPathInfo = QFileInfo(m_localPath);
QFileInfo remotePathInfo = QFileInfo(m_remotePath);
if (!localPathInfo.exists() && !remotePathInfo.exists())
{
// Nothing exists, no operation
LOG_W(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath, m_remotePath));
}
else if (!localPathInfo.exists() && remotePathInfo.exists() &&
((m_direction == SyncOperation::ONLY_DOWNLOAD ||
m_direction == SyncOperation::TWO_WAY)))
{
// Remote path exist only - operation will be FROM_REMOTE
LOG_I(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath, m_remotePath));
m_type = SyncOperation::FROM_REMOTE;
m_indexValue = m_remoteContentHash;
}
else if (localPathInfo.exists() && !remotePathInfo.exists() &&
((m_direction == SyncOperation::ONLY_UPLOAD ||
m_direction == SyncOperation::TWO_WAY)))
{
// Local path exist only - operation will be FROM_LOCAL
LOG_I(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath, m_remotePath));
m_type = SyncOperation::FROM_LOCAL;
m_indexValue = m_localContentHash;
}
else if (localPathInfo.exists() && remotePathInfo.exists())
{
// Both files / directories exist, let's check whether this is the first
// time the local-remote pair occurs and if so then use mtime comparison
// else compare content hashes
LOG_D(LOG_SYNC_OPERATION,
FileSync::DUMMY_MESSAGE.arg(m_localPath, m_remotePath));
if (m_indexValue == "")
{
LOG_W(LOG_SYNC_OPERATION,
FileSync::SYNC_DETERMINATION_BY_MTIME.arg(m_localPath,
m_remotePath));
calcFromMTime(m_direction,
localPathInfo.lastModified().toMSecsSinceEpoch(),
remotePathInfo.lastModified().toMSecsSinceEpoch(),
m_indexKey, m_localContentHash, m_remoteContentHash);
}
else
{
LOG_I(LOG_SYNC_OPERATION,
FileSync::SYNC_DETERMINATION_BY_MTIME.arg(m_localPath,
m_remotePath));
calcFromContentHash(m_direction,
m_indexKey, m_indexValue, m_localContentHash,
m_remoteContentHash);
}
}
}
#include "sync_operation.moc"
......@@ -23,69 +23,69 @@
#include <QtCore/QString>
#include <QtCore/QPair>
#include <QObject>
namespace FileSync
{
class SyncOperation
class SyncOperation : public QObject
{
Q_OBJECT
public:
enum SyncDirection
{
ONLY_DOWNLOAD = 0, ONLY_UPLOAD, TWO_WAY
};
Q_ENUM(SyncDirection);
enum SyncOperationType
{
UNKNOWN = 0, NONE, CONFLICT, FROM_REMOTE, FROM_LOCAL
};
SyncOperation();
Q_ENUM(SyncOperationType);
SyncOperation(const SyncOperationType &type, const QString &indexRecordKey,
const QString &indexRecordValue) : m_type(type),
m_indexRecordKey(
indexRecordKey),
m_indexRecordValue(
indexRecordValue)
{
}
SyncOperation();
SyncOperation(const SyncOperation &other);
SyncOperationType getType() const;
SyncOperation(const SyncDirection &direction, const QString &indexKey,
const QString &indexValue, const QString &localPath,
const QString &remotePath, const QString &remoteContentHash);
QPair<QString, QString> getHashRecord() const;
SyncOperationType type() const;
void setType(SyncOperationType newType);
QString localPath() const;
static SyncOperation calculateFromTimestamp(const qint64 localMTime, const qint64 remoteMTime,
const QString &indexRecordKey,
const QString &localHash, const QString &remoteHash)
{
SyncOperationType type = SyncOperation::UNKNOWN;
QString m_indexRecordKey = ;
if (localMTime ==
remoteMTime)
{
type = SyncOperation::NONE;
}
else if (localMTime >=
remoteMTime)
{
type = SyncOperation::FROM_LOCAL;
updateRecord(indexRecordKey,
localHash);
}
else
{
type = SyncOperation::FROM_REMOTE;
updateRecord(indexRecordKey,
remoteHash);
}
}
QString remotePath() const;
QPair<QString, QString> indexRecord() const;
private:
SyncOperationType m_type;
QString m_indexRecordKey;
QString m_indexRecordValue;
SyncDirection m_direction;
QString m_indexKey;
QString m_indexValue;
QString m_localPath;
QString m_remotePath;
QString m_localContentHash;
QString m_remoteContentHash;
void calcOperation();
void
calcFromMTime(const SyncOperation::SyncDirection &direction,
const qint64 localMTime, const qint64 remoteMTime,
const QString &currentIndexKey,
const QString &localContentHash,
const QString &remoteContentHash);
void updateRecord(const QString &newIndexRecordKey,
const QString &newIndexRecordValue);
void
calcFromContentHash(const SyncOperation::SyncDirection &direction,
const QString &currentIndexKey,
const QString &currentIndexValue,
const QString &localContentHash,
const QString &remoteContentHash);
};
}
......
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