2016-02-11 23:20:12 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2018-10-13 23:48:30 -04:00
|
|
|
#include "fs_exists.hpp"
|
|
|
|
#include "fs_info.hpp"
|
2016-02-11 23:20:12 -05:00
|
|
|
#include "fs_path.hpp"
|
2018-12-12 10:08:17 -05:00
|
|
|
#include "fs_statvfs_cache.hpp"
|
2016-02-11 23:20:12 -05:00
|
|
|
#include "policy.hpp"
|
2018-10-20 23:40:02 -04:00
|
|
|
#include "policy_error.hpp"
|
2019-09-24 10:27:46 -04:00
|
|
|
#include "rwlock.hpp"
|
2016-02-11 23:20:12 -05:00
|
|
|
|
2018-10-13 23:48:30 -04:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-02-11 23:20:12 -05:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
namespace eplfs
|
2016-02-11 23:20:12 -05:00
|
|
|
{
|
2018-10-20 23:40:02 -04:00
|
|
|
static
|
|
|
|
int
|
2019-09-24 10:27:46 -04:00
|
|
|
create(const Branches &branches_,
|
2020-08-16 18:22:25 -04:00
|
|
|
const char *fusepath_,
|
|
|
|
const uint64_t minfreespace_,
|
|
|
|
vector<string> *paths_)
|
2018-10-20 23:40:02 -04:00
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
rwlock::ReadGuard guard(&branches_.lock);
|
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
uint64_t eplfs;
|
|
|
|
fs::info_t info;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *eplfsbasepath;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
eplfs = std::numeric_limits<uint64_t>::max();
|
|
|
|
eplfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
if(!fs::exists(branch->path,fusepath_))
|
2018-10-20 23:40:02 -04:00
|
|
|
error_and_continue(error,ENOENT);
|
2018-11-01 23:37:45 -04:00
|
|
|
if(branch->ro_or_nc())
|
2018-10-20 23:40:02 -04:00
|
|
|
error_and_continue(error,EROFS);
|
2020-08-16 18:22:25 -04:00
|
|
|
rv = fs::info(branch->path,&info);
|
2018-10-20 23:40:02 -04:00
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(info.readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
2020-08-16 18:22:25 -04:00
|
|
|
if(info.spaceavail < minfreespace_)
|
2018-10-20 23:40:02 -04:00
|
|
|
error_and_continue(error,ENOSPC);
|
|
|
|
if(info.spaceavail > eplfs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
eplfs = info.spaceavail;
|
|
|
|
eplfsbasepath = &branch->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eplfsbasepath == NULL)
|
|
|
|
return (errno=error,-1);
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
paths_->push_back(*eplfsbasepath);
|
2018-10-20 23:40:02 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-11 23:20:12 -05:00
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
static
|
|
|
|
int
|
2019-09-24 10:27:46 -04:00
|
|
|
action(const Branches &branches_,
|
2020-08-16 18:22:25 -04:00
|
|
|
const char *fusepath_,
|
|
|
|
vector<string> *paths_)
|
2018-10-20 23:40:02 -04:00
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
rwlock::ReadGuard guard(&branches_.lock);
|
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
uint64_t eplfs;
|
|
|
|
fs::info_t info;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *eplfsbasepath;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
eplfs = std::numeric_limits<uint64_t>::max();
|
|
|
|
eplfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
if(!fs::exists(branch->path,fusepath_))
|
2018-10-20 23:40:02 -04:00
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(branch->ro())
|
|
|
|
error_and_continue(error,EROFS);
|
2020-08-16 18:22:25 -04:00
|
|
|
rv = fs::info(branch->path,&info);
|
2018-10-20 23:40:02 -04:00
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(info.readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
if(info.spaceavail > eplfs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
eplfs = info.spaceavail;
|
|
|
|
eplfsbasepath = &branch->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eplfsbasepath == NULL)
|
|
|
|
return (errno=error,-1);
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
paths_->push_back(*eplfsbasepath);
|
2018-10-20 23:40:02 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-29 22:58:13 -05:00
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
static
|
|
|
|
int
|
|
|
|
search(const Branches &branches_,
|
2020-08-16 18:22:25 -04:00
|
|
|
const char *fusepath_,
|
|
|
|
vector<string> *paths_)
|
2018-10-20 23:40:02 -04:00
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
rwlock::ReadGuard guard(&branches_.lock);
|
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
int rv;
|
|
|
|
uint64_t eplfs;
|
|
|
|
uint64_t spaceavail;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *eplfsbasepath;
|
|
|
|
|
|
|
|
eplfs = std::numeric_limits<uint64_t>::max();
|
|
|
|
eplfsbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
if(!fs::exists(branch->path,fusepath_))
|
2018-10-20 23:40:02 -04:00
|
|
|
continue;
|
2018-12-12 10:08:17 -05:00
|
|
|
rv = fs::statvfs_cache_spaceavail(branch->path,&spaceavail);
|
2018-10-20 23:40:02 -04:00
|
|
|
if(rv == -1)
|
|
|
|
continue;
|
|
|
|
if(spaceavail > eplfs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
eplfs = spaceavail;
|
|
|
|
eplfsbasepath = &branch->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eplfsbasepath == NULL)
|
|
|
|
return (errno=ENOENT,-1);
|
|
|
|
|
2020-08-16 18:22:25 -04:00
|
|
|
paths_->push_back(*eplfsbasepath);
|
2018-10-20 23:40:02 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-29 22:58:13 -05:00
|
|
|
}
|
|
|
|
|
2019-01-06 12:52:55 -05:00
|
|
|
int
|
2020-08-16 18:22:25 -04:00
|
|
|
Policy::Func::eplfs(const Category type_,
|
2020-08-16 13:30:43 -04:00
|
|
|
const Branches &branches_,
|
2020-08-16 18:22:25 -04:00
|
|
|
const char *fusepath_,
|
|
|
|
const uint64_t minfreespace_,
|
|
|
|
vector<string> *paths_)
|
2016-02-11 23:20:12 -05:00
|
|
|
{
|
2020-08-16 18:22:25 -04:00
|
|
|
switch(type_)
|
2019-01-06 12:52:55 -05:00
|
|
|
{
|
2020-08-16 13:30:43 -04:00
|
|
|
case Category::CREATE:
|
2020-08-16 18:22:25 -04:00
|
|
|
return eplfs::create(branches_,fusepath_,minfreespace_,paths_);
|
2020-08-16 13:30:43 -04:00
|
|
|
case Category::ACTION:
|
2020-08-16 18:22:25 -04:00
|
|
|
return eplfs::action(branches_,fusepath_,paths_);
|
2020-08-16 13:30:43 -04:00
|
|
|
case Category::SEARCH:
|
2019-01-06 12:52:55 -05:00
|
|
|
default:
|
2020-08-16 18:22:25 -04:00
|
|
|
return eplfs::search(branches_,fusepath_,paths_);
|
2019-01-06 12:52:55 -05:00
|
|
|
}
|
2016-02-11 23:20:12 -05:00
|
|
|
}
|