2016-02-12 12:20:12 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
#include <limits>
|
2016-02-12 12:20:12 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
#include "fs.hpp"
|
2016-02-12 12:20:12 +08:00
|
|
|
#include "fs_path.hpp"
|
|
|
|
#include "policy.hpp"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using mergerfs::Category;
|
|
|
|
|
|
|
|
static
|
2016-03-01 11:58:13 +08:00
|
|
|
int
|
|
|
|
_eplfs_create(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-03-01 11:58:13 +08:00
|
|
|
vector<const string*> &paths)
|
2016-02-12 12:20:12 +08:00
|
|
|
{
|
2016-03-01 11:58:13 +08:00
|
|
|
string fullpath;
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t eplfs;
|
2016-03-01 11:58:13 +08:00
|
|
|
const string *eplfsbasepath;
|
2016-02-12 12:20:12 +08:00
|
|
|
|
2016-07-11 00:44:29 +08:00
|
|
|
eplfs = std::numeric_limits<uint64_t>::max();
|
2016-03-01 11:58:13 +08:00
|
|
|
eplfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
2016-02-12 12:20:12 +08:00
|
|
|
{
|
2016-03-01 11:58:13 +08:00
|
|
|
bool readonly;
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t spaceavail;
|
|
|
|
uint64_t _spaceused;
|
2016-03-01 11:58:13 +08:00
|
|
|
const string *basepath = &basepaths[i];
|
|
|
|
|
|
|
|
fs::path::make(basepath,fusepath,fullpath);
|
|
|
|
|
|
|
|
if(!fs::exists(fullpath))
|
|
|
|
continue;
|
2016-05-08 02:05:38 +08:00
|
|
|
if(!fs::info(*basepath,readonly,spaceavail,_spaceused))
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
|
|
|
if(readonly)
|
|
|
|
continue;
|
|
|
|
if(spaceavail < minfreespace)
|
|
|
|
continue;
|
|
|
|
if(spaceavail > eplfs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
eplfs = spaceavail;
|
|
|
|
eplfsbasepath = basepath;
|
2016-02-12 12:20:12 +08:00
|
|
|
}
|
2016-03-01 11:58:13 +08:00
|
|
|
|
|
|
|
if(eplfsbasepath == NULL)
|
|
|
|
return POLICY_FAIL_ENOENT;
|
|
|
|
|
|
|
|
paths.push_back(eplfsbasepath);
|
|
|
|
|
|
|
|
return POLICY_SUCCESS;
|
2016-02-12 12:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2016-03-01 11:58:13 +08:00
|
|
|
_eplfs_other(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
2016-02-12 12:20:12 +08:00
|
|
|
{
|
|
|
|
string fullpath;
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t eplfs;
|
2016-02-12 12:20:12 +08:00
|
|
|
const string *eplfsbasepath;
|
|
|
|
|
2016-07-11 00:44:29 +08:00
|
|
|
eplfs = std::numeric_limits<uint64_t>::max();
|
2016-02-12 12:20:12 +08:00
|
|
|
eplfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t spaceavail;
|
2016-02-12 12:20:12 +08:00
|
|
|
const string *basepath = &basepaths[i];
|
|
|
|
|
|
|
|
fs::path::make(basepath,fusepath,fullpath);
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
if(!fs::exists(fullpath))
|
|
|
|
continue;
|
|
|
|
if(!fs::spaceavail(*basepath,spaceavail))
|
|
|
|
continue;
|
|
|
|
if(spaceavail > eplfs)
|
2016-02-19 07:48:11 +08:00
|
|
|
continue;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
eplfs = spaceavail;
|
|
|
|
eplfsbasepath = basepath;
|
2016-02-12 12:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(eplfsbasepath == NULL)
|
2016-03-01 11:58:13 +08:00
|
|
|
return POLICY_FAIL_ENOENT;
|
2016-02-12 12:20:12 +08:00
|
|
|
|
|
|
|
paths.push_back(eplfsbasepath);
|
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
return POLICY_SUCCESS;
|
2016-02-12 12:20:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_eplfs(const Category::Enum::Type type,
|
|
|
|
const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-03-01 11:58:13 +08:00
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
if(type == Category::Enum::create)
|
|
|
|
return _eplfs_create(basepaths,fusepath,minfreespace,paths);
|
|
|
|
|
|
|
|
return _eplfs_other(basepaths,fusepath,paths);
|
|
|
|
}
|
|
|
|
|
2016-02-12 12:20:12 +08:00
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
int
|
|
|
|
Policy::Func::eplfs(const Category::Enum::Type type,
|
|
|
|
const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-02-19 07:48:11 +08:00
|
|
|
vector<const string*> &paths)
|
2016-02-12 12:20:12 +08:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
rv = _eplfs(type,basepaths,fusepath,minfreespace,paths);
|
2016-02-13 08:16:04 +08:00
|
|
|
if(POLICY_FAILED(rv))
|
2016-02-12 12:20:12 +08:00
|
|
|
rv = Policy::Func::lfs(type,basepaths,fusepath,minfreespace,paths);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|