2016-07-11 22:08:25 +08: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 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2016-07-11 22:08:25 +08:00
|
|
|
#include "fs.hpp"
|
2018-10-14 11:48:30 +08:00
|
|
|
#include "fs_exists.hpp"
|
|
|
|
#include "fs_info.hpp"
|
2016-07-11 22:08:25 +08:00
|
|
|
#include "fs_path.hpp"
|
|
|
|
#include "policy.hpp"
|
2018-10-21 11:40:02 +08:00
|
|
|
#include "policy_error.hpp"
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-14 11:48:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-07-11 22:08:25 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using mergerfs::Category;
|
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
namespace epff
|
2016-07-11 22:08:25 +08:00
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
create(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
const uint64_t minfreespace,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
fs::info_t info;
|
|
|
|
const Branch *branch;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
|
|
|
if(!fs::exists(branch->path,fusepath))
|
|
|
|
error_and_continue(error,ENOENT);
|
2018-11-02 11:37:45 +08:00
|
|
|
if(branch->ro_or_nc())
|
2018-10-21 11:40:02 +08:00
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
rv = fs::info(&branch->path,&info);
|
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(info.readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
if(info.spaceavail < minfreespace)
|
|
|
|
error_and_continue(error,ENOSPC);
|
|
|
|
|
|
|
|
paths.push_back(&branch->path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (errno=error,-1);
|
|
|
|
}
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
action(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
bool readonly;
|
|
|
|
const Branch *branch;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
|
|
|
if(!fs::exists(branch->path,fusepath))
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(branch->ro())
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
rv = fs::readonly(&branch->path,&readonly);
|
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
|
|
|
paths.push_back(&branch->path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (errno=error,-1);
|
|
|
|
}
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
search(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
const Branch *branch;
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
if(!fs::exists(branch->path,fusepath))
|
|
|
|
continue;
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
paths.push_back(&branch->path);
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-11 22:08:25 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
return (errno=ENOENT,-1);
|
|
|
|
}
|
2016-07-11 22:08:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
int
|
|
|
|
Policy::Func::epff(const Category::Enum::Type type,
|
2018-10-21 11:40:02 +08:00
|
|
|
const Branches &branches_,
|
2016-07-11 22:08:25 +08:00
|
|
|
const char *fusepath,
|
|
|
|
const uint64_t minfreespace,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Category::Enum::create:
|
|
|
|
return epff::create(branches_,fusepath,minfreespace,paths);
|
|
|
|
case Category::Enum::action:
|
|
|
|
return epff::action(branches_,fusepath,paths);
|
|
|
|
case Category::Enum::search:
|
|
|
|
default:
|
|
|
|
return epff::search(branches_,fusepath,paths);
|
|
|
|
}
|
2016-07-11 22:08:25 +08:00
|
|
|
}
|
|
|
|
}
|