From 4fbe8e1773aee00b87b8cfc6ea3abb8a02177240 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Tue, 2 Sep 2025 10:48:54 -0500 Subject: [PATCH] App: Fix safe mode and home directory --- src/App/Application.cpp | 8 ++++---- src/App/ApplicationDirectories.cpp | 32 ++++++++++++++++++++++++++++++ src/App/ApplicationDirectories.h | 9 +++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index bc84016a77..35957b7e8a 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -2569,13 +2569,13 @@ void Application::initConfig(int argc, char ** argv) mConfig["KeepDeprecatedPaths"] = "1"; } + if (vm.contains("safe-mode")) { + mConfig["SafeMode"] = "1"; + } + // extract home paths _appDirs = std::make_unique(mConfig); - if (vm.contains("safe-mode")) { - SafeMode::StartSafeMode(); - } - # ifdef FC_DEBUG mConfig["Debug"] = "1"; # else diff --git a/src/App/ApplicationDirectories.cpp b/src/App/ApplicationDirectories.cpp index 011b37d8a8..22b357be47 100644 --- a/src/App/ApplicationDirectories.cpp +++ b/src/App/ApplicationDirectories.cpp @@ -37,6 +37,7 @@ #include #include +#include "SafeMode.h" #include #include @@ -68,6 +69,11 @@ const fs::path& ApplicationDirectories::getHomePath() const return this->_home; } +const fs::path& ApplicationDirectories::getUserHomePath() const +{ + return this->_userHome; +} + const fs::path& ApplicationDirectories::getTempPath() const { return this->_temp; } @@ -182,10 +188,19 @@ void ApplicationDirectories::configurePaths(std::map& m // get the system standard paths auto [configHome, dataHome, cacheHome, tempPath] = getStandardPaths(); + if (mConfig.contains("SafeMode")) { + if (startSafeMode(mConfig)) { + // If we're in safe mode, don't try to set any directories here, they've been overridden + // by temp directories in the SafeMode setup. + return; + } + } + // User home path // fs::path homePath = findUserHomePath(customHome); mConfig["UserHomePath"] = Base::FileInfo::pathToString(homePath); + _userHome = homePath; // the old path name to save config and data files std::vector subdirs; @@ -243,6 +258,23 @@ void ApplicationDirectories::configurePaths(std::map& m mConfig["UserMacroPath"] = Base::FileInfo::pathToString(macro) + PATHSEP; } +bool ApplicationDirectories::startSafeMode(std::map& mConfig) +{ + SafeMode::StartSafeMode(); + if (SafeMode::SafeModeEnabled()) { + _userAppData = mConfig["UserAppData"]; + _userConfig = mConfig["UserConfigPath"]; + _userCache = mConfig["UserCachePath"]; + _temp = mConfig["AppTempPath"]; + _userMacro = mConfig["UserMacroPath"]; + _userHome = mConfig["UserHomePath"]; + _usingCustomDirectories = true; + return true; + } + return false; +} + + void ApplicationDirectories::configureResourceDirectory(const std::map& mConfig) { #ifdef RESOURCEDIR // #6892: Conda may inject null characters => remove them using c_str() diff --git a/src/App/ApplicationDirectories.h b/src/App/ApplicationDirectories.h index 87f86df7b7..5e8d770ad3 100644 --- a/src/App/ApplicationDirectories.h +++ b/src/App/ApplicationDirectories.h @@ -56,6 +56,10 @@ namespace App { /// `App::ApplicationDirectories` class. const std::filesystem::path& getHomePath() const; + /// Get the user's home directory. This should not be used for many things, use caution when + /// deciding to use it for anything, there are usually better places. + const std::filesystem::path& getUserHomePath() const; + /// Temp path is the location of all temporary files: it is not guaranteed to preserve /// information between runs of the program, but *is* guaranteed to exist for the duration /// of a single program execution (that is, files are not deleted from it *during* the run). @@ -174,6 +178,10 @@ namespace App { protected: + /// Override all application directories with temp directories. Returns true on success and + /// false if the temp directory creation failed. + bool startSafeMode(std::map& mConfig); + /// Take a path and add a version to it, if it's possible to do so. A version can be /// appended only if a) the versioned subdirectory already exists, or b) pathToCheck/subdirs /// does NOT yet exist. This does not actually create any directories, just determines @@ -228,6 +236,7 @@ namespace App { std::filesystem::path _userConfig; std::filesystem::path _userAppData; std::filesystem::path _userMacro; + std::filesystem::path _userHome; std::filesystem::path _resource; std::filesystem::path _library; std::filesystem::path _help;