[libcxx-commits] [PATCH] D91168: [13/N] [libcxx] Implement the space function for windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 13 03:44:41 PST 2020


mstorsjo updated this revision to Diff 305081.
mstorsjo added a reviewer: amccarth.
mstorsjo added a comment.

Rebased on top of updated patches.


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

https://reviews.llvm.org/D91168

Files:
  libcxx/src/filesystem/operations.cpp


Index: libcxx/src/filesystem/operations.cpp
===================================================================
--- libcxx/src/filesystem/operations.cpp
+++ libcxx/src/filesystem/operations.cpp
@@ -1489,6 +1489,7 @@
 space_info __space(const path& p, error_code* ec) {
   ErrorHandler<void> err("space", ec, &p);
   space_info si;
+#if !defined(_LIBCPP_WIN32API)
   struct statvfs m_svfs = {};
   if (::statvfs(p.c_str(), &m_svfs) == -1) {
     err.report(capture_errno());
@@ -1504,6 +1505,34 @@
   do_mult(si.capacity, m_svfs.f_blocks);
   do_mult(si.free, m_svfs.f_bfree);
   do_mult(si.available, m_svfs.f_bavail);
+#else
+  ULARGE_INTEGER free_bytes_available_to_caller, total_number_of_bytes,
+      total_number_of_free_bytes;
+  path dir = p;
+  while (true) {
+    error_code local_ec;
+    const file_status st = status(dir, local_ec);
+    if (!exists(st) || is_directory(st))
+      break;
+    path parent = dir.parent_path();
+    if (parent == dir) {
+      err.report(make_error_code(errc::no_such_file_or_directory));
+      si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
+      return si;
+    }
+    dir = parent;
+  }
+  if (!GetDiskFreeSpaceExW(dir.c_str(), &free_bytes_available_to_caller,
+                           &total_number_of_bytes,
+                           &total_number_of_free_bytes)) {
+    err.report(detail::make_windows_error(GetLastError()));
+    si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
+    return si;
+  }
+  si.capacity = total_number_of_bytes.QuadPart;
+  si.free = total_number_of_free_bytes.QuadPart;
+  si.available = free_bytes_available_to_caller.QuadPart;
+#endif
   return si;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91168.305081.patch
Type: text/x-patch
Size: 1683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201113/3ce1ff3f/attachment-0001.bin>


More information about the libcxx-commits mailing list