| 1 |
// Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++17 } } |
| 19 |
// { dg-require-filesystem-ts "" } |
| 20 |
|
| 21 |
// 15.4 Copy [fs.op.copy_file] |
| 22 |
|
| 23 |
#include <filesystem> |
| 24 |
#include <fstream> |
| 25 |
#include <testsuite_fs.h> |
| 26 |
#include <testsuite_hooks.h> |
| 27 |
|
| 28 |
void |
| 29 |
test01() |
| 30 |
{ |
| 31 |
using std::filesystem::copy_options; |
| 32 |
std::error_code ec; |
| 33 |
|
| 34 |
auto from = __gnu_test::nonexistent_path(); |
| 35 |
auto to = __gnu_test::nonexistent_path(); |
| 36 |
|
| 37 |
// test non-existent file |
| 38 |
bool b = copy_file(from, to, ec); |
| 39 |
VERIFY( !b ); |
| 40 |
VERIFY( ec ); |
| 41 |
VERIFY( !exists(to) ); |
| 42 |
|
| 43 |
// test empty file |
| 44 |
std::ofstream{from}; |
| 45 |
VERIFY( exists(from) ); |
| 46 |
VERIFY( file_size(from) == 0 ); |
| 47 |
|
| 48 |
b = copy_file(from, to); |
| 49 |
VERIFY( b ); |
| 50 |
VERIFY( exists(to) ); |
| 51 |
VERIFY( file_size(to) == 0 ); |
| 52 |
remove(to); |
| 53 |
VERIFY( !exists(to) ); |
| 54 |
b = copy_file(from, to, copy_options::none, ec); |
| 55 |
VERIFY( b ); |
| 56 |
VERIFY( !ec ); |
| 57 |
VERIFY( exists(to) ); |
| 58 |
VERIFY( file_size(to) == 0 ); |
| 59 |
|
| 60 |
std::ofstream{from} << "Hello, filesystem!"; |
| 61 |
VERIFY( file_size(from) != 0 ); |
| 62 |
remove(to); |
| 63 |
VERIFY( !exists(to) ); |
| 64 |
b = copy_file(from, to); |
| 65 |
VERIFY( b ); |
| 66 |
VERIFY( exists(to) ); |
| 67 |
VERIFY( file_size(to) == file_size(from) ); |
| 68 |
remove(to); |
| 69 |
VERIFY( !exists(to) ); |
| 70 |
b = copy_file(from, to); |
| 71 |
VERIFY( b ); |
| 72 |
VERIFY( exists(to) ); |
| 73 |
VERIFY( file_size(to) == file_size(from) ); |
| 74 |
|
| 75 |
remove(from); |
| 76 |
remove(to); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
test_directory() |
| 81 |
{ |
| 82 |
std::error_code ec; |
| 83 |
|
| 84 |
auto dir = __gnu_test::nonexistent_path(); |
| 85 |
auto file = __gnu_test::nonexistent_path(); |
| 86 |
|
| 87 |
// error condition: is_regular_file(from) is false |
| 88 |
create_directory(dir); |
| 89 |
bool b = copy_file(dir, file, ec); |
| 90 |
VERIFY( !b ); |
| 91 |
VERIFY( ec == std::errc::invalid_argument ); |
| 92 |
VERIFY( !exists(file) ); |
| 93 |
VERIFY( is_directory(dir) ); |
| 94 |
|
| 95 |
ec = std::make_error_code(std::errc::address_in_use); |
| 96 |
// error condition: exists(to) is true and is_regular_file(to) is false |
| 97 |
std::ofstream{file} << "regular file"; |
| 98 |
b = copy_file(file, dir, ec); |
| 99 |
VERIFY( !b ); |
| 100 |
VERIFY( ec == std::errc::invalid_argument ); |
| 101 |
VERIFY( exists(file) ); |
| 102 |
VERIFY( is_directory(dir) ); |
| 103 |
|
| 104 |
remove(dir); |
| 105 |
remove(file); |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
test_existing() |
| 110 |
{ |
| 111 |
using std::filesystem::copy_options; |
| 112 |
std::error_code ec; |
| 113 |
|
| 114 |
auto from = __gnu_test::nonexistent_path(); |
| 115 |
auto to = from; |
| 116 |
|
| 117 |
// error condition: exists(to) is true and equivalent(from, to) is true |
| 118 |
std::ofstream{from} << "source"; |
| 119 |
bool b = copy_file(from, to, ec); |
| 120 |
VERIFY( !b ); |
| 121 |
VERIFY( ec == std::errc::file_exists ); |
| 122 |
|
| 123 |
to = __gnu_test::nonexistent_path(); |
| 124 |
std::ofstream{to} << "overwrite me"; |
| 125 |
|
| 126 |
// error condition: exists(to) is true and options == copy_options::none |
| 127 |
b = copy_file(from, to, ec); |
| 128 |
VERIFY( !b ); |
| 129 |
VERIFY( ec == std::errc::file_exists ); |
| 130 |
VERIFY( file_size(to) != file_size(from) ); |
| 131 |
|
| 132 |
#if __cpp_exceptions |
| 133 |
try |
| 134 |
{ |
| 135 |
(void) copy_file(from, to); |
| 136 |
VERIFY(false); |
| 137 |
} |
| 138 |
catch (const std::filesystem::filesystem_error& e) |
| 139 |
{ |
| 140 |
std::string_view msg = e.what(); |
| 141 |
VERIFY( msg.find(from.string()) != msg.npos ); |
| 142 |
VERIFY( msg.find(to.string()) != msg.npos ); |
| 143 |
VERIFY( e.code() == std::errc::file_exists ); |
| 144 |
} |
| 145 |
VERIFY( file_size(to) == 12 ); |
| 146 |
#endif |
| 147 |
|
| 148 |
b = copy_file(from, to, copy_options::skip_existing, ec); |
| 149 |
VERIFY( !b ); |
| 150 |
VERIFY( !ec ); |
| 151 |
VERIFY( file_size(to) != file_size(from) ); |
| 152 |
|
| 153 |
b = copy_file(from, to, copy_options::skip_existing); // doesn't throw |
| 154 |
VERIFY( !b ); |
| 155 |
VERIFY( file_size(to) != file_size(from) ); |
| 156 |
|
| 157 |
b = copy_file(from, to, copy_options::overwrite_existing, ec); |
| 158 |
VERIFY( b ); |
| 159 |
VERIFY( !ec ); |
| 160 |
VERIFY( file_size(to) == file_size(from) ); |
| 161 |
|
| 162 |
std::ofstream{to} << "overwrite me again"; |
| 163 |
b = copy_file(from, to, copy_options::overwrite_existing); // doesn't throw |
| 164 |
VERIFY( b ); |
| 165 |
VERIFY( file_size(to) == file_size(from) ); |
| 166 |
|
| 167 |
ec = std::make_error_code(std::errc::address_in_use); |
| 168 |
auto time = last_write_time(from); |
| 169 |
std::ofstream{to} << "touched"; |
| 170 |
last_write_time(to, time + std::chrono::seconds(60)); |
| 171 |
b = copy_file(from, to, copy_options::update_existing, ec); |
| 172 |
VERIFY( !b ); |
| 173 |
VERIFY( !ec ); |
| 174 |
VERIFY( file_size(to) != file_size(from) ); |
| 175 |
|
| 176 |
b = copy_file(from, to, copy_options::update_existing); // doesn't throw |
| 177 |
VERIFY( !b ); |
| 178 |
VERIFY( file_size(to) != file_size(from) ); |
| 179 |
|
| 180 |
ec = std::make_error_code(std::errc::address_in_use); |
| 181 |
time = last_write_time(to); |
| 182 |
std::ofstream{from} << "touched more recently"; |
| 183 |
last_write_time(from, time + std::chrono::seconds(60)); |
| 184 |
b = copy_file(from, to, copy_options::update_existing, ec); |
| 185 |
VERIFY( b ); |
| 186 |
VERIFY( !ec ); |
| 187 |
VERIFY( file_size(to) == file_size(from) ); |
| 188 |
|
| 189 |
time = last_write_time(to); |
| 190 |
std::ofstream{from} << "touched even more recently"; |
| 191 |
last_write_time(from, time + std::chrono::seconds(60)); |
| 192 |
b = copy_file(from, to, copy_options::update_existing); // doesn't throw |
| 193 |
VERIFY( b ); |
| 194 |
VERIFY( file_size(to) == file_size(from) ); |
| 195 |
|
| 196 |
remove(from); |
| 197 |
remove(to); |
| 198 |
} |
| 199 |
|
| 200 |
int |
| 201 |
main() |
| 202 |
{ |
| 203 |
test01(); |
| 204 |
test_existing(); |
| 205 |
} |