1 // { dg-options "-std=gnu++17 -lstdc++fs" }
2 // { dg-do run { target c++17 } }
3 // { dg-require-filesystem-ts "" }
4 
5 // Copyright (C) 2014-2018 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 <filesystem>
25 #include <testsuite_fs.h>
26 #include <testsuite_hooks.h>
27 
28 namespace fs = std::filesystem;
29 
30 // Test error conditions.
31 void
test01()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
test02()68 test02()
69 {
70   const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
71   auto from = __gnu_test::nonexistent_path();
72   auto to = __gnu_test::nonexistent_path();
73   std::error_code ec;
74 
75   ec = bad_ec;
76   fs::create_symlink(".", from, ec);
77   VERIFY( !ec );
78   VERIFY( fs::exists(from) );
79 
80   ec = bad_ec;
81   fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
82   VERIFY( !ec );
83   VERIFY( !fs::exists(to) );
84 
85   ec = bad_ec;
86   fs::copy(from, to, fs::copy_options::skip_symlinks, ec);
87   VERIFY( !ec );
88   VERIFY( !fs::exists(to) );
89 
90   ec = bad_ec;
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_ec;
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   ec.clear();
104   fs::copy(from, to, fs::copy_options::copy_symlinks, ec);
105   VERIFY( ec );
106 
107   remove(from, ec);
108   remove(to, ec);
109 }
110 
111 // Test is_regular_file(f) case.
112 void
test03()113 test03()
114 {
115   auto from = __gnu_test::nonexistent_path();
116   auto to = __gnu_test::nonexistent_path();
117 
118   // test empty file
119   std::ofstream{from.native()};
120   VERIFY( fs::exists(from) );
121   VERIFY( fs::file_size(from) == 0 );
122   fs::copy(from, to);
123   VERIFY( fs::exists(to) );
124   VERIFY( fs::file_size(to) == 0 );
125 
126   remove(to);
127   VERIFY( !fs::exists(to) );
128   std::ofstream{from.native()} << "Hello, filesystem!";
129   VERIFY( fs::file_size(from) != 0 );
130   fs::copy(from, to);
131   VERIFY( fs::exists(to) );
132   VERIFY( fs::file_size(to) == fs::file_size(from) );
133 
134   remove(from);
135   remove(to);
136 }
137 
138 // Test is_directory(f) case.
139 void
test04()140 test04()
141 {
142   const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
143   auto from = __gnu_test::nonexistent_path();
144   auto to = __gnu_test::nonexistent_path();
145   std::error_code ec;
146 
147   create_directories(from/"a/b/c");
148 
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} << "file one";
157   __gnu_test::scoped_file f2(from/"a/b/f2");
158   std::ofstream{f2.path} << "file two";
159 
160   copy(from, to, ec);
161   VERIFY( !ec );
162   VERIFY( exists(to) && is_empty(to) );
163   remove(to);
164 
165   ec = bad_ec;
166   copy(from, to, fs::copy_options::recursive, ec);
167   VERIFY( !ec );
168   VERIFY( exists(to) && !is_empty(to) );
169   VERIFY( is_regular_file(to/"a/f1") && !is_empty(to/"a/f1") );
170   VERIFY( file_size(from/"a/f1") == file_size(to/"a/f1") );
171   VERIFY( is_regular_file(to/"a/b/f2") && !is_empty(to/"a/b/f2") );
172   VERIFY( file_size(from/"a/b/f2") == file_size(to/"a/b/f2") );
173   VERIFY( is_directory(to/"a/b/c") && is_empty(to/"a/b/c") );
174 
175   f1.path.clear();
176   f2.path.clear();
177   remove_all(from, ec);
178   remove_all(to, ec);
179 }
180 
181 // Test no-op cases.
182 void
test05()183 test05()
184 {
185   auto to = __gnu_test::nonexistent_path();
186   std::error_code ec = std::make_error_code(std::errc::invalid_argument);
187 
188   fs::copy("/", to, fs::copy_options::copy_symlinks, ec);
189   VERIFY( !ec );  // Previous value should be cleared (LWG 2683)
190 }
191 
192 int
main()193 main()
194 {
195   test01();
196   test02();
197   test03();
198   test04();
199   test05();
200 }
201