]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/operations/remove_all.cc
1 // Copyright (C) 2016-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
25 namespace fs
= std::filesystem
;
31 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
34 n
= fs::remove_all("", ec
);
35 VERIFY( !ec
); // This seems odd, but is what the standard requires.
38 auto p
= __gnu_test::nonexistent_path();
40 n
= remove_all(p
, ec
);
45 auto link
= __gnu_test::nonexistent_path();
46 create_symlink(p
, link
); // dangling symlink
48 n
= remove_all(link
, ec
);
51 VERIFY( !exists(symlink_status(link
)) ); // DR 2721
53 __gnu_test::scoped_file
f(p
);
54 create_symlink(p
, link
);
56 n
= remove_all(link
, ec
);
59 VERIFY( !exists(symlink_status(link
)) ); // The symlink is removed, but
60 VERIFY( exists(p
) ); // its target is not.
63 const auto dir
= __gnu_test::nonexistent_path();
64 create_directories(dir
/"a/b/c");
66 n
= remove_all(dir
/"a", ec
);
69 VERIFY( exists(dir
) );
70 VERIFY( !exists(dir
/"a") );
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");
78 n
= remove_all(dir
, ec
);
81 VERIFY( !exists(dir
) );
92 const auto dir
= __gnu_test::nonexistent_path();
93 create_directories(dir
/"a/b/c");
94 std::uintmax_t n
= remove_all(dir
/"a");
96 VERIFY( exists(dir
) );
97 VERIFY( !exists(dir
/"a") );
99 n
= remove_all(dir
/"a");
101 VERIFY( exists(dir
) );
105 VERIFY( !exists(dir
) );
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
);
114 std::error_code ec
= bad_ec
;
115 const auto p
= __gnu_test::nonexistent_path() / ""; // with trailing slash
117 create_directories(p
);
118 removed
= remove_all(p
, 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
) );
127 const auto p_subs
= p
/"foo/bar";
129 create_directories(p_subs
);
130 removed
= remove_all(p
, ec
);
132 VERIFY( removed
== 3 );
133 VERIFY( !exists(p
) );
134 create_directories(p_subs
);
136 VERIFY( removed
== 3 );
137 VERIFY( !exists(p
) );
143 if (!__gnu_test::permissions_are_testable())
146 // PR libstdc++/93201
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
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" );
170 fs::permissions(dir
, fs::perms::owner_write
, fs::perm_options::add
);
171 fs::remove_all(dir
, ec
);