mergerfs/src/fuse_link.cpp

164 lines
4.6 KiB
C++
Raw Normal View History

2019-01-25 07:03:46 -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.
*/
#include "config.hpp"
#include "errno.hpp"
#include "fs_clonepath.hpp"
2020-08-17 16:29:22 -04:00
#include "fs_link.hpp"
#include "fs_lstat.hpp"
2019-01-25 07:03:46 -05:00
#include "fs_path.hpp"
#include "fuse_getattr.hpp"
#include "fuse_symlink.hpp"
#include "ghc/filesystem.hpp"
2021-03-28 12:34:30 -04:00
#include "state.hpp"
2019-01-25 07:03:46 -05:00
#include "ugid.hpp"
#include "fuse.h"
2019-01-25 07:03:46 -05:00
namespace gfs = ghc::filesystem;
2019-01-25 07:03:46 -05:00
namespace l
{
static
int
link_exdev_rel_symlink(const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
gfs::path target(oldpath_);
gfs::path linkpath(newpath_);
target = target.lexically_relative(linkpath.parent_path());
2021-03-28 12:34:30 -04:00
rv = FUSE::SYMLINK::symlink(target.c_str(),linkpath.c_str());
if(rv == 0)
2021-03-28 12:34:30 -04:00
rv = FUSE::GETATTR::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
timeouts_->entry = 0;
return rv;
}
static
int
2021-03-28 12:34:30 -04:00
link_exdev_abs_base_symlink(const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
StrVec basepaths;
2021-03-28 12:34:30 -04:00
gfs::path target;
2021-03-28 12:34:30 -04:00
target = "FIXME";
2021-03-28 12:34:30 -04:00
rv = FUSE::SYMLINK::symlink(target.c_str(),newpath_);
if(rv == 0)
2021-03-28 12:34:30 -04:00
rv = FUSE::GETATTR::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
timeouts_->entry = 0;
return rv;
}
static
int
2021-03-28 12:34:30 -04:00
link_exdev_abs_pool_symlink(const gfs::path &mount_,
const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
2021-03-28 12:34:30 -04:00
gfs::path target;
2021-03-28 12:34:30 -04:00
target = mount_ / &oldpath_[1];
2021-03-28 12:34:30 -04:00
rv = FUSE::SYMLINK::symlink(target.c_str(),newpath_);
if(rv == 0)
2021-03-28 12:34:30 -04:00
rv = FUSE::GETATTR::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
timeouts_->entry = 0;
return rv;
}
static
int
2021-03-28 12:34:30 -04:00
link_exdev(State &s_,
const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
2021-03-28 12:34:30 -04:00
switch(s_->link_exdev)
{
2021-03-28 12:34:30 -04:00
case LinkEXDEV::INVALID:
case LinkEXDEV::PASSTHROUGH:
return -EXDEV;
2021-03-28 12:34:30 -04:00
case LinkEXDEV::REL_SYMLINK:
return l::link_exdev_rel_symlink(oldpath_,
newpath_,
st_,
timeouts_);
2021-03-28 12:34:30 -04:00
case LinkEXDEV::ABS_BASE_SYMLINK:
return l::link_exdev_abs_base_symlink(oldpath_,
newpath_,
st_,
timeouts_);
2021-03-28 12:34:30 -04:00
case LinkEXDEV::ABS_POOL_SYMLINK:
return l::link_exdev_abs_pool_symlink(s_->mountpoint,
oldpath_,
newpath_,
st_,
timeouts_);
}
return -EXDEV;
2019-01-25 07:03:46 -05:00
}
}
2021-03-28 12:34:30 -04:00
namespace FUSE::LINK
2019-01-25 07:03:46 -05:00
{
int
link(const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
2019-01-25 07:03:46 -05:00
{
int rv;
2021-03-28 12:34:30 -04:00
State s;
const fuse_context *fc = fuse_get_context();
const ugid::Set ugid(fc->uid,fc->gid);
2021-03-28 12:34:30 -04:00
rv = s->link(oldpath_,newpath_);
if(rv == -EXDEV)
2021-03-28 12:34:30 -04:00
return l::link_exdev(s,oldpath_,newpath_,st_,timeouts_);
2021-03-28 12:34:30 -04:00
return s->getattr(newpath_,st_,timeouts_);
2019-01-25 07:03:46 -05:00
}
}