1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// REQUIRES: can-create-symlinks
10// UNSUPPORTED: c++03, c++11, c++14
11// UNSUPPORTED: no-filesystem
12// UNSUPPORTED: availability-filesystem-missing
13
14// <filesystem>
15
16// uintmax_t remove_all(const path& p);
17// uintmax_t remove_all(const path& p, error_code& ec) noexcept;
18
19#include <filesystem>
20
21#include "test_macros.h"
22#include "filesystem_test_helper.h"
23namespace fs = std::filesystem;
24using namespace fs;
25
26static void test_signatures()
27{
28 const path p; ((void)p);
29 std::error_code ec; ((void)ec);
30 ASSERT_SAME_TYPE(decltype(fs::remove_all(p)), std::uintmax_t);
31 ASSERT_SAME_TYPE(decltype(fs::remove_all(p, ec)), std::uintmax_t);
32
33 ASSERT_NOT_NOEXCEPT(fs::remove_all(p: p));
34 ASSERT_NOT_NOEXCEPT(fs::remove_all(p: p, ec&: ec));
35}
36
37static void test_error_reporting()
38{
39 scoped_test_env env;
40 // Windows doesn't support setting perms::none to trigger failures
41 // reading directories.
42#ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
43 auto checkThrow = [](path const& f, const std::error_code& ec)
44 {
45#ifndef TEST_HAS_NO_EXCEPTIONS
46 try {
47 fs::remove_all(p: f);
48 return false;
49 } catch (filesystem_error const& err) {
50 return err.path1() == f
51 && err.path2() == ""
52 && err.code() == ec;
53 }
54#else
55 ((void)f); ((void)ec);
56 return true;
57#endif
58 };
59 const path non_empty_dir = env.create_dir("dir");
60 env.create_file(non_empty_dir / "file1", 42);
61 const path bad_perms_dir = env.create_dir("bad_dir");
62 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
63 permissions(p: bad_perms_dir, prms: perms::none);
64 const path bad_perms_file = env.create_file("file2", 42);
65 permissions(p: bad_perms_file, prms: perms::none);
66
67 const path testCases[] = {
68 file_in_bad_dir
69 };
70 const auto BadRet = static_cast<std::uintmax_t>(-1);
71 for (auto& p : testCases) {
72 std::error_code ec;
73
74 assert(fs::remove_all(p, ec) == BadRet);
75 assert(ec);
76 assert(checkThrow(p, ec));
77 }
78#endif
79
80 // PR#35780
81 const path testCasesNonexistant[] = {
82 "",
83 env.make_env_path("dne")
84 };
85 for (auto &p : testCasesNonexistant) {
86 std::error_code ec;
87
88 assert(fs::remove_all(p, ec) == 0);
89 assert(!ec);
90 }
91}
92
93static void basic_remove_all_test()
94{
95 scoped_test_env env;
96 const path dne = env.make_env_path("dne");
97 const path link = env.create_symlink(dne, "link");
98 const path nested_link = env.make_env_path("nested_link");
99 create_symlink(to: link, new_symlink: nested_link);
100 const path testCases[] = {
101 env.create_file("file", 42),
102 env.create_dir("empty_dir"),
103 nested_link,
104 link
105 };
106 for (auto& p : testCases) {
107 std::error_code ec = std::make_error_code(std::errc::address_in_use);
108 assert(remove(p, ec));
109 assert(!ec);
110 assert(!exists(symlink_status(p)));
111 }
112}
113
114static void symlink_to_dir()
115{
116 scoped_test_env env;
117 const path dir = env.create_dir("dir");
118 const path file = env.create_file(dir / "file", 42);
119 const path link = env.create_directory_symlink(dir, "sym");
120
121 {
122 std::error_code ec = std::make_error_code(e: std::errc::address_in_use);
123 assert(remove_all(p: link, ec&: ec) == 1);
124 assert(!ec);
125 assert(!exists(s: symlink_status(p: link)));
126 assert(exists(p: dir));
127 assert(exists(p: file));
128 }
129}
130
131
132static void nested_dir()
133{
134 scoped_test_env env;
135 const path dir = env.create_dir("dir");
136 const path dir1 = env.create_dir(dir / "dir1");
137 const path out_of_dir_file = env.create_file("file1", 42);
138 const path all_files[] = {
139 dir, dir1,
140 env.create_file(dir / "file1", 42),
141 env.create_symlink(out_of_dir_file, dir / "sym1"),
142 env.create_file(dir1 / "file2", 42),
143 env.create_directory_symlink(dir, dir1 / "sym2")
144 };
145 const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
146
147 std::error_code ec = std::make_error_code(e: std::errc::address_in_use);
148 assert(remove_all(p: dir, ec&: ec) == expected_count);
149 assert(!ec);
150 for (auto const& p : all_files) {
151 assert(!exists(symlink_status(p)));
152 }
153 assert(exists(p: out_of_dir_file));
154}
155
156int main(int, char**) {
157 test_signatures();
158 test_error_reporting();
159 basic_remove_all_test();
160 symlink_to_dir();
161 nested_dir();
162
163 return 0;
164}
165

source code of libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp