|
| 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 | += File I/O |
| 11 | + |
| 12 | +Corosio provides two classes for asynchronous file operations: |
| 13 | +`stream_file` for sequential access and `random_access_file` for |
| 14 | +offset-based access. Both dispatch I/O to a worker thread on POSIX |
| 15 | +platforms and use native overlapped I/O on Windows. |
| 16 | + |
| 17 | +[NOTE] |
| 18 | +==== |
| 19 | +Code snippets assume: |
| 20 | +[source,cpp] |
| 21 | +---- |
| 22 | +#include <boost/corosio/stream_file.hpp> |
| 23 | +#include <boost/corosio/random_access_file.hpp> |
| 24 | +namespace corosio = boost::corosio; |
| 25 | +---- |
| 26 | +==== |
| 27 | + |
| 28 | +== Stream File |
| 29 | + |
| 30 | +`stream_file` reads and writes sequentially, maintaining an internal |
| 31 | +position that advances after each operation. It inherits from `io_stream`, |
| 32 | +so it works with any algorithm that accepts an `io_stream&`. |
| 33 | + |
| 34 | +=== Reading a File |
| 35 | + |
| 36 | +[source,cpp] |
| 37 | +---- |
| 38 | +corosio::stream_file f(ioc); |
| 39 | +f.open("data.bin", corosio::file_base::read_only); |
| 40 | +
|
| 41 | +char buf[4096]; |
| 42 | +auto [ec, n] = co_await f.read_some( |
| 43 | + capy::mutable_buffer(buf, sizeof(buf))); |
| 44 | +
|
| 45 | +if (ec == capy::cond::eof) |
| 46 | + // reached end of file |
| 47 | +---- |
| 48 | + |
| 49 | +=== Writing a File |
| 50 | + |
| 51 | +[source,cpp] |
| 52 | +---- |
| 53 | +corosio::stream_file f(ioc); |
| 54 | +f.open("output.bin", |
| 55 | + corosio::file_base::write_only |
| 56 | + | corosio::file_base::create |
| 57 | + | corosio::file_base::truncate); |
| 58 | +
|
| 59 | +std::string data = "hello world"; |
| 60 | +auto [ec, n] = co_await f.write_some( |
| 61 | + capy::const_buffer(data.data(), data.size())); |
| 62 | +---- |
| 63 | + |
| 64 | +=== Seeking |
| 65 | + |
| 66 | +The file position can be moved with `seek()`: |
| 67 | + |
| 68 | +[source,cpp] |
| 69 | +---- |
| 70 | +f.seek(0, corosio::file_base::seek_set); // beginning |
| 71 | +f.seek(100, corosio::file_base::seek_cur); // forward 100 bytes |
| 72 | +f.seek(-10, corosio::file_base::seek_end); // 10 bytes before end |
| 73 | +---- |
| 74 | + |
| 75 | +== Random Access File |
| 76 | + |
| 77 | +`random_access_file` reads and writes at explicit byte offsets |
| 78 | +without maintaining an internal position. This is useful for |
| 79 | +databases, indices, or any workload that accesses non-sequential |
| 80 | +regions of a file. |
| 81 | + |
| 82 | +=== Reading at an Offset |
| 83 | + |
| 84 | +[source,cpp] |
| 85 | +---- |
| 86 | +corosio::random_access_file f(ioc); |
| 87 | +f.open("data.bin", corosio::file_base::read_only); |
| 88 | +
|
| 89 | +char buf[256]; |
| 90 | +auto [ec, n] = co_await f.read_some_at( |
| 91 | + 1024, // byte offset |
| 92 | + capy::mutable_buffer(buf, sizeof(buf))); |
| 93 | +---- |
| 94 | + |
| 95 | +=== Writing at an Offset |
| 96 | + |
| 97 | +[source,cpp] |
| 98 | +---- |
| 99 | +corosio::random_access_file f(ioc); |
| 100 | +f.open("data.bin", corosio::file_base::read_write); |
| 101 | +
|
| 102 | +auto [ec, n] = co_await f.write_some_at( |
| 103 | + 512, capy::const_buffer("patched", 7)); |
| 104 | +---- |
| 105 | + |
| 106 | +== Open Flags |
| 107 | + |
| 108 | +Both file types accept a bitmask of `file_base::flags` when opening: |
| 109 | + |
| 110 | +[cols="1,3"] |
| 111 | +|=== |
| 112 | +| Flag | Meaning |
| 113 | + |
| 114 | +| `read_only` | Open for reading (default) |
| 115 | +| `write_only` | Open for writing |
| 116 | +| `read_write` | Open for both reading and writing |
| 117 | +| `create` | Create the file if it does not exist |
| 118 | +| `exclusive` | Fail if the file already exists (requires `create`) |
| 119 | +| `truncate` | Truncate the file to zero length on open |
| 120 | +| `append` | Seek to end on open (stream_file only) |
| 121 | +| `sync_all_on_write` | Synchronize data to disk on each write |
| 122 | +|=== |
| 123 | + |
| 124 | +Flags are combined with `|`: |
| 125 | + |
| 126 | +[source,cpp] |
| 127 | +---- |
| 128 | +f.open("log.txt", |
| 129 | + corosio::file_base::write_only |
| 130 | + | corosio::file_base::create |
| 131 | + | corosio::file_base::append); |
| 132 | +---- |
| 133 | + |
| 134 | +== File Metadata |
| 135 | + |
| 136 | +Both file types provide synchronous metadata operations: |
| 137 | + |
| 138 | +[source,cpp] |
| 139 | +---- |
| 140 | +auto bytes = f.size(); // file size in bytes |
| 141 | +f.resize(1024); // truncate or extend |
| 142 | +f.sync_data(); // flush data to stable storage |
| 143 | +f.sync_all(); // flush data and metadata |
| 144 | +---- |
| 145 | + |
| 146 | +`stream_file` additionally provides `seek()` for repositioning. |
| 147 | + |
| 148 | +== Native Handle Access |
| 149 | + |
| 150 | +Both file types support adopting and releasing native handles: |
| 151 | + |
| 152 | +[source,cpp] |
| 153 | +---- |
| 154 | +// Release ownership — caller must close the handle |
| 155 | +auto handle = f.release(); |
| 156 | +assert(!f.is_open()); |
| 157 | +
|
| 158 | +// Adopt an existing handle — file takes ownership |
| 159 | +corosio::random_access_file f2(ioc); |
| 160 | +f2.assign(handle); |
| 161 | +---- |
| 162 | + |
| 163 | +== Error Handling |
| 164 | + |
| 165 | +File operations follow the same error model as sockets. Reads past |
| 166 | +end-of-file return `capy::cond::eof`: |
| 167 | + |
| 168 | +[source,cpp] |
| 169 | +---- |
| 170 | +auto [ec, n] = co_await f.read_some(buf); |
| 171 | +if (ec == capy::cond::eof) |
| 172 | +{ |
| 173 | + // no more data |
| 174 | +} |
| 175 | +else if (ec) |
| 176 | +{ |
| 177 | + // I/O error |
| 178 | +} |
| 179 | +---- |
| 180 | + |
| 181 | +Opening a nonexistent file with `read_only` throws `std::system_error`. |
| 182 | +Use `create` to create files that may not exist. |
| 183 | + |
| 184 | +== Thread Safety |
| 185 | + |
| 186 | +* Distinct objects are safe to use concurrently. |
| 187 | +* `random_access_file` supports multiple concurrent reads and writes |
| 188 | + from coroutines sharing the same file object. Each operation is |
| 189 | + independently heap-allocated. |
| 190 | +* `stream_file` allows at most one asynchronous operation in flight at |
| 191 | + a time (same as Asio's stream_file). Sequential access with an |
| 192 | + implicit position makes concurrent ops semantically undefined. |
| 193 | +* Non-async operations (open, close, size, resize, etc.) require |
| 194 | + external synchronization. |
| 195 | + |
| 196 | +== Platform Notes |
| 197 | + |
| 198 | +On Linux, macOS, and BSD, file I/O is dispatched to a shared worker |
| 199 | +thread pool using `preadv`/`pwritev`. This is the same pool used by |
| 200 | +the resolver. |
| 201 | + |
| 202 | +On Windows, file I/O uses native IOCP overlapped I/O via |
| 203 | +`ReadFile`/`WriteFile` with `FILE_FLAG_OVERLAPPED`. |
0 commit comments