From e322a4a3de53afae69ff1ed5068ccafb0d48bdc0 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 27 Jul 2016 11:09:41 +0200 Subject: [PATCH] WIP: Fs refactoring --- src/uvw/fs.hpp | 114 +++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 51 deletions(-) diff --git a/src/uvw/fs.hpp b/src/uvw/fs.hpp index 8d95db52..4de70211 100644 --- a/src/uvw/fs.hpp +++ b/src/uvw/fs.hpp @@ -182,6 +182,18 @@ class Fs final: public Request { using Request::Request; + template + auto cleanupAndInvoke(Args&&... args) { + uv_fs_req_cleanup(get()); + return invoke(std::forward(args)...); + } + + template + auto cleanupAndExec(F &&f, Args&&... args) { + uv_fs_req_cleanup(get()); + return std::forward(f)(std::forward(args)...); + } + public: using Time = std::chrono::seconds; using Type = details::UVFsType; @@ -198,37 +210,37 @@ public: } void close(FileHandle file) { - invoke(&uv_fs_close, parent(), get(), file, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_close, parent(), get(), file, &fsGenericCallback); } auto closeSync(FileHandle file) { - auto err = uv_fs_close(parent(), get(), file, nullptr); + auto err = cleanupAndExec(&uv_fs_close, parent(), get(), file, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void open(std::string path, int flags, int mode) { - invoke(&uv_fs_open, parent(), get(), path.data(), flags, mode, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_open, parent(), get(), path.data(), flags, mode, &fsGenericCallback); } auto openSync(std::string path, int flags, int mode) { - auto err = uv_fs_open(parent(), get(), path.data(), flags, mode, nullptr); + auto err = cleanupAndExec(&uv_fs_open, parent(), get(), path.data(), flags, mode, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } // TODO uv_fs_read (sync (cb null)/async) void unlink(std::string path) { - invoke(&uv_fs_unlink, parent(), get(), path.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_unlink, parent(), get(), path.data(), &fsGenericCallback); } auto unlinkSync(std::string path) { - auto err = uv_fs_unlink(parent(), get(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_unlink, parent(), get(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void write(FileHandle file, std::unique_ptr data, ssize_t len, int64_t offset) { uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; - invoke(&uv_fs_write, parent(), get(), file, bufs, 1, offset, &fsWriteCallback); + cleanupAndInvoke(&uv_fs_write, parent(), get(), file, bufs, 1, offset, &fsWriteCallback); } auto writeSync(FileHandle file, std::unique_ptr data, ssize_t len, int64_t offset) { @@ -236,7 +248,7 @@ public: } void mkdir(std::string path, int mode) { - invoke(&uv_fs_mkdir, parent(), get(), path.data(), mode, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_mkdir, parent(), get(), path.data(), mode, &fsGenericCallback); } auto mkdirSync(std::string path, int mode) { @@ -245,29 +257,29 @@ public: } void mkdtemp(std::string tpl) { - invoke(&uv_fs_mkdtemp, parent(), get(), tpl.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_mkdtemp, parent(), get(), tpl.data(), &fsGenericCallback); } auto mkdtempSync(std::string tpl) { - auto err = uv_fs_mkdtemp(parent(), get(), tpl.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_mkdtemp, parent(), get(), tpl.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void rmdir(std::string path) { - invoke(&uv_fs_rmdir, parent(), get(), path.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_rmdir, parent(), get(), path.data(), &fsGenericCallback); } auto rmdirSync(std::string path) { - auto err = uv_fs_rmdir(parent(), get(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_rmdir, parent(), get(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void scandir(std::string path, int flags) { - invoke(&uv_fs_scandir, parent(), get(), path.data(), flags, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_scandir, parent(), get(), path.data(), flags, &fsGenericCallback); } auto scandirSync(std::string path, int flags) { - auto err = uv_fs_scandir(parent(), get(), path.data(), flags, nullptr); + auto err = cleanupAndExec(&uv_fs_scandir, parent(), get(), path.data(), flags, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } @@ -286,170 +298,170 @@ public: } void stat(std::string path) { - invoke(&uv_fs_stat, parent(), get(), path.data(), &fsStatCallback); + cleanupAndInvoke(&uv_fs_stat, parent(), get(), path.data(), &fsStatCallback); } auto statSync(std::string path) { - auto err = uv_fs_stat(parent(), get(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_stat, parent(), get(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{get()->statbuf}); } void fstat(FileHandle file) { - invoke(&uv_fs_fstat, parent(), get(), file, &fsStatCallback); + cleanupAndInvoke(&uv_fs_fstat, parent(), get(), file, &fsStatCallback); } auto fstatSync(FileHandle file) { - auto err = uv_fs_fstat(parent(), get(), file, nullptr); + auto err = cleanupAndExec(&uv_fs_fstat, parent(), get(), file, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{get()->statbuf}); } void lstat(std::string path) { - invoke(&uv_fs_lstat, parent(), get(), path.data(), &fsStatCallback); + cleanupAndInvoke(&uv_fs_lstat, parent(), get(), path.data(), &fsStatCallback); } auto lstatSync(std::string path) { - auto err = uv_fs_lstat(parent(), get(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_lstat, parent(), get(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{get()->statbuf}); } void rename(std::string old, std::string path) { - invoke(&uv_fs_rename, parent(), get(), old.data(), path.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_rename, parent(), get(), old.data(), path.data(), &fsGenericCallback); } auto renameSync(std::string old, std::string path) { - auto err = uv_fs_rename(parent(), get(), old.data(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_rename, parent(), get(), old.data(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void fsync(FileHandle file) { - invoke(&uv_fs_fsync, parent(), get(), file, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_fsync, parent(), get(), file, &fsGenericCallback); } auto fsyncSync(FileHandle file) { - auto err = uv_fs_fsync(parent(), get(), file, nullptr); + auto err = cleanupAndExec(&uv_fs_fsync, parent(), get(), file, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void fdatasync(FileHandle file) { - invoke(&uv_fs_fdatasync, parent(), get(), file, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_fdatasync, parent(), get(), file, &fsGenericCallback); } auto fdatasyncSync(FileHandle file) { - auto err = uv_fs_fdatasync(parent(), get(), file, nullptr); + auto err = cleanupAndExec(&uv_fs_fdatasync, parent(), get(), file, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void ftruncate(FileHandle file, int64_t offset) { - invoke(&uv_fs_ftruncate, parent(), get(), file, offset, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_ftruncate, parent(), get(), file, offset, &fsGenericCallback); } auto ftruncateSync(FileHandle file, int64_t offset) { - auto err = uv_fs_ftruncate(parent(), get(), file, offset, nullptr); + auto err = cleanupAndExec(&uv_fs_ftruncate, parent(), get(), file, offset, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void sendfile(FileHandle out, FileHandle in, int64_t offset, size_t length) { - invoke(&uv_fs_sendfile, parent(), get(), out, in, offset, length, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_sendfile, parent(), get(), out, in, offset, length, &fsGenericCallback); } auto sendfileSync(FileHandle out, FileHandle in, int64_t offset, size_t length) { - auto err = uv_fs_sendfile(parent(), get(), out, in, offset, length, nullptr); + auto err = cleanupAndExec(&uv_fs_sendfile, parent(), get(), out, in, offset, length, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void access(std::string path, int mode) { - invoke(&uv_fs_access, parent(), get(), path.data(), mode, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_access, parent(), get(), path.data(), mode, &fsGenericCallback); } auto accessSync(std::string path, int mode) { - auto err = uv_fs_access(parent(), get(), path.data(), mode, nullptr); + auto err = cleanupAndExec(&uv_fs_access, parent(), get(), path.data(), mode, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void chmod(std::string path, int mode) { - invoke(&uv_fs_chmod, parent(), get(), path.data(), mode, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_chmod, parent(), get(), path.data(), mode, &fsGenericCallback); } auto chmodSync(std::string path, int mode) { - auto err = uv_fs_chmod(parent(), get(), path.data(), mode, nullptr); + auto err = cleanupAndExec(&uv_fs_chmod, parent(), get(), path.data(), mode, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void fchmod(FileHandle file, int mode) { - invoke(&uv_fs_fchmod, parent(), get(), file, mode, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_fchmod, parent(), get(), file, mode, &fsGenericCallback); } auto fchmodSync(FileHandle file, int mode) { - auto err = uv_fs_fchmod(parent(), get(), file, mode, nullptr); + auto err = cleanupAndExec(&uv_fs_fchmod, parent(), get(), file, mode, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void utime(std::string path, Time atime, Time mtime) { - invoke(&uv_fs_utime, parent(), get(), path.data(), atime.count(), mtime.count(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_utime, parent(), get(), path.data(), atime.count(), mtime.count(), &fsGenericCallback); } auto utimeSync(std::string path, Time atime, Time mtime) { - auto err = uv_fs_utime(parent(), get(), path.data(), atime.count(), mtime.count(), nullptr); + auto err = cleanupAndExec(&uv_fs_utime, parent(), get(), path.data(), atime.count(), mtime.count(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void futime(FileHandle file, Time atime, Time mtime) { - invoke(&uv_fs_futime, parent(), get(), file, atime.count(), mtime.count(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_futime, parent(), get(), file, atime.count(), mtime.count(), &fsGenericCallback); } auto futimeSync(FileHandle file, Time atime, Time mtime) { - auto err = uv_fs_futime(parent(), get(), file, atime.count(), mtime.count(), nullptr); + auto err = cleanupAndExec(&uv_fs_futime, parent(), get(), file, atime.count(), mtime.count(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void link(std::string old, std::string path) { - invoke(&uv_fs_link, parent(), get(), old.data(), path.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_link, parent(), get(), old.data(), path.data(), &fsGenericCallback); } auto linkSync(std::string old, std::string path) { - auto err = uv_fs_link(parent(), get(), old.data(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_link, parent(), get(), old.data(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void symlink(std::string old, std::string path, int flags) { - invoke(&uv_fs_symlink, parent(), get(), old.data(), path.data(), flags, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_symlink, parent(), get(), old.data(), path.data(), flags, &fsGenericCallback); } auto symlinkSync(std::string old, std::string path, int flags) { - auto err = uv_fs_symlink(parent(), get(), old.data(), path.data(), flags, nullptr); + auto err = cleanupAndExec(&uv_fs_symlink, parent(), get(), old.data(), path.data(), flags, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void readlink(std::string path) { - invoke(&uv_fs_readlink, parent(), get(), path.data(), &fsReadlinkCallback); + cleanupAndInvoke(&uv_fs_readlink, parent(), get(), path.data(), &fsReadlinkCallback); } // TODO uv_fs_readlink (sync (cb null)) void realpath(std::string path) { - invoke(&uv_fs_realpath, parent(), get(), path.data(), &fsGenericCallback); + cleanupAndInvoke(&uv_fs_realpath, parent(), get(), path.data(), &fsGenericCallback); } auto realpathSync(std::string path) { - auto err = uv_fs_realpath(parent(), get(), path.data(), nullptr); + auto err = cleanupAndExec(&uv_fs_realpath, parent(), get(), path.data(), nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void chown(std::string path, Uid uid, Gid gid) { - invoke(&uv_fs_chown, parent(), get(), path.data(), uid, gid, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_chown, parent(), get(), path.data(), uid, gid, &fsGenericCallback); } auto chownSync(std::string path, Uid uid, Gid gid) { - auto err = uv_fs_chown(parent(), get(), path.data(), uid, gid, nullptr); + auto err = cleanupAndExec(&uv_fs_chown, parent(), get(), path.data(), uid, gid, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } void fchown(FileHandle file, Uid uid, Gid gid) { - invoke(&uv_fs_fchown, parent(), get(), file, uid, gid, &fsGenericCallback); + cleanupAndInvoke(&uv_fs_fchown, parent(), get(), file, uid, gid, &fsGenericCallback); } auto fchownSync(FileHandle file, Uid uid, Gid gid) { - auto err = uv_fs_fchown(parent(), get(), file, uid, gid, nullptr); + auto err = cleanupAndExec(&uv_fs_fchown, parent(), get(), file, uid, gid, nullptr); return std::make_pair(ErrorEvent{err}, FsEvent{}); } };