]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Use util::getenv_path for environment variables that contain a path
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 7 Nov 2024 20:54:38 +0000 (21:54 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 9 Nov 2024 19:08:02 +0000 (20:08 +0100)
This should improve support for non-ASCII paths in Windows.

Hopefully fixes #1409.

src/ccache/config.cpp

index 8f596ea08be384d24d78d8b30f0a9f8ecb802925..6157d996d7dd6c90826dd0c9aa28b75b5c6b921a 100644 (file)
@@ -517,15 +517,17 @@ fs::path
 home_directory()
 {
 #ifdef _WIN32
-  if (const char* p = getenv("USERPROFILE")) {
-    return p;
+  auto user_profile = util::getenv_path("USERPROFILE");
+  if (user_profile) {
+    return *user_profile;
   }
   throw core::Fatal(
     "The USERPROFILE environment variable must be set to your user profile"
     " folder");
 #else
-  if (const char* p = getenv("HOME")) {
-    return p;
+  auto home = util::getenv_path("HOME");
+  if (home) {
+    return *home;
   }
 #  ifdef HAVE_GETPWUID
   {
@@ -593,53 +595,53 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
   const bool legacy_ccache_dir_exists =
     DirEntry(legacy_ccache_dir).is_directory();
 #ifdef _WIN32
-  const char* const env_appdata = getenv("APPDATA");
-  const char* const env_local_appdata = getenv("LOCALAPPDATA");
+  auto env_appdata = util::getenv_path("APPDATA");
+  auto env_local_appdata = util::getenv_path("LOCALAPPDATA");
 #else
-  const char* const env_xdg_cache_home = getenv("XDG_CACHE_HOME");
-  const char* const env_xdg_config_home = getenv("XDG_CONFIG_HOME");
+  auto env_xdg_cache_home = util::getenv_path("XDG_CACHE_HOME");
+  auto env_xdg_config_home = util::getenv_path("XDG_CONFIG_HOME");
 #endif
 
-  const char* env_ccache_configpath = getenv("CCACHE_CONFIGPATH");
+  auto env_ccache_configpath = util::getenv_path("CCACHE_CONFIGPATH");
   if (env_ccache_configpath) {
-    set_config_path(env_ccache_configpath);
+    set_config_path(*env_ccache_configpath);
   } else {
     // Only used for ccache tests:
-    const char* const env_ccache_configpath2 = getenv("CCACHE_CONFIGPATH2");
+    auto env_ccache_configpath2 = util::getenv_path("CCACHE_CONFIGPATH2");
 
     fs::path sysconfdir(k_sysconfdir);
 #ifdef _WIN32
-    if (const char* program_data = getenv("ALLUSERSPROFILE"))
-      sysconfdir = fs::path(program_data) / "ccache";
+    auto program_data = util::getenv_path("ALLUSERSPROFILE");
+    if (program_data) {
+      sysconfdir = *program_data / "ccache";
+    }
 #endif
 
-    set_system_config_path(env_ccache_configpath2 ? env_ccache_configpath2
+    set_system_config_path(env_ccache_configpath2 ? *env_ccache_configpath2
                                                   : sysconfdir / "ccache.conf");
     // A missing config file in SYSCONFDIR is OK so don't check return value.
     update_from_file(system_config_path());
 
-    const char* const env_ccache_dir = getenv("CCACHE_DIR");
+    auto env_ccache_dir = util::getenv_path("CCACHE_DIR");
     auto cmdline_cache_dir = cmdline_settings_map.find("cache_dir");
 
     fs::path config_dir;
     if (cmdline_cache_dir != cmdline_settings_map.end()) {
       config_dir = cmdline_cache_dir->second;
-    } else if (env_ccache_dir && *env_ccache_dir) {
-      config_dir = env_ccache_dir;
+    } else if (env_ccache_dir && !env_ccache_dir->empty()) {
+      config_dir = *env_ccache_dir;
     } else if (!cache_dir().empty() && !env_ccache_dir) {
       config_dir = cache_dir();
     } else if (legacy_ccache_dir_exists) {
       config_dir = legacy_ccache_dir;
 #ifdef _WIN32
     } else if (env_local_appdata
-               && DirEntry(
-                 make_path(env_local_appdata, "ccache", "ccache.conf"))) {
-      config_dir = make_path(env_local_appdata, "ccache");
-    } else if (env_appdata
-               && DirEntry(make_path(env_appdata, "ccache", "ccache.conf"))) {
-      config_dir = make_path(env_appdata, "ccache");
+               && fs::exists(*env_local_appdata / "ccache/ccache.conf")) {
+      config_dir = *env_local_appdata / "ccache";
+    } else if (env_appdata && fs::exists(*env_appdata / "ccache/ccache.conf")) {
+      config_dir = make_path(*env_appdata, "ccache");
     } else if (env_local_appdata) {
-      config_dir = make_path(env_local_appdata, "ccache");
+      config_dir = *env_local_appdata / "ccache";
     } else {
       throw core::Fatal(
         "could not find configuration file and the LOCALAPPDATA environment"
@@ -647,7 +649,7 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
     }
 #else
     } else if (env_xdg_config_home) {
-      config_dir = make_path(env_xdg_config_home, "ccache");
+      config_dir = *env_xdg_config_home / "ccache";
     } else {
       config_dir = default_config_dir(home_dir);
     }
@@ -672,7 +674,7 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
       set_cache_dir(legacy_ccache_dir);
 #ifdef _WIN32
     } else if (env_local_appdata) {
-      set_cache_dir(fs::path(env_local_appdata) / "ccache");
+      set_cache_dir(*env_local_appdata / "ccache");
     } else {
       throw core::Fatal(
         "could not find cache directory and the LOCALAPPDATA environment"
@@ -680,7 +682,7 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
     }
 #else
     } else if (env_xdg_cache_home) {
-      set_cache_dir(make_path(env_xdg_cache_home, "ccache"));
+      set_cache_dir(*env_xdg_cache_home / "ccache");
     } else {
       set_cache_dir(default_cache_dir(home_dir));
     }