2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2016-01-14 16:50:22 -05: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.
|
2015-12-15 23:52:09 -05:00
|
|
|
*/
|
2014-06-17 19:12:09 -04:00
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
#include "config.hpp"
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2015-12-15 23:52:09 -05:00
|
|
|
#include "fs_clonepath.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_path.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_remove.hpp"
|
|
|
|
#include "fs_rename.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "ugid.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2020-09-13 00:25:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2015-08-30 17:10:15 -04:00
|
|
|
bool
|
2019-09-24 10:27:46 -04:00
|
|
|
member(const vector<string> &haystack,
|
|
|
|
const string &needle)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
2016-01-29 16:54:39 -05:00
|
|
|
for(size_t i = 0, ei = haystack.size(); i != ei; i++)
|
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
if(haystack[i] == needle)
|
2016-01-29 16:54:39 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-12-15 23:52:09 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 17:10:15 -04:00
|
|
|
static
|
|
|
|
void
|
2015-12-15 23:52:09 -05:00
|
|
|
_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]);
|
2015-12-15 23:52:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2019-09-24 10:27:46 -04:00
|
|
|
_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)
|
2015-02-24 18:47:31 -05:00
|
|
|
{
|
|
|
|
int rv;
|
2015-12-15 23:52:09 -05:00
|
|
|
bool ismember;
|
|
|
|
string oldfullpath;
|
|
|
|
string newfullpath;
|
|
|
|
|
|
|
|
ismember = member(oldbasepaths,oldbasepath);
|
|
|
|
if(ismember)
|
2015-02-24 18:47:31 -05:00
|
|
|
{
|
2018-03-07 18:24:47 -05:00
|
|
|
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);
|
2018-03-07 18:24:47 -05:00
|
|
|
|
|
|
|
rv = fs::rename(oldfullpath,newfullpath);
|
|
|
|
}
|
2015-12-15 23:52:09 -05:00
|
|
|
|
2016-12-21 00:00:00 -05:00
|
|
|
error = error::calc(rv,error,errno);
|
2018-03-07 18:24:47 -05:00
|
|
|
if(rv == -1)
|
2015-12-15 23:52:09 -05:00
|
|
|
tounlink.push_back(oldfullpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-25 07:03:46 -05:00
|
|
|
newfullpath = fs::path::make(oldbasepath,newfusepath);
|
2018-03-07 18:24:47 -05:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
tounlink.push_back(newfullpath);
|
2015-08-30 17:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2015-12-15 23:52:09 -05:00
|
|
|
_rename_create_path(Policy::Func::Search searchFunc,
|
|
|
|
Policy::Func::Action actionFunc,
|
2018-10-20 23:40:02 -04:00
|
|
|
const Branches &branches_,
|
2016-01-29 16:54:39 -05:00
|
|
|
const char *oldfusepath,
|
|
|
|
const char *newfusepath)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
2018-10-13 23:48:30 -04:00
|
|
|
string newfusedirpath;
|
2015-12-15 23:52:09 -05:00
|
|
|
vector<string> toremove;
|
2019-09-24 10:27:46 -04:00
|
|
|
vector<string> newbasepath;
|
|
|
|
vector<string> oldbasepaths;
|
|
|
|
vector<string> branches;
|
2015-12-15 23:52:09 -05:00
|
|
|
|
2020-08-20 18:47:04 -04:00
|
|
|
rv = actionFunc(branches_,oldfusepath,&oldbasepaths);
|
2018-10-12 11:04:33 -04:00
|
|
|
if(rv == -1)
|
2015-12-15 23:52:09 -05:00
|
|
|
return -errno;
|
|
|
|
|
2018-10-13 23:48:30 -04:00
|
|
|
newfusedirpath = fs::path::dirname(newfusepath);
|
|
|
|
|
2020-08-20 18:47:04 -04:00
|
|
|
rv = searchFunc(branches_,newfusedirpath,&newbasepath);
|
2018-10-12 11:04:33 -04:00
|
|
|
if(rv == -1)
|
2015-12-15 23:52:09 -05:00
|
|
|
return -errno;
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
branches_.to_paths(branches);
|
|
|
|
|
2018-03-07 18:24:47 -05:00
|
|
|
error = -1;
|
2019-09-24 10:27:46 -04:00
|
|
|
for(size_t i = 0, ei = branches.size(); i != ei; i++)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
const string &oldbasepath = branches[i];
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2016-01-29 16:54:39 -05:00
|
|
|
_rename_create_path_core(oldbasepaths,
|
2019-09-24 10:27:46 -04:00
|
|
|
oldbasepath,newbasepath[0],
|
2016-01-29 16:54:39 -05:00
|
|
|
oldfusepath,newfusepath,
|
|
|
|
newfusedirpath,
|
|
|
|
error,toremove);
|
2015-02-24 18:47:31 -05:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:24:47 -05:00
|
|
|
|
|
|
|
if(error == 0)
|
2015-12-15 23:52:09 -05:00
|
|
|
_remove(toremove);
|
2015-08-30 17:10:15 -04:00
|
|
|
|
|
|
|
return -error;
|
2015-02-24 18:47:31 -05:00
|
|
|
}
|
|
|
|
|
2016-02-26 16:57:14 -05:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_clonepath(Policy::Func::Search searchFunc,
|
2018-10-20 23:40:02 -04:00
|
|
|
const Branches &branches_,
|
2016-02-26 16:57:14 -05:00
|
|
|
const string &dstbasepath,
|
|
|
|
const string &fusedirpath)
|
|
|
|
{
|
|
|
|
int rv;
|
2019-09-24 10:27:46 -04:00
|
|
|
vector<string> srcbasepath;
|
2016-02-26 16:57:14 -05:00
|
|
|
|
2020-08-20 18:47:04 -04:00
|
|
|
rv = searchFunc(branches_,fusedirpath,&srcbasepath);
|
2018-10-12 11:04:33 -04:00
|
|
|
if(rv == -1)
|
2018-03-07 18:24:47 -05:00
|
|
|
return -errno;
|
2016-02-26 16:57:14 -05:00
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
fs::clonepath_as_root(srcbasepath[0],dstbasepath,fusedirpath);
|
2016-02-26 16:57:14 -05:00
|
|
|
|
2018-10-12 11:04:33 -04:00
|
|
|
return 0;
|
2016-02-26 16:57:14 -05:00
|
|
|
}
|
|
|
|
|
2015-02-24 18:47:31 -05:00
|
|
|
static
|
|
|
|
int
|
2015-12-15 23:52:09 -05:00
|
|
|
_clonepath_if_would_create(Policy::Func::Search searchFunc,
|
|
|
|
Policy::Func::Create createFunc,
|
2018-10-20 23:40:02 -04:00
|
|
|
const Branches &branches_,
|
2015-12-15 23:52:09 -05:00
|
|
|
const string &oldbasepath,
|
2016-01-29 16:54:39 -05:00
|
|
|
const char *oldfusepath,
|
|
|
|
const char *newfusepath)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
int rv;
|
2015-12-15 23:52:09 -05:00
|
|
|
string newfusedirpath;
|
2019-09-24 10:27:46 -04:00
|
|
|
vector<string> newbasepath;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2018-10-13 23:48:30 -04:00
|
|
|
newfusedirpath = fs::path::dirname(newfusepath);
|
|
|
|
|
2020-08-20 18:47:04 -04:00
|
|
|
rv = createFunc(branches_,newfusedirpath,&newbasepath);
|
2018-10-12 11:04:33 -04:00
|
|
|
if(rv == -1)
|
2016-02-24 17:10:56 -05:00
|
|
|
return rv;
|
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
if(oldbasepath == newbasepath[0])
|
2020-08-20 18:47:04 -04:00
|
|
|
return _clonepath(searchFunc,branches_,oldbasepath,newfusedirpath);
|
2015-12-15 23:52:09 -05:00
|
|
|
|
2018-10-12 11:04:33 -04:00
|
|
|
return (errno=EXDEV,-1);
|
2015-12-15 23:52:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2019-09-24 10:27:46 -04:00
|
|
|
_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)
|
2015-12-15 23:52:09 -05:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
bool ismember;
|
|
|
|
string newfullpath;
|
|
|
|
|
2019-01-25 07:03:46 -05:00
|
|
|
newfullpath = fs::path::make(oldbasepath,newfusepath);
|
2015-12-15 23:52:09 -05:00
|
|
|
|
|
|
|
ismember = member(oldbasepaths,oldbasepath);
|
|
|
|
if(ismember)
|
|
|
|
{
|
|
|
|
string oldfullpath;
|
|
|
|
|
2019-01-25 07:03:46 -05:00
|
|
|
oldfullpath = fs::path::make(oldbasepath,oldfusepath);
|
2015-12-15 23:52:09 -05:00
|
|
|
|
2016-10-19 09:28:06 -04:00
|
|
|
rv = fs::rename(oldfullpath,newfullpath);
|
2018-03-07 18:24:47 -05:00
|
|
|
if((rv == -1) && (errno == ENOENT))
|
2015-12-15 23:52:09 -05:00
|
|
|
{
|
|
|
|
rv = _clonepath_if_would_create(searchFunc,createFunc,
|
2020-08-20 18:47:04 -04:00
|
|
|
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);
|
2015-12-15 23:52:09 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 00:00:00 -05:00
|
|
|
error = error::calc(rv,error,errno);
|
2018-03-07 18:24:47 -05:00
|
|
|
if(rv == -1)
|
2015-12-15 23:52:09 -05:00
|
|
|
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,
|
2018-10-20 23:40:02 -04:00
|
|
|
const Branches &branches_,
|
2016-01-29 16:54:39 -05:00
|
|
|
const char *oldfusepath,
|
|
|
|
const char *newfusepath)
|
2015-12-15 23:52:09 -05:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
vector<string> toremove;
|
2019-09-24 10:27:46 -04:00
|
|
|
vector<string> oldbasepaths;
|
|
|
|
vector<string> branches;
|
2015-08-30 17:10:15 -04:00
|
|
|
|
2020-08-20 18:47:04 -04:00
|
|
|
rv = actionFunc(branches_,oldfusepath,&oldbasepaths);
|
2018-10-12 11:04:33 -04:00
|
|
|
if(rv == -1)
|
2014-08-16 13:59:55 -04:00
|
|
|
return -errno;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
branches_.to_paths(branches);
|
|
|
|
|
2018-03-07 18:24:47 -05:00
|
|
|
error = -1;
|
2019-09-24 10:27:46 -04:00
|
|
|
for(size_t i = 0, ei = branches.size(); i != ei; i++)
|
2015-12-15 23:52:09 -05:00
|
|
|
{
|
2019-09-24 10:27:46 -04:00
|
|
|
const string &oldbasepath = branches[i];
|
2015-12-15 23:52:09 -05:00
|
|
|
|
2016-01-29 16:54:39 -05:00
|
|
|
_rename_preserve_path_core(searchFunc,createFunc,
|
2020-08-20 18:47:04 -04:00
|
|
|
branches_,
|
2016-01-29 16:54:39 -05:00
|
|
|
oldbasepaths,oldbasepath,
|
|
|
|
oldfusepath,newfusepath,
|
|
|
|
error,toremove);
|
2015-12-15 23:52:09 -05:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:24:47 -05:00
|
|
|
if(error == 0)
|
2015-12-15 23:52:09 -05:00
|
|
|
_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
|
|
|
{
|
2019-09-24 10:27: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);
|
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
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);
|
|
|
|
|
2019-09-24 10:27:46 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|