clearly separate usage of statvfs from stat for file existance

Used statvfs to also check for existance vs lstat. On dead symlinks
this resulted in ENOENT making certain functions fail.

closes #239
This commit is contained in:
Antonio SJ Musumeci 2016-02-29 22:58:13 -05:00
parent 4a63c000ca
commit 4ecf3c5797
11 changed files with 389 additions and 285 deletions

View file

@ -15,7 +15,6 @@
*/
#include <errno.h>
#include <sys/statvfs.h>
#include <string>
#include <vector>
@ -23,63 +22,99 @@
#include "fs.hpp"
#include "fs_path.hpp"
#include "policy.hpp"
#include "statvfs_util.hpp"
using std::string;
using std::vector;
using std::size_t;
static
void
_calc_mfs(const struct statvfs &st,
const string *basepath,
fsblkcnt_t &mfs,
const string *&mfsbasepath)
{
fsblkcnt_t spaceavail;
spaceavail = StatVFS::spaceavail(st);
if(spaceavail > mfs)
{
mfs = spaceavail;
mfsbasepath = basepath;
}
}
using mergerfs::Category;
static
int
_mfs(const vector<string> &basepaths,
const char *fusepath,
const bool needswritablefs,
vector<const string*> &paths)
_mfs_create(const vector<string> &basepaths,
vector<const string*> &paths)
{
string fullpath;
struct statvfs st;
fsblkcnt_t mfs;
size_t mfs;
const string *mfsbasepath;
mfs = 0;
mfsbasepath = NULL;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
bool readonly;
size_t spaceavail;
const string *basepath = &basepaths[i];
fs::path::make(basepath,fusepath,fullpath);
if(!fs::available(fullpath,needswritablefs,st))
if(!fs::info(*basepath,readonly,spaceavail))
continue;
if(readonly)
continue;
if(spaceavail < mfs)
continue;
_calc_mfs(st,basepath,mfs,mfsbasepath);
mfs = spaceavail;
mfsbasepath = basepath;
}
if(mfsbasepath == NULL)
return (errno=ENOENT,POLICY_FAIL);
return POLICY_FAIL_ENOENT;
paths.push_back(mfsbasepath);
return POLICY_SUCCESS;
}
static
int
_mfs_other(const vector<string> &basepaths,
const char *fusepath,
vector<const string*> &paths)
{
string fullpath;
size_t mfs;
const string *mfsbasepath;
mfs = 0;
mfsbasepath = NULL;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
size_t spaceavail;
const string *basepath = &basepaths[i];
fs::path::make(basepath,fusepath,fullpath);
if(!fs::exists(fullpath))
continue;
if(!fs::spaceavail(*basepath,spaceavail))
continue;
if(spaceavail < mfs)
continue;
mfs = spaceavail;
mfsbasepath = basepath;
}
if(mfsbasepath == NULL)
return POLICY_FAIL_ENOENT;
paths.push_back(mfsbasepath);
return POLICY_SUCCESS;
}
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);
}
namespace mergerfs
{
int
@ -90,12 +125,8 @@ namespace mergerfs
vector<const string*> &paths)
{
int rv;
const char *fp =
((type == Category::Enum::create) ? "" : fusepath);
const bool needswritablefs =
(type == Category::Enum::create);
rv = _mfs(basepaths,fp,needswritablefs,paths);
rv = _mfs(type,basepaths,fusepath,paths);
if(POLICY_FAILED(rv))
rv = Policy::Func::ff(type,basepaths,fusepath,minfreespace,paths);