2013-03-30 13:37:14 +00:00
|
|
|
/*
|
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
2017-05-07 11:24:17 +00:00
|
|
|
* Copyright(C) 2007 Takeshi Nakatani <ggtakec.com>
|
2013-03-30 13:37:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2019-07-12 03:33:53 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2013-03-30 13:37:14 +00:00
|
|
|
#include <unistd.h>
|
2019-07-12 03:33:53 -07:00
|
|
|
#include <cerrno>
|
2013-03-30 13:37:14 +00:00
|
|
|
#include <grp.h>
|
2023-07-27 09:12:28 +09:00
|
|
|
#include <memory>
|
2024-06-23 21:18:01 +05:30
|
|
|
#include <mutex>
|
2020-08-22 12:40:53 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <dirent.h>
|
2022-07-30 12:06:47 +09:00
|
|
|
#include <sys/stat.h>
|
2020-03-19 15:13:21 +00:00
|
|
|
#include <sys/utsname.h>
|
2013-03-30 13:37:14 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
2022-07-30 12:06:47 +09:00
|
|
|
#include "s3fs_logger.h"
|
2013-03-30 13:37:14 +00:00
|
|
|
#include "s3fs_util.h"
|
2013-11-17 08:50:41 +00:00
|
|
|
#include "string_util.h"
|
2021-03-06 03:04:14 +00:00
|
|
|
#include "s3fs_help.h"
|
2013-03-30 13:37:14 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
2014-04-04 22:11:55 -07:00
|
|
|
// Global variables
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
2019-01-22 23:15:19 -08:00
|
|
|
std::string mount_prefix;
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2019-07-16 21:52:53 -07:00
|
|
|
static size_t max_password_size;
|
|
|
|
static size_t max_group_name_length;
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
// Utilities
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string get_realpath(const char *path)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string realpath = mount_prefix;
|
2020-08-22 12:40:53 +00:00
|
|
|
realpath += path;
|
2013-04-20 19:17:28 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
return realpath;
|
Changes codes for performance(part 3)
* Summay
This revision includes big change about temporary file and local cache file.
By this big change, s3fs works with good performance when s3fs opens/
closes/syncs/reads object.
I made a big change about the handling about temporary file and local cache
file to do this implementation.
* Detail
1) About temporary file(local file)
s3fs uses a temporary file on local file system when s3fs does download/
upload/open/seek object on S3.
After this revision, s3fs calls ftruncate() function when s3fs makes the
temporary file.
In this way s3fs can set a file size of precisely length without downloading.
(Notice - ftruncate function is for XSI-compliant systems, so that possibly
you have a problem on non-XSI-compliant systems.)
By this change, s3fs can download a part of a object by requesting with
"Range" http header. It seems like downloading by each block unit.
The default block(part) size is 50MB, it is caused the result which is default
parallel requests count(5) by default multipart upload size(10MB).
If you need to change this block size, you can change by new option
"fd_page_size". This option can take from 1MB(1024 * 1024) to any bytes.
So that, you have to take care about that fdcache.cpp(and fdcache.h) were
changed a lot.
2) About local cache
Local cache files which are in directory specified by "use_cache" option do
not have always all of object data.
This cause is that s3fs uses ftruncate function and reads(writes) each block
unit of a temporary file.
s3fs manages each block unit's status which are "downloaded area" or "not".
For this status, s3fs makes new temporary file in cache directory which is
specified by "use_cache" option. This status files is in a directory which is
named "<use_cache sirectory>/.<bucket_name>/".
When s3fs opens this status file, s3fs locks this file for exclusive control by
calling flock function. You need to take care about this, the status files can
not be laid on network drive(like NFS).
This revision changes about file open mode, s3fs always opens a local cache
file and each status file with writable mode.
Last, this revision adds new option "del_cache", this option means that s3fs
deletes all local cache file when s3fs starts and exits.
3) Uploading
When s3fs writes data to file descriptor through FUSE request, old s3fs
revision downloads all of the object. But new revision does not download all,
it downloads only small percial area(some block units) including writing data
area.
And when s3fs closes or flushes the file descriptor, s3fs downloads other area
which is not downloaded from server. After that, s3fs uploads all of data.
Already r456 revision has parallel upload function, then this revision with
r456 and r457 are very big change for performance.
4) Downloading
By changing a temporary file and a local cache file, when s3fs downloads a
object, it downloads only the required range(some block units).
And s3fs downloads units by parallel GET request, it is same as a case of
uploading. (Maximum parallel request count and each download size are
specified same parameters for uploading.)
In the new revision, when s3fs opens file, s3fs returns file descriptor soon.
Because s3fs only opens(makes) the file descriptor with no downloading
data. And when s3fs reads a data, s3fs downloads only some block unit
including specified area.
This result is good for performance.
5) Changes option name
The option "parallel_upload" which added at r456 is changed to new option
name as "parallel_count". This reason is this option value is not only used by
uploading object, but a uploading object also uses this option. (For a while,
you can use old option name "parallel_upload" for compatibility.)
git-svn-id: http://s3fs.googlecode.com/svn/trunk@458 df820570-a93a-0410-bd06-b72b767a4274
2013-07-23 16:01:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:52:53 -07:00
|
|
|
void init_sysconf_vars()
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
// SUSv4tc1 says the following about _SC_GETGR_R_SIZE_MAX and
|
|
|
|
// _SC_GETPW_R_SIZE_MAX:
|
|
|
|
// Note that sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if
|
|
|
|
// there is no hard limit on the size of the buffer needed to
|
|
|
|
// store all the groups returned.
|
|
|
|
|
2024-08-24 09:48:54 +09:00
|
|
|
errno = 0;
|
2020-08-22 12:40:53 +00:00
|
|
|
long res = sysconf(_SC_GETPW_R_SIZE_MAX);
|
|
|
|
if(0 > res){
|
|
|
|
if (errno != 0){
|
2024-08-24 09:48:54 +09:00
|
|
|
S3FS_PRN_ERR("could not get max password length.");
|
2020-08-22 12:40:53 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
res = 1024; // default initial length
|
2019-07-16 21:52:53 -07:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
max_password_size = res;
|
2019-07-16 21:52:53 -07:00
|
|
|
|
2024-08-24 09:48:54 +09:00
|
|
|
errno = 0;
|
2020-08-22 12:40:53 +00:00
|
|
|
res = sysconf(_SC_GETGR_R_SIZE_MAX);
|
|
|
|
if(0 > res) {
|
|
|
|
if (errno != 0) {
|
2024-08-24 09:48:54 +09:00
|
|
|
S3FS_PRN_ERR("could not get max group name length.");
|
2020-08-22 12:40:53 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
res = 1024; // default initial length
|
2019-07-16 21:52:53 -07:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
max_group_name_length = res;
|
2019-07-16 21:52:53 -07:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility for UID/GID
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// get user name from uid
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string get_username(uid_t uid)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
size_t maxlen = max_password_size;
|
|
|
|
int result;
|
|
|
|
struct passwd pwinfo;
|
2023-07-27 21:56:58 +09:00
|
|
|
struct passwd* ppwinfo = nullptr;
|
2013-09-14 21:50:39 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// make buffer
|
2023-07-27 09:12:28 +09:00
|
|
|
std::unique_ptr<char[]> pbuf(new char[maxlen]);
|
2020-08-22 12:40:53 +00:00
|
|
|
// get pw information
|
2023-07-27 09:12:28 +09:00
|
|
|
while(ERANGE == (result = getpwuid_r(uid, &pwinfo, pbuf.get(), maxlen, &ppwinfo))){
|
2020-08-22 12:40:53 +00:00
|
|
|
maxlen *= 2;
|
2023-07-27 09:12:28 +09:00
|
|
|
pbuf.reset(new char[maxlen]);
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2018-03-02 15:58:41 -05:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(0 != result){
|
|
|
|
S3FS_PRN_ERR("could not get pw information(%d).", result);
|
2023-08-17 22:12:28 +09:00
|
|
|
return "";
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2018-03-02 15:58:41 -05:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// check pw
|
2023-07-27 21:56:58 +09:00
|
|
|
if(nullptr == ppwinfo){
|
2023-08-17 22:12:28 +09:00
|
|
|
return "";
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string name = SAFESTRPTR(ppwinfo->pw_name);
|
2020-08-22 12:40:53 +00:00
|
|
|
return name;
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 22:11:55 -07:00
|
|
|
int is_uid_include_group(uid_t uid, gid_t gid)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
size_t maxlen = max_group_name_length;
|
|
|
|
int result;
|
|
|
|
struct group ginfo;
|
2023-07-27 21:56:58 +09:00
|
|
|
struct group* pginfo = nullptr;
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// make buffer
|
2023-07-27 09:12:28 +09:00
|
|
|
std::unique_ptr<char[]> pbuf(new char[maxlen]);
|
2020-08-22 12:40:53 +00:00
|
|
|
// get group information
|
2023-07-27 09:12:28 +09:00
|
|
|
while(ERANGE == (result = getgrgid_r(gid, &ginfo, pbuf.get(), maxlen, &pginfo))){
|
2020-08-22 12:40:53 +00:00
|
|
|
maxlen *= 2;
|
2023-07-27 09:12:28 +09:00
|
|
|
pbuf.reset(new char[maxlen]);
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2017-05-23 00:46:41 +09:30
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(0 != result){
|
|
|
|
S3FS_PRN_ERR("could not get group information(%d).", result);
|
|
|
|
return -result;
|
|
|
|
}
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// check group
|
2023-07-27 21:56:58 +09:00
|
|
|
if(nullptr == pginfo){
|
2020-08-22 12:40:53 +00:00
|
|
|
// there is not gid in group.
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string username = get_username(uid);
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
char** ppgr_mem;
|
|
|
|
for(ppgr_mem = pginfo->gr_mem; ppgr_mem && *ppgr_mem; ppgr_mem++){
|
|
|
|
if(username == *ppgr_mem){
|
|
|
|
// Found username in group.
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return 0;
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility for file and directory
|
|
|
|
//-------------------------------------------------------------------
|
2022-07-24 03:11:18 +00:00
|
|
|
// [NOTE]
|
|
|
|
// basename/dirname returns a static variable pointer as the return value.
|
|
|
|
// Normally this shouldn't be a problem, but in macos10 we found a case
|
|
|
|
// where dirname didn't receive its return value correctly due to thread
|
|
|
|
// conflicts.
|
|
|
|
// To avoid this, exclusive control is performed by mutex.
|
|
|
|
//
|
2024-06-23 21:18:01 +05:30
|
|
|
static std::mutex basename_lock;
|
2022-07-24 03:11:18 +00:00
|
|
|
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string mydirname(const std::string& path)
|
2020-08-22 12:40:53 +00:00
|
|
|
{
|
2024-06-23 21:18:01 +05:30
|
|
|
const std::lock_guard<std::mutex> lock(basename_lock);
|
2022-07-24 03:11:18 +00:00
|
|
|
|
2022-07-28 23:34:01 +09:00
|
|
|
return mydirname(path.c_str());
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
// safe variant of dirname
|
|
|
|
// dirname clobbers path so let it operate on a tmp copy
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string mydirname(const char* path)
|
2016-07-03 03:37:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!path || '\0' == path[0]){
|
2023-08-17 22:12:28 +09:00
|
|
|
return "";
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2022-07-28 23:34:01 +09:00
|
|
|
|
|
|
|
char *buf = strdup(path);
|
|
|
|
std::string result = dirname(buf);
|
|
|
|
free(buf);
|
|
|
|
return result;
|
2016-07-03 03:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string mybasename(const std::string& path)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2024-06-23 21:18:01 +05:30
|
|
|
const std::lock_guard<std::mutex> data_lock(basename_lock);
|
2022-07-24 03:11:18 +00:00
|
|
|
|
2022-07-28 23:34:01 +09:00
|
|
|
return mybasename(path.c_str());
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// safe variant of basename
|
|
|
|
// basename clobbers path so let it operate on a tmp copy
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string mybasename(const char* path)
|
2016-07-03 03:37:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!path || '\0' == path[0]){
|
2023-08-17 22:12:28 +09:00
|
|
|
return "";
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2022-07-28 23:34:01 +09:00
|
|
|
|
|
|
|
char *buf = strdup(path);
|
|
|
|
std::string result = basename(buf);
|
|
|
|
free(buf);
|
|
|
|
return result;
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mkdir --parents
|
2020-09-11 18:37:24 +09:00
|
|
|
int mkdirp(const std::string& path, mode_t mode)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string base;
|
|
|
|
std::string component;
|
|
|
|
std::istringstream ss(path);
|
2020-08-22 12:40:53 +00:00
|
|
|
while (getline(ss, component, '/')) {
|
2020-10-10 18:13:23 +09:00
|
|
|
base += component + "/";
|
2015-08-23 03:57:34 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
struct stat st;
|
|
|
|
if(0 == stat(base.c_str(), &st)){
|
|
|
|
if(!S_ISDIR(st.st_mode)){
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(0 != mkdir(base.c_str(), mode) && errno != EEXIST){
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 03:57:34 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return 0;
|
2015-08-23 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 08:10:16 +00:00
|
|
|
// get existed directory path
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string get_exist_directory_path(const std::string& path)
|
2017-04-02 08:10:16 +00:00
|
|
|
{
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string existed("/"); // "/" is existed.
|
|
|
|
std::string base;
|
|
|
|
std::string component;
|
|
|
|
std::istringstream ss(path);
|
2020-08-22 12:40:53 +00:00
|
|
|
while (getline(ss, component, '/')) {
|
|
|
|
if(base != "/"){
|
|
|
|
base += "/";
|
|
|
|
}
|
|
|
|
base += component;
|
|
|
|
struct stat st;
|
|
|
|
if(0 == stat(base.c_str(), &st) && S_ISDIR(st.st_mode)){
|
|
|
|
existed = base;
|
|
|
|
}else{
|
|
|
|
break;
|
|
|
|
}
|
2017-04-02 08:10:16 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return existed;
|
2017-04-02 08:10:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 03:57:34 +00:00
|
|
|
bool check_exist_dir_permission(const char* dirpath)
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!dirpath || '\0' == dirpath[0]){
|
2015-08-23 03:57:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
Changes codes for performance(part 3)
* Summay
This revision includes big change about temporary file and local cache file.
By this big change, s3fs works with good performance when s3fs opens/
closes/syncs/reads object.
I made a big change about the handling about temporary file and local cache
file to do this implementation.
* Detail
1) About temporary file(local file)
s3fs uses a temporary file on local file system when s3fs does download/
upload/open/seek object on S3.
After this revision, s3fs calls ftruncate() function when s3fs makes the
temporary file.
In this way s3fs can set a file size of precisely length without downloading.
(Notice - ftruncate function is for XSI-compliant systems, so that possibly
you have a problem on non-XSI-compliant systems.)
By this change, s3fs can download a part of a object by requesting with
"Range" http header. It seems like downloading by each block unit.
The default block(part) size is 50MB, it is caused the result which is default
parallel requests count(5) by default multipart upload size(10MB).
If you need to change this block size, you can change by new option
"fd_page_size". This option can take from 1MB(1024 * 1024) to any bytes.
So that, you have to take care about that fdcache.cpp(and fdcache.h) were
changed a lot.
2) About local cache
Local cache files which are in directory specified by "use_cache" option do
not have always all of object data.
This cause is that s3fs uses ftruncate function and reads(writes) each block
unit of a temporary file.
s3fs manages each block unit's status which are "downloaded area" or "not".
For this status, s3fs makes new temporary file in cache directory which is
specified by "use_cache" option. This status files is in a directory which is
named "<use_cache sirectory>/.<bucket_name>/".
When s3fs opens this status file, s3fs locks this file for exclusive control by
calling flock function. You need to take care about this, the status files can
not be laid on network drive(like NFS).
This revision changes about file open mode, s3fs always opens a local cache
file and each status file with writable mode.
Last, this revision adds new option "del_cache", this option means that s3fs
deletes all local cache file when s3fs starts and exits.
3) Uploading
When s3fs writes data to file descriptor through FUSE request, old s3fs
revision downloads all of the object. But new revision does not download all,
it downloads only small percial area(some block units) including writing data
area.
And when s3fs closes or flushes the file descriptor, s3fs downloads other area
which is not downloaded from server. After that, s3fs uploads all of data.
Already r456 revision has parallel upload function, then this revision with
r456 and r457 are very big change for performance.
4) Downloading
By changing a temporary file and a local cache file, when s3fs downloads a
object, it downloads only the required range(some block units).
And s3fs downloads units by parallel GET request, it is same as a case of
uploading. (Maximum parallel request count and each download size are
specified same parameters for uploading.)
In the new revision, when s3fs opens file, s3fs returns file descriptor soon.
Because s3fs only opens(makes) the file descriptor with no downloading
data. And when s3fs reads a data, s3fs downloads only some block unit
including specified area.
This result is good for performance.
5) Changes option name
The option "parallel_upload" which added at r456 is changed to new option
name as "parallel_count". This reason is this option value is not only used by
uploading object, but a uploading object also uses this option. (For a while,
you can use old option name "parallel_upload" for compatibility.)
git-svn-id: http://s3fs.googlecode.com/svn/trunk@458 df820570-a93a-0410-bd06-b72b767a4274
2013-07-23 16:01:48 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// exists
|
Changes codes for performance(part 3)
* Summay
This revision includes big change about temporary file and local cache file.
By this big change, s3fs works with good performance when s3fs opens/
closes/syncs/reads object.
I made a big change about the handling about temporary file and local cache
file to do this implementation.
* Detail
1) About temporary file(local file)
s3fs uses a temporary file on local file system when s3fs does download/
upload/open/seek object on S3.
After this revision, s3fs calls ftruncate() function when s3fs makes the
temporary file.
In this way s3fs can set a file size of precisely length without downloading.
(Notice - ftruncate function is for XSI-compliant systems, so that possibly
you have a problem on non-XSI-compliant systems.)
By this change, s3fs can download a part of a object by requesting with
"Range" http header. It seems like downloading by each block unit.
The default block(part) size is 50MB, it is caused the result which is default
parallel requests count(5) by default multipart upload size(10MB).
If you need to change this block size, you can change by new option
"fd_page_size". This option can take from 1MB(1024 * 1024) to any bytes.
So that, you have to take care about that fdcache.cpp(and fdcache.h) were
changed a lot.
2) About local cache
Local cache files which are in directory specified by "use_cache" option do
not have always all of object data.
This cause is that s3fs uses ftruncate function and reads(writes) each block
unit of a temporary file.
s3fs manages each block unit's status which are "downloaded area" or "not".
For this status, s3fs makes new temporary file in cache directory which is
specified by "use_cache" option. This status files is in a directory which is
named "<use_cache sirectory>/.<bucket_name>/".
When s3fs opens this status file, s3fs locks this file for exclusive control by
calling flock function. You need to take care about this, the status files can
not be laid on network drive(like NFS).
This revision changes about file open mode, s3fs always opens a local cache
file and each status file with writable mode.
Last, this revision adds new option "del_cache", this option means that s3fs
deletes all local cache file when s3fs starts and exits.
3) Uploading
When s3fs writes data to file descriptor through FUSE request, old s3fs
revision downloads all of the object. But new revision does not download all,
it downloads only small percial area(some block units) including writing data
area.
And when s3fs closes or flushes the file descriptor, s3fs downloads other area
which is not downloaded from server. After that, s3fs uploads all of data.
Already r456 revision has parallel upload function, then this revision with
r456 and r457 are very big change for performance.
4) Downloading
By changing a temporary file and a local cache file, when s3fs downloads a
object, it downloads only the required range(some block units).
And s3fs downloads units by parallel GET request, it is same as a case of
uploading. (Maximum parallel request count and each download size are
specified same parameters for uploading.)
In the new revision, when s3fs opens file, s3fs returns file descriptor soon.
Because s3fs only opens(makes) the file descriptor with no downloading
data. And when s3fs reads a data, s3fs downloads only some block unit
including specified area.
This result is good for performance.
5) Changes option name
The option "parallel_upload" which added at r456 is changed to new option
name as "parallel_count". This reason is this option value is not only used by
uploading object, but a uploading object also uses this option. (For a while,
you can use old option name "parallel_upload" for compatibility.)
git-svn-id: http://s3fs.googlecode.com/svn/trunk@458 df820570-a93a-0410-bd06-b72b767a4274
2013-07-23 16:01:48 +00:00
|
|
|
struct stat st;
|
2020-08-22 12:40:53 +00:00
|
|
|
if(0 != stat(dirpath, &st)){
|
|
|
|
if(ENOENT == errno){
|
|
|
|
// dir does not exist
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(EACCES == errno){
|
|
|
|
// could not access directory
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// something error occurred
|
Changes codes for performance(part 3)
* Summay
This revision includes big change about temporary file and local cache file.
By this big change, s3fs works with good performance when s3fs opens/
closes/syncs/reads object.
I made a big change about the handling about temporary file and local cache
file to do this implementation.
* Detail
1) About temporary file(local file)
s3fs uses a temporary file on local file system when s3fs does download/
upload/open/seek object on S3.
After this revision, s3fs calls ftruncate() function when s3fs makes the
temporary file.
In this way s3fs can set a file size of precisely length without downloading.
(Notice - ftruncate function is for XSI-compliant systems, so that possibly
you have a problem on non-XSI-compliant systems.)
By this change, s3fs can download a part of a object by requesting with
"Range" http header. It seems like downloading by each block unit.
The default block(part) size is 50MB, it is caused the result which is default
parallel requests count(5) by default multipart upload size(10MB).
If you need to change this block size, you can change by new option
"fd_page_size". This option can take from 1MB(1024 * 1024) to any bytes.
So that, you have to take care about that fdcache.cpp(and fdcache.h) were
changed a lot.
2) About local cache
Local cache files which are in directory specified by "use_cache" option do
not have always all of object data.
This cause is that s3fs uses ftruncate function and reads(writes) each block
unit of a temporary file.
s3fs manages each block unit's status which are "downloaded area" or "not".
For this status, s3fs makes new temporary file in cache directory which is
specified by "use_cache" option. This status files is in a directory which is
named "<use_cache sirectory>/.<bucket_name>/".
When s3fs opens this status file, s3fs locks this file for exclusive control by
calling flock function. You need to take care about this, the status files can
not be laid on network drive(like NFS).
This revision changes about file open mode, s3fs always opens a local cache
file and each status file with writable mode.
Last, this revision adds new option "del_cache", this option means that s3fs
deletes all local cache file when s3fs starts and exits.
3) Uploading
When s3fs writes data to file descriptor through FUSE request, old s3fs
revision downloads all of the object. But new revision does not download all,
it downloads only small percial area(some block units) including writing data
area.
And when s3fs closes or flushes the file descriptor, s3fs downloads other area
which is not downloaded from server. After that, s3fs uploads all of data.
Already r456 revision has parallel upload function, then this revision with
r456 and r457 are very big change for performance.
4) Downloading
By changing a temporary file and a local cache file, when s3fs downloads a
object, it downloads only the required range(some block units).
And s3fs downloads units by parallel GET request, it is same as a case of
uploading. (Maximum parallel request count and each download size are
specified same parameters for uploading.)
In the new revision, when s3fs opens file, s3fs returns file descriptor soon.
Because s3fs only opens(makes) the file descriptor with no downloading
data. And when s3fs reads a data, s3fs downloads only some block unit
including specified area.
This result is good for performance.
5) Changes option name
The option "parallel_upload" which added at r456 is changed to new option
name as "parallel_count". This reason is this option value is not only used by
uploading object, but a uploading object also uses this option. (For a while,
you can use old option name "parallel_upload" for compatibility.)
git-svn-id: http://s3fs.googlecode.com/svn/trunk@458 df820570-a93a-0410-bd06-b72b767a4274
2013-07-23 16:01:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// check type
|
|
|
|
if(!S_ISDIR(st.st_mode)){
|
|
|
|
// path is not directory
|
|
|
|
return false;
|
2019-01-20 08:28:06 +00:00
|
|
|
}
|
2019-01-06 17:51:42 -08:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// check permission
|
|
|
|
uid_t myuid = geteuid();
|
|
|
|
if(myuid == st.st_uid){
|
|
|
|
if(S_IRWXU != (st.st_mode & S_IRWXU)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(1 == is_uid_include_group(myuid, st.st_gid)){
|
|
|
|
if(S_IRWXG != (st.st_mode & S_IRWXG)){
|
|
|
|
return false;
|
2016-02-13 05:58:59 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
}else{
|
|
|
|
if(S_IRWXO != (st.st_mode & S_IRWXO)){
|
|
|
|
return false;
|
2013-05-09 08:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2013-05-09 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
bool delete_files_in_dir(const char* dir, bool is_remove_own)
|
2013-06-19 14:53:58 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
DIR* dp;
|
|
|
|
struct dirent* dent;
|
2013-06-19 14:53:58 +00:00
|
|
|
|
2023-07-27 21:56:58 +09:00
|
|
|
if(nullptr == (dp = opendir(dir))){
|
2020-08-22 12:40:53 +00:00
|
|
|
S3FS_PRN_ERR("could not open dir(%s) - errno(%d)", dir, errno);
|
|
|
|
return false;
|
2020-08-02 13:37:06 +00:00
|
|
|
}
|
2019-01-30 18:29:34 -08:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
for(dent = readdir(dp); dent; dent = readdir(dp)){
|
|
|
|
if(0 == strcmp(dent->d_name, "..") || 0 == strcmp(dent->d_name, ".")){
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-11 18:37:24 +09:00
|
|
|
std::string fullpath = dir;
|
|
|
|
fullpath += "/";
|
|
|
|
fullpath += dent->d_name;
|
2020-08-22 12:40:53 +00:00
|
|
|
struct stat st;
|
|
|
|
if(0 != lstat(fullpath.c_str(), &st)){
|
|
|
|
S3FS_PRN_ERR("could not get stats of file(%s) - errno(%d)", fullpath.c_str(), errno);
|
|
|
|
closedir(dp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(S_ISDIR(st.st_mode)){
|
|
|
|
// dir -> Reentrant
|
|
|
|
if(!delete_files_in_dir(fullpath.c_str(), true)){
|
|
|
|
S3FS_PRN_ERR("could not remove sub dir(%s) - errno(%d)", fullpath.c_str(), errno);
|
|
|
|
closedir(dp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(0 != unlink(fullpath.c_str())){
|
|
|
|
S3FS_PRN_ERR("could not remove file(%s) - errno(%d)", fullpath.c_str(), errno);
|
|
|
|
closedir(dp);
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-30 18:29:34 -08:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
closedir(dp);
|
2019-01-30 18:29:34 -08:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(is_remove_own && 0 != rmdir(dir)){
|
|
|
|
S3FS_PRN_ERR("could not remove dir(%s) - errno(%d)", dir, errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-01-30 18:29:34 -08:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
// Utility for system information
|
2013-03-30 13:37:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
bool compare_sysname(const char* target)
|
2013-03-30 13:37:14 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
// [NOTE]
|
|
|
|
// The buffer size of sysname member in struct utsname is
|
|
|
|
// OS dependent, but 512 bytes is sufficient for now.
|
|
|
|
//
|
2024-01-25 00:46:45 +09:00
|
|
|
static const char* psysname = nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
static char sysname[512];
|
|
|
|
if(!psysname){
|
|
|
|
struct utsname sysinfo;
|
|
|
|
if(0 != uname(&sysinfo)){
|
|
|
|
S3FS_PRN_ERR("could not initialize system name to internal buffer(errno:%d), thus use \"Linux\".", errno);
|
|
|
|
strcpy(sysname, "Linux");
|
|
|
|
}else{
|
|
|
|
S3FS_PRN_INFO("system name is %s", sysinfo.sysname);
|
|
|
|
sysname[sizeof(sysname) - 1] = '\0';
|
|
|
|
strncpy(sysname, sysinfo.sysname, sizeof(sysname) - 1);
|
|
|
|
}
|
|
|
|
psysname = &sysname[0];
|
|
|
|
}
|
2013-03-30 13:37:14 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!target || 0 != strcmp(psysname, target)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 03:04:14 +00:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility for print message at launching
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
void print_launch_message(int argc, char** argv)
|
|
|
|
{
|
|
|
|
std::string message = short_version();
|
|
|
|
|
|
|
|
if(argv){
|
|
|
|
message += " :";
|
|
|
|
for(int cnt = 0; cnt < argc; ++cnt){
|
|
|
|
if(argv[cnt]){
|
|
|
|
message += " ";
|
|
|
|
if(0 == cnt){
|
|
|
|
message += basename(argv[cnt]);
|
|
|
|
}else{
|
|
|
|
message += argv[cnt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
S3FS_PRN_LAUNCH_INFO("%s", message.c_str());
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:10:04 +00:00
|
|
|
//
|
|
|
|
// result: -1 ts1 < ts2
|
|
|
|
// 0 ts1 == ts2
|
|
|
|
// 1 ts1 > ts2
|
|
|
|
//
|
|
|
|
int compare_timespec(const struct timespec& ts1, const struct timespec& ts2)
|
|
|
|
{
|
|
|
|
if(ts1.tv_sec < ts2.tv_sec){
|
|
|
|
return -1;
|
|
|
|
}else if(ts1.tv_sec > ts2.tv_sec){
|
|
|
|
return 1;
|
|
|
|
}else{
|
|
|
|
if(ts1.tv_nsec < ts2.tv_nsec){
|
|
|
|
return -1;
|
|
|
|
}else if(ts1.tv_nsec > ts2.tv_nsec){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// result: -1 st < ts
|
|
|
|
// 0 st == ts
|
|
|
|
// 1 st > ts
|
|
|
|
//
|
|
|
|
int compare_timespec(const struct stat& st, stat_time_type type, const struct timespec& ts)
|
|
|
|
{
|
|
|
|
struct timespec st_ts;
|
|
|
|
set_stat_to_timespec(st, type, st_ts);
|
|
|
|
|
|
|
|
return compare_timespec(st_ts, ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_timespec_to_stat(struct stat& st, stat_time_type type, const struct timespec& ts)
|
|
|
|
{
|
2023-07-30 22:53:17 +09:00
|
|
|
if(stat_time_type::ATIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
st.st_atime = ts.tv_sec;
|
|
|
|
st.st_atimespec.tv_nsec = ts.tv_nsec;
|
|
|
|
#else
|
|
|
|
st.st_atim.tv_sec = ts.tv_sec;
|
|
|
|
st.st_atim.tv_nsec = ts.tv_nsec;
|
|
|
|
#endif
|
2023-07-30 22:53:17 +09:00
|
|
|
}else if(stat_time_type::MTIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
st.st_mtime = ts.tv_sec;
|
|
|
|
st.st_mtimespec.tv_nsec = ts.tv_nsec;
|
|
|
|
#else
|
|
|
|
st.st_mtim.tv_sec = ts.tv_sec;
|
|
|
|
st.st_mtim.tv_nsec = ts.tv_nsec;
|
|
|
|
#endif
|
2023-07-30 22:53:17 +09:00
|
|
|
}else if(stat_time_type::CTIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
st.st_ctime = ts.tv_sec;
|
|
|
|
st.st_ctimespec.tv_nsec = ts.tv_nsec;
|
|
|
|
#else
|
|
|
|
st.st_ctim.tv_sec = ts.tv_sec;
|
|
|
|
st.st_ctim.tv_nsec = ts.tv_nsec;
|
|
|
|
#endif
|
|
|
|
}else{
|
2023-07-30 22:53:17 +09:00
|
|
|
S3FS_PRN_ERR("unknown type(%d), so skip to set value.", static_cast<int>(type));
|
2022-07-25 11:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec* set_stat_to_timespec(const struct stat& st, stat_time_type type, struct timespec& ts)
|
|
|
|
{
|
2023-07-30 22:53:17 +09:00
|
|
|
if(stat_time_type::ATIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
ts.tv_sec = st.st_atime;
|
|
|
|
ts.tv_nsec = st.st_atimespec.tv_nsec;
|
|
|
|
#else
|
|
|
|
ts = st.st_atim;
|
|
|
|
#endif
|
2023-07-30 22:53:17 +09:00
|
|
|
}else if(stat_time_type::MTIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
ts.tv_sec = st.st_mtime;
|
|
|
|
ts.tv_nsec = st.st_mtimespec.tv_nsec;
|
|
|
|
#else
|
|
|
|
ts = st.st_mtim;
|
|
|
|
#endif
|
2023-07-30 22:53:17 +09:00
|
|
|
}else if(stat_time_type::CTIME == type){
|
2022-07-25 11:10:04 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
ts.tv_sec = st.st_ctime;
|
|
|
|
ts.tv_nsec = st.st_ctimespec.tv_nsec;
|
|
|
|
#else
|
|
|
|
ts = st.st_ctim;
|
|
|
|
#endif
|
|
|
|
}else{
|
2023-07-30 22:53:17 +09:00
|
|
|
S3FS_PRN_ERR("unknown type(%d), so use 0 as timespec.", static_cast<int>(type));
|
2022-07-25 11:10:04 +00:00
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
}
|
|
|
|
return &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str_stat_time(const struct stat& st, stat_time_type type)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
return str(*set_stat_to_timespec(st, type, ts));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec* s3fs_realtime(struct timespec& ts)
|
|
|
|
{
|
|
|
|
if(-1 == clock_gettime(static_cast<clockid_t>(CLOCK_REALTIME), &ts)){
|
|
|
|
S3FS_PRN_WARN("failed to clock_gettime by errno(%d)", errno);
|
2023-07-27 21:56:58 +09:00
|
|
|
ts.tv_sec = time(nullptr);
|
2022-07-25 11:10:04 +00:00
|
|
|
ts.tv_nsec = 0;
|
|
|
|
}
|
|
|
|
return &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string s3fs_str_realtime()
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
2023-07-28 18:21:55 +09:00
|
|
|
return str(*s3fs_realtime(ts));
|
2022-07-25 11:10:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 23:55:11 +09:00
|
|
|
int s3fs_fclose(FILE* fp)
|
|
|
|
{
|
|
|
|
if(fp == nullptr){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-09-07 15:08:27 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
2020-08-22 12:40:53 +00:00
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2014-09-07 15:08:27 +00:00
|
|
|
* End:
|
2020-08-22 12:40:53 +00:00
|
|
|
* vim600: expandtab sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: expandtab sw=4 ts=4
|
2014-09-07 15:08:27 +00:00
|
|
|
*/
|