]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/operations/remove_all.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / remove_all.cc
1 // Copyright (C) 2016-2023 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-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20
21 #include <filesystem>
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
24
25 namespace fs = std::filesystem;
26
27 void
28 test01()
29 {
30 std::error_code ec;
31 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
32 std::uintmax_t n;
33
34 n = fs::remove_all("", ec);
35 VERIFY( !ec ); // This seems odd, but is what the standard requires.
36 VERIFY( n == 0 );
37
38 auto p = __gnu_test::nonexistent_path();
39 ec = bad_ec;
40 n = remove_all(p, ec);
41 VERIFY( !ec );
42 VERIFY( n == 0 );
43
44 #ifndef NO_SYMLINKS
45 auto link = __gnu_test::nonexistent_path();
46 create_symlink(p, link); // dangling symlink
47 ec = bad_ec;
48 n = remove_all(link, ec);
49 VERIFY( !ec );
50 VERIFY( n == 1 );
51 VERIFY( !exists(symlink_status(link)) ); // DR 2721
52
53 __gnu_test::scoped_file f(p);
54 create_symlink(p, link);
55 ec = bad_ec;
56 n = remove_all(link, ec);
57 VERIFY( !ec );
58 VERIFY( n == 1 );
59 VERIFY( !exists(symlink_status(link)) ); // The symlink is removed, but
60 VERIFY( exists(p) ); // its target is not.
61 #endif
62
63 const auto dir = __gnu_test::nonexistent_path();
64 create_directories(dir/"a/b/c");
65 ec = bad_ec;
66 n = remove_all(dir/"a", ec);
67 VERIFY( !ec );
68 VERIFY( n == 3 );
69 VERIFY( exists(dir) );
70 VERIFY( !exists(dir/"a") );
71
72 create_directories(dir/"a/b/c");
73 __gnu_test::scoped_file a1(dir/"a/1");
74 __gnu_test::scoped_file a2(dir/"a/2");
75 __gnu_test::scoped_file b1(dir/"a/b/1");
76 __gnu_test::scoped_file b2(dir/"a/b/2");
77 ec = bad_ec;
78 n = remove_all(dir, ec);
79 VERIFY( !ec );
80 VERIFY( n == 8 );
81 VERIFY( !exists(dir) );
82
83 a1.path.clear();
84 a2.path.clear();
85 b1.path.clear();
86 b2.path.clear();
87 }
88
89 void
90 test02()
91 {
92 const auto dir = __gnu_test::nonexistent_path();
93 create_directories(dir/"a/b/c");
94 std::uintmax_t n = remove_all(dir/"a");
95 VERIFY( n == 3 );
96 VERIFY( exists(dir) );
97 VERIFY( !exists(dir/"a") );
98
99 n = remove_all(dir/"a");
100 VERIFY( n == 0 );
101 VERIFY( exists(dir) );
102
103 n = remove_all(dir);
104 VERIFY( n == 1 );
105 VERIFY( !exists(dir) );
106 }
107
108 void
109 test03()
110 {
111 // PR libstdc++/88881 symlink_status confused by trailing slash on Windows
112 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
113 unsigned removed;
114 std::error_code ec = bad_ec;
115 const auto p = __gnu_test::nonexistent_path() / ""; // with trailing slash
116
117 create_directories(p);
118 removed = remove_all(p, ec);
119 VERIFY( !ec );
120 VERIFY( removed == 1 );
121 VERIFY( !exists(p) );
122 create_directories(p);
123 removed = remove_all(p);
124 VERIFY( removed == 1 );
125 VERIFY( !exists(p) );
126
127 const auto p_subs = p/"foo/bar";
128 ec = bad_ec;
129 create_directories(p_subs);
130 removed = remove_all(p, ec);
131 VERIFY( !ec );
132 VERIFY( removed == 3 );
133 VERIFY( !exists(p) );
134 create_directories(p_subs);
135 remove_all(p);
136 VERIFY( removed == 3 );
137 VERIFY( !exists(p) );
138 }
139
140 void
141 test04()
142 {
143 if (!__gnu_test::permissions_are_testable())
144 return;
145
146 // PR libstdc++/93201
147 std::error_code ec;
148 std::uintmax_t n;
149
150 auto dir = __gnu_test::nonexistent_path();
151 fs::create_directory(dir);
152 __gnu_test::scoped_file f(dir/"file");
153 // remove write permission on the directory:
154 fs::permissions(dir, fs::perms::owner_read|fs::perms::owner_exec);
155 n = fs::remove_all(dir, ec);
156 VERIFY( n == std::uintmax_t(-1) );
157 VERIFY( ec == std::errc::permission_denied ); // not ENOTEMPTY
158
159 try {
160 fs::remove_all(dir);
161 VERIFY( false );
162 } catch (const fs::filesystem_error& e) {
163 VERIFY( e.code() == std::errc::permission_denied );
164 // First path is the argument to remove_all
165 VERIFY( e.path1() == dir );
166 // Second path is the first file that couldn't be removed
167 VERIFY( e.path2() == dir/"file" );
168 }
169
170 fs::permissions(dir, fs::perms::owner_write, fs::perm_options::add);
171 fs::remove_all(dir, ec);
172 f.path.clear();
173 }
174
175 int
176 main()
177 {
178 test01();
179 test02();
180 test03();
181 test04();
182 }