Page MenuHomePhorge

D189.1729333448.diff
No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None

D189.1729333448.diff

diff --git a/packaging/GNU-Linux/appimage/build-unprivileged.sh b/packaging/GNU-Linux/appimage/build-unprivileged.sh
new file mode 100755
--- /dev/null
+++ b/packaging/GNU-Linux/appimage/build-unprivileged.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+export XDG_RUNTIME_DIR=/run/user/test
+
+cmake .. -DCMAKE_INSTALL_PREFIX="$KAZV_INSTALL_DIR" \
+ -DCMAKE_PREFIX_PATH="$LIBKAZV_INSTALL_DIR;$DEPS_INSTALL_DIR" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -Dkazv_KF_QT_MAJOR_VERSION=$KF_VER -Dkazv_LINK_BREEZE_ICONS=ON && \
+ make -j$JOBS && \
+ make -j$JOBS DESTDIR=AppDir install && \
+ dbus-launch --exit-with-session -- bash "$thisDir"/test.sh
diff --git a/packaging/GNU-Linux/appimage/build.sh b/packaging/GNU-Linux/appimage/build.sh
--- a/packaging/GNU-Linux/appimage/build.sh
+++ b/packaging/GNU-Linux/appimage/build.sh
@@ -3,6 +3,8 @@
set -x
set -e
+export thisDir="$(readlink -f "$(dirname "$(realpath "$0")")")"
+
# Collect deps
wget https://raw.githubusercontent.com/KDE/docker-neon/Neon/stable/public.key && \
apt-key add public.key && \
@@ -60,26 +62,23 @@
apt-get -y install aptitude && \
aptitude -y install "${DEP_PACKAGES[@]}"
-# Actual build
-mkdir -pv /run/user/test
-export XDG_RUNTIME_DIR=/run/user/test
-
-thisDir="$(readlink -f "$(dirname "$(realpath "$0")")")"
-
-BUILD_TYPE="$1"
+export BUILD_TYPE="$1"
if [[ ! "$BUILD_TYPE" ]]; then
export BUILD_TYPE=Debug
fi
+
+useradd -U builder
+mkdir build
+chown builder:builder build
+cd build
+
export JOBS=${JOBS:-$(( $(nproc) / 2))} DEPS_INSTALL_DIR=/opt/libkazv-deps LIBKAZV_INSTALL_DIR=/opt/libkazv KAZV_INSTALL_DIR=/usr
export LD_LIBRARY_PATH="$LIBKAZV_INSTALL_DIR/lib:$LIBKAZV_INSTALL_DIR/lib/x86_64-linux-gnu:$DEPS_INSTALL_DIR/lib:$DEPS_INSTALL_DIR/lib/x86_64-linux-gnu"
-mkdir build && cd build && \
- cmake .. -DCMAKE_INSTALL_PREFIX="$KAZV_INSTALL_DIR" \
- -DCMAKE_PREFIX_PATH="$LIBKAZV_INSTALL_DIR;$DEPS_INSTALL_DIR" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -Dkazv_KF_QT_MAJOR_VERSION=$KF_VER -Dkazv_LINK_BREEZE_ICONS=ON && \
- make -j$JOBS && \
- make -j$JOBS DESTDIR=AppDir install && \
- dbus-launch --exit-with-session -- bash "$thisDir"/test.sh
+mkdir -pv /run/user/test
+chown builder:builder /run/user/test
+su builder -c "$thisDir"/build-unprivileged.sh || exit 1
wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
wget https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage
diff --git a/src/matrix-sdk.hpp b/src/matrix-sdk.hpp
--- a/src/matrix-sdk.hpp
+++ b/src/matrix-sdk.hpp
@@ -12,6 +12,7 @@
#include <QString>
#include <memory>
+#include <filesystem>
#include <lager/extra/qt.hpp>
@@ -38,6 +39,8 @@
struct MatrixSdkPrivate;
+std::filesystem::path sessionDirForUserAndDeviceId(std::filesystem::path userDataDir, std::string userId, std::string deviceId);
+
class MatrixSdk : public QObject
{
Q_OBJECT
diff --git a/src/matrix-sdk.cpp b/src/matrix-sdk.cpp
--- a/src/matrix-sdk.cpp
+++ b/src/matrix-sdk.cpp
@@ -80,6 +80,14 @@
void resume() { throw std::runtime_error{"not implemented!"}; }
};
+std::filesystem::path sessionDirForUserAndDeviceId(std::filesystem::path userDataDir, std::string userId, std::string deviceId)
+{
+ auto encodedUserId = encodeBase64(userId, Base64Opts::urlSafe);
+ auto sessionDir = userDataDir / "sessions"
+ / encodedUserId / deviceId;
+ return sessionDir;
+}
+
struct MatrixSdkPrivate
{
MatrixSdkPrivate(MatrixSdk *q, bool testing, std::unique_ptr<KazvSessionLockGuard> lockGuard);
@@ -169,11 +177,10 @@
using StdPath = std::filesystem::path;
auto userDataDir = StdPath(this->userDataDir);
- auto encodedUserId = encodeBase64(userId, Base64Opts::urlSafe);
- auto sessionDir = userDataDir / "sessions"
- / encodedUserId / deviceId;
+ auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId);
auto storeFile = sessionDir / "store";
+ auto storeFileNew = sessionDir / "store.new";
auto metadataFile = sessionDir / "metadata";
@@ -195,18 +202,29 @@
}
}
- {
- auto storeStream = std::ofstream(storeFile);
+ try {
+ auto storeStream = std::ofstream(storeFileNew);
if (! storeStream) {
- qDebug() << "Unable to open storeFile";
+ qCWarning(kazvLog) << "Unable to open storeFile";
return;
}
using OAr = boost::archive::text_oarchive;
auto archive = OAr{storeStream};
c.serializeTo(archive);
- qDebug() << "Serialization done";
+ } catch (const std::exception &e) {
+ qCWarning(kazvLog) << "Cannot write to store file: " << e.what();
+ return;
}
+ err.clear();
+ std::filesystem::rename(storeFileNew, storeFile, err);
+ if (err) {
+ qCWarning(kazvLog) << "Cannot move storeFile into place: " << QString::fromStdString(err.message());
+ return;
+ }
+ qDebug() << "Serialization done";
+
+
// store metadata
{
KConfig metadata(QString::fromStdString(metadataFile.string()));
@@ -530,9 +548,8 @@
auto parts = sessionName.split(u'/');
if (parts.size() == 2) {
auto userId = parts[0].toStdString();
- auto sessionId = parts[1].toStdString();
- auto encodedUserId = encodeBase64(userId, Base64Opts::urlSafe);
- auto sessionDir = userDataDir / "sessions" / encodedUserId / sessionId;
+ auto deviceId = parts[1].toStdString();
+ auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId);
try {
lockGuard = std::make_unique<KazvSessionLockGuard>(sessionDir);
} catch (const std::runtime_error &e) {
@@ -552,16 +569,14 @@
auto parts = sessionName.split(u'/');
if (parts.size() == 2) {
auto userId = parts[0].toStdString();
- auto sessionId = parts[1].toStdString();
- auto encodedUserId = encodeBase64(userId, Base64Opts::urlSafe);
- qCDebug(kazvLog) << "trying new path";
- auto sessionDir = userDataDir / "sessions" / encodedUserId / sessionId;
+ auto deviceId = parts[1].toStdString();
+ auto sessionDir = sessionDirForUserAndDeviceId(userDataDir, userId, deviceId);
if (std::filesystem::exists(sessionDir)) {
qCDebug(kazvLog) << "new path works";
return std::filesystem::remove_all(sessionDir);
}
qCDebug(kazvLog) << "trying legacy path";
- auto legacySessionDir = userDataDir / "sessions" / userId / sessionId;
+ auto legacySessionDir = userDataDir / "sessions" / userId / deviceId;
if (std::filesystem::exists(legacySessionDir)) {
qCDebug(kazvLog) << "legacy path works";
return std::filesystem::remove_all(legacySessionDir);
diff --git a/src/tests/matrix-sdk-sessions-test.hpp b/src/tests/matrix-sdk-sessions-test.hpp
--- a/src/tests/matrix-sdk-sessions-test.hpp
+++ b/src/tests/matrix-sdk-sessions-test.hpp
@@ -44,4 +44,5 @@
void testListLegacySessions();
void testLoadSession();
void testSessionLock();
+ void testSaveSessionFailure();
};
diff --git a/src/tests/matrix-sdk-sessions-test.cpp b/src/tests/matrix-sdk-sessions-test.cpp
--- a/src/tests/matrix-sdk-sessions-test.cpp
+++ b/src/tests/matrix-sdk-sessions-test.cpp
@@ -136,6 +136,60 @@
#endif
}
+void MatrixSdkSessionsTest::testSaveSessionFailure()
+{
+#if KAZV_IS_WINDOWS
+ QSKIP("Skipping because session the condition cannot be easily satisfied on Windows");
+#endif
+
+ auto userId = std::string("@mew:example.com");
+ auto deviceId = std::string("device1");
+ createSession(userId, deviceId);
+
+ auto sessionDir = sessionDirForUserAndDeviceId(Fs::path(m_userDataDir), userId, deviceId);
+ // there is store, metadata and lock under the sessionDir
+
+ std::error_code err;
+ // make the sessionDir readonly, so that new file cannot be created
+ Fs::permissions(
+ sessionDir,
+ Fs::perms::owner_read | Fs::perms::owner_exec,
+ Fs::perm_options::replace,
+ err);
+ QVERIFY(!err);
+ err.clear();
+
+ // a model that is slightly different from the original one
+ SdkModel model;
+ model.client.userId = userId;
+ model.client.deviceId = deviceId;
+ model.client.serverUrl = "https://example.com";
+
+ {
+ auto sdk = makeMatrixSdk(model, /* testing = */ false);
+ // this should fail
+ sdk->serializeToFile();
+ }
+ // Not ideal, but this is the only way to wait for
+ // MatrixSdkPravite to be destroyed.
+ QTest::qWait(100);
+
+ auto sdk = makeMatrixSdk();
+ auto res = sdk->loadSession(QString::fromStdString(userId + "/" + deviceId));
+ QCOMPARE(res, MatrixSdk::SessionLoadSuccess);
+ // verify that the original data is kept
+ QCOMPARE(sdk->serverUrl(), u""_s);
+
+ // before cleaning up, add write permissions back so the files can be removed
+ Fs::permissions(
+ sessionDir,
+ Fs::perms::owner_read | Fs::perms::owner_write | Fs::perms::owner_exec,
+ Fs::perm_options::replace,
+ err);
+ QVERIFY(!err);
+ err.clear();
+}
+
#ifndef MATRIX_SDK_SESSIONS_TEST_NO_MAIN
QTEST_MAIN(MatrixSdkSessionsTest)
#endif

File Metadata

Mime Type
text/plain
Expires
Sat, Oct 19, 3:24 AM (22 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
26266
Default Alt Text
D189.1729333448.diff (9 KB)

Event Timeline