https://gcc.gnu.org/g:a66d9754b8779446214853c86af41d74577da14a

commit r14-11353-ga66d9754b8779446214853c86af41d74577da14a
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Mon Dec 30 13:08:41 2024 +0000

    libstdc++: Implement LWG 2937 for std::filesystem::equivalent [PR118158]
    
    Do not report an error for (is_other(s1) && is_other(s2)) as the
    standard originally said, nor for (is_other(s1) || is_other(s2)) as
    libstdc++ was doing. We can compare inode numbers for special files and
    so give sensible answers.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/118158
            * src/c++17/fs_ops.cc (fs::equivalent): Remove error reporting
            for is_other(s1) && is_other(s2) case, as per LWG 2937.
            * testsuite/27_io/filesystem/operations/pr118158.cc: New test.
    
    (cherry picked from commit 301a961ffd0567eece55ece42e80a7ba9e855ba0)

Diff:
---
 libstdc++-v3/src/c++17/fs_ops.cc                   | 22 +++-----
 .../27_io/filesystem/operations/pr118158.cc        | 62 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 15 deletions(-)

diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc
index 946fefd9e449..53c8b6d445ab 100644
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -914,24 +914,16 @@ fs::equivalent(const path& p1, const path& p2, 
error_code& ec) noexcept
   else
     err = errno;
 
-  if (exists(s1) && exists(s2))
-    {
-      if (is_other(s1) && is_other(s2))
-       {
-         ec = std::__unsupported();
-         return false;
-       }
-      ec.clear();
-      if (is_other(s1) || is_other(s2))
-       return false;
-      return fs::equiv_files(p1.c_str(), st1, p2.c_str(), st2, ec);
-    }
+  if (err)
+    ec.assign(err, std::generic_category());
   else if (!exists(s1) || !exists(s2))
     ec = std::make_error_code(std::errc::no_such_file_or_directory);
-  else if (err)
-    ec.assign(err, std::generic_category());
   else
-    ec.clear();
+    {
+      ec.clear();
+      if (s1.type() == s2.type())
+       return fs::equiv_files(p1.c_str(), st1, p2.c_str(), st2, ec);
+    }
   return false;
 #else
   ec = std::make_error_code(std::errc::function_not_supported);
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/pr118158.cc 
b/libstdc++-v3/testsuite/27_io/filesystem/operations/pr118158.cc
new file mode 100644
index 000000000000..b57a2d184f41
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/pr118158.cc
@@ -0,0 +1,62 @@
+// { dg-do run { target c++17 } }
+// { dg-require-filesystem-ts "" }
+
+#include <filesystem>
+#include <testsuite_fs.h>
+#include <testsuite_hooks.h>
+
+#if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H)
+# include <sys/types.h>
+# include <sys/stat.h>  // mkfifo
+#endif
+
+namespace fs = std::filesystem;
+
+void
+test_pr118158()
+{
+#if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
+  && defined(S_IWUSR) && defined(S_IRUSR)
+  auto p1 = __gnu_test::nonexistent_path();
+  auto p2 = __gnu_test::nonexistent_path();
+  auto p3 = __gnu_test::nonexistent_path();
+  const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
+  std::error_code ec;
+  bool result;
+
+  VERIFY( ! ::mkfifo(p1.c_str(), S_IWUSR | S_IRUSR) );
+  __gnu_test::scoped_file f1(p1, __gnu_test::scoped_file::adopt_file);
+
+  // Special file is equivalent to itself.
+  VERIFY( equivalent(p1, p1) );
+  VERIFY( equivalent(p1, p1, ec) );
+  VERIFY( ! ec );
+
+  VERIFY( ! ::mkfifo(p2.c_str(), S_IWUSR | S_IRUSR) );
+  __gnu_test::scoped_file f2(p2, __gnu_test::scoped_file::adopt_file);
+
+  ec = bad_ec;
+  // Distinct special files are not equivalent.
+  VERIFY( ! equivalent(p1, p2, ec) );
+  VERIFY( ! ec );
+
+  // Non-existent paths are always an error.
+  VERIFY( ! equivalent(p1, p3, ec) );
+  VERIFY( ec == std::errc::no_such_file_or_directory );
+  ec = bad_ec;
+  VERIFY( ! equivalent(p3, p2, ec) );
+  VERIFY( ec == std::errc::no_such_file_or_directory );
+
+  // Special file is not equivalent to regular file.
+  __gnu_test::scoped_file f3(p3);
+  ec = bad_ec;
+  VERIFY( ! equivalent(p1, p3, ec) );
+  VERIFY( ! ec );
+#endif
+}
+
+int
+main()
+{
+  test_pr118158();
+}

Reply via email to