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 11 // <filesystem> 12 13 // bool copy_file(const path& from, const path& to); 14 // bool copy_file(const path& from, const path& to, error_code& ec) noexcept; 15 // bool copy_file(const path& from, const path& to, copy_options options); 16 // bool copy_file(const path& from, const path& to, copy_options options, 17 // error_code& ec) noexcept; 18 19 #include "filesystem_include.h" 20 #include <type_traits> 21 #include <chrono> 22 #include <cassert> 23 24 #include "test_macros.h" 25 #include "rapid-cxx-test.h" 26 #include "filesystem_test_helper.h" 27 28 using namespace fs; 29 30 using CO = fs::copy_options; 31 32 TEST_SUITE(filesystem_copy_file_test_suite) 33 34 TEST_CASE(test_signatures) { 35 const path p; 36 ((void)p); 37 const copy_options opts{}; 38 ((void)opts); 39 std::error_code ec; 40 ((void)ec); 41 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p)), bool); 42 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts)), bool); 43 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, ec)), bool); 44 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts, ec)), bool); 45 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p)); 46 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts)); 47 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, ec)); 48 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts, ec)); 49 } 50 51 TEST_CASE(test_error_reporting) { 52 53 scoped_test_env env; 54 const path file = env.create_file("file1", 42); 55 const path file2 = env.create_file("file2", 55); 56 57 { // exists(to) && equivalent(to, from) 58 std::error_code ec; 59 TEST_CHECK(fs::copy_file(file, file, copy_options::overwrite_existing, 60 ec) == false); 61 TEST_CHECK(ErrorIs(ec, std::errc::file_exists)); 62 ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file"); 63 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing)); 64 65 } 66 { // exists(to) && !(skip_existing | overwrite_existing | update_existing) 67 std::error_code ec; 68 TEST_CHECK(fs::copy_file(file, file2, ec) == false); 69 TEST_CHECK(ErrorIs(ec, std::errc::file_exists)); 70 ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file"); 71 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing)); 72 73 } 74 } 75 76 TEST_CASE(non_regular_file_test) { 77 scoped_test_env env; 78 const path fifo = env.create_fifo("fifo"); 79 const path dest = env.make_env_path("dest"); 80 const path file = env.create_file("file", 42); 81 82 { 83 std::error_code ec = GetTestEC(); 84 TEST_REQUIRE(fs::copy_file(fifo, dest, ec) == false); 85 TEST_CHECK(ErrorIs(ec, std::errc::not_supported)); 86 TEST_CHECK(!exists(dest)); 87 } 88 { 89 std::error_code ec = GetTestEC(); 90 TEST_REQUIRE(fs::copy_file(file, fifo, copy_options::overwrite_existing, 91 ec) == false); 92 TEST_CHECK(ErrorIs(ec, std::errc::not_supported)); 93 TEST_CHECK(is_fifo(fifo)); 94 } 95 96 } 97 98 TEST_CASE(test_attributes_get_copied) { 99 scoped_test_env env; 100 const path file = env.create_file("file1", 42); 101 const path dest = env.make_env_path("file2"); 102 (void)status(file); 103 perms new_perms = perms::owner_read; 104 permissions(file, new_perms); 105 std::error_code ec = GetTestEC(); 106 TEST_REQUIRE(fs::copy_file(file, dest, ec) == true); 107 TEST_CHECK(!ec); 108 auto new_st = status(dest); 109 TEST_CHECK(new_st.permissions() == new_perms); 110 } 111 112 TEST_CASE(copy_dir_test) { 113 scoped_test_env env; 114 const path file = env.create_file("file1", 42); 115 const path dest = env.create_dir("dir1"); 116 std::error_code ec = GetTestEC(); 117 TEST_CHECK(fs::copy_file(file, dest, ec) == false); 118 TEST_CHECK(ec); 119 TEST_CHECK(ec != GetTestEC()); 120 ec = GetTestEC(); 121 TEST_CHECK(fs::copy_file(dest, file, ec) == false); 122 TEST_CHECK(ec); 123 TEST_CHECK(ec != GetTestEC()); 124 } 125 126 TEST_CASE(copy_file) { 127 scoped_test_env env; 128 const path file = env.create_file("file1", 42); 129 130 { // !exists(to) 131 const path dest = env.make_env_path("dest1"); 132 std::error_code ec = GetTestEC(); 133 134 TEST_REQUIRE(fs::copy_file(file, dest, ec) == true); 135 TEST_CHECK(!ec); 136 TEST_CHECK(file_size(dest) == 42); 137 } 138 { // exists(to) && overwrite_existing 139 const path dest = env.create_file("dest2", 55); 140 permissions(dest, perms::all); 141 permissions(file, 142 perms::group_write | perms::owner_write | perms::others_write, 143 perm_options::remove); 144 145 std::error_code ec = GetTestEC(); 146 TEST_REQUIRE(fs::copy_file(file, dest, copy_options::overwrite_existing, 147 ec) == true); 148 TEST_CHECK(!ec); 149 TEST_CHECK(file_size(dest) == 42); 150 TEST_CHECK(status(dest).permissions() == status(file).permissions()); 151 } 152 { // exists(to) && update_existing 153 using Sec = std::chrono::seconds; 154 const path older = env.create_file("older_file", 1); 155 156 SleepFor(Sec(2)); 157 const path from = env.create_file("update_from", 55); 158 159 SleepFor(Sec(2)); 160 const path newer = env.create_file("newer_file", 2); 161 162 std::error_code ec = GetTestEC(); 163 TEST_REQUIRE( 164 fs::copy_file(from, older, copy_options::update_existing, ec) == true); 165 TEST_CHECK(!ec); 166 TEST_CHECK(file_size(older) == 55); 167 168 TEST_REQUIRE( 169 fs::copy_file(from, newer, copy_options::update_existing, ec) == false); 170 TEST_CHECK(!ec); 171 TEST_CHECK(file_size(newer) == 2); 172 } 173 { // skip_existing 174 const path file2 = env.create_file("file2", 55); 175 std::error_code ec = GetTestEC(); 176 TEST_REQUIRE(fs::copy_file(file, file2, copy_options::skip_existing, ec) == 177 false); 178 TEST_CHECK(!ec); 179 TEST_CHECK(file_size(file2) == 55); 180 } 181 } 182 183 184 TEST_SUITE_END() 185