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>
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-01-14 16:50:22 -05:00
|
|
|
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.
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-01-14 16:50:22 -05:00
|
|
|
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 <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "config.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fileinfo.hpp"
|
|
|
|
|
#include "fs_path.hpp"
|
|
|
|
|
#include "fs_clonepath.hpp"
|
2014-08-03 15:50:28 -04:00
|
|
|
#include "rwlock.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "ugid.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using mergerfs::Policy;
|
2015-09-08 15:56:02 -04:00
|
|
|
using namespace mergerfs;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
2015-06-25 17:55:16 -04:00
|
|
|
_create(Policy::Func::Search searchFunc,
|
|
|
|
|
Policy::Func::Create createFunc,
|
|
|
|
|
const vector<string> &srcmounts,
|
|
|
|
|
const size_t minfreespace,
|
|
|
|
|
const string &fusepath,
|
|
|
|
|
const mode_t mode,
|
|
|
|
|
const int flags,
|
|
|
|
|
uint64_t &fh)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
|
int fd;
|
2014-08-16 13:59:55 -04:00
|
|
|
int rv;
|
2014-05-12 12:41:46 -04:00
|
|
|
string dirname;
|
2015-07-01 07:47:40 -04:00
|
|
|
vector<string> createpath;
|
|
|
|
|
vector<string> existingpath;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-07-01 07:47:40 -04:00
|
|
|
dirname = fs::path::dirname(fusepath);
|
2015-06-16 22:12:55 -04:00
|
|
|
rv = searchFunc(srcmounts,dirname,minfreespace,existingpath);
|
2014-08-16 13:59:55 -04:00
|
|
|
if(rv == -1)
|
|
|
|
|
return -errno;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-06-16 22:12:55 -04:00
|
|
|
rv = createFunc(srcmounts,dirname,minfreespace,createpath);
|
2014-08-16 13:59:55 -04:00
|
|
|
if(rv == -1)
|
|
|
|
|
return -errno;
|
2014-06-17 19:12:09 -04:00
|
|
|
|
2015-07-01 07:47:40 -04:00
|
|
|
if(createpath[0] != existingpath[0])
|
2015-01-29 00:24:57 -05:00
|
|
|
{
|
2015-09-08 15:56:02 -04:00
|
|
|
const ugid::SetRootGuard ugidGuard;
|
2015-07-01 07:47:40 -04:00
|
|
|
fs::clonepath(existingpath[0],createpath[0],dirname);
|
2015-01-29 00:24:57 -05:00
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-07-01 07:47:40 -04:00
|
|
|
fs::path::append(createpath[0],fusepath);
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-07-01 07:47:40 -04:00
|
|
|
fd = ::open(createpath[0].c_str(),flags,mode);
|
2014-05-12 12:41:46 -04:00
|
|
|
if(fd == -1)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
2015-09-15 18:49:18 -04:00
|
|
|
fh = reinterpret_cast<uint64_t>(new FileInfo(fd));
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
create(const char *fusepath,
|
|
|
|
|
mode_t mode,
|
2015-09-15 18:49:18 -04:00
|
|
|
fuse_file_info *ffi)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
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);
|
|
|
|
|
const rwlock::ReadGuard readlock(&config.srcmountslock);
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-06-25 17:55:16 -04:00
|
|
|
return _create(config.getattr,
|
|
|
|
|
config.create,
|
2014-05-12 12:41:46 -04:00
|
|
|
config.srcmounts,
|
2015-06-16 22:12:55 -04:00
|
|
|
config.minfreespace,
|
2014-05-12 12:41:46 -04:00
|
|
|
fusepath,
|
2014-06-17 19:12:09 -04:00
|
|
|
(mode & ~fc->umask),
|
2015-09-15 18:49:18 -04:00
|
|
|
ffi->flags,
|
|
|
|
|
ffi->fh);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|