Commit e848cd23 authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Refefactoring in progress #6

- Improved mount detection
parent 53de5762
......@@ -23,3 +23,6 @@ target_link_libraries(kdeconnect-filesync-daemon
)
install(TARGETS kdeconnect-filesync-daemon ${INSTALL_TARGETS_DEFAULT_ARGS})
# Add tests
#add_subdirectory(tests/index_test)
\ No newline at end of file
......@@ -33,7 +33,7 @@ namespace FileSync
static const QString SFTP_PLUGIN_DBUS_OBJ_PATH = "/modules/kdeconnect/devices/%1/sftp";
static const QString SYNC_PLUGIN_DBUS_OBJ_PATH = "/modules/kdeconnect/devices/%1/sync";
// TODO dependency on findmnt??? maybe it will require RPM dependency update / separate update?
static const QString SSHFS_FIND_MOUNT_CMD = "findmnt -t fuse.sshfs --json";
static const QString SSHFS_FIND_MOUNT_CMD = "cat /proc/mounts";
// TODO phone path changing?
static const QString SSHFS_MOUNT_ROOT = "%1/storage/emulated/0/%2";
static const QString MTIME_MOD_CMD = "touch -d \"%1\" \"%2\"";
......@@ -75,6 +75,7 @@ namespace FileSync
static const QString INDEX_READ_OPEN_ERROR = "Could not open sync index file (%1) for reading, error: %2";
static const QString INDEX_WRITE_OPEN_ERROR = "Could not open sync index file (%1) for writing, error: %2";
static const QString INDEX_ALREADY_EXISTS = "Sync index for the device %1 already exists, doing nothing";
static const QString MOUNT_POINT_NOT_FOUND = "Mount point for device %1 not found, error: %2";
// Sync operation messages
static const QString SYNC_OPERATION_CONFLICT = "Conflict in versions (%1) and (%2), both versions have changed since the last sync!";
......@@ -89,6 +90,9 @@ namespace FileSync
static const QString DUMMY_MESSAGE = "DUMMY_MESSAGE"; // TODO redo to normal
// Normal log messages
static const QString MOUNT_POINT_FOUND = "Found mount point for device %1 at %2";
class Util
{
public:
......
......@@ -57,9 +57,10 @@ void FileSync::Executor::run()
&Executor::resumeAfterWaitForSftp);
connect(m_sftpMounter, &SftpMounter::sftpError, this,
&Executor::abortDueToSftpError);
LOG_D(LOG_EXECUTOR, "Waiting for mounter (sftp) ...");
m_sftpMounter->run();
LOG_D(LOG_EXECUTOR, "Waiting for mounter ...");
m_sftpReadyMutex.lock();
m_sftpReady.wait(&m_sftpReadyMutex);
m_sftpReadyMutex.unlock();
......
......@@ -21,7 +21,7 @@
#include <log.h>
#include <sftp_mounter.h>
Q_LOGGING_CATEGORY(LOG_SFTP_MOUNTER, "kdeconnect.filesyncapp.daemon.sftp");
Q_LOGGING_CATEGORY(LOG_SFTP_MOUNTER, "kdeconnect.filesyncapp.daemon.sftpMounter");
FileSync::SftpMounter::SftpMounter(const QString &deviceId) : m_deviceId(
deviceId)
......@@ -83,27 +83,24 @@ QString FileSync::SftpMounter::findMountFolderForDevice(const QString &deviceId)
(stdout.trimmed().length() > 0) &&
(stderr.trimmed().length() == 0))
{
QJsonDocument findMntJson = QJsonDocument::fromJson(stdout.toUtf8());
QJsonArray fountMounts = findMntJson.object().value(
"filesystems").toArray();
for (QJsonValueRef mount : fountMounts)
QStringList mounts = stdout.trimmed().split("\n");
for (QString mount : mounts)
{
QString mountTarget = mount.toObject().value("target").toString();
if (mountTarget.contains(deviceId))
if (mount.contains(deviceId) && mount.contains("fuse.sshfs"))
{
retValue = mountTarget;
LOG_I(LOG_SFTP_MOUNTER, "Found mount point for device "
<< deviceId << "(" << mountTarget
<< ")");
}
else
{
LOG_I(LOG_SFTP_MOUNTER, "Mount point for device "
<< deviceId << " not found");
retValue = mount.split(" ")[1];
LOG_I(LOG_SFTP_MOUNTER,
MOUNT_POINT_FOUND.arg(deviceId, retValue));
}
}
}
if (retValue == "")
{
LOG_W(LOG_SFTP_MOUNTER,
MOUNT_POINT_NOT_FOUND.arg(deviceId, stderr.trimmed()));
}
return retValue;
}
......
......@@ -21,8 +21,8 @@
#ifndef FILESYNC_SINGLETON_POOL_H
#define FILESYNC_SINGLETON_POOL_H
#import <QString>
#import <QMap>
#include <QString>
#include <QMap>
namespace FileSync
{
......
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS Test)
include_directories(
${KDEConnectCore_BINARY_DIR}
${CMAKE_SOURCE_DIR}
../../
)
set(kdeconnect_libraries
kdeconnectcore
KF5::I18n
KF5::KIOWidgets
Qt5::DBus
Qt5::Network
Qt5::Test
qca-qt5
pthread
)
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/index_test)
file(COPY test_resources DESTINATION ${CMAKE_BINARY_DIR}/index_test)
ecm_add_test(
../../common.cpp
../../sync_operation.cpp
../../log.cpp
../../index.cpp
index_test.cpp LINK_LIBRARIES ${kdeconnect_libraries} TEST_NAME index_test)
/**
* Copyright 2019 Viktor Porvaznik <viktor.porvaznik@gmail.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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QTest>
#include "index.h"
#include "common.h"
namespace FileSync
{
class IndexTest : public QObject
{
Q_OBJECT
private:
Index *m_index;
QString m_testDeviceId;
private Q_SLOTS:
void initIndex()
{
m_testDeviceId = "testDeviceId";
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/sync_db.json";
m_index = &Index::instance(m_testDeviceId);
QVERIFY(m_index != NULL);
QVERIFY(QFileInfo(testIndexPath).exists());
}
void testGetAndDestroyInstance0()
{
Index::destroyInstance(m_testDeviceId);
QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
}
void testGetAndDestroyInstance1ExistingIndex()
{
QString testDeviceId("testDeviceId");
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/testGetAndDestroyInstance1ExistingIndex/sync_db.json";
Index *indexA = &Index::instance(testDeviceId, testIndexPath);
Index *indexB = &Index::instance();
QVERIFY(indexA == indexB);
QVERIFY(QFileInfo(testIndexPath).exists());
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
}
void testGetAndDestroyInstance1NotExistingIndex()
{
QString testDeviceId("testDeviceId");
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/sync_db_that_does_not_exist.json";
Index *indexA = &Index::instance(testDeviceId, testIndexPath);
Index *indexB = &Index::instance();
QVERIFY(indexA == indexB);
QVERIFY(QFileInfo(testIndexPath).exists());
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
}
void testGetAll()
{
initIndex();
QMap<QString, QString> indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData.size() == 1);
destroyIndex();
}
void testSearch()
{
initIndex();
QString search = m_index->search("file_key1");
QVERIFY(search == "file_value1");
destroyIndex();
}
void testUpdateAndPersistency()
{
initIndex();
m_index->update(QPair<QString, QString>("new_key", "new_value"));
QMap<QString, QString> indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData["new_key"] == "new_value");
QVERIFY(indexAData.size() == 2);
destroyIndex();
initIndex();
indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData["new_key"] == "new_value");
QVERIFY(indexAData.size() == 2);
destroyIndex();
}
void destroyIndex()
{
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
m_index = NULL;
}
};
}
QTEST_MAIN(FileSync::IndexTest)
#include "index_test.moc"
{
"data": {
"file_key1": "file_value1"
},
"lastUpdateTime": 0
}
......@@ -30,9 +30,3 @@ ecm_add_test(testnotificationlistener.cpp
../plugins/sendnotifications/notifyingapplication.cpp
TEST_NAME testnotificationlistener
LINK_LIBRARIES ${kdeconnect_libraries} Qt5::DBus KF5::Notifications KF5::IconThemes)
# TODO temporarily disabled
#add_subdirectory(plugins/sync/common_test)
#add_subdirectory(plugins/sync/sync_operation_test)
#add_subdirectory(plugins/sync/sync_index_test)
#add_subdirectory(plugins/sync/sync_executor_test)
\ No newline at end of file
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