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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_path.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
#include "policy.hpp"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::size_t;
|
|
|
|
|
2015-07-01 11:35:28 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_mfs_create(const vector<string> &basepaths,
|
|
|
|
const string &fusepath,
|
2015-07-01 19:47:40 +08:00
|
|
|
vector<string> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2015-07-22 02:19:17 +08:00
|
|
|
fsblkcnt_t mfs;
|
|
|
|
string mfsstr;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
|
|
|
mfs = 0;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct statvfs fsstats;
|
2015-07-22 02:19:17 +08:00
|
|
|
const string &basepath = basepaths[i];
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2015-07-22 02:19:17 +08:00
|
|
|
rv = ::statvfs(basepath.c_str(),&fsstats);
|
2015-07-01 11:35:28 +08:00
|
|
|
if(rv == 0)
|
|
|
|
{
|
|
|
|
fsblkcnt_t spaceavail;
|
|
|
|
|
|
|
|
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
|
|
|
if(spaceavail > mfs)
|
|
|
|
{
|
|
|
|
mfs = spaceavail;
|
|
|
|
mfsstr = basepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:19:17 +08:00
|
|
|
if(mfsstr.empty())
|
2015-07-01 11:35:28 +08:00
|
|
|
return (errno=ENOENT,-1);
|
|
|
|
|
2015-07-01 19:47:40 +08:00
|
|
|
paths.push_back(mfsstr);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
_mfs(const vector<string> &basepaths,
|
2015-07-01 19:47:40 +08:00
|
|
|
const string &fusepath,
|
|
|
|
vector<string> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2015-07-22 02:19:17 +08:00
|
|
|
fsblkcnt_t mfs;
|
|
|
|
string mfsstr;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
|
|
|
mfs = 0;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
string fullpath;
|
|
|
|
struct statvfs fsstats;
|
2015-07-22 02:19:17 +08:00
|
|
|
const string &basepath = basepaths[i];
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
fs::path::make(basepath,fusepath,fullpath);
|
2015-07-22 02:19:17 +08:00
|
|
|
|
2015-07-01 11:35:28 +08:00
|
|
|
rv = ::statvfs(fullpath.c_str(),&fsstats);
|
|
|
|
if(rv == 0)
|
|
|
|
{
|
|
|
|
fsblkcnt_t spaceavail;
|
|
|
|
|
|
|
|
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
|
|
|
if(spaceavail > mfs)
|
|
|
|
{
|
|
|
|
mfs = spaceavail;
|
|
|
|
mfsstr = basepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:19:17 +08:00
|
|
|
if(mfsstr.empty())
|
2015-07-01 11:35:28 +08:00
|
|
|
return (errno=ENOENT,-1);
|
|
|
|
|
2015-07-01 19:47:40 +08:00
|
|
|
paths.push_back(mfsstr);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const string &fusepath,
|
|
|
|
const size_t minfreespace,
|
2015-07-01 19:47:40 +08:00
|
|
|
vector<string> &paths)
|
2015-06-24 10:24:55 +08:00
|
|
|
{
|
2015-07-01 11:35:28 +08:00
|
|
|
if(type == Category::Enum::create)
|
|
|
|
return _mfs_create(basepaths,fusepath,paths);
|
|
|
|
|
|
|
|
return _mfs(basepaths,fusepath,paths);
|
2015-06-24 10:24:55 +08:00
|
|
|
}
|
|
|
|
}
|