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 // UNSUPPORTED: c++03
10 // REQUIRES: long_tests
11
12 // <filesystem>
13
14 // bool copy_file(const path& from, const path& to);
15 // bool copy_file(const path& from, const path& to, error_code& ec) noexcept;
16 // bool copy_file(const path& from, const path& to, copy_options options);
17 // bool copy_file(const path& from, const path& to, copy_options options,
18 // error_code& ec) noexcept;
19
20 #include "filesystem_include.h"
21 #include <cassert>
22 #include <cstdio>
23 #include <string>
24
25 #include "test_macros.h"
26 #include "rapid-cxx-test.h"
27 #include "filesystem_test_helper.h"
28
29 using namespace fs;
30
31 TEST_SUITE(filesystem_copy_file_test_suite)
32
33 // This test is intended to test 'sendfile's 2gb limit for a single call, and
34 // to ensure that libc++ correctly copies files larger than that limit.
35 // However it requires allocating ~5GB of filesystem space. This might not
36 // be acceptable on all systems.
TEST_CASE(large_file)37 TEST_CASE(large_file) {
38 using namespace fs;
39 constexpr uintmax_t sendfile_size_limit = 2147479552ull;
40 constexpr uintmax_t additional_size = 1024;
41 constexpr uintmax_t test_file_size = sendfile_size_limit + additional_size;
42 static_assert(test_file_size > sendfile_size_limit, "");
43
44 scoped_test_env env;
45
46 // Check that we have more than sufficient room to create the files needed
47 // to perform the test.
48 if (space(env.test_root).available < 3 * test_file_size) {
49 TEST_UNSUPPORTED();
50 }
51
52 // Create a file right at the size limit. The file is full of '\0's.
53 const path source = env.create_file("source", sendfile_size_limit);
54 const std::string additional_data(additional_size, 'x');
55 // Append known data to the end of the source file.
56 {
57 std::FILE* outf = std::fopen(source.string().c_str(), "a");
58 TEST_REQUIRE(outf != nullptr);
59 std::fputs(additional_data.c_str(), outf);
60 std::fclose(outf);
61 }
62 TEST_REQUIRE(file_size(source) == test_file_size);
63 const path dest = env.make_env_path("dest");
64
65 std::error_code ec = GetTestEC();
66 TEST_CHECK(copy_file(source, dest, ec));
67 TEST_CHECK(!ec);
68
69 TEST_REQUIRE(is_regular_file(dest));
70 TEST_CHECK(file_size(dest) == test_file_size);
71
72 // Read the data from the end of the destination file, and ensure it matches
73 // the data at the end of the source file.
74 std::string out_data(additional_size, 'z');
75 {
76 std::FILE* dest_file = std::fopen(dest.string().c_str(), "rb");
77 TEST_REQUIRE(dest_file != nullptr);
78 TEST_REQUIRE(std::fseek(dest_file, sendfile_size_limit, SEEK_SET) == 0);
79 TEST_REQUIRE(std::fread(&out_data[0], sizeof(out_data[0]), additional_size, dest_file) == additional_size);
80 std::fclose(dest_file);
81 }
82 TEST_CHECK(out_data.size() == additional_data.size());
83 TEST_CHECK(out_data == additional_data);
84 }
85
86 TEST_SUITE_END()
87