1#include "base.h" 2 3extern string ftp_url; 4extern Config ftp_config,ftp_config_stop; 5extern Server_TreeNode *ServerNode_del; 6extern sync_list **g_pSyncList; 7extern _info LOCAL_FILE,SERVER_FILE; 8extern int upload_break; 9extern int upload_file_lost; 10extern int del_acount; 11 12int code_convert(char *from_charset,char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen) 13{ 14 iconv_t cd; 15 char **pin = &inbuf; 16 char **pout = &outbuf; 17 18 cd = iconv_open(to_charset,from_charset); 19 20 if(cd == (iconv_t)-1) 21 return -1; 22 memset(outbuf,0,outlen); 23 24 if(iconv(cd,pin,&inlen,pout,&outlen) == -1) 25 return -1; 26 27 iconv_close(cd); 28 return 0; 29} 30 31char *to_utf8(char *buf,int index) 32{ 33 int status = 0; 34 char *out = my_str_malloc(256); 35 if(ftp_config.multrule[index]->encoding == 0) 36 { 37 sprintf(out,"%s",buf); 38 return out; 39 } 40 else if(ftp_config.multrule[index]->encoding == 1) 41 { 42 DEBUG("converting from GB18030 to utf-8...%s\n",buf); 43 status = code_convert("GB18030","utf-8",buf,strlen(buf),out,OUTLEN); 44 if(status == 0) 45 { 46 DEBUG("convert ok\n"); 47 return out; 48 }else{ 49 DEBUG("convert failed\n"); 50 sprintf(out,"%s",buf); 51 return out; 52 } 53 } 54 else if(ftp_config.multrule[index]->encoding == 2) 55 { 56 DEBUG("converting from BIG5 to utf-8...%s\n",buf); 57 status = code_convert("BIG5","utf-8",buf,strlen(buf),out,OUTLEN); 58 if(status == 0) 59 { 60 DEBUG("convert ok\n"); 61 return out; 62 }else{ 63 DEBUG("convert failed\n"); 64 sprintf(out,"%s",buf); 65 return out; 66 } 67 } 68} 69 70char *utf8_to(char *buf,int index) 71{ 72 int status = 0; 73 char *out = my_str_malloc(256); 74 if(ftp_config.multrule[index]->encoding == 0) 75 { 76 sprintf(out,"%s",buf); 77 return out; 78 } 79 else if(ftp_config.multrule[index]->encoding == 1) 80 { 81 DEBUG("converting from utf-8 to GB18030...%s\n",buf); 82 status = code_convert("utf-8","GB18030",buf,strlen(buf),out,OUTLEN); 83 if(status == 0) 84 { 85 DEBUG("convert ok\n"); 86 return out; 87 }else{ 88 DEBUG("convert failed\n"); 89 sprintf(out,"%s",buf); 90 return out; 91 } 92 } 93 else if(ftp_config.multrule[index]->encoding == 2) 94 { 95 DEBUG("converting from utf-8 to BIG5...%s\n",buf); 96 status = code_convert("utf-8","BIG5",buf,strlen(buf),out,OUTLEN); 97 if(status == 0) 98 { 99 DEBUG("convert ok\n"); 100 return out; 101 }else{ 102 DEBUG("convert failed\n"); 103 sprintf(out,"%s",buf); 104 return out; 105 } 106 } 107} 108 109int download_(char *serverpath,int index) 110{ 111 DEBUG("%s\n",serverpath); 112 char *temp = serverpath_to_localpath(serverpath,index); 113 char *where_it_put = (char*)malloc(sizeof(char)*(strlen(temp) + 9)); 114 memset(where_it_put,'\0',sizeof(char)*(strlen(temp) + 9)); 115 sprintf(where_it_put,"%s%s",temp,".asus.td"); 116 117 char *chr_coding = utf8_to(serverpath,index); 118 char *url_coding = oauth_url_escape(chr_coding); 119 char *where_it_from = (char*)malloc(sizeof(char)*(ftp_config.multrule[index]->server_ip_len + strlen(url_coding) + 2)); 120 memset(where_it_from,'\0',sizeof(char)*(ftp_config.multrule[index]->server_ip_len + strlen(url_coding) + 2)); 121 sprintf(where_it_from,"%s/%s",ftp_config.multrule[index]->server_ip,url_coding); 122 DEBUG("from:%s\n",where_it_from); 123 DEBUG("put:%s\n",where_it_put); 124 free(url_coding); 125 126 write_log(S_DOWNLOAD,"",serverpath,index); 127 128#if PROGRESSBAR 129 char *progress_data = "*"; 130#endif 131 CURL *curl; 132 CURLcode res; 133 134 FILE *fp; 135 curl_off_t local_file_len = -1; 136 struct stat info; 137 int use_resume = 0; 138 if(stat(where_it_put,&info) == 0) 139 { 140 local_file_len = info.st_size; 141 use_resume = 1; 142 } 143 fp = fopen(where_it_put,"ab+"); 144 if(fp == NULL) 145 { 146 return -1; 147 } 148 149 if(SERVER_FILE.path != NULL) 150 free(SERVER_FILE.path); 151 SERVER_FILE.path = (char*)malloc(sizeof(char)*(strlen(temp) + 1)); 152 sprintf(SERVER_FILE.path,"%s",temp); 153 SERVER_FILE.index = index; 154 155 curl = curl_easy_init(); 156 if(curl) 157 { 158#if PROGRESSBAR 159 curl_easy_setopt(curl, CURLOPT_NOPROGRESS,0L); 160 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,my_progress_func); 161 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data); 162#endif 163 curl_easy_setopt(curl, CURLOPT_URL, where_it_from); 164 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 165 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 166 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30); 167 curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0); 168 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); 169 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); 170 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1); 171 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 30); 172 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 173 res = curl_easy_perform(curl); 174 DEBUG("\nres:%d\n",res); 175 if(res != CURLE_OK && res != CURLE_PARTIAL_FILE && res != CURLE_OPERATION_TIMEDOUT) 176 { 177 fclose(fp); 178 free(temp); 179 free(where_it_from); 180 write_log(S_ERROR,"Download failed","",index); 181 curl_easy_cleanup(curl); 182 if(del_acount == index) 183 unlink(where_it_put); 184 free(where_it_put); 185 return -1; 186 } 187 else 188 { 189 fclose(fp); 190 DEBUG("rename .asus.td to filename.\n"); 191 rename(where_it_put,temp); 192 free(temp); 193 free(where_it_put); 194 free(where_it_from); 195 curl_easy_cleanup(curl); 196 return 0; 197 } 198 } 199 else 200 { 201 fclose(fp); 202 free(temp); 203 free(where_it_put); 204 free(where_it_from); 205 return 0; 206 } 207} 208 209int upload(char *localpath1,int index) 210{ 211#if PROGRESSBAR 212 char *progress_data = "*"; 213#endif 214 if(access(localpath1,F_OK) != 0) 215 { 216 DEBUG("Local has no %s\n",localpath1); 217 return LOCAL_FILE_LOST;//902 218 } 219 write_log(S_UPLOAD,"",localpath1,index); 220 221 char *serverpath = localpath_to_serverpath(localpath1,index); 222 char *chr_coding = utf8_to(serverpath,index); 223 free(serverpath); 224 char *url_coding = oauth_url_escape(chr_coding); 225 226 char *fullserverpath = (char*)malloc(sizeof(char)*(ftp_config.multrule[index]->server_ip_len + strlen(url_coding) + 2)); 227 memset(fullserverpath,'\0',sizeof(char)*(ftp_config.multrule[index]->server_ip_len + strlen(url_coding) + 2)); 228 sprintf(fullserverpath,"%s/%s",ftp_config.multrule[index]->server_ip,url_coding); 229 230 free(url_coding); 231 CURL *curl; 232 CURLcode res; 233 struct stat info; 234 if(stat(localpath1,&info) == -1) 235 return LOCAL_FILE_LOST;//902 236 237 FILE *g_stream,*fp; 238 g_stream = fopen(localpath1,"rb"); 239 if(g_stream == NULL) 240 return LOCAL_FILE_LOST;//902 241 242 fp = fopen(CURL_HEAD,"wb"); 243 244 if(LOCAL_FILE.path != NULL) 245 free(LOCAL_FILE.path); 246 LOCAL_FILE.path = (char*)malloc(sizeof(char)*(strlen(localpath1) + 1)); 247 //2014.10.29 by sherry 未初始化 248 memset(LOCAL_FILE.path,'\0',sizeof(char)*(strlen(localpath1) + 1)); 249 sprintf(LOCAL_FILE.path,"%s",localpath1); 250 LOCAL_FILE.index = index; 251 252 curl = curl_easy_init(); 253 if(curl) 254 { 255#if PROGRESSBAR 256 curl_easy_setopt(curl, CURLOPT_NOPROGRESS,0L); 257 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,my_progress_func); 258 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data); 259#endif 260 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 261 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)info.st_size); 262 curl_easy_setopt(curl, CURLOPT_URL, fullserverpath); 263 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 264 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 265 curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); 266 curl_easy_setopt(curl, CURLOPT_READDATA, g_stream); 267 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, fp); 268 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); 269 //curl_easy_setopt(curl, CURLOPT_FTPPORT, "-"); 270 //curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); 271 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,1); 272 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,30); 273 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 274 curl_easy_setopt(curl,CURLOPT_TIMEOUT,90);//设置一个长整形数,作为最大延续多少秒 275 //curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debugFun); 276 //curl_easy_setopt(curl, CURLOPT_DEBUGDATA, debugFile); 277 res = curl_easy_perform(curl); 278 DEBUG("\nres = %d\n",res); 279 free(fullserverpath); 280 //2014.11.06 by sherry 281 //if(res != CURLE_OK && res != CURLE_OPERATION_TIMEDOUT) 282 if(res != CURLE_OK) 283 { 284 curl_easy_cleanup(curl); 285 fclose(fp); 286 fclose(g_stream); 287 if(upload_break == 1)//程序退出 288 { 289 FILE *f_stream = fopen(g_pSyncList[index]->up_item_file, "w"); 290 if(f_stream == NULL) 291 { 292 DEBUG("open %s error\n", g_pSyncList[index]->up_item_file); 293 return res; 294 } 295 fprintf(f_stream,"%s",localpath1); 296 fclose(f_stream); 297 upload_break = 0; 298 return res; 299 } 300 if(upload_file_lost == 1)//上传的文件丢失 301 { 302 upload_file_lost = 0; 303 return 0; 304 } 305 //2014.11.07 by sherry 306 if(res==55)//CURLE_SEND_ERROR 307 { 308 Delete(localpath1,index); 309 action_item *item; 310 item = get_action_item("upload",localpath1,g_pSyncList[index]->unfinished_list,index); 311 if(item == NULL) 312 { 313 add_action_item("upload",localpath1,g_pSyncList[index]->unfinished_list); 314 } 315 write_log(S_ERROR,"failed sending network data","",index); 316 return CURLE_SEND_ERROR; 317 } 318 if(res==28)//CURLE_OPERATION_TIMEDOUT 319 { 320 Delete(localpath1,index); 321 action_item *item; 322 item = get_action_item("upload",localpath1,g_pSyncList[index]->unfinished_list,index); 323 if(item == NULL) 324 { 325 add_action_item("upload",localpath1,g_pSyncList[index]->unfinished_list); 326 } 327 write_log(S_ERROR,"the timeout time was reached","",index); 328 return CURLE_OPERATION_TIMEDOUT; 329 } 330 331 FILE *fp1 = fopen(CURL_HEAD,"rb"); 332 char buff[512]={0}; 333 char *p; 334 while(fgets(buff,sizeof(buff),fp1) != NULL) 335 { 336 p = strstr(buff,"Permission denied"); 337 if(p != NULL) 338 { 339 write_log(S_ERROR,"Permission Denied!","",index); 340 fclose(fp1); 341 return PERMISSION_DENIED; 342 } 343 } 344 fclose(fp1); 345 return -1; 346 } 347 else 348 { 349 DEBUG("uploading ok!\n"); 350 del_action_item("upload",localpath1,g_pSyncList[index]->unfinished_list); 351 curl_easy_cleanup(curl); 352 fclose(fp); 353 fclose(g_stream); 354 return 0; 355 } 356 } 357 else 358 { 359 fclose(fp); 360 free(fullserverpath); 361 return 0; 362 } 363} 364 365int my_delete_DELE(char *localpath,int index) 366{ 367 int status; 368 char *tmp = get_prepath(localpath,strlen(localpath)); 369 status = is_server_exist(tmp,localpath,index); 370 free(tmp); 371 if(status) 372 { 373 char *serverpath = localpath_to_serverpath(localpath,index); 374 char *command = (char *)malloc(sizeof(char)*(strlen(serverpath) + 6)); 375 memset(command,'\0',sizeof(command)); 376 sprintf(command,"DELE %s",serverpath); 377 DEBUG("%s\n",command); 378 char *temp = utf8_to(command,index); 379 free(command); 380 FILE *fp = fopen(CURL_HEAD,"w"); 381 CURL *curl; 382 CURLcode res; 383 curl = curl_easy_init(); 384 if(curl) 385 { 386 curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 387 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 388 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 389 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,temp); 390 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, fp); 391 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1); 392 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,30); 393 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 394 res = curl_easy_perform(curl); 395 DEBUG("\nres = %d\n",res); 396 if(res != CURLE_OK) 397 { 398 curl_easy_cleanup(curl); 399 free(temp); 400 fclose(fp); 401 free(serverpath); 402 FILE *fp1 = fopen(CURL_HEAD,"r"); 403 char buff[512]={0}; 404 const char *split=" "; 405 char *p; 406 while(fgets(buff,sizeof(buff),fp1) != NULL) 407 { 408 p=strtok(buff,split); 409 if(atoi(p) == 250) 410 { 411 DEBUG("delete file ok!\n"); 412 fclose(fp1); 413 return 0; 414 } 415 } 416 fclose(fp1); 417 return -1; 418 } 419 else 420 { 421 curl_easy_cleanup(curl); 422 free(temp);//free(command); 423 fclose(fp); 424 free(serverpath); 425 return 0; 426 } 427 } 428 else 429 { 430 free(temp);//free(command); 431 free(serverpath); 432 fclose(fp); 433 return 0; 434 } 435 } 436 else{ 437 return 0; 438 } 439} 440int my_delete_RMD(char *localpath,int index) 441{ 442 int status; 443 char *tmp = get_prepath(localpath,strlen(localpath)); 444 status = is_server_exist(tmp,localpath,index); 445 free(tmp); 446 if(status) 447 { 448 char *serverpath = localpath_to_serverpath(localpath,index); 449 char *command = (char *)malloc(sizeof(char)*(strlen(serverpath) + 5)); 450 memset(command,'\0',sizeof(command)); 451 sprintf(command,"RMD %s",serverpath); 452 DEBUG("%s\n",command); 453 char *temp = utf8_to(command,index); 454 free(command); 455 FILE *fp = fopen(CURL_HEAD,"w"); 456 CURL *curl; 457 CURLcode res; 458 curl = curl_easy_init(); 459 if(curl) 460 { 461 curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 462 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 463 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 464 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,temp); 465 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, fp); 466 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1); 467 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,30); 468 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 469 res = curl_easy_perform(curl); 470 DEBUG("\nres = %d\n",res); 471 if(res != CURLE_OK) 472 { 473 curl_easy_cleanup(curl); 474 free(temp);//free(command); 475 fclose(fp); 476 free(serverpath); 477 FILE *fp1 = fopen(CURL_HEAD,"r"); 478 char buff[512]={0}; 479 const char *split=" "; 480 char *p; 481 while(fgets(buff,sizeof(buff),fp1) != NULL) 482 { 483 p = strstr(buff,"Permission denied"); 484 if(p != NULL) 485 { 486 write_log(S_ERROR,"Permission denied","",index); 487 fclose(fp1); 488 return PERMISSION_DENIED; 489 } 490 p=strtok(buff,split); 491 if(atoi(p) == 250) 492 { 493 DEBUG("delete folder ok!\n"); 494 fclose(fp1); 495 return 0; 496 } 497 } 498 return -1; 499 } 500 else 501 { 502 curl_easy_cleanup(curl); 503 free(temp);//free(command); 504 fclose(fp); 505 free(serverpath); 506 return 0; 507 } 508 } 509 else 510 { 511 free(temp);//free(command); 512 fclose(fp); 513 free(serverpath); 514 curl_easy_cleanup(curl); 515 return 0; 516 } 517 } 518 else 519 { 520 return 0; 521 } 522} 523void SearchServerTree_(Server_TreeNode* treeRoot,int index) 524{ 525 DEBUG("#################### SsarchServerTree ###################\n"); 526 int i; 527 //int j; 528 for(i=0;i<treeRoot->level;i++) 529 DEBUG("-"); 530 531 if((treeRoot->Child!=NULL)) 532 SearchServerTree_(treeRoot->Child,index); 533 534 if(treeRoot->NextBrother != NULL) 535 SearchServerTree_(treeRoot->NextBrother,index); 536 537 if(treeRoot->browse != NULL) 538 { 539 CloudFile *de_foldercurrent,*de_filecurrent; 540 de_foldercurrent = treeRoot->browse->folderlist->next; 541 de_filecurrent = treeRoot->browse->filelist->next; 542 while(de_foldercurrent != NULL) 543 { 544 //DEBUG("serverfolder->href = %s\n",de_foldercurrent->href); 545 char *temp = serverpath_to_localpath(de_foldercurrent->href,index); 546 my_delete_RMD(temp,index); 547 free(temp); 548 de_foldercurrent = de_foldercurrent->next; 549 } 550 while(de_filecurrent != NULL) 551 { 552 //DEBUG("serverfile->href = %s\n",de_filecurrent->href); 553 char *temp = serverpath_to_localpath(de_filecurrent->href,index); 554 my_delete_DELE(temp,index); 555 free(temp); 556 de_filecurrent = de_filecurrent->next; 557 } 558 } 559} 560int Delete(char *localpath,int index) 561{ 562 int flag = my_delete_DELE(localpath,index); 563 if(flag != 0) 564 { 565 char *serverpath = localpath_to_serverpath(localpath,index); 566 ServerNode_del = create_server_treeroot(); 567 browse_to_tree(serverpath,ServerNode_del,index); 568 free(serverpath); 569 SearchServerTree_(ServerNode_del,index); 570 free_server_tree(ServerNode_del); 571 flag = my_delete_RMD(localpath,index); 572 } 573 //my_delete_RMD(localpath,index); 574 return flag; 575} 576 577int my_rename_(char *localpath,char *newername,int index) 578{ 579 char *prepath = get_prepath(localpath,strlen(localpath)); 580 char *oldname = strstr(localpath,prepath) + strlen(prepath); 581 char *cd_path = localpath_to_serverpath(prepath,index); 582 583 char *str1 = my_str_malloc(strlen(cd_path) + 5); 584 char *str2 = my_str_malloc(strlen(oldname) + 6); 585 char *str3 = my_str_malloc(strlen(newername) + 6); 586 587 sprintf(str1,"CWD %s",cd_path); 588 sprintf(str2,"RNFR %s",oldname + 1); 589 sprintf(str3,"RNTO %s",newername); 590 591 char *A = utf8_to(str1,index); 592 char *B = utf8_to(str2,index); 593 char *C = utf8_to(str3,index); 594 free(str1); 595 free(str2); 596 free(str3); 597 struct curl_slist *headerlist = NULL; 598 CURL *curl; 599 CURLcode res; 600 curl = curl_easy_init(); 601 if(curl)//curl初始化成功 602 { 603 headerlist = curl_slist_append(headerlist,A); 604 headerlist = curl_slist_append(headerlist,B); 605 headerlist = curl_slist_append(headerlist,C); 606 607 curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 608 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 609 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 610 curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); 611 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1); 612 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 30); 613 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 614 res=curl_easy_perform(curl); 615 if(res != CURLE_OK) 616 { //2014.10.30 by sherry 处理“rename会挂掉"的bug 617// char *temp = (char*)malloc(sizeof(char)*(strlen(prepath) + strlen(newername) + 1)); 618// memset(temp,'\0',sizeof(char)*(strlen(prepath) + strlen(newername) + 1)); 619// sprintf(temp,"%s/%s",prepath,newername); 620 char *temp = (char*)malloc(sizeof(char)*(strlen(prepath) + strlen(newername) + 2)); 621 memset(temp,'\0',sizeof(char)*(strlen(prepath) + strlen(newername) + 2)); 622 sprintf(temp,"%s/%s",prepath,newername); 623 int status = 0; 624 if(!test_if_dir(temp))//test_if_dir如果是folder则返回1 625 { 626 //file 627 status = upload(temp,index); 628 } 629 else 630 { 631 //folder 632 status = moveFolder(localpath,temp,index); 633 } 634 free(A); 635 free(B); 636 free(C); 637 free(temp); 638 free(prepath); 639 free(cd_path); 640 //free(newername);形参,函数开始调用时分配动态存储空间,函数结束时释放 641 curl_slist_free_all(headerlist); 642 curl_easy_cleanup(curl); 643 return status; 644 } 645 else 646 { 647 free(A); 648 free(B); 649 free(C); 650 free(prepath); 651 free(cd_path); 652 curl_slist_free_all(headerlist); 653 curl_easy_cleanup(curl); 654 return 0; 655 } 656 } 657 else 658 { 659 free(A); 660 free(B); 661 free(C); 662 free(prepath); 663 free(cd_path); 664 return 0; 665 } 666} 667 668int my_mkdir_(char *localpath,int index) 669{ 670 if(access(localpath,0) != 0)//检查本地端文件是否存在, 671 //如果文件存在,返回0,不存在,返回-1 672 { 673 DEBUG("Local has no %s\n",localpath); 674 return LOCAL_FILE_LOST; 675 } 676 677// if(strcmp(localpath,ftp_config.multrule[index]->base_path) == 0) 678// { 679// write_log(S_ERROR,"Server Del Sync Folder!","",index); 680// return SERVER_ROOT_DELETED; 681// } 682 683 char *serverpath = localpath_to_serverpath(localpath,index); 684 char *command = (char *)malloc(sizeof(char)*(strlen(serverpath) + 5)); 685 memset(command,'\0',sizeof(command)); 686 sprintf(command,"MKD %s",serverpath); 687 DEBUG("%s\n",command); 688 char *temp = utf8_to(command,index); 689 free(command); 690 FILE *fp = fopen(CURL_HEAD,"w"); 691 CURL *curl; 692 CURLcode res; 693 curl = curl_easy_init(); 694 if(curl) 695 { 696 curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 697 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 698 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 699 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, temp); 700 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, fp); 701 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1); 702 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 30); 703 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 704 res = curl_easy_perform(curl); 705 DEBUG("res:%d\n",res); 706 if(res != CURLE_OK) 707 { 708 curl_easy_cleanup(curl); 709 fclose(fp); 710 free(temp);//free(command); 711 free(serverpath); 712 FILE *fp1 = fopen(CURL_HEAD,"rb"); 713 char buff[512]={0}; 714 const char *split=" "; 715 char *p = NULL; 716 int i = 0; 717 while(fgets(buff,sizeof(buff),fp1) != NULL) 718 { 719 p = strstr(buff,"Permission denied"); 720 if(p != NULL) 721 { 722 write_log(S_ERROR,"Permission denied","",index); 723 fclose(fp1); 724 return PERMISSION_DENIED; 725 } 726 p=strtok(buff,split); 727 if(atoi(p) == 257) 728 { 729 i++; 730 if(i > 1){ 731 DEBUG("Create Ok.\n"); 732 fclose(fp1); 733 return 0; 734 } 735 } 736 } 737 fclose(fp1); 738 write_log(S_ERROR,"Make Dir failed.","",index); 739 return -1; 740 } 741 else 742 { 743 curl_easy_cleanup(curl); 744 fclose(fp); 745 free(temp);//free(command); 746 free(serverpath); 747 return 0; 748 } 749 } 750 else 751 { 752 fclose(fp); 753 free(serverpath); 754 free(temp);//free(command); 755 return 0; 756 } 757} 758 759int moveFolder(char *old_dir,char *new_dir,int index) 760{ 761 //my_delete(old_dir,index); 762 Delete(old_dir,index); 763 int status; 764 765 struct dirent *ent = NULL; 766 DIR *pDir; 767 pDir=opendir(new_dir); 768 769 if(pDir != NULL ) 770 { 771 status = my_mkdir_(new_dir,index); 772 if(status != 0) 773 { 774 DEBUG("Create %s failed\n",new_dir); 775 closedir(pDir); 776 return status; 777 } 778 779 while ((ent=readdir(pDir)) != NULL) 780 { 781 if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) 782 continue; 783 784 char *old_fullname = my_str_malloc(strlen(old_dir)+strlen(ent->d_name)+2); 785 char *new_fullname = my_str_malloc(strlen(new_dir)+strlen(ent->d_name)+2); 786 787 sprintf(new_fullname,"%s/%s",new_dir,ent->d_name); 788 sprintf(old_fullname,"%s/%s",old_dir,ent->d_name); 789 790 if(socket_check(new_dir,ent->d_name,index) == 1) 791 continue; 792 if(test_if_dir(new_fullname) == 1) 793 { 794 status = moveFolder(old_fullname,new_fullname,index); 795 if(status != 0) 796 { 797 DEBUG("CreateFolder %s failed\n",new_fullname); 798 free(new_fullname); 799 free(old_fullname); 800 closedir(pDir); 801 return status; 802 } 803 } 804 else 805 { 806 status = upload(new_fullname,index); 807 if(status != 0) 808 { 809 DEBUG("move %s failed\n",new_fullname); 810 free(new_fullname); 811 free(old_fullname); 812 closedir(pDir); 813 return status; 814 } 815 else 816 { 817 char *serverpath = localpath_to_serverpath(new_fullname,index); 818 mod_time *onefiletime = Getmodtime(serverpath,index); 819 free(serverpath); 820 if(ChangeFile_modtime(new_fullname,onefiletime->modtime,index)) 821 { 822 DEBUG("ChangeFile_modtime failed!\n"); 823 } 824 } 825 } 826 free(new_fullname); 827 free(old_fullname); 828 829 } 830 closedir(pDir); 831 return 0; 832 } 833 else 834 { 835 DEBUG("open %s fail \n",new_dir); 836 return LOCAL_FILE_LOST; 837 } 838} 839 840int socket_check(char *dir,char *name,int index) 841{ 842 printf("here is socket check\n"); 843 int res = 0; 844 char *dir1,*dir2; 845 846 char *sock_buf; 847 char *part_one; 848 char *part_two; 849 char *part_three; 850 char *part_four; 851 852 char *p = NULL; 853 char *q = NULL; 854 855 //char *fullpath = my_str_malloc(strlen(dir) + strlen(name) + 1);//2014.11.5 by sherry 856 char *fullpath = my_str_malloc(strlen(dir) + strlen(name) + 2);//内存分配不足 857 sprintf(fullpath,"%s/%s",dir,name); 858 859 DEBUG("fullpath:%s\n",fullpath); 860 861 Node *pTemp = g_pSyncList[index]->SocketActionList->next; 862 while(pTemp != NULL) 863 { 864 printf("%s\n",pTemp->cmdName); 865 if(!strncmp(pTemp->cmdName,"move",4) || !strncmp(pTemp->cmdName,"rename",6)) 866 { 867 sock_buf = my_str_malloc(strlen(pTemp->cmdName) + 1); 868 sprintf(sock_buf,"%s",pTemp->cmdName); 869 char *ret = strchr(sock_buf,'\n'); 870 part_one = my_str_malloc(strlen(sock_buf) - strlen(ret) + 1); 871 snprintf(part_one,strlen(sock_buf) - strlen(ret) + 1,"%s",sock_buf); 872 char *ret1 = strchr(ret + 1,'\n'); 873 part_two = my_str_malloc(strlen(ret + 1) - strlen(ret1) + 1); 874 snprintf(part_two,strlen(ret + 1) - strlen(ret1) + 1,"%s",ret + 1); 875 char *ret2 = strchr(ret1 + 1,'\n'); 876 part_three = my_str_malloc(strlen(ret1 + 1) - strlen(ret2) + 1); 877 snprintf(part_three,strlen(ret1 + 1) - strlen(ret2) + 1,"%s",ret1 + 1); 878 part_four = my_str_malloc(strlen(ret2 + 1) + 1); 879 snprintf(part_four,strlen(ret2 + 1) + 1,"%s",ret2 + 1); 880 881 printf("1:%s\n",part_one); 882 printf("2:%s\n",part_two); 883 printf("3:%s\n",part_three); 884 printf("4:%s\n",part_four); 885 886 if(!strncmp(part_one,"move",4)) 887 { 888 dir1 = my_str_malloc(strlen(part_two) + strlen(part_four) + 1); 889 dir2 = my_str_malloc(strlen(part_three) + strlen(part_four) + 1); 890 } 891 else if(!strncmp(part_one,"rename",6)) 892 { 893 dir1 = my_str_malloc(strlen(part_two) + strlen(part_three) + 1); 894 dir2 = my_str_malloc(strlen(part_two) + strlen(part_four) + 1); 895 } 896 p = strstr(dir1,fullpath); 897 q = strstr(dir2,fullpath); 898 if(p != NULL || q != NULL) 899 { 900 res = 1; 901 free(sock_buf); 902 free(part_one); 903 free(part_two); 904 free(part_three); 905 free(part_four); 906 break; 907 } 908 free(sock_buf); 909 free(part_one); 910 free(part_two); 911 free(part_three); 912 free(part_four); 913 } 914 else 915 { 916 dir1 = my_str_malloc(strlen(dir) + 3); 917 dir2 = my_str_malloc(strlen(dir) + 3); 918 sprintf(dir1,"\n%s\n",dir); 919 sprintf(dir2,"\n%s/",dir); 920 921 DEBUG("dir1:%s\n",dir1); 922 DEBUG("dir2:%s\n",dir2); 923 924 p = strstr(pTemp->cmdName,dir1); 925 q = strstr(pTemp->cmdName,dir2); 926 if(p != NULL || q != NULL) 927 { 928 res = 1; 929 free(dir1); 930 free(dir2); 931 break; 932 } 933 } 934 pTemp=pTemp->next; 935 free(dir1); 936 free(dir2); 937 } 938 free(fullpath); 939 return res; 940} 941