2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2016-01-14 16:50:22 -05: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-12 12:41:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
|
|
|
|
#include <string>
|
2014-09-26 18:31:22 -04:00
|
|
|
#include <vector>
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-05-11 11:22:00 -04:00
|
|
|
#include <fcntl.h>
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2014-09-26 18:31:22 -04:00
|
|
|
#include "config.hpp"
|
2017-04-11 08:53:46 -04:00
|
|
|
#include "dirinfo.hpp"
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fileinfo.hpp"
|
2016-10-19 09:28:06 -04:00
|
|
|
#include "fs_base_close.hpp"
|
|
|
|
#include "fs_base_ioctl.hpp"
|
|
|
|
#include "fs_base_open.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_path.hpp"
|
2014-09-26 18:31:22 -04:00
|
|
|
#include "rwlock.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "ugid.hpp"
|
2014-09-26 18:31:22 -04:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using namespace mergerfs;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2017-04-11 08:53:46 -04:00
|
|
|
_ioctl(const int fd,
|
|
|
|
const unsigned long cmd,
|
|
|
|
void *data)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2016-10-19 09:28:06 -04:00
|
|
|
rv = fs::ioctl(fd,cmd,data);
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
}
|
|
|
|
|
2017-04-11 08:53:46 -04:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_ioctl_file(fuse_file_info *ffi,
|
|
|
|
const unsigned long cmd,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
|
|
|
|
|
|
|
|
return _ioctl(fi->fd,cmd,data);
|
|
|
|
}
|
|
|
|
|
2014-11-04 13:42:20 -05:00
|
|
|
#ifdef FUSE_IOCTL_DIR
|
2015-08-05 09:18:34 -04:00
|
|
|
|
|
|
|
#ifndef O_NOATIME
|
|
|
|
#define O_NOATIME 0
|
|
|
|
#endif
|
|
|
|
|
2014-09-26 18:31:22 -04:00
|
|
|
static
|
|
|
|
int
|
2015-06-25 17:55:16 -04:00
|
|
|
_ioctl_dir_base(Policy::Func::Search searchFunc,
|
2018-10-20 23:40:02 -04:00
|
|
|
const Branches &branches_,
|
2016-07-10 12:44:29 -04:00
|
|
|
const uint64_t minfreespace,
|
2016-01-29 16:54:39 -05:00
|
|
|
const char *fusepath,
|
2017-04-11 08:53:46 -04:00
|
|
|
const unsigned long cmd,
|
2015-06-25 17:55:16 -04:00
|
|
|
void *data)
|
2014-09-26 18:31:22 -04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rv;
|
2016-01-29 16:54:39 -05:00
|
|
|
string fullpath;
|
|
|
|
vector<const string*> basepaths;
|
2014-09-26 18:31:22 -04:00
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
rv = searchFunc(branches_,fusepath,minfreespace,basepaths);
|
2014-09-26 18:31:22 -04:00
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2016-01-29 16:54:39 -05:00
|
|
|
fs::path::make(basepaths[0],fusepath,fullpath);
|
2015-07-01 07:47:40 -04:00
|
|
|
|
2016-05-11 11:22:00 -04:00
|
|
|
const int flags = O_RDONLY | O_NOATIME | O_NONBLOCK;
|
2016-10-19 09:28:06 -04:00
|
|
|
fd = fs::open(fullpath,flags);
|
2014-09-26 18:31:22 -04:00
|
|
|
if(fd == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2015-08-05 09:18:34 -04:00
|
|
|
rv = _ioctl(fd,cmd,data);
|
2014-09-26 18:31:22 -04:00
|
|
|
|
2016-10-19 09:28:06 -04:00
|
|
|
fs::close(fd);
|
2014-09-26 18:31:22 -04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2017-04-11 08:53:46 -04:00
|
|
|
_ioctl_dir(fuse_file_info *ffi,
|
|
|
|
const unsigned long cmd,
|
|
|
|
void *data)
|
2014-09-26 18:31:22 -04:00
|
|
|
{
|
2017-04-11 08:53:46 -04:00
|
|
|
DirInfo *di = reinterpret_cast<DirInfo*>(ffi->fh);
|
2015-09-08 15:56:02 -04:00
|
|
|
const fuse_context *fc = fuse_get_context();
|
|
|
|
const Config &config = Config::get(fc);
|
|
|
|
const ugid::Set ugid(fc->uid,fc->gid);
|
2018-10-20 23:40:02 -04:00
|
|
|
const rwlock::ReadGuard readlock(&config.branches_lock);
|
2014-09-26 18:31:22 -04:00
|
|
|
|
2015-06-25 17:55:16 -04:00
|
|
|
return _ioctl_dir_base(config.getattr,
|
2018-10-20 23:40:02 -04:00
|
|
|
config.branches,
|
2015-06-16 22:12:55 -04:00
|
|
|
config.minfreespace,
|
2017-04-11 08:53:46 -04:00
|
|
|
di->fusepath.c_str(),
|
2014-09-26 18:31:22 -04:00
|
|
|
cmd,
|
|
|
|
data);
|
|
|
|
}
|
2014-11-04 13:42:20 -05:00
|
|
|
#endif
|
2014-09-26 18:31:22 -04:00
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
namespace mergerfs
|
|
|
|
{
|
2015-07-15 10:04:57 -04:00
|
|
|
namespace fuse
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
int
|
2015-07-21 14:19:17 -04:00
|
|
|
ioctl(const char *fusepath,
|
|
|
|
int cmd,
|
|
|
|
void *arg,
|
|
|
|
fuse_file_info *ffi,
|
|
|
|
unsigned int flags,
|
|
|
|
void *data)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2014-11-04 13:42:20 -05:00
|
|
|
#ifdef FUSE_IOCTL_DIR
|
2014-09-26 18:31:22 -04:00
|
|
|
if(flags & FUSE_IOCTL_DIR)
|
2017-04-11 08:53:46 -04:00
|
|
|
return ::_ioctl_dir(ffi,cmd,data);
|
2014-11-04 13:42:20 -05:00
|
|
|
#endif
|
2014-09-26 18:31:22 -04:00
|
|
|
|
2017-04-11 08:53:46 -04:00
|
|
|
return ::_ioctl_file(ffi,cmd,data);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|