src/corosio/src/random_access_file.cpp

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