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
376 template<details::UVFsType e>
377 static void fsGenericCallback(uv_fs_t *req) {
379 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
383 template<details::UVFsType e>
384 static void fsResultCallback(uv_fs_t *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) {
393 if(req->result < 0) { ptr->publish(
ErrorEvent{req->result}); }
397 static void fsStatfsCallback(uv_fs_t *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;
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);
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;
825 void mkdir(
const std::string &path,
int mode);
854 std::pair<bool, const char *>
mkdtempSync(
const std::string &tpl);
886 std::pair<bool, std::pair<std::string, std::size_t>>
mkstempSync(
const std::string &tpl);
900 void lutime(
const std::string &path, Time atime, Time mtime);
911 bool lutimeSync(
const std::string &path, Time atime, Time mtime);
921 void rmdir(
const std::string &path);
939 void scandir(
const std::string &path,
int flags);
951 std::pair<bool, std::size_t>
scandirSync(
const std::string &path,
int flags);
982 std::pair<bool, std::pair<EntryType, const char *>>
scandirNext();
992 void stat(
const std::string &path);
1003 std::pair<bool, Stat>
statSync(
const std::string &path);
1062 void rename(
const std::string &old,
const std::string &path);
1070 bool renameSync(
const std::string &old,
const std::string &path);
1133 void access(
const std::string &path,
int mode);
1152 void chmod(
const std::string &path,
int mode);
1174 void utime(
const std::string &path, Time atime, Time mtime);
1185 bool utimeSync(
const std::string &path, Time atime, Time mtime);
1196 void link(
const std::string &old,
const std::string &path);
1204 bool linkSync(
const std::string &old,
const std::string &path);
1263 std::pair<bool, std::pair<const char *, std::size_t>>
readlinkSync(
const std::string &path);
1423 uv_dirent_t dirents[1];
std::pair< bool, std::size_t > sendfileSync(FileHandle out, int64_t offset, std::size_t length)
Sync sendfile.
void futime(Time atime, Time mtime)
Async futime.
bool truncateSync(int64_t offset)
Sync ftruncate.
void truncate(int64_t offset)
Async ftruncate.
void datasync()
Async fdatasync.
void chmod(int mode)
Async fchmod.
bool chmodSync(int mode)
Sync fchmod.
bool futimeSync(Time atime, Time mtime)
Sync futime.
std::pair< bool, std::size_t > writeSync(std::unique_ptr< char[]> buf, unsigned int len, int64_t offset)
Sync write.
std::pair< bool, std::pair< std::unique_ptr< const char[]>, std::size_t > > readSync(int64_t offset, unsigned int len)
Sync read.
void open(const std::string &path, Flags< FileOpen > flags, int mode)
Async open.
void chown(Uid uid, Gid gid)
Async fchown.
bool syncSync()
Sync fsync.
bool closeSync()
Sync close.
std::pair< bool, Stat > statSync()
Sync fstat.
bool chownSync(Uid uid, Gid gid)
Sync fchown.
bool datasyncSync()
Sync fdatasync.
void sendfile(FileHandle out, int64_t offset, std::size_t length)
Async sendfile.
void read(int64_t offset, unsigned int len)
Async read.
void write(char *buf, unsigned int len, int64_t offset)
Async write.
void write(std::unique_ptr< char[]> buf, unsigned int len, int64_t offset)
Async write.
bool openSync(const std::string &path, Flags< FileOpen > flags, int mode)
Sync open.
Utility class to handle flags.
bool closedirSync()
Closes synchronously a directory stream.
void readdir()
Iterates asynchronously over a directory stream one entry at a time.
void access(const std::string &path, int mode)
Async access.
void link(const std::string &old, const std::string &path)
Async link.
bool chownSync(const std::string &path, Uid uid, Gid gid)
Sync chown.
void copyfile(const std::string &old, const std::string &path, Flags< CopyFile > flags=Flags< CopyFile >{})
Copies a file asynchronously from a path to a new one.
void mkstemp(const std::string &tpl)
Async mkstemp.
bool copyfileSync(const std::string &old, const std::string &path, Flags< CopyFile > flags=Flags< CopyFile >{})
Copies a file synchronously from a path to a new one.
void statfs(const std::string &path)
Async statfs.
bool rmdirSync(const std::string &path)
Sync rmdir.
void closedir()
Closes asynchronously a directory stream.
std::pair< bool, const char * > realpathSync(const std::string &path)
Sync realpath.
bool symlinkSync(const std::string &old, const std::string &path, Flags< SymLink > flags=Flags< SymLink >{})
Sync symlink.
void chmod(const std::string &path, int mode)
Async chmod.
std::pair< bool, Statfs > statfsSync(const std::string &path)
Sync statfs.
void symlink(const std::string &old, const std::string &path, Flags< SymLink > flags=Flags< SymLink >{})
Async symlink.
std::pair< bool, std::pair< EntryType, const char * > > scandirNext()
Gets entries populated with the next directory entry data.
bool lchownSync(const std::string &path, Uid uid, Gid gid)
Sync lchown.
void readlink(const std::string &path)
Async readlink.
std::pair< bool, Stat > statSync(const std::string &path)
Sync stat.
bool lutimeSync(const std::string &path, Time atime, Time mtime)
Sync lutime.
void scandir(const std::string &path, int flags)
Async scandir.
bool utimeSync(const std::string &path, Time atime, Time mtime)
Sync utime.
void lchown(const std::string &path, Uid uid, Gid gid)
Async lchown.
void unlink(const std::string &path)
Async unlink.
void mkdtemp(const std::string &tpl)
Async mktemp.
void rename(const std::string &old, const std::string &path)
Async rename.
std::pair< bool, Stat > lstatSync(const std::string &path)
Sync lstat.
std::pair< bool, std::pair< std::string, std::size_t > > mkstempSync(const std::string &tpl)
Sync mkstemp.
void utime(const std::string &path, Time atime, Time mtime)
Async utime.
bool mkdirSync(const std::string &path, int mode)
Sync mkdir.
std::pair< bool, const char * > mkdtempSync(const std::string &tpl)
Sync mktemp.
void chown(const std::string &path, Uid uid, Gid gid)
Async chown.
void stat(const std::string &path)
Async stat.
void lutime(const std::string &path, Time atime, Time mtime)
Async lutime.
void opendir(const std::string &path)
Opens a path asynchronously as a directory stream.
bool renameSync(const std::string &old, const std::string &path)
Sync rename.
bool linkSync(const std::string &old, const std::string &path)
Sync link.
std::pair< bool, std::size_t > scandirSync(const std::string &path, int flags)
Sync scandir.
void lstat(const std::string &path)
Async lstat.
bool opendirSync(const std::string &path)
Opens a path synchronously as a directory stream.
void mkdir(const std::string &path, int mode)
Async mkdir.
bool unlinkSync(const std::string &path)
Sync unlink.
std::pair< bool, std::pair< EntryType, const char * > > readdirSync()
Iterates synchronously over a directory stream one entry at a time.
std::pair< bool, std::pair< const char *, std::size_t > > readlinkSync(const std::string &path)
Sync readlink.
void rmdir(const std::string &path)
Async rmdir.
bool chmodSync(const std::string &path, int mode)
Sync chmod.
void realpath(const std::string &path)
Async realpath.
bool accessSync(const std::string &path, int mode)
Sync access.
Base class for FsReq and/or FileReq.
details::UVTypeWrapper< uv_file > FileHandle
static constexpr std::uint32_t type() noexcept
Returns a numerical identifier for a given type.
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
std::unique_ptr< const char[]> data
static OSFileDescriptor handle(FileHandle file) noexcept
Gets the OS dependent handle.
static FileHandle open(OSFileDescriptor descriptor) noexcept
Gets the file descriptor.