[libcxx-commits] [PATCH] D91170: [15/N] [libcxx] Implement the canonical function for windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 4 04:07:21 PST 2020


mstorsjo updated this revision to Diff 309509.
mstorsjo added a comment.

Check the return value of GetFinalPathNameFromHandleW if it overflowed the buffer size and handle it appropriately.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91170/new/

https://reviews.llvm.org/D91170

Files:
  libcxx/src/filesystem/operations.cpp


Index: libcxx/src/filesystem/operations.cpp
===================================================================
--- libcxx/src/filesystem/operations.cpp
+++ libcxx/src/filesystem/operations.cpp
@@ -859,6 +859,32 @@
   if (hold.get() == nullptr)
     return err.report(capture_errno());
   return {hold.get()};
+#elif defined(_LIBCPP_WIN32API)
+  size_t buff_size = MAX_PATH + 10;
+  wchar_t stack_buff[buff_size];
+  std::unique_ptr<wchar_t[]> dyn_buff;
+  wchar_t *buff = stack_buff;
+  detail::WinHandle h(p.c_str(), FILE_READ_ATTRIBUTES, 0);
+  if (!h)
+    return err.report(detail::make_windows_error(GetLastError()));
+  DWORD retval = GetFinalPathNameByHandleW(
+      h, buff, buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+  if (retval > buff_size) {
+    buff_size = retval;
+    dyn_buff = std::unique_ptr<wchar_t[]>(new wchar_t[buff_size]);
+    buff = dyn_buff.get();
+    retval = GetFinalPathNameByHandleW(h, buff, buff_size,
+                                       FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+  }
+  if (!retval)
+    return err.report(detail::make_windows_error(GetLastError()));
+  if (!wcsncmp(buff, L"\\\\?\\", 4)) {
+    if (buff[5] == ':') // \\?\X: -> X:
+      return {&buff[4]};
+    if (!wcsncmp(&buff[4], L"UNC\\", 4)) // \\?\UNC\server -> \\server
+      return {std::wstring(L"\\\\") + &buff[8]};
+  }
+  return {buff};
 #else
   char buff[PATH_MAX + 1];
   char* ret;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91170.309509.patch
Type: text/x-patch
Size: 1418 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201204/21189846/attachment.bin>


More information about the libcxx-commits mailing list