]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | // Copyright (C) 2015-2020 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 JW |
63 | |
64 | // Test inaccessible directory. | |
db62ad7c | 65 | ec = bad_ec; |
429ee11a JW |
66 | permissions(p, fs::perms::none, ec); |
67 | VERIFY( !ec ); | |
68 | iter = fs::recursive_directory_iterator(p, ec); | |
69 | VERIFY( ec ); | |
69af1c04 | 70 | VERIFY( iter == end(iter) ); |
429ee11a JW |
71 | |
72 | // Test inaccessible directory, skipping permission denied. | |
73 | const auto opts = fs::directory_options::skip_permission_denied; | |
db62ad7c | 74 | ec = bad_ec; |
429ee11a JW |
75 | iter = fs::recursive_directory_iterator(p, opts, ec); |
76 | VERIFY( !ec ); | |
69af1c04 | 77 | VERIFY( iter == end(iter) ); |
429ee11a JW |
78 | |
79 | // Test inaccessible sub-directory. | |
db62ad7c | 80 | ec = bad_ec; |
429ee11a JW |
81 | permissions(p, fs::perms::owner_all, ec); |
82 | VERIFY( !ec ); | |
db62ad7c | 83 | ec = bad_ec; |
429ee11a JW |
84 | permissions(p/"d1/d2", fs::perms::none, ec); |
85 | VERIFY( !ec ); | |
db62ad7c | 86 | ec = bad_ec; |
429ee11a JW |
87 | iter = fs::recursive_directory_iterator(p, ec); |
88 | VERIFY( !ec ); | |
69af1c04 | 89 | VERIFY( iter != end(iter) ); |
429ee11a JW |
90 | VERIFY( iter->path() == p/"d1" ); |
91 | ++iter; // should recurse into d1 | |
4e6c4298 | 92 | VERIFY( iter != end(iter) ); |
429ee11a JW |
93 | VERIFY( iter->path() == p/"d1/d2" ); |
94 | iter.increment(ec); // should fail to recurse into p/d1/d2 | |
95 | VERIFY( ec ); | |
69af1c04 | 96 | VERIFY( iter == end(iter) ); |
429ee11a JW |
97 | |
98 | // Test inaccessible sub-directory, skipping permission denied. | |
db62ad7c | 99 | ec = bad_ec; |
429ee11a JW |
100 | iter = fs::recursive_directory_iterator(p, opts, ec); |
101 | VERIFY( !ec ); | |
69af1c04 | 102 | VERIFY( iter != end(iter) ); |
429ee11a JW |
103 | VERIFY( iter->path() == p/"d1" ); |
104 | ++iter; // should recurse into d1 | |
105 | VERIFY( iter->path() == p/"d1/d2" ); | |
db62ad7c | 106 | ec = bad_ec; |
429ee11a JW |
107 | iter.increment(ec); // should fail to recurse into p/d1/d2, so skip it |
108 | VERIFY( !ec ); | |
69af1c04 | 109 | VERIFY( iter == end(iter) ); |
429ee11a JW |
110 | |
111 | permissions(p/"d1/d2", fs::perms::owner_all, ec); | |
112 | remove_all(p, ec); | |
113 | } | |
114 | ||
e4cce0ce JW |
115 | void |
116 | test02() | |
117 | { | |
db62ad7c | 118 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
d7187f9e JW |
119 | std::error_code ec; |
120 | const auto p = __gnu_test::nonexistent_path(); | |
db62ad7c | 121 | ec = bad_ec; |
d7187f9e JW |
122 | create_directories(p / "d1/d2", ec); |
123 | VERIFY( !ec ); | |
124 | ||
125 | // Test post-increment (libstdc++/71005) | |
db62ad7c | 126 | ec = bad_ec; |
d7187f9e JW |
127 | auto iter = fs::recursive_directory_iterator(p, ec); |
128 | VERIFY( !ec ); | |
69af1c04 | 129 | VERIFY( iter != end(iter) ); |
d7187f9e JW |
130 | const auto entry1 = *iter; |
131 | const auto entry2 = *iter++; | |
132 | VERIFY( entry1 == entry2 ); | |
133 | VERIFY( entry1.path() == p/"d1" ); | |
134 | const auto entry3 = *iter; | |
135 | const auto entry4 = *iter++; | |
136 | VERIFY( entry3 == entry4 ); | |
137 | VERIFY( entry3.path() == p/"d1/d2" ); | |
69af1c04 | 138 | VERIFY( iter == end(iter) ); |
d7187f9e JW |
139 | |
140 | remove_all(p, ec); | |
141 | } | |
142 | ||
143 | void | |
144 | test03() | |
145 | { | |
db62ad7c | 146 | std::error_code ec = make_error_code(std::errc::invalid_argument); |
d7187f9e JW |
147 | const auto p = __gnu_test::nonexistent_path(); |
148 | create_directories(p / "longer_than_small_string_buffer", ec); | |
149 | VERIFY( !ec ); | |
150 | ||
151 | // Test for no reallocation on each dereference (this is a GNU extension) | |
152 | auto iter = fs::recursive_directory_iterator(p, ec); | |
153 | const auto* s1 = iter->path().c_str(); | |
154 | const auto* s2 = iter->path().c_str(); | |
155 | VERIFY( s1 == s2 ); | |
156 | ||
157 | remove_all(p, ec); | |
158 | } | |
159 | ||
160 | void | |
161 | test04() | |
162 | { | |
d7187f9e | 163 | // libstdc++/71004 |
e4cce0ce | 164 | const fs::recursive_directory_iterator it; |
69af1c04 | 165 | VERIFY( it == end(it) ); |
e4cce0ce JW |
166 | } |
167 | ||
7f99d40a JW |
168 | void |
169 | test05() | |
170 | { | |
7f99d40a JW |
171 | auto p = __gnu_test::nonexistent_path(); |
172 | create_directory(p); | |
173 | create_directory_symlink(p, p / "l"); | |
174 | fs::recursive_directory_iterator it(p), endit; | |
175 | VERIFY( begin(it) == it ); | |
176 | static_assert( noexcept(begin(it)), "begin is noexcept" ); | |
177 | VERIFY( end(it) == endit ); | |
178 | static_assert( noexcept(end(it)), "end is noexcept" ); | |
7c4979b2 JW |
179 | |
180 | std::error_code ec; | |
181 | remove_all(p, ec); | |
7f99d40a JW |
182 | } |
183 | ||
429ee11a JW |
184 | int |
185 | main() | |
186 | { | |
187 | test01(); | |
7972e246 | 188 | test02(); |
d7187f9e JW |
189 | test03(); |
190 | test04(); | |
7f99d40a | 191 | test05(); |
429ee11a | 192 | } |