2014-05-13 00:41:46 +08:00
|
|
|
/*
|
2016-01-15 05:50:22 +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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
|
|
|
|
#include <string>
|
2014-09-27 06:31:22 +08:00
|
|
|
#include <vector>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2016-05-11 23:22:00 +08:00
|
|
|
#include <fcntl.h>
|
2014-05-13 00:41:46 +08:00
|
|
|
#include <sys/ioctl.h>
|
2016-05-11 23:22:00 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2014-09-27 06:31:22 +08:00
|
|
|
#include "config.hpp"
|
2016-09-14 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fileinfo.hpp"
|
|
|
|
#include "fs_path.hpp"
|
2014-09-27 06:31:22 +08:00
|
|
|
#include "rwlock.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "ugid.hpp"
|
2014-09-27 06:31:22 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using namespace mergerfs;
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2015-09-15 10:11:52 +08:00
|
|
|
_ioctl(const int fd,
|
|
|
|
const int cmd,
|
|
|
|
void *data)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2015-08-05 21:18:34 +08:00
|
|
|
rv = ::ioctl(fd,cmd,data);
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
}
|
|
|
|
|
2014-11-05 02:42:20 +08:00
|
|
|
#ifdef FUSE_IOCTL_DIR
|
2015-08-05 21:18:34 +08:00
|
|
|
|
|
|
|
#ifndef O_NOATIME
|
|
|
|
#define O_NOATIME 0
|
|
|
|
#endif
|
|
|
|
|
2014-09-27 06:31:22 +08:00
|
|
|
static
|
|
|
|
int
|
2015-06-26 05:55:16 +08:00
|
|
|
_ioctl_dir_base(Policy::Func::Search searchFunc,
|
|
|
|
const vector<string> &srcmounts,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-01-30 05:54:39 +08:00
|
|
|
const char *fusepath,
|
2015-06-26 05:55:16 +08:00
|
|
|
const int cmd,
|
|
|
|
void *data)
|
2014-09-27 06:31:22 +08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rv;
|
2016-01-30 05:54:39 +08:00
|
|
|
string fullpath;
|
|
|
|
vector<const string*> basepaths;
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2016-01-30 05:54:39 +08:00
|
|
|
rv = searchFunc(srcmounts,fusepath,minfreespace,basepaths);
|
2014-09-27 06:31:22 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2016-01-30 05:54:39 +08:00
|
|
|
fs::path::make(basepaths[0],fusepath,fullpath);
|
2015-07-01 19:47:40 +08:00
|
|
|
|
2016-05-11 23:22:00 +08:00
|
|
|
const int flags = O_RDONLY | O_NOATIME | O_NONBLOCK;
|
2016-01-30 05:54:39 +08:00
|
|
|
fd = ::open(fullpath.c_str(),flags);
|
2014-09-27 06:31:22 +08:00
|
|
|
if(fd == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2015-08-05 21:18:34 +08:00
|
|
|
rv = _ioctl(fd,cmd,data);
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2015-07-01 19:47:40 +08:00
|
|
|
::close(fd);
|
2014-09-27 06:31:22 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2016-01-30 05:54:39 +08:00
|
|
|
_ioctl_dir(const char *fusepath,
|
|
|
|
const int cmd,
|
|
|
|
void *data)
|
2014-09-27 06:31:22 +08:00
|
|
|
{
|
2015-09-09 03:56:02 +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-09-27 06:31:22 +08:00
|
|
|
|
2015-06-26 05:55:16 +08:00
|
|
|
return _ioctl_dir_base(config.getattr,
|
2014-09-27 06:31:22 +08:00
|
|
|
config.srcmounts,
|
2015-06-17 10:12:55 +08:00
|
|
|
config.minfreespace,
|
2014-09-27 06:31:22 +08:00
|
|
|
fusepath,
|
|
|
|
cmd,
|
|
|
|
data);
|
|
|
|
}
|
2014-11-05 02:42:20 +08:00
|
|
|
#endif
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
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
|
|
|
ioctl(const char *fusepath,
|
|
|
|
int cmd,
|
|
|
|
void *arg,
|
|
|
|
fuse_file_info *ffi,
|
|
|
|
unsigned int flags,
|
|
|
|
void *data)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2014-11-05 02:42:20 +08:00
|
|
|
#ifdef FUSE_IOCTL_DIR
|
2014-09-27 06:31:22 +08:00
|
|
|
if(flags & FUSE_IOCTL_DIR)
|
|
|
|
return _ioctl_dir(fusepath,
|
|
|
|
cmd,
|
|
|
|
data);
|
2014-11-05 02:42:20 +08:00
|
|
|
#endif
|
2015-09-16 06:49:18 +08:00
|
|
|
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
return _ioctl(fi->fd,
|
2014-07-22 19:07:25 +08:00
|
|
|
cmd,
|
2014-05-13 00:41:46 +08:00
|
|
|
data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|