mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-21 23:46:26 -05:00
use getdents64 on linux
This commit is contained in:
parent
d119807adb
commit
62873d2d3b
15 changed files with 898 additions and 229 deletions
172
src/fuse_readdir_plus_linux.icpp
Normal file
172
src/fuse_readdir_plus_linux.icpp
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
Copyright (c) 2019, 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.
|
||||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include "config.hpp"
|
||||
#include "dirinfo.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_base_close.hpp"
|
||||
#include "fs_base_fstatat.hpp"
|
||||
#include "fs_base_open.hpp"
|
||||
#include "fs_base_stat.hpp"
|
||||
#include "fs_devid.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "mempools.hpp"
|
||||
#include "rwlock.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include <fuse.h>
|
||||
#include <fuse_dirents.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
struct linux_dirent64
|
||||
{
|
||||
uint64_t d_ino;
|
||||
int64_t d_off;
|
||||
uint16_t d_reclen;
|
||||
uint8_t d_type;
|
||||
char d_name[];
|
||||
};
|
||||
|
||||
namespace l
|
||||
{
|
||||
static
|
||||
inline
|
||||
int
|
||||
getdents64(unsigned int fd_,
|
||||
char *dirp_,
|
||||
unsigned int count_)
|
||||
{
|
||||
return syscall(SYS_getdents64,fd_,dirp_,count_);
|
||||
}
|
||||
|
||||
int
|
||||
close_free_ret_enomem(int fd_,
|
||||
void *buf_)
|
||||
{
|
||||
fs::close(fd_);
|
||||
g_DENTS_BUF_POOL.free(buf_);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
readdir_plus(const Branches &branches_,
|
||||
const char *dirname_,
|
||||
fuse_dirents_t *buf_)
|
||||
{
|
||||
int rv;
|
||||
dev_t dev;
|
||||
char *buf;
|
||||
HashSet names;
|
||||
string basepath;
|
||||
struct stat st;
|
||||
fuse_entry_t entry;
|
||||
struct linux_dirent64 *d;
|
||||
|
||||
buf = (char*)g_DENTS_BUF_POOL.alloc();
|
||||
|
||||
entry.nodeid = 0;
|
||||
entry.generation = 0;
|
||||
entry.entry_valid = 1;
|
||||
entry.attr_valid = 1;
|
||||
entry.entry_valid_nsec = 0;
|
||||
entry.attr_valid_nsec = 0;
|
||||
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
||||
{
|
||||
int dirfd;
|
||||
int64_t nread;
|
||||
uint64_t namelen;
|
||||
|
||||
basepath = fs::path::make(&branches_[i].path,dirname_);
|
||||
|
||||
dirfd = fs::open_dir_ro(basepath);
|
||||
if(dirfd == -1)
|
||||
continue;
|
||||
|
||||
dev = fs::devid(dirfd);
|
||||
if(dev == (dev_t)-1)
|
||||
dev = i;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
nread = l::getdents64(dirfd,buf,g_DENTS_BUF_POOL.size());
|
||||
if(nread == -1)
|
||||
break;
|
||||
if(nread == 0)
|
||||
break;
|
||||
|
||||
for(int64_t pos = 0; pos < nread;)
|
||||
{
|
||||
d = (struct linux_dirent64*)(buf + pos);
|
||||
namelen = (d->d_reclen - offsetof(struct linux_dirent64,d_name));
|
||||
|
||||
rv = names.put(d->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rv = fs::fstatat_nofollow(dirfd,d->d_name,&st);
|
||||
if(rv == -1)
|
||||
memset(&st,0,sizeof(st));
|
||||
|
||||
d->d_ino = fs::inode::recompute(d->d_ino,dev);
|
||||
st.st_ino = d->d_ino;
|
||||
|
||||
rv = fuse_dirents_add_linux_plus(buf_,d,&entry,&st);
|
||||
if(rv)
|
||||
return close_free_ret_enomem(dirfd,buf);
|
||||
|
||||
pos += d->d_reclen;
|
||||
}
|
||||
}
|
||||
|
||||
fs::close(dirfd);
|
||||
}
|
||||
|
||||
g_DENTS_BUF_POOL.free(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
namespace FUSE
|
||||
{
|
||||
int
|
||||
readdir_plus(fuse_file_info *ffi_,
|
||||
fuse_dirents_t *buf_)
|
||||
{
|
||||
DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
const Config &config = Config::get(fc);
|
||||
const ugid::Set ugid(fc->uid,fc->gid);
|
||||
const rwlock::ReadGuard readlock(&config.branches_lock);
|
||||
|
||||
return l::readdir_plus(config.branches,
|
||||
di->fusepath.c_str(),
|
||||
buf_);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue