URING++
Loading...
Searching...
No Matches
file.h
1#pragma once
2
3#include <fcntl.h>
4#include <utility>
5
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"
12
13#include "uringpp/detail/noncopyable.h"
14
15namespace uringpp {
16
21class file : public noncopyable {
22protected:
23 std::shared_ptr<event_loop> loop_;
24 int fd_;
25
26public:
32 int fd() const { return fd_; }
33
43 static task<file> open(std::shared_ptr<event_loop> loop, char const *path,
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");
47 co_return file(loop, fd);
48 }
49
60 static task<file> openat(std::shared_ptr<event_loop> loop, dir const &dir,
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");
64 co_return file(loop, fd);
65 }
66
76 static task<file> openat2(std::shared_ptr<event_loop> loop, dir const &dir,
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");
80 co_return file(loop, fd);
81 }
82
88 file(file &&other) noexcept
89 : loop_(std::move(other.loop_)), fd_(std::exchange(other.fd_, -1)) {}
90
97 file(std::shared_ptr<event_loop> loop, int fd) : loop_(loop), fd_(fd) {}
98
105 if (fd_ > 0) {
106 co_await loop_->close(fd_);
107 fd_ = -1;
108 }
109 }
110
116 if (fd_ > 0) {
117 loop_->close_detach(fd_);
118 }
119 }
120
129 sqe_awaitable read(void *buf, size_t count, off_t offset = 0) {
130 return loop_->read(fd_, buf, count, offset);
131 }
132
141 sqe_awaitable write(void const *buf, size_t count, off_t offset = 0) {
142 return loop_->write(fd_, buf, count, offset);
143 }
144
153 sqe_awaitable readv(struct iovec const *iov, int iovcnt, off_t offset = 0) {
154 return loop_->readv(fd_, iov, iovcnt, offset);
155 }
156
165 sqe_awaitable writev(struct iovec const *iov, int iovcnt, off_t offset = 0) {
166 return loop_->writev(fd_, iov, iovcnt, offset);
167 }
168
178 sqe_awaitable read_fixed(void *buf, size_t count, off_t offset,
179 int buf_index) {
180 return loop_->read_fixed(fd_, buf, count, offset, buf_index);
181 }
182
192 sqe_awaitable write_fixed(void const *buf, size_t count, off_t offset,
193 int buf_index) {
194 return loop_->write_fixed(fd_, buf, count, offset, buf_index);
195 }
196
203 sqe_awaitable fsync(int flags) { return loop_->fsync(fd_, flags); }
204
213 sqe_awaitable sync_file_range(off_t offset, off_t nbytes,
214 unsigned sync_range_flags) {
215 return loop_->sync_file_range(fd_, offset, nbytes, sync_range_flags);
216 }
217
226 sqe_awaitable tee(file const &out, size_t count, unsigned int flags) {
227 return loop_->tee(fd_, out.fd(), count, flags);
228 }
229
238 sqe_awaitable tee(socket const &out, size_t count, unsigned int flags) {
239 return loop_->tee(fd_, out.fd(), count, flags);
240 }
241
251 sqe_awaitable splice_to(loff_t off_in, size_t nbytes, pipe const &out,
252 unsigned flags) {
253 return loop_->splice(fd_, off_in, out.writable_fd(), 0, nbytes, flags);
254 }
255
265 sqe_awaitable splice_from(pipe const &in, loff_t off_out, size_t nbytes,
266 unsigned flags) {
267 return loop_->splice(in.readable_fd(), 0, fd_, off_out, nbytes, flags);
268 }
269};
270
271} // namespace uringpp
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
Definition: socket.h:25
int fd() const
Get the file descriptor of the socket.
Definition: socket.h:36
Definition: awaitable.h:10
Definition: task.h:53