diff --git a/include/luxrays/utils/config.h b/include/luxrays/utils/config.h index 130460355..b77ed9536 100644 --- a/include/luxrays/utils/config.h +++ b/include/luxrays/utils/config.h @@ -28,7 +28,7 @@ namespace luxrays { extern std::string SanitizeFileName(const std::string &name); -extern boost::filesystem::path GetConfigDir(); +extern boost::filesystem::path GetCacheDir(); } diff --git a/src/luxrays/utils/config.cpp b/src/luxrays/utils/config.cpp index fa1783d8e..49ff5e8a8 100644 --- a/src/luxrays/utils/config.cpp +++ b/src/luxrays/utils/config.cpp @@ -40,12 +40,38 @@ string SanitizeFileName(const string &name) { return sanitizedName; } -boost::filesystem::path GetConfigDir() { +boost::filesystem::path GetEnvPath(const string &name) { + char *path = getenv(name.c_str()); + + if (path && path[0]) { + return boost::filesystem::path(path); + } + + return ""; +} + +boost::filesystem::path GetCacheDir() { #if defined(__linux__) // boost::filesystem::temp_directory_path() is usually mapped to /tmp and // the content of the directory is often deleted at each reboot - boost::filesystem::path kernelConfigDir = getenv("HOME"); - kernelConfigDir = kernelConfigDir / ".config" / "luxcorerender.org"; + + // XDG standard says XDG_CACHE_HOME is unset by default. + boost::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. + boost::filesystem::path home = GetEnvPath("HOME"); + + if (home.size()) { + xdgCacheHome = home / ".config"; + } + else { + xdgCacheHome = boost::filesystem::temp_directory_path(); + } + } + + boost::filesystem::path kernelConfigDir = xdgCacheHome / "luxcorerender.org"; #elif defined(__APPLE__) // boost::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 6ae9e9f42..2cb56e97c 100644 --- a/src/luxrays/utils/cuda.cpp +++ b/src/luxrays/utils/cuda.cpp @@ -133,7 +133,7 @@ bool cudaKernelCache::ForcedCompilePTX(const vector &kernelsParameters, //------------------------------------------------------------------------------ boost::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 785836cef..b5844c127 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 //------------------------------------------------------------------------------ boost::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) {