https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118003

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Changing directory does work:

--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -1327,8 +1327,22 @@ fs::remove(const path& p, error_code& ec) noexcept
 std::uintmax_t
 fs::remove_all(const path& p)
 {
-  error_code ec;
   uintmax_t count = 0;
+#if _GLIBCXX_FILESYSTEM_IS_WINDOWS
+  if (is_directory(p))
+    {
+      struct Guard
+      {
+       fs::path cwd = fs::current_path();
+       ~Guard() { fs::current_path(cwd); }
+      } g;
+      fs::current_path(p);
+      directory_iterator end;
+      for (directory_iterator dir(L"."); dir != end; ++dir)
+       count += fs::remove_all(*dir);
+    }
+#else
+  error_code ec;
   recursive_directory_iterator dir(p, directory_options{64|128}, ec);
   switch (ec.value()) // N.B. assumes ec.category() == std::generic_category()
   {
@@ -1363,6 +1377,7 @@ fs::remove_all(const path& p)
     // An error occurred.
     _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p, ec));
   }
+#endif

   // Remove p itself, which is either a non-directory or is now empty.
   return count + fs::remove(p);
@@ -1372,6 +1387,22 @@ std::uintmax_t
 fs::remove_all(const path& p, error_code& ec)
 {
   uintmax_t count = 0;
+#if _GLIBCXX_FILESYSTEM_IS_WINDOWS
+  if (is_directory(p, ec))
+    {
+      struct Guard
+      {
+       fs::path cwd = fs::current_path();
+       ~Guard() { fs::current_path(cwd); }
+      } g;
+      fs::current_path(p);
+      directory_iterator end;
+      for (directory_iterator dir(L".", ec); !ec && dir != end;
dir.increment(ec))
+       count += fs::remove_all(*dir, ec);
+    }
+  else if (ec)
+    return -1;
+#else
   recursive_directory_iterator dir(p, directory_options{64|128}, ec);
   switch (ec.value()) // N.B. assumes ec.category() == std::generic_category()
   {
@@ -1409,6 +1440,7 @@ fs::remove_all(const path& p, error_code& ec)
     // An error occurred.
     return -1;
   }
+#endif

   // Remove p itself, which is either a non-directory or is now empty.
   if (int last = fs::remove(p, ec); !ec)

Reply via email to