WIP: Fs refactoring

This commit is contained in:
Michele Caini 2016-07-27 11:09:41 +02:00
parent 464154d103
commit e322a4a3de

View File

@ -182,6 +182,18 @@ class Fs final: public Request<Fs, uv_fs_t> {
using Request::Request;
template<typename... Args>
auto cleanupAndInvoke(Args&&... args) {
uv_fs_req_cleanup(get<uv_fs_t>());
return invoke(std::forward<Args>(args)...);
}
template<typename F, typename... Args>
auto cleanupAndExec(F &&f, Args&&... args) {
uv_fs_req_cleanup(get<uv_fs_t>());
return std::forward<F>(f)(std::forward<Args>(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<uv_fs_t>(), file, &fsGenericCallback<Type::CLOSE>);
cleanupAndInvoke(&uv_fs_close, parent(), get<uv_fs_t>(), file, &fsGenericCallback<Type::CLOSE>);
}
auto closeSync(FileHandle file) {
auto err = uv_fs_close(parent(), get<uv_fs_t>(), file, nullptr);
auto err = cleanupAndExec(&uv_fs_close, parent(), get<uv_fs_t>(), file, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::CLOSE>{});
}
void open(std::string path, int flags, int mode) {
invoke(&uv_fs_open, parent(), get<uv_fs_t>(), path.data(), flags, mode, &fsGenericCallback<Type::OPEN>);
cleanupAndInvoke(&uv_fs_open, parent(), get<uv_fs_t>(), path.data(), flags, mode, &fsGenericCallback<Type::OPEN>);
}
auto openSync(std::string path, int flags, int mode) {
auto err = uv_fs_open(parent(), get<uv_fs_t>(), path.data(), flags, mode, nullptr);
auto err = cleanupAndExec(&uv_fs_open, parent(), get<uv_fs_t>(), path.data(), flags, mode, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::OPEN>{});
}
// TODO uv_fs_read (sync (cb null)/async)
void unlink(std::string path) {
invoke(&uv_fs_unlink, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::UNLINK>);
cleanupAndInvoke(&uv_fs_unlink, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::UNLINK>);
}
auto unlinkSync(std::string path) {
auto err = uv_fs_unlink(parent(), get<uv_fs_t>(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_unlink, parent(), get<uv_fs_t>(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::UNLINK>{});
}
void write(FileHandle file, std::unique_ptr<char[]> data, ssize_t len, int64_t offset) {
uv_buf_t bufs[] = { uv_buf_init(data.get(), len) };
invoke(&uv_fs_write, parent(), get<uv_fs_t>(), file, bufs, 1, offset, &fsWriteCallback);
cleanupAndInvoke(&uv_fs_write, parent(), get<uv_fs_t>(), file, bufs, 1, offset, &fsWriteCallback);
}
auto writeSync(FileHandle file, std::unique_ptr<char[]> 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<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::MKDIR>);
cleanupAndInvoke(&uv_fs_mkdir, parent(), get<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::MKDIR>);
}
auto mkdirSync(std::string path, int mode) {
@ -245,29 +257,29 @@ public:
}
void mkdtemp(std::string tpl) {
invoke(&uv_fs_mkdtemp, parent(), get<uv_fs_t>(), tpl.data(), &fsGenericCallback<Type::MKDTEMP>);
cleanupAndInvoke(&uv_fs_mkdtemp, parent(), get<uv_fs_t>(), tpl.data(), &fsGenericCallback<Type::MKDTEMP>);
}
auto mkdtempSync(std::string tpl) {
auto err = uv_fs_mkdtemp(parent(), get<uv_fs_t>(), tpl.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_mkdtemp, parent(), get<uv_fs_t>(), tpl.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::MKDTEMP>{});
}
void rmdir(std::string path) {
invoke(&uv_fs_rmdir, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::RMDIR>);
cleanupAndInvoke(&uv_fs_rmdir, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::RMDIR>);
}
auto rmdirSync(std::string path) {
auto err = uv_fs_rmdir(parent(), get<uv_fs_t>(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_rmdir, parent(), get<uv_fs_t>(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::RMDIR>{});
}
void scandir(std::string path, int flags) {
invoke(&uv_fs_scandir, parent(), get<uv_fs_t>(), path.data(), flags, &fsGenericCallback<Type::SCANDIR>);
cleanupAndInvoke(&uv_fs_scandir, parent(), get<uv_fs_t>(), path.data(), flags, &fsGenericCallback<Type::SCANDIR>);
}
auto scandirSync(std::string path, int flags) {
auto err = uv_fs_scandir(parent(), get<uv_fs_t>(), path.data(), flags, nullptr);
auto err = cleanupAndExec(&uv_fs_scandir, parent(), get<uv_fs_t>(), path.data(), flags, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::SCANDIR>{});
}
@ -286,170 +298,170 @@ public:
}
void stat(std::string path) {
invoke(&uv_fs_stat, parent(), get<uv_fs_t>(), path.data(), &fsStatCallback<Type::STAT>);
cleanupAndInvoke(&uv_fs_stat, parent(), get<uv_fs_t>(), path.data(), &fsStatCallback<Type::STAT>);
}
auto statSync(std::string path) {
auto err = uv_fs_stat(parent(), get<uv_fs_t>(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_stat, parent(), get<uv_fs_t>(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::STAT>{get<uv_fs_t>()->statbuf});
}
void fstat(FileHandle file) {
invoke(&uv_fs_fstat, parent(), get<uv_fs_t>(), file, &fsStatCallback<Type::FSTAT>);
cleanupAndInvoke(&uv_fs_fstat, parent(), get<uv_fs_t>(), file, &fsStatCallback<Type::FSTAT>);
}
auto fstatSync(FileHandle file) {
auto err = uv_fs_fstat(parent(), get<uv_fs_t>(), file, nullptr);
auto err = cleanupAndExec(&uv_fs_fstat, parent(), get<uv_fs_t>(), file, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FSTAT>{get<uv_fs_t>()->statbuf});
}
void lstat(std::string path) {
invoke(&uv_fs_lstat, parent(), get<uv_fs_t>(), path.data(), &fsStatCallback<Type::LSTAT>);
cleanupAndInvoke(&uv_fs_lstat, parent(), get<uv_fs_t>(), path.data(), &fsStatCallback<Type::LSTAT>);
}
auto lstatSync(std::string path) {
auto err = uv_fs_lstat(parent(), get<uv_fs_t>(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_lstat, parent(), get<uv_fs_t>(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::LSTAT>{get<uv_fs_t>()->statbuf});
}
void rename(std::string old, std::string path) {
invoke(&uv_fs_rename, parent(), get<uv_fs_t>(), old.data(), path.data(), &fsGenericCallback<Type::RENAME>);
cleanupAndInvoke(&uv_fs_rename, parent(), get<uv_fs_t>(), old.data(), path.data(), &fsGenericCallback<Type::RENAME>);
}
auto renameSync(std::string old, std::string path) {
auto err = uv_fs_rename(parent(), get<uv_fs_t>(), old.data(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_rename, parent(), get<uv_fs_t>(), old.data(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::RENAME>{});
}
void fsync(FileHandle file) {
invoke(&uv_fs_fsync, parent(), get<uv_fs_t>(), file, &fsGenericCallback<Type::FSYNC>);
cleanupAndInvoke(&uv_fs_fsync, parent(), get<uv_fs_t>(), file, &fsGenericCallback<Type::FSYNC>);
}
auto fsyncSync(FileHandle file) {
auto err = uv_fs_fsync(parent(), get<uv_fs_t>(), file, nullptr);
auto err = cleanupAndExec(&uv_fs_fsync, parent(), get<uv_fs_t>(), file, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FSYNC>{});
}
void fdatasync(FileHandle file) {
invoke(&uv_fs_fdatasync, parent(), get<uv_fs_t>(), file, &fsGenericCallback<Type::FDATASYNC>);
cleanupAndInvoke(&uv_fs_fdatasync, parent(), get<uv_fs_t>(), file, &fsGenericCallback<Type::FDATASYNC>);
}
auto fdatasyncSync(FileHandle file) {
auto err = uv_fs_fdatasync(parent(), get<uv_fs_t>(), file, nullptr);
auto err = cleanupAndExec(&uv_fs_fdatasync, parent(), get<uv_fs_t>(), file, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FDATASYNC>{});
}
void ftruncate(FileHandle file, int64_t offset) {
invoke(&uv_fs_ftruncate, parent(), get<uv_fs_t>(), file, offset, &fsGenericCallback<Type::FTRUNCATE>);
cleanupAndInvoke(&uv_fs_ftruncate, parent(), get<uv_fs_t>(), file, offset, &fsGenericCallback<Type::FTRUNCATE>);
}
auto ftruncateSync(FileHandle file, int64_t offset) {
auto err = uv_fs_ftruncate(parent(), get<uv_fs_t>(), file, offset, nullptr);
auto err = cleanupAndExec(&uv_fs_ftruncate, parent(), get<uv_fs_t>(), file, offset, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FTRUNCATE>{});
}
void sendfile(FileHandle out, FileHandle in, int64_t offset, size_t length) {
invoke(&uv_fs_sendfile, parent(), get<uv_fs_t>(), out, in, offset, length, &fsGenericCallback<Type::SENDFILE>);
cleanupAndInvoke(&uv_fs_sendfile, parent(), get<uv_fs_t>(), out, in, offset, length, &fsGenericCallback<Type::SENDFILE>);
}
auto sendfileSync(FileHandle out, FileHandle in, int64_t offset, size_t length) {
auto err = uv_fs_sendfile(parent(), get<uv_fs_t>(), out, in, offset, length, nullptr);
auto err = cleanupAndExec(&uv_fs_sendfile, parent(), get<uv_fs_t>(), out, in, offset, length, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::SENDFILE>{});
}
void access(std::string path, int mode) {
invoke(&uv_fs_access, parent(), get<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::ACCESS>);
cleanupAndInvoke(&uv_fs_access, parent(), get<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::ACCESS>);
}
auto accessSync(std::string path, int mode) {
auto err = uv_fs_access(parent(), get<uv_fs_t>(), path.data(), mode, nullptr);
auto err = cleanupAndExec(&uv_fs_access, parent(), get<uv_fs_t>(), path.data(), mode, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::ACCESS>{});
}
void chmod(std::string path, int mode) {
invoke(&uv_fs_chmod, parent(), get<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::CHMOD>);
cleanupAndInvoke(&uv_fs_chmod, parent(), get<uv_fs_t>(), path.data(), mode, &fsGenericCallback<Type::CHMOD>);
}
auto chmodSync(std::string path, int mode) {
auto err = uv_fs_chmod(parent(), get<uv_fs_t>(), path.data(), mode, nullptr);
auto err = cleanupAndExec(&uv_fs_chmod, parent(), get<uv_fs_t>(), path.data(), mode, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::CHMOD>{});
}
void fchmod(FileHandle file, int mode) {
invoke(&uv_fs_fchmod, parent(), get<uv_fs_t>(), file, mode, &fsGenericCallback<Type::FCHMOD>);
cleanupAndInvoke(&uv_fs_fchmod, parent(), get<uv_fs_t>(), file, mode, &fsGenericCallback<Type::FCHMOD>);
}
auto fchmodSync(FileHandle file, int mode) {
auto err = uv_fs_fchmod(parent(), get<uv_fs_t>(), file, mode, nullptr);
auto err = cleanupAndExec(&uv_fs_fchmod, parent(), get<uv_fs_t>(), file, mode, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FCHMOD>{});
}
void utime(std::string path, Time atime, Time mtime) {
invoke(&uv_fs_utime, parent(), get<uv_fs_t>(), path.data(), atime.count(), mtime.count(), &fsGenericCallback<Type::UTIME>);
cleanupAndInvoke(&uv_fs_utime, parent(), get<uv_fs_t>(), path.data(), atime.count(), mtime.count(), &fsGenericCallback<Type::UTIME>);
}
auto utimeSync(std::string path, Time atime, Time mtime) {
auto err = uv_fs_utime(parent(), get<uv_fs_t>(), path.data(), atime.count(), mtime.count(), nullptr);
auto err = cleanupAndExec(&uv_fs_utime, parent(), get<uv_fs_t>(), path.data(), atime.count(), mtime.count(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::UTIME>{});
}
void futime(FileHandle file, Time atime, Time mtime) {
invoke(&uv_fs_futime, parent(), get<uv_fs_t>(), file, atime.count(), mtime.count(), &fsGenericCallback<Type::FUTIME>);
cleanupAndInvoke(&uv_fs_futime, parent(), get<uv_fs_t>(), file, atime.count(), mtime.count(), &fsGenericCallback<Type::FUTIME>);
}
auto futimeSync(FileHandle file, Time atime, Time mtime) {
auto err = uv_fs_futime(parent(), get<uv_fs_t>(), file, atime.count(), mtime.count(), nullptr);
auto err = cleanupAndExec(&uv_fs_futime, parent(), get<uv_fs_t>(), file, atime.count(), mtime.count(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FUTIME>{});
}
void link(std::string old, std::string path) {
invoke(&uv_fs_link, parent(), get<uv_fs_t>(), old.data(), path.data(), &fsGenericCallback<Type::LINK>);
cleanupAndInvoke(&uv_fs_link, parent(), get<uv_fs_t>(), old.data(), path.data(), &fsGenericCallback<Type::LINK>);
}
auto linkSync(std::string old, std::string path) {
auto err = uv_fs_link(parent(), get<uv_fs_t>(), old.data(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_link, parent(), get<uv_fs_t>(), old.data(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::LINK>{});
}
void symlink(std::string old, std::string path, int flags) {
invoke(&uv_fs_symlink, parent(), get<uv_fs_t>(), old.data(), path.data(), flags, &fsGenericCallback<Type::SYMLINK>);
cleanupAndInvoke(&uv_fs_symlink, parent(), get<uv_fs_t>(), old.data(), path.data(), flags, &fsGenericCallback<Type::SYMLINK>);
}
auto symlinkSync(std::string old, std::string path, int flags) {
auto err = uv_fs_symlink(parent(), get<uv_fs_t>(), old.data(), path.data(), flags, nullptr);
auto err = cleanupAndExec(&uv_fs_symlink, parent(), get<uv_fs_t>(), old.data(), path.data(), flags, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::SYMLINK>{});
}
void readlink(std::string path) {
invoke(&uv_fs_readlink, parent(), get<uv_fs_t>(), path.data(), &fsReadlinkCallback);
cleanupAndInvoke(&uv_fs_readlink, parent(), get<uv_fs_t>(), path.data(), &fsReadlinkCallback);
}
// TODO uv_fs_readlink (sync (cb null))
void realpath(std::string path) {
invoke(&uv_fs_realpath, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::REALPATH>);
cleanupAndInvoke(&uv_fs_realpath, parent(), get<uv_fs_t>(), path.data(), &fsGenericCallback<Type::REALPATH>);
}
auto realpathSync(std::string path) {
auto err = uv_fs_realpath(parent(), get<uv_fs_t>(), path.data(), nullptr);
auto err = cleanupAndExec(&uv_fs_realpath, parent(), get<uv_fs_t>(), path.data(), nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::REALPATH>{});
}
void chown(std::string path, Uid uid, Gid gid) {
invoke(&uv_fs_chown, parent(), get<uv_fs_t>(), path.data(), uid, gid, &fsGenericCallback<Type::CHOWN>);
cleanupAndInvoke(&uv_fs_chown, parent(), get<uv_fs_t>(), path.data(), uid, gid, &fsGenericCallback<Type::CHOWN>);
}
auto chownSync(std::string path, Uid uid, Gid gid) {
auto err = uv_fs_chown(parent(), get<uv_fs_t>(), path.data(), uid, gid, nullptr);
auto err = cleanupAndExec(&uv_fs_chown, parent(), get<uv_fs_t>(), path.data(), uid, gid, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::CHOWN>{});
}
void fchown(FileHandle file, Uid uid, Gid gid) {
invoke(&uv_fs_fchown, parent(), get<uv_fs_t>(), file, uid, gid, &fsGenericCallback<Type::FCHOWN>);
cleanupAndInvoke(&uv_fs_fchown, parent(), get<uv_fs_t>(), file, uid, gid, &fsGenericCallback<Type::FCHOWN>);
}
auto fchownSync(FileHandle file, Uid uid, Gid gid) {
auto err = uv_fs_fchown(parent(), get<uv_fs_t>(), file, uid, gid, nullptr);
auto err = cleanupAndExec(&uv_fs_fchown, parent(), get<uv_fs_t>(), file, uid, gid, nullptr);
return std::make_pair(ErrorEvent{err}, FsEvent<Type::FCHOWN>{});
}
};