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
|
|
|
|
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
|
|
|
|
|
int
|
2025-09-17 23:51:16 -04:00
|
|
|
_readlink_core_standard(const fs::path &fullpath_,
|
|
|
|
|
char *buf_,
|
2026-03-14 11:23:31 -05:00
|
|
|
const size_t bufsize_)
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
2026-03-14 11:23:31 -05:00
|
|
|
rv = fs::readlink(fullpath_,buf_,(bufsize_ - 1));
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
buf_[rv] = '\0';
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
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
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
|
|
rv = fs::lstat(fullpath_,&st);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
if(!symlinkify::can_be_symlink(st,symlinkify_timeout_))
|
2026-03-14 11:23:31 -05:00
|
|
|
return ::_readlink_core_standard(fullpath_,buf_,bufsize_);
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-03-14 11:23:31 -05:00
|
|
|
strncpy(buf_,fullpath_.c_str(),(bufsize_ - 1));
|
|
|
|
|
buf_[bufsize_ - 1] = '\0';
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
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-03-14 11:23:31 -05:00
|
|
|
return ::_readlink_core_standard(fullpath,buf_,bufsize_);
|
2025-07-25 17:53:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_readlink(const Policy::Search &searchFunc_,
|
|
|
|
|
const Branches &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
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
int rv;
|
|
|
|
|
std::vector<Branch*> obranches;
|
|
|
|
|
|
|
|
|
|
rv = searchFunc_(ibranches_,fusepath_,obranches);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
int
|
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
|
|
|
{
|
2025-12-05 21:03:48 -06:00
|
|
|
const fs::path fusepath{fusepath_};
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-03-14 11:23:31 -05:00
|
|
|
if(bufsize_ == 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
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
|
|
|
}
|