mergerfs/src/option_parser.cpp

264 lines
5.2 KiB
C++
Raw Normal View History

2014-05-12 12:41:46 -04:00
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
2015-06-16 22:12:55 -04:00
#include <stdlib.h>
2014-05-12 12:41:46 -04:00
#include <string>
#include <vector>
2014-05-12 12:41:46 -04:00
#include <sstream>
#include <iostream>
2014-05-12 12:41:46 -04:00
#include "str.hpp"
2015-07-03 11:56:01 -04:00
#include "num.hpp"
2014-05-12 12:41:46 -04:00
#include "config.hpp"
#include "policy.hpp"
using std::string;
using std::vector;
2014-05-12 12:41:46 -04:00
using namespace mergerfs;
2015-03-11 18:14:05 -04:00
static
void
2015-07-21 14:19:17 -04:00
set_option(fuse_args &args,
2015-03-11 18:14:05 -04:00
const std::string &option_)
{
string option;
option = "-o" + option_;
fuse_opt_insert_arg(&args,1,option.c_str());
}
static
void
2015-07-21 14:19:17 -04:00
set_kv_option(fuse_args &args,
2015-03-11 18:14:05 -04:00
const std::string &key,
const std::string &value)
{
std::string option;
option = key + '=' + value;
set_option(args,option);
}
static
void
2015-07-21 14:19:17 -04:00
set_fsname(fuse_args &args,
2015-03-11 18:14:05 -04:00
const vector<string> &srcmounts)
{
if(srcmounts.size() > 0)
{
std::string fsname;
fsname = str::remove_common_prefix_and_join(srcmounts,':');
set_kv_option(args,"fsname",fsname);
}
}
static
void
2015-07-21 14:19:17 -04:00
set_subtype(fuse_args &args)
2015-03-11 18:14:05 -04:00
{
set_kv_option(args,"subtype","mergerfs");
}
static
void
2015-07-21 14:19:17 -04:00
set_default_options(fuse_args &args)
2015-03-11 18:14:05 -04:00
{
// set_option(args,"big_writes");
// set_option(args,"splice_read");
// set_option(args,"splice_write");
// set_option(args,"splice_move");
2015-03-11 18:14:05 -04:00
set_option(args,"auto_cache");
// set_option(args,"atomic_o_trunc");
2015-03-11 18:14:05 -04:00
}
2015-06-16 22:12:55 -04:00
static
int
parse_and_process_minfreespace(const std::string &value,
size_t &minfreespace)
{
2015-07-03 11:56:01 -04:00
int rv;
2015-06-16 22:12:55 -04:00
2015-07-03 11:56:01 -04:00
rv = num::to_size_t(value,minfreespace);
if(rv == -1)
return 1;
2015-06-16 22:12:55 -04:00
return 0;
}
2015-03-11 18:14:05 -04:00
static
int
2015-07-21 14:19:17 -04:00
parse_and_process_arg(Config &config,
2015-03-11 18:14:05 -04:00
const std::string &arg,
2015-07-21 14:19:17 -04:00
fuse_args *outargs)
2015-03-11 18:14:05 -04:00
{
if(arg == "defaults")
{
set_default_options(*outargs);
return 0;
}
return 1;
}
2015-03-10 18:42:59 -04:00
static
int
2015-07-21 14:19:17 -04:00
parse_and_process_kv_arg(Config &config,
2015-03-10 18:42:59 -04:00
const std::string &key,
const std::string &value)
{
2015-06-16 22:12:55 -04:00
int rv = -1;
2015-03-10 18:42:59 -04:00
std::vector<std::string> keypart;
str::split(keypart,key,'.');
2015-06-16 22:12:55 -04:00
if(keypart.size() == 2)
{
if(keypart[0] == "func")
rv = config.set_func_policy(keypart[1],value);
else if(keypart[0] == "category")
rv = config.set_category_policy(keypart[1],value);
}
2015-03-11 18:14:05 -04:00
else
2015-06-16 22:12:55 -04:00
{
if(key == "minfreespace")
rv = parse_and_process_minfreespace(value,config.minfreespace);
}
2015-03-10 18:42:59 -04:00
if(rv == -1)
rv = 1;
return rv;
}
2014-05-12 12:41:46 -04:00
static
int
2015-07-21 14:19:17 -04:00
process_opt(Config &config,
2015-03-11 18:14:05 -04:00
const std::string &arg,
2015-07-21 14:19:17 -04:00
fuse_args *outargs)
2014-05-12 12:41:46 -04:00
{
2015-03-11 18:14:05 -04:00
int rv;
2014-05-12 12:41:46 -04:00
std::vector<std::string> argvalue;
2015-03-11 18:14:05 -04:00
rv = 1;
str::split(argvalue,arg,'=');
2014-05-12 12:41:46 -04:00
switch(argvalue.size())
{
2015-03-11 18:14:05 -04:00
case 1:
rv = parse_and_process_arg(config,argvalue[0],outargs);
break;
2014-05-12 12:41:46 -04:00
case 2:
2015-03-10 18:42:59 -04:00
rv = parse_and_process_kv_arg(config,argvalue[0],argvalue[1]);
2014-05-12 12:41:46 -04:00
break;
default:
rv = 1;
2014-05-12 12:41:46 -04:00
break;
};
return rv;
}
static
int
2015-07-21 14:19:17 -04:00
process_srcmounts(const char *arg,
Config &config)
{
vector<string> paths;
str::split(paths,arg,':');
fs::glob(paths,config.srcmounts);
return 0;
}
static
int
2015-07-21 14:19:17 -04:00
process_destmounts(const char *arg,
Config &config)
{
config.destmount = arg;
return 1;
}
2014-05-12 12:41:46 -04:00
static
int
2015-07-21 14:19:17 -04:00
option_processor(void *data,
const char *arg,
int key,
fuse_args *outargs)
2014-05-12 12:41:46 -04:00
{
2015-07-21 14:19:17 -04:00
int rv = 0;
Config &config = *(Config*)data;
2014-05-12 12:41:46 -04:00
switch(key)
{
case FUSE_OPT_KEY_OPT:
2015-03-11 18:14:05 -04:00
rv = process_opt(config,arg,outargs);
2014-05-12 12:41:46 -04:00
break;
case FUSE_OPT_KEY_NONOPT:
rv = config.srcmounts.empty() ?
process_srcmounts(arg,config) :
process_destmounts(arg,config);
2014-05-12 12:41:46 -04:00
break;
default:
break;
}
return rv;
}
namespace mergerfs
{
namespace options
{
void
2015-07-21 14:19:17 -04:00
parse(fuse_args &args,
Config &config)
2014-05-12 12:41:46 -04:00
{
2014-05-12 12:41:46 -04:00
fuse_opt_parse(&args,
&config,
NULL,
::option_processor);
2015-03-11 18:14:05 -04:00
set_fsname(args,config.srcmounts);
set_subtype(args);
2014-05-12 12:41:46 -04:00
}
}
}