Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file_large.pass.cpp

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