src/corosio/src/random_access_file.cpp

100.0% Lines (48 / 48) 100.0% Functions (12 / 12)
random_access_file.cpp
f(x) Functions (12)
Line 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 #include "src/detail/use_backend_service.hpp"
21
22 namespace boost::corosio {
23
24 221x random_access_file::~random_access_file()
25 {
26 221x close();
27 221x }
28
29 205x random_access_file::random_access_file(capy::execution_context& ctx)
30 205x : 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 205x >(ctx)))
40 {
41 205x }
42
43 std::error_code
44 205x random_access_file::open(
45 std::filesystem::path const& path, file_base::flags mode) noexcept
46 {
47 205x if (is_open())
48 2x close();
49 205x auto& svc = static_cast<detail::random_access_file_service&>(h_.service());
50 205x return svc.open_file(get(), path, mode);
51 }
52
53 void
54 339x random_access_file::close() noexcept
55 {
56 339x if (!is_open())
57 145x return;
58 194x h_.service().close(h_);
59 }
60
61 void
62 6x random_access_file::cancel() noexcept
63 {
64 6x if (!is_open())
65 2x return;
66 4x get().cancel();
67 }
68
69 native_handle_type
70 12x random_access_file::native_handle() const noexcept
71 {
72 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 }
82
83 std::uint64_t
84 15x random_access_file::size() const
85 {
86 15x if (!is_open())
87 2x detail::throw_system_error(
88 2x make_error_code(std::errc::bad_file_descriptor),
89 "random_access_file::size");
90 13x return get().size();
91 }
92
93 std::error_code
94 15x random_access_file::resize(std::uint64_t new_size) noexcept
95 {
96 15x if (!is_open())
97 2x return make_error_code(std::errc::bad_file_descriptor);
98 13x return get().resize(new_size);
99 }
100
101 std::error_code
102 9x random_access_file::sync_data() noexcept
103 {
104 9x if (!is_open())
105 2x return make_error_code(std::errc::bad_file_descriptor);
106 7x return get().sync_data();
107 }
108
109 std::error_code
110 9x random_access_file::sync_all() noexcept
111 {
112 9x if (!is_open())
113 2x return make_error_code(std::errc::bad_file_descriptor);
114 7x return get().sync_all();
115 }
116
117 native_handle_type
118 5x random_access_file::release()
119 {
120 5x if (!is_open())
121 2x detail::throw_system_error(
122 2x make_error_code(std::errc::bad_file_descriptor),
123 "random_access_file::release");
124 3x return get().release();
125 }
126
127 std::error_code
128 13x random_access_file::assign(native_handle_type handle) noexcept
129 {
130 // Closing here first, unconditionally, would defeat the impl's own
131 // validate-before-mutate contract: get().assign() validates handle
132 // and only then closes whatever this object currently holds.
133 13x return get().assign(handle);
134 }
135
136 } // namespace boost::corosio
137