1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
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}}
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,
27 #include <type_traits>
31 #include "test_macros.h"
32 #include "filesystem_test_helper.h"
33 namespace fs
= std::filesystem
;
36 using CO
= fs::copy_options
;
38 static void signature_test()
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(p
, p
));
44 ASSERT_NOT_NOEXCEPT(fs::copy(p
, p
, ec
));
45 ASSERT_NOT_NOEXCEPT(copy(p
, p
, opts
));
46 ASSERT_NOT_NOEXCEPT(copy(p
, p
, opts
, ec
));
49 // There are 4 cases is the proposal for absolute path.
50 // Each scope tests one of the cases.
51 static void test_error_reporting()
53 auto checkThrow
= [](path
const& f
, path
const& t
, const std::error_code
& ec
)
55 #ifndef TEST_HAS_NO_EXCEPTIONS
59 } catch (filesystem_error
const& err
) {
60 return err
.path1() == f
65 ((void)f
); ((void)t
); ((void)ec
);
70 static_test_env static_env
;
72 const path file
= env
.create_file("file1", 42);
73 const path dir
= env
.create_dir("dir");
75 const path fifo
= env
.create_fifo("fifo");
76 assert(is_other(fifo
));
79 const auto test_ec
= GetTestEC();
83 std::error_code ec
= test_ec
;
84 const path f
= static_env
.DNE
;
85 const path t
= env
.test_root
;
88 assert(ec
!= test_ec
);
89 assert(checkThrow(f
, t
, ec
));
91 { // equivalent(f, t) == true
92 std::error_code ec
= test_ec
;
93 fs::copy(file
, file
, ec
);
95 assert(ec
!= test_ec
);
96 assert(checkThrow(file
, file
, ec
));
98 { // is_directory(from) && is_file(to)
99 std::error_code ec
= test_ec
;
100 fs::copy(dir
, file
, ec
);
102 assert(ec
!= test_ec
);
103 assert(checkThrow(dir
, file
, ec
));
107 std::error_code ec
= test_ec
;
108 fs::copy(fifo
, dir
, ec
);
110 assert(ec
!= test_ec
);
111 assert(checkThrow(fifo
, dir
, ec
));
114 std::error_code ec
= test_ec
;
115 fs::copy(file
, fifo
, ec
);
117 assert(ec
!= test_ec
);
118 assert(checkThrow(file
, fifo
, ec
));
123 static void from_is_symlink()
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");
131 std::error_code ec
= GetTestEC();
132 fs::copy(symlink
, dne
, copy_options::skip_symlinks
, ec
);
134 assert(!exists(dne
));
137 const path dest
= env
.make_env_path("dest");
138 std::error_code ec
= GetTestEC();
139 fs::copy(symlink
, dest
, copy_options::copy_symlinks
, ec
);
141 assert(exists(dest
));
142 assert(is_symlink(dest
));
144 { // copy symlink but target exists
145 std::error_code ec
= GetTestEC();
146 fs::copy(symlink
, file
, copy_options::copy_symlinks
, ec
);
148 assert(ec
!= GetTestEC());
150 { // create symlinks but target exists
151 std::error_code ec
= GetTestEC();
152 fs::copy(symlink
, file
, copy_options::create_symlinks
, ec
);
154 assert(ec
!= GetTestEC());
158 static void from_is_regular_file()
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(file
, dest
, CO::directories_only
, ec
);
168 assert(!exists(dest
));
170 { // create symlink to file
171 const path dest
= env
.make_env_path("sym");
172 std::error_code ec
= GetTestEC();
173 fs::copy(file
, dest
, CO::create_symlinks
, ec
);
175 assert(is_symlink(dest
));
176 assert(equivalent(file
, canonical(dest
)));
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(file
, dest
, CO::create_hard_links
, ec
);
184 assert(exists(dest
));
185 assert(hard_link_count(file
) == 2);
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(file
, dest_dir
, ec
);
193 assert(is_regular_file(expect_dest
));
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(file
, dest
, ec
);
200 assert(is_regular_file(dest
));
204 static void from_is_directory()
210 const FileInfo files
[] = {
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");
220 for (auto& FI
: files
) {
221 env
.create_file(dir
/ FI
.filename
, FI
.size
);
222 env
.create_file(nested_dir
/ FI
.filename
, FI
.size
);
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(dir
, dest
, 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
);
235 assert(!is_directory(dest
/ nested_dir_name
));
237 { // test for existing directory
238 const path dest
= env
.create_dir("dest_dir2");
239 std::error_code ec
= GetTestEC();
240 fs::copy(dir
, dest
, 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
);
248 assert(!is_directory(dest
/ nested_dir_name
));
250 { // test recursive copy
251 const path dest
= env
.make_env_path("dest_dir3");
252 std::error_code ec
= GetTestEC();
253 fs::copy(dir
, dest
, CO::recursive
, 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
);
269 static void test_copy_symlinks_to_symlink_dir()
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");
278 std::error_code ec
= GetTestEC();
279 fs::copy(file1
, dir_sym
, copy_options::copy_symlinks
, 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);
289 static void test_dir_create_symlink()
292 const path dir
= env
.create_dir("dir1");
293 const path dest
= env
.make_env_path("dne");
295 std::error_code ec
= GetTestEC();
296 fs::copy(dir
, dest
, copy_options::create_symlinks
, ec
);
297 assert(ErrorIs(ec
, std::errc::is_a_directory
));
298 assert(!exists(dest
));
299 assert(!is_symlink(dest
));
302 std::error_code ec
= GetTestEC();
303 fs::copy(dir
, dest
, copy_options::create_symlinks
|copy_options::recursive
, ec
);
304 assert(ErrorIs(ec
, std::errc::is_a_directory
));
305 assert(!exists(dest
));
306 assert(!is_symlink(dest
));
310 static void test_otherwise_no_effects_clause()
313 const path dir
= env
.create_dir("dir1");
314 { // skip copy because of directory
315 const path dest
= env
.make_env_path("dest1");
317 fs::copy(dir
, dest
, CO::directories_only
, ec
);
319 assert(!exists(dest
));
323 int main(int, char**) {
325 test_error_reporting();
327 from_is_regular_file();
329 test_copy_symlinks_to_symlink_dir();
330 test_dir_create_symlink();
331 test_otherwise_no_effects_clause();