2023-08-03 07:15:36 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2023, 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_readdir_cosr.hpp"
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "dirinfo.hpp"
|
|
|
|
#include "errno.hpp"
|
|
|
|
#include "fs_closedir.hpp"
|
|
|
|
#include "fs_devid.hpp"
|
|
|
|
#include "fs_dirfd.hpp"
|
|
|
|
#include "fs_inode.hpp"
|
|
|
|
#include "fs_opendir.hpp"
|
|
|
|
#include "fs_path.hpp"
|
|
|
|
#include "fs_readdir.hpp"
|
|
|
|
#include "fs_stat.hpp"
|
|
|
|
#include "hashset.hpp"
|
2023-09-25 06:55:09 +08:00
|
|
|
#include "scope_guard.hpp"
|
2023-08-03 07:15:36 +08:00
|
|
|
#include "ugid.hpp"
|
|
|
|
|
|
|
|
#include "fuse_dirents.h"
|
|
|
|
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
FUSE::ReadDirCOSR::ReadDirCOSR(unsigned concurrency_,
|
|
|
|
unsigned max_queue_depth_)
|
|
|
|
: _tp(concurrency_,max_queue_depth_,"readdir.cosr")
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FUSE::ReadDirCOSR::~ReadDirCOSR()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace l
|
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
struct DirRV
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
int err;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Error
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
int _err;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Error()
|
|
|
|
: _err(ENOENT)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int()
|
|
|
|
{
|
|
|
|
return _err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error&
|
|
|
|
operator=(int const v_)
|
|
|
|
{
|
|
|
|
if(_err != 0)
|
|
|
|
_err = v_;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-03 07:15:36 +08:00
|
|
|
static
|
|
|
|
uint64_t
|
|
|
|
dirent_exact_namelen(const struct dirent *d_)
|
|
|
|
{
|
|
|
|
#ifdef _D_EXACT_NAMLEN
|
|
|
|
return _D_EXACT_NAMLEN(d_);
|
|
|
|
#elif defined _DIRENT_HAVE_D_NAMLEN
|
|
|
|
return d_->d_namlen;
|
|
|
|
#else
|
|
|
|
return strlen(d_->d_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
2023-09-25 06:55:09 +08:00
|
|
|
std::vector<std::future<DirRV>>
|
2023-09-03 08:27:40 +08:00
|
|
|
opendir(ThreadPool &tp_,
|
2023-08-03 07:15:36 +08:00
|
|
|
const Branches::CPtr &branches_,
|
2023-08-21 12:06:05 +08:00
|
|
|
char const *dirname_,
|
|
|
|
uid_t const uid_,
|
|
|
|
gid_t const gid_)
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
std::vector<std::future<DirRV>> futures;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
futures.reserve(branches_->size());
|
2023-08-03 07:15:36 +08:00
|
|
|
for(auto const &branch : *branches_)
|
|
|
|
{
|
2023-08-21 12:06:05 +08:00
|
|
|
auto func = [&branch,dirname_,uid_,gid_]()
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
DirRV rv;
|
2023-08-21 12:06:05 +08:00
|
|
|
std::string basepath;
|
|
|
|
ugid::Set const ugid(uid_,gid_);
|
|
|
|
|
|
|
|
basepath = fs::path::make(branch.path,dirname_);
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
errno = 0;
|
|
|
|
rv.dir = fs::opendir(basepath);
|
|
|
|
rv.err = errno;
|
|
|
|
|
|
|
|
return rv;
|
2023-08-03 07:15:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto rv = tp_.enqueue_task(func);
|
|
|
|
|
|
|
|
futures.emplace_back(std::move(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
return futures;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2023-09-25 06:55:09 +08:00
|
|
|
readdir(std::vector<std::future<DirRV>> &dh_futures_,
|
|
|
|
char const *dirname_,
|
|
|
|
fuse_dirents_t *buf_)
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
Error error;
|
2023-08-03 07:15:36 +08:00
|
|
|
HashSet names;
|
|
|
|
std::string fullpath;
|
|
|
|
|
|
|
|
for(auto &dh_future : dh_futures_)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
dev_t dev;
|
2023-09-25 06:55:09 +08:00
|
|
|
DirRV dirrv;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
dirrv = dh_future.get();
|
|
|
|
error = dirrv.err;
|
|
|
|
if(dirrv.dir == NULL)
|
2023-08-03 07:15:36 +08:00
|
|
|
continue;
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
DEFER { fs::closedir(dirrv.dir); };
|
|
|
|
|
|
|
|
dev = fs::devid(dirrv.dir);
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
rv = 0;
|
2023-09-25 06:55:09 +08:00
|
|
|
for(dirent *de = fs::readdir(dirrv.dir); de && !rv; de = fs::readdir(dirrv.dir))
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
|
|
|
std::uint64_t namelen;
|
|
|
|
|
|
|
|
namelen = l::dirent_exact_namelen(de);
|
|
|
|
|
|
|
|
rv = names.put(de->d_name,namelen);
|
|
|
|
if(rv == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fullpath = fs::path::make(dirname_,de->d_name);
|
|
|
|
de->d_ino = fs::inode::calc(fullpath,
|
|
|
|
DTTOIF(de->d_type),
|
|
|
|
dev,
|
|
|
|
de->d_ino);
|
|
|
|
|
|
|
|
rv = fuse_dirents_add(buf_,de,namelen);
|
|
|
|
if(rv == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
error = ENOMEM;
|
2023-08-03 07:15:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
return -error;
|
2023-08-03 07:15:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2023-09-03 08:27:40 +08:00
|
|
|
readdir(ThreadPool &tp_,
|
2023-08-03 07:15:36 +08:00
|
|
|
const Branches::CPtr &branches_,
|
|
|
|
const char *dirname_,
|
2023-08-21 12:06:05 +08:00
|
|
|
fuse_dirents_t *buf_,
|
|
|
|
uid_t const uid_,
|
|
|
|
gid_t const gid_)
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
|
|
|
int rv;
|
2023-09-25 06:55:09 +08:00
|
|
|
std::vector<std::future<DirRV>> futures;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
fuse_dirents_reset(buf_);
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
futures = l::opendir(tp_,branches_,dirname_,uid_,gid_);
|
|
|
|
rv = l::readdir(futures,dirname_,buf_);
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FUSE::ReadDirCOSR::operator()(fuse_file_info_t const *ffi_,
|
|
|
|
fuse_dirents_t *buf_)
|
|
|
|
{
|
|
|
|
Config::Read cfg;
|
|
|
|
DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
|
|
|
|
const fuse_context *fc = fuse_get_context();
|
|
|
|
|
2023-08-21 12:06:05 +08:00
|
|
|
return l::readdir(_tp,
|
|
|
|
cfg->branches,
|
|
|
|
di->fusepath.c_str(),
|
|
|
|
buf_,
|
|
|
|
fc->uid,
|
|
|
|
fc->gid);
|
2023-08-03 07:15:36 +08:00
|
|
|
}
|