The open source OpenXR runtime

a/util: fallback to XDG_CACHE_HOME then ~/.cache in u_file_get_runtime_dir

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2465>

authored by

Simon Zeni and committed by
Marge Bot
970e3c1a a5221e1e

+21 -12
+20 -8
src/xrt/auxiliary/util/u_file.c
··· 1 - // Copyright 2019-2022, Collabora, Ltd. 1 + // Copyright 2019-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 9 9 */ 10 10 11 11 #include "xrt/xrt_config_os.h" 12 + #include "xrt/xrt_windows.h" 12 13 #include "util/u_file.h" 13 14 14 15 #include <errno.h> 16 + #include <stdbool.h> 15 17 #include <stdio.h> 16 18 #include <stdlib.h> 17 19 #include <string.h> 18 20 19 - 20 21 #if defined(XRT_OS_WINDOWS) && !defined(XRT_ENV_MINGW) 21 - #define PATH_MAX MAX_PATH 22 + #define PATH_MAX 4096 22 23 #endif 23 24 24 25 #ifdef XRT_OS_LINUX ··· 196 197 int 197 198 u_file_get_runtime_dir(char *out_path, size_t out_path_size) 198 199 { 199 - const char *xgd_rt = getenv("XDG_RUNTIME_DIR"); 200 - if (xgd_rt != NULL) { 201 - return snprintf(out_path, out_path_size, "%s", xgd_rt); 200 + const char *xdg_rt = getenv("XDG_RUNTIME_DIR"); 201 + if (xdg_rt != NULL) { 202 + return snprintf(out_path, out_path_size, "%s", xdg_rt); 203 + } 204 + 205 + const char *xdg_cache = getenv("XDG_CACHE_HOME"); 206 + if (xdg_cache != NULL) { 207 + return snprintf(out_path, out_path_size, "%s", xdg_cache); 202 208 } 203 209 204 - const char *tmp = "/tmp"; 205 - return snprintf(out_path, out_path_size, "%s", tmp); 210 + #ifdef XRT_OS_WINDOWS 211 + WCHAR temp[MAX_PATH] = {0}; 212 + GetTempPath2W(sizeof(temp), temp); 213 + return wcstombs(out_path, temp, out_path_size); 214 + #else 215 + const char *cache = "~/.cache"; 216 + return snprintf(out_path, out_path_size, "%s", cache); 217 + #endif 206 218 } 207 219 208 220 int
+1 -4
src/xrt/auxiliary/util/u_file.h
··· 1 - // Copyright 2019-2020, Collabora, Ltd. 1 + // Copyright 2019-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 9 9 10 10 #pragma once 11 11 12 - #include "xrt/xrt_compiler.h" 13 - 14 12 #include <stdio.h> 15 - 16 13 17 14 #ifdef __cplusplus 18 15 extern "C" {