mergerfs/src/fuse_rename.cpp

317 lines
8.3 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_path.hpp"
2020-08-17 16:29:22 -04:00
#include "fs_remove.hpp"
#include "fs_rename.hpp"
#include "ugid.hpp"
2014-05-12 12:41:46 -04:00
#include <algorithm>
#include <string>
#include <vector>
2014-05-12 12:41:46 -04:00
using std::string;
using std::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
static
bool
member(const vector<string> &haystack,
const string &needle)
{
for(size_t i = 0, ei = haystack.size(); i != ei; i++)
{
if(haystack[i] == needle)
return true;
}
return false;
}
static
void
_remove(const vector<string> &toremove)
{
for(size_t i = 0, ei = toremove.size(); i != ei; i++)
2016-12-22 15:53:17 -05:00
fs::remove(toremove[i]);
}
static
void
_rename_create_path_core(const vector<string> &oldbasepaths,
const string &oldbasepath,
const string &newbasepath,
const char *oldfusepath,
const char *newfusepath,
const string &newfusedirpath,
int &error,
vector<string> &tounlink)
{
int rv;
bool ismember;
string oldfullpath;
string newfullpath;
ismember = member(oldbasepaths,oldbasepath);
if(ismember)
{
rv = fs::clonepath_as_root(newbasepath,oldbasepath,newfusedirpath);
if(rv != -1)
{
2019-01-25 07:03:46 -05:00
oldfullpath = fs::path::make(oldbasepath,oldfusepath);
newfullpath = fs::path::make(oldbasepath,newfusepath);
rv = fs::rename(oldfullpath,newfullpath);
}
2016-12-21 00:00:00 -05:00
error = error::calc(rv,error,errno);
if(rv == -1)
tounlink.push_back(oldfullpath);
}
else
{
2019-01-25 07:03:46 -05:00
newfullpath = fs::path::make(oldbasepath,newfusepath);
tounlink.push_back(newfullpath);
}
}
static
int
_rename_create_path(Policy::Func::Search searchFunc,
Policy::Func::Action actionFunc,
const Branches &branches_,
const char *oldfusepath,
const char *newfusepath)
{
int rv;
int error;
2018-10-13 23:48:30 -04:00
string newfusedirpath;
vector<string> toremove;
vector<string> newbasepath;
vector<string> oldbasepaths;
vector<string> branches;
rv = actionFunc(branches_,oldfusepath,&oldbasepaths);
2018-10-12 11:04:33 -04:00
if(rv == -1)
return -errno;
2018-10-13 23:48:30 -04:00
newfusedirpath = fs::path::dirname(newfusepath);
rv = searchFunc(branches_,newfusedirpath,&newbasepath);
2018-10-12 11:04:33 -04:00
if(rv == -1)
return -errno;
branches_.to_paths(branches);
error = -1;
for(size_t i = 0, ei = branches.size(); i != ei; i++)
{
const string &oldbasepath = branches[i];
_rename_create_path_core(oldbasepaths,
oldbasepath,newbasepath[0],
oldfusepath,newfusepath,
newfusedirpath,
error,toremove);
}
if(error == 0)
_remove(toremove);
return -error;
}
static
int
_clonepath(Policy::Func::Search searchFunc,
const Branches &branches_,
const string &dstbasepath,
const string &fusedirpath)
{
int rv;
vector<string> srcbasepath;
rv = searchFunc(branches_,fusedirpath,&srcbasepath);
2018-10-12 11:04:33 -04:00
if(rv == -1)
return -errno;
fs::clonepath_as_root(srcbasepath[0],dstbasepath,fusedirpath);
2018-10-12 11:04:33 -04:00
return 0;
}
static
int
_clonepath_if_would_create(Policy::Func::Search searchFunc,
Policy::Func::Create createFunc,
const Branches &branches_,
const string &oldbasepath,
const char *oldfusepath,
const char *newfusepath)
2014-05-12 12:41:46 -04:00
{
int rv;
string newfusedirpath;
vector<string> newbasepath;
2014-05-12 12:41:46 -04:00
2018-10-13 23:48:30 -04:00
newfusedirpath = fs::path::dirname(newfusepath);
rv = createFunc(branches_,newfusedirpath,&newbasepath);
2018-10-12 11:04:33 -04:00
if(rv == -1)
return rv;
if(oldbasepath == newbasepath[0])
return _clonepath(searchFunc,branches_,oldbasepath,newfusedirpath);
2018-10-12 11:04:33 -04:00
return (errno=EXDEV,-1);
}
static
void
_rename_preserve_path_core(Policy::Func::Search searchFunc,
Policy::Func::Create createFunc,
const Branches &branches_,
const vector<string> &oldbasepaths,
const string &oldbasepath,
const char *oldfusepath,
const char *newfusepath,
int &error,
vector<string> &toremove)
{
int rv;
bool ismember;
string newfullpath;
2019-01-25 07:03:46 -05:00
newfullpath = fs::path::make(oldbasepath,newfusepath);
ismember = member(oldbasepaths,oldbasepath);
if(ismember)
{
string oldfullpath;
2019-01-25 07:03:46 -05:00
oldfullpath = fs::path::make(oldbasepath,oldfusepath);
2016-10-19 09:28:06 -04:00
rv = fs::rename(oldfullpath,newfullpath);
if((rv == -1) && (errno == ENOENT))
{
rv = _clonepath_if_would_create(searchFunc,createFunc,
branches_,oldbasepath,
oldfusepath,newfusepath);
2018-10-12 11:04:33 -04:00
if(rv == 0)
2016-10-19 09:28:06 -04:00
rv = fs::rename(oldfullpath,newfullpath);
}
2016-12-21 00:00:00 -05:00
error = error::calc(rv,error,errno);
if(rv == -1)
toremove.push_back(oldfullpath);
}
else
{
toremove.push_back(newfullpath);
}
}
static
int
_rename_preserve_path(Policy::Func::Search searchFunc,
Policy::Func::Action actionFunc,
Policy::Func::Create createFunc,
const Branches &branches_,
const char *oldfusepath,
const char *newfusepath)
{
int rv;
int error;
vector<string> toremove;
vector<string> oldbasepaths;
vector<string> branches;
rv = actionFunc(branches_,oldfusepath,&oldbasepaths);
2018-10-12 11:04:33 -04:00
if(rv == -1)
return -errno;
2014-05-12 12:41:46 -04:00
branches_.to_paths(branches);
error = -1;
for(size_t i = 0, ei = branches.size(); i != ei; i++)
{
const string &oldbasepath = branches[i];
_rename_preserve_path_core(searchFunc,createFunc,
branches_,
oldbasepaths,oldbasepath,
oldfusepath,newfusepath,
error,toremove);
}
if(error == 0)
_remove(toremove);
return -error;
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 *oldpath,
const char *newpath)
2014-05-12 12:41:46 -04:00
{
const fuse_context *fc = fuse_get_context();
Config &config = Config::rw();
const ugid::Set ugid(fc->uid,fc->gid);
2019-01-25 07:03:46 -05:00
config.open_cache.erase(oldpath);
if(config.func.create.policy->path_preserving() && !config.ignorepponrename)
return _rename_preserve_path(config.func.getattr.policy,
config.func.rename.policy,
config.func.create.policy,
2019-01-25 07:03:46 -05:00
config.branches,
oldpath,
newpath);
return _rename_create_path(config.func.getattr.policy,
config.func.rename.policy,
2019-01-25 07:03:46 -05:00
config.branches,
oldpath,
newpath);
2014-05-12 12:41:46 -04:00
}
}