2020-09-13 07:49:25 +00:00
|
|
|
/*
|
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
|
|
|
* Copyright(C) 2007 Takeshi Nakatani <ggtakec.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "s3fs.h"
|
|
|
|
#include "fdcache_auto.h"
|
|
|
|
#include "fdcache.h"
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// AutoFdEntity methods
|
|
|
|
//------------------------------------------------
|
2021-05-23 16:28:50 +00:00
|
|
|
AutoFdEntity::AutoFdEntity() : pFdEntity(NULL), pseudo_fd(-1)
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// [NOTE]
|
|
|
|
// The copy constructor should not be called, then this is private method.
|
|
|
|
// Even if it is called, the consistency of the number of
|
|
|
|
// references can be maintained, but this case is not assumed.
|
|
|
|
//
|
2021-05-23 16:28:50 +00:00
|
|
|
AutoFdEntity::AutoFdEntity(AutoFdEntity& other) : pFdEntity(NULL), pseudo_fd(-1)
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
S3FS_PRN_WARN("This method should not be called. Please check the caller.");
|
|
|
|
|
|
|
|
if(other.pFdEntity){
|
2021-05-23 16:28:50 +00:00
|
|
|
if(-1 != (pseudo_fd = other.pFdEntity->Dup(other.pseudo_fd))){
|
|
|
|
pFdEntity = other.pFdEntity;
|
|
|
|
}else{
|
|
|
|
S3FS_PRN_ERR("Failed duplicating fd in AutoFdEntity.");
|
|
|
|
}
|
2020-09-13 07:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoFdEntity::~AutoFdEntity()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoFdEntity::Close()
|
|
|
|
{
|
|
|
|
if(pFdEntity){
|
2021-05-23 16:28:50 +00:00
|
|
|
if(!FdManager::get()->Close(pFdEntity, pseudo_fd)){
|
2020-09-13 07:49:25 +00:00
|
|
|
S3FS_PRN_ERR("Failed to close fdentity.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pFdEntity = NULL;
|
2021-05-23 16:28:50 +00:00
|
|
|
pseudo_fd = -1;
|
2020-09-13 07:49:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [NOTE]
|
|
|
|
// This method touches the internal fdentity with.
|
|
|
|
// This is used to keep the file open.
|
|
|
|
//
|
2021-05-23 16:28:50 +00:00
|
|
|
int AutoFdEntity::Detach()
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
if(!pFdEntity){
|
|
|
|
S3FS_PRN_ERR("Does not have a associated FdEntity.");
|
2021-05-23 16:28:50 +00:00
|
|
|
return -1;
|
2020-09-13 07:49:25 +00:00
|
|
|
}
|
2021-05-23 16:28:50 +00:00
|
|
|
int fd = pseudo_fd;
|
|
|
|
pseudo_fd = -1;
|
2020-09-13 07:49:25 +00:00
|
|
|
pFdEntity = NULL;
|
2021-05-23 16:28:50 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoFdEntity::Attach(const char* path, int existfd)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
|
|
|
if(NULL == (pFdEntity = FdManager::get()->GetFdEntity(path, existfd, false))){
|
|
|
|
S3FS_PRN_DBG("Could not find fd entity object(file=%s, existfd=%d)", path, existfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pseudo_fd = existfd;
|
2020-09-13 07:49:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
FdEntity* AutoFdEntity::Open(const char* path, headers_t* pmeta, off_t size, time_t time, int flags, bool force_tmpfile, bool is_create, bool no_fd_lock_wait)
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
if(NULL == (pFdEntity = FdManager::get()->Open(pseudo_fd, path, pmeta, size, time, flags, force_tmpfile, is_create, no_fd_lock_wait))){
|
|
|
|
pseudo_fd = -1;
|
2020-09-13 07:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pFdEntity;
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
// [NOTE]
|
|
|
|
// the fd obtained by this method is not a newly created pseudo fd.
|
|
|
|
//
|
|
|
|
FdEntity* AutoFdEntity::GetExistFdEntiy(const char* path, int existfd)
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
FdEntity* ent;
|
|
|
|
if(NULL == (ent = FdManager::get()->GetExistFdEntiy(path, existfd))){
|
2020-09-13 07:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-23 16:28:50 +00:00
|
|
|
return ent;
|
2020-09-13 07:49:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
FdEntity* AutoFdEntity::OpenExistFdEntiy(const char* path, int flags)
|
2020-09-13 07:49:25 +00:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
2021-05-23 16:28:50 +00:00
|
|
|
if(NULL == (pFdEntity = FdManager::get()->OpenExistFdEntiy(path, pseudo_fd, flags))){
|
2020-09-13 07:49:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pFdEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [NOTE]
|
|
|
|
// This operator should not be called, then this is private method.
|
|
|
|
// Even if it is called, the consistency of the number of
|
|
|
|
// references can be maintained, but this case is not assumed.
|
|
|
|
//
|
|
|
|
bool AutoFdEntity::operator=(AutoFdEntity& other)
|
|
|
|
{
|
|
|
|
S3FS_PRN_WARN("This method should not be called. Please check the caller.");
|
|
|
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
if(other.pFdEntity){
|
2021-05-23 16:28:50 +00:00
|
|
|
if(-1 != (pseudo_fd = other.pFdEntity->Dup(other.pseudo_fd))){
|
|
|
|
pFdEntity = other.pFdEntity;
|
|
|
|
}else{
|
|
|
|
S3FS_PRN_ERR("Failed duplicating fd in AutoFdEntity.");
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-13 07:49:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: expandtab sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: expandtab sw=4 ts=4
|
|
|
|
*/
|