mergerfs/src/ioctl.cpp

143 lines
3.4 KiB
C++
Raw Normal View History

2014-05-12 12:41:46 -04: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>
#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
#include "config.hpp"
2017-04-11 08:53:46 -04:00
#include "dirinfo.hpp"
#include "errno.hpp"
#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"
#include "fs_path.hpp"
#include "rwlock.hpp"
#include "ugid.hpp"
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
#ifndef O_NOATIME
#define O_NOATIME 0
#endif
static
int
_ioctl_dir_base(Policy::Func::Search searchFunc,
const Branches &branches_,
const uint64_t minfreespace,
const char *fusepath,
2017-04-11 08:53:46 -04:00
const unsigned long cmd,
void *data)
{
int fd;
int rv;
string fullpath;
vector<const string*> basepaths;
rv = searchFunc(branches_,fusepath,minfreespace,basepaths);
if(rv == -1)
return -errno;
fs::path::make(basepaths[0],fusepath,fullpath);
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);
if(fd == -1)
return -errno;
rv = _ioctl(fd,cmd,data);
2016-10-19 09:28:06 -04:00
fs::close(fd);
return rv;
}
static
int
2017-04-11 08:53:46 -04:00
_ioctl_dir(fuse_file_info *ffi,
const unsigned long cmd,
void *data)
{
2017-04-11 08:53:46 -04:00
DirInfo *di = reinterpret_cast<DirInfo*>(ffi->fh);
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.branches_lock);
return _ioctl_dir_base(config.getattr,
config.branches,
2015-06-16 22:12:55 -04:00
config.minfreespace,
2017-04-11 08:53:46 -04:00
di->fusepath.c_str(),
cmd,
data);
}
2014-11-04 13:42:20 -05:00
#endif
2014-05-12 12:41:46 -04:00
namespace mergerfs
{
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
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
2017-04-11 08:53:46 -04:00
return ::_ioctl_file(ffi,cmd,data);
2014-05-12 12:41:46 -04:00
}
}
}