Skip to content

Commit

Permalink
honor the $XDG_CACHE_HOME env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
twolife committed Oct 9, 2024
1 parent 48c8775 commit 40dcb60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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;
}

0 comments on commit 40dcb60

Please sign in to comment.