Make callbacks parameters type-safe

Instead of storing and passing void pointers, lambdas can capture
additional parameters.
This commit is contained in:
Andrew Gaul 2024-11-06 16:37:12 -08:00
parent 45b32046cd
commit d977e3148f
5 changed files with 28 additions and 51 deletions

View File

@ -1231,9 +1231,9 @@ bool S3fsCurl::SetIPResolveType(const char* value)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}
@ -1243,9 +1243,9 @@ bool S3fsCurl::UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::MixMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::MixMultipartPostCallback(S3fsCurl* s3fscurl)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}
@ -4385,9 +4385,9 @@ bool S3fsCurl::UploadMultipartPostComplete()
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameter
// cppcheck-suppress constParameterCallback
bool S3fsCurl::CopyMultipartPostCallback(S3fsCurl* s3fscurl, void* param)
bool S3fsCurl::CopyMultipartPostCallback(S3fsCurl* s3fscurl)
{
if(!s3fscurl || param){ // this callback does not need a parameter
if(!s3fscurl){
return false;
}

View File

@ -244,9 +244,9 @@ class S3fsCurl
static size_t UploadReadCallback(void *ptr, size_t size, size_t nmemb, void *userp);
static size_t DownloadWriteCallback(void* ptr, size_t size, size_t nmemb, void* userp);
static bool UploadMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool CopyMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool MixMultipartPostCallback(S3fsCurl* s3fscurl, void* param);
static bool UploadMultipartPostCallback(S3fsCurl* s3fscurl);
static bool CopyMultipartPostCallback(S3fsCurl* s3fscurl);
static bool MixMultipartPostCallback(S3fsCurl* s3fscurl);
static std::unique_ptr<S3fsCurl> UploadMultipartPostRetryCallback(S3fsCurl* s3fscurl);
static std::unique_ptr<S3fsCurl> CopyMultipartPostRetryCallback(S3fsCurl* s3fscurl);
static std::unique_ptr<S3fsCurl> MixMultipartPostRetryCallback(S3fsCurl* s3fscurl);

View File

@ -36,7 +36,7 @@
//-------------------------------------------------------------------
// Class S3fsMultiCurl
//-------------------------------------------------------------------
S3fsMultiCurl::S3fsMultiCurl(int maxParallelism, bool not_abort) : maxParallelism(maxParallelism), not_abort(not_abort), SuccessCallback(nullptr), NotFoundCallback(nullptr), RetryCallback(nullptr), pSuccessCallbackParam(nullptr), pNotFoundCallbackParam(nullptr)
S3fsMultiCurl::S3fsMultiCurl(int maxParallelism, bool not_abort) : maxParallelism(maxParallelism), not_abort(not_abort), SuccessCallback(nullptr), NotFoundCallback(nullptr), RetryCallback(nullptr)
{
}
@ -69,14 +69,14 @@ bool S3fsMultiCurl::ClearEx(bool is_all)
return true;
}
S3fsMultiSuccessCallback S3fsMultiCurl::SetSuccessCallback(S3fsMultiSuccessCallback function)
S3fsMultiSuccessCallback S3fsMultiCurl::SetSuccessCallback(const S3fsMultiSuccessCallback &function)
{
S3fsMultiSuccessCallback old = SuccessCallback;
SuccessCallback = function;
return old;
}
S3fsMultiNotFoundCallback S3fsMultiCurl::SetNotFoundCallback(S3fsMultiNotFoundCallback function)
S3fsMultiNotFoundCallback S3fsMultiCurl::SetNotFoundCallback(const S3fsMultiNotFoundCallback &function)
{
S3fsMultiNotFoundCallback old = NotFoundCallback;
NotFoundCallback = function;
@ -90,20 +90,6 @@ S3fsMultiRetryCallback S3fsMultiCurl::SetRetryCallback(S3fsMultiRetryCallback fu
return old;
}
void* S3fsMultiCurl::SetSuccessCallbackParam(void* param)
{
void* old = pSuccessCallbackParam;
pSuccessCallbackParam = param;
return old;
}
void* S3fsMultiCurl::SetNotFoundCallbackParam(void* param)
{
void* old = pNotFoundCallbackParam;
pNotFoundCallbackParam = param;
return old;
}
bool S3fsMultiCurl::SetS3fsCurlObject(std::unique_ptr<S3fsCurl> s3fscurl)
{
if(!s3fscurl){
@ -212,7 +198,7 @@ int S3fsMultiCurl::MultiRead()
// add into stat cache
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress knownPointerToBool
if(SuccessCallback && !SuccessCallback(s3fscurl.get(), pSuccessCallbackParam)){
if(SuccessCallback && !SuccessCallback(s3fscurl.get())){
S3FS_PRN_WARN("error from success callback function(%s).", s3fscurl->url.c_str());
}
}else if(400 == responseCode){
@ -228,7 +214,7 @@ int S3fsMultiCurl::MultiRead()
// Call callback function
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress knownPointerToBool
if(NotFoundCallback && !NotFoundCallback(s3fscurl.get(), pNotFoundCallbackParam)){
if(NotFoundCallback && !NotFoundCallback(s3fscurl.get())){
S3FS_PRN_WARN("error from not found callback function(%s).", s3fscurl->url.c_str());
}
}else if(500 == responseCode){

View File

@ -35,8 +35,8 @@
class S3fsCurl;
typedef std::vector<std::unique_ptr<S3fsCurl>> s3fscurllist_t;
typedef bool (*S3fsMultiSuccessCallback)(S3fsCurl* s3fscurl, void* param); // callback for succeed multi request
typedef bool (*S3fsMultiNotFoundCallback)(S3fsCurl* s3fscurl, void* param); // callback for succeed multi request
typedef std::function<bool(S3fsCurl*)> S3fsMultiSuccessCallback; // callback for succeed multi request
typedef std::function<bool(S3fsCurl*)> S3fsMultiNotFoundCallback; // callback for succeed multi request
typedef std::unique_ptr<S3fsCurl> (*S3fsMultiRetryCallback)(S3fsCurl* s3fscurl); // callback for failure and retrying
//----------------------------------------------
@ -54,8 +54,6 @@ class S3fsMultiCurl
S3fsMultiSuccessCallback SuccessCallback;
S3fsMultiNotFoundCallback NotFoundCallback;
S3fsMultiRetryCallback RetryCallback;
void* pSuccessCallbackParam;
void* pNotFoundCallbackParam;
std::mutex completed_tids_lock;
std::vector<std::thread::id> completed_tids GUARDED_BY(completed_tids_lock);
@ -77,11 +75,9 @@ class S3fsMultiCurl
int GetMaxParallelism() const { return maxParallelism; }
S3fsMultiSuccessCallback SetSuccessCallback(S3fsMultiSuccessCallback function);
S3fsMultiNotFoundCallback SetNotFoundCallback(S3fsMultiNotFoundCallback function);
S3fsMultiSuccessCallback SetSuccessCallback(const S3fsMultiSuccessCallback &function);
S3fsMultiNotFoundCallback SetNotFoundCallback(const S3fsMultiNotFoundCallback &function);
S3fsMultiRetryCallback SetRetryCallback(S3fsMultiRetryCallback function);
void* SetSuccessCallbackParam(void* param);
void* SetNotFoundCallbackParam(void* param);
bool Clear() { return ClearEx(true); }
bool SetS3fsCurlObject(std::unique_ptr<S3fsCurl> s3fscurl);
int Request();

View File

@ -125,7 +125,8 @@ static int check_object_access(const char* path, int mask, struct stat* pstbuf);
static int check_object_owner(const char* path, struct stat* pstbuf);
static int check_parent_object_access(const char* path, int mask);
static int get_local_fent(AutoFdEntity& autoent, FdEntity **entity, const char* path, int flags = O_RDONLY, bool is_load = false);
static bool multi_head_callback(S3fsCurl* s3fscurl, void* param);
class SyncFiller;
static bool multi_head_callback(S3fsCurl* s3fscurl, SyncFiller* param);
static std::unique_ptr<S3fsCurl> multi_head_retry_callback(S3fsCurl* s3fscurl);
static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf, fuse_fill_dir_t filler);
static int list_bucket(const char* path, S3ObjList& head, const char* delimiter, bool check_content_only = false);
@ -3123,7 +3124,7 @@ static int s3fs_opendir(const char* _path, struct fuse_file_info* fi)
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress constParameterCallback
static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
static bool multi_head_callback(S3fsCurl* s3fscurl, SyncFiller* pcbparam)
{
if(!s3fscurl){
return false;
@ -3141,8 +3142,7 @@ static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
if(use_wtf8){
bpath = s3fs_wtf8_decode(bpath);
}
if(param){
auto* pcbparam = reinterpret_cast<SyncFiller*>(param);
if(pcbparam){
struct stat st;
if(StatCache::getStatCacheData()->GetStat(saved_path, &st)){
pcbparam->Fill(bpath.c_str(), &st, 0);
@ -3152,6 +3152,7 @@ static bool multi_head_callback(S3fsCurl* s3fscurl, void* param)
}
}else{
S3FS_PRN_WARN("param(multi_head_callback_param*) is nullptr, then can not call filler.");
return false;
}
return true;
@ -3163,21 +3164,19 @@ struct multi_head_notfound_callback_param
s3obj_list_t notfound_list;
};
static bool multi_head_notfound_callback(S3fsCurl* s3fscurl, void* param)
static bool multi_head_notfound_callback(S3fsCurl* s3fscurl, multi_head_notfound_callback_param* pcbparam)
{
if(!s3fscurl){
return false;
}
S3FS_PRN_INFO("HEAD returned NotFound(404) for %s object, it maybe only the path exists and the object does not exist.", s3fscurl->GetPath().c_str());
if(!param){
if(!pcbparam){
S3FS_PRN_WARN("param(multi_head_notfound_callback_param*) is nullptr, then can not call filler.");
return false;
}
// set path to not found list
auto* pcbparam = reinterpret_cast<struct multi_head_notfound_callback_param*>(param);
const std::lock_guard<std::mutex> lock(pcbparam->list_lock);
pcbparam->notfound_list.push_back(s3fscurl->GetBasePath());
@ -3231,18 +3230,14 @@ static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf
StatCache::getStatCacheData()->GetNotruncateCache(std::string(path), headlist); // Add notruncate file name from stat cache
// Initialize S3fsMultiCurl
curlmulti.SetSuccessCallback(multi_head_callback);
curlmulti.SetRetryCallback(multi_head_retry_callback);
// Success Callback function parameter(SyncFiller object)
SyncFiller syncfiller(buf, filler);
curlmulti.SetSuccessCallbackParam(reinterpret_cast<void*>(&syncfiller));
curlmulti.SetSuccessCallback([&syncfiller](S3fsCurl *s3fsCurl) mutable { return multi_head_callback(s3fsCurl, &syncfiller); });
curlmulti.SetRetryCallback(multi_head_retry_callback);
// Not found Callback function parameter
struct multi_head_notfound_callback_param notfound_param;
if(support_compat_dir){
curlmulti.SetNotFoundCallback(multi_head_notfound_callback);
curlmulti.SetNotFoundCallbackParam(reinterpret_cast<void*>(&notfound_param));
curlmulti.SetNotFoundCallback([&notfound_param](S3fsCurl *s3fsCurl) mutable { return multi_head_notfound_callback(s3fsCurl, &notfound_param); });
}
// Make single head request(with max).