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: linux
11// UNSUPPORTED: no-filesystem
12// XFAIL: no-localization
13// UNSUPPORTED: availability-filesystem-missing
14
15// <filesystem>
16
17// bool copy_file(const path& from, const path& to);
18// bool copy_file(const path& from, const path& to, error_code& ec) noexcept;
19// bool copy_file(const path& from, const path& to, copy_options options);
20// bool copy_file(const path& from, const path& to, copy_options options,
21// error_code& ec) noexcept;
22
23#include <cassert>
24#include <filesystem>
25#include <system_error>
26
27#include "test_macros.h"
28#include "filesystem_test_helper.h"
29
30namespace fs = std::filesystem;
31
32// Linux has various virtual filesystems such as /proc and /sys
33// where files may have no length (st_size == 0), but still contain data.
34// This is because the to-be-read data is usually generated ad-hoc by the reading syscall
35// These files can not be copied with kernel-side copies like copy_file_range or sendfile,
36// and must instead be copied via a traditional userspace read + write loop.
37int main(int, char** argv) {
38 const fs::path procfile{"/proc/self/comm"};
39 assert(file_size(procfile) == 0);
40
41 scoped_test_env env;
42 std::error_code ec = GetTestEC();
43
44 const fs::path dest = env.make_env_path("dest");
45
46 assert(copy_file(procfile, dest, ec));
47 assert(!ec);
48
49 // /proc/self/comm contains the filename of the executable, plus a null terminator
50 assert(file_size(dest) == fs::path(argv[0]).filename().string().size() + 1);
51
52 return 0;
53}
54

source code of libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy_file/copy_file_procfs.pass.cpp