mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-22 07:56:32 -05:00
Move everything to negative errno return types (#1499)
This commit is contained in:
parent
2b500f194a
commit
e8cd74cab0
177 changed files with 5952 additions and 5843 deletions
|
|
@ -14,7 +14,10 @@
|
|||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "fuse_rename.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "error.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_clonepath.hpp"
|
||||
#include "fs_link.hpp"
|
||||
|
|
@ -34,359 +37,332 @@
|
|||
#include <vector>
|
||||
|
||||
|
||||
namespace error
|
||||
static
|
||||
bool
|
||||
_contains(const std::vector<Branch*> &haystack_,
|
||||
const char *needle_)
|
||||
{
|
||||
static
|
||||
inline
|
||||
int
|
||||
calc(const int rv_,
|
||||
const int prev_,
|
||||
const int cur_)
|
||||
{
|
||||
if(rv_ == -1)
|
||||
{
|
||||
if(prev_ == 0)
|
||||
return 0;
|
||||
return cur_;
|
||||
}
|
||||
for(auto &hay : haystack_)
|
||||
{
|
||||
if(hay->path == needle_)
|
||||
return true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
namespace l
|
||||
static
|
||||
bool
|
||||
_contains(const std::vector<Branch*> &haystack_,
|
||||
const std::string &needle_)
|
||||
{
|
||||
static
|
||||
bool
|
||||
contains(const std::vector<Branch*> &haystack_,
|
||||
const char *needle_)
|
||||
{
|
||||
for(auto &hay : haystack_)
|
||||
{
|
||||
if(hay->path == needle_)
|
||||
return true;
|
||||
}
|
||||
return ::_contains(haystack_,needle_.c_str());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
static
|
||||
void
|
||||
_remove(const StrVec &toremove_)
|
||||
{
|
||||
for(auto &path : toremove_)
|
||||
fs::remove(path);
|
||||
}
|
||||
|
||||
static
|
||||
bool
|
||||
contains(const std::vector<Branch*> &haystack_,
|
||||
const std::string &needle_)
|
||||
{
|
||||
return l::contains(haystack_,needle_.c_str());
|
||||
}
|
||||
static
|
||||
int
|
||||
_rename_create_path(const Policy::Search &searchPolicy_,
|
||||
const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
Err err;
|
||||
StrVec toremove;
|
||||
std::vector<Branch*> newbranches;
|
||||
std::vector<Branch*> oldbranches;
|
||||
std::filesystem::path oldfullpath;
|
||||
std::filesystem::path newfullpath;
|
||||
|
||||
static
|
||||
void
|
||||
remove(const StrVec &toremove_)
|
||||
{
|
||||
for(auto &path : toremove_)
|
||||
fs::remove(path);
|
||||
}
|
||||
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
static
|
||||
int
|
||||
rename_create_path(const Policy::Search &searchPolicy_,
|
||||
const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
int error;
|
||||
StrVec toremove;
|
||||
std::vector<Branch*> newbranches;
|
||||
std::vector<Branch*> oldbranches;
|
||||
std::filesystem::path oldfullpath;
|
||||
std::filesystem::path newfullpath;
|
||||
rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
for(auto &branch : *branches_)
|
||||
{
|
||||
newfullpath = branch.path;
|
||||
newfullpath += newfusepath_;
|
||||
|
||||
rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
if(!::_contains(oldbranches,branch.path))
|
||||
{
|
||||
toremove.push_back(newfullpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
error = -1;
|
||||
for(auto &branch : *branches_)
|
||||
{
|
||||
newfullpath = branch.path;
|
||||
newfullpath += newfusepath_;
|
||||
oldfullpath = branch.path;
|
||||
oldfullpath += oldfusepath_;
|
||||
|
||||
if(!l::contains(oldbranches,branch.path))
|
||||
{
|
||||
toremove.push_back(newfullpath);
|
||||
continue;
|
||||
}
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
if(rv < 0)
|
||||
{
|
||||
rv = fs::clonepath_as_root(newbranches[0]->path,
|
||||
branch.path,
|
||||
newfusepath_.parent_path());
|
||||
if(rv >= 0)
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
}
|
||||
|
||||
oldfullpath = branch.path;
|
||||
oldfullpath += oldfusepath_;
|
||||
err = rv;
|
||||
if(rv < 0)
|
||||
toremove.push_back(oldfullpath);
|
||||
}
|
||||
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
if(rv == -1)
|
||||
{
|
||||
rv = fs::clonepath_as_root(newbranches[0]->path,
|
||||
branch.path,
|
||||
newfusepath_.parent_path());
|
||||
if(rv == 0)
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
}
|
||||
if(err == 0)
|
||||
::_remove(toremove);
|
||||
|
||||
error = error::calc(rv,error,errno);
|
||||
if(rv == -1)
|
||||
return err;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename_preserve_path(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
bool success;
|
||||
StrVec toremove;
|
||||
std::vector<Branch*> oldbranches;
|
||||
std::filesystem::path oldfullpath;
|
||||
std::filesystem::path newfullpath;
|
||||
|
||||
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
success = false;
|
||||
for(auto &branch : *branches_)
|
||||
{
|
||||
newfullpath = branch.path;
|
||||
newfullpath += newfusepath_;
|
||||
|
||||
if(!::_contains(oldbranches,branch.path))
|
||||
{
|
||||
toremove.push_back(newfullpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
oldfullpath = branch.path;
|
||||
oldfullpath += oldfusepath_;
|
||||
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
if(rv < 0)
|
||||
{
|
||||
toremove.push_back(oldfullpath);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if(error == 0)
|
||||
l::remove(toremove);
|
||||
|
||||
return -error;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename_preserve_path(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
bool success;
|
||||
StrVec toremove;
|
||||
std::vector<Branch*> oldbranches;
|
||||
std::filesystem::path oldfullpath;
|
||||
std::filesystem::path newfullpath;
|
||||
|
||||
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
success = false;
|
||||
for(auto &branch : *branches_)
|
||||
{
|
||||
newfullpath = branch.path;
|
||||
newfullpath += newfusepath_;
|
||||
|
||||
if(!l::contains(oldbranches,branch.path))
|
||||
{
|
||||
toremove.push_back(newfullpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
oldfullpath = branch.path;
|
||||
oldfullpath += oldfusepath_;
|
||||
|
||||
rv = fs::rename(oldfullpath,newfullpath);
|
||||
if(rv == -1)
|
||||
{
|
||||
toremove.push_back(oldfullpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
success = true;
|
||||
}
|
||||
|
||||
// TODO: probably should try to be nuanced here.
|
||||
if(success == false)
|
||||
return -EXDEV;
|
||||
|
||||
l::remove(toremove);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
rename_exdev_rename_back(const std::vector<Branch*> &branches_,
|
||||
const std::filesystem::path &oldfusepath_)
|
||||
{
|
||||
std::filesystem::path oldpath;
|
||||
std::filesystem::path newpath;
|
||||
|
||||
for(auto &branch : branches_)
|
||||
{
|
||||
oldpath = branch->path;
|
||||
oldpath /= ".mergerfs_rename_exdev";
|
||||
oldpath += oldfusepath_;
|
||||
|
||||
newpath = branch->path;
|
||||
newpath += oldfusepath_;
|
||||
|
||||
fs::rename(oldpath,newpath);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename_exdev_rename_target(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &ibranches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
std::vector<Branch*> &obranches_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path clonesrc;
|
||||
std::filesystem::path clonetgt;
|
||||
|
||||
rv = actionPolicy_(ibranches_,oldfusepath_,obranches_);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
ugid::SetRootGuard ugidGuard;
|
||||
for(auto &branch : obranches_)
|
||||
{
|
||||
clonesrc = branch->path;
|
||||
clonetgt = branch->path;
|
||||
clonetgt /= ".mergerfs_rename_exdev";
|
||||
|
||||
rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
|
||||
if((rv == -1) && (errno == ENOENT))
|
||||
{
|
||||
fs::mkdir(clonetgt,01777);
|
||||
rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
|
||||
}
|
||||
|
||||
if(rv == -1)
|
||||
goto error;
|
||||
|
||||
clonesrc += oldfusepath_;
|
||||
clonetgt += oldfusepath_;
|
||||
|
||||
rv = fs::rename(clonesrc,clonetgt);
|
||||
if(rv == -1)
|
||||
goto error;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
l::rename_exdev_rename_back(obranches_,oldfusepath_);
|
||||
success = true;
|
||||
}
|
||||
|
||||
// TODO: probably should try to be nuanced here.
|
||||
if(success == false)
|
||||
return -EXDEV;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path target;
|
||||
std::filesystem::path linkpath;
|
||||
std::vector<Branch*> branches;
|
||||
::_remove(toremove);
|
||||
|
||||
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
linkpath = newfusepath_;
|
||||
target = "/.mergerfs_rename_exdev";
|
||||
target += oldfusepath_;
|
||||
target = target.lexically_relative(linkpath.parent_path());
|
||||
|
||||
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
|
||||
if(rv < 0)
|
||||
l::rename_exdev_rename_back(branches,oldfusepath_);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &mount_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path target;
|
||||
std::filesystem::path linkpath;
|
||||
std::vector<Branch*> branches;
|
||||
|
||||
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
linkpath = newfusepath_;
|
||||
target = mount_;
|
||||
target /= ".mergerfs_rename_exdev";
|
||||
target += oldfusepath_;
|
||||
|
||||
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
|
||||
if(rv < 0)
|
||||
l::rename_exdev_rename_back(branches,oldfusepath_);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename_exdev(Config::Read &cfg_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
switch(cfg_->rename_exdev)
|
||||
{
|
||||
case RenameEXDEV::ENUM::PASSTHROUGH:
|
||||
return -EXDEV;
|
||||
case RenameEXDEV::ENUM::REL_SYMLINK:
|
||||
return l::rename_exdev_rel_symlink(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldfusepath_,
|
||||
newfusepath_);
|
||||
case RenameEXDEV::ENUM::ABS_SYMLINK:
|
||||
return l::rename_exdev_abs_symlink(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
cfg_->mountpoint,
|
||||
oldfusepath_,
|
||||
newfusepath_);
|
||||
}
|
||||
|
||||
return -EXDEV;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
rename(Config::Read &cfg_,
|
||||
const std::filesystem::path &oldpath_,
|
||||
const std::filesystem::path &newpath_)
|
||||
{
|
||||
if(cfg_->func.create.policy.path_preserving() && !cfg_->ignorepponrename)
|
||||
return l::rename_preserve_path(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldpath_,
|
||||
newpath_);
|
||||
|
||||
return l::rename_create_path(cfg_->func.getattr.policy,
|
||||
cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldpath_,
|
||||
newpath_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
namespace FUSE
|
||||
static
|
||||
void
|
||||
_rename_exdev_rename_back(const std::vector<Branch*> &branches_,
|
||||
const std::filesystem::path &oldfusepath_)
|
||||
{
|
||||
int
|
||||
rename(const char *oldfusepath_,
|
||||
const char *newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
Config::Read cfg;
|
||||
std::filesystem::path oldfusepath(oldfusepath_);
|
||||
std::filesystem::path newfusepath(newfusepath_);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
const ugid::Set ugid(fc->uid,fc->gid);
|
||||
std::filesystem::path oldpath;
|
||||
std::filesystem::path newpath;
|
||||
|
||||
rv = l::rename(cfg,oldfusepath,newfusepath);
|
||||
if(rv == -EXDEV)
|
||||
return l::rename_exdev(cfg,oldfusepath,newfusepath);
|
||||
for(auto &branch : branches_)
|
||||
{
|
||||
oldpath = branch->path;
|
||||
oldpath /= ".mergerfs_rename_exdev";
|
||||
oldpath += oldfusepath_;
|
||||
|
||||
return rv;
|
||||
}
|
||||
newpath = branch->path;
|
||||
newpath += oldfusepath_;
|
||||
|
||||
fs::rename(oldpath,newpath);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename_exdev_rename_target(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &ibranches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
std::vector<Branch*> &obranches_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path clonesrc;
|
||||
std::filesystem::path clonetgt;
|
||||
|
||||
rv = actionPolicy_(ibranches_,oldfusepath_,obranches_);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
ugid::SetRootGuard ugidGuard;
|
||||
for(auto &branch : obranches_)
|
||||
{
|
||||
clonesrc = branch->path;
|
||||
clonetgt = branch->path;
|
||||
clonetgt /= ".mergerfs_rename_exdev";
|
||||
|
||||
rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
|
||||
if(rv == -ENOENT)
|
||||
{
|
||||
fs::mkdir(clonetgt,01777);
|
||||
rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
|
||||
}
|
||||
|
||||
if(rv < 0)
|
||||
goto error;
|
||||
|
||||
clonesrc += oldfusepath_;
|
||||
clonetgt += oldfusepath_;
|
||||
|
||||
rv = fs::rename(clonesrc,clonetgt);
|
||||
if(rv < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
::_rename_exdev_rename_back(obranches_,oldfusepath_);
|
||||
|
||||
return -EXDEV;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path target;
|
||||
std::filesystem::path linkpath;
|
||||
std::vector<Branch*> branches;
|
||||
|
||||
rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
linkpath = newfusepath_;
|
||||
target = "/.mergerfs_rename_exdev";
|
||||
target += oldfusepath_;
|
||||
target = target.lexically_relative(linkpath.parent_path());
|
||||
|
||||
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
|
||||
if(rv < 0)
|
||||
::_rename_exdev_rename_back(branches,oldfusepath_);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const std::filesystem::path &mount_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
std::filesystem::path target;
|
||||
std::filesystem::path linkpath;
|
||||
std::vector<Branch*> branches;
|
||||
|
||||
rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
linkpath = newfusepath_;
|
||||
target = mount_;
|
||||
target /= ".mergerfs_rename_exdev";
|
||||
target += oldfusepath_;
|
||||
|
||||
rv = FUSE::symlink(target.c_str(),linkpath.c_str());
|
||||
if(rv < 0)
|
||||
::_rename_exdev_rename_back(branches,oldfusepath_);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename_exdev(Config::Read &cfg_,
|
||||
const std::filesystem::path &oldfusepath_,
|
||||
const std::filesystem::path &newfusepath_)
|
||||
{
|
||||
switch(cfg_->rename_exdev)
|
||||
{
|
||||
case RenameEXDEV::ENUM::PASSTHROUGH:
|
||||
return -EXDEV;
|
||||
case RenameEXDEV::ENUM::REL_SYMLINK:
|
||||
return ::_rename_exdev_rel_symlink(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldfusepath_,
|
||||
newfusepath_);
|
||||
case RenameEXDEV::ENUM::ABS_SYMLINK:
|
||||
return ::_rename_exdev_abs_symlink(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
cfg_->mountpoint,
|
||||
oldfusepath_,
|
||||
newfusepath_);
|
||||
}
|
||||
|
||||
return -EXDEV;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_rename(Config::Read &cfg_,
|
||||
const std::filesystem::path &oldpath_,
|
||||
const std::filesystem::path &newpath_)
|
||||
{
|
||||
if(cfg_->func.create.policy.path_preserving() && !cfg_->ignorepponrename)
|
||||
return ::_rename_preserve_path(cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldpath_,
|
||||
newpath_);
|
||||
|
||||
return ::_rename_create_path(cfg_->func.getattr.policy,
|
||||
cfg_->func.rename.policy,
|
||||
cfg_->branches,
|
||||
oldpath_,
|
||||
newpath_);
|
||||
}
|
||||
|
||||
int
|
||||
FUSE::rename(const char *oldfusepath_,
|
||||
const char *newfusepath_)
|
||||
{
|
||||
int rv;
|
||||
Config::Read cfg;
|
||||
std::filesystem::path oldfusepath(oldfusepath_);
|
||||
std::filesystem::path newfusepath(newfusepath_);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
const ugid::Set ugid(fc->uid,fc->gid);
|
||||
|
||||
rv = ::_rename(cfg,oldfusepath,newfusepath);
|
||||
if(rv == -EXDEV)
|
||||
return ::_rename_exdev(cfg,oldfusepath,newfusepath);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue