1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
// Copyright Andrey Semashev 2020.
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Library home page: http://www.boost.org/libs/filesystem
// This test verifies copy operation behavior.
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/directory.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/fstream.hpp> // for BOOST_FILESYSTEM_C_STR
#include <boost/system/error_code.hpp>
#include <set>
#include <string>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <boost/throw_exception.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/core/lightweight_test.hpp>
namespace fs = boost::filesystem;
namespace {
void create_file(fs::path const& ph, std::string const& contents = std::string())
{
std::ofstream f(BOOST_FILESYSTEM_C_STR(ph), std::ios_base::out | std::ios_base::trunc);
if (!f)
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create file: " + ph.string()));
if (!contents.empty())
f << contents;
}
void verify_file(fs::path const& ph, std::string const& expected)
{
std::ifstream f(BOOST_FILESYSTEM_C_STR(ph));
if (!f)
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to open file: " + ph.string()));
std::string contents;
f >> contents;
BOOST_TEST_EQ(contents, expected);
if (contents != expected)
{
BOOST_THROW_EXCEPTION(std::runtime_error("verify_file failed: contents \"" + contents + "\" != \"" + expected + "\" in " + ph.string()));
}
}
fs::path create_tree()
{
fs::path root_dir = fs::unique_path();
fs::create_directory(root_dir);
create_file(root_dir / "f1", "f1");
create_file(root_dir / "f2", "f2");
fs::create_directory(root_dir / "d1");
create_file(root_dir / "d1/f1", "d1f1");
fs::create_directory(root_dir / "d1/d1");
create_file(root_dir / "d1/d1/f1", "d1d1f1");
fs::create_directory(root_dir / "d1/d2");
fs::create_directory(root_dir / "d2");
create_file(root_dir / "d2/f1", "d2f1");
return root_dir;
}
typedef std::set< fs::path > directory_tree;
directory_tree collect_directory_tree(fs::path const& root_dir)
{
std::cout << "Collecting directory tree in: " << root_dir << '\n';
directory_tree tree;
fs::recursive_directory_iterator it(root_dir, fs::directory_options::skip_permission_denied |
fs::directory_options::follow_directory_symlink | fs::directory_options::skip_dangling_symlinks);
fs::recursive_directory_iterator end;
while (it != end)
{
fs::path p = fs::relative(it->path(), root_dir);
std::cout << p << '\n';
tree.insert(p);
++it;
}
std::cout << "done." << std::endl;
return tree;
}
void test_copy_file_default(fs::path const& root_dir)
{
std::cout << "test_copy_file_default" << std::endl;
fs::path target_dir = fs::unique_path();
fs::create_directory(target_dir);
fs::copy(root_dir / "f1", target_dir);
fs::copy(root_dir / "f2", target_dir / "f3");
directory_tree tree = collect_directory_tree(target_dir);
BOOST_TEST_EQ(tree.size(), 2u);
BOOST_TEST(tree.find("f1") != tree.end());
BOOST_TEST(tree.find("f3") != tree.end());
verify_file(target_dir / "f1", "f1");
verify_file(target_dir / "f3", "f2");
fs::remove_all(target_dir);
}
void test_copy_dir_default(fs::path const& root_dir, bool with_symlinks)
{
std::cout << "test_copy_dir_default" << std::endl;
fs::path target_dir = fs::unique_path();
fs::copy(root_dir, target_dir);
directory_tree tree = collect_directory_tree(target_dir);
BOOST_TEST_EQ(tree.size(), 4u + with_symlinks);
BOOST_TEST(tree.find("f1") != tree.end());
BOOST_TEST(tree.find("f2") != tree.end());
BOOST_TEST(tree.find("d1") != tree.end());
BOOST_TEST(tree.find("d2") != tree.end());
if (with_symlinks)
{
BOOST_TEST(tree.find("s1") != tree.end());
}
verify_file(target_dir / "f1", "f1");
verify_file(target_dir / "f2", "f2");
fs::remove_all(target_dir);
}
void test_copy_dir_default_ec(fs::path const& root_dir, bool with_symlinks)
{
// This test is similar to test_copy_dir_default, but uses an error_code overload of the operation.
// Tests for https://github.com/boostorg/filesystem/issues/152 fix.
std::cout << "test_copy_dir_default_ec" << std::endl;
fs::path target_dir = fs::unique_path();
boost::system::error_code ec;
fs::copy(root_dir, target_dir, ec);
BOOST_TEST(!ec);
directory_tree tree = collect_directory_tree(target_dir);
BOOST_TEST_EQ(tree.size(), 4u + with_symlinks);
BOOST_TEST(tree.find("f1") != tree.end());
BOOST_TEST(tree.find("f2") != tree.end());
BOOST_TEST(tree.find("d1") != tree.end());
BOOST_TEST(tree.find("d2") != tree.end());
if (with_symlinks)
{
BOOST_TEST(tree.find("s1") != tree.end());
}
verify_file(target_dir / "f1", "f1");
verify_file(target_dir / "f2", "f2");
fs::remove_all(target_dir);
}
void test_copy_dir_recursive(fs::path const& root_dir)
{
std::cout << "test_copy_dir_recursive" << std::endl;
fs::path target_dir = fs::unique_path();
fs::copy(root_dir, target_dir, fs::copy_options::recursive);
directory_tree tree = collect_directory_tree(target_dir);
BOOST_TEST_EQ(tree.size(), 9u);
BOOST_TEST(tree.find("f1") != tree.end());
BOOST_TEST(tree.find("f2") != tree.end());
BOOST_TEST(tree.find("d1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "f1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "d1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "d1" / "f1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "d2") != tree.end());
BOOST_TEST(tree.find("d2") != tree.end());
BOOST_TEST(tree.find(fs::path("d2") / "f1") != tree.end());
verify_file(target_dir / "f1", "f1");
verify_file(target_dir / "f2", "f2");
verify_file(target_dir / "d1/f1", "d1f1");
verify_file(target_dir / "d1/d1/f1", "d1d1f1");
verify_file(target_dir / "d2/f1", "d2f1");
fs::remove_all(target_dir);
}
void test_copy_dir_recursive_tree(fs::path const& root_dir)
{
std::cout << "test_copy_dir_recursive_tree" << std::endl;
fs::path target_dir = fs::unique_path();
fs::copy(root_dir, target_dir, fs::copy_options::recursive | fs::copy_options::directories_only);
directory_tree tree = collect_directory_tree(target_dir);
BOOST_TEST_EQ(tree.size(), 4u);
BOOST_TEST(tree.find("d1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "d1") != tree.end());
BOOST_TEST(tree.find(fs::path("d1") / "d2") != tree.end());
BOOST_TEST(tree.find("d2") != tree.end());
fs::remove_all(target_dir);
}
void test_copy_file_symlinks(fs::path const& root_dir)
{
std::cout << "test_copy_file_symlinks" << std::endl;
fs::path target_dir = fs::unique_path();
fs::create_directory(target_dir);
fs::copy(root_dir / "f1", target_dir);
fs::path prev_cur_dir = fs::current_path();
fs::current_path(target_dir);
fs::copy(".." / root_dir / "f2", "f2", fs::copy_options::create_symlinks);
fs::current_path(prev_cur_dir);
// Copying from a relative path with copy_options::create_symlinks to a directory other than current directory is a non-standard extension
fs::copy(target_dir / "f1", target_dir / "f3", fs::copy_options::create_symlinks);
verify_file(target_dir / "f1", "f1");
fs::path link_target = fs::read_symlink(target_dir / "f2");
if (link_target != (".." / root_dir / "f2"))
{
BOOST_ERROR("Incorrect f2 symlink in test_copy_file_symlinks");
std::cout << (target_dir / "f2") << " => " << link_target << std::endl;
}
link_target = fs::read_symlink(target_dir / "f3");
if (link_target != "f1" && link_target != (fs::path(".") / "f1"))
{
BOOST_ERROR("Incorrect f3 symlink in test_copy_file_symlinks");
std::cout << (target_dir / "f3") << " => " << link_target << std::endl;
}
fs::remove_all(target_dir);
}
void test_copy_errors(fs::path const& root_dir, bool symlinks_supported)
{
std::cout << "test_copy_errors" << std::endl;
fs::path target_dir = fs::unique_path();
fs::create_directory(target_dir);
BOOST_TEST_THROWS(fs::copy(root_dir / "non-existing", target_dir), fs::filesystem_error);
create_file(target_dir / "f1");
BOOST_TEST_THROWS(fs::copy(root_dir / "f1", target_dir), fs::filesystem_error);
BOOST_TEST_THROWS(fs::copy(root_dir / "f1", target_dir / "f1"), fs::filesystem_error);
BOOST_TEST_THROWS(fs::copy(root_dir / "d1", target_dir / "f1"), fs::filesystem_error);
BOOST_TEST_THROWS(fs::copy(target_dir, target_dir), fs::filesystem_error);
BOOST_TEST_THROWS(fs::copy(target_dir / "f1", target_dir / "f1"), fs::filesystem_error);
if (symlinks_supported)
{
// Should fail with is_a_directory error code
BOOST_TEST_THROWS(fs::copy(root_dir, target_dir, fs::copy_options::create_symlinks), fs::filesystem_error);
}
fs::remove_all(target_dir);
}
} // namespace
int main()
{
try
{
fs::path root_dir = create_tree();
test_copy_file_default(root_dir);
test_copy_dir_default(root_dir, false);
test_copy_dir_default_ec(root_dir, false);
test_copy_dir_recursive(root_dir);
test_copy_dir_recursive_tree(root_dir);
bool symlinks_supported = false;
try
{
fs::create_symlink("f1", root_dir / "s1");
symlinks_supported = true;
std::cout << " *** For information only ***\n"
" create_symlink() attempt succeeded"
<< std::endl;
}
catch (fs::filesystem_error& e)
{
std::cout << " *** For information only ***\n"
" create_symlink() attempt failed\n"
" filesystem_error.what() reports: "
<< e.what() << "\n"
" create_symlink() may not be supported on this operating system or file system"
<< std::endl;
}
if (symlinks_supported)
{
test_copy_dir_default(root_dir, true);
test_copy_file_symlinks(root_dir);
}
test_copy_errors(root_dir, symlinks_supported);
fs::remove_all(root_dir);
return boost::report_errors();
}
catch (std::exception& e)
{
std::cout << "FAIL, exception caught: " << boost::diagnostic_information(e) << std::endl;
return 1;
}
}
|