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 <errno.h>
|
2016-02-19 07:48:11 +08:00
|
|
|
#include <sys/statvfs.h>
|
2015-06-24 10:24:55 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
#include "fs.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_path.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
#include "policy.hpp"
|
2016-02-19 07:48:11 +08:00
|
|
|
#include "statvfs_util.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::size_t;
|
|
|
|
|
2015-07-01 11:35:28 +08:00
|
|
|
static
|
2016-02-13 08:16:04 +08:00
|
|
|
void
|
2016-02-19 07:48:11 +08:00
|
|
|
_calc_mfs(const struct statvfs &st,
|
2016-02-13 08:16:04 +08:00
|
|
|
const string *basepath,
|
|
|
|
fsblkcnt_t &mfs,
|
|
|
|
const string *&mfsbasepath)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2016-02-13 08:16:04 +08:00
|
|
|
fsblkcnt_t spaceavail;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
spaceavail = StatVFS::spaceavail(st);
|
2016-02-13 08:16:04 +08:00
|
|
|
if(spaceavail > mfs)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2016-02-13 08:16:04 +08:00
|
|
|
mfs = spaceavail;
|
|
|
|
mfsbasepath = basepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2016-01-30 05:54:39 +08:00
|
|
|
_mfs(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
2016-02-19 07:48:11 +08:00
|
|
|
const bool needswritablefs,
|
2016-01-30 05:54:39 +08:00
|
|
|
vector<const string*> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2016-01-30 05:54:39 +08:00
|
|
|
string fullpath;
|
2016-02-19 07:48:11 +08:00
|
|
|
struct statvfs st;
|
2015-07-22 02:19:17 +08:00
|
|
|
fsblkcnt_t mfs;
|
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++)
|
|
|
|
{
|
2016-01-30 05:54:39 +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
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
if(!fs::available(fullpath,needswritablefs,st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
_calc_mfs(st,basepath,mfs,mfsbasepath);
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
if(mfsbasepath == NULL)
|
|
|
|
return (errno=ENOENT,POLICY_FAIL);
|
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
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
return POLICY_SUCCESS;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2015-06-26 05:55:16 +08:00
|
|
|
const size_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-02-13 08:16:04 +08:00
|
|
|
const char *fp =
|
|
|
|
((type == Category::Enum::create) ? "" : fusepath);
|
2016-02-19 07:48:11 +08:00
|
|
|
const bool needswritablefs =
|
|
|
|
(type == Category::Enum::create);
|
|
|
|
|
|
|
|
rv = _mfs(basepaths,fp,needswritablefs,paths);
|
|
|
|
if(POLICY_FAILED(rv))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|