From 509e218676cebc30c72f15234d900528bc5e06fe Mon Sep 17 00:00:00 2001 From: jNullj <15849761+jNullj@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:42:32 +0300 Subject: [PATCH] Change conf path XDG_CACHE_HOME to XDG_STATE_HOME (#9755) Keepassxc saves application state at XDG_CACHE_HOME which can be cleared on some systems periodicly. This is not desireable as app state like window size is not consistent when openning the app. To avoid this this commit is switching the path to XDG_STATE_HOME which is more fitting based on the freedesktop basedir spec (https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html), this will allow to prevent state file deletion as well. Resolves #9738 --- docs/topics/DownloadInstall.adoc | 2 +- src/core/Config.cpp | 33 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/topics/DownloadInstall.adoc b/docs/topics/DownloadInstall.adoc index f5a967ec5..21172fae4 100644 --- a/docs/topics/DownloadInstall.adoc +++ b/docs/topics/DownloadInstall.adoc @@ -59,7 +59,7 @@ image::linux_store.png[] The Snap and Flatpak options are sandboxed applications (more secure). The Native option is installed with the operating system files. Read more about the limitations of these options here: https://keepassxc.org/docs/#faq-appsnap-yubikey[KeePassXC Snap FAQ] -NOTE: KeePassXC stores a configuration file in `~/.cache` to remember window position, recent files, and other local settings. If you mount this folder to a tmpdisk you will lose settings after reboot. +NOTE: KeePassXC stores a configuration file in `~/.local/state` to remember window position, recent files, and other local settings. If you mount this folder to a tmpdisk you will lose settings after reboot. === macOS To install the KeePassXC app on macOS, double click on the downloaded DMG file and use the click and drag option as shown: diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 25048513e..a5ab7e2b6 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -472,6 +472,28 @@ void Config::init(const QString& configFileName, const QString& localConfigFileN QDir().rmdir(QFileInfo(localConfigFileName).absolutePath()); } +#if defined(Q_OS_LINUX) + // Upgrade from previous KeePassXC version which stores its config + // in ~/.cache on Linux instead of ~/.local/state. + // Move file to correct location before continuing. + if (!QFile::exists(localConfigFileName)) { + QString oldLocalConfigPath = + QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc"; + QString suffix; +#ifdef QT_DEBUG + suffix = "_debug"; +#endif + oldLocalConfigPath += QString("/keepassxc%1.ini").arg(suffix); + oldLocalConfigPath = QDir::toNativeSeparators(oldLocalConfigPath); + if (QFile::exists(oldLocalConfigPath)) { + QDir().mkpath(QFileInfo(localConfigFileName).absolutePath()); + QFile::copy(oldLocalConfigPath, localConfigFileName); + QFile::remove(oldLocalConfigPath); + QDir().rmdir(QFileInfo(oldLocalConfigPath).absolutePath()); + } + } +#endif + m_settings.reset(new QSettings(configFileName, QSettings::IniFormat)); if (!localConfigFileName.isEmpty() && configFileName != localConfigFileName) { m_localSettings.reset(new QSettings(localConfigFileName, QSettings::IniFormat)); @@ -512,7 +534,16 @@ QPair Config::defaultConfigFiles() #else // On case-sensitive Operating Systems, force use of lowercase app directories configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/keepassxc"; - localConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc"; + // Qt does not support XDG_STATE_HOME yet, change this once XDG_STATE_HOME is added + QString xdgStateHome = QFile::decodeName(qgetenv("XDG_STATE_HOME")); + if (!xdgStateHome.startsWith(u'/')) { + xdgStateHome.clear(); // spec says relative paths should be ignored + } + if (xdgStateHome.isEmpty()) { + xdgStateHome = QDir::homePath() + "/.local/state"; + } + + localConfigPath = xdgStateHome + "/keepassxc"; #endif QString suffix;