src/corosio/src/random_access_file.cpp

100.0% Lines (50/50) 100.0% List of functions (12/12) 100.0% Branches (22/22)
random_access_file.cpp
f(x) Functions (12)
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/random_access_file.hpp>
11 #include <boost/corosio/error.hpp>
12 #include <boost/corosio/detail/except.hpp>
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_IOCP
16 #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
17 #else
18 #include <boost/corosio/detail/random_access_file_service.hpp>
19 #endif
20
21 #include "src/detail/use_backend_service.hpp"
22
23 namespace boost::corosio {
24
25 116x random_access_file::~random_access_file()
26 {
27 116x close();
28 116x }
29
30 108x random_access_file::random_access_file(capy::execution_context& ctx)
31 108x : io_object(handle(
32 ctx,
33 detail::use_backend_service<
34 detail::random_access_file_service_of,
35 #if BOOST_COROSIO_HAS_IOCP
36 detail::win_random_access_file_service
37 #else
38 detail::random_access_file_service
39 #endif
40
2/2
✓ Branch 2 → 3 taken 108 times.
✓ Branch 3 → 4 taken 108 times.
108x >(ctx)))
41 {
42 108x }
43
44 std::error_code
45 119x random_access_file::open(
46 std::filesystem::path const& path, file_base::flags mode) noexcept
47 {
48
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 118 times.
119x if (is_open())
49 1x close();
50 119x auto& svc = static_cast<detail::random_access_file_service&>(h_.service());
51 119x return svc.open_file(get(), path, mode);
52 }
53
54 void
55 175x random_access_file::close() noexcept
56 {
57
2/2
✓ Branch 3 → 4 taken 78 times.
✓ Branch 3 → 5 taken 97 times.
175x if (!is_open())
58 78x return;
59 97x h_.service().close(h_);
60 }
61
62 void
63 3x random_access_file::cancel() noexcept
64 {
65
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2 times.
3x if (!is_open())
66 1x return;
67 2x get().cancel();
68 }
69
70 native_handle_type
71 8x random_access_file::native_handle() const noexcept
72 {
73
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 7 times.
8x if (!is_open())
74 {
75 #if BOOST_COROSIO_HAS_IOCP
76 1x return static_cast<native_handle_type>(~0ull);
77 #else
78 return -1;
79 #endif
80 }
81 7x return get().native_handle();
82 }
83
84 std::uint64_t
85 6x random_access_file::size() const
86 {
87
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 5 times.
6x if (!is_open())
88 1x detail::throw_system_error(
89 1x make_error_code(std::errc::bad_file_descriptor),
90 "random_access_file::size");
91 5x return get().size();
92 }
93
94 std::error_code
95 6x random_access_file::resize(std::uint64_t new_size) noexcept
96 {
97
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 5 times.
6x if (!is_open())
98 1x return make_error_code(std::errc::bad_file_descriptor);
99 5x return get().resize(new_size);
100 }
101
102 std::error_code
103 4x random_access_file::sync_data() noexcept
104 {
105
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 3 times.
4x if (!is_open())
106 1x return make_error_code(std::errc::bad_file_descriptor);
107 3x return get().sync_data();
108 }
109
110 std::error_code
111 3x random_access_file::sync_all() noexcept
112 {
113
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 2 times.
3x if (!is_open())
114 1x return make_error_code(std::errc::bad_file_descriptor);
115 2x return get().sync_all();
116 }
117
118 native_handle_type
119 6x random_access_file::release()
120 {
121
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 5 times.
6x if (!is_open())
122 1x detail::throw_system_error(
123 1x make_error_code(std::errc::bad_file_descriptor),
124 "random_access_file::release");
125 5x return get().release();
126 }
127
128 std::error_code
129 11x random_access_file::assign(native_handle_type handle) noexcept
130 {
131
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 8 times.
11x if (is_open())
132 3x return make_error_code(error::already_open);
133 8x return get().assign(handle);
134 }
135
136 } // namespace boost::corosio
137