2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2015-12-15 23:52:09 -05:00
|
|
|
The MIT License (MIT)
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
Copyright (c) 2016 Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
2014-06-17 19:12:09 -04:00
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
#include <errno.h>
|
2015-12-15 23:52:09 -05:00
|
|
|
#include <stdio.h>
|
2014-05-12 12:41:46 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
2014-05-12 12:41:46 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "config.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"
|
2016-01-13 11:33:39 -05:00
|
|
|
#include "rv.hpp"
|
2014-08-03 15:50:28 -04:00
|
|
|
#include "rwlock.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "ugid.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2015-08-30 17:10:15 -04:00
|
|
|
using std::set;
|
2015-06-22 23:35:46 -04:00
|
|
|
using mergerfs::Policy;
|
2015-12-15 23:52:09 -05:00
|
|
|
using namespace mergerfs;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
static
|
2015-08-30 17:10:15 -04:00
|
|
|
bool
|
2015-12-15 23:52:09 -05:00
|
|
|
member(const vector<string> &haystack,
|
|
|
|
const string &needle)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
2015-12-15 23:52:09 -05:00
|
|
|
return (std::find(haystack.begin(),haystack.end(),needle) != haystack.end());
|
|
|
|
}
|
|
|
|
|
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++)
|
|
|
|
::remove(toremove[i].c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
_rename_create_path_one(const vector<string> &oldbasepaths,
|
|
|
|
const string &oldbasepath,
|
|
|
|
const string &newbasepath,
|
|
|
|
const string &oldfusepath,
|
|
|
|
const string &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;
|
|
|
|
|
|
|
|
fs::path::make(oldbasepath,newfusepath,newfullpath);
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
ismember = member(oldbasepaths,oldbasepath);
|
|
|
|
if(ismember)
|
2015-02-24 18:47:31 -05:00
|
|
|
{
|
2015-12-15 23:52:09 -05:00
|
|
|
if(oldbasepath != newbasepath)
|
|
|
|
{
|
|
|
|
const ugid::SetRootGuard ugidGuard;
|
|
|
|
fs::clonepath(newbasepath,oldbasepath,newfusedirpath);
|
|
|
|
}
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
fs::path::make(oldbasepath,oldfusepath,oldfullpath);
|
|
|
|
|
|
|
|
rv = ::rename(oldfullpath.c_str(),newfullpath.c_str());
|
2016-01-13 11:33:39 -05:00
|
|
|
error = calc_error(rv,error,errno);
|
2015-12-15 23:52:09 -05:00
|
|
|
if(rv == -1)
|
|
|
|
tounlink.push_back(oldfullpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
const vector<string> &srcmounts,
|
|
|
|
const size_t minfreespace,
|
|
|
|
const string &oldfusepath,
|
|
|
|
const string &newfusepath)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
2015-12-15 23:52:09 -05:00
|
|
|
string newbasepath;
|
|
|
|
vector<string> toremove;
|
|
|
|
vector<string> oldbasepaths;
|
|
|
|
|
|
|
|
rv = actionFunc(srcmounts,oldfusepath,minfreespace,oldbasepaths);
|
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
const string newfusedirpath = fs::path::dirname(newfusepath);
|
|
|
|
rv = searchFunc(srcmounts,newfusedirpath,minfreespace,newbasepath);
|
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
error = -1;
|
|
|
|
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
|
2015-08-30 17:10:15 -04:00
|
|
|
{
|
2015-12-15 23:52:09 -05:00
|
|
|
const string &oldbasepath = srcmounts[i];
|
2015-02-24 18:47:31 -05:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
_rename_create_path_one(oldbasepaths,oldbasepath,newbasepath,
|
|
|
|
oldfusepath,newfusepath,
|
|
|
|
newfusedirpath,
|
|
|
|
error,toremove);
|
2015-02-24 18:47:31 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 17:10:15 -04: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
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2015-12-15 23:52:09 -05:00
|
|
|
_clonepath_if_would_create(Policy::Func::Search searchFunc,
|
|
|
|
Policy::Func::Create createFunc,
|
|
|
|
const vector<string> &srcmounts,
|
|
|
|
const size_t minfreespace,
|
|
|
|
const string &oldbasepath,
|
|
|
|
const string &oldfusepath,
|
|
|
|
const string &newfusepath)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
int rv;
|
2015-12-15 23:52:09 -05:00
|
|
|
string newbasepath;
|
|
|
|
string newfusedirpath;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
newfusedirpath = fs::path::dirname(newfusepath);
|
|
|
|
|
|
|
|
rv = createFunc(srcmounts,newfusedirpath,minfreespace,newbasepath);
|
|
|
|
if(rv != -1)
|
|
|
|
{
|
|
|
|
if(oldbasepath == newbasepath)
|
|
|
|
{
|
|
|
|
rv = searchFunc(srcmounts,newfusedirpath,minfreespace,newbasepath);
|
|
|
|
if(rv != -1)
|
|
|
|
{
|
|
|
|
const ugid::SetRootGuard ugidGuard;
|
|
|
|
fs::clonepath(newbasepath,oldbasepath,newfusedirpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = -1;
|
|
|
|
errno = EXDEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
_rename_preserve_path_one(Policy::Func::Search searchFunc,
|
|
|
|
Policy::Func::Create createFunc,
|
|
|
|
const vector<string> &srcmounts,
|
|
|
|
const size_t minfreespace,
|
|
|
|
const vector<string> &oldbasepaths,
|
|
|
|
const string &oldbasepath,
|
|
|
|
const string &oldfusepath,
|
|
|
|
const string &newfusepath,
|
|
|
|
int &error,
|
|
|
|
vector<string> &toremove)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
bool ismember;
|
|
|
|
string newfullpath;
|
|
|
|
|
|
|
|
fs::path::make(oldbasepath,newfusepath,newfullpath);
|
|
|
|
|
|
|
|
ismember = member(oldbasepaths,oldbasepath);
|
|
|
|
if(ismember)
|
|
|
|
{
|
|
|
|
string oldfullpath;
|
|
|
|
|
|
|
|
fs::path::make(oldbasepath,oldfusepath,oldfullpath);
|
|
|
|
|
|
|
|
rv = ::rename(oldfullpath.c_str(),newfullpath.c_str());
|
|
|
|
if((rv == -1) && (errno == ENOENT))
|
|
|
|
{
|
|
|
|
rv = _clonepath_if_would_create(searchFunc,createFunc,
|
|
|
|
srcmounts,minfreespace,
|
|
|
|
oldbasepath,oldfusepath,newfusepath);
|
|
|
|
if(rv != -1)
|
|
|
|
rv = ::rename(oldfullpath.c_str(),newfullpath.c_str());
|
|
|
|
}
|
|
|
|
|
2016-01-13 11:33:39 -05:00
|
|
|
error = calc_error(rv,error,errno);
|
2015-12-15 23:52:09 -05:00
|
|
|
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 vector<string> &srcmounts,
|
|
|
|
const size_t minfreespace,
|
|
|
|
const string &oldfusepath,
|
|
|
|
const string &newfusepath)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
vector<string> toremove;
|
|
|
|
vector<string> oldbasepaths;
|
2015-08-30 17:10:15 -04:00
|
|
|
|
2015-07-01 07:47:40 -04:00
|
|
|
rv = actionFunc(srcmounts,oldfusepath,minfreespace,oldbasepaths);
|
2014-08-16 13:59:55 -04:00
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
error = -1;
|
|
|
|
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
const string &oldbasepath = srcmounts[i];
|
|
|
|
|
|
|
|
_rename_preserve_path_one(searchFunc,createFunc,
|
|
|
|
srcmounts,minfreespace,
|
|
|
|
oldbasepaths,oldbasepath,
|
|
|
|
oldfusepath,newfusepath,
|
|
|
|
error,toremove);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(error == 0)
|
|
|
|
_remove(toremove);
|
|
|
|
|
|
|
|
return -error;
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace mergerfs
|
|
|
|
{
|
2015-07-15 10:04:57 -04:00
|
|
|
namespace fuse
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
int
|
2015-02-24 18:47:31 -05:00
|
|
|
rename(const char *oldpath,
|
|
|
|
const char *newpath)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2015-09-08 15:56:02 -04:00
|
|
|
const fuse_context *fc = fuse_get_context();
|
|
|
|
const Config &config = Config::get(fc);
|
|
|
|
const ugid::Set ugid(fc->uid,fc->gid);
|
|
|
|
const rwlock::ReadGuard readlock(&config.srcmountslock);
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-12-15 23:52:09 -05:00
|
|
|
if(config.create != Policy::epmfs)
|
|
|
|
return _rename_create_path(config.rename,
|
|
|
|
config.create,
|
|
|
|
config.srcmounts,
|
|
|
|
config.minfreespace,
|
|
|
|
oldpath,
|
|
|
|
newpath);
|
|
|
|
|
|
|
|
return _rename_preserve_path(config.getattr,
|
|
|
|
config.rename,
|
|
|
|
config.create,
|
|
|
|
config.srcmounts,
|
|
|
|
config.minfreespace,
|
|
|
|
oldpath,
|
|
|
|
newpath);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|