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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __POLICY_HPP__
|
|
|
|
#define __POLICY_HPP__
|
|
|
|
|
|
|
|
#include <string>
|
2015-06-22 23:35:46 -04:00
|
|
|
#include <vector>
|
2014-05-27 21:21:45 -04:00
|
|
|
#include <map>
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-06-22 23:35:46 -04:00
|
|
|
#include "path.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
#include "fs.hpp"
|
2015-06-25 17:55:16 -04:00
|
|
|
#include "category.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
class Policy
|
|
|
|
{
|
|
|
|
public:
|
2014-05-27 21:21:45 -04:00
|
|
|
struct Enum
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
enum Type
|
|
|
|
{
|
2014-05-27 21:21:45 -04:00
|
|
|
invalid = -1,
|
|
|
|
BEGIN = 0,
|
2015-02-24 18:47:31 -05:00
|
|
|
all = BEGIN,
|
|
|
|
epmfs,
|
2014-05-27 21:21:45 -04:00
|
|
|
ff,
|
|
|
|
ffwp,
|
2015-06-16 22:48:45 -04:00
|
|
|
fwfs,
|
2015-06-16 22:12:55 -04:00
|
|
|
lfs,
|
2014-05-27 21:21:45 -04:00
|
|
|
mfs,
|
|
|
|
newest,
|
|
|
|
rand,
|
|
|
|
END
|
2014-05-12 12:41:46 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-06-23 22:24:55 -04:00
|
|
|
struct Func
|
|
|
|
{
|
|
|
|
typedef std::string string;
|
|
|
|
typedef std::size_t size_t;
|
|
|
|
typedef std::vector<string> strvec;
|
2015-06-25 17:55:16 -04:00
|
|
|
typedef const string cstring;
|
|
|
|
typedef const size_t csize_t;
|
|
|
|
typedef const strvec cstrvec;
|
|
|
|
typedef const Category::Enum::Type CType;
|
|
|
|
|
|
|
|
typedef int (*Ptr)(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
|
|
|
|
class Action
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Action(const Policy *p)
|
|
|
|
: func(p->_func)
|
|
|
|
{}
|
|
|
|
|
|
|
|
int
|
|
|
|
operator()(cstrvec& b,cstring& c,csize_t d,Paths& e)
|
|
|
|
{
|
|
|
|
return func(Category::Enum::action,b,c,d,e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Ptr func;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Create
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Create(const Policy *p)
|
|
|
|
: func(p->_func)
|
|
|
|
{}
|
|
|
|
|
|
|
|
int
|
|
|
|
operator()(cstrvec& b,cstring& c,csize_t d,Paths& e)
|
|
|
|
{
|
|
|
|
return func(Category::Enum::create,b,c,d,e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Ptr func;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Search
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Search(const Policy *p)
|
|
|
|
: func(p->_func)
|
|
|
|
{}
|
|
|
|
|
|
|
|
int
|
|
|
|
operator()(cstrvec& b,cstring& c,csize_t d,Paths& e)
|
|
|
|
{
|
|
|
|
return func(Category::Enum::search,b,c,d,e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Ptr func;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int invalid(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int all(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int epmfs(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int ff(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int ffwp(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int fwfs(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int lfs(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int mfs(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int newest(CType,cstrvec&,cstring&,csize_t,Paths&);
|
|
|
|
static int rand(CType,cstrvec&,cstring&,csize_t,Paths&);
|
2015-06-23 22:24:55 -04:00
|
|
|
};
|
|
|
|
|
2014-05-27 21:21:45 -04:00
|
|
|
private:
|
2015-06-22 23:35:46 -04:00
|
|
|
Enum::Type _enum;
|
|
|
|
std::string _str;
|
2015-06-23 22:24:55 -04:00
|
|
|
Func::Ptr _func;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2014-05-27 21:21:45 -04:00
|
|
|
public:
|
|
|
|
Policy()
|
|
|
|
: _enum(invalid),
|
|
|
|
_str(invalid),
|
|
|
|
_func(invalid)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2014-05-27 21:21:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:35:46 -04:00
|
|
|
Policy(const Enum::Type enum_,
|
|
|
|
const std::string &str_,
|
2015-06-23 22:24:55 -04:00
|
|
|
const Func::Ptr func_)
|
2014-05-27 21:21:45 -04:00
|
|
|
: _enum(enum_),
|
|
|
|
_str(str_),
|
|
|
|
_func(func_)
|
|
|
|
{
|
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2014-05-27 21:21:45 -04:00
|
|
|
public:
|
|
|
|
operator const Enum::Type() const { return _enum; }
|
2014-11-10 21:01:20 -05:00
|
|
|
operator const std::string&() const { return _str; }
|
2015-06-23 22:24:55 -04:00
|
|
|
operator const Func::Ptr() const { return _func; }
|
2014-05-27 21:21:45 -04:00
|
|
|
operator const Policy*() const { return this; }
|
|
|
|
|
|
|
|
bool operator==(const Enum::Type enum_) const
|
|
|
|
{ return _enum == enum_; }
|
2014-11-10 21:01:20 -05:00
|
|
|
bool operator==(const std::string &str_) const
|
2014-05-27 21:21:45 -04:00
|
|
|
{ return _str == str_; }
|
2015-06-23 22:24:55 -04:00
|
|
|
bool operator==(const Func::Ptr func_) const
|
2014-05-27 21:21:45 -04:00
|
|
|
{ return _func == func_; }
|
2014-11-10 21:01:20 -05:00
|
|
|
|
2014-05-27 21:21:45 -04:00
|
|
|
bool operator!=(const Policy &r) const
|
|
|
|
{ return _enum != r._enum; }
|
|
|
|
|
|
|
|
bool operator<(const Policy &r) const
|
|
|
|
{ return _enum < r._enum; }
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
public:
|
2014-11-10 21:01:20 -05:00
|
|
|
static const Policy &find(const std::string&);
|
2014-05-27 21:21:45 -04:00
|
|
|
static const Policy &find(const Enum::Type);
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
public:
|
2014-05-27 21:21:45 -04:00
|
|
|
static const std::vector<Policy> _policies_;
|
|
|
|
static const Policy * const policies;
|
2015-06-25 17:55:16 -04:00
|
|
|
|
|
|
|
static const Policy &invalid;
|
|
|
|
static const Policy &all;
|
|
|
|
static const Policy &epmfs;
|
|
|
|
static const Policy &ff;
|
|
|
|
static const Policy &ffwp;
|
|
|
|
static const Policy &fwfs;
|
|
|
|
static const Policy &lfs;
|
|
|
|
static const Policy &mfs;
|
|
|
|
static const Policy &newest;
|
|
|
|
static const Policy &rand;
|
2014-05-12 12:41:46 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|