From aab90b05037c5da30b3bac67cad3b4de7ba94c73 Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Tue, 27 May 2014 21:21:45 -0400 Subject: [PATCH] rework policy code --- README.md | 72 +++--- src/access.cpp | 21 +- src/buildmap.hpp | 51 ++++ src/buildvector.hpp | 51 ++++ src/category.cpp | 70 ++++++ src/category.hpp | 98 ++++++++ src/chmod.cpp | 14 +- src/chown.cpp | 16 +- src/config.cpp | 15 +- src/config.hpp | 15 +- src/create.cpp | 37 +-- src/fs.cpp | 535 ++++++++++++++++++++---------------------- src/fs.hpp | 83 +++---- src/getattr.cpp | 18 +- src/getxattr.cpp | 35 +-- src/link.cpp | 14 +- src/listxattr.cpp | 42 ++-- src/mergerfs.cpp | 1 - src/mkdir.cpp | 30 +-- src/mknod.cpp | 32 +-- src/open.cpp | 22 +- src/option_parser.cpp | 7 +- src/policy.cpp | 259 ++++---------------- src/policy.hpp | 180 +++++--------- src/readlink.cpp | 20 +- src/removexattr.cpp | 14 +- src/rename.cpp | 20 +- src/rmdir.cpp | 12 +- src/setxattr.cpp | 95 ++++---- src/symlink.cpp | 10 +- src/test.cpp | 2 + src/truncate.cpp | 14 +- src/unlink.cpp | 12 +- src/utimens.cpp | 14 +- src/write.cpp | 41 ++-- 35 files changed, 982 insertions(+), 990 deletions(-) create mode 100644 src/buildmap.hpp create mode 100644 src/buildvector.hpp create mode 100644 src/category.cpp create mode 100644 src/category.hpp diff --git a/README.md b/README.md index cae9745a..cb5daa56 100644 --- a/README.md +++ b/README.md @@ -10,36 +10,30 @@ Why create mergerfs when those exist? mhddfs isn't really maintained or flexible Policies ======== -Filesystem calls are broken up into 4 categories of policies: search, action, create, and none. - -Below shows each policy class, the FUSE calls they impact, and the policy names. - -#### Policy classifications #### -| Class | FUSE calls | Policies | -|-------|------------|----------| -| search | access, getattr, getxattr, listxattr, open, readlink | First Found (ff), First Found w/ Permission (ffwp), Newest (newest) | -| action | chmod, link, removexattr, rmdir, setxattr, truncate, unlink, utimens | First Found (ff), First Found w/ Permission (ffwp), Newest (newest), All Found (all) | -| create | create, mkdir, mknod | Existing Path (ep), Most Free Space (mfs), Existing Path Most Free Space (epmfs), Random (rand) | -| none | fallocate, fsync, ftruncate, ioctl, read, readdir, rename, statfs, symlink, write, release | | - -#### Descriptions #### -| Class/Policy | Description | +Filesystem calls are broken up into 4 functional categories: search, action, create, and none. These categories can be assigned a policy which dictates how [mergerfs](http://github.com/trapexit/mergerfs) behaves while when action on the filesystem. Any policy can be assigned to a category though some aren't terribly practical. For instance: rand (Random) may be useful for **create** but could lead to very odd behavior if used for **search** or **action**. Since the input for any policy is the source mounts and fusepath and the output a vector of targets the choice was made to simplify the implementation and allow a policies usage in any category. **NOTE:** In any policy which can return more than one location (currently only **all**) the first value will be used in **search** and **create** policies since they can only ever act on 1 filepath. + +#### Functional classifications #### +| Class | FUSE calls | +|-------|------------| +| search | access, getattr, getxattr, listxattr, open, readlink | +| action | chmod, link, removexattr, rmdir, setxattr, truncate, unlink, utimens | +| create | create, mkdir, mknod | +| none | fallocate, fsync, ftruncate, ioctl, read, readdir, rename, statfs, symlink, write, release | + +#### Policy descriptions #### +| Policy | Description | |--------------|-------------| -| search/ff | Given the order the paths were provided at mount time act on the first one found (regardless if stat would return EACCES). | -| search/ffwp | Given the order the paths were provided at mount time act on the first one found which you have access (stat does not error with EACCES). | -| search/newest | If multiple files exist return the one with the most recent mtime. | -| action/ff | (same as search/ff) | -| action/ffwp | (same as search/ffwp) | -| action/newest | (same as search/newest) | -| action/all | Attempt to apply the call to each file found. If any sub call succeeds the entire operation succeeds and other errors ignored. If all fail then the last error is reported. | -| create/ep | Choose the first path which is found. | -| create/mfs | Assuming the path is found to exist (ENOENT would not be returned) use the drive with the most free space available. | -| create/epmfs | If the path exists in multiple locations use the one with the most free space. Otherwise fall back to mfs. | -| create/rand | Pick a destination at random. Again the dirname of the full path must exist somewhere. | +| ff (first found) | Given the order the paths were provided at mount time act on the first one found (regardless if stat would return EACCES). | +| ffwp (first found w/ permissions) | Given the order the paths were provided at mount time act on the first one found which you have access (stat does not error with EACCES). | +| newest (newest file) | If multiple files exist return the one with the most recent mtime. | +| all (all files found) | Attempt to apply the call to each file found. If any sub call succeeds the entire operation succeeds and other errors ignored. If all fail then the last error is reported. | +| mfs (most free space) | Assuming the path is found to exist (ENOENT would not be returned) use the drive with the most free space available. | +| epmfs (existing path, most free space) | If the path exists in multiple locations use the one with the most free space. Otherwise fall back to mfs. | +| rand (random) | Pick a destination at random. Again the dirname of the full path must exist somewhere. | #### statvfs #### -Since we aren't trying to stripe data across drives the free space of the mountpoint is just that of the source mount with the most free space at the moment. +Since we aren't trying to stripe data across drives or move them should ENOSPC be return on a write the free space of the mountpoint is just that of the source mount with the most free space at the moment. **NOTE:** create is really a search for existence and then create. The 'search' policy applies to the first part. If the [dirname](http://linux.die.net/man/3/dirname) of the full path is not found to exist [ENOENT](http://linux.die.net/man/3/errno) is returned. @@ -50,17 +44,17 @@ Usage $ mergerfs -o create=epmfs,search=ff,action=ff :: ``` -| Option | Values | Default | -|--------|--------|---------| -| search | ff, ffwp, newest | ff | -| action | ff, ffwp, newest, all | ff | -| create | ep, mfs, epmfs, rand | epmfs | +| Option | Default | +|--------|--------| +| search | ff | +| action | ff | +| create | epmfs | Building ======== -Need to install FUSE development libraries (libfuse-dev). -Optionally need libattr1 (libattr1-dev). +* Need to install FUSE development libraries (libfuse-dev). +* Optionally need libattr1 (libattr1-dev). ``` @@ -77,7 +71,7 @@ Runtime Settings /.mergerfs ``` -There is a pseudo file available at the mountpoint which currently allows for the runtime modification of policies. The file will not show up in readdirs but can be stat'ed, read, and writen. Most other calls will fail with EPERM, EINVAL, or whatever may be appropriate for that call. Anything not understood while writing will result in EINVAL otherwise the number of bytes written will be returned. +There is a pseudo file available at the mountpoint which allows for the runtime modification of policies. The file will not show up in readdirs but can be stat'ed, read, and writen. Most other calls will fail with EPERM, EINVAL, or whatever may be appropriate for that call. Anything not understood while writing will result in EINVAL otherwise the number of bytes written will be returned. Reading the file will result in a newline delimited list of current settings as followed: @@ -106,15 +100,15 @@ If xattrs has been enabled you can also use [{list,get,set}xattrs](http://linux. ``` [trapexit:/tmp/mount] $ attr -l .mergerfs -Attribute "mergerfs.action" has a 3 byte value for .mergerfs -Attribute "mergerfs.create" has a 6 byte value for .mergerfs -Attribute "mergerfs.search" has a 3 byte value for .mergerfs +Attribute "mergerfs.action" has a 2 byte value for .mergerfs +Attribute "mergerfs.create" has a 5 byte value for .mergerfs +Attribute "mergerfs.search" has a 2 byte value for .mergerfs [trapexit:/tmp/mount] $ attr -g mergerfs.action .mergerfs -Attribute "mergerfs.action" had a 3 byte value for .mergerfs: +Attribute "mergerfs.action" had a 2 byte value for .mergerfs: ff [trapexit:/tmp/mount] 1 $ attr -s mergerfs.action -V ffwp .mergerfs -Attribute "mergerfs.action" set to a 3 byte value for .mergerfs: +Attribute "mergerfs.action" set to a 4 byte value for .mergerfs: ffwp ``` diff --git a/src/access.cpp b/src/access.cpp index b243302b..a51cc2f1 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -45,19 +45,19 @@ using mergerfs::Policy; static int -_access(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const int mask) +_access(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const int mask) { int rv; - string path; + fs::PathVector path; - path = searchFunc(srcmounts,fusepath).full; + searchFunc(srcmounts,fusepath,path); if(path.empty()) return -ENOENT; - rv = ::eaccess(path.c_str(),mask); + rv = ::eaccess(path[0].full.c_str(),mask); return ((rv == -1) ? -errno : 0); } @@ -74,9 +74,12 @@ namespace mergerfs const config::Config &config = config::get(); if(fusepath == config.controlfile) - return 0; + return _access(*config.search, + config.srcmounts, + "/", + mask); - return _access(config.policy.search, + return _access(*config.search, config.srcmounts, fusepath, mask); diff --git a/src/buildmap.hpp b/src/buildmap.hpp new file mode 100644 index 00000000..6ceea1e1 --- /dev/null +++ b/src/buildmap.hpp @@ -0,0 +1,51 @@ +/* + 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 + +template +class buildmap +{ +public: + buildmap(const K &key, + const V &val) + { + _map.insert(std::make_pair(key,val)); + } + + buildmap &operator()(const K &key, + const V &val) + { + _map.insert(std::make_pair(key,val)); + return *this; + } + + operator std::map() + { + return _map; + } + +private: + std::map _map; +}; diff --git a/src/buildvector.hpp b/src/buildvector.hpp new file mode 100644 index 00000000..9982789e --- /dev/null +++ b/src/buildvector.hpp @@ -0,0 +1,51 @@ +/* + 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 + +template +class buildvector +{ +public: + buildvector(const V &val) + { + _vector.push_back(val); + } + + buildvector &operator()(const V &val) + { + _vector.push_back(val); + return *this; + } + + operator std::vector() + { + if(SORT == true) + std::sort(_vector.begin(),_vector.end()); + return _vector; + } + +private: + std::vector _vector; +}; diff --git a/src/category.cpp b/src/category.cpp new file mode 100644 index 00000000..dc8e1413 --- /dev/null +++ b/src/category.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 "category.hpp" +#include "buildvector.hpp" + +#define CATEGORY(X) Category(Category::Enum::X,#X) + +namespace mergerfs +{ + const std::vector Category::_categories_ = + buildvector + (CATEGORY(invalid)) + (CATEGORY(action)) + (CATEGORY(create)) + (CATEGORY(search)); + + const Category * const Category::categories = &_categories_[1]; + + const Category &Category::invalid = Category::categories[Category::Enum::invalid]; + const Category &Category::action = Category::categories[Category::Enum::action]; + const Category &Category::create = Category::categories[Category::Enum::create]; + const Category &Category::search = Category::categories[Category::Enum::search]; + + const Category& + Category::find(const std::string str) + { + for(int i = Enum::BEGIN; i != Enum::END; ++i) + { + if(categories[i] == str) + return categories[i]; + } + + return invalid; + } + + const Category& + Category::find(const Category::Enum::Type i) + { + if(i >= Category::Enum::BEGIN && + i < Category::Enum::END) + return categories[i]; + + return invalid; + } +} diff --git a/src/category.hpp b/src/category.hpp new file mode 100644 index 00000000..a60a84a3 --- /dev/null +++ b/src/category.hpp @@ -0,0 +1,98 @@ +/* + 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 __CATEGORY_HPP__ +#define __CATEGORY_HPP__ + +#include +#include + +namespace mergerfs +{ + class Category + { + public: + struct Enum + { + enum Type + { + invalid = -1, + BEGIN = 0, + action = BEGIN, + create, + search, + END + }; + }; + + private: + Enum::Type _enum; + std::string _str; + + public: + Category() + : _enum(invalid), + _str(invalid) + { + } + + Category(const Enum::Type enum_, + const std::string str_) + : _enum(enum_), + _str(str_) + { + } + + public: + operator const Enum::Type() const { return _enum; } + operator const std::string() const { return _str; } + operator const Category*() const { return this; } + + bool operator==(const std::string str_) const + { return _str == str_; } + + bool operator==(const Enum::Type enum_) const + { return _enum == enum_; } + + bool operator!=(const Category &r) const + { return _enum != r._enum; } + + bool operator<(const Category &r) const + { return _enum < r._enum; } + + public: + static const Category &find(const std::string); + static const Category &find(const Enum::Type); + + public: + static const std::vector _categories_; + static const Category * const categories; + static const Category &invalid; + static const Category &action; + static const Category &create; + static const Category &search; + }; +} + +#endif diff --git a/src/chmod.cpp b/src/chmod.cpp index df9d6f8f..468c2e25 100644 --- a/src/chmod.cpp +++ b/src/chmod.cpp @@ -39,14 +39,14 @@ using mergerfs::Policy; static int -_chmod(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const mode_t mode) +_chmod(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -54,7 +54,7 @@ _chmod(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::chmod(i->full.c_str(),mode); @@ -79,7 +79,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EPERM; - return _chmod(config.policy.action, + return _chmod(*config.action, config.srcmounts, fusepath, mode); diff --git a/src/chown.cpp b/src/chown.cpp index 41c910d8..10639727 100644 --- a/src/chown.cpp +++ b/src/chown.cpp @@ -40,15 +40,15 @@ using mergerfs::Policy; static int -_chown(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const uid_t uid, - const gid_t gid) +_chown(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const uid_t uid, + const gid_t gid) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -56,7 +56,7 @@ _chown(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::lchown(i->full.c_str(),uid,gid); @@ -82,7 +82,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EPERM; - return _chown(config.policy.action, + return _chown(*config.action, config.srcmounts, fusepath, uid, diff --git a/src/config.cpp b/src/config.cpp index e9543092..a443aca2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -36,9 +36,16 @@ namespace mergerfs namespace config { Config::Config() - : controlfile("/.mergerfs"), + : action(policies[0]), + create(policies[1]), + search(policies[2]), + controlfile("/.mergerfs"), testmode(false) { + action = &Policy::ff; + create = &Policy::epmfs; + search = &Policy::ff; + time_t now = time(NULL); controlfilestat.st_dev = 0; @@ -61,9 +68,9 @@ namespace mergerfs { std::stringstream ss; - ss << "action=" << policy.action.str() << std::endl - << "create=" << policy.create.str() << std::endl - << "search=" << policy.search.str() << std::endl; + ss << "action=" << (std::string)*action << std::endl + << "create=" << (std::string)*create << std::endl + << "search=" << (std::string)*search << std::endl; return ss.str(); } diff --git a/src/config.hpp b/src/config.hpp index 29a200da..633575c9 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -31,6 +31,7 @@ #include #include "policy.hpp" +#include "category.hpp" namespace mergerfs { @@ -47,13 +48,17 @@ namespace mergerfs public: std::string destmount; std::vector srcmounts; - Policy policy; - const std::string controlfile; - struct stat controlfilestat; - std::string readstr; + const Policy *policies[Category::Enum::END]; + const Policy *&action; + const Policy *&create; + const Policy *&search; - bool testmode; + const std::string controlfile; + struct stat controlfilestat; + std::string readstr; + + bool testmode; }; const Config &get(void); diff --git a/src/create.cpp b/src/create.cpp index 65720162..99bda9e4 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -54,30 +54,33 @@ _create_controlfile(uint64_t &fh) static int -_create(const Policy::Search::Func searchFunc, - const Policy::Create::Func createPathFunc, - const vector &srcmounts, - const string fusepath, - const mode_t mode, - const int flags, - uint64_t &fh) +_create(const fs::SearchFunc searchFunc, + const fs::SearchFunc createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode, + const int flags, + uint64_t &fh) { int fd; string path; string dirname; - fs::Path createpath; - fs::Path existingpath; + fs::PathVector createpath; + fs::PathVector existingpath; dirname = fs::dirname(fusepath); - existingpath = searchFunc(srcmounts,dirname); - if(existingpath.base.empty()) + searchFunc(srcmounts,dirname,existingpath); + if(existingpath.empty()) return -ENOENT; - createpath = createPathFunc(srcmounts,dirname); - if(createpath.base != existingpath.base) - fs::clonepath(existingpath.base,createpath.base,dirname); + createPathFunc(srcmounts,dirname,createpath); + if(createpath.empty()) + return -ENOSPC; + + if(createpath[0].base != existingpath[0].base) + fs::clonepath(existingpath[0].base,createpath[0].base,dirname); - path = fs::make_path(createpath.base,fusepath); + path = fs::make_path(createpath[0].base,fusepath); fd = ::open(path.c_str(),flags,mode); if(fd == -1) @@ -103,8 +106,8 @@ namespace mergerfs if(fusepath == config.controlfile) return _create_controlfile(fileinfo->fh); - return _create(config.policy.search, - config.policy.create, + return _create(*config.search, + *config.create, config.srcmounts, fusepath, mode, diff --git a/src/fs.cpp b/src/fs.cpp index c875e757..86f3564c 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -136,12 +136,12 @@ namespace fs string make_path(const string base, - const string suffix) + const string fusepath) { if(*base.rbegin() == '/' || - *suffix.rbegin() == '/') - return base + suffix; - return base + '/' + suffix; + *fusepath.rbegin() == '/') + return base + fusepath; + return base + '/' + fusepath; } bool @@ -153,11 +153,10 @@ namespace fs iter = begin; iter != end; ++iter) { int rv; - struct stat st; string path; - path = make_path(*iter,fusepath); - rv = ::lstat(path.c_str(),&st); + path = fs::make_path(*iter,fusepath); + rv = ::eaccess(path.c_str(),F_OK); if(rv == 0) return true; } @@ -174,282 +173,6 @@ namespace fs fusepath); } - Path - find_first(vector::const_iterator beginiter, - vector::const_iterator enditer, - const string suffix) - { - for(vector::const_iterator - iter = beginiter; iter != enditer; ++iter) - { - int rv; - struct stat st; - string path; - - path = fs::make_path(*iter,suffix); - rv = ::lstat(path.c_str(),&st); - if(rv == 0 || (rv == -1 && errno == EACCES)) - return Path(*iter,path); - } - - return Path(); - } - - Path - find_first(const vector &basepaths, - const string suffix) - { - return find_first(basepaths.begin(), - basepaths.end(), - suffix); - } - - void - find_first(const vector &basepaths, - const string suffix, - vector &paths) - { - paths.push_back(find_first(basepaths,suffix)); - } - - Path - find_first_with_permission(vector::const_iterator beginiter, - vector::const_iterator enditer, - const string suffix) - { - for(vector::const_iterator - iter = beginiter; iter != enditer; ++iter) - { - int rv; - string path; - - path = make_path(*iter,suffix); - rv = ::eaccess(path.c_str(),R_OK); - if(rv == 0) - return Path(*iter,path); - rv = ::eaccess(path.c_str(),W_OK); - if(rv == 0) - return Path(*iter,path); - rv = ::eaccess(path.c_str(),X_OK); - if(rv == 0) - return Path(*iter,path); - } - - return Path(); - } - - Path - find_first_with_permission(const vector &basepaths, - const string suffix) - { - return find_first_with_permission(basepaths.begin(), - basepaths.end(), - suffix); - } - - void - find_first_with_permission(const vector &basepaths, - const string suffix, - vector &paths) - { - paths.push_back(find_first_with_permission(basepaths,suffix)); - } - - Path - find_newest(vector::const_iterator beginiter, - vector::const_iterator enditer, - const string suffix) - { - time_t newest; - string npath; - vector::const_iterator niter; - - newest = 0; - for(vector::const_iterator - iter = beginiter; iter != enditer; ++iter) - { - int rv; - struct stat st; - const string path = make_path(*iter,suffix); - - rv = ::lstat(path.c_str(),&st); - if(rv == 0 && st.st_mtime > newest) - { - newest = st.st_mtime; - niter = iter; - npath = path; - } - } - - if(newest) - return Path(*niter,npath); - - return Path(); - } - - Path - find_newest(const vector &basepaths, - const string suffix) - { - return find_newest(basepaths.begin(), - basepaths.end(), - suffix); - } - - void - find_newest(const vector &basepaths, - const string suffix, - vector &paths) - { - paths.push_back(find_newest(basepaths,suffix)); - } - - void - find_all(const vector &basepaths, - const string suffix, - vector &paths) - { - for(vector::const_iterator - i = basepaths.begin(), ei = basepaths.end(); i != ei; ++i) - { - int rv; - struct stat st; - string path; - - path = fs::make_path(*i,suffix); - rv = ::lstat(path.c_str(),&st); - if(rv == 0 || (rv == -1 && errno == EACCES)) - paths.push_back(Path(*i,path)); - } - } - - Path - find_mfs(vector::const_iterator iter, - vector::const_iterator enditer, - const string fusepath) - { - fsblkcnt_t mfs = 0; - string mfspath; - string fullmfspath; - - for(;iter != enditer; ++iter) - { - int rv; - struct statvfs fsstats; - const string mountpoint = *iter; - - rv = ::statvfs(mountpoint.c_str(),&fsstats); - if(rv == 0) - { - fsblkcnt_t spaceavail; - - spaceavail = (fsstats.f_bsize * fsstats.f_bavail); - if(spaceavail > mfs) - { - mfs = spaceavail; - mfspath = mountpoint; - } - } - } - - fullmfspath = make_path(mfspath,fusepath); - - return Path(mfspath,fullmfspath); - } - - Path - find_mfs(const vector &paths, - const string fusepath) - { - return find_mfs(paths.begin(), - paths.end(), - fusepath); - } - - Path - find_mfs_existing(vector::const_iterator iter, - vector::const_iterator enditer, - const string fusepath) - { - fsblkcnt_t existingmfs = 0; - fsblkcnt_t generalmfs = 0; - string path; - string generalmfspath; - string existingmfspath; - - do - { - int rv; - struct statvfs fsstats; - const string mountpoint = *iter; - - rv = ::statvfs(mountpoint.c_str(),&fsstats); - if(rv == 0) - { - struct stat filestats; - fsblkcnt_t spaceavail; - - spaceavail = (fsstats.f_bsize * fsstats.f_bavail); - if(spaceavail > generalmfs) - { - generalmfs = spaceavail; - generalmfspath = mountpoint; - } - - path = make_path(mountpoint,fusepath); - rv = ::lstat(path.c_str(),&filestats); - if(rv == 0) - { - if(spaceavail > existingmfs) - { - existingmfs = spaceavail; - existingmfspath = mountpoint; - } - } - } - - ++iter; - } - while(iter != enditer); - - if(existingmfspath.empty()) - existingmfspath = generalmfspath; - - return Path(existingmfspath,path); - } - - Path - find_mfs_existing(const vector &paths, - const string fusepath) - { - return find_mfs_existing(paths.begin(), - paths.end(), - fusepath); - } - - Path - find_random(vector::const_iterator iter, - vector::const_iterator enditer, - string fusepath) - { - string randombasepath; - string randomfullpath; - - randombasepath = *random_element(iter,enditer); - randomfullpath = make_path(randombasepath,fusepath); - - return Path(randombasepath,randomfullpath); - } - - Path - find_random(const vector &paths, - const string fusepath) - { - return find_random(paths.begin(), - paths.end(), - fusepath); - } - int listxattr(const string path, vector &attrs) @@ -701,7 +424,7 @@ namespace fs return -1; } - frompath = make_path(fromsrc,relative); + frompath = fs::make_path(fromsrc,relative); rv = ::stat(frompath.c_str(),&st); if(rv == -1) @@ -709,7 +432,7 @@ namespace fs else if(!S_ISDIR(st.st_mode)) return (errno = ENOTDIR,-1); - topath = make_path(tosrc,relative); + topath = fs::make_path(tosrc,relative); rv = ::mkdir(topath.c_str(),st.st_mode); if(rv == -1) return -1; @@ -729,4 +452,246 @@ namespace fs return (errno = 0); } + + namespace find + { + void + invalid(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + return; + } + + void + ff(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + string path; + + path = fs::make_path(*iter,fusepath); + rv = ::eaccess(path.c_str(),F_OK); + if(rv == 0) + { + paths.push_back(Path(*iter,path)); + return; + } + } + + return; + } + + void + ffwp(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + Path fallback; + + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + string path; + + path = fs::make_path(*iter,fusepath); + + rv = ((::eaccess(path.c_str(),R_OK) == 0) || + (::eaccess(path.c_str(),X_OK) == 0) || + (::eaccess(path.c_str(),W_OK) == 0)); + + if(rv) + { + paths.push_back(Path(*iter,path)); + return; + } + else if(errno == EACCES) + { + fallback.base = *iter; + fallback.full = path; + } + } + + if(!fallback.base.empty()) + paths.push_back(fallback); + + return; + } + + void + newest(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + time_t newest; + string npath; + vector::const_iterator niter; + + newest = 0; + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + struct stat st; + const string path = fs::make_path(*iter,fusepath); + + rv = ::lstat(path.c_str(),&st); + if(rv == 0 && st.st_mtime > newest) + { + newest = st.st_mtime; + niter = iter; + npath = path; + } + } + + if(newest) + paths.push_back(Path(*niter,npath)); + + return; + } + + void + all(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + string path; + + path = fs::make_path(*iter,fusepath); + rv = ::eaccess(path.c_str(),F_OK); + if(rv == 0) + paths.push_back(Path(*iter,path)); + } + + return; + } + + void + mfs(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + fsblkcnt_t mfs = 0; + string mfspath; + string fullmfspath; + + for(vector::const_iterator + iter = basepaths.begin(), eiter = basepaths.end(); + iter != eiter; + ++iter) + { + int rv; + struct statvfs fsstats; + const string mountpoint = *iter; + + rv = ::statvfs(mountpoint.c_str(),&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_bsize * fsstats.f_bavail); + if(spaceavail > mfs) + { + mfs = spaceavail; + mfspath = mountpoint; + } + } + } + + fullmfspath = fs::make_path(mfspath,fusepath); + + paths.push_back(Path(mfspath,fullmfspath)); + + return; + } + + void + epmfs(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + fsblkcnt_t existingmfs = 0; + fsblkcnt_t generalmfs = 0; + string path; + string generalmfspath; + string existingmfspath; + vector::const_iterator iter = basepaths.begin(); + vector::const_iterator eiter = basepaths.end(); + + do + { + int rv; + struct statvfs fsstats; + const string mountpoint = *iter; + + rv = ::statvfs(mountpoint.c_str(),&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_bsize * fsstats.f_bavail); + if(spaceavail > generalmfs) + { + generalmfs = spaceavail; + generalmfspath = mountpoint; + } + + path = fs::make_path(mountpoint,fusepath); + rv = ::eaccess(path.c_str(),F_OK); + if(rv == 0) + { + if(spaceavail > existingmfs) + { + existingmfs = spaceavail; + existingmfspath = mountpoint; + } + } + } + + ++iter; + } + while(iter != eiter); + + if(existingmfspath.empty()) + existingmfspath = generalmfspath; + + paths.push_back(Path(existingmfspath,path)); + + return; + } + + void + rand(const vector &basepaths, + const string fusepath, + PathVector &paths) + { + string randombasepath; + string randomfullpath; + + randombasepath = *random_element(basepaths.begin(), + basepaths.end()); + + randomfullpath = fs::make_path(randombasepath, + fusepath); + + paths.push_back(Path(randombasepath,randomfullpath)); + } + } }; diff --git a/src/fs.hpp b/src/fs.hpp index 5d51fe31..a4385e3b 100644 --- a/src/fs.hpp +++ b/src/fs.hpp @@ -48,8 +48,8 @@ namespace fs string full; }; - typedef fs::Path (*SearchFunc)(const vector&,const string); - typedef void (*VecSearchFunc)(const vector&,const string,vector&); + typedef vector PathVector; + typedef void (*SearchFunc)(const vector&,const string,PathVector&); string dirname(const string path); string basename(const string path); @@ -65,57 +65,6 @@ namespace fs bool path_exists(const vector &srcmounts, const string fusepath); - Path find_first(vector::const_iterator begin, - vector::const_iterator end, - const string fusepath); - Path find_first(const vector &paths, - const string fusepath); - void find_first(const vector &paths, - const string fusepath, - vector &rv); - - Path find_first_with_permission(vector::const_iterator begin, - vector::const_iterator end, - const string fusepath); - Path find_first_with_permission(const vector &paths, - const string fusepath); - void find_first_with_permission(const vector &paths, - const string fusepath, - vector &rv); - - Path find_newest(vector::const_iterator begin, - vector::const_iterator end, - const string fusepath); - Path find_newest(const vector &paths, - const string fusepath); - void find_newest(const vector &paths, - const string fusepath, - vector &rv); - - void find_all(const vector &paths, - const string fusepath, - vector &rv); - - Path find_mfs(vector::const_iterator iter, - vector::const_iterator enditer, - const string fusepath); - Path find_mfs(const vector &paths, - const string fusepath); - - Path find_mfs_existing(vector::const_iterator iter, - vector::const_iterator enditer, - const string fusepath); - Path find_mfs_existing(const vector &paths, - const string fusepath); - - Path find_random(vector::const_iterator iter, - vector::const_iterator enditer, - const string fusepath); - Path find_random(const vector &paths, - const string fusepath); - - - int clonepath(const string srcfrom, const string srcto, const string relative); @@ -154,6 +103,34 @@ namespace fs int copyattr(const string from, const string to); + + namespace find + { + void invalid(const vector &basepaths, + const string fusepath, + PathVector &paths); + void ff(const vector &basepaths, + const string fusepath, + PathVector &paths); + void ffwp(const vector &paths, + const string fusepath, + PathVector &rv); + void newest(const vector &paths, + const string fusepath, + PathVector &rv); + void all(const vector &paths, + const string fusepath, + PathVector &rv); + void mfs(const vector &paths, + const string fusepath, + PathVector &rv); + void epmfs(const vector &paths, + const string fusepath, + PathVector &rv); + void rand(const vector &paths, + const string fusepath, + PathVector &rv); + } }; #endif // __FS_HPP__ diff --git a/src/getattr.cpp b/src/getattr.cpp index 5d34691a..bfed8942 100644 --- a/src/getattr.cpp +++ b/src/getattr.cpp @@ -43,19 +43,19 @@ using mergerfs::Policy; static int -_getattr(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string fusepath, - struct stat &buf) +_getattr(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + struct stat &buf) { int rv; - string path; + fs::PathVector paths; - path = searchFunc(srcmounts,fusepath).full; - if(path.empty()) + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) return -ENOENT; - rv = ::lstat(path.c_str(),&buf); + rv = ::lstat(paths[0].full.c_str(),&buf); return ((rv == -1) ? -errno : 0); } @@ -74,7 +74,7 @@ namespace mergerfs if(fusepath == config.controlfile) return (*buf = config.controlfilestat,0); - return _getattr(config.policy.search, + return _getattr(*config.search, config.srcmounts, fusepath, *buf); diff --git a/src/getxattr.cpp b/src/getxattr.cpp index eed257cc..6937427b 100644 --- a/src/getxattr.cpp +++ b/src/getxattr.cpp @@ -38,10 +38,11 @@ using std::string; using std::vector; using mergerfs::Policy; +using namespace mergerfs::config; static int -_getxattr_controlfile(const Policy &policy, +_getxattr_controlfile(const Config &config, const string attrname, char *buf, const size_t count) @@ -51,16 +52,16 @@ _getxattr_controlfile(const Policy &policy, string attrvalue; if(attrname == "user.mergerfs.action") - attrvalue = policy.action.str(); + attrvalue = (std::string)*config.action; else if(attrname == "user.mergerfs.create") - attrvalue = policy.create.str(); + attrvalue = (std::string)*config.create; else if(attrname == "user.mergerfs.search") - attrvalue = policy.search.str(); + attrvalue = (std::string)*config.search; if(attrvalue.empty()) return -ENOATTR; - len = attrvalue.size() + 1; + len = attrvalue.size(); if(count == 0) return len; @@ -78,22 +79,22 @@ _getxattr_controlfile(const Policy &policy, static int -_getxattr(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const char *attrname, - char *buf, - const size_t count) +_getxattr(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname, + char *buf, + const size_t count) { #ifndef WITHOUT_XATTR int rv; - string path; + fs::PathVector paths; - path = searchFunc(srcmounts,fusepath).full; - if(path.empty()) + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) return -ENOENT; - rv = ::lgetxattr(path.c_str(),attrname,buf,count); + rv = ::lgetxattr(paths[0].full.c_str(),attrname,buf,count); return ((rv == -1) ? -errno : rv); #else @@ -115,12 +116,12 @@ namespace mergerfs const config::Config &config = config::get(); if(fusepath == config.controlfile) - return _getxattr_controlfile(config.policy, + return _getxattr_controlfile(config, attrname, buf, count); - return _getxattr(config.policy.search, + return _getxattr(*config.search, config.srcmounts, fusepath, attrname, diff --git a/src/link.cpp b/src/link.cpp index a0b4a63c..428022e6 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -39,14 +39,14 @@ using mergerfs::Policy; static int -_link(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string from, - const string to) +_link(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string from, + const string to) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,from,paths); if(paths.empty()) @@ -54,7 +54,7 @@ _link(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { const string pathfrom = fs::make_path(i->base,from); @@ -82,7 +82,7 @@ namespace mergerfs if(from == config.controlfile) return -EPERM; - return _link(config.policy.action, + return _link(*config.action, config.srcmounts, from, to); diff --git a/src/listxattr.cpp b/src/listxattr.cpp index 8a6ec1c2..de3af976 100644 --- a/src/listxattr.cpp +++ b/src/listxattr.cpp @@ -30,6 +30,7 @@ #include #include "config.hpp" +#include "category.hpp" #include "fs.hpp" #include "ugid.hpp" #include "assert.hpp" @@ -37,7 +38,6 @@ using std::string; using std::vector; -using mergerfs::Policy; using namespace mergerfs; static @@ -46,21 +46,23 @@ _listxattr_controlfile(char *list, const size_t size) { #ifndef WITHOUT_XATTR - const char xattrs[] = - "user.mergerfs.action\0" - "user.mergerfs.create\0" - "user.mergerfs.search\0" - "user.mergerfs.statfs\0"; + size_t xattrssize; + string xattrs; + + for(int i = Category::Enum::BEGIN; i < Category::Enum::END; ++i) + xattrs += "user.mergerfs." + (string)Category::categories[i] + '\0'; + + xattrssize = xattrs.size(); if(size == 0) - return sizeof(xattrs); + return xattrssize; - if(size < sizeof(xattrs)) + if(size < xattrssize) return -ERANGE; - memcpy(list,xattrs,sizeof(xattrs)); + memcpy(list,xattrs.data(),xattrssize); - return sizeof(xattrs); + return xattrssize; #else return -ENOTSUP; #endif @@ -68,21 +70,21 @@ _listxattr_controlfile(char *list, static int -_listxattr(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string fusepath, - char *list, - const size_t size) +_listxattr(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + char *list, + const size_t size) { #ifndef WITHOUT_XATTR int rv; - string path; + fs::PathVector paths; - path = searchFunc(srcmounts,fusepath).full; - if(path.empty()) + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) return -ENOENT; - rv = ::llistxattr(path.c_str(),list,size); + rv = ::llistxattr(paths[0].full.c_str(),list,size); return ((rv == -1) ? -errno : rv); #else @@ -106,7 +108,7 @@ namespace mergerfs return _listxattr_controlfile(list, size); - return _listxattr(config.policy.search, + return _listxattr(*config.search, config.srcmounts, fusepath, list, diff --git a/src/mergerfs.cpp b/src/mergerfs.cpp index 0502f89d..f42a6faa 100644 --- a/src/mergerfs.cpp +++ b/src/mergerfs.cpp @@ -90,7 +90,6 @@ get_fuse_operations() ops.getdir = NULL; /* deprecated; use readdir */ ops.getxattr = mergerfs::getxattr::getxattr; ops.init = NULL; - ops.ioctl = NULL; ops.ioctl = mergerfs::ioctl::ioctl; ops.link = mergerfs::link::link; ops.listxattr = mergerfs::listxattr::listxattr; diff --git a/src/mkdir.cpp b/src/mkdir.cpp index b42479da..f250b236 100644 --- a/src/mkdir.cpp +++ b/src/mkdir.cpp @@ -43,31 +43,31 @@ using mergerfs::Policy; static int -_mkdir(const Policy::Search::Func searchFunc, - const Policy::Create::Func createPathFunc, - const vector &srcmounts, - const string fusepath, - const mode_t mode) +_mkdir(const fs::SearchFunc searchFunc, + const fs::SearchFunc createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode) { int rv; string path; string dirname; - fs::Path createpath; - fs::Path existingpath; + fs::PathVector createpath; + fs::PathVector existingpath; if(fs::path_exists(srcmounts,fusepath)) return -EEXIST; dirname = fs::dirname(fusepath); - existingpath = searchFunc(srcmounts,dirname); - if(existingpath.base.empty()) + searchFunc(srcmounts,dirname,existingpath); + if(existingpath.empty()) return -ENOENT; - createpath = createPathFunc(srcmounts,dirname); - if(createpath.base != existingpath.base) - fs::clonepath(existingpath.base,createpath.base,dirname); + createPathFunc(srcmounts,dirname,createpath); + if(createpath[0].base != existingpath[0].base) + fs::clonepath(existingpath[0].base,createpath[0].base,dirname); - path = fs::make_path(createpath.base,fusepath); + path = fs::make_path(createpath[0].base,fusepath); rv = ::mkdir(path.c_str(),mode); @@ -88,8 +88,8 @@ namespace mergerfs if(fusepath == config.controlfile) return -EEXIST; - return _mkdir(config.policy.search, - config.policy.create, + return _mkdir(*config.search, + *config.create, config.srcmounts, fusepath, mode); diff --git a/src/mknod.cpp b/src/mknod.cpp index 8a8210b2..9c860f38 100644 --- a/src/mknod.cpp +++ b/src/mknod.cpp @@ -44,32 +44,32 @@ using mergerfs::Policy; static int -_mknod(const Policy::Search::Func searchFunc, - const Policy::Create::Func createPathFunc, - const vector &srcmounts, - const string fusepath, - const mode_t mode, - const dev_t dev) +_mknod(const fs::SearchFunc searchFunc, + const fs::SearchFunc createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode, + const dev_t dev) { int rv; string path; string dirname; - fs::Path createpath; - fs::Path existingpath; + fs::PathVector createpath; + fs::PathVector existingpath; if(fs::path_exists(srcmounts,fusepath)) return -EEXIST; dirname = fs::dirname(fusepath); - existingpath = searchFunc(srcmounts,dirname); - if(existingpath.base.empty()) + searchFunc(srcmounts,dirname,existingpath); + if(existingpath.empty()) return -ENOENT; - createpath = createPathFunc(srcmounts,dirname); - if(existingpath.base != createpath.base) - fs::clonepath(existingpath.base,createpath.base,dirname); + createPathFunc(srcmounts,dirname,createpath); + if(existingpath[0].base != createpath[0].base) + fs::clonepath(existingpath[0].base,createpath[0].base,dirname); - path = fs::make_path(createpath.base,fusepath); + path = fs::make_path(createpath[0].base,fusepath); rv = ::mknod(path.c_str(),mode,dev); @@ -91,8 +91,8 @@ namespace mergerfs if(fusepath == config.controlfile) return -EEXIST; - return _mknod(config.policy.search, - config.policy.create, + return _mknod(*config.search, + *config.create, config.srcmounts, fusepath, mode, diff --git a/src/open.cpp b/src/open.cpp index b2ffcc12..9b05a968 100644 --- a/src/open.cpp +++ b/src/open.cpp @@ -53,24 +53,24 @@ _open_controlfile(uint64_t &fh) static int -_open(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const int flags, - uint64_t &fh) +_open(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const int flags, + uint64_t &fh) { int fd; - fs::Path path; + fs::PathVector paths; - path = searchFunc(srcmounts,fusepath); - if(path.full.empty()) + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) return -ENOENT; - fd = ::open(path.full.c_str(),flags); + fd = ::open(paths[0].full.c_str(),flags); if(fd == -1) return -errno; - fh = (uint64_t)new FileInfo(fd,flags,path.full); + fh = (uint64_t)new FileInfo(fd,flags,paths[0].full); return 0; } @@ -89,7 +89,7 @@ namespace mergerfs if(fusepath == config.controlfile) return _open_controlfile(fileinfo->fh); - return _open(config.policy.search, + return _open(*config.search, config.srcmounts, fusepath, fileinfo->flags, diff --git a/src/option_parser.cpp b/src/option_parser.cpp index 17b9fb1d..14fea606 100644 --- a/src/option_parser.cpp +++ b/src/option_parser.cpp @@ -62,18 +62,17 @@ process_opt(config::Config &config, { int rv = 0; std::vector argvalue; - Policy &policy = config.policy; split(argvalue,arg,'='); switch(argvalue.size()) { case 2: if(argvalue[0] == "create") - policy.create.fromString(argvalue[1]); + config.create = Policy::find(argvalue[1]); else if(argvalue[0] == "search") - policy.search.fromString(argvalue[1]); + config.search = Policy::find(argvalue[1]); else if(argvalue[0] == "action") - policy.action.fromString(argvalue[1]); + config.action = Policy::find(argvalue[1]); else rv = 1; break; diff --git a/src/policy.cpp b/src/policy.cpp index 7955fc9c..233a5b48 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -22,236 +22,57 @@ THE SOFTWARE. */ +#include + #include "policy.hpp" #include "fs.hpp" +#include "buildvector.hpp" + +#define POLICY(X) (Policy(Policy::Enum::X,#X,fs::find::X)) namespace mergerfs { - Policy::Create::Create(Type value) - { - *this = value; - } - - Policy::Create& - Policy::Create::operator=(const Policy::Create::Type value) - { - _str = toString(value); - _value = fromString(_str); - _func = findFunc(_value); - - return *this; - } - - Policy::Create& - Policy::Create::operator=(const std::string str) - { - _value = fromString(str); - _str = toString(_value); - _func = findFunc(_value); - - return *this; - } - - Policy::Create::Type - Policy::Create::fromString(const std::string str) - { - if(str == "ep") - return Policy::Create::ExistingPath; - else if(str == "mfs") - return Policy::Create::MostFreeSpace; - else if(str == "epmfs") - return Policy::Create::ExistingPathMostFreeSpace; - else if(str == "rand") - return Policy::Create::Random; - - return Policy::Create::Invalid; - } - - std::string - Policy::Create::toString(const Type value) - { - switch(value) - { - case Policy::Create::ExistingPath: - return "ep"; - case Policy::Create::ExistingPathMostFreeSpace: - return "epmfs"; - case Policy::Create::MostFreeSpace: - return "mfs"; - case Policy::Create::Random: - return "rand"; - case Policy::Create::Invalid: - default: - return "invalid"; - } - } - - Policy::Create::Func - Policy::Create::findFunc(Policy::Create::Type value) - { - switch(value) - { - case Policy::Create::ExistingPath: - return fs::find_first; - case Policy::Create::ExistingPathMostFreeSpace: - return fs::find_mfs_existing; - case Policy::Create::MostFreeSpace: - return fs::find_mfs; - case Policy::Create::Random: - return fs::find_random; - case Policy::Create::Invalid: - default: - return NULL; - } - } - - Policy::Search::Search(Type value) - { - *this = value; - } - - Policy::Search& - Policy::Search::operator=(const Policy::Search::Type value) - { - _str = toString(value); - _value = fromString(_str); - _func = findFunc(_value); - - return *this; - } - - Policy::Search& - Policy::Search::operator=(const std::string str) - { - _value = fromString(str); - _str = toString(_value); - _func = findFunc(_value); - - return *this; - } - - Policy::Search::Type - Policy::Search::fromString(const std::string str) - { - if(str == "ff") - return Policy::Search::FirstFound; - else if(str == "ffwp") - return Policy::Search::FirstFoundWithPermissions; - else if(str == "newest") - return Policy::Search::Newest; - - return Policy::Search::Invalid; - } - - std::string - Policy::Search::toString(Policy::Search::Type value) - { - switch(value) + const std::vector Policy::_policies_ = + buildvector + (POLICY(invalid)) + (POLICY(all)) + (POLICY(epmfs)) + (POLICY(ff)) + (POLICY(ffwp)) + (POLICY(mfs)) + (POLICY(newest)) + (POLICY(rand)); + + const Policy * const Policy::policies = &_policies_[1]; + + const Policy &Policy::invalid = Policy::policies[Policy::Enum::invalid]; + const Policy &Policy::all = Policy::policies[Policy::Enum::all]; + const Policy &Policy::epmfs = Policy::policies[Policy::Enum::epmfs]; + const Policy &Policy::ff = Policy::policies[Policy::Enum::ff]; + const Policy &Policy::ffwp = Policy::policies[Policy::Enum::ffwp]; + const Policy &Policy::mfs = Policy::policies[Policy::Enum::mfs]; + const Policy &Policy::newest = Policy::policies[Policy::Enum::newest]; + const Policy &Policy::rand = Policy::policies[Policy::Enum::rand]; + + const Policy& + Policy::find(const std::string str) + { + for(int i = Enum::BEGIN; i != Enum::END; ++i) { - case Policy::Search::FirstFound: - return "ff"; - case Policy::Search::FirstFoundWithPermissions: - return "ffwp"; - case Policy::Search::Newest: - return "newest"; - case Policy::Search::Invalid: - default: - return "invalid"; + if(policies[i] == str) + return policies[i]; } - } - - Policy::Search::Func - Policy::Search::findFunc(Policy::Search::Type value) - { - switch(value) - { - case Policy::Search::FirstFound: - return fs::find_first; - case Policy::Search::FirstFoundWithPermissions: - return fs::find_first_with_permission; - case Policy::Search::Newest: - return fs::find_newest; - case Policy::Search::Invalid: - default: - return NULL; - } - } - - Policy::Action::Action(const Policy::Action::Type value) - { - *this = value; - } - - Policy::Action& - Policy::Action::operator=(const Policy::Action::Type value) - { - _str = toString(value); - _value = fromString(_str); - _func = findFunc(_value); - return *this; + return invalid; } - Policy::Action& - Policy::Action::operator=(const std::string str) + const Policy& + Policy::find(const Policy::Enum::Type i) { - _value = fromString(str); - _str = toString(_value); - _func = findFunc(_value); - - return *this; - } - - Policy::Action::Type - Policy::Action::fromString(const std::string str) - { - if(str == "ff") - return Policy::Action::FirstFound; - else if(str == "ffwp") - return Policy::Action::FirstFoundWithPermissions; - else if(str == "newest") - return Policy::Action::Newest; - else if(str == "all") - return Policy::Action::All; - - return Policy::Action::Invalid; - } + if(i >= Policy::Enum::BEGIN && + i < Policy::Enum::END) + return policies[i]; - std::string - Policy::Action::toString(Policy::Action::Type value) - { - switch(value) - { - case Policy::Action::FirstFound: - return "ff"; - case Policy::Action::FirstFoundWithPermissions: - return "ffwp"; - case Policy::Action::Newest: - return "newest"; - case Policy::Action::All: - return "all"; - case Policy::Action::Invalid: - default: - return "invalid"; - } - } - - Policy::Action::Func - Policy::Action::findFunc(const Policy::Action::Type value) - { - switch(value) - { - case Policy::Action::FirstFound: - return fs::find_first; - case Policy::Action::FirstFoundWithPermissions: - return fs::find_first_with_permission; - case Policy::Action::Newest: - return fs::find_newest; - case Policy::Action::All: - return fs::find_all; - case Policy::Action::Invalid: - default: - return NULL; - } + return invalid; } } diff --git a/src/policy.hpp b/src/policy.hpp index 5497a70c..87054729 100644 --- a/src/policy.hpp +++ b/src/policy.hpp @@ -26,6 +26,7 @@ #define __POLICY_HPP__ #include +#include #include "fs.hpp" @@ -34,138 +35,79 @@ namespace mergerfs class Policy { public: - enum Type - { - ACTION, - CREATE, - SEARCH - }; - - public: - class Create + struct Enum { - public: - typedef fs::SearchFunc Func; enum Type { - Invalid = -1, - ExistingPath, - MostFreeSpace, - ExistingPathMostFreeSpace, - Random, - Max + invalid = -1, + BEGIN = 0, + all = BEGIN, + epmfs, + ff, + ffwp, + mfs, + newest, + rand, + END }; - - public: - Create(Type); - - operator Type() const { return _value; } - operator Func() const { return _func; } - operator std::string() const { return _str; } - std::string str() const { return _str; } - - Create& operator=(const Type); - Create& operator=(const std::string); - - static Type fromString(const std::string); - static std::string toString(const Type); - static Func findFunc(const Type); - - private: - Create(); - - private: - Type _value; - std::string _str; - Func _func; }; - class Search - { - public: - typedef fs::SearchFunc Func; - enum Type - { - Invalid = -1, - FirstFound, - FirstFoundWithPermissions, - Newest, - Max - }; - - public: - Search(Type); - - operator Type() const { return _value; } - operator Func() const { return _func; } - operator std::string() const { return _str; } - std::string str() const { return _str; } - - Search& operator=(const Type); - Search& operator=(const std::string); - - static Type fromString(const std::string); - static std::string toString(const Type); - static Func findFunc(const Type); + private: + Enum::Type _enum; + std::string _str; + fs::SearchFunc _func; - private: - Search(); - - private: - Type _value; - std::string _str; - Func _func; - }; - - class Action + public: + Policy() + : _enum(invalid), + _str(invalid), + _func(invalid) { - public: - typedef fs::VecSearchFunc Func; - enum Type - { - Invalid = -1, - FirstFound, - FirstFoundWithPermissions, - Newest, - All, - Max - }; - - public: - Action(Type); - - operator Type() const { return _value; } - operator Func() const { return _func; } - operator std::string() const { return _str; } - std::string str() const { return _str; } - - Action& operator=(const Type); - Action& operator=(const std::string); - - static Type fromString(const std::string); - static std::string toString(const Type); - static Func findFunc(const Type); - - private: - Action(); + } + + Policy(const Enum::Type enum_, + const std::string str_, + const fs::SearchFunc func_) + : _enum(enum_), + _str(str_), + _func(func_) + { + } - private: - Type _value; - std::string _str; - Func _func; - }; + public: + operator const Enum::Type() const { return _enum; } + operator const std::string() const { return _str; } + operator const fs::SearchFunc() 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::SearchFunc func_) const + { return _func == func_; } + + bool operator!=(const Policy &r) const + { return _enum != r._enum; } + + bool operator<(const Policy &r) const + { return _enum < r._enum; } public: - Policy() : - create(Create::ExistingPathMostFreeSpace), - search(Search::FirstFound), - action(Action::FirstFound) - {} + static const Policy &find(const std::string); + static const Policy &find(const Enum::Type); public: - Create create; - Search search; - Action action; + static const std::vector _policies_; + static const Policy * const policies; + static const Policy &invalid; + static const Policy &all; + static const Policy &epmfs; + static const Policy &ff; + static const Policy &ffwp; + static const Policy &mfs; + static const Policy &newest; + static const Policy &rand; }; } diff --git a/src/readlink.cpp b/src/readlink.cpp index 05887485..863be8f6 100644 --- a/src/readlink.cpp +++ b/src/readlink.cpp @@ -41,20 +41,20 @@ using mergerfs::Policy; static int -_readlink(const Policy::Search::Func searchFunc, - const vector& srcmounts, - const string fusepath, - char *buf, - const size_t size) +_readlink(const fs::SearchFunc searchFunc, + const vector& srcmounts, + const string fusepath, + char *buf, + const size_t size) { int rv; - string path; + fs::PathVector paths; - path = searchFunc(srcmounts,fusepath).full; - if(path.empty()) + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) return -ENOENT; - rv = ::readlink(path.c_str(),buf,size); + rv = ::readlink(paths[0].full.c_str(),buf,size); if(rv == -1) return -errno; @@ -78,7 +78,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EINVAL; - return _readlink(config.policy.search, + return _readlink(*config.search, config.srcmounts, fusepath, buf, diff --git a/src/removexattr.cpp b/src/removexattr.cpp index 2b866ca4..6afe315a 100644 --- a/src/removexattr.cpp +++ b/src/removexattr.cpp @@ -40,15 +40,15 @@ using mergerfs::Policy; static int -_removexattr(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const char *attrname) +_removexattr(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname) { #ifndef WITHOUT_XATTR int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -56,7 +56,7 @@ _removexattr(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::lremovexattr(i->full.c_str(),attrname); @@ -84,7 +84,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -ENOTSUP; - return _removexattr(config.policy.action, + return _removexattr(*config.action, config.srcmounts, fusepath, attrname); diff --git a/src/rename.cpp b/src/rename.cpp index 49a277a2..989110d8 100644 --- a/src/rename.cpp +++ b/src/rename.cpp @@ -39,22 +39,22 @@ using mergerfs::Policy; static int -_rename(const Policy::Search::Func searchFunc, - const vector &srcmounts, - const string from, - const string to) +_rename(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string from, + const string to) { int rv; string pathto; - fs::Path pathfrom; + fs::PathVector pathfrom; - pathfrom = searchFunc(srcmounts,from); - if(pathfrom.base.empty()) + searchFunc(srcmounts,from,pathfrom); + if(pathfrom.empty()) return -ENOENT; - pathto = fs::make_path(pathfrom.base,to); + pathto = fs::make_path(pathfrom[0].base,to); - rv = ::rename(pathfrom.full.c_str(),pathto.c_str()); + rv = ::rename(pathfrom[0].full.c_str(),pathto.c_str()); return ((rv == -1) ? -errno : 0); } @@ -73,7 +73,7 @@ namespace mergerfs if(from == config.controlfile) return -ENOENT; - return _rename(config.policy.search, + return _rename(*config.search, config.srcmounts, from, to); diff --git a/src/rmdir.cpp b/src/rmdir.cpp index ff0df2ed..ed519ef5 100644 --- a/src/rmdir.cpp +++ b/src/rmdir.cpp @@ -38,13 +38,13 @@ using mergerfs::Policy; static int -_rmdir(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath) +_rmdir(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -52,7 +52,7 @@ _rmdir(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::rmdir(i->full.c_str()); @@ -76,7 +76,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -ENOTDIR; - return _rmdir(config.policy.action, + return _rmdir(*config.action, config.srcmounts, fusepath); } diff --git a/src/setxattr.cpp b/src/setxattr.cpp index f8f9fd61..03bc5656 100644 --- a/src/setxattr.cpp +++ b/src/setxattr.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -37,8 +38,24 @@ using std::string; using std::vector; using mergerfs::Policy; +using mergerfs::Category; using namespace mergerfs; +template +Container& +split(Container &result, + const typename Container::value_type &s, + typename Container::value_type::value_type delimiter) +{ + std::string str; + std::istringstream ss(s); + + while(std::getline(ss,str,delimiter)) + result.push_back(str); + + return result; +} + static int _setxattr_controlfile(config::Config &config, @@ -48,37 +65,33 @@ _setxattr_controlfile(config::Config &config, const int flags) { #ifndef WITHOUT_XATTR - if(attrname == "user.mergerfs.action") - { - if((flags & XATTR_CREATE) == XATTR_CREATE) - return -EEXIST; - if(Policy::Action::fromString(attrval) != -1) - config.policy.action = attrval; - else - return -ENOSPC; - } - else if(attrname == "user.mergerfs.create") - { - if((flags & XATTR_CREATE) == XATTR_CREATE) - return -EEXIST; - if(Policy::Create::fromString(attrval) != -1) - config.policy.create = attrval; - else - return -ENOSPC; - } - else if(attrname == "user.mergerfs.search") - { - if((flags & XATTR_CREATE) == XATTR_CREATE) - return -EEXIST; - if(Policy::Search::fromString(attrval) != -1) - config.policy.search = attrval; - else - return -ENOSPC; - } - else - { - return -ENOATTR; - } + const Category *cat; + const Policy *policy; + vector nameparts; + + split(nameparts,attrname,'.'); + + if(nameparts.size() != 3) + return -EINVAL; + + if(nameparts[0] != "user") + return -ENOATTR; + + if(nameparts[1] != "mergerfs") + return -ENOATTR; + + cat = Category::find(nameparts[2]); + if(cat == Category::invalid) + return -ENOATTR; + + if((flags & XATTR_CREATE) == XATTR_CREATE) + return -EEXIST; + + policy = Policy::find(attrval); + if(policy == Policy::invalid) + return -EINVAL; + + config.policies[*cat] = policy; config.updateReadStr(); @@ -90,18 +103,18 @@ _setxattr_controlfile(config::Config &config, static int -_setxattr(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const char *attrname, - const char *attrval, - const size_t attrvalsize, - const int flags) +_setxattr(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname, + const char *attrval, + const size_t attrvalsize, + const int flags) { #ifndef WITHOUT_XATTR int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -109,7 +122,7 @@ _setxattr(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::lsetxattr(i->full.c_str(),attrname,attrval,attrvalsize,flags); @@ -143,7 +156,7 @@ namespace mergerfs attrvalsize, flags); - return _setxattr(config.policy.action, + return _setxattr(*config.action, config.srcmounts, fusepath, attrname, diff --git a/src/symlink.cpp b/src/symlink.cpp index 6f5b04b2..14db2474 100644 --- a/src/symlink.cpp +++ b/src/symlink.cpp @@ -43,15 +43,15 @@ _symlink(const vector &srcmounts, const string to) { int rv; - fs::Path path; + fs::PathVector paths; - path = fs::find_first(srcmounts,fs::dirname(to)); - if(path.base.empty()) + fs::find::ff(srcmounts,fs::dirname(to),paths); + if(paths.empty()) return -ENOENT; - path.full = fs::make_path(path.base,to); + paths[0].full = fs::make_path(paths[0].base,to); - rv = symlink(from.c_str(),path.full.c_str()); + rv = symlink(from.c_str(),paths[0].full.c_str()); return ((rv == -1) ? -errno : 0); } diff --git a/src/test.cpp b/src/test.cpp index be57116b..d86fc677 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -48,6 +48,8 @@ namespace mergerfs test(const struct fuse_args &args, const mergerfs::config::Config &config) { + + return 0; } } diff --git a/src/truncate.cpp b/src/truncate.cpp index f38cbd49..b153d20f 100644 --- a/src/truncate.cpp +++ b/src/truncate.cpp @@ -42,14 +42,14 @@ using mergerfs::Policy; static int -_truncate(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const off_t size) +_truncate(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const off_t size) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -57,7 +57,7 @@ _truncate(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::truncate(i->full.c_str(),size); @@ -82,7 +82,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EPERM; - return _truncate(config.policy.action, + return _truncate(*config.action, config.srcmounts, fusepath, size); diff --git a/src/unlink.cpp b/src/unlink.cpp index f9d046ed..4003a448 100644 --- a/src/unlink.cpp +++ b/src/unlink.cpp @@ -39,13 +39,13 @@ using mergerfs::Policy; static int -_unlink(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath) +_unlink(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -53,7 +53,7 @@ _unlink(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::unlink(i->full.c_str()); @@ -77,7 +77,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EPERM; - return _unlink(config.policy.action, + return _unlink(*config.action, config.srcmounts, fusepath); } diff --git a/src/utimens.cpp b/src/utimens.cpp index b68fb0cf..2cfdcdf5 100644 --- a/src/utimens.cpp +++ b/src/utimens.cpp @@ -39,14 +39,14 @@ using mergerfs::Policy; static int -_utimens(const Policy::Action::Func searchFunc, - const vector &srcmounts, - const string fusepath, - const struct timespec ts[2]) +_utimens(const fs::SearchFunc searchFunc, + const vector &srcmounts, + const string fusepath, + const struct timespec ts[2]) { int rv; int error; - vector paths; + fs::PathVector paths; searchFunc(srcmounts,fusepath,paths); if(paths.empty()) @@ -54,7 +54,7 @@ _utimens(const Policy::Action::Func searchFunc, rv = -1; error = 0; - for(vector::const_iterator + for(fs::PathVector::const_iterator i = paths.begin(), ei = paths.end(); i != ei; ++i) { rv &= ::utimensat(0,i->full.c_str(),ts,AT_SYMLINK_NOFOLLOW); @@ -79,7 +79,7 @@ namespace mergerfs if(fusepath == config.controlfile) return -EPERM; - return _utimens(config.policy.action, + return _utimens(*config.action, config.srcmounts, fusepath, ts); diff --git a/src/write.cpp b/src/write.cpp index 5de17dd4..15432a07 100644 --- a/src/write.cpp +++ b/src/write.cpp @@ -33,11 +33,13 @@ #include "config.hpp" #include "policy.hpp" +#include "category.hpp" #include "ugid.hpp" #include "fileinfo.hpp" using mergerfs::config::Config; using mergerfs::Policy; +using mergerfs::Category; using std::string; using std::vector; using std::stringstream; @@ -48,35 +50,22 @@ _process_kv(Config &config, const string key, const string value) { - int rv = 0; + const Category *cat; + const Policy *policy; - if(key == "search") - { - rv = (Policy::Search::fromString(value) != -1) ? 0 : -EINVAL; - if(rv == 0) - config.policy.search = value; - } - else if(key == "action") - { - rv = (Policy::Action::fromString(value) != -1) ? 0 : -EINVAL; - if(rv == 0) - config.policy.action = value; - } - else if(key == "create") - { - rv = (Policy::Create::fromString(value) != -1) ? 0 : -EINVAL; - if(rv == 0) - config.policy.create = value; - } - else - { - rv = -EINVAL; - } + cat = Category::find(key); + if(cat == Category::invalid) + return -EINVAL; + + policy = Policy::find(value); + if(policy == Policy::invalid) + return -EINVAL; + + config.policies[*cat] = policy; - if(rv == 0) - config.updateReadStr(); + config.updateReadStr(); - return rv; + return 0; } static