2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2016-01-14 16:50:22 -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.
|
2014-05-12 12:41:46 -04:00
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "fuse_rmdir.hpp"
|
|
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
#include "config.hpp"
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_path.hpp"
|
2020-06-21 14:45:14 -04:00
|
|
|
#include "fs_rmdir.hpp"
|
|
|
|
|
#include "fs_unlink.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include "fuse.h"
|
2019-01-06 12:52:55 -05:00
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2026-04-29 09:19:28 -05:00
|
|
|
// 1. First error sets the initial value
|
|
|
|
|
// 2. If we get -EEXIST or -ENOTEMPTY, it takes priority (overwrites anything)
|
|
|
|
|
// 3. If we already have -EEXIST or -ENOTEMPTY, keep them (don't overwrite)
|
|
|
|
|
// 4. Success (>= 0) only sets to 0 if we don't have a priority error
|
|
|
|
|
// 5. Any other error only overwrites if we don't have a priority error
|
|
|
|
|
struct RmdirErr
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::optional<int> _err;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
RmdirErr()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator int()
|
|
|
|
|
{
|
|
|
|
|
return (_err.has_value() ? _err.value() : -ENOENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RmdirErr&
|
|
|
|
|
operator=(int v_)
|
|
|
|
|
{
|
|
|
|
|
if(!_err.has_value())
|
|
|
|
|
_err = ((v_ >= 0) ? 0 : v_);
|
|
|
|
|
else if((v_ == -EEXIST) || (v_ == -ENOTEMPTY))
|
|
|
|
|
_err = v_;
|
|
|
|
|
else if((*_err == -EEXIST) || (*_err == -ENOTEMPTY))
|
|
|
|
|
;
|
|
|
|
|
else if(v_ >= 0)
|
|
|
|
|
_err = 0;
|
|
|
|
|
else
|
|
|
|
|
_err = v_;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
operator==(int v_)
|
|
|
|
|
{
|
|
|
|
|
if(_err.has_value())
|
|
|
|
|
return (_err.value() == v_);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-09-13 00:25:24 -04:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_should_unlink(int rv_,
|
|
|
|
|
FollowSymlinks followsymlinks_)
|
|
|
|
|
{
|
|
|
|
|
return ((rv_ == -ENOTDIR) &&
|
|
|
|
|
(followsymlinks_ != FollowSymlinks::ENUM::NEVER));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
2025-09-17 23:51:16 -04:00
|
|
|
_rmdir_core(const fs::path &basepath_,
|
|
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
const FollowSymlinks followsymlinks_)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
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
|
|
|
|
|
|
|
|
rv = fs::rmdir(fullpath);
|
|
|
|
|
if(::_should_unlink(rv,followsymlinks_))
|
|
|
|
|
rv = fs::unlink(fullpath);
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_rmdir_loop(const std::vector<Branch*> &branches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_,
|
2025-07-25 17:53:48 -05:00
|
|
|
const FollowSymlinks followsymlinks_)
|
2020-09-13 00:25:24 -04:00
|
|
|
{
|
2026-04-29 09:19:28 -05:00
|
|
|
RmdirErr err;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2025-08-05 00:39:52 -04:00
|
|
|
for(const auto &branch : branches_)
|
2025-07-25 17:53:48 -05:00
|
|
|
{
|
|
|
|
|
err = ::_rmdir_core(branch->path,fusepath_,followsymlinks_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
2020-09-13 00:25:24 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_rmdir(const Policy::Action &actionFunc_,
|
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 branches_,
|
2025-07-25 17:53:48 -05:00
|
|
|
const FollowSymlinks followsymlinks_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &fusepath_)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
int rv;
|
|
|
|
|
std::vector<Branch*> branches;
|
|
|
|
|
|
|
|
|
|
rv = actionFunc_(branches_,fusepath_,branches);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
return ::_rmdir_loop(branches,fusepath_,followsymlinks_);
|
2016-01-29 16:54:39 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
int
|
2025-10-13 00:21:36 -04:00
|
|
|
FUSE::rmdir(const fuse_req_ctx_t *ctx_,
|
|
|
|
|
const char *fusepath_)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2025-10-13 00:21:36 -04:00
|
|
|
const fs::path fusepath{fusepath_};
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2025-09-06 12:03:51 -04:00
|
|
|
return ::_rmdir(cfg.func.rmdir.policy,
|
|
|
|
|
cfg.branches,
|
|
|
|
|
cfg.follow_symlinks,
|
2025-09-17 23:51:16 -04:00
|
|
|
fusepath);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|