mergerfs/src/fs.cpp

277 lines
5.5 KiB
C++
Raw Normal View History

2014-05-12 12:41:46 -04:00
/*
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
2014-05-12 12:41:46 -04:00
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.
2014-05-12 12:41:46 -04:00
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.
2014-05-12 12:41:46 -04:00
*/
#include <string>
#include <vector>
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <stdlib.h>
2014-05-12 12:41:46 -04:00
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <unistd.h>
#include "fs_attr.hpp"
#include "fs_path.hpp"
#include "fs_xattr.hpp"
#include "statvfs_util.hpp"
#include "str.hpp"
#include "success_fail.hpp"
2014-05-12 12:41:46 -04:00
using std::string;
using std::vector;
namespace fs
{
bool
exists(const string &path,
struct stat &st)
{
int rv;
rv = ::lstat(path.c_str(),&st);
return LSTAT_SUCCEEDED(rv);
}
bool
exists(const string &path,
struct statvfs &st)
{
int rv;
rv = ::statvfs(path.c_str(),&st);
return STATVFS_SUCCEEDED(rv);
}
bool
exists(const string &path)
{
struct stat st;
return exists(path,st);
}
bool
exists(const string &path,
bool &readonly,
size_t &spaceavail)
{
bool rv;
struct statvfs st;
rv = exists(path,st);
if(rv)
{
readonly = StatVFS::readonly(st);
spaceavail = StatVFS::spaceavail(st);
}
return rv;
}
bool
exists_on_rw_fs(const string &path,
struct statvfs &st)
{
int rv;
rv = ::statvfs(path.c_str(),&st);
return (STATVFS_SUCCEEDED(rv) && !StatVFS::readonly(st));
}
bool
exists_on_rw_fs(const string &path)
{
struct statvfs st;
return exists_on_rw_fs(path,st);
}
bool
exists_on_rw_fs_with_at_least(const string &path,
const size_t minfreespace)
{
struct statvfs st;
return (exists_on_rw_fs(path,st)
&&
StatVFS::spaceavail(st) >= minfreespace);
}
void
findallfiles(const vector<string> &srcmounts,
const char *fusepath,
vector<string> &paths)
{
string fullpath;
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
{
fs::path::make(&srcmounts[i],fusepath,fullpath);
if(!fs::exists(fullpath))
continue;
paths.push_back(fullpath);
}
}
2014-05-12 12:41:46 -04:00
int
findonfs(const vector<string> &srcmounts,
const char *fusepath,
const int fd,
string &basepath)
2014-05-12 12:41:46 -04:00
{
int rv;
dev_t dev;
string fullpath;
struct stat st;
2014-05-12 12:41:46 -04:00
rv = ::fstat(fd,&st);
if(FSTAT_FAILED(rv))
2014-05-12 12:41:46 -04:00
return -1;
dev = st.st_dev;
for(int i = 0, ei = srcmounts.size(); i != ei; i++)
{
fs::path::make(&srcmounts[i],fusepath,fullpath);
rv = ::lstat(fullpath.c_str(),&st);
if(FSTAT_FAILED(rv))
continue;
if(st.st_dev != dev)
continue;
basepath = srcmounts[i];
return 0;
}
2014-05-12 12:41:46 -04:00
return (errno=ENOENT,-1);
2014-05-12 12:41:46 -04:00
}
2014-05-27 21:21:45 -04:00
void
glob(const vector<string> &patterns,
vector<string> &strs)
{
int flags;
size_t veclen;
glob_t gbuf = {0};
veclen = patterns.size();
if(veclen == 0)
return;
flags = 0;
glob(patterns[0].c_str(),flags,NULL,&gbuf);
flags = GLOB_APPEND;
for(size_t i = 1; i < veclen; i++)
glob(patterns[i].c_str(),flags,NULL,&gbuf);
for(size_t i = 0; i < gbuf.gl_pathc; ++i)
strs.push_back(gbuf.gl_pathv[i]);
globfree(&gbuf);
}
void
realpathize(vector<string> &strs)
{
char *rv;
char buf[PATH_MAX];
for(size_t i = 0; i < strs.size(); i++)
{
rv = ::realpath(strs[i].c_str(),buf);
if(rv == NULL)
continue;
strs[i] = buf;
}
}
int
getfl(const int fd)
{
return ::fcntl(fd,F_GETFL,0);
}
int
mfs(const vector<string> &basepaths,
const size_t minfreespace,
string &path)
{
fsblkcnt_t mfs;
fsblkcnt_t spaceavail;
const string *mfsbasepath;
mfs = 0;
mfsbasepath = NULL;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
struct statvfs st;
const string &basepath = basepaths[i];
if(!fs::exists(basepath,st))
continue;
spaceavail = StatVFS::spaceavail(st);
if(spaceavail < minfreespace)
continue;
if(spaceavail <= mfs)
continue;
mfs = spaceavail;
mfsbasepath = &basepaths[i];
}
if(mfsbasepath == NULL)
return (errno=ENOENT,-1);
path = *mfsbasepath;
return 0;
}
bool
available(const string &path,
const bool needswritablefs,
struct statvfs &st)
{
return (needswritablefs ?
fs::exists_on_rw_fs(path,st) :
fs::exists(path,st));
}
bool
available(const string &path,
const bool needswritablefs)
{
return (needswritablefs ?
fs::exists_on_rw_fs(path) :
fs::exists(path));
}
2014-05-12 12:41:46 -04:00
};