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 //===----------------------------------------------------------------------===//
10 // REQUIRES: long_tests
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
16 // bool copy_file(const path& from, const path& to);
17 // bool copy_file(const path& from, const path& to, error_code& ec) noexcept;
18 // bool copy_file(const path& from, const path& to, copy_options options);
19 // bool copy_file(const path& from, const path& to, copy_options options,
20 // error_code& ec) noexcept;
22 #include "filesystem_include.h"
27 #include "test_macros.h"
28 #include "filesystem_test_helper.h"
32 // This test is intended to test 'sendfile's 2gb limit for a single call, and
33 // to ensure that libc++ correctly copies files larger than that limit.
34 // However it requires allocating ~5GB of filesystem space. This might not
35 // be acceptable on all systems.
36 static void large_file() {
38 constexpr std::uintmax_t sendfile_size_limit
= 2147479552ull;
39 constexpr std::uintmax_t additional_size
= 1024;
40 constexpr std::uintmax_t test_file_size
= sendfile_size_limit
+ additional_size
;
41 static_assert(test_file_size
> sendfile_size_limit
, "");
45 // Check that we have more than sufficient room to create the files needed
46 // to perform the test.
47 if (space(env
.test_root
).available
< 3 * test_file_size
) {
51 // Create a file right at the size limit. The file is full of '\0's.
52 const path source
= env
.create_file("source", sendfile_size_limit
);
53 const std::string
additional_data(additional_size
, 'x');
54 // Append known data to the end of the source file.
56 std::FILE* outf
= std::fopen(source
.string().c_str(), "a");
57 assert(outf
!= nullptr);
58 std::fputs(additional_data
.c_str(), outf
);
61 assert(file_size(source
) == test_file_size
);
62 const path dest
= env
.make_env_path("dest");
64 std::error_code ec
= GetTestEC();
65 assert(copy_file(source
, dest
, ec
));
68 assert(is_regular_file(dest
));
69 assert(file_size(dest
) == test_file_size
);
71 // Read the data from the end of the destination file, and ensure it matches
72 // the data at the end of the source file.
73 std::string
out_data(additional_size
, 'z');
75 std::FILE* dest_file
= std::fopen(dest
.string().c_str(), "rb");
76 assert(dest_file
!= nullptr);
77 assert(std::fseek(dest_file
, sendfile_size_limit
, SEEK_SET
) == 0);
78 assert(std::fread(&out_data
[0], sizeof(out_data
[0]), additional_size
, dest_file
) == additional_size
);
79 std::fclose(dest_file
);
81 assert(out_data
.size() == additional_data
.size());
82 assert(out_data
== additional_data
);
85 int main(int, char**) {