]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/files/blocking_file_copy.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / files / blocking_file_copy.cpp
1 //
2 // blocking_file_copy.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <iostream>
12 #include <boost/asio.hpp>
13
14 #if defined(BOOST_ASIO_HAS_FILE)
15
16 int main(int argc, char* argv[])
17 {
18 try
19 {
20 if (argc != 3)
21 {
22 std::cerr << "Usage: blocking_file_copy <from> <to>\n";
23 return 1;
24 }
25
26 boost::asio::io_context io_context;
27
28 boost::asio::stream_file from_file(io_context, argv[1],
29 boost::asio::stream_file::read_only);
30
31 boost::asio::stream_file to_file(io_context, argv[2],
32 boost::asio::stream_file::write_only
33 | boost::asio::stream_file::create
34 | boost::asio::stream_file::truncate);
35
36 char data[4096];
37 boost::system::error_code error;
38 for (;;)
39 {
40 std::size_t n = from_file.read_some(boost::asio::buffer(data), error);
41 if (error)
42 break;
43 boost::asio::write(to_file, boost::asio::buffer(data, n), error);
44 if (error)
45 break;
46 }
47
48 if (error && error != boost::asio::error::eof)
49 {
50 std::cerr << "Error copying file: " << error.message() << "\n";
51 return 1;
52 }
53 }
54 catch (std::exception& e)
55 {
56 std::cerr << "Exception: " << e.what() << "\n";
57 return 1;
58 }
59
60 return 0;
61 }
62
63 #else // defined(BOOST_ASIO_HAS_FILE)
64 int main() {}
65 #endif // defined(BOOST_ASIO_HAS_FILE)