mergerfs/src/readdir.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

2014-05-13 00:41:46 +08:00
/*
Copyright (c) 2016, 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.
2014-05-13 00:41:46 +08:00
*/
#define _BSD_SOURCE
2014-05-13 00:41:46 +08:00
#include <fuse.h>
#include <string>
#include <set>
#include <vector>
#include <errno.h>
#include <dirent.h>
#include "config.hpp"
#include "fs_path.hpp"
#include "readdir.hpp"
#include "rwlock.hpp"
#include "ugid.hpp"
2014-05-13 00:41:46 +08:00
using std::string;
using std::vector;
using std::set;
using std::pair;
#define NO_OFFSET 0
static
int
_readdir(const vector<string> &srcmounts,
const char *dirname,
2014-05-13 00:41:46 +08:00
void *buf,
const fuse_fill_dir_t filler)
{
set<string> found;
struct stat st = {0};
2014-05-13 00:41:46 +08:00
for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
2014-05-13 00:41:46 +08:00
{
DIR *dh;
string basepath;
fs::path::make(&srcmounts[i],dirname,basepath);
2014-05-13 00:41:46 +08:00
dh = ::opendir(basepath.c_str());
if(!dh)
continue;
for(struct dirent *de = ::readdir(dh); de != NULL; de = ::readdir(dh))
{
if(found.insert(de->d_name).second == false)
2014-05-13 00:41:46 +08:00
continue;
st.st_mode = DTTOIF(de->d_type);
if(filler(buf,de->d_name,&st,NO_OFFSET) != 0)
return -ENOMEM;
2014-05-13 00:41:46 +08:00
}
::closedir(dh);
}
if(found.empty())
return -ENOENT;
return 0;
}
namespace mergerfs
{
namespace fuse
2014-05-13 00:41:46 +08:00
{
int
2015-07-22 02:19:17 +08:00
readdir(const char *fusepath,
void *buf,
fuse_fill_dir_t filler,
off_t offset,
fuse_file_info *fi)
2014-05-13 00:41:46 +08:00
{
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.srcmountslock);
2014-05-13 00:41:46 +08:00
return _readdir(config.srcmounts,
fusepath,
buf,
filler);
}
}
}