From 2bd44568a199c833a61411ef851e770e251d175d Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Mon, 22 Jun 2015 23:22:51 -0400 Subject: [PATCH 1/3] move Path object to separate file --- src/access.cpp | 2 +- src/chmod.cpp | 4 ++-- src/chown.cpp | 4 ++-- src/create.cpp | 4 ++-- src/fs.hpp | 26 ++------------------- src/getattr.cpp | 2 +- src/getxattr.cpp | 4 ++-- src/ioctl.cpp | 2 +- src/link.cpp | 6 ++--- src/listxattr.cpp | 2 +- src/mkdir.cpp | 6 ++--- src/mknod.cpp | 6 ++--- src/open.cpp | 2 +- src/path.hpp | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/readlink.cpp | 2 +- src/removexattr.cpp | 4 ++-- src/rename.cpp | 8 +++---- src/rmdir.cpp | 4 ++-- src/setxattr.cpp | 4 ++-- src/symlink.cpp | 4 ++-- src/truncate.cpp | 4 ++-- src/unlink.cpp | 4 ++-- src/utimens.cpp | 4 ++-- 23 files changed, 98 insertions(+), 65 deletions(-) create mode 100644 src/path.hpp diff --git a/src/access.cpp b/src/access.cpp index f1611310..824dbfd9 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -52,7 +52,7 @@ _access(const fs::find::Func searchFunc, const int mask) { int rv; - fs::Paths paths; + Paths paths; rv = searchFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) diff --git a/src/chmod.cpp b/src/chmod.cpp index 7109bdbb..8861533b 100644 --- a/src/chmod.cpp +++ b/src/chmod.cpp @@ -47,14 +47,14 @@ _chmod(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::chmod(i->full.c_str(),mode); diff --git a/src/chown.cpp b/src/chown.cpp index 4ff52fcb..67847c38 100644 --- a/src/chown.cpp +++ b/src/chown.cpp @@ -49,14 +49,14 @@ _chown(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::lchown(i->full.c_str(),uid,gid); diff --git a/src/create.cpp b/src/create.cpp index 0eb9c84d..44569dcb 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -56,8 +56,8 @@ _create(const fs::find::Func searchFunc, int rv; string path; string dirname; - fs::Paths createpath; - fs::Paths existingpath; + Paths createpath; + Paths existingpath; dirname = fs::dirname(fusepath); rv = searchFunc(srcmounts,dirname,minfreespace,existingpath); diff --git a/src/fs.hpp b/src/fs.hpp index bc1518f9..92841c1f 100644 --- a/src/fs.hpp +++ b/src/fs.hpp @@ -29,6 +29,8 @@ #include #include +#include "path.hpp" + namespace fs { using std::size_t; @@ -36,30 +38,6 @@ namespace fs using std::vector; using std::map; - struct Path - { - Path() {} - - explicit - Path(const string &b, - const string &f) - : base(b), - full(f) - {} - - explicit - Path(const char *b, - const string &f) - : base(b), - full(f) - {} - - string base; - string full; - }; - - typedef vector Paths; - string dirname(const string &path); string basename(const string &path); diff --git a/src/getattr.cpp b/src/getattr.cpp index 9779894a..3b83cb0e 100644 --- a/src/getattr.cpp +++ b/src/getattr.cpp @@ -72,7 +72,7 @@ _getattr(const fs::find::Func searchFunc, struct stat &buf) { int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/getxattr.cpp b/src/getxattr.cpp index 52b949dd..44f7a1b4 100644 --- a/src/getxattr.cpp +++ b/src/getxattr.cpp @@ -167,7 +167,7 @@ _getxattr_user_mergerfs_allpaths(const vector &srcmounts, static int -_getxattr_user_mergerfs(const fs::Path &path, +_getxattr_user_mergerfs(const Path &path, const vector &srcmounts, const string &fusepath, const char *attrname, @@ -200,7 +200,7 @@ _getxattr(const fs::find::Func searchFunc, { #ifndef WITHOUT_XATTR int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/ioctl.cpp b/src/ioctl.cpp index d3e62fee..dd5223b6 100644 --- a/src/ioctl.cpp +++ b/src/ioctl.cpp @@ -93,7 +93,7 @@ _ioctl_dir_base(const fs::find::Func searchFunc, { int fd; int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/link.cpp b/src/link.cpp index 64c991a4..644f7395 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -56,7 +56,7 @@ _single_link(const fs::find::Func searchFunc, if(rv == -1 && errno == ENOENT) { string newpathdir; - fs::Paths foundpath; + Paths foundpath; newpathdir = fs::dirname(newpath); rv = searchFunc(srcmounts,newpathdir,minfreespace,foundpath); @@ -85,14 +85,14 @@ _link(const fs::find::Func searchFunc, { int rv; int error; - fs::Paths oldpaths; + Paths oldpaths; rv = actionFunc(srcmounts,oldpath,minfreespace,oldpaths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = oldpaths.begin(), ei = oldpaths.end(); i != ei; ++i) { rv = _single_link(searchFunc,srcmounts,minfreespace,i->base,oldpath,newpath); diff --git a/src/listxattr.cpp b/src/listxattr.cpp index dcfdb17d..442624b5 100644 --- a/src/listxattr.cpp +++ b/src/listxattr.cpp @@ -78,7 +78,7 @@ _listxattr(const fs::find::Func searchFunc, { #ifndef WITHOUT_XATTR int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/mkdir.cpp b/src/mkdir.cpp index 6d063a48..971a1bd8 100644 --- a/src/mkdir.cpp +++ b/src/mkdir.cpp @@ -52,8 +52,8 @@ _mkdir(const fs::find::Func searchFunc, int error; string dirname; string fullpath; - fs::Paths createpaths; - fs::Paths existingpath; + Paths createpaths; + Paths existingpath; dirname = fs::dirname(fusepath); rv = searchFunc(srcmounts,dirname,minfreespace,existingpath); @@ -65,7 +65,7 @@ _mkdir(const fs::find::Func searchFunc, return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = createpaths.begin(), ei = createpaths.end(); i != ei; ++i) { if(i->base != existingpath[0].base) diff --git a/src/mknod.cpp b/src/mknod.cpp index 17d4e171..156f75ce 100644 --- a/src/mknod.cpp +++ b/src/mknod.cpp @@ -55,8 +55,8 @@ _mknod(const fs::find::Func searchFunc, int error; string dirname; string fullpath; - fs::Paths createpaths; - fs::Paths existingpath; + Paths createpaths; + Paths existingpath; dirname = fs::dirname(fusepath); rv = searchFunc(srcmounts,dirname,minfreespace,existingpath); @@ -68,7 +68,7 @@ _mknod(const fs::find::Func searchFunc, return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = createpaths.begin(), ei = createpaths.end(); i != ei; ++i) { if(i->base != existingpath[0].base) diff --git a/src/open.cpp b/src/open.cpp index b918908d..8eb60916 100644 --- a/src/open.cpp +++ b/src/open.cpp @@ -50,7 +50,7 @@ _open(const fs::find::Func searchFunc, { int fd; int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/path.hpp b/src/path.hpp new file mode 100644 index 00000000..9344f5d3 --- /dev/null +++ b/src/path.hpp @@ -0,0 +1,55 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __PATH_HPP__ +#define __PATH_HPP__ + +#include +#include + +struct Path +{ + Path() {} + + explicit + Path(const std::string &b, + const std::string &f) + : base(b), + full(f) + {} + + explicit + Path(const char *b, + const std::string &f) + : base(b), + full(f) + {} + + std::string base; + std::string full; +}; + +typedef std::vector Paths; + +#endif /* __PATH_HPP__ */ diff --git a/src/readlink.cpp b/src/readlink.cpp index 223abb9f..9a17bf01 100644 --- a/src/readlink.cpp +++ b/src/readlink.cpp @@ -49,7 +49,7 @@ _readlink(const fs::find::Func searchFunc, const size_t size) { int rv; - fs::Paths path; + Paths path; rv = searchFunc(srcmounts,fusepath,minfreespace,path); if(rv == -1) diff --git a/src/removexattr.cpp b/src/removexattr.cpp index 4c2cc6ef..bbf6576e 100644 --- a/src/removexattr.cpp +++ b/src/removexattr.cpp @@ -50,14 +50,14 @@ _removexattr(const fs::find::Func actionFunc, #ifndef WITHOUT_XATTR int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::lremovexattr(i->full.c_str(),attrname); diff --git a/src/rename.cpp b/src/rename.cpp index f1f6e9f8..822c50dc 100644 --- a/src/rename.cpp +++ b/src/rename.cpp @@ -44,7 +44,7 @@ int _single_rename(const fs::find::Func searchFunc, const vector &srcmounts, const size_t minfreespace, - const fs::Path &oldpath, + const Path &oldpath, const string &newpath) { int rv; @@ -54,7 +54,7 @@ _single_rename(const fs::find::Func searchFunc, if(rv == -1 && errno == ENOENT) { string dirname; - fs::Paths newpathdir; + Paths newpathdir; dirname = fs::dirname(newpath); rv = searchFunc(srcmounts,dirname,minfreespace,newpathdir); @@ -83,14 +83,14 @@ _rename(const fs::find::Func searchFunc, { int rv; int error; - fs::Paths oldpaths; + Paths oldpaths; rv = actionFunc(srcmounts,oldpath,minfreespace,oldpaths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = oldpaths.begin(), ei = oldpaths.end(); i != ei; ++i) { rv = _single_rename(searchFunc,srcmounts,minfreespace,*i,newpath); diff --git a/src/rmdir.cpp b/src/rmdir.cpp index 79558872..25befb49 100644 --- a/src/rmdir.cpp +++ b/src/rmdir.cpp @@ -47,14 +47,14 @@ _rmdir(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::rmdir(i->full.c_str()); diff --git a/src/setxattr.cpp b/src/setxattr.cpp index b144d583..527d385c 100644 --- a/src/setxattr.cpp +++ b/src/setxattr.cpp @@ -252,14 +252,14 @@ _setxattr(const fs::find::Func actionFunc, #ifndef WITHOUT_XATTR int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::lsetxattr(i->full.c_str(),attrname,attrval,attrvalsize,flags); diff --git a/src/symlink.cpp b/src/symlink.cpp index b897ee32..4f8c5453 100644 --- a/src/symlink.cpp +++ b/src/symlink.cpp @@ -48,7 +48,7 @@ _symlink(const fs::find::Func createFunc, int rv; int error; string newpathdir; - fs::Paths newpathdirs; + Paths newpathdirs; newpathdir = fs::dirname(newpath); rv = createFunc(srcmounts,newpathdir,minfreespace,newpathdirs); @@ -56,7 +56,7 @@ _symlink(const fs::find::Func createFunc, return -errno; error = 0; - for(fs::Paths::iterator + for(Paths::iterator i = newpathdirs.begin(), ei = newpathdirs.end(); i != ei; ++i) { i->full = fs::make_path(i->base,newpath); diff --git a/src/truncate.cpp b/src/truncate.cpp index ab1ced6e..dabc53a6 100644 --- a/src/truncate.cpp +++ b/src/truncate.cpp @@ -49,14 +49,14 @@ _truncate(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::truncate(i->full.c_str(),size); diff --git a/src/unlink.cpp b/src/unlink.cpp index 39aec2ce..f54e51c6 100644 --- a/src/unlink.cpp +++ b/src/unlink.cpp @@ -47,14 +47,14 @@ _unlink(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::unlink(i->full.c_str()); diff --git a/src/utimens.cpp b/src/utimens.cpp index 7d9185c6..74220b69 100644 --- a/src/utimens.cpp +++ b/src/utimens.cpp @@ -49,14 +49,14 @@ _utimens(const fs::find::Func actionFunc, { int rv; int error; - fs::Paths paths; + Paths paths; rv = actionFunc(srcmounts,fusepath,minfreespace,paths); if(rv == -1) return -errno; error = 0; - for(fs::Paths::const_iterator + for(Paths::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv = ::utimensat(0,i->full.c_str(),ts,AT_SYMLINK_NOFOLLOW); From 3c8f12281f10e3e3434fe8021ebd4944cf3c5c2f Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Mon, 22 Jun 2015 23:35:46 -0400 Subject: [PATCH 2/3] move policy function type from fs to policy --- src/access.cpp | 2 +- src/chmod.cpp | 2 +- src/chown.cpp | 2 +- src/create.cpp | 4 ++-- src/fs.hpp | 2 -- src/getattr.cpp | 3 ++- src/getxattr.cpp | 2 +- src/ioctl.cpp | 2 +- src/link.cpp | 6 +++--- src/listxattr.cpp | 2 +- src/mkdir.cpp | 5 +++-- src/mknod.cpp | 5 +++-- src/open.cpp | 3 ++- src/policy.hpp | 20 ++++++++++++-------- src/readlink.cpp | 12 ++++++------ src/removexattr.cpp | 3 ++- src/rename.cpp | 7 ++++--- src/rmdir.cpp | 2 +- src/setxattr.cpp | 2 +- src/symlink.cpp | 3 ++- src/truncate.cpp | 3 ++- src/unlink.cpp | 3 ++- src/utimens.cpp | 9 +++++---- 23 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/access.cpp b/src/access.cpp index 824dbfd9..dab24164 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -45,7 +45,7 @@ using mergerfs::Policy; static int -_access(const fs::find::Func searchFunc, +_access(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/chmod.cpp b/src/chmod.cpp index 8861533b..bd54befe 100644 --- a/src/chmod.cpp +++ b/src/chmod.cpp @@ -39,7 +39,7 @@ using mergerfs::Policy; static int -_chmod(const fs::find::Func actionFunc, +_chmod(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/chown.cpp b/src/chown.cpp index 67847c38..f08f29e9 100644 --- a/src/chown.cpp +++ b/src/chown.cpp @@ -40,7 +40,7 @@ using mergerfs::Policy; static int -_chown(const fs::find::Func actionFunc, +_chown(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/create.cpp b/src/create.cpp index 44569dcb..7f018958 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -43,8 +43,8 @@ using mergerfs::Policy; static int -_create(const fs::find::Func searchFunc, - const fs::find::Func createFunc, +_create(const Policy::FuncPtr searchFunc, + const Policy::FuncPtr createFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/fs.hpp b/src/fs.hpp index 92841c1f..bbd0e6fc 100644 --- a/src/fs.hpp +++ b/src/fs.hpp @@ -100,8 +100,6 @@ namespace fs namespace find { - typedef int (*Func)(const vector&,const string&,const size_t,Paths&); - int invalid(const vector &basepaths, const string &fusepath, const size_t minfreespace, diff --git a/src/getattr.cpp b/src/getattr.cpp index 3b83cb0e..25169505 100644 --- a/src/getattr.cpp +++ b/src/getattr.cpp @@ -39,6 +39,7 @@ using std::string; using std::vector; +using mergerfs::Policy; static int @@ -65,7 +66,7 @@ _getattr_controlfile(struct stat &buf) static int -_getattr(const fs::find::Func searchFunc, +_getattr(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/getxattr.cpp b/src/getxattr.cpp index 44f7a1b4..61d2621f 100644 --- a/src/getxattr.cpp +++ b/src/getxattr.cpp @@ -190,7 +190,7 @@ _getxattr_user_mergerfs(const Path &path, static int -_getxattr(const fs::find::Func searchFunc, +_getxattr(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/ioctl.cpp b/src/ioctl.cpp index dd5223b6..fa82494e 100644 --- a/src/ioctl.cpp +++ b/src/ioctl.cpp @@ -82,7 +82,7 @@ _ioctl(const int fd, #ifdef FUSE_IOCTL_DIR static int -_ioctl_dir_base(const fs::find::Func searchFunc, +_ioctl_dir_base(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/link.cpp b/src/link.cpp index 644f7395..15d88f41 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -41,7 +41,7 @@ using mergerfs::Policy; static int -_single_link(const fs::find::Func searchFunc, +_single_link(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &base, @@ -76,8 +76,8 @@ _single_link(const fs::find::Func searchFunc, static int -_link(const fs::find::Func searchFunc, - const fs::find::Func actionFunc, +_link(const Policy::FuncPtr searchFunc, + const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &oldpath, diff --git a/src/listxattr.cpp b/src/listxattr.cpp index 442624b5..ab4e96d2 100644 --- a/src/listxattr.cpp +++ b/src/listxattr.cpp @@ -69,7 +69,7 @@ _listxattr_controlfile(char *list, static int -_listxattr(const fs::find::Func searchFunc, +_listxattr(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/mkdir.cpp b/src/mkdir.cpp index 971a1bd8..cfa3e38d 100644 --- a/src/mkdir.cpp +++ b/src/mkdir.cpp @@ -38,11 +38,12 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_mkdir(const fs::find::Func searchFunc, - const fs::find::Func createFunc, +_mkdir(const Policy::FuncPtr searchFunc, + const Policy::FuncPtr createFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/mknod.cpp b/src/mknod.cpp index 156f75ce..79ff80f1 100644 --- a/src/mknod.cpp +++ b/src/mknod.cpp @@ -40,11 +40,12 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_mknod(const fs::find::Func searchFunc, - const fs::find::Func createFunc, +_mknod(const Policy::FuncPtr searchFunc, + const Policy::FuncPtr createFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/open.cpp b/src/open.cpp index 8eb60916..e81e998a 100644 --- a/src/open.cpp +++ b/src/open.cpp @@ -38,10 +38,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_open(const fs::find::Func searchFunc, +_open(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/policy.hpp b/src/policy.hpp index 752c8983..6807aee8 100644 --- a/src/policy.hpp +++ b/src/policy.hpp @@ -26,8 +26,10 @@ #define __POLICY_HPP__ #include +#include #include +#include "path.hpp" #include "fs.hpp" namespace mergerfs @@ -35,6 +37,8 @@ namespace mergerfs class Policy { public: + typedef int (*FuncPtr)(const std::vector&,const std::string&,const std::size_t,Paths&); + struct Enum { enum Type @@ -55,9 +59,9 @@ namespace mergerfs }; private: - Enum::Type _enum; - std::string _str; - fs::find::Func _func; + Enum::Type _enum; + std::string _str; + FuncPtr _func; public: Policy() @@ -67,9 +71,9 @@ namespace mergerfs { } - Policy(const Enum::Type enum_, - const std::string &str_, - const fs::find::Func func_) + Policy(const Enum::Type enum_, + const std::string &str_, + const FuncPtr func_) : _enum(enum_), _str(str_), _func(func_) @@ -79,14 +83,14 @@ namespace mergerfs public: operator const Enum::Type() const { return _enum; } operator const std::string&() const { return _str; } - operator const fs::find::Func() const { return _func; } + operator const FuncPtr() const { return _func; } operator const Policy*() const { return this; } bool operator==(const Enum::Type enum_) const { return _enum == enum_; } bool operator==(const std::string &str_) const { return _str == str_; } - bool operator==(const fs::find::Func func_) const + bool operator==(const FuncPtr func_) const { return _func == func_; } bool operator!=(const Policy &r) const diff --git a/src/readlink.cpp b/src/readlink.cpp index 9a17bf01..dc7ea697 100644 --- a/src/readlink.cpp +++ b/src/readlink.cpp @@ -41,12 +41,12 @@ using mergerfs::Policy; static int -_readlink(const fs::find::Func searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - char *buf, - const size_t size) +_readlink(const Policy::FuncPtr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + char *buf, + const size_t size) { int rv; Paths path; diff --git a/src/removexattr.cpp b/src/removexattr.cpp index bbf6576e..f1fc26ef 100644 --- a/src/removexattr.cpp +++ b/src/removexattr.cpp @@ -38,10 +38,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_removexattr(const fs::find::Func actionFunc, +_removexattr(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/rename.cpp b/src/rename.cpp index 822c50dc..f97372ac 100644 --- a/src/rename.cpp +++ b/src/rename.cpp @@ -38,10 +38,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_single_rename(const fs::find::Func searchFunc, +_single_rename(const Policy::FuncPtr searchFunc, const vector &srcmounts, const size_t minfreespace, const Path &oldpath, @@ -74,8 +75,8 @@ _single_rename(const fs::find::Func searchFunc, static int -_rename(const fs::find::Func searchFunc, - const fs::find::Func actionFunc, +_rename(const Policy::FuncPtr searchFunc, + const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &oldpath, diff --git a/src/rmdir.cpp b/src/rmdir.cpp index 25befb49..77890801 100644 --- a/src/rmdir.cpp +++ b/src/rmdir.cpp @@ -40,7 +40,7 @@ using mergerfs::Policy; static int -_rmdir(const fs::find::Func actionFunc, +_rmdir(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath) diff --git a/src/setxattr.cpp b/src/setxattr.cpp index 527d385c..ccdfda59 100644 --- a/src/setxattr.cpp +++ b/src/setxattr.cpp @@ -240,7 +240,7 @@ _setxattr_controlfile(config::Config &config, static int -_setxattr(const fs::find::Func actionFunc, +_setxattr(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/symlink.cpp b/src/symlink.cpp index 4f8c5453..18c97fa5 100644 --- a/src/symlink.cpp +++ b/src/symlink.cpp @@ -36,10 +36,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_symlink(const fs::find::Func createFunc, +_symlink(const Policy::FuncPtr createFunc, const vector &srcmounts, const size_t minfreespace, const string &oldpath, diff --git a/src/truncate.cpp b/src/truncate.cpp index dabc53a6..17b0bd0c 100644 --- a/src/truncate.cpp +++ b/src/truncate.cpp @@ -38,10 +38,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_truncate(const fs::find::Func actionFunc, +_truncate(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath, diff --git a/src/unlink.cpp b/src/unlink.cpp index f54e51c6..8c6e5c6b 100644 --- a/src/unlink.cpp +++ b/src/unlink.cpp @@ -37,10 +37,11 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_unlink(const fs::find::Func actionFunc, +_unlink(const Policy::FuncPtr actionFunc, const vector &srcmounts, const size_t minfreespace, const string &fusepath) diff --git a/src/utimens.cpp b/src/utimens.cpp index 74220b69..9eb82ac3 100644 --- a/src/utimens.cpp +++ b/src/utimens.cpp @@ -38,13 +38,14 @@ using std::string; using std::vector; +using mergerfs::Policy; static int -_utimens(const fs::find::Func actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, +_utimens(const Policy::FuncPtr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, const struct timespec ts[2]) { int rv; From 6ca43893ea0971966ced7574aedb31ee70e11656 Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Tue, 23 Jun 2015 22:24:55 -0400 Subject: [PATCH 3/3] separate policies into individual modules --- src/access.cpp | 10 +- src/chmod.cpp | 10 +- src/chown.cpp | 12 +- src/create.cpp | 16 +- src/fs.cpp | 338 ----------------------------------------- src/fs.hpp | 44 ------ src/getattr.cpp | 10 +- src/getxattr.cpp | 14 +- src/ioctl.cpp | 16 +- src/link.cpp | 24 +-- src/listxattr.cpp | 12 +- src/mkdir.cpp | 12 +- src/mknod.cpp | 14 +- src/open.cpp | 12 +- src/policy.cpp | 2 +- src/policy.hpp | 30 +++- src/policy_all.cpp | 65 ++++++++ src/policy_epmfs.cpp | 104 +++++++++++++ src/policy_ff.cpp | 70 +++++++++ src/policy_ffwp.cpp | 79 ++++++++++ src/policy_fwfs.cpp | 69 +++++++++ src/policy_invalid.cpp | 46 ++++++ src/policy_lfs.cpp | 83 ++++++++++ src/policy_mfs.cpp | 78 ++++++++++ src/policy_newest.cpp | 78 ++++++++++ src/policy_rand.cpp | 55 +++++++ src/readlink.cpp | 12 +- src/removexattr.cpp | 10 +- src/rename.cpp | 22 +-- src/rmdir.cpp | 8 +- src/setxattr.cpp | 16 +- src/symlink.cpp | 10 +- src/truncate.cpp | 10 +- src/unlink.cpp | 8 +- src/utimens.cpp | 10 +- 35 files changed, 886 insertions(+), 523 deletions(-) create mode 100644 src/policy_all.cpp create mode 100644 src/policy_epmfs.cpp create mode 100644 src/policy_ff.cpp create mode 100644 src/policy_ffwp.cpp create mode 100644 src/policy_fwfs.cpp create mode 100644 src/policy_invalid.cpp create mode 100644 src/policy_lfs.cpp create mode 100644 src/policy_mfs.cpp create mode 100644 src/policy_newest.cpp create mode 100644 src/policy_rand.cpp diff --git a/src/access.cpp b/src/access.cpp index dab24164..86418d94 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -45,11 +45,11 @@ using mergerfs::Policy; static int -_access(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const int mask) +_access(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const int mask) { int rv; Paths paths; diff --git a/src/chmod.cpp b/src/chmod.cpp index bd54befe..ecd57ae4 100644 --- a/src/chmod.cpp +++ b/src/chmod.cpp @@ -39,11 +39,11 @@ using mergerfs::Policy; static int -_chmod(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const mode_t mode) +_chmod(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const mode_t mode) { int rv; int error; diff --git a/src/chown.cpp b/src/chown.cpp index f08f29e9..19469d6a 100644 --- a/src/chown.cpp +++ b/src/chown.cpp @@ -40,12 +40,12 @@ using mergerfs::Policy; static int -_chown(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const uid_t uid, - const gid_t gid) +_chown(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const uid_t uid, + const gid_t gid) { int rv; int error; diff --git a/src/create.cpp b/src/create.cpp index 7f018958..80a1a836 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -43,14 +43,14 @@ using mergerfs::Policy; static int -_create(const Policy::FuncPtr searchFunc, - const Policy::FuncPtr createFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const mode_t mode, - const int flags, - uint64_t &fh) +_create(const Policy::Func::Ptr searchFunc, + const Policy::Func::Ptr createFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const mode_t mode, + const int flags, + uint64_t &fh) { int fd; int rv; diff --git a/src/fs.cpp b/src/fs.cpp index cb7bc451..3e41b4bb 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -514,341 +513,4 @@ namespace fs globfree(&gbuf); } - - namespace find - { - int - invalid(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &rv) - { - return (errno = EINVAL,-1); - } - - int - ff(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - errno = ENOENT; - for(vector::const_iterator - iter = basepaths.begin(), eiter = basepaths.end(); - iter != eiter; - ++iter) - { - int rv; - struct stat st; - string fullpath; - - fullpath = fs::make_path(*iter,fusepath); - - rv = ::lstat(fullpath.c_str(),&st); - if(rv == 0) - { - paths.push_back(Path(*iter,fullpath)); - return 0; - } - } - - return -1; - } - - int - ffwp(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - Path fallback; - - errno = ENOENT; - for(vector::const_iterator - iter = basepaths.begin(), eiter = basepaths.end(); - iter != eiter; - ++iter) - { - int rv; - struct stat st; - string fullpath; - - fullpath = fs::make_path(*iter,fusepath); - - rv = ::lstat(fullpath.c_str(),&st); - if(rv == 0) - { - paths.push_back(Path(*iter,fullpath)); - return 0; - } - else if(errno == EACCES) - { - fallback.base = *iter; - fallback.full = fullpath; - } - } - - if(!fallback.base.empty()) - return (paths.push_back(fallback),0); - - return -1; - } - - int - fwfs(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - for(size_t i = 0, size = basepaths.size(); i != size; i++) - { - int rv; - const char *basepath; - struct statvfs fsstats; - - basepath = basepaths[i].c_str(); - rv = ::statvfs(basepath,&fsstats); - if(rv == 0) - { - fsblkcnt_t spaceavail; - - spaceavail = (fsstats.f_frsize * fsstats.f_bavail); - if(spaceavail > minfreespace) - { - paths.push_back(Path(basepath, - fs::make_path(basepath,fusepath))); - return 0; - } - } - } - - return mfs(basepaths,fusepath,minfreespace,paths); - } - - int - newest(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - time_t newest; - string npath; - vector::const_iterator niter; - - newest = 0; - errno = ENOENT; - for(vector::const_iterator - iter = basepaths.begin(), eiter = basepaths.end(); - iter != eiter; - ++iter) - { - int rv; - struct stat st; - string fullpath; - - fullpath = fs::make_path(*iter,fusepath); - - rv = ::lstat(fullpath.c_str(),&st); - if(rv == 0 && st.st_mtime > newest) - { - newest = st.st_mtime; - niter = iter; - npath = fullpath; - } - } - - if(newest) - return (paths.push_back(Path(*niter,npath)),0); - - return -1; - } - - int - lfs(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - fsblkcnt_t lfs; - const char *lfsstr; - - lfs = -1; - lfsstr = NULL; - for(size_t i = 0, size = basepaths.size(); i != size; i++) - { - int rv; - const char *basepath; - struct statvfs fsstats; - - basepath = basepaths[i].c_str(); - rv = ::statvfs(basepath,&fsstats); - if(rv == 0) - { - fsblkcnt_t spaceavail; - - spaceavail = (fsstats.f_frsize * fsstats.f_bavail); - if((spaceavail > minfreespace) && - (spaceavail < lfs)) - { - lfs = spaceavail; - lfsstr = basepath; - } - } - } - - if(lfsstr == NULL) - return mfs(basepaths,fusepath,minfreespace,paths); - - paths.push_back(Path(lfsstr, - fs::make_path(lfsstr,fusepath))); - - return 0; - } - - - int - mfs(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - fsblkcnt_t mfs; - size_t mfsidx; - - mfs = 0; - for(size_t i = 0, size = basepaths.size(); - i != size; - i++) - { - int rv; - struct statvfs fsstats; - - rv = ::statvfs(basepaths[i].c_str(),&fsstats); - if(rv == 0) - { - fsblkcnt_t spaceavail; - - spaceavail = (fsstats.f_frsize * fsstats.f_bavail); - if(spaceavail > mfs) - { - mfs = spaceavail; - mfsidx = i; - } - } - } - - if(mfs == 0) - return (errno=ENOENT,-1); - - paths.push_back(Path(basepaths[mfsidx], - fs::make_path(basepaths[mfsidx],fusepath))); - - return 0; - } - - int - epmfs(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - fsblkcnt_t existingmfs; - fsblkcnt_t generalmfs; - string fullpath; - string generalmfspath; - string existingmfspath; - vector::const_iterator iter = basepaths.begin(); - vector::const_iterator eiter = basepaths.end(); - - if(iter == eiter) - return (errno = ENOENT,-1); - - existingmfs = 0; - generalmfs = 0; - do - { - int rv; - struct statvfs fsstats; - const string &mountpoint = *iter; - - rv = ::statvfs(mountpoint.c_str(),&fsstats); - if(rv == 0) - { - fsblkcnt_t spaceavail; - struct stat st; - - spaceavail = (fsstats.f_frsize * fsstats.f_bavail); - if(spaceavail > generalmfs) - { - generalmfs = spaceavail; - generalmfspath = mountpoint; - } - - fullpath = fs::make_path(mountpoint,fusepath); - rv = ::lstat(fullpath.c_str(),&st); - if(rv == 0) - { - if(spaceavail > existingmfs) - { - existingmfs = spaceavail; - existingmfspath = mountpoint; - } - } - } - - ++iter; - } - while(iter != eiter); - - if(existingmfspath.empty()) - existingmfspath = generalmfspath; - - paths.push_back(Path(existingmfspath, - fullpath)); - - return 0; - } - - int - all(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - int rv; - struct stat st; - string fullpath; - - for(vector::const_iterator - iter = basepaths.begin(), eiter = basepaths.end(); - iter != eiter; - ++iter) - { - fullpath = fs::make_path(*iter,fusepath); - - rv = ::lstat(fullpath.c_str(),&st); - if(rv == 0) - paths.push_back(Path(*iter,fullpath)); - } - - return paths.empty() ? (errno=ENOENT,-1) : 0; - } - - int - rand(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &paths) - { - int rv; - - rv = all(basepaths,fusepath,minfreespace,paths); - if(rv == -1) - return -1; - - std::random_shuffle(paths.begin(),paths.end()); - - return 0; - } - } }; diff --git a/src/fs.hpp b/src/fs.hpp index bbd0e6fc..1ef65b31 100644 --- a/src/fs.hpp +++ b/src/fs.hpp @@ -97,50 +97,6 @@ namespace fs void glob(const vector &patterns, vector &strs); - - namespace find - { - int invalid(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int all(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int ff(const vector &basepaths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int ffwp(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int fwfs(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int newest(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int lfs(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int mfs(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int epmfs(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - int rand(const vector &paths, - const string &fusepath, - const size_t minfreespace, - Paths &path); - } }; #endif // __FS_HPP__ diff --git a/src/getattr.cpp b/src/getattr.cpp index 25169505..fd471c5b 100644 --- a/src/getattr.cpp +++ b/src/getattr.cpp @@ -66,11 +66,11 @@ _getattr_controlfile(struct stat &buf) static int -_getattr(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - struct stat &buf) +_getattr(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + struct stat &buf) { int rv; Paths path; diff --git a/src/getxattr.cpp b/src/getxattr.cpp index 61d2621f..9f277e90 100644 --- a/src/getxattr.cpp +++ b/src/getxattr.cpp @@ -190,13 +190,13 @@ _getxattr_user_mergerfs(const Path &path, static int -_getxattr(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const char *attrname, - char *buf, - const size_t count) +_getxattr(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const char *attrname, + char *buf, + const size_t count) { #ifndef WITHOUT_XATTR int rv; diff --git a/src/ioctl.cpp b/src/ioctl.cpp index fa82494e..41947431 100644 --- a/src/ioctl.cpp +++ b/src/ioctl.cpp @@ -82,14 +82,14 @@ _ioctl(const int fd, #ifdef FUSE_IOCTL_DIR static int -_ioctl_dir_base(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const int cmd, - void *arg, - const unsigned int flags, - void *data) +_ioctl_dir_base(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const int cmd, + void *arg, + const unsigned int flags, + void *data) { int fd; int rv; diff --git a/src/link.cpp b/src/link.cpp index 15d88f41..8f53a18c 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -41,12 +41,12 @@ using mergerfs::Policy; static int -_single_link(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &base, - const string &oldpath, - const string &newpath) +_single_link(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &base, + const string &oldpath, + const string &newpath) { int rv; const string fulloldpath = fs::make_path(base,oldpath); @@ -76,12 +76,12 @@ _single_link(const Policy::FuncPtr searchFunc, static int -_link(const Policy::FuncPtr searchFunc, - const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &oldpath, - const string &newpath) +_link(const Policy::Func::Ptr searchFunc, + const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &oldpath, + const string &newpath) { int rv; int error; diff --git a/src/listxattr.cpp b/src/listxattr.cpp index ab4e96d2..59428cc8 100644 --- a/src/listxattr.cpp +++ b/src/listxattr.cpp @@ -69,12 +69,12 @@ _listxattr_controlfile(char *list, static int -_listxattr(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - char *list, - const size_t size) +_listxattr(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + char *list, + const size_t size) { #ifndef WITHOUT_XATTR int rv; diff --git a/src/mkdir.cpp b/src/mkdir.cpp index cfa3e38d..fb8caf23 100644 --- a/src/mkdir.cpp +++ b/src/mkdir.cpp @@ -42,12 +42,12 @@ using mergerfs::Policy; static int -_mkdir(const Policy::FuncPtr searchFunc, - const Policy::FuncPtr createFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const mode_t mode) +_mkdir(const Policy::Func::Ptr searchFunc, + const Policy::Func::Ptr createFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const mode_t mode) { int rv; int error; diff --git a/src/mknod.cpp b/src/mknod.cpp index 79ff80f1..21ab3f7d 100644 --- a/src/mknod.cpp +++ b/src/mknod.cpp @@ -44,13 +44,13 @@ using mergerfs::Policy; static int -_mknod(const Policy::FuncPtr searchFunc, - const Policy::FuncPtr createFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const mode_t mode, - const dev_t dev) +_mknod(const Policy::Func::Ptr searchFunc, + const Policy::Func::Ptr createFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const mode_t mode, + const dev_t dev) { int rv; int error; diff --git a/src/open.cpp b/src/open.cpp index e81e998a..b06cec3d 100644 --- a/src/open.cpp +++ b/src/open.cpp @@ -42,12 +42,12 @@ using mergerfs::Policy; static int -_open(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const int flags, - uint64_t &fh) +_open(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const int flags, + uint64_t &fh) { int fd; int rv; diff --git a/src/policy.cpp b/src/policy.cpp index 4ca300cd..6428e705 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -28,7 +28,7 @@ #include "fs.hpp" #include "buildvector.hpp" -#define POLICY(X) (Policy(Policy::Enum::X,#X,fs::find::X)) +#define POLICY(X) (Policy(Policy::Enum::X,#X,Policy::Func::X)) namespace mergerfs { diff --git a/src/policy.hpp b/src/policy.hpp index 6807aee8..a40b069c 100644 --- a/src/policy.hpp +++ b/src/policy.hpp @@ -37,8 +37,6 @@ namespace mergerfs class Policy { public: - typedef int (*FuncPtr)(const std::vector&,const std::string&,const std::size_t,Paths&); - struct Enum { enum Type @@ -58,10 +56,30 @@ namespace mergerfs }; }; + struct Func + { + typedef std::string string; + typedef std::size_t size_t; + typedef std::vector strvec; + + typedef int (*Ptr)(const strvec&,const string&,const size_t,Paths&); + + static int invalid(const strvec&,const string&,const size_t,Paths&); + static int all(const strvec&,const string&,const size_t,Paths&); + static int epmfs(const strvec&,const string&,const size_t,Paths&); + static int ff(const strvec&,const string&,const size_t,Paths&); + static int ffwp(const strvec&,const string&,const size_t,Paths&); + static int fwfs(const strvec&,const string&,const size_t,Paths&); + static int lfs(const strvec&,const string&,const size_t,Paths&); + static int mfs(const strvec&,const string&,const size_t,Paths&); + static int newest(const strvec&,const string&,const size_t,Paths&); + static int rand(const strvec&,const string&,const size_t,Paths&); + }; + private: Enum::Type _enum; std::string _str; - FuncPtr _func; + Func::Ptr _func; public: Policy() @@ -73,7 +91,7 @@ namespace mergerfs Policy(const Enum::Type enum_, const std::string &str_, - const FuncPtr func_) + const Func::Ptr func_) : _enum(enum_), _str(str_), _func(func_) @@ -83,14 +101,14 @@ namespace mergerfs public: operator const Enum::Type() const { return _enum; } operator const std::string&() const { return _str; } - operator const FuncPtr() const { return _func; } + operator const Func::Ptr() const { return _func; } operator const Policy*() const { return this; } bool operator==(const Enum::Type enum_) const { return _enum == enum_; } bool operator==(const std::string &str_) const { return _str == str_; } - bool operator==(const FuncPtr func_) const + bool operator==(const Func::Ptr func_) const { return _func == func_; } bool operator!=(const Policy &r) const diff --git a/src/policy_all.cpp b/src/policy_all.cpp new file mode 100644 index 00000000..9260651c --- /dev/null +++ b/src/policy_all.cpp @@ -0,0 +1,65 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::all(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + int rv; + struct stat st; + string fullpath; + + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + fullpath = fs::make_path(*iter,fusepath); + + rv = ::lstat(fullpath.c_str(),&st); + if(rv == 0) + paths.push_back(Path(*iter,fullpath)); + } + + return paths.empty() ? (errno=ENOENT,-1) : 0; + } +} diff --git a/src/policy_epmfs.cpp b/src/policy_epmfs.cpp new file mode 100644 index 00000000..b6456f5d --- /dev/null +++ b/src/policy_epmfs.cpp @@ -0,0 +1,104 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::epmfs(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + fsblkcnt_t existingmfs; + fsblkcnt_t generalmfs; + string fullpath; + string generalmfspath; + string existingmfspath; + vector::const_iterator iter = basepaths.begin(); + vector::const_iterator eiter = basepaths.end(); + + if(iter == eiter) + return (errno = ENOENT,-1); + + existingmfs = 0; + generalmfs = 0; + do + { + int rv; + struct statvfs fsstats; + const string &mountpoint = *iter; + + rv = ::statvfs(mountpoint.c_str(),&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + struct stat st; + + spaceavail = (fsstats.f_frsize * fsstats.f_bavail); + if(spaceavail > generalmfs) + { + generalmfs = spaceavail; + generalmfspath = mountpoint; + } + + fullpath = fs::make_path(mountpoint,fusepath); + rv = ::lstat(fullpath.c_str(),&st); + if(rv == 0) + { + if(spaceavail > existingmfs) + { + existingmfs = spaceavail; + existingmfspath = mountpoint; + } + } + } + + ++iter; + } + while(iter != eiter); + + if(existingmfspath.empty()) + existingmfspath = generalmfspath; + + paths.push_back(Path(existingmfspath, + fullpath)); + + return 0; + } +} diff --git a/src/policy_ff.cpp b/src/policy_ff.cpp new file mode 100644 index 00000000..a555ee1c --- /dev/null +++ b/src/policy_ff.cpp @@ -0,0 +1,70 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "path.hpp" +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::ff(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + errno = ENOENT; + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + struct stat st; + string fullpath; + + fullpath = fs::make_path(*iter,fusepath); + + rv = ::lstat(fullpath.c_str(),&st); + if(rv == 0) + { + paths.push_back(Path(*iter,fullpath)); + return 0; + } + } + + return -1; + } +} diff --git a/src/policy_ffwp.cpp b/src/policy_ffwp.cpp new file mode 100644 index 00000000..fc908c45 --- /dev/null +++ b/src/policy_ffwp.cpp @@ -0,0 +1,79 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::ffwp(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + Path fallback; + + errno = ENOENT; + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + struct stat st; + string fullpath; + + fullpath = fs::make_path(*iter,fusepath); + + rv = ::lstat(fullpath.c_str(),&st); + if(rv == 0) + { + paths.push_back(Path(*iter,fullpath)); + return 0; + } + else if(errno == EACCES) + { + fallback.base = *iter; + fallback.full = fullpath; + } + } + + if(!fallback.base.empty()) + return (paths.push_back(fallback),0); + + return -1; + } +} diff --git a/src/policy_fwfs.cpp b/src/policy_fwfs.cpp new file mode 100644 index 00000000..517060d1 --- /dev/null +++ b/src/policy_fwfs.cpp @@ -0,0 +1,69 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::fwfs(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + for(size_t i = 0, size = basepaths.size(); i != size; i++) + { + int rv; + const char *basepath; + struct statvfs fsstats; + + basepath = basepaths[i].c_str(); + rv = ::statvfs(basepath,&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_frsize * fsstats.f_bavail); + if(spaceavail > minfreespace) + { + paths.push_back(Path(basepath, + fs::make_path(basepath,fusepath))); + return 0; + } + } + } + + return mfs(basepaths,fusepath,minfreespace,paths); + } +} diff --git a/src/policy_invalid.cpp b/src/policy_invalid.cpp new file mode 100644 index 00000000..829db3e5 --- /dev/null +++ b/src/policy_invalid.cpp @@ -0,0 +1,46 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::invalid(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &rv) + { + return (errno = EINVAL,-1); + } +} diff --git a/src/policy_lfs.cpp b/src/policy_lfs.cpp new file mode 100644 index 00000000..89cc22be --- /dev/null +++ b/src/policy_lfs.cpp @@ -0,0 +1,83 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::lfs(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + fsblkcnt_t lfs; + const char *lfsstr; + + lfs = -1; + lfsstr = NULL; + for(size_t i = 0, size = basepaths.size(); i != size; i++) + { + int rv; + const char *basepath; + struct statvfs fsstats; + + basepath = basepaths[i].c_str(); + rv = ::statvfs(basepath,&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_frsize * fsstats.f_bavail); + if((spaceavail > minfreespace) && + (spaceavail < lfs)) + { + lfs = spaceavail; + lfsstr = basepath; + } + } + } + + if(lfsstr == NULL) + return Policy::Func::mfs(basepaths,fusepath,minfreespace,paths); + + paths.push_back(Path(lfsstr, + fs::make_path(lfsstr,fusepath))); + + return 0; + } +} diff --git a/src/policy_mfs.cpp b/src/policy_mfs.cpp new file mode 100644 index 00000000..62c0596e --- /dev/null +++ b/src/policy_mfs.cpp @@ -0,0 +1,78 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::mfs(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + fsblkcnt_t mfs; + size_t mfsidx; + + mfs = 0; + for(size_t i = 0, size = basepaths.size(); + i != size; + i++) + { + int rv; + struct statvfs fsstats; + + rv = ::statvfs(basepaths[i].c_str(),&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_frsize * fsstats.f_bavail); + if(spaceavail > mfs) + { + mfs = spaceavail; + mfsidx = i; + } + } + } + + if(mfs == 0) + return (errno=ENOENT,-1); + + paths.push_back(Path(basepaths[mfsidx], + fs::make_path(basepaths[mfsidx],fusepath))); + + return 0; + } +} diff --git a/src/policy_newest.cpp b/src/policy_newest.cpp new file mode 100644 index 00000000..d260aba7 --- /dev/null +++ b/src/policy_newest.cpp @@ -0,0 +1,78 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::newest(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + time_t newest; + string npath; + vector::const_iterator niter; + + newest = 0; + errno = ENOENT; + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + struct stat st; + string fullpath; + + fullpath = fs::make_path(*iter,fusepath); + + rv = ::lstat(fullpath.c_str(),&st); + if(rv == 0 && st.st_mtime > newest) + { + newest = st.st_mtime; + niter = iter; + npath = fullpath; + } + } + + if(newest) + return (paths.push_back(Path(*niter,npath)),0); + + return -1; + } +} diff --git a/src/policy_rand.cpp b/src/policy_rand.cpp new file mode 100644 index 00000000..b4fdc2d7 --- /dev/null +++ b/src/policy_rand.cpp @@ -0,0 +1,55 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include "policy.hpp" + +using std::string; +using std::vector; +using std::size_t; + +namespace mergerfs +{ + int + Policy::Func::rand(const vector &basepaths, + const string &fusepath, + const size_t minfreespace, + Paths &paths) + { + int rv; + + rv = Policy::Func::all(basepaths,fusepath,minfreespace,paths); + if(rv == -1) + return -1; + + std::random_shuffle(paths.begin(),paths.end()); + + return 0; + } +} diff --git a/src/readlink.cpp b/src/readlink.cpp index dc7ea697..8ca42216 100644 --- a/src/readlink.cpp +++ b/src/readlink.cpp @@ -41,12 +41,12 @@ using mergerfs::Policy; static int -_readlink(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - char *buf, - const size_t size) +_readlink(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + char *buf, + const size_t size) { int rv; Paths path; diff --git a/src/removexattr.cpp b/src/removexattr.cpp index f1fc26ef..235bb1bf 100644 --- a/src/removexattr.cpp +++ b/src/removexattr.cpp @@ -42,11 +42,11 @@ using mergerfs::Policy; static int -_removexattr(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const char *attrname) +_removexattr(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const char *attrname) { #ifndef WITHOUT_XATTR int rv; diff --git a/src/rename.cpp b/src/rename.cpp index f97372ac..e0e05299 100644 --- a/src/rename.cpp +++ b/src/rename.cpp @@ -42,11 +42,11 @@ using mergerfs::Policy; static int -_single_rename(const Policy::FuncPtr searchFunc, - const vector &srcmounts, - const size_t minfreespace, - const Path &oldpath, - const string &newpath) +_single_rename(const Policy::Func::Ptr searchFunc, + const vector &srcmounts, + const size_t minfreespace, + const Path &oldpath, + const string &newpath) { int rv; const string fullnewpath = fs::make_path(oldpath.base,newpath); @@ -75,12 +75,12 @@ _single_rename(const Policy::FuncPtr searchFunc, static int -_rename(const Policy::FuncPtr searchFunc, - const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &oldpath, - const string &newpath) +_rename(const Policy::Func::Ptr searchFunc, + const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &oldpath, + const string &newpath) { int rv; int error; diff --git a/src/rmdir.cpp b/src/rmdir.cpp index 77890801..dd9e1abe 100644 --- a/src/rmdir.cpp +++ b/src/rmdir.cpp @@ -40,10 +40,10 @@ using mergerfs::Policy; static int -_rmdir(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath) +_rmdir(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath) { int rv; int error; diff --git a/src/setxattr.cpp b/src/setxattr.cpp index ccdfda59..c2d5c870 100644 --- a/src/setxattr.cpp +++ b/src/setxattr.cpp @@ -240,14 +240,14 @@ _setxattr_controlfile(config::Config &config, static int -_setxattr(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const char *attrname, - const char *attrval, - const size_t attrvalsize, - const int flags) +_setxattr(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const char *attrname, + const char *attrval, + const size_t attrvalsize, + const int flags) { #ifndef WITHOUT_XATTR int rv; diff --git a/src/symlink.cpp b/src/symlink.cpp index 18c97fa5..418310d0 100644 --- a/src/symlink.cpp +++ b/src/symlink.cpp @@ -40,11 +40,11 @@ using mergerfs::Policy; static int -_symlink(const Policy::FuncPtr createFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &oldpath, - const string &newpath) +_symlink(const Policy::Func::Ptr createFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &oldpath, + const string &newpath) { int rv; int error; diff --git a/src/truncate.cpp b/src/truncate.cpp index 17b0bd0c..70f0df6b 100644 --- a/src/truncate.cpp +++ b/src/truncate.cpp @@ -42,11 +42,11 @@ using mergerfs::Policy; static int -_truncate(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const off_t size) +_truncate(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const off_t size) { int rv; int error; diff --git a/src/unlink.cpp b/src/unlink.cpp index 8c6e5c6b..e6965635 100644 --- a/src/unlink.cpp +++ b/src/unlink.cpp @@ -41,10 +41,10 @@ using mergerfs::Policy; static int -_unlink(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath) +_unlink(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath) { int rv; int error; diff --git a/src/utimens.cpp b/src/utimens.cpp index 9eb82ac3..42f4bc99 100644 --- a/src/utimens.cpp +++ b/src/utimens.cpp @@ -42,11 +42,11 @@ using mergerfs::Policy; static int -_utimens(const Policy::FuncPtr actionFunc, - const vector &srcmounts, - const size_t minfreespace, - const string &fusepath, - const struct timespec ts[2]) +_utimens(const Policy::Func::Ptr actionFunc, + const vector &srcmounts, + const size_t minfreespace, + const string &fusepath, + const struct timespec ts[2]) { int rv; int error;