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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-02-19 07:48:11 +08:00
|
|
|
#include <sys/stat.h>
|
2015-06-24 10:24:55 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-08-05 02:03:44 +08:00
|
|
|
#include <limits>
|
2015-06-24 10:24:55 +08:00
|
|
|
|
2016-03-01 07:02:37 +08:00
|
|
|
#include "fs.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_path.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
#include "policy.hpp"
|
2016-02-13 08:16:04 +08:00
|
|
|
#include "success_fail.hpp"
|
2015-06-24 10:24:55 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2016-03-01 07:02:37 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
_newest_create(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
|
|
|
{
|
|
|
|
time_t newest;
|
|
|
|
string fullpath;
|
|
|
|
const string *newestbasepath;
|
|
|
|
|
|
|
|
newest = std::numeric_limits<time_t>::min();
|
|
|
|
newestbasepath = NULL;
|
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
2016-03-01 11:58:13 +08:00
|
|
|
|
|
|
|
struct stat st;
|
2016-03-01 07:02:37 +08:00
|
|
|
const string *basepath = &basepaths[i];
|
|
|
|
|
|
|
|
fs::path::make(basepath,fusepath,fullpath);
|
|
|
|
|
|
|
|
if(!fs::exists(fullpath,st))
|
|
|
|
continue;
|
|
|
|
if(st.st_mtime < newest)
|
|
|
|
continue;
|
2016-03-01 11:58:13 +08:00
|
|
|
if(fs::readonly(*basepath))
|
2016-03-01 07:02:37 +08:00
|
|
|
continue;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
newest = st.st_mtime;
|
2016-03-01 07:02:37 +08:00
|
|
|
newestbasepath = basepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newestbasepath == NULL)
|
|
|
|
return POLICY_FAIL_ENOENT;
|
|
|
|
|
|
|
|
paths.push_back(newestbasepath);
|
|
|
|
|
|
|
|
return POLICY_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-07-01 11:35:28 +08:00
|
|
|
static
|
|
|
|
int
|
2016-03-01 11:58:13 +08:00
|
|
|
_newest_other(const vector<string> &basepaths,
|
|
|
|
const char *fusepath,
|
|
|
|
vector<const string*> &paths)
|
2015-07-01 11:35:28 +08:00
|
|
|
{
|
2016-01-30 05:54:39 +08:00
|
|
|
time_t newest;
|
2016-02-19 07:48:11 +08:00
|
|
|
string fullpath;
|
2016-02-13 08:16:04 +08:00
|
|
|
const string *newestbasepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-01-30 05:54:39 +08:00
|
|
|
newest = std::numeric_limits<time_t>::min();
|
2016-02-13 08:16:04 +08:00
|
|
|
newestbasepath = NULL;
|
2015-07-01 11:35:28 +08:00
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
{
|
2016-03-01 11:58:13 +08:00
|
|
|
struct stat st;
|
2016-01-30 05:54:39 +08:00
|
|
|
const string *basepath = &basepaths[i];
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
fs::path::make(basepath,fusepath,fullpath);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-19 07:48:11 +08:00
|
|
|
if(!fs::exists(fullpath,st))
|
|
|
|
continue;
|
|
|
|
if(st.st_mtime < newest)
|
|
|
|
continue;
|
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
newest = st.st_mtime;
|
2016-02-19 07:48:11 +08:00
|
|
|
newestbasepath = basepath;
|
2015-07-01 11:35:28 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
if(newestbasepath == NULL)
|
2016-03-01 07:02:37 +08:00
|
|
|
return POLICY_FAIL_ENOENT;
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
paths.push_back(newestbasepath);
|
2015-07-01 11:35:28 +08:00
|
|
|
|
2016-02-13 08:16:04 +08:00
|
|
|
return POLICY_SUCCESS;
|
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,
|
|
|
|
const vector<string> &basepaths,
|
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
|
|
|
{
|
2016-03-01 07:02:37 +08:00
|
|
|
if(type == Category::Enum::create)
|
|
|
|
return _newest_create(basepaths,fusepath,paths);
|
2016-02-19 07:48:11 +08:00
|
|
|
|
2016-03-01 11:58:13 +08:00
|
|
|
return _newest_other(basepaths,fusepath,paths);
|
2015-06-24 10:24:55 +08:00
|
|
|
}
|
|
|
|
}
|