Rework policies to return branches rather than path strings

This commit is contained in:
trapexit 2025-05-17 16:35:18 -05:00 • committed by GitHub
parent 7e084ea7db
commit 86b50d2d4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
86 changed files with 1259 additions and 3088 deletions

View file

@ -35,7 +35,6 @@
#include <iostream>
using std::string;
namespace gfs = ghc::filesystem;
namespace error
@ -62,12 +61,12 @@ namespace l
{
static
bool
contains(const StrVec &haystack_,
const char *needle_)
contains(const std::vector<Branch*> &haystack_,
const char *needle_)
{
for(auto &hay : haystack_)
{
if(hay == needle_)
if(hay->path == needle_)
return true;
}
@ -76,8 +75,8 @@ namespace l
static
bool
contains(const StrVec &haystack_,
const string &needle_)
contains(const std::vector<Branch*> &haystack_,
const std::string &needle_)
{
return l::contains(haystack_,needle_.c_str());
}
@ -90,41 +89,27 @@ namespace l
fs::remove(path);
}
static
void
remove(const Branches::CPtr &branches_,
const std::string &relpath_)
{
std::string fullpath;
for(auto &branch : *branches_)
{
fullpath = fs::path::make(branch.path,relpath_);
fs::remove(fullpath);
}
}
static
int
rename_create_path(const Policy::Search &searchPolicy_,
const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const Branches::Ptr &branches_,
const gfs::path &oldfusepath_,
const gfs::path &newfusepath_)
{
int rv;
int error;
StrVec toremove;
StrVec newbasepath;
StrVec oldbasepaths;
std::vector<Branch*> newbranches;
std::vector<Branch*> oldbranches;
gfs::path oldfullpath;
gfs::path newfullpath;
rv = actionPolicy_(branches_,oldfusepath_,&oldbasepaths);
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
if(rv == -1)
return -errno;
rv = searchPolicy_(branches_,newfusepath_.parent_path(),&newbasepath);
rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches);
if(rv == -1)
return -errno;
@ -134,7 +119,7 @@ namespace l
newfullpath = branch.path;
newfullpath += newfusepath_;
if(!l::contains(oldbasepaths,branch.path))
if(!l::contains(oldbranches,branch.path))
{
toremove.push_back(newfullpath);
continue;
@ -146,7 +131,9 @@ namespace l
rv = fs::rename(oldfullpath,newfullpath);
if(rv == -1)
{
rv = fs::clonepath_as_root(newbasepath[0],branch.path,newfusepath_.parent_path());
rv = fs::clonepath_as_root(newbranches[0]->path,
branch.path,
newfusepath_.parent_path());
if(rv == 0)
rv = fs::rename(oldfullpath,newfullpath);
}
@ -165,18 +152,18 @@ namespace l
static
int
rename_preserve_path(const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const Branches::Ptr &branches_,
const gfs::path &oldfusepath_,
const gfs::path &newfusepath_)
{
int rv;
bool success;
StrVec toremove;
StrVec oldbasepaths;
std::vector<Branch*> oldbranches;
gfs::path oldfullpath;
gfs::path newfullpath;
rv = actionPolicy_(branches_,oldfusepath_,&oldbasepaths);
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
if(rv == -1)
return -errno;
@ -186,7 +173,7 @@ namespace l
newfullpath = branch.path;
newfullpath += newfusepath_;
if(!l::contains(oldbasepaths,branch.path))
if(!l::contains(oldbranches,branch.path))
{
toremove.push_back(newfullpath);
continue;
@ -216,19 +203,19 @@ namespace l
static
void
rename_exdev_rename_back(const StrVec &basepaths_,
const gfs::path &oldfusepath_)
rename_exdev_rename_back(const std::vector<Branch*> &branches_,
const gfs::path &oldfusepath_)
{
gfs::path oldpath;
gfs::path newpath;
for(auto &basepath : basepaths_)
for(auto &branch : branches_)
{
oldpath = basepath;
oldpath = branch->path;
oldpath /= ".mergerfs_rename_exdev";
oldpath += oldfusepath_;
newpath = basepath;
newpath = branch->path;
newpath += oldfusepath_;
fs::rename(oldpath,newpath);
@ -238,23 +225,23 @@ namespace l
static
int
rename_exdev_rename_target(const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const Branches::Ptr &ibranches_,
const gfs::path &oldfusepath_,
StrVec *basepaths_)
std::vector<Branch*> &obranches_)
{
int rv;
gfs::path clonesrc;
gfs::path clonetgt;
rv = actionPolicy_(branches_,oldfusepath_,basepaths_);
rv = actionPolicy_(ibranches_,oldfusepath_,obranches_);
if(rv == -1)
return -errno;
ugid::SetRootGuard ugidGuard;
for(auto &basepath : *basepaths_)
for(auto &branch : obranches_)
{
clonesrc = basepath;
clonetgt = basepath;
clonesrc = branch->path;
clonetgt = branch->path;
clonetgt /= ".mergerfs_rename_exdev";
rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
@ -278,7 +265,7 @@ namespace l
return 0;
error:
l::rename_exdev_rename_back(*basepaths_,oldfusepath_);
l::rename_exdev_rename_back(obranches_,oldfusepath_);
return -EXDEV;
}
@ -286,16 +273,16 @@ namespace l
static
int
rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const Branches::Ptr &branches_,
const gfs::path &oldfusepath_,
const gfs::path &newfusepath_)
{
int rv;
StrVec basepaths;
gfs::path target;
gfs::path linkpath;
std::vector<Branch*> branches;
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,&basepaths);
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
if(rv < 0)
return rv;
@ -306,7 +293,7 @@ namespace l
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
if(rv < 0)
l::rename_exdev_rename_back(basepaths,oldfusepath_);
l::rename_exdev_rename_back(branches,oldfusepath_);
return rv;
}
@ -314,17 +301,17 @@ namespace l
static
int
rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const Branches::Ptr &branches_,
const gfs::path &mount_,
const gfs::path &oldfusepath_,
const gfs::path &newfusepath_)
{
int rv;
StrVec basepaths;
gfs::path target;
gfs::path linkpath;
std::vector<Branch*> branches;
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,&basepaths);
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
if(rv < 0)
return rv;
@ -335,7 +322,7 @@ namespace l
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
if(rv < 0)
l::rename_exdev_rename_back(basepaths,oldfusepath_);
l::rename_exdev_rename_back(branches,oldfusepath_);
return rv;
}