[llvm] [Support][Windows] Use the original path if GetFinalPathNameByHandleW() failed (PR #87749)

Dmitry Vasilyev via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 5 00:16:39 PDT 2024


https://github.com/slydiman created https://github.com/llvm/llvm-project/pull/87749

The commit f11b056c (#76304) breaks `clang` and other tools if they are used from a RAMDrive. `GetFinalPathNameByHandleW()` may return 0 and GetLastError 0x28. This patch fixes that issue. Note `real_path()` uses `openFileForRead()` but it reports the error only if failed to open a file. Getting `RealPath` is optional functionality.

BTW, `sys::fs::real_path()` resolves not only symlinks, but also network drives and virtual drives created by the `subst` tool. It may break an automation. It is better to detect symlinks and resolve only symlinks.

>From 5fde0e8ab4cb58f431e9b99ab29c916921821d67 Mon Sep 17 00:00:00 2001
From: Dmitry Vasilyev <dvassiliev at accesssoftek.com>
Date: Fri, 5 Apr 2024 11:11:03 +0400
Subject: [PATCH] [Support][Windows] Use the original path if
 GetFinalPathNameByHandleW() failed

The commit f11b056c (#76304) breaks `clang` and other tools if they are used from a RAMDrive.
`GetFinalPathNameByHandleW()` may return 0 and GetLastError 0x28. This patch fixes that issue.
Note `real_path()` uses `openFileForRead()` but it reports the error only if failed to open a file. Getting `RealPath` is optional functionality.

BTW, `sys::fs::real_path()` resolves not only symlinks, but also network drives and virtual drives created by the `subst` tool. It may break an automation. It is better to detect symlinks and resolve only symlinks.
---
 llvm/lib/Support/Windows/Path.inc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 854d531ab371fc..4f0336a85daaa5 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -157,7 +157,9 @@ std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
 
   SmallString<256> RealPath;
   sys::fs::real_path(PathNameUTF8, RealPath);
-  return std::string(RealPath);
+  if (RealPath.size())
+    return std::string(RealPath);
+  return std::string(PathNameUTF8.data());
 }
 
 UniqueID file_status::getUniqueID() const {



More information about the llvm-commits mailing list