src/corosio/src/stream_file.cpp

100.0% Lines (53 / 53) 100.0% Functions (13 / 13)
stream_file.cpp
f(x) Functions (13)
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/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 920x stream_file::~stream_file()
25 {
26 920x close();
27 920x }
28
29 889x stream_file::stream_file(capy::execution_context& ctx)
30 889x : io_object(handle(
31 ctx,
32 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 1778x >(ctx)))
40 {
41 889x }
42
43 std::error_code
44 882x stream_file::open(
45 std::filesystem::path const& path, file_base::flags mode) noexcept
46 {
47 882x if (is_open())
48 3x close();
49 882x auto& svc = static_cast<detail::file_service&>(h_.service());
50 882x return svc.open_file(get(), path, mode);
51 }
52
53 void
54 1529x stream_file::close() noexcept
55 {
56 1529x if (!is_open())
57 276x return;
58 1253x h_.service().close(h_);
59 }
60
61 void
62 6x stream_file::cancel() noexcept
63 {
64 6x if (!is_open())
65 3x return;
66 3x get().cancel();
67 }
68
69 native_handle_type
70 28x stream_file::native_handle() const noexcept
71 {
72 28x if (!is_open())
73 {
74 #if BOOST_COROSIO_HAS_IOCP
75 return static_cast<native_handle_type>(~0ull);
76 #else
77 3x return -1;
78 #endif
79 }
80 25x return get().native_handle();
81 }
82
83 capy::results<std::uint64_t>
84 26x stream_file::size() const noexcept
85 {
86 26x if (!is_open())
87 3x return {make_error_code(std::errc::bad_file_descriptor), 0};
88 23x return get().size();
89 }
90
91 std::error_code
92 22x stream_file::resize(std::uint64_t new_size) noexcept
93 {
94 22x if (!is_open())
95 3x return make_error_code(std::errc::bad_file_descriptor);
96 19x return get().resize(new_size);
97 }
98
99 std::error_code
100 16x stream_file::sync_data() noexcept
101 {
102 16x if (!is_open())
103 3x return make_error_code(std::errc::bad_file_descriptor);
104 13x return get().sync_data();
105 }
106
107 std::error_code
108 16x stream_file::sync_all() noexcept
109 {
110 16x if (!is_open())
111 3x return make_error_code(std::errc::bad_file_descriptor);
112 13x return get().sync_all();
113 }
114
115 capy::results<native_handle_type>
116 22x stream_file::release() noexcept
117 {
118 22x if (!is_open())
119 return {
120 6x make_error_code(std::errc::bad_file_descriptor),
121 3x detail::invalid_native_handle};
122 19x return get().release();
123 }
124
125 std::error_code
126 426x stream_file::assign(native_handle_type handle) noexcept
127 {
128 426x if (is_open())
129 8x return make_error_code(error::already_open);
130 418x return get().assign(handle);
131 }
132
133 capy::results<std::uint64_t>
134 455x stream_file::seek(std::int64_t offset, file_base::seek_basis origin) noexcept
135 {
136 455x if (!is_open())
137 3x return {make_error_code(std::errc::bad_file_descriptor), 0};
138 452x return get().seek(offset, origin);
139 }
140
141 } // namespace boost::corosio
142