/* ISC License Copyright (c) 2025, Antonio SJ Musumeci 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 "fuse_statx.hpp" #include "config.hpp" #include "errno.hpp" #include "fileinfo.hpp" #include "fs_inode.hpp" #include "fs_path.hpp" #include "fs_statx.hpp" #include "state.hpp" #include "str.hpp" #include "symlinkify.hpp" #include "ugid.hpp" #include "fuse.h" #include static int _statx_fake_root(struct fuse_statx *st_) { st_->ino = 0; st_->mode = (S_IFDIR|S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); st_->nlink = 2; st_->uid = 0; st_->gid = 0; st_->size = 0; st_->blocks = 0; st_->atime.tv_sec = 0; st_->mtime.tv_sec = 0; st_->ctime.tv_sec = 0; return 0; } static int _statx_controlfile(struct fuse_statx *st_) { static const uid_t uid = ::getuid(); static const gid_t gid = ::getgid(); static const time_t now = ::time(NULL); st_->ino = 0; st_->mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); st_->nlink = 1; st_->uid = uid; st_->gid = gid; st_->size = 0; st_->blocks = 0; st_->atime.tv_sec = now; st_->mtime.tv_sec = now; st_->ctime.tv_sec = now; return 0; } static int _statx(const fs::path &fusepath_, const u32 flags_, const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { int rv; rv = cfg.statx(cfg.branches, fusepath_, flags_, mask_, st_, cfg.follow_symlinks, cfg.symlinkify_timeout); if((rv < 0) && Config::is_rootdir(fusepath_)) return ::_statx_fake_root(st_); timeout_->entry = ((rv >= 0) ? cfg.cache_entry : cfg.cache_negative_entry); timeout_->attr = cfg.cache_attr; return rv; } int FUSE::statx(const fuse_req_ctx_t *ctx_, const char *fusepath_, const u32 flags_, const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { const fs::path fusepath{fusepath_}; if(Config::is_ctrl_file(fusepath)) return ::_statx_controlfile(st_); return ::_statx(fusepath, flags_|AT_STATX_DONT_SYNC, mask_, st_, timeout_); } int FUSE::statx_fh(const fuse_req_ctx_t *ctx_, const u64 fh_, const u32 flags_, const u32 mask_, struct fuse_statx *st_, fuse_timeouts_t *timeout_) { uint64_t fh; fh = fh_; if(fh == 0) { state.open_files.cvisit(ctx_->nodeid, [&](auto &val_) { fh = val_.second.fi->to_fh(); }); } if(fh == 0) return -ENOENT; FileInfo *fi = FileInfo::from_fh(fh); return ::_statx(fi->fusepath, flags_|AT_STATX_DONT_SYNC, mask_, st_, timeout_); }