1 #ifndef UVW_FS_INCLUDE_H
2 #define UVW_FS_INCLUDE_H
10 #include "request.hpp"
17 enum class UVFsType : std::underlying_type_t<uv_fs_type> {
18 UNKNOWN = UV_FS_UNKNOWN,
19 CUSTOM = UV_FS_CUSTOM,
24 SENDFILE = UV_FS_SENDFILE,
28 FTRUNCATE = UV_FS_FTRUNCATE,
30 FUTIME = UV_FS_FUTIME,
31 ACCESS = UV_FS_ACCESS,
33 FCHMOD = UV_FS_FCHMOD,
35 FDATASYNC = UV_FS_FDATASYNC,
36 UNLINK = UV_FS_UNLINK,
39 MKDTEMP = UV_FS_MKDTEMP,
40 RENAME = UV_FS_RENAME,
41 SCANDIR = UV_FS_SCANDIR,
43 SYMLINK = UV_FS_SYMLINK,
44 READLINK = UV_FS_READLINK,
46 FCHOWN = UV_FS_FCHOWN,
47 REALPATH = UV_FS_REALPATH,
48 COPYFILE = UV_FS_COPYFILE,
49 LCHOWN = UV_FS_LCHOWN,
50 OPENDIR = UV_FS_OPENDIR,
51 READDIR = UV_FS_READDIR,
52 CLOSEDIR = UV_FS_CLOSEDIR,
53 STATFS = UV_FS_STATFS,
54 MKSTEMP = UV_FS_MKSTEMP,
58 enum class UVDirentTypeT : std::underlying_type_t<uv_dirent_type_t> {
59 UNKNOWN = UV_DIRENT_UNKNOWN,
60 FILE = UV_DIRENT_FILE,
62 LINK = UV_DIRENT_LINK,
63 FIFO = UV_DIRENT_FIFO,
64 SOCKET = UV_DIRENT_SOCKET,
65 CHAR = UV_DIRENT_CHAR,
66 BLOCK = UV_DIRENT_BLOCK
69 enum class UVFileOpenFlags : int {
70 APPEND = UV_FS_O_APPEND,
71 CREAT = UV_FS_O_CREAT,
72 DIRECT = UV_FS_O_DIRECT,
73 DIRECTORY = UV_FS_O_DIRECTORY,
74 DSYNC = UV_FS_O_DSYNC,
76 EXLOCK = UV_FS_O_EXLOCK,
77 FILEMAP = UV_FS_O_FILEMAP,
78 NOATIME = UV_FS_O_NOATIME,
79 NOCTTY = UV_FS_O_NOCTTY,
80 NOFOLLOW = UV_FS_O_NOFOLLOW,
81 NONBLOCK = UV_FS_O_NONBLOCK,
82 RANDOM = UV_FS_O_RANDOM,
83 RDONLY = UV_FS_O_RDONLY,
85 SEQUENTIAL = UV_FS_O_SEQUENTIAL,
86 SHORT_LIVED = UV_FS_O_SHORT_LIVED,
87 SYMLINK = UV_FS_O_SYMLINK,
89 TEMPORARY = UV_FS_O_TEMPORARY,
90 TRUNC = UV_FS_O_TRUNC,
91 WRONLY = UV_FS_O_WRONLY
94 enum class UVCopyFileFlags : int {
95 EXCL = UV_FS_COPYFILE_EXCL,
96 FICLONE = UV_FS_COPYFILE_FICLONE,
97 FICLONE_FORCE = UV_FS_COPYFILE_FICLONE_FORCE
100 enum class UVSymLinkFlags : int {
101 DIR = UV_FS_SYMLINK_DIR,
102 JUNCTION = UV_FS_SYMLINK_JUNCTION
158 template<details::UVFsType e>
160 FsEvent(
const char *pathname) noexcept
174 FsEvent(
const char *pathname, std::unique_ptr<
const char[]> buf, std::size_t sz) noexcept
175 :
path{pathname}, data{std::move(buf)}, size{sz} {}
178 std::unique_ptr<const char[]>
data;
190 FsEvent(
const char *pathname, std::size_t sz) noexcept
191 :
path{pathname}, size{sz} {}
205 FsEvent(
const char *pathname, std::size_t sz) noexcept
206 :
path{pathname}, size{sz} {}
221 :
path{pathname}, stat{std::move(curr)} {}
236 :
path{pathname}, stat{std::move(curr)} {}
251 :
path{pathname}, stat{std::move(curr)} {}
266 :
path{pathname}, statfs{std::move(curr)} {}
280 FsEvent(
const char *pathname, std::size_t desc) noexcept
281 :
path{pathname}, descriptor{desc} {}
295 FsEvent(
const char *pathname, std::size_t sz) noexcept
296 :
path{pathname}, size{sz} {}
310 explicit FsEvent(
const char *pathname,
const char *buf, std::size_t sz) noexcept
311 :
path{pathname}, data{buf}, size{sz} {}
326 using EntryType = details::UVDirentTypeT;
328 FsEvent(
const char *name, EntryType
type,
bool eos) noexcept
329 : name{name},
type{
type}, eos{eos} {}
344 template<details::UVFsType e>
345 static void fsGenericCallback(uv_fs_t *req) {
353 template<details::UVFsType e>
354 static void fsResultCallback(uv_fs_t *req) {
358 ptr->publish(
FsEvent<e>{req->
path,
static_cast<std::size_t
>(req->result)});
362 template<details::UVFsType e>
363 static void fsStatCallback(uv_fs_t *req) {
371 static void fsStatfsCallback(uv_fs_t *req) {
379 template<
typename... Args>
380 void cleanupAndInvoke(Args &&...args) {
381 uv_fs_req_cleanup(this->get());
382 this->invoke(std::forward<Args>(args)...);
385 template<
typename F,
typename... Args>
386 void cleanupAndInvokeSync(F &&f, Args &&...args) {
387 uv_fs_req_cleanup(this->get());
388 std::forward<F>(f)(std::forward<Args>(args)...,
nullptr);
392 using Time = std::chrono::duration<double>;
393 using Type = details::UVFsType;
394 using EntryType = details::UVDirentTypeT;
412 static constexpr uv_file BAD_FD = -1;
414 static void fsOpenCallback(uv_fs_t *req);
415 static void fsCloseCallback(uv_fs_t *req);
416 static void fsReadCallback(uv_fs_t *req);
419 using FileOpen = details::UVFileOpenFlags;
421 using FsRequest::FsRequest;
528 void read(int64_t offset,
unsigned int len);
542 std::pair<bool, std::pair<std::unique_ptr<const char[]>, std::size_t>>
readSync(int64_t offset,
unsigned int len);
557 void write(std::unique_ptr<
char[]> buf,
unsigned int len, int64_t offset);
572 void write(
char *buf,
unsigned int len, int64_t offset);
585 std::pair<bool, std::size_t>
writeSync(std::unique_ptr<
char[]> buf,
unsigned int len, int64_t offset);
744 std::unique_ptr<
char[]> current{
nullptr};
746 uv_file file{BAD_FD};
762 static void fsReadlinkCallback(uv_fs_t *req);
763 static void fsReaddirCallback(uv_fs_t *req);
766 using CopyFile = details::UVCopyFileFlags;
767 using SymLink = details::UVSymLinkFlags;
769 using FsRequest::FsRequest;
799 void mkdir(
const std::string &path,
int mode);
828 std::pair<bool, const char *>
mkdtempSync(
const std::string &tpl);
860 std::pair<bool, std::pair<std::string, std::size_t>>
mkstempSync(
const std::string &tpl);
874 void lutime(
const std::string &path, Time atime, Time mtime);
885 bool lutimeSync(
const std::string &path, Time atime, Time mtime);
895 void rmdir(
const std::string &path);
913 void scandir(
const std::string &path,
int flags);
925 std::pair<bool, std::size_t>
scandirSync(
const std::string &path,
int flags);
956 std::pair<bool, std::pair<EntryType, const char *>>
scandirNext();
966 void stat(
const std::string &path);
977 std::pair<bool, Stat>
statSync(
const std::string &path);
987 void lstat(
const std::string &path);
998 std::pair<bool, Stat>
lstatSync(
const std::string &path);
1036 void rename(
const std::string &old,
const std::string &path);
1044 bool renameSync(
const std::string &old,
const std::string &path);
1107 void access(
const std::string &path,
int mode);
1126 void chmod(
const std::string &path,
int mode);
1148 void utime(
const std::string &path, Time atime, Time mtime);
1159 bool utimeSync(
const std::string &path, Time atime, Time mtime);
1170 void link(
const std::string &old,
const std::string &path);
1178 bool linkSync(
const std::string &old,
const std::string &path);
1237 std::pair<bool, std::pair<const char *, std::size_t>>
readlinkSync(
const std::string &path);
1397 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.