2014-05-13 00:41:46 +08: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>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <stdio.h>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <vector>
|
2014-05-13 00:41:46 +08:00
|
|
|
#include <sstream>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <iostream>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
#include "str.hpp"
|
2014-05-13 00:41:46 +08:00
|
|
|
#include "config.hpp"
|
|
|
|
#include "policy.hpp"
|
|
|
|
|
2014-06-01 05:54:18 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2014-05-13 00:41:46 +08:00
|
|
|
using namespace mergerfs;
|
|
|
|
|
2015-03-11 06:42:59 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
parse_and_process_kv_arg(config::Config &config,
|
|
|
|
const std::string &key,
|
|
|
|
const std::string &value)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
std::vector<std::string> keypart;
|
|
|
|
|
|
|
|
str::split(keypart,key,'.');
|
|
|
|
if(keypart.size() != 2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(rv == -1)
|
|
|
|
rv = 1;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
process_opt(config::Config &config,
|
2014-11-11 10:01:20 +08:00
|
|
|
const std::string &arg)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
std::vector<std::string> argvalue;
|
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
str::split(argvalue,arg,'=');
|
2014-05-13 00:41:46 +08:00
|
|
|
switch(argvalue.size())
|
|
|
|
{
|
|
|
|
case 2:
|
2015-03-11 06:42:59 +08:00
|
|
|
rv = parse_and_process_kv_arg(config,argvalue[0],argvalue[1]);
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-06-26 03:17:26 +08:00
|
|
|
rv = 1;
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-01 05:54:18 +08:00
|
|
|
static
|
2014-08-04 03:50:28 +08:00
|
|
|
int
|
2014-06-01 05:54:18 +08:00
|
|
|
process_srcmounts(const char *arg,
|
|
|
|
config::Config &config)
|
|
|
|
{
|
|
|
|
vector<string> paths;
|
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
str::split(paths,arg,':');
|
|
|
|
|
|
|
|
fs::glob(paths,config.srcmounts);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
process_destmounts(const char *arg,
|
|
|
|
config::Config &config)
|
|
|
|
{
|
|
|
|
config.destmount = arg;
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
return 1;
|
2014-06-01 05:54:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
option_processor(void *data,
|
|
|
|
const char *arg,
|
|
|
|
int key,
|
|
|
|
struct fuse_args *outargs)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
config::Config &config = *(config::Config*)data;
|
|
|
|
|
|
|
|
switch(key)
|
|
|
|
{
|
|
|
|
case FUSE_OPT_KEY_OPT:
|
|
|
|
rv = process_opt(config,arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_OPT_KEY_NONOPT:
|
2014-08-04 03:50:28 +08:00
|
|
|
rv = config.srcmounts.empty() ?
|
|
|
|
process_srcmounts(arg,config) :
|
|
|
|
process_destmounts(arg,config);
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-31 11:23:45 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
set_fsname(struct fuse_args &args,
|
|
|
|
const config::Config &config)
|
|
|
|
{
|
|
|
|
if(config.srcmounts.size() > 0)
|
|
|
|
{
|
|
|
|
std::string fsname;
|
|
|
|
|
2015-03-12 01:50:19 +08:00
|
|
|
fsname = str::remove_common_prefix_and_join(config.srcmounts,':');
|
|
|
|
fsname = "-ofsname=" + fsname;
|
2014-05-31 11:23:45 +08:00
|
|
|
|
|
|
|
fuse_opt_insert_arg(&args,1,fsname.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 01:50:19 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
set_subtype(struct fuse_args &args)
|
|
|
|
{
|
|
|
|
fuse_opt_insert_arg(&args,1,"-osubtype=mergerfs");
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
namespace options
|
|
|
|
{
|
|
|
|
void
|
|
|
|
parse(struct fuse_args &args,
|
|
|
|
config::Config &config)
|
|
|
|
{
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
fuse_opt_parse(&args,
|
|
|
|
&config,
|
|
|
|
NULL,
|
|
|
|
::option_processor);
|
2014-05-31 11:23:45 +08:00
|
|
|
|
|
|
|
set_fsname(args,config);
|
2015-03-12 01:50:19 +08:00
|
|
|
set_subtype(args);
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|