100.00% Lines (38/38) 100.00% Functions (11/11)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Michael Vandeberg 2   // Copyright (c) 2026 Michael Vandeberg
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP 10   #ifndef BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP
11   #define BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP 11   #define BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP
12   12  
13   #include <boost/corosio/detail/config.hpp> 13   #include <boost/corosio/detail/config.hpp>
14   #include <boost/corosio/detail/platform.hpp> 14   #include <boost/corosio/detail/platform.hpp>
15   #include <boost/corosio/detail/except.hpp> 15   #include <boost/corosio/detail/except.hpp>
16   #include <boost/corosio/detail/native_handle.hpp> 16   #include <boost/corosio/detail/native_handle.hpp>
17   #include <boost/corosio/detail/buffer_param.hpp> 17   #include <boost/corosio/detail/buffer_param.hpp>
18   #include <boost/corosio/detail/op_base.hpp> 18   #include <boost/corosio/detail/op_base.hpp>
19   #include <boost/corosio/file_base.hpp> 19   #include <boost/corosio/file_base.hpp>
20   #include <boost/corosio/io/io_object.hpp> 20   #include <boost/corosio/io/io_object.hpp>
21   #include <boost/capy/io_result.hpp> 21   #include <boost/capy/io_result.hpp>
22   #include <boost/capy/ex/executor_ref.hpp> 22   #include <boost/capy/ex/executor_ref.hpp>
23   #include <boost/capy/ex/execution_context.hpp> 23   #include <boost/capy/ex/execution_context.hpp>
24   #include <boost/capy/ex/io_env.hpp> 24   #include <boost/capy/ex/io_env.hpp>
25   #include <boost/capy/concept/executor.hpp> 25   #include <boost/capy/concept/executor.hpp>
26   #include <boost/capy/buffers.hpp> 26   #include <boost/capy/buffers.hpp>
27   27  
28   #include <concepts> 28   #include <concepts>
29   #include <coroutine> 29   #include <coroutine>
30   #include <cstddef> 30   #include <cstddef>
31   #include <cstdint> 31   #include <cstdint>
32   #include <type_traits> 32   #include <type_traits>
33   #include <filesystem> 33   #include <filesystem>
34   #include <stop_token> 34   #include <stop_token>
35   #include <system_error> 35   #include <system_error>
36   36  
37   namespace boost::corosio { 37   namespace boost::corosio {
38   38  
39   /** Reads and writes a file at arbitrary offsets, from a coroutine. 39   /** Reads and writes a file at arbitrary offsets, from a coroutine.
40   40  
41   Provides asynchronous read and write operations at explicit 41   Provides asynchronous read and write operations at explicit
42   byte offsets, without maintaining an implicit file position. 42   byte offsets, without maintaining an implicit file position.
43   43  
44   On POSIX platforms, file I/O is dispatched to a thread pool 44   On POSIX platforms, file I/O is dispatched to a thread pool
45   (blocking `preadv`/`pwritev`) with completion posted back to 45   (blocking `preadv`/`pwritev`) with completion posted back to
46   the scheduler. On Windows, true overlapped I/O is used via IOCP. 46   the scheduler. On Windows, true overlapped I/O is used via IOCP.
47   47  
48   @par Thread Safety 48   @par Thread Safety
49   Distinct objects: Safe.@n 49   Distinct objects: Safe.@n
50   Shared objects: Unsafe. Coroutines sharing the same file object may 50   Shared objects: Unsafe. Coroutines sharing the same file object may
51   run multiple concurrent reads and writes. Non-async operations such as open, close, size, and resize require external synchronization. 51   run multiple concurrent reads and writes. Non-async operations such as open, close, size, and resize require external synchronization.
52   52  
53   @par Example 53   @par Example
54   @par !example random_access_file 54   @par !example random_access_file
55   */ 55   */
56   class BOOST_COROSIO_DECL random_access_file : public io_object 56   class BOOST_COROSIO_DECL random_access_file : public io_object
57   { 57   {
58   public: 58   public:
59   /** Declares the offset-based file operations a platform backend 59   /** Declares the offset-based file operations a platform backend
60   must implement. 60   must implement.
61   61  
62   Backends derive from this to provide offset-based file I/O. 62   Backends derive from this to provide offset-based file I/O.
63   */ 63   */
64   struct implementation : io_object::implementation 64   struct implementation : io_object::implementation
65   { 65   {
66   /** Initiate a read at the given offset. 66   /** Initiate a read at the given offset.
67   67  
68   @param offset Byte offset into the file. 68   @param offset Byte offset into the file.
69   @param h Coroutine handle to resume on completion. 69   @param h Coroutine handle to resume on completion.
70   @param ex Executor for dispatching the completion. 70   @param ex Executor for dispatching the completion.
71   @param buf The buffer to read into. 71   @param buf The buffer to read into.
72   @param token Stop token for cancellation. 72   @param token Stop token for cancellation.
73   @param ec Output error code. 73   @param ec Output error code.
74   @param bytes_out Output bytes transferred. 74   @param bytes_out Output bytes transferred.
75   @return Coroutine handle to resume immediately. 75   @return Coroutine handle to resume immediately.
76   */ 76   */
77   virtual std::coroutine_handle<> read_some_at( 77   virtual std::coroutine_handle<> read_some_at(
78   std::uint64_t offset, 78   std::uint64_t offset,
79   std::coroutine_handle<> h, 79   std::coroutine_handle<> h,
80   capy::executor_ref ex, 80   capy::executor_ref ex,
81   buffer_param buf, 81   buffer_param buf,
82   std::stop_token token, 82   std::stop_token token,
83   std::error_code* ec, 83   std::error_code* ec,
84   std::size_t* bytes_out) = 0; 84   std::size_t* bytes_out) = 0;
85   85  
86   /** Initiate a write at the given offset. 86   /** Initiate a write at the given offset.
87   87  
88   @param offset Byte offset into the file. 88   @param offset Byte offset into the file.
89   @param h Coroutine handle to resume on completion. 89   @param h Coroutine handle to resume on completion.
90   @param ex Executor for dispatching the completion. 90   @param ex Executor for dispatching the completion.
91   @param buf The buffer to write from. 91   @param buf The buffer to write from.
92   @param token Stop token for cancellation. 92   @param token Stop token for cancellation.
93   @param ec Output error code. 93   @param ec Output error code.
94   @param bytes_out Output bytes transferred. 94   @param bytes_out Output bytes transferred.
95   @return Coroutine handle to resume immediately. 95   @return Coroutine handle to resume immediately.
96   */ 96   */
97   virtual std::coroutine_handle<> write_some_at( 97   virtual std::coroutine_handle<> write_some_at(
98   std::uint64_t offset, 98   std::uint64_t offset,
99   std::coroutine_handle<> h, 99   std::coroutine_handle<> h,
100   capy::executor_ref ex, 100   capy::executor_ref ex,
101   buffer_param buf, 101   buffer_param buf,
102   std::stop_token token, 102   std::stop_token token,
103   std::error_code* ec, 103   std::error_code* ec,
104   std::size_t* bytes_out) = 0; 104   std::size_t* bytes_out) = 0;
105   105  
106   /// Return the platform file descriptor or handle. 106   /// Return the platform file descriptor or handle.
107   virtual native_handle_type native_handle() const noexcept = 0; 107   virtual native_handle_type native_handle() const noexcept = 0;
108   108  
109   /// Cancel pending asynchronous operations. 109   /// Cancel pending asynchronous operations.
110   virtual void cancel() noexcept = 0; 110   virtual void cancel() noexcept = 0;
111   111  
112   /// Return the file size in bytes. 112   /// Return the file size in bytes.
113   virtual std::uint64_t size() const = 0; 113   virtual std::uint64_t size() const = 0;
114   114  
115   /** Resize the file to @p new_size bytes. 115   /** Resize the file to @p new_size bytes.
116   116  
117   @param new_size The requested size in bytes. 117   @param new_size The requested size in bytes.
118   118  
119   @return The error code, empty on success. 119   @return The error code, empty on success.
120   */ 120   */
121   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0; 121   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0;
122   122  
123   /** Synchronize file data to stable storage. 123   /** Synchronize file data to stable storage.
124   124  
125   @return The error code, empty on success. 125   @return The error code, empty on success.
126   */ 126   */
127   virtual std::error_code sync_data() noexcept = 0; 127   virtual std::error_code sync_data() noexcept = 0;
128   128  
129   /** Synchronize file data and metadata to stable storage. 129   /** Synchronize file data and metadata to stable storage.
130   130  
131   @return The error code, empty on success. 131   @return The error code, empty on success.
132   */ 132   */
133   virtual std::error_code sync_all() noexcept = 0; 133   virtual std::error_code sync_all() noexcept = 0;
134   134  
135   /// Release ownership of the native handle. 135   /// Release ownership of the native handle.
136   virtual native_handle_type release() = 0; 136   virtual native_handle_type release() = 0;
137   137  
138   /** Adopt an existing native handle. 138   /** Adopt an existing native handle.
139   139  
140   @param handle The native handle to adopt. The implementation takes 140   @param handle The native handle to adopt. The implementation takes
141   ownership and closes it. 141   ownership and closes it.
142   142  
143   @return The error code, empty on success. 143   @return The error code, empty on success.
144   */ 144   */
145   virtual std::error_code assign(native_handle_type handle) noexcept = 0; 145   virtual std::error_code assign(native_handle_type handle) noexcept = 0;
146   }; 146   };
147   147  
148   /** Awaitable for async read-at operations. */ 148   /** Awaitable for async read-at operations. */
149   template<class MutableBufferSequence> 149   template<class MutableBufferSequence>
150   struct read_some_at_awaitable 150   struct read_some_at_awaitable
151   : detail::bytes_op_base<read_some_at_awaitable<MutableBufferSequence>> 151   : detail::bytes_op_base<read_some_at_awaitable<MutableBufferSequence>>
152   { 152   {
153   private: 153   private:
154   friend random_access_file; 154   friend random_access_file;
155   friend detail::bytes_op_base< 155   friend detail::bytes_op_base<
156   read_some_at_awaitable<MutableBufferSequence>>; 156   read_some_at_awaitable<MutableBufferSequence>>;
157   157  
158   random_access_file& f_; 158   random_access_file& f_;
159   std::uint64_t offset_; 159   std::uint64_t offset_;
160   MutableBufferSequence buffers_; 160   MutableBufferSequence buffers_;
161   161  
HITCBC 162   343 read_some_at_awaitable( 162   345 read_some_at_awaitable(
163   random_access_file& f, 163   random_access_file& f,
164   std::uint64_t offset, 164   std::uint64_t offset,
165   MutableBufferSequence 165   MutableBufferSequence
166   buffers) noexcept(std:: 166   buffers) noexcept(std::
167   is_nothrow_move_constructible_v< 167   is_nothrow_move_constructible_v<
168   MutableBufferSequence>) 168   MutableBufferSequence>)
HITCBC 169   343 : f_(f) 169   345 : f_(f)
HITCBC 170   343 , offset_(offset) 170   345 , offset_(offset)
HITCBC 171   343 , buffers_(std::move(buffers)) 171   345 , buffers_(std::move(buffers))
172   { 172   {
HITCBC 173   343 } 173   345 }
174   174  
175   std::coroutine_handle<> 175   std::coroutine_handle<>
HITCBC 176   337 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const 176   339 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
177   { 177   {
HITCBC 178   674 return f_.get().read_some_at( 178   678 return f_.get().read_some_at(
HITCBC 179   337 offset_, h, ex, buffers_, this->token_, &this->ec_, 179   339 offset_, h, ex, buffers_, this->token_, &this->ec_,
HITCBC 180   674 &this->bytes_); 180   678 &this->bytes_);
181   } 181   }
182   }; 182   };
183   183  
184   /** Awaitable for async write-at operations. */ 184   /** Awaitable for async write-at operations. */
185   template<class ConstBufferSequence> 185   template<class ConstBufferSequence>
186   struct write_some_at_awaitable 186   struct write_some_at_awaitable
187   : detail::bytes_op_base<write_some_at_awaitable<ConstBufferSequence>> 187   : detail::bytes_op_base<write_some_at_awaitable<ConstBufferSequence>>
188   { 188   {
189   private: 189   private:
190   friend random_access_file; 190   friend random_access_file;
191   friend detail::bytes_op_base< 191   friend detail::bytes_op_base<
192   write_some_at_awaitable<ConstBufferSequence>>; 192   write_some_at_awaitable<ConstBufferSequence>>;
193   193  
194   random_access_file& f_; 194   random_access_file& f_;
195   std::uint64_t offset_; 195   std::uint64_t offset_;
196   ConstBufferSequence buffers_; 196   ConstBufferSequence buffers_;
197   197  
HITCBC 198   93 write_some_at_awaitable( 198   93 write_some_at_awaitable(
199   random_access_file& f, 199   random_access_file& f,
200   std::uint64_t offset, 200   std::uint64_t offset,
201   ConstBufferSequence 201   ConstBufferSequence
202   buffers) noexcept(std:: 202   buffers) noexcept(std::
203   is_nothrow_move_constructible_v< 203   is_nothrow_move_constructible_v<
204   ConstBufferSequence>) 204   ConstBufferSequence>)
HITCBC 205   93 : f_(f) 205   93 : f_(f)
HITCBC 206   93 , offset_(offset) 206   93 , offset_(offset)
HITCBC 207   93 , buffers_(std::move(buffers)) 207   93 , buffers_(std::move(buffers))
208   { 208   {
HITCBC 209   93 } 209   93 }
210   210  
211   std::coroutine_handle<> 211   std::coroutine_handle<>
HITCBC 212   89 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const 212   89 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
213   { 213   {
HITCBC 214   178 return f_.get().write_some_at( 214   178 return f_.get().write_some_at(
HITCBC 215   89 offset_, h, ex, buffers_, this->token_, &this->ec_, 215   89 offset_, h, ex, buffers_, this->token_, &this->ec_,
HITCBC 216   178 &this->bytes_); 216   178 &this->bytes_);
217   } 217   }
218   }; 218   };
219   219  
220   public: 220   public:
221   /** Destructor. 221   /** Destructor.
222   222  
223   Closes the file if open, cancelling any pending operations. 223   Closes the file if open, cancelling any pending operations.
224   */ 224   */
225   ~random_access_file() override; 225   ~random_access_file() override;
226   226  
227   /** Construct from an execution context. 227   /** Construct from an execution context.
228   228  
229   @param ctx The execution context that owns this file. 229   @param ctx The execution context that owns this file.
230   */ 230   */
231   explicit random_access_file(capy::execution_context& ctx); 231   explicit random_access_file(capy::execution_context& ctx);
232   232  
233   /** Construct from an executor. 233   /** Construct from an executor.
234   234  
235   @param ex The executor whose context owns this file. 235   @param ex The executor whose context owns this file.
236   */ 236   */
237   template<class Ex> 237   template<class Ex>
238   requires(!std::same_as<std::remove_cvref_t<Ex>, random_access_file>) && 238   requires(!std::same_as<std::remove_cvref_t<Ex>, random_access_file>) &&
239   capy::Executor<Ex> 239   capy::Executor<Ex>
HITCBC 240   2 explicit random_access_file(Ex const& ex) : random_access_file(ex.context()) 240   2 explicit random_access_file(Ex const& ex) : random_access_file(ex.context())
241   { 241   {
HITCBC 242   2 } 242   2 }
243   243  
244   /** Move constructor. */ 244   /** Move constructor. */
HITCBC 245   2 random_access_file(random_access_file&& other) noexcept 245   2 random_access_file(random_access_file&& other) noexcept
HITCBC 246   2 : io_object(std::move(other)) 246   2 : io_object(std::move(other))
247   { 247   {
HITCBC 248   2 } 248   2 }
249   249  
250   /** Move assignment operator. */ 250   /** Move assignment operator. */
251   random_access_file& operator=(random_access_file&& other) noexcept 251   random_access_file& operator=(random_access_file&& other) noexcept
252   { 252   {
253   if (this != &other) 253   if (this != &other)
254   { 254   {
255   close(); 255   close();
256   h_ = std::move(other.h_); 256   h_ = std::move(other.h_);
257   } 257   }
258   return *this; 258   return *this;
259   } 259   }
260   260  
261   /// Copy construction is disabled; the handle is uniquely owned. 261   /// Copy construction is disabled; the handle is uniquely owned.
262   random_access_file(random_access_file const&) = delete; 262   random_access_file(random_access_file const&) = delete;
263   /// Copy assignment is disabled; the handle is uniquely owned. 263   /// Copy assignment is disabled; the handle is uniquely owned.
264   random_access_file& operator=(random_access_file const&) = delete; 264   random_access_file& operator=(random_access_file const&) = delete;
265   265  
266   /** Open a file. 266   /** Open a file.
267   267  
268   Failures such as a missing file or insufficient permissions 268   Failures such as a missing file or insufficient permissions
269   are expected runtime conditions and are reported through the 269   are expected runtime conditions and are reported through the
270   returned error code. If the file is already open, it is 270   returned error code. If the file is already open, it is
271   closed first. 271   closed first.
272   272  
273   @param path The filesystem path to open. 273   @param path The filesystem path to open.
274   @param mode Bitmask of @ref file_base::flags specifying 274   @param mode Bitmask of @ref file_base::flags specifying
275   access mode and creation behavior. 275   access mode and creation behavior.
276   276  
277   @return The error code, empty on success. 277   @return The error code, empty on success.
278   */ 278   */
279   [[nodiscard]] std::error_code open( 279   [[nodiscard]] std::error_code open(
280   std::filesystem::path const& path, 280   std::filesystem::path const& path,
281   file_base::flags mode = file_base::read_only) noexcept; 281   file_base::flags mode = file_base::read_only) noexcept;
282   282  
283   /** Close the file. 283   /** Close the file.
284   284  
285   Releases file resources. Pending operations complete through the 285   Releases file resources. Pending operations complete through the
286   same path as @ref cancel: one still in flight completes with 286   same path as @ref cancel: one still in flight completes with
287   `errc::operation_canceled`. An operation whose result is already 287   `errc::operation_canceled`. An operation whose result is already
288   decided reports that result. 288   decided reports that result.
289   */ 289   */
290   void close() noexcept; 290   void close() noexcept;
291   291  
292   /** Check if the file is open. 292   /** Check if the file is open.
293   293  
294   @return `true` if the file holds an open handle. 294   @return `true` if the file holds an open handle.
295   */ 295   */
HITCBC 296   1084 bool is_open() const noexcept 296   1097 bool is_open() const noexcept
297   { 297   {
298   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS) 298   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS)
299   return h_ && get().native_handle() != ~native_handle_type(0); 299   return h_ && get().native_handle() != ~native_handle_type(0);
300   #else 300   #else
HITCBC 301   1084 return h_ && get().native_handle() >= 0; 301   1097 return h_ && get().native_handle() >= 0;
302   #endif 302   #endif
303   } 303   }
304   304  
305   /** Read data at the given offset. 305   /** Read data at the given offset.
306   306  
307   @param offset Byte offset into the file. 307   @param offset Byte offset into the file.
308   @param buffers The buffer sequence to read into. 308   @param buffers The buffer sequence to read into.
309   309  
310   @return An awaitable yielding `(error_code, std::size_t)`. 310   @return An awaitable yielding `(error_code, std::size_t)`.
311   311  
312   A closed file reports `errc::bad_file_descriptor`. 312   A closed file reports `errc::bad_file_descriptor`.
313   */ 313   */
314   template<capy::MutableBufferSequence MB> 314   template<capy::MutableBufferSequence MB>
HITCBC 315   343 [[nodiscard]] auto read_some_at(std::uint64_t offset, MB const& buffers) 315   345 [[nodiscard]] auto read_some_at(std::uint64_t offset, MB const& buffers)
316   { 316   {
HITCBC 317   343 read_some_at_awaitable<MB> aw(*this, offset, buffers); 317   345 read_some_at_awaitable<MB> aw(*this, offset, buffers);
HITCBC 318   343 if (!is_open()) 318   345 if (!is_open())
HITCBC 319   2 aw.ec_ = make_error_code(std::errc::bad_file_descriptor); 319   2 aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
HITCBC 320   343 return aw; 320   345 return aw;
321   } 321   }
322   322  
323   /** Write data at the given offset. 323   /** Write data at the given offset.
324   324  
325   @param offset Byte offset into the file. 325   @param offset Byte offset into the file.
326   @param buffers The buffer sequence to write from. 326   @param buffers The buffer sequence to write from.
327   327  
328   @return An awaitable yielding `(error_code, std::size_t)`. 328   @return An awaitable yielding `(error_code, std::size_t)`.
329   329  
330   A closed file reports `errc::bad_file_descriptor`. 330   A closed file reports `errc::bad_file_descriptor`.
331   */ 331   */
332   template<capy::ConstBufferSequence CB> 332   template<capy::ConstBufferSequence CB>
HITCBC 333   93 [[nodiscard]] auto write_some_at(std::uint64_t offset, CB const& buffers) 333   93 [[nodiscard]] auto write_some_at(std::uint64_t offset, CB const& buffers)
334   { 334   {
HITCBC 335   93 write_some_at_awaitable<CB> aw(*this, offset, buffers); 335   93 write_some_at_awaitable<CB> aw(*this, offset, buffers);
HITCBC 336   93 if (!is_open()) 336   93 if (!is_open())
HITCBC 337   2 aw.ec_ = make_error_code(std::errc::bad_file_descriptor); 337   2 aw.ec_ = make_error_code(std::errc::bad_file_descriptor);
HITCBC 338   93 return aw; 338   93 return aw;
339   } 339   }
340   340  
341   /** Cancel pending asynchronous operations. */ 341   /** Cancel pending asynchronous operations. */
342   void cancel() noexcept; 342   void cancel() noexcept;
343   343  
344   /** Get the native file descriptor or handle. */ 344   /** Get the native file descriptor or handle. */
345   native_handle_type native_handle() const noexcept; 345   native_handle_type native_handle() const noexcept;
346   346  
347   /** Return the file size in bytes. 347   /** Return the file size in bytes.
348   348  
349   @return The current size of the file, in bytes. 349   @return The current size of the file, in bytes.
350   350  
351   @throws std::system_error If the file is not open, or if the 351   @throws std::system_error If the file is not open, or if the
352   underlying size query fails. 352   underlying size query fails.
353   */ 353   */
354   std::uint64_t size() const; 354   std::uint64_t size() const;
355   355  
356   /** Resize the file to @p new_size bytes. 356   /** Resize the file to @p new_size bytes.
357   357  
358   Failures such as insufficient disk space are reported 358   Failures such as insufficient disk space are reported
359   through the returned error code. A closed file reports 359   through the returned error code. A closed file reports
360   `errc::bad_file_descriptor`. 360   `errc::bad_file_descriptor`.
361   361  
362   @param new_size The new file size. 362   @param new_size The new file size.
363   363  
364   @return The error code, empty on success. 364   @return The error code, empty on success.
365   */ 365   */
366   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept; 366   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept;
367   367  
368   /** Synchronize file data to stable storage. 368   /** Synchronize file data to stable storage.
369   369  
370   Write-back failures such as device I/O errors surface here 370   Write-back failures such as device I/O errors surface here
371   and are reported through the returned error code. A closed 371   and are reported through the returned error code. A closed
372   file reports `errc::bad_file_descriptor`. 372   file reports `errc::bad_file_descriptor`.
373   373  
374   @return The error code, empty on success. 374   @return The error code, empty on success.
375   */ 375   */
376   [[nodiscard]] std::error_code sync_data() noexcept; 376   [[nodiscard]] std::error_code sync_data() noexcept;
377   377  
378   /** Synchronize file data and metadata to stable storage. 378   /** Synchronize file data and metadata to stable storage.
379   379  
380   Write-back failures such as device I/O errors surface here 380   Write-back failures such as device I/O errors surface here
381   and are reported through the returned error code. A closed 381   and are reported through the returned error code. A closed
382   file reports `errc::bad_file_descriptor`. 382   file reports `errc::bad_file_descriptor`.
383   383  
384   @return The error code, empty on success. 384   @return The error code, empty on success.
385   */ 385   */
386   [[nodiscard]] std::error_code sync_all() noexcept; 386   [[nodiscard]] std::error_code sync_all() noexcept;
387   387  
388   /** Release ownership of the native handle. 388   /** Release ownership of the native handle.
389   389  
390   The file object becomes not-open. The caller is 390   The file object becomes not-open. The caller is
391   responsible for closing the returned handle. 391   responsible for closing the returned handle.
392   392  
393   @return The native file descriptor or handle. 393   @return The native file descriptor or handle.
394   394  
395   @throws std::system_error `errc::bad_file_descriptor` if the 395   @throws std::system_error `errc::bad_file_descriptor` if the
396   file is not open. 396   file is not open.
397   */ 397   */
398   native_handle_type release(); 398   native_handle_type release();
399   399  
400   /** Adopt an existing native handle. 400   /** Adopt an existing native handle.
401   401  
402 - Closes any currently open file before adopting. 402 + Validation runs before anything is mutated or closed. On
403 - The file object takes ownership of the handle. Handles 403 + error the object still holds whatever file it held before,
404 - created elsewhere may be unsuitable for asynchronous I/O; 404 + and the caller still owns @p handle. On success the object
405 - such failures are reported through the returned error code. 405 + takes ownership of @p handle and closes any file it
  406 + previously held. Handles created elsewhere may be unsuitable
  407 + for asynchronous I/O; such failures are reported through the
  408 + returned error code.
406   409  
407   @param handle The native file descriptor or handle. 410   @param handle The native file descriptor or handle.
408   411  
409 - @return The error code, empty on success. 412 + @return An error code describing the outcome. The codes
  413 + that follow are those of the POSIX and io_uring
  414 + backends. `errc::invalid_argument` if @p handle is the
  415 + one this object already holds.
  416 + `errc::bad_file_descriptor` if it is invalid.
  417 + `errc::operation_not_supported` if it names something a
  418 + file object cannot position. Otherwise, the `errno`
  419 + reported by the kernel, or an empty code. The Windows
  420 + backend validates nothing and reports the Win32 error
  421 + from IOCP registration.
  422 +
  423 + @par Exception Safety
  424 + Throws nothing. Strong guarantee.
  425 +
  426 + @note On POSIX, "something a file object cannot position"
  427 + means, in practice, a pipe, a socket, or any other
  428 + anonymous inode. Adopt those into a @ref posix_descriptor
  429 + instead.
  430 +
  431 + @note The strong guarantee above holds on the POSIX and
  432 + io_uring backends. On Windows (IOCP), a failed @p handle
  433 + can still close the file this object held. That backend's
  434 + file services are unified in a later stage, which is
  435 + where this gap is closed.
  436 +
  437 + @see release
410   */ 438   */
411   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept; 439   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept;
412   440  
413   protected: 441   protected:
414 - /// Construct from a pre-built handle (for native_random_access_file). 442 + /// Construct from a pre-built handle (for `native_random_access_file`).
HITCBC 415   16 explicit random_access_file(handle h) noexcept : io_object(std::move(h)) {} 443   16 explicit random_access_file(handle h) noexcept : io_object(std::move(h)) {}
416   444  
417   private: 445   private:
HITCBC 418   1767 inline implementation& get() const noexcept 446   1798 inline implementation& get() const noexcept
419   { 447   {
HITCBC 420   1767 return *static_cast<implementation*>(h_.get()); 448   1798 return *static_cast<implementation*>(h_.get());
421   } 449   }
422   }; 450   };
423   451  
424   } // namespace boost::corosio 452   } // namespace boost::corosio
425   453  
426   #endif // BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP 454   #endif // BOOST_COROSIO_RANDOM_ACCESS_FILE_HPP