From b3109ac506b6f5782c437e21fa71f52f27c8313c Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Fri, 3 Jul 2015 11:56:01 -0400 Subject: [PATCH] add minfreespace to xattr interface --- README.md | 2 ++ src/getxattr.cpp | 15 +++++++++++++++ src/listxattr.cpp | 12 +++++++++--- src/option_parser.cpp | 27 +++++---------------------- src/setxattr.cpp | 23 +++++++++++++++++++++++ 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4df80c66..d10b1d45 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ Even if xattrs are disabled the [{list,get,set}xattrs](http://linux.die.net/man/ ##### Keys ##### * user.mergerfs.srcmounts +* user.mergerfs.minfreespace * user.mergerfs.category.action * user.mergerfs.category.create * user.mergerfs.category.search @@ -149,6 +150,7 @@ Even if xattrs are disabled the [{list,get,set}xattrs](http://linux.die.net/man/ ``` [trapexit:/tmp/mount] $ xattr -l .mergerfs user.mergerfs.srcmounts: /tmp/a:/tmp/b +user.mergerfs.minfreespace: 4294967295 user.mergerfs.category.action: all user.mergerfs.category.create: epmfs user.mergerfs.category.search: ff diff --git a/src/getxattr.cpp b/src/getxattr.cpp index e8e6d6e5..6e040d96 100644 --- a/src/getxattr.cpp +++ b/src/getxattr.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -92,6 +93,18 @@ _getxattr_controlfile_srcmounts(const Config &config, attrvalue = str::join(config.srcmounts,':'); } +static +void +_getxattr_controlfile_minfreespace(const Config &config, + string &attrvalue) +{ + char buf[64]; + + snprintf(buf,sizeof(buf),"%li",config.minfreespace); + + attrvalue = buf; +} + static int _getxattr_controlfile(const Config &config, @@ -108,6 +121,8 @@ _getxattr_controlfile(const Config &config, if(!strcmp("srcmounts",attrbasename)) _getxattr_controlfile_srcmounts(config,attrvalue); + else if(!strcmp("minfreespace",attrbasename)) + _getxattr_controlfile_minfreespace(config,attrvalue); else if(!strncmp("category.",attrbasename,sizeof("category.")-1)) _getxattr_controlfile_category_policy(config,&attrbasename[sizeof("category.")-1],attrvalue); else if(!strncmp("func.",attrbasename,sizeof("func.")-1)) diff --git a/src/listxattr.cpp b/src/listxattr.cpp index 4715f443..8c21795d 100644 --- a/src/listxattr.cpp +++ b/src/listxattr.cpp @@ -37,6 +37,7 @@ #include "ugid.hpp" #include "rwlock.hpp" #include "xattr.hpp" +#include "buildvector.hpp" using std::string; using std::vector; @@ -48,12 +49,17 @@ _listxattr_controlfile(char *list, const size_t size) { string xattrs; + const vector strs = + buildvector + ("user.mergerfs.srcmounts") + ("user.mergerfs.minfreespace"); xattrs.reserve(512); - xattrs.append("user.mergerfs.srcmounts",sizeof("user.mergerfs.srcmounts")); - for(int i = Category::Enum::BEGIN; i < Category::Enum::END; i++) + for(size_t i = 0; i < strs.size(); i++) + xattrs += (strs[i] + '\0'); + for(size_t i = Category::Enum::BEGIN; i < Category::Enum::END; i++) xattrs += ("user.mergerfs.category." + (std::string)*Category::categories[i] + '\0'); - for(int i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++) + for(size_t i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++) xattrs += ("user.mergerfs.func." + (std::string)*FuseFunc::fusefuncs[i] + '\0'); if(size == 0) diff --git a/src/option_parser.cpp b/src/option_parser.cpp index 96bc9563..ef355a7e 100644 --- a/src/option_parser.cpp +++ b/src/option_parser.cpp @@ -35,6 +35,7 @@ #include #include "str.hpp" +#include "num.hpp" #include "config.hpp" #include "policy.hpp" @@ -106,29 +107,11 @@ int parse_and_process_minfreespace(const std::string &value, size_t &minfreespace) { - char *endptr; - - minfreespace = strtoll(value.c_str(),&endptr,10); - switch(*endptr) - { - case 'k': - case 'K': - minfreespace *= 1024; - break; - - case 'm': - case 'M': - minfreespace *= (1024 * 1024); - break; - - case 'g': - case 'G': - minfreespace *= (1024 * 1024 * 1024); - break; + int rv; - default: - return 1; - } + rv = num::to_size_t(value,minfreespace); + if(rv == -1) + return 1; return 0; } diff --git a/src/setxattr.cpp b/src/setxattr.cpp index 00754f8b..2084e73a 100644 --- a/src/setxattr.cpp +++ b/src/setxattr.cpp @@ -38,6 +38,7 @@ #include "rwlock.hpp" #include "xattr.hpp" #include "str.hpp" +#include "num.hpp" using std::string; using std::vector; @@ -165,6 +166,24 @@ _setxattr_srcmounts(vector &srcmounts, return -EINVAL; } +static +int +_setxattr_minfreespace(config::Config &config, + const string &attrval, + const int flags) +{ + int rv; + + if((flags & XATTR_CREATE) == XATTR_CREATE) + return -EEXIST; + + rv = num::to_size_t(attrval,config.minfreespace); + if(rv == -1) + return -EINVAL; + + return 0; +} + static int _setxattr_controlfile_func_policy(config::Config &config, @@ -224,6 +243,10 @@ _setxattr_controlfile(config::Config &config, config.destmount, attrval, flags); + else if(!strcmp("minfreespace",attrbasename)) + return _setxattr_minfreespace(config, + attrval, + flags); else if(!strncmp("category.",attrbasename,sizeof("category.")-1)) return _setxattr_controlfile_category_policy(config, &attrbasename[sizeof("category.")-1],