2019-01-25 20:03:46 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "errno.hpp"
|
|
|
|
#include "fs_acl.hpp"
|
|
|
|
#include "fs_clonepath.hpp"
|
2020-08-18 04:29:22 +08:00
|
|
|
#include "fs_mkdir.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fs_path.hpp"
|
2020-12-20 04:06:08 +08:00
|
|
|
#include "policy.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "ugid.hpp"
|
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
#include "fuse.h"
|
2019-09-24 22:27:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
using std::string;
|
2020-12-20 04:06:08 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
|
2020-09-13 12:25:24 +08:00
|
|
|
namespace error
|
|
|
|
{
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
|
|
|
calc(const int rv_,
|
|
|
|
const int prev_,
|
|
|
|
const int cur_)
|
|
|
|
{
|
|
|
|
if(rv_ == -1)
|
|
|
|
{
|
|
|
|
if(prev_ == 0)
|
|
|
|
return 0;
|
|
|
|
return cur_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
namespace l
|
|
|
|
{
|
|
|
|
static
|
|
|
|
int
|
|
|
|
mkdir_core(const string &fullpath_,
|
|
|
|
mode_t mode_,
|
|
|
|
const mode_t umask_)
|
|
|
|
{
|
|
|
|
if(!fs::acl::dir_has_defaults(fullpath_))
|
|
|
|
mode_ &= ~umask_;
|
|
|
|
|
|
|
|
return fs::mkdir(fullpath_,mode_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
mkdir_loop_core(const string &createpath_,
|
|
|
|
const char *fusepath_,
|
|
|
|
const mode_t mode_,
|
|
|
|
const mode_t umask_,
|
|
|
|
const int error_)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
string fullpath;
|
|
|
|
|
|
|
|
fullpath = fs::path::make(createpath_,fusepath_);
|
|
|
|
|
|
|
|
rv = l::mkdir_core(fullpath,mode_,umask_);
|
|
|
|
|
|
|
|
return error::calc(rv,error_,errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2020-12-20 04:06:08 +08:00
|
|
|
mkdir_loop(const string &existingpath_,
|
|
|
|
const StrVec &createpaths_,
|
|
|
|
const char *fusepath_,
|
|
|
|
const string &fusedirpath_,
|
|
|
|
const mode_t mode_,
|
|
|
|
const mode_t umask_)
|
2019-01-25 20:03:46 +08:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = -1;
|
|
|
|
for(size_t i = 0, ei = createpaths_.size(); i != ei; i++)
|
|
|
|
{
|
2019-09-24 22:27:46 +08:00
|
|
|
rv = fs::clonepath_as_root(existingpath_,createpaths_[i],fusedirpath_);
|
2019-01-25 20:03:46 +08:00
|
|
|
if(rv == -1)
|
|
|
|
error = error::calc(rv,error,errno);
|
|
|
|
else
|
2019-09-24 22:27:46 +08:00
|
|
|
error = l::mkdir_loop_core(createpaths_[i],
|
2019-01-25 20:03:46 +08:00
|
|
|
fusepath_,
|
|
|
|
mode_,
|
|
|
|
umask_,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
2020-12-20 04:06:08 +08:00
|
|
|
mkdir(const Policy::Search &getattrPolicy_,
|
|
|
|
const Policy::Create &mkdirPolicy_,
|
2019-01-25 20:03:46 +08:00
|
|
|
const Branches &branches_,
|
|
|
|
const char *fusepath_,
|
|
|
|
const mode_t mode_,
|
|
|
|
const mode_t umask_)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
string fusedirpath;
|
2020-12-20 04:06:08 +08:00
|
|
|
StrVec createpaths;
|
|
|
|
StrVec existingpaths;
|
2019-01-25 20:03:46 +08:00
|
|
|
|
|
|
|
fusedirpath = fs::path::dirname(fusepath_);
|
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
rv = getattrPolicy_(branches_,fusedirpath.c_str(),&existingpaths);
|
2019-01-25 20:03:46 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
rv = mkdirPolicy_(branches_,fusedirpath.c_str(),&createpaths);
|
2019-01-25 20:03:46 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
return l::mkdir_loop(existingpaths[0],
|
2019-01-25 20:03:46 +08:00
|
|
|
createpaths,
|
|
|
|
fusepath_,
|
|
|
|
fusedirpath,
|
|
|
|
mode_,
|
|
|
|
umask_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace FUSE
|
|
|
|
{
|
|
|
|
int
|
|
|
|
mkdir(const char *fusepath_,
|
|
|
|
mode_t mode_)
|
|
|
|
{
|
2023-07-10 11:42:47 +08:00
|
|
|
int rv;
|
2020-12-20 04:06:08 +08:00
|
|
|
Config::Read cfg;
|
|
|
|
const fuse_context *fc = fuse_get_context();
|
2019-09-24 22:27:46 +08:00
|
|
|
const ugid::Set ugid(fc->uid,fc->gid);
|
2019-01-25 20:03:46 +08:00
|
|
|
|
2023-07-10 11:42:47 +08:00
|
|
|
rv = l::mkdir(cfg->func.getattr.policy,
|
|
|
|
cfg->func.mkdir.policy,
|
|
|
|
cfg->branches,
|
|
|
|
fusepath_,
|
|
|
|
mode_,
|
|
|
|
fc->umask);
|
|
|
|
if(rv == -EROFS)
|
|
|
|
{
|
|
|
|
Config::Write()->branches.find_and_set_mode_ro();
|
|
|
|
rv = l::mkdir(cfg->func.getattr.policy,
|
|
|
|
cfg->func.mkdir.policy,
|
|
|
|
cfg->branches,
|
|
|
|
fusepath_,
|
|
|
|
mode_,
|
|
|
|
fc->umask);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2019-01-25 20:03:46 +08:00
|
|
|
}
|
|
|
|
}
|