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 <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2015-09-15 18:49:18 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <limits.h>
|
2016-07-10 12:44:29 -04:00
|
|
|
#include <stdint.h>
|
2015-09-15 18:49:18 -04:00
|
|
|
#include <stdlib.h>
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_attr.hpp"
|
2016-10-20 16:51:49 -04:00
|
|
|
#include "fs_base_realpath.hpp"
|
2016-10-19 09:28:06 -04:00
|
|
|
#include "fs_base_stat.hpp"
|
2018-10-13 23:48:30 -04:00
|
|
|
#include "fs_exists.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_path.hpp"
|
2018-12-12 10:08:17 -05:00
|
|
|
#include "fs_statvfs_cache.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_xattr.hpp"
|
2014-08-03 15:50:28 -04:00
|
|
|
#include "str.hpp"
|
2014-05-12 12:41:46 -04:00
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
|
namespace fs
|
|
|
|
|
{
|
2015-02-07 18:27:47 -05:00
|
|
|
void
|
2018-10-20 23:40:02 -04:00
|
|
|
findallfiles(const vector<string> &basepaths,
|
2016-01-29 16:54:39 -05:00
|
|
|
const char *fusepath,
|
2015-02-07 18:27:47 -05:00
|
|
|
vector<string> &paths)
|
|
|
|
|
{
|
2016-01-29 16:54:39 -05:00
|
|
|
string fullpath;
|
|
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
2015-02-07 18:27:47 -05:00
|
|
|
{
|
2018-10-20 23:40:02 -04:00
|
|
|
fs::path::make(&basepaths[i],fusepath,fullpath);
|
2015-07-01 07:47:40 -04:00
|
|
|
|
2016-02-23 12:28:20 -05:00
|
|
|
if(!fs::exists(fullpath))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
paths.push_back(fullpath);
|
2015-02-07 18:27:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
int
|
2018-10-20 23:40:02 -04:00
|
|
|
findonfs(const vector<string> &basepaths,
|
2017-06-24 17:03:41 -04:00
|
|
|
const string &fusepath,
|
2015-09-15 18:49:18 -04:00
|
|
|
const int fd,
|
|
|
|
|
string &basepath)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
|
|
|
|
int rv;
|
2016-02-23 12:06:16 -05:00
|
|
|
dev_t dev;
|
2016-01-29 16:54:39 -05:00
|
|
|
string fullpath;
|
2016-02-23 12:06:16 -05:00
|
|
|
struct stat st;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-10-19 09:28:06 -04:00
|
|
|
rv = fs::fstat(fd,st);
|
2018-10-13 23:48:30 -04:00
|
|
|
if(rv == -1)
|
2014-05-12 12:41:46 -04:00
|
|
|
return -1;
|
|
|
|
|
|
2016-02-23 12:06:16 -05:00
|
|
|
dev = st.st_dev;
|
2018-10-20 23:40:02 -04:00
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
2015-07-13 12:20:19 -04:00
|
|
|
{
|
2018-10-20 23:40:02 -04:00
|
|
|
fs::path::make(&basepaths[i],fusepath,fullpath);
|
2015-07-13 12:20:19 -04:00
|
|
|
|
2016-10-20 16:51:49 -04:00
|
|
|
rv = fs::lstat(fullpath,st);
|
2018-10-13 23:48:30 -04:00
|
|
|
if(rv == -1)
|
2016-02-23 12:06:16 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if(st.st_dev != dev)
|
2015-09-15 18:49:18 -04:00
|
|
|
continue;
|
2015-01-27 18:54:35 -05:00
|
|
|
|
2018-10-20 23:40:02 -04:00
|
|
|
basepath = basepaths[i];
|
2016-02-23 12:06:16 -05:00
|
|
|
|
|
|
|
|
return 0;
|
2015-01-27 18:54:35 -05:00
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2015-09-15 18:49:18 -04:00
|
|
|
return (errno=ENOENT,-1);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
2014-05-27 21:21:45 -04:00
|
|
|
|
2015-09-06 18:24:36 -04:00
|
|
|
void
|
|
|
|
|
realpathize(vector<string> &strs)
|
|
|
|
|
{
|
|
|
|
|
char *rv;
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i < strs.size(); i++)
|
|
|
|
|
{
|
2016-10-20 16:51:49 -04:00
|
|
|
rv = fs::realpath(strs[i]);
|
2015-09-06 18:24:36 -04:00
|
|
|
if(rv == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2016-08-04 14:52:27 -04:00
|
|
|
strs[i] = rv;
|
|
|
|
|
|
|
|
|
|
::free(rv);
|
2015-09-06 18:24:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-15 18:49:18 -04:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
getfl(const int fd)
|
|
|
|
|
{
|
|
|
|
|
return ::fcntl(fd,F_GETFL,0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 21:17:47 -04:00
|
|
|
int
|
|
|
|
|
setfl(const int fd,
|
|
|
|
|
const mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
return ::fcntl(fd,F_SETFL,mode);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 18:49:18 -04:00
|
|
|
int
|
|
|
|
|
mfs(const vector<string> &basepaths,
|
2016-07-10 12:44:29 -04:00
|
|
|
const uint64_t minfreespace,
|
2015-09-15 18:49:18 -04:00
|
|
|
string &path)
|
|
|
|
|
{
|
2018-10-13 23:48:30 -04:00
|
|
|
int rv;
|
2017-04-06 21:29:39 -04:00
|
|
|
uint64_t mfs;
|
2018-10-13 23:48:30 -04:00
|
|
|
uint64_t spaceavail;
|
2016-02-23 12:28:20 -05:00
|
|
|
const string *mfsbasepath;
|
2015-09-15 18:49:18 -04:00
|
|
|
|
2016-02-23 12:28:20 -05:00
|
|
|
mfs = 0;
|
|
|
|
|
mfsbasepath = NULL;
|
2015-09-15 18:49:18 -04:00
|
|
|
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
|
|
|
|
|
{
|
2018-12-12 10:08:17 -05:00
|
|
|
rv = fs::statvfs_cache_spaceavail(basepaths[i],&spaceavail);
|
2018-10-13 23:48:30 -04:00
|
|
|
if(rv == -1)
|
2016-02-23 12:28:20 -05:00
|
|
|
continue;
|
|
|
|
|
if(spaceavail < minfreespace)
|
|
|
|
|
continue;
|
|
|
|
|
if(spaceavail <= mfs)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
mfs = spaceavail;
|
|
|
|
|
mfsbasepath = &basepaths[i];
|
2015-09-15 18:49:18 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-23 12:28:20 -05:00
|
|
|
if(mfsbasepath == NULL)
|
2015-09-15 18:49:18 -04:00
|
|
|
return (errno=ENOENT,-1);
|
|
|
|
|
|
2016-02-23 12:28:20 -05:00
|
|
|
path = *mfsbasepath;
|
2015-09-15 18:49:18 -04:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
};
|