]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | // Copyright (C) 2016-2020 Free Software Foundation, Inc. |
641cb5a6 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 | ||
de4db54f | 18 | // { dg-options "-std=gnu++17" } |
641cb5a6 JW |
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 | void | |
29 | test01() | |
30 | { | |
31 | std::error_code ec; | |
994844d3 | 32 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
641cb5a6 JW |
33 | std::uintmax_t n; |
34 | ||
35 | n = fs::remove_all("", ec); | |
994844d3 JW |
36 | VERIFY( !ec ); // This seems odd, but is what the standard requires. |
37 | VERIFY( n == 0 ); | |
641cb5a6 JW |
38 | |
39 | auto p = __gnu_test::nonexistent_path(); | |
994844d3 | 40 | ec = bad_ec; |
641cb5a6 | 41 | n = remove_all(p, ec); |
994844d3 JW |
42 | VERIFY( !ec ); |
43 | VERIFY( n == 0 ); | |
641cb5a6 | 44 | |
edfe833a JW |
45 | #if defined(__MINGW32__) || defined(__MINGW64__) |
46 | // No symlink support | |
47 | #else | |
641cb5a6 JW |
48 | auto link = __gnu_test::nonexistent_path(); |
49 | create_symlink(p, link); // dangling symlink | |
50 | ec = bad_ec; | |
51 | n = remove_all(link, ec); | |
52 | VERIFY( !ec ); | |
53 | VERIFY( n == 1 ); | |
54 | VERIFY( !exists(symlink_status(link)) ); // DR 2721 | |
55 | ||
56 | __gnu_test::scoped_file f(p); | |
57 | create_symlink(p, link); | |
58 | ec = bad_ec; | |
59 | n = remove_all(link, ec); | |
60 | VERIFY( !ec ); | |
61 | VERIFY( n == 1 ); | |
62 | VERIFY( !exists(symlink_status(link)) ); // The symlink is removed, but | |
63 | VERIFY( exists(p) ); // its target is not. | |
edfe833a | 64 | #endif |
641cb5a6 | 65 | |
994844d3 | 66 | const auto dir = __gnu_test::nonexistent_path(); |
641cb5a6 JW |
67 | create_directories(dir/"a/b/c"); |
68 | ec = bad_ec; | |
69 | n = remove_all(dir/"a", ec); | |
70 | VERIFY( !ec ); | |
71 | VERIFY( n == 3 ); | |
72 | VERIFY( exists(dir) ); | |
73 | VERIFY( !exists(dir/"a") ); | |
74 | ||
75 | create_directories(dir/"a/b/c"); | |
76 | __gnu_test::scoped_file a1(dir/"a/1"); | |
77 | __gnu_test::scoped_file a2(dir/"a/2"); | |
78 | __gnu_test::scoped_file b1(dir/"a/b/1"); | |
79 | __gnu_test::scoped_file b2(dir/"a/b/2"); | |
80 | ec = bad_ec; | |
81 | n = remove_all(dir, ec); | |
82 | VERIFY( !ec ); | |
83 | VERIFY( n == 8 ); | |
84 | VERIFY( !exists(dir) ); | |
85 | ||
86 | a1.path.clear(); | |
87 | a2.path.clear(); | |
88 | b1.path.clear(); | |
89 | b2.path.clear(); | |
90 | } | |
91 | ||
388058dd JW |
92 | void |
93 | test02() | |
94 | { | |
95 | const auto dir = __gnu_test::nonexistent_path(); | |
96 | create_directories(dir/"a/b/c"); | |
97 | std::uintmax_t n = remove_all(dir/"a"); | |
98 | VERIFY( n == 3 ); | |
99 | VERIFY( exists(dir) ); | |
100 | VERIFY( !exists(dir/"a") ); | |
101 | ||
102 | n = remove_all(dir/"a"); | |
103 | VERIFY( n == 0 ); | |
104 | VERIFY( exists(dir) ); | |
105 | ||
106 | n = remove_all(dir); | |
107 | VERIFY( n == 1 ); | |
108 | VERIFY( !exists(dir) ); | |
109 | } | |
110 | ||
3cb929a3 JW |
111 | void |
112 | test03() | |
113 | { | |
114 | // PR libstdc++/88881 symlink_status confused by trailing slash on Windows | |
115 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); | |
116 | unsigned removed; | |
117 | std::error_code ec = bad_ec; | |
118 | const auto p = __gnu_test::nonexistent_path() / ""; // with trailing slash | |
119 | ||
120 | create_directories(p); | |
121 | removed = remove_all(p, ec); | |
122 | VERIFY( !ec ); | |
123 | VERIFY( removed == 1 ); | |
124 | VERIFY( !exists(p) ); | |
125 | create_directories(p); | |
126 | removed = remove_all(p); | |
127 | VERIFY( removed == 1 ); | |
128 | VERIFY( !exists(p) ); | |
129 | ||
130 | const auto p_subs = p/"foo/bar"; | |
131 | ec = bad_ec; | |
132 | create_directories(p_subs); | |
133 | removed = remove_all(p, ec); | |
134 | VERIFY( !ec ); | |
135 | VERIFY( removed == 3 ); | |
136 | VERIFY( !exists(p) ); | |
137 | create_directories(p_subs); | |
138 | remove_all(p); | |
139 | VERIFY( removed == 3 ); | |
140 | VERIFY( !exists(p) ); | |
141 | } | |
142 | ||
fff148b7 JW |
143 | void |
144 | test04() | |
145 | { | |
146 | #if defined(__MINGW32__) || defined(__MINGW64__) | |
147 | // no permissions | |
148 | #else | |
149 | // PR libstdc++/93201 | |
150 | std::error_code ec; | |
151 | std::uintmax_t n; | |
152 | ||
153 | auto dir = __gnu_test::nonexistent_path(); | |
154 | fs::create_directory(dir); | |
155 | __gnu_test::scoped_file f(dir/"file"); | |
156 | // remove write permission on the directory: | |
157 | fs::permissions(dir, fs::perms::owner_read|fs::perms::owner_exec); | |
158 | n = fs::remove_all(dir, ec); | |
13feb023 | 159 | VERIFY( n == std::uintmax_t(-1) ); |
fff148b7 JW |
160 | VERIFY( ec == std::errc::permission_denied ); // not ENOTEMPTY |
161 | ||
162 | try { | |
163 | fs::remove_all(dir); | |
164 | VERIFY( false ); | |
165 | } catch (const fs::filesystem_error& e) { | |
166 | VERIFY( e.code() == std::errc::permission_denied ); | |
167 | // First path is the argument to remove_all | |
168 | VERIFY( e.path1() == dir ); | |
169 | // Second path is the first file that couldn't be removed | |
170 | VERIFY( e.path2() == dir/"file" ); | |
171 | } | |
172 | ||
173 | fs::permissions(dir, fs::perms::owner_write, fs::perm_options::add); | |
174 | #endif | |
175 | } | |
176 | ||
641cb5a6 JW |
177 | int |
178 | main() | |
179 | { | |
180 | test01(); | |
388058dd | 181 | test02(); |
3cb929a3 | 182 | test03(); |
fff148b7 | 183 | test04(); |
641cb5a6 | 184 | } |