Skip to content

Commit

Permalink
fix(linux): automatically migrate config directory (LizardByte#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored and KuleRucket committed Jun 6, 2024
1 parent 5e9b294 commit 826cd69
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions packaging/linux/flatpak/dev.lizardbyte.sunshine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ separate-locales: false
finish-args:
- --device=all # access all devices
- --env=PULSE_PROP_media.category=Manager # allow sunshine to manage audio sinks
- --env=SUNSHINE_MIGRATE_CONFIG=1 # migrate config files to the new location
- --filesystem=home # need to save files in user's home directory
- --share=ipc # required for X11 shared memory extension
- --share=network # access network
Expand Down
48 changes: 40 additions & 8 deletions src/platform/linux/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,54 @@ namespace platf {

fs::path
appdata() {
bool found = false;
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 (migrate_config && found && getenv("SUNSHINE_MIGRATE_CONFIG") == "1"sv) {
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;
}
}
}

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

std::string
Expand Down

0 comments on commit 826cd69

Please sign in to comment.