2015-06-24 10:24:55 +08:00
|
|
|
/*
|
2016-01-15 05:50:22 +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.
|
2015-06-24 10:24:55 +08:00
|
|
|
*/
|
|
|
|
|
2016-09-14 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2016-02-19 07:48:11 +08:00
|
|
|
#include "fs.hpp"
|
2018-10-14 11:48:30 +08:00
|
|
|
#include "fs_exists.hpp"
|
|
|
|
#include "fs_info.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_path.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
#include "policy.hpp"
|
|
|
|
|
2018-10-14 11:48:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-06-24 10:24:55 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2016-03-01 11:58:13 +08:00
|
|
|
using mergerfs::Category;
|
2015-06-24 10:24:55 +08:00
|
|
|
|
2015-07-01 11:35:28 +08:00
|
|
|
static
|
2016-03-01 11:58:13 +08:00
|
|
|
int
|
|
|
|
_mfs_create(const vector<string> &basepaths,
|
|
|
|
vector<const string*> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2018-10-14 11:48:30 +08:00
|
|
|
int rv;
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t mfs;
|
2018-10-14 11:48:30 +08:00
|
|
|
fs::info_t info;
|
|
|
|
const string *basepath;
|
2016-03-01 11:58:13 +08:00
|
|
|
const string *mfsbasepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
mfs = 0;
|
|
|
|
mfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2018-10-14 11:48:30 +08:00
|
|
|
basepath = &basepaths[i];
|
2016-03-01 11:58:13 +08:00
|
|
|
|
2018-10-14 11:48:30 +08:00
|
|
|
rv = fs::info(basepath,&info);
|
|
|
|
if(rv == -1)
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
2018-10-14 11:48:30 +08:00
|
|
|
if(info.readonly)
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
2018-10-14 11:48:30 +08:00
|
|
|
if(info.spaceavail < mfs)
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
|
|
|
|
2018-10-14 11:48:30 +08:00
|
|
|
mfs = info.spaceavail;
|
2016-02-13 08:16:04 +08:00
|
|
|
mfsbasepath = basepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
2016-03-01 11:58:13 +08:00
|
|
|
|
|
|
|
if(mfsbasepath == NULL)
|
2018-10-12 23:04:33 +08:00
|
|
|
return (errno=ENOENT,-1);
|
2016-03-01 11:58:13 +08:00
|
|
|
|
|
|
|
paths.push_back(mfsbasepath);
|
|
|
|
|
2018-10-12 23:04:33 +08:00
|
|
|
return 0;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2016-03-01 11:58:13 +08:00
|
|
|
_mfs_other(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2018-10-14 11:48:30 +08:00
|
|
|
int rv;
|
2016-07-11 00:44:29 +08:00
|
|
|
uint64_t mfs;
|
2018-10-14 11:48:30 +08:00
|
|
|
uint64_t spaceavail;
|
|
|
|
const string *basepath;
|
2016-02-13 08:16:04 +08:00
|
|
|
const string *mfsbasepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
|
|
|
mfs = 0;
|
2016-02-13 08:16:04 +08:00
|
|
|
mfsbasepath = NULL;
|
2015-07-01 11:35:28 +08:00
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
2018-10-14 11:48:30 +08:00
|
|
|
basepath = &basepaths[i];
|
2015-07-22 02:19:17 +08:00
|
|
|
|
2018-10-14 11:48:30 +08:00
|
|
|
if(!fs::exists(*basepath,fusepath))
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
2018-10-14 11:48:30 +08:00
|
|
|
rv = fs::spaceavail(basepath,&spaceavail);
|
|
|
|
if(rv == -1)
|
2016-03-01 11:58:13 +08:00
|
|
|
continue;
|
|
|
|
if(spaceavail < mfs)
|
2016-02-19 07:48:11 +08:00
|
|
|
continue;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
mfs = spaceavail;
|
|
|
|
mfsbasepath = basepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
if(mfsbasepath == NULL)
|
2018-10-12 23:04:33 +08:00
|
|
|
return (errno=ENOENT,-1);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
paths.push_back(mfsbasepath);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-12 23:04:33 +08:00
|
|
|
return 0;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_mfs(const Category::Enum::Type type,
|
|
|
|
const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
if(type == Category::Enum::create)
|
|
|
|
return _mfs_create(basepaths,paths);
|
|
|
|
|
|
|
|
return _mfs_other(basepaths,fusepath,paths);
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:24:55 +08:00
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
int
|
2015-06-26 05:55:16 +08:00
|
|
|
Policy::Func::mfs(const Category::Enum::Type type,
|
|
|
|
const vector<string> &basepaths,
|
2016-01-30 05:54:39 +08:00
|
|
|
const char *fusepath,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-01-30 05:54:39 +08:00
|
|
|
vector<const string*> &paths)
|
2015-06-24 10:24:55 +08:00
|
|
|
{
|
2016-02-19 07:48:11 +08:00
|
|
|
int rv;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
rv = _mfs(type,basepaths,fusepath,paths);
|
2018-10-12 23:04:33 +08:00
|
|
|
if(rv == -1)
|
2016-02-19 07:48:11 +08:00
|
|
|
rv = Policy::Func::ff(type,basepaths,fusepath,minfreespace,paths);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
return rv;
|
2015-06-24 10:24:55 +08:00
|
|
|
}
|
|
|
|
}
|