2020-02-25 07:14:00 +08:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2020-06-27 04:50:49 +08:00
|
|
|
#include "branch.hpp"
|
2020-02-25 07:14:00 +08:00
|
|
|
#include "errno.hpp"
|
2020-08-18 04:29:22 +08:00
|
|
|
#include "fs_closedir.hpp"
|
|
|
|
#include "fs_dirfd.hpp"
|
|
|
|
#include "fs_opendir.hpp"
|
|
|
|
#include "fs_readdir.hpp"
|
|
|
|
#include "fs_stat.hpp"
|
2020-02-25 07:14:00 +08:00
|
|
|
#include "fs_devid.hpp"
|
|
|
|
#include "fs_inode.hpp"
|
|
|
|
#include "fs_path.hpp"
|
|
|
|
#include "hashset.hpp"
|
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
#include <fuse_dirents.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace l
|
|
|
|
{
|
2020-03-13 02:42:08 +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
|
|
|
|
}
|
|
|
|
|
2020-02-25 07:14:00 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
readdir(const Branches &branches_,
|
|
|
|
const char *dirname_,
|
|
|
|
fuse_dirents_t *buf_)
|
|
|
|
{
|
|
|
|
dev_t dev;
|
|
|
|
HashSet names;
|
|
|
|
string basepath;
|
2020-06-23 01:03:48 +08:00
|
|
|
string fullpath;
|
2020-03-13 02:42:08 +08:00
|
|
|
uint64_t namelen;
|
2020-02-25 07:14:00 +08:00
|
|
|
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int dirfd;
|
|
|
|
DIR *dh;
|
|
|
|
|
2020-08-17 06:22:25 +08:00
|
|
|
basepath = fs::path::make(branches_[i].path,dirname_);
|
2020-02-25 07:14:00 +08:00
|
|
|
|
|
|
|
dh = fs::opendir(basepath);
|
|
|
|
if(!dh)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dirfd = fs::dirfd(dh);
|
|
|
|
dev = fs::devid(dirfd);
|
|
|
|
if(dev == (dev_t)-1)
|
|
|
|
dev = i;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
for(struct dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
|
|
|
|
{
|
2020-06-27 04:50:49 +08:00
|
|
|
namelen = l::dirent_exact_namelen(de);
|
2020-03-13 02:42:08 +08:00
|
|
|
|
|
|
|
rv = names.put(de->d_name,namelen);
|
2020-02-25 07:14:00 +08:00
|
|
|
if(rv == 0)
|
|
|
|
continue;
|
|
|
|
|
2020-06-23 01:03:48 +08:00
|
|
|
fullpath = fs::path::make(dirname_,de->d_name);
|
|
|
|
de->d_ino = fs::inode::calc(fullpath.c_str(),
|
|
|
|
fullpath.size(),
|
|
|
|
DTTOIF(de->d_type),
|
|
|
|
dev,
|
|
|
|
de->d_ino);
|
2020-02-25 07:14:00 +08:00
|
|
|
|
2020-03-13 02:42:08 +08:00
|
|
|
rv = fuse_dirents_add(buf_,de,namelen);
|
2020-02-25 07:14:00 +08:00
|
|
|
if(rv)
|
|
|
|
return (fs::closedir(dh),-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::closedir(dh);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace FUSE
|
|
|
|
{
|
|
|
|
int
|
2020-06-27 04:50:49 +08:00
|
|
|
readdir_posix(const Branches &branches_,
|
|
|
|
const char *dirname_,
|
|
|
|
fuse_dirents_t *buf_)
|
2020-02-25 07:14:00 +08:00
|
|
|
{
|
2020-06-27 04:50:49 +08:00
|
|
|
return l::readdir(branches_,dirname_,buf_);
|
2020-02-25 07:14:00 +08:00
|
|
|
}
|
|
|
|
}
|