87.59% Lines (120/137)
94.74% Functions (18/19)
| 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_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | 10 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | |||||
| 11 | #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | 11 | #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/corosio/detail/platform.hpp> | 13 | #include <boost/corosio/detail/platform.hpp> | |||||
| 14 | 14 | |||||||
| 15 | #if BOOST_COROSIO_POSIX | 15 | #if BOOST_COROSIO_POSIX | |||||
| 16 | 16 | |||||||
| 17 | #include <boost/corosio/detail/config.hpp> | 17 | #include <boost/corosio/detail/config.hpp> | |||||
| 18 | #include <boost/corosio/stream_file.hpp> | 18 | #include <boost/corosio/stream_file.hpp> | |||||
| 19 | #include <boost/corosio/file_base.hpp> | 19 | #include <boost/corosio/file_base.hpp> | |||||
| 20 | #include <boost/corosio/detail/intrusive.hpp> | 20 | #include <boost/corosio/detail/intrusive.hpp> | |||||
| 21 | #include <boost/corosio/detail/dispatch_coro.hpp> | 21 | #include <boost/corosio/detail/dispatch_coro.hpp> | |||||
| 22 | #include <boost/corosio/detail/scheduler_op.hpp> | 22 | #include <boost/corosio/detail/scheduler_op.hpp> | |||||
| 23 | #include <boost/corosio/detail/thread_pool.hpp> | 23 | #include <boost/corosio/detail/thread_pool.hpp> | |||||
| 24 | #include <boost/corosio/detail/scheduler.hpp> | 24 | #include <boost/corosio/detail/scheduler.hpp> | |||||
| 25 | #include <boost/corosio/detail/buffer_param.hpp> | 25 | #include <boost/corosio/detail/buffer_param.hpp> | |||||
| 26 | #include <boost/corosio/native/detail/make_err.hpp> | 26 | #include <boost/corosio/native/detail/make_err.hpp> | |||||
| 27 | #include <boost/capy/ex/executor_ref.hpp> | 27 | #include <boost/capy/ex/executor_ref.hpp> | |||||
| 28 | #include <boost/capy/error.hpp> | 28 | #include <boost/capy/error.hpp> | |||||
| 29 | #include <boost/capy/buffers.hpp> | 29 | #include <boost/capy/buffers.hpp> | |||||
| 30 | 30 | |||||||
| 31 | #include <atomic> | 31 | #include <atomic> | |||||
| 32 | #include <coroutine> | 32 | #include <coroutine> | |||||
| 33 | #include <cstddef> | 33 | #include <cstddef> | |||||
| 34 | #include <cstdint> | 34 | #include <cstdint> | |||||
| 35 | #include <filesystem> | 35 | #include <filesystem> | |||||
| 36 | #include <limits> | 36 | #include <limits> | |||||
| 37 | #include <memory> | 37 | #include <memory> | |||||
| 38 | #include <optional> | 38 | #include <optional> | |||||
| 39 | #include <stop_token> | 39 | #include <stop_token> | |||||
| 40 | #include <system_error> | 40 | #include <system_error> | |||||
| 41 | 41 | |||||||
| 42 | #include <errno.h> | 42 | #include <errno.h> | |||||
| 43 | #include <fcntl.h> | 43 | #include <fcntl.h> | |||||
| 44 | #include <sys/stat.h> | 44 | #include <sys/stat.h> | |||||
| 45 | #include <sys/uio.h> | 45 | #include <sys/uio.h> | |||||
| 46 | #include <unistd.h> | 46 | #include <unistd.h> | |||||
| 47 | 47 | |||||||
| 48 | /* | 48 | /* | |||||
| 49 | POSIX Stream File Implementation | 49 | POSIX Stream File Implementation | |||||
| 50 | ================================= | 50 | ================================= | |||||
| 51 | 51 | |||||||
| 52 | Regular files cannot be monitored by epoll/kqueue/select — the kernel | 52 | Regular files cannot be monitored by epoll/kqueue/select — the kernel | |||||
| 53 | always reports them as ready. Blocking I/O (pread/pwrite) is dispatched | 53 | always reports them as ready. Blocking I/O (pread/pwrite) is dispatched | |||||
| 54 | to a shared thread pool, with completion posted back to the scheduler. | 54 | to a shared thread pool, with completion posted back to the scheduler. | |||||
| 55 | 55 | |||||||
| 56 | This follows the same pattern as posix_resolver: pool_work_item for | 56 | This follows the same pattern as posix_resolver: pool_work_item for | |||||
| 57 | dispatch, scheduler_op for completion, shared_from_this for lifetime. | 57 | dispatch, scheduler_op for completion, shared_from_this for lifetime. | |||||
| 58 | 58 | |||||||
| 59 | Completion Flow | 59 | Completion Flow | |||||
| 60 | --------------- | 60 | --------------- | |||||
| 61 | 1. read_some() sets up file_read_op, posts to thread pool | 61 | 1. read_some() sets up file_read_op, posts to thread pool | |||||
| 62 | 2. Pool thread runs preadv() (blocking) | 62 | 2. Pool thread runs preadv() (blocking) | |||||
| 63 | 3. Pool thread stores results, posts scheduler_op to scheduler | 63 | 3. Pool thread stores results, posts scheduler_op to scheduler | |||||
| 64 | 4. Scheduler invokes op() which resumes the coroutine | 64 | 4. Scheduler invokes op() which resumes the coroutine | |||||
| 65 | 65 | |||||||
| 66 | Single-Inflight Constraint | 66 | Single-Inflight Constraint | |||||
| 67 | -------------------------- | 67 | -------------------------- | |||||
| 68 | Only one asynchronous operation may be in flight at a time on a | 68 | Only one asynchronous operation may be in flight at a time on a | |||||
| 69 | given file object. Concurrent read and write is not supported | 69 | given file object. Concurrent read and write is not supported | |||||
| 70 | because both share offset_ without synchronization. | 70 | because both share offset_ without synchronization. | |||||
| 71 | */ | 71 | */ | |||||
| 72 | 72 | |||||||
| 73 | namespace boost::corosio::detail { | 73 | namespace boost::corosio::detail { | |||||
| 74 | 74 | |||||||
| 75 | struct scheduler; | 75 | struct scheduler; | |||||
| 76 | class posix_stream_file_service; | 76 | class posix_stream_file_service; | |||||
| 77 | 77 | |||||||
| 78 | /** Stream file implementation for POSIX backends. | 78 | /** Stream file implementation for POSIX backends. | |||||
| 79 | 79 | |||||||
| 80 | Each instance contains embedded operation objects (read_op_, write_op_) | 80 | Each instance contains embedded operation objects (read_op_, write_op_) | |||||
| 81 | that are reused across calls. This avoids per-operation heap allocation. | 81 | that are reused across calls. This avoids per-operation heap allocation. | |||||
| 82 | */ | 82 | */ | |||||
| 83 | class posix_stream_file final | 83 | class posix_stream_file final | |||||
| 84 | : public stream_file::implementation | 84 | : public stream_file::implementation | |||||
| 85 | , public std::enable_shared_from_this<posix_stream_file> | 85 | , public std::enable_shared_from_this<posix_stream_file> | |||||
| 86 | , public intrusive_list<posix_stream_file>::node | 86 | , public intrusive_list<posix_stream_file>::node | |||||
| 87 | { | 87 | { | |||||
| 88 | friend class posix_stream_file_service; | 88 | friend class posix_stream_file_service; | |||||
| 89 | 89 | |||||||
| 90 | public: | 90 | public: | |||||
| 91 | static constexpr std::size_t max_buffers = 16; | 91 | static constexpr std::size_t max_buffers = 16; | |||||
| 92 | 92 | |||||||
| 93 | /** Operation state for a single file read or write. */ | 93 | /** Operation state for a single file read or write. */ | |||||
| 94 | struct file_op : scheduler_op | 94 | struct file_op : scheduler_op | |||||
| 95 | { | 95 | { | |||||
| 96 | struct canceller | 96 | struct canceller | |||||
| 97 | { | 97 | { | |||||
| 98 | file_op* op; | 98 | file_op* op; | |||||
| HITCBC | 99 | 2 | void operator()() const noexcept | 99 | 2 | void operator()() const noexcept | ||
| 100 | { | 100 | { | |||||
| HITCBC | 101 | 2 | op->request_cancel(); | 101 | 2 | op->request_cancel(); | ||
| HITCBC | 102 | 2 | } | 102 | 2 | } | ||
| 103 | }; | 103 | }; | |||||
| 104 | 104 | |||||||
| 105 | // Coroutine state | 105 | // Coroutine state | |||||
| 106 | std::coroutine_handle<> h; | 106 | std::coroutine_handle<> h; | |||||
| 107 | capy::continuation cont; | 107 | capy::continuation cont; | |||||
| 108 | capy::executor_ref ex; | 108 | capy::executor_ref ex; | |||||
| 109 | 109 | |||||||
| 110 | // Output pointers | 110 | // Output pointers | |||||
| 111 | std::error_code* ec_out = nullptr; | 111 | std::error_code* ec_out = nullptr; | |||||
| 112 | std::size_t* bytes_out = nullptr; | 112 | std::size_t* bytes_out = nullptr; | |||||
| 113 | 113 | |||||||
| 114 | // Buffer data (copied from buffer_param at submission time) | 114 | // Buffer data (copied from buffer_param at submission time) | |||||
| 115 | iovec iovecs[max_buffers]; | 115 | iovec iovecs[max_buffers]; | |||||
| 116 | int iovec_count = 0; | 116 | int iovec_count = 0; | |||||
| 117 | 117 | |||||||
| 118 | // Result storage (populated by worker thread) | 118 | // Result storage (populated by worker thread) | |||||
| 119 | int errn = 0; | 119 | int errn = 0; | |||||
| 120 | std::size_t bytes_transferred = 0; | 120 | std::size_t bytes_transferred = 0; | |||||
| 121 | bool is_read = false; | 121 | bool is_read = false; | |||||
| 122 | 122 | |||||||
| 123 | // Thread coordination | 123 | // Thread coordination | |||||
| 124 | std::atomic<bool> cancelled{false}; | 124 | std::atomic<bool> cancelled{false}; | |||||
| 125 | std::optional<std::stop_callback<canceller>> stop_cb; | 125 | std::optional<std::stop_callback<canceller>> stop_cb; | |||||
| 126 | 126 | |||||||
| 127 | /// Prevents use-after-free when file is closed with pending ops. | 127 | /// Prevents use-after-free when file is closed with pending ops. | |||||
| 128 | std::shared_ptr<void> impl_ref; | 128 | std::shared_ptr<void> impl_ref; | |||||
| 129 | 129 | |||||||
| HITCBC | 130 | 156 | file_op() = default; | 130 | 156 | file_op() = default; | ||
| 131 | 131 | |||||||
| HITCBC | 132 | 36 | void reset() noexcept | 132 | 36 | void reset() noexcept | ||
| 133 | { | 133 | { | |||||
| HITCBC | 134 | 36 | iovec_count = 0; | 134 | 36 | iovec_count = 0; | ||
| HITCBC | 135 | 36 | errn = 0; | 135 | 36 | errn = 0; | ||
| HITCBC | 136 | 36 | bytes_transferred = 0; | 136 | 36 | bytes_transferred = 0; | ||
| HITCBC | 137 | 36 | is_read = false; | 137 | 36 | is_read = false; | ||
| HITCBC | 138 | 36 | cancelled.store(false, std::memory_order_relaxed); | 138 | 36 | cancelled.store(false, std::memory_order_relaxed); | ||
| HITCBC | 139 | 36 | stop_cb.reset(); | 139 | 36 | stop_cb.reset(); | ||
| HITCBC | 140 | 36 | impl_ref.reset(); | 140 | 36 | impl_ref.reset(); | ||
| HITCBC | 141 | 36 | ec_out = nullptr; | 141 | 36 | ec_out = nullptr; | ||
| HITCBC | 142 | 36 | bytes_out = nullptr; | 142 | 36 | bytes_out = nullptr; | ||
| HITCBC | 143 | 36 | } | 143 | 36 | } | ||
| 144 | 144 | |||||||
| 145 | void operator()() override; | 145 | void operator()() override; | |||||
| 146 | void destroy() override; | 146 | void destroy() override; | |||||
| 147 | 147 | |||||||
| HITCBC | 148 | 430 | void request_cancel() noexcept | 148 | 430 | void request_cancel() noexcept | ||
| 149 | { | 149 | { | |||||
| HITCBC | 150 | 430 | cancelled.store(true, std::memory_order_release); | 150 | 430 | cancelled.store(true, std::memory_order_release); | ||
| HITCBC | 151 | 430 | } | 151 | 430 | } | ||
| 152 | 152 | |||||||
| HITCBC | 153 | 32 | void start(std::stop_token const& token) | 153 | 32 | void start(std::stop_token const& token) | ||
| 154 | { | 154 | { | |||||
| HITCBC | 155 | 32 | cancelled.store(false, std::memory_order_release); | 155 | 32 | cancelled.store(false, std::memory_order_release); | ||
| HITCBC | 156 | 32 | stop_cb.reset(); | 156 | 32 | stop_cb.reset(); | ||
| HITCBC | 157 | 32 | if (token.stop_possible()) | 157 | 32 | if (token.stop_possible()) | ||
| HITCBC | 158 | 2 | stop_cb.emplace(token, canceller{this}); | 158 | 2 | stop_cb.emplace(token, canceller{this}); | ||
| HITCBC | 159 | 32 | } | 159 | 32 | } | ||
| 160 | }; | 160 | }; | |||||
| 161 | 161 | |||||||
| 162 | /** Pool work item for thread pool dispatch. */ | 162 | /** Pool work item for thread pool dispatch. */ | |||||
| 163 | struct pool_op : pool_work_item | 163 | struct pool_op : pool_work_item | |||||
| 164 | { | 164 | { | |||||
| 165 | posix_stream_file* file_ = nullptr; | 165 | posix_stream_file* file_ = nullptr; | |||||
| 166 | std::shared_ptr<posix_stream_file> ref_; | 166 | std::shared_ptr<posix_stream_file> ref_; | |||||
| 167 | }; | 167 | }; | |||||
| 168 | 168 | |||||||
| 169 | explicit posix_stream_file(posix_stream_file_service& svc) noexcept; | 169 | explicit posix_stream_file(posix_stream_file_service& svc) noexcept; | |||||
| 170 | 170 | |||||||
| 171 | // -- io_stream::implementation -- | 171 | // -- io_stream::implementation -- | |||||
| 172 | 172 | |||||||
| 173 | std::coroutine_handle<> read_some( | 173 | std::coroutine_handle<> read_some( | |||||
| 174 | std::coroutine_handle<>, | 174 | std::coroutine_handle<>, | |||||
| 175 | capy::executor_ref, | 175 | capy::executor_ref, | |||||
| 176 | buffer_param, | 176 | buffer_param, | |||||
| 177 | std::stop_token, | 177 | std::stop_token, | |||||
| 178 | std::error_code*, | 178 | std::error_code*, | |||||
| 179 | std::size_t*) override; | 179 | std::size_t*) override; | |||||
| 180 | 180 | |||||||
| 181 | std::coroutine_handle<> write_some( | 181 | std::coroutine_handle<> write_some( | |||||
| 182 | std::coroutine_handle<>, | 182 | std::coroutine_handle<>, | |||||
| 183 | capy::executor_ref, | 183 | capy::executor_ref, | |||||
| 184 | buffer_param, | 184 | buffer_param, | |||||
| 185 | std::stop_token, | 185 | std::stop_token, | |||||
| 186 | std::error_code*, | 186 | std::error_code*, | |||||
| 187 | std::size_t*) override; | 187 | std::size_t*) override; | |||||
| 188 | 188 | |||||||
| 189 | // -- stream_file::implementation -- | 189 | // -- stream_file::implementation -- | |||||
| 190 | 190 | |||||||
| HITCBC | 191 | 238 | native_handle_type native_handle() const noexcept override | 191 | 238 | native_handle_type native_handle() const noexcept override | ||
| 192 | { | 192 | { | |||||
| HITCBC | 193 | 238 | return fd_; | 193 | 238 | return fd_; | ||
| 194 | } | 194 | } | |||||
| 195 | 195 | |||||||
| HITCBC | 196 | 214 | void cancel() noexcept override | 196 | 214 | void cancel() noexcept override | ||
| 197 | { | 197 | { | |||||
| HITCBC | 198 | 214 | read_op_.request_cancel(); | 198 | 214 | read_op_.request_cancel(); | ||
| HITCBC | 199 | 214 | write_op_.request_cancel(); | 199 | 214 | write_op_.request_cancel(); | ||
| HITCBC | 200 | 214 | } | 200 | 214 | } | ||
| 201 | 201 | |||||||
| 202 | std::uint64_t size() const override; | 202 | std::uint64_t size() const override; | |||||
| 203 | void resize(std::uint64_t new_size) override; | 203 | void resize(std::uint64_t new_size) override; | |||||
| 204 | void sync_data() override; | 204 | void sync_data() override; | |||||
| 205 | void sync_all() override; | 205 | void sync_all() override; | |||||
| 206 | native_handle_type release() override; | 206 | native_handle_type release() override; | |||||
| 207 | void assign(native_handle_type handle) override; | 207 | void assign(native_handle_type handle) override; | |||||
| 208 | std::uint64_t seek(std::int64_t offset, file_base::seek_basis origin) override; | 208 | std::uint64_t seek(std::int64_t offset, file_base::seek_basis origin) override; | |||||
| 209 | 209 | |||||||
| 210 | // -- Internal -- | 210 | // -- Internal -- | |||||
| 211 | 211 | |||||||
| 212 | /** Open the file and store the fd. */ | 212 | /** Open the file and store the fd. */ | |||||
| 213 | std::error_code open_file( | 213 | std::error_code open_file( | |||||
| 214 | std::filesystem::path const& path, file_base::flags mode); | 214 | std::filesystem::path const& path, file_base::flags mode); | |||||
| 215 | 215 | |||||||
| 216 | /** Close the file descriptor. */ | 216 | /** Close the file descriptor. */ | |||||
| 217 | void close_file() noexcept; | 217 | void close_file() noexcept; | |||||
| 218 | 218 | |||||||
| 219 | private: | 219 | private: | |||||
| 220 | posix_stream_file_service& svc_; | 220 | posix_stream_file_service& svc_; | |||||
| 221 | int fd_ = -1; | 221 | int fd_ = -1; | |||||
| 222 | std::uint64_t offset_ = 0; | 222 | std::uint64_t offset_ = 0; | |||||
| 223 | 223 | |||||||
| 224 | file_op read_op_; | 224 | file_op read_op_; | |||||
| 225 | file_op write_op_; | 225 | file_op write_op_; | |||||
| 226 | pool_op read_pool_op_; | 226 | pool_op read_pool_op_; | |||||
| 227 | pool_op write_pool_op_; | 227 | pool_op write_pool_op_; | |||||
| 228 | 228 | |||||||
| 229 | static void do_read_work(pool_work_item*) noexcept; | 229 | static void do_read_work(pool_work_item*) noexcept; | |||||
| 230 | static void do_write_work(pool_work_item*) noexcept; | 230 | static void do_write_work(pool_work_item*) noexcept; | |||||
| 231 | }; | 231 | }; | |||||
| 232 | 232 | |||||||
| 233 | // --------------------------------------------------------------------------- | 233 | // --------------------------------------------------------------------------- | |||||
| 234 | // Inline implementation | 234 | // Inline implementation | |||||
| 235 | // --------------------------------------------------------------------------- | 235 | // --------------------------------------------------------------------------- | |||||
| 236 | 236 | |||||||
| 237 | inline | 237 | inline | |||||
| HITCBC | 238 | 78 | posix_stream_file::posix_stream_file(posix_stream_file_service& svc) noexcept | 238 | 78 | posix_stream_file::posix_stream_file(posix_stream_file_service& svc) noexcept | ||
| HITCBC | 239 | 78 | : svc_(svc) | 239 | 78 | : svc_(svc) | ||
| 240 | { | 240 | { | |||||
| HITCBC | 241 | 78 | } | 241 | 78 | } | ||
| 242 | 242 | |||||||
| 243 | inline std::error_code | 243 | inline std::error_code | |||||
| HITCBC | 244 | 60 | posix_stream_file::open_file( | 244 | 60 | posix_stream_file::open_file( | ||
| 245 | std::filesystem::path const& path, file_base::flags mode) | 245 | std::filesystem::path const& path, file_base::flags mode) | |||||
| 246 | { | 246 | { | |||||
| HITCBC | 247 | 60 | close_file(); | 247 | 60 | close_file(); | ||
| 248 | 248 | |||||||
| HITCBC | 249 | 60 | int oflags = 0; | 249 | 60 | int oflags = 0; | ||
| 250 | 250 | |||||||
| 251 | // Access mode | 251 | // Access mode | |||||
| HITCBC | 252 | 60 | unsigned access = static_cast<unsigned>(mode) & 3u; | 252 | 60 | unsigned access = static_cast<unsigned>(mode) & 3u; | ||
| HITCBC | 253 | 60 | if (access == static_cast<unsigned>(file_base::read_write)) | 253 | 60 | if (access == static_cast<unsigned>(file_base::read_write)) | ||
| HITCBC | 254 | 4 | oflags |= O_RDWR; | 254 | 4 | oflags |= O_RDWR; | ||
| HITCBC | 255 | 56 | else if (access == static_cast<unsigned>(file_base::write_only)) | 255 | 56 | else if (access == static_cast<unsigned>(file_base::write_only)) | ||
| HITCBC | 256 | 20 | oflags |= O_WRONLY; | 256 | 20 | oflags |= O_WRONLY; | ||
| 257 | else | 257 | else | |||||
| HITCBC | 258 | 36 | oflags |= O_RDONLY; | 258 | 36 | oflags |= O_RDONLY; | ||
| 259 | 259 | |||||||
| 260 | // Creation flags | 260 | // Creation flags | |||||
| HITCBC | 261 | 60 | if ((mode & file_base::create) != file_base::flags(0)) | 261 | 60 | if ((mode & file_base::create) != file_base::flags(0)) | ||
| HITCBC | 262 | 18 | oflags |= O_CREAT; | 262 | 18 | oflags |= O_CREAT; | ||
| HITCBC | 263 | 60 | if ((mode & file_base::exclusive) != file_base::flags(0)) | 263 | 60 | if ((mode & file_base::exclusive) != file_base::flags(0)) | ||
| HITCBC | 264 | 2 | oflags |= O_EXCL; | 264 | 2 | oflags |= O_EXCL; | ||
| HITCBC | 265 | 60 | if ((mode & file_base::truncate) != file_base::flags(0)) | 265 | 60 | if ((mode & file_base::truncate) != file_base::flags(0)) | ||
| HITCBC | 266 | 16 | oflags |= O_TRUNC; | 266 | 16 | oflags |= O_TRUNC; | ||
| HITCBC | 267 | 60 | if ((mode & file_base::append) != file_base::flags(0)) | 267 | 60 | if ((mode & file_base::append) != file_base::flags(0)) | ||
| HITCBC | 268 | 2 | oflags |= O_APPEND; | 268 | 2 | oflags |= O_APPEND; | ||
| HITCBC | 269 | 60 | if ((mode & file_base::sync_all_on_write) != file_base::flags(0)) | 269 | 60 | if ((mode & file_base::sync_all_on_write) != file_base::flags(0)) | ||
| HITCBC | 270 | 2 | oflags |= O_SYNC; | 270 | 2 | oflags |= O_SYNC; | ||
| 271 | 271 | |||||||
| HITCBC | 272 | 60 | int fd = ::open(path.c_str(), oflags, 0666); | 272 | 60 | int fd = ::open(path.c_str(), oflags, 0666); | ||
| HITCBC | 273 | 60 | if (fd < 0) | 273 | 60 | if (fd < 0) | ||
| HITCBC | 274 | 4 | return make_err(errno); | 274 | 4 | return make_err(errno); | ||
| 275 | 275 | |||||||
| HITCBC | 276 | 56 | fd_ = fd; | 276 | 56 | fd_ = fd; | ||
| HITCBC | 277 | 56 | offset_ = 0; | 277 | 56 | offset_ = 0; | ||
| 278 | 278 | |||||||
| 279 | // Append mode: position at end-of-file (preadv/pwritev use | 279 | // Append mode: position at end-of-file (preadv/pwritev use | |||||
| 280 | // explicit offsets, so O_APPEND alone is not sufficient). | 280 | // explicit offsets, so O_APPEND alone is not sufficient). | |||||
| HITCBC | 281 | 56 | if ((mode & file_base::append) != file_base::flags(0)) | 281 | 56 | if ((mode & file_base::append) != file_base::flags(0)) | ||
| 282 | { | 282 | { | |||||
| 283 | struct stat st; | 283 | struct stat st; | |||||
| HITCBC | 284 | 2 | if (::fstat(fd, &st) < 0) | 284 | 2 | if (::fstat(fd, &st) < 0) | ||
| 285 | { | 285 | { | |||||
| MISUBC | 286 | ✗ | int err = errno; | 286 | ✗ | int err = errno; | ||
| MISUBC | 287 | ✗ | ::close(fd); | 287 | ✗ | ::close(fd); | ||
| MISUBC | 288 | ✗ | fd_ = -1; | 288 | ✗ | fd_ = -1; | ||
| MISUBC | 289 | ✗ | return make_err(err); | 289 | ✗ | return make_err(err); | ||
| 290 | } | 290 | } | |||||
| HITCBC | 291 | 2 | offset_ = static_cast<std::uint64_t>(st.st_size); | 291 | 2 | offset_ = static_cast<std::uint64_t>(st.st_size); | ||
| 292 | } | 292 | } | |||||
| 293 | 293 | |||||||
| 294 | #ifdef POSIX_FADV_SEQUENTIAL | 294 | #ifdef POSIX_FADV_SEQUENTIAL | |||||
| HITCBC | 295 | 56 | ::posix_fadvise(fd_, 0, 0, POSIX_FADV_SEQUENTIAL); | 295 | 56 | ::posix_fadvise(fd_, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
| 296 | #endif | 296 | #endif | |||||
| 297 | 297 | |||||||
| HITCBC | 298 | 56 | return {}; | 298 | 56 | return {}; | ||
| 299 | } | 299 | } | |||||
| 300 | 300 | |||||||
| 301 | inline void | 301 | inline void | |||||
| HITCBC | 302 | 274 | posix_stream_file::close_file() noexcept | 302 | 274 | posix_stream_file::close_file() noexcept | ||
| 303 | { | 303 | { | |||||
| HITCBC | 304 | 274 | if (fd_ >= 0) | 304 | 274 | if (fd_ >= 0) | ||
| 305 | { | 305 | { | |||||
| HITCBC | 306 | 56 | ::close(fd_); | 306 | 56 | ::close(fd_); | ||
| HITCBC | 307 | 56 | fd_ = -1; | 307 | 56 | fd_ = -1; | ||
| 308 | } | 308 | } | |||||
| HITCBC | 309 | 274 | } | 309 | 274 | } | ||
| 310 | 310 | |||||||
| 311 | inline std::uint64_t | 311 | inline std::uint64_t | |||||
| HITCBC | 312 | 6 | posix_stream_file::size() const | 312 | 6 | posix_stream_file::size() const | ||
| 313 | { | 313 | { | |||||
| 314 | struct stat st; | 314 | struct stat st; | |||||
| HITCBC | 315 | 6 | if (::fstat(fd_, &st) < 0) | 315 | 6 | if (::fstat(fd_, &st) < 0) | ||
| MISUBC | 316 | ✗ | throw_system_error(make_err(errno), "stream_file::size"); | 316 | ✗ | throw_system_error(make_err(errno), "stream_file::size"); | ||
| HITCBC | 317 | 6 | return static_cast<std::uint64_t>(st.st_size); | 317 | 6 | return static_cast<std::uint64_t>(st.st_size); | ||
| 318 | } | 318 | } | |||||
| 319 | 319 | |||||||
| 320 | inline void | 320 | inline void | |||||
| HITCBC | 321 | 4 | posix_stream_file::resize(std::uint64_t new_size) | 321 | 4 | posix_stream_file::resize(std::uint64_t new_size) | ||
| 322 | { | 322 | { | |||||
| HITCBC | 323 | 4 | if (new_size > static_cast<std::uint64_t>(std::numeric_limits<off_t>::max())) | 323 | 4 | if (new_size > static_cast<std::uint64_t>(std::numeric_limits<off_t>::max())) | ||
| HITCBC | 324 | 2 | throw_system_error(make_err(EOVERFLOW), "stream_file::resize"); | 324 | 2 | throw_system_error(make_err(EOVERFLOW), "stream_file::resize"); | ||
| HITCBC | 325 | 2 | if (::ftruncate(fd_, static_cast<off_t>(new_size)) < 0) | 325 | 2 | if (::ftruncate(fd_, static_cast<off_t>(new_size)) < 0) | ||
| MISUBC | 326 | ✗ | throw_system_error(make_err(errno), "stream_file::resize"); | 326 | ✗ | throw_system_error(make_err(errno), "stream_file::resize"); | ||
| HITCBC | 327 | 2 | } | 327 | 2 | } | ||
| 328 | 328 | |||||||
| 329 | inline void | 329 | inline void | |||||
| HITCBC | 330 | 2 | posix_stream_file::sync_data() | 330 | 2 | posix_stream_file::sync_data() | ||
| 331 | { | 331 | { | |||||
| 332 | #if BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | 332 | #if BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | |||||
| HITCBC | 333 | 2 | if (::fdatasync(fd_) < 0) | 333 | 2 | if (::fdatasync(fd_) < 0) | ||
| 334 | #else // BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | 334 | #else // BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | |||||
| 335 | if (::fsync(fd_) < 0) | 335 | if (::fsync(fd_) < 0) | |||||
| 336 | #endif // BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | 336 | #endif // BOOST_COROSIO_HAS_POSIX_SYNCHRONIZED_IO | |||||
| MISUBC | 337 | ✗ | throw_system_error(make_err(errno), "stream_file::sync_data"); | 337 | ✗ | throw_system_error(make_err(errno), "stream_file::sync_data"); | ||
| HITCBC | 338 | 2 | } | 338 | 2 | } | ||
| 339 | 339 | |||||||
| 340 | inline void | 340 | inline void | |||||
| HITCBC | 341 | 2 | posix_stream_file::sync_all() | 341 | 2 | posix_stream_file::sync_all() | ||
| 342 | { | 342 | { | |||||
| HITCBC | 343 | 2 | if (::fsync(fd_) < 0) | 343 | 2 | if (::fsync(fd_) < 0) | ||
| MISUBC | 344 | ✗ | throw_system_error(make_err(errno), "stream_file::sync_all"); | 344 | ✗ | throw_system_error(make_err(errno), "stream_file::sync_all"); | ||
| HITCBC | 345 | 2 | } | 345 | 2 | } | ||
| 346 | 346 | |||||||
| 347 | inline native_handle_type | 347 | inline native_handle_type | |||||
| HITCBC | 348 | 2 | posix_stream_file::release() | 348 | 2 | posix_stream_file::release() | ||
| 349 | { | 349 | { | |||||
| HITCBC | 350 | 2 | int fd = fd_; | 350 | 2 | int fd = fd_; | ||
| HITCBC | 351 | 2 | fd_ = -1; | 351 | 2 | fd_ = -1; | ||
| HITCBC | 352 | 2 | offset_ = 0; | 352 | 2 | offset_ = 0; | ||
| HITCBC | 353 | 2 | return fd; | 353 | 2 | return fd; | ||
| 354 | } | 354 | } | |||||
| 355 | 355 | |||||||
| 356 | inline void | 356 | inline void | |||||
| HITCBC | 357 | 2 | posix_stream_file::assign(native_handle_type handle) | 357 | 2 | posix_stream_file::assign(native_handle_type handle) | ||
| 358 | { | 358 | { | |||||
| HITCBC | 359 | 2 | close_file(); | 359 | 2 | close_file(); | ||
| HITCBC | 360 | 2 | fd_ = handle; | 360 | 2 | fd_ = handle; | ||
| HITCBC | 361 | 2 | offset_ = 0; | 361 | 2 | offset_ = 0; | ||
| HITCBC | 362 | 2 | } | 362 | 2 | } | ||
| 363 | 363 | |||||||
| 364 | inline std::uint64_t | 364 | inline std::uint64_t | |||||
| HITCBC | 365 | 14 | posix_stream_file::seek(std::int64_t offset, file_base::seek_basis origin) | 365 | 14 | posix_stream_file::seek(std::int64_t offset, file_base::seek_basis origin) | ||
| 366 | { | 366 | { | |||||
| 367 | // We track offset_ ourselves (not the kernel fd offset) | 367 | // We track offset_ ourselves (not the kernel fd offset) | |||||
| 368 | // because preadv/pwritev use explicit offsets. | 368 | // because preadv/pwritev use explicit offsets. | |||||
| 369 | std::int64_t new_pos; | 369 | std::int64_t new_pos; | |||||
| 370 | 370 | |||||||
| HITCBC | 371 | 14 | if (origin == file_base::seek_set) | 371 | 14 | if (origin == file_base::seek_set) | ||
| 372 | { | 372 | { | |||||
| HITCBC | 373 | 6 | new_pos = offset; | 373 | 6 | new_pos = offset; | ||
| 374 | } | 374 | } | |||||
| HITCBC | 375 | 8 | else if (origin == file_base::seek_cur) | 375 | 8 | else if (origin == file_base::seek_cur) | ||
| 376 | { | 376 | { | |||||
| HITCBC | 377 | 4 | new_pos = static_cast<std::int64_t>(offset_) + offset; | 377 | 4 | new_pos = static_cast<std::int64_t>(offset_) + offset; | ||
| 378 | } | 378 | } | |||||
| 379 | else | 379 | else | |||||
| 380 | { | 380 | { | |||||
| 381 | struct stat st; | 381 | struct stat st; | |||||
| HITCBC | 382 | 4 | if (::fstat(fd_, &st) < 0) | 382 | 4 | if (::fstat(fd_, &st) < 0) | ||
| MISUBC | 383 | ✗ | throw_system_error(make_err(errno), "stream_file::seek"); | 383 | ✗ | throw_system_error(make_err(errno), "stream_file::seek"); | ||
| HITCBC | 384 | 4 | new_pos = st.st_size + offset; | 384 | 4 | new_pos = st.st_size + offset; | ||
| 385 | } | 385 | } | |||||
| 386 | 386 | |||||||
| HITCBC | 387 | 14 | if (new_pos < 0) | 387 | 14 | if (new_pos < 0) | ||
| HITCBC | 388 | 6 | throw_system_error(make_err(EINVAL), "stream_file::seek"); | 388 | 6 | throw_system_error(make_err(EINVAL), "stream_file::seek"); | ||
| HITCBC | 389 | 8 | if (new_pos > static_cast<std::int64_t>(std::numeric_limits<off_t>::max())) | 389 | 8 | if (new_pos > static_cast<std::int64_t>(std::numeric_limits<off_t>::max())) | ||
| MISUBC | 390 | ✗ | throw_system_error(make_err(EOVERFLOW), "stream_file::seek"); | 390 | ✗ | throw_system_error(make_err(EOVERFLOW), "stream_file::seek"); | ||
| 391 | 391 | |||||||
| HITCBC | 392 | 8 | offset_ = static_cast<std::uint64_t>(new_pos); | 392 | 8 | offset_ = static_cast<std::uint64_t>(new_pos); | ||
| 393 | 393 | |||||||
| HITCBC | 394 | 8 | return offset_; | 394 | 8 | return offset_; | ||
| 395 | } | 395 | } | |||||
| 396 | 396 | |||||||
| 397 | // -- file_op completion handler -- | 397 | // -- file_op completion handler -- | |||||
| 398 | // (read_some, write_some, do_read_work, do_write_work are | 398 | // (read_some, write_some, do_read_work, do_write_work are | |||||
| 399 | // defined in posix_stream_file_service.hpp after the service) | 399 | // defined in posix_stream_file_service.hpp after the service) | |||||
| 400 | 400 | |||||||
| 401 | inline void | 401 | inline void | |||||
| HITCBC | 402 | 32 | posix_stream_file::file_op::operator()() | 402 | 32 | posix_stream_file::file_op::operator()() | ||
| 403 | { | 403 | { | |||||
| HITCBC | 404 | 32 | stop_cb.reset(); | 404 | 32 | stop_cb.reset(); | ||
| 405 | 405 | |||||||
| HITCBC | 406 | 32 | bool const was_cancelled = cancelled.load(std::memory_order_acquire); | 406 | 32 | bool const was_cancelled = cancelled.load(std::memory_order_acquire); | ||
| 407 | 407 | |||||||
| HITCBC | 408 | 32 | if (ec_out) | 408 | 32 | if (ec_out) | ||
| 409 | { | 409 | { | |||||
| HITCBC | 410 | 32 | if (was_cancelled) | 410 | 32 | if (was_cancelled) | ||
| HITCBC | 411 | 2 | *ec_out = capy::error::canceled; | 411 | 2 | *ec_out = capy::error::canceled; | ||
| HITCBC | 412 | 30 | else if (errn != 0) | 412 | 30 | else if (errn != 0) | ||
| MISUBC | 413 | ✗ | *ec_out = make_err(errn); | 413 | ✗ | *ec_out = make_err(errn); | ||
| HITCBC | 414 | 30 | else if (is_read && bytes_transferred == 0) | 414 | 30 | else if (is_read && bytes_transferred == 0) | ||
| HITCBC | 415 | 2 | *ec_out = capy::error::eof; | 415 | 2 | *ec_out = capy::error::eof; | ||
| 416 | else | 416 | else | |||||
| HITCBC | 417 | 28 | *ec_out = {}; | 417 | 28 | *ec_out = {}; | ||
| 418 | } | 418 | } | |||||
| 419 | 419 | |||||||
| HITCBC | 420 | 32 | if (bytes_out) | 420 | 32 | if (bytes_out) | ||
| HITCBC | 421 | 32 | *bytes_out = was_cancelled ? 0 : bytes_transferred; | 421 | 32 | *bytes_out = was_cancelled ? 0 : bytes_transferred; | ||
| 422 | 422 | |||||||
| 423 | // Move impl_ref to a local so members remain valid through | 423 | // Move impl_ref to a local so members remain valid through | |||||
| 424 | // dispatch — impl_ref may be the last shared_ptr keeping | 424 | // dispatch — impl_ref may be the last shared_ptr keeping | |||||
| 425 | // the parent posix_stream_file (which embeds this file_op) alive. | 425 | // the parent posix_stream_file (which embeds this file_op) alive. | |||||
| HITCBC | 426 | 32 | auto prevent_destroy = std::move(impl_ref); | 426 | 32 | auto prevent_destroy = std::move(impl_ref); | ||
| HITCBC | 427 | 32 | ex.on_work_finished(); | 427 | 32 | ex.on_work_finished(); | ||
| HITCBC | 428 | 32 | cont.h = h; | 428 | 32 | cont.h = h; | ||
| HITCBC | 429 | 32 | dispatch_coro(ex, cont).resume(); | 429 | 32 | dispatch_coro(ex, cont).resume(); | ||
| HITCBC | 430 | 32 | } | 430 | 32 | } | ||
| 431 | 431 | |||||||
| 432 | inline void | 432 | inline void | |||||
| MISUBC | 433 | ✗ | posix_stream_file::file_op::destroy() | 433 | ✗ | posix_stream_file::file_op::destroy() | ||
| 434 | { | 434 | { | |||||
| MISUBC | 435 | ✗ | stop_cb.reset(); | 435 | ✗ | stop_cb.reset(); | ||
| MISUBC | 436 | ✗ | auto local_ex = ex; | 436 | ✗ | auto local_ex = ex; | ||
| MISUBC | 437 | ✗ | impl_ref.reset(); | 437 | ✗ | impl_ref.reset(); | ||
| MISUBC | 438 | ✗ | local_ex.on_work_finished(); | 438 | ✗ | local_ex.on_work_finished(); | ||
| MISUBC | 439 | ✗ | } | 439 | ✗ | } | ||
| 440 | 440 | |||||||
| 441 | } // namespace boost::corosio::detail | 441 | } // namespace boost::corosio::detail | |||||
| 442 | 442 | |||||||
| 443 | #endif // BOOST_COROSIO_POSIX | 443 | #endif // BOOST_COROSIO_POSIX | |||||
| 444 | 444 | |||||||
| 445 | #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | 445 | #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_STREAM_FILE_HPP | |||||