]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Correct debug dir handling on Windows (#955)
authorLuboš Luňák <l.lunak@centrum.cz>
Sat, 6 Nov 2021 15:28:01 +0000 (16:28 +0100)
committerGitHub <noreply@github.com>
Sat, 6 Nov 2021 15:28:01 +0000 (16:28 +0100)
src/ccache.cpp
src/util/path.cpp
src/util/path.hpp
unittest/test_util_path.cpp

index 2d8197f7baf0934316936155b674515ac28c3226..c32200f40982f012a4611f0527dbf11861f44c55 100644 (file)
@@ -170,13 +170,8 @@ prepare_debug_path(const std::string& debug_dir,
 {
   auto prefix = debug_dir.empty()
                   ? output_obj
-                  : debug_dir + util::to_absolute_path(output_obj);
-#ifdef _WIN32
-  prefix.erase(std::remove(prefix.begin(), prefix.end(), ':'), prefix.end());
-#endif
-  try {
-    Util::ensure_dir_exists(Util::dir_name(prefix));
-  } catch (core::Error&) {
+                  : debug_dir + util::to_absolute_path_no_drive(output_obj);
+  if (!Util::create_dir(Util::dir_name(prefix))) {
     // Ignore since we can't handle an error in another way in this context. The
     // caller takes care of logging when trying to open the path for writing.
   }
index 0648c2057e859b35c736b740ecc4e460b95c55cb..5468c47ff00e7a997ea17971a7bd2e3586cb127f 100644 (file)
@@ -58,4 +58,15 @@ to_absolute_path(nonstd::string_view path)
   }
 }
 
+std::string
+to_absolute_path_no_drive(nonstd::string_view path)
+{
+  std::string abs_path = to_absolute_path(path);
+#ifdef _WIN32
+  if (abs_path.length() >= 2 && abs_path[1] == ':')
+    abs_path.erase(0, 2);
+#endif
+  return abs_path;
+}
+
 } // namespace util
index 3e26251225a8dd9af56f6f14eb4033661e31f548..be5a999e30d5c78d58a17cfa877a31a9e0d5a953 100644 (file)
@@ -40,6 +40,9 @@ std::vector<std::string> split_path_list(nonstd::string_view path_list);
 // Make `path` an absolute path.
 std::string to_absolute_path(nonstd::string_view path);
 
+// Make `path` an absolute path, but do not include Windows drive.
+std::string to_absolute_path_no_drive(nonstd::string_view path);
+
 // --- Inline implementations ---
 
 inline bool
index ea3f3359030c223ad34f114836fe992c586e333c..b069041cc3e8d6f12ca21937c7cbb02cdace8760 100644 (file)
@@ -94,3 +94,24 @@ TEST_CASE("util::to_absolute_path")
   CHECK(util::to_absolute_path("../foo/bar")
         == FMT("{}/foo/bar", Util::dir_name(cwd)));
 }
+
+TEST_CASE("util::to_absolute_path_no_drive")
+{
+  CHECK(util::to_absolute_path_no_drive("/foo/bar") == "/foo/bar");
+
+#ifdef _WIN32
+  CHECK(util::to_absolute_path_no_drive("C:\\foo\\bar") == "\\foo\\bar");
+#endif
+
+  auto cwd = Util::get_actual_cwd();
+#ifdef _WIN32
+  cwd = cwd.substr(2);
+#endif
+
+  CHECK(util::to_absolute_path_no_drive("") == cwd);
+  CHECK(util::to_absolute_path_no_drive(".") == cwd);
+  CHECK(util::to_absolute_path_no_drive("..") == Util::dir_name(cwd));
+  CHECK(util::to_absolute_path_no_drive("foo") == FMT("{}/foo", cwd));
+  CHECK(util::to_absolute_path_no_drive("../foo/bar")
+        == FMT("{}/foo/bar", Util::dir_name(cwd)));
+}