1 #ifndef UVW_FS_INCLUDE_H
2 #define UVW_FS_INCLUDE_H
10 #include "request.hpp"
21 enum class UVFsType: std::underlying_type_t<uv_fs_type> {
22 UNKNOWN = UV_FS_UNKNOWN,
23 CUSTOM = UV_FS_CUSTOM,
28 SENDFILE = UV_FS_SENDFILE,
32 FTRUNCATE = UV_FS_FTRUNCATE,
34 FUTIME = UV_FS_FUTIME,
35 ACCESS = UV_FS_ACCESS,
37 FCHMOD = UV_FS_FCHMOD,
39 FDATASYNC = UV_FS_FDATASYNC,
40 UNLINK = UV_FS_UNLINK,
43 MKDTEMP = UV_FS_MKDTEMP,
44 RENAME = UV_FS_RENAME,
45 SCANDIR = UV_FS_SCANDIR,
47 SYMLINK = UV_FS_SYMLINK,
48 READLINK = UV_FS_READLINK,
50 FCHOWN = UV_FS_FCHOWN,
51 REALPATH = UV_FS_REALPATH,
52 COPYFILE = UV_FS_COPYFILE,
53 LCHOWN = UV_FS_LCHOWN,
54 OPENDIR = UV_FS_OPENDIR,
55 READDIR = UV_FS_READDIR,
56 CLOSEDIR = UV_FS_CLOSEDIR,
57 STATFS = UV_FS_STATFS,
58 MKSTEMP = UV_FS_MKSTEMP,
63 enum class UVDirentTypeT: std::underlying_type_t<uv_dirent_type_t> {
64 UNKNOWN = UV_DIRENT_UNKNOWN,
65 FILE = UV_DIRENT_FILE,
67 LINK = UV_DIRENT_LINK,
68 FIFO = UV_DIRENT_FIFO,
69 SOCKET = UV_DIRENT_SOCKET,
70 CHAR = UV_DIRENT_CHAR,
71 BLOCK = UV_DIRENT_BLOCK
75 enum class UVFileOpenFlags: int {
76 APPEND = UV_FS_O_APPEND,
77 CREAT = UV_FS_O_CREAT,
78 DIRECT = UV_FS_O_DIRECT,
79 DIRECTORY = UV_FS_O_DIRECTORY,
80 DSYNC = UV_FS_O_DSYNC,
82 EXLOCK = UV_FS_O_EXLOCK,
83 FILEMAP = UV_FS_O_FILEMAP,
84 NOATIME = UV_FS_O_NOATIME,
85 NOCTTY = UV_FS_O_NOCTTY,
86 NOFOLLOW = UV_FS_O_NOFOLLOW,
87 NONBLOCK = UV_FS_O_NONBLOCK,
88 RANDOM = UV_FS_O_RANDOM,
89 RDONLY = UV_FS_O_RDONLY,
91 SEQUENTIAL = UV_FS_O_SEQUENTIAL,
92 SHORT_LIVED = UV_FS_O_SHORT_LIVED,
93 SYMLINK = UV_FS_O_SYMLINK,
95 TEMPORARY = UV_FS_O_TEMPORARY,
96 TRUNC = UV_FS_O_TRUNC,
97 WRONLY = UV_FS_O_WRONLY
101 enum class UVCopyFileFlags: int {
102 EXCL = UV_FS_COPYFILE_EXCL,
103 FICLONE = UV_FS_COPYFILE_FICLONE,
104 FICLONE_FORCE = UV_FS_COPYFILE_FICLONE_FORCE
108 enum class UVSymLinkFlags: int {
109 DIR = UV_FS_SYMLINK_DIR,
110 JUNCTION = UV_FS_SYMLINK_JUNCTION
168 template<details::UVFsType e>
170 FsEvent(
const char *pathname) noexcept:
path{pathname} {}
184 FsEvent(
const char *pathname, std::unique_ptr<
const char[]> buf, std::size_t sz) noexcept
185 :
path{pathname}, data{std::move(buf)}, size{sz}
189 std::unique_ptr<const char[]>
data;
202 FsEvent(
const char *pathname, std::size_t sz) noexcept
203 :
path{pathname}, size{sz}
219 FsEvent(
const char *pathname, std::size_t sz) noexcept
220 :
path{pathname}, size{sz}
237 :
path{pathname}, stat{std::move(curr)}
254 :
path{pathname}, stat{std::move(curr)}
271 :
path{pathname}, stat{std::move(curr)}
288 :
path{pathname}, statfs{std::move(curr)}
304 FsEvent(
const char *pathname, std::size_t desc) noexcept
305 :
path{pathname}, descriptor{desc}
321 FsEvent(
const char *pathname, std::size_t sz) noexcept
322 :
path{pathname}, size{sz}
338 explicit FsEvent(
const char *pathname,
const char *buf, std::size_t sz) noexcept
339 :
path{pathname}, data{buf}, size{sz}
356 using EntryType = details::UVDirentTypeT;
358 FsEvent(
const char *name, EntryType type,
bool eos) noexcept
359 : name{name}, type{type}, eos{eos}
376 template<details::UVFsType e>
377 static void fsGenericCallback(uv_fs_t *req) {
378 auto ptr = Request<T, uv_fs_t>::reserve(req);
379 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
383 template<details::UVFsType e>
384 static void fsResultCallback(uv_fs_t *req) {
385 auto ptr = Request<T, uv_fs_t>::reserve(req);
386 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
387 else { ptr->publish(
FsEvent<e>{req->
path,
static_cast<std::size_t
>(req->result)}); }
390 template<details::UVFsType e>
391 static void fsStatCallback(uv_fs_t *req) {
392 auto ptr = Request<T, uv_fs_t>::reserve(req);
393 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
397 static void fsStatfsCallback(uv_fs_t *req) {
398 auto ptr = Request<T, uv_fs_t>::reserve(req);
399 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
403 template<
typename... Args>
404 void cleanupAndInvoke(Args&&... args) {
405 uv_fs_req_cleanup(this->get());
406 this->invoke(std::forward<Args>(args)...);
409 template<
typename F,
typename... Args>
410 void cleanupAndInvokeSync(F &&f, Args&&... args) {
411 uv_fs_req_cleanup(this->get());
412 std::forward<F>(f)(std::forward<Args>(args)...,
nullptr);
416 using Time = std::chrono::duration<double>;
417 using Type = details::UVFsType;
418 using EntryType = details::UVDirentTypeT;
420 using Request<T, uv_fs_t>::Request;
437 static constexpr uv_file BAD_FD = -1;
439 static void fsOpenCallback(uv_fs_t *req);
440 static void fsCloseCallback(uv_fs_t *req);
441 static void fsReadCallback(uv_fs_t *req);
444 using FileOpen = details::UVFileOpenFlags;
446 using FsRequest::FsRequest;
553 void read(int64_t offset,
unsigned int len);
567 std::pair<bool, std::pair<std::unique_ptr<const char[]>, std::size_t>>
readSync(int64_t offset,
unsigned int len);
582 void write(std::unique_ptr<
char[]> buf,
unsigned int len, int64_t offset);
597 void write(
char *buf,
unsigned int len, int64_t offset);
610 std::pair<bool, std::size_t>
writeSync(std::unique_ptr<
char[]> buf,
unsigned int len, int64_t offset);
707 void chmod(
int mode);
727 void futime(Time atime, Time mtime);
769 std::unique_ptr<char[]> current{
nullptr};
771 uv_file file{BAD_FD};
788 static void fsReadlinkCallback(uv_fs_t *req);
789 static void fsReaddirCallback(uv_fs_t *req);
792 using CopyFile = details::UVCopyFileFlags;
793 using SymLink = details::UVSymLinkFlags;
795 using FsRequest::FsRequest;
807 void unlink(std::string path);
825 void mkdir(std::string path,
int mode);
833 bool mkdirSync(std::string path,
int mode);
854 std::pair<bool, const char *>
mkdtempSync(std::string tpl);
886 std::pair<bool, std::pair<std::string, std::size_t>>
mkstempSync(std::string tpl);
900 void lutime(std::string path, Time atime, Time mtime);
911 bool lutimeSync(std::string path, Time atime, Time mtime);
921 void rmdir(std::string path);
939 void scandir(std::string path,
int flags);
951 std::pair<bool, std::size_t>
scandirSync(std::string path,
int flags);
982 std::pair<bool, std::pair<EntryType, const char *>>
scandirNext();
992 void stat(std::string path);
1003 std::pair<bool, Stat>
statSync(std::string path);
1013 void lstat(std::string path);
1024 std::pair<bool, Stat>
lstatSync(std::string path);
1037 void statfs(std::string path);
1051 std::pair<bool, Statfs>
statfsSync(std::string path);
1062 void rename(std::string old, std::string path);
1070 bool renameSync(std::string old, std::string path);
1133 void access(std::string path,
int mode);
1152 void chmod(std::string path,
int mode);
1160 bool chmodSync(std::string path,
int mode);
1174 void utime(std::string path, Time atime, Time mtime);
1185 bool utimeSync(std::string path, Time atime, Time mtime);
1196 void link(std::string old, std::string path);
1204 bool linkSync(std::string old, std::string path);
1263 std::pair<bool, std::pair<const char *, std::size_t>>
readlinkSync(std::string path);
1284 std::pair<bool, const char *>
realpathSync(std::string path);
1340 void opendir(std::string path);
1420 std::pair<bool, std::pair<EntryType, const char *>>
readdirSync();
1423 uv_dirent_t dirents[1];
1462 #endif // UVW_FS_INCLUDE_H