| 1 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++17 } } |
| 19 | // { dg-require-filesystem-ts "" } |
| 20 | |
| 21 | #include <filesystem> |
| 22 | #include <testsuite_hooks.h> |
| 23 | #include <testsuite_fs.h> |
| 24 | |
| 25 | namespace fs = std::filesystem; |
| 26 | |
| 27 | __gnu_test::scoped_file |
| 28 | create_dir(fs::path dir = __gnu_test::nonexistent_path()) |
| 29 | { |
| 30 | fs::create_directory(dir); |
| 31 | return { dir, __gnu_test::scoped_file::adopt_file }; |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | test01() |
| 36 | { |
| 37 | const auto testdir = create_dir(); |
| 38 | __gnu_test::scoped_file file(testdir.path / "file"); |
| 39 | |
| 40 | fs::recursive_directory_iterator r(testdir.path); |
| 41 | VERIFY( r.recursion_pending() ); |
| 42 | |
| 43 | r.disable_recursion_pending(); |
| 44 | VERIFY( !r.recursion_pending() ); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | test02() |
| 49 | { |
| 50 | const auto testdir = create_dir(); |
| 51 | __gnu_test::scoped_file file(testdir.path / "file"); |
| 52 | |
| 53 | fs::recursive_directory_iterator r(testdir.path); |
| 54 | VERIFY( r.recursion_pending() ); |
| 55 | const auto r2 = r; |
| 56 | // recursion pending flag should be copied: |
| 57 | VERIFY( r2.recursion_pending() == r.recursion_pending() ); |
| 58 | |
| 59 | r.disable_recursion_pending(); |
| 60 | VERIFY( !r.recursion_pending() ); |
| 61 | const auto r3 = r; |
| 62 | // recursion pending flag should be copied: |
| 63 | VERIFY( r3.recursion_pending() == r.recursion_pending() ); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | test03() |
| 68 | { |
| 69 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
| 70 | |
| 71 | const auto testdir = create_dir(); |
| 72 | __gnu_test::scoped_file file1(testdir.path / "file1"); |
| 73 | __gnu_test::scoped_file file2(testdir.path / "file2"); |
| 74 | __gnu_test::scoped_file file3(testdir.path / "file3"); |
| 75 | __gnu_test::scoped_file file4(testdir.path / "file4"); |
| 76 | |
| 77 | fs::recursive_directory_iterator r(testdir.path); |
| 78 | r.disable_recursion_pending(); |
| 79 | VERIFY( !r.recursion_pending() ); |
| 80 | ++r; |
| 81 | // recursion pending flag should be true after incrementing: |
| 82 | VERIFY( r.recursion_pending() ); |
| 83 | |
| 84 | r.disable_recursion_pending(); |
| 85 | VERIFY( !r.recursion_pending() ); |
| 86 | r.increment(ec); |
| 87 | VERIFY( !ec ); |
| 88 | // recursion pending flag should be true after incrementing: |
| 89 | VERIFY( r.recursion_pending() ); |
| 90 | |
| 91 | r.disable_recursion_pending(); |
| 92 | VERIFY( !r.recursion_pending() ); |
| 93 | r++; |
| 94 | // recursion pending flag should be true after post-incrementing: |
| 95 | VERIFY( r.recursion_pending() ); |
| 96 | |
| 97 | VERIFY( ++r == fs::recursive_directory_iterator() ); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | test04() |
| 102 | { |
| 103 | const auto testdir = create_dir(); |
| 104 | const auto sub1 = create_dir(testdir.path/"sub1"); |
| 105 | __gnu_test::scoped_file file1(sub1.path / "file"); |
| 106 | const auto sub2 = create_dir(testdir.path/"sub2"); |
| 107 | __gnu_test::scoped_file file2(sub2.path / "file"); |
| 108 | |
| 109 | fs::recursive_directory_iterator r(testdir.path); |
| 110 | ++r; |
| 111 | r.pop(); |
| 112 | // recursion pending flag should be true after popping: |
| 113 | VERIFY( r.recursion_pending() ); |
| 114 | |
| 115 | // and recursion should actually happen: |
| 116 | ++r; |
| 117 | VERIFY( r.depth() == 1 ); |
| 118 | VERIFY( r->is_regular_file() ); |
| 119 | // recursion pending flag should still be true: |
| 120 | VERIFY( r.recursion_pending() ); |
| 121 | |
| 122 | r = fs::recursive_directory_iterator(testdir.path); |
| 123 | r.disable_recursion_pending(); |
| 124 | ++r; |
| 125 | // when recursion is disabled, should not enter subdirectories: |
| 126 | VERIFY( r.depth() == 0 ); |
| 127 | r.disable_recursion_pending(); |
| 128 | ++r; |
| 129 | VERIFY( r == fs::recursive_directory_iterator() ); |
| 130 | } |
| 131 | |
| 132 | int main() |
| 133 | { |
| 134 | test01(); |
| 135 | test02(); |
| 136 | test03(); |
| 137 | test04(); |
| 138 | } |