6#include "uringpp/awaitable.h"
7#include "uringpp/dir.h"
8#include "uringpp/error.h"
9#include "uringpp/event_loop.h"
10#include "uringpp/pipe.h"
11#include "uringpp/socket.h"
13#include "uringpp/detail/noncopyable.h"
23 std::shared_ptr<event_loop> loop_;
32 int fd()
const {
return fd_; }
44 int flags, mode_t mode) {
45 int fd =
co_await loop->openat(AT_FDCWD, path, flags, mode);
46 check_nerrno(
fd,
"failed to open file");
61 char const *path,
int flags, mode_t mode) {
62 int fd =
co_await loop->openat(
dir.
fd(), path, flags, mode);
63 check_nerrno(
fd,
"failed to open file");
77 char const *path,
struct open_how *how) {
78 int fd =
co_await loop->openat2(
dir.
fd(), path, how);
79 check_nerrno(
fd,
"failed to open file");
89 : loop_(std::move(other.loop_)), fd_(std::exchange(other.fd_, -1)) {}
97 file(std::shared_ptr<event_loop> loop,
int fd) : loop_(loop), fd_(
fd) {}
106 co_await loop_->close(fd_);
117 loop_->close_detach(fd_);
130 return loop_->read(fd_, buf, count, offset);
142 return loop_->write(fd_, buf, count, offset);
154 return loop_->readv(fd_, iov, iovcnt, offset);
166 return loop_->writev(fd_, iov, iovcnt, offset);
180 return loop_->read_fixed(fd_, buf, count, offset, buf_index);
194 return loop_->write_fixed(fd_, buf, count, offset, buf_index);
214 unsigned sync_range_flags) {
215 return loop_->sync_file_range(fd_, offset, nbytes, sync_range_flags);
227 return loop_->tee(fd_, out.
fd(), count, flags);
239 return loop_->tee(fd_, out.
fd(), count, flags);
253 return loop_->splice(fd_, off_in, out.
writable_fd(), 0, nbytes, flags);
267 return loop_->splice(in.
readable_fd(), 0, fd_, off_out, nbytes, flags);
An opened directory.
Definition: dir.h:17
int fd() const
Get the file descriptor of the directory.
Definition: dir.h:28
An opened file.
Definition: file.h:21
sqe_awaitable splice_to(loff_t off_in, size_t nbytes, pipe const &out, unsigned flags)
Splice data from the file to a pipe.
Definition: file.h:251
file(std::shared_ptr< event_loop > loop, int fd)
Construct a new file object using the given event loop and file descriptor. The fd will be closed whe...
Definition: file.h:97
sqe_awaitable write_fixed(void const *buf, size_t count, off_t offset, int buf_index)
Write data from a preregistered buffer to the file.
Definition: file.h:192
sqe_awaitable readv(struct iovec const *iov, int iovcnt, off_t offset=0)
Vectorized read from the file.
Definition: file.h:153
sqe_awaitable write(void const *buf, size_t count, off_t offset=0)
Write data to the file.
Definition: file.h:141
static task< file > openat(std::shared_ptr< event_loop > loop, dir const &dir, char const *path, int flags, mode_t mode)
Open a file in the given directory.
Definition: file.h:60
sqe_awaitable read_fixed(void *buf, size_t count, off_t offset, int buf_index)
Read data to a preregistered buffer from the file.
Definition: file.h:178
sqe_awaitable tee(socket const &out, size_t count, unsigned int flags)
Tee data from the file to a socket.
Definition: file.h:238
~file()
Destroy the file object. If the fd is still open, it will be closed.
Definition: file.h:115
sqe_awaitable read(void *buf, size_t count, off_t offset=0)
Read data from the file.
Definition: file.h:129
int fd() const
Get the file descriptor of the file.
Definition: file.h:32
static task< file > openat2(std::shared_ptr< event_loop > loop, dir const &dir, char const *path, struct open_how *how)
Open a file in the given directory.
Definition: file.h:76
sqe_awaitable splice_from(pipe const &in, loff_t off_out, size_t nbytes, unsigned flags)
Splice data from a pipe to the file.
Definition: file.h:265
sqe_awaitable writev(struct iovec const *iov, int iovcnt, off_t offset=0)
Vectorized write to the file.
Definition: file.h:165
sqe_awaitable fsync(int flags)
Asynchronously flush the file.
Definition: file.h:203
static task< file > open(std::shared_ptr< event_loop > loop, char const *path, int flags, mode_t mode)
Opens a file in the current working directory.
Definition: file.h:43
sqe_awaitable sync_file_range(off_t offset, off_t nbytes, unsigned sync_range_flags)
Asynchronously flush a range of the file.
Definition: file.h:213
sqe_awaitable tee(file const &out, size_t count, unsigned int flags)
Tee data from the file to another file.
Definition: file.h:226
file(file &&other) noexcept
Move construct a new file object.
Definition: file.h:88
task< void > close()
Close the file.
Definition: file.h:104
Definition: noncopyable.h:5
A pipe.
Definition: pipe.h:15
int writable_fd() const
Get the write end of the pipe.
Definition: pipe.h:56
int readable_fd() const
Get the read end of the pipe.
Definition: pipe.h:46
int fd() const
Get the file descriptor of the socket.
Definition: socket.h:36
Definition: awaitable.h:10