src/detail/send_file.cpp

100.0% Lines (2/2) 100.0% List of functions (1/1) 100.0% Branches (1/1)
send_file.cpp
f(x) Functions (1)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Mohammad Nejati
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/burl
8 //
9
10 #include "send_file.hpp"
11
12 #include "util.hpp"
13
14 #include <boost/burl/error.hpp>
15 #include <boost/capy/cond.hpp>
16 #include <boost/capy/ex/this_coro.hpp>
17 #include <boost/capy/io/any_buffer_sink.hpp>
18 #include <boost/corosio/file_base.hpp>
19 #include <boost/corosio/stream_file.hpp>
20
21 #include <system_error>
22
23 namespace boost
24 {
25 namespace burl
26 {
27 namespace detail
28 {
29
30 capy::io_task<>
31
1/1
✓ Branch 1 taken 10 times.
10x send_file(
32 capy::any_buffer_sink& sink,
33 std::filesystem::path const& path,
34 std::uint64_t size,
35 bool call_eof)
36 {
37 corosio::stream_file f(co_await capy::this_coro::executor);
38 // TODO: switch to a non-throwing open() overload once available.
39 try
40 {
41 f.open(path, corosio::file_base::read_only);
42 }
43 catch(std::system_error const& e)
44 {
45 co_return { e.code() };
46 }
47
48 auto remaining = size;
49 while(remaining > 0)
50 {
51 capy::mutable_buffer arr[2];
52 auto dst = sink.prepare(arr);
53 if(dst.empty())
54 {
55 if(auto [ec] = co_await sink.commit(0); ec)
56 co_return { ec };
57 continue;
58 }
59
60 auto [rec, n] = co_await f.read_some(dst);
61
62 auto take = clamp(remaining, n);
63 if(take)
64 {
65 if(call_eof && take == remaining)
66 {
67 if(auto [ec] = co_await sink.commit_eof(take); ec)
68 co_return { ec };
69 }
70 if(auto [ec] = co_await sink.commit(take); ec)
71 co_return { ec };
72 remaining -= take;
73 }
74
75 if(remaining == 0)
76 break;
77 if(rec == capy::cond::eof)
78 co_return { error::file_changed };
79 if(rec)
80 co_return { rec };
81 }
82
83 co_return {};
84 20x }
85
86 } // namespace detail
87 } // namespace burl
88 } // namespace boost
89