]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2015-2022 Free Software Foundation, Inc. |
429ee11a JW |
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 | ||
ef0e80d2 | 18 | // { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" } |
52066eae | 19 | // { dg-do run { target c++11 } } |
429ee11a JW |
20 | // { dg-require-filesystem-ts "" } |
21 | ||
22 | #include <experimental/filesystem> | |
23 | #include <testsuite_hooks.h> | |
24 | #include <testsuite_fs.h> | |
25 | ||
26 | namespace fs = std::experimental::filesystem; | |
27 | ||
28 | void | |
29 | test01() | |
30 | { | |
db62ad7c | 31 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
429ee11a JW |
32 | std::error_code ec; |
33 | ||
34 | // Test non-existent path. | |
35 | const auto p = __gnu_test::nonexistent_path(); | |
36 | fs::recursive_directory_iterator iter(p, ec); | |
37 | VERIFY( ec ); | |
69af1c04 | 38 | VERIFY( iter == end(iter) ); |
429ee11a JW |
39 | |
40 | // Test empty directory. | |
db62ad7c | 41 | ec = bad_ec; |
429ee11a JW |
42 | create_directory(p, fs::current_path(), ec); |
43 | VERIFY( !ec ); | |
db62ad7c | 44 | ec = bad_ec; |
429ee11a JW |
45 | iter = fs::recursive_directory_iterator(p, ec); |
46 | VERIFY( !ec ); | |
69af1c04 | 47 | VERIFY( iter == end(iter) ); |
429ee11a JW |
48 | |
49 | // Test non-empty directory. | |
db62ad7c JW |
50 | ec = bad_ec; |
51 | create_directories(p / "d1/d2", ec); | |
429ee11a | 52 | VERIFY( !ec ); |
db62ad7c | 53 | ec = bad_ec; |
429ee11a JW |
54 | iter = fs::recursive_directory_iterator(p, ec); |
55 | VERIFY( !ec ); | |
69af1c04 | 56 | VERIFY( iter != end(iter) ); |
429ee11a JW |
57 | VERIFY( iter->path() == p/"d1" ); |
58 | ++iter; | |
4e6c4298 | 59 | VERIFY( iter != end(iter) ); |
429ee11a JW |
60 | VERIFY( iter->path() == p/"d1/d2" ); |
61 | ++iter; | |
69af1c04 | 62 | VERIFY( iter == end(iter) ); |
429ee11a | 63 | |
29b2fd37 JW |
64 | if (__gnu_test::permissions_are_testable()) |
65 | { | |
66 | // Test inaccessible directory. | |
67 | ec = bad_ec; | |
68 | permissions(p, fs::perms::none, ec); | |
69 | VERIFY( !ec ); | |
70 | iter = fs::recursive_directory_iterator(p, ec); | |
71 | VERIFY( ec ); | |
72 | VERIFY( iter == end(iter) ); | |
73 | ||
74 | // Test inaccessible directory, skipping permission denied. | |
75 | const auto opts = fs::directory_options::skip_permission_denied; | |
76 | ec = bad_ec; | |
77 | iter = fs::recursive_directory_iterator(p, opts, ec); | |
78 | VERIFY( !ec ); | |
79 | VERIFY( iter == end(iter) ); | |
80 | ||
81 | // Test inaccessible sub-directory. | |
82 | ec = bad_ec; | |
83 | permissions(p, fs::perms::owner_all, ec); | |
84 | VERIFY( !ec ); | |
85 | ec = bad_ec; | |
86 | permissions(p/"d1/d2", fs::perms::none, ec); | |
87 | VERIFY( !ec ); | |
88 | ec = bad_ec; | |
89 | iter = fs::recursive_directory_iterator(p, ec); | |
90 | VERIFY( !ec ); | |
91 | VERIFY( iter != end(iter) ); | |
92 | VERIFY( iter->path() == p/"d1" ); | |
93 | ++iter; // should recurse into d1 | |
94 | VERIFY( iter != end(iter) ); | |
95 | VERIFY( iter->path() == p/"d1/d2" ); | |
96 | iter.increment(ec); // should fail to recurse into p/d1/d2 | |
97 | VERIFY( ec ); | |
98 | VERIFY( iter == end(iter) ); | |
99 | ||
100 | // Test inaccessible sub-directory, skipping permission denied. | |
101 | ec = bad_ec; | |
102 | iter = fs::recursive_directory_iterator(p, opts, ec); | |
103 | VERIFY( !ec ); | |
104 | VERIFY( iter != end(iter) ); | |
105 | VERIFY( iter->path() == p/"d1" ); | |
106 | ++iter; // should recurse into d1 | |
107 | VERIFY( iter->path() == p/"d1/d2" ); | |
108 | ec = bad_ec; | |
109 | iter.increment(ec); // should fail to recurse into p/d1/d2, so skip it | |
110 | VERIFY( !ec ); | |
111 | VERIFY( iter == end(iter) ); | |
112 | ||
113 | permissions(p/"d1/d2", fs::perms::owner_all, ec); | |
114 | } | |
429ee11a | 115 | |
429ee11a JW |
116 | remove_all(p, ec); |
117 | } | |
118 | ||
e4cce0ce JW |
119 | void |
120 | test02() | |
121 | { | |
db62ad7c | 122 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
d7187f9e JW |
123 | std::error_code ec; |
124 | const auto p = __gnu_test::nonexistent_path(); | |
db62ad7c | 125 | ec = bad_ec; |
d7187f9e JW |
126 | create_directories(p / "d1/d2", ec); |
127 | VERIFY( !ec ); | |
128 | ||
129 | // Test post-increment (libstdc++/71005) | |
db62ad7c | 130 | ec = bad_ec; |
d7187f9e JW |
131 | auto iter = fs::recursive_directory_iterator(p, ec); |
132 | VERIFY( !ec ); | |
69af1c04 | 133 | VERIFY( iter != end(iter) ); |
d7187f9e JW |
134 | const auto entry1 = *iter; |
135 | const auto entry2 = *iter++; | |
136 | VERIFY( entry1 == entry2 ); | |
137 | VERIFY( entry1.path() == p/"d1" ); | |
138 | const auto entry3 = *iter; | |
139 | const auto entry4 = *iter++; | |
140 | VERIFY( entry3 == entry4 ); | |
141 | VERIFY( entry3.path() == p/"d1/d2" ); | |
69af1c04 | 142 | VERIFY( iter == end(iter) ); |
d7187f9e JW |
143 | |
144 | remove_all(p, ec); | |
145 | } | |
146 | ||
147 | void | |
148 | test03() | |
149 | { | |
db62ad7c | 150 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
d7187f9e JW |
151 | const auto p = __gnu_test::nonexistent_path(); |
152 | create_directories(p / "longer_than_small_string_buffer", ec); | |
153 | VERIFY( !ec ); | |
154 | ||
155 | // Test for no reallocation on each dereference (this is a GNU extension) | |
156 | auto iter = fs::recursive_directory_iterator(p, ec); | |
157 | const auto* s1 = iter->path().c_str(); | |
158 | const auto* s2 = iter->path().c_str(); | |
159 | VERIFY( s1 == s2 ); | |
160 | ||
161 | remove_all(p, ec); | |
162 | } | |
163 | ||
164 | void | |
165 | test04() | |
166 | { | |
d7187f9e | 167 | // libstdc++/71004 |
e4cce0ce | 168 | const fs::recursive_directory_iterator it; |
69af1c04 | 169 | VERIFY( it == end(it) ); |
e4cce0ce JW |
170 | } |
171 | ||
7f99d40a JW |
172 | void |
173 | test05() | |
174 | { | |
7f99d40a JW |
175 | auto p = __gnu_test::nonexistent_path(); |
176 | create_directory(p); | |
177 | create_directory_symlink(p, p / "l"); | |
178 | fs::recursive_directory_iterator it(p), endit; | |
179 | VERIFY( begin(it) == it ); | |
180 | static_assert( noexcept(begin(it)), "begin is noexcept" ); | |
181 | VERIFY( end(it) == endit ); | |
182 | static_assert( noexcept(end(it)), "end is noexcept" ); | |
7c4979b2 JW |
183 | |
184 | std::error_code ec; | |
185 | remove_all(p, ec); | |
7f99d40a JW |
186 | } |
187 | ||
429ee11a JW |
188 | int |
189 | main() | |
190 | { | |
191 | test01(); | |
7972e246 | 192 | test02(); |
d7187f9e JW |
193 | test03(); |
194 | test04(); | |
7f99d40a | 195 | test05(); |
429ee11a | 196 | } |