Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

honor the $XDG_CACHE_HOME env variable #625

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Descent3/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,13 +1957,7 @@ void SetupTempDirectory(void) {
if (t_arg) {
Descent3_temp_directory = GameArgs[t_arg + 1];
} else {
std::error_code ec;
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
if (ec) {
Error("Could not find temporary directory: \"%s\"", ec.message().c_str() );
exit(1);
}
Descent3_temp_directory = tempPath / "Descent3" / "cache";
Descent3_temp_directory = ddio_GetTempPath();
}

std::error_code ec;
Expand Down
6 changes: 6 additions & 0 deletions ddio/ddio.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,10 @@ bool ddio_CreateLockFile(const std::filesystem::path &dir);
*/
bool ddio_DeleteLockFile(const std::filesystem::path &dir);

/**
* Gets path where to write temporary/cache files.
* @return path where user can write cache files.
*/
std::filesystem::path ddio_GetTempPath();

#endif
27 changes: 27 additions & 0 deletions ddio/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,30 @@ std::filesystem::path ddio_GetTmpFileName(const std::filesystem::path &basedir,
mem_free(random_name);
return result;
}

std::filesystem::path ddio_GetTempPath() {
std::filesystem::path result;

#if defined(POSIX)
char *envr = SDL_getenv("XDG_CACHE_HOME");
if (envr) {
result = std::filesystem::path(envr) / "Descent3";
} else {
envr = SDL_getenv("HOME");
if (envr) {
result = std::filesystem::path(envr) / ".cache" / "Descent3";
} else {
#endif
std::error_code ec;
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
if (ec) {
Error("Could not find temporary directory: \"%s\"", ec.message().c_str() );
exit(1);
}
result = tempPath / "Descent3" / "cache";
#if defined(POSIX)
}
}
#endif
return result;
}