• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03
11 
12 // <filesystem>
13 
14 // bool copy_file(const path& from, const path& to);
15 // bool copy_file(const path& from, const path& to, error_code& ec) noexcept;
16 // bool copy_file(const path& from, const path& to, copy_options options);
17 // bool copy_file(const path& from, const path& to, copy_options options,
18 //           error_code& ec) noexcept;
19 
20 #include "filesystem_include.hpp"
21 #include <type_traits>
22 #include <chrono>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "rapid-cxx-test.hpp"
27 #include "filesystem_test_helper.hpp"
28 
29 #include <iostream>
30 
31 using namespace fs;
32 
33 using CO = fs::copy_options;
34 
35 TEST_SUITE(filesystem_copy_file_test_suite)
36 
TEST_CASE(test_signatures)37 TEST_CASE(test_signatures) {
38   const path p;
39   ((void)p);
40   const copy_options opts{};
41   ((void)opts);
42   std::error_code ec;
43   ((void)ec);
44   ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p)), bool);
45   ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts)), bool);
46   ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, ec)), bool);
47   ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts, ec)), bool);
48   ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p));
49   ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts));
50   ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, ec));
51   ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts, ec));
52 }
53 
TEST_CASE(test_error_reporting)54 TEST_CASE(test_error_reporting) {
55 
56   scoped_test_env env;
57   const path file = env.create_file("file1", 42);
58   const path file2 = env.create_file("file2", 55);
59   const path non_regular_file = env.create_fifo("non_reg");
60   const path dne = env.make_env_path("dne");
61 
62   { // exists(to) && equivalent(to, from)
63     std::error_code ec;
64     TEST_CHECK(fs::copy_file(file, file, copy_options::overwrite_existing,
65                              ec) == false);
66     TEST_CHECK(ErrorIs(ec, std::errc::file_exists));
67     ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file");
68     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing));
69 
70   }
71   { // exists(to) && !(skip_existing | overwrite_existing | update_existing)
72     std::error_code ec;
73     TEST_CHECK(fs::copy_file(file, file2, ec) == false);
74     TEST_CHECK(ErrorIs(ec, std::errc::file_exists));
75     ExceptionChecker Checker(file, file, std::errc::file_exists, "copy_file");
76     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, copy_file(file, file, copy_options::overwrite_existing));
77 
78   }
79 }
80 
TEST_CASE(non_regular_file_test)81 TEST_CASE(non_regular_file_test) {
82   scoped_test_env env;
83   const path fifo = env.create_fifo("fifo");
84   const path dest = env.make_env_path("dest");
85   const path file = env.create_file("file", 42);
86 
87   {
88     std::error_code ec = GetTestEC();
89     TEST_REQUIRE(fs::copy_file(fifo, dest, ec) == false);
90     TEST_CHECK(ErrorIs(ec, std::errc::not_supported));
91     TEST_CHECK(!exists(dest));
92   }
93   {
94     std::error_code ec = GetTestEC();
95     TEST_REQUIRE(fs::copy_file(file, fifo, copy_options::overwrite_existing,
96                                ec) == false);
97     TEST_CHECK(ErrorIs(ec, std::errc::not_supported));
98     TEST_CHECK(is_fifo(fifo));
99   }
100 
101 }
102 
TEST_CASE(test_attributes_get_copied)103 TEST_CASE(test_attributes_get_copied) {
104   scoped_test_env env;
105   const path file = env.create_file("file1", 42);
106   const path dest = env.make_env_path("file2");
107   auto st = status(file);
108   perms new_perms = perms::owner_read;
109   permissions(file, new_perms);
110   std::error_code ec = GetTestEC();
111   TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
112   TEST_CHECK(!ec);
113   auto new_st = status(dest);
114   TEST_CHECK(new_st.permissions() == new_perms);
115 }
116 
TEST_CASE(copy_dir_test)117 TEST_CASE(copy_dir_test) {
118   scoped_test_env env;
119   const path file = env.create_file("file1", 42);
120   const path dest = env.create_dir("dir1");
121   std::error_code ec = GetTestEC();
122   TEST_CHECK(fs::copy_file(file, dest, ec) == false);
123   TEST_CHECK(ec);
124   TEST_CHECK(ec != GetTestEC());
125   ec = GetTestEC();
126   TEST_CHECK(fs::copy_file(dest, file, ec) == false);
127   TEST_CHECK(ec);
128   TEST_CHECK(ec != GetTestEC());
129 }
130 
TEST_CASE(copy_file)131 TEST_CASE(copy_file) {
132   scoped_test_env env;
133   const path file = env.create_file("file1", 42);
134 
135   { // !exists(to)
136     const path dest = env.make_env_path("dest1");
137     std::error_code ec = GetTestEC();
138 
139     TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
140     TEST_CHECK(!ec);
141     TEST_CHECK(file_size(dest) == 42);
142   }
143   { // exists(to) && overwrite_existing
144     const path dest = env.create_file("dest2", 55);
145     permissions(dest, perms::all);
146     permissions(file,
147                 perms::group_write | perms::owner_write | perms::others_write,
148                 perm_options::remove);
149 
150     std::error_code ec = GetTestEC();
151     TEST_REQUIRE(fs::copy_file(file, dest, copy_options::overwrite_existing,
152                                ec) == true);
153     TEST_CHECK(!ec);
154     TEST_CHECK(file_size(dest) == 42);
155     TEST_CHECK(status(dest).permissions() == status(file).permissions());
156   }
157   { // exists(to) && update_existing
158     using Sec = std::chrono::seconds;
159     const path older = env.create_file("older_file", 1);
160 
161     SleepFor(Sec(2));
162     const path from = env.create_file("update_from", 55);
163 
164     SleepFor(Sec(2));
165     const path newer = env.create_file("newer_file", 2);
166 
167     std::error_code ec = GetTestEC();
168     TEST_REQUIRE(
169         fs::copy_file(from, older, copy_options::update_existing, ec) == true);
170     TEST_CHECK(!ec);
171     TEST_CHECK(file_size(older) == 55);
172 
173     TEST_REQUIRE(
174         fs::copy_file(from, newer, copy_options::update_existing, ec) == false);
175     TEST_CHECK(!ec);
176     TEST_CHECK(file_size(newer) == 2);
177   }
178   { // skip_existing
179     const path file2 = env.create_file("file2", 55);
180     std::error_code ec = GetTestEC();
181     TEST_REQUIRE(fs::copy_file(file, file2, copy_options::skip_existing, ec) ==
182                  false);
183     TEST_CHECK(!ec);
184     TEST_CHECK(file_size(file2) == 55);
185   }
186 }
187 
188 
189 TEST_SUITE_END()
190