Skip to content

Commit 40dcb60

Browse files
committed
honor the $XDG_CACHE_HOME env variable
1 parent 48c8775 commit 40dcb60

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

Descent3/init.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,13 +1957,7 @@ void SetupTempDirectory(void) {
19571957
if (t_arg) {
19581958
Descent3_temp_directory = GameArgs[t_arg + 1];
19591959
} else {
1960-
std::error_code ec;
1961-
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
1962-
if (ec) {
1963-
Error("Could not find temporary directory: \"%s\"", ec.message().c_str() );
1964-
exit(1);
1965-
}
1966-
Descent3_temp_directory = tempPath / "Descent3" / "cache";
1960+
Descent3_temp_directory = ddio_GetTempPath();
19671961
}
19681962

19691963
std::error_code ec;

ddio/ddio.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,10 @@ bool ddio_CreateLockFile(const std::filesystem::path &dir);
412412
*/
413413
bool ddio_DeleteLockFile(const std::filesystem::path &dir);
414414

415+
/**
416+
* Gets path where to write temporary/cache files.
417+
* @return path where user can write cache files.
418+
*/
419+
std::filesystem::path ddio_GetTempPath();
420+
415421
#endif

ddio/file.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,30 @@ std::filesystem::path ddio_GetTmpFileName(const std::filesystem::path &basedir,
192192
mem_free(random_name);
193193
return result;
194194
}
195+
196+
std::filesystem::path ddio_GetTempPath() {
197+
std::filesystem::path result;
198+
199+
#if defined(POSIX)
200+
char *envr = SDL_getenv("XDG_CACHE_HOME");
201+
if (envr) {
202+
result = std::filesystem::path(envr) / "Descent3";
203+
} else {
204+
envr = SDL_getenv("HOME");
205+
if (envr) {
206+
result = std::filesystem::path(envr) / ".cache" / "Descent3";
207+
} else {
208+
#endif
209+
std::error_code ec;
210+
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
211+
if (ec) {
212+
Error("Could not find temporary directory: \"%s\"", ec.message().c_str() );
213+
exit(1);
214+
}
215+
result = tempPath / "Descent3" / "cache";
216+
#if defined(POSIX)
217+
}
218+
}
219+
#endif
220+
return result;
221+
}

0 commit comments

Comments
 (0)