2014-05-13 00:41:46 +08:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-11-10 20:41:15 +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 "readdir.hpp"
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "ugid.hpp"
|
|
|
|
#include "fs.hpp"
|
2014-08-04 03:50:28 +08:00
|
|
|
#include "rwlock.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,
|
2014-11-11 10:01:20 +08:00
|
|
|
const string &dirname,
|
2014-05-13 00:41:46 +08:00
|
|
|
void *buf,
|
|
|
|
const fuse_fill_dir_t filler)
|
|
|
|
{
|
|
|
|
set<string> found;
|
2014-11-10 20:41:15 +08:00
|
|
|
struct stat st = {0};
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2015-07-01 11:35:28 +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;
|
|
|
|
|
2015-07-01 19:47:40 +08:00
|
|
|
basepath = fs::path::make(srcmounts[i],dirname);
|
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))
|
|
|
|
{
|
2014-11-10 20:41:15 +08:00
|
|
|
if(found.insert(de->d_name).second == false)
|
2014-05-13 00:41:46 +08:00
|
|
|
continue;
|
|
|
|
|
2014-11-10 20:41:15 +08:00
|
|
|
st.st_mode = DTTOIF(de->d_type);
|
|
|
|
if(filler(buf,de->d_name,&st,NO_OFFSET) != 0)
|
2014-05-29 06:04:29 +08:00
|
|
|
return -ENOMEM;
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
::closedir(dh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found.empty())
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mergerfs
|
|
|
|
{
|
2015-07-15 22:04:57 +08:00
|
|
|
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
|
|
|
{
|
2015-07-22 02:19:17 +08:00
|
|
|
const fuse_context *fc = fuse_get_context();
|
|
|
|
const Config &config = Config::get(fc);
|
2014-06-18 07:12:09 +08:00
|
|
|
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
|
2014-08-04 03:50:28 +08:00
|
|
|
const rwlock::ReadGuard readlock(&config.srcmountslock);
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
return _readdir(config.srcmounts,
|
|
|
|
fusepath,
|
|
|
|
buf,
|
|
|
|
filler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|