]>
Commit | Line | Data |
---|---|---|
52066eae JW |
1 | // { dg-options "-lstdc++fs" } |
2 | // { dg-do run { target c++11 } } | |
0ca7ba9a JW |
3 | // { dg-require-filesystem-ts "" } |
4 | ||
818ab71a | 5 | // Copyright (C) 2014-2016 Free Software Foundation, Inc. |
0ca7ba9a JW |
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 | ||
14905251 | 28 | namespace fs = std::experimental::filesystem; |
0ca7ba9a | 29 | |
14905251 | 30 | // Test error conditions. |
0ca7ba9a JW |
31 | void |
32 | test01() | |
33 | { | |
14905251 JW |
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 | ||
b3dec9e5 | 45 | __gnu_test::scoped_file f(p); |
14905251 JW |
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 | ||
b3dec9e5 JW |
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) ); | |
0ca7ba9a JW |
64 | } |
65 | ||
14905251 | 66 | // Test is_symlink(f) case. |
0ca7ba9a JW |
67 | void |
68 | test02() | |
69 | { | |
14905251 JW |
70 | auto from = __gnu_test::nonexistent_path(); |
71 | auto to = __gnu_test::nonexistent_path(); | |
b3dec9e5 | 72 | std::error_code ec, bad = std::make_error_code(std::errc::invalid_argument); |
14905251 | 73 | |
b3dec9e5 | 74 | ec = bad; |
14905251 JW |
75 | fs::create_symlink(".", from, ec); |
76 | VERIFY( !ec ); | |
77 | VERIFY( fs::exists(from) ); | |
78 | ||
b3dec9e5 | 79 | ec = bad; |
14905251 JW |
80 | fs::copy(from, to, fs::copy_options::skip_symlinks, ec); |
81 | VERIFY( !ec ); | |
82 | VERIFY( !fs::exists(to) ); | |
83 | ||
b3dec9e5 | 84 | ec = bad; |
14905251 JW |
85 | fs::copy(from, to, fs::copy_options::skip_symlinks, ec); |
86 | VERIFY( !ec ); | |
87 | VERIFY( !fs::exists(to) ); | |
88 | ||
b3dec9e5 | 89 | ec = bad; |
14905251 JW |
90 | fs::copy(from, to, |
91 | fs::copy_options::skip_symlinks|fs::copy_options::copy_symlinks, | |
92 | ec); | |
93 | VERIFY( !ec ); | |
94 | VERIFY( !fs::exists(to) ); | |
95 | ||
b3dec9e5 | 96 | ec = bad; |
14905251 JW |
97 | fs::copy(from, to, fs::copy_options::copy_symlinks, ec); |
98 | VERIFY( !ec ); | |
99 | VERIFY( fs::exists(to) ); | |
b3dec9e5 | 100 | VERIFY( is_symlink(to) ); |
14905251 JW |
101 | |
102 | fs::copy(from, to, fs::copy_options::copy_symlinks, ec); | |
103 | VERIFY( ec ); | |
104 | ||
105 | remove(from, ec); | |
106 | remove(to, ec); | |
107 | } | |
108 | ||
109 | // Test is_regular_file(f) case. | |
110 | void | |
111 | test03() | |
112 | { | |
14905251 JW |
113 | auto from = __gnu_test::nonexistent_path(); |
114 | auto to = __gnu_test::nonexistent_path(); | |
115 | ||
116 | // test empty file | |
117 | std::ofstream{from.native()}; | |
118 | VERIFY( fs::exists(from) ); | |
119 | VERIFY( fs::file_size(from) == 0 ); | |
120 | fs::copy(from, to); | |
121 | VERIFY( fs::exists(to) ); | |
122 | VERIFY( fs::file_size(to) == 0 ); | |
123 | ||
124 | remove(to); | |
125 | VERIFY( !fs::exists(to) ); | |
126 | std::ofstream{from.native()} << "Hello, filesystem!"; | |
127 | VERIFY( fs::file_size(from) != 0 ); | |
128 | fs::copy(from, to); | |
129 | VERIFY( fs::exists(to) ); | |
130 | VERIFY( fs::file_size(to) == fs::file_size(from) ); | |
ec04aad7 JW |
131 | |
132 | remove(from); | |
133 | remove(to); | |
14905251 JW |
134 | } |
135 | ||
136 | // Test is_directory(f) case. | |
137 | void | |
138 | test04() | |
139 | { | |
14905251 JW |
140 | auto from = __gnu_test::nonexistent_path(); |
141 | auto to = __gnu_test::nonexistent_path(); | |
142 | std::error_code ec; | |
143 | ||
ec04aad7 JW |
144 | create_directories(from/"a/b/c"); |
145 | ||
146 | { | |
147 | __gnu_test::scoped_file f(to); | |
148 | copy(from, to, ec); | |
149 | VERIFY( ec ); | |
150 | } | |
151 | ||
152 | __gnu_test::scoped_file f1(from/"a/f1"); | |
153 | std::ofstream{f1.path} << "file one"; | |
154 | __gnu_test::scoped_file f2(from/"a/b/f2"); | |
155 | std::ofstream{f2.path} << "file two"; | |
156 | ||
157 | copy(from, to, ec); | |
158 | VERIFY( !ec ); | |
159 | VERIFY( exists(to) && is_empty(to) ); | |
160 | remove(to); | |
161 | ||
162 | copy(from, to, fs::copy_options::recursive, ec); | |
163 | VERIFY( !ec ); | |
164 | VERIFY( exists(to) && !is_empty(to) ); | |
165 | VERIFY( is_regular_file(to/"a/f1") && !is_empty(to/"a/f1") ); | |
166 | VERIFY( file_size(from/"a/f1") == file_size(to/"a/f1") ); | |
167 | VERIFY( is_regular_file(to/"a/b/f2") && !is_empty(to/"a/b/f2") ); | |
168 | VERIFY( file_size(from/"a/b/f2") == file_size(to/"a/b/f2") ); | |
169 | VERIFY( is_directory(to/"a/b/c") && is_empty(to/"a/b/c") ); | |
170 | ||
171 | f1.path.clear(); | |
172 | f2.path.clear(); | |
173 | remove_all(from, ec); | |
174 | remove_all(to, ec); | |
14905251 JW |
175 | } |
176 | ||
177 | // Test no-op cases. | |
178 | void | |
179 | test05() | |
180 | { | |
14905251 | 181 | auto to = __gnu_test::nonexistent_path(); |
b3dec9e5 | 182 | std::error_code ec = std::make_error_code(std::errc::invalid_argument); |
14905251 | 183 | |
b3dec9e5 JW |
184 | fs::copy("/", to, fs::copy_options::copy_symlinks, ec); |
185 | VERIFY( !ec ); // Previous value should be cleared (LWG 2683) | |
0ca7ba9a JW |
186 | } |
187 | ||
188 | int | |
189 | main() | |
190 | { | |
191 | test01(); | |
192 | test02(); | |
14905251 JW |
193 | test03(); |
194 | test04(); | |
195 | test05(); | |
0ca7ba9a | 196 | } |