2019-01-25 07:03:46 -05:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2019-01-25 07:03:46 -05:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "fuse_readlink.hpp"
|
|
|
|
|
|
2019-01-25 07:03:46 -05:00
|
|
|
#include "config.hpp"
|
|
|
|
|
#include "errno.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_lstat.hpp"
|
2019-01-25 07:03:46 -05:00
|
|
|
#include "fs_path.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_readlink.hpp"
|
2019-01-25 07:03:46 -05:00
|
|
|
#include "symlinkify.hpp"
|
|
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include "fuse.h"
|
2019-01-25 07:03:46 -05:00
|
|
|
|
2026-03-19 12:17:44 -05:00
|
|
|
#include <algorithm>
|
2025-09-17 23:51:16 -04:00
|
|
|
#include <cstring>
|
2020-12-19 15:06:08 -05:00
|
|
|
|
2019-01-25 07:03:46 -05:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
static
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t
|
2025-09-17 23:51:16 -04:00
|
|
|
_readlink_core_symlinkify(const fs::path &fullpath_,
|
|
|
|
|
char *buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
const size_t bufsize_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const time_t symlinkify_timeout_)
|
2025-07-25 17:53:48 -05:00
|
|
|
{
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t rv;
|
2025-07-25 17:53:48 -05:00
|
|
|
struct stat st;
|
2026-04-24 17:35:47 -05:00
|
|
|
std::string fullpath_str;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-03-19 12:17:44 -05:00
|
|
|
fullpath_str = fullpath_.string();
|
|
|
|
|
|
|
|
|
|
rv = fs::lstat(fullpath_str,&st);
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
if(!symlinkify::can_be_symlink(st,symlinkify_timeout_))
|
2026-04-24 17:35:47 -05:00
|
|
|
return fs::readlink(fullpath_,buf_,bufsize_);
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-04-24 17:35:47 -05:00
|
|
|
rv = std::min(fullpath_str.size(),bufsize_);
|
|
|
|
|
memcpy(buf_,fullpath_str.c_str(),rv);
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-04-24 17:35:47 -05:00
|
|
|
return rv;
|
2025-07-25 17:53:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t
|
2025-09-17 23:51:16 -04:00
|
|
|
_readlink_core(const fs::path &basepath_,
|
|
|
|
|
const fs::path &fusepath_,
|
|
|
|
|
char *buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
const size_t bufsize_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const bool symlinkify_,
|
|
|
|
|
const time_t symlinkify_timeout_)
|
2025-07-25 17:53:48 -05:00
|
|
|
{
|
2025-09-17 23:51:16 -04:00
|
|
|
fs::path fullpath;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
fullpath = basepath_ / fusepath_;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
if(symlinkify_)
|
2026-03-14 11:23:31 -05:00
|
|
|
return ::_readlink_core_symlinkify(fullpath,buf_,bufsize_,symlinkify_timeout_);
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-04-24 17:35:47 -05:00
|
|
|
return fs::readlink(fullpath,buf_,bufsize_);
|
2025-07-25 17:53:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t
|
2025-07-25 17:53:48 -05:00
|
|
|
_readlink(const Policy::Search &searchFunc_,
|
FUSE: pass Branches::Ptr by value with empty-branches guards
Previously most FUSE ops took a reference to the live cfg.branches
object. With atomic_load now producing a shared_ptr snapshot, pass
it by value: the op works against a single stable view of the
configuration for its whole lifetime, immune to concurrent
reconfiguration.
Also bail out with -ENOENT when the policy returns an empty branch
set rather than dereferencing branches[0]. This was reachable if a
policy rejected every branch (e.g. everything readonly while
looking for a writable target).
Covers access, chmod, chown, create, getattr, getxattr, ioctl,
link, listxattr, mkdir, mknod, open, readdir (cor/cosr),
readlink, removexattr, rename, rmdir, setxattr, statfs, statx,
symlink, truncate, unlink, utimens.
While here, in _getattr only dereference st_->st_mode when lstat
succeeded (it is otherwise uninitialized).
2026-04-18 00:56:15 -04:00
|
|
|
const Branches::Ptr ibranches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
char *buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
const size_t bufsize_,
|
2025-07-25 17:53:48 -05:00
|
|
|
const bool symlinkify_,
|
|
|
|
|
const time_t symlinkify_timeout_)
|
2019-01-25 07:03:46 -05:00
|
|
|
{
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t rv;
|
2025-07-25 17:53:48 -05:00
|
|
|
std::vector<Branch*> obranches;
|
|
|
|
|
|
|
|
|
|
rv = searchFunc_(ibranches_,fusepath_,obranches);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
2026-03-19 12:17:44 -05:00
|
|
|
if(obranches.empty())
|
|
|
|
|
return -ENOENT;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
return ::_readlink_core(obranches[0]->path,
|
|
|
|
|
fusepath_,
|
|
|
|
|
buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
bufsize_,
|
2025-07-25 17:53:48 -05:00
|
|
|
symlinkify_,
|
|
|
|
|
symlinkify_timeout_);
|
2019-01-25 07:03:46 -05:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 17:35:47 -05:00
|
|
|
ssize_t
|
2025-10-13 00:21:36 -04:00
|
|
|
FUSE::readlink(const fuse_req_ctx_t *ctx_,
|
|
|
|
|
const char *fusepath_,
|
|
|
|
|
char *buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
size_t bufsize_)
|
2019-01-25 07:03:46 -05:00
|
|
|
{
|
2026-03-14 11:23:31 -05:00
|
|
|
if(bufsize_ == 0)
|
2026-04-24 17:35:47 -05:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
const fs::path fusepath{fusepath_};
|
2026-03-14 11:23:31 -05:00
|
|
|
|
2025-09-06 12:03:51 -04:00
|
|
|
return ::_readlink(cfg.func.readlink.policy,
|
|
|
|
|
cfg.branches,
|
2025-09-17 23:51:16 -04:00
|
|
|
fusepath,
|
2025-07-25 17:53:48 -05:00
|
|
|
buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
bufsize_,
|
2025-09-06 12:03:51 -04:00
|
|
|
cfg.symlinkify,
|
|
|
|
|
cfg.symlinkify_timeout);
|
2019-01-25 07:03:46 -05:00
|
|
|
}
|