[libc++] [P1164] [C++20] Make fs::create_directory() error if there is already a non-directory.

Also mark LWG2935 and LWG3079 as complete.

Applied retroactively to previous standards too, as it's a DR.

* https://wg21.link/P1164
* https://wg21.link/lwg2935
* https://wg21.link/lwg3079

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D92769

GitOrigin-RevId: e4ed349c765827a824cb38ec6ef3447263b768cf
diff --git a/src/filesystem/operations.cpp b/src/filesystem/operations.cpp
index 6a25979..fb27d54 100644
--- a/src/filesystem/operations.cpp
+++ b/src/filesystem/operations.cpp
@@ -849,8 +849,17 @@
 
   if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
     return true;
-  if (errno != EEXIST)
+
+  if (errno == EEXIST) {
+    error_code mec = capture_errno();
+    error_code ignored_ec;
+    const file_status st = status(p, ignored_ec);
+    if (!is_directory(st)) {
+      err.report(mec);
+    }
+  } else {
     err.report(capture_errno());
+  }
   return false;
 }
 
@@ -868,8 +877,17 @@
 
   if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
     return true;
-  if (errno != EEXIST)
+
+  if (errno == EEXIST) {
+    error_code mec = capture_errno();
+    error_code ignored_ec;
+    const file_status st = status(p, ignored_ec);
+    if (!is_directory(st)) {
+      err.report(mec);
+    }
+  } else {
     err.report(capture_errno());
+  }
   return false;
 }