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_cor.hpp"
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "dirinfo.hpp"
|
|
|
|
#include "errno.hpp"
|
2023-09-25 06:55:09 +08:00
|
|
|
#include "fs_close.hpp"
|
2023-08-03 07:15:36 +08:00
|
|
|
#include "fs_devid.hpp"
|
2023-09-25 06:55:09 +08:00
|
|
|
#include "fs_getdents64.hpp"
|
2023-08-03 07:15:36 +08:00
|
|
|
#include "fs_inode.hpp"
|
2023-09-25 06:55:09 +08:00
|
|
|
#include "fs_open.hpp"
|
2023-08-03 07:15:36 +08:00
|
|
|
#include "fs_path.hpp"
|
|
|
|
#include "fs_readdir.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
|
|
|
#include "linux_dirent64.h"
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_,
|
|
|
|
unsigned max_queue_depth_)
|
|
|
|
: _tp(concurrency_,max_queue_depth_,"readdir.cor")
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FUSE::ReadDirCOR::~ReadDirCOR()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace l
|
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
struct Error
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
private:
|
|
|
|
int _err;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Error()
|
|
|
|
: _err(ENOENT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int()
|
|
|
|
{
|
|
|
|
return _err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error&
|
|
|
|
operator=(int v_)
|
|
|
|
{
|
|
|
|
if(_err != 0)
|
|
|
|
_err = v_;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
|
|
|
readdir(std::string basepath_,
|
|
|
|
HashSet &names_,
|
|
|
|
fuse_dirents_t *buf_,
|
2023-09-25 06:55:09 +08:00
|
|
|
std::mutex &mutex_)
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
|
|
|
int rv;
|
2023-09-25 06:55:09 +08:00
|
|
|
int dfd;
|
2023-08-03 07:15:36 +08:00
|
|
|
dev_t dev;
|
|
|
|
std::string filepath;
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
dfd = fs::open_dir_ro(basepath_);
|
|
|
|
if(dfd == -1)
|
|
|
|
return errno;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
DEFER{ fs::close(dfd); };
|
|
|
|
|
|
|
|
dev = fs::devid(dfd);
|
2023-08-03 07:15:36 +08:00
|
|
|
|
|
|
|
rv = 0;
|
2023-09-25 06:55:09 +08:00
|
|
|
for(;;)
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
2023-09-25 06:55:09 +08:00
|
|
|
long nread;
|
|
|
|
char buf[32 * 1024];
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
nread = fs::getdents_64(dfd,buf,sizeof(buf));
|
|
|
|
if(nread == -1)
|
|
|
|
return errno;
|
|
|
|
if(nread == 0)
|
|
|
|
break;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
linux_dirent64_t *d;
|
|
|
|
std::lock_guard<std::mutex> lk(mutex_);
|
|
|
|
for(long pos = 0; pos < nread; pos += d->reclen)
|
|
|
|
{
|
|
|
|
std::uint64_t namelen;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
d = (linux_dirent64_t*)&buf[pos];
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
namelen = DIRENT_NAMELEN(d);
|
|
|
|
|
|
|
|
rv = names_.put(d->name,namelen);
|
|
|
|
if(rv == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
filepath = fs::path::make(basepath_,d->name);
|
|
|
|
d->ino = fs::inode::calc(filepath,
|
|
|
|
DTTOIF(d->type),
|
|
|
|
dev,
|
|
|
|
d->ino);
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
rv = fuse_dirents_add_linux(buf_,d,namelen);
|
|
|
|
if(rv >= 0)
|
|
|
|
continue;
|
2023-08-03 07:15:36 +08:00
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-08-03 07:15:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2023-09-25 06:55:09 +08:00
|
|
|
int
|
2023-09-03 08:27:40 +08:00
|
|
|
concurrent_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
|
|
|
{
|
|
|
|
HashSet names;
|
2023-09-25 06:55:09 +08:00
|
|
|
std::mutex mutex;
|
2023-08-03 07:15:36 +08:00
|
|
|
std::vector<std::future<int>> futures;
|
|
|
|
|
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 = [&,dirname_,buf_,uid_,gid_]()
|
2023-08-03 07:15:36 +08:00
|
|
|
{
|
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
|
|
|
return l::readdir(basepath,names,buf_,mutex);
|
2023-08-03 07:15:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto rv = tp_.enqueue_task(func);
|
|
|
|
|
|
|
|
futures.emplace_back(std::move(rv));
|
|
|
|
}
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
Error error;
|
2023-08-03 07:15:36 +08:00
|
|
|
for(auto &future : futures)
|
2023-09-25 06:55:09 +08:00
|
|
|
error = future.get();
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
fuse_dirents_reset(buf_);
|
|
|
|
|
2023-09-25 06:55:09 +08:00
|
|
|
return l::concurrent_readdir(tp_,branches_,dirname_,buf_,uid_,gid_);
|
2023-08-03 07:15:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FUSE::ReadDirCOR::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
|
|
|
}
|