src/corosio/src/random_access_file.cpp

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