1 // Copyright (C) 2015-2017 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 -lstdc++fs" }
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 // Test inaccessible directory.
65 permissions(p
, fs::perms::none
, ec
);
67 iter
= fs::recursive_directory_iterator(p
, ec
);
69 VERIFY( iter
== end(iter
) );
71 // Test inaccessible directory, skipping permission denied.
72 const auto opts
= fs::directory_options::skip_permission_denied
;
73 iter
= fs::recursive_directory_iterator(p
, opts
, ec
);
75 VERIFY( iter
== end(iter
) );
77 // Test inaccessible sub-directory.
79 permissions(p
, fs::perms::owner_all
, ec
);
82 permissions(p
/"d1/d2", fs::perms::none
, ec
);
85 iter
= fs::recursive_directory_iterator(p
, ec
);
87 VERIFY( iter
!= end(iter
) );
88 VERIFY( iter
->path() == p
/"d1" );
89 ++iter
; // should recurse into d1
90 VERIFY( iter
!= end(iter
) );
91 VERIFY( iter
->path() == p
/"d1/d2" );
92 iter
.increment(ec
); // should fail to recurse into p/d1/d2
94 VERIFY( iter
== end(iter
) );
96 // Test inaccessible sub-directory, skipping permission denied.
98 iter
= fs::recursive_directory_iterator(p
, opts
, ec
);
100 VERIFY( iter
!= end(iter
) );
101 VERIFY( iter
->path() == p
/"d1" );
102 ++iter
; // should recurse into d1
103 VERIFY( iter
!= end(iter
) );
104 VERIFY( iter
->path() == p
/"d1/d2" );
106 iter
.increment(ec
); // should fail to recurse into p/d1/d2, so skip it
108 VERIFY( iter
== end(iter
) );
110 permissions(p
/"d1/d2", fs::perms::owner_all
, ec
);
117 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
119 const auto p
= __gnu_test::nonexistent_path();
121 create_directories(p
/ "d1/d2", ec
);
124 // Test post-increment (libstdc++/71005)
126 auto iter
= fs::recursive_directory_iterator(p
, ec
);
128 VERIFY( iter
!= end(iter
) );
129 const auto entry1
= *iter
;
130 const auto entry2
= *iter
++;
131 VERIFY( entry1
== entry2
);
132 VERIFY( entry1
.path() == p
/"d1" );
133 const auto entry3
= *iter
;
134 const auto entry4
= *iter
++;
135 VERIFY( entry3
== entry4
);
136 VERIFY( entry3
.path() == p
/"d1/d2" );
137 VERIFY( iter
== end(iter
) );
145 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
147 const auto p
= __gnu_test::nonexistent_path();
149 create_directories(p
/ "longer_than_small_string_buffer", ec
);
152 // Test for no reallocation on each dereference (this is a GNU extension)
153 auto iter
= fs::recursive_directory_iterator(p
, ec
);
154 const auto* s1
= iter
->path().c_str();
155 const auto* s2
= iter
->path().c_str();
165 const fs::recursive_directory_iterator it
;
166 VERIFY( it
== end(it
) );
172 auto p
= __gnu_test::nonexistent_path();
174 create_directory_symlink(p
, p
/ "l");
175 fs::recursive_directory_iterator
it(p
), endit
;
176 VERIFY( begin(it
) == it
);
177 static_assert( noexcept(begin(it
)), "begin is noexcept" );
178 VERIFY( end(it
) == endit
);
179 static_assert( noexcept(end(it
)), "end is noexcept" );