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   /** An asynchronous sequential file for coroutine I/O. 30   /** An asynchronous sequential file for coroutine I/O.
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 - @code 50 + @par !example stream_file
51 - io_context ioc;  
52 - stream_file f(ioc);  
53 - if (auto ec = f.open("data.bin", file_base::read_only))  
54 - co_return; // report the error  
55 -  
56 - char buf[4096];  
57 - for (;;)  
58 - {  
59 - auto [ec, n] = co_await f.read_some(  
60 - capy::mutable_buffer(buf, sizeof(buf)));  
61 - if (ec == capy::cond::eof)  
62 - break;  
63 - if (ec)  
64 - co_return;  
65 - }  
66 - @endcode  
67   */ 51   */
68   class BOOST_COROSIO_DECL stream_file : public io_stream 52   class BOOST_COROSIO_DECL stream_file : public io_stream
69   { 53   {
70   public: 54   public:
71   /** Platform-specific file implementation interface. 55   /** Platform-specific file implementation interface.
72   56  
73   Backends derive from this to provide file I/O. 57   Backends derive from this to provide file I/O.
74   `read_some` and `write_some` are inherited from 58   `read_some` and `write_some` are inherited from
75   @ref io_stream::implementation. 59   @ref io_stream::implementation.
76   */ 60   */
77   struct implementation : io_stream::implementation 61   struct implementation : io_stream::implementation
78   { 62   {
79   /// Return the platform file descriptor or handle. 63   /// Return the platform file descriptor or handle.
80   virtual native_handle_type native_handle() const noexcept = 0; 64   virtual native_handle_type native_handle() const noexcept = 0;
81   65  
82   /// Cancel pending asynchronous operations. 66   /// Cancel pending asynchronous operations.
83   virtual void cancel() noexcept = 0; 67   virtual void cancel() noexcept = 0;
84   68  
85   /// Return the file size in bytes. 69   /// Return the file size in bytes.
86   virtual std::uint64_t size() const = 0; 70   virtual std::uint64_t size() const = 0;
87   71  
88   /// Resize the file to @p new_size bytes. 72   /// Resize the file to @p new_size bytes.
89   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0; 73   virtual std::error_code resize(std::uint64_t new_size) noexcept = 0;
90   74  
91   /// Synchronize file data to stable storage. 75   /// Synchronize file data to stable storage.
92   virtual std::error_code sync_data() noexcept = 0; 76   virtual std::error_code sync_data() noexcept = 0;
93   77  
94   /// Synchronize file data and metadata to stable storage. 78   /// Synchronize file data and metadata to stable storage.
95   virtual std::error_code sync_all() noexcept = 0; 79   virtual std::error_code sync_all() noexcept = 0;
96   80  
97   /// Release ownership of the native handle. 81   /// Release ownership of the native handle.
98   virtual native_handle_type release() = 0; 82   virtual native_handle_type release() = 0;
99   83  
100   /// Adopt an existing native handle. 84   /// Adopt an existing native handle.
101   virtual std::error_code assign(native_handle_type handle) noexcept = 0; 85   virtual std::error_code assign(native_handle_type handle) noexcept = 0;
102   86  
103   /** Move the file position. 87   /** Move the file position.
104   88  
105   @param offset Signed offset from @p origin. 89   @param offset Signed offset from @p origin.
106   @param origin The reference point for the seek. 90   @param origin The reference point for the seek.
107   @return The error code and new absolute position. 91   @return The error code and new absolute position.
108   */ 92   */
109   virtual capy::io_result<std::uint64_t> 93   virtual capy::io_result<std::uint64_t>
110   seek(std::int64_t offset, file_base::seek_basis origin) noexcept = 0; 94   seek(std::int64_t offset, file_base::seek_basis origin) noexcept = 0;
111   }; 95   };
112   96  
113   /** Destructor. 97   /** Destructor.
114   98  
115   Closes the file if open, cancelling any pending operations. 99   Closes the file if open, cancelling any pending operations.
116   */ 100   */
117   ~stream_file() override; 101   ~stream_file() override;
118   102  
119   /** Construct from an execution context. 103   /** Construct from an execution context.
120   104  
121   @param ctx The execution context that will own this file. 105   @param ctx The execution context that will own this file.
122   */ 106   */
123   explicit stream_file(capy::execution_context& ctx); 107   explicit stream_file(capy::execution_context& ctx);
124   108  
125   /** Construct from an executor. 109   /** Construct from an executor.
126   110  
127   @param ex The executor whose context will own this file. 111   @param ex The executor whose context will own this file.
128   */ 112   */
129   template<class Ex> 113   template<class Ex>
130   requires(!std::same_as<std::remove_cvref_t<Ex>, stream_file>) && 114   requires(!std::same_as<std::remove_cvref_t<Ex>, stream_file>) &&
131   capy::Executor<Ex> 115   capy::Executor<Ex>
HITCBC 132   2 explicit stream_file(Ex const& ex) : stream_file(ex.context()) 116   2 explicit stream_file(Ex const& ex) : stream_file(ex.context())
133   { 117   {
HITCBC 134   2 } 118   2 }
135   119  
136   /** Move constructor. 120   /** Move constructor.
137   121  
138   Transfers ownership of the file resources. 122   Transfers ownership of the file resources.
139   */ 123   */
HITCBC 140   2 stream_file(stream_file&& other) noexcept : io_object(std::move(other)) {} 124   2 stream_file(stream_file&& other) noexcept : io_object(std::move(other)) {}
141   125  
142   /** Move assignment operator. 126   /** Move assignment operator.
143   127  
144   Closes any existing file and transfers ownership. 128   Closes any existing file and transfers ownership.
145   */ 129   */
HITCBC 146   2 stream_file& operator=(stream_file&& other) noexcept 130   2 stream_file& operator=(stream_file&& other) noexcept
147   { 131   {
HITCBC 148   2 if (this != &other) 132   2 if (this != &other)
149   { 133   {
HITCBC 150   2 close(); 134   2 close();
HITCBC 151   2 h_ = std::move(other.h_); 135   2 h_ = std::move(other.h_);
152   } 136   }
HITCBC 153   2 return *this; 137   2 return *this;
154   } 138   }
155   139  
156   stream_file(stream_file const&) = delete; 140   stream_file(stream_file const&) = delete;
157   stream_file& operator=(stream_file const&) = delete; 141   stream_file& operator=(stream_file const&) = delete;
158   142  
159   // read_some() inherited from io_read_stream 143   // read_some() inherited from io_read_stream
160   // write_some() inherited from io_write_stream 144   // write_some() inherited from io_write_stream
161   145  
162   /** Open a file. 146   /** Open a file.
163   147  
164   Failures such as a missing file or insufficient permissions 148   Failures such as a missing file or insufficient permissions
165   are expected runtime conditions and are reported through the 149   are expected runtime conditions and are reported through the
166   returned error code. If the file is already open, it is 150   returned error code. If the file is already open, it is
167   closed first. 151   closed first.
168   152  
169   @param path The filesystem path to open. 153   @param path The filesystem path to open.
170   @param mode Bitmask of @ref file_base::flags specifying 154   @param mode Bitmask of @ref file_base::flags specifying
171   access mode and creation behavior. 155   access mode and creation behavior.
172   156  
173   @return The error code, empty on success. 157   @return The error code, empty on success.
174   */ 158   */
175   [[nodiscard]] std::error_code open( 159   [[nodiscard]] std::error_code open(
176   std::filesystem::path const& path, 160   std::filesystem::path const& path,
177   file_base::flags mode = file_base::read_only) noexcept; 161   file_base::flags mode = file_base::read_only) noexcept;
178   162  
179   /** Close the file. 163   /** Close the file.
180   164  
181   Releases file resources. Any pending operations complete 165   Releases file resources. Any pending operations complete
182   with `errc::operation_canceled`. 166   with `errc::operation_canceled`.
183   */ 167   */
184   void close() noexcept; 168   void close() noexcept;
185   169  
186   /** Check if the file is open. 170   /** Check if the file is open.
187   171  
188   @return `true` if the file is open and ready for I/O. 172   @return `true` if the file is open and ready for I/O.
189   */ 173   */
HITCBC 190   495 bool is_open() const noexcept 174   495 bool is_open() const noexcept
191   { 175   {
192   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS) 176   #if BOOST_COROSIO_HAS_IOCP && !defined(BOOST_COROSIO_MRDOCS)
193   return h_ && get().native_handle() != ~native_handle_type(0); 177   return h_ && get().native_handle() != ~native_handle_type(0);
194   #else 178   #else
HITCBC 195   495 return h_ && get().native_handle() >= 0; 179   495 return h_ && get().native_handle() >= 0;
196   #endif 180   #endif
197   } 181   }
198   182  
199   /** Cancel pending asynchronous operations. 183   /** Cancel pending asynchronous operations.
200   184  
201   All outstanding operations complete with 185   All outstanding operations complete with
202   `errc::operation_canceled`. 186   `errc::operation_canceled`.
203   */ 187   */
204   void cancel() noexcept; 188   void cancel() noexcept;
205   189  
206   /** Get the native file descriptor or handle. 190   /** Get the native file descriptor or handle.
207   191  
208   @return The native handle, or -1/INVALID_HANDLE_VALUE 192   @return The native handle, or -1/INVALID_HANDLE_VALUE
209   if not open. 193   if not open.
210   */ 194   */
211   native_handle_type native_handle() const noexcept; 195   native_handle_type native_handle() const noexcept;
212   196  
213   /** Return the file size in bytes. 197   /** Return the file size in bytes.
214   198  
215   @throws std::system_error If the file is not open, or if the 199   @throws std::system_error If the file is not open, or if the
216   underlying size query fails. 200   underlying size query fails.
217   */ 201   */
218   std::uint64_t size() const; 202   std::uint64_t size() const;
219   203  
220   /** Resize the file to @p new_size bytes. 204   /** Resize the file to @p new_size bytes.
221   205  
222   Failures such as insufficient disk space are reported 206   Failures such as insufficient disk space are reported
223   through the returned error code. A closed file reports 207   through the returned error code. A closed file reports
224   `errc::bad_file_descriptor`. 208   `errc::bad_file_descriptor`.
225   209  
226   @param new_size The new file size. 210   @param new_size The new file size.
227   211  
228   @return The error code, empty on success. 212   @return The error code, empty on success.
229   */ 213   */
230   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept; 214   [[nodiscard]] std::error_code resize(std::uint64_t new_size) noexcept;
231   215  
232   /** Synchronize file data to stable storage. 216   /** Synchronize file data to stable storage.
233   217  
234   Write-back failures such as device I/O errors surface here 218   Write-back failures such as device I/O errors surface here
235   and are reported through the returned error code. A closed 219   and are reported through the returned error code. A closed
236   file reports `errc::bad_file_descriptor`. 220   file reports `errc::bad_file_descriptor`.
237   221  
238   @return The error code, empty on success. 222   @return The error code, empty on success.
239   */ 223   */
240   [[nodiscard]] std::error_code sync_data() noexcept; 224   [[nodiscard]] std::error_code sync_data() noexcept;
241   225  
242   /** Synchronize file data and metadata to stable storage. 226   /** Synchronize file data and metadata to stable storage.
243   227  
244   Write-back failures such as device I/O errors surface here 228   Write-back failures such as device I/O errors surface here
245   and are reported through the returned error code. A closed 229   and are reported through the returned error code. A closed
246   file reports `errc::bad_file_descriptor`. 230   file reports `errc::bad_file_descriptor`.
247   231  
248   @return The error code, empty on success. 232   @return The error code, empty on success.
249   */ 233   */
250   [[nodiscard]] std::error_code sync_all() noexcept; 234   [[nodiscard]] std::error_code sync_all() noexcept;
251   235  
252   /** Release ownership of the native handle. 236   /** Release ownership of the native handle.
253   237  
254   The file object becomes not-open. The caller is 238   The file object becomes not-open. The caller is
255   responsible for closing the returned handle. 239   responsible for closing the returned handle.
256   240  
257   @return The native file descriptor or handle. 241   @return The native file descriptor or handle.
258   242  
259   @throws std::system_error `errc::bad_file_descriptor` if the 243   @throws std::system_error `errc::bad_file_descriptor` if the
260   file is not open. 244   file is not open.
261   */ 245   */
262   native_handle_type release(); 246   native_handle_type release();
263   247  
264   /** Adopt an existing native handle. 248   /** Adopt an existing native handle.
265   249  
266   Closes any currently open file before adopting. 250   Closes any currently open file before adopting.
267   The file object takes ownership of the handle. Handles 251   The file object takes ownership of the handle. Handles
268   created elsewhere may be unsuitable for asynchronous I/O; 252   created elsewhere may be unsuitable for asynchronous I/O;
269   such failures are reported through the returned error code. 253   such failures are reported through the returned error code.
270   254  
271   @param handle The native file descriptor or handle. 255   @param handle The native file descriptor or handle.
272   256  
273   @return The error code, empty on success. 257   @return The error code, empty on success.
274   */ 258   */
275   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept; 259   [[nodiscard]] std::error_code assign(native_handle_type handle) noexcept;
276   260  
277   /** Move the file position. 261   /** Move the file position.
278   262  
279   Positions beyond the end of the file are allowed. A 263   Positions beyond the end of the file are allowed. A
280   resulting negative position is reported through the error 264   resulting negative position is reported through the error
281   code, as offsets often originate from file contents. A 265   code, as offsets often originate from file contents. A
282   closed file reports `errc::bad_file_descriptor`. 266   closed file reports `errc::bad_file_descriptor`.
283   267  
284   @param offset Signed offset from @p origin. 268   @param offset Signed offset from @p origin.
285   @param origin The reference point for the seek. 269   @param origin The reference point for the seek.
286   270  
287   @return The error code and new absolute position. 271   @return The error code and new absolute position.
288   */ 272   */
289   [[nodiscard]] capy::io_result<std::uint64_t> 273   [[nodiscard]] capy::io_result<std::uint64_t>
290   seek(std::int64_t offset, 274   seek(std::int64_t offset,
291   file_base::seek_basis origin = file_base::seek_set) noexcept; 275   file_base::seek_basis origin = file_base::seek_set) noexcept;
292   276  
293   protected: 277   protected:
294   /// Default-construct (for derived types that initialize io_object directly). 278   /// Default-construct (for derived types that initialize io_object directly).
HITCBC 295   16 stream_file() noexcept = default; 279   16 stream_file() noexcept = default;
296   280  
297   /// Construct from a pre-built handle (for native_stream_file). 281   /// Construct from a pre-built handle (for native_stream_file).
298   explicit stream_file(handle h) noexcept : io_object(std::move(h)) {} 282   explicit stream_file(handle h) noexcept : io_object(std::move(h)) {}
299   283  
300   private: 284   private:
HITCBC 301   731 inline implementation& get() const noexcept 285   731 inline implementation& get() const noexcept
302   { 286   {
HITCBC 303   731 return *static_cast<implementation*>(h_.get()); 287   731 return *static_cast<implementation*>(h_.get());
304   } 288   }
305   }; 289   };
306   290  
307   } // namespace boost::corosio 291   } // namespace boost::corosio
308   292  
309   #endif // BOOST_COROSIO_STREAM_FILE_HPP 293   #endif // BOOST_COROSIO_STREAM_FILE_HPP