[llvm] [Windows][Support] Add helper to expand short 8.3 form paths (PR #178480)
Ben Dunbobbin via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 9 16:52:50 PST 2026
================
@@ -2485,6 +2486,145 @@ TEST_F(FileSystemTest, widenPath) {
#endif
#ifdef _WIN32
+/// Checks whether short 8.3 form names are enabled in the given UTF-8 path.
+static llvm::Expected<bool> areShortNamesEnabled(llvm::StringRef Path8) {
+ // Create a directory under Path8 with a name long enough that Windows will
+ // provide a short 8.3 form name, if short 8.3 form names are enabled.
+ SmallString<256> Dir(Path8);
+ path::append(Dir, "verylongdir");
+ if (std::error_code EC = fs::create_directories(Dir))
+ return llvm::errorCodeToError(EC);
+ scope_exit Close([&] { fs::remove_directories(Dir); });
+
+ SmallVector<wchar_t, MAX_PATH> Path16;
+ if (std::error_code EC = sys::windows::widenPath(Dir, Path16))
+ return llvm::errorCodeToError(EC);
+
+ WIN32_FIND_DATAW Data;
+ HANDLE H = ::FindFirstFileW(Path16.data(), &Data);
+ if (H == INVALID_HANDLE_VALUE)
+ return llvm::make_error<llvm::StringError>(
+ "FindFirstFileW returned invalid handle",
+ llvm::inconvertibleErrorCode());
+ ::FindClose(H);
+
+ return (Data.cAlternateFileName[0] != L'\0');
----------------
bd1976bris wrote:
Sorry for misinterpreting! We shouldn’t need to worry about `.` or `..` here. The MS docs state that `FindFirstFileW` returns information about the single file or directory that matches the given path when no wildcards are present. Passing the directory path itself with no wildcards returns metadata for that directory, not an enumeration of its contents. There's an example given: _For example, an argument of C:\Windows returns information about the directory C:\Windows, not about a directory or file in C:\Windows._
While looking again at the `FindFirstFileW` documentation, I did notice that the string must be null-terminated. I modified the code to ensure that.
https://github.com/llvm/llvm-project/pull/178480
More information about the llvm-commits
mailing list