Skip to content

Commit 62c862d

Browse files
authored
Merge pull request #615 from illwieckz/fixes/xdg-cache-home
luxrays: rename GetConfigDir as GetCacheDir and use XDG_CACHE_HOME according to specifications
2 parents d610adc + ba756e1 commit 62c862d

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

include/luxrays/utils/config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
namespace luxrays {
2929

3030
extern std::string SanitizeFileName(const std::string &name);
31-
extern std::filesystem::path GetConfigDir();
31+
32+
extern std::filesystem::path GetCacheDir();
33+
3234

3335
}
3436

src/luxrays/utils/config.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,39 @@ string SanitizeFileName(const string &name) {
4040
return sanitizedName;
4141
}
4242

43-
std::filesystem::path GetConfigDir() {
43+
std::filesystem::path GetEnvPath(const string &name) {
44+
char *path = getenv(name.c_str());
45+
46+
if (path && path[0]) {
47+
return std::filesystem::path(path);
48+
}
49+
50+
return "";
51+
}
52+
53+
std::filesystem::path GetCacheDir() {
54+
4455
#if defined(__linux__)
4556
// std::filesystem::temp_directory_path() is usually mapped to /tmp and
4657
// the content of the directory is often deleted at each reboot
47-
std::filesystem::path kernelConfigDir = getenv("HOME");
48-
kernelConfigDir = kernelConfigDir / ".config" / "luxcorerender.org";
58+
59+
// XDG standard says XDG_CACHE_HOME is unset by default.
60+
std::filesystem::path xdgCacheHome = GetEnvPath("XDG_CACHE_HOME");
61+
62+
if (!xdgCacheHome.size()) {
63+
// HOME should never be unset, but we better not want to
64+
// crash if that happens.
65+
std::filesystem::path home = GetEnvPath("HOME");
66+
67+
if (home.size()) {
68+
xdgCacheHome = home / ".config";
69+
}
70+
else {
71+
xdgCacheHome = std::filesystem::temp_directory_path();
72+
}
73+
}
74+
75+
std::filesystem::path kernelConfigDir = xdgCacheHome / "luxcorerender.org";
4976
#elif defined(__APPLE__)
5077
// std::filesystem::temp_directory_path() is usually mapped to /tmp and
5178
// the content of the directory is deleted at each reboot on MacOS

src/luxrays/utils/cuda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ bool cudaKernelCache::ForcedCompilePTX(const vector<string> &kernelsParameters,
136136
//------------------------------------------------------------------------------
137137

138138
std::filesystem::path cudaKernelPersistentCache::GetCacheDir(const string &applicationName) {
139-
return GetConfigDir() / "cuda_kernel_cache" / SanitizeFileName(applicationName);
139+
return luxrays::GetCacheDir() / "cuda_kernel_cache" / SanitizeFileName(applicationName);
140140
}
141141

142142
cudaKernelPersistentCache::cudaKernelPersistentCache(const string &applicationName) {

src/luxrays/utils/ocl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ cl_program oclKernelCache::ForcedCompile(cl_context context, cl_device_id device
202202
//------------------------------------------------------------------------------
203203

204204
std::filesystem::path oclKernelPersistentCache::GetCacheDir(const string &applicationName) {
205-
return GetConfigDir() / "ocl_kernel_cache" / SanitizeFileName(applicationName);
205+
return luxrays::GetCacheDir() / "ocl_kernel_cache" / SanitizeFileName(applicationName);
206206
}
207207

208208
oclKernelPersistentCache::oclKernelPersistentCache(const string &applicationName) {

0 commit comments

Comments
 (0)