100.00% Lines (13/13) 100.00% Functions (6/6)
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_STREAM_FILE_HPP 10   #ifndef BOOST_COROSIO_STREAM_FILE_HPP
11   #define BOOST_COROSIO_STREAM_FILE_HPP 11   #define BOOST_COROSIO_STREAM_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/file_base.hpp> 17   #include <boost/corosio/file_base.hpp>
18   #include <boost/corosio/io/io_stream.hpp> 18   #include <boost/corosio/io/io_stream.hpp>
19   #include <boost/capy/ex/execution_context.hpp> 19   #include <boost/capy/ex/execution_context.hpp>
20   #include <boost/capy/concept/executor.hpp> 20   #include <boost/capy/concept/executor.hpp>
21   #include <boost/capy/io_result.hpp> 21   #include <boost/capy/io_result.hpp>
22   22  
23   #include <concepts> 23   #include <concepts>
24   #include <cstdint> 24   #include <cstdint>
25   #include <filesystem> 25   #include <filesystem>
26   #include <system_error> 26   #include <system_error>
27   27  
28   namespace boost::corosio { 28   namespace boost::corosio {
29   29  
30   /** Reads and writes a file sequentially, from a coroutine. 30   /** Reads and writes a file sequentially, from a coroutine.
31   31  
32   Provides asynchronous read and write operations on a regular 32   Provides asynchronous read and write operations on a regular
33   file with an implicit position that advances after each 33   file with an implicit position that advances after each
34   operation. 34   operation.
35   35  
36   Inherits from @ref io_stream, so `read_some` and `write_some` 36   Inherits from @ref io_stream, so `read_some` and `write_some`
37   are available and work with any algorithm that accepts an 37   are available and work with any algorithm that accepts an
38   `io_stream&`. 38   `io_stream&`.
39   39  
40   On POSIX platforms, file I/O is dispatched to a thread pool 40   On POSIX platforms, file I/O is dispatched to a thread pool
41   (blocking `preadv`/`pwritev`) with completion posted back to 41   (blocking `preadv`/`pwritev`) with completion posted back to
42   the scheduler. On Windows, true overlapped I/O is used via IOCP. 42   the scheduler. On Windows, true overlapped I/O is used via IOCP.
43   43  
44   @par Thread Safety 44   @par Thread Safety
45   Distinct objects: Safe.@n 45   Distinct objects: Safe.@n
46   Shared objects: Unsafe. Only one asynchronous operation 46   Shared objects: Unsafe. Only one asynchronous operation
47   may be in flight at a time. 47   may be in flight at a time.
48   48  
49   @par Example 49   @par Example
50   @par !example stream_file 50   @par !example stream_file
51   */ 51   */
52   class BOOST_COROSIO_DECL stream_file : public io_stream 52   class BOOST_COROSIO_DECL stream_file : public io_stream
53   { 53   {
54   public: 54   public:
55   /** Defines the file operations a platform backend implements. 55   /** Defines the file operations a platform backend implements.
56   56  
57   Backends derive from this to provide file I/O. 57   Backends derive from this to provide file I/O.
58   `read_some` and `write_some` are inherited from 58   `read_some` and `write_some` are inherited from
59   @ref io_stream::implementation. 59   @ref io_stream::implementation.
60   */ 60   */
61   struct implementation : io_stream::implementation 61   struct implementation : io_stream::implementation
62   { 62   {
63   /// Return the platform file descriptor or handle. 63   /// Return the platform file descriptor or handle.
64   virtual native_handle_type native_handle() const noexcept = 0; 64   virtual native_handle_type native_handle() const noexcept = 0;
65   65  
66   /// Cancel pending asynchronous operations. 66   /// Cancel pending asynchronous operations.
67   virtual void cancel() noexcept = 0; 67   virtual void cancel() noexcept = 0;
68   68  
69   /** Return the file size in bytes. 69   /** Return the file size in bytes.
70   70  
71   @return The current size of the file, in bytes. 71   @return The current size of the file, in bytes.
72   72  
73   @throws std::system_error if the underlying size query fails. 73   @throws std::system_error if the underlying size query fails.
74   */ 74   */
75   virtual std::uint64_t size() const = 0; 75   virtual std::uint64_t size() const = 0;
76   76  
77   /** Resize the file to @p new_size bytes. 77   /** Resize the file to @p new_size bytes.
78   78  
79   @param new_size The requested size in bytes. 79   @param new_size The requested size in bytes.
80   80  
81   @return The error code, empty on success. 81   @return The error code, empty on success.
82   */ 82   */
83   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0; 83   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0;
84   84  
85   /** Synchronize file data to stable storage. 85   /** Synchronize file data to stable storage.
86   86  
87   @return The error code, empty on success. 87   @return The error code, empty on success.
88   */ 88   */
89   virtual std::error_code sync_data() noexcept = 0; 89   virtual std::error_code sync_data() noexcept = 0;
90   90  
91   /** Synchronize file data and metadata to stable storage. 91   /** Synchronize file data and metadata to stable storage.
92   92  
93   @return The error code, empty on success. 93   @return The error code, empty on success.
94   */ 94   */
95   virtual std::error_code sync_all() noexcept = 0; 95   virtual std::error_code sync_all() noexcept = 0;
96   96  
97   /** Release ownership of the native handle. 97   /** Release ownership of the native handle.
98   98  
99   @return The native handle, which the caller now owns. 99   @return The native handle, which the caller now owns.
100   100  
101   @throws std::system_error if the file is not open. 101   @throws std::system_error if the file is not open.
102   */ 102   */
103   virtual native_handle_type release() = 0; 103   virtual native_handle_type release() = 0;
104   104  
105   /** Adopt an existing native handle. 105   /** Adopt an existing native handle.
106   106  
107   @param handle The native handle to adopt. The implementation takes 107   @param handle The native handle to adopt. The implementation takes
108   ownership and closes it. 108   ownership and closes it.
109   109  
110   @return The error code, empty on success. 110   @return The error code, empty on success.
111   */ 111   */
112   virtual std::error_code assign(native_handle_type handle) noexcept = 0; 112   virtual std::error_code assign(native_handle_type handle) noexcept = 0;
113   113  
114   /** Move the file position. 114   /** Move the file position.
115   115  
116   @param offset Signed offset from @p origin. 116   @param offset Signed offset from @p origin.
117   @param origin The reference point for the seek. 117   @param origin The reference point for the seek.
118   @return The error code and new absolute position. 118   @return The error code and new absolute position.
119   */ 119   */
120   virtual capy::io_result<std::uint64_t> 120   virtual capy::io_result<std::uint64_t>
121   seek(std::int64_t offset, file_base::seek_basis origin) noexcept = 0; 121   seek(std::int64_t offset, file_base::seek_basis origin) noexcept = 0;
122   }; 122   };
123   123  
124   /** Closes the file if open, cancelling any pending operations. 124   /** Closes the file if open, cancelling any pending operations.
125   */ 125   */
126   ~stream_file() override; 126   ~stream_file() override;
127   127  
128   /** Construct from an execution context. 128   /** Construct from an execution context.
129   129  
130   @param ctx The execution context that owns this file. 130   @param ctx The execution context that owns this file.
131   */ 131   */
132   explicit stream_file(capy::execution_context& ctx); 132   explicit stream_file(capy::execution_context& ctx);
133   133  
134   /** Construct from an executor. 134   /** Construct from an executor.
135   135  
136   @param ex The executor whose context owns this file. 136   @param ex The executor whose context owns this file.
137   */ 137   */
138   template<class Ex> 138   template<class Ex>
139   requires(!std::same_as<std::remove_cvref_t<Ex>, stream_file>) && 139   requires(!std::same_as<std::remove_cvref_t<Ex>, stream_file>) &&
140   capy::Executor<Ex> 140   capy::Executor<Ex>
HITCBC 141   2 explicit stream_file(Ex const& ex) : stream_file(ex.context()) 141   2 explicit stream_file(Ex const& ex) : stream_file(ex.context())
142   { 142   {
HITCBC 143   2 } 143   2 }
144   144  
145   /** Transfers ownership of the file resources. 145   /** Transfers ownership of the file resources.
146   */ 146   */
HITCBC 147   2 stream_file(stream_file&& other) noexcept : io_object(std::move(other)) {} 147   2 stream_file(stream_file&& other) noexcept : io_object(std::move(other)) {}
148   148  
149   /** Closes any existing file and transfers ownership. 149   /** Closes any existing file and transfers ownership.
150   150  
151   @return Reference to this object. 151   @return Reference to this object.
152   */ 152   */
HITCBC 153   2 stream_file& operator=(stream_file&& other) noexcept 153   2 stream_file& operator=(stream_file&& other) noexcept
154   { 154   {
HITCBC 155   2 if (this != &other) 155   2 if (this != &other)
156   { 156   {
HITCBC 157   2 close(); 157   2 close();
HITCBC 158   2 h_ = std::move(other.h_); 158   2 h_ = std::move(other.h_);
159   } 159   }
HITCBC 160   2 return *this; 160   2 return *this;
161   } 161   }
162   162  
163   /// Copy construction is disabled; the handle is uniquely owned. 163   /// Copy construction is disabled; the handle is uniquely owned.
164   stream_file(stream_file const&) = delete; 164   stream_file(stream_file const&) = delete;
165   /// Copy assignment is disabled; the handle is uniquely owned. 165   /// Copy assignment is disabled; the handle is uniquely owned.
166   stream_file& operator=(stream_file const&) = delete; 166   stream_file& operator=(stream_file const&) = delete;
167   167  
168   // read_some() inherited from io_read_stream 168   // read_some() inherited from io_read_stream
169   // write_some() inherited from io_write_stream 169   // write_some() inherited from io_write_stream
170   170  
171   /** Open a file. 171   /** Open a file.
172   172  
173   Failures such as a missing file or insufficient permissions 173   Failures such as a missing file or insufficient permissions
174   are expected runtime conditions and are reported through the 174   are expected runtime conditions and are reported through the
175   returned error code. If the file is already open, it is 175   returned error code. If the file is already open, it is
176   closed first. 176   closed first.
177   177  
178   @param path The filesystem path to open. 178   @param path The filesystem path to open.
179   @param mode Bitmask of @ref file_base::flags specifying 179   @param mode Bitmask of @ref file_base::flags specifying
180   access mode and creation behavior. 180   access mode and creation behavior.
181   181  
182   @return The error code, empty on success. 182   @return The error code, empty on success.
183   */ 183   */
184   [[nodiscard]] std::error_code open( 184   [[nodiscard]] std::error_code open(
185   std::filesystem::path const& path, 185   std::filesystem::path const& path,
186   file_base::flags mode = file_base::read_only) noexcept; 186   file_base::flags mode = file_base::read_only) noexcept;
187   187  
188   /** Close the file. 188   /** Close the file.
189   189  
190   Releases file resources. Pending operations complete through the 190   Releases file resources. Pending operations complete through the
191   same path as @ref cancel: one still in flight completes with 191   same path as @ref cancel: one still in flight completes with
192   `errc::operation_canceled`. An operation whose result is already 192   `errc::operation_canceled`. An operation whose result is already
193   decided reports that result. 193   decided reports that result.
194   */ 194   */
195   void close() noexcept; 195   void close() noexcept;
196   196  
197   /** Check if the file is open. 197   /** Check if the file is open.
198   198  
199   @return `true` if the file is open and ready for I/O. 199   @return `true` if the file is open and ready for I/O.
200   */ 200   */
HITCBC 201   797 bool is_open() const noexcept 201   809 bool is_open() const noexcept
202   { 202   {
203   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS) 203   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS)
204   return h_ && get().native_handle() != ~native_handle_type(0); 204   return h_ && get().native_handle() != ~native_handle_type(0);
205   #else 205   #else
HITCBC 206   797 return h_ && get().native_handle() >= 0; 206   809 return h_ && get().native_handle() >= 0;
207   #endif 207   #endif
208   } 208   }
209   209  
210   /** Cancel pending asynchronous operations. 210   /** Cancel pending asynchronous operations.
211   211  
212   Operations still in flight complete with 212   Operations still in flight complete with
213   `errc::operation_canceled`; an operation whose result is 213   `errc::operation_canceled`; an operation whose result is
214   already decided reports that result. 214   already decided reports that result.
215   */ 215   */
216   void cancel() noexcept; 216   void cancel() noexcept;
217   217  
218   /** Get the native file descriptor or handle. 218   /** Get the native file descriptor or handle.
219   219  
220   @return The native handle, or -1/INVALID_HANDLE_VALUE 220   @return The native handle, or -1/INVALID_HANDLE_VALUE
221   if not open. 221   if not open.
222   */ 222   */
223   native_handle_type native_handle() const noexcept; 223   native_handle_type native_handle() const noexcept;
224   224  
225   /** Return the file size in bytes. 225   /** Return the file size in bytes.
226   226  
227   @return The file size in bytes. 227   @return The file size in bytes.
228   228  
229   @throws std::system_error If the file is not open, or if the 229   @throws std::system_error If the file is not open, or if the
230   underlying size query fails. 230   underlying size query fails.
231   */ 231   */
232   std::uint64_t size() const; 232   std::uint64_t size() const;
233   233  
234   /** Resize the file to @p new_size bytes. 234   /** Resize the file to @p new_size bytes.
235   235  
236   Failures such as insufficient disk space are reported 236   Failures such as insufficient disk space are reported
237   through the returned error code. A closed file reports 237   through the returned error code. A closed file reports
238   `errc::bad_file_descriptor`. 238   `errc::bad_file_descriptor`.
239   239  
240   @param new_size The new file size. 240   @param new_size The new file size.
241   241  
242   @return The error code, empty on success. 242   @return The error code, empty on success.
243   */ 243   */
244   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept; 244   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept;
245   245  
246   /** Synchronize file data to stable storage. 246   /** Synchronize file data to stable storage.
247   247  
248   Write-back failures such as device I/O errors surface here 248   Write-back failures such as device I/O errors surface here
249   and are reported through the returned error code. A closed 249   and are reported through the returned error code. A closed
250   file reports `errc::bad_file_descriptor`. 250   file reports `errc::bad_file_descriptor`.
251   251  
252   @return The error code, empty on success. 252   @return The error code, empty on success.
253   */ 253   */
254   [[nodiscard]] std::error_code sync_data() noexcept; 254   [[nodiscard]] std::error_code sync_data() noexcept;
255   255  
256   /** Synchronize file data and metadata to stable storage. 256   /** Synchronize file data and metadata to stable storage.
257   257  
258   Write-back failures such as device I/O errors surface here 258   Write-back failures such as device I/O errors surface here
259   and are reported through the returned error code. A closed 259   and are reported through the returned error code. A closed
260   file reports `errc::bad_file_descriptor`. 260   file reports `errc::bad_file_descriptor`.
261   261  
262   @return The error code, empty on success. 262   @return The error code, empty on success.
263   */ 263   */
264   [[nodiscard]] std::error_code sync_all() noexcept; 264   [[nodiscard]] std::error_code sync_all() noexcept;
265   265  
266   /** Release ownership of the native handle. 266   /** Release ownership of the native handle.
267   267  
268   The file object becomes not-open. The caller is 268   The file object becomes not-open. The caller is
269   responsible for closing the returned handle. 269   responsible for closing the returned handle.
270   270  
271   @return The native file descriptor or handle. 271   @return The native file descriptor or handle.
272   272  
273   @throws std::system_error `errc::bad_file_descriptor` if the 273   @throws std::system_error `errc::bad_file_descriptor` if the
274   file is not open. 274   file is not open.
275   */ 275   */
276   native_handle_type release(); 276   native_handle_type release();
277   277  
278   /** Adopt an existing native handle. 278   /** Adopt an existing native handle.
279   279  
280 - Closes any currently open file before adopting. 280 + Validation runs before anything is mutated or closed. On
281 - The file object takes ownership of the handle. Handles 281 + error the object still holds whatever file it held before,
282 - created elsewhere may be unsuitable for asynchronous I/O. 282 + and the caller still owns @p handle. On success the object
283 - Such failures are reported through the returned error code. 283 + takes ownership of @p handle and closes any file it
  284 + previously held. Handles created elsewhere may be unsuitable
  285 + for asynchronous I/O; such failures are reported through the
  286 + returned error code.
284   287  
285   @param handle The native file descriptor or handle. 288   @param handle The native file descriptor or handle.
286   289  
287 - @return The error code, empty on success. 290 + @return An error code describing the outcome. The codes
  291 + that follow are those of the POSIX and io_uring
  292 + backends. `errc::invalid_argument` if @p handle is the
  293 + one this object already holds.
  294 + `errc::bad_file_descriptor` if it is invalid.
  295 + `errc::operation_not_supported` if it names something a
  296 + file object cannot position. Otherwise, the `errno`
  297 + reported by the kernel, or an empty code. The Windows
  298 + backend validates nothing and reports the Win32 error
  299 + from IOCP registration.
  300 +
  301 + @par Exception Safety
  302 + Throws nothing. Strong guarantee.
  303 +
  304 + @note On POSIX, "something a file object cannot position"
  305 + means, in practice, a pipe, a socket, or any other
  306 + anonymous inode. Adopt those into a @ref posix_descriptor
  307 + instead.
  308 +
  309 + @note The strong guarantee above holds on the POSIX and
  310 + io_uring backends. On Windows (IOCP), a failed @p handle
  311 + can still close the file this object held. That backend's
  312 + file services are unified in a later stage, which is
  313 + where this gap is closed.
  314 +
  315 + @see release
288   */ 316   */
289   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept; 317   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept;
290   318  
291   /** Move the file position. 319   /** Move the file position.
292   320  
293   Positions beyond the end of the file are allowed. A 321   Positions beyond the end of the file are allowed. A
294   resulting negative position is reported through the error 322   resulting negative position is reported through the error
295   code, as offsets often originate from file contents. A 323   code, as offsets often originate from file contents. A
296   closed file reports `errc::bad_file_descriptor`. 324   closed file reports `errc::bad_file_descriptor`.
297   325  
298   @param offset Signed offset from @p origin. 326   @param offset Signed offset from @p origin.
299   @param origin The reference point for the seek. 327   @param origin The reference point for the seek.
300   328  
301   @return The error code and new absolute position. 329   @return The error code and new absolute position.
302   */ 330   */
303   [[nodiscard]] capy::io_result<std::uint64_t> seek( 331   [[nodiscard]] capy::io_result<std::uint64_t> seek(
304   std::int64_t offset, 332   std::int64_t offset,
305   file_base::seek_basis origin = file_base::seek_set) noexcept; 333   file_base::seek_basis origin = file_base::seek_set) noexcept;
306   334  
307   protected: 335   protected:
308 - /// Default-construct (for derived types that initialize io_object directly). 336 + /// Default-construct (for derived types that initialize `io_object` directly).
HITCBC 309   16 stream_file() noexcept = default; 337   16 stream_file() noexcept = default;
310   338  
311 - /** Construct from a pre-built handle (for native_stream_file). 339 + /** Construct from a pre-built handle (for `native_stream_file`).
312   340  
313   @param h The pre-built handle to adopt. 341   @param h The pre-built handle to adopt.
314   */ 342   */
315   explicit stream_file(handle h) noexcept : io_object(std::move(h)) {} 343   explicit stream_file(handle h) noexcept : io_object(std::move(h)) {}
316   344  
317   private: 345   private:
HITCBC 318   1133 inline implementation& get() const noexcept 346   1161 inline implementation& get() const noexcept
319   { 347   {
HITCBC 320   1133 return *static_cast<implementation*>(h_.get()); 348   1161 return *static_cast<implementation*>(h_.get());
321   } 349   }
322   }; 350   };
323   351  
324   } // namespace boost::corosio 352   } // namespace boost::corosio
325   353  
326   #endif // BOOST_COROSIO_STREAM_FILE_HPP 354   #endif // BOOST_COROSIO_STREAM_FILE_HPP