1 // Copyright (C) 2015-2021 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
26 namespace fs
= std::filesystem
;
31 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
34 // Test non-existent path.
35 const auto p
= __gnu_test::nonexistent_path();
36 fs::recursive_directory_iterator
iter(p
, ec
);
38 VERIFY( iter
== end(iter
) );
40 // Test empty directory.
42 create_directory(p
, fs::current_path(), ec
);
45 iter
= fs::recursive_directory_iterator(p
, ec
);
47 VERIFY( iter
== end(iter
) );
49 // Test non-empty directory.
51 create_directories(p
/ "d1/d2", ec
);
54 iter
= fs::recursive_directory_iterator(p
, ec
);
56 VERIFY( iter
!= end(iter
) );
57 VERIFY( iter
->path() == p
/"d1" );
59 VERIFY( iter
->path() == p
/"d1/d2" );
61 VERIFY( iter
== end(iter
) );
63 #if ! (defined (__MINGW32__) || defined(__MINGW64__))
64 // Test inaccessible directory.
66 permissions(p
, fs::perms::none
, ec
);
68 iter
= fs::recursive_directory_iterator(p
, ec
);
70 VERIFY( iter
== end(iter
) );
72 // Test inaccessible directory, skipping permission denied.
73 const auto opts
= fs::directory_options::skip_permission_denied
;
74 iter
= fs::recursive_directory_iterator(p
, opts
, ec
);
76 VERIFY( iter
== end(iter
) );
78 // Test inaccessible sub-directory.
80 permissions(p
, fs::perms::owner_all
, ec
);
83 permissions(p
/"d1/d2", fs::perms::none
, ec
);
86 iter
= fs::recursive_directory_iterator(p
, ec
);
88 VERIFY( iter
!= end(iter
) );
89 VERIFY( iter
->path() == p
/"d1" );
90 ++iter
; // should recurse into d1
91 VERIFY( iter
!= end(iter
) );
92 VERIFY( iter
->path() == p
/"d1/d2" );
93 iter
.increment(ec
); // should fail to recurse into p/d1/d2
95 VERIFY( iter
== end(iter
) );
97 // Test inaccessible sub-directory, skipping permission denied.
99 iter
= fs::recursive_directory_iterator(p
, opts
, ec
);
101 VERIFY( iter
!= end(iter
) );
102 VERIFY( iter
->path() == p
/"d1" );
103 ++iter
; // should recurse into d1
104 VERIFY( iter
!= end(iter
) );
105 VERIFY( iter
->path() == p
/"d1/d2" );
107 iter
.increment(ec
); // should fail to recurse into p/d1/d2, so skip it
109 VERIFY( iter
== end(iter
) );
112 permissions(p
/"d1/d2", fs::perms::owner_all
, ec
);
119 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
121 const auto p
= __gnu_test::nonexistent_path();
123 create_directories(p
/ "d1/d2", ec
);
126 // Test post-increment (libstdc++/71005)
128 auto iter
= fs::recursive_directory_iterator(p
, ec
);
130 VERIFY( iter
!= end(iter
) );
131 const auto entry1
= *iter
;
132 const auto entry2
= *iter
++;
133 VERIFY( entry1
== entry2
);
134 VERIFY( entry1
.path() == p
/"d1" );
135 const auto entry3
= *iter
;
136 const auto entry4
= *iter
++;
137 VERIFY( entry3
== entry4
);
138 VERIFY( entry3
.path() == p
/"d1/d2" );
139 VERIFY( iter
== end(iter
) );
147 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
149 const auto p
= __gnu_test::nonexistent_path();
151 create_directories(p
/ "longer_than_small_string_buffer", ec
);
154 // Test for no reallocation on each dereference (this is a GNU extension)
155 auto iter
= fs::recursive_directory_iterator(p
, ec
);
156 const auto* s1
= iter
->path().c_str();
157 const auto* s2
= iter
->path().c_str();
167 const fs::recursive_directory_iterator it
;
168 VERIFY( it
== end(it
) );
174 auto p
= __gnu_test::nonexistent_path();
176 create_directory(p
/ "x");
177 fs::recursive_directory_iterator
it(p
), endit
;
178 VERIFY( begin(it
) == it
);
179 static_assert( noexcept(begin(it
)), "begin is noexcept" );
180 VERIFY( end(it
) == endit
);
181 static_assert( noexcept(end(it
)), "end is noexcept" );