From a6d326874e7a5c0c8dd89e43119eeb4bd0231b8a Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Tue, 4 Jul 2017 07:13:01 +0200 Subject: [PATCH 1/3] Linux: follow XDG guidelines - use ~/.local/share/unvanquished instead of ~/.unvanquished - move legacy path if exists - fix Unvanquished/Unvanquished#730 --- src/common/FileSystem.cpp | 46 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index bf54ee8299..49ace4647a 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -2067,7 +2067,51 @@ std::string DefaultHomePath() #ifdef __APPLE__ return std::string(home) + "/Library/Application Support/" PRODUCT_NAME; #else - return std::string(home) + "/." PRODUCT_NAME_LOWER; + struct stat stl, stx; + + std::string legacyHomePath = Path::Build(std::string(home), "." PRODUCT_NAME_LOWER); + const char* _xdgDataHome = getenv("XDG_DATA_HOME"); + std::string xdgDataHome = _xdgDataHome == NULL ? "" : std::string(xdgDataHome); + std::string xdgHomePath; + + if (xdgDataHome.empty()) { + xdgDataHome = Path::Build(Path::Build(std::string(home), ".local") ,"share"); + } + + xdgHomePath = Path::Build(xdgDataHome, PRODUCT_NAME_LOWER); + + // move legacy ~/.unvanquished directory to XDG ~/.local/share/unvanquished + // if ~/.unvanquished exists but ~/.local/share/unvanquished does not exist + // unless ~/.unvanquished is a symlink (symlink can be relative, behavior is unpredictable) + if (lstat(legacyHomePath.c_str(), &stl) == 0) { + if (S_ISLNK(stl.st_mode) && stat(xdgHomePath.c_str(), &stx) != 0) { + Sys::Error("Legacy home path %s is a symlink and symlink can be relative, will not rename to %s" + ", do it yourself!", legacyHomePath, xdgHomePath); + } + + if (S_ISDIR(stl.st_mode)) { + if (stat(xdgHomePath.c_str(), &stx) != 0) { + std::error_code err; + + RawPath::CreatePathTo(xdgDataHome, err); + + if (err) { + Sys::Error("Could not create %s: %s", xdgDataHome, err.message()); + } + + fsLogs.Warn("Renaming legacy home path %s to %s", legacyHomePath, xdgHomePath); + RawPath::MoveFile(xdgHomePath, legacyHomePath, err); + + if (err) { + Sys::Error("Could not rename legacy home path to %s: %s", xdgHomePath, err.message()); + } + } else { + fsLogs.Warn("XDG home path already exist, doing nothing: %s", xdgHomePath); + } + } + } + + return xdgHomePath; #endif #endif } From 94078679c154aaf2c0433624dfb41d239fc1b389 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sat, 8 Jul 2017 09:21:50 +0200 Subject: [PATCH 2/3] handle symlink when moving legacy home path to XDG one --- src/common/FileSystem.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 49ace4647a..e7624c7f46 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -2080,33 +2080,41 @@ std::string DefaultHomePath() xdgHomePath = Path::Build(xdgDataHome, PRODUCT_NAME_LOWER); - // move legacy ~/.unvanquished directory to XDG ~/.local/share/unvanquished - // if ~/.unvanquished exists but ~/.local/share/unvanquished does not exist - // unless ~/.unvanquished is a symlink (symlink can be relative, behavior is unpredictable) if (lstat(legacyHomePath.c_str(), &stl) == 0) { - if (S_ISLNK(stl.st_mode) && stat(xdgHomePath.c_str(), &stx) != 0) { - Sys::Error("Legacy home path %s is a symlink and symlink can be relative, will not rename to %s" - ", do it yourself!", legacyHomePath, xdgHomePath); - } - - if (S_ISDIR(stl.st_mode)) { + if (S_ISDIR(stl.st_mode) || S_ISLNK(stl.st_mode)) { if (stat(xdgHomePath.c_str(), &stx) != 0) { std::error_code err; RawPath::CreatePathTo(xdgDataHome, err); if (err) { - Sys::Error("Could not create %s: %s", xdgDataHome, err.message()); + Sys::Error("Could not create XDG data directory %s: %s", xdgDataHome, err.message()); } - fsLogs.Warn("Renaming legacy home path %s to %s", legacyHomePath, xdgHomePath); - RawPath::MoveFile(xdgHomePath, legacyHomePath, err); + if (S_ISLNK(stl.st_mode)) { + int ret; + int fd = open(xdgDataHome.c_str(), O_DIRECTORY); + + if (fd == -1) { + Sys::Error("Could not open XDG data directory %s", xdgDataHome); + } + + fsLogs.Warn("Creating legacy home path symlink %s to XDG home path %s", legacyHomePath, xdgHomePath); + ret = symlinkat(legacyHomePath.c_str(), fd, xdgHomePath.c_str()); + + if (ret == -1) { + Sys::Error("Could not create symlink %s", xdgHomePath); + } + } else { + fsLogs.Warn("Renaming legacy home path %s to XDG home path %s", legacyHomePath, xdgHomePath); + RawPath::MoveFile(xdgHomePath, legacyHomePath, err); + } if (err) { Sys::Error("Could not rename legacy home path to %s: %s", xdgHomePath, err.message()); } } else { - fsLogs.Warn("XDG home path already exist, doing nothing: %s", xdgHomePath); + fsLogs.Warn("Legacy home path %s exists but XDG home path %s already exists, doing nothing", legacyHomePath, xdgHomePath); } } } From 267b375d0a48c3bf916d7873682c2db6d17e9c58 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sun, 9 Jul 2017 22:54:47 +0200 Subject: [PATCH 3/3] print c errors --- src/common/FileSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index e7624c7f46..203b262732 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -2096,14 +2096,14 @@ std::string DefaultHomePath() int fd = open(xdgDataHome.c_str(), O_DIRECTORY); if (fd == -1) { - Sys::Error("Could not open XDG data directory %s", xdgDataHome); + Sys::Error("Could not open XDG data directory %s: %s", xdgDataHome, strerror(errno)); } fsLogs.Warn("Creating legacy home path symlink %s to XDG home path %s", legacyHomePath, xdgHomePath); ret = symlinkat(legacyHomePath.c_str(), fd, xdgHomePath.c_str()); if (ret == -1) { - Sys::Error("Could not create symlink %s", xdgHomePath); + Sys::Error("Could not create symlink %s: %s", xdgHomePath, strerror(errno)); } } else { fsLogs.Warn("Renaming legacy home path %s to XDG home path %s", legacyHomePath, xdgHomePath);