uvw 3.1.0
Loading...
Searching...
No Matches
fs.h
1#ifndef UVW_FS_INCLUDE_H
2#define UVW_FS_INCLUDE_H
3
4#include <chrono>
5#include <memory>
6#include <string>
7#include <utility>
8#include <uv.h>
9#include "config.h"
10#include "enum.hpp"
11#include "loop.h"
12#include "request.hpp"
13#include "util.h"
14
15namespace uvw {
16
17namespace details {
18
19enum class uvw_fs_type : std::underlying_type_t<uv_fs_type> {
20 UNKNOWN = UV_FS_UNKNOWN,
21 CUSTOM = UV_FS_CUSTOM,
22 OPEN = UV_FS_OPEN,
23 CLOSE = UV_FS_CLOSE,
24 READ = UV_FS_READ,
25 WRITE = UV_FS_WRITE,
26 SENDFILE = UV_FS_SENDFILE,
27 STAT = UV_FS_STAT,
28 LSTAT = UV_FS_LSTAT,
29 FSTAT = UV_FS_FSTAT,
30 FTRUNCATE = UV_FS_FTRUNCATE,
31 UTIME = UV_FS_UTIME,
32 FUTIME = UV_FS_FUTIME,
33 ACCESS = UV_FS_ACCESS,
34 CHMOD = UV_FS_CHMOD,
35 FCHMOD = UV_FS_FCHMOD,
36 FSYNC = UV_FS_FSYNC,
37 FDATASYNC = UV_FS_FDATASYNC,
38 UNLINK = UV_FS_UNLINK,
39 RMDIR = UV_FS_RMDIR,
40 MKDIR = UV_FS_MKDIR,
41 MKDTEMP = UV_FS_MKDTEMP,
42 RENAME = UV_FS_RENAME,
43 SCANDIR = UV_FS_SCANDIR,
44 LINK = UV_FS_LINK,
45 SYMLINK = UV_FS_SYMLINK,
46 READLINK = UV_FS_READLINK,
47 CHOWN = UV_FS_CHOWN,
48 FCHOWN = UV_FS_FCHOWN,
49 REALPATH = UV_FS_REALPATH,
50 COPYFILE = UV_FS_COPYFILE,
51 LCHOWN = UV_FS_LCHOWN,
52 OPENDIR = UV_FS_OPENDIR,
53 READDIR = UV_FS_READDIR,
54 CLOSEDIR = UV_FS_CLOSEDIR,
55 STATFS = UV_FS_STATFS,
56 MKSTEMP = UV_FS_MKSTEMP,
57 LUTIME = UV_FS_LUTIME
58};
59
60enum class uvw_dirent_type_t : std::underlying_type_t<uv_dirent_type_t> {
61 UNKNOWN = UV_DIRENT_UNKNOWN,
62 FILE = UV_DIRENT_FILE,
63 DIR = UV_DIRENT_DIR,
64 LINK = UV_DIRENT_LINK,
65 FIFO = UV_DIRENT_FIFO,
66 SOCKET = UV_DIRENT_SOCKET,
67 CHAR = UV_DIRENT_CHAR,
68 BLOCK = UV_DIRENT_BLOCK
69};
70
71enum class uvw_file_open_flags : int {
72 APPEND = UV_FS_O_APPEND,
73 CREAT = UV_FS_O_CREAT,
74 DIRECT = UV_FS_O_DIRECT,
75 DIRECTORY = UV_FS_O_DIRECTORY,
76 DSYNC = UV_FS_O_DSYNC,
77 EXCL = UV_FS_O_EXCL,
78 EXLOCK = UV_FS_O_EXLOCK,
79 FILEMAP = UV_FS_O_FILEMAP,
80 NOATIME = UV_FS_O_NOATIME,
81 NOCTTY = UV_FS_O_NOCTTY,
82 NOFOLLOW = UV_FS_O_NOFOLLOW,
83 NONBLOCK = UV_FS_O_NONBLOCK,
84 RANDOM = UV_FS_O_RANDOM,
85 RDONLY = UV_FS_O_RDONLY,
86 RDWR = UV_FS_O_RDWR,
87 SEQUENTIAL = UV_FS_O_SEQUENTIAL,
88 SHORT_LIVED = UV_FS_O_SHORT_LIVED,
89 SYMLINK = UV_FS_O_SYMLINK,
90 SYNC = UV_FS_O_SYNC,
91 TEMPORARY = UV_FS_O_TEMPORARY,
92 TRUNC = UV_FS_O_TRUNC,
93 WRONLY = UV_FS_O_WRONLY,
94 _UVW_ENUM = 0
95};
96
97enum class uvw_copy_file_flags : int {
98 EXCL = UV_FS_COPYFILE_EXCL,
99 FICLONE = UV_FS_COPYFILE_FICLONE,
100 FICLONE_FORCE = UV_FS_COPYFILE_FICLONE_FORCE,
101 _UVW_ENUM = 0
102};
103
104enum class uvw_symlink_flags : int {
105 DIR = UV_FS_SYMLINK_DIR,
106 JUNCTION = UV_FS_SYMLINK_JUNCTION,
107 _UVW_ENUM = 0
108};
109
110} // namespace details
111
160struct fs_event {
161 using fs_type = details::uvw_fs_type;
162 using entry_type = details::uvw_dirent_type_t;
163
164 fs_event(const uv_fs_t &req, std::unique_ptr<const char[]> data)
165 : fs_event{req} {
166 read.data = std::move(data);
167 }
168
169 fs_event(const uv_fs_t &req)
170 : type{req.fs_type},
171 path{req.path},
172 result{static_cast<std::size_t>(req.result)} {
173 switch(type) {
174 case fs_type::STAT:
175 case fs_type::LSTAT:
176 case fs_type::FSTAT:
177 stat = *static_cast<file_info *>(req.ptr);
178 break;
179 case fs_type::READLINK:
180 readlink.data = static_cast<char *>(req.ptr);
181 break;
182 case fs_type::READDIR:
183 dirent.name = static_cast<uv_dir_t *>(req.ptr)->dirents[0].name;
184 dirent.type = static_cast<entry_type>(static_cast<uv_dir_t *>(req.ptr)->dirents[0].type);
185 dirent.eos = !req.result;
186 break;
187 case fs_type::STATFS:
188 statfs = *static_cast<fs_info *>(req.ptr);
189 break;
190 default:
191 // nothing to do here
192 break;
193 }
194 }
195
196 fs_type type;
197 const char *path;
198 std::size_t result;
200 struct {
201 std::unique_ptr<const char[]> data;
202 } read;
203
204 struct {
205 const char *data;
206 } readlink;
207
211 struct {
212 const char *name;
213 entry_type type;
214 bool eos;
215 } dirent;
216};
217
223template<typename T>
224class fs_request: public request<T, uv_fs_t, fs_event> {
225protected:
226 static void fs_request_callback(uv_fs_t *req) {
227 if(auto ptr = request<T, uv_fs_t, fs_event>::reserve(req); req->result < 0) {
228 ptr->publish(error_event{req->result});
229 } else {
230 ptr->publish(fs_event{*req});
231 }
232 }
233
234public:
235 using time = std::chrono::duration<double>;
236 using fs_type = details::uvw_fs_type;
237 using entry_type = details::uvw_dirent_type_t;
238
239 using request<T, uv_fs_t, fs_event>::request;
240};
241
254class file_req final: public fs_request<file_req> {
255 static constexpr uv_file BAD_FD = -1;
256
257 static void fs_open_callback(uv_fs_t *req);
258 static void fs_close_callback(uv_fs_t *req);
259 static void fs_read_callback(uv_fs_t *req);
260
261public:
262 using file_open_flags = details::uvw_file_open_flags;
263
264 using fs_request::fs_request;
265
266 ~file_req() noexcept;
267
273 void close();
274
280
319 void open(const std::string &path, file_open_flags flags, int mode);
320
358 bool open_sync(const std::string &path, file_open_flags flags, int mode);
359
368 void read(int64_t offset, unsigned int len);
369
382 std::pair<bool, std::pair<std::unique_ptr<const char[]>, std::size_t>> read_sync(int64_t offset, unsigned int len);
383
396 void write(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset);
397
410 void write(char *buf, unsigned int len, int64_t offset);
411
423 std::pair<bool, std::size_t> write_sync(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset);
424
430 void stat();
431
439 std::pair<bool, file_info> stat_sync();
440
446 void sync();
447
452 bool sync_sync();
453
459 void datasync();
460
466
474 void truncate(int64_t offset);
475
481 bool truncate_sync(int64_t offset);
482
492 void sendfile(file_handle out, int64_t offset, std::size_t length);
493
505 std::pair<bool, std::size_t> sendfile_sync(file_handle out, int64_t offset, std::size_t length);
506
514 void chmod(int mode);
515
521 bool chmod_sync(int mode);
522
533 void futime(time atime, time mtime);
534
543 bool futime_sync(time atime, time mtime);
544
553 void chown(uid_type uid, gid_type gid);
554
562
571 operator file_handle() const noexcept;
572
573private:
574 std::unique_ptr<char[]> current{nullptr};
575 uv_buf_t buffer{};
576 uv_file file{BAD_FD};
577};
578
591class fs_req final: public fs_request<fs_req> {
592public:
593 using copy_file_flags = details::uvw_copy_file_flags;
594 using symlink_flags = details::uvw_symlink_flags;
595
596 using fs_request::fs_request;
597
598 ~fs_req() noexcept;
599
607 void unlink(const std::string &path);
608
614 bool unlink_sync(const std::string &path);
615
624 void mkdir(const std::string &path, int mode);
625
632 bool mkdir_sync(const std::string &path, int mode);
633
641 void mkdtemp(const std::string &tpl);
642
652 std::pair<bool, const char *> mkdtemp_sync(const std::string &tpl);
653
661 void mkstemp(const std::string &tpl);
662
683 std::pair<bool, std::pair<std::string, std::size_t>> mkstemp_sync(const std::string &tpl);
684
696 void lutime(const std::string &path, time atime, time mtime);
697
707 bool lutime_sync(const std::string &path, time atime, time mtime);
708
716 void rmdir(const std::string &path);
717
723 bool rmdir_sync(const std::string &path);
724
733 void scandir(const std::string &path, int flags);
734
745 std::pair<bool, std::size_t> scandir_sync(const std::string &path, int flags);
746
776 std::pair<bool, std::pair<entry_type, const char *>> scandir_next();
777
785 void stat(const std::string &path);
786
796 std::pair<bool, file_info> stat_sync(const std::string &path);
797
805 void lstat(const std::string &path);
806
816 std::pair<bool, file_info> lstat_sync(const std::string &path);
817
828 void statfs(const std::string &path);
829
842 std::pair<bool, fs_info> statfs_sync(const std::string &path);
843
852 void rename(const std::string &old, const std::string &path);
853
860 bool rename_sync(const std::string &old, const std::string &path);
861
889 void copyfile(const std::string &old, const std::string &path, copy_file_flags flags = copy_file_flags::_UVW_ENUM);
890
910 bool copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags = copy_file_flags::_UVW_ENUM);
911
920 void access(const std::string &path, int mode);
921
928 bool access_sync(const std::string &path, int mode);
929
938 void chmod(const std::string &path, int mode);
939
946 bool chmod_sync(const std::string &path, int mode);
947
959 void utime(const std::string &path, time atime, time mtime);
960
970 bool utime_sync(const std::string &path, time atime, time mtime);
971
980 void link(const std::string &old, const std::string &path);
981
988 bool link_sync(const std::string &old, const std::string &path);
989
1006 void symlink(const std::string &old, const std::string &path, symlink_flags flags = symlink_flags::_UVW_ENUM);
1007
1023 bool symlink_sync(const std::string &old, const std::string &path, symlink_flags flags = symlink_flags::_UVW_ENUM);
1024
1032 void readlink(const std::string &path);
1033
1045 std::pair<bool, std::pair<const char *, std::size_t>> readlink_sync(const std::string &path);
1046
1054 void realpath(const std::string &path);
1055
1065 std::pair<bool, const char *> realpath_sync(const std::string &path);
1066
1076 void chown(const std::string &path, uid_type uid, gid_type gid);
1077
1085 bool chown_sync(const std::string &path, uid_type uid, gid_type gid);
1086
1096 void lchown(const std::string &path, uid_type uid, gid_type gid);
1097
1105 bool lchown_sync(const std::string &path, uid_type uid, gid_type gid);
1106
1118 void opendir(const std::string &path);
1119
1130 bool opendir_sync(const std::string &path);
1131
1140 void closedir();
1141
1151
1161 void readdir();
1162
1196 std::pair<bool, std::pair<entry_type, const char *>> readdir_sync();
1197
1198private:
1199 uv_dirent_t dirents[1];
1200};
1201
1215
1226 static file_handle open(os_file_descriptor descriptor) noexcept;
1227};
1228
1229} // namespace uvw
1230
1231#ifndef UVW_AS_LIB
1232# include "fs.cpp"
1233#endif
1234
1235#endif // UVW_FS_INCLUDE_H
The file request.
Definition: fs.h:254
std::pair< bool, std::size_t > sendfile_sync(file_handle out, int64_t offset, std::size_t length)
Sync sendfile.
bool datasync_sync()
Sync fdatasync.
void chown(uid_type uid, gid_type gid)
Async fchown.
void write(std::unique_ptr< char[]> buf, unsigned int len, int64_t offset)
Async write.
bool chown_sync(uid_type uid, gid_type gid)
Sync fchown.
std::pair< bool, std::size_t > write_sync(std::unique_ptr< char[]> buf, unsigned int len, int64_t offset)
Sync write.
void close()
Async close.
std::pair< bool, file_info > stat_sync()
Sync fstat.
void sync()
Async fsync.
bool chmod_sync(int mode)
Sync fchmod.
void sendfile(file_handle out, int64_t offset, std::size_t length)
Async sendfile.
void chmod(int mode)
Async fchmod.
void datasync()
Async fdatasync.
bool close_sync()
Sync close.
bool futime_sync(time atime, time mtime)
Sync futime.
void futime(time atime, time mtime)
Async futime.
std::pair< bool, std::pair< std::unique_ptr< const char[]>, std::size_t > > read_sync(int64_t offset, unsigned int len)
Sync read.
bool open_sync(const std::string &path, file_open_flags flags, int mode)
Sync open.
void write(char *buf, unsigned int len, int64_t offset)
Async write.
void read(int64_t offset, unsigned int len)
Async read.
void open(const std::string &path, file_open_flags flags, int mode)
Async open.
void truncate(int64_t offset)
Async ftruncate.
void stat()
Async fstat.
bool sync_sync()
Sync fsync.
bool truncate_sync(int64_t offset)
Sync ftruncate.
The fs request.
Definition: fs.h:591
bool lutime_sync(const std::string &path, time atime, time mtime)
Sync lutime.
void mkdtemp(const std::string &tpl)
Async mktemp.
void opendir(const std::string &path)
Opens a path asynchronously as a directory stream.
bool rmdir_sync(const std::string &path)
Sync rmdir.
std::pair< bool, std::pair< entry_type, const char * > > readdir_sync()
Iterates synchronously over a directory stream one entry at a time.
bool mkdir_sync(const std::string &path, int mode)
Sync mkdir.
void lutime(const std::string &path, time atime, time mtime)
Async lutime.
bool copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags=copy_file_flags::_UVW_ENUM)
Copies a file synchronously from a path to a new one.
bool utime_sync(const std::string &path, time atime, time mtime)
Sync utime.
void readdir()
Iterates asynchronously over a directory stream one entry at a time.
void copyfile(const std::string &old, const std::string &path, copy_file_flags flags=copy_file_flags::_UVW_ENUM)
Copies a file asynchronously from a path to a new one.
void chown(const std::string &path, uid_type uid, gid_type gid)
Async chown.
void access(const std::string &path, int mode)
Async access.
bool opendir_sync(const std::string &path)
Opens a path synchronously as a directory stream.
bool chown_sync(const std::string &path, uid_type uid, gid_type gid)
Sync chown.
void utime(const std::string &path, time atime, time mtime)
Async utime.
void closedir()
Closes asynchronously a directory stream.
std::pair< bool, file_info > stat_sync(const std::string &path)
Sync stat.
void symlink(const std::string &old, const std::string &path, symlink_flags flags=symlink_flags::_UVW_ENUM)
Async symlink.
std::pair< bool, std::size_t > scandir_sync(const std::string &path, int flags)
Sync scandir.
void link(const std::string &old, const std::string &path)
Async link.
void chmod(const std::string &path, int mode)
Async chmod.
bool access_sync(const std::string &path, int mode)
Sync access.
void rename(const std::string &old, const std::string &path)
Async rename.
bool unlink_sync(const std::string &path)
Sync unlink.
std::pair< bool, const char * > mkdtemp_sync(const std::string &tpl)
Sync mktemp.
bool rename_sync(const std::string &old, const std::string &path)
Sync rename.
std::pair< bool, fs_info > statfs_sync(const std::string &path)
Sync statfs.
void realpath(const std::string &path)
Async realpath.
void lchown(const std::string &path, uid_type uid, gid_type gid)
Async lchown.
void unlink(const std::string &path)
Async unlink.
bool chmod_sync(const std::string &path, int mode)
Sync chmod.
void readlink(const std::string &path)
Async readlink.
void mkdir(const std::string &path, int mode)
Async mkdir.
std::pair< bool, std::pair< std::string, std::size_t > > mkstemp_sync(const std::string &tpl)
Sync mkstemp.
std::pair< bool, file_info > lstat_sync(const std::string &path)
Sync lstat.
std::pair< bool, std::pair< const char *, std::size_t > > readlink_sync(const std::string &path)
Sync readlink.
std::pair< bool, std::pair< entry_type, const char * > > scandir_next()
Gets entries populated with the next directory entry data.
bool symlink_sync(const std::string &old, const std::string &path, symlink_flags flags=symlink_flags::_UVW_ENUM)
Sync symlink.
std::pair< bool, const char * > realpath_sync(const std::string &path)
Sync realpath.
void lstat(const std::string &path)
Async lstat.
void mkstemp(const std::string &tpl)
Async mkstemp.
bool link_sync(const std::string &old, const std::string &path)
Sync link.
bool lchown_sync(const std::string &path, uid_type uid, gid_type gid)
Sync lchown.
void statfs(const std::string &path)
Async statfs.
void rmdir(const std::string &path)
Async rmdir.
void scandir(const std::string &path, int flags)
Async scandir.
void stat(const std::string &path)
Async stat.
bool closedir_sync()
Closes synchronously a directory stream.
Base class for fs/file request.
Definition: fs.h:224
Request base class.
Definition: request.hpp:19
uvw default namespace.
Definition: async.h:8
uv_uid_t uid_type
Definition: util.h:98
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
Definition: util.h:87
details::uv_type_wrapper< uv_file > file_handle
Definition: util.h:85
uv_gid_t gid_type
Definition: util.h:99
uv_stat_t file_info
Definition: util.h:96
uv_statfs_t fs_info
Definition: util.h:97
Error event.
Definition: emitter.h:23
Common fs event.
Definition: fs.h:160
std::size_t result
Definition: fs.h:198
file_info stat
Definition: fs.h:208
fs_info statfs
Definition: fs.h:209
const char * name
Definition: fs.h:212
const char * path
Definition: fs.h:197
std::unique_ptr< const char[]> data
Definition: fs.h:201
bool eos
Definition: fs.h:214
entry_type type
Definition: fs.h:213
fs_type type
Definition: fs.h:196
const char * data
Definition: fs.h:205
Helper functions.
Definition: fs.h:1203
static os_file_descriptor handle(file_handle file) noexcept
Gets the OS dependent handle.
static file_handle open(os_file_descriptor descriptor) noexcept
Gets the file descriptor.