Skip to content

Commit 7b83448

Browse files
committed
增加删除时业务pid信息
1 parent 6b38fb7 commit 7b83448

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

src/curl.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ int S3fsCurl::max_parallel_cnt = 10; // default
311311
off_t S3fsCurl::multipart_size = MULTIPART_SIZE; // default
312312
bool S3fsCurl::is_sigv4 = true; // default
313313
string S3fsCurl::skUserAgent = "tencentyun-cosfs-v5-" + string(VERSION);
314+
bool S3fsCurl::is_client_info_in_delete = false; // default
314315

315316
//-------------------------------------------------------------------
316317
// Class methods for S3fsCurl
@@ -2135,7 +2136,7 @@ bool S3fsCurl::GetUploadId(string& upload_id)
21352136
return result;
21362137
}
21372138

2138-
int S3fsCurl::DeleteRequest(const char* tpath)
2139+
int S3fsCurl::DeleteRequest(const char* tpath, int pid)
21392140
{
21402141
S3FS_PRN_INFO3("[tpath=%s]", SAFESTRPTR(tpath));
21412142

@@ -2163,6 +2164,13 @@ int S3fsCurl::DeleteRequest(const char* tpath)
21632164
string Signature = CalcSignature("DELETE", "", "", date, resource, "");
21642165
requestHeaders = curl_slist_sort_insert(requestHeaders, "Authorization", Signature.c_str());
21652166
}
2167+
if(S3fsCurl::IsClientInfoInDelete()){
2168+
ostringstream pidstr;
2169+
pidstr << pid;
2170+
string clientinfo = S3fsCurl::GetClientInfo(pidstr.str());
2171+
requestHeaders = curl_slist_sort_insert(requestHeaders, "x-delete-client-pid", pidstr.str().c_str());
2172+
requestHeaders = curl_slist_sort_insert(requestHeaders, "x-delete-client-cgroup", clientinfo.c_str());
2173+
}
21662174

21672175
curl_easy_setopt(hCurl, CURLOPT_URL, url.c_str());
21682176
curl_easy_setopt(hCurl, CURLOPT_CUSTOMREQUEST, "DELETE");
@@ -3384,6 +3392,29 @@ int S3fsCurl::MultipartRenameRequest(const char* from, const char* to, headers_t
33843392
return 0;
33853393
}
33863394

3395+
std::string S3fsCurl::GetClientInfo(std::string pid)
3396+
{
3397+
if (pid == "-1") {
3398+
return "can not get pid";
3399+
}
3400+
std::string path = "/proc/" + pid + "/cgroup";
3401+
std::ifstream ifs(path.c_str());
3402+
if (!ifs) {
3403+
S3FS_PRN_ERR("failed to open %s", path.c_str());
3404+
return "failed to open " + path;
3405+
}
3406+
std::string line;
3407+
while (std::getline(ifs, line)) {
3408+
if (line.find("cpu,cpuacct") != std::string::npos) {
3409+
return line;
3410+
}
3411+
if (line.find("cpuacct,cpu") != std::string::npos) {
3412+
return line;
3413+
}
3414+
}
3415+
return "not found cpu,cpuacct";
3416+
}
3417+
33873418
//-------------------------------------------------------------------
33883419
// Class S3fsMultiCurl
33893420
//-------------------------------------------------------------------

src/curl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class S3fsCurl
238238
static off_t multipart_size;
239239
static bool is_sigv4;
240240
static std::string skUserAgent;
241+
static bool is_client_info_in_delete;
241242

242243
// variables
243244
CURL* hCurl;
@@ -370,6 +371,9 @@ class S3fsCurl
370371
static off_t GetMultipartSize(void) { return S3fsCurl::multipart_size; }
371372
static bool SetSignatureV4(bool isset) { bool bresult = S3fsCurl::is_sigv4; S3fsCurl::is_sigv4 = isset; return bresult; }
372373
static bool IsSignatureV4(void) { return S3fsCurl::is_sigv4; }
374+
static void SetClientInfoInDelete(bool is_set) { S3fsCurl::is_client_info_in_delete = is_set; }
375+
static bool IsClientInfoInDelete(void) { return S3fsCurl::is_client_info_in_delete; }
376+
static std::string GetClientInfo(std::string pid);
373377

374378
// methods
375379
bool CreateCurlHandle(bool force = false);
@@ -378,7 +382,7 @@ class S3fsCurl
378382
bool AddSseRequestHead(sse_type_t ssetype, std::string& ssevalue, bool is_only_c, bool is_copy);
379383
bool GetResponseCode(long& responseCode);
380384
int RequestPerform(void);
381-
int DeleteRequest(const char* tpath);
385+
int DeleteRequest(const char* tpath, int pid);
382386
bool PreHeadRequest(const char* tpath, const char* bpath = NULL, const char* savedpath = NULL, int ssekey_pos = -1);
383387
bool PreHeadRequest(std::string& tpath, std::string& bpath, std::string& savedpath, int ssekey_pos = -1) {
384388
return PreHeadRequest(tpath.c_str(), bpath.c_str(), savedpath.c_str(), ssekey_pos);

src/s3fs.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,16 +1025,18 @@ static int s3fs_unlink(const char* path)
10251025

10261026
S3FS_PRN_INFO("[path=%s]", path);
10271027

1028+
int pid = -1;
10281029
struct fuse_context* pcxt;
10291030
if(NULL != (pcxt = fuse_get_context())){
1031+
pid = pcxt->pid;
10301032
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
10311033
}
10321034

10331035
if(0 != (result = check_parent_object_access(path, W_OK | X_OK))){
10341036
return result;
10351037
}
10361038
S3fsCurl s3fscurl;
1037-
result = s3fscurl.DeleteRequest(path);
1039+
result = s3fscurl.DeleteRequest(path, pid);
10381040
FdManager::DeleteCacheFile(path);
10391041
StatCache::getStatCacheData()->DelStat(path);
10401042
S3FS_MALLOCTRIM(0);
@@ -1065,8 +1067,10 @@ static int s3fs_rmdir(const char* path)
10651067

10661068
S3FS_PRN_INFO("[path=%s]", path);
10671069

1070+
int pid = -1;
10681071
struct fuse_context* pcxt;
10691072
if(NULL != (pcxt = fuse_get_context())){
1073+
pid = pcxt->pid;
10701074
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
10711075
}
10721076

@@ -1084,7 +1088,7 @@ static int s3fs_rmdir(const char* path)
10841088
strpath += "/";
10851089
}
10861090
S3fsCurl s3fscurl;
1087-
result = s3fscurl.DeleteRequest(strpath.c_str());
1091+
result = s3fscurl.DeleteRequest(strpath.c_str(), pid);
10881092
s3fscurl.DestroyCurlHandle();
10891093
StatCache::getStatCacheData()->DelStat(strpath.c_str());
10901094

@@ -1099,7 +1103,7 @@ static int s3fs_rmdir(const char* path)
10991103
if(0 == get_object_attribute(strpath.c_str(), &stbuf, NULL, false)){
11001104
if(S_ISDIR(stbuf.st_mode)){
11011105
// Found "dir" object.
1102-
result = s3fscurl.DeleteRequest(strpath.c_str());
1106+
result = s3fscurl.DeleteRequest(strpath.c_str(), pid);
11031107
s3fscurl.DestroyCurlHandle();
11041108
StatCache::getStatCacheData()->DelStat(strpath.c_str());
11051109
}
@@ -1111,7 +1115,7 @@ static int s3fs_rmdir(const char* path)
11111115
// This processing is necessary for other OSS clients compatibility.
11121116
if(is_special_name_folder_object(strpath.c_str())){
11131117
strpath += "_$folder$";
1114-
result = s3fscurl.DeleteRequest(strpath.c_str());
1118+
result = s3fscurl.DeleteRequest(strpath.c_str(), pid);
11151119
}
11161120
S3FS_MALLOCTRIM(0);
11171121

@@ -1566,8 +1570,10 @@ static int s3fs_chmod(const char* path, mode_t mode)
15661570

15671571
S3FS_PRN_INFO("[path=%s][mode=%04o]", path, mode);
15681572

1573+
int pid = -1;
15691574
struct fuse_context* pcxt;
15701575
if(NULL != (pcxt = fuse_get_context())){
1576+
pid = pcxt->pid;
15711577
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
15721578
}
15731579

@@ -1600,7 +1606,7 @@ static int s3fs_chmod(const char* path, mode_t mode)
16001606
// At first, remove directory old object
16011607
if(IS_RMTYPEDIR(nDirType)){
16021608
S3fsCurl s3fscurl;
1603-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
1609+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
16041610
return result;
16051611
}
16061612
}
@@ -1665,8 +1671,10 @@ static int s3fs_chmod_nocopy(const char* path, mode_t mode)
16651671

16661672
S3FS_PRN_INFO1("[path=%s][mode=%04o]", path, mode);
16671673

1674+
int pid = -1;
16681675
struct fuse_context* pcxt;
16691676
if(NULL != (pcxt = fuse_get_context())){
1677+
pid = pcxt->pid;
16701678
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
16711679
}
16721680

@@ -1700,7 +1708,7 @@ static int s3fs_chmod_nocopy(const char* path, mode_t mode)
17001708
// At first, remove directory old object
17011709
if(IS_RMTYPEDIR(nDirType)){
17021710
S3fsCurl s3fscurl;
1703-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
1711+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
17041712
return result;
17051713
}
17061714
}
@@ -1750,8 +1758,10 @@ static int s3fs_chown(const char* path, uid_t uid, gid_t gid)
17501758

17511759
S3FS_PRN_INFO("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid);
17521760

1761+
int pid = -1;
17531762
struct fuse_context* pcxt;
17541763
if(NULL != (pcxt = fuse_get_context())){
1764+
pid = pcxt->pid;
17551765
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
17561766
}
17571767

@@ -1799,7 +1809,7 @@ static int s3fs_chown(const char* path, uid_t uid, gid_t gid)
17991809
// At first, remove directory old object
18001810
if(IS_RMTYPEDIR(nDirType)){
18011811
S3fsCurl s3fscurl;
1802-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
1812+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
18031813
return result;
18041814
}
18051815
}
@@ -1864,8 +1874,10 @@ static int s3fs_chown_nocopy(const char* path, uid_t uid, gid_t gid)
18641874
int nDirType = DIRTYPE_UNKNOWN;
18651875

18661876
S3FS_PRN_INFO1("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid);
1877+
int pid = -1;
18671878
struct fuse_context* pcxt;
18681879
if(NULL != (pcxt = fuse_get_context())){
1880+
pid = pcxt->pid;
18691881
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
18701882
}
18711883

@@ -1908,7 +1920,7 @@ static int s3fs_chown_nocopy(const char* path, uid_t uid, gid_t gid)
19081920
// At first, remove directory old object
19091921
if(IS_RMTYPEDIR(nDirType)){
19101922
S3fsCurl s3fscurl;
1911-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
1923+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
19121924
return result;
19131925
}
19141926
}
@@ -1958,8 +1970,10 @@ static int s3fs_utimens(const char* path, const struct timespec ts[2])
19581970
int nDirType = DIRTYPE_UNKNOWN;
19591971

19601972
S3FS_PRN_INFO("[path=%s][mtime=%jd]", path, (intmax_t)(ts[1].tv_sec));
1973+
int pid = -1;
19611974
struct fuse_context* pcxt;
19621975
if(NULL != (pcxt = fuse_get_context())){
1976+
pid = pcxt->pid;
19631977
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
19641978
}
19651979

@@ -1994,7 +2008,7 @@ static int s3fs_utimens(const char* path, const struct timespec ts[2])
19942008
// At first, remove directory old object
19952009
if(IS_RMTYPEDIR(nDirType)){
19962010
S3fsCurl s3fscurl;
1997-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
2011+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
19982012
return result;
19992013
}
20002014
}
@@ -2056,8 +2070,10 @@ static int s3fs_utimens_nocopy(const char* path, const struct timespec ts[2])
20562070
int nDirType = DIRTYPE_UNKNOWN;
20572071

20582072
S3FS_PRN_INFO1("[path=%s][mtime=%s]", path, str(ts[1].tv_sec).c_str());
2073+
int pid = -1;
20592074
struct fuse_context* pcxt;
20602075
if(NULL != (pcxt = fuse_get_context())){
2076+
pid = pcxt->pid;
20612077
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
20622078
}
20632079

@@ -2093,7 +2109,7 @@ static int s3fs_utimens_nocopy(const char* path, const struct timespec ts[2])
20932109
// At first, remove directory old object
20942110
if(IS_RMTYPEDIR(nDirType)){
20952111
S3fsCurl s3fscurl;
2096-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
2112+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
20972113
return result;
20982114
}
20992115
}
@@ -3223,6 +3239,13 @@ static int s3fs_setxattr(const char* path, const char* name, const char* value,
32233239
}
32243240
#endif
32253241

3242+
int pid = -1;
3243+
struct fuse_context* pcxt;
3244+
if(NULL != (pcxt = fuse_get_context())){
3245+
pid = pcxt->pid;
3246+
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
3247+
}
3248+
32263249
int result;
32273250
string strpath;
32283251
string newpath;
@@ -3265,7 +3288,7 @@ static int s3fs_setxattr(const char* path, const char* name, const char* value,
32653288
// At first, remove directory old object
32663289
if(IS_RMTYPEDIR(nDirType)){
32673290
S3fsCurl s3fscurl;
3268-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
3291+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
32693292
return result;
32703293
}
32713294
}
@@ -3497,8 +3520,10 @@ static int s3fs_listxattr(const char* path, char* list, size_t size)
34973520
static int s3fs_removexattr(const char* path, const char* name)
34983521
{
34993522
S3FS_PRN_INFO("[path=%s][name=%s]", path, name);
3523+
int pid = -1;
35003524
struct fuse_context* pcxt;
35013525
if(NULL != (pcxt = fuse_get_context())){
3526+
pid = pcxt->pid;
35023527
S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid);
35033528
}
35043529

@@ -3575,7 +3600,7 @@ static int s3fs_removexattr(const char* path, const char* name)
35753600
// At first, remove directory old object
35763601
if(IS_RMTYPEDIR(nDirType)){
35773602
S3fsCurl s3fscurl;
3578-
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){
3603+
if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){
35793604
free_xattrs(xattrs);
35803605
return result;
35813606
}
@@ -4633,6 +4658,10 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar
46334658
FdManager::SetCacheDir(strchr(arg, '=') + sizeof(char));
46344659
return 0;
46354660
}
4661+
if(0 == STR2NCMP(arg, "enable_clientinfo")){
4662+
S3fsCurl::SetClientInfoInDelete(true);
4663+
return 0;
4664+
}
46364665
if(0 == strcmp(arg, "del_cache")){
46374666
is_remove_cache = true;
46384667
return 0;

0 commit comments

Comments
 (0)