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// Starting in Android N (API 24), SELinux policy prevents the shell user from
15// creating a FIFO file.
16// XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22|23}}
17
18// <filesystem>
19
20// void copy(const path& from, const path& to);
21// void copy(const path& from, const path& to, error_code& ec);
22// void copy(const path& from, const path& to, copy_options options);
23// void copy(const path& from, const path& to, copy_options options,
24// error_code& ec);
25
26#include <filesystem>
27#include <type_traits>
28#include <cstddef>
29#include <cassert>
30
31#include "test_macros.h"
32#include "filesystem_test_helper.h"
33namespace fs = std::filesystem;
34using namespace fs;
35
36using CO = fs::copy_options;
37
38static void signature_test()
39{
40 const path p; ((void)p);
41 std::error_code ec; ((void)ec);
42 const copy_options opts{}; ((void)opts);
43 ASSERT_NOT_NOEXCEPT(fs::copy(from: p, to: p));
44 ASSERT_NOT_NOEXCEPT(fs::copy(from: p, to: p, ec&: ec));
45 ASSERT_NOT_NOEXCEPT(copy(from: p, to: p, options: opts));
46 ASSERT_NOT_NOEXCEPT(copy(from: p, to: p, options: opts, ec&: ec));
47}
48
49// There are 4 cases is the proposal for absolute path.
50// Each scope tests one of the cases.
51static void test_error_reporting()
52{
53 auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
54 {
55#ifndef TEST_HAS_NO_EXCEPTIONS
56 try {
57 fs::copy(from: f, to: t);
58 return false;
59 } catch (filesystem_error const& err) {
60 return err.path1() == f
61 && err.path2() == t
62 && err.code() == ec;
63 }
64#else
65 ((void)f); ((void)t); ((void)ec);
66 return true;
67#endif
68 };
69
70 static_test_env static_env;
71 scoped_test_env env;
72 const path file = env.create_file("file1", 42);
73 const path dir = env.create_dir("dir");
74#ifndef _WIN32
75 const path fifo = env.create_fifo("fifo");
76 assert(is_other(fifo));
77#endif
78
79 const auto test_ec = GetTestEC();
80
81 // !exists(f)
82 {
83 std::error_code ec = test_ec;
84 const path f = static_env.DNE;
85 const path t = env.test_root;
86 fs::copy(from: f, to: t, ec&: ec);
87 assert(ec);
88 assert(ec != test_ec);
89 assert(checkThrow(f, t, ec));
90 }
91 { // equivalent(f, t) == true
92 std::error_code ec = test_ec;
93 fs::copy(from: file, to: file, ec&: ec);
94 assert(ec);
95 assert(ec != test_ec);
96 assert(checkThrow(file, file, ec));
97 }
98 { // is_directory(from) && is_file(to)
99 std::error_code ec = test_ec;
100 fs::copy(from: dir, to: file, ec&: ec);
101 assert(ec);
102 assert(ec != test_ec);
103 assert(checkThrow(dir, file, ec));
104 }
105#ifndef _WIN32
106 { // is_other(from)
107 std::error_code ec = test_ec;
108 fs::copy(from: fifo, to: dir, ec&: ec);
109 assert(ec);
110 assert(ec != test_ec);
111 assert(checkThrow(fifo, dir, ec));
112 }
113 { // is_other(to)
114 std::error_code ec = test_ec;
115 fs::copy(from: file, to: fifo, ec&: ec);
116 assert(ec);
117 assert(ec != test_ec);
118 assert(checkThrow(file, fifo, ec));
119 }
120#endif
121}
122
123static void from_is_symlink()
124{
125 scoped_test_env env;
126 const path file = env.create_file("file", 42);
127 const path symlink = env.create_symlink(file, "sym");
128 const path dne = env.make_env_path("dne");
129
130 { // skip symlinks
131 std::error_code ec = GetTestEC();
132 fs::copy(from: symlink, to: dne, options: copy_options::skip_symlinks, ec&: ec);
133 assert(!ec);
134 assert(!exists(dne));
135 }
136 {
137 const path dest = env.make_env_path("dest");
138 std::error_code ec = GetTestEC();
139 fs::copy(from: symlink, to: dest, options: copy_options::copy_symlinks, ec&: ec);
140 assert(!ec);
141 assert(exists(dest));
142 assert(is_symlink(dest));
143 }
144 { // copy symlink but target exists
145 std::error_code ec = GetTestEC();
146 fs::copy(from: symlink, to: file, options: copy_options::copy_symlinks, ec&: ec);
147 assert(ec);
148 assert(ec != GetTestEC());
149 }
150 { // create symlinks but target exists
151 std::error_code ec = GetTestEC();
152 fs::copy(from: symlink, to: file, options: copy_options::create_symlinks, ec&: ec);
153 assert(ec);
154 assert(ec != GetTestEC());
155 }
156}
157
158static void from_is_regular_file()
159{
160 scoped_test_env env;
161 const path file = env.create_file("file", 42);
162 const path dir = env.create_dir("dir");
163 { // skip copy because of directory
164 const path dest = env.make_env_path("dest1");
165 std::error_code ec = GetTestEC();
166 fs::copy(from: file, to: dest, options: CO::directories_only, ec&: ec);
167 assert(!ec);
168 assert(!exists(dest));
169 }
170 { // create symlink to file
171 const path dest = env.make_env_path("sym");
172 std::error_code ec = GetTestEC();
173 fs::copy(from: file, to: dest, options: CO::create_symlinks, ec&: ec);
174 assert(!ec);
175 assert(is_symlink(dest));
176 assert(equivalent(file, canonical(dest)));
177 }
178 { // create hard link to file
179 const path dest = env.make_env_path("hardlink");
180 assert(hard_link_count(file) == 1);
181 std::error_code ec = GetTestEC();
182 fs::copy(from: file, to: dest, options: CO::create_hard_links, ec&: ec);
183 assert(!ec);
184 assert(exists(dest));
185 assert(hard_link_count(file) == 2);
186 }
187 { // is_directory(t)
188 const path dest_dir = env.create_dir("dest_dir");
189 const path expect_dest = dest_dir / file.filename();
190 std::error_code ec = GetTestEC();
191 fs::copy(from: file, to: dest_dir, ec&: ec);
192 assert(!ec);
193 assert(is_regular_file(expect_dest));
194 }
195 { // otherwise copy_file(from, to, ...)
196 const path dest = env.make_env_path("file_copy");
197 std::error_code ec = GetTestEC();
198 fs::copy(from: file, to: dest, ec&: ec);
199 assert(!ec);
200 assert(is_regular_file(dest));
201 }
202}
203
204static void from_is_directory()
205{
206 struct FileInfo {
207 path filename;
208 std::size_t size;
209 };
210 const FileInfo files[] = {
211 {.filename: "file1", .size: 0},
212 {.filename: "file2", .size: 42},
213 {.filename: "file3", .size: 300}
214 };
215 scoped_test_env env;
216 const path dir = env.create_dir("dir");
217 const path nested_dir_name = "dir2";
218 const path nested_dir = env.create_dir("dir/dir2");
219
220 for (auto& FI : files) {
221 env.create_file(dir / FI.filename, FI.size);
222 env.create_file(nested_dir / FI.filename, FI.size);
223 }
224 { // test for non-existent directory
225 const path dest = env.make_env_path("dest_dir1");
226 std::error_code ec = GetTestEC();
227 fs::copy(from: dir, to: dest, ec&: ec);
228 assert(!ec);
229 assert(is_directory(dest));
230 for (auto& FI : files) {
231 path created = dest / FI.filename;
232 assert(is_regular_file(created));
233 assert(file_size(created) == FI.size);
234 }
235 assert(!is_directory(dest / nested_dir_name));
236 }
237 { // test for existing directory
238 const path dest = env.create_dir("dest_dir2");
239 std::error_code ec = GetTestEC();
240 fs::copy(from: dir, to: dest, ec&: ec);
241 assert(!ec);
242 assert(is_directory(dest));
243 for (auto& FI : files) {
244 path created = dest / FI.filename;
245 assert(is_regular_file(created));
246 assert(file_size(created) == FI.size);
247 }
248 assert(!is_directory(dest / nested_dir_name));
249 }
250 { // test recursive copy
251 const path dest = env.make_env_path("dest_dir3");
252 std::error_code ec = GetTestEC();
253 fs::copy(from: dir, to: dest, options: CO::recursive, ec&: ec);
254 assert(!ec);
255 assert(is_directory(dest));
256 const path nested_dest = dest / nested_dir_name;
257 assert(is_directory(nested_dest));
258 for (auto& FI : files) {
259 path created = dest / FI.filename;
260 path nested_created = nested_dest / FI.filename;
261 assert(is_regular_file(created));
262 assert(file_size(created) == FI.size);
263 assert(is_regular_file(nested_created));
264 assert(file_size(nested_created) == FI.size);
265 }
266 }
267}
268
269static void test_copy_symlinks_to_symlink_dir()
270{
271 scoped_test_env env;
272 const path file1 = env.create_file("file1", 42);
273 const path file2 = env.create_file("file2", 101);
274 const path file2_sym = env.create_symlink(file2, "file2_sym");
275 const path dir = env.create_dir("dir");
276 const path dir_sym = env.create_directory_symlink(dir, "dir_sym");
277 {
278 std::error_code ec = GetTestEC();
279 fs::copy(from: file1, to: dir_sym, options: copy_options::copy_symlinks, ec&: ec);
280 assert(!ec);
281 const path dest = env.make_env_path("dir/file1");
282 assert(exists(dest));
283 assert(!is_symlink(dest));
284 assert(file_size(dest) == 42);
285 }
286}
287
288
289static void test_dir_create_symlink()
290{
291 scoped_test_env env;
292 const path dir = env.create_dir("dir1");
293 const path dest = env.make_env_path("dne");
294 {
295 std::error_code ec = GetTestEC();
296 fs::copy(from: dir, to: dest, options: copy_options::create_symlinks, ec&: ec);
297 assert(ErrorIs(ec, std::errc::is_a_directory));
298 assert(!exists(dest));
299 assert(!is_symlink(dest));
300 }
301 {
302 std::error_code ec = GetTestEC();
303 fs::copy(from: dir, to: dest, options: copy_options::create_symlinks|copy_options::recursive, ec&: ec);
304 assert(ErrorIs(ec, std::errc::is_a_directory));
305 assert(!exists(dest));
306 assert(!is_symlink(dest));
307 }
308}
309
310static void test_otherwise_no_effects_clause()
311{
312 scoped_test_env env;
313 const path dir = env.create_dir("dir1");
314 { // skip copy because of directory
315 const path dest = env.make_env_path("dest1");
316 std::error_code ec;
317 fs::copy(from: dir, to: dest, options: CO::directories_only, ec&: ec);
318 assert(!ec);
319 assert(!exists(dest));
320 }
321}
322
323int main(int, char**) {
324 signature_test();
325 test_error_reporting();
326 from_is_symlink();
327 from_is_regular_file();
328 from_is_directory();
329 test_copy_symlinks_to_symlink_dir();
330 test_dir_create_symlink();
331 test_otherwise_no_effects_clause();
332
333 return 0;
334}
335

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