Debian Codegraph @

libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc

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