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
|
|
|
*/
|
|
|
|
|
2014-09-27 06:31:22 +08:00
|
|
|
#include "config.hpp"
|
2017-04-11 20:53:46 +08:00
|
|
|
#include "dirinfo.hpp"
|
2016-09-14 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fileinfo.hpp"
|
2016-10-19 21:28:06 +08:00
|
|
|
#include "fs_base_close.hpp"
|
|
|
|
#include "fs_base_ioctl.hpp"
|
|
|
|
#include "fs_base_open.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#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
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
#include <fuse.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2014-09-27 06:31:22 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
namespace l
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2019-01-19 11:41:52 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
ioctl(const int fd_,
|
|
|
|
const unsigned long cmd_,
|
|
|
|
void *data_)
|
|
|
|
{
|
|
|
|
int rv;
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
rv = fs::ioctl(fd_,cmd_,data_);
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
}
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
ioctl_file(fuse_file_info *ffi_,
|
|
|
|
const unsigned long cmd_,
|
|
|
|
void *data_)
|
|
|
|
{
|
|
|
|
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi_->fh);
|
2017-04-11 20:53:46 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
return l::ioctl(fi->fd,cmd_,data_);
|
2019-01-19 11:41:52 +08:00
|
|
|
}
|
2017-04-11 20:53:46 +08:00
|
|
|
|
2015-08-05 21:18:34 +08:00
|
|
|
|
|
|
|
#ifndef O_NOATIME
|
|
|
|
#define O_NOATIME 0
|
|
|
|
#endif
|
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
ioctl_dir_base(Policy::Func::Search searchFunc_,
|
|
|
|
const Branches &branches_,
|
|
|
|
const uint64_t minfreespace_,
|
|
|
|
const char *fusepath_,
|
|
|
|
const unsigned long cmd_,
|
|
|
|
void *data_)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rv;
|
|
|
|
string fullpath;
|
|
|
|
vector<const string*> basepaths;
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
rv = searchFunc_(branches_,fusepath_,minfreespace_,basepaths);
|
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
fullpath = fs::path::make(basepaths[0],fusepath_);
|
2015-07-01 19:47:40 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
const int flags = O_RDONLY | O_NOATIME | O_NONBLOCK;
|
|
|
|
fd = fs::open(fullpath,flags);
|
|
|
|
if(fd == -1)
|
|
|
|
return -errno;
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
rv = l::ioctl(fd,cmd_,data_);
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
fs::close(fd);
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
return rv;
|
|
|
|
}
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-19 11:41:52 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
ioctl_dir(fuse_file_info *ffi_,
|
|
|
|
const unsigned long cmd_,
|
|
|
|
void *data_)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
return l::ioctl_dir_base(config.open,
|
|
|
|
config.branches,
|
|
|
|
config.minfreespace,
|
|
|
|
di->fusepath.c_str(),
|
|
|
|
cmd_,
|
|
|
|
data_);
|
2019-01-19 11:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-27 06:31:22 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
namespace FUSE
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2019-01-25 20:03:46 +08:00
|
|
|
int
|
|
|
|
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
|
|
|
{
|
2019-01-25 20:03:46 +08:00
|
|
|
if(flags_ & FUSE_IOCTL_DIR)
|
|
|
|
return l::ioctl_dir(ffi_,cmd_,data_);
|
|
|
|
|
|
|
|
|
|
|
|
return l::ioctl_file(ffi_,cmd_,data_);
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|
|
|
|
}
|