Browse Source

Merge pull request #471 from trapexit/clonepath

stop clonepath at base directory. fixes #467
pull/472/head
trapexit 7 years ago
committed by GitHub
parent
commit
aa6e4c1865
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src/create.cpp
  2. 29
      src/fs_clonepath.cpp
  3. 7
      src/fs_clonepath.hpp
  4. 18
      src/link.cpp
  5. 23
      src/mkdir.cpp
  6. 21
      src/mknod.cpp
  7. 57
      src/rename.cpp
  8. 22
      src/symlink.cpp
  9. 3
      src/ugid.cpp
  10. 3
      src/ugid.hpp
  11. 4
      src/ugid_linux.hpp
  12. 3
      src/ugid_linux.icpp
  13. 3
      src/ugid_rwlock.hpp
  14. 3
      src/ugid_rwlock.icpp

21
src/create.cpp

@ -34,7 +34,6 @@ using std::vector;
using namespace mergerfs; using namespace mergerfs;
static static
inline
int int
_create_core(const string &fullpath, _create_core(const string &fullpath,
mode_t mode, mode_t mode,
@ -49,10 +48,8 @@ _create_core(const string &fullpath,
static static
int int
_create_core(const string &existingpath,
const string &createpath,
_create_core(const string &createpath,
const char *fusepath, const char *fusepath,
const char *fusedirpath,
const mode_t mode, const mode_t mode,
const mode_t umask, const mode_t umask,
const int flags, const int flags,
@ -61,14 +58,6 @@ _create_core(const string &existingpath,
int rv; int rv;
string fullpath; string fullpath;
if(createpath != existingpath)
{
const ugid::SetRootGuard ugidGuard;
rv = fs::clonepath(existingpath,createpath,fusedirpath);
if(rv == -1)
return -errno;
}
fs::path::make(&createpath,fusepath,fullpath); fs::path::make(&createpath,fusepath,fullpath);
rv = _create_core(fullpath,mode,umask,flags); rv = _create_core(fullpath,mode,umask,flags);
@ -111,8 +100,12 @@ _create(Policy::Func::Search searchFunc,
if(rv == -1) if(rv == -1)
return -errno; return -errno;
return _create_core(*existingpaths[0],*createpaths[0],
fusepath,fusedirpathcstr,
rv = fs::clonepath_as_root(*existingpaths[0],*createpaths[0],fusedirpath);
if(rv == -1)
return -errno;
return _create_core(*createpaths[0],
fusepath,
mode,umask,flags,fh); mode,umask,flags,fh);
} }

29
src/fs_clonepath.cpp

@ -26,6 +26,7 @@
#include "fs_clonepath.hpp" #include "fs_clonepath.hpp"
#include "fs_path.hpp" #include "fs_path.hpp"
#include "fs_xattr.hpp" #include "fs_xattr.hpp"
#include "ugid.hpp"
using std::string; using std::string;
@ -59,6 +60,9 @@ namespace fs
string frompath; string frompath;
string dirname; string dirname;
if((relative == NULL) || (relative[0] == '\0'))
return 0;
dirname = relative; dirname = relative;
fs::path::dirname(dirname); fs::path::dirname(dirname);
if(!dirname.empty()) if(!dirname.empty())
@ -114,4 +118,29 @@ namespace fs
{ {
return fs::clonepath(from,to,relative.c_str()); return fs::clonepath(from,to,relative.c_str());
} }
int
clonepath_as_root(const string &from,
const string &to,
const char *relative)
{
if((relative == NULL) || (relative[0] == '\0'))
return 0;
if(from == to)
return 0;
{
const ugid::SetRootGuard ugidGuard;
return fs::clonepath(from,to,relative);
}
}
int
clonepath_as_root(const std::string &from,
const std::string &to,
const std::string &relative)
{
return fs::clonepath_as_root(from,to,relative.c_str());
}
} }

7
src/fs_clonepath.hpp

@ -26,4 +26,11 @@ namespace fs
int clonepath(const std::string &from, int clonepath(const std::string &from,
const std::string &to, const std::string &to,
const std::string &relative); const std::string &relative);
int clonepath_as_root(const std::string &from,
const std::string &to,
const char *relative);
int clonepath_as_root(const std::string &from,
const std::string &to,
const std::string &relative);
} }

18
src/link.cpp

@ -39,21 +39,12 @@ _link_create_path_core(const string &oldbasepath,
const string &newbasepath, const string &newbasepath,
const char *oldfusepath, const char *oldfusepath,
const char *newfusepath, const char *newfusepath,
const string &newfusedirpath,
const int error) const int error)
{ {
int rv; int rv;
string oldfullpath; string oldfullpath;
string newfullpath; string newfullpath;
if(oldbasepath != newbasepath)
{
const ugid::SetRootGuard ugidGuard;
rv = fs::clonepath(newbasepath,oldbasepath,newfusedirpath);
if(rv == -1)
return errno;
}
fs::path::make(&oldbasepath,oldfusepath,oldfullpath); fs::path::make(&oldbasepath,oldfusepath,oldfullpath);
fs::path::make(&oldbasepath,newfusepath,newfullpath); fs::path::make(&oldbasepath,newfusepath,newfullpath);
@ -70,14 +61,18 @@ _link_create_path_loop(const vector<const string*> &oldbasepaths,
const char *newfusepath, const char *newfusepath,
const string &newfusedirpath) const string &newfusedirpath)
{ {
int rv;
int error; int error;
error = -1; error = -1;
for(size_t i = 0, ei = oldbasepaths.size(); i != ei; i++) for(size_t i = 0, ei = oldbasepaths.size(); i != ei; i++)
{ {
rv = fs::clonepath_as_root(newbasepath,*oldbasepaths[i],newfusedirpath);
if(rv == -1)
error = error::calc(rv,error,errno);
else
error = _link_create_path_core(*oldbasepaths[i],newbasepath, error = _link_create_path_core(*oldbasepaths[i],newbasepath,
oldfusepath,newfusepath, oldfusepath,newfusepath,
newfusedirpath,
error); error);
} }
@ -143,8 +138,7 @@ _clonepath_if_would_create(Policy::Func::Search searchFunc,
if(rv == -1) if(rv == -1)
return -1; return -1;
const ugid::SetRootGuard ugidGuard;
return fs::clonepath(*newbasepath[0],oldbasepath,newfusedirpathcstr);
return fs::clonepath_as_root(*newbasepath[0],oldbasepath,newfusedirpathcstr);
} }
static static

23
src/mkdir.cpp

@ -34,7 +34,6 @@ using std::vector;
using namespace mergerfs; using namespace mergerfs;
static static
inline
int int
_mkdir_core(const string &fullpath, _mkdir_core(const string &fullpath,
mode_t mode, mode_t mode,
@ -48,10 +47,8 @@ _mkdir_core(const string &fullpath,
static static
int int
_mkdir_loop_core(const string &existingpath,
const string &createpath,
_mkdir_loop_core(const string &createpath,
const char *fusepath, const char *fusepath,
const char *fusedirpath,
const mode_t mode, const mode_t mode,
const mode_t umask, const mode_t umask,
const int error) const int error)
@ -59,14 +56,6 @@ _mkdir_loop_core(const string &existingpath,
int rv; int rv;
string fullpath; string fullpath;
if(createpath != existingpath)
{
const ugid::SetRootGuard ugidGuard;
rv = fs::clonepath(existingpath,createpath,fusedirpath);
if(rv == -1)
return errno;
}
fs::path::make(&createpath,fusepath,fullpath); fs::path::make(&createpath,fusepath,fullpath);
rv = _mkdir_core(fullpath,mode,umask); rv = _mkdir_core(fullpath,mode,umask);
@ -83,13 +72,19 @@ _mkdir_loop(const string &existingpath,
const mode_t mode, const mode_t mode,
const mode_t umask) const mode_t umask)
{ {
int rv;
int error; int error;
error = -1; error = -1;
for(size_t i = 0, ei = createpaths.size(); i != ei; i++) for(size_t i = 0, ei = createpaths.size(); i != ei; i++)
{ {
error = _mkdir_loop_core(existingpath,*createpaths[i],
fusepath,fusedirpath,mode,umask,error);
rv = fs::clonepath_as_root(existingpath,*createpaths[i],fusedirpath);
if(rv == -1)
error = error::calc(rv,error,errno);
else
error = _mkdir_loop_core(*createpaths[i],
fusepath,
mode,umask,error);
} }
return -error; return -error;

21
src/mknod.cpp

@ -49,10 +49,8 @@ _mknod_core(const string &fullpath,
static static
int int
_mknod_loop_core(const string &existingpath,
const string &createpath,
_mknod_loop_core(const string &createpath,
const char *fusepath, const char *fusepath,
const char *fusedirpath,
const mode_t mode, const mode_t mode,
const mode_t umask, const mode_t umask,
const dev_t dev, const dev_t dev,
@ -61,14 +59,6 @@ _mknod_loop_core(const string &existingpath,
int rv; int rv;
string fullpath; string fullpath;
if(createpath != existingpath)
{
const ugid::SetRootGuard ugidGuard;
rv = fs::clonepath(existingpath,createpath,fusedirpath);
if(rv == -1)
return -1;
}
fs::path::make(&createpath,fusepath,fullpath); fs::path::make(&createpath,fusepath,fullpath);
rv = _mknod_core(fullpath,mode,umask,dev); rv = _mknod_core(fullpath,mode,umask,dev);
@ -86,13 +76,18 @@ _mknod_loop(const string &existingpath,
const mode_t umask, const mode_t umask,
const dev_t dev) const dev_t dev)
{ {
int rv;
int error; int error;
error = -1; error = -1;
for(size_t i = 0, ei = createpaths.size(); i != ei; i++) for(size_t i = 0, ei = createpaths.size(); i != ei; i++)
{ {
error = _mknod_loop_core(existingpath,*createpaths[i],
fusepath,fusedirpath,
rv = fs::clonepath_as_root(existingpath,*createpaths[i],fusedirpath);
if(rv == -1)
error = error::calc(rv,error,errno);
else
error = _mknod_loop_core(*createpaths[i],
fusepath,
mode,umask,dev,error); mode,umask,dev,error);
} }

57
src/rename.cpp

@ -27,7 +27,6 @@
#include "fs_path.hpp" #include "fs_path.hpp"
#include "rv.hpp" #include "rv.hpp"
#include "rwlock.hpp" #include "rwlock.hpp"
#include "success_fail.hpp"
#include "ugid.hpp" #include "ugid.hpp"
using std::string; using std::string;
@ -57,27 +56,6 @@ _remove(const vector<string> &toremove)
fs::remove(toremove[i]); fs::remove(toremove[i]);
} }
static
int
_rename(const std::string &oldbasepath,
const std::string &oldfullpath,
const std::string &newbasepath,
const std::string &newfusedirpath,
const std::string &newfullpath)
{
int rv;
if(oldbasepath != newbasepath)
{
const ugid::SetRootGuard guard;
rv = fs::clonepath(newbasepath,oldbasepath,newfusedirpath);
if(rv == -1)
return -1;
}
return fs::rename(oldfullpath,newfullpath);
}
static static
void void
_rename_create_path_core(const vector<const string*> &oldbasepaths, _rename_create_path_core(const vector<const string*> &oldbasepaths,
@ -94,21 +72,26 @@ _rename_create_path_core(const vector<const string*> &oldbasepaths,
string oldfullpath; string oldfullpath;
string newfullpath; string newfullpath;
fs::path::make(&oldbasepath,newfusepath,newfullpath);
ismember = member(oldbasepaths,oldbasepath); ismember = member(oldbasepaths,oldbasepath);
if(ismember) if(ismember)
{
rv = fs::clonepath_as_root(newbasepath,oldbasepath,newfusedirpath);
if(rv != -1)
{ {
fs::path::make(&oldbasepath,oldfusepath,oldfullpath); fs::path::make(&oldbasepath,oldfusepath,oldfullpath);
fs::path::make(&oldbasepath,newfusepath,newfullpath);
rv = fs::rename(oldfullpath,newfullpath);
}
rv = _rename(oldbasepath,oldfullpath,
newbasepath,newfusedirpath,newfullpath);
error = error::calc(rv,error,errno); error = error::calc(rv,error,errno);
if(RENAME_FAILED(rv))
if(rv == -1)
tounlink.push_back(oldfullpath); tounlink.push_back(oldfullpath);
} }
else else
{ {
fs::path::make(&oldbasepath,newfusepath,newfullpath);
tounlink.push_back(newfullpath); tounlink.push_back(newfullpath);
} }
} }
@ -138,7 +121,7 @@ _rename_create_path(Policy::Func::Search searchFunc,
if(POLICY_FAILED(rv)) if(POLICY_FAILED(rv))
return -errno; return -errno;
error = RENAME_FAIL;
error = -1;
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++) for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
{ {
const string &oldbasepath = srcmounts[i]; const string &oldbasepath = srcmounts[i];
@ -150,7 +133,8 @@ _rename_create_path(Policy::Func::Search searchFunc,
error,toremove); error,toremove);
} }
if(RENAME_SUCCEEDED(error))
if(error == 0)
_remove(toremove); _remove(toremove);
return -error; return -error;
@ -169,12 +153,9 @@ _clonepath(Policy::Func::Search searchFunc,
rv = searchFunc(srcmounts,fusedirpath.c_str(),minfreespace,srcbasepath); rv = searchFunc(srcmounts,fusedirpath.c_str(),minfreespace,srcbasepath);
if(POLICY_FAILED(rv)) if(POLICY_FAILED(rv))
return rv;
return -errno;
{
const ugid::SetRootGuard ugidGuard;
fs::clonepath(*srcbasepath[0],dstbasepath,fusedirpath);
}
fs::clonepath_as_root(*srcbasepath[0],dstbasepath,fusedirpath);
return POLICY_SUCCESS; return POLICY_SUCCESS;
} }
@ -232,7 +213,7 @@ _rename_preserve_path_core(Policy::Func::Search searchFunc,
fs::path::make(&oldbasepath,oldfusepath,oldfullpath); fs::path::make(&oldbasepath,oldfusepath,oldfullpath);
rv = fs::rename(oldfullpath,newfullpath); rv = fs::rename(oldfullpath,newfullpath);
if(RENAME_FAILED_WITH(rv,ENOENT))
if((rv == -1) && (errno == ENOENT))
{ {
rv = _clonepath_if_would_create(searchFunc,createFunc, rv = _clonepath_if_would_create(searchFunc,createFunc,
srcmounts,minfreespace, srcmounts,minfreespace,
@ -242,7 +223,7 @@ _rename_preserve_path_core(Policy::Func::Search searchFunc,
} }
error = error::calc(rv,error,errno); error = error::calc(rv,error,errno);
if(RENAME_FAILED(rv))
if(rv == -1)
toremove.push_back(oldfullpath); toremove.push_back(oldfullpath);
} }
else else
@ -270,7 +251,7 @@ _rename_preserve_path(Policy::Func::Search searchFunc,
if(POLICY_FAILED(rv)) if(POLICY_FAILED(rv))
return -errno; return -errno;
error = RENAME_FAIL;
error = -1;
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++) for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
{ {
const string &oldbasepath = srcmounts[i]; const string &oldbasepath = srcmounts[i];
@ -282,7 +263,7 @@ _rename_preserve_path(Policy::Func::Search searchFunc,
error,toremove); error,toremove);
} }
if(RENAME_SUCCEEDED(error))
if(error == 0)
_remove(toremove); _remove(toremove);
return -error; return -error;

22
src/symlink.cpp

@ -36,24 +36,14 @@ using namespace mergerfs;
static static
int int
_symlink_loop_core(const string &existingpath,
const string &newbasepath,
_symlink_loop_core(const string &newbasepath,
const char *oldpath, const char *oldpath,
const char *newpath, const char *newpath,
const char *newdirpath,
const int error) const int error)
{ {
int rv; int rv;
string fullnewpath; string fullnewpath;
if(newbasepath != existingpath)
{
const ugid::SetRootGuard ugidGuard;
rv = fs::clonepath(existingpath,newbasepath,newdirpath);
if(rv == -1)
return -1;
}
fs::path::make(&newbasepath,newpath,fullnewpath); fs::path::make(&newbasepath,newpath,fullnewpath);
rv = fs::symlink(oldpath,fullnewpath); rv = fs::symlink(oldpath,fullnewpath);
@ -69,13 +59,19 @@ _symlink_loop(const string &existingpath,
const char *newpath, const char *newpath,
const char *newdirpath) const char *newdirpath)
{ {
int rv;
int error; int error;
error = -1; error = -1;
for(size_t i = 0, ei = newbasepaths.size(); i != ei; i++) for(size_t i = 0, ei = newbasepaths.size(); i != ei; i++)
{ {
error = _symlink_loop_core(existingpath,*newbasepaths[i],
oldpath,newpath,newdirpath,
rv = fs::clonepath_as_root(existingpath,*newbasepaths[i],newdirpath);
if(rv == -1)
error = error::calc(rv,error,errno);
else
error = _symlink_loop_core(*newbasepaths[i],
oldpath,
newpath,
error); error);
} }

3
src/ugid.cpp

@ -22,8 +22,6 @@
#include "ugid_rwlock.icpp" #include "ugid_rwlock.icpp"
#endif #endif
namespace mergerfs
{
namespace ugid namespace ugid
{ {
void void
@ -35,4 +33,3 @@ namespace mergerfs
cache.initgroups(uid,gid); cache.initgroups(uid,gid);
} }
} }
}

3
src/ugid.hpp

@ -21,14 +21,11 @@
#include <vector> #include <vector>
namespace mergerfs
{
namespace ugid namespace ugid
{ {
void init(); void init();
void initgroups(const uid_t uid, const gid_t gid); void initgroups(const uid_t uid, const gid_t gid);
} }
}
#if defined __linux__ and UGID_USE_RWLOCK == 0 #if defined __linux__ and UGID_USE_RWLOCK == 0
#include "ugid_linux.hpp" #include "ugid_linux.hpp"

4
src/ugid_linux.hpp

@ -51,9 +51,6 @@
#define GETEGID() (::syscall(SYS_getegid)) #define GETEGID() (::syscall(SYS_getegid))
#endif #endif
namespace mergerfs
{
namespace ugid namespace ugid
{ {
extern __thread uid_t currentuid; extern __thread uid_t currentuid;
@ -113,7 +110,6 @@ namespace mergerfs
const gid_t prevgid; const gid_t prevgid;
}; };
} }
}
#undef SETREUID #undef SETREUID
#undef SETREGID #undef SETREGID

3
src/ugid_linux.icpp

@ -21,8 +21,6 @@
#include <vector> #include <vector>
namespace mergerfs
{
namespace ugid namespace ugid
{ {
__thread uid_t currentuid = 0; __thread uid_t currentuid = 0;
@ -34,4 +32,3 @@ namespace mergerfs
{ {
} }
} }
}

3
src/ugid_rwlock.hpp

@ -21,8 +21,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
namespace mergerfs
{
namespace ugid namespace ugid
{ {
extern uid_t currentuid; extern uid_t currentuid;
@ -98,4 +96,3 @@ namespace mergerfs
const gid_t prevgid; const gid_t prevgid;
}; };
} }
}

3
src/ugid_rwlock.icpp

@ -21,8 +21,6 @@
#include <vector> #include <vector>
namespace mergerfs
{
namespace ugid namespace ugid
{ {
uid_t currentuid; uid_t currentuid;
@ -45,4 +43,3 @@ namespace mergerfs
currentgid = ::getegid(); currentgid = ::getegid();
} }
} }
}
Loading…
Cancel
Save