src/corosio/src/stream_file.cpp

100.0% Lines (67 / 67) 92.3% Branches (24 / 26) 100.0% Functions (13 / 13)
stream_file.cpp
f(x) Functions (13)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
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/corosio
8 //
9
10 #include <boost/corosio/stream_file.hpp>
11 #include <boost/corosio/error.hpp>
12 #include <boost/corosio/detail/platform.hpp>
13
14 #if BOOST_COROSIO_HAS_IOCP
15 #include <boost/corosio/native/detail/iocp/win_file_service.hpp>
16 #else
17 #include <boost/corosio/detail/file_service.hpp>
18 #endif
19
20 #include "src/detail/use_backend_service.hpp"
21
22 namespace boost::corosio {
23
24 1414x stream_file::~stream_file()
25 1414x {
26 715x close();
27 1414x }
28
29 1386x stream_file::stream_file(capy::execution_context& ctx)
30 693x : io_object(handle(
31 693x ctx,
32 693x detail::use_backend_service<
33 detail::stream_file_service_of,
34 #if BOOST_COROSIO_HAS_IOCP
35 detail::win_file_service
36 #else
37 detail::file_service
38 #endif
39 693x >(ctx)))
40 1386x {
41 693x }
42
43 std::error_code
44 685x stream_file::open(
45 std::filesystem::path const& path, file_base::flags mode) noexcept
46 {
47
2/2
✓ Branch 0 taken 683 times.
✓ Branch 1 taken 2 times.
685x if (is_open())
48 2x close();
49 685x auto& svc = static_cast<detail::file_service&>(h_.service());
50
1/2
✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
685x return svc.open_file(get(), path, mode);
51 }
52
53 void
54 1239x stream_file::close() noexcept
55 {
56
2/2
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 168 times.
1239x if (!is_open())
57 168x return;
58
1/2
✓ Branch 0 taken 1071 times.
✗ Branch 1 not taken.
1071x h_.service().close(h_);
59 1239x }
60
61 void
62 4x stream_file::cancel() noexcept
63 {
64
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4x if (!is_open())
65 2x return;
66 2x get().cancel();
67 4x }
68
69 native_handle_type
70 12x stream_file::native_handle() const noexcept
71 {
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12x if (!is_open())
73 {
74 #if BOOST_COROSIO_HAS_IOCP
75 return static_cast<native_handle_type>(~0ull);
76 #else
77 2x return -1;
78 #endif
79 }
80 10x return get().native_handle();
81 12x }
82
83 capy::results<std::uint64_t>
84 14x stream_file::size() const noexcept
85 {
86
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14x if (!is_open())
87 2x return {make_error_code(std::errc::bad_file_descriptor), 0};
88 12x return get().size();
89 14x }
90
91 std::error_code
92 11x stream_file::resize(std::uint64_t new_size) noexcept
93 {
94
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11x if (!is_open())
95 2x return make_error_code(std::errc::bad_file_descriptor);
96 9x return get().resize(new_size);
97 11x }
98
99 std::error_code
100 7x stream_file::sync_data() noexcept
101 {
102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7x if (!is_open())
103 2x return make_error_code(std::errc::bad_file_descriptor);
104 5x return get().sync_data();
105 7x }
106
107 std::error_code
108 7x stream_file::sync_all() noexcept
109 {
110
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7x if (!is_open())
111 2x return make_error_code(std::errc::bad_file_descriptor);
112 5x return get().sync_all();
113 7x }
114
115 capy::results<native_handle_type>
116 10x stream_file::release() noexcept
117 {
118
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10x if (!is_open())
119 2x return {
120 2x make_error_code(std::errc::bad_file_descriptor),
121 detail::invalid_native_handle};
122 8x return get().release();
123 10x }
124
125 std::error_code
126 416x stream_file::assign(native_handle_type handle) noexcept
127 {
128
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 410 times.
416x if (is_open())
129 6x return make_error_code(error::already_open);
130 410x return get().assign(handle);
131 416x }
132
133 capy::results<std::uint64_t>
134 428x stream_file::seek(std::int64_t offset, file_base::seek_basis origin) noexcept
135 {
136
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 426 times.
428x if (!is_open())
137 2x return {make_error_code(std::errc::bad_file_descriptor), 0};
138 426x return get().seek(offset, origin);
139 428x }
140
141 } // namespace boost::corosio
142