]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2016-2022 Free Software Foundation, Inc. |
ec04aad7 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" } |
ec04aad7 JW |
19 | // { dg-do run { target c++11 } } |
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 | { | |
31 | std::error_code ec; | |
994844d3 | 32 | const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); |
ec04aad7 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 TS requires. |
37 | VERIFY( n == 0 ); | |
ec04aad7 JW |
38 | |
39 | auto p = __gnu_test::nonexistent_path(); | |
994844d3 | 40 | ec = bad_ec; |
ec04aad7 | 41 | n = remove_all(p, ec); |
994844d3 JW |
42 | VERIFY( !ec ); |
43 | VERIFY( n == 0 ); | |
ec04aad7 | 44 | |
edfe833a JW |
45 | #if defined(__MINGW32__) || defined(__MINGW64__) |
46 | // No symlink support | |
47 | #else | |
ec04aad7 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 |
ec04aad7 | 65 | |
994844d3 | 66 | const auto dir = __gnu_test::nonexistent_path(); |
ec04aad7 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 | ||
fff148b7 JW |
111 | void |
112 | test04() | |
113 | { | |
29b2fd37 JW |
114 | if (!__gnu_test::permissions_are_testable()) |
115 | return; | |
116 | ||
fff148b7 JW |
117 | // PR libstdc++/93201 |
118 | std::error_code ec; | |
119 | std::uintmax_t n; | |
120 | ||
121 | auto dir = __gnu_test::nonexistent_path(); | |
122 | fs::create_directory(dir); | |
123 | __gnu_test::scoped_file f(dir/"file"); | |
124 | // remove write permission on the directory: | |
125 | fs::permissions(dir, fs::perms::owner_read|fs::perms::owner_exec); | |
126 | n = fs::remove_all(dir, ec); | |
13feb023 | 127 | VERIFY( n == std::uintmax_t(-1) ); |
fff148b7 JW |
128 | VERIFY( ec == std::errc::permission_denied ); // not ENOTEMPTY |
129 | ||
130 | try { | |
131 | fs::remove_all(dir); | |
132 | VERIFY( false ); | |
133 | } catch (const fs::filesystem_error& e) { | |
134 | VERIFY( e.code() == std::errc::permission_denied ); | |
135 | // First path is the argument to remove_all | |
136 | VERIFY( e.path1() == dir ); | |
137 | } | |
138 | ||
139 | fs::permissions(dir, fs::perms::owner_write|fs::perms::add_perms); | |
bfdb7b8c JW |
140 | fs::remove_all(dir, ec); |
141 | f.path.clear(); | |
fff148b7 JW |
142 | } |
143 | ||
ec04aad7 JW |
144 | int |
145 | main() | |
146 | { | |
147 | test01(); | |
388058dd | 148 | test02(); |
fff148b7 | 149 | test04(); |
ec04aad7 | 150 | } |