mergerfs/src/fuse_rename.cpp

393 lines
10 KiB
C++
Raw Normal View History

2014-05-12 12:41:46 -04: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.
*/
2014-05-12 12:41:46 -04:00
#include "config.hpp"
#include "errno.hpp"
#include "fs_clonepath.hpp"
#include "fs_link.hpp"
#include "fs_mkdir_as_root.hpp"
#include "fs_path.hpp"
2020-08-17 16:29:22 -04:00
#include "fs_remove.hpp"
#include "fs_rename.hpp"
#include "fs_symlink.hpp"
#include "fs_unlink.hpp"
#include "fuse_symlink.hpp"
#include "ugid.hpp"
2014-05-12 12:41:46 -04:00
#include <algorithm>
2025-06-05 23:47:42 -05:00
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
namespace error
{
static
inline
int
calc(const int rv_,
const int prev_,
const int cur_)
{
if(rv_ == -1)
{
if(prev_ == 0)
return 0;
return cur_;
}
return 0;
}
}
2014-05-12 12:41:46 -04:00
namespace l
{
static
bool
contains(const std::vector<Branch*> &haystack_,
const char *needle_)
{
for(auto &hay : haystack_)
{
if(hay->path == needle_)
return true;
}
return false;
}
static
bool
contains(const std::vector<Branch*> &haystack_,
const std::string &needle_)
{
return l::contains(haystack_,needle_.c_str());
}
static
void
remove(const StrVec &toremove_)
{
for(auto &path : toremove_)
fs::remove(path);
}
static
int
2025-06-05 23:47:42 -05:00
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;
2025-06-05 23:47:42 -05:00
std::filesystem::path oldfullpath;
std::filesystem::path newfullpath;
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
if(rv == -1)
return -errno;
rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches);
if(rv == -1)
return -errno;
error = -1;
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)
{
rv = fs::clonepath_as_root(newbranches[0]->path,
branch.path,
newfusepath_.parent_path());
if(rv == 0)
rv = fs::rename(oldfullpath,newfullpath);
}
error = error::calc(rv,error,errno);
if(rv == -1)
toremove.push_back(oldfullpath);
}
2018-10-13 23:48:30 -04:00
if(error == 0)
l::remove(toremove);
return -error;
}
static
int
2025-06-05 23:47:42 -05:00
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;
2025-06-05 23:47:42 -05:00
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
2025-06-05 23:47:42 -05:00
rename_exdev_rename_back(const std::vector<Branch*> &branches_,
const std::filesystem::path &oldfusepath_)
{
2025-06-05 23:47:42 -05:00
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
2025-06-05 23:47:42 -05:00
rename_exdev_rename_target(const Policy::Action &actionPolicy_,
const Branches::Ptr &ibranches_,
const std::filesystem::path &oldfusepath_,
std::vector<Branch*> &obranches_)
{
int rv;
2025-06-05 23:47:42 -05:00
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_);
return -EXDEV;
}
static
int
2025-06-05 23:47:42 -05:00
rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
const Branches::Ptr &branches_,
const std::filesystem::path &oldfusepath_,
const std::filesystem::path &newfusepath_)
{
int rv;
2025-06-05 23:47:42 -05:00
std::filesystem::path target;
std::filesystem::path linkpath;
std::vector<Branch*> branches;
2014-05-12 12:41:46 -04:00
rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
if(rv < 0)
return rv;
2018-10-13 23:48:30 -04:00
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
2025-06-05 23:47:42 -05:00
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;
2025-06-05 23:47:42 -05:00
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
2025-06-05 23:47:42 -05:00
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
2025-06-05 23:47:42 -05:00
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_);
}
2014-05-12 12:41:46 -04:00
}
2019-01-25 07:03:46 -05:00
namespace FUSE
2014-05-12 12:41:46 -04:00
{
2019-01-25 07:03:46 -05:00
int
rename(const char *oldfusepath_,
const char *newfusepath_)
2014-05-12 12:41:46 -04:00
{
int rv;
Config::Read cfg;
2025-06-05 23:47:42 -05:00
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);
2019-01-25 07:03:46 -05:00
rv = l::rename(cfg,oldfusepath,newfusepath);
if(rv == -EXDEV)
return l::rename_exdev(cfg,oldfusepath,newfusepath);
return rv;
2014-05-12 12:41:46 -04:00
}
}