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++98, 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 <type_traits>
22 #include <chrono>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "rapid-cxx-test.h"
27 #include "filesystem_test_helper.h"
28 
29 using namespace fs;
30 
TEST_SUITE(filesystem_copy_file_test_suite)31 TEST_SUITE(filesystem_copy_file_test_suite)
32 
33 static std::string random_hex_chars(uintmax_t size) {
34   std::string data;
35   data.reserve(size);
36   for (uintmax_t I = 0; I < size; ++I)
37     data.push_back(random_utils::random_hex_char());
38   return data;
39 }
40 
41 // This test is intended to test 'sendfile's 2gb limit for a single call, and
42 // to ensure that libc++ correctly copies files larger than that limit.
43 // However it requires allocating ~5GB of filesystem space. This might not
44 // be acceptable on all systems.
TEST_CASE(large_file)45 TEST_CASE(large_file) {
46   using namespace fs;
47   constexpr uintmax_t sendfile_size_limit = 2147479552ull;
48   constexpr uintmax_t additional_size = 1024;
49   constexpr uintmax_t test_file_size = sendfile_size_limit + additional_size;
50   static_assert(test_file_size > sendfile_size_limit, "");
51 
52   scoped_test_env env;
53 
54   // Check that we have more than sufficient room to create the files needed
55   // to perform the test.
56   if (space(env.test_root).available < 3 * test_file_size) {
57     TEST_UNSUPPORTED();
58   }
59 
60   // Use python to create a file right at the size limit.
61   const path file = env.create_file("source", sendfile_size_limit);
62   // Create some random data that looks different than the data before the
63   // size limit.
64   const std::string additional_data = random_hex_chars(additional_size);
65   // Append this known data to the end of the source file.
66   {
67     std::ofstream outf(file.native(), std::ios_base::app);
68     TEST_REQUIRE(outf.good());
69     outf << additional_data;
70     TEST_REQUIRE(outf);
71   }
72   TEST_REQUIRE(file_size(file) == test_file_size);
73   const path dest = env.make_env_path("dest");
74 
75   std::error_code ec = GetTestEC();
76   TEST_CHECK(copy_file(file, dest, ec));
77   TEST_CHECK(!ec);
78 
79   TEST_REQUIRE(is_regular_file(dest));
80   TEST_CHECK(file_size(dest) == test_file_size);
81 
82   // Read the data from the end of the destination file, and ensure it matches
83   // the data at the end of the source file.
84   std::string out_data;
85   out_data.reserve(additional_size);
86   {
87     std::ifstream dest_file(dest.native());
88     TEST_REQUIRE(dest_file);
89     dest_file.seekg(sendfile_size_limit);
90     TEST_REQUIRE(dest_file);
91     dest_file >> out_data;
92     TEST_CHECK(dest_file.eof());
93   }
94   TEST_CHECK(out_data.size() == additional_data.size());
95   TEST_CHECK(out_data == additional_data);
96 }
97 
98 TEST_SUITE_END()
99