diff --git a/include/luxrays/utils/config.h b/include/luxrays/utils/config.h index d4f07429e..0e6a7eecd 100644 --- a/include/luxrays/utils/config.h +++ b/include/luxrays/utils/config.h @@ -28,7 +28,9 @@ namespace luxrays { extern std::string SanitizeFileName(const std::string &name); -extern std::filesystem::path GetConfigDir(); + +extern std::filesystem::path GetCacheDir(); + } diff --git a/src/luxrays/utils/config.cpp b/src/luxrays/utils/config.cpp index 454ffc62a..35902eb5c 100644 --- a/src/luxrays/utils/config.cpp +++ b/src/luxrays/utils/config.cpp @@ -40,12 +40,39 @@ string SanitizeFileName(const string &name) { return sanitizedName; } -std::filesystem::path GetConfigDir() { +std::filesystem::path GetEnvPath(const string &name) { + char *path = getenv(name.c_str()); + + if (path && path[0]) { + return std::filesystem::path(path); + } + + return ""; +} + +std::filesystem::path GetCacheDir() { + #if defined(__linux__) // std::filesystem::temp_directory_path() is usually mapped to /tmp and // the content of the directory is often deleted at each reboot - std::filesystem::path kernelConfigDir = getenv("HOME"); - kernelConfigDir = kernelConfigDir / ".config" / "luxcorerender.org"; + + // XDG standard says XDG_CACHE_HOME is unset by default. + std::filesystem::path xdgCacheHome = GetEnvPath("XDG_CACHE_HOME"); + + if (!xdgCacheHome.size()) { + // HOME should never be unset, but we better not want to + // crash if that happens. + std::filesystem::path home = GetEnvPath("HOME"); + + if (home.size()) { + xdgCacheHome = home / ".config"; + } + else { + xdgCacheHome = std::filesystem::temp_directory_path(); + } + } + + std::filesystem::path kernelConfigDir = xdgCacheHome / "luxcorerender.org"; #elif defined(__APPLE__) // std::filesystem::temp_directory_path() is usually mapped to /tmp and // the content of the directory is deleted at each reboot on MacOS diff --git a/src/luxrays/utils/cuda.cpp b/src/luxrays/utils/cuda.cpp index e3894f709..3419b7db9 100644 --- a/src/luxrays/utils/cuda.cpp +++ b/src/luxrays/utils/cuda.cpp @@ -136,7 +136,7 @@ bool cudaKernelCache::ForcedCompilePTX(const vector &kernelsParameters, //------------------------------------------------------------------------------ std::filesystem::path cudaKernelPersistentCache::GetCacheDir(const string &applicationName) { - return GetConfigDir() / "cuda_kernel_cache" / SanitizeFileName(applicationName); + return luxrays::GetCacheDir() / "cuda_kernel_cache" / SanitizeFileName(applicationName); } cudaKernelPersistentCache::cudaKernelPersistentCache(const string &applicationName) { diff --git a/src/luxrays/utils/ocl.cpp b/src/luxrays/utils/ocl.cpp index 52c4ff10b..6e31f5f64 100644 --- a/src/luxrays/utils/ocl.cpp +++ b/src/luxrays/utils/ocl.cpp @@ -202,7 +202,7 @@ cl_program oclKernelCache::ForcedCompile(cl_context context, cl_device_id device //------------------------------------------------------------------------------ std::filesystem::path oclKernelPersistentCache::GetCacheDir(const string &applicationName) { - return GetConfigDir() / "ocl_kernel_cache" / SanitizeFileName(applicationName); + return luxrays::GetCacheDir() / "ocl_kernel_cache" / SanitizeFileName(applicationName); } oclKernelPersistentCache::oclKernelPersistentCache(const string &applicationName) {