Skip to content

Commit

Permalink
create different policies based on category of use
Browse files Browse the repository at this point in the history
  • Loading branch information
trapexit committed Jul 3, 2015
1 parent 51b6d3f commit 0c60701
Show file tree
Hide file tree
Showing 23 changed files with 593 additions and 294 deletions.
5 changes: 2 additions & 3 deletions src/chmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ _chmod(Policy::Func::Action actionFunc,
return -errno;

error = 0;
for(Paths::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
for(size_t i = 0, ei = paths.size(); i != ei; i++)
{
rv = ::chmod(i->full.c_str(),mode);
rv = ::chmod(paths[i].full.c_str(),mode);
if(rv == -1)
error = errno;
}
Expand Down
5 changes: 2 additions & 3 deletions src/chown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ _chown(Policy::Func::Action actionFunc,
return -errno;

error = 0;
for(Paths::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
for(size_t i = 0, ei = paths.size(); i != ei; i++)
{
rv = ::lchown(i->full.c_str(),uid,gid);
rv = ::lchown(paths[i].full.c_str(),uid,gid);
if(rv == -1)
error = errno;
}
Expand Down
5 changes: 2 additions & 3 deletions src/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ _link(Policy::Func::Search searchFunc,
return -errno;

error = 0;
for(Paths::const_iterator
i = oldpaths.begin(), ei = oldpaths.end(); i != ei; ++i)
for(size_t i = 0, ei = oldpaths.size(); i != ei; i++)
{
rv = _single_link(searchFunc,srcmounts,minfreespace,i->base,oldpath,newpath);
rv = _single_link(searchFunc,srcmounts,minfreespace,oldpaths[i].base,oldpath,newpath);
if(rv == -1)
error = errno;
}
Expand Down
11 changes: 6 additions & 5 deletions src/mkdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ _mkdir(Policy::Func::Search searchFunc,
return -errno;

error = 0;
for(Paths::const_iterator
i = createpaths.begin(), ei = createpaths.end(); i != ei; ++i)
for(size_t i = 0, ei = createpaths.size(); i != ei; i++)
{
if(i->base != existingpath[0].base)
const string &createpath = createpaths[i].base;

if(createpath != existingpath[0].base)
{
const mergerfs::ugid::SetResetGuard ugid(0,0);
fs::clonepath(existingpath[0].base,i->base,dirname);
fs::clonepath(existingpath[0].base,createpath,dirname);
}

fullpath = fs::make_path(i->base,fusepath);
fullpath = fs::make_path(createpath,fusepath);

rv = ::mkdir(fullpath.c_str(),mode);
if(rv == -1)
Expand Down
11 changes: 6 additions & 5 deletions src/mknod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ _mknod(Policy::Func::Search searchFunc,
return -errno;

error = 0;
for(Paths::const_iterator
i = createpaths.begin(), ei = createpaths.end(); i != ei; ++i)
for(size_t i = 0, ei = createpaths.size(); i != ei; i++)
{
if(i->base != existingpath[0].base)
const string &createpath = createpaths[0].base;

if(createpath != existingpath[0].base)
{
const mergerfs::ugid::SetResetGuard ugid(0,0);
fs::clonepath(existingpath[0].base,i->base,dirname);
fs::clonepath(existingpath[0].base,createpath,dirname);
}

fullpath = fs::make_path(i->base,fusepath);
fullpath = fs::make_path(createpath,fusepath);

rv = ::mknod(fullpath.c_str(),mode,dev);
if(rv == -1)
Expand Down
43 changes: 26 additions & 17 deletions src/policy_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ using std::string;
using std::vector;
using std::size_t;

static
int
_all(const vector<string> &basepaths,
const string &fusepath,
Paths &paths)
{
int rv;
struct stat st;
string fullpath;

for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
const char *basepath;

basepath = basepaths[i].c_str();
fullpath = fs::make_path(basepath,fusepath);

rv = ::lstat(fullpath.c_str(),&st);
if(rv == 0)
paths.push_back(Path(basepath,fullpath));
}

return paths.empty() ? (errno=ENOENT,-1) : 0;
}

namespace mergerfs
{
int
Expand All @@ -45,22 +70,6 @@ namespace mergerfs
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;
return _all(basepaths,fusepath,paths);
}
}
215 changes: 160 additions & 55 deletions src/policy_epmfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,162 @@ using std::string;
using std::vector;
using std::size_t;

static
inline
void
_calc_epmfs(const struct statvfs &fsstats,
const char *basepath,
fsblkcnt_t &epmfs,
const char *&epmfsbasepath,
fsblkcnt_t &mfs,
const char *&mfsbasepath)
{
fsblkcnt_t spaceavail;

spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
if(spaceavail > epmfs)
{
epmfs = spaceavail;
epmfsbasepath = basepath;
}

if(spaceavail > mfs)
{
mfs = spaceavail;
mfsbasepath = basepath;
}
}

static
inline
void
_calc_mfs(const struct statvfs &fsstats,
const char *basepath,
fsblkcnt_t &mfs,
const char *&mfsbasepath)
{
fsblkcnt_t spaceavail;

spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
if(spaceavail > mfs)
{
mfs = spaceavail;
mfsbasepath = basepath;
}
}

static
inline
int
_try_statvfs(const char *basepath,
const string &fullpath,
fsblkcnt_t &epmfs,
const char *&epmfsbasepath,
fsblkcnt_t &mfs,
const char *&mfsbasepath)
{
int rv;
struct statvfs fsstats;

rv = ::statvfs(fullpath.c_str(),&fsstats);
if(rv == 0)
_calc_epmfs(fsstats,basepath,epmfs,epmfsbasepath,mfs,mfsbasepath);

return rv;
}

static
inline
int
_try_statvfs(const char *basepath,
fsblkcnt_t &mfs,
const char *&mfsbasepath)
{
int rv;
struct statvfs fsstats;

rv = ::statvfs(basepath,&fsstats);
if(rv == 0)
_calc_mfs(fsstats,basepath,mfs,mfsbasepath);

return rv;
}

static
int
_epmfs_create(const vector<string> &basepaths,
const string &fusepath,
Paths &paths)

{
fsblkcnt_t epmfs;
fsblkcnt_t mfs;
const char *basepath;
const char *mfsbasepath;
const char *epmfsbasepath;
string fullpath;

mfs = 0;
epmfs = 0;
mfsbasepath = NULL;
epmfsbasepath = NULL;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
int rv;

basepath = basepaths[i].c_str();
fullpath = fs::make_path(basepath,fusepath);

rv = _try_statvfs(basepath,fusepath,epmfs,epmfsbasepath,mfs,mfsbasepath);
if(rv == -1)
_try_statvfs(basepath,mfs,mfsbasepath);
}

if(epmfsbasepath == NULL)
epmfsbasepath = mfsbasepath;

paths.push_back(Path(epmfsbasepath,
fs::make_path(epmfsbasepath,fusepath)));

return 0;
}

static
int
_epmfs(const vector<string> &basepaths,
const string &fusepath,
Paths &paths)

{
fsblkcnt_t epmfs;
const char *basepath;
const char *epmfsbasepath;
string fullpath;

epmfs = 0;
epmfsbasepath = NULL;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
int rv;
struct statvfs fsstats;

basepath = basepaths[i].c_str();
fullpath = fs::make_path(basepath,fusepath);

rv = ::statvfs(fullpath.c_str(),&fsstats);
if(rv == 0)
_calc_mfs(fsstats,basepath,epmfs,epmfsbasepath);
}

if(epmfsbasepath == NULL)
return (errno=ENOENT,-1);

paths.push_back(Path(epmfsbasepath,
fs::make_path(epmfsbasepath,fusepath)));

return 0;
}

namespace mergerfs
{
int
Expand All @@ -46,60 +202,9 @@ namespace mergerfs
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;
if(type == Category::Enum::create)
return _epmfs_create(basepaths,fusepath,paths);

return _epmfs(basepaths,fusepath,paths);
}
}
Loading

0 comments on commit 0c60701

Please sign in to comment.