2016-05-07 15:15:51 -04:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2016-05-07 15:15:51 -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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "policy_eplus.hpp"
|
|
|
|
|
|
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-05-07 15:15:51 -04:00
|
|
|
#include "fs_path.hpp"
|
2018-12-12 10:08:17 -05:00
|
|
|
#include "fs_statvfs_cache.hpp"
|
2016-05-07 15:15:51 -04: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-05-07 15:15:51 -04:00
|
|
|
|
2018-10-13 23:48:30 -04:00
|
|
|
#include <limits>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_create(const Branches::Ptr &branches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
std::vector<Branch*> &paths_)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
int error;
|
2026-03-01 13:58:06 -06:00
|
|
|
u64 eplus;
|
2025-07-25 17:53:48 -05:00
|
|
|
fs::info_t info;
|
|
|
|
|
Branch *obranch;
|
|
|
|
|
|
|
|
|
|
obranch = nullptr;
|
|
|
|
|
error = ENOENT;
|
2026-03-01 13:58:06 -06:00
|
|
|
eplus = std::numeric_limits<u64>::max();
|
2025-07-25 17:53:48 -05:00
|
|
|
for(auto &branch : *branches_)
|
|
|
|
|
{
|
|
|
|
|
if(branch.ro_or_nc())
|
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
if(!fs::exists(branch.path,fusepath_))
|
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
|
rv = fs::info(branch.path,&info);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
|
if(info.readonly)
|
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
if(info.spaceavail < branch.minfreespace())
|
|
|
|
|
error_and_continue(error,ENOSPC);
|
|
|
|
|
if(info.spaceused >= eplus)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
eplus = info.spaceused;
|
|
|
|
|
obranch = &branch;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 14:47:50 -06:00
|
|
|
if(!obranch)
|
2025-07-25 17:53:48 -05:00
|
|
|
return -error;
|
|
|
|
|
|
|
|
|
|
paths_.emplace_back(obranch);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_action(const Branches::Ptr &branches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
std::vector<Branch*> &paths_)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
int error;
|
2026-03-01 13:58:06 -06:00
|
|
|
u64 eplus;
|
2025-07-25 17:53:48 -05:00
|
|
|
fs::info_t info;
|
|
|
|
|
Branch *obranch;
|
|
|
|
|
|
|
|
|
|
obranch = nullptr;
|
|
|
|
|
error = ENOENT;
|
2026-03-01 13:58:06 -06:00
|
|
|
eplus = std::numeric_limits<u64>::max();
|
2025-07-25 17:53:48 -05:00
|
|
|
for(auto &branch : *branches_)
|
|
|
|
|
{
|
|
|
|
|
if(branch.ro())
|
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
if(!fs::exists(branch.path,fusepath_))
|
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
|
rv = fs::info(branch.path,&info);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
|
if(info.readonly)
|
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
if(info.spaceused >= eplus)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
eplus = info.spaceused;
|
|
|
|
|
obranch = &branch;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 14:47:50 -06:00
|
|
|
if(!obranch)
|
2025-07-25 17:53:48 -05:00
|
|
|
return -error;
|
|
|
|
|
|
|
|
|
|
paths_.emplace_back(obranch);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_search(const Branches::Ptr &branches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
std::vector<Branch*> &paths_)
|
2016-05-07 15:15:51 -04:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
int rv;
|
2026-03-01 13:58:06 -06:00
|
|
|
u64 eplus;
|
|
|
|
|
u64 spaceused;
|
2025-07-25 17:53:48 -05:00
|
|
|
Branch *obranch;
|
|
|
|
|
|
|
|
|
|
obranch = nullptr;
|
2026-03-09 11:17:39 -05:00
|
|
|
eplus = std::numeric_limits<u64>::max();
|
2025-07-25 17:53:48 -05:00
|
|
|
for(auto &branch : *branches_)
|
|
|
|
|
{
|
|
|
|
|
if(!fs::exists(branch.path,fusepath_))
|
|
|
|
|
continue;
|
|
|
|
|
rv = fs::statvfs_cache_spaceused(branch.path,&spaceused);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
continue;
|
|
|
|
|
if(spaceused >= eplus)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
eplus = spaceused;
|
|
|
|
|
obranch = &branch;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 14:47:50 -06:00
|
|
|
if(!obranch)
|
2025-07-25 17:53:48 -05:00
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
paths_.emplace_back(obranch);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2020-12-19 15:06:08 -05:00
|
|
|
}
|
2020-08-20 18:47:04 -04:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
int
|
2025-09-17 23:51:16 -04:00
|
|
|
Policy::EPLUS::Action::operator()(const Branches::Ptr &branches_,
|
|
|
|
|
const fs::path &fusepath_,
|
2025-05-17 16:35:18 -05:00
|
|
|
std::vector<Branch*> &paths_) const
|
2020-12-19 15:06:08 -05:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
return ::_action(branches_,fusepath_,paths_);
|
2020-12-19 15:06:08 -05:00
|
|
|
}
|
2020-08-20 18:47:04 -04:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
int
|
2025-09-17 23:51:16 -04:00
|
|
|
Policy::EPLUS::Create::operator()(const Branches::Ptr &branches_,
|
|
|
|
|
const fs::path &fusepath_,
|
2025-05-17 16:35:18 -05:00
|
|
|
std::vector<Branch*> &paths_) const
|
2020-12-19 15:06:08 -05:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
return ::_create(branches_,fusepath_,paths_);
|
2016-05-07 15:15:51 -04:00
|
|
|
}
|
|
|
|
|
|
2019-01-06 12:52:55 -05:00
|
|
|
int
|
2025-09-17 23:51:16 -04:00
|
|
|
Policy::EPLUS::Search::operator()(const Branches::Ptr &branches_,
|
|
|
|
|
const fs::path &fusepath_,
|
2025-05-17 16:35:18 -05:00
|
|
|
std::vector<Branch*> &paths_) const
|
2016-05-07 15:15:51 -04:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
return ::_search(branches_,fusepath_,paths_);
|
2016-05-07 15:15:51 -04:00
|
|
|
}
|