mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-10-26 19:01:56 -04:00
initial code commit
This commit is contained in:
parent
2b78be18f4
commit
bb7a966296
79 changed files with 6293 additions and 18 deletions
160
src/statfs.cpp
Normal file
160
src/statfs.cpp
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <fuse.h>
|
||||
|
||||
#include <sys/statvfs.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "ugid.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::map;
|
||||
using std::pair;
|
||||
using mergerfs::Policy;
|
||||
|
||||
static
|
||||
void
|
||||
_normalize_statvfs(struct statvfs *fsstat,
|
||||
const unsigned long min_bsize,
|
||||
const unsigned long min_frsize,
|
||||
const unsigned long min_namemax)
|
||||
{
|
||||
if(fsstat->f_bsize > min_bsize)
|
||||
{
|
||||
fsstat->f_bfree *= fsstat->f_bsize / min_bsize;
|
||||
fsstat->f_bavail *= fsstat->f_bsize / min_bsize;
|
||||
fsstat->f_bsize = min_bsize;
|
||||
}
|
||||
|
||||
if(fsstat->f_frsize > min_frsize)
|
||||
{
|
||||
fsstat->f_blocks *= fsstat->f_frsize / min_frsize;
|
||||
fsstat->f_frsize = min_frsize;
|
||||
}
|
||||
|
||||
if(fsstat->f_namemax > min_namemax)
|
||||
fsstat->f_namemax = min_namemax;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
_merge_statvfs(struct statvfs * const out,
|
||||
const struct statvfs * const in,
|
||||
const Policy::StatFS policy)
|
||||
{
|
||||
switch(policy)
|
||||
{
|
||||
case Policy::StatFS::SumUsedMaxFree:
|
||||
if(out->f_bfree < in->f_bfree)
|
||||
{
|
||||
out->f_bfree = in->f_bfree;
|
||||
out->f_bavail = in->f_bavail;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
case Policy::StatFS::SumUsedSumFree:
|
||||
out->f_bfree += in->f_bfree;
|
||||
out->f_bavail += in->f_bavail;
|
||||
break;
|
||||
}
|
||||
|
||||
out->f_ffree += in->f_ffree;
|
||||
out->f_favail += in->f_favail;
|
||||
out->f_files += in->f_files;
|
||||
out->f_blocks += in->f_blocks;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_statfs(const Policy::StatFS policy,
|
||||
const vector<string> &srcmounts,
|
||||
struct statvfs &fsstat)
|
||||
{
|
||||
unsigned long min_bsize = ULONG_MAX;
|
||||
unsigned long min_frsize = ULONG_MAX;
|
||||
unsigned long min_namemax = ULONG_MAX;
|
||||
map<unsigned long,struct statvfs> fsstats;
|
||||
vector<string>::const_iterator iter;
|
||||
vector<string>::const_iterator enditer;
|
||||
|
||||
for(iter = srcmounts.begin(), enditer = srcmounts.end(); iter != enditer; ++iter)
|
||||
{
|
||||
int rv;
|
||||
struct statvfs fsstat;
|
||||
rv = ::statvfs(iter->c_str(),&fsstat);
|
||||
if(rv != 0)
|
||||
continue;
|
||||
|
||||
if(min_bsize > fsstat.f_bsize)
|
||||
min_bsize = fsstat.f_bsize;
|
||||
if(min_frsize > fsstat.f_frsize)
|
||||
min_frsize = fsstat.f_frsize;
|
||||
if(min_namemax > fsstat.f_namemax)
|
||||
min_namemax = fsstat.f_namemax;
|
||||
|
||||
fsstats.insert(pair<unsigned long,struct statvfs>(fsstat.f_fsid,fsstat));
|
||||
}
|
||||
|
||||
map<unsigned long,struct statvfs>::iterator fsstatiter = fsstats.begin();
|
||||
map<unsigned long,struct statvfs>::iterator endfsstatiter = fsstats.end();
|
||||
if(fsstatiter != endfsstatiter)
|
||||
{
|
||||
fsstat = (fsstatiter++)->second;
|
||||
_normalize_statvfs(&fsstat,min_bsize,min_frsize,min_namemax);
|
||||
for(;fsstatiter != endfsstatiter;++fsstatiter)
|
||||
{
|
||||
_normalize_statvfs(&fsstatiter->second,min_bsize,min_frsize,min_namemax);
|
||||
_merge_statvfs(&fsstat,&fsstatiter->second,policy);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
namespace mergerfs
|
||||
{
|
||||
namespace statfs
|
||||
{
|
||||
int
|
||||
statfs(const char *fusepath,
|
||||
struct statvfs *stat)
|
||||
{
|
||||
const ugid::SetResetGuard uid;
|
||||
const config::Config &config = config::get();
|
||||
|
||||
return _statfs(config.policy.statfs,
|
||||
config.srcmounts,
|
||||
*stat);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue