1 // { dg-do run { target c++17 } }
2 // { dg-require-filesystem-ts "" }
3
4 // Copyright (C) 2014-2021 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // 15.3 Copy [fs.op.copy]
22
23 #include <filesystem>
24 #include <testsuite_fs.h>
25 #include <testsuite_hooks.h>
26
27 namespace fs = std::filesystem;
28
29 // Test error conditions.
30 void
test01()31 test01()
32 {
33 auto p = __gnu_test::nonexistent_path();
34 std::error_code ec;
35
36 VERIFY( !fs::exists(p) );
37 fs::copy(p, ".", fs::copy_options::none, ec);
38 VERIFY( ec );
39
40 ec.clear();
41 fs::copy(".", ".", fs::copy_options::none, ec);
42 VERIFY( ec );
43
44 __gnu_test::scoped_file f(p);
45 VERIFY( fs::is_directory(".") );
46 VERIFY( fs::is_regular_file(p) );
47 ec.clear();
48 fs::copy(".", p, fs::copy_options::none, ec);
49 VERIFY( ec );
50
51 auto to = __gnu_test::nonexistent_path();
52 ec.clear();
53 auto opts = fs::copy_options::create_symlinks;
54 fs::copy("/", to, opts, ec);
55 VERIFY( ec == std::make_error_code(std::errc::is_a_directory) );
56 VERIFY( !exists(to) );
57
58 ec.clear();
59 opts |= fs::copy_options::recursive;
60 fs::copy("/", to, opts, ec);
61 VERIFY( ec == std::make_error_code(std::errc::is_a_directory) );
62 VERIFY( !exists(to) );
63 }
64
65 // Test is_symlink(f) case.
66 void
test02()67 test02()
68 {
69 #if defined(__MINGW32__) || defined(__MINGW64__)
70 // No symlink support
71 return;
72 #endif
73
74 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
75 auto from = __gnu_test::nonexistent_path();
76 auto to = __gnu_test::nonexistent_path();
77 std::error_code ec;
78
79 ec = bad_ec;
80 fs::create_symlink(".", from, ec);
81 VERIFY( !ec );
82 VERIFY( fs::exists(from) );
83
84 ec = bad_ec;
85 fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
86 VERIFY( !ec );
87 VERIFY( !fs::exists(to) );
88
89 ec = bad_ec;
90 fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
91 VERIFY( !ec );
92 VERIFY( !fs::exists(to) );
93
94 ec = bad_ec;
95 fs::copy(from, to,
96 fs::copy_options::skip_symlinks|fs::copy_options::copy_symlinks,
97 ec);
98 VERIFY( !ec );
99 VERIFY( !fs::exists(to) );
100
101 ec = bad_ec;
102 fs::copy(from, to, fs::copy_options::copy_symlinks, ec);
103 VERIFY( !ec );
104 VERIFY( fs::exists(to) );
105 VERIFY( is_symlink(to) );
106
107 ec.clear();
108 fs::copy(from, to, fs::copy_options::copy_symlinks, ec);
109 VERIFY( ec );
110
111 remove(from, ec);
112 remove(to, ec);
113 }
114
115 // Test is_regular_file(f) case.
116 void
test03()117 test03()
118 {
119 auto from = __gnu_test::nonexistent_path();
120 auto to = __gnu_test::nonexistent_path();
121
122 // test empty file
123 std::ofstream{from};
124 VERIFY( fs::exists(from) );
125 VERIFY( fs::file_size(from) == 0 );
126 fs::copy(from, to);
127 VERIFY( fs::exists(to) );
128 VERIFY( fs::file_size(to) == 0 );
129
130 remove(to);
131 VERIFY( !fs::exists(to) );
132 std::ofstream{from} << "Hello, filesystem!";
133 VERIFY( fs::file_size(from) != 0 );
134 fs::copy(from, to);
135 VERIFY( fs::exists(to) );
136 VERIFY( fs::file_size(to) == fs::file_size(from) );
137
138 remove(from);
139 remove(to);
140 }
141
142 // Test is_directory(f) case.
143 void
test04()144 test04()
145 {
146 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
147 auto from = __gnu_test::nonexistent_path();
148 auto to = __gnu_test::nonexistent_path();
149 std::error_code ec;
150
151 create_directories(from/"a/b/c");
152
153 {
154 __gnu_test::scoped_file f(to);
155 copy(from, to, ec);
156 VERIFY( ec );
157 }
158
159 __gnu_test::scoped_file f1(from/"a/f1");
160 std::ofstream{f1.path} << "file one";
161 __gnu_test::scoped_file f2(from/"a/b/f2");
162 std::ofstream{f2.path} << "file two";
163
164 copy(from, to, ec);
165 VERIFY( !ec );
166 VERIFY( exists(to) && is_empty(to) );
167 remove(to);
168
169 ec = bad_ec;
170 copy(from, to, fs::copy_options::recursive, ec);
171 VERIFY( !ec );
172 VERIFY( exists(to) && !is_empty(to) );
173 VERIFY( is_regular_file(to/"a/f1") && !is_empty(to/"a/f1") );
174 VERIFY( file_size(from/"a/f1") == file_size(to/"a/f1") );
175 VERIFY( is_regular_file(to/"a/b/f2") && !is_empty(to/"a/b/f2") );
176 VERIFY( file_size(from/"a/b/f2") == file_size(to/"a/b/f2") );
177 VERIFY( is_directory(to/"a/b/c") && is_empty(to/"a/b/c") );
178
179 f1.path.clear();
180 f2.path.clear();
181 remove_all(from, ec);
182 remove_all(to, ec);
183 }
184
185 // Test no-op cases.
186 void
test05()187 test05()
188 {
189 auto to = __gnu_test::nonexistent_path();
190 std::error_code ec = std::make_error_code(std::errc::invalid_argument);
191
192 fs::copy("/", to, fs::copy_options::copy_symlinks, ec);
193 VERIFY( !ec ); // Previous value should be cleared (LWG 2683)
194 }
195
196 int
main()197 main()
198 {
199 test01();
200 test02();
201 test03();
202 test04();
203 test05();
204 }
205