#pragma once #include #include #include #include #include #include #include #include #include #include namespace Void::IO { // Flags for Driver::open_file. read+write maps to O_RDWR, write-only to // O_WRONLY and read-only to O_RDONLY; neither read nor write, truncate without // write, or exclusive without create is invalid_argument. O_CLOEXEC is always // set and open(2) runs on the calling thread with ordinary path semantics. struct FileOpenOptions { bool read{true}; bool write{false}; bool create{false}; bool truncate{false}; bool exclusive{false}; std::uint32_t mode{0600}; }; enum class SyncMode : std::uint8_t { data, // fdatasync full // fsync }; // Result of one positional-file syscall: on success `error == 0` and `value` // holds the byte count (0 for sync/close); on failure `error` is the errno. struct FileSyscallResult { std::int64_t value{0}; int error{0}; }; // Injectable syscall shim used by the Driver's file workers. The base class // is the native implementation (one syscall per call, no EINTR retry; the // Driver retries EINTR). An override runs on file workers or inside // Driver::run_file_work(); it must not call Driver, Executor or File APIs, and // it must outlive Driver teardown (teardown closes remaining fds through it). class FileSyscalls { public: virtual ~FileSyscalls() = default; [[nodiscard]] virtual FileSyscallResult pread( int fd, std::span buf, std::uint64_t offset) noexcept; [[nodiscard]] virtual FileSyscallResult pwrite( int fd, std::span buf, std::uint64_t offset) noexcept; [[nodiscard]] virtual FileSyscallResult sync(int fd, SyncMode mode) noexcept; [[nodiscard]] virtual FileSyscallResult close(int fd) noexcept; [[nodiscard]] static FileSyscalls& native() noexcept; }; namespace detail { class FileState; } // namespace detail // Move-only handle to a positional file owned by a Driver. Requests run on // the Driver's bounded file workers (or Driver::run_file_work) and are // delivered only by a poll turn. // // - Writes and syncs on one File run in admission order, one at a time; reads // may run concurrently and are unordered relative to writes. // - A queued request that is stopped or expires completes cancelled/timeout // without a syscall. An active request keeps its buffer and reports the // syscall's actual outcome even after stop or expiry. // - Dropping a File with requests in flight defers the close until they have // been delivered; the close never blocks the dropping thread. // - Unlike ByteStream::close_and_join (which consumes the stream at once), // File::close_and_join waits for the file's queued and active requests, // never cancels them, and invalidates the File only on success. On deadline // or stop it returns incomplete_shutdown and the File stays valid but // closing (new requests fail with closed; a repeated call waits again). // - After Driver teardown every request fails with closed without a syscall // and close_and_join succeeds immediately. class File { public: File() noexcept; File(File&& other) noexcept; File& operator=(File&& other) noexcept; ~File(); File(const File&) = delete; File& operator=(const File&) = delete; [[nodiscard]] bool valid() const noexcept { return state_ != nullptr; } [[nodiscard]] DescriptorId id() const noexcept; [[nodiscard]] Coro::Task> read_at( std::uint64_t offset, std::span buf, Async::OperationOptions options = {}); [[nodiscard]] Coro::Task> write_at( std::uint64_t offset, std::span buf, Async::OperationOptions options = {}); [[nodiscard]] Coro::Task> read_at_owned( std::uint64_t offset, BufferLease& lease, Async::OperationOptions options = {}) { auto res = co_await read_at(offset, lease.writable(), std::move(options)); if (res.has_value()) { lease.set_valid_bytes(res->bytes); } co_return res; } [[nodiscard]] Coro::Task> write_at_owned( std::uint64_t offset, const BufferLease& lease, Async::OperationOptions options = {}) { co_return co_await write_at(offset, lease.readable(), std::move(options)); } [[nodiscard]] Coro::Task> sync( SyncMode mode = SyncMode::full, Async::OperationOptions options = {}); [[nodiscard]] Coro::Task> close_and_join( Async::OperationOptions options = {}); private: friend class Driver; // the only producer of open Files explicit File(std::shared_ptr state) noexcept; void release_handle() noexcept; std::shared_ptr state_{}; }; // Composed positional transfers mirroring read_exact/write_all: the offset // advances with each chunk and one deadline/stop applies to the whole call. [[nodiscard]] inline Coro::Task> read_exact_at(File& file, std::uint64_t offset, std::span buf, Async::OperationOptions options = {}) { std::size_t total = 0; while (total < buf.size()) { if (options.stop.stop_requested()) { co_return std::unexpected{ComposedTransferError{ total, false, make_io_error(IoErrorCode::cancelled, "read_exact_at stopped before completion")}}; } const auto chunk = co_await file.read_at(offset + total, buf.subspan(total), options); if (!chunk.has_value()) { co_return std::unexpected{ComposedTransferError{total, false, chunk.error()}}; } total += chunk->bytes; if (total < buf.size() && chunk->eof) { co_return std::unexpected{ComposedTransferError{ total, true, make_io_error(IoErrorCode::eof, "Unexpected EOF before read_exact_at finished")}}; } } co_return total; } [[nodiscard]] inline Coro::Task> write_all_at(File& file, std::uint64_t offset, std::span buf, Async::OperationOptions options = {}) { std::size_t total = 0; while (total < buf.size()) { if (options.stop.stop_requested()) { co_return std::unexpected{ComposedTransferError{ total, false, make_io_error(IoErrorCode::cancelled, "write_all_at stopped before completion")}}; } const auto chunk = co_await file.write_at(offset + total, buf.subspan(total), options); if (!chunk.has_value()) { co_return std::unexpected{ComposedTransferError{total, false, chunk.error()}}; } if (chunk->bytes == 0) { // A zero-byte pwrite with bytes remaining makes no progress; report // it instead of looping. co_return std::unexpected{ComposedTransferError{ total, false, make_io_error(IoErrorCode::os_error, "pwrite made no progress in write_all_at")}}; } total += chunk->bytes; } co_return total; } } // namespace Void::IO