URING++
Loading...
Searching...
No Matches
dir.h
1#pragma once
2
3#include <fcntl.h>
4#include <memory>
5
6#include "uringpp/awaitable.h"
7#include "uringpp/event_loop.h"
8
9#include "uringpp/detail/noncopyable.h"
10
11namespace uringpp {
12
17class dir : public noncopyable {
18protected:
19 std::shared_ptr<event_loop> loop_;
20 int fd_;
21
22public:
28 int fd() const { return fd_; }
29
36 dir(std::shared_ptr<event_loop> loop, int fd) : loop_(loop), fd_(fd) {}
37
43 dir(dir &&other) noexcept
44 : loop_(std::move(other.loop_)), fd_(std::exchange(other.fd_, -1)) {}
45
50 ~dir() {
51 if (fd_ > 0) {
52 loop_->close_detach(fd_);
53 }
54 }
55
62 if (fd_ > 0) {
63 co_await loop_->close(fd_);
64 fd_ = -1;
65 }
66 }
67
77 static task<dir> open(std::shared_ptr<event_loop> loop, char const *path,
78 int flags, mode_t mode) {
79 int fd = co_await loop->openat(AT_FDCWD, path, flags, mode);
80 check_nerrno(fd, "failed to open dir");
81 co_return dir(loop, fd);
82 }
83
94 static task<dir> openat(std::shared_ptr<event_loop> loop, dir const &dir,
95 char const *path, int flags, mode_t mode) {
96 int fd = co_await loop->openat(dir.fd(), path, flags, mode);
97 check_nerrno(fd, "failed to open dir");
98 co_return uringpp::dir(loop, fd);
99 }
100
110 static task<dir> openat2(std::shared_ptr<event_loop> loop, dir const &dir,
111 char const *path, struct open_how *how) {
112 int fd = co_await loop->openat2(dir.fd(), path, how);
113 check_nerrno(fd, "failed to open dir");
114 co_return uringpp::dir(loop, fd);
115 }
116
126 sqe_awaitable statx(const char *path, int flags, unsigned mask,
127 struct statx *statxbuf) {
128 return loop_->statx(fd_, path, flags, mask, statxbuf);
129 }
130
138 sqe_awaitable mkdir(const char *path, mode_t mode) {
139 return loop_->mkdirat(fd_, path, mode);
140 }
141
149 sqe_awaitable symlink(const char *oldpath, const char *newpath) {
150 return loop_->symlinkat(oldpath, fd_, newpath);
151 }
152
162 sqe_awaitable symlink(const char *oldpath, dir const &newdir,
163 const char *newpath) {
164 return loop_->symlinkat(oldpath, newdir.fd(), newpath);
165 }
166
175 sqe_awaitable link(const char *oldpath, const char *newpath, int flags) {
176 return loop_->linkat(fd_, oldpath, fd_, newpath, flags);
177 }
178
188 sqe_awaitable link(const char *oldpath, dir const &newdir,
189 const char *newpath, int flags) {
190 return loop_->linkat(fd_, oldpath, newdir.fd(), newpath, flags);
191 }
192
201 sqe_awaitable rename(const char *oldpath, const char *newpath, int flags) {
202 return loop_->renameat(fd_, oldpath, fd_, newpath, flags);
203 }
204
214 sqe_awaitable rename(const char *oldpath, dir const &newdir,
215 const char *newpath, int flags) {
216 return loop_->renameat(fd_, oldpath, newdir.fd(), newpath, flags);
217 }
218
226 sqe_awaitable unlink(const char *path, int flags) {
227 return loop_->unlinkat(fd_, path, flags);
228 }
229};
230
231} // namespace uringpp
An opened directory.
Definition: dir.h:17
sqe_awaitable symlink(const char *oldpath, dir const &newdir, const char *newpath)
Symbolic link a file/directory in the directory to another directory.
Definition: dir.h:162
sqe_awaitable unlink(const char *path, int flags)
Unlink a file/directory in the directory.
Definition: dir.h:226
dir(dir &&other) noexcept
Move construct a new dir object.
Definition: dir.h:43
sqe_awaitable rename(const char *oldpath, const char *newpath, int flags)
Rename a file/directory in the directory.
Definition: dir.h:201
task< void > close()
Close the directory.
Definition: dir.h:61
static task< dir > open(std::shared_ptr< event_loop > loop, char const *path, int flags, mode_t mode)
Open a directory in the current working directory.
Definition: dir.h:77
sqe_awaitable link(const char *oldpath, dir const &newdir, const char *newpath, int flags)
Link a file/directory in the directory to another directory.
Definition: dir.h:188
static task< dir > openat2(std::shared_ptr< event_loop > loop, dir const &dir, char const *path, struct open_how *how)
Open a directory in the given directory.
Definition: dir.h:110
dir(std::shared_ptr< event_loop > loop, int fd)
Construct a new dir object from a file descriptor.
Definition: dir.h:36
static task< dir > openat(std::shared_ptr< event_loop > loop, dir const &dir, char const *path, int flags, mode_t mode)
Open a directory in the given directory.
Definition: dir.h:94
int fd() const
Get the file descriptor of the directory.
Definition: dir.h:28
sqe_awaitable rename(const char *oldpath, dir const &newdir, const char *newpath, int flags)
Rename a file/directory in the directory to another directory.
Definition: dir.h:214
sqe_awaitable mkdir(const char *path, mode_t mode)
Make a directory.
Definition: dir.h:138
sqe_awaitable statx(const char *path, int flags, unsigned mask, struct statx *statxbuf)
Stat a file/directory in the directory.
Definition: dir.h:126
~dir()
Destroy the dir object. If the directory is open, it will be closed.
Definition: dir.h:50
sqe_awaitable link(const char *oldpath, const char *newpath, int flags)
Link a file/directory in the directory.
Definition: dir.h:175
sqe_awaitable symlink(const char *oldpath, const char *newpath)
Symbolic link a file/directory in the directory.
Definition: dir.h:149
Definition: noncopyable.h:5
Definition: awaitable.h:10
Definition: task.h:53