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 770x stream_file::~stream_file()
25 {
26 770x close();
27 770x }
28
29 748x stream_file::stream_file(capy::execution_context& ctx)
30 748x : 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 1496x >(ctx)))
40 {
41 748x }
42
43 std::error_code
44 743x stream_file::open(
45 std::filesystem::path const& path, file_base::flags mode) noexcept
46 {
47 743x if (is_open())
48 2x close();
49 743x auto& svc = static_cast<detail::file_service&>(h_.service());
50 743x return svc.open_file(get(), path, mode);
51 }
52
53 void
54 1309x stream_file::close() noexcept
55 {
56 1309x if (!is_open())
57 186x return;
58 1123x h_.service().close(h_);
59 }
60
61 void
62 4x stream_file::cancel() noexcept
63 {
64 4x if (!is_open())
65 2x return;
66 2x get().cancel();
67 }
68
69 native_handle_type
70 18x stream_file::native_handle() const noexcept
71 {
72 18x 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 16x return get().native_handle();
81 }
82
83 capy::results<std::uint64_t>
84 17x stream_file::size() const noexcept
85 {
86 17x if (!is_open())
87 2x return {make_error_code(std::errc::bad_file_descriptor), 0};
88 15x return get().size();
89 }
90
91 std::error_code
92 14x stream_file::resize(std::uint64_t new_size) noexcept
93 {
94 14x if (!is_open())
95 2x return make_error_code(std::errc::bad_file_descriptor);
96 12x return get().resize(new_size);
97 }
98
99 std::error_code
100 10x stream_file::sync_data() noexcept
101 {
102 10x if (!is_open())
103 2x return make_error_code(std::errc::bad_file_descriptor);
104 8x return get().sync_data();
105 }
106
107 std::error_code
108 10x stream_file::sync_all() noexcept
109 {
110 10x if (!is_open())
111 2x return make_error_code(std::errc::bad_file_descriptor);
112 8x return get().sync_all();
113 }
114
115 capy::results<native_handle_type>
116 16x stream_file::release() noexcept
117 {
118 16x if (!is_open())
119 return {
120 4x make_error_code(std::errc::bad_file_descriptor),
121 2x detail::invalid_native_handle};
122 14x return get().release();
123 }
124
125 std::error_code
126 419x stream_file::assign(native_handle_type handle) noexcept
127 {
128 419x if (is_open())
129 6x return make_error_code(error::already_open);
130 413x return get().assign(handle);
131 }
132
133 capy::results<std::uint64_t>
134 437x stream_file::seek(std::int64_t offset, file_base::seek_basis origin) noexcept
135 {
136 437x if (!is_open())
137 2x return {make_error_code(std::errc::bad_file_descriptor), 0};
138 435x return get().seek(offset, origin);
139 }
140
141 } // namespace boost::corosio
142