aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-12-30 13:08:41 +0000
committerJonathan Wakely <redi@gcc.gnu.org>2025-01-16 09:41:00 +0000
commit301a961ffd0567eece55ece42e80a7ba9e855ba0 (patch)
tree1b59a14bae6c8771314198edbb3ed2bb253ce63b
parent408f5b847b5b4e552274dc7b02ccaf106395936d (diff)
downloadgcc-301a961ffd0567eece55ece42e80a7ba9e855ba0.zip
gcc-301a961ffd0567eece55ece42e80a7ba9e855ba0.tar.gz
gcc-301a961ffd0567eece55ece42e80a7ba9e855ba0.tar.bz2
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.
-rw-r--r--libstdc++-v3/src/c++17/fs_ops.cc22
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/operations/pr118158.cc62
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 1d75f24..4f18815 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 0000000..b57a2d1
--- /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();
+}