Files
mergerfs/src/fuse_getxattr.cpp
T
Antonio SJ Musumeci ef1e01b5a5 Add unit and integration tests for recently changed code
Unit tests (29 new in tests/tests.cpp):
- str::eq() and str::startswith() char* nullptr safety
- str::from() u64/s64/int suffix parsing (K/M/G/T, overflow, invalid)
- num::humanize() byte formatting (0, 512, 1K, 1M, 1G, 1T, non-exact)
- RmdirErr priority error accumulation (ENOTEMPTY/EEXIST override rules)
- HashSet inline-to-heap growth, empty string, and large insertion
- fs::inode algorithm switching (valid/invalid names, passthrough raw ino,
  distinct inodes across algorithms)
- RND::rand64() bounds (range, max, shrink_to_rand_elem)
- Branches::Impl copy and move assignment re-links default_minfreespace

Integration tests (5 new in tests/):
- TEST_policy_lup: LUP policy create and search
- TEST_rmdir_enotempty_priority: rmdir returns ENOTEMPTY for non-empty dirs
- TEST_open_after_unlink: read/write via fd after unlink
- TEST_api_xattr: mergerfs xattr API (fullpath, relpath, basepath, allpaths)
- TEST_movefile_enospc: moveonenospc configuration round-trip

Also: remove xattr nosys checks from TEST_cfg_xattr_modes, reformat
integration test scripts to use temp_dir/cleanup_dir helpers, and
minor parameter alignment changes in fuse_getxattr, fuse_init,
fuse_rename.
2026-04-29 17:41:00 -04:00

206 lines
5.3 KiB
C++

/*
ISC License
Copyright (c) 2026, 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 "fuse_getxattr.hpp"
#include "config.hpp"
#include "errno.hpp"
#include "fs_findallfiles.hpp"
#include "fs_lgetxattr.hpp"
#include "fs_path.hpp"
#include "fs_statvfs_cache.hpp"
#include "str.hpp"
#include "version.hpp"
#include "fuse.h"
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <stdio.h>
static const char SECURITY_CAPABILITY[] = "security.capability";
static
bool
_is_attrname_security_capability(const char *attrname_)
{
return str::eq(attrname_,SECURITY_CAPABILITY);
}
static
int
_getxattr_ctrl_file(Config &cfg_,
const char *attrname_,
char *buf_,
const size_t count_)
{
int rv;
std::string key;
std::string val;
if(!Config::is_mergerfs_xattr(attrname_))
return -ENOATTR;
key = Config::prune_ctrl_xattr(attrname_);
rv = cfg_.get(key,&val);
if(rv < 0)
return rv;
if(count_ == 0)
return val.size();
if(count_ < val.size())
return -ERANGE;
memcpy(buf_,val.c_str(),val.size());
return (int)val.size();
}
static
int
_getxattr_from_string(char *destbuf_,
const size_t destbufsize_,
const std::string &src_)
{
const size_t srcbufsize = src_.size();
if(destbufsize_ == 0)
return srcbufsize;
if(srcbufsize > destbufsize_)
return -ERANGE;
memcpy(destbuf_,src_.data(),srcbufsize);
return srcbufsize;
}
static
int
_getxattr_user_mergerfs_allpaths(const Branches::Ptr branches_,
const fs::path &fusepath_,
char *buf_,
const size_t count_)
{
std::string concated;
StrVec paths;
StrVec branches;
branches_->to_paths(branches);
fs::findallfiles(branches,fusepath_,&paths);
concated = str::join(paths,'\0');
return ::_getxattr_from_string(buf_,count_,concated);
}
static
int
_getxattr_user_mergerfs(const fs::path &basepath_,
const fs::path &fusepath_,
const fs::path &fullpath_,
const Branches::Ptr branches_,
const char *attrname_,
char *buf_,
const size_t count_)
{
std::string key;
key = Config::prune_ctrl_xattr(attrname_);
if(key == "basepath")
return ::_getxattr_from_string(buf_,count_,basepath_);
if(key == "relpath")
return ::_getxattr_from_string(buf_,count_,fusepath_);
if(key == "fullpath")
return ::_getxattr_from_string(buf_,count_,fullpath_);
if(key == "allpaths")
return ::_getxattr_user_mergerfs_allpaths(branches_,fusepath_,buf_,count_);
return -ENOATTR;
}
static
int
_getxattr(const Policy::Search &searchFunc_,
const Branches::Ptr branches_,
const fs::path &fusepath_,
const char *attrname_,
char *buf_,
const size_t count_)
{
int rv;
fs::path fullpath;
std::vector<Branch*> branches;
rv = searchFunc_(branches_,fusepath_,branches);
if(rv < 0)
return rv;
if(branches.empty())
return -ENOENT;
fullpath = branches[0]->path / fusepath_;
if(Config::is_mergerfs_xattr(attrname_))
return ::_getxattr_user_mergerfs(branches[0]->path,
fusepath_,
fullpath,
branches_,
attrname_,
buf_,
count_);
return fs::lgetxattr(fullpath,attrname_,buf_,count_);
}
int
FUSE::getxattr(const fuse_req_ctx_t *ctx_,
const char *fusepath_,
const char *attrname_,
char *attrvalue_,
size_t attrvalue_size_)
{
const fs::path fusepath{fusepath_};
if(Config::is_ctrl_file(fusepath))
return ::_getxattr_ctrl_file(cfg,
attrname_,
attrvalue_,
attrvalue_size_);
if((cfg.security_capability == false) &&
::_is_attrname_security_capability(attrname_))
return -ENOATTR;
if(cfg.xattr.to_int())
return -cfg.xattr.to_int();
return ::_getxattr(cfg.func.getxattr.policy,
cfg.branches,
fusepath,
attrname_,
attrvalue_,
attrvalue_size_);
}