diff --git a/.gitignore b/.gitignore index 7590ea28..e0053a74 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ rpmbuild/ # mkdocs files mkdocs/venv __pycache__/ + +# claude code harness +.claude/ diff --git a/src/branches.hpp b/src/branches.hpp index dae01822..6f169fe3 100644 --- a/src/branches.hpp +++ b/src/branches.hpp @@ -24,6 +24,7 @@ #include "tofrom_string.hpp" #include +#include #include #include #include @@ -78,10 +79,60 @@ public: { } +public: + class ConstIterator + { + private: + Branches::Ptr _impl; + size_t _idx = 0; + + public: + ConstIterator() = default; + ConstIterator(Branches::Ptr impl_, + const size_t idx_) + : _impl(impl_), + _idx(idx_) + { + } + + public: + const Branch& operator*() const + { + return (*_impl)[_idx]; + } + + ConstIterator& operator++() + { + ++_idx; + return *this; + } + + bool operator!=(const ConstIterator &rhs_) const + { + if(rhs_._impl == nullptr) + return (_impl != nullptr && _idx < _impl->size()); + if(_impl == nullptr) + return (rhs_._impl != nullptr && rhs_._idx < rhs_._impl->size()); + return ((_impl != rhs_._impl) || (_idx != rhs_._idx)); + } + }; + public: int from_string(const std::string_view str) final; std::string to_string(void) const final; +public: + ConstIterator begin() const + { + std::shared_lock lk(_mutex); + return ConstIterator(_impl,0); + } + + ConstIterator end() const + { + return ConstIterator(); + } + public: operator Ptr() const { diff --git a/src/category.cpp b/src/category.cpp index bdb68b53..e140a30a 100644 --- a/src/category.cpp +++ b/src/category.cpp @@ -18,8 +18,8 @@ #include "category.hpp" #include "errno.hpp" -#include "str.hpp" +#include #include int @@ -34,16 +34,45 @@ Category::Base::from_string(const std::string_view s_) return rv; } + { + std::lock_guard lk(_last_set_mutex); + _last_set = std::string(s_); + } + return 0; } std::string Category::Base::to_string(void) const { - std::set rv; + // Snapshot the last category-wide input under the lock so a + // concurrent from_string can't tear the string mid-read. + std::string last; + { + std::lock_guard lk(_last_set_mutex); + last = _last_set; + } + // Collect current constituent names. Each FuncWrapper::to_string is + // itself mutex-protected. + std::set names; for(const auto func : funcs) - rv.insert(func->to_string()); + names.insert(func->to_string()); - return str::join(rv,','); + if(names.size() == 1) + { + // All children agree. Prefer _last_set (round-trippable through + // the category-wide setter even when it maps to the agreed + // name); otherwise fall back to the agreed name. + if(!last.empty()) + return last; + return *names.begin(); + } + + // Children diverge — either no category-wide assignment ever ran, + // or a per-function override has changed a child since the last + // category-wide set. Report "mixed" rather than a stale _last_set, + // since per-function .to_string()s are the source of truth for the + // actual policy in effect. + return std::string("mixed"); } diff --git a/src/category.hpp b/src/category.hpp index 8791eff6..b0bf85c0 100644 --- a/src/category.hpp +++ b/src/category.hpp @@ -19,10 +19,10 @@ #pragma once #include "tofrom_string.hpp" -#include "funcs.hpp" -#include "func.hpp" +#include #include +#include namespace Category { @@ -34,6 +34,13 @@ namespace Category protected: std::vector funcs; + // The last value successfully applied via from_string. Categories + // can map a single user input to different per-function default + // classes, so the constituent name()s may disagree. Returning the + // last-set value here keeps to_string output round-trippable, but + // only if no per-function override has since diverged the children. + std::string _last_set; + mutable std::mutex _last_set_mutex; }; class Action final : public Base @@ -42,18 +49,27 @@ namespace Category Action(); public: - Action(Funcs &funcs_) + Action(ToFromString &chmod_, + ToFromString &chown_, + ToFromString &link_, + ToFromString &removexattr_, + ToFromString &rename_, + ToFromString &rmdir_, + ToFromString &setxattr_, + ToFromString &truncate_, + ToFromString &unlink_, + ToFromString &utimens_) { - funcs.push_back(&funcs_.chmod); - funcs.push_back(&funcs_.chown); - funcs.push_back(&funcs_.link); - funcs.push_back(&funcs_.removexattr); - funcs.push_back(&funcs_.rename); - funcs.push_back(&funcs_.rmdir); - funcs.push_back(&funcs_.setxattr); - funcs.push_back(&funcs_.truncate); - funcs.push_back(&funcs_.unlink); - funcs.push_back(&funcs_.utimens); + funcs.push_back(&chmod_); + funcs.push_back(&chown_); + funcs.push_back(&link_); + funcs.push_back(&removexattr_); + funcs.push_back(&rename_); + funcs.push_back(&rmdir_); + funcs.push_back(&setxattr_); + funcs.push_back(&truncate_); + funcs.push_back(&unlink_); + funcs.push_back(&utimens_); } }; @@ -63,12 +79,15 @@ namespace Category Create(); public: - Create(Funcs &funcs_) + Create(ToFromString &create_, + ToFromString &mkdir_, + ToFromString &mknod_, + ToFromString &symlink_) { - funcs.push_back(&funcs_.create); - funcs.push_back(&funcs_.mkdir); - funcs.push_back(&funcs_.mknod); - funcs.push_back(&funcs_.symlink); + funcs.push_back(&create_); + funcs.push_back(&mkdir_); + funcs.push_back(&mknod_); + funcs.push_back(&symlink_); } }; @@ -78,14 +97,21 @@ namespace Category Search(); public: - Search(Funcs &funcs_) + Search(ToFromString &access_, + ToFromString &getattr_, + ToFromString &getxattr_, + ToFromString &ioctl_, + ToFromString &listxattr_, + ToFromString &open_, + ToFromString &readlink_) { - funcs.push_back(&funcs_.access); - funcs.push_back(&funcs_.getattr); - funcs.push_back(&funcs_.getxattr); - funcs.push_back(&funcs_.listxattr); - funcs.push_back(&funcs_.open); - funcs.push_back(&funcs_.readlink); + funcs.push_back(&access_); + funcs.push_back(&getattr_); + funcs.push_back(&getxattr_); + funcs.push_back(&ioctl_); + funcs.push_back(&listxattr_); + funcs.push_back(&open_); + funcs.push_back(&readlink_); } }; } @@ -96,10 +122,45 @@ private: Categories(); public: - Categories(Funcs &funcs_) - : action(funcs_), - create(funcs_), - search(funcs_) + Categories(ToFromString &access_, + ToFromString &chmod_, + ToFromString &chown_, + ToFromString &link_, + ToFromString &getattr_, + ToFromString &getxattr_, + ToFromString &ioctl_, + ToFromString &listxattr_, + ToFromString &open_, + ToFromString &readlink_, + ToFromString &rename_, + ToFromString &removexattr_, + ToFromString &rmdir_, + ToFromString &setxattr_, + ToFromString &truncate_, + ToFromString &unlink_, + ToFromString &utimens_, + ToFromString &create_, + ToFromString &mkdir_, + ToFromString &mknod_, + ToFromString &symlink_) + : action(chmod_, + chown_, + link_, + removexattr_, + rename_, + rmdir_, + setxattr_, + truncate_, + unlink_, + utimens_), + create(create_,mkdir_,mknod_,symlink_), + search(access_, + getattr_, + getxattr_, + ioctl_, + listxattr_, + open_, + readlink_) {} public: diff --git a/src/config.cpp b/src/config.cpp index 2194c1dc..a93add95 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -85,7 +85,27 @@ Config::Config() cache_statfs(0), cache_symlinks(false), cache_writeback(false), - category(func), + category(access, + chmod, + chown, + link, + _getattr_statx, + getxattr, + ioctl, + listxattr, + open, + readlink, + rename, + removexattr, + rmdir, + setxattr, + truncate, + unlink, + utimens, + create, + mkdir, + mknod, + symlink), config_file(), direct_io_allow_mmap(true), dropcacheonclose(false), @@ -93,7 +113,6 @@ Config::Config() flushonclose(FlushOnClose::ENUM::OPENED_FOR_WRITE), follow_symlinks(FollowSymlinks::ENUM::NEVER), fsname(), - func(), fuse_msg_size("1M"), handle_killpriv(true), handle_killpriv_v2(true), @@ -219,27 +238,29 @@ Config::Config() _map["flush-on-close"] = &flushonclose; _map["follow-symlinks"] = &follow_symlinks; _map["fsname"] = &fsname; - _map["func.access"] = &func.access; - _map["func.chmod"] = &func.chmod; - _map["func.chown"] = &func.chown; - _map["func.create"] = &func.create; - _map["func.getattr"] = &func.getattr; - _map["func.getxattr"] = &func.getxattr; - _map["func.link"] = &func.link; - _map["func.listxattr"] = &func.listxattr; - _map["func.mkdir"] = &func.mkdir; - _map["func.mknod"] = &func.mknod; - _map["func.open"] = &func.open; + _map["func.access"] = &access; + _map["func.chmod"] = &chmod; + _map["func.chown"] = &chown; + _map["func.create"] = &create; + _map["func.getattr"] = &_getattr_statx; + _map["func.getxattr"] = &getxattr; + _map["func.ioctl"] = &ioctl; + _map["func.link"] = &link; + _map["func.listxattr"] = &listxattr; + _map["func.mkdir"] = &mkdir; + _map["func.mknod"] = &mknod; + _map["func.open"] = &open; _map["func.readdir"] = &readdir; - _map["func.readlink"] = &func.readlink; - _map["func.removexattr"] = &func.removexattr; - _map["func.rename"] = &func.rename; - _map["func.rmdir"] = &func.rmdir; - _map["func.setxattr"] = &func.setxattr; - _map["func.symlink"] = &func.symlink; - _map["func.truncate"] = &func.truncate; - _map["func.unlink"] = &func.unlink; - _map["func.utimens"] = &func.utimens; + _map["func.readlink"] = &readlink; + _map["func.removexattr"] = &removexattr; + _map["func.rename"] = &rename; + _map["func.rmdir"] = &rmdir; + _map["func.setxattr"] = &setxattr; + _map["func.statx"] = &_getattr_statx; + _map["func.symlink"] = &symlink; + _map["func.truncate"] = &truncate; + _map["func.unlink"] = &unlink; + _map["func.utimens"] = &utimens; _map["fuse-msg-size"] = &fuse_msg_size; _map["gid"] = &_gid; _map["gid-cache.expire-timeout"] = &_dummy; @@ -448,6 +469,34 @@ Config::finish_initializing() _initialized = true; } +void +Config::initialize_funcs() +{ + access.initialize(); + chmod.initialize(); + chown.initialize(); + create.initialize(); + getattr.initialize(); + getxattr.initialize(); + ioctl.initialize(); + link.initialize(); + listxattr.initialize(); + mkdir.initialize(); + mknod.initialize(); + open.initialize(); + readlink.initialize(); + removexattr.initialize(); + rename.initialize(); + rmdir.initialize(); + setxattr.initialize(); + statx.initialize(); + symlink.initialize(); + truncate.initialize(); + unlink.initialize(); + utimens.initialize(); + readdir.initialize(); +} + bool Config::is_rootdir(const fs::path &fusepath_) { diff --git a/src/config.hpp b/src/config.hpp index 0005a2e3..5bc1a3ec 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -18,6 +18,29 @@ #pragma once +#include "func_access.hpp" +#include "func_chmod.hpp" +#include "func_chown.hpp" +#include "func_create.hpp" +#include "func_getattr.hpp" +#include "func_getxattr.hpp" +#include "func_ioctl.hpp" +#include "func_link.hpp" +#include "func_listxattr.hpp" +#include "func_mkdir.hpp" +#include "func_mknod.hpp" +#include "func_open.hpp" +#include "func_readlink.hpp" +#include "func_removexattr.hpp" +#include "func_rename.hpp" +#include "func_rmdir.hpp" +#include "func_setxattr.hpp" +#include "func_statx.hpp" +#include "func_symlink.hpp" +#include "func_truncate.hpp" +#include "func_unlink.hpp" +#include "func_utimens.hpp" + #include "branches.hpp" #include "category.hpp" #include "config_cachefiles.hpp" @@ -25,6 +48,7 @@ #include "config_dummy.hpp" #include "config_flushonclose.hpp" #include "config_follow_symlinks.hpp" +#include "config_getattr_statx.hpp" #include "config_inodecalc.hpp" #include "config_link_exdev.hpp" #include "config_log_file.hpp" @@ -43,10 +67,8 @@ #include "enum.hpp" #include "errno.hpp" #include "fs_path.hpp" -#include "funcs.hpp" #include "fuse_cfg.hpp" #include "fuse_readdir.hpp" -#include "policy.hpp" #include "tofrom_ref.hpp" #include "tofrom_wrapper.hpp" #include "syslog.hpp" @@ -107,6 +129,31 @@ public: Config(); public: + Func2::Access access{"ff"}; + Func2::Chmod chmod{"all"}; + Func2::Chown chown{"all"}; + Func2::Create create{"pfrd"}; + Func2::GetAttr getattr{"cdfo"}; + Func2::Getxattr getxattr{"ff"}; + Func2::Ioctl ioctl{"ff"}; + Func2::Link link{"epall"}; + Func2::Listxattr listxattr{"ff"}; + Func2::Mkdir mkdir{"pfrd"}; + Func2::Mknod mknod{"ff"}; + Func2::Open open{"ff"}; + Func2::Readlink readlink{"ff"}; + Func2::Removexattr removexattr{"all"}; + Func2::Rename rename{"epall"}; + Func2::Rmdir rmdir{"all"}; + Func2::Setxattr setxattr{"all"}; + Func2::Statx statx{"cdfo"}; + Func2::Symlink symlink{"pfrd"}; + Func2::Truncate truncate{"all"}; + Func2::Unlink unlink{"all"}; + Func2::Utimens utimens{"all"}; + + ConfigGetAttrStatx _getattr_statx{getattr,statx}; + ConfigBOOL allow_idmap; ConfigBOOL async_read; Branches branches; @@ -129,7 +176,6 @@ public: FlushOnClose flushonclose; FollowSymlinks follow_symlinks; ConfigSTR fsname; - Funcs func; ConfigPageSize fuse_msg_size; ConfigBOOL handle_killpriv; ConfigBOOL handle_killpriv_v2; @@ -189,6 +235,7 @@ private: public: void finish_initializing(); + void initialize_funcs(); public: friend std::ostream& operator<<(std::ostream &s, diff --git a/src/config_follow_symlinks.hpp b/src/config_follow_symlinks.hpp index b001a23f..dcf06231 100644 --- a/src/config_follow_symlinks.hpp +++ b/src/config_follow_symlinks.hpp @@ -19,12 +19,6 @@ #pragma once #include "enum.hpp" +#include "follow_symlinks_enum.hpp" -enum class FollowSymlinksEnum - { - NEVER, - DIRECTORY, - REGULAR, - ALL - }; typedef Enum FollowSymlinks; diff --git a/src/config_getattr_statx.hpp b/src/config_getattr_statx.hpp new file mode 100644 index 00000000..09fbae49 --- /dev/null +++ b/src/config_getattr_statx.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include "config.hpp" +#include "tofrom_string.hpp" + +class ConfigGetAttrStatx : public ToFromString +{ +private: + Func2::GetAttr &_getattr; + Func2::Statx &_statx; + +public: + ConfigGetAttrStatx() = delete; + ConfigGetAttrStatx(Func2::GetAttr &getattr_, + Func2::Statx &statx_) + : _getattr(getattr_), + _statx(statx_) + { + } + + std::string + to_string() const + { + std::string g = _getattr.to_string(); + std::string s = _statx.to_string(); + + if(g == s) + return g; + + return g + "," + s; + } + + int + from_string(const std::string_view str_) + { + int rv; + + if(!Func2::GetAttrFactory::valid(std::string(str_))) + return -EINVAL; + if(!Func2::StatxFactory::valid(std::string(str_))) + return -EINVAL; + + rv = _getattr.from_string(str_); + if(rv < 0) + return rv; + + rv = _statx.from_string(str_); + if(rv < 0) + return rv; + + return 0; + } +}; diff --git a/src/config_moveonenospc.cpp b/src/config_moveonenospc.cpp index 13ad7a6d..4811bcfc 100644 --- a/src/config_moveonenospc.cpp +++ b/src/config_moveonenospc.cpp @@ -17,7 +17,7 @@ */ #include "config_moveonenospc.hpp" -#include "ef.hpp" + #include "errno.hpp" #include "from_string.hpp" @@ -25,34 +25,16 @@ int MoveOnENOSPC::from_string(const std::string_view s_) { - int rv; - std::string s; - Policy::CreateImpl *tmp; - - rv = str::from(s_,&enabled); - if((rv == 0) && (enabled == true)) - s = "pfrd"; - ef((rv == 0) && (enabled == false)) - return (enabled = false,0); - ef(rv != 0) - s = s_; - else + bool tmp; + const int rv = str::from(s_,&tmp); + if(rv != 0) return -EINVAL; - - tmp = Policies::Create::find(s); - if(tmp == NULL) - return -EINVAL; - - policy = tmp; - enabled = true; - + enabled = tmp; return 0; } std::string MoveOnENOSPC::to_string(void) const { - if(enabled) - return policy.name(); - return "false"; + return (enabled ? "true" : "false"); } diff --git a/src/config_moveonenospc.hpp b/src/config_moveonenospc.hpp index 47f99423..1a3d4806 100644 --- a/src/config_moveonenospc.hpp +++ b/src/config_moveonenospc.hpp @@ -18,8 +18,6 @@ #pragma once -#include "policy.hpp" -#include "policies.hpp" #include "tofrom_string.hpp" #include @@ -29,8 +27,7 @@ class MoveOnENOSPC : public ToFromString { public: MoveOnENOSPC(const bool enabled_) - : enabled(enabled_), - policy(&Policies::Create::pfrd) + : enabled(enabled_) { } @@ -40,5 +37,4 @@ public: public: bool enabled; - Policy::Create policy; }; diff --git a/src/error.hpp b/src/error.hpp deleted file mode 100644 index 8f05bac0..00000000 --- a/src/error.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#pragma once - -#include "errno.hpp" - -#include - - -struct Err -{ -private: - std::optional _err; - -public: - Err() - { - } - - operator int() - { - return (_err.has_value() ? _err.value() : -ENOENT); - } - - Err& - operator=(int v_) - { - if(!_err.has_value()) - _err = ((v_ >= 0) ? 0 : v_); - else if(v_ >= 0) - _err = 0; - - return *this; - } - - bool - operator==(int v_) - { - if(_err.has_value()) - return (_err.value() == v_); - return false; - } -}; diff --git a/src/follow_symlinks_enum.hpp b/src/follow_symlinks_enum.hpp new file mode 100644 index 00000000..c708b898 --- /dev/null +++ b/src/follow_symlinks_enum.hpp @@ -0,0 +1,9 @@ +#pragma once + +enum class FollowSymlinksEnum + { + NEVER, + DIRECTORY, + REGULAR, + ALL + }; diff --git a/src/fs_lstat.hpp b/src/fs_lstat.hpp index 45abe4fa..965d93c2 100644 --- a/src/fs_lstat.hpp +++ b/src/fs_lstat.hpp @@ -19,8 +19,7 @@ #pragma once #include "to_neg_errno.hpp" - -#include +#include "to_cstr.hpp" #include #include @@ -29,25 +28,17 @@ namespace fs { + template static inline int - lstat(const char *path_, - struct stat *st_) + lstat(const PathType &path_, + struct stat *st_) { int rv; - rv = ::lstat(path_,st_); + rv = ::lstat(to_cstr(path_),st_); return ::to_neg_errno(rv); } - - static - inline - int - lstat(const std::string &path_, - struct stat *st_) - { - return fs::lstat(path_.c_str(),st_); - } } diff --git a/src/fs_mknod_as.hpp b/src/fs_mknod_as.hpp index 5a6faa0a..b2753fe2 100644 --- a/src/fs_mknod_as.hpp +++ b/src/fs_mknod_as.hpp @@ -18,6 +18,7 @@ #pragma once +#include "fs_acl.hpp" #include "fs_mknod.hpp" #include "ugid.hpp" @@ -31,9 +32,13 @@ namespace fs int mknod_as(const ugid_t ugid_, const T &path_, - const mode_t mode_, - const dev_t dev_) + mode_t mode_, + dev_t dev_, + mode_t umask_) { + if(not fs::acl::dir_has_defaults(path_)) + mode_ &= ~umask_; + const ugid::SetGuard _(ugid_); return fs::mknod(path_,mode_,dev_); @@ -41,6 +46,7 @@ namespace fs } #elif defined __FreeBSD__ #include "fs_lchown.hpp" +#include "fs_unlink.hpp" namespace fs { @@ -50,16 +56,29 @@ namespace fs int mknod_as(const ugid_t ugid_, const T &path_, - const mode_t mode_, - const dev_t dev_) + mode_t mode_, + dev_t dev_, + mode_t umask_) { int rv; + if(not fs::acl::dir_has_defaults(path_)) + mode_ &= ~umask_; + rv = fs::mknod(path_,mode_,dev_); + if(rv < 0) + return rv; - fs::lchown(path_,ugid_.uid,ugid_.gid); + const int lrv = fs::lchown(path_,ugid_.uid,ugid_.gid); + if(lrv < 0) + { + // Clean up the just-created node so a retry sees -ENOENT + // instead of -EEXIST and userspace doesn't observe a phantom. + fs::unlink(path_); + return lrv; + } - return rv; + return 0; } } #else diff --git a/src/fs_movefile_and_open.cpp b/src/fs_movefile_and_open.cpp index 6f3bc86a..e72476d4 100644 --- a/src/fs_movefile_and_open.cpp +++ b/src/fs_movefile_and_open.cpp @@ -21,28 +21,20 @@ #include "base_types.h" #include "errno.hpp" #include "fs_clonepath.hpp" -#include "fs_close.hpp" #include "fs_copyfile.hpp" #include "fs_file_size.hpp" -#include "fs_findonfs.hpp" #include "fs_getfl.hpp" #include "fs_has_space.hpp" -#include "fs_mktemp.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" #include "fs_open.hpp" #include "fs_path.hpp" -#include "fs_rename.hpp" -#include "fs_stat.hpp" #include "fs_unlink.hpp" -#include "policy.hpp" +#include "rnd.hpp" -#include #include #include -#include -#include -#include -#include static @@ -59,13 +51,61 @@ _cleanup_flags(const int flags_) return rv; } +// Pick a writable destination branch using PFRD (proportional to free +// space). Self-contained for moveonenospc — mirrors the same algorithm +// used by Func2::CreatePFRD but on a fresh selection (no clonepath/open). static int -_movefile_and_open(const Policy::Create &createFunc_, - const Branches::Ptr &branches_, - const fs::path &branchpath_, - const fs::path &fusepath_, - int origfd_) +_pick_pfrd_branch(const Branches &branches_, + Branch *&chosen_) +{ + int err = ENOENT; + Branches::Ptr branches = branches_; + fs::info_t info; + struct W { Branch *b; uint64_t weight; }; + std::vector writable; + uint64_t sum = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + { if(err == ENOENT) err = EROFS; continue; } + const int irv = fs::info(branch.path,&info); + if(irv < 0) + { if(err == ENOENT) err = ENOENT; continue; } + if(info.readonly) + { if(err == ENOENT) err = EROFS; continue; } + if(info.spaceavail < branch.minfreespace()) + { if(err == ENOENT) err = ENOSPC; continue; } + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -err; + if(sum == 0) + return -ENOSPC; + + const uint64_t threshold = RND::rand64(sum); + uint64_t idx = 0; + for(const auto &w : writable) + { + if(w.weight == 0) continue; + idx += w.weight; + if(idx > threshold) { chosen_ = w.b; return 0; } + } + + chosen_ = writable.back().b; + return 0; +} + +static +int +_movefile_and_open(const Branches &branches_, + const fs::path &branchpath_, + const fs::path &fusepath_, + int origfd_) { int rv; int dstfd_flags; @@ -75,14 +115,14 @@ _movefile_and_open(const Policy::Create &createFunc_, fs::path src_branch; fs::path src_filepath; fs::path dst_filepath; - std::vector dst_branch; + Branch *dst_branch = nullptr; src_branch = branchpath_; - rv = createFunc_(branches_,fusepath_,dst_branch); + rv = ::_pick_pfrd_branch(branches_,dst_branch); if(rv < 0) return rv; - if(dst_branch.empty()) + if(dst_branch == nullptr) return -ENOSPC; origfd_flags = fs::getfl(origfd_); @@ -93,17 +133,17 @@ _movefile_and_open(const Policy::Create &createFunc_, if(src_size < 0) return src_size; - if(fs::has_space(dst_branch[0]->path,src_size) == false) + if(fs::has_space(dst_branch->path,src_size) == false) return -ENOSPC; fusedir = fusepath_.parent_path(); - rv = fs::clonepath(src_branch,dst_branch[0]->path,fusedir); + rv = fs::clonepath(src_branch,dst_branch->path,fusedir); if(rv < 0) return -ENOSPC; src_filepath = src_branch / fusepath_; - dst_filepath = dst_branch[0]->path / fusepath_; + dst_filepath = dst_branch->path / fusepath_; rv = fs::copyfile(src_filepath,dst_filepath,{.cleanup_failure = true}); if(rv < 0) @@ -120,29 +160,19 @@ _movefile_and_open(const Policy::Create &createFunc_, } int -fs::movefile_and_open(const Policy::Create &policy_, - const Branches::Ptr &branches_, - const fs::path &branchpath_, - const fs::path &fusepath_, - const int origfd_) +fs::movefile_and_open(const Branches &branches_, + const fs::path &branchpath_, + const fs::path &fusepath_, + const int origfd_) { - return ::_movefile_and_open(policy_, - branches_, - branchpath_, - fusepath_, - origfd_); + return ::_movefile_and_open(branches_,branchpath_,fusepath_,origfd_); } int -fs::movefile_and_open_as_root(const Policy::Create &policy_, - const Branches::Ptr &branches_, - const fs::path &branchpath_, - const fs::path &fusepath_, - const int origfd_) +fs::movefile_and_open_as_root(const Branches &branches_, + const fs::path &branchpath_, + const fs::path &fusepath_, + const int origfd_) { - return fs::movefile_and_open(policy_, - branches_, - branchpath_, - fusepath_, - origfd_); + return fs::movefile_and_open(branches_,branchpath_,fusepath_,origfd_); } diff --git a/src/fs_movefile_and_open.hpp b/src/fs_movefile_and_open.hpp index 2510287d..112611cc 100644 --- a/src/fs_movefile_and_open.hpp +++ b/src/fs_movefile_and_open.hpp @@ -20,24 +20,19 @@ #include "branches.hpp" #include "fs_path.hpp" -#include "policy.hpp" - -#include namespace fs { int - movefile_and_open(const Policy::Create &policy, - const Branches::Ptr &branches_, - const fs::path &branchpath, - const fs::path &fusepath, - const int origfd); + movefile_and_open(const Branches &branches, + const fs::path &branchpath, + const fs::path &fusepath, + const int origfd); int - movefile_and_open_as_root(const Policy::Create &policy, - const Branches::Ptr &branches_, - const fs::path &branchpath, - const fs::path &fusepath, - int origfd); + movefile_and_open_as_root(const Branches &branches, + const fs::path &branchpath, + const fs::path &fusepath, + int origfd); } diff --git a/src/fs_stat.cpp b/src/fs_stat.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/fs_stat.hpp b/src/fs_stat.hpp index 2bfc0164..2f095701 100644 --- a/src/fs_stat.hpp +++ b/src/fs_stat.hpp @@ -18,7 +18,10 @@ #pragma once +#include "follow_symlinks_enum.hpp" #include "to_neg_errno.hpp" +#include "to_cstr.hpp" +#include "fs_lstat.hpp" #include @@ -29,25 +32,66 @@ namespace fs { + template static inline int - stat(const char *path_, - struct stat *st_) + stat(const PathType &path_, + struct stat *st_) { int rv; - rv = ::stat(path_,st_); + rv = ::stat(to_cstr(path_),st_); return ::to_neg_errno(rv); } + template static inline int - stat(const std::string &path_, - struct stat *st_) + stat(const PathType &path_, + struct stat *st_, + FollowSymlinksEnum follow_) { - return fs::stat(path_.c_str(),st_); + int rv; + + switch(follow_) + { + default: + case FollowSymlinksEnum::NEVER: + rv = fs::lstat(path_,st_); + return rv; + case FollowSymlinksEnum::DIRECTORY: + rv = fs::lstat(path_,st_); + if((rv >= 0) && S_ISLNK(st_->st_mode)) + { + struct stat st{}; + + // If the link's target lookup fails (e.g. dangling link), + // keep the link's own lstat data so callers like ls -l can + // still display the entry. + if((fs::stat(path_,&st) >= 0) && S_ISDIR(st.st_mode)) + *st_ = st; + } + return rv; + case FollowSymlinksEnum::REGULAR: + rv = fs::lstat(path_,st_); + if((rv >= 0) && S_ISLNK(st_->st_mode)) + { + struct stat st{}; + + if((fs::stat(path_,&st) >= 0) && S_ISREG(st.st_mode)) + *st_ = st; + } + return rv; + case FollowSymlinksEnum::ALL: + rv = fs::stat(path_,st_); + if(rv < 0) + rv = fs::lstat(path_,st_); + return rv; + } + + return -ENOENT; } } diff --git a/src/fs_statx.hpp b/src/fs_statx.hpp index 8dfd11fe..e6ee1154 100644 --- a/src/fs_statx.hpp +++ b/src/fs_statx.hpp @@ -18,6 +18,8 @@ #pragma once +#include "follow_symlinks_enum.hpp" +#include "to_cstr.hpp" #include "to_neg_errno.hpp" #include "fuse_kernel.h" @@ -33,13 +35,15 @@ #include "supported_statx.hpp" + namespace fs { + template static inline int statx(const int dirfd_, - const char *pathname_, + const PathType &pathname_, const int flags_, const unsigned int mask_, struct fuse_statx *st_) @@ -48,7 +52,7 @@ namespace fs int rv; rv = ::statx(dirfd_, - pathname_, + to_cstr(pathname_), flags_, mask_, (struct statx*)st_); @@ -59,19 +63,64 @@ namespace fs #endif } + template static inline int statx(const int dirfd_, - const std::string &pathname_, + const PathType &pathname_, const int flags_, const unsigned int mask_, - struct fuse_statx *st_) + struct fuse_statx *st_, + FollowSymlinksEnum followsymlinks_) { - return fs::statx(dirfd_, - pathname_.c_str(), - flags_, - mask_, - st_); + int rv; + + // statx(2) recognises AT_SYMLINK_NOFOLLOW; follow-the-link is the + // default when that flag is absent. AT_SYMLINK_FOLLOW (a linkat(2) + // flag) is not meaningful here and is omitted. + const int follow_flags = (flags_ & ~AT_SYMLINK_NOFOLLOW); + const int nofollow_flags = (flags_ | AT_SYMLINK_NOFOLLOW); + + switch(followsymlinks_) + { + case FollowSymlinksEnum::NEVER: + rv = fs::statx(dirfd_,pathname_,nofollow_flags,mask_,st_); + return rv; + case FollowSymlinksEnum::DIRECTORY: + rv = fs::statx(dirfd_,pathname_,nofollow_flags,mask_,st_); + if((rv >= 0) && S_ISLNK(st_->mode)) + { + struct fuse_statx st{}; + + // Request the same mask as the caller: STATX_TYPE alone + // would leave the rest of `st` unspecified and clobber the + // caller's data when we copy back. On follow failure + // (dangling link, etc.), keep the lstat data so callers + // can still display the entry. + if((fs::statx(dirfd_,pathname_,follow_flags,mask_,&st) >= 0) && + S_ISDIR(st.mode)) + *st_ = st; + } + return rv; + case FollowSymlinksEnum::REGULAR: + rv = fs::statx(dirfd_,pathname_,nofollow_flags,mask_,st_); + if((rv >= 0) && S_ISLNK(st_->mode)) + { + struct fuse_statx st{}; + + if((fs::statx(dirfd_,pathname_,follow_flags,mask_,&st) >= 0) && + S_ISREG(st.mode)) + *st_ = st; + } + return rv; + case FollowSymlinksEnum::ALL: + rv = fs::statx(dirfd_,pathname_,follow_flags,mask_,st_); + if(rv < 0) + rv = fs::statx(dirfd_,pathname_,nofollow_flags,mask_,st_); + return rv; + } + + return -ENOENT; } } diff --git a/src/fsproxy.cpp b/src/fsproxy.cpp new file mode 100644 index 00000000..aa4d3716 --- /dev/null +++ b/src/fsproxy.cpp @@ -0,0 +1,36 @@ +#include "fsproxy.hpp" + +#include "fs_close.hpp" + +int +FSProxy::_spawn_proxy_if_needed(const uid_t uid_, + const gid_t gid_) +{ + int rv; + FSProxy::Process process; + + if(_proxies.count({uid_,gid_})) + return 0; + + rv = pipe(process.req_pipe); + if(rv == -1) + return rv; + rv = pipe(process.res_pipe); + if(rv == -1) + return rv; + + process.pid = fork(); + if(process.pid != 0) + { + fs::close(process.req_pipe[1]); + fs::close(process.res_pipe[0]); + // listen on req in loop for instructions + return 0; + } + + fs::close(process.req_pipe[0]); + fs::close(process.res_pipe[1]); + _proxies[UGID{uid_,gid_}] = process; + + return 0; +} diff --git a/src/fsproxy.hpp b/src/fsproxy.hpp new file mode 100644 index 00000000..95043769 --- /dev/null +++ b/src/fsproxy.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include + +#include +#include + +class FSProxy +{ +public: + struct UGID + { + bool + operator==(const UGID &other_) const + { + return ((uid == other_.uid) && + (gid == other_.gid)); + } + + uid_t uid; + gid_t gid; + }; + + struct UGIDHash + { + size_t + operator()(const UGID &key_) const + { + size_t h0 = std::hash{}(key_.uid); + size_t h1 = std::hash{}(key_.gid); + + return (h0 ^ (h1 << 1)); + } + }; + + struct Process + { + int pid; + int req_pipe[2]; + int res_pipe[2]; + }; + +public: + int create(uid_t uid, + gid_t gid, + const char *pathname, + mode_t mode); + +private: + int _spawn_proxy_if_needed(uid_t,gid_t); + +private: + std::unordered_map _proxies; +}; diff --git a/src/func.cpp b/src/func.cpp deleted file mode 100644 index 4c46d98a..00000000 --- a/src/func.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "func.hpp" - - -int -Func::Base::Action::from_string(const std::string_view policyname_) -{ - auto *tmp = Policies::Action::find(policyname_); - - if(not tmp) - return -EINVAL; - - policy = tmp; - - return 0; -} - -std::string -Func::Base::Action::to_string(void) const -{ - return policy.name(); -} - -int -Func::Base::Create::from_string(const std::string_view policyname_) -{ - auto *tmp = Policies::Create::find(policyname_); - - if(not tmp) - return -EINVAL; - - policy = tmp; - - return 0; -} - -std::string -Func::Base::Create::to_string(void) const -{ - return policy.name(); -} - -int -Func::Base::Search::from_string(const std::string_view policyname_) -{ - auto *tmp = Policies::Search::find(policyname_); - - if(not tmp) - return -EINVAL; - - policy = tmp; - - return 0; -} - -std::string -Func::Base::Search::to_string(void) const -{ - return policy.name(); -} diff --git a/src/func.hpp b/src/func.hpp deleted file mode 100644 index ba0ef016..00000000 --- a/src/func.hpp +++ /dev/null @@ -1,184 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" -#include "policies.hpp" -#include "tofrom_string.hpp" - -#include - - -namespace Func -{ - namespace Base - { - class Action : public ToFromString - { - public: - Action(Policy::ActionImpl *policy_) - : policy(policy_) - {} - - public: - int from_string(const std::string_view s) final; - std::string to_string() const final; - - public: - Policy::Action policy; - }; - - class Create : public ToFromString - { - public: - Create(Policy::CreateImpl *policy_) - : policy(policy_) - {} - - public: - int from_string(const std::string_view s) final; - std::string to_string() const final; - - public: - Policy::Create policy; - }; - - class Search : public ToFromString - { - public: - Search(Policy::SearchImpl *policy_) - : policy(policy_) - {} - - public: - int from_string(const std::string_view s) final; - std::string to_string() const final; - - public: - Policy::Search policy; - }; - - class ActionDefault : public Action - { - public: - ActionDefault() - : Func::Base::Action(&Policies::Action::epall) - { - } - }; - - class CreateDefault : public Create - { - public: - CreateDefault() - : Func::Base::Create(&Policies::Create::pfrd) - { - } - }; - - class SearchDefault : public Search - { - public: - SearchDefault() - : Func::Base::Search(&Policies::Search::ff) - { - } - }; - } - - class Access final : public Base::SearchDefault - { - }; - - class Chmod final : public Base::ActionDefault - { - }; - - class Chown final : public Base::ActionDefault - { - }; - - class Create final : public Base::CreateDefault - { - }; - - class GetAttr final : public Base::SearchDefault - { - }; - - class GetXAttr final : public Base::SearchDefault - { - }; - - class Link final : public Base::ActionDefault - { - }; - - class ListXAttr final : public Base::SearchDefault - { - }; - - class Mkdir final : public Base::CreateDefault - { - }; - - class Mknod final : public Base::CreateDefault - { - }; - - class Open final : public Base::SearchDefault - { - }; - - class Readlink final : public Base::SearchDefault - { - }; - - class RemoveXAttr final : public Base::ActionDefault - { - }; - - class Rename final : public Base::ActionDefault - { - }; - - class Rmdir final : public Base::ActionDefault - { - }; - - class SetXAttr final : public Base::ActionDefault - { - }; - - class Symlink final : public Base::CreateDefault - { - }; - - class Truncate final : public Base::ActionDefault - { - }; - - class Unlink final : public Base::ActionDefault - { - }; - - class Utimens final : public Base::ActionDefault - { - }; -} diff --git a/src/func_access.hpp b/src/func_access.hpp new file mode 100644 index 00000000..524b32a4 --- /dev/null +++ b/src/func_access.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "func_access_base.hpp" +#include "func_access_factory.hpp" + +#include "func_wrapper.hpp" + +namespace Func2 +{ + using Access = FuncWrapper; +} diff --git a/src/func_access_all.cpp b/src/func_access_all.cpp new file mode 100644 index 00000000..a0808eef --- /dev/null +++ b/src/func_access_all.cpp @@ -0,0 +1,26 @@ +#include "func_access_all.hpp" +#include "errno.hpp" +#include "fs_eaccess.hpp" + +std::string_view Func2::AccessAll::name() const { return "all"; } + +// "all": every branch must satisfy eaccess (including the file existing +// there). Returns the first failure. +int +Func2::AccessAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const int mode_) +{ + bool any = false; + for(const auto &branch : branches_) + { + any = true; + const fs::path fullpath = branch.path / fusepath_; + const int rv = fs::eaccess(fullpath,mode_); + if(rv < 0) + return rv; + } + if(!any) + return -ENOENT; + return 0; +} diff --git a/src/func_access_all.hpp b/src/func_access_all.hpp new file mode 100644 index 00000000..05a5970c --- /dev/null +++ b/src/func_access_all.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_access_base.hpp" +namespace Func2 { + class AccessAll : public AccessBase { + public: + AccessAll() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const int); + }; +} diff --git a/src/func_access_base.hpp b/src/func_access_base.hpp new file mode 100644 index 00000000..0be5ee2b --- /dev/null +++ b/src/func_access_base.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + + +namespace Func2 +{ + class AccessBase + { + public: + AccessBase() {} + ~AccessBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const int mode) = 0; + }; +} diff --git a/src/func_access_epall.cpp b/src/func_access_epall.cpp new file mode 100644 index 00000000..f08ab20b --- /dev/null +++ b/src/func_access_epall.cpp @@ -0,0 +1,26 @@ +#include "func_access_epall.hpp" +#include "errno.hpp" +#include "fs_eaccess.hpp" + +std::string_view Func2::AccessEPAll::name() const { return "epall"; } + +int +Func2::AccessEPAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const int mode_) +{ + bool found = false; + for(const auto &branch : branches_) + { + const fs::path fullpath = branch.path / fusepath_; + const int rv = fs::eaccess(fullpath,mode_); + if(rv == -ENOENT) + continue; + found = true; + if(rv < 0) + return rv; + } + if(!found) + return -ENOENT; + return 0; +} diff --git a/src/func_access_epall.hpp b/src/func_access_epall.hpp new file mode 100644 index 00000000..935c5d00 --- /dev/null +++ b/src/func_access_epall.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_access_base.hpp" +namespace Func2 { + class AccessEPAll : public AccessBase { + public: + AccessEPAll() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const int); + }; +} diff --git a/src/func_access_erofs.cpp b/src/func_access_erofs.cpp new file mode 100644 index 00000000..57989677 --- /dev/null +++ b/src/func_access_erofs.cpp @@ -0,0 +1,12 @@ +#include "func_access_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::AccessEROFS::name() const { return "erofs"; } + +int +Func2::AccessEROFS::operator()(const Branches&, + const fs::path&, + const int) +{ + return -EROFS; +} diff --git a/src/func_access_erofs.hpp b/src/func_access_erofs.hpp new file mode 100644 index 00000000..f3583ae5 --- /dev/null +++ b/src/func_access_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_access_base.hpp" +namespace Func2 { + class AccessEROFS : public AccessBase { + public: + AccessEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const int); + }; +} diff --git a/src/func_access_factory.cpp b/src/func_access_factory.cpp new file mode 100644 index 00000000..5992dbc9 --- /dev/null +++ b/src/func_access_factory.cpp @@ -0,0 +1,24 @@ +#include "func_access_factory.hpp" + +#include "func_access_all.hpp" +#include "func_access_epall.hpp" +#include "func_access_erofs.hpp" +#include "func_access_ff.hpp" + + +bool +Func2::AccessFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::AccessFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_access_factory.hpp b/src/func_access_factory.hpp new file mode 100644 index 00000000..786592a5 --- /dev/null +++ b/src/func_access_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_access_base.hpp" + +namespace Func2 +{ + class AccessFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_access_ff.cpp b/src/func_access_ff.cpp new file mode 100644 index 00000000..e1035a80 --- /dev/null +++ b/src/func_access_ff.cpp @@ -0,0 +1,32 @@ +#include "func_access_ff.hpp" + +#include "errno.hpp" +#include "fs_eaccess.hpp" + + +std::string_view +Func2::AccessFF::name() const +{ + return "ff"; +} + +int +Func2::AccessFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + const int mode_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int rv = fs::eaccess(fullpath,mode_); + if(rv == -ENOENT) + continue; + + return rv; + } + + return -ENOENT; +} diff --git a/src/func_access_ff.hpp b/src/func_access_ff.hpp new file mode 100644 index 00000000..cac9aadf --- /dev/null +++ b/src/func_access_ff.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_access_base.hpp" + +namespace Func2 +{ + class AccessFF : public AccessBase + { + public: + AccessFF() {} + ~AccessFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const int mode); + }; +} diff --git a/src/func_chmod.hpp b/src/func_chmod.hpp new file mode 100644 index 00000000..03894ece --- /dev/null +++ b/src/func_chmod.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_chmod_base.hpp" +#include "func_chmod_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Chmod = FuncWrapper; +} diff --git a/src/func_chmod_all.cpp b/src/func_chmod_all.cpp new file mode 100644 index 00000000..629e54ac --- /dev/null +++ b/src/func_chmod_all.cpp @@ -0,0 +1,42 @@ +#include "func_chmod_all.hpp" + +#include "errno.hpp" +#include "fs_lchmod.hpp" + + +std::string_view +Func2::ChmodAll::name() const +{ + return "all"; +} + +int +Func2::ChmodAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::lchmod(fullpath,mode_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_chmod_all.hpp b/src/func_chmod_all.hpp new file mode 100644 index 00000000..64ac69db --- /dev/null +++ b/src/func_chmod_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_chmod_base.hpp" + +namespace Func2 +{ + class ChmodAll : public ChmodBase + { + public: + ChmodAll() {} + ~ChmodAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const mode_t mode); + }; +} diff --git a/src/func_chmod_base.hpp b/src/func_chmod_base.hpp new file mode 100644 index 00000000..4c350bc6 --- /dev/null +++ b/src/func_chmod_base.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +#include + + +namespace Func2 +{ + class ChmodBase + { + public: + ChmodBase() {} + ~ChmodBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const mode_t mode) = 0; + }; +} diff --git a/src/func_chmod_erofs.cpp b/src/func_chmod_erofs.cpp new file mode 100644 index 00000000..4258d816 --- /dev/null +++ b/src/func_chmod_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_chmod_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::ChmodEROFS::name() const { return "erofs"; } + +int +Func2::ChmodEROFS::operator()(const Branches&,const fs::path&,const mode_t) +{ + return -EROFS; +} diff --git a/src/func_chmod_erofs.hpp b/src/func_chmod_erofs.hpp new file mode 100644 index 00000000..d90c2286 --- /dev/null +++ b/src/func_chmod_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_chmod_base.hpp" +namespace Func2 { + class ChmodEROFS : public ChmodBase { + public: + ChmodEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const mode_t); + }; +} diff --git a/src/func_chmod_factory.cpp b/src/func_chmod_factory.cpp new file mode 100644 index 00000000..68f6b32d --- /dev/null +++ b/src/func_chmod_factory.cpp @@ -0,0 +1,20 @@ +#include "func_chmod_factory.hpp" + +#include "func_chmod_all.hpp" +#include "func_chmod_erofs.hpp" + + +bool +Func2::ChmodFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::ChmodFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_chmod_factory.hpp b/src/func_chmod_factory.hpp new file mode 100644 index 00000000..4d6ad154 --- /dev/null +++ b/src/func_chmod_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_chmod_base.hpp" + +namespace Func2 +{ + class ChmodFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_chown.hpp b/src/func_chown.hpp new file mode 100644 index 00000000..7a63c04b --- /dev/null +++ b/src/func_chown.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "func_chown_base.hpp" +#include "func_chown_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Chown = FuncWrapper; +} diff --git a/src/func_chown_all.cpp b/src/func_chown_all.cpp new file mode 100644 index 00000000..b4e6dfd6 --- /dev/null +++ b/src/func_chown_all.cpp @@ -0,0 +1,43 @@ +#include "func_chown_all.hpp" + +#include "errno.hpp" +#include "fs_lchown.hpp" + + +std::string_view +Func2::ChownAll::name() const +{ + return "all"; +} + +int +Func2::ChownAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const uid_t uid_, + const gid_t gid_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::lchown(fullpath,uid_,gid_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_chown_all.hpp b/src/func_chown_all.hpp new file mode 100644 index 00000000..c8ed1ab1 --- /dev/null +++ b/src/func_chown_all.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_chown_base.hpp" + +namespace Func2 +{ + class ChownAll : public ChownBase + { + public: + ChownAll() {} + ~ChownAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const uid_t uid, + const gid_t gid); + }; +} diff --git a/src/func_chown_base.hpp b/src/func_chown_base.hpp new file mode 100644 index 00000000..8ad6ad3c --- /dev/null +++ b/src/func_chown_base.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +#include + + +namespace Func2 +{ + class ChownBase + { + public: + ChownBase() {} + ~ChownBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const uid_t uid, + const gid_t gid) = 0; + }; +} diff --git a/src/func_chown_erofs.cpp b/src/func_chown_erofs.cpp new file mode 100644 index 00000000..25cc16dc --- /dev/null +++ b/src/func_chown_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_chown_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::ChownEROFS::name() const { return "erofs"; } + +int +Func2::ChownEROFS::operator()(const Branches&,const fs::path&,const uid_t,const gid_t) +{ + return -EROFS; +} diff --git a/src/func_chown_erofs.hpp b/src/func_chown_erofs.hpp new file mode 100644 index 00000000..d8dbe379 --- /dev/null +++ b/src/func_chown_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_chown_base.hpp" +namespace Func2 { + class ChownEROFS : public ChownBase { + public: + ChownEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const uid_t,const gid_t); + }; +} diff --git a/src/func_chown_factory.cpp b/src/func_chown_factory.cpp new file mode 100644 index 00000000..0289656d --- /dev/null +++ b/src/func_chown_factory.cpp @@ -0,0 +1,20 @@ +#include "func_chown_factory.hpp" + +#include "func_chown_all.hpp" +#include "func_chown_erofs.hpp" + + +bool +Func2::ChownFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::ChownFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_chown_factory.hpp b/src/func_chown_factory.hpp new file mode 100644 index 00000000..72529519 --- /dev/null +++ b/src/func_chown_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_chown_base.hpp" + +namespace Func2 +{ + class ChownFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_create.hpp b/src/func_create.hpp new file mode 100644 index 00000000..f390b731 --- /dev/null +++ b/src/func_create.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_create_base.hpp" +#include "func_create_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Create = FuncWrapper; +} diff --git a/src/func_create_all.cpp b/src/func_create_all.cpp new file mode 100644 index 00000000..93d482e4 --- /dev/null +++ b/src/func_create_all.cpp @@ -0,0 +1,131 @@ +#include "func_create_all.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateAll::name() const +{ + return "all"; +} + +bool +Func2::CreateAll::path_preserving() const +{ + return false; +} + + +int +Func2::CreateAll::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_all.hpp b/src/func_create_all.hpp new file mode 100644 index 00000000..54aee265 --- /dev/null +++ b/src/func_create_all.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateAll : public CreateBase + { + public: + CreateAll() {} + ~CreateAll() {} + + public: + std::string_view name() const; + bool path_preserving() const; + + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_base.hpp b/src/func_create_base.hpp new file mode 100644 index 00000000..4ce875a8 --- /dev/null +++ b/src/func_create_base.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" +#include "ugid.hpp" + +#include "fuse.h" + + +namespace Func2 +{ + class CreateBase + { + public: + CreateBase() {} + ~CreateBase() {} + + public: + virtual std::string_view name() const = 0; + virtual bool path_preserving() const = 0; + + public: + virtual int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask) = 0; + }; +} diff --git a/src/func_create_epall.cpp b/src/func_create_epall.cpp new file mode 100644 index 00000000..78f8bbc2 --- /dev/null +++ b/src/func_create_epall.cpp @@ -0,0 +1,113 @@ +#include "func_create_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPALL::name() const +{ + return "epall"; +} + +bool +Func2::CreateEPALL::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPALL::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_epall.hpp b/src/func_create_epall.hpp new file mode 100644 index 00000000..7f2fa8cd --- /dev/null +++ b/src/func_create_epall.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPALL : public CreateBase + { + public: + CreateEPALL() {} + ~CreateEPALL() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_epff.cpp b/src/func_create_epff.cpp new file mode 100644 index 00000000..90f86e34 --- /dev/null +++ b/src/func_create_epff.cpp @@ -0,0 +1,113 @@ +#include "func_create_epff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPFF::name() const +{ + return "epff"; +} + +bool +Func2::CreateEPFF::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_epff.hpp b/src/func_create_epff.hpp new file mode 100644 index 00000000..49558261 --- /dev/null +++ b/src/func_create_epff.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPFF : public CreateBase + { + public: + CreateEPFF() {} + ~CreateEPFF() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_eplfs.cpp b/src/func_create_eplfs.cpp new file mode 100644 index 00000000..ff4bf17a --- /dev/null +++ b/src/func_create_eplfs.cpp @@ -0,0 +1,119 @@ +#include "func_create_eplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPLFS::name() const +{ + return "eplfs"; +} + +bool +Func2::CreateEPLFS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lfs; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_eplfs.hpp b/src/func_create_eplfs.hpp new file mode 100644 index 00000000..66f9061a --- /dev/null +++ b/src/func_create_eplfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPLFS : public CreateBase + { + public: + CreateEPLFS() {} + ~CreateEPLFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_eplus.cpp b/src/func_create_eplus.cpp new file mode 100644 index 00000000..446f889e --- /dev/null +++ b/src/func_create_eplus.cpp @@ -0,0 +1,119 @@ +#include "func_create_eplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPLUS::name() const +{ + return "eplus"; +} + +bool +Func2::CreateEPLUS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lus; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_eplus.hpp b/src/func_create_eplus.hpp new file mode 100644 index 00000000..e2f46f3f --- /dev/null +++ b/src/func_create_eplus.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPLUS : public CreateBase + { + public: + CreateEPLUS() {} + ~CreateEPLUS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_epmfs.cpp b/src/func_create_epmfs.cpp new file mode 100644 index 00000000..d3b9d92b --- /dev/null +++ b/src/func_create_epmfs.cpp @@ -0,0 +1,118 @@ +#include "func_create_epmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPMFS::name() const +{ + return "epmfs"; +} + +bool +Func2::CreateEPMFS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 mfs; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_epmfs.hpp b/src/func_create_epmfs.hpp new file mode 100644 index 00000000..cf827fa4 --- /dev/null +++ b/src/func_create_epmfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPMFS : public CreateBase + { + public: + CreateEPMFS() {} + ~CreateEPMFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_eppfrd.cpp b/src/func_create_eppfrd.cpp new file mode 100644 index 00000000..6eecbbfd --- /dev/null +++ b/src/func_create_eppfrd.cpp @@ -0,0 +1,141 @@ +#include "func_create_eppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPPFRD::name() const +{ + return "eppfrd"; +} + +bool +Func2::CreateEPPFRD::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + struct W { Branch *b; u64 weight; }; + std::vector writable; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_eppfrd.hpp b/src/func_create_eppfrd.hpp new file mode 100644 index 00000000..07ede9de --- /dev/null +++ b/src/func_create_eppfrd.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPPFRD : public CreateBase + { + public: + CreateEPPFRD() {} + ~CreateEPPFRD() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_eprand.cpp b/src/func_create_eprand.cpp new file mode 100644 index 00000000..1ea1bcd7 --- /dev/null +++ b/src/func_create_eprand.cpp @@ -0,0 +1,116 @@ +#include "func_create_eprand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateEPRAND::name() const +{ + return "eprand"; +} + +bool +Func2::CreateEPRAND::path_preserving() const +{ + return true; +} + + +int +Func2::CreateEPRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + std::vector writable; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_eprand.hpp b/src/func_create_eprand.hpp new file mode 100644 index 00000000..9692bed3 --- /dev/null +++ b/src/func_create_eprand.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEPRAND : public CreateBase + { + public: + CreateEPRAND() {} + ~CreateEPRAND() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_erofs.cpp b/src/func_create_erofs.cpp new file mode 100644 index 00000000..a8f75e98 --- /dev/null +++ b/src/func_create_erofs.cpp @@ -0,0 +1,35 @@ +#include "func_create_erofs.hpp" + +#include "errno.hpp" + + +std::string_view +Func2::CreateEROFS::name() const +{ + return "erofs"; +} + +bool +Func2::CreateEROFS::path_preserving() const +{ + return false; +} + + +int +Func2::CreateEROFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + (void)ugid_; + (void)branches_; + (void)fusepath_; + (void)ffi_; + (void)mode_; + (void)umask_; + + return -EROFS; +} diff --git a/src/func_create_erofs.hpp b/src/func_create_erofs.hpp new file mode 100644 index 00000000..35d41f1c --- /dev/null +++ b/src/func_create_erofs.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateEROFS : public CreateBase + { + public: + CreateEROFS() {} + ~CreateEROFS() {} + + public: + std::string_view name() const; + bool path_preserving() const; + + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_factory.cpp b/src/func_create_factory.cpp new file mode 100644 index 00000000..06961fcb --- /dev/null +++ b/src/func_create_factory.cpp @@ -0,0 +1,58 @@ +#include "func_create_factory.hpp" + +#include "func_create_all.hpp" +#include "func_create_epall.hpp" +#include "func_create_epff.hpp" +#include "func_create_eplfs.hpp" +#include "func_create_eplus.hpp" +#include "func_create_epmfs.hpp" +#include "func_create_eppfrd.hpp" +#include "func_create_eprand.hpp" +#include "func_create_erofs.hpp" +#include "func_create_ff.hpp" +#include "func_create_lfs.hpp" +#include "func_create_lup.hpp" +#include "func_create_lus.hpp" +#include "func_create_mfs.hpp" +#include "func_create_msplfs.hpp" +#include "func_create_msplus.hpp" +#include "func_create_mspmfs.hpp" +#include "func_create_msppfrd.hpp" +#include "func_create_newest.hpp" +#include "func_create_pfrd.hpp" +#include "func_create_rand.hpp" + + +bool +Func2::CreateFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::CreateFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "epff") return std::make_shared(); + if(str_ == "eplfs") return std::make_shared(); + if(str_ == "eplus") return std::make_shared(); + if(str_ == "epmfs") return std::make_shared(); + if(str_ == "eppfrd") return std::make_shared(); + if(str_ == "eprand") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "lfs") return std::make_shared(); + if(str_ == "lup") return std::make_shared(); + if(str_ == "lus") return std::make_shared(); + if(str_ == "mfs") return std::make_shared(); + if(str_ == "msplfs") return std::make_shared(); + if(str_ == "msplus") return std::make_shared(); + if(str_ == "mspmfs") return std::make_shared(); + if(str_ == "msppfrd") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + if(str_ == "pfrd") return std::make_shared(); + if(str_ == "rand") return std::make_shared(); + + return {}; +} diff --git a/src/func_create_factory.hpp b/src/func_create_factory.hpp new file mode 100644 index 00000000..3ef2e0ef --- /dev/null +++ b/src/func_create_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class CreateBase; + + class CreateFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} diff --git a/src/func_create_ff.cpp b/src/func_create_ff.cpp new file mode 100644 index 00000000..b3151b61 --- /dev/null +++ b/src/func_create_ff.cpp @@ -0,0 +1,131 @@ +#include "func_create_ff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateFF::name() const +{ + return "ff"; +} + +bool +Func2::CreateFF::path_preserving() const +{ + return false; +} + + +int +Func2::CreateFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_ff.hpp b/src/func_create_ff.hpp new file mode 100644 index 00000000..6db5d211 --- /dev/null +++ b/src/func_create_ff.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateFF : public CreateBase + { + public: + CreateFF() {} + ~CreateFF() {} + + public: + std::string_view name() const; + bool path_preserving() const; + + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_lfs.cpp b/src/func_create_lfs.cpp new file mode 100644 index 00000000..0e0733fb --- /dev/null +++ b/src/func_create_lfs.cpp @@ -0,0 +1,137 @@ +#include "func_create_lfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateLFS::name() const +{ + return "lfs"; +} + +bool +Func2::CreateLFS::path_preserving() const +{ + return false; +} + + +int +Func2::CreateLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lfs; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_lfs.hpp b/src/func_create_lfs.hpp new file mode 100644 index 00000000..9fd879a3 --- /dev/null +++ b/src/func_create_lfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateLFS : public CreateBase + { + public: + CreateLFS() {} + ~CreateLFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_lup.cpp b/src/func_create_lup.cpp new file mode 100644 index 00000000..7bcba8e7 --- /dev/null +++ b/src/func_create_lup.cpp @@ -0,0 +1,145 @@ +#include "func_create_lup.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateLUP::name() const +{ + return "lup"; +} + +bool +Func2::CreateLUP::path_preserving() const +{ + return false; +} + + +int +Func2::CreateLUP::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + long double best_ratio; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + best_ratio = 2.0L; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + { + u64 used = info.spaceused; + u64 total = info.spaceused + info.spaceavail; + long double ratio; + + if(total == 0) + { used = 0; total = 1; } + ratio = (long double)used / (long double)total; + if(ratio >= best_ratio) + continue; + + best_ratio = ratio; + chosen = &branch; + } + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_lup.hpp b/src/func_create_lup.hpp new file mode 100644 index 00000000..598a8005 --- /dev/null +++ b/src/func_create_lup.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateLUP : public CreateBase + { + public: + CreateLUP() {} + ~CreateLUP() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_lus.cpp b/src/func_create_lus.cpp new file mode 100644 index 00000000..92eeb81d --- /dev/null +++ b/src/func_create_lus.cpp @@ -0,0 +1,137 @@ +#include "func_create_lus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateLUS::name() const +{ + return "lus"; +} + +bool +Func2::CreateLUS::path_preserving() const +{ + return false; +} + + +int +Func2::CreateLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lus; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_lus.hpp b/src/func_create_lus.hpp new file mode 100644 index 00000000..14a4bdb7 --- /dev/null +++ b/src/func_create_lus.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateLUS : public CreateBase + { + public: + CreateLUS() {} + ~CreateLUS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_mfs.cpp b/src/func_create_mfs.cpp new file mode 100644 index 00000000..a572c042 --- /dev/null +++ b/src/func_create_mfs.cpp @@ -0,0 +1,136 @@ +#include "func_create_mfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateMFS::name() const +{ + return "mfs"; +} + +bool +Func2::CreateMFS::path_preserving() const +{ + return false; +} + + +int +Func2::CreateMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 mfs; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_mfs.hpp b/src/func_create_mfs.hpp new file mode 100644 index 00000000..788b2af0 --- /dev/null +++ b/src/func_create_mfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateMFS : public CreateBase + { + public: + CreateMFS() {} + ~CreateMFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_msplfs.cpp b/src/func_create_msplfs.cpp new file mode 100644 index 00000000..1c1986cc --- /dev/null +++ b/src/func_create_msplfs.cpp @@ -0,0 +1,172 @@ +#include "func_create_msplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateMSPLFS::name() const +{ + return "msplfs"; +} + +bool +Func2::CreateMSPLFS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateMSPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lfs; + + any = false; + lfs = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail > lfs) + continue; + lfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_msplfs.hpp b/src/func_create_msplfs.hpp new file mode 100644 index 00000000..ade6ebc6 --- /dev/null +++ b/src/func_create_msplfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateMSPLFS : public CreateBase + { + public: + CreateMSPLFS() {} + ~CreateMSPLFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_msplus.cpp b/src/func_create_msplus.cpp new file mode 100644 index 00000000..9f375430 --- /dev/null +++ b/src/func_create_msplus.cpp @@ -0,0 +1,172 @@ +#include "func_create_msplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateMSPLUS::name() const +{ + return "msplus"; +} + +bool +Func2::CreateMSPLUS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateMSPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lus; + + any = false; + lus = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceused > lus) + continue; + lus = info.spaceused; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_msplus.hpp b/src/func_create_msplus.hpp new file mode 100644 index 00000000..6d331135 --- /dev/null +++ b/src/func_create_msplus.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateMSPLUS : public CreateBase + { + public: + CreateMSPLUS() {} + ~CreateMSPLUS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_mspmfs.cpp b/src/func_create_mspmfs.cpp new file mode 100644 index 00000000..1c2fea53 --- /dev/null +++ b/src/func_create_mspmfs.cpp @@ -0,0 +1,172 @@ +#include "func_create_mspmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateMSPMFS::name() const +{ + return "mspmfs"; +} + +bool +Func2::CreateMSPMFS::path_preserving() const +{ + return true; +} + + +int +Func2::CreateMSPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 mfs; + + any = false; + mfs = 0; + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail < mfs) + continue; + mfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_mspmfs.hpp b/src/func_create_mspmfs.hpp new file mode 100644 index 00000000..286bd816 --- /dev/null +++ b/src/func_create_mspmfs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateMSPMFS : public CreateBase + { + public: + CreateMSPMFS() {} + ~CreateMSPMFS() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_msppfrd.cpp b/src/func_create_msppfrd.cpp new file mode 100644 index 00000000..66d58ccc --- /dev/null +++ b/src/func_create_msppfrd.cpp @@ -0,0 +1,195 @@ +#include "func_create_msppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateMSPPFRD::name() const +{ + return "msppfrd"; +} + +bool +Func2::CreateMSPPFRD::path_preserving() const +{ + return true; +} + + +int +Func2::CreateMSPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + u64 sum; + struct W { Branch *b; u64 weight; }; + std::vector writable; + + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(!writable.empty()) + { + if(sum == 0) + { + chosen = writable.front().b; + } + else + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + break; + } + + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_msppfrd.hpp b/src/func_create_msppfrd.hpp new file mode 100644 index 00000000..ded6b0c2 --- /dev/null +++ b/src/func_create_msppfrd.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateMSPPFRD : public CreateBase + { + public: + CreateMSPPFRD() {} + ~CreateMSPPFRD() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_newest.cpp b/src/func_create_newest.cpp new file mode 100644 index 00000000..5366355a --- /dev/null +++ b/src/func_create_newest.cpp @@ -0,0 +1,139 @@ +#include "func_create_newest.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "timespec_utils.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateNewest::name() const +{ + return "newest"; +} + +bool +Func2::CreateNewest::path_preserving() const +{ + return false; +} + + +int +Func2::CreateNewest::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + struct stat st; + timespec newest; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + newest = {0,0}; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_,&st)) + error_and_continue(error,ENOENT); + if(chosen && (st.st_mtim < newest)) + continue; + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + newest = st.st_mtim; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_newest.hpp b/src/func_create_newest.hpp new file mode 100644 index 00000000..65505ef3 --- /dev/null +++ b/src/func_create_newest.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateNewest : public CreateBase + { + public: + CreateNewest() {} + ~CreateNewest() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_pfrd.cpp b/src/func_create_pfrd.cpp new file mode 100644 index 00000000..cd2b19bf --- /dev/null +++ b/src/func_create_pfrd.cpp @@ -0,0 +1,159 @@ +#include "func_create_pfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreatePFRD::name() const +{ + return "pfrd"; +} + +bool +Func2::CreatePFRD::path_preserving() const +{ + return false; +} + + +int +Func2::CreatePFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + struct W { Branch *b; u64 weight; }; + std::vector writable; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_pfrd.hpp b/src/func_create_pfrd.hpp new file mode 100644 index 00000000..1f3b0fab --- /dev/null +++ b/src/func_create_pfrd.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreatePFRD : public CreateBase + { + public: + CreatePFRD() {} + ~CreatePFRD() {} + + public: + std::string_view name() const; + bool path_preserving() const; + + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_create_rand.cpp b/src/func_create_rand.cpp new file mode 100644 index 00000000..40738cd8 --- /dev/null +++ b/src/func_create_rand.cpp @@ -0,0 +1,134 @@ +#include "func_create_rand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fileinfo.hpp" +#include "fs_acl.hpp" +#include "fs_open_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::CreateRAND::name() const +{ + return "rand"; +} + +bool +Func2::CreateRAND::path_preserving() const +{ + return false; +} + + +int +Func2::CreateRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + std::vector writable; + int fd; + fs::path fullpath; + mode_t mode; + FileInfo *fi; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + fd = fs::open_as(ugid_,fullpath,ffi_->flags,mode); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,chosen,fusepath_,ffi_->direct_io); + ffi_->fh = fi->to_fh(); + + return 0; +} diff --git a/src/func_create_rand.hpp b/src/func_create_rand.hpp new file mode 100644 index 00000000..c1e43bd6 --- /dev/null +++ b/src/func_create_rand.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_create_base.hpp" + +namespace Func2 +{ + class CreateRAND : public CreateBase + { + public: + CreateRAND() {} + ~CreateRAND() {} + public: + std::string_view name() const; + bool path_preserving() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_getattr.hpp b/src/func_getattr.hpp new file mode 100644 index 00000000..ce54fa1e --- /dev/null +++ b/src/func_getattr.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_getattr_base.hpp" +#include "func_getattr_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using GetAttr = FuncWrapper; +} diff --git a/src/func_getattr_base.hpp b/src/func_getattr_base.hpp new file mode 100644 index 00000000..784198b8 --- /dev/null +++ b/src/func_getattr_base.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "follow_symlinks_enum.hpp" + +#include "fs_path.hpp" +#include "branches.hpp" +#include "base_types.h" + +#include "fuse.h" + +#include + +#include +#include +#include + +namespace Func2 +{ + class GetAttrBase + { + public: + GetAttrBase() {} + ~GetAttrBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + struct stat *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout) = 0; + }; +} diff --git a/src/func_getattr_cdco.cpp b/src/func_getattr_cdco.cpp new file mode 100644 index 00000000..781ca2d2 --- /dev/null +++ b/src/func_getattr_cdco.cpp @@ -0,0 +1,78 @@ +#include "func_getattr_cdco.hpp" + +#include "fs_inode.hpp" +#include "fs_stat.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::GetAttrCDCO::name() const +{ + return "cdco"; +} + +int +Func2::GetAttrCDCO::operator()(const Branches &branches_, + const fs::path &fusepath_, + struct stat *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *first_branch; + + err = 0; + first_branch = nullptr; + for(const auto &branch : branches_) + { + struct stat tmp_st; + + fullpath = branch.path / fusepath_; + + rv = fs::stat(fullpath,&tmp_st,follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(not first_branch) + { + *st_ = tmp_st; + first_branch = &branch; + continue; + } + + // This upgrades the uid:gid because mergerfs now does most + // file interaction as root and relies on `default_permissions` + // to manage permissions. Want to ensure that root owned files + // can't be changed so we treat them all as root owned. + if(tmp_st.st_uid == 0) + st_->st_uid = 0; + if(tmp_st.st_gid == 0) + st_->st_gid = 0; + st_->st_atim = TimeSpec::newest(st_->st_atim,tmp_st.st_atim); + st_->st_ctim = TimeSpec::newest(st_->st_ctim,tmp_st.st_ctim); + st_->st_mtim = TimeSpec::newest(st_->st_mtim,tmp_st.st_mtim); + st_->st_nlink += tmp_st.st_nlink; + } + + if(!first_branch) + return err; + + if(symlinkify_) + { + fullpath = first_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(first_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_getattr_cdco.hpp b/src/func_getattr_cdco.hpp new file mode 100644 index 00000000..6ae1919d --- /dev/null +++ b/src/func_getattr_cdco.hpp @@ -0,0 +1,22 @@ +#include "func_getattr_base.hpp" + +namespace Func2 +{ + class GetAttrCDCO : public GetAttrBase + { + public: + GetAttrCDCO() {} + ~GetAttrCDCO() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + struct stat *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_getattr_cdfo.cpp b/src/func_getattr_cdfo.cpp new file mode 100644 index 00000000..8474ff5a --- /dev/null +++ b/src/func_getattr_cdfo.cpp @@ -0,0 +1,80 @@ +#include "func_getattr_cdfo.hpp" + +#include "fs_inode.hpp" +#include "fs_stat.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::GetAttrCDFO::name() const +{ + return "cdfo"; +} + +int +Func2::GetAttrCDFO::operator()(const Branches &branches_, + const fs::path &fusepath_, + struct stat *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *first_branch; + + err = 0; + first_branch = nullptr; + for(const auto &branch : branches_) + { + struct stat tmp_st; + + fullpath = branch.path / fusepath_; + + rv = fs::stat(fullpath,&tmp_st,follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(not first_branch) + { + *st_ = tmp_st; + first_branch = &branch; + if(not S_ISDIR(tmp_st.st_mode)) + break; + continue; + } + + // This upgrades the uid:gid because mergerfs now does most + // file interaction as root and relies on `default_permissions` + // to manage permissions. Want to ensure that root owned files + // can't be changed so we treat them all as root owned. + if(tmp_st.st_uid == 0) + st_->st_uid = 0; + if(tmp_st.st_gid == 0) + st_->st_gid = 0; + st_->st_atim = TimeSpec::newest(st_->st_atim,tmp_st.st_atim); + st_->st_ctim = TimeSpec::newest(st_->st_ctim,tmp_st.st_ctim); + st_->st_mtim = TimeSpec::newest(st_->st_mtim,tmp_st.st_mtim); + st_->st_nlink += tmp_st.st_nlink; + } + + if(!first_branch) + return err; + + if(symlinkify_) + { + fullpath = first_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(first_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_getattr_cdfo.hpp b/src/func_getattr_cdfo.hpp new file mode 100644 index 00000000..67c4816c --- /dev/null +++ b/src/func_getattr_cdfo.hpp @@ -0,0 +1,22 @@ +#include "func_getattr_base.hpp" + +namespace Func2 +{ + class GetAttrCDFO : public GetAttrBase + { + public: + GetAttrCDFO() {} + ~GetAttrCDFO() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + struct stat *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_getattr_factory.cpp b/src/func_getattr_factory.cpp new file mode 100644 index 00000000..cdc7cbc0 --- /dev/null +++ b/src/func_getattr_factory.cpp @@ -0,0 +1,24 @@ +#include "func_getattr_factory.hpp" + +#include "func_getattr_cdfo.hpp" +#include "func_getattr_cdco.hpp" +#include "func_getattr_ff.hpp" +#include "func_getattr_newest.hpp" + + +bool +Func2::GetAttrFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::GetAttrFactory::make(const std::string_view str_) +{ + if(str_ == "cdfo") return std::make_shared(); + if(str_ == "cdco") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + + return {}; +} diff --git a/src/func_getattr_factory.hpp b/src/func_getattr_factory.hpp new file mode 100644 index 00000000..1e085980 --- /dev/null +++ b/src/func_getattr_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_getattr_base.hpp" + +namespace Func2 +{ + class GetAttrFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_getattr_ff.cpp b/src/func_getattr_ff.cpp new file mode 100644 index 00000000..b4e5eb43 --- /dev/null +++ b/src/func_getattr_ff.cpp @@ -0,0 +1,46 @@ +#include "func_getattr_ff.hpp" + +#include "errno.hpp" +#include "fs_inode.hpp" +#include "fs_stat.hpp" +#include "symlinkify.hpp" + + +std::string_view +Func2::GetAttrFF::name() const +{ + return "ff"; +} + +int +Func2::GetAttrFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + struct stat *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int rv = fs::stat(fullpath,st_,follow_symlinks_); + if(rv == -ENOENT) + continue; + if(rv < 0) + return rv; + + if(symlinkify_) + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + + fs::inode::calc(branch.path,fusepath_,st_); + + return 0; + } + + return -ENOENT; +} diff --git a/src/func_getattr_ff.hpp b/src/func_getattr_ff.hpp new file mode 100644 index 00000000..5e9da20a --- /dev/null +++ b/src/func_getattr_ff.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "func_getattr_base.hpp" + +namespace Func2 +{ + class GetAttrFF : public GetAttrBase + { + public: + GetAttrFF() {} + ~GetAttrFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + struct stat *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_getattr_newest.cpp b/src/func_getattr_newest.cpp new file mode 100644 index 00000000..51ae7834 --- /dev/null +++ b/src/func_getattr_newest.cpp @@ -0,0 +1,63 @@ +#include "func_getattr_newest.hpp" + +#include "fs_inode.hpp" +#include "fs_stat.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::GetAttrNewest::name() const +{ + return "newest"; +} + +int +Func2::GetAttrNewest::operator()(const Branches &branches_, + const fs::path &fusepath_, + struct stat *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *newest_branch; + + err = 0; + newest_branch = nullptr; + for(const auto &branch : branches_) + { + struct stat tmp_st; + + fullpath = branch.path / fusepath_; + rv = fs::stat(fullpath,&tmp_st,follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(newest_branch && (st_->st_mtim > tmp_st.st_mtim)) + continue; + + *st_ = tmp_st; + newest_branch = &branch; + } + + if(!newest_branch) + return err; + + if(symlinkify_) + { + fullpath = newest_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(newest_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_getattr_newest.hpp b/src/func_getattr_newest.hpp new file mode 100644 index 00000000..3ef094bc --- /dev/null +++ b/src/func_getattr_newest.hpp @@ -0,0 +1,22 @@ +#include "func_getattr_base.hpp" + +namespace Func2 +{ + class GetAttrNewest : public GetAttrBase + { + public: + GetAttrNewest() {} + ~GetAttrNewest() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + struct stat *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_getxattr.hpp b/src/func_getxattr.hpp new file mode 100644 index 00000000..e5c41986 --- /dev/null +++ b/src/func_getxattr.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "func_getxattr_base.hpp" +#include "func_getxattr_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Getxattr = FuncWrapper; +} diff --git a/src/func_getxattr_base.hpp b/src/func_getxattr_base.hpp new file mode 100644 index 00000000..563ac6be --- /dev/null +++ b/src/func_getxattr_base.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class GetxattrBase + { + public: + GetxattrBase() {} + ~GetxattrBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname, + char *attrval, + const size_t attrval_size) = 0; + }; +} diff --git a/src/func_getxattr_erofs.cpp b/src/func_getxattr_erofs.cpp new file mode 100644 index 00000000..2883a17d --- /dev/null +++ b/src/func_getxattr_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_getxattr_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::GetxattrEROFS::name() const { return "erofs"; } +int Func2::GetxattrEROFS::operator()(const Branches&,const fs::path&,const char*,char*,const size_t) { return -EROFS; } diff --git a/src/func_getxattr_erofs.hpp b/src/func_getxattr_erofs.hpp new file mode 100644 index 00000000..ce7afcea --- /dev/null +++ b/src/func_getxattr_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_getxattr_base.hpp" +namespace Func2 { + class GetxattrEROFS : public GetxattrBase { + public: + GetxattrEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const char*,char*,const size_t); + }; +} diff --git a/src/func_getxattr_factory.cpp b/src/func_getxattr_factory.cpp new file mode 100644 index 00000000..944e7668 --- /dev/null +++ b/src/func_getxattr_factory.cpp @@ -0,0 +1,20 @@ +#include "func_getxattr_factory.hpp" + +#include "func_getxattr_ff.hpp" +#include "func_getxattr_erofs.hpp" + + +bool +Func2::GetxattrFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::GetxattrFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_getxattr_factory.hpp b/src/func_getxattr_factory.hpp new file mode 100644 index 00000000..0d8c7c87 --- /dev/null +++ b/src/func_getxattr_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_getxattr_base.hpp" + +namespace Func2 +{ + class GetxattrFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_getxattr_ff.cpp b/src/func_getxattr_ff.cpp new file mode 100644 index 00000000..ecee4644 --- /dev/null +++ b/src/func_getxattr_ff.cpp @@ -0,0 +1,126 @@ +#include "func_getxattr_ff.hpp" + +#include "config.hpp" +#include "errno.hpp" +#include "fs_exists.hpp" +#include "fs_findallfiles.hpp" +#include "fs_lgetxattr.hpp" +#include "str.hpp" + +#include + + +static +int +_from_string(char *destbuf_, + const size_t destbufsize_, + const std::string &src_) +{ + const size_t srcbufsize = src_.size(); + + if(destbufsize_ == 0) + return srcbufsize; + + if(srcbufsize > destbufsize_) + return -ERANGE; + + memcpy(destbuf_,src_.data(),srcbufsize); + + return srcbufsize; +} + +static +int +_user_mergerfs_allpaths(const Branches &branches_, + const fs::path &fusepath_, + char *buf_, + const size_t count_) +{ + std::string concated; + StrVec paths; + StrVec branchpaths; + + for(const auto &branch : branches_) + branchpaths.push_back(branch.path); + + fs::findallfiles(branchpaths,fusepath_,&paths); + + concated = str::join(paths,'\0'); + + return ::_from_string(buf_,count_,concated); +} + +static +int +_user_mergerfs(const fs::path &basepath_, + const fs::path &fusepath_, + const fs::path &fullpath_, + const Branches &branches_, + const char *attrname_, + char *attrval_, + const size_t attrvalsize_) +{ + const std::string_view key = Config::prune_ctrl_xattr(attrname_); + + if(key == "basepath") + return ::_from_string(attrval_,attrvalsize_,basepath_); + if(key == "relpath") + return ::_from_string(attrval_,attrvalsize_,fusepath_); + if(key == "fullpath") + return ::_from_string(attrval_,attrvalsize_,fullpath_); + if(key == "allpaths") + return ::_user_mergerfs_allpaths(branches_,fusepath_,attrval_,attrvalsize_); + + return -ENOATTR; +} + + +std::string_view +Func2::GetxattrFF::name() const +{ + return "ff"; +} + +int +Func2::GetxattrFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + const char *attrname_, + char *attrval_, + const size_t attrvalsize_) +{ + fs::path fullpath; + + if(Config::is_mergerfs_xattr(attrname_)) + { + for(const auto &branch : branches_) + { + if(!fs::exists(branch.path,fusepath_)) + continue; + + fullpath = branch.path / fusepath_; + + return ::_user_mergerfs(branch.path, + fusepath_, + fullpath, + branches_, + attrname_, + attrval_, + attrvalsize_); + } + + return -ENOENT; + } + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int rv = fs::lgetxattr(fullpath,attrname_,attrval_,attrvalsize_); + if(rv == -ENOENT) + continue; + + return rv; + } + + return -ENOENT; +} diff --git a/src/func_getxattr_ff.hpp b/src/func_getxattr_ff.hpp new file mode 100644 index 00000000..1f36ca10 --- /dev/null +++ b/src/func_getxattr_ff.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_getxattr_base.hpp" + +namespace Func2 +{ + class GetxattrFF : public GetxattrBase + { + public: + GetxattrFF() {} + ~GetxattrFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname, + char *attrval, + const size_t attrvalsize); + }; +} diff --git a/src/func_ioctl.hpp b/src/func_ioctl.hpp new file mode 100644 index 00000000..3bd9b23f --- /dev/null +++ b/src/func_ioctl.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "func_ioctl_base.hpp" +#include "func_ioctl_factory.hpp" + +#include "func_wrapper.hpp" + +namespace Func2 +{ + using Ioctl = FuncWrapper; +} diff --git a/src/func_ioctl_base.hpp b/src/func_ioctl_base.hpp new file mode 100644 index 00000000..5f391af1 --- /dev/null +++ b/src/func_ioctl_base.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +#include + +namespace Func2 +{ + class IoctlBase + { + public: + IoctlBase() {} + ~IoctlBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const uint32_t &cmd, + void *&data, + uint32_t *&out_bufsz) = 0; + }; +} diff --git a/src/func_ioctl_erofs.cpp b/src/func_ioctl_erofs.cpp new file mode 100644 index 00000000..dd3fe383 --- /dev/null +++ b/src/func_ioctl_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_ioctl_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::IoctlEROFS::name() const { return "erofs"; } +int Func2::IoctlEROFS::operator()(const Branches&,const fs::path&,const uint32_t&,void*&,uint32_t*&) { return -EROFS; } diff --git a/src/func_ioctl_erofs.hpp b/src/func_ioctl_erofs.hpp new file mode 100644 index 00000000..3e306aa1 --- /dev/null +++ b/src/func_ioctl_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_ioctl_base.hpp" +namespace Func2 { + class IoctlEROFS : public IoctlBase { + public: + IoctlEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const uint32_t&,void*&,uint32_t*&); + }; +} diff --git a/src/func_ioctl_factory.cpp b/src/func_ioctl_factory.cpp new file mode 100644 index 00000000..d6fd5e60 --- /dev/null +++ b/src/func_ioctl_factory.cpp @@ -0,0 +1,20 @@ +#include "func_ioctl_factory.hpp" + +#include "func_ioctl_ff.hpp" +#include "func_ioctl_erofs.hpp" + + +bool +Func2::IoctlFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::IoctlFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_ioctl_factory.hpp b/src/func_ioctl_factory.hpp new file mode 100644 index 00000000..353df41b --- /dev/null +++ b/src/func_ioctl_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_ioctl_base.hpp" + +namespace Func2 +{ + class IoctlFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_ioctl_ff.cpp b/src/func_ioctl_ff.cpp new file mode 100644 index 00000000..a89111c1 --- /dev/null +++ b/src/func_ioctl_ff.cpp @@ -0,0 +1,91 @@ +#include "func_ioctl_ff.hpp" + +#include "endian.hpp" +#include "errno.hpp" +#include "fs_close.hpp" +#include "fs_ioctl.hpp" +#include "fs_open.hpp" + +// From linux/btrfs.h +#define BTRFS_IOCTL_MAGIC 0x94 + +#ifndef FS_IOC_GETFLAGS +# define FS_IOC_GETFLAGS _IOR('f',1,long) +#endif + +#ifndef FS_IOC_SETFLAGS +# define FS_IOC_SETFLAGS _IOW('f',2,long) +#endif + +#ifndef FS_IOC_GETVERSION +# define FS_IOC_GETVERSION _IOR('v',1,long) +#endif + +#ifndef FS_IOC_SETVERSION +# define FS_IOC_SETVERSION _IOW('v',2,long) +#endif + +#ifndef O_NOATIME +#define O_NOATIME 0 +#endif + +static +int +_ioctl(const int fd_, + const uint32_t cmd_, + void *data_, + uint32_t *out_bufsz_) +{ + int rv; + + switch(cmd_) + { + case FS_IOC_GETFLAGS: + case FS_IOC_SETFLAGS: + case FS_IOC_GETVERSION: + case FS_IOC_SETVERSION: + if(endian::is_big() && (sizeof(long) != sizeof(int))) + return -ENOTTY; + if((data_ != NULL) && (*out_bufsz_ > 4)) + *out_bufsz_ = 4; + break; + } + + rv = fs::ioctl(fd_,cmd_,data_); + + return rv; +} + + +std::string_view +Func2::IoctlFF::name() const +{ + return "ff"; +} + +int +Func2::IoctlFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + const uint32_t &cmd_, + void *&data_, + uint32_t *&out_bufsz_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int fd = fs::open(fullpath,O_RDONLY|O_NOATIME|O_NONBLOCK); + if(fd == -ENOENT) + continue; + if(fd < 0) + return fd; + + const int rv = ::_ioctl(fd,cmd_,data_,out_bufsz_); + fs::close(fd); + return rv; + } + + return -ENOENT; +} diff --git a/src/func_ioctl_ff.hpp b/src/func_ioctl_ff.hpp new file mode 100644 index 00000000..4fae10b2 --- /dev/null +++ b/src/func_ioctl_ff.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_ioctl_base.hpp" + +namespace Func2 +{ + class IoctlFF : public IoctlBase + { + public: + IoctlFF() {} + ~IoctlFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const uint32_t &cmd, + void *&data, + uint32_t *&out_bufsz); + }; +} diff --git a/src/func_link.hpp b/src/func_link.hpp new file mode 100644 index 00000000..176451ae --- /dev/null +++ b/src/func_link.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_link_base.hpp" +#include "func_link_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Link = FuncWrapper; +} diff --git a/src/func_link_base.hpp b/src/func_link_base.hpp new file mode 100644 index 00000000..715a6938 --- /dev/null +++ b/src/func_link_base.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" + +namespace Func2 +{ + class LinkBase + { + public: + LinkBase() {} + ~LinkBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &oldfusepath, + const fs::path &newfusepath) = 0; + }; +} diff --git a/src/func_link_epall.cpp b/src/func_link_epall.cpp new file mode 100644 index 00000000..93f87673 --- /dev/null +++ b/src/func_link_epall.cpp @@ -0,0 +1,76 @@ +#include "func_link_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_link.hpp" + + +std::string_view +Func2::LinkEPAll::name() const +{ + return "epall"; +} + +int +Func2::LinkEPAll::operator()(const Branches &branches_, + const fs::path &oldfusepath_, + const fs::path &newfusepath_) +{ + // Snapshot the Branches::Impl once; iterating the live Branches object + // each time would release the shared_ptr at iterator destruction and + // could leave Branch* pointers dangling if cfg.branches is mutated + // concurrently. + Branches::Ptr branches = branches_; + int err; + bool found; + fs::path oldfullpath; + fs::path newfullpath; + const fs::path newfusedirpath = newfusepath_.parent_path(); + const Branch *clone_src = nullptr; + + err = 0; + found = false; + for(auto &branch : *branches) + { + if(branch.ro()) + continue; + + oldfullpath = branch.path / oldfusepath_; + newfullpath = branch.path / newfusepath_; + + int rv = fs::link(oldfullpath,newfullpath); + if(rv == -ENOENT) + { + if(!fs::exists(branch.path,oldfusepath_)) + continue; + + if(clone_src == nullptr) + { + for(auto &b : *branches) + { + if(fs::exists(b.path,newfusedirpath)) + { + clone_src = &b; + break; + } + } + } + + if(clone_src != nullptr) + { + rv = fs::clonepath(clone_src->path,branch.path,newfusedirpath); + if(rv >= 0) + rv = fs::link(oldfullpath,newfullpath); + } + } + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_link_epall.hpp b/src/func_link_epall.hpp new file mode 100644 index 00000000..7bf9c6e5 --- /dev/null +++ b/src/func_link_epall.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_link_base.hpp" + +namespace Func2 +{ + class LinkEPAll : public LinkBase + { + public: + LinkEPAll() {} + ~LinkEPAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &oldfusepath, + const fs::path &newfusepath); + }; +} diff --git a/src/func_link_erofs.cpp b/src/func_link_erofs.cpp new file mode 100644 index 00000000..ebc4306c --- /dev/null +++ b/src/func_link_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_link_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::LinkEROFS::name() const { return "erofs"; } +int Func2::LinkEROFS::operator()(const Branches&,const fs::path&,const fs::path&) { return -EROFS; } diff --git a/src/func_link_erofs.hpp b/src/func_link_erofs.hpp new file mode 100644 index 00000000..f9b6f1d0 --- /dev/null +++ b/src/func_link_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_link_base.hpp" +namespace Func2 { + class LinkEROFS : public LinkBase { + public: + LinkEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const fs::path&); + }; +} diff --git a/src/func_link_factory.cpp b/src/func_link_factory.cpp new file mode 100644 index 00000000..0233cf4d --- /dev/null +++ b/src/func_link_factory.cpp @@ -0,0 +1,20 @@ +#include "func_link_factory.hpp" + +#include "func_link_epall.hpp" +#include "func_link_erofs.hpp" + + +bool +Func2::LinkFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::LinkFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_link_factory.hpp b/src/func_link_factory.hpp new file mode 100644 index 00000000..e7291c8a --- /dev/null +++ b/src/func_link_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class LinkBase; + + class LinkFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} diff --git a/src/func_listxattr.hpp b/src/func_listxattr.hpp new file mode 100644 index 00000000..82084df7 --- /dev/null +++ b/src/func_listxattr.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "func_listxattr_base.hpp" +#include "func_listxattr_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Listxattr = FuncWrapper; +} diff --git a/src/func_listxattr_all.cpp b/src/func_listxattr_all.cpp new file mode 100644 index 00000000..38a97bc4 --- /dev/null +++ b/src/func_listxattr_all.cpp @@ -0,0 +1,85 @@ +#include "func_listxattr_all.hpp" + +#include "errno.hpp" +#include "fs_llistxattr.hpp" + +#include +#include +#include + + +std::string_view +Func2::ListxattrAll::name() const +{ + return "all"; +} + +// listxattr=all: aggregate attribute names across every branch that +// has the file, deduplicating. Names are collected in a std::set so +// the result is sorted and stable across calls. Two passes per +// branch: size query then read into buffer. +ssize_t +Func2::ListxattrAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + char *list_, + const size_t size_) +{ + std::set names; + fs::path fullpath; + bool any = false; + ssize_t branch_err = -ENOENT; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + ssize_t need = fs::llistxattr(fullpath,NULL,0); + if(need == -ENOENT) + continue; + if(need < 0) + { + branch_err = need; + continue; + } + any = true; + if(need == 0) + continue; + + std::string buf(need,'\0'); + const ssize_t got = fs::llistxattr(fullpath,buf.data(),need); + if(got < 0) + continue; + + const char *p = buf.data(); + const char *end = p + got; + while(p < end) + { + const size_t len = ::strnlen(p,end - p); + names.emplace(p,len); + p += len + 1; + } + } + + if(!any) + return branch_err; + + size_t total = 0; + for(const auto &n : names) + total += n.size() + 1; + + if(size_ == 0) + return (ssize_t)total; + + if(total > size_) + return -ERANGE; + + char *out = list_; + for(const auto &n : names) + { + std::memcpy(out,n.data(),n.size()); + out[n.size()] = '\0'; + out += n.size() + 1; + } + + return (ssize_t)total; +} diff --git a/src/func_listxattr_all.hpp b/src/func_listxattr_all.hpp new file mode 100644 index 00000000..00696918 --- /dev/null +++ b/src/func_listxattr_all.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_listxattr_base.hpp" + +namespace Func2 +{ + class ListxattrAll : public ListxattrBase + { + public: + ListxattrAll() {} + ~ListxattrAll() {} + + public: + std::string_view name() const; + + public: + ssize_t operator()(const Branches &branches, + const fs::path &fusepath, + char *list, + const size_t size); + }; +} diff --git a/src/func_listxattr_base.hpp b/src/func_listxattr_base.hpp new file mode 100644 index 00000000..46dbe089 --- /dev/null +++ b/src/func_listxattr_base.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class ListxattrBase + { + public: + ListxattrBase() {} + ~ListxattrBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual ssize_t operator()(const Branches &branches, + const fs::path &fusepath, + char *list, + const size_t size) = 0; + }; +} diff --git a/src/func_listxattr_erofs.cpp b/src/func_listxattr_erofs.cpp new file mode 100644 index 00000000..0f078477 --- /dev/null +++ b/src/func_listxattr_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_listxattr_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::ListxattrEROFS::name() const { return "erofs"; } +ssize_t Func2::ListxattrEROFS::operator()(const Branches&,const fs::path&,char*,const size_t) { return -EROFS; } diff --git a/src/func_listxattr_erofs.hpp b/src/func_listxattr_erofs.hpp new file mode 100644 index 00000000..6ee3cc5a --- /dev/null +++ b/src/func_listxattr_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_listxattr_base.hpp" +namespace Func2 { + class ListxattrEROFS : public ListxattrBase { + public: + ListxattrEROFS() {} + std::string_view name() const; + ssize_t operator()(const Branches&,const fs::path&,char*,const size_t); + }; +} diff --git a/src/func_listxattr_factory.cpp b/src/func_listxattr_factory.cpp new file mode 100644 index 00000000..a015915c --- /dev/null +++ b/src/func_listxattr_factory.cpp @@ -0,0 +1,23 @@ +#include "func_listxattr_factory.hpp" + +#include "func_listxattr_all.hpp" +#include "func_listxattr_erofs.hpp" +#include "func_listxattr_ff.hpp" + + +bool +Func2::ListxattrFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::ListxattrFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_listxattr_factory.hpp b/src/func_listxattr_factory.hpp new file mode 100644 index 00000000..52b13f17 --- /dev/null +++ b/src/func_listxattr_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_listxattr_base.hpp" + +namespace Func2 +{ + class ListxattrFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_listxattr_ff.cpp b/src/func_listxattr_ff.cpp new file mode 100644 index 00000000..9ccb0aa9 --- /dev/null +++ b/src/func_listxattr_ff.cpp @@ -0,0 +1,33 @@ +#include "func_listxattr_ff.hpp" + +#include "errno.hpp" +#include "fs_llistxattr.hpp" + + +std::string_view +Func2::ListxattrFF::name() const +{ + return "ff"; +} + +ssize_t +Func2::ListxattrFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + char *list_, + const size_t size_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const ssize_t rv = fs::llistxattr(fullpath,list_,size_); + if(rv == -ENOENT) + continue; + + return rv; + } + + return -ENOENT; +} diff --git a/src/func_listxattr_ff.hpp b/src/func_listxattr_ff.hpp new file mode 100644 index 00000000..e625927b --- /dev/null +++ b/src/func_listxattr_ff.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_listxattr_base.hpp" + +namespace Func2 +{ + class ListxattrFF : public ListxattrBase + { + public: + ListxattrFF() {} + ~ListxattrFF() {} + + public: + std::string_view name() const; + + public: + ssize_t operator()(const Branches &branches, + const fs::path &fusepath, + char *list, + const size_t size); + }; +} diff --git a/src/func_mkdir.hpp b/src/func_mkdir.hpp new file mode 100644 index 00000000..ec301fe9 --- /dev/null +++ b/src/func_mkdir.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "func_mkdir_base.hpp" +#include "func_mkdir_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Mkdir = FuncWrapper; +} diff --git a/src/func_mkdir_all.cpp b/src/func_mkdir_all.cpp new file mode 100644 index 00000000..f10342be --- /dev/null +++ b/src/func_mkdir_all.cpp @@ -0,0 +1,113 @@ +#include "func_mkdir_all.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirAll::name() const +{ + return "all"; +} + +int +Func2::MkdirAll::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_all.hpp b/src/func_mkdir_all.hpp new file mode 100644 index 00000000..7640034d --- /dev/null +++ b/src/func_mkdir_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirAll : public MkdirBase + { + public: + MkdirAll() {} + ~MkdirAll() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_base.hpp b/src/func_mkdir_base.hpp new file mode 100644 index 00000000..b66c028d --- /dev/null +++ b/src/func_mkdir_base.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" +#include "ugid.hpp" + + +namespace Func2 +{ + class MkdirBase + { + public: + MkdirBase() {} + ~MkdirBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask) = 0; + }; +} diff --git a/src/func_mkdir_epall.cpp b/src/func_mkdir_epall.cpp new file mode 100644 index 00000000..8b10ef29 --- /dev/null +++ b/src/func_mkdir_epall.cpp @@ -0,0 +1,95 @@ +#include "func_mkdir_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPALL::name() const +{ + return "epall"; +} + +int +Func2::MkdirEPALL::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_epall.hpp b/src/func_mkdir_epall.hpp new file mode 100644 index 00000000..b88a4376 --- /dev/null +++ b/src/func_mkdir_epall.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPALL : public MkdirBase + { + public: + MkdirEPALL() {} + ~MkdirEPALL() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_epff.cpp b/src/func_mkdir_epff.cpp new file mode 100644 index 00000000..c614c9df --- /dev/null +++ b/src/func_mkdir_epff.cpp @@ -0,0 +1,95 @@ +#include "func_mkdir_epff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPFF::name() const +{ + return "epff"; +} + +int +Func2::MkdirEPFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_epff.hpp b/src/func_mkdir_epff.hpp new file mode 100644 index 00000000..45e88f2b --- /dev/null +++ b/src/func_mkdir_epff.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPFF : public MkdirBase + { + public: + MkdirEPFF() {} + ~MkdirEPFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_eplfs.cpp b/src/func_mkdir_eplfs.cpp new file mode 100644 index 00000000..d84e44ff --- /dev/null +++ b/src/func_mkdir_eplfs.cpp @@ -0,0 +1,101 @@ +#include "func_mkdir_eplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPLFS::name() const +{ + return "eplfs"; +} + +int +Func2::MkdirEPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lfs; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_eplfs.hpp b/src/func_mkdir_eplfs.hpp new file mode 100644 index 00000000..27fd896a --- /dev/null +++ b/src/func_mkdir_eplfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPLFS : public MkdirBase + { + public: + MkdirEPLFS() {} + ~MkdirEPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_eplus.cpp b/src/func_mkdir_eplus.cpp new file mode 100644 index 00000000..5330af75 --- /dev/null +++ b/src/func_mkdir_eplus.cpp @@ -0,0 +1,101 @@ +#include "func_mkdir_eplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPLUS::name() const +{ + return "eplus"; +} + +int +Func2::MkdirEPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lus; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_eplus.hpp b/src/func_mkdir_eplus.hpp new file mode 100644 index 00000000..c9de17b7 --- /dev/null +++ b/src/func_mkdir_eplus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPLUS : public MkdirBase + { + public: + MkdirEPLUS() {} + ~MkdirEPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_epmfs.cpp b/src/func_mkdir_epmfs.cpp new file mode 100644 index 00000000..9be23b2f --- /dev/null +++ b/src/func_mkdir_epmfs.cpp @@ -0,0 +1,100 @@ +#include "func_mkdir_epmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPMFS::name() const +{ + return "epmfs"; +} + +int +Func2::MkdirEPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 mfs; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_epmfs.hpp b/src/func_mkdir_epmfs.hpp new file mode 100644 index 00000000..38100513 --- /dev/null +++ b/src/func_mkdir_epmfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPMFS : public MkdirBase + { + public: + MkdirEPMFS() {} + ~MkdirEPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_eppfrd.cpp b/src/func_mkdir_eppfrd.cpp new file mode 100644 index 00000000..b44976a9 --- /dev/null +++ b/src/func_mkdir_eppfrd.cpp @@ -0,0 +1,123 @@ +#include "func_mkdir_eppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPPFRD::name() const +{ + return "eppfrd"; +} + +int +Func2::MkdirEPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_eppfrd.hpp b/src/func_mkdir_eppfrd.hpp new file mode 100644 index 00000000..56aa950e --- /dev/null +++ b/src/func_mkdir_eppfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPPFRD : public MkdirBase + { + public: + MkdirEPPFRD() {} + ~MkdirEPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_eprand.cpp b/src/func_mkdir_eprand.cpp new file mode 100644 index 00000000..de36dcb5 --- /dev/null +++ b/src/func_mkdir_eprand.cpp @@ -0,0 +1,98 @@ +#include "func_mkdir_eprand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirEPRAND::name() const +{ + return "eprand"; +} + +int +Func2::MkdirEPRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + std::vector writable; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_eprand.hpp b/src/func_mkdir_eprand.hpp new file mode 100644 index 00000000..70de3d0a --- /dev/null +++ b/src/func_mkdir_eprand.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEPRAND : public MkdirBase + { + public: + MkdirEPRAND() {} + ~MkdirEPRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_erofs.cpp b/src/func_mkdir_erofs.cpp new file mode 100644 index 00000000..fbce14c2 --- /dev/null +++ b/src/func_mkdir_erofs.cpp @@ -0,0 +1,26 @@ +#include "func_mkdir_erofs.hpp" + +#include "errno.hpp" + + +std::string_view +Func2::MkdirEROFS::name() const +{ + return "erofs"; +} + +int +Func2::MkdirEROFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + (void)ugid_; + (void)branches_; + (void)fusepath_; + (void)mode_; + (void)umask_; + + return -EROFS; +} diff --git a/src/func_mkdir_erofs.hpp b/src/func_mkdir_erofs.hpp new file mode 100644 index 00000000..0b0b0c7a --- /dev/null +++ b/src/func_mkdir_erofs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirEROFS : public MkdirBase + { + public: + MkdirEROFS() {} + ~MkdirEROFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_factory.cpp b/src/func_mkdir_factory.cpp new file mode 100644 index 00000000..c4b0c5ff --- /dev/null +++ b/src/func_mkdir_factory.cpp @@ -0,0 +1,58 @@ +#include "func_mkdir_factory.hpp" + +#include "func_mkdir_all.hpp" +#include "func_mkdir_epall.hpp" +#include "func_mkdir_epff.hpp" +#include "func_mkdir_eplfs.hpp" +#include "func_mkdir_eplus.hpp" +#include "func_mkdir_epmfs.hpp" +#include "func_mkdir_eppfrd.hpp" +#include "func_mkdir_eprand.hpp" +#include "func_mkdir_erofs.hpp" +#include "func_mkdir_ff.hpp" +#include "func_mkdir_lfs.hpp" +#include "func_mkdir_lup.hpp" +#include "func_mkdir_lus.hpp" +#include "func_mkdir_mfs.hpp" +#include "func_mkdir_msplfs.hpp" +#include "func_mkdir_msplus.hpp" +#include "func_mkdir_mspmfs.hpp" +#include "func_mkdir_msppfrd.hpp" +#include "func_mkdir_newest.hpp" +#include "func_mkdir_pfrd.hpp" +#include "func_mkdir_rand.hpp" + + +bool +Func2::MkdirFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::MkdirFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "epff") return std::make_shared(); + if(str_ == "eplfs") return std::make_shared(); + if(str_ == "eplus") return std::make_shared(); + if(str_ == "epmfs") return std::make_shared(); + if(str_ == "eppfrd") return std::make_shared(); + if(str_ == "eprand") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "lfs") return std::make_shared(); + if(str_ == "lup") return std::make_shared(); + if(str_ == "lus") return std::make_shared(); + if(str_ == "mfs") return std::make_shared(); + if(str_ == "msplfs") return std::make_shared(); + if(str_ == "msplus") return std::make_shared(); + if(str_ == "mspmfs") return std::make_shared(); + if(str_ == "msppfrd") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + if(str_ == "pfrd") return std::make_shared(); + if(str_ == "rand") return std::make_shared(); + + return {}; +} diff --git a/src/func_mkdir_factory.hpp b/src/func_mkdir_factory.hpp new file mode 100644 index 00000000..310cae13 --- /dev/null +++ b/src/func_mkdir_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class MkdirBase; + + class MkdirFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} diff --git a/src/func_mkdir_ff.cpp b/src/func_mkdir_ff.cpp new file mode 100644 index 00000000..fc30f414 --- /dev/null +++ b/src/func_mkdir_ff.cpp @@ -0,0 +1,113 @@ +#include "func_mkdir_ff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirFF::name() const +{ + return "ff"; +} + +int +Func2::MkdirFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_ff.hpp b/src/func_mkdir_ff.hpp new file mode 100644 index 00000000..ea64501b --- /dev/null +++ b/src/func_mkdir_ff.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirFF : public MkdirBase + { + public: + MkdirFF() {} + ~MkdirFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_lfs.cpp b/src/func_mkdir_lfs.cpp new file mode 100644 index 00000000..8d987a97 --- /dev/null +++ b/src/func_mkdir_lfs.cpp @@ -0,0 +1,119 @@ +#include "func_mkdir_lfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirLFS::name() const +{ + return "lfs"; +} + +int +Func2::MkdirLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lfs; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_lfs.hpp b/src/func_mkdir_lfs.hpp new file mode 100644 index 00000000..915057eb --- /dev/null +++ b/src/func_mkdir_lfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirLFS : public MkdirBase + { + public: + MkdirLFS() {} + ~MkdirLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_lup.cpp b/src/func_mkdir_lup.cpp new file mode 100644 index 00000000..b43f966c --- /dev/null +++ b/src/func_mkdir_lup.cpp @@ -0,0 +1,127 @@ +#include "func_mkdir_lup.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirLUP::name() const +{ + return "lup"; +} + +int +Func2::MkdirLUP::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + long double best_ratio; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + best_ratio = 2.0L; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + { + u64 used = info.spaceused; + u64 total = info.spaceused + info.spaceavail; + long double ratio; + + if(total == 0) + { used = 0; total = 1; } + ratio = (long double)used / (long double)total; + if(ratio >= best_ratio) + continue; + + best_ratio = ratio; + chosen = &branch; + } + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_lup.hpp b/src/func_mkdir_lup.hpp new file mode 100644 index 00000000..f07f619f --- /dev/null +++ b/src/func_mkdir_lup.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirLUP : public MkdirBase + { + public: + MkdirLUP() {} + ~MkdirLUP() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_lus.cpp b/src/func_mkdir_lus.cpp new file mode 100644 index 00000000..03f2ffa8 --- /dev/null +++ b/src/func_mkdir_lus.cpp @@ -0,0 +1,119 @@ +#include "func_mkdir_lus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirLUS::name() const +{ + return "lus"; +} + +int +Func2::MkdirLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lus; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_lus.hpp b/src/func_mkdir_lus.hpp new file mode 100644 index 00000000..390d37ea --- /dev/null +++ b/src/func_mkdir_lus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirLUS : public MkdirBase + { + public: + MkdirLUS() {} + ~MkdirLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_mfs.cpp b/src/func_mkdir_mfs.cpp new file mode 100644 index 00000000..648351ff --- /dev/null +++ b/src/func_mkdir_mfs.cpp @@ -0,0 +1,118 @@ +#include "func_mkdir_mfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirMFS::name() const +{ + return "mfs"; +} + +int +Func2::MkdirMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 mfs; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_mfs.hpp b/src/func_mkdir_mfs.hpp new file mode 100644 index 00000000..30b6c84b --- /dev/null +++ b/src/func_mkdir_mfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirMFS : public MkdirBase + { + public: + MkdirMFS() {} + ~MkdirMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_msplfs.cpp b/src/func_mkdir_msplfs.cpp new file mode 100644 index 00000000..25698af5 --- /dev/null +++ b/src/func_mkdir_msplfs.cpp @@ -0,0 +1,153 @@ +#include "func_mkdir_msplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirMSPLFS::name() const +{ + return "msplfs"; +} + +int +Func2::MkdirMSPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t mode; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lfs; + + any = false; + lfs = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail > lfs) + continue; + lfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_msplfs.hpp b/src/func_mkdir_msplfs.hpp new file mode 100644 index 00000000..22e483e5 --- /dev/null +++ b/src/func_mkdir_msplfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirMSPLFS : public MkdirBase + { + public: + MkdirMSPLFS() {} + ~MkdirMSPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_msplus.cpp b/src/func_mkdir_msplus.cpp new file mode 100644 index 00000000..7d6a6ca5 --- /dev/null +++ b/src/func_mkdir_msplus.cpp @@ -0,0 +1,153 @@ +#include "func_mkdir_msplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirMSPLUS::name() const +{ + return "msplus"; +} + +int +Func2::MkdirMSPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t mode; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lus; + + any = false; + lus = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceused > lus) + continue; + lus = info.spaceused; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_msplus.hpp b/src/func_mkdir_msplus.hpp new file mode 100644 index 00000000..ef7abd71 --- /dev/null +++ b/src/func_mkdir_msplus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirMSPLUS : public MkdirBase + { + public: + MkdirMSPLUS() {} + ~MkdirMSPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_mspmfs.cpp b/src/func_mkdir_mspmfs.cpp new file mode 100644 index 00000000..6df076d5 --- /dev/null +++ b/src/func_mkdir_mspmfs.cpp @@ -0,0 +1,153 @@ +#include "func_mkdir_mspmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirMSPMFS::name() const +{ + return "mspmfs"; +} + +int +Func2::MkdirMSPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t mode; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 mfs; + + any = false; + mfs = 0; + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail < mfs) + continue; + mfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_mspmfs.hpp b/src/func_mkdir_mspmfs.hpp new file mode 100644 index 00000000..9d01b896 --- /dev/null +++ b/src/func_mkdir_mspmfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirMSPMFS : public MkdirBase + { + public: + MkdirMSPMFS() {} + ~MkdirMSPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_msppfrd.cpp b/src/func_mkdir_msppfrd.cpp new file mode 100644 index 00000000..2d40dd87 --- /dev/null +++ b/src/func_mkdir_msppfrd.cpp @@ -0,0 +1,176 @@ +#include "func_mkdir_msppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirMSPPFRD::name() const +{ + return "msppfrd"; +} + +int +Func2::MkdirMSPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t mode; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + u64 sum; + struct W { Branch *b; u64 weight; }; + std::vector writable; + + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(!writable.empty()) + { + if(sum == 0) + { + chosen = writable.front().b; + } + else + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + break; + } + + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_msppfrd.hpp b/src/func_mkdir_msppfrd.hpp new file mode 100644 index 00000000..497dd49b --- /dev/null +++ b/src/func_mkdir_msppfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirMSPPFRD : public MkdirBase + { + public: + MkdirMSPPFRD() {} + ~MkdirMSPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_newest.cpp b/src/func_mkdir_newest.cpp new file mode 100644 index 00000000..a52cbb55 --- /dev/null +++ b/src/func_mkdir_newest.cpp @@ -0,0 +1,121 @@ +#include "func_mkdir_newest.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "timespec_utils.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirNewest::name() const +{ + return "newest"; +} + +int +Func2::MkdirNewest::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + struct stat st; + timespec newest; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + newest = {0,0}; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_,&st)) + error_and_continue(error,ENOENT); + if(chosen && (st.st_mtim < newest)) + continue; + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + newest = st.st_mtim; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_newest.hpp b/src/func_mkdir_newest.hpp new file mode 100644 index 00000000..e54b7ff5 --- /dev/null +++ b/src/func_mkdir_newest.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirNewest : public MkdirBase + { + public: + MkdirNewest() {} + ~MkdirNewest() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_pfrd.cpp b/src/func_mkdir_pfrd.cpp new file mode 100644 index 00000000..714502f9 --- /dev/null +++ b/src/func_mkdir_pfrd.cpp @@ -0,0 +1,141 @@ +#include "func_mkdir_pfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirPFRD::name() const +{ + return "pfrd"; +} + +int +Func2::MkdirPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_pfrd.hpp b/src/func_mkdir_pfrd.hpp new file mode 100644 index 00000000..bbaa4291 --- /dev/null +++ b/src/func_mkdir_pfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirPFRD : public MkdirBase + { + public: + MkdirPFRD() {} + ~MkdirPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mkdir_rand.cpp b/src/func_mkdir_rand.cpp new file mode 100644 index 00000000..d8347bd2 --- /dev/null +++ b/src/func_mkdir_rand.cpp @@ -0,0 +1,116 @@ +#include "func_mkdir_rand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_acl.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MkdirRAND::name() const +{ + return "rand"; +} + +int +Func2::MkdirRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t &mode_, + const mode_t &umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + std::vector writable; + fs::path fullpath; + mode_t mode; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + mode = mode_; + if(!fs::acl::dir_has_defaults(fullpath)) + mode &= ~umask_; + + return fs::mkdir_as(ugid_,fullpath,mode); +} diff --git a/src/func_mkdir_rand.hpp b/src/func_mkdir_rand.hpp new file mode 100644 index 00000000..8a9ed29b --- /dev/null +++ b/src/func_mkdir_rand.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_mkdir_base.hpp" + +namespace Func2 +{ + class MkdirRAND : public MkdirBase + { + public: + MkdirRAND() {} + ~MkdirRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t &mode, + const mode_t &umask); + }; +} diff --git a/src/func_mknod.hpp b/src/func_mknod.hpp new file mode 100644 index 00000000..32f08fad --- /dev/null +++ b/src/func_mknod.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_mknod_base.hpp" +#include "func_mknod_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Mknod = FuncWrapper; +} diff --git a/src/func_mknod_all.cpp b/src/func_mknod_all.cpp new file mode 100644 index 00000000..8e8e166e --- /dev/null +++ b/src/func_mknod_all.cpp @@ -0,0 +1,109 @@ +#include "func_mknod_all.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodAll::name() const +{ + return "all"; +} + +int +Func2::MknodAll::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_all.hpp b/src/func_mknod_all.hpp new file mode 100644 index 00000000..2922db41 --- /dev/null +++ b/src/func_mknod_all.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodAll : public MknodBase + { + public: + MknodAll() {} + ~MknodAll() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_base.hpp b/src/func_mknod_base.hpp new file mode 100644 index 00000000..147fdbf5 --- /dev/null +++ b/src/func_mknod_base.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" +#include "ugid.hpp" + +#include +#include + +namespace Func2 +{ + class MknodBase + { + public: + MknodBase() {} + ~MknodBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask) = 0; + }; +} diff --git a/src/func_mknod_epall.cpp b/src/func_mknod_epall.cpp new file mode 100644 index 00000000..84a63058 --- /dev/null +++ b/src/func_mknod_epall.cpp @@ -0,0 +1,91 @@ +#include "func_mknod_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPALL::name() const +{ + return "epall"; +} + +int +Func2::MknodEPALL::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_epall.hpp b/src/func_mknod_epall.hpp new file mode 100644 index 00000000..b32fcc22 --- /dev/null +++ b/src/func_mknod_epall.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPALL : public MknodBase + { + public: + MknodEPALL() {} + ~MknodEPALL() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_epff.cpp b/src/func_mknod_epff.cpp new file mode 100644 index 00000000..68b95a8b --- /dev/null +++ b/src/func_mknod_epff.cpp @@ -0,0 +1,91 @@ +#include "func_mknod_epff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPFF::name() const +{ + return "epff"; +} + +int +Func2::MknodEPFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_epff.hpp b/src/func_mknod_epff.hpp new file mode 100644 index 00000000..14a8baf4 --- /dev/null +++ b/src/func_mknod_epff.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPFF : public MknodBase + { + public: + MknodEPFF() {} + ~MknodEPFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_eplfs.cpp b/src/func_mknod_eplfs.cpp new file mode 100644 index 00000000..7e9485e7 --- /dev/null +++ b/src/func_mknod_eplfs.cpp @@ -0,0 +1,97 @@ +#include "func_mknod_eplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPLFS::name() const +{ + return "eplfs"; +} + +int +Func2::MknodEPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_eplfs.hpp b/src/func_mknod_eplfs.hpp new file mode 100644 index 00000000..a71f2d4a --- /dev/null +++ b/src/func_mknod_eplfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPLFS : public MknodBase + { + public: + MknodEPLFS() {} + ~MknodEPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_eplus.cpp b/src/func_mknod_eplus.cpp new file mode 100644 index 00000000..2a8e1201 --- /dev/null +++ b/src/func_mknod_eplus.cpp @@ -0,0 +1,97 @@ +#include "func_mknod_eplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPLUS::name() const +{ + return "eplus"; +} + +int +Func2::MknodEPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lus; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_eplus.hpp b/src/func_mknod_eplus.hpp new file mode 100644 index 00000000..334d5bf4 --- /dev/null +++ b/src/func_mknod_eplus.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPLUS : public MknodBase + { + public: + MknodEPLUS() {} + ~MknodEPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_epmfs.cpp b/src/func_mknod_epmfs.cpp new file mode 100644 index 00000000..c0b4bf37 --- /dev/null +++ b/src/func_mknod_epmfs.cpp @@ -0,0 +1,96 @@ +#include "func_mknod_epmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPMFS::name() const +{ + return "epmfs"; +} + +int +Func2::MknodEPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 mfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_epmfs.hpp b/src/func_mknod_epmfs.hpp new file mode 100644 index 00000000..e668b859 --- /dev/null +++ b/src/func_mknod_epmfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPMFS : public MknodBase + { + public: + MknodEPMFS() {} + ~MknodEPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_eppfrd.cpp b/src/func_mknod_eppfrd.cpp new file mode 100644 index 00000000..99df911d --- /dev/null +++ b/src/func_mknod_eppfrd.cpp @@ -0,0 +1,119 @@ +#include "func_mknod_eppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPPFRD::name() const +{ + return "eppfrd"; +} + +int +Func2::MknodEPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_eppfrd.hpp b/src/func_mknod_eppfrd.hpp new file mode 100644 index 00000000..2fc15134 --- /dev/null +++ b/src/func_mknod_eppfrd.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPPFRD : public MknodBase + { + public: + MknodEPPFRD() {} + ~MknodEPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_eprand.cpp b/src/func_mknod_eprand.cpp new file mode 100644 index 00000000..5308c41c --- /dev/null +++ b/src/func_mknod_eprand.cpp @@ -0,0 +1,94 @@ +#include "func_mknod_eprand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodEPRAND::name() const +{ + return "eprand"; +} + +int +Func2::MknodEPRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_eprand.hpp b/src/func_mknod_eprand.hpp new file mode 100644 index 00000000..c32b062d --- /dev/null +++ b/src/func_mknod_eprand.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEPRAND : public MknodBase + { + public: + MknodEPRAND() {} + ~MknodEPRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_erofs.cpp b/src/func_mknod_erofs.cpp new file mode 100644 index 00000000..09c06eb0 --- /dev/null +++ b/src/func_mknod_erofs.cpp @@ -0,0 +1,28 @@ +#include "func_mknod_erofs.hpp" + +#include "errno.hpp" + + +std::string_view +Func2::MknodEROFS::name() const +{ + return "erofs"; +} + +int +Func2::MknodEROFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + (void)ugid_; + (void)branches_; + (void)fusepath_; + (void)mode_; + (void)dev_; + (void)umask_; + + return -EROFS; +} diff --git a/src/func_mknod_erofs.hpp b/src/func_mknod_erofs.hpp new file mode 100644 index 00000000..a93a6bfd --- /dev/null +++ b/src/func_mknod_erofs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodEROFS : public MknodBase + { + public: + MknodEROFS() {} + ~MknodEROFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_factory.cpp b/src/func_mknod_factory.cpp new file mode 100644 index 00000000..df113e4b --- /dev/null +++ b/src/func_mknod_factory.cpp @@ -0,0 +1,58 @@ +#include "func_mknod_factory.hpp" + +#include "func_mknod_all.hpp" +#include "func_mknod_epall.hpp" +#include "func_mknod_epff.hpp" +#include "func_mknod_eplfs.hpp" +#include "func_mknod_eplus.hpp" +#include "func_mknod_epmfs.hpp" +#include "func_mknod_eppfrd.hpp" +#include "func_mknod_eprand.hpp" +#include "func_mknod_erofs.hpp" +#include "func_mknod_ff.hpp" +#include "func_mknod_lfs.hpp" +#include "func_mknod_lup.hpp" +#include "func_mknod_lus.hpp" +#include "func_mknod_mfs.hpp" +#include "func_mknod_msplfs.hpp" +#include "func_mknod_msplus.hpp" +#include "func_mknod_mspmfs.hpp" +#include "func_mknod_msppfrd.hpp" +#include "func_mknod_newest.hpp" +#include "func_mknod_pfrd.hpp" +#include "func_mknod_rand.hpp" + + +bool +Func2::MknodFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::MknodFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "epff") return std::make_shared(); + if(str_ == "eplfs") return std::make_shared(); + if(str_ == "eplus") return std::make_shared(); + if(str_ == "epmfs") return std::make_shared(); + if(str_ == "eppfrd") return std::make_shared(); + if(str_ == "eprand") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "lfs") return std::make_shared(); + if(str_ == "lup") return std::make_shared(); + if(str_ == "lus") return std::make_shared(); + if(str_ == "mfs") return std::make_shared(); + if(str_ == "msplfs") return std::make_shared(); + if(str_ == "msplus") return std::make_shared(); + if(str_ == "mspmfs") return std::make_shared(); + if(str_ == "msppfrd") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + if(str_ == "pfrd") return std::make_shared(); + if(str_ == "rand") return std::make_shared(); + + return {}; +} diff --git a/src/func_mknod_factory.hpp b/src/func_mknod_factory.hpp new file mode 100644 index 00000000..53be363d --- /dev/null +++ b/src/func_mknod_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class MknodBase; + + class MknodFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} \ No newline at end of file diff --git a/src/func_mknod_ff.cpp b/src/func_mknod_ff.cpp new file mode 100644 index 00000000..2dca9692 --- /dev/null +++ b/src/func_mknod_ff.cpp @@ -0,0 +1,109 @@ +#include "func_mknod_ff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodFF::name() const +{ + return "ff"; +} + +int +Func2::MknodFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_ff.hpp b/src/func_mknod_ff.hpp new file mode 100644 index 00000000..307f903a --- /dev/null +++ b/src/func_mknod_ff.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodFF : public MknodBase + { + public: + MknodFF() {} + ~MknodFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_lfs.cpp b/src/func_mknod_lfs.cpp new file mode 100644 index 00000000..5c46a160 --- /dev/null +++ b/src/func_mknod_lfs.cpp @@ -0,0 +1,115 @@ +#include "func_mknod_lfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodLFS::name() const +{ + return "lfs"; +} + +int +Func2::MknodLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_lfs.hpp b/src/func_mknod_lfs.hpp new file mode 100644 index 00000000..ca991014 --- /dev/null +++ b/src/func_mknod_lfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodLFS : public MknodBase + { + public: + MknodLFS() {} + ~MknodLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_lup.cpp b/src/func_mknod_lup.cpp new file mode 100644 index 00000000..177aced1 --- /dev/null +++ b/src/func_mknod_lup.cpp @@ -0,0 +1,123 @@ +#include "func_mknod_lup.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodLUP::name() const +{ + return "lup"; +} + +int +Func2::MknodLUP::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + long double best_ratio; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + best_ratio = 2.0L; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + { + u64 used = info.spaceused; + u64 total = info.spaceused + info.spaceavail; + long double ratio; + + if(total == 0) + { used = 0; total = 1; } + ratio = (long double)used / (long double)total; + if(ratio >= best_ratio) + continue; + + best_ratio = ratio; + chosen = &branch; + } + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_lup.hpp b/src/func_mknod_lup.hpp new file mode 100644 index 00000000..1cdee620 --- /dev/null +++ b/src/func_mknod_lup.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodLUP : public MknodBase + { + public: + MknodLUP() {} + ~MknodLUP() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_lus.cpp b/src/func_mknod_lus.cpp new file mode 100644 index 00000000..97c9ee7b --- /dev/null +++ b/src/func_mknod_lus.cpp @@ -0,0 +1,115 @@ +#include "func_mknod_lus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodLUS::name() const +{ + return "lus"; +} + +int +Func2::MknodLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lus; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_lus.hpp b/src/func_mknod_lus.hpp new file mode 100644 index 00000000..8bcdbba8 --- /dev/null +++ b/src/func_mknod_lus.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodLUS : public MknodBase + { + public: + MknodLUS() {} + ~MknodLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_mfs.cpp b/src/func_mknod_mfs.cpp new file mode 100644 index 00000000..bc891e80 --- /dev/null +++ b/src/func_mknod_mfs.cpp @@ -0,0 +1,114 @@ +#include "func_mknod_mfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodMFS::name() const +{ + return "mfs"; +} + +int +Func2::MknodMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 mfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_mfs.hpp b/src/func_mknod_mfs.hpp new file mode 100644 index 00000000..e9425b77 --- /dev/null +++ b/src/func_mknod_mfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodMFS : public MknodBase + { + public: + MknodMFS() {} + ~MknodMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_msplfs.cpp b/src/func_mknod_msplfs.cpp new file mode 100644 index 00000000..1772df2c --- /dev/null +++ b/src/func_mknod_msplfs.cpp @@ -0,0 +1,150 @@ +#include "func_mknod_msplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodMSPLFS::name() const +{ + return "msplfs"; +} + +int +Func2::MknodMSPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lfs; + + any = false; + lfs = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail > lfs) + continue; + lfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_msplfs.hpp b/src/func_mknod_msplfs.hpp new file mode 100644 index 00000000..5e1aad73 --- /dev/null +++ b/src/func_mknod_msplfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodMSPLFS : public MknodBase + { + public: + MknodMSPLFS() {} + ~MknodMSPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_msplus.cpp b/src/func_mknod_msplus.cpp new file mode 100644 index 00000000..8c17cf1d --- /dev/null +++ b/src/func_mknod_msplus.cpp @@ -0,0 +1,150 @@ +#include "func_mknod_msplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodMSPLUS::name() const +{ + return "msplus"; +} + +int +Func2::MknodMSPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 lus; + + any = false; + lus = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceused > lus) + continue; + lus = info.spaceused; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_msplus.hpp b/src/func_mknod_msplus.hpp new file mode 100644 index 00000000..5c97181f --- /dev/null +++ b/src/func_mknod_msplus.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodMSPLUS : public MknodBase + { + public: + MknodMSPLUS() {} + ~MknodMSPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_mspmfs.cpp b/src/func_mknod_mspmfs.cpp new file mode 100644 index 00000000..43e5eaf9 --- /dev/null +++ b/src/func_mknod_mspmfs.cpp @@ -0,0 +1,150 @@ +#include "func_mknod_mspmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodMSPMFS::name() const +{ + return "mspmfs"; +} + +int +Func2::MknodMSPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + bool any; + u64 mfs; + + any = false; + mfs = 0; + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail < mfs) + continue; + mfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_mspmfs.hpp b/src/func_mknod_mspmfs.hpp new file mode 100644 index 00000000..05006143 --- /dev/null +++ b/src/func_mknod_mspmfs.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodMSPMFS : public MknodBase + { + public: + MknodMSPMFS() {} + ~MknodMSPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_msppfrd.cpp b/src/func_mknod_msppfrd.cpp new file mode 100644 index 00000000..5d09f466 --- /dev/null +++ b/src/func_mknod_msppfrd.cpp @@ -0,0 +1,173 @@ +#include "func_mknod_msppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodMSPPFRD::name() const +{ + return "msppfrd"; +} + +int +Func2::MknodMSPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = fusepath_.parent_path(); + + for(;;) + { + u64 sum; + struct W { Branch *b; u64 weight; }; + std::vector writable; + + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(!writable.empty()) + { + if(sum == 0) + { + chosen = writable.front().b; + } + else + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + break; + } + + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = fusepath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = (0777 & ~umask_); + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_msppfrd.hpp b/src/func_mknod_msppfrd.hpp new file mode 100644 index 00000000..df86e11c --- /dev/null +++ b/src/func_mknod_msppfrd.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodMSPPFRD : public MknodBase + { + public: + MknodMSPPFRD() {} + ~MknodMSPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_newest.cpp b/src/func_mknod_newest.cpp new file mode 100644 index 00000000..fc532110 --- /dev/null +++ b/src/func_mknod_newest.cpp @@ -0,0 +1,117 @@ +#include "func_mknod_newest.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "timespec_utils.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodNewest::name() const +{ + return "newest"; +} + +int +Func2::MknodNewest::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + struct stat st; + timespec newest; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + newest = {0,0}; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,fusepath_,&st)) + error_and_continue(error,ENOENT); + if(chosen && (st.st_mtim < newest)) + continue; + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + newest = st.st_mtim; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_newest.hpp b/src/func_mknod_newest.hpp new file mode 100644 index 00000000..4c41f62a --- /dev/null +++ b/src/func_mknod_newest.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodNewest : public MknodBase + { + public: + MknodNewest() {} + ~MknodNewest() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_pfrd.cpp b/src/func_mknod_pfrd.cpp new file mode 100644 index 00000000..6b5b5ad3 --- /dev/null +++ b/src/func_mknod_pfrd.cpp @@ -0,0 +1,137 @@ +#include "func_mknod_pfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodPFRD::name() const +{ + return "pfrd"; +} + +int +Func2::MknodPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_pfrd.hpp b/src/func_mknod_pfrd.hpp new file mode 100644 index 00000000..172f4a6b --- /dev/null +++ b/src/func_mknod_pfrd.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodPFRD : public MknodBase + { + public: + MknodPFRD() {} + ~MknodPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_mknod_rand.cpp b/src/func_mknod_rand.cpp new file mode 100644 index 00000000..df20d401 --- /dev/null +++ b/src/func_mknod_rand.cpp @@ -0,0 +1,112 @@ +#include "func_mknod_rand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_mknod_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::MknodRAND::name() const +{ + return "rand"; +} + +int +Func2::MknodRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const fs::path &fusepath_, + const mode_t mode_, + const dev_t dev_, + const mode_t umask_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,fusepath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,fusepath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / fusepath_; + + return fs::mknod_as(ugid_,fullpath,mode_,dev_,umask_); +} diff --git a/src/func_mknod_rand.hpp b/src/func_mknod_rand.hpp new file mode 100644 index 00000000..cee4c84e --- /dev/null +++ b/src/func_mknod_rand.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_mknod_base.hpp" + +namespace Func2 +{ + class MknodRAND : public MknodBase + { + public: + MknodRAND() {} + ~MknodRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const fs::path &fusepath, + const mode_t mode, + const dev_t dev, + const mode_t umask); + }; +} diff --git a/src/func_open.hpp b/src/func_open.hpp new file mode 100644 index 00000000..b8098663 --- /dev/null +++ b/src/func_open.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "func_open_base.hpp" +#include "func_open_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Open = FuncWrapper; +} diff --git a/src/func_open_base.hpp b/src/func_open_base.hpp new file mode 100644 index 00000000..659320fa --- /dev/null +++ b/src/func_open_base.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "branches.hpp" +#include "config_nfsopenhack.hpp" + +#include "fs_path.hpp" +#include "fuse.h" + +#include + + +namespace Func2 +{ + class OpenBase + { + public: + OpenBase() {} + ~OpenBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const bool &link_cow, + const NFSOpenHack &nfsopenhack) = 0; + }; +} diff --git a/src/func_open_erofs.cpp b/src/func_open_erofs.cpp new file mode 100644 index 00000000..a2d2a2a1 --- /dev/null +++ b/src/func_open_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_open_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::OpenEROFS::name() const { return "erofs"; } +int Func2::OpenEROFS::operator()(const Branches&,const fs::path&,fuse_file_info_t*&,const bool&,const NFSOpenHack&) { return -EROFS; } diff --git a/src/func_open_erofs.hpp b/src/func_open_erofs.hpp new file mode 100644 index 00000000..dcaf9059 --- /dev/null +++ b/src/func_open_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_open_base.hpp" +namespace Func2 { + class OpenEROFS : public OpenBase { + public: + OpenEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,fuse_file_info_t*&,const bool&,const NFSOpenHack&); + }; +} diff --git a/src/func_open_factory.cpp b/src/func_open_factory.cpp new file mode 100644 index 00000000..0b519dd0 --- /dev/null +++ b/src/func_open_factory.cpp @@ -0,0 +1,20 @@ +#include "func_open_factory.hpp" + +#include "func_open_ff.hpp" +#include "func_open_erofs.hpp" + + +bool +Func2::OpenFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::OpenFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_open_factory.hpp b/src/func_open_factory.hpp new file mode 100644 index 00000000..c36164e9 --- /dev/null +++ b/src/func_open_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_open_base.hpp" + +namespace Func2 +{ + class OpenFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_open_ff.cpp b/src/func_open_ff.cpp new file mode 100644 index 00000000..b76a664b --- /dev/null +++ b/src/func_open_ff.cpp @@ -0,0 +1,132 @@ +#include "func_open_ff.hpp" + +#include "errno.hpp" +#include "fileinfo.hpp" +#include "fs_cow.hpp" +#include "fs_fchmod.hpp" +#include "fs_lchmod.hpp" +#include "fs_lstat.hpp" +#include "fs_open.hpp" +#include "fs_openat.hpp" +#include "stat_util.hpp" + + +static +bool +_rdonly(const int flags_) +{ + return ((flags_ & O_ACCMODE) == O_RDONLY); +} + +static +int +_lchmod_and_open_if_not_writable_and_empty(const fs::path &fullpath_, + const int flags_) +{ + int rv; + struct stat st; + + rv = fs::lstat(fullpath_,&st); + if(rv < 0) + return -EACCES; + + if(StatUtil::writable(st)) + return -EACCES; + + rv = fs::lchmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP)); + if(rv < 0) + return -EACCES; + + rv = fs::open(fullpath_,flags_); + if(rv < 0) + return -EACCES; + + fs::fchmod(rv,st.st_mode); + + return rv; +} + +static +int +_nfsopenhack(const fs::path &fullpath_, + const int flags_, + const NFSOpenHack nfsopenhack_) +{ + switch(nfsopenhack_) + { + default: + case NFSOpenHack::ENUM::OFF: + return -EACCES; + case NFSOpenHack::ENUM::GIT: + if(::_rdonly(flags_)) + return -EACCES; + if(fullpath_.string().find("/.git/") == std::string::npos) + return -EACCES; + return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); + case NFSOpenHack::ENUM::ALL: + if(::_rdonly(flags_)) + return -EACCES; + return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); + } +} + +static +int +_open_path(const fs::path &fullpath_, + const Branch *branch_, + const fs::path &fusepath_, + fuse_file_info_t *ffi_, + const NFSOpenHack nfsopenhack_) +{ + int fd; + FileInfo *fi; + + fd = fs::openat(AT_FDCWD,fullpath_,ffi_->flags); + if(fd == -EACCES) + fd = ::_nfsopenhack(fullpath_,ffi_->flags,nfsopenhack_); + if(fd < 0) + return fd; + + fi = new FileInfo(fd,branch_,fusepath_,ffi_->direct_io); + + ffi_->fh = fi->to_fh(); + + return 0; +} + + +std::string_view +Func2::OpenFF::name() const +{ + return "ff"; +} + +int +Func2::OpenFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + fuse_file_info_t *&ffi_, + const bool &link_cow_, + const NFSOpenHack &nfsopenhack_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + if(link_cow_ && fs::cow::is_eligible(fullpath,ffi_->flags)) + fs::cow::break_link(fullpath); + + const int rv = ::_open_path(fullpath, + &branch, + fusepath_, + ffi_, + nfsopenhack_); + if(rv == -ENOENT) + continue; + + return rv; + } + + return -ENOENT; +} diff --git a/src/func_open_ff.hpp b/src/func_open_ff.hpp new file mode 100644 index 00000000..37b2c11b --- /dev/null +++ b/src/func_open_ff.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "func_open_base.hpp" + +namespace Func2 +{ + class OpenFF : public OpenBase + { + public: + OpenFF() {} + ~OpenFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + fuse_file_info_t *&ffi, + const bool &link_cow, + const NFSOpenHack &nfsopenhack); + }; +} diff --git a/src/func_readlink.hpp b/src/func_readlink.hpp new file mode 100644 index 00000000..5ab7a4ac --- /dev/null +++ b/src/func_readlink.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_readlink_base.hpp" +#include "func_readlink_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Readlink = FuncWrapper; +} diff --git a/src/func_readlink_base.hpp b/src/func_readlink_base.hpp new file mode 100644 index 00000000..2ad132e4 --- /dev/null +++ b/src/func_readlink_base.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class ReadlinkBase + { + public: + ReadlinkBase() {} + ~ReadlinkBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + char *buf, + const size_t bufsize, + const bool symlinkify, + const time_t symlinkify_timeout) = 0; + }; +} diff --git a/src/func_readlink_erofs.cpp b/src/func_readlink_erofs.cpp new file mode 100644 index 00000000..885ea979 --- /dev/null +++ b/src/func_readlink_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_readlink_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::ReadlinkEROFS::name() const { return "erofs"; } +int Func2::ReadlinkEROFS::operator()(const Branches&,const fs::path&,char*,const size_t,const bool,const time_t) { return -EROFS; } diff --git a/src/func_readlink_erofs.hpp b/src/func_readlink_erofs.hpp new file mode 100644 index 00000000..3e440b82 --- /dev/null +++ b/src/func_readlink_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_readlink_base.hpp" +namespace Func2 { + class ReadlinkEROFS : public ReadlinkBase { + public: + ReadlinkEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,char*,const size_t,const bool,const time_t); + }; +} diff --git a/src/func_readlink_factory.cpp b/src/func_readlink_factory.cpp new file mode 100644 index 00000000..fa32deb9 --- /dev/null +++ b/src/func_readlink_factory.cpp @@ -0,0 +1,20 @@ +#include "func_readlink_factory.hpp" + +#include "func_readlink_ff.hpp" +#include "func_readlink_erofs.hpp" + + +bool +Func2::ReadlinkFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::ReadlinkFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + + return {}; +} diff --git a/src/func_readlink_factory.hpp b/src/func_readlink_factory.hpp new file mode 100644 index 00000000..bdc833a3 --- /dev/null +++ b/src/func_readlink_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_readlink_base.hpp" + +namespace Func2 +{ + class ReadlinkFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_readlink_ff.cpp b/src/func_readlink_ff.cpp new file mode 100644 index 00000000..4a63a262 --- /dev/null +++ b/src/func_readlink_ff.cpp @@ -0,0 +1,67 @@ +#include "func_readlink_ff.hpp" + +#include "errno.hpp" +#include "fs_lstat.hpp" +#include "fs_readlink.hpp" +#include "symlinkify.hpp" + +#include +#include + + +std::string_view +Func2::ReadlinkFF::name() const +{ + return "ff"; +} + +int +Func2::ReadlinkFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + char *buf_, + const size_t bufsize_, + const bool symlinkify_, + const time_t symlinkify_timeout_) +{ + fs::path fullpath; + + if(!symlinkify_) + { + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int rv = fs::readlink(fullpath,buf_,bufsize_); + if(rv == -ENOENT) + continue; + + return rv; + } + + return -ENOENT; + } + + for(const auto &branch : branches_) + { + struct stat st; + + fullpath = branch.path / fusepath_; + + const int rv = fs::lstat(fullpath,&st); + if(rv == -ENOENT) + continue; + if(rv < 0) + return rv; + + if(!symlinkify::can_be_symlink(st,symlinkify_timeout_)) + return fs::readlink(fullpath,buf_,bufsize_); + + const std::string fullpath_str = fullpath.string(); + const size_t len = std::min(fullpath_str.size(),bufsize_); + memcpy(buf_,fullpath_str.c_str(),len); + + return len; + } + + return -ENOENT; +} diff --git a/src/func_readlink_ff.hpp b/src/func_readlink_ff.hpp new file mode 100644 index 00000000..a7e6499e --- /dev/null +++ b/src/func_readlink_ff.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "func_readlink_base.hpp" + +namespace Func2 +{ + class ReadlinkFF : public ReadlinkBase + { + public: + ReadlinkFF() {} + ~ReadlinkFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + char *buf, + const size_t bufsize, + const bool symlinkify, + const time_t symlinkify_timeout); + }; +} diff --git a/src/func_removexattr.hpp b/src/func_removexattr.hpp new file mode 100644 index 00000000..aef5e65e --- /dev/null +++ b/src/func_removexattr.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_removexattr_base.hpp" +#include "func_removexattr_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Removexattr = FuncWrapper; +} diff --git a/src/func_removexattr_all.cpp b/src/func_removexattr_all.cpp new file mode 100644 index 00000000..a54fc7b4 --- /dev/null +++ b/src/func_removexattr_all.cpp @@ -0,0 +1,42 @@ +#include "func_removexattr_all.hpp" + +#include "errno.hpp" +#include "fs_lremovexattr.hpp" + + +std::string_view +Func2::RemovexattrAll::name() const +{ + return "all"; +} + +int +Func2::RemovexattrAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const char *attrname_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::lremovexattr(fullpath,attrname_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_removexattr_all.hpp b/src/func_removexattr_all.hpp new file mode 100644 index 00000000..2e5b4008 --- /dev/null +++ b/src/func_removexattr_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_removexattr_base.hpp" + +namespace Func2 +{ + class RemovexattrAll : public RemovexattrBase + { + public: + RemovexattrAll() {} + ~RemovexattrAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname); + }; +} diff --git a/src/func_removexattr_base.hpp b/src/func_removexattr_base.hpp new file mode 100644 index 00000000..a9d6aa8f --- /dev/null +++ b/src/func_removexattr_base.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class RemovexattrBase + { + public: + RemovexattrBase() {} + ~RemovexattrBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname) = 0; + }; +} diff --git a/src/func_removexattr_erofs.cpp b/src/func_removexattr_erofs.cpp new file mode 100644 index 00000000..ea8b2fd5 --- /dev/null +++ b/src/func_removexattr_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_removexattr_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::RemovexattrEROFS::name() const { return "erofs"; } + +int +Func2::RemovexattrEROFS::operator()(const Branches&,const fs::path&,const char*) +{ + return -EROFS; +} diff --git a/src/func_removexattr_erofs.hpp b/src/func_removexattr_erofs.hpp new file mode 100644 index 00000000..dfd13874 --- /dev/null +++ b/src/func_removexattr_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_removexattr_base.hpp" +namespace Func2 { + class RemovexattrEROFS : public RemovexattrBase { + public: + RemovexattrEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const char*); + }; +} diff --git a/src/func_removexattr_factory.cpp b/src/func_removexattr_factory.cpp new file mode 100644 index 00000000..4305b840 --- /dev/null +++ b/src/func_removexattr_factory.cpp @@ -0,0 +1,20 @@ +#include "func_removexattr_factory.hpp" + +#include "func_removexattr_all.hpp" +#include "func_removexattr_erofs.hpp" + + +bool +Func2::RemovexattrFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::RemovexattrFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_removexattr_factory.hpp b/src/func_removexattr_factory.hpp new file mode 100644 index 00000000..6057550f --- /dev/null +++ b/src/func_removexattr_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_removexattr_base.hpp" + +namespace Func2 +{ + class RemovexattrFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_rename.hpp b/src/func_rename.hpp new file mode 100644 index 00000000..e0c15282 --- /dev/null +++ b/src/func_rename.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_rename_base.hpp" +#include "func_rename_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Rename = FuncWrapper; +} diff --git a/src/func_rename_base.hpp b/src/func_rename_base.hpp new file mode 100644 index 00000000..b3c2a52d --- /dev/null +++ b/src/func_rename_base.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" + +namespace Func2 +{ + class RenameBase + { + public: + RenameBase() {} + ~RenameBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &oldfusepath, + const fs::path &newfusepath) = 0; + }; +} diff --git a/src/func_rename_epall.cpp b/src/func_rename_epall.cpp new file mode 100644 index 00000000..5e65442c --- /dev/null +++ b/src/func_rename_epall.cpp @@ -0,0 +1,87 @@ +#include "func_rename_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_remove.hpp" +#include "fs_rename.hpp" + + +std::string_view +Func2::RenameEPAll::name() const +{ + return "epall"; +} + +int +Func2::RenameEPAll::operator()(const Branches &branches_, + const fs::path &oldfusepath_, + const fs::path &newfusepath_) +{ + // Snapshot the Branches::Impl once; iterating the live Branches each + // time would release the shared_ptr at iterator destruction and could + // leave Branch* pointers dangling if cfg.branches is mutated + // concurrently. + Branches::Ptr branches = branches_; + int err; + bool found; + StrVec toremove; + fs::path oldfullpath; + fs::path newfullpath; + const fs::path newfusedirpath = newfusepath_.parent_path(); + const Branch *clone_src = nullptr; + + err = 0; + found = false; + for(auto &branch : *branches) + { + if(branch.ro()) + continue; + + oldfullpath = branch.path / oldfusepath_; + newfullpath = branch.path / newfusepath_; + + if(!fs::exists(branch.path,oldfusepath_)) + { + toremove.push_back(newfullpath); + continue; + } + + int rv = fs::rename(oldfullpath,newfullpath); + if(rv == -ENOENT) + { + if(clone_src == nullptr) + { + for(auto &b : *branches) + { + if(fs::exists(b.path,newfusedirpath)) + { + clone_src = &b; + break; + } + } + } + + if(clone_src != nullptr) + { + rv = fs::clonepath(clone_src->path,branch.path,newfusedirpath); + if(rv >= 0) + rv = fs::rename(oldfullpath,newfullpath); + } + } + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + if(err == 0) + { + for(const auto &path : toremove) + fs::remove(path); + } + + return err; +} diff --git a/src/func_rename_epall.hpp b/src/func_rename_epall.hpp new file mode 100644 index 00000000..1c5a5430 --- /dev/null +++ b/src/func_rename_epall.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_rename_base.hpp" + +namespace Func2 +{ + class RenameEPAll : public RenameBase + { + public: + RenameEPAll() {} + ~RenameEPAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &oldfusepath, + const fs::path &newfusepath); + }; +} diff --git a/src/func_rename_erofs.cpp b/src/func_rename_erofs.cpp new file mode 100644 index 00000000..08cbf774 --- /dev/null +++ b/src/func_rename_erofs.cpp @@ -0,0 +1,4 @@ +#include "func_rename_erofs.hpp" +#include "errno.hpp" +std::string_view Func2::RenameEROFS::name() const { return "erofs"; } +int Func2::RenameEROFS::operator()(const Branches&,const fs::path&,const fs::path&) { return -EROFS; } diff --git a/src/func_rename_erofs.hpp b/src/func_rename_erofs.hpp new file mode 100644 index 00000000..81dd93ce --- /dev/null +++ b/src/func_rename_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_rename_base.hpp" +namespace Func2 { + class RenameEROFS : public RenameBase { + public: + RenameEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const fs::path&); + }; +} diff --git a/src/func_rename_factory.cpp b/src/func_rename_factory.cpp new file mode 100644 index 00000000..fd3f3255 --- /dev/null +++ b/src/func_rename_factory.cpp @@ -0,0 +1,20 @@ +#include "func_rename_factory.hpp" + +#include "func_rename_epall.hpp" +#include "func_rename_erofs.hpp" + + +bool +Func2::RenameFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::RenameFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_rename_factory.hpp b/src/func_rename_factory.hpp new file mode 100644 index 00000000..a2279e02 --- /dev/null +++ b/src/func_rename_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class RenameBase; + + class RenameFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} diff --git a/src/func_rmdir.hpp b/src/func_rmdir.hpp new file mode 100644 index 00000000..691ce495 --- /dev/null +++ b/src/func_rmdir.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "func_rmdir_base.hpp" +#include "func_rmdir_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Rmdir = FuncWrapper; +} diff --git a/src/func_rmdir_all.cpp b/src/func_rmdir_all.cpp new file mode 100644 index 00000000..57999135 --- /dev/null +++ b/src/func_rmdir_all.cpp @@ -0,0 +1,53 @@ +#include "func_rmdir_all.hpp" + +#include "config.hpp" +#include "errno.hpp" +#include "fs_rmdir.hpp" +#include "fs_unlink.hpp" + + +std::string_view +Func2::RmdirAll::name() const +{ + return "all"; +} + +static +bool +_should_unlink(int rv_) +{ + return ((rv_ == -ENOTDIR) && + (cfg.follow_symlinks != FollowSymlinks::ENUM::NEVER)); +} + +int +Func2::RmdirAll::operator()(const Branches &branches_, + const fs::path &fusepath_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + int rv = fs::rmdir(fullpath); + if(::_should_unlink(rv)) + rv = fs::unlink(fullpath); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_rmdir_all.hpp b/src/func_rmdir_all.hpp new file mode 100644 index 00000000..e5fd9c6d --- /dev/null +++ b/src/func_rmdir_all.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_rmdir_base.hpp" + +namespace Func2 +{ + class RmdirAll : public RmdirBase + { + public: + RmdirAll() {} + ~RmdirAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath); + }; +} diff --git a/src/func_rmdir_base.hpp b/src/func_rmdir_base.hpp new file mode 100644 index 00000000..8aaf5748 --- /dev/null +++ b/src/func_rmdir_base.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class RmdirBase + { + public: + RmdirBase() {} + ~RmdirBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath) = 0; + }; +} diff --git a/src/func_rmdir_erofs.cpp b/src/func_rmdir_erofs.cpp new file mode 100644 index 00000000..a1f8800b --- /dev/null +++ b/src/func_rmdir_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_rmdir_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::RmdirEROFS::name() const { return "erofs"; } + +int +Func2::RmdirEROFS::operator()(const Branches&,const fs::path&) +{ + return -EROFS; +} diff --git a/src/func_rmdir_erofs.hpp b/src/func_rmdir_erofs.hpp new file mode 100644 index 00000000..21b1298a --- /dev/null +++ b/src/func_rmdir_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_rmdir_base.hpp" +namespace Func2 { + class RmdirEROFS : public RmdirBase { + public: + RmdirEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&); + }; +} diff --git a/src/func_rmdir_factory.cpp b/src/func_rmdir_factory.cpp new file mode 100644 index 00000000..fbf24e2c --- /dev/null +++ b/src/func_rmdir_factory.cpp @@ -0,0 +1,20 @@ +#include "func_rmdir_factory.hpp" + +#include "func_rmdir_all.hpp" +#include "func_rmdir_erofs.hpp" + + +bool +Func2::RmdirFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::RmdirFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_rmdir_factory.hpp b/src/func_rmdir_factory.hpp new file mode 100644 index 00000000..962cc470 --- /dev/null +++ b/src/func_rmdir_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_rmdir_base.hpp" + +namespace Func2 +{ + class RmdirFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_setxattr.hpp b/src/func_setxattr.hpp new file mode 100644 index 00000000..13227109 --- /dev/null +++ b/src/func_setxattr.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_setxattr_base.hpp" +#include "func_setxattr_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Setxattr = FuncWrapper; +} diff --git a/src/func_setxattr_all.cpp b/src/func_setxattr_all.cpp new file mode 100644 index 00000000..ff0a93df --- /dev/null +++ b/src/func_setxattr_all.cpp @@ -0,0 +1,45 @@ +#include "func_setxattr_all.hpp" + +#include "errno.hpp" +#include "fs_lsetxattr.hpp" + + +std::string_view +Func2::SetxattrAll::name() const +{ + return "all"; +} + +int +Func2::SetxattrAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const char *attrname_, + const char *attrval_, + size_t attrvalsize_, + int flags_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::lsetxattr(fullpath,attrname_,attrval_,attrvalsize_,flags_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_setxattr_all.hpp b/src/func_setxattr_all.hpp new file mode 100644 index 00000000..ce2f02ea --- /dev/null +++ b/src/func_setxattr_all.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "func_setxattr_base.hpp" + +namespace Func2 +{ + class SetxattrAll : public SetxattrBase + { + public: + SetxattrAll() {} + ~SetxattrAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname_, + const char *attrval_, + size_t attrvalsize_, + int flags_); + }; +} diff --git a/src/func_setxattr_base.hpp b/src/func_setxattr_base.hpp new file mode 100644 index 00000000..5058a7e4 --- /dev/null +++ b/src/func_setxattr_base.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class SetxattrBase + { + public: + SetxattrBase() {} + ~SetxattrBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const char *attrname, + const char *attrval, + size_t attrvalsize, + int flags) = 0; + }; +} diff --git a/src/func_setxattr_erofs.cpp b/src/func_setxattr_erofs.cpp new file mode 100644 index 00000000..e4761998 --- /dev/null +++ b/src/func_setxattr_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_setxattr_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::SetxattrEROFS::name() const { return "erofs"; } + +int +Func2::SetxattrEROFS::operator()(const Branches&,const fs::path&,const char*,const char*,size_t,int) +{ + return -EROFS; +} diff --git a/src/func_setxattr_erofs.hpp b/src/func_setxattr_erofs.hpp new file mode 100644 index 00000000..4a3b98d2 --- /dev/null +++ b/src/func_setxattr_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_setxattr_base.hpp" +namespace Func2 { + class SetxattrEROFS : public SetxattrBase { + public: + SetxattrEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const char*,const char*,size_t,int); + }; +} diff --git a/src/func_setxattr_factory.cpp b/src/func_setxattr_factory.cpp new file mode 100644 index 00000000..8717365b --- /dev/null +++ b/src/func_setxattr_factory.cpp @@ -0,0 +1,20 @@ +#include "func_setxattr_factory.hpp" + +#include "func_setxattr_all.hpp" +#include "func_setxattr_erofs.hpp" + + +bool +Func2::SetxattrFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::SetxattrFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_setxattr_factory.hpp b/src/func_setxattr_factory.hpp new file mode 100644 index 00000000..99559f5d --- /dev/null +++ b/src/func_setxattr_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_setxattr_base.hpp" + +namespace Func2 +{ + class SetxattrFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_statx.hpp b/src/func_statx.hpp new file mode 100644 index 00000000..978d4bc7 --- /dev/null +++ b/src/func_statx.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "func_statx_base.hpp" +#include "func_statx_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Statx = FuncWrapper; +} diff --git a/src/func_statx_base.hpp b/src/func_statx_base.hpp new file mode 100644 index 00000000..eefa5a16 --- /dev/null +++ b/src/func_statx_base.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "follow_symlinks_enum.hpp" + +#include "fs_path.hpp" +#include "branches.hpp" +#include "base_types.h" + +#include "fuse.h" + +#include + +#include +#include +#include + + +namespace Func2 +{ + class StatxBase + { + public: + StatxBase() {} + ~StatxBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout) = 0; + }; +} diff --git a/src/func_statx_cdco.cpp b/src/func_statx_cdco.cpp new file mode 100644 index 00000000..ee67b71f --- /dev/null +++ b/src/func_statx_cdco.cpp @@ -0,0 +1,85 @@ +#include "func_statx_cdco.hpp" + +#include "fs_inode.hpp" +#include "fs_statx.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::StatxCDCO::name() const +{ + return "cdco"; +} + +int +Func2::StatxCDCO::operator()(const Branches &branches_, + const fs::path &fusepath_, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *first_branch; + + err = 0; + first_branch = nullptr; + for(const auto &branch : branches_) + { + struct fuse_statx tmp_st; + + fullpath = branch.path / fusepath_; + rv = fs::statx(AT_FDCWD, + fullpath, + flags_|AT_SYMLINK_NOFOLLOW, + mask_, + &tmp_st, + follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(!first_branch) + { + *st_ = tmp_st; + first_branch = &branch; + continue; + } + + // This upgrades the uid:gid because mergerfs now does most + // file interaction as root and relies on `default_permissions` + // to manage permissions. Want to ensure that root owned files + // can't be changed so we treat them all as root owned. + if(tmp_st.uid == 0) + st_->uid = 0; + if(tmp_st.gid == 0) + st_->gid = 0; + st_->atime = TimeSpec::newest(st_->atime,tmp_st.atime); + st_->ctime = TimeSpec::newest(st_->ctime,tmp_st.ctime); + st_->mtime = TimeSpec::newest(st_->mtime,tmp_st.mtime); + st_->btime = TimeSpec::newest(st_->btime,tmp_st.btime); + st_->nlink += tmp_st.nlink; + } + + if(!first_branch) + return err; + + if(symlinkify_) + { + fullpath = first_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(first_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_statx_cdco.hpp b/src/func_statx_cdco.hpp new file mode 100644 index 00000000..fbf59fa8 --- /dev/null +++ b/src/func_statx_cdco.hpp @@ -0,0 +1,24 @@ +#include "func_statx_base.hpp" + +namespace Func2 +{ + class StatxCDCO : public StatxBase + { + public: + StatxCDCO() {} + ~StatxCDCO() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_statx_cdfo.cpp b/src/func_statx_cdfo.cpp new file mode 100644 index 00000000..2c829775 --- /dev/null +++ b/src/func_statx_cdfo.cpp @@ -0,0 +1,88 @@ +#include "func_statx_cdfo.hpp" + +#include "fs_inode.hpp" +#include "fs_statx.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::StatxCDFO::name() const +{ + return "cdfo"; +} + +int +Func2::StatxCDFO::operator()(const Branches &branches_, + const fs::path &fusepath_, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *first_branch; + + err = 0; + first_branch = nullptr; + for(const auto &branch : branches_) + { + struct fuse_statx tmp_st; + + fullpath = branch.path / fusepath_; + + rv = fs::statx(AT_FDCWD, + fullpath, + flags_|AT_SYMLINK_NOFOLLOW, + mask_, + &tmp_st, + follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(!first_branch) + { + *st_ = tmp_st; + first_branch = &branch; + if(not S_ISDIR(tmp_st.mode)) + break; + continue; + } + + // This upgrades the uid:gid because mergerfs now does most + // file interaction as root and relies on `default_permissions` + // to manage permissions. Want to ensure that root owned files + // can't be changed so we treat them all as root owned. + if(tmp_st.uid == 0) + st_->uid = 0; + if(tmp_st.gid == 0) + st_->gid = 0; + st_->atime = TimeSpec::newest(st_->atime,tmp_st.atime); + st_->ctime = TimeSpec::newest(st_->ctime,tmp_st.ctime); + st_->mtime = TimeSpec::newest(st_->mtime,tmp_st.mtime); + st_->btime = TimeSpec::newest(st_->btime,tmp_st.btime); + st_->nlink += tmp_st.nlink; + } + + if(!first_branch) + return err; + + if(symlinkify_) + { + fullpath = first_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(first_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_statx_cdfo.hpp b/src/func_statx_cdfo.hpp new file mode 100644 index 00000000..0dbe3bdd --- /dev/null +++ b/src/func_statx_cdfo.hpp @@ -0,0 +1,24 @@ +#include "func_statx_base.hpp" + +namespace Func2 +{ + class StatxCDFO : public StatxBase + { + public: + StatxCDFO() {} + ~StatxCDFO() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_statx_factory.cpp b/src/func_statx_factory.cpp new file mode 100644 index 00000000..96e64eeb --- /dev/null +++ b/src/func_statx_factory.cpp @@ -0,0 +1,24 @@ +#include "func_statx_factory.hpp" + +#include "func_statx_cdfo.hpp" +#include "func_statx_cdco.hpp" +#include "func_statx_ff.hpp" +#include "func_statx_newest.hpp" + + +bool +Func2::StatxFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::StatxFactory::make(const std::string_view str_) +{ + if(str_ == "cdfo") return std::make_shared(); + if(str_ == "cdco") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + + return {}; +} diff --git a/src/func_statx_factory.hpp b/src/func_statx_factory.hpp new file mode 100644 index 00000000..50ae11e6 --- /dev/null +++ b/src/func_statx_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_statx_base.hpp" + +namespace Func2 +{ + class StatxFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_statx_ff.cpp b/src/func_statx_ff.cpp new file mode 100644 index 00000000..1df8577a --- /dev/null +++ b/src/func_statx_ff.cpp @@ -0,0 +1,53 @@ +#include "func_statx_ff.hpp" + +#include "errno.hpp" +#include "fs_inode.hpp" +#include "fs_statx.hpp" +#include "symlinkify.hpp" + + +std::string_view +Func2::StatxFF::name() const +{ + return "ff"; +} + +int +Func2::StatxFF::operator()(const Branches &branches_, + const fs::path &fusepath_, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + fs::path fullpath; + + for(const auto &branch : branches_) + { + fullpath = branch.path / fusepath_; + + const int rv = fs::statx(AT_FDCWD, + fullpath, + flags_|AT_SYMLINK_NOFOLLOW, + mask_, + st_, + follow_symlinks_); + if(rv == -ENOENT) + continue; + if(rv < 0) + return rv; + + if(symlinkify_) + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + + fs::inode::calc(branch.path,fusepath_,st_); + + return 0; + } + + return -ENOENT; +} diff --git a/src/func_statx_ff.hpp b/src/func_statx_ff.hpp new file mode 100644 index 00000000..ba9a8165 --- /dev/null +++ b/src/func_statx_ff.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "func_statx_base.hpp" + +namespace Func2 +{ + class StatxFF : public StatxBase + { + public: + StatxFF() {} + ~StatxFF() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_statx_newest.cpp b/src/func_statx_newest.cpp new file mode 100644 index 00000000..9b25dc79 --- /dev/null +++ b/src/func_statx_newest.cpp @@ -0,0 +1,70 @@ +#include "func_statx_newest.hpp" + +#include "fs_inode.hpp" +#include "fs_statx.hpp" +#include "symlinkify.hpp" +#include "timespec_utils.hpp" + + +std::string_view +Func2::StatxNewest::name() const +{ + return "newest"; +} + +int +Func2::StatxNewest::operator()(const Branches &branches_, + const fs::path &fusepath_, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st_, + const FollowSymlinksEnum follow_symlinks_, + const bool symlinkify_, + const s64 symlinkify_timeout_) +{ + int rv; + int err; + fs::path fullpath; + const Branch *newest_branch; + + err = 0; + newest_branch = nullptr; + for(const auto &branch : branches_) + { + struct fuse_statx tmp_st; + + fullpath = branch.path / fusepath_; + rv = fs::statx(AT_FDCWD, + fullpath, + flags_|AT_SYMLINK_NOFOLLOW, + mask_, + &tmp_st, + follow_symlinks_); + if(rv < 0) + { + err = rv; + continue; + } + + if(newest_branch && (st_->mtime > tmp_st.mtime)) + continue; + + *st_ = tmp_st; + newest_branch = &branch; + } + + if(!newest_branch) + return err; + + if(symlinkify_) + { + fullpath = newest_branch->path / fusepath_; + symlinkify::convert_if_can_be_symlink(fullpath, + st_, + symlinkify_timeout_); + } + + fs::inode::calc(newest_branch->path,fusepath_,st_); + + return 0; +} diff --git a/src/func_statx_newest.hpp b/src/func_statx_newest.hpp new file mode 100644 index 00000000..74833020 --- /dev/null +++ b/src/func_statx_newest.hpp @@ -0,0 +1,24 @@ +#include "func_statx_base.hpp" + +namespace Func2 +{ + class StatxNewest : public StatxBase + { + public: + StatxNewest() {} + ~StatxNewest() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const u32 flags_, + const u32 mask_, + struct fuse_statx *st, + const FollowSymlinksEnum follow_symlinks, + const bool symlinkify, + const s64 symlinkify_timeout); + }; +} diff --git a/src/func_symlink.hpp b/src/func_symlink.hpp new file mode 100644 index 00000000..95bd6927 --- /dev/null +++ b/src/func_symlink.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "func_symlink_base.hpp" +#include "func_symlink_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Symlink = FuncWrapper; +} diff --git a/src/func_symlink_all.cpp b/src/func_symlink_all.cpp new file mode 100644 index 00000000..5f928bff --- /dev/null +++ b/src/func_symlink_all.cpp @@ -0,0 +1,118 @@ +#include "func_symlink_all.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkAll::name() const +{ + return "all"; +} + +int +Func2::SymlinkAll::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_all.hpp b/src/func_symlink_all.hpp new file mode 100644 index 00000000..cce6b88a --- /dev/null +++ b/src/func_symlink_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkAll : public SymlinkBase + { + public: + SymlinkAll() {} + ~SymlinkAll() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_base.hpp b/src/func_symlink_base.hpp new file mode 100644 index 00000000..12fcd9b9 --- /dev/null +++ b/src/func_symlink_base.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "branches.hpp" +#include "fs_path.hpp" +#include "ugid.hpp" + +#include + + +namespace Func2 +{ + class SymlinkBase + { + public: + SymlinkBase() {} + ~SymlinkBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st) = 0; + }; +} diff --git a/src/func_symlink_epall.cpp b/src/func_symlink_epall.cpp new file mode 100644 index 00000000..ae2e8949 --- /dev/null +++ b/src/func_symlink_epall.cpp @@ -0,0 +1,100 @@ +#include "func_symlink_epall.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPALL::name() const +{ + return "epall"; +} + +int +Func2::SymlinkEPALL::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_epall.hpp b/src/func_symlink_epall.hpp new file mode 100644 index 00000000..40b96d54 --- /dev/null +++ b/src/func_symlink_epall.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPALL : public SymlinkBase + { + public: + SymlinkEPALL() {} + ~SymlinkEPALL() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_epff.cpp b/src/func_symlink_epff.cpp new file mode 100644 index 00000000..7841562c --- /dev/null +++ b/src/func_symlink_epff.cpp @@ -0,0 +1,100 @@ +#include "func_symlink_epff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPFF::name() const +{ + return "epff"; +} + +int +Func2::SymlinkEPFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_epff.hpp b/src/func_symlink_epff.hpp new file mode 100644 index 00000000..e0b1d5cd --- /dev/null +++ b/src/func_symlink_epff.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPFF : public SymlinkBase + { + public: + SymlinkEPFF() {} + ~SymlinkEPFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_eplfs.cpp b/src/func_symlink_eplfs.cpp new file mode 100644 index 00000000..daa09211 --- /dev/null +++ b/src/func_symlink_eplfs.cpp @@ -0,0 +1,106 @@ +#include "func_symlink_eplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPLFS::name() const +{ + return "eplfs"; +} + +int +Func2::SymlinkEPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_eplfs.hpp b/src/func_symlink_eplfs.hpp new file mode 100644 index 00000000..2d3c8b10 --- /dev/null +++ b/src/func_symlink_eplfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPLFS : public SymlinkBase + { + public: + SymlinkEPLFS() {} + ~SymlinkEPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_eplus.cpp b/src/func_symlink_eplus.cpp new file mode 100644 index 00000000..1daaf89b --- /dev/null +++ b/src/func_symlink_eplus.cpp @@ -0,0 +1,106 @@ +#include "func_symlink_eplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPLUS::name() const +{ + return "eplus"; +} + +int +Func2::SymlinkEPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 lus; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_eplus.hpp b/src/func_symlink_eplus.hpp new file mode 100644 index 00000000..8c31338b --- /dev/null +++ b/src/func_symlink_eplus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPLUS : public SymlinkBase + { + public: + SymlinkEPLUS() {} + ~SymlinkEPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_epmfs.cpp b/src/func_symlink_epmfs.cpp new file mode 100644 index 00000000..b0cbcda1 --- /dev/null +++ b/src/func_symlink_epmfs.cpp @@ -0,0 +1,105 @@ +#include "func_symlink_epmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPMFS::name() const +{ + return "epmfs"; +} + +int +Func2::SymlinkEPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + u64 mfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_epmfs.hpp b/src/func_symlink_epmfs.hpp new file mode 100644 index 00000000..dabd650f --- /dev/null +++ b/src/func_symlink_epmfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPMFS : public SymlinkBase + { + public: + SymlinkEPMFS() {} + ~SymlinkEPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_eppfrd.cpp b/src/func_symlink_eppfrd.cpp new file mode 100644 index 00000000..96c95213 --- /dev/null +++ b/src/func_symlink_eppfrd.cpp @@ -0,0 +1,128 @@ +#include "func_symlink_eppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPPFRD::name() const +{ + return "eppfrd"; +} + +int +Func2::SymlinkEPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_eppfrd.hpp b/src/func_symlink_eppfrd.hpp new file mode 100644 index 00000000..15e0379e --- /dev/null +++ b/src/func_symlink_eppfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPPFRD : public SymlinkBase + { + public: + SymlinkEPPFRD() {} + ~SymlinkEPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_eprand.cpp b/src/func_symlink_eprand.cpp new file mode 100644 index 00000000..9b6fd054 --- /dev/null +++ b/src/func_symlink_eprand.cpp @@ -0,0 +1,103 @@ +#include "func_symlink_eprand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkEPRAND::name() const +{ + return "eprand"; +} + +int +Func2::SymlinkEPRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_.parent_path())) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_eprand.hpp b/src/func_symlink_eprand.hpp new file mode 100644 index 00000000..e80693d2 --- /dev/null +++ b/src/func_symlink_eprand.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEPRAND : public SymlinkBase + { + public: + SymlinkEPRAND() {} + ~SymlinkEPRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_erofs.cpp b/src/func_symlink_erofs.cpp new file mode 100644 index 00000000..b2e2be70 --- /dev/null +++ b/src/func_symlink_erofs.cpp @@ -0,0 +1,26 @@ +#include "func_symlink_erofs.hpp" + +#include "errno.hpp" + + +std::string_view +Func2::SymlinkEROFS::name() const +{ + return "erofs"; +} + +int +Func2::SymlinkEROFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + (void)ugid_; + (void)branches_; + (void)target_; + (void)linkpath_; + (void)st_; + + return -EROFS; +} diff --git a/src/func_symlink_erofs.hpp b/src/func_symlink_erofs.hpp new file mode 100644 index 00000000..79891452 --- /dev/null +++ b/src/func_symlink_erofs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkEROFS : public SymlinkBase + { + public: + SymlinkEROFS() {} + ~SymlinkEROFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_factory.cpp b/src/func_symlink_factory.cpp new file mode 100644 index 00000000..539d50bc --- /dev/null +++ b/src/func_symlink_factory.cpp @@ -0,0 +1,58 @@ +#include "func_symlink_factory.hpp" + +#include "func_symlink_all.hpp" +#include "func_symlink_epall.hpp" +#include "func_symlink_epff.hpp" +#include "func_symlink_eplfs.hpp" +#include "func_symlink_eplus.hpp" +#include "func_symlink_epmfs.hpp" +#include "func_symlink_eppfrd.hpp" +#include "func_symlink_eprand.hpp" +#include "func_symlink_erofs.hpp" +#include "func_symlink_ff.hpp" +#include "func_symlink_lfs.hpp" +#include "func_symlink_lup.hpp" +#include "func_symlink_lus.hpp" +#include "func_symlink_mfs.hpp" +#include "func_symlink_msplfs.hpp" +#include "func_symlink_msplus.hpp" +#include "func_symlink_mspmfs.hpp" +#include "func_symlink_msppfrd.hpp" +#include "func_symlink_newest.hpp" +#include "func_symlink_pfrd.hpp" +#include "func_symlink_rand.hpp" + + +bool +Func2::SymlinkFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::SymlinkFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "epff") return std::make_shared(); + if(str_ == "eplfs") return std::make_shared(); + if(str_ == "eplus") return std::make_shared(); + if(str_ == "epmfs") return std::make_shared(); + if(str_ == "eppfrd") return std::make_shared(); + if(str_ == "eprand") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "ff") return std::make_shared(); + if(str_ == "lfs") return std::make_shared(); + if(str_ == "lup") return std::make_shared(); + if(str_ == "lus") return std::make_shared(); + if(str_ == "mfs") return std::make_shared(); + if(str_ == "msplfs") return std::make_shared(); + if(str_ == "msplus") return std::make_shared(); + if(str_ == "mspmfs") return std::make_shared(); + if(str_ == "msppfrd") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + if(str_ == "pfrd") return std::make_shared(); + if(str_ == "rand") return std::make_shared(); + + return {}; +} diff --git a/src/func_symlink_factory.hpp b/src/func_symlink_factory.hpp new file mode 100644 index 00000000..ff8a2494 --- /dev/null +++ b/src/func_symlink_factory.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace Func2 +{ + class SymlinkBase; + + class SymlinkFactory + { + public: + static bool valid(const std::string str_); + + static std::shared_ptr make(const std::string_view str_); + }; +} diff --git a/src/func_symlink_ff.cpp b/src/func_symlink_ff.cpp new file mode 100644 index 00000000..4da79e7b --- /dev/null +++ b/src/func_symlink_ff.cpp @@ -0,0 +1,118 @@ +#include "func_symlink_ff.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkFF::name() const +{ + return "ff"; +} + +int +Func2::SymlinkFF::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + chosen = &branch; + break; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_ff.hpp b/src/func_symlink_ff.hpp new file mode 100644 index 00000000..35933075 --- /dev/null +++ b/src/func_symlink_ff.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkFF : public SymlinkBase + { + public: + SymlinkFF() {} + ~SymlinkFF() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_lfs.cpp b/src/func_symlink_lfs.cpp new file mode 100644 index 00000000..927c4565 --- /dev/null +++ b/src/func_symlink_lfs.cpp @@ -0,0 +1,124 @@ +#include "func_symlink_lfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkLFS::name() const +{ + return "lfs"; +} + +int +Func2::SymlinkLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lfs = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail > lfs) + continue; + + lfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_lfs.hpp b/src/func_symlink_lfs.hpp new file mode 100644 index 00000000..17f2564c --- /dev/null +++ b/src/func_symlink_lfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkLFS : public SymlinkBase + { + public: + SymlinkLFS() {} + ~SymlinkLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_lup.cpp b/src/func_symlink_lup.cpp new file mode 100644 index 00000000..50dda5fc --- /dev/null +++ b/src/func_symlink_lup.cpp @@ -0,0 +1,132 @@ +#include "func_symlink_lup.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkLUP::name() const +{ + return "lup"; +} + +int +Func2::SymlinkLUP::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + long double best_ratio; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + best_ratio = 2.0L; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + { + u64 used = info.spaceused; + u64 total = info.spaceused + info.spaceavail; + long double ratio; + + if(total == 0) + { used = 0; total = 1; } + ratio = (long double)used / (long double)total; + if(ratio >= best_ratio) + continue; + + best_ratio = ratio; + chosen = &branch; + } + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_lup.hpp b/src/func_symlink_lup.hpp new file mode 100644 index 00000000..460ce402 --- /dev/null +++ b/src/func_symlink_lup.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkLUP : public SymlinkBase + { + public: + SymlinkLUP() {} + ~SymlinkLUP() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_lus.cpp b/src/func_symlink_lus.cpp new file mode 100644 index 00000000..70c5afbd --- /dev/null +++ b/src/func_symlink_lus.cpp @@ -0,0 +1,124 @@ +#include "func_symlink_lus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkLUS::name() const +{ + return "lus"; +} + +int +Func2::SymlinkLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 lus; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + lus = std::numeric_limits::max(); + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceused > lus) + continue; + + lus = info.spaceused; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_lus.hpp b/src/func_symlink_lus.hpp new file mode 100644 index 00000000..79939ab9 --- /dev/null +++ b/src/func_symlink_lus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkLUS : public SymlinkBase + { + public: + SymlinkLUS() {} + ~SymlinkLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_mfs.cpp b/src/func_symlink_mfs.cpp new file mode 100644 index 00000000..ac7325ea --- /dev/null +++ b/src/func_symlink_mfs.cpp @@ -0,0 +1,123 @@ +#include "func_symlink_mfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkMFS::name() const +{ + return "mfs"; +} + +int +Func2::SymlinkMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + u64 mfs; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + mfs = 0; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + if(info.spaceavail < mfs) + continue; + + mfs = info.spaceavail; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_mfs.hpp b/src/func_symlink_mfs.hpp new file mode 100644 index 00000000..8dd01e8a --- /dev/null +++ b/src/func_symlink_mfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkMFS : public SymlinkBase + { + public: + SymlinkMFS() {} + ~SymlinkMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_msplfs.cpp b/src/func_symlink_msplfs.cpp new file mode 100644 index 00000000..022524ef --- /dev/null +++ b/src/func_symlink_msplfs.cpp @@ -0,0 +1,159 @@ +#include "func_symlink_msplfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkMSPLFS::name() const +{ + return "msplfs"; +} + +int +Func2::SymlinkMSPLFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = linkpath_.parent_path(); + + for(;;) + { + bool any; + u64 lfs; + + any = false; + lfs = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail > lfs) + continue; + lfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = linkpath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = 0777; + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_msplfs.hpp b/src/func_symlink_msplfs.hpp new file mode 100644 index 00000000..d46ab1a5 --- /dev/null +++ b/src/func_symlink_msplfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkMSPLFS : public SymlinkBase + { + public: + SymlinkMSPLFS() {} + ~SymlinkMSPLFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_msplus.cpp b/src/func_symlink_msplus.cpp new file mode 100644 index 00000000..f985f55d --- /dev/null +++ b/src/func_symlink_msplus.cpp @@ -0,0 +1,159 @@ +#include "func_symlink_msplus.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkMSPLUS::name() const +{ + return "msplus"; +} + +int +Func2::SymlinkMSPLUS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = linkpath_.parent_path(); + + for(;;) + { + bool any; + u64 lus; + + any = false; + lus = std::numeric_limits::max(); + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceused > lus) + continue; + lus = info.spaceused; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = linkpath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = 0777; + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_msplus.hpp b/src/func_symlink_msplus.hpp new file mode 100644 index 00000000..a775eff9 --- /dev/null +++ b/src/func_symlink_msplus.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkMSPLUS : public SymlinkBase + { + public: + SymlinkMSPLUS() {} + ~SymlinkMSPLUS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_mspmfs.cpp b/src/func_symlink_mspmfs.cpp new file mode 100644 index 00000000..27544472 --- /dev/null +++ b/src/func_symlink_mspmfs.cpp @@ -0,0 +1,159 @@ +#include "func_symlink_mspmfs.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "fs_mkdir_as.hpp" +#include +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkMSPMFS::name() const +{ + return "mspmfs"; +} + +int +Func2::SymlinkMSPMFS::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = linkpath_.parent_path(); + + for(;;) + { + bool any; + u64 mfs; + + any = false; + mfs = 0; + chosen = nullptr; + + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + any = true; + if(info.spaceavail < mfs) + continue; + mfs = info.spaceavail; + chosen = &branch; + } + + if(any) + break; + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = linkpath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = 0777; + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_mspmfs.hpp b/src/func_symlink_mspmfs.hpp new file mode 100644 index 00000000..1110d6cd --- /dev/null +++ b/src/func_symlink_mspmfs.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkMSPMFS : public SymlinkBase + { + public: + SymlinkMSPMFS() {} + ~SymlinkMSPMFS() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_msppfrd.cpp b/src/func_symlink_msppfrd.cpp new file mode 100644 index 00000000..afcde777 --- /dev/null +++ b/src/func_symlink_msppfrd.cpp @@ -0,0 +1,182 @@ +#include "func_symlink_msppfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "fs_mkdir_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkMSPPFRD::name() const +{ + return "msppfrd"; +} + +int +Func2::SymlinkMSPPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + fs::path walk; + std::vector segments; + fs::path fullpath; + mode_t dirmode; + + branches = branches_; + chosen = nullptr; + error = ENOENT; + walk = linkpath_.parent_path(); + + for(;;) + { + u64 sum; + struct W { Branch *b; u64 weight; }; + std::vector writable; + + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,walk)) + error_and_continue(error,ENOENT); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(!writable.empty()) + { + if(sum == 0) + { + chosen = writable.front().b; + } + else + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + break; + } + + if(walk == "/") + break; + walk = walk.parent_path(); + } + + if(!chosen) + return -error; + + for(fs::path p = linkpath_.parent_path(); + !p.empty() && p != "/"; + p = p.parent_path()) + segments.push_back(p); + + dirmode = 0777; + for(auto it = segments.rbegin(); it != segments.rend(); ++it) + { + const Branch *src; + const fs::path full = chosen->path / *it; + + if(fs::exists(full)) + continue; + + src = nullptr; + for(auto &b : *branches) + { + if(&b == chosen) + continue; + if(!fs::exists(b.path,*it)) + continue; + src = &b; + break; + } + + if(src != nullptr) + rv = fs::clonepath(src->path,chosen->path,*it); + else + rv = fs::mkdir_as(ugid_,full,dirmode); + if((rv < 0) && (rv != -EEXIST)) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_msppfrd.hpp b/src/func_symlink_msppfrd.hpp new file mode 100644 index 00000000..27036922 --- /dev/null +++ b/src/func_symlink_msppfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkMSPPFRD : public SymlinkBase + { + public: + SymlinkMSPPFRD() {} + ~SymlinkMSPPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_newest.cpp b/src/func_symlink_newest.cpp new file mode 100644 index 00000000..58b65cd3 --- /dev/null +++ b/src/func_symlink_newest.cpp @@ -0,0 +1,126 @@ +#include "func_symlink_newest.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "timespec_utils.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkNewest::name() const +{ + return "newest"; +} + +int +Func2::SymlinkNewest::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + struct stat st; + timespec newest; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + newest = {0,0}; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + if(!fs::exists(branch.path,linkpath_,&st)) + error_and_continue(error,ENOENT); + if(chosen && (st.st_mtim < newest)) + continue; + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + newest = st.st_mtim; + chosen = &branch; + } + + if(!chosen) + return -error; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_newest.hpp b/src/func_symlink_newest.hpp new file mode 100644 index 00000000..8de7209f --- /dev/null +++ b/src/func_symlink_newest.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkNewest : public SymlinkBase + { + public: + SymlinkNewest() {} + ~SymlinkNewest() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_pfrd.cpp b/src/func_symlink_pfrd.cpp new file mode 100644 index 00000000..aad4f56a --- /dev/null +++ b/src/func_symlink_pfrd.cpp @@ -0,0 +1,146 @@ +#include "func_symlink_pfrd.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkPFRD::name() const +{ + return "pfrd"; +} + +int +Func2::SymlinkPFRD::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + u64 sum; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + struct W { Branch *b; u64 weight; }; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + sum = 0; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back({&branch,info.spaceavail}); + sum += info.spaceavail; + } + + if(writable.empty()) + return -error; + if(sum == 0) + return -ENOSPC; + + { + u64 idx; + u64 threshold; + + idx = 0; + threshold = RND::rand64(sum); + for(const auto &w : writable) + { + if(w.weight == 0) + continue; + idx += w.weight; + if(idx > threshold) + { + chosen = w.b; + break; + } + } + if(!chosen) + chosen = writable.back().b; + } + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_pfrd.hpp b/src/func_symlink_pfrd.hpp new file mode 100644 index 00000000..2019386b --- /dev/null +++ b/src/func_symlink_pfrd.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkPFRD : public SymlinkBase + { + public: + SymlinkPFRD() {} + ~SymlinkPFRD() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_symlink_rand.cpp b/src/func_symlink_rand.cpp new file mode 100644 index 00000000..ca10d76a --- /dev/null +++ b/src/func_symlink_rand.cpp @@ -0,0 +1,121 @@ +#include "func_symlink_rand.hpp" + +#include "errno.hpp" +#include "fs_clonepath.hpp" +#include "fs_exists.hpp" +#include "fs_info.hpp" +#include "fs_info_t.hpp" +#include "fs_path.hpp" +#include "fs_inode.hpp" +#include "fs_lstat.hpp" +#include "fs_symlink_as.hpp" +#include "rnd.hpp" +#include + + +#define error_and_continue(CUR,ERR) \ + do { \ + ::_calc_error(CUR,ERR); \ + continue; \ + } while(0) + +static +inline +void +_calc_error(int &cur_, + const int err_) +{ + switch(cur_) + { + default: + case ENOENT: + cur_ = err_; + break; + case ENOSPC: + if(err_ != ENOENT) + cur_ = err_; + break; + case EROFS: + if((err_ != ENOENT) && (err_ != ENOSPC)) + cur_ = err_; + break; + } +} + + +std::string_view +Func2::SymlinkRAND::name() const +{ + return "rand"; +} + +int +Func2::SymlinkRAND::operator()(const ugid_t &ugid_, + const Branches &branches_, + const char *const &target_, + const fs::path &linkpath_, + struct stat *&st_) +{ + int rv; + int error; + fs::info_t info; + Branches::Ptr branches; + const Branch *chosen; + const Branch *clone_src; + std::vector writable; + fs::path fullpath; + + branches = branches_; + chosen = nullptr; + clone_src = nullptr; + error = ENOENT; + for(auto &branch : *branches) + { + if(branch.ro_or_nc()) + error_and_continue(error,EROFS); + rv = fs::info(branch.path,&info); + if(rv < 0) + error_and_continue(error,ENOENT); + if(info.readonly) + error_and_continue(error,EROFS); + if(info.spaceavail < branch.minfreespace()) + error_and_continue(error,ENOSPC); + + writable.push_back(&branch); + } + + if(writable.empty()) + return -error; + + chosen = writable[RND::rand64(writable.size())]; + + for(auto &branch : *branches) + { + if(!fs::exists(branch.path,linkpath_.parent_path())) + continue; + clone_src = &branch; + break; + } + + if(!clone_src) + return -ENOENT; + + if(clone_src != chosen) + { + rv = fs::clonepath(clone_src->path,chosen->path,linkpath_.parent_path()); + if(rv < 0) + return rv; + } + + fullpath = chosen->path / linkpath_; + + rv = fs::symlink_as(ugid_,target_,fullpath); + if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) + { + fs::lstat(fullpath,st_); + if(st_->st_ino != 0) + fs::inode::calc(chosen->path,linkpath_,st_); + } + + return rv; +} diff --git a/src/func_symlink_rand.hpp b/src/func_symlink_rand.hpp new file mode 100644 index 00000000..9cb9b08b --- /dev/null +++ b/src/func_symlink_rand.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_symlink_base.hpp" + +namespace Func2 +{ + class SymlinkRAND : public SymlinkBase + { + public: + SymlinkRAND() {} + ~SymlinkRAND() {} + public: + std::string_view name() const; + public: + int operator()(const ugid_t &ugid, + const Branches &branches, + const char *const &target, + const fs::path &linkpath, + struct stat *&st); + }; +} diff --git a/src/func_truncate.hpp b/src/func_truncate.hpp new file mode 100644 index 00000000..d6b0e79d --- /dev/null +++ b/src/func_truncate.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_truncate_base.hpp" +#include "func_truncate_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Truncate = FuncWrapper; +} diff --git a/src/func_truncate_all.cpp b/src/func_truncate_all.cpp new file mode 100644 index 00000000..77ecedcf --- /dev/null +++ b/src/func_truncate_all.cpp @@ -0,0 +1,42 @@ +#include "func_truncate_all.hpp" + +#include "errno.hpp" +#include "fs_truncate.hpp" + + +std::string_view +Func2::TruncateAll::name() const +{ + return "all"; +} + +int +Func2::TruncateAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const off_t size_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::truncate(fullpath,size_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_truncate_all.hpp b/src/func_truncate_all.hpp new file mode 100644 index 00000000..43222804 --- /dev/null +++ b/src/func_truncate_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_truncate_base.hpp" + +namespace Func2 +{ + class TruncateAll : public TruncateBase + { + public: + TruncateAll() {} + ~TruncateAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const off_t size_); + }; +} diff --git a/src/func_truncate_base.hpp b/src/func_truncate_base.hpp new file mode 100644 index 00000000..9a818c67 --- /dev/null +++ b/src/func_truncate_base.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class TruncateBase + { + public: + TruncateBase() {} + ~TruncateBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const off_t size) = 0; + }; +} diff --git a/src/func_truncate_erofs.cpp b/src/func_truncate_erofs.cpp new file mode 100644 index 00000000..b6cca6b7 --- /dev/null +++ b/src/func_truncate_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_truncate_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::TruncateEROFS::name() const { return "erofs"; } + +int +Func2::TruncateEROFS::operator()(const Branches&,const fs::path&,const off_t) +{ + return -EROFS; +} diff --git a/src/func_truncate_erofs.hpp b/src/func_truncate_erofs.hpp new file mode 100644 index 00000000..adfe9b49 --- /dev/null +++ b/src/func_truncate_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_truncate_base.hpp" +namespace Func2 { + class TruncateEROFS : public TruncateBase { + public: + TruncateEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const off_t); + }; +} diff --git a/src/func_truncate_factory.cpp b/src/func_truncate_factory.cpp new file mode 100644 index 00000000..7b4b7727 --- /dev/null +++ b/src/func_truncate_factory.cpp @@ -0,0 +1,20 @@ +#include "func_truncate_factory.hpp" + +#include "func_truncate_all.hpp" +#include "func_truncate_erofs.hpp" + + +bool +Func2::TruncateFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::TruncateFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_truncate_factory.hpp b/src/func_truncate_factory.hpp new file mode 100644 index 00000000..92a03fc0 --- /dev/null +++ b/src/func_truncate_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_truncate_base.hpp" + +namespace Func2 +{ + class TruncateFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_unlink.hpp b/src/func_unlink.hpp new file mode 100644 index 00000000..5a6d86c6 --- /dev/null +++ b/src/func_unlink.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "func_unlink_base.hpp" +#include "func_unlink_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Unlink = FuncWrapper; +} diff --git a/src/func_unlink_all.cpp b/src/func_unlink_all.cpp new file mode 100644 index 00000000..1e564343 --- /dev/null +++ b/src/func_unlink_all.cpp @@ -0,0 +1,41 @@ +#include "func_unlink_all.hpp" + +#include "errno.hpp" +#include "fs_unlink.hpp" + + +std::string_view +Func2::UnlinkAll::name() const +{ + return "all"; +} + +int +Func2::UnlinkAll::operator()(const Branches &branches_, + const fs::path &fusepath_) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::unlink(fullpath); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_unlink_all.hpp b/src/func_unlink_all.hpp new file mode 100644 index 00000000..558678ab --- /dev/null +++ b/src/func_unlink_all.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "func_unlink_base.hpp" + +namespace Func2 +{ + class UnlinkAll : public UnlinkBase + { + public: + UnlinkAll() {} + ~UnlinkAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath); + }; +} diff --git a/src/func_unlink_base.hpp b/src/func_unlink_base.hpp new file mode 100644 index 00000000..9737866f --- /dev/null +++ b/src/func_unlink_base.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class UnlinkBase + { + public: + UnlinkBase() {} + ~UnlinkBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath) = 0; + }; +} diff --git a/src/func_unlink_erofs.cpp b/src/func_unlink_erofs.cpp new file mode 100644 index 00000000..c1bc1d8f --- /dev/null +++ b/src/func_unlink_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_unlink_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::UnlinkEROFS::name() const { return "erofs"; } + +int +Func2::UnlinkEROFS::operator()(const Branches&,const fs::path&) +{ + return -EROFS; +} diff --git a/src/func_unlink_erofs.hpp b/src/func_unlink_erofs.hpp new file mode 100644 index 00000000..654eacb6 --- /dev/null +++ b/src/func_unlink_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_unlink_base.hpp" +namespace Func2 { + class UnlinkEROFS : public UnlinkBase { + public: + UnlinkEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&); + }; +} diff --git a/src/func_unlink_factory.cpp b/src/func_unlink_factory.cpp new file mode 100644 index 00000000..884866c9 --- /dev/null +++ b/src/func_unlink_factory.cpp @@ -0,0 +1,23 @@ +#include "func_unlink_factory.hpp" + +#include "func_unlink_all.hpp" +#include "func_unlink_erofs.hpp" +#include "func_unlink_newest.hpp" + + +bool +Func2::UnlinkFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::UnlinkFactory::make(const std::string_view str_) +{ + if(str_ == "all") return std::make_shared(); + if(str_ == "epall") return std::make_shared(); + if(str_ == "erofs") return std::make_shared(); + if(str_ == "newest") return std::make_shared(); + + return {}; +} diff --git a/src/func_unlink_factory.hpp b/src/func_unlink_factory.hpp new file mode 100644 index 00000000..1d8058e0 --- /dev/null +++ b/src/func_unlink_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_unlink_base.hpp" + +namespace Func2 +{ + class UnlinkFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_unlink_newest.cpp b/src/func_unlink_newest.cpp new file mode 100644 index 00000000..bf374c23 --- /dev/null +++ b/src/func_unlink_newest.cpp @@ -0,0 +1,29 @@ +#include "func_unlink_newest.hpp" +#include "errno.hpp" +#include "fs_exists.hpp" +#include "fs_unlink.hpp" +#include "timespec_utils.hpp" + +std::string_view Func2::UnlinkNewest::name() const { return "newest"; } + +int +Func2::UnlinkNewest::operator()(const Branches &branches_, + const fs::path &fusepath_) +{ + const Branch *chosen = nullptr; + timespec newest{}; + struct stat st; + for(const auto &branch : branches_) + { + if(branch.ro()) continue; + if(!fs::exists(branch.path,fusepath_,&st)) + continue; + if(chosen && (st.st_mtim < newest)) + continue; + newest = st.st_mtim; + chosen = &branch; + } + if(chosen == nullptr) + return -ENOENT; + return fs::unlink(chosen->path / fusepath_); +} diff --git a/src/func_unlink_newest.hpp b/src/func_unlink_newest.hpp new file mode 100644 index 00000000..711bcf95 --- /dev/null +++ b/src/func_unlink_newest.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_unlink_base.hpp" +namespace Func2 { + class UnlinkNewest : public UnlinkBase { + public: + UnlinkNewest() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&); + }; +} diff --git a/src/func_utimens.hpp b/src/func_utimens.hpp new file mode 100644 index 00000000..0aa697a5 --- /dev/null +++ b/src/func_utimens.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "func_utimens_base.hpp" +#include "func_utimens_factory.hpp" + +#include "func_wrapper.hpp" + + +namespace Func2 +{ + using Utimens = FuncWrapper; +} diff --git a/src/func_utimens_all.cpp b/src/func_utimens_all.cpp new file mode 100644 index 00000000..2aed2d5a --- /dev/null +++ b/src/func_utimens_all.cpp @@ -0,0 +1,42 @@ +#include "func_utimens_all.hpp" + +#include "errno.hpp" +#include "fs_lutimens.hpp" + + +std::string_view +Func2::UtimensAll::name() const +{ + return "all"; +} + +int +Func2::UtimensAll::operator()(const Branches &branches_, + const fs::path &fusepath_, + const timespec times_[2]) +{ + int err; + bool found; + fs::path fullpath; + + err = 0; + found = false; + for(const auto &branch : branches_) + { + if(branch.ro()) + continue; + fullpath = branch.path / fusepath_; + + const int rv = fs::lutimens(fullpath,times_); + if(rv == -ENOENT) + continue; + + found = true; + err = rv; + } + + if(!found) + return -ENOENT; + + return err; +} diff --git a/src/func_utimens_all.hpp b/src/func_utimens_all.hpp new file mode 100644 index 00000000..248f9cea --- /dev/null +++ b/src/func_utimens_all.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "func_utimens_base.hpp" + +namespace Func2 +{ + class UtimensAll : public UtimensBase + { + public: + UtimensAll() {} + ~UtimensAll() {} + + public: + std::string_view name() const; + + public: + int operator()(const Branches &branches, + const fs::path &fusepath, + const timespec times_[2]); + }; +} diff --git a/src/func_utimens_base.hpp b/src/func_utimens_base.hpp new file mode 100644 index 00000000..9f118439 --- /dev/null +++ b/src/func_utimens_base.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "branches.hpp" + +#include "fs_path.hpp" + +namespace Func2 +{ + class UtimensBase + { + public: + UtimensBase() {} + ~UtimensBase() {} + + public: + virtual std::string_view name() const = 0; + + public: + virtual int operator()(const Branches &branches, + const fs::path &fusepath, + const timespec times_[2]) = 0; + }; +} diff --git a/src/func_utimens_erofs.cpp b/src/func_utimens_erofs.cpp new file mode 100644 index 00000000..b3aaa43d --- /dev/null +++ b/src/func_utimens_erofs.cpp @@ -0,0 +1,10 @@ +#include "func_utimens_erofs.hpp" +#include "errno.hpp" + +std::string_view Func2::UtimensEROFS::name() const { return "erofs"; } + +int +Func2::UtimensEROFS::operator()(const Branches&,const fs::path&,const timespec[2]) +{ + return -EROFS; +} diff --git a/src/func_utimens_erofs.hpp b/src/func_utimens_erofs.hpp new file mode 100644 index 00000000..a03cf31d --- /dev/null +++ b/src/func_utimens_erofs.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "func_utimens_base.hpp" +namespace Func2 { + class UtimensEROFS : public UtimensBase { + public: + UtimensEROFS() {} + std::string_view name() const; + int operator()(const Branches&,const fs::path&,const timespec[2]); + }; +} diff --git a/src/func_utimens_factory.cpp b/src/func_utimens_factory.cpp new file mode 100644 index 00000000..4516776e --- /dev/null +++ b/src/func_utimens_factory.cpp @@ -0,0 +1,20 @@ +#include "func_utimens_factory.hpp" + +#include "func_utimens_all.hpp" +#include "func_utimens_erofs.hpp" + + +bool +Func2::UtimensFactory::valid(const std::string str_) +{ + return (bool)make(str_); +} + +std::shared_ptr +Func2::UtimensFactory::make(const std::string_view str_) +{ + if(str_ == "erofs") return std::make_shared(); + if(str_ == "all" || str_ == "epall") return std::make_shared(); + + return {}; +} diff --git a/src/func_utimens_factory.hpp b/src/func_utimens_factory.hpp new file mode 100644 index 00000000..62fd94c4 --- /dev/null +++ b/src/func_utimens_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "func_utimens_base.hpp" + +namespace Func2 +{ + class UtimensFactory + { + public: + static bool valid(const std::string str); + static std::shared_ptr make(const std::string_view str); + }; +} diff --git a/src/func_wrapper.hpp b/src/func_wrapper.hpp new file mode 100644 index 00000000..164601cb --- /dev/null +++ b/src/func_wrapper.hpp @@ -0,0 +1,103 @@ +#pragma once + +#include "fatal.hpp" +#include "tofrom_string.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + +template +class FuncWrapper : public ToFromString +{ +private: + bool _initialized = false; + mutable std::shared_mutex _mutex; + std::shared_ptr _impl; + +public: + explicit FuncWrapper(const std::string &name_) + { + // Hard-coded defaults must always parse. Failure here is a build + // error masquerading as a runtime bug, so check unconditionally + // (assert alone is elided under NDEBUG). + const int rv = from_string(name_); + if(rv != 0) + fatal::abort("FuncWrapper: hard-coded default '{}' is not a valid policy name",name_); + } + +public: + ReturnType + operator()(Args&&... args) + { + std::shared_ptr impl; + + { + std::shared_lock lk(_mutex); + impl = _impl; + } + if(!impl) + fatal::abort("function policy impl is null"); + + return (*impl)(std::forward(args)...); + } + +public: + std::shared_ptr + impl() const + { + std::shared_lock lk(_mutex); + return _impl; + } + +public: + std::string + to_string() const + { + std::shared_ptr impl; + { + std::shared_lock lk(_mutex); + impl = _impl; + } + if(!impl) + return {}; + + return std::string(impl->name()); + } + + int + from_string(const std::string_view str_) + { + std::shared_ptr p; + + p = FactoryType::make(str_); + if(!p) + return -EINVAL; + + { + std::unique_lock lk(_mutex); + + _impl = std::move(p); + } + + return 0; + } + +public: + void + initialize() + { + std::unique_lock lk(_mutex); + + _initialized = true; + } +}; diff --git a/src/funcs.hpp b/src/funcs.hpp deleted file mode 100644 index 6ea21f21..00000000 --- a/src/funcs.hpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "func.hpp" - - -struct Funcs -{ - Func::Access access; - Func::Chmod chmod; - Func::Chown chown; - Func::Create create; - Func::GetAttr getattr; - Func::GetXAttr getxattr; - Func::Link link; - Func::ListXAttr listxattr; - Func::Mkdir mkdir; - Func::Mknod mknod; - Func::Open open; - Func::Readlink readlink; - Func::RemoveXAttr removexattr; - Func::Rename rename; - Func::Rmdir rmdir; - Func::SetXAttr setxattr; - Func::Symlink symlink; - Func::Truncate truncate; - Func::Unlink unlink; - Func::Utimens utimens; -}; diff --git a/src/fuse_access.cpp b/src/fuse_access.cpp index 23e1e28c..c507823f 100644 --- a/src/fuse_access.cpp +++ b/src/fuse_access.cpp @@ -23,35 +23,6 @@ #include "fs_eaccess.hpp" #include "fs_path.hpp" -#include -#include - - -static -int -_access(const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const int mask_) -{ - int rv; - StrVec basepaths; - fs::path fullpath; - std::vector branches; - - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - fullpath = branches[0]->path / fusepath_; - - rv = fs::eaccess(fullpath,mask_); - - return rv; -} - int FUSE::access(const fuse_req_ctx_t *ctx_, const char *fusepath_, @@ -59,8 +30,7 @@ FUSE::access(const fuse_req_ctx_t *ctx_, { const fs::path fusepath{fusepath_}; - return ::_access(cfg.func.access.policy, - cfg.branches, - fusepath, - mask_); + return cfg.access(cfg.branches, + fusepath, + mask_); } diff --git a/src/fuse_chmod.cpp b/src/fuse_chmod.cpp index 22362c8e..68f9e14f 100644 --- a/src/fuse_chmod.cpp +++ b/src/fuse_chmod.cpp @@ -19,100 +19,15 @@ #include "fuse_chmod.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_lchmod.hpp" -#include "fs_path.hpp" -#include "policy_rv.hpp" - -#include "fuse.h" - -#include -#include - - -static -void -_chmod_loop_core(const std::string &basepath_, - const fs::path &fusepath_, - const mode_t mode_, - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::lchmod(fullpath,mode_); - - prv_->insert(rv,basepath_); -} - -static -void -_chmod_loop(const std::vector &branches_, - const fs::path &fusepath_, - const mode_t mode_, - PolicyRV *prv_) -{ - for(auto &branch : branches_) - { - ::_chmod_loop_core(branch->path,fusepath_,mode_,prv_); - } -} - -static -int -_chmod(const Policy::Action &actionFunc_, - const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const mode_t mode_) -{ - int rv; - PolicyRV prv; - std::vector branches; - - rv = actionFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - ::_chmod_loop(branches,fusepath_,mode_,&prv); - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - branches.clear(); - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - return prv.get_error(branches[0]->path); -} - -static -int -_chmod(const fs::path &fusepath_, - const mode_t mode_) -{ - - return ::_chmod(cfg.func.chmod.policy, - cfg.func.getattr.policy, - cfg.branches, - fusepath_, - mode_); -} int FUSE::chmod(const fuse_req_ctx_t *ctx_, const char *fusepath_, - mode_t mode_) + const mode_t mode_) { const fs::path fusepath{fusepath_}; - return ::_chmod(fusepath,mode_); + return cfg.chmod(cfg.branches, + fusepath, + mode_); } diff --git a/src/fuse_chmod.hpp b/src/fuse_chmod.hpp index f9cbc50b..c46d1ceb 100644 --- a/src/fuse_chmod.hpp +++ b/src/fuse_chmod.hpp @@ -28,5 +28,5 @@ namespace FUSE int chmod(const fuse_req_ctx_t *ctx, const char *fusepath, - mode_t mode); + const mode_t mode); } diff --git a/src/fuse_chown.cpp b/src/fuse_chown.cpp index 466f525d..d6c00178 100644 --- a/src/fuse_chown.cpp +++ b/src/fuse_chown.cpp @@ -19,97 +19,17 @@ #include "fuse_chown.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_lchown.hpp" -#include "fs_path.hpp" -#include "policy_rv.hpp" - -#include "fuse.h" - -#include -#include - - -static -void -_chown_loop_core(const fs::path &basepath_, - const fs::path &fusepath_, - const uid_t uid_, - const gid_t gid_, - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::lchown(fullpath,uid_,gid_); - - prv_->insert(rv,basepath_); -} - -static -void -_chown_loop(const std::vector &branches_, - const fs::path &fusepath_, - const uid_t uid_, - const gid_t gid_, - PolicyRV *prv_) -{ - for(const auto &branch : branches_) - { - ::_chown_loop_core(branch->path,fusepath_,uid_,gid_,prv_); - } -} - -static -int -_chown(const Policy::Action &actionFunc_, - const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const uid_t uid_, - const gid_t gid_) -{ - int rv; - PolicyRV prv; - std::vector branches; - - rv = actionFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - ::_chown_loop(branches,fusepath_,uid_,gid_,&prv); - - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - branches.clear(); - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - return prv.get_error(branches[0]->path); -} int FUSE::chown(const fuse_req_ctx_t *ctx_, const char *fusepath_, - uid_t uid_, - gid_t gid_) + const uid_t uid_, + const gid_t gid_) { const fs::path fusepath{fusepath_}; - return ::_chown(cfg.func.chown.policy, - cfg.func.getattr.policy, - cfg.branches, - fusepath, - uid_, - gid_); + return cfg.chown(cfg.branches, + fusepath, + uid_, + gid_); } diff --git a/src/fuse_create.cpp b/src/fuse_create.cpp index 2fe50b40..d8515df9 100644 --- a/src/fuse_create.cpp +++ b/src/fuse_create.cpp @@ -162,91 +162,6 @@ _config_to_ffi_flags(const int tid_, ffi_->parallel_direct_writes = ffi_->direct_io; } -static -int -_create_core(const ugid_t ugid_, - const fs::path &fullpath_, - mode_t mode_, - const mode_t umask_, - const int flags_) -{ - if(!fs::acl::dir_has_defaults(fullpath_)) - mode_ &= ~umask_; - - return fs::open_as(ugid_,fullpath_,flags_,mode_); -} - -static -int -_create_core(const ugid_t ugid_, - const Branch *branch_, - const fs::path &fusepath_, - fuse_file_info_t *ffi_, - const mode_t mode_, - const mode_t umask_) -{ - int rv; - FileInfo *fi; - fs::path fullpath; - - fullpath = branch_->path / fusepath_; - - rv = ::_create_core(ugid_,fullpath,mode_,umask_,ffi_->flags); - if(rv < 0) - return rv; - - fi = new FileInfo(rv,*branch_,fusepath_,ffi_->direct_io); - - ffi_->fh = fi->to_fh(); - - return 0; -} - -static -int -_create(const ugid_t ugid_, - const Policy::Search &searchFunc_, - const Policy::Create &createFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - fuse_file_info_t *ffi_, - const mode_t mode_, - const mode_t umask_) -{ - int rv; - fs::path fullpath; - fs::path fusedirpath; - std::vector createpaths; - std::vector existingpaths; - - fusedirpath = fusepath_.parent_path(); - - rv = searchFunc_(branches_,fusedirpath,existingpaths); - if(rv < 0) - return rv; - if(existingpaths.empty()) - return -ENOENT; - - rv = createFunc_(branches_,fusedirpath,createpaths); - if(rv < 0) - return rv; - if(createpaths.empty()) - return -ENOENT; - - rv = fs::clonepath(existingpaths[0]->path, - createpaths[0]->path, - fusedirpath); - if(rv < 0) - return rv; - - return ::_create_core(ugid_, - createpaths[0], - fusepath_, - ffi_, - mode_, - umask_); -} - constexpr u64 _(const PassthroughIOEnum e_, @@ -262,32 +177,30 @@ _create(const fuse_req_ctx_t *ctx_, mode_t mode_, fuse_file_info_t *ffi_) { + int rv; auto &of = state.open_files; + const ugid_t ugid(ctx_); ::_config_to_ffi_flags(ctx_->pid,ffi_); if(cfg.cache_writeback) ::_tweak_flags_cache_writeback(&ffi_->flags); ffi_->noflush = !::_calculate_flush(cfg.flushonclose,ffi_->flags); - int rv = ::_create(ctx_, - cfg.func.getattr.policy, - cfg.func.create.policy, - cfg.branches, - fusepath_, - ffi_, - mode_, - ctx_->umask); + rv = cfg.create(ugid, + cfg.branches, + fusepath_, + ffi_, + mode_, + ctx_->umask); if(rv == -EROFS) { cfg.branches.find_and_set_mode_ro(); - rv = ::_create(ctx_, - cfg.func.getattr.policy, - cfg.func.create.policy, - cfg.branches, - fusepath_, - ffi_, - mode_, - ctx_->umask); + rv = cfg.create(ugid, + cfg.branches, + fusepath_, + ffi_, + mode_, + ctx_->umask); } if(rv < 0) diff --git a/src/fuse_getattr.cpp b/src/fuse_getattr.cpp index 23174d3c..e4ba9942 100644 --- a/src/fuse_getattr.cpp +++ b/src/fuse_getattr.cpp @@ -35,42 +35,6 @@ #include -static -void -_set_stat_if_leads_to_dir(const fs::path &path_, - struct stat *st_) -{ - int rv; - struct stat st; - - rv = fs::stat(path_,&st); - if(rv < 0) - return; - - if(S_ISDIR(st.st_mode)) - *st_ = st; - - return; -} - -static -void -_set_stat_if_leads_to_reg(const fs::path &path_, - struct stat *st_) -{ - int rv; - struct stat st; - - rv = fs::stat(path_,&st); - if(rv < 0) - return; - - if(S_ISREG(st.st_mode)) - *st_ = st; - - return; -} - static int _getattr_fake_root(struct stat *st_, @@ -125,63 +89,6 @@ _getattr_controlfile(struct stat *st_, return 0; } -static -int -_getattr(const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - struct stat *st_, - const bool symlinkify_, - const time_t symlinkify_timeout_, - FollowSymlinks followsymlinks_) -{ - int rv; - fs::path fullpath; - std::vector branches; - - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - fullpath = branches[0]->path / fusepath_; - - switch(followsymlinks_) - { - case FollowSymlinks::ENUM::NEVER: - rv = fs::lstat(fullpath,st_); - break; - case FollowSymlinks::ENUM::DIRECTORY: - rv = fs::lstat(fullpath,st_); - if((rv >= 0) && S_ISLNK(st_->st_mode)) - ::_set_stat_if_leads_to_dir(fullpath,st_); - break; - case FollowSymlinks::ENUM::REGULAR: - rv = fs::lstat(fullpath,st_); - if((rv >= 0) && S_ISLNK(st_->st_mode)) - ::_set_stat_if_leads_to_reg(fullpath,st_); - break; - case FollowSymlinks::ENUM::ALL: - rv = fs::stat(fullpath,st_); - if(rv < 0) - rv = fs::lstat(fullpath,st_); - break; - } - - if(rv < 0) - return rv; - - if(symlinkify_ && symlinkify::can_be_symlink(*st_,symlinkify_timeout_)) - symlinkify::convert(fullpath,st_); - - fs::inode::calc(branches[0]->path, - fusepath_, - st_); - - return 0; -} - int _getattr(const fs::path &fusepath_, struct stat *st_, @@ -189,13 +96,12 @@ _getattr(const fs::path &fusepath_, { int rv; - rv = ::_getattr(cfg.func.getattr.policy, - cfg.branches, - fusepath_, - st_, - cfg.symlinkify, - cfg.symlinkify_timeout, - cfg.follow_symlinks); + rv = cfg.getattr(cfg.branches, + fusepath_, + st_, + cfg.follow_symlinks, + cfg.symlinkify, + cfg.symlinkify_timeout); if((rv < 0) && Config::is_rootdir(fusepath_)) return ::_getattr_fake_root(st_,timeout_); diff --git a/src/fuse_getxattr.cpp b/src/fuse_getxattr.cpp index 8bedd2d7..9cff3359 100644 --- a/src/fuse_getxattr.cpp +++ b/src/fuse_getxattr.cpp @@ -20,21 +20,13 @@ #include "config.hpp" #include "errno.hpp" -#include "fs_findallfiles.hpp" -#include "fs_lgetxattr.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" #include "str.hpp" -#include "version.hpp" #include "fuse.h" -#include #include -#include #include -#include static const char SECURITY_CAPABILITY[] = "security.capability"; @@ -76,118 +68,20 @@ _getxattr_ctrl_file(Config &cfg_, return (int)val.size(); } -static -int -_getxattr_from_string(char *destbuf_, - const size_t destbufsize_, - const std::string &src_) -{ - const size_t srcbufsize = src_.size(); - - if(destbufsize_ == 0) - return srcbufsize; - - if(srcbufsize > destbufsize_) - return -ERANGE; - - memcpy(destbuf_,src_.data(),srcbufsize); - - return srcbufsize; -} - -static -int -_getxattr_user_mergerfs_allpaths(const Branches::Ptr branches_, - const fs::path &fusepath_, - char *buf_, - const size_t count_) -{ - std::string concated; - StrVec paths; - StrVec branches; - - branches_->to_paths(branches); - - fs::findallfiles(branches,fusepath_,&paths); - - concated = str::join(paths,'\0'); - - return ::_getxattr_from_string(buf_,count_,concated); -} - -static -int -_getxattr_user_mergerfs(const fs::path &basepath_, - const fs::path &fusepath_, - const fs::path &fullpath_, - const Branches::Ptr branches_, - const char *attrname_, - char *buf_, - const size_t count_) -{ - std::string key; - - key = Config::prune_ctrl_xattr(attrname_); - - if(key == "basepath") - return ::_getxattr_from_string(buf_,count_,basepath_); - if(key == "relpath") - return ::_getxattr_from_string(buf_,count_,fusepath_); - if(key == "fullpath") - return ::_getxattr_from_string(buf_,count_,fullpath_); - if(key == "allpaths") - return ::_getxattr_user_mergerfs_allpaths(branches_,fusepath_,buf_,count_); - - return -ENOATTR; -} - -static -int -_getxattr(const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const char *attrname_, - char *buf_, - const size_t count_) -{ - int rv; - fs::path fullpath; - std::vector branches; - - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - fullpath = branches[0]->path / fusepath_; - - if(Config::is_mergerfs_xattr(attrname_)) - return ::_getxattr_user_mergerfs(branches[0]->path, - fusepath_, - fullpath, - branches_, - attrname_, - buf_, - count_); - - return fs::lgetxattr(fullpath,attrname_,buf_,count_); -} - int FUSE::getxattr(const fuse_req_ctx_t *ctx_, const char *fusepath_, const char *attrname_, - char *attrvalue_, - size_t attrvalue_size_) + char *attrval_, + size_t attrvalsize_) { const fs::path fusepath{fusepath_}; if(Config::is_ctrl_file(fusepath)) return ::_getxattr_ctrl_file(cfg, attrname_, - attrvalue_, - attrvalue_size_); + attrval_, + attrvalsize_); if((cfg.security_capability == false) && ::_is_attrname_security_capability(attrname_)) @@ -196,10 +90,9 @@ FUSE::getxattr(const fuse_req_ctx_t *ctx_, if(cfg.xattr.to_int()) return -cfg.xattr.to_int(); - return ::_getxattr(cfg.func.getxattr.policy, - cfg.branches, - fusepath, - attrname_, - attrvalue_, - attrvalue_size_); + return cfg.getxattr(cfg.branches, + fusepath, + attrname_, + attrval_, + attrvalsize_); } diff --git a/src/fuse_init.cpp b/src/fuse_init.cpp index de27eb82..ac089f1c 100644 --- a/src/fuse_init.cpp +++ b/src/fuse_init.cpp @@ -188,7 +188,7 @@ void * FUSE::init(fuse_conn_info_t *conn_) { procfs::init(); - cfg.readdir.initialize(); + cfg.initialize_funcs(); ::_want_if_capable(conn_,FUSE_CAP_ASYNC_DIO); ::_want_if_capable(conn_,FUSE_CAP_ASYNC_READ,&cfg.async_read); diff --git a/src/fuse_ioctl.cpp b/src/fuse_ioctl.cpp index bd0a0259..e3fb7ea3 100644 --- a/src/fuse_ioctl.cpp +++ b/src/fuse_ioctl.cpp @@ -127,39 +127,6 @@ _ioctl_file(const fuse_req_ctx_t *ctx_, #define O_NOATIME 0 #endif -static -int -_ioctl_dir_base(const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const u32 cmd_, - void *data_, - u32 *out_bufsz_) -{ - int fd; - int rv; - fs::path fullpath; - std::vector branches; - - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - fullpath = branches[0]->path / fusepath_; - - fd = fs::open(fullpath,O_RDONLY|O_NOATIME|O_NONBLOCK); - if(fd < 0) - return fd; - - rv = ::_ioctl(fd,cmd_,data_,out_bufsz_); - - fs::close(fd); - - return rv; -} - static int _ioctl_dir(const fuse_req_ctx_t *ctx_, @@ -173,12 +140,11 @@ _ioctl_dir(const fuse_req_ctx_t *ctx_, if(not di) return -EBADF; - return ::_ioctl_dir_base(cfg.func.open.policy, - cfg.branches, - di->fusepath.c_str(), - cmd_, - data_, - out_bufsz_); + return cfg.ioctl(cfg.branches, + di->fusepath.c_str(), + cmd_, + data_, + out_bufsz_); } static diff --git a/src/fuse_link.cpp b/src/fuse_link.cpp index b9b5c3da..6bccbf6d 100644 --- a/src/fuse_link.cpp +++ b/src/fuse_link.cpp @@ -21,11 +21,13 @@ #include "config.hpp" #include "errno.hpp" #include "fs_clonepath.hpp" +#include "fs_exists.hpp" #include "fs_link.hpp" #include "fs_lstat.hpp" #include "fs_path.hpp" #include "fuse_getattr.hpp" #include "fuse_symlink.hpp" +#include "ugid.hpp" #include "fuse.h" @@ -35,161 +37,11 @@ static -int -_link_create_path_loop(const std::vector &oldbranches_, - const Branch *newbranch_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_, - const fs::path &newfusedirpath_) +bool +_create_is_path_preserving() { - int rv; - int err; - fs::path oldfullpath; - fs::path newfullpath; - - err = -ENOENT; - for(const auto &oldbranch : oldbranches_) - { - oldfullpath = oldbranch->path / oldfusepath_; - newfullpath = oldbranch->path / newfusepath_; - - rv = fs::link(oldfullpath,newfullpath); - if(rv == -ENOENT) - { - rv = fs::clonepath(newbranch_->path, - oldbranch->path, - newfusedirpath_); - if(rv == 0) - rv = fs::link(oldfullpath,newfullpath); - } - - if(err < 0) - err = rv; - } - - return err; -} - -static -int -_link_create_path(const Policy::Search &searchFunc_, - const Policy::Action &actionFunc_, - const Branches::Ptr ibranches_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_) -{ - int rv; - fs::path newfusedirpath; - std::vector oldbranches; - std::vector newbranches; - - rv = actionFunc_(ibranches_,oldfusepath_,oldbranches); - if(rv < 0) - return rv; - if(oldbranches.empty()) - return -ENOENT; - - newfusedirpath = newfusepath_.parent_path(); - - rv = searchFunc_(ibranches_,newfusedirpath,newbranches); - if(rv < 0) - return rv; - if(newbranches.empty()) - return -ENOENT; - - return ::_link_create_path_loop(oldbranches,newbranches[0], - oldfusepath_,newfusepath_, - newfusedirpath); -} - -static -int -_link_preserve_path_core(const fs::path &oldbasepath_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_, - struct stat *st_) -{ - int rv; - fs::path oldfullpath; - fs::path newfullpath; - - oldfullpath = oldbasepath_ / oldfusepath_; - newfullpath = oldbasepath_ / newfusepath_; - - rv = fs::link(oldfullpath,newfullpath); - if(rv == -ENOENT) - rv = -EXDEV; - if((rv == 0) && (st_->st_ino == 0)) - rv = fs::lstat(oldfullpath,st_); - - return rv; -} - -static -int -_link_preserve_path_loop(const std::vector &oldbranches_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_, - struct stat *st_) -{ - int rv; - int err; - - err = -ENOENT; - for(const auto &oldbranch : oldbranches_) - { - rv = ::_link_preserve_path_core(oldbranch->path, - oldfusepath_, - newfusepath_, - st_); - if(err < 0) - err = rv; - } - - return err; -} - -static -int -_link_preserve_path(const Policy::Action &actionFunc_, - const Branches::Ptr branches_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_, - struct stat *st_) -{ - int rv; - std::vector oldbranches; - - rv = actionFunc_(branches_,oldfusepath_,oldbranches); - if(rv < 0) - return rv; - if(oldbranches.empty()) - return -ENOENT; - - return ::_link_preserve_path_loop(oldbranches, - oldfusepath_, - newfusepath_, - st_); -} - -static -int -_link(const fs::path &oldpath_, - const fs::path &newpath_, - struct stat *st_) -{ - if(cfg.func.create.policy.path_preserving() && !cfg.ignorepponrename) - return ::_link_preserve_path(cfg.func.link.policy, - cfg.branches, - oldpath_, - newpath_, - st_); - - return ::_link_create_path(cfg.func.getattr.policy, - cfg.func.link.policy, - cfg.branches, - oldpath_, - newpath_); + auto impl = cfg.create.impl(); + return impl && impl->path_preserving(); } static @@ -202,7 +54,27 @@ _link(const fuse_req_ctx_t *ctx_, { int rv; - rv = ::_link(oldpath_,newpath_,st_); + // Honor path-preserving create policies: don't clone the new path + // structure into branches that don't already have it. Fall through + // to the link_exdev handling (PASSTHROUGH/REL_SYMLINK/etc.). + if(!cfg.ignorepponrename && ::_create_is_path_preserving()) + { + Branches::Ptr branches = cfg.branches; + const fs::path newdir = newpath_.parent_path(); + bool any_target = false; + for(const auto &branch : *branches) + { + if(branch.ro()) + continue; + if(fs::exists(branch.path,oldpath_) && + fs::exists(branch.path,newdir)) + { any_target = true; break; } + } + if(!any_target) + return -EXDEV; + } + + rv = cfg.link(cfg.branches,oldpath_,newpath_); if(rv < 0) return rv; @@ -237,8 +109,7 @@ _link_exdev_rel_symlink(const fuse_req_ctx_t *ctx_, static int _link_exdev_abs_base_symlink(const fuse_req_ctx_t *ctx_, - const Policy::Search &openPolicy_, - const Branches::Ptr ibranches_, + const Branches &branches_, const fs::path &oldpath_, const fs::path &newpath_, struct stat *st_, @@ -246,25 +117,26 @@ _link_exdev_abs_base_symlink(const fuse_req_ctx_t *ctx_, { int rv; fs::path target; - std::vector obranches; - rv = openPolicy_(ibranches_,oldpath_,obranches); - if(rv < 0) - return rv; - if(obranches.empty()) - return -ENOENT; + for(const auto &branch : branches_) + { + if(!fs::exists(branch.path,oldpath_)) + continue; - target = obranches[0]->path / oldpath_; + target = branch.path / oldpath_; - rv = FUSE::symlink(ctx_,target.c_str(),newpath_); - if(rv == 0) - rv = FUSE::getattr(ctx_,oldpath_,st_,timeouts_); + rv = FUSE::symlink(ctx_,target.c_str(),newpath_); + if(rv == 0) + rv = FUSE::getattr(ctx_,oldpath_,st_,timeouts_); - // Disable caching since we created a symlink but should be a regular. - timeouts_->attr = 0; - timeouts_->entry = 0; + // Disable caching since we created a symlink but should be a regular. + timeouts_->attr = 0; + timeouts_->entry = 0; - return rv; + return rv; + } + + return -ENOENT; } static @@ -277,7 +149,6 @@ _link_exdev_abs_pool_symlink(const fuse_req_ctx_t *ctx_, fuse_timeouts_t *timeouts_) { int rv; - StrVec basepaths; fs::path target; target = mount_ / oldpath_; @@ -313,7 +184,6 @@ _link_exdev(const fuse_req_ctx_t *ctx_, timeouts_); case LinkEXDEV::ENUM::ABS_BASE_SYMLINK: return ::_link_exdev_abs_base_symlink(ctx_, - cfg.func.open.policy, cfg.branches, oldpath_, newpath_, diff --git a/src/fuse_listxattr.cpp b/src/fuse_listxattr.cpp index ce84e64e..6d1b8327 100644 --- a/src/fuse_listxattr.cpp +++ b/src/fuse_listxattr.cpp @@ -19,132 +19,15 @@ #include "fuse_listxattr.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_llistxattr.hpp" #include "xattr.hpp" -#include "fuse.h" - -#include -#include -#include - - -static -ssize_t -_listxattr_size(const std::vector &branches_, - const fs::path &fusepath_) -{ - ssize_t size; - ssize_t err; - bool success; - fs::path fullpath; - - if(branches_.empty()) - return -ENOENT; - - size = 0; - err = -ENOENT; - success = false; - for(const auto branch : branches_) - { - ssize_t rv; - - fullpath = branch->path / fusepath_; - - rv = fs::llistxattr(fullpath,NULL,0); - if(rv < 0) - { - if(err == -ENOENT) - err = rv; - continue; - } - - success = true; - size += rv; - } - - if(success == false) - return err; - - return size; -} - -static -ssize_t -_listxattr(const std::vector &branches_, - const fs::path &fusepath_, - char *list_, - size_t size_) -{ - ssize_t rv; - ssize_t size; - ssize_t err; - bool success; - fs::path fullpath; - - if(size_ == 0) - return ::_listxattr_size(branches_,fusepath_); - - size = 0; - err = -ENOENT; - success = false; - for(const auto branch : branches_) - { - fullpath = branch->path / fusepath_; - - rv = fs::llistxattr(fullpath,list_,size_); - if(rv < 0) - { - if(rv == -ERANGE) - return -ERANGE; - if(err == -ENOENT) - err = rv; - continue; - } - - if((size_t)rv > size_) - return -ERANGE; - - success = true; - list_ += rv; - size_ -= rv; - size += rv; - } - - if(success == false) - return err; - - return size; -} - -static -int -_listxattr(const Policy::Search &searchFunc_, - const Branches::Ptr ibranches_, - const fs::path &fusepath_, - char *list_, - const size_t size_) -{ - int rv; - std::vector obranches; - - rv = searchFunc_(ibranches_,fusepath_,obranches); - if(rv < 0) - return rv; - - if(size_ == 0) - return ::_listxattr_size(obranches,fusepath_); - - return ::_listxattr(obranches,fusepath_,list_,size_); -} - int FUSE::listxattr(const fuse_req_ctx_t *ctx_, const char *fusepath_, char *list_, - size_t size_) + const size_t size_) { + ssize_t rv; const fs::path fusepath{fusepath_}; if(Config::is_ctrl_file(fusepath)) @@ -160,9 +43,10 @@ FUSE::listxattr(const fuse_req_ctx_t *ctx_, return -ENOSYS; } - return ::_listxattr(cfg.func.listxattr.policy, - cfg.branches, - fusepath, - list_, - size_); + rv = cfg.listxattr(cfg.branches, + fusepath, + list_, + size_); + + return rv; } diff --git a/src/fuse_mkdir.cpp b/src/fuse_mkdir.cpp index bd3e9c68..b804b203 100644 --- a/src/fuse_mkdir.cpp +++ b/src/fuse_mkdir.cpp @@ -20,121 +20,11 @@ #include "config.hpp" #include "errno.hpp" -#include "error.hpp" -#include "fs_acl.hpp" -#include "fs_clonepath.hpp" -#include "fs_mkdir_as.hpp" #include "fs_path.hpp" -#include "policy.hpp" #include "ugid.hpp" #include "fuse.h" -#include - - -static -int -_mkdir_core(const ugid_t ugid_, - const fs::path &fullpath_, - mode_t mode_, - const mode_t umask_) -{ - if(!fs::acl::dir_has_defaults(fullpath_)) - mode_ &= ~umask_; - - return fs::mkdir_as(ugid_,fullpath_,mode_); -} - -static -int -_mkdir_loop_core(const ugid_t ugid_, - const fs::path &createpath_, - const fs::path &fusepath_, - const mode_t mode_, - const mode_t umask_) -{ - int rv; - fs::path fullpath; - - fullpath = createpath_ / fusepath_; - - rv = ::_mkdir_core(ugid_,fullpath,mode_,umask_); - - return rv; -} - -static -int -_mkdir_loop(const ugid_t ugid_, - const Branch *existingbranch_, - const std::vector &createbranches_, - const fs::path &fusepath_, - const fs::path &fusedirpath_, - const mode_t mode_, - const mode_t umask_) -{ - int rv; - Err err; - - for(const auto &createbranch : createbranches_) - { - rv = fs::clonepath(existingbranch_->path, - createbranch->path, - fusedirpath_); - if(rv < 0) - { - err = rv; - continue; - } - - err = ::_mkdir_loop_core(ugid_, - createbranch->path, - fusepath_, - mode_, - umask_); - } - - return err; -} - -static -int -_mkdir(const ugid_t ugid_, - const Policy::Search &getattrPolicy_, - const Policy::Create &mkdirPolicy_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const mode_t mode_, - const mode_t umask_) -{ - int rv; - fs::path fusedirpath; - std::vector createbranches; - std::vector existingbranches; - - fusedirpath = fusepath_.parent_path(); - - rv = getattrPolicy_(branches_,fusedirpath,existingbranches); - if(rv < 0) - return rv; - if(existingbranches.empty()) - return -ENOENT; - - rv = mkdirPolicy_(branches_,fusedirpath,createbranches); - if(rv < 0) - return rv; - if(createbranches.empty()) - return -ENOENT; - - return ::_mkdir_loop(ugid_, - existingbranches[0], - createbranches, - fusepath_, - fusedirpath, - mode_, - umask_); -} int FUSE::mkdir(const fuse_req_ctx_t *ctx_, @@ -143,24 +33,13 @@ FUSE::mkdir(const fuse_req_ctx_t *ctx_, { int rv; const fs::path fusepath{fusepath_}; + const ugid_t ugid(ctx_); - rv = ::_mkdir(ctx_, - cfg.func.getattr.policy, - cfg.func.mkdir.policy, - cfg.branches, - fusepath, - mode_, - ctx_->umask); + rv = cfg.mkdir(ugid,cfg.branches,fusepath,mode_,ctx_->umask); if(rv == -EROFS) { cfg.branches.find_and_set_mode_ro(); - rv = ::_mkdir(ctx_, - cfg.func.getattr.policy, - cfg.func.mkdir.policy, - cfg.branches, - fusepath, - mode_, - ctx_->umask); + rv = cfg.mkdir(ugid,cfg.branches,fusepath,mode_,ctx_->umask); } return rv; diff --git a/src/fuse_mknod.cpp b/src/fuse_mknod.cpp index 889f1a96..8eb3f699 100644 --- a/src/fuse_mknod.cpp +++ b/src/fuse_mknod.cpp @@ -19,129 +19,9 @@ #include "fuse_mknod.hpp" #include "config.hpp" -#include "errno.hpp" -#include "error.hpp" -#include "fs_acl.hpp" -#include "fs_mknod_as.hpp" -#include "fs_clonepath.hpp" -#include "fs_path.hpp" #include "ugid.hpp" -#include "fuse.h" - #include -#include - - -static -inline -int -_mknod_core(const ugid_t ugid_, - const fs::path &fullpath_, - mode_t mode_, - const mode_t umask_, - const dev_t dev_) -{ - if(!fs::acl::dir_has_defaults(fullpath_)) - mode_ &= ~umask_; - - return fs::mknod_as(ugid_,fullpath_,mode_,dev_); -} - -static -int -_mknod_loop_core(const ugid_t ugid_, - const fs::path &createbranch_, - const fs::path &fusepath_, - const mode_t mode_, - const mode_t umask_, - const dev_t dev_) -{ - int rv; - fs::path fullpath; - - fullpath = createbranch_ / fusepath_; - - rv = ::_mknod_core(ugid_,fullpath,mode_,umask_,dev_); - - return rv; -} - -static -int -_mknod_loop(const ugid_t ugid_, - const fs::path &existingbranch_, - const std::vector &createbranches_, - const fs::path &fusepath_, - const fs::path &fusedirpath_, - const mode_t mode_, - const mode_t umask_, - const dev_t dev_) -{ - int rv; - Err err; - - for(const auto &createbranch : createbranches_) - { - rv = fs::clonepath(existingbranch_, - createbranch->path, - fusedirpath_); - if(rv < 0) - { - err = rv; - continue; - } - - err = ::_mknod_loop_core(ugid_, - createbranch->path, - fusepath_, - mode_, - umask_, - dev_); - } - - return err; -} - -static -int -_mknod(const ugid_t ugid_, - const Policy::Search &searchFunc_, - const Policy::Create &createFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const mode_t mode_, - const mode_t umask_, - const dev_t dev_) -{ - int rv; - fs::path fusedirpath; - std::vector createbranches; - std::vector existingbranches; - - fusedirpath = fusepath_.parent_path(); - - rv = searchFunc_(branches_,fusedirpath,existingbranches); - if(rv < 0) - return rv; - if(existingbranches.empty()) - return -ENOENT; - - rv = createFunc_(branches_,fusedirpath,createbranches); - if(rv < 0) - return rv; - if(createbranches.empty()) - return -ENOENT; - - return ::_mknod_loop(ugid_, - existingbranches[0]->path, - createbranches, - fusepath_, - fusedirpath, - mode_, - umask_, - dev_); -} int FUSE::mknod(const fuse_req_ctx_t *ctx_, @@ -151,26 +31,13 @@ FUSE::mknod(const fuse_req_ctx_t *ctx_, { int rv; const fs::path fusepath{fusepath_}; + const ugid_t ugid(ctx_); - rv = ::_mknod(ctx_, - cfg.func.getattr.policy, - cfg.func.mknod.policy, - cfg.branches, - fusepath, - mode_, - ctx_->umask, - rdev_); + rv = cfg.mknod(ugid,cfg.branches,fusepath,mode_,rdev_,ctx_->umask); if(rv == -EROFS) { cfg.branches.find_and_set_mode_ro(); - rv = ::_mknod(ctx_, - cfg.func.getattr.policy, - cfg.func.mknod.policy, - cfg.branches, - fusepath, - mode_, - ctx_->umask, - rdev_); + rv = cfg.mknod(ugid,cfg.branches,fusepath,mode_,rdev_,ctx_->umask); } return rv; diff --git a/src/fuse_open.cpp b/src/fuse_open.cpp index 1d247079..9ef74bf0 100644 --- a/src/fuse_open.cpp +++ b/src/fuse_open.cpp @@ -121,58 +121,6 @@ _rdonly(const int flags_) return ((flags_ & O_ACCMODE) == O_RDONLY); } -static -int -_lchmod_and_open_if_not_writable_and_empty(const fs::path &fullpath_, - const int flags_) -{ - int rv; - struct stat st; - - rv = fs::lstat(fullpath_,&st); - if(rv < 0) - return -EACCES; - - if(StatUtil::writable(st)) - return -EACCES; - - rv = fs::lchmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP)); - if(rv < 0) - return -EACCES; - - rv = fs::open(fullpath_,flags_); - if(rv < 0) - return -EACCES; - - fs::fchmod(rv,st.st_mode); - - return rv; -} - -static -int -_nfsopenhack(const fs::path &fullpath_, - const int flags_, - const NFSOpenHack nfsopenhack_) -{ - switch(nfsopenhack_) - { - default: - case NFSOpenHack::ENUM::OFF: - return -EACCES; - case NFSOpenHack::ENUM::GIT: - if(::_rdonly(flags_)) - return -EACCES; - if(fullpath_.string().find("/.git/") == std::string::npos) - return -EACCES; - return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); - case NFSOpenHack::ENUM::ALL: - if(::_rdonly(flags_)) - return -EACCES; - return ::_lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_); - } -} - /* The kernel expects being able to issue read requests when running with writeback caching enabled so we must change O_WRONLY to @@ -260,30 +208,6 @@ _config_to_ffi_flags(const int tid_, ffi_->parallel_direct_writes = ffi_->direct_io; } -static -int -_open_path(const fs::path &filepath_, - const Branch *branch_, - const fs::path &fusepath_, - fuse_file_info_t *ffi_, - const NFSOpenHack nfsopenhack_) -{ - int fd; - FileInfo *fi; - - fd = fs::openat(AT_FDCWD,filepath_,ffi_->flags); - if(fd == -EACCES) - fd = ::_nfsopenhack(filepath_,ffi_->flags,nfsopenhack_); - if(fd < 0) - return fd; - - fi = new FileInfo(fd,branch_,fusepath_,ffi_->direct_io); - - ffi_->fh = fi->to_fh(); - - return 0; -} - static int _open_fd(const int fd_, @@ -305,39 +229,6 @@ _open_fd(const int fd_, return 0; } -static -int -_open(const Policy::Search &searchFunc_, - const Branches::Ptr ibranches_, - const fs::path &fusepath_, - fuse_file_info_t *ffi_, - const bool link_cow_, - const NFSOpenHack nfsopenhack_) -{ - int rv; - fs::path filepath; - std::vector obranches; - - rv = searchFunc_(ibranches_,fusepath_,obranches); - if(rv < 0) - return rv; - if(obranches.empty()) - return -ENOENT; - - filepath = obranches[0]->path / fusepath_; - - if(link_cow_ && fs::cow::is_eligible(filepath,ffi_->flags)) - fs::cow::break_link(filepath); - - rv = ::_open_path(filepath, - obranches[0], - fusepath_, - ffi_, - nfsopenhack_); - - return rv; -} - constexpr u64 _(const PassthroughIOEnum e_, @@ -424,12 +315,11 @@ _open(const fuse_req_ctx_t *ctx_, // Was not open, do first open, try to insert, if someone beat us // to it in another thread then throw it away and try again. - rv = ::_open(cfg.func.open.policy, - cfg.branches, - fusepath_, - ffi_, - cfg.link_cow, - cfg.nfsopenhack); + rv = cfg.open(cfg.branches, + fusepath_, + ffi_, + cfg.link_cow, + cfg.nfsopenhack); if(rv < 0) return rv; diff --git a/src/fuse_readdir_cor.cpp b/src/fuse_readdir_cor.cpp index a8ef5c21..6c9f0f88 100644 --- a/src/fuse_readdir_cor.cpp +++ b/src/fuse_readdir_cor.cpp @@ -22,7 +22,6 @@ #include "config.hpp" #include "dirinfo.hpp" -#include "error.hpp" FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_, @@ -81,9 +80,15 @@ _concurrent_readdir(ThreadPool &tp_, futures.emplace_back(std::move(rv)); } - Err err; + int err; + + err = 0; for(auto &future : futures) - err = future.get(); + { + const int rv = future.get(); + if(rv < 0) + err = rv; + } return err; } diff --git a/src/fuse_readdir_cosr_getdents.icpp b/src/fuse_readdir_cosr_getdents.icpp index 52a48adb..582b3911 100644 --- a/src/fuse_readdir_cosr_getdents.icpp +++ b/src/fuse_readdir_cosr_getdents.icpp @@ -22,7 +22,6 @@ #include "fs_getdents64.hpp" #include "hashset.hpp" #include "branches.hpp" -#include "error.hpp" #include "fs_close.hpp" #include "fs_open.hpp" #include "fs_path.hpp" @@ -82,7 +81,7 @@ _readdir(std::vector> &dh_futures_, const fs::path &rel_dirpath_, fuse_dirents_t *dirents_) { - Err err; + int err; HashSet names; fuse_msgbuf_t *buf; @@ -91,15 +90,18 @@ _readdir(std::vector> &dh_futures_, return -ENOMEM; DEFER { msgbuf_free(buf); }; + err = 0; for(auto &dh_future : dh_futures_) { DirRV dirrv; dirrv = dh_future.get(); - err = dirrv.fd; if(dirrv.fd < 0) - continue; + { + err = dirrv.fd; + continue; + } DEFER { fs::close(dirrv.fd); }; fs::inode::ReaddirCalc inodecalc(*dirrv.branch_path,rel_dirpath_); diff --git a/src/fuse_readdir_cosr_readdir.icpp b/src/fuse_readdir_cosr_readdir.icpp index e8689199..61748272 100644 --- a/src/fuse_readdir_cosr_readdir.icpp +++ b/src/fuse_readdir_cosr_readdir.icpp @@ -18,7 +18,6 @@ #pragma message "using readdir" -#include "error.hpp" #include "fs_closedir.hpp" #include "fs_inode.hpp" #include "fs_opendir.hpp" @@ -92,18 +91,21 @@ _readdir(std::vector> &dh_futures_, const fs::path &rel_dirpath_, fuse_dirents_t *dirents_) { - Err err; + int err; HashSet names; + err = 0; for(auto &dh_future : dh_futures_) { DirRV dirrv; dirrv = dh_future.get(); - err = dirrv.err; if(dirrv.dir == NULL) - continue; + { + err = dirrv.err; + continue; + } DEFER { fs::closedir(dirrv.dir); }; fs::inode::ReaddirCalc inodecalc(*dirrv.branch_path,rel_dirpath_); diff --git a/src/fuse_readdir_seq_getdents.icpp b/src/fuse_readdir_seq_getdents.icpp index da99bb56..7ae72fb9 100644 --- a/src/fuse_readdir_seq_getdents.icpp +++ b/src/fuse_readdir_seq_getdents.icpp @@ -19,7 +19,6 @@ #pragma message "using getdents" #include "dirinfo.hpp" -#include "error.hpp" #include "fs_close.hpp" #include "fs_getdents64.hpp" #include "fs_inode.hpp" @@ -37,7 +36,7 @@ _readdir(const Branches::Ptr &branches_, const fs::path &rel_dirpath_, fuse_dirents_t *dirents_) { - Err err; + int err; HashSet names; fs::path abs_dirpath; fuse_msgbuf_t *buf; @@ -49,6 +48,7 @@ _readdir(const Branches::Ptr &branches_, return -ENOMEM; DEFER { msgbuf_free(buf); }; + err = 0; for(const auto &branch : *branches_) { int fd; @@ -57,9 +57,11 @@ _readdir(const Branches::Ptr &branches_, abs_dirpath = branch.path / rel_dirpath_; fd = fs::open_dir_ro(abs_dirpath); - err = fd; if(fd < 0) - continue; + { + err = fd; + continue; + } DEFER{ fs::close(fd); }; while(true) diff --git a/src/fuse_readdir_seq_readdir.icpp b/src/fuse_readdir_seq_readdir.icpp index 4ca4225c..b64618b3 100644 --- a/src/fuse_readdir_seq_readdir.icpp +++ b/src/fuse_readdir_seq_readdir.icpp @@ -21,7 +21,6 @@ #include "fs_opendir.hpp" #include "fs_closedir.hpp" #include "fs_readdir.hpp" -#include "error.hpp" #include "hashset.hpp" #include "scope_guard/scope_guard.hpp" #include "fs_inode.hpp" @@ -50,12 +49,13 @@ _readdir(const Branches::Ptr &branches_, const fs::path &rel_dirpath_, fuse_dirents_t *dirents_) { - Err err; + int err; HashSet names; fs::path abs_dirpath; fuse_dirents_reset(dirents_); + err = 0; for(const auto &branch : *branches_) { DIR *dh; @@ -65,9 +65,11 @@ _readdir(const Branches::Ptr &branches_, errno = 0; dh = fs::opendir(abs_dirpath); - err = -errno; if(!dh) - continue; + { + err = -errno; + continue; + } DEFER{ fs::closedir(dh); }; for(dirent *de = fs::readdir(dh); diff --git a/src/fuse_readlink.cpp b/src/fuse_readlink.cpp index cb23a894..6e4e03b7 100644 --- a/src/fuse_readlink.cpp +++ b/src/fuse_readlink.cpp @@ -19,89 +19,6 @@ #include "fuse_readlink.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_lstat.hpp" -#include "fs_path.hpp" -#include "fs_readlink.hpp" -#include "symlinkify.hpp" - -#include "fuse.h" - -#include -#include - - -static -ssize_t -_readlink_core_symlinkify(const fs::path &fullpath_, - char *buf_, - const size_t bufsize_, - const time_t symlinkify_timeout_) -{ - ssize_t rv; - struct stat st; - std::string fullpath_str; - - fullpath_str = fullpath_.string(); - - rv = fs::lstat(fullpath_str,&st); - if(rv < 0) - return rv; - - if(!symlinkify::can_be_symlink(st,symlinkify_timeout_)) - return fs::readlink(fullpath_,buf_,bufsize_); - - rv = std::min(fullpath_str.size(),bufsize_); - memcpy(buf_,fullpath_str.c_str(),rv); - - return rv; -} - -static -ssize_t -_readlink_core(const fs::path &basepath_, - const fs::path &fusepath_, - char *buf_, - const size_t bufsize_, - const bool symlinkify_, - const time_t symlinkify_timeout_) -{ - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - if(symlinkify_) - return ::_readlink_core_symlinkify(fullpath,buf_,bufsize_,symlinkify_timeout_); - - return fs::readlink(fullpath,buf_,bufsize_); -} - -static -ssize_t -_readlink(const Policy::Search &searchFunc_, - const Branches::Ptr ibranches_, - const fs::path &fusepath_, - char *buf_, - const size_t bufsize_, - const bool symlinkify_, - const time_t symlinkify_timeout_) -{ - ssize_t rv; - std::vector obranches; - - rv = searchFunc_(ibranches_,fusepath_,obranches); - if(rv < 0) - return rv; - if(obranches.empty()) - return -ENOENT; - - return ::_readlink_core(obranches[0]->path, - fusepath_, - buf_, - bufsize_, - symlinkify_, - symlinkify_timeout_); -} ssize_t FUSE::readlink(const fuse_req_ctx_t *ctx_, @@ -114,11 +31,10 @@ FUSE::readlink(const fuse_req_ctx_t *ctx_, const fs::path fusepath{fusepath_}; - return ::_readlink(cfg.func.readlink.policy, - cfg.branches, - fusepath, - buf_, - bufsize_, - cfg.symlinkify, - cfg.symlinkify_timeout); + return cfg.readlink(cfg.branches, + fusepath, + buf_, + bufsize_, + cfg.symlinkify, + cfg.symlinkify_timeout); } diff --git a/src/fuse_removexattr.cpp b/src/fuse_removexattr.cpp index f43bb411..600e4aa1 100644 --- a/src/fuse_removexattr.cpp +++ b/src/fuse_removexattr.cpp @@ -20,79 +20,9 @@ #include "config.hpp" #include "errno.hpp" -#include "fs_lremovexattr.hpp" -#include "fs_path.hpp" -#include "policy_rv.hpp" #include "fuse.h" -#include - - -static -void -_removexattr_loop_core(const fs::path &basepath_, - const fs::path &fusepath_, - const char *attrname_, - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::lremovexattr(fullpath,attrname_); - - prv_->insert(rv,basepath_); -} - -static -void -_removexattr_loop(const std::vector &branches_, - const fs::path &fusepath_, - const char *attrname_, - PolicyRV *prv_) -{ - for(auto &branch : branches_) - { - ::_removexattr_loop_core(branch->path,fusepath_,attrname_,prv_); - } -} - -static -int -_removexattr(const Policy::Action &actionFunc_, - const Policy::Search &searchFunc_, - const Branches::Ptr ibranches_, - const fs::path &fusepath_, - const char *attrname_) -{ - int rv; - PolicyRV prv; - std::vector obranches; - - rv = actionFunc_(ibranches_,fusepath_,obranches); - if(rv < 0) - return rv; - if(obranches.empty()) - return -ENOENT; - - ::_removexattr_loop(obranches,fusepath_,attrname_,&prv); - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - obranches.clear(); - rv = searchFunc_(ibranches_,fusepath_,obranches); - if(rv < 0) - return rv; - if(obranches.empty()) - return -ENOENT; - - return prv.get_error(obranches[0]->path); -} - int FUSE::removexattr(const fuse_req_ctx_t *ctx_, const char *fusepath_, @@ -106,9 +36,7 @@ FUSE::removexattr(const fuse_req_ctx_t *ctx_, if(cfg.xattr.to_int()) return -cfg.xattr.to_int(); - return ::_removexattr(cfg.func.removexattr.policy, - cfg.func.getxattr.policy, - cfg.branches, - fusepath, - attrname_); + return cfg.removexattr(cfg.branches, + fusepath, + attrname_); } diff --git a/src/fuse_rename.cpp b/src/fuse_rename.cpp index 9aeebdba..debd94dd 100644 --- a/src/fuse_rename.cpp +++ b/src/fuse_rename.cpp @@ -19,9 +19,9 @@ #include "fuse_rename.hpp" #include "config.hpp" -#include "error.hpp" #include "errno.hpp" #include "fs_clonepath.hpp" +#include "fs_exists.hpp" #include "fs_link.hpp" #include "fs_mkdir_as.hpp" #include "fs_path.hpp" @@ -37,147 +37,6 @@ #include -static -bool -_contains(const std::vector &haystack_, - const char *needle_) -{ - for(auto &hay : haystack_) - { - if(hay->path == needle_) - return true; - } - - return false; -} - -static -bool -_contains(const std::vector &haystack_, - const std::string &needle_) -{ - return ::_contains(haystack_,needle_.c_str()); -} - -static -void -_remove(const StrVec &toremove_) -{ - for(auto &path : toremove_) - fs::remove(path); -} - -static -int -_rename_create_path(const Policy::Search &searchPolicy_, - const Policy::Action &actionPolicy_, - const Branches::Ptr branches_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_) -{ - int rv; - Err err; - StrVec toremove; - std::vector newbranches; - std::vector oldbranches; - fs::path oldfullpath; - fs::path newfullpath; - - rv = actionPolicy_(branches_,oldfusepath_,oldbranches); - if(rv < 0) - return rv; - if(oldbranches.empty()) - return -ENOENT; - - rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches); - if(rv < 0) - return rv; - if(newbranches.empty()) - return -ENOENT; - - for(auto &branch : *branches_) - { - newfullpath = branch.path / newfusepath_; - - if(!::_contains(oldbranches,branch.path)) - { - toremove.push_back(newfullpath); - continue; - } - - oldfullpath = branch.path / oldfusepath_; - - rv = fs::rename(oldfullpath,newfullpath); - if(rv < 0) - { - rv = fs::clonepath(newbranches[0]->path, - branch.path, - newfusepath_.parent_path()); - if(rv >= 0) - rv = fs::rename(oldfullpath,newfullpath); - } - - err = rv; - if(rv < 0) - toremove.push_back(oldfullpath); - } - - if(err == 0) - ::_remove(toremove); - - return err; -} - -static -int -_rename_preserve_path(const Policy::Action &actionPolicy_, - const Branches::Ptr branches_, - const fs::path &oldfusepath_, - const fs::path &newfusepath_) -{ - int rv; - bool success; - StrVec toremove; - std::vector oldbranches; - fs::path oldfullpath; - fs::path newfullpath; - - rv = actionPolicy_(branches_,oldfusepath_,oldbranches); - if(rv < 0) - return rv; - - success = false; - for(auto &branch : *branches_) - { - newfullpath = branch.path / newfusepath_; - - if(!::_contains(oldbranches,branch.path)) - { - toremove.push_back(newfullpath); - continue; - } - - oldfullpath = branch.path / oldfusepath_; - - rv = fs::rename(oldfullpath,newfullpath); - if(rv < 0) - { - toremove.push_back(oldfullpath); - continue; - } - - success = true; - } - - // TODO: probably should try to be nuanced here. - if(success == false) - return -EXDEV; - - ::_remove(toremove); - - return 0; -} - static void _rename_exdev_rename_back(const std::vector &branches_, @@ -201,8 +60,7 @@ _rename_exdev_rename_back(const std::vector &branches_, static int -_rename_exdev_rename_target(const Policy::Action &actionPolicy_, - const Branches::Ptr ibranches_, +_rename_exdev_rename_target(const Branches::Ptr &ibranches_, const fs::path &oldfusepath_, std::vector &obranches_) { @@ -210,9 +68,16 @@ _rename_exdev_rename_target(const Policy::Action &actionPolicy_, fs::path clonesrc; fs::path clonedst; - rv = actionPolicy_(ibranches_,oldfusepath_,obranches_); - if(rv < 0) - return rv; + for(auto &branch : *ibranches_) + { + if(branch.ro()) + continue; + if(!fs::exists(branch.path,oldfusepath_)) + continue; + obranches_.emplace_back(&branch); + } + if(obranches_.empty()) + return -ENOENT; for(auto &branch : obranches_) { @@ -249,8 +114,7 @@ _rename_exdev_rename_target(const Policy::Action &actionPolicy_, static int _rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_, - const Policy::Action &actionPolicy_, - const Branches::Ptr branches_, + const Branches::Ptr &branches_, const fs::path &oldfusepath_, const fs::path &newfusepath_) { @@ -259,7 +123,9 @@ _rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_, fs::path linkpath; std::vector branches; - rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches); + rv = ::_rename_exdev_rename_target(branches_, + oldfusepath_, + branches); if(rv < 0) return rv; @@ -278,8 +144,7 @@ _rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_, static int _rename_exdev_abs_symlink(const fuse_req_ctx_t *ctx_, - const Policy::Action &actionPolicy_, - const Branches::Ptr branches_, + const Branches::Ptr &branches_, const fs::path &mount_, const fs::path &oldfusepath_, const fs::path &newfusepath_) @@ -289,7 +154,9 @@ _rename_exdev_abs_symlink(const fuse_req_ctx_t *ctx_, fs::path linkpath; std::vector branches; - rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches); + rv = ::_rename_exdev_rename_target(branches_, + oldfusepath_, + branches); if(rv < 0) return rv; @@ -317,13 +184,11 @@ _rename_exdev(const fuse_req_ctx_t *ctx_, return -EXDEV; case RenameEXDEV::ENUM::REL_SYMLINK: return ::_rename_exdev_rel_symlink(ctx_, - cfg.func.rename.policy, cfg.branches, oldfusepath_, newfusepath_); case RenameEXDEV::ENUM::ABS_SYMLINK: return ::_rename_exdev_abs_symlink(ctx_, - cfg.func.rename.policy, cfg.branches, cfg.mountpoint, oldfusepath_, @@ -333,22 +198,45 @@ _rename_exdev(const fuse_req_ctx_t *ctx_, return -EXDEV; } +static +bool +_create_is_path_preserving() +{ + auto impl = cfg.create.impl(); + return impl && impl->path_preserving(); +} + static int _rename(const fs::path &oldpath_, const fs::path &newpath_) { - if(cfg.func.create.policy.path_preserving() && !cfg.ignorepponrename) - return ::_rename_preserve_path(cfg.func.rename.policy, - cfg.branches, - oldpath_, - newpath_); + // Honor ignorepponrename: when the create policy is path-preserving + // and the user has not opted out, refuse to clone the new path + // structure into branches that don't already have it. The fallback + // is the EXDEV path which lets the caller handle the move via + // copy+unlink or symlink per cfg.rename_exdev. + if(!cfg.ignorepponrename && ::_create_is_path_preserving()) + { + // Snapshot the Branches::Impl so the per-branch fs::exists + // calls below all observe the same set of branches even if + // cfg.branches is concurrently mutated. + Branches::Ptr branches = cfg.branches; + const fs::path newdir = newpath_.parent_path(); + bool any_target = false; + for(const auto &branch : *branches) + { + if(branch.ro()) + continue; + if(fs::exists(branch.path,oldpath_) && + fs::exists(branch.path,newdir)) + { any_target = true; break; } + } + if(!any_target) + return -EXDEV; + } - return ::_rename_create_path(cfg.func.getattr.policy, - cfg.func.rename.policy, - cfg.branches, - oldpath_, - newpath_); + return cfg.rename(cfg.branches,oldpath_,newpath_); } int diff --git a/src/fuse_rmdir.cpp b/src/fuse_rmdir.cpp index ccecd2dc..b9e0a4fd 100644 --- a/src/fuse_rmdir.cpp +++ b/src/fuse_rmdir.cpp @@ -19,131 +19,13 @@ #include "fuse_rmdir.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_path.hpp" -#include "fs_rmdir.hpp" -#include "fs_unlink.hpp" - -#include "fuse.h" - -#include - -#include - -// 1. First error sets the initial value -// 2. If we get -EEXIST or -ENOTEMPTY, it takes priority (overwrites anything) -// 3. If we already have -EEXIST or -ENOTEMPTY, keep them (don't overwrite) -// 4. Success (>= 0) only sets to 0 if we don't have a priority error -// 5. Any other error only overwrites if we don't have a priority error -struct RmdirErr -{ -private: - std::optional _err; - -public: - RmdirErr() - { - } - - operator int() - { - return (_err.has_value() ? _err.value() : -ENOENT); - } - - RmdirErr& - operator=(int v_) - { - if(!_err.has_value()) - _err = ((v_ >= 0) ? 0 : v_); - else if((v_ == -EEXIST) || (v_ == -ENOTEMPTY)) - _err = v_; - else if((*_err == -EEXIST) || (*_err == -ENOTEMPTY)) - ; - else if(v_ >= 0) - _err = 0; - else - _err = v_; - - return *this; - } - - bool - operator==(int v_) - { - if(_err.has_value()) - return (_err.value() == v_); - return false; - } -}; - -static -int -_should_unlink(int rv_, - FollowSymlinks followsymlinks_) -{ - return ((rv_ == -ENOTDIR) && - (followsymlinks_ != FollowSymlinks::ENUM::NEVER)); -} - -static -int -_rmdir_core(const fs::path &basepath_, - const fs::path &fusepath_, - const FollowSymlinks followsymlinks_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::rmdir(fullpath); - if(::_should_unlink(rv,followsymlinks_)) - rv = fs::unlink(fullpath); - - return rv; -} - -static -int -_rmdir_loop(const std::vector &branches_, - const fs::path &fusepath_, - const FollowSymlinks followsymlinks_) -{ - RmdirErr err; - - for(const auto &branch : branches_) - { - err = ::_rmdir_core(branch->path,fusepath_,followsymlinks_); - } - - return err; -} - -static -int -_rmdir(const Policy::Action &actionFunc_, - const Branches::Ptr branches_, - const FollowSymlinks followsymlinks_, - const fs::path &fusepath_) -{ - int rv; - std::vector branches; - - rv = actionFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - - return ::_rmdir_loop(branches,fusepath_,followsymlinks_); -} int FUSE::rmdir(const fuse_req_ctx_t *ctx_, const char *fusepath_) { - const fs::path fusepath{fusepath_}; + const fs::path fusepath{fusepath_}; - return ::_rmdir(cfg.func.rmdir.policy, - cfg.branches, - cfg.follow_symlinks, - fusepath); + return cfg.rmdir(cfg.branches, + fusepath); } diff --git a/src/fuse_setxattr.cpp b/src/fuse_setxattr.cpp index 260d2beb..e19c0d4c 100644 --- a/src/fuse_setxattr.cpp +++ b/src/fuse_setxattr.cpp @@ -100,111 +100,6 @@ _setxattr_ctrl_file(const char *attrname_, return rv; } -static -void -_setxattr_loop_core(const fs::path &basepath_, - const fs::path &fusepath_, - const char *attrname_, - const char *attrval_, - const size_t attrvalsize_, - const int flags_, - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::lsetxattr(fullpath,attrname_,attrval_,attrvalsize_,flags_); - - prv_->insert(rv,basepath_); -} - -static -void -_setxattr_loop(const std::vector &branches_, - const fs::path &fusepath_, - const char *attrname_, - const char *attrval_, - const size_t attrvalsize_, - const int flags_, - PolicyRV *prv_) -{ - for(auto &branch : branches_) - { - ::_setxattr_loop_core(branch->path, - fusepath_, - attrname_, - attrval_,attrvalsize_, - flags_, - prv_); - } -} - -static -int -_setxattr(const Policy::Action &setxattrPolicy_, - const Policy::Search &getxattrPolicy_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const char *attrname_, - const char *attrval_, - const size_t attrvalsize_, - const int flags_) -{ - int rv; - PolicyRV prv; - std::vector branches; - - rv = setxattrPolicy_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - ::_setxattr_loop(branches,fusepath_,attrname_,attrval_,attrvalsize_,flags_,&prv); - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - branches.clear(); - rv = getxattrPolicy_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - return prv.get_error(branches[0]->path); -} - -static -int -_setxattr(const fuse_req_ctx_t *ctx_, - const fs::path &fusepath_, - const char *attrname_, - const char *attrval_, - size_t attrvalsize_, - int flags_) -{ - if((cfg.security_capability == false) && - ::_is_attrname_security_capability(attrname_)) - return -ENOATTR; - - if(cfg.xattr.to_int()) - return -cfg.xattr.to_int(); - - return ::_setxattr(cfg.func.setxattr.policy, - cfg.func.getxattr.policy, - cfg.branches, - fusepath_, - attrname_, - attrval_, - attrvalsize_, - flags_); -} - - int FUSE::setxattr(const fuse_req_ctx_t *ctx_, const char *fusepath_, @@ -221,10 +116,17 @@ FUSE::setxattr(const fuse_req_ctx_t *ctx_, attrvalsize_, flags_); - return ::_setxattr(ctx_, - fusepath, - attrname_, - attrval_, - attrvalsize_, - flags_); + if((cfg.security_capability == false) && + ::_is_attrname_security_capability(attrname_)) + return -ENOATTR; + + if(cfg.xattr.to_int()) + return -cfg.xattr.to_int(); + + return cfg.setxattr(cfg.branches, + fusepath, + attrname_, + attrval_, + attrvalsize_, + flags_); } diff --git a/src/fuse_statx_supported.icpp b/src/fuse_statx_supported.icpp index 5ec930a8..f8d43a34 100644 --- a/src/fuse_statx_supported.icpp +++ b/src/fuse_statx_supported.icpp @@ -16,7 +16,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - #include "fuse_statx.hpp" #include "config.hpp" @@ -30,49 +29,11 @@ #include "symlinkify.hpp" #include "ugid.hpp" -#include "fmt/core.h" - #include "fuse.h" #include -static -void -_set_stat_if_leads_to_dir(const fs::path &path_, - struct fuse_statx *st_) -{ - int rv; - struct fuse_statx st; - - rv = fs::statx(AT_FDCWD,path_,AT_SYMLINK_FOLLOW,STATX_TYPE,&st); - if(rv < 0) - return; - - if(S_ISDIR(st.mode)) - *st_ = st; - - return; -} - -static -void -_set_stat_if_leads_to_reg(const fs::path &path_, - struct fuse_statx *st_) -{ - int rv; - struct fuse_statx st; - - rv = fs::statx(AT_FDCWD,path_,AT_SYMLINK_FOLLOW,STATX_TYPE,&st); - if(rv < 0) - return; - - if(S_ISREG(st.mode)) - *st_ = st; - - return; -} - static int _statx_fake_root(struct fuse_statx *st_, @@ -121,84 +82,24 @@ _statx_controlfile(struct fuse_statx *st_, return 0; } -static -int -_statx(const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const uint32_t flags_, - const uint32_t mask_, - struct fuse_statx *st_, - const bool symlinkify_, - const time_t symlinkify_timeout_, - FollowSymlinks followsymlinks_) -{ - int rv; - fs::path fullpath; - std::vector branches; - - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - fullpath = branches[0]->path / fusepath_; - - switch(followsymlinks_) - { - case FollowSymlinks::ENUM::NEVER: - rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_); - break; - case FollowSymlinks::ENUM::DIRECTORY: - rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_); - if((rv >= 0) && S_ISLNK(st_->mode)) - ::_set_stat_if_leads_to_dir(fullpath,st_); - break; - case FollowSymlinks::ENUM::REGULAR: - rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_); - if((rv >= 0) && S_ISLNK(st_->mode)) - ::_set_stat_if_leads_to_reg(fullpath,st_); - break; - case FollowSymlinks::ENUM::ALL: - rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_FOLLOW,mask_,st_); - if(rv < 0) - rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_); - break; - } - - if(rv < 0) - return rv; - - if(symlinkify_ && symlinkify::can_be_symlink(*st_,symlinkify_timeout_)) - symlinkify::convert(fullpath,st_); - - fs::inode::calc(branches[0]->path, - fusepath_, - st_); - - return 0; -} - static int _statx(const fs::path &fusepath_, - const uint32_t flags_, - const uint32_t mask_, + const u32 flags_, + const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { int rv; - rv = ::_statx(cfg.func.getattr.policy, - cfg.branches, - fusepath_, - flags_, - mask_, - st_, - cfg.symlinkify, - cfg.symlinkify_timeout, - cfg.follow_symlinks); + rv = cfg.statx(cfg.branches, + fusepath_, + flags_, + mask_, + st_, + cfg.follow_symlinks, + cfg.symlinkify, + cfg.symlinkify_timeout); if((rv < 0) && Config::is_rootdir(fusepath_)) return ::_statx_fake_root(st_,timeout_); @@ -237,8 +138,8 @@ _statx(const FileInfo *fi_, int FUSE::statx(const fuse_req_ctx_t *ctx_, const char *fusepath_, - const uint32_t flags_, - const uint32_t mask_, + const u32 flags_, + const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { @@ -256,9 +157,9 @@ FUSE::statx(const fuse_req_ctx_t *ctx_, int FUSE::statx_fh(const fuse_req_ctx_t *ctx_, - const uint64_t fh_, - const uint32_t flags_, - const uint32_t mask_, + const u64 fh_, + const u32 flags_, + const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { diff --git a/src/fuse_symlink.cpp b/src/fuse_symlink.cpp index 5dbc818a..df0cd036 100644 --- a/src/fuse_symlink.cpp +++ b/src/fuse_symlink.cpp @@ -20,7 +20,6 @@ #include "config.hpp" #include "errno.hpp" -#include "error.hpp" #include "fs_clonepath.hpp" #include "fs_lstat.hpp" #include "fs_path.hpp" @@ -32,104 +31,10 @@ #include "fuse.h" #include +#include #include -static -int -_symlink_loop_core(const ugid_t ugid_, - const fs::path &newbranch_, - const char *target_, - const fs::path &linkpath_, - struct stat *st_) -{ - int rv; - fs::path fullnewpath; - - fullnewpath = newbranch_ / linkpath_; - - rv = fs::symlink_as(ugid_,target_,fullnewpath); - if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0)) - { - fs::lstat(fullnewpath,st_); - if(st_->st_ino != 0) - fs::inode::calc(newbranch_, - linkpath_, - st_); - } - - return rv; -} - -static -int -_symlink_loop(const ugid_t ugid_, - const fs::path &existingbranch_, - const std::vector &newbranches_, - const char *target_, - const fs::path &linkpath_, - const fs::path &newdirpath_, - struct stat *st_) -{ - int rv; - Err err; - - for(auto &newbranch :newbranches_) - { - rv = fs::clonepath(existingbranch_, - newbranch->path, - newdirpath_); - if(rv < 0) - err = rv; - else - err = ::_symlink_loop_core(ugid_, - newbranch->path, - target_, - linkpath_, - st_); - } - - return err; -} - -static -int -_symlink(const ugid_t ugid_, - const Policy::Search &searchFunc_, - const Policy::Create &createFunc_, - const Branches::Ptr branches_, - const char *target_, - const fs::path &linkpath_, - struct stat *st_) -{ - int rv; - fs::path newdirpath; - std::vector newbranches; - std::vector existingbranches; - - newdirpath = linkpath_.parent_path(); - - rv = searchFunc_(branches_,newdirpath,existingbranches); - if(rv < 0) - return rv; - if(existingbranches.empty()) - return -ENOENT; - - rv = createFunc_(branches_,newdirpath,newbranches); - if(rv < 0) - return rv; - if(newbranches.empty()) - return -ENOENT; - - return ::_symlink_loop(ugid_, - existingbranches[0]->path, - newbranches, - target_, - linkpath_, - newdirpath, - st_); -} - int FUSE::symlink(const fuse_req_ctx_t *ctx_, const char *target_, @@ -138,24 +43,13 @@ FUSE::symlink(const fuse_req_ctx_t *ctx_, fuse_timeouts_t *timeouts_) { int rv; + const ugid_t ugid(ctx_); - rv = ::_symlink(ctx_, - cfg.func.getattr.policy, - cfg.func.symlink.policy, - cfg.branches, - target_, - linkpath_, - st_); + rv = cfg.symlink(ugid,cfg.branches,target_,linkpath_,st_); if(rv == -EROFS) { cfg.branches.find_and_set_mode_ro(); - rv = ::_symlink(ctx_, - cfg.func.getattr.policy, - cfg.func.symlink.policy, - cfg.branches, - target_, - linkpath_, - st_); + rv = cfg.symlink(ugid,cfg.branches,target_,linkpath_,st_); } if(timeouts_ != NULL) diff --git a/src/fuse_truncate.cpp b/src/fuse_truncate.cpp index 6ea0f8a1..7396baf1 100644 --- a/src/fuse_truncate.cpp +++ b/src/fuse_truncate.cpp @@ -19,80 +19,6 @@ #include "fuse_truncate.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_path.hpp" -#include "fs_truncate.hpp" -#include "policy_rv.hpp" - -#include "fuse.h" - -#include -#include - - -static -void -_truncate_loop_core(const fs::path &basepath_, - const fs::path &fusepath_, - const off_t size_, - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::truncate(fullpath,size_); - - prv_->insert(rv,basepath_); -} - -static -void -_truncate_loop(const std::vector &branches_, - const fs::path &fusepath_, - const off_t size_, - PolicyRV *prv_) -{ - for(auto &branch : branches_) - { - ::_truncate_loop_core(branch->path,fusepath_,size_,prv_); - } -} - -static -int -_truncate(const Policy::Action &actionFunc_, - const Policy::Search &searchFunc_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const off_t size_) -{ - int rv; - PolicyRV prv; - std::vector branches; - - rv = actionFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - ::_truncate_loop(branches,fusepath_,size_,&prv); - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - branches.clear(); - rv = searchFunc_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - return prv.get_error(branches[0]->path); -} int FUSE::truncate(const fuse_req_ctx_t *ctx_, @@ -101,9 +27,5 @@ FUSE::truncate(const fuse_req_ctx_t *ctx_, { const fs::path fusepath{fusepath_}; - return ::_truncate(cfg.func.truncate.policy, - cfg.func.getattr.policy, - cfg.branches, - fusepath, - size_); + return cfg.truncate(cfg.branches,fusepath,size_); } diff --git a/src/fuse_unlink.cpp b/src/fuse_unlink.cpp index abd7ee35..e08f1e65 100644 --- a/src/fuse_unlink.cpp +++ b/src/fuse_unlink.cpp @@ -19,51 +19,6 @@ #include "fuse_unlink.hpp" #include "config.hpp" -#include "errno.hpp" -#include "error.hpp" -#include "fs_path.hpp" -#include "fs_unlink.hpp" - -#include "fuse.h" - -#include - -#include - - -static -int -_unlink_loop(const std::vector &branches_, - const fs::path &fusepath_) -{ - Err err; - fs::path fullpath; - - for(const auto &branch : branches_) - { - fullpath = branch->path / fusepath_; - - err = fs::unlink(fullpath); - } - - return err; -} - -static -int -_unlink(const Policy::Action &unlinkPolicy_, - const Branches::Ptr branches_, - const fs::path &fusepath_) -{ - int rv; - std::vector branches; - - rv = unlinkPolicy_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - - return ::_unlink_loop(branches,fusepath_); -} int FUSE::unlink(const fuse_req_ctx_t *ctx_, @@ -71,7 +26,5 @@ FUSE::unlink(const fuse_req_ctx_t *ctx_, { const fs::path fusepath{fusepath_}; - return ::_unlink(cfg.func.unlink.policy, - cfg.branches, - fusepath); + return cfg.unlink(cfg.branches,fusepath); } diff --git a/src/fuse_utimens.cpp b/src/fuse_utimens.cpp index 7fbec390..c8280c86 100644 --- a/src/fuse_utimens.cpp +++ b/src/fuse_utimens.cpp @@ -19,79 +19,6 @@ #include "fuse_utimens.hpp" #include "config.hpp" -#include "errno.hpp" -#include "fs_lutimens.hpp" -#include "fs_path.hpp" -#include "policy_rv.hpp" - -#include "fuse.h" - -#include - - -static -void -_utimens_loop_core(const fs::path &basepath_, - const fs::path &fusepath_, - const timespec ts_[2], - PolicyRV *prv_) -{ - int rv; - fs::path fullpath; - - fullpath = basepath_ / fusepath_; - - rv = fs::lutimens(fullpath,ts_); - - prv_->insert(rv,basepath_); -} - -static -void -_utimens_loop(const std::vector &branches_, - const fs::path &fusepath_, - const timespec ts_[2], - PolicyRV *prv_) -{ - for(auto &branch : branches_) - { - ::_utimens_loop_core(branch->path,fusepath_,ts_,prv_); - } -} - -static -int -_utimens(const Policy::Action &utimensPolicy_, - const Policy::Search &getattrPolicy_, - const Branches::Ptr branches_, - const fs::path &fusepath_, - const timespec ts_[2]) -{ - int rv; - PolicyRV prv; - std::vector branches; - - rv = utimensPolicy_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - ::_utimens_loop(branches,fusepath_,ts_,&prv); - if(prv.errors.empty()) - return 0; - if(prv.successes.empty()) - return prv.errors[0].rv; - - branches.clear(); - rv = getattrPolicy_(branches_,fusepath_,branches); - if(rv < 0) - return rv; - if(branches.empty()) - return -ENOENT; - - return prv.get_error(branches[0]->path); -} int FUSE::utimens(const fuse_req_ctx_t *ctx_, @@ -100,9 +27,5 @@ FUSE::utimens(const fuse_req_ctx_t *ctx_, { const fs::path fusepath{fusepath_}; - return ::_utimens(cfg.func.utimens.policy, - cfg.func.getattr.policy, - cfg.branches, - fusepath, - ts_); + return cfg.utimens(cfg.branches,fusepath,ts_); } diff --git a/src/fuse_write.cpp b/src/fuse_write.cpp index b91c7ba0..ffff4891 100644 --- a/src/fuse_write.cpp +++ b/src/fuse_write.cpp @@ -59,8 +59,7 @@ _move_and_pwrite(const char *buf_, if(cfg.moveonenospc.enabled == false) return err_; - rv = fs::movefile_and_open_as_root(cfg.moveonenospc.policy, - cfg.branches, + rv = fs::movefile_and_open_as_root(cfg.branches, fi_->branch.path, fi_->fusepath, fi_->fd); @@ -90,8 +89,7 @@ _move_and_pwriten(char const *buf_, if(cfg.moveonenospc.enabled == false) return err_; - rv = fs::movefile_and_open_as_root(cfg.moveonenospc.policy, - cfg.branches, + rv = fs::movefile_and_open_as_root(cfg.branches, fi_->branch.path, fi_->fusepath, fi_->fd); diff --git a/src/option_parser.cpp b/src/option_parser.cpp index 7d885722..3b8a4499 100644 --- a/src/option_parser.cpp +++ b/src/option_parser.cpp @@ -25,7 +25,6 @@ #include "fs_statvfs_cache.hpp" #include "hw_cpu.hpp" #include "num.hpp" -#include "policy.hpp" #include "str.hpp" #include "syslog.hpp" #include "version.hpp" diff --git a/src/policies.cpp b/src/policies.cpp deleted file mode 100644 index 9e7e22f6..00000000 --- a/src/policies.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policies.hpp" - -#define IFERTA(X) if(name_ == #X) return &Policies::Action::X; -#define IFERTC(X) if(name_ == #X) return &Policies::Create::X; -#define IFERTS(X) if(name_ == #X) return &Policies::Search::X; - -#define IFERT(FUNC) \ - FUNC(all) \ - FUNC(epall) \ - FUNC(epff) \ - FUNC(eplfs) \ - FUNC(eplus) \ - FUNC(epmfs) \ - FUNC(eppfrd) \ - FUNC(eprand) \ - FUNC(erofs) \ - FUNC(ff) \ - FUNC(lfs) \ - FUNC(lus) \ - FUNC(lup) \ - FUNC(mfs) \ - FUNC(msplfs) \ - FUNC(msplus) \ - FUNC(mspmfs) \ - FUNC(msppfrd) \ - FUNC(newest) \ - FUNC(pfrd) \ - FUNC(rand) - -Policy::ActionImpl* -Policies::Action::find(const std::string_view name_) -{ - IFERT(IFERTA); - - return NULL; -} - -Policy::CreateImpl* -Policies::Create::find(const std::string_view name_) -{ - IFERT(IFERTC); - - return NULL; -} - -Policy::SearchImpl* -Policies::Search::find(const std::string_view name_) -{ - IFERT(IFERTS); - - return NULL; -} - -Policy::All::Action Policies::Action::all; -Policy::EPAll::Action Policies::Action::epall; -Policy::EPFF::Action Policies::Action::epff; -Policy::EPLFS::Action Policies::Action::eplfs; -Policy::EPLUS::Action Policies::Action::eplus; -Policy::EPMFS::Action Policies::Action::epmfs; -Policy::EPPFRD::Action Policies::Action::eppfrd; -Policy::EPRand::Action Policies::Action::eprand; -Policy::ERoFS::Action Policies::Action::erofs; -Policy::FF::Action Policies::Action::ff; -Policy::LFS::Action Policies::Action::lfs; -Policy::LUS::Action Policies::Action::lus; -Policy::LUP::Action Policies::Action::lup; -Policy::MFS::Action Policies::Action::mfs; -Policy::MSPLFS::Action Policies::Action::msplfs; -Policy::MSPLUS::Action Policies::Action::msplus; -Policy::MSPMFS::Action Policies::Action::mspmfs; -Policy::MSPPFRD::Action Policies::Action::msppfrd; -Policy::Newest::Action Policies::Action::newest; -Policy::PFRD::Action Policies::Action::pfrd; -Policy::Rand::Action Policies::Action::rand; - -Policy::All::Create Policies::Create::all; -Policy::EPAll::Create Policies::Create::epall; -Policy::EPFF::Create Policies::Create::epff; -Policy::EPLFS::Create Policies::Create::eplfs; -Policy::EPLUS::Create Policies::Create::eplus; -Policy::EPMFS::Create Policies::Create::epmfs; -Policy::EPPFRD::Create Policies::Create::eppfrd; -Policy::EPRand::Create Policies::Create::eprand; -Policy::ERoFS::Create Policies::Create::erofs; -Policy::FF::Create Policies::Create::ff; -Policy::LFS::Create Policies::Create::lfs; -Policy::LUS::Create Policies::Create::lus; -Policy::LUP::Create Policies::Create::lup; -Policy::MFS::Create Policies::Create::mfs; -Policy::MSPLFS::Create Policies::Create::msplfs; -Policy::MSPLUS::Create Policies::Create::msplus; -Policy::MSPMFS::Create Policies::Create::mspmfs; -Policy::MSPPFRD::Create Policies::Create::msppfrd; -Policy::Newest::Create Policies::Create::newest; -Policy::PFRD::Create Policies::Create::pfrd; -Policy::Rand::Create Policies::Create::rand; - -Policy::All::Search Policies::Search::all; -Policy::EPAll::Search Policies::Search::epall; -Policy::EPFF::Search Policies::Search::epff; -Policy::EPLFS::Search Policies::Search::eplfs; -Policy::EPLUS::Search Policies::Search::eplus; -Policy::EPMFS::Search Policies::Search::epmfs; -Policy::EPPFRD::Search Policies::Search::eppfrd; -Policy::EPRand::Search Policies::Search::eprand; -Policy::ERoFS::Search Policies::Search::erofs; -Policy::FF::Search Policies::Search::ff; -Policy::LFS::Search Policies::Search::lfs; -Policy::LUS::Search Policies::Search::lus; -Policy::LUP::Search Policies::Search::lup; -Policy::MFS::Search Policies::Search::mfs; -Policy::MSPLFS::Search Policies::Search::msplfs; -Policy::MSPLUS::Search Policies::Search::msplus; -Policy::MSPMFS::Search Policies::Search::mspmfs; -Policy::MSPPFRD::Search Policies::Search::msppfrd; -Policy::Newest::Search Policies::Search::newest; -Policy::PFRD::Search Policies::Search::pfrd; -Policy::Rand::Search Policies::Search::rand; diff --git a/src/policies.hpp b/src/policies.hpp deleted file mode 100644 index b7077f69..00000000 --- a/src/policies.hpp +++ /dev/null @@ -1,125 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy_all.hpp" -#include "policy_epall.hpp" -#include "policy_epff.hpp" -#include "policy_eplfs.hpp" -#include "policy_eplus.hpp" -#include "policy_epmfs.hpp" -#include "policy_eppfrd.hpp" -#include "policy_eprand.hpp" -#include "policy_erofs.hpp" -#include "policy_ff.hpp" -#include "policy_lfs.hpp" -#include "policy_lus.hpp" -#include "policy_lup.hpp" -#include "policy_mfs.hpp" -#include "policy_msplfs.hpp" -#include "policy_msplus.hpp" -#include "policy_mspmfs.hpp" -#include "policy_msppfrd.hpp" -#include "policy_newest.hpp" -#include "policy_pfrd.hpp" -#include "policy_rand.hpp" - -struct Policies -{ - struct Action - { - static Policy::ActionImpl *find(const std::string_view name); - - static Policy::All::Action all; - static Policy::EPAll::Action epall; - static Policy::EPFF::Action epff; - static Policy::EPLFS::Action eplfs; - static Policy::EPLUS::Action eplus; - static Policy::EPMFS::Action epmfs; - static Policy::EPPFRD::Action eppfrd; - static Policy::EPRand::Action eprand; - static Policy::ERoFS::Action erofs; - static Policy::FF::Action ff; - static Policy::LFS::Action lfs; - static Policy::LUP::Action lup; - static Policy::LUS::Action lus; - static Policy::MFS::Action mfs; - static Policy::MSPLFS::Action msplfs; - static Policy::MSPLUS::Action msplus; - static Policy::MSPMFS::Action mspmfs; - static Policy::MSPPFRD::Action msppfrd; - static Policy::Newest::Action newest; - static Policy::PFRD::Action pfrd; - static Policy::Rand::Action rand; - }; - - struct Create - { - static Policy::CreateImpl *find(const std::string_view name); - - static Policy::All::Create all; - static Policy::EPAll::Create epall; - static Policy::EPFF::Create epff; - static Policy::EPLFS::Create eplfs; - static Policy::EPLUS::Create eplus; - static Policy::EPMFS::Create epmfs; - static Policy::EPPFRD::Create eppfrd; - static Policy::EPRand::Create eprand; - static Policy::ERoFS::Create erofs; - static Policy::FF::Create ff; - static Policy::LFS::Create lfs; - static Policy::LUP::Create lup; - static Policy::LUS::Create lus; - static Policy::MFS::Create mfs; - static Policy::MSPLFS::Create msplfs; - static Policy::MSPLUS::Create msplus; - static Policy::MSPMFS::Create mspmfs; - static Policy::MSPPFRD::Create msppfrd; - static Policy::Newest::Create newest; - static Policy::PFRD::Create pfrd; - static Policy::Rand::Create rand; - }; - - struct Search - { - static Policy::SearchImpl *find(const std::string_view name); - - static Policy::All::Search all; - static Policy::EPAll::Search epall; - static Policy::EPFF::Search epff; - static Policy::EPLFS::Search eplfs; - static Policy::EPLUS::Search eplus; - static Policy::EPMFS::Search epmfs; - static Policy::EPPFRD::Search eppfrd; - static Policy::EPRand::Search eprand; - static Policy::ERoFS::Search erofs; - static Policy::FF::Search ff; - static Policy::LFS::Search lfs; - static Policy::LUP::Search lup; - static Policy::LUS::Search lus; - static Policy::MFS::Search mfs; - static Policy::MSPLFS::Search msplfs; - static Policy::MSPLUS::Search msplus; - static Policy::MSPMFS::Search mspmfs; - static Policy::MSPPFRD::Search msppfrd; - static Policy::Newest::Search newest; - static Policy::PFRD::Search pfrd; - static Policy::Rand::Search rand; - }; -}; diff --git a/src/policy.hpp b/src/policy.hpp deleted file mode 100644 index d0189f46..00000000 --- a/src/policy.hpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "branches.hpp" -#include "strvec.hpp" -#include "fs_path.hpp" - -#include -#include -#include - - -namespace Policy -{ - class ActionImpl - { - public: - ActionImpl(const std::string &name_) - : name(name_) - { - } - - public: - std::string name; - virtual int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const = 0; - }; - - class Action - { - public: - Action(ActionImpl *impl_) - : impl(impl_) - {} - - Action& - operator=(ActionImpl *impl_) - { - impl = impl_; - return *this; - } - - const - std::string& - name(void) const - { - return impl->name; - } - - int - operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &output_) const - { - return (*impl)(branches_,fusepath_,output_); - } - - operator bool() const - { - return (bool)impl; - } - - private: - ActionImpl *impl; - }; - - class CreateImpl - { - public: - CreateImpl(const std::string &name_) - : name(name_) - { - } - - public: - std::string name; - virtual bool path_preserving(void) const = 0; - virtual int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const = 0; - }; - - class Create - { - public: - Create(CreateImpl *impl_) - : impl(impl_) - {} - - Create& - operator=(CreateImpl *impl_) - { - impl = impl_; - return *this; - } - - const - std::string& - name(void) const - { - return impl->name; - } - - bool - path_preserving(void) const - { - return impl->path_preserving(); - } - - int - operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &output_) const - { - return (*impl)(branches_,fusepath_,output_); - } - - operator bool() const - { - return (bool)impl; - } - - private: - CreateImpl *impl; - }; - - class SearchImpl - { - public: - SearchImpl(const std::string &name_) - : name(name_) - { - } - - public: - std::string name; - virtual int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const = 0; - }; - - class Search - { - public: - Search(SearchImpl *impl_) - : impl(impl_) - {} - - Search& - operator=(SearchImpl *impl_) - { - impl = impl_; - return *this; - } - - const - std::string& - name(void) const - { - return impl->name; - } - - int - operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &output_) const - { - return (*impl)(branches_,fusepath_,output_); - } - - operator bool() const - { - return (bool)impl; - } - - private: - SearchImpl *impl; - }; -} diff --git a/src/policy_all.cpp b/src/policy_all.cpp deleted file mode 100644 index da55f4dd..00000000 --- a/src/policy_all.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_all.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policy.hpp" -#include "policies.hpp" -#include "policy_error.hpp" -#include "strvec.hpp" - -#include - -static -int -_create(const Branches::Ptr &ibranches_, - std::vector &obranches_) -{ - int rv; - int error; - fs::info_t info; - - error = ENOENT; - for(auto &branch : *ibranches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - obranches_.emplace_back(&branch); - } - - if(obranches_.empty()) - return -error; - - return 0; -} - -int -Policy::All::Action::operator()(const Branches::Ptr &ibranches_, - const fs::path &fusepath_, - std::vector &obranches_) const -{ - return Policies::Action::epall(ibranches_,fusepath_,obranches_); -} - -int -Policy::All::Create::operator()(const Branches::Ptr &ibranches_, - const fs::path &fusepath_, - std::vector &obranches_) const -{ - return ::_create(ibranches_,obranches_); -} - -int -Policy::All::Search::operator()(const Branches::Ptr &ibranches_, - const fs::path &fusepath_, - std::vector &obranches_) const -{ - return Policies::Search::epall(ibranches_,fusepath_,obranches_); -} diff --git a/src/policy_all.hpp b/src/policy_all.hpp deleted file mode 100644 index c02ac123..00000000 --- a/src/policy_all.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace All - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("all") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("all") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("all") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_epall.cpp b/src/policy_epall.cpp deleted file mode 100644 index c80aac66..00000000 --- a/src/policy_epall.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_epall.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_epall.hpp" -#include "policy_error.hpp" -#include "strvec.hpp" - -#include - - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - fs::info_t info; - - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - paths_.emplace_back(&branch); - } - - if(paths_.empty()) - return -error; - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - bool readonly; - - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::statvfs_cache_readonly(branch.path,&readonly); - if(rv < 0) - error_and_continue(error,ENOENT); - if(readonly) - error_and_continue(error,EROFS); - - paths_.emplace_back(&branch); - } - - if(paths_.empty()) - return -error; - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - - paths_.emplace_back(&branch); - } - - if(paths_.empty()) - return -ENOENT; - - return 0; -} - -int -Policy::EPAll::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPAll::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPAll::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_epall.hpp b/src/policy_epall.hpp deleted file mode 100644 index ffac86b7..00000000 --- a/src/policy_epall.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPAll - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("epall") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("epall") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("epall") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_epff.cpp b/src/policy_epff.cpp deleted file mode 100644 index 71323eeb..00000000 --- a/src/policy_epff.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_epff.hpp" - -#include "branches.hpp" -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include -#include - -using std::vector; - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - fs::info_t info; - - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - paths_.emplace_back(&branch); - - return 0; - } - - return -error; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - bool readonly; - - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::statvfs_cache_readonly(branch.path,&readonly); - if(rv < 0) - error_and_continue(error,ENOENT); - if(readonly) - error_and_continue(error,EROFS); - - paths_.emplace_back(&branch); - - return 0; - } - - return -error; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - - paths_.emplace_back(&branch); - - return 0; - } - - return -ENOENT; -} - -int -Policy::EPFF::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPFF::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPFF::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_epff.hpp b/src/policy_epff.hpp deleted file mode 100644 index 2e4bcbe5..00000000 --- a/src/policy_epff.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPFF - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("epff") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("epff") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("epff") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_eplfs.cpp b/src/policy_eplfs.cpp deleted file mode 100644 index a94844b7..00000000 --- a/src/policy_eplfs.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_eplfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include -#include -#include - -using std::vector; - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 eplfs; - Branch *obranch; - fs::info_t info; - - obranch = nullptr; - error = ENOENT; - eplfs = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceavail > eplfs) - continue; - - eplfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 eplfs; - Branch *obranch; - fs::info_t info; - - obranch = nullptr; - error = ENOENT; - eplfs = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail > eplfs) - continue; - - eplfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - u64 eplfs; - u64 spaceavail; - Branch *obranch; - - obranch = nullptr; - eplfs = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - rv = fs::statvfs_cache_spaceavail(branch.path,&spaceavail); - if(rv < 0) - continue; - if(spaceavail > eplfs) - continue; - - eplfs = spaceavail; - obranch = &branch; - } - - if(!obranch) - return -ENOENT; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::EPLFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPLFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPLFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_eplfs.hpp b/src/policy_eplfs.hpp deleted file mode 100644 index 4a215ea7..00000000 --- a/src/policy_eplfs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPLFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("eplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("eplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("eplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_eplus.cpp b/src/policy_eplus.cpp deleted file mode 100644 index 2884de11..00000000 --- a/src/policy_eplus.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_eplus.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include -#include - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 eplus; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - eplus = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceused >= eplus) - continue; - - eplus = info.spaceused; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 eplus; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - eplus = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceused >= eplus) - continue; - - eplus = info.spaceused; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - u64 eplus; - u64 spaceused; - Branch *obranch; - - obranch = nullptr; - eplus = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - rv = fs::statvfs_cache_spaceused(branch.path,&spaceused); - if(rv < 0) - continue; - if(spaceused >= eplus) - continue; - - eplus = spaceused; - obranch = &branch; - } - - if(!obranch) - return -ENOENT; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::EPLUS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPLUS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPLUS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_eplus.hpp b/src/policy_eplus.hpp deleted file mode 100644 index 92a11c9c..00000000 --- a/src/policy_eplus.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPLUS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("eplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("eplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("eplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_epmfs.cpp b/src/policy_epmfs.cpp deleted file mode 100644 index ee66ddb4..00000000 --- a/src/policy_epmfs.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_epmfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_epmfs.hpp" -#include "policy_error.hpp" - -#include -#include - - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 epmfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - epmfs = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceavail < epmfs) - continue; - - epmfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - u64 epmfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - epmfs = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < epmfs) - continue; - - epmfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - u64 epmfs; - u64 spaceavail; - Branch *obranch; - - obranch = nullptr; - epmfs = 0; - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - rv = fs::statvfs_cache_spaceavail(branch.path,&spaceavail); - if(rv < 0) - continue; - if(spaceavail < epmfs) - continue; - - epmfs = spaceavail; - obranch = &branch; - } - - if(!obranch) - return -ENOENT; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::EPMFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPMFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPMFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_epmfs.hpp b/src/policy_epmfs.hpp deleted file mode 100644 index 48f2ce85..00000000 --- a/src/policy_epmfs.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPMFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("epmfs") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("epmfs") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("epmfs") - { - } - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_eppfrd.cpp b/src/policy_eppfrd.cpp deleted file mode 100644 index 6220d2ea..00000000 --- a/src/policy_eppfrd.cpp +++ /dev/null @@ -1,251 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_eppfrd.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "rnd.hpp" -#include "strvec.hpp" - -#include -#include - -struct BranchInfo -{ - u64 spaceavail; - Branch *branch; -}; - -typedef std::vector BranchInfoVec; - -static -int -_get_branchinfo_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - int error; - fs::info_t info; - - *sum_ = 0; - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - *sum_ += info.spaceavail; - - branchinfo_->push_back({info.spaceavail,&branch}); - } - - return -error; -} - -static -int -_get_branchinfo_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - int error; - fs::info_t info; - - *sum_ = 0; - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - - *sum_ += info.spaceavail; - - branchinfo_->push_back({info.spaceavail,&branch}); - } - - return -error; -} - -static -int -_get_branchinfo_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - u64 spaceavail; - - *sum_ = 0; - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - rv = fs::statvfs_cache_spaceavail(branch.path,&spaceavail); - if(rv < 0) - continue; - - *sum_ += spaceavail; - - branchinfo_->push_back({spaceavail,&branch}); - } - - if(branchinfo_->empty()) - return -ENOENT; - - return 0; -} - -static -Branch* -_get_branch(const BranchInfoVec &branchinfo_, - cu64 sum_) -{ - u64 idx; - u64 threshold; - - if(sum_ == 0) - return NULL; - - idx = 0; - threshold = RND::rand64(sum_); - for(size_t i = 0; i < branchinfo_.size(); i++) - { - idx += branchinfo_[i].spaceavail; - - if(idx < threshold) - continue; - - return branchinfo_[i].branch; - } - - return NULL; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int err; - u64 sum; - Branch *branch; - BranchInfoVec branchinfo; - - err = ::_get_branchinfo_create(branches_,fusepath_,&branchinfo,&sum); - branch = ::_get_branch(branchinfo,sum); - if(!branch) - return err; - - paths_.emplace_back(branch); - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int err; - u64 sum; - Branch *branch; - BranchInfoVec branchinfo; - - err = ::_get_branchinfo_action(branches_,fusepath_,&branchinfo,&sum); - branch = ::_get_branch(branchinfo,sum); - if(!branch) - return err; - - paths_.emplace_back(branch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int err; - u64 sum; - Branch *branch; - BranchInfoVec branchinfo; - - err = ::_get_branchinfo_search(branches_,fusepath_,&branchinfo,&sum); - branch = ::_get_branch(branchinfo,sum); - if(!branch) - return err; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::EPPFRD::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::EPPFRD::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::EPPFRD::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_eppfrd.hpp b/src/policy_eppfrd.hpp deleted file mode 100644 index 89dd58e5..00000000 --- a/src/policy_eppfrd.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPPFRD - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("eppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("eppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("eppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_eprand.cpp b/src/policy_eprand.cpp deleted file mode 100644 index 87552b9c..00000000 --- a/src/policy_eprand.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_eprand.hpp" - -#include "errno.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_eprand.hpp" -#include "rnd.hpp" - - -int -Policy::EPRand::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const - -{ - int rv; - - rv = Policies::Action::epall(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} - -int -Policy::EPRand::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - int rv; - - rv = Policies::Create::epall(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} - -int -Policy::EPRand::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - int rv; - - rv = Policies::Search::epall(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} diff --git a/src/policy_eprand.hpp b/src/policy_eprand.hpp deleted file mode 100644 index c2e94c05..00000000 --- a/src/policy_eprand.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace EPRand - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("eprand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("eprand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("eprand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_erofs.cpp b/src/policy_erofs.cpp deleted file mode 100644 index 0d0e74f8..00000000 --- a/src/policy_erofs.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_erofs.hpp" - -#include "errno.hpp" -#include "policy.hpp" - -#include - - -int -Policy::ERoFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return -EROFS; -} - -int -Policy::ERoFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return -EROFS; -} - -int -Policy::ERoFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return -EROFS; -} diff --git a/src/policy_erofs.hpp b/src/policy_erofs.hpp deleted file mode 100644 index 0d7965bd..00000000 --- a/src/policy_erofs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace ERoFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("erofs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("erofs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving(void) const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("erofs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_error.hpp b/src/policy_error.hpp deleted file mode 100644 index 6e02f042..00000000 --- a/src/policy_error.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#define error_and_continue(CUR,ERR) \ - { \ - policy::calc_error(CUR,ERR); \ - continue; \ - } - -namespace policy -{ - static - inline - void - calc_error(int &cur_, - int err_) - { - switch(cur_) - { - default: - case ENOENT: - cur_ = err_; - break; - case ENOSPC: - if(err_ != ENOENT) - cur_ = err_; - break; - case EROFS: - if((err_ != ENOENT) && (err_ != ENOSPC)) - cur_ = err_; - break; - } - } -} diff --git a/src/policy_ff.cpp b/src/policy_ff.cpp deleted file mode 100644 index 5d632715..00000000 --- a/src/policy_ff.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_ff.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include - - -static -int -_create(const Branches::Ptr &ibranches_, - std::vector &obranches_) -{ - int rv; - int error; - fs::info_t info; - - error = ENOENT; - for(auto &branch : *ibranches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - obranches_.emplace_back(&branch); - - return 0; - } - - return -error; -} - -int -Policy::FF::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::epff(branches_,fusepath_,paths_); -} - -int -Policy::FF::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,paths_); -} - -int -Policy::FF::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &output_) const -{ - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - - output_.emplace_back(&branch); - - return 0; - } - - return -ENOENT; -} diff --git a/src/policy_ff.hpp b/src/policy_ff.hpp deleted file mode 100644 index 387ca23c..00000000 --- a/src/policy_ff.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace FF - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("ff") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("ff") - {} - - public: - bool path_preserving(void) const final { return false; } - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("ff") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_lfs.cpp b/src/policy_lfs.cpp deleted file mode 100644 index 3e811ac3..00000000 --- a/src/policy_lfs.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_lfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "strvec.hpp" - -#include -#include - - -static -int -_create(const Branches::Ptr &branches_, - std::vector &paths_) -{ - int rv; - int error; - u64 lfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - lfs = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceavail > lfs) - continue; - - lfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::LFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eplfs(branches_,fusepath_,paths_); -} - -int -Policy::LFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,paths_); -} - -int -Policy::LFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eplfs(branches_,fusepath_,paths_); -} diff --git a/src/policy_lfs.hpp b/src/policy_lfs.hpp deleted file mode 100644 index 5a85d8a6..00000000 --- a/src/policy_lfs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace LFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("lfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("lfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("lfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_lup.cpp b/src/policy_lup.cpp deleted file mode 100644 index a7f13db4..00000000 --- a/src/policy_lup.cpp +++ /dev/null @@ -1,245 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci and contributors - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_lup.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include "base_types.h" - -#include -#include - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int error; - fs::info_t info; - Branch *obranch; - u64 best_used; - u64 best_total; - - best_used = 0; - best_total = 1; - obranch = nullptr; - error = ENOENT; - - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - - u64 used = info.spaceused; - u64 total = info.spaceused + info.spaceavail; - if(total == 0) - { - used = 0; - total = 1; - } - - if(!obranch) - { - best_used = used; - best_total = total; - obranch = &branch; - continue; - } - - f80 lhs = (f80)used * (f80)best_total; - f80 rhs = (f80)best_used * (f80)total; - if(lhs >= rhs) - continue; - - best_used = used; - best_total = total; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - u64 used; - u64 avail; - u64 best_used; - u64 best_total; - Branch *obranch; - - best_used = 0; - best_total = 1; - obranch = nullptr; - - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_)) - continue; - rv = fs::statvfs_cache_spaceused(branch.path,&used); - if(rv < 0) - continue; - rv = fs::statvfs_cache_spaceavail(branch.path,&avail); - if(rv < 0) - continue; - - u64 total = used + avail; - if(total == 0) - { - used = 0; - total = 1; - } - - if(!obranch) - { - best_used = used; - best_total = total; - obranch = &branch; - continue; - } - - f80 lhs = (f80)used * (f80)best_total; - f80 rhs = (f80)best_used * (f80)total; - if(lhs >= rhs) - continue; - - best_used = used; - best_total = total; - obranch = &branch; - } - - if(!obranch) - return -ENOENT; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_create(const Branches::Ptr &branches_, - std::vector &paths_) -{ - int rv; - int error; - u64 best_used; - u64 best_total; - fs::info_t info; - Branch *obranch; - - best_used = 0; - best_total = 1; - obranch = nullptr; - error = ENOENT; - - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - u64 used = info.spaceused; - u64 total = info.spaceused + info.spaceavail; - if(total == 0) - { - used = 0; - total = 1; - } - - if(!obranch) - { - best_used = used; - best_total = total; - obranch = &branch; - continue; - } - - f80 lhs = (f80)used * (f80)best_total; - f80 rhs = (f80)best_used * (f80)total; - if(lhs >= rhs) - continue; - - best_used = used; - best_total = total; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::LUP::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::LUP::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,paths_); -} - -int -Policy::LUP::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_lup.hpp b/src/policy_lup.hpp deleted file mode 100644 index 3ef19b53..00000000 --- a/src/policy_lup.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci and contributors - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace LUP - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("lup") - {} - - public: - int operator()(const Branches::Ptr &, - const fs::path &, - std::vector &) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("lup") - {} - - public: - int operator()(const Branches::Ptr &, - const fs::path &, - std::vector &) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("lup") - {} - - public: - int operator()(const Branches::Ptr &, - const fs::path &, - std::vector &) const final; - }; - } -} diff --git a/src/policy_lus.cpp b/src/policy_lus.cpp deleted file mode 100644 index ea161737..00000000 --- a/src/policy_lus.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_lus.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "policy_lus.hpp" -#include "strvec.hpp" - -#include -#include -#include - -using std::vector; - -static -int -_create(const Branches::Ptr &branches_, - std::vector &paths_) -{ - int rv; - int error; - u64 lus; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - lus = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceused >= lus) - continue; - - lus = info.spaceused; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::LUS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eplus(branches_,fusepath_,paths_); -} - -int -Policy::LUS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,paths_); -} - -int -Policy::LUS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eplus(branches_,fusepath_,paths_); -} diff --git a/src/policy_lus.hpp b/src/policy_lus.hpp deleted file mode 100644 index 813db455..00000000 --- a/src/policy_lus.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace LUS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("lus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("lus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("lus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_mfs.cpp b/src/policy_mfs.cpp deleted file mode 100644 index d7d9447f..00000000 --- a/src/policy_mfs.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_mfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include - - -static -int -_create(const Branches::Ptr &branches_, - std::vector &paths_) -{ - int rv; - int error; - u64 mfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - error = ENOENT; - mfs = 0; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - if(info.spaceavail < mfs) - continue; - - mfs = info.spaceavail; - obranch = &branch; - } - - if(!obranch) - return -error; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::MFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::epmfs(branches_,fusepath_,paths_); -} - -int -Policy::MFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,paths_); -} - -int -Policy::MFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::epmfs(branches_,fusepath_,paths_); -} diff --git a/src/policy_mfs.hpp b/src/policy_mfs.hpp deleted file mode 100644 index e381d34a..00000000 --- a/src/policy_mfs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace MFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("mfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("mfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("mfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_msplfs.cpp b/src/policy_msplfs.cpp deleted file mode 100644 index d3b88350..00000000 --- a/src/policy_msplfs.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_msplfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "strvec.hpp" - -#include -#include -#include - - -static -Branch* -_create_1(const Branches::Ptr &branches_, - const fs::path &fusepath_, - int *err_) -{ - int rv; - u64 lfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - lfs = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(*err_,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(*err_,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(*err_,ENOENT); - if(info.readonly) - error_and_continue(*err_,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(*err_,ENOSPC); - if(info.spaceavail > lfs) - continue; - - lfs = info.spaceavail; - obranch = &branch; - } - - return obranch; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int error; - Branch *branch; - fs::path fusepath; - - error = ENOENT; - fusepath = fusepath_; - for(;;) - { - branch = ::_create_1(branches_,fusepath,&error); - if(branch) - break; - if(fusepath == "/") - break; - fusepath = fusepath.parent_path(); - } - - if(!branch) - return -error; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::MSPLFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eplfs(branches_,fusepath_,paths_); -} - -int -Policy::MSPLFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::MSPLFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eplfs(branches_,fusepath_,paths_); -} diff --git a/src/policy_msplfs.hpp b/src/policy_msplfs.hpp deleted file mode 100644 index 774d5dd9..00000000 --- a/src/policy_msplfs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace MSPLFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("msplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("msplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("msplfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_msplus.cpp b/src/policy_msplus.cpp deleted file mode 100644 index 6ce2e58a..00000000 --- a/src/policy_msplus.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_msplus.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include - - -static -Branch* -_create_1(const Branches::Ptr &branches_, - const fs::path &fusepath_, - int *err_) -{ - int rv; - u64 lus; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - lus = std::numeric_limits::max(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(*err_,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(*err_,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(*err_,ENOENT); - if(info.readonly) - error_and_continue(*err_,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(*err_,ENOSPC); - if(info.spaceused >= lus) - continue; - - lus = info.spaceused; - obranch = &branch; - } - - return obranch; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int err; - Branch *branch; - fs::path fusepath; - - err = ENOENT; - fusepath = fusepath_; - for(;;) - { - branch = ::_create_1(branches_,fusepath,&err); - if(branch) - break; - if(fusepath == "/") - break; - fusepath = fusepath.parent_path(); - } - - if(!branch) - return -err; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::MSPLUS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eplus(branches_,fusepath_,paths_); -} - -int -Policy::MSPLUS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::MSPLUS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eplus(branches_,fusepath_,paths_); -} diff --git a/src/policy_msplus.hpp b/src/policy_msplus.hpp deleted file mode 100644 index d669c495..00000000 --- a/src/policy_msplus.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace MSPLUS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("msplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("msplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("msplus") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_mspmfs.cpp b/src/policy_mspmfs.cpp deleted file mode 100644 index 0a090213..00000000 --- a/src/policy_mspmfs.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_mspmfs.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "policy_mspmfs.hpp" - -#include -#include -#include - - -static -Branch* -_create_1(const Branches::Ptr &branches_, - const fs::path &fusepath_, - int *err_) -{ - int rv; - u64 mfs; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - mfs = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(*err_,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(*err_,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(*err_,ENOENT); - if(info.readonly) - error_and_continue(*err_,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(*err_,ENOSPC); - if(info.spaceavail < mfs) - continue; - - mfs = info.spaceavail; - obranch = &branch; - } - - return obranch; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int error; - Branch *branch; - fs::path fusepath; - - error = ENOENT; - fusepath = fusepath_; - for(;;) - { - branch = ::_create_1(branches_,fusepath,&error); - if(branch) - break; - if(fusepath == "/") - break; - - fusepath = fusepath.parent_path(); - } - - if(!branch) - return -error; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::MSPMFS::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::epmfs(branches_,fusepath_,paths_); -} - -int -Policy::MSPMFS::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::MSPMFS::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::epmfs(branches_,fusepath_,paths_); -} diff --git a/src/policy_mspmfs.hpp b/src/policy_mspmfs.hpp deleted file mode 100644 index 29e84a9d..00000000 --- a/src/policy_mspmfs.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace MSPMFS - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("mspmfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("mspmfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return true; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("mspmfs") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_msppfrd.cpp b/src/policy_msppfrd.cpp deleted file mode 100644 index a5c349f2..00000000 --- a/src/policy_msppfrd.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_msppfrd.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_msppfrd.hpp" -#include "policies.hpp" -#include "policy_error.hpp" -#include "rnd.hpp" - -#include - - -struct BranchInfo -{ - u64 spaceavail; - Branch *branch; -}; - -typedef std::vector BranchInfoVec; - -static -int -_create_1(const Branches::Ptr &branches_, - const fs::path &fusepath_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - int error; - fs::info_t info; - - *sum_ = 0; - error = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(error,EROFS); - if(!fs::exists(branch.path,fusepath_)) - error_and_continue(error,ENOENT); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(error,ENOSPC); - - *sum_ += info.spaceavail; - - branchinfo_->push_back({info.spaceavail,&branch}); - } - - return -error; -} - -static -int -_get_branchinfo(const Branches::Ptr &branches_, - const fs::path &fusepath_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - fs::path fusepath; - - fusepath = fusepath_; - for(;;) - { - rv = ::_create_1(branches_,fusepath,branchinfo_,sum_); - if(branchinfo_->size()) - break; - if(fusepath == "/") - break; - fusepath = fusepath.parent_path(); - } - - return rv; -} - -static -Branch* -_get_branch(const BranchInfoVec &branchinfo_, - cu64 sum_) -{ - u64 idx; - u64 threshold; - - if(sum_ == 0) - return nullptr; - - idx = 0; - threshold = RND::rand64(sum_); - for(const auto &branchinfo : branchinfo_) - { - idx += branchinfo.spaceavail; - - if(idx < threshold) - continue; - - return branchinfo.branch; - } - - return nullptr; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - u64 sum; - Branch *branch; - BranchInfoVec branchinfo; - - rv = ::_get_branchinfo(branches_,fusepath_,&branchinfo,&sum); - branch = ::_get_branch(branchinfo,sum); - if(!branch) - return rv; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::MSPPFRD::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eppfrd(branches_,fusepath_,paths_); -} - -int -Policy::MSPPFRD::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::MSPPFRD::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eppfrd(branches_,fusepath_,paths_); -} diff --git a/src/policy_msppfrd.hpp b/src/policy_msppfrd.hpp deleted file mode 100644 index 08ebe419..00000000 --- a/src/policy_msppfrd.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace MSPPFRD - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("msppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("msppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return true; }; - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("msppfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_newest.cpp b/src/policy_newest.cpp deleted file mode 100644 index fe3a96a2..00000000 --- a/src/policy_newest.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_newest.hpp" - -#include "errno.hpp" -#include "fs_exists.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "fs_statvfs_cache.hpp" -#include "policy.hpp" -#include "policy_error.hpp" - -#include -#include - -#include - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int err; - time_t newest; - struct stat st; - fs::info_t info; - Branch *obranch; - - obranch = nullptr; - err = ENOENT; - newest = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(err,EROFS); - if(!fs::exists(branch.path,fusepath_,&st)) - error_and_continue(err,ENOENT); - if(st.st_mtime < newest) - continue; - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(err,ENOENT); - if(info.readonly) - error_and_continue(err,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(err,ENOSPC); - - newest = st.st_mtime; - obranch = &branch; - } - - if(!obranch) - return -err; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_action(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int rv; - int err; - bool readonly; - time_t newest; - struct stat st; - Branch *obranch; - - obranch = nullptr; - err = ENOENT; - newest = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(branch.ro()) - error_and_continue(err,EROFS); - if(!fs::exists(branch.path,fusepath_,&st)) - error_and_continue(err,ENOENT); - if(st.st_mtime < newest) - continue; - rv = fs::statvfs_cache_readonly(branch.path,&readonly); - if(rv < 0) - error_and_continue(err,ENOENT); - if(readonly) - error_and_continue(err,EROFS); - - newest = st.st_mtime; - obranch = &branch; - } - - if(!obranch) - return -err; - - paths_.emplace_back(obranch); - - return 0; -} - -static -int -_search(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - time_t newest; - struct stat st; - Branch *obranch; - - obranch = nullptr; - newest = std::numeric_limits::min(); - for(auto &branch : *branches_) - { - if(!fs::exists(branch.path,fusepath_,&st)) - continue; - if(st.st_mtime < newest) - continue; - - newest = st.st_mtime; - obranch = &branch; - } - - if(!obranch) - return -ENOENT; - - paths_.emplace_back(obranch); - - return 0; -} - -int -Policy::Newest::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_action(branches_,fusepath_,paths_); -} - -int -Policy::Newest::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::Newest::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_search(branches_,fusepath_,paths_); -} diff --git a/src/policy_newest.hpp b/src/policy_newest.hpp deleted file mode 100644 index 10a37576..00000000 --- a/src/policy_newest.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace Newest - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("newest") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("newest") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("newest") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_pfrd.cpp b/src/policy_pfrd.cpp deleted file mode 100644 index f3b12960..00000000 --- a/src/policy_pfrd.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_pfrd.hpp" - -#include "errno.hpp" -#include "fs_info.hpp" -#include "fs_path.hpp" -#include "policies.hpp" -#include "policy.hpp" -#include "policy_error.hpp" -#include "policy_pfrd.hpp" -#include "rnd.hpp" -#include "strvec.hpp" - -#include -#include - - -struct BranchInfo -{ - u64 spaceavail; - Branch *branch; -}; - -typedef std::vector BranchInfoVec; - -static -int -_get_branchinfo(const Branches::Ptr &branches_, - BranchInfoVec *branchinfo_, - u64 *sum_) -{ - int rv; - int err; - fs::info_t info; - - *sum_ = 0; - err = ENOENT; - for(auto &branch : *branches_) - { - if(branch.ro_or_nc()) - error_and_continue(err,EROFS); - rv = fs::info(branch.path,&info); - if(rv < 0) - error_and_continue(err,ENOENT); - if(info.readonly) - error_and_continue(err,EROFS); - if(info.spaceavail < branch.minfreespace()) - error_and_continue(err,ENOSPC); - - *sum_ += info.spaceavail; - - branchinfo_->push_back({info.spaceavail,&branch}); - } - - return -err; -} - -static -Branch* -_get_branch(const BranchInfoVec &branchinfo_, - cu64 sum_) -{ - u64 idx; - u64 threshold; - - if(sum_ == 0) - return nullptr; - - idx = 0; - threshold = RND::rand64(sum_); - for(const auto &bi : branchinfo_) - { - idx += bi.spaceavail; - - if(idx < threshold) - continue; - - return bi.branch; - } - - return nullptr; -} - -static -int -_create(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) -{ - int err; - u64 sum; - Branch *branch; - BranchInfoVec branchinfo; - - err = ::_get_branchinfo(branches_,&branchinfo,&sum); - branch = ::_get_branch(branchinfo,sum); - if(!branch) - return err; - - paths_.emplace_back(branch); - - return 0; -} - -int -Policy::PFRD::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Action::eppfrd(branches_,fusepath_,paths_); -} - -int -Policy::PFRD::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return ::_create(branches_,fusepath_,paths_); -} - -int -Policy::PFRD::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - return Policies::Search::eppfrd(branches_,fusepath_,paths_); -} diff --git a/src/policy_pfrd.hpp b/src/policy_pfrd.hpp deleted file mode 100644 index ec3e674d..00000000 --- a/src/policy_pfrd.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace PFRD - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("pfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("pfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("pfrd") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/policy_rand.cpp b/src/policy_rand.cpp deleted file mode 100644 index ced7537f..00000000 --- a/src/policy_rand.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "policy_rand.hpp" - -#include "errno.hpp" -#include "policy.hpp" -#include "policies.hpp" -#include "rnd.hpp" - -int -Policy::Rand::Action::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - int rv; - - rv = Policies::Action::all(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} - -int -Policy::Rand::Create::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - int rv; - - rv = Policies::Create::all(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} - -int -Policy::Rand::Search::operator()(const Branches::Ptr &branches_, - const fs::path &fusepath_, - std::vector &paths_) const -{ - int rv; - - rv = Policies::Search::all(branches_,fusepath_,paths_); - if(rv == 0) - RND::shrink_to_rand_elem(paths_); - - return rv; -} diff --git a/src/policy_rand.hpp b/src/policy_rand.hpp deleted file mode 100644 index ee394ebe..00000000 --- a/src/policy_rand.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - ISC License - - Copyright (c) 2026, Antonio SJ Musumeci - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#pragma once - -#include "policy.hpp" - -namespace Policy -{ - namespace Rand - { - class Action final : public Policy::ActionImpl - { - public: - Action() - : Policy::ActionImpl("rand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - - class Create final : public Policy::CreateImpl - { - public: - Create() - : Policy::CreateImpl("rand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - bool path_preserving() const final { return false; } - }; - - class Search final : public Policy::SearchImpl - { - public: - Search() - : Policy::SearchImpl("rand") - {} - - public: - int operator()(const Branches::Ptr&, - const fs::path&, - std::vector&) const final; - }; - } -} diff --git a/src/symlinkify.hpp b/src/symlinkify.hpp index 6a07716f..426ead62 100644 --- a/src/symlinkify.hpp +++ b/src/symlinkify.hpp @@ -88,8 +88,6 @@ namespace symlinkify struct stat *st_, const s64 timeout_) { - if(timeout_ < 0) - return; if(!symlinkify::can_be_symlink(*st_,timeout_)) return; @@ -103,8 +101,6 @@ namespace symlinkify fuse_statx *st_, const s64 timeout_) { - if(timeout_ < 0) - return; if(!symlinkify::can_be_symlink(*st_,timeout_)) return; diff --git a/src/timespec_utils.hpp b/src/timespec_utils.hpp new file mode 100644 index 00000000..9ccb9117 --- /dev/null +++ b/src/timespec_utils.hpp @@ -0,0 +1,161 @@ +#pragma once + +#include "fuse_kernel.h" + +#include + +static +inline +bool +operator<(const timespec &lhs_, + const timespec &rhs_) +{ + if(lhs_.tv_sec != rhs_.tv_sec) + return lhs_.tv_sec < rhs_.tv_sec; + return lhs_.tv_nsec < rhs_.tv_nsec; +} + +static +inline +bool +operator>(const timespec &lhs_, + const timespec &rhs_) +{ + if(lhs_.tv_sec != rhs_.tv_sec) + return lhs_.tv_sec > rhs_.tv_sec; + return lhs_.tv_nsec > rhs_.tv_nsec; +} + +static +inline +bool +operator==(const timespec &lhs_, + const timespec &rhs_) +{ + return ((lhs_.tv_sec == rhs_.tv_sec) && + (lhs_.tv_nsec == rhs_.tv_nsec)); +} + +static +inline +bool +operator!=(const timespec &lhs_, + const timespec &rhs_) +{ + return !(lhs_ == rhs_); +} + +static +inline +bool +operator<=(const timespec &lhs_, + const timespec &rhs_) +{ + return !(lhs_ > rhs_); +} + +static +inline +bool +operator>=(const timespec &lhs_, + const timespec &rhs_) +{ + return !(lhs_ < rhs_); +} + +static +inline +bool +operator<(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + if(lhs_.tv_sec != rhs_.tv_sec) + return lhs_.tv_sec < rhs_.tv_sec; + return lhs_.tv_nsec < rhs_.tv_nsec; +} + +static +inline +bool +operator>(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + if(lhs_.tv_sec != rhs_.tv_sec) + return lhs_.tv_sec > rhs_.tv_sec; + return lhs_.tv_nsec > rhs_.tv_nsec; +} + +static +inline +bool +operator==(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + return ((lhs_.tv_sec == rhs_.tv_sec) && + (lhs_.tv_nsec == rhs_.tv_nsec)); +} + +static +inline +bool +operator!=(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + return !(lhs_ == rhs_); +} + +static +inline +bool +operator<=(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + return !(lhs_ > rhs_); +} + +static +inline +bool +operator>=(const fuse_sx_time &lhs_, + const fuse_sx_time &rhs_) +{ + return !(lhs_ < rhs_); +} + + +namespace TimeSpec +{ + static + inline + timespec + newest(const timespec &t0_, + const timespec &t1_) + { + if(t0_.tv_sec > t1_.tv_sec) + return t0_; + if(t0_.tv_sec == t1_.tv_sec) + { + if(t0_.tv_nsec > t1_.tv_nsec) + return t0_; + } + + return t1_; + } + + static + inline + fuse_sx_time + newest(const fuse_sx_time &t0_, + const fuse_sx_time &t1_) + { + if(t0_.tv_sec > t1_.tv_sec) + return t0_; + if(t0_.tv_sec == t1_.tv_sec) + { + if(t0_.tv_nsec > t1_.tv_nsec) + return t0_; + } + + return t1_; + } +} diff --git a/src/to_cstr.hpp b/src/to_cstr.hpp new file mode 100644 index 00000000..85da6ff4 --- /dev/null +++ b/src/to_cstr.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + + +template +const char* +to_cstr(const PathType &p_) +{ + if constexpr (std::is_same_v, std::filesystem::path>) + return p_.c_str(); + if constexpr (std::is_same_v, std::string>) + return p_.c_str(); + if constexpr (std::is_same_v, const char*> || + std::is_same_v, char*>) + return p_; + + abort(); + return NULL; +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 89ffee31..09a83daa 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -11,8 +11,18 @@ #include "str.hpp" #include "thread_pool.hpp" +#include "error.hpp" +#include "func_getattr_ff.hpp" +#include "func_link_epall.hpp" +#include "func_mkdir_pfrd.hpp" +#include "func_mknod_ff.hpp" +#include "func_readlink_ff.hpp" +#include "func_rename_epall.hpp" +#include "fs_exists.hpp" + #include #include +#include #include #include #include @@ -28,6 +38,27 @@ #include #include +static +fs::path +_make_test_root(const char *name_) +{ + fs::path root; + + if(const char *test_root = std::getenv("MERGERFS_TEST_ROOT"); + test_root && test_root[0]) + root = fs::path(test_root); + else + root = fs::path("/tmp"); + root /= "mergerfs-tests"; + root /= std::to_string(::getpid()); + root /= name_; + + std::filesystem::remove_all(root); + std::filesystem::create_directories(root); + + return root; +} + template bool wait_until(Predicate pred_, @@ -2972,15 +3003,8 @@ test_fs_copyfile_basic() fs::path src_path; fs::path dst_path; fs::path tmp_dir; - char tmp_template[] = "/tmp/mergerfs-test-copyfile-basic-XXXXXX"; - if(::mkdtemp(tmp_template) == nullptr) - { - TEST_CHECK(false); - return; - } - - tmp_dir = tmp_template; + tmp_dir = _make_test_root("copyfile-basic"); src_path = tmp_dir / "src.bin"; dst_path = tmp_dir / "dst.bin"; @@ -3037,15 +3061,8 @@ test_fs_copyfile_source_changes_cleanup_tmpfiles() fs::path tmp_dir; std::atomic stop_mutator{false}; std::atomic mutator_updates{0}; - char tmp_template[] = "/tmp/mergerfs-test-copyfile-race-XXXXXX"; - if(::mkdtemp(tmp_template) == nullptr) - { - TEST_CHECK(false); - return; - } - - tmp_dir = tmp_template; + tmp_dir = _make_test_root("copyfile-race"); src_path = tmp_dir / "src.bin"; dst_path = tmp_dir / "dst.bin"; @@ -3647,6 +3664,667 @@ test_rapidhash_withSeed_preserves_default_output() } } +void +test_config_bespoke_policy_compat() +{ + Config cfg; + + // Defaults reflect each function's natural class. + TEST_CHECK(cfg.access.to_string() == "ff"); + + // Setting a legacy create-focused policy on a search/action func is + // accepted but maps to the function's default class. + TEST_CHECK(cfg.set("func.open","mfs") == 0); + TEST_CHECK(cfg.open.to_string() == "ff"); + TEST_CHECK(cfg.set("func.ioctl","mfs") == 0); + TEST_CHECK(cfg.ioctl.to_string() == "ff"); + TEST_CHECK(cfg.set("func.getxattr","mfs") == 0); + TEST_CHECK(cfg.getxattr.to_string() == "ff"); + + // Each create func has a real per-policy class; "all" round-trips. + TEST_CHECK(cfg.set("func.create","all") == 0); + TEST_CHECK(cfg.create.to_string() == "all"); + TEST_CHECK(cfg.set("func.mkdir","all") == 0); + TEST_CHECK(cfg.mkdir.to_string() == "all"); + TEST_CHECK(cfg.set("func.mknod","all") == 0); + TEST_CHECK(cfg.mknod.to_string() == "all"); + TEST_CHECK(cfg.set("func.symlink","all") == 0); + TEST_CHECK(cfg.symlink.to_string() == "all"); + + TEST_CHECK(cfg.set("category.create","pfrd") == 0); + TEST_CHECK(cfg.create.to_string() == "pfrd"); + TEST_CHECK(cfg.mkdir.to_string() == "pfrd"); + TEST_CHECK(cfg.mknod.to_string() == "pfrd"); + TEST_CHECK(cfg.symlink.to_string() == "pfrd"); + + // "erofs" is a real per-class behavior on every action and search + // function that has an EROFS variant. + TEST_CHECK(cfg.set("category.action","erofs") == 0); + TEST_CHECK(cfg.chmod.to_string() == "erofs"); + TEST_CHECK(cfg.chown.to_string() == "erofs"); + TEST_CHECK(cfg.link.to_string() == "erofs"); + TEST_CHECK(cfg.removexattr.to_string() == "erofs"); + TEST_CHECK(cfg.rename.to_string() == "erofs"); + TEST_CHECK(cfg.rmdir.to_string() == "erofs"); + TEST_CHECK(cfg.setxattr.to_string() == "erofs"); + TEST_CHECK(cfg.truncate.to_string() == "erofs"); + TEST_CHECK(cfg.unlink.to_string() == "erofs"); + TEST_CHECK(cfg.utimens.to_string() == "erofs"); + + // Category-wide assignment of a create-focused legacy name parses on + // every member, but each func collapses to its default class. + TEST_CHECK(cfg.set("category.action","mfs") == 0); + TEST_CHECK(cfg.chmod.to_string() == "all"); + TEST_CHECK(cfg.chown.to_string() == "all"); + TEST_CHECK(cfg.link.to_string() == "epall"); + TEST_CHECK(cfg.removexattr.to_string() == "all"); + TEST_CHECK(cfg.rename.to_string() == "epall"); + TEST_CHECK(cfg.rmdir.to_string() == "all"); + TEST_CHECK(cfg.setxattr.to_string() == "all"); + TEST_CHECK(cfg.truncate.to_string() == "all"); + TEST_CHECK(cfg.unlink.to_string() == "all"); + TEST_CHECK(cfg.utimens.to_string() == "all"); + + // Search funcs: erofs is a real per-class behavior. + TEST_CHECK(cfg.set("category.search","erofs") == 0); + TEST_CHECK(cfg.access.to_string() == "erofs"); + // getattr/statx don't have an erofs variant, so they keep their + // default class. + TEST_CHECK(cfg.getattr.to_string() == "cdfo"); + TEST_CHECK(cfg.getxattr.to_string() == "erofs"); + TEST_CHECK(cfg.ioctl.to_string() == "erofs"); + TEST_CHECK(cfg.listxattr.to_string() == "erofs"); + TEST_CHECK(cfg.open.to_string() == "erofs"); + TEST_CHECK(cfg.readlink.to_string() == "erofs"); + + TEST_CHECK(cfg.set("category.search","mfs") == 0); + TEST_CHECK(cfg.access.to_string() == "ff"); + TEST_CHECK(cfg.getattr.to_string() == "cdfo"); + TEST_CHECK(cfg.getxattr.to_string() == "ff"); + TEST_CHECK(cfg.ioctl.to_string() == "ff"); + TEST_CHECK(cfg.listxattr.to_string() == "ff"); + TEST_CHECK(cfg.open.to_string() == "ff"); + TEST_CHECK(cfg.readlink.to_string() == "ff"); + + TEST_CHECK(cfg.set("func.link","ff") == 0); + TEST_CHECK(cfg.link.to_string() == "epall"); + TEST_CHECK(cfg.set("func.rename","ff") == 0); + TEST_CHECK(cfg.rename.to_string() == "epall"); + + cfg.finish_initializing(); + + TEST_CHECK(cfg.open.to_string() == "ff"); + cfg.initialize_funcs(); + + TEST_CHECK(cfg.open.to_string() == "ff"); + TEST_CHECK(cfg.set("func.open","ff") == 0); + TEST_CHECK(cfg.open.to_string() == "ff"); + TEST_CHECK(cfg.set("func.open","not-a-policy") == -EINVAL); + TEST_CHECK(cfg.open.to_string() == "ff"); +} + +static +void +test_legacy_policy_erofs_runtime() +{ + int rv; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::Access access("erofs"); + Func2::Mkdir mkdir("erofs"); + Func2::Unlink unlink("erofs"); + fs::path root; + fs::path a; + + root = ::_make_test_root("legacy-policy-erofs-runtime"); + a = root / "a"; + + std::filesystem::create_directories(a); + std::ofstream(a / "file") << "data"; + + branches_str = a.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + access.initialize(); + mkdir.initialize(); + unlink.initialize(); + + rv = access(branches,"file",F_OK); + TEST_CHECK(rv == -EROFS); + + rv = unlink(branches,"file"); + TEST_CHECK(rv == -EROFS); + TEST_CHECK(fs::exists(a / "file") == true); + + rv = mkdir(ugid,branches,"dir",0777,0); + TEST_CHECK(rv == -EROFS); + TEST_CHECK(fs::exists(a / "dir") == false); + + std::filesystem::remove_all(root); +} + +static +void +test_access_policy_all_vs_ff() +{ + int rv; + std::string branches_str; + Branches branches; + Func2::Access all("all"); + Func2::Access epall("epall"); + Func2::Access ff("ff"); + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("access-policy-all-vs-ff"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a); + std::filesystem::create_directories(b); + std::ofstream(a / "file") << "data"; + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + all.initialize(); + epall.initialize(); + ff.initialize(); + + rv = all(branches,"file",F_OK); + TEST_CHECK(rv == -ENOENT); + + rv = ff(branches,"file",F_OK); + TEST_CHECK(rv == 0); + + std::ofstream(b / "file") << "data"; + TEST_CHECK(::chmod((a / "file").c_str(),0755) == 0); + TEST_CHECK(::chmod((b / "file").c_str(),0644) == 0); + + rv = epall(branches,"file",X_OK); + TEST_CHECK(rv == -EACCES); + + std::filesystem::remove_all(root); +} + +static +void +test_mkdir_all_policy() +{ + int rv; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::Mkdir mkdir("all"); + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("mkdir-all-policy"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a / "parent"); + std::filesystem::create_directories(b / "parent"); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + mkdir.initialize(); + + rv = mkdir(ugid,branches,"parent/dir",0777,0); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(a / "parent" / "dir") == true); + TEST_CHECK(fs::exists(b / "parent" / "dir") == true); + + std::filesystem::remove_all(root); +} + +static +void +test_create_parent_uses_search_policy() +{ + int rv; + struct stat st; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::Mkdir mkdir("ff"); + fs::path root; + fs::path a; + fs::path b; + fs::path c; + timespec atime{}; + timespec old_times[2]{}; + timespec new_times[2]{}; + + root = ::_make_test_root("create-parent-uses-search-policy"); + a = root / "a"; + b = root / "b"; + c = root / "c"; + + std::filesystem::create_directories(a / "parent"); + std::filesystem::create_directories(b / "parent"); + std::filesystem::create_directories(c); + TEST_CHECK(::chmod((a / "parent").c_str(),0701) == 0); + TEST_CHECK(::chmod((b / "parent").c_str(),0755) == 0); + + atime.tv_sec = 1700000000; + old_times[0] = atime; + old_times[1] = {1700000000,100000000}; + new_times[0] = atime; + new_times[1] = {1700000000,900000000}; + TEST_CHECK(::utimensat(AT_FDCWD,(a / "parent").c_str(),old_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(b / "parent").c_str(),new_times,0) == 0); + + branches_str = a.string(); + branches_str += "=RO:"; + branches_str += b.string(); + branches_str += "=RO:"; + branches_str += c.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + mkdir.initialize(); + + rv = mkdir(ugid,branches,"parent/dir",0777,0); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(c / "parent" / "dir") == true); + TEST_CHECK(::stat((c / "parent").c_str(),&st) == 0); + // clone source is picked first-found over the branches (a), so the + // cloned parent inherits a's mode rather than b's. + TEST_CHECK((st.st_mode & 0777) == 0701); + + std::filesystem::remove_all(root); +} + +static +void +test_link_rename_parent_uses_search_policy() +{ + int rv; + struct stat st; + std::string branches_str; + Branches branches; + Func2::LinkEPAll link; + Func2::RenameEPAll rename; + fs::path root; + fs::path a; + fs::path b; + fs::path c; + timespec atime{}; + timespec old_times[2]{}; + timespec new_times[2]{}; + + root = ::_make_test_root("link-rename-parent-uses-search-policy"); + a = root / "a"; + b = root / "b"; + c = root / "c"; + + std::filesystem::create_directories(a / "linkparent"); + std::filesystem::create_directories(b / "linkparent"); + std::filesystem::create_directories(a / "renameparent"); + std::filesystem::create_directories(b / "renameparent"); + std::filesystem::create_directories(c / "old"); + std::ofstream(c / "old" / "linkfile") << "link"; + std::ofstream(c / "old" / "renamefile") << "rename"; + + TEST_CHECK(::chmod((a / "linkparent").c_str(),0701) == 0); + TEST_CHECK(::chmod((b / "linkparent").c_str(),0755) == 0); + TEST_CHECK(::chmod((a / "renameparent").c_str(),0701) == 0); + TEST_CHECK(::chmod((b / "renameparent").c_str(),0755) == 0); + + atime.tv_sec = 1700000000; + old_times[0] = atime; + old_times[1] = {1700000000,100000000}; + new_times[0] = atime; + new_times[1] = {1700000000,900000000}; + TEST_CHECK(::utimensat(AT_FDCWD,(a / "linkparent").c_str(),old_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(b / "linkparent").c_str(),new_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(a / "renameparent").c_str(),old_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(b / "renameparent").c_str(),new_times,0) == 0); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + branches_str += ":"; + branches_str += c.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + rv = link(branches,"old/linkfile","linkparent/linkfile"); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(c / "linkparent" / "linkfile") == true); + TEST_CHECK(::stat((c / "linkparent").c_str(),&st) == 0); + // clone source is picked first-found, so c inherits a's mode. + TEST_CHECK((st.st_mode & 0777) == 0701); + + rv = rename(branches,"old/renamefile","renameparent/renamefile"); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(c / "old" / "renamefile") == false); + TEST_CHECK(fs::exists(c / "renameparent" / "renamefile") == true); + TEST_CHECK(::stat((c / "renameparent").c_str(),&st) == 0); + // clone source is picked first-found, so c inherits a's mode. + TEST_CHECK((st.st_mode & 0777) == 0701); + + std::filesystem::remove_all(root); +} + +static +void +test_action_newest_subsecond() +{ + int rv; + std::string branches_str; + Branches branches; + Func2::Unlink unlink("newest"); + fs::path root; + fs::path a; + fs::path b; + timespec atime{}; + timespec old_times[2]{}; + timespec new_times[2]{}; + + root = ::_make_test_root("action-newest-subsecond"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a); + std::filesystem::create_directories(b); + std::ofstream(a / "file") << "old"; + std::ofstream(b / "file") << "new"; + + atime.tv_sec = 1700000000; + old_times[0] = atime; + old_times[1] = {1700000000,100000000}; + new_times[0] = atime; + new_times[1] = {1700000000,900000000}; + TEST_CHECK(::utimensat(AT_FDCWD,(a / "file").c_str(),old_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(b / "file").c_str(),new_times,0) == 0); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + unlink.initialize(); + + rv = unlink(branches,"file"); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(a / "file") == true); + TEST_CHECK(fs::exists(b / "file") == false); + + std::filesystem::remove_all(root); +} + +void +test_err() +{ + Err err; + + TEST_CHECK((err = -ENOENT) == -ENOENT); + TEST_CHECK((err = -EROFS) == -EROFS); + TEST_CHECK((err = -ENOENT) == -EROFS); + TEST_CHECK((err = -ENAMETOOLONG) == -ENAMETOOLONG); + TEST_CHECK((err = -EIO) == -ENAMETOOLONG); + TEST_CHECK((err = 0) == 0); + TEST_CHECK((err = -EIO) == 0); +} + +static +void +test_mkdir_pfrd() +{ + int rv; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::MkdirPFRD mkdir; + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("mkdir-pfrd"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a / "c"); + std::filesystem::create_directories(b); + + branches_str = a.string(); + branches_str += "=RO:"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + rv = mkdir(ugid,branches,"c/d",0777,0); + TEST_CHECK(rv == 0); + TEST_CHECK(std::filesystem::exists(b / "c" / "d")); + + std::filesystem::remove_all(root); +} + +static +void +test_link_epall() +{ + int rv; + std::string branches_str; + Branches branches; + Func2::LinkEPAll link; + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("link-epall"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a / "old"); + std::filesystem::create_directories(a / "new"); + std::filesystem::create_directories(b / "old"); + std::ofstream(a / "old" / "file") << "a"; + std::ofstream(b / "old" / "file") << "b"; + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + rv = link(branches,"old/file","new/file"); + TEST_CHECK(rv == 0); + TEST_CHECK(std::filesystem::exists(a / "new" / "file")); + TEST_CHECK(std::filesystem::exists(b / "new" / "file")); + + std::filesystem::remove_all(root); +} + +static +void +test_getattr_ff_symlinkify() +{ + int rv; + struct stat st; + std::string branches_str; + Branches branches; + Func2::GetAttrFF getattr; + fs::path root; + fs::path a; + + root = ::_make_test_root("getattr-ff-symlinkify"); + a = root / "a"; + + std::filesystem::create_directories(a); + std::ofstream(a / "file") << "x"; + ::chmod((a / "file").c_str(),0444); + + branches_str = a.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + std::memset(&st,0,sizeof(st)); + rv = getattr(branches,"file",&st,FollowSymlinksEnum::NEVER,false,-1); + TEST_CHECK(rv == 0); + TEST_CHECK(S_ISREG(st.st_mode)); + + std::memset(&st,0,sizeof(st)); + rv = getattr(branches,"file",&st,FollowSymlinksEnum::NEVER,true,-1); + TEST_CHECK(rv == 0); + TEST_CHECK(S_ISLNK(st.st_mode)); + TEST_CHECK(st.st_size == (off_t)(a / "file").string().size()); + + std::filesystem::remove_all(root); +} + +static +void +test_readlink_ff() +{ + int rv; + char buf[3] = {}; + std::string branches_str; + Branches branches; + Func2::ReadlinkFF readlink; + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("readlink-ff"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a); + std::filesystem::create_directories(b); + std::filesystem::create_symlink("abcdef",a / "link"); + std::ofstream(a / "shadow") << "x"; + std::filesystem::create_symlink("lower",b / "shadow"); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + rv = readlink(branches,"link",buf,sizeof(buf),false,0); + TEST_CHECK(rv == 3); + TEST_CHECK(std::memcmp(buf,"abc",3) == 0); + + rv = readlink(branches,"shadow",buf,sizeof(buf),false,0); + TEST_CHECK(rv == -EINVAL); + rv = readlink(branches,"shadow",buf,sizeof(buf),true,0); + TEST_CHECK(rv == -EINVAL); + + std::ofstream(a / "file") << "x"; + ::chmod((a / "file").c_str(),0444); + + rv = readlink(branches,"file",buf,sizeof(buf),false,-1); + TEST_CHECK(rv < 0); + + rv = readlink(branches,"file",buf,sizeof(buf),true,-1); + TEST_CHECK(rv == 3); + TEST_CHECK(std::memcmp(buf,(a / "file").c_str(),3) == 0); + + std::filesystem::remove_all(root); +} + +static +void +test_mknod_ff() +{ + int rv; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::Mknod mknod("ff"); + fs::path root; + fs::path a; + fs::path b; + + root = ::_make_test_root("mknod-ff"); + a = root / "a"; + b = root / "b"; + + std::filesystem::create_directories(a); + std::filesystem::create_directories(b / "c" / "d"); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + mknod.initialize(); + + umask(0); + rv = mknod(ugid,branches,"c/d/fifo",S_IFIFO|0777,0,0); + TEST_CHECK(rv == 0); + rv = mknod(ugid,branches,"c/d/fifo",S_IFIFO|0777,0,0); + TEST_CHECK(rv == -EEXIST); + TEST_CHECK(fs::exists(a / "c/d/fifo") == true); + + std::filesystem::remove_all(root); +} + +static +void +test_mknod_create_search_policy() +{ + int rv; + struct stat st; + std::string branches_str; + Branches branches; + ugid_t ugid{::getuid(),::getgid()}; + Func2::MknodFF mknod; + struct timespec newer_times[2] = {{100,200},{100,200}}; + struct timespec older_times[2] = {{100,100},{100,100}}; + fs::path root; + fs::path a; + fs::path b; + fs::path c; + + root = ::_make_test_root("mknod-create-search"); + a = root / "a"; + b = root / "b"; + c = root / "c"; + + std::filesystem::create_directories(a); + std::filesystem::create_directories(b / "d"); + std::filesystem::create_directories(c / "d"); + ::chmod((b / "d").c_str(),0755); + ::chmod((c / "d").c_str(),0700); + TEST_CHECK(::utimensat(AT_FDCWD,(b / "d").c_str(),newer_times,0) == 0); + TEST_CHECK(::utimensat(AT_FDCWD,(c / "d").c_str(),older_times,0) == 0); + + branches_str = a.string(); + branches_str += ":"; + branches_str += b.string(); + branches_str += ":"; + branches_str += c.string(); + + TEST_CHECK(branches.from_string(branches_str) == 0); + + umask(0); + rv = mknod(ugid,branches,"d/fifo",S_IFIFO|0777,0,0); + TEST_CHECK(rv == 0); + TEST_CHECK(fs::exists(a / "d/fifo") == true); + + rv = ::lstat((a / "d").c_str(),&st); + TEST_CHECK(rv == 0); + TEST_CHECK((st.st_mode & 0777) == 0755); + + std::filesystem::remove_all(root); +} + TEST_LIST = { {"nop",test_nop}, @@ -3825,8 +4503,22 @@ TEST_LIST = {"tp_many_producers_many_tasks_stress",test_tp_many_producers_many_tasks_stress}, {"tp_heavy_mixed_resize_and_enqueue_stress",test_tp_heavy_mixed_resize_and_enqueue_stress}, {"tp_heavy_try_enqueue_pressure",test_tp_heavy_try_enqueue_pressure}, - {"tp_heavy_repeated_construct_destroy",test_tp_heavy_repeated_construct_destroy}, - {"tp_heavy_enqueue_task_mixed_outcomes",test_tp_heavy_enqueue_task_mixed_outcomes}, - {"tp_heavy_add_remove_churn_under_enqueue",test_tp_heavy_add_remove_churn_under_enqueue}, + {"tp_heavy_repeated_construct_destroy",test_tp_heavy_repeated_construct_destroy}, + {"tp_heavy_enqueue_task_mixed_outcomes",test_tp_heavy_enqueue_task_mixed_outcomes}, + {"tp_heavy_add_remove_churn_under_enqueue",test_tp_heavy_add_remove_churn_under_enqueue}, + {"config_bespoke_policy_compat",test_config_bespoke_policy_compat}, + {"legacy_policy_erofs_runtime",test_legacy_policy_erofs_runtime}, + {"access_policy_all_vs_ff",test_access_policy_all_vs_ff}, + {"mkdir_all_policy",test_mkdir_all_policy}, + {"create_parent_uses_search_policy",test_create_parent_uses_search_policy}, + {"link_rename_parent_uses_search_policy",test_link_rename_parent_uses_search_policy}, + {"action_newest_subsecond",test_action_newest_subsecond}, + {"err",test_err}, + {"mkdir_pfrd",test_mkdir_pfrd}, + {"link_epall",test_link_epall}, + {"getattr_ff_symlinkify",test_getattr_ff_symlinkify}, + {"readlink_ff",test_readlink_ff}, + {"mknod_ff",test_mknod_ff}, + {"mknod_create_search_policy",test_mknod_create_search_policy}, {NULL,NULL} }; diff --git a/vendored/libfuse/lib/fuse_lowlevel.cpp b/vendored/libfuse/lib/fuse_lowlevel.cpp index d754fe85..1142d351 100644 --- a/vendored/libfuse/lib/fuse_lowlevel.cpp +++ b/vendored/libfuse/lib/fuse_lowlevel.cpp @@ -1644,6 +1644,8 @@ fuse_ll_buf_process_read(struct fuse_session *se_, if(fuse_ll_funcs[in->opcode] == NULL) goto reply_err; + // debug_fuse_in_header(in); + fuse_ll_funcs[in->opcode](req, in); return;