mirror of
https://github.com/trapexit/mergerfs.git
synced 2024-04-21 12:31:43 +00:00
separate policies into individual modules
This commit is contained in:
-338
@@ -28,7 +28,6 @@
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
@@ -514,341 +513,4 @@ namespace fs
|
||||
|
||||
globfree(&gbuf);
|
||||
}
|
||||
|
||||
namespace find
|
||||
{
|
||||
int
|
||||
invalid(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &rv)
|
||||
{
|
||||
return (errno = EINVAL,-1);
|
||||
}
|
||||
|
||||
int
|
||||
ff(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
errno = ENOENT;
|
||||
for(vector<string>::const_iterator
|
||||
iter = basepaths.begin(), eiter = basepaths.end();
|
||||
iter != eiter;
|
||||
++iter)
|
||||
{
|
||||
int rv;
|
||||
struct stat st;
|
||||
string fullpath;
|
||||
|
||||
fullpath = fs::make_path(*iter,fusepath);
|
||||
|
||||
rv = ::lstat(fullpath.c_str(),&st);
|
||||
if(rv == 0)
|
||||
{
|
||||
paths.push_back(Path(*iter,fullpath));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
ffwp(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
Path fallback;
|
||||
|
||||
errno = ENOENT;
|
||||
for(vector<string>::const_iterator
|
||||
iter = basepaths.begin(), eiter = basepaths.end();
|
||||
iter != eiter;
|
||||
++iter)
|
||||
{
|
||||
int rv;
|
||||
struct stat st;
|
||||
string fullpath;
|
||||
|
||||
fullpath = fs::make_path(*iter,fusepath);
|
||||
|
||||
rv = ::lstat(fullpath.c_str(),&st);
|
||||
if(rv == 0)
|
||||
{
|
||||
paths.push_back(Path(*iter,fullpath));
|
||||
return 0;
|
||||
}
|
||||
else if(errno == EACCES)
|
||||
{
|
||||
fallback.base = *iter;
|
||||
fallback.full = fullpath;
|
||||
}
|
||||
}
|
||||
|
||||
if(!fallback.base.empty())
|
||||
return (paths.push_back(fallback),0);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
fwfs(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
for(size_t i = 0, size = basepaths.size(); i != size; i++)
|
||||
{
|
||||
int rv;
|
||||
const char *basepath;
|
||||
struct statvfs fsstats;
|
||||
|
||||
basepath = basepaths[i].c_str();
|
||||
rv = ::statvfs(basepath,&fsstats);
|
||||
if(rv == 0)
|
||||
{
|
||||
fsblkcnt_t spaceavail;
|
||||
|
||||
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
||||
if(spaceavail > minfreespace)
|
||||
{
|
||||
paths.push_back(Path(basepath,
|
||||
fs::make_path(basepath,fusepath)));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mfs(basepaths,fusepath,minfreespace,paths);
|
||||
}
|
||||
|
||||
int
|
||||
newest(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
time_t newest;
|
||||
string npath;
|
||||
vector<string>::const_iterator niter;
|
||||
|
||||
newest = 0;
|
||||
errno = ENOENT;
|
||||
for(vector<string>::const_iterator
|
||||
iter = basepaths.begin(), eiter = basepaths.end();
|
||||
iter != eiter;
|
||||
++iter)
|
||||
{
|
||||
int rv;
|
||||
struct stat st;
|
||||
string fullpath;
|
||||
|
||||
fullpath = fs::make_path(*iter,fusepath);
|
||||
|
||||
rv = ::lstat(fullpath.c_str(),&st);
|
||||
if(rv == 0 && st.st_mtime > newest)
|
||||
{
|
||||
newest = st.st_mtime;
|
||||
niter = iter;
|
||||
npath = fullpath;
|
||||
}
|
||||
}
|
||||
|
||||
if(newest)
|
||||
return (paths.push_back(Path(*niter,npath)),0);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lfs(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
fsblkcnt_t lfs;
|
||||
const char *lfsstr;
|
||||
|
||||
lfs = -1;
|
||||
lfsstr = NULL;
|
||||
for(size_t i = 0, size = basepaths.size(); i != size; i++)
|
||||
{
|
||||
int rv;
|
||||
const char *basepath;
|
||||
struct statvfs fsstats;
|
||||
|
||||
basepath = basepaths[i].c_str();
|
||||
rv = ::statvfs(basepath,&fsstats);
|
||||
if(rv == 0)
|
||||
{
|
||||
fsblkcnt_t spaceavail;
|
||||
|
||||
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
||||
if((spaceavail > minfreespace) &&
|
||||
(spaceavail < lfs))
|
||||
{
|
||||
lfs = spaceavail;
|
||||
lfsstr = basepath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(lfsstr == NULL)
|
||||
return mfs(basepaths,fusepath,minfreespace,paths);
|
||||
|
||||
paths.push_back(Path(lfsstr,
|
||||
fs::make_path(lfsstr,fusepath)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mfs(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
fsblkcnt_t mfs;
|
||||
size_t mfsidx;
|
||||
|
||||
mfs = 0;
|
||||
for(size_t i = 0, size = basepaths.size();
|
||||
i != size;
|
||||
i++)
|
||||
{
|
||||
int rv;
|
||||
struct statvfs fsstats;
|
||||
|
||||
rv = ::statvfs(basepaths[i].c_str(),&fsstats);
|
||||
if(rv == 0)
|
||||
{
|
||||
fsblkcnt_t spaceavail;
|
||||
|
||||
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
||||
if(spaceavail > mfs)
|
||||
{
|
||||
mfs = spaceavail;
|
||||
mfsidx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(mfs == 0)
|
||||
return (errno=ENOENT,-1);
|
||||
|
||||
paths.push_back(Path(basepaths[mfsidx],
|
||||
fs::make_path(basepaths[mfsidx],fusepath)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
epmfs(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
fsblkcnt_t existingmfs;
|
||||
fsblkcnt_t generalmfs;
|
||||
string fullpath;
|
||||
string generalmfspath;
|
||||
string existingmfspath;
|
||||
vector<string>::const_iterator iter = basepaths.begin();
|
||||
vector<string>::const_iterator eiter = basepaths.end();
|
||||
|
||||
if(iter == eiter)
|
||||
return (errno = ENOENT,-1);
|
||||
|
||||
existingmfs = 0;
|
||||
generalmfs = 0;
|
||||
do
|
||||
{
|
||||
int rv;
|
||||
struct statvfs fsstats;
|
||||
const string &mountpoint = *iter;
|
||||
|
||||
rv = ::statvfs(mountpoint.c_str(),&fsstats);
|
||||
if(rv == 0)
|
||||
{
|
||||
fsblkcnt_t spaceavail;
|
||||
struct stat st;
|
||||
|
||||
spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
|
||||
if(spaceavail > generalmfs)
|
||||
{
|
||||
generalmfs = spaceavail;
|
||||
generalmfspath = mountpoint;
|
||||
}
|
||||
|
||||
fullpath = fs::make_path(mountpoint,fusepath);
|
||||
rv = ::lstat(fullpath.c_str(),&st);
|
||||
if(rv == 0)
|
||||
{
|
||||
if(spaceavail > existingmfs)
|
||||
{
|
||||
existingmfs = spaceavail;
|
||||
existingmfspath = mountpoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
++iter;
|
||||
}
|
||||
while(iter != eiter);
|
||||
|
||||
if(existingmfspath.empty())
|
||||
existingmfspath = generalmfspath;
|
||||
|
||||
paths.push_back(Path(existingmfspath,
|
||||
fullpath));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
all(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
int rv;
|
||||
struct stat st;
|
||||
string fullpath;
|
||||
|
||||
for(vector<string>::const_iterator
|
||||
iter = basepaths.begin(), eiter = basepaths.end();
|
||||
iter != eiter;
|
||||
++iter)
|
||||
{
|
||||
fullpath = fs::make_path(*iter,fusepath);
|
||||
|
||||
rv = ::lstat(fullpath.c_str(),&st);
|
||||
if(rv == 0)
|
||||
paths.push_back(Path(*iter,fullpath));
|
||||
}
|
||||
|
||||
return paths.empty() ? (errno=ENOENT,-1) : 0;
|
||||
}
|
||||
|
||||
int
|
||||
rand(const vector<string> &basepaths,
|
||||
const string &fusepath,
|
||||
const size_t minfreespace,
|
||||
Paths &paths)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = all(basepaths,fusepath,minfreespace,paths);
|
||||
if(rv == -1)
|
||||
return -1;
|
||||
|
||||
std::random_shuffle(paths.begin(),paths.end());
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user