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.
This commit is contained in:
Jonathan Wakely 2024-12-30 13:08:41 +00:00 committed by Jonathan Wakely
parent 408f5b847b
commit 301a961ffd
No known key found for this signature in database
2 changed files with 69 additions and 15 deletions

View file

@ -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);

View file

@ -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();
}