Skip to content

Commit 9504979

Browse files
committed
Linux: follow XDG guidelines
- use ~/.local/share/unvanquished instead of ~/.unvanquished - move legacy path if exists
1 parent 9f1b691 commit 9504979

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

src/common/FileSystem.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,51 @@ std::string DefaultHomePath()
20672067
#ifdef __APPLE__
20682068
return std::string(home) + "/Library/Application Support/" PRODUCT_NAME;
20692069
#else
2070-
return std::string(home) + "/." PRODUCT_NAME_LOWER;
2070+
struct stat stl, stx;
2071+
2072+
std::string legacyHomePath = std::string(home) + "/." PRODUCT_NAME_LOWER;
2073+
const char* _xdgDataHome = getenv("XDG_DATA_HOME");
2074+
std::string xdgDataHome = _xdgDataHome == NULL ? "" : std::string(xdgDataHome);
2075+
std::string xdgHomePath;
2076+
2077+
if (xdgDataHome.empty()) {
2078+
xdgDataHome = std::string(home) + "/.local/share";
2079+
}
2080+
2081+
xdgHomePath = xdgDataHome + "/" PRODUCT_NAME_LOWER;
2082+
2083+
// move legacy ~/.unvanquished directory to XDG ~/.local/share/unvanquished
2084+
// if ~/.unvanquished exists but ~/.local/share/unvanquished does not exist
2085+
// unless ~/.unvanquished is a symlink (symlink can be relative, behavior is unpredictable)
2086+
if (lstat(legacyHomePath.c_str(), &stl) == 0) {
2087+
if (S_ISLNK(stl.st_mode) && stat(xdgHomePath.c_str(), &stx) != 0) {
2088+
Sys::Error("Legacy home path %s is a symlink and symlink can be relative, will not rename to %s"
2089+
", do it yourself!", legacyHomePath, xdgHomePath);
2090+
}
2091+
2092+
if (S_ISDIR(stl.st_mode)) {
2093+
if (stat(xdgHomePath.c_str(), &stx) != 0) {
2094+
std::error_code err;
2095+
2096+
RawPath::CreatePathTo(xdgDataHome, err);
2097+
2098+
if (err) {
2099+
Sys::Error("Could not create %s: %s", xdgDataHome, err.message());
2100+
}
2101+
2102+
fsLogs.Warn("Renaming legacy home path %s to %s", legacyHomePath, xdgHomePath);
2103+
RawPath::MoveFile(xdgHomePath, legacyHomePath, err);
2104+
2105+
if (err) {
2106+
Sys::Error("Could not rename legacy home path to %s: %s", xdgHomePath, err.message());
2107+
}
2108+
} else {
2109+
fsLogs.Warn("XDG home path already exist, doing nothing: %s", xdgHomePath);
2110+
}
2111+
}
2112+
}
2113+
2114+
return xdgHomePath;
20712115
#endif
20722116
#endif
20732117
}

0 commit comments

Comments
 (0)