1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03
10
11 // <filesystem>
12
13 // class recursive_directory_iterator
14
15 // recursive_directory_iterator& operator++();
16 // recursive_directory_iterator& increment(error_code& ec) noexcept;
17
18 #include "filesystem_include.h"
19 #include <type_traits>
20 #include <set>
21 #include <cassert>
22
23 #include "test_macros.h"
24 #include "rapid-cxx-test.h"
25 #include "filesystem_test_helper.h"
26
27 using namespace fs;
28
29 TEST_SUITE(recursive_directory_iterator_increment_tests)
30
TEST_CASE(test_increment_signatures)31 TEST_CASE(test_increment_signatures)
32 {
33 recursive_directory_iterator d; ((void)d);
34 std::error_code ec; ((void)ec);
35
36 ASSERT_SAME_TYPE(decltype(++d), recursive_directory_iterator&);
37 ASSERT_NOT_NOEXCEPT(++d);
38
39 ASSERT_SAME_TYPE(decltype(d.increment(ec)), recursive_directory_iterator&);
40 ASSERT_NOT_NOEXCEPT(d.increment(ec));
41 }
42
TEST_CASE(test_prefix_increment)43 TEST_CASE(test_prefix_increment)
44 {
45 static_test_env static_env;
46 const path testDir = static_env.Dir;
47 const std::set<path> dir_contents(static_env.RecDirIterationList.begin(),
48 static_env.RecDirIterationList.end());
49 const recursive_directory_iterator endIt{};
50
51 std::error_code ec;
52 recursive_directory_iterator it(testDir, ec);
53 TEST_REQUIRE(!ec);
54
55 std::set<path> unseen_entries = dir_contents;
56 while (!unseen_entries.empty()) {
57 TEST_REQUIRE(it != endIt);
58 const path entry = *it;
59 TEST_REQUIRE(unseen_entries.erase(entry) == 1);
60 recursive_directory_iterator& it_ref = ++it;
61 TEST_CHECK(&it_ref == &it);
62 }
63
64 TEST_CHECK(it == endIt);
65 }
66
TEST_CASE(test_postfix_increment)67 TEST_CASE(test_postfix_increment)
68 {
69 static_test_env static_env;
70 const path testDir = static_env.Dir;
71 const std::set<path> dir_contents(static_env.RecDirIterationList.begin(),
72 static_env.RecDirIterationList.end());
73 const recursive_directory_iterator endIt{};
74
75 std::error_code ec;
76 recursive_directory_iterator it(testDir, ec);
77 TEST_REQUIRE(!ec);
78
79 std::set<path> unseen_entries = dir_contents;
80 while (!unseen_entries.empty()) {
81 TEST_REQUIRE(it != endIt);
82 const path entry = *it;
83 TEST_REQUIRE(unseen_entries.erase(entry) == 1);
84 const path entry2 = *it++;
85 TEST_CHECK(entry2 == entry);
86 }
87 TEST_CHECK(it == endIt);
88 }
89
90
TEST_CASE(test_increment_method)91 TEST_CASE(test_increment_method)
92 {
93 static_test_env static_env;
94 const path testDir = static_env.Dir;
95 const std::set<path> dir_contents(static_env.RecDirIterationList.begin(),
96 static_env.RecDirIterationList.end());
97 const recursive_directory_iterator endIt{};
98
99 std::error_code ec;
100 recursive_directory_iterator it(testDir, ec);
101 TEST_REQUIRE(!ec);
102
103 std::set<path> unseen_entries = dir_contents;
104 while (!unseen_entries.empty()) {
105 TEST_REQUIRE(it != endIt);
106 const path entry = *it;
107 TEST_REQUIRE(unseen_entries.erase(entry) == 1);
108 recursive_directory_iterator& it_ref = it.increment(ec);
109 TEST_REQUIRE(!ec);
110 TEST_CHECK(&it_ref == &it);
111 }
112
113 TEST_CHECK(it == endIt);
114 }
115
TEST_CASE(test_follow_symlinks)116 TEST_CASE(test_follow_symlinks)
117 {
118 static_test_env static_env;
119 const path testDir = static_env.Dir;
120 auto const& IterList = static_env.RecDirFollowSymlinksIterationList;
121
122 const std::set<path> dir_contents(IterList.begin(), IterList.end());
123 const recursive_directory_iterator endIt{};
124
125 std::error_code ec;
126 recursive_directory_iterator it(testDir,
127 directory_options::follow_directory_symlink, ec);
128 TEST_REQUIRE(!ec);
129
130 std::set<path> unseen_entries = dir_contents;
131 while (!unseen_entries.empty()) {
132 TEST_REQUIRE(it != endIt);
133 const path entry = *it;
134
135 TEST_REQUIRE(unseen_entries.erase(entry) == 1);
136 recursive_directory_iterator& it_ref = it.increment(ec);
137 TEST_REQUIRE(!ec);
138 TEST_CHECK(&it_ref == &it);
139 }
140 TEST_CHECK(it == endIt);
141 }
142
143 // Windows doesn't support setting perms::none to trigger failures
144 // reading directories.
145 #ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
TEST_CASE(access_denied_on_recursion_test_case)146 TEST_CASE(access_denied_on_recursion_test_case)
147 {
148 using namespace fs;
149 scoped_test_env env;
150 const path testFiles[] = {
151 env.create_dir("dir1"),
152 env.create_dir("dir1/dir2"),
153 env.create_file("dir1/dir2/file1"),
154 env.create_file("dir1/file2")
155 };
156 const path startDir = testFiles[0];
157 const path permDeniedDir = testFiles[1];
158 const path otherFile = testFiles[3];
159 auto SkipEPerm = directory_options::skip_permission_denied;
160
161 // Change the permissions so we can no longer iterate
162 permissions(permDeniedDir, perms::none);
163
164 const recursive_directory_iterator endIt;
165
166 // Test that recursion resulting in a "EACCESS" error is not ignored
167 // by default.
168 {
169 std::error_code ec = GetTestEC();
170 recursive_directory_iterator it(startDir, ec);
171 TEST_REQUIRE(ec != GetTestEC());
172 TEST_REQUIRE(!ec);
173 while (it != endIt && it->path() != permDeniedDir)
174 ++it;
175 TEST_REQUIRE(it != endIt);
176 TEST_REQUIRE(*it == permDeniedDir);
177
178 it.increment(ec);
179 TEST_CHECK(ec);
180 TEST_CHECK(it == endIt);
181 }
182 // Same as above but test operator++().
183 {
184 std::error_code ec = GetTestEC();
185 recursive_directory_iterator it(startDir, ec);
186 TEST_REQUIRE(!ec);
187 while (it != endIt && it->path() != permDeniedDir)
188 ++it;
189 TEST_REQUIRE(it != endIt);
190 TEST_REQUIRE(*it == permDeniedDir);
191
192 TEST_REQUIRE_THROW(filesystem_error, ++it);
193 }
194 // Test that recursion resulting in a "EACCESS" error is ignored when the
195 // correct options are given to the constructor.
196 {
197 std::error_code ec = GetTestEC();
198 recursive_directory_iterator it(startDir, SkipEPerm, ec);
199 TEST_REQUIRE(!ec);
200 TEST_REQUIRE(it != endIt);
201
202 bool seenOtherFile = false;
203 if (*it == otherFile) {
204 ++it;
205 seenOtherFile = true;
206 TEST_REQUIRE (it != endIt);
207 }
208 TEST_REQUIRE(*it == permDeniedDir);
209
210 ec = GetTestEC();
211 it.increment(ec);
212 TEST_REQUIRE(!ec);
213
214 if (seenOtherFile) {
215 TEST_CHECK(it == endIt);
216 } else {
217 TEST_CHECK(it != endIt);
218 TEST_CHECK(*it == otherFile);
219 }
220 }
221 // Test that construction resulting in a "EACCESS" error is not ignored
222 // by default.
223 {
224 std::error_code ec;
225 recursive_directory_iterator it(permDeniedDir, ec);
226 TEST_REQUIRE(ec);
227 TEST_REQUIRE(it == endIt);
228 }
229 // Same as above but testing the throwing constructors
230 {
231 TEST_REQUIRE_THROW(filesystem_error,
232 recursive_directory_iterator(permDeniedDir));
233 }
234 // Test that construction resulting in a "EACCESS" error constructs the
235 // end iterator when the correct options are given.
236 {
237 std::error_code ec = GetTestEC();
238 recursive_directory_iterator it(permDeniedDir, SkipEPerm, ec);
239 TEST_REQUIRE(!ec);
240 TEST_REQUIRE(it == endIt);
241 }
242 }
243
244 // See llvm.org/PR35078
TEST_CASE(test_PR35078)245 TEST_CASE(test_PR35078)
246 {
247 using namespace fs;
248 scoped_test_env env;
249 const path testFiles[] = {
250 env.create_dir("dir1"),
251 env.create_dir("dir1/dir2"),
252 env.create_dir("dir1/dir2/dir3"),
253 env.create_file("dir1/file1"),
254 env.create_file("dir1/dir2/dir3/file2")
255 };
256 const path startDir = testFiles[0];
257 const path permDeniedDir = testFiles[1];
258 const path nestedDir = testFiles[2];
259 const path nestedFile = testFiles[3];
260
261 // Change the permissions so we can no longer iterate
262 permissions(permDeniedDir,
263 perms::group_exec|perms::owner_exec|perms::others_exec,
264 perm_options::remove);
265
266 const std::errc eacess = std::errc::permission_denied;
267 std::error_code ec = GetTestEC();
268
269 const recursive_directory_iterator endIt;
270
271 auto SetupState = [&](bool AllowEAccess, bool& SeenFile3) {
272 SeenFile3 = false;
273 auto Opts = AllowEAccess ? directory_options::skip_permission_denied
274 : directory_options::none;
275 recursive_directory_iterator it(startDir, Opts, ec);
276 while (!ec && it != endIt && *it != nestedDir) {
277 if (*it == nestedFile)
278 SeenFile3 = true;
279 it.increment(ec);
280 }
281 return it;
282 };
283
284 {
285 bool SeenNestedFile = false;
286 recursive_directory_iterator it = SetupState(false, SeenNestedFile);
287 TEST_REQUIRE(it != endIt);
288 TEST_REQUIRE(*it == nestedDir);
289 ec = GetTestEC();
290 it.increment(ec);
291 TEST_CHECK(ec);
292 TEST_CHECK(ErrorIs(ec, eacess));
293 TEST_CHECK(it == endIt);
294 }
295 {
296 bool SeenNestedFile = false;
297 recursive_directory_iterator it = SetupState(true, SeenNestedFile);
298 TEST_REQUIRE(it != endIt);
299 TEST_REQUIRE(*it == nestedDir);
300 ec = GetTestEC();
301 it.increment(ec);
302 TEST_CHECK(!ec);
303 if (SeenNestedFile) {
304 TEST_CHECK(it == endIt);
305 } else {
306 TEST_REQUIRE(it != endIt);
307 TEST_CHECK(*it == nestedFile);
308 }
309 }
310 {
311 bool SeenNestedFile = false;
312 recursive_directory_iterator it = SetupState(false, SeenNestedFile);
313 TEST_REQUIRE(it != endIt);
314 TEST_REQUIRE(*it == nestedDir);
315
316 ExceptionChecker Checker(std::errc::permission_denied,
317 "recursive_directory_iterator::operator++()",
318 format_string("attempting recursion into \"%s\"",
319 nestedDir.string().c_str()));
320 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ++it);
321 }
322 }
323
324
325 // See llvm.org/PR35078
TEST_CASE(test_PR35078_with_symlink)326 TEST_CASE(test_PR35078_with_symlink)
327 {
328 using namespace fs;
329 scoped_test_env env;
330 const path testFiles[] = {
331 env.create_dir("dir1"),
332 env.create_file("dir1/file1"),
333 env.create_dir("sym_dir"),
334 env.create_dir("sym_dir/nested_sym_dir"),
335 env.create_directory_symlink("sym_dir/nested_sym_dir", "dir1/dir2"),
336 env.create_dir("sym_dir/dir1"),
337 env.create_dir("sym_dir/dir1/dir2"),
338
339 };
340 // const unsigned TestFilesSize = sizeof(testFiles) / sizeof(testFiles[0]);
341 const path startDir = testFiles[0];
342 const path nestedFile = testFiles[1];
343 const path permDeniedDir = testFiles[2];
344 const path symDir = testFiles[4];
345
346 // Change the permissions so we can no longer iterate
347 permissions(permDeniedDir,
348 perms::group_exec|perms::owner_exec|perms::others_exec,
349 perm_options::remove);
350
351 const std::errc eacess = std::errc::permission_denied;
352 std::error_code ec = GetTestEC();
353
354 const recursive_directory_iterator endIt;
355
356 auto SetupState = [&](bool AllowEAccess, bool FollowSym, bool& SeenFile3) {
357 SeenFile3 = false;
358 auto Opts = AllowEAccess ? directory_options::skip_permission_denied
359 : directory_options::none;
360 if (FollowSym)
361 Opts |= directory_options::follow_directory_symlink;
362 recursive_directory_iterator it(startDir, Opts, ec);
363 while (!ec && it != endIt && *it != symDir) {
364 if (*it == nestedFile)
365 SeenFile3 = true;
366 it.increment(ec);
367 }
368 return it;
369 };
370
371 struct {
372 bool SkipPermDenied;
373 bool FollowSymlinks;
374 bool ExpectSuccess;
375 } TestCases[] = {
376 // Passing cases
377 {false, false, true}, {true, true, true}, {true, false, true},
378 // Failing cases
379 {false, true, false}
380 };
381 for (auto TC : TestCases) {
382 bool SeenNestedFile = false;
383 recursive_directory_iterator it = SetupState(TC.SkipPermDenied,
384 TC.FollowSymlinks,
385 SeenNestedFile);
386 TEST_REQUIRE(!ec);
387 TEST_REQUIRE(it != endIt);
388 TEST_REQUIRE(*it == symDir);
389 ec = GetTestEC();
390 it.increment(ec);
391 if (TC.ExpectSuccess) {
392 TEST_CHECK(!ec);
393 if (SeenNestedFile) {
394 TEST_CHECK(it == endIt);
395 } else {
396 TEST_REQUIRE(it != endIt);
397 TEST_CHECK(*it == nestedFile);
398 }
399 } else {
400 TEST_CHECK(ec);
401 TEST_CHECK(ErrorIs(ec, eacess));
402 TEST_CHECK(it == endIt);
403 }
404 }
405 }
406
407
408 // See llvm.org/PR35078
TEST_CASE(test_PR35078_with_symlink_file)409 TEST_CASE(test_PR35078_with_symlink_file)
410 {
411 using namespace fs;
412 scoped_test_env env;
413 const path testFiles[] = {
414 env.create_dir("dir1"),
415 env.create_dir("dir1/dir2"),
416 env.create_file("dir1/file2"),
417 env.create_dir("sym_dir"),
418 env.create_dir("sym_dir/sdir1"),
419 env.create_file("sym_dir/sdir1/sfile1"),
420 env.create_symlink("sym_dir/sdir1/sfile1", "dir1/dir2/file1")
421 };
422 const unsigned TestFilesSize = sizeof(testFiles) / sizeof(testFiles[0]);
423 const path startDir = testFiles[0];
424 const path nestedDir = testFiles[1];
425 const path nestedFile = testFiles[2];
426 const path permDeniedDir = testFiles[3];
427 const path symFile = testFiles[TestFilesSize - 1];
428
429 // Change the permissions so we can no longer iterate
430 permissions(permDeniedDir,
431 perms::group_exec|perms::owner_exec|perms::others_exec,
432 perm_options::remove);
433
434 const std::errc eacess = std::errc::permission_denied;
435 std::error_code ec = GetTestEC();
436
437 const recursive_directory_iterator EndIt;
438
439 auto SetupState = [&](bool AllowEAccess, bool FollowSym, bool& SeenNestedFile) {
440 SeenNestedFile = false;
441 auto Opts = AllowEAccess ? directory_options::skip_permission_denied
442 : directory_options::none;
443 if (FollowSym)
444 Opts |= directory_options::follow_directory_symlink;
445 recursive_directory_iterator it(startDir, Opts, ec);
446 while (!ec && it != EndIt && *it != nestedDir) {
447 if (*it == nestedFile)
448 SeenNestedFile = true;
449 it.increment(ec);
450 }
451 return it;
452 };
453
454 struct {
455 bool SkipPermDenied;
456 bool FollowSymlinks;
457 bool ExpectSuccess;
458 } TestCases[] = {
459 // Passing cases
460 {false, false, true}, {true, true, true}, {true, false, true},
461 // Failing cases
462 {false, true, false}
463 };
464 for (auto TC : TestCases){
465 bool SeenNestedFile = false;
466 recursive_directory_iterator it = SetupState(TC.SkipPermDenied,
467 TC.FollowSymlinks,
468 SeenNestedFile);
469 TEST_REQUIRE(!ec);
470 TEST_REQUIRE(it != EndIt);
471 TEST_REQUIRE(*it == nestedDir);
472 ec = GetTestEC();
473 it.increment(ec);
474 TEST_REQUIRE(it != EndIt);
475 TEST_CHECK(!ec);
476 TEST_CHECK(*it == symFile);
477 ec = GetTestEC();
478 it.increment(ec);
479 if (TC.ExpectSuccess) {
480 if (!SeenNestedFile) {
481 TEST_CHECK(!ec);
482 TEST_REQUIRE(it != EndIt);
483 TEST_CHECK(*it == nestedFile);
484 ec = GetTestEC();
485 it.increment(ec);
486 }
487 TEST_CHECK(!ec);
488 TEST_CHECK(it == EndIt);
489 } else {
490 TEST_CHECK(ec);
491 TEST_CHECK(ErrorIs(ec, eacess));
492 TEST_CHECK(it == EndIt);
493 }
494 }
495 }
496 #endif
497
498
499 TEST_SUITE_END()
500