Skip to content

Commit 9407867

Browse files
committed
handle symlink when moving legacy home path to XDG one
1 parent a6d3268 commit 9407867

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

src/common/FileSystem.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,33 +2080,41 @@ std::string DefaultHomePath()
20802080

20812081
xdgHomePath = Path::Build(xdgDataHome, PRODUCT_NAME_LOWER);
20822082

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)
20862083
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)) {
2084+
if (S_ISDIR(stl.st_mode) || S_ISLNK(stl.st_mode)) {
20932085
if (stat(xdgHomePath.c_str(), &stx) != 0) {
20942086
std::error_code err;
20952087

20962088
RawPath::CreatePathTo(xdgDataHome, err);
20972089

20982090
if (err) {
2099-
Sys::Error("Could not create %s: %s", xdgDataHome, err.message());
2091+
Sys::Error("Could not create XDG data directory %s: %s", xdgDataHome, err.message());
21002092
}
21012093

2102-
fsLogs.Warn("Renaming legacy home path %s to %s", legacyHomePath, xdgHomePath);
2103-
RawPath::MoveFile(xdgHomePath, legacyHomePath, err);
2094+
if (S_ISLNK(stl.st_mode)) {
2095+
int ret;
2096+
int fd = open(xdgDataHome.c_str(), O_DIRECTORY);
2097+
2098+
if (fd == -1) {
2099+
Sys::Error("Could not open XDG data directory %s", xdgDataHome);
2100+
}
2101+
2102+
fsLogs.Warn("Creating legacy home path symlink %s to XDG home path %s", legacyHomePath, xdgHomePath);
2103+
ret = symlinkat(legacyHomePath.c_str(), fd, xdgHomePath.c_str());
2104+
2105+
if (ret == -1) {
2106+
Sys::Error("Could not create symlink %s", xdgHomePath);
2107+
}
2108+
} else {
2109+
fsLogs.Warn("Renaming legacy home path %s to XDG home path %s", legacyHomePath, xdgHomePath);
2110+
RawPath::MoveFile(xdgHomePath, legacyHomePath, err);
2111+
}
21042112

21052113
if (err) {
21062114
Sys::Error("Could not rename legacy home path to %s: %s", xdgHomePath, err.message());
21072115
}
21082116
} else {
2109-
fsLogs.Warn("XDG home path already exist, doing nothing: %s", xdgHomePath);
2117+
fsLogs.Warn("Legacy home path %s exists but XDG home path %s already exists, doing nothing", legacyHomePath, xdgHomePath);
21102118
}
21112119
}
21122120
}

0 commit comments

Comments
 (0)