1 // Copyright (C) 2019 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
21 
22 #include <filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
25 
26 namespace fs = std::filesystem;
27 
28 __gnu_test::scoped_file
create_dir(fs::path dir=__gnu_test::nonexistent_path ())29 create_dir(fs::path dir = __gnu_test::nonexistent_path())
30 {
31   fs::create_directory(dir);
32   return { dir, __gnu_test::scoped_file::adopt_file };
33 }
34 
35 void
test01()36 test01()
37 {
38   const auto testdir = create_dir();
39   __gnu_test::scoped_file file(testdir.path / "file");
40 
41   fs::recursive_directory_iterator r(testdir.path);
42   VERIFY( r.recursion_pending() );
43 
44   r.disable_recursion_pending();
45   VERIFY( !r.recursion_pending() );
46 }
47 
48 void
test02()49 test02()
50 {
51   const auto testdir = create_dir();
52   __gnu_test::scoped_file file(testdir.path / "file");
53 
54   fs::recursive_directory_iterator r(testdir.path);
55   VERIFY( r.recursion_pending() );
56   const auto r2 = r;
57   // recursion pending flag should be copied:
58   VERIFY( r2.recursion_pending() == r.recursion_pending() );
59 
60   r.disable_recursion_pending();
61   VERIFY( !r.recursion_pending() );
62   const auto r3 = r;
63   // recursion pending flag should be copied:
64   VERIFY( r3.recursion_pending() == r.recursion_pending() );
65 }
66 
67 void
test03()68 test03()
69 {
70   std::error_code ec = make_error_code(std::errc::invalid_argument);
71 
72   const auto testdir = create_dir();
73   __gnu_test::scoped_file file1(testdir.path / "file1");
74   __gnu_test::scoped_file file2(testdir.path / "file2");
75   __gnu_test::scoped_file file3(testdir.path / "file3");
76   __gnu_test::scoped_file file4(testdir.path / "file4");
77 
78   fs::recursive_directory_iterator r(testdir.path);
79   r.disable_recursion_pending();
80   VERIFY( !r.recursion_pending() );
81   ++r;
82   // recursion pending flag should be true after incrementing:
83   VERIFY( r.recursion_pending() );
84 
85   r.disable_recursion_pending();
86   VERIFY( !r.recursion_pending() );
87   r.increment(ec);
88   VERIFY( !ec );
89   // recursion pending flag should be true after incrementing:
90   VERIFY( r.recursion_pending() );
91 
92   r.disable_recursion_pending();
93   VERIFY( !r.recursion_pending() );
94   r++;
95   // recursion pending flag should be true after post-incrementing:
96   VERIFY( r.recursion_pending() );
97 
98   VERIFY( ++r == fs::recursive_directory_iterator() );
99 }
100 
101 void
test04()102 test04()
103 {
104   const auto testdir = create_dir();
105   const auto sub1 = create_dir(testdir.path/"sub1");
106   __gnu_test::scoped_file file1(sub1.path / "file");
107   const auto sub2 = create_dir(testdir.path/"sub2");
108   __gnu_test::scoped_file file2(sub2.path / "file");
109 
110   fs::recursive_directory_iterator r(testdir.path);
111   ++r;
112   r.pop();
113   // recursion pending flag should be true after popping:
114   VERIFY( r.recursion_pending() );
115 
116   // and recursion should actually happen:
117   ++r;
118   VERIFY( r.depth() == 1 );
119   VERIFY( r->is_regular_file() );
120   // recursion pending flag should still be true:
121   VERIFY( r.recursion_pending() );
122 
123   r = fs::recursive_directory_iterator(testdir.path);
124   r.disable_recursion_pending();
125   ++r;
126   // when recursion is disabled, should not enter subdirectories:
127   VERIFY( r.depth() == 0 );
128   r.disable_recursion_pending();
129   ++r;
130   VERIFY( r == fs::recursive_directory_iterator() );
131 }
132 
main()133 int main()
134 {
135   test01();
136   test02();
137   test03();
138   test04();
139 }
140