2015-06-24 10:24:55 +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.
|
2015-06-24 10:24:55 +08:00
|
|
|
*/
|
|
|
|
|
2016-09-14 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2016-03-01 07:02:37 +08:00
|
|
|
#include "fs.hpp"
|
2018-10-14 11:48:30 +08:00
|
|
|
#include "fs_exists.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_path.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
#include "policy.hpp"
|
2018-10-21 11:40:02 +08:00
|
|
|
#include "policy_error.hpp"
|
2018-10-14 11:48:30 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2015-06-24 10:24:55 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
namespace newest
|
2016-03-01 07:02:37 +08:00
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
create(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
bool readonly;
|
|
|
|
time_t newest;
|
|
|
|
struct stat st;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *newestbasepath;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
newest = std::numeric_limits<time_t>::min();
|
|
|
|
newestbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
|
|
|
if(!fs::exists(branch->path,fusepath,st))
|
|
|
|
error_and_continue(error,ENOENT);
|
2018-11-02 11:37:45 +08:00
|
|
|
if(branch->ro_or_nc())
|
2018-10-21 11:40:02 +08:00
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
if(st.st_mtime < newest)
|
|
|
|
continue;
|
|
|
|
rv = fs::readonly(&branch->path,&readonly);
|
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
|
|
|
newest = st.st_mtime;
|
|
|
|
newestbasepath = &branch->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newestbasepath == NULL)
|
|
|
|
return (errno=error,-1);
|
|
|
|
|
|
|
|
paths.push_back(newestbasepath);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-01 07:02:37 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
action(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int error;
|
|
|
|
bool readonly;
|
|
|
|
time_t newest;
|
|
|
|
struct stat st;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *newestbasepath;
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
newest = std::numeric_limits<time_t>::min();
|
|
|
|
newestbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
|
|
|
|
|
|
|
if(!fs::exists(branch->path,fusepath,st))
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(branch->ro())
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
if(st.st_mtime < newest)
|
|
|
|
continue;
|
|
|
|
rv = fs::readonly(&branch->path,&readonly);
|
|
|
|
if(rv == -1)
|
|
|
|
error_and_continue(error,ENOENT);
|
|
|
|
if(readonly)
|
|
|
|
error_and_continue(error,EROFS);
|
|
|
|
|
|
|
|
newest = st.st_mtime;
|
|
|
|
newestbasepath = &branch->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newestbasepath == NULL)
|
|
|
|
return (errno=error,-1);
|
|
|
|
|
|
|
|
paths.push_back(newestbasepath);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
search(const Branches &branches_,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
time_t newest;
|
|
|
|
struct stat st;
|
|
|
|
const Branch *branch;
|
|
|
|
const string *newestbasepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
newest = std::numeric_limits<time_t>::min();
|
|
|
|
newestbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
|
|
|
|
{
|
|
|
|
branch = &branches_[i];
|
2016-02-19 07:48:11 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
if(!fs::exists(branch->path,fusepath,st))
|
|
|
|
continue;
|
|
|
|
if(st.st_mtime < newest)
|
|
|
|
continue;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
newest = st.st_mtime;
|
|
|
|
newestbasepath = &branch->path;
|
|
|
|
}
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
if(newestbasepath == NULL)
|
|
|
|
return (errno=ENOENT,-1);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
paths.push_back(newestbasepath);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:24:55 +08:00
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
int
|
2015-06-26 05:55:16 +08:00
|
|
|
Policy::Func::newest(const Category::Enum::Type type,
|
2018-10-21 11:40:02 +08:00
|
|
|
const Branches &branches_,
|
2016-01-30 05:54:39 +08:00
|
|
|
const char *fusepath,
|
2016-07-11 00:44:29 +08:00
|
|
|
const uint64_t minfreespace,
|
2016-01-30 05:54:39 +08:00
|
|
|
vector<const string*> &paths)
|
2015-06-24 10:24:55 +08:00
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Category::Enum::create:
|
|
|
|
return newest::create(branches_,fusepath,paths);
|
|
|
|
case Category::Enum::action:
|
|
|
|
return newest::action(branches_,fusepath,paths);
|
|
|
|
case Category::Enum::search:
|
|
|
|
default:
|
|
|
|
return newest::search(branches_,fusepath,paths);
|
|
|
|
}
|
2015-06-24 10:24:55 +08:00
|
|
|
}
|
|
|
|
}
|