From 2dc2f417288d4f0839b4bc01388e676ee343f941 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 1 Feb 2022 14:02:56 +0000 Subject: [PATCH] libstdc++: Add more tests for filesystem directory iterators The PR 97731 test was added to verify a fix to the Filesystem TS code, but we should also have the same test to avoid similar regressions in the C++17 std::filesystem code. Also add tests for directory_options::follow_directory_symlink libstdc++-v3/ChangeLog: * testsuite/27_io/filesystem/iterators/97731.cc: New test. * testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc: Check follow_directory_symlink option. * testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc: Likewise. --- .../testsuite/27_io/filesystem/iterators/97731.cc | 48 ++++++++++++++++++++++ .../iterators/recursive_directory_iterator.cc | 19 +++++++++ .../iterators/recursive_directory_iterator.cc | 21 +++++++++- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/27_io/filesystem/iterators/97731.cc diff --git a/libstdc++-v3/testsuite/27_io/filesystem/iterators/97731.cc b/libstdc++-v3/testsuite/27_io/filesystem/iterators/97731.cc new file mode 100644 index 0000000..9021e6e --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/filesystem/iterators/97731.cc @@ -0,0 +1,48 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++17 } } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +bool used_custom_readdir = false; + +extern "C" void* readdir(void*) +{ + used_custom_readdir = true; + errno = EIO; + return nullptr; +} + +void +test01() +{ + using std::filesystem::recursive_directory_iterator; + std::error_code ec; + recursive_directory_iterator it(".", ec); + if (used_custom_readdir) + VERIFY( ec.value() == EIO ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc b/libstdc++-v3/testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc index b0ccdfe..e67e2cf 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc @@ -184,6 +184,24 @@ test05() remove_all(p, ec); } +void +test06() +{ +#if !(defined __MINGW32__ || defined __MINGW64__) + auto p = __gnu_test::nonexistent_path(); + create_directories(p/"d1/d2"); + create_directory_symlink("d1", p/"link"); + fs::recursive_directory_iterator it(p), endit; + VERIFY( std::distance(it, endit) == 3 ); // d1 and d2 and link + + it = fs::recursive_directory_iterator(p, fs::directory_options::follow_directory_symlink); + VERIFY( std::distance(it, endit) == 4 ); // d1 and d1/d2 and link and link/d2 + + std::error_code ec; + remove_all(p, ec); +#endif +} + int main() { @@ -192,4 +210,5 @@ main() test03(); test04(); test05(); + test06(); } diff --git a/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc b/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc index 455dbf2..a201415 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc @@ -174,7 +174,7 @@ test05() { auto p = __gnu_test::nonexistent_path(); create_directory(p); - create_directory_symlink(p, p / "l"); + create_directory(p / "x"); fs::recursive_directory_iterator it(p), endit; VERIFY( begin(it) == it ); static_assert( noexcept(begin(it)), "begin is noexcept" ); @@ -185,6 +185,24 @@ test05() remove_all(p, ec); } +void +test06() +{ +#if !(defined __MINGW32__ || defined __MINGW64__) + auto p = __gnu_test::nonexistent_path(); + create_directories(p/"d1/d2"); + create_directory_symlink("d1", p/"link"); + fs::recursive_directory_iterator it(p), endit; + VERIFY( std::distance(it, endit) == 3 ); // d1 and d2 and link + + it = fs::recursive_directory_iterator(p, fs::directory_options::follow_directory_symlink); + VERIFY( std::distance(it, endit) == 4 ); // d1 and d1/d2 and link and link/d2 + + std::error_code ec; + remove_all(p, ec); +#endif +} + int main() { @@ -193,4 +211,5 @@ main() test03(); test04(); test05(); + test06(); } -- 2.7.4