Skip to content

Commit

Permalink
fix(linux): automatically migrate config directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Mar 10, 2024
1 parent bc0a478 commit cfb2ea8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmake/compile_definitions/linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ if(NOT ${CUDA_FOUND}
message(FATAL_ERROR "Couldn't find either cuda, wayland, x11, (libdrm and libcap), or libva")
endif()

if(${SUNSHINE_BUILD_FLATPAK})
list(APPEND SUNSHINE_DEFINITIONS SUNSHINE_BUILD_FLATPAK=1)
endif()

# tray icon
if(${SUNSHINE_ENABLE_TRAY})
pkg_check_modules(APPINDICATOR appindicator3-0.1)
Expand Down
50 changes: 42 additions & 8 deletions src/platform/linux/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,56 @@ namespace platf {

fs::path
appdata() {
bool found = false;
[[maybe_unused]] bool migrate_config = true;
const char *dir;
const char *homedir;
fs::path config_path;

// Get the home directory
if ((homedir = getenv("HOME")) == nullptr || strlen(homedir) == 0) {
// If HOME is empty or not set, use the current user's home directory
homedir = getpwuid(geteuid())->pw_dir;
}

// May be set if running under a systemd service with the ConfigurationDirectory= option set.
if ((dir = getenv("CONFIGURATION_DIRECTORY")) != nullptr) {
return fs::path { dir } / "sunshine"sv;
if ((dir = getenv("CONFIGURATION_DIRECTORY")) != nullptr && strlen(dir) > 0) {
found = true;
config_path = fs::path(dir) / "sunshine"sv;
}
// Otherwise, follow the XDG base directory specification:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if ((dir = getenv("XDG_CONFIG_HOME")) != nullptr) {
return fs::path { dir } / "sunshine"sv;
}
if ((dir = getenv("HOME")) == nullptr) {
dir = getpwuid(geteuid())->pw_dir;
if (!found && (dir = getenv("XDG_CONFIG_HOME")) != nullptr && strlen(dir) > 0) {
found = true;
config_path = fs::path(dir) / "sunshine"sv;
}
// As a last resort, use the home directory
if (!found) {
migrate_config = false;
config_path = fs::path(homedir) / ".config/sunshine"sv;
}

// migrate from the old config location if necessary
#if defined SUNSHINE_BUILD_FLATPAK && SUNSHINE_BUILD_FLATPAK == 1
if (migrate_config && found) {
fs::path old_config_path = fs::path(homedir) / ".config/sunshine"sv;
if (old_config_path != config_path && fs::exists(old_config_path)) {
if (!fs::exists(config_path)) {
BOOST_LOG(info) << "Migrating config from "sv << old_config_path << " to "sv << config_path;
std::error_code ec;
fs::rename(old_config_path, config_path, ec);
if (ec) {
return old_config_path;
}
}
else {
BOOST_LOG(warning) << "Config exists in both "sv << old_config_path << " and "sv << config_path << ", using "sv << config_path << "... it is recommended to remove "sv << old_config_path;
}
}
}
#endif

return fs::path { dir } / ".config/sunshine"sv;
return config_path;
}

std::string
Expand Down

0 comments on commit cfb2ea8

Please sign in to comment.