1#include "base.h" 2 3int no_config; 4int exit_loop; 5int stop_down; 6int stop_up; 7 8 9int stop_progress; 10 11int my_index; 12int click_apply; 13 14int upload_break; 15int upload_file_lost; 16 17int del_acount; 18 19int disk_change; 20int sync_disk_removed; 21int sighandler_finished; 22int mountflag; 23 24int local_sync; 25int server_sync; 26int first_sync; 27 28char *HW; 29string ftp_url; 30 31_info LOCAL_FILE,SERVER_FILE; 32char log_base_path[MAX_LENGTH]; 33char log_path[MAX_LENGTH]; 34 35pthread_t newthid1,newthid2,newthid3,newthid4; 36pthread_cond_t cond,cond_socket,cond_log; 37pthread_mutex_t mutex,mutex_socket,mutex_socket_1,mutex_receve_socket,mutex_log,mutex_rename; 38 39Config ftp_config,ftp_config_stop; 40Node *newNode; 41CloudFile *FileList_one; 42CloudFile *FileTail_one; 43CloudFile *FolderList_one; 44CloudFile *FolderTail_one; 45CloudFile *FolderTmp_one; 46CloudFile *FileList_size; 47CloudFile *FileTail_size; 48CloudFile *FolderTmp_size; 49CloudFile *TreeFolderList; 50CloudFile *TreeFileList; 51CloudFile *TreeFolderTail; 52CloudFile *TreeFileTail; 53CloudFile *FolderTmp; 54Server_TreeNode *ServerNode_del,*ServerNode_List; 55sync_list **g_pSyncList; 56struct tokenfile_info_tag *tokenfile_info,*tokenfile_info_start,*tokenfile_info_tmp; 57 58int check_link_internet() 59{ 60 struct timeval now; 61 struct timespec outtime; 62 int link_flag = 0; 63 int i; 64 65 while(!link_flag && !exit_loop) 66 { 67#if defined NVRAM_ 68 #ifdef USE_TCAPI 69 char *link_internet; 70 char tmp[MAXLEN_TCAPI_MSG] = {0}; 71 tcapi_get(WANDUCK,"link_internet",tmp); 72 link_internet = my_str_malloc(strlen(tmp) + 1); 73 sprintf(link_internet,"%s",tmp); 74 link_flag = atoi(link_internet);//字符串转化为值 75 free(link_internet); 76 #else 77 char *link_internet; 78 link_internet = strdup(nvram_safe_get("link_internet")); 79 link_flag = atoi(link_internet); 80 free(link_internet); 81 #endif 82#else 83 char cmd_p[128] = {0}; 84 85#ifdef _PC 86 //sprintf(cmd_p,"sh %s",GET_INTERNET); 87#else 88 sprintf(cmd_p,"sh %s",GET_INTERNET); 89#endif 90 system(cmd_p); 91 sleep(2); 92 93 char nv[64] = {0}; 94 FILE *fp; 95 96#ifdef _PC 97 // fp = fopen(VAL_INTERNET,"r"); 98#else 99 fp = fopen(VAL_INTERNET,"r"); 100#endif 101 102 fgets(nv,sizeof(nv),fp); 103 fclose(fp); 104 105 link_flag = atoi(nv); 106 DEBUG("link_flag = %d\n",link_flag); 107#endif 108 if(!link_flag) 109 { 110 for(i = 0; i < ftp_config.dir_num; i++) 111 { 112 g_pSyncList[i]->IsNetWorkUnlink = 1;//2014.11.03 by sherry ip不对和网络连接有问题 113 write_log(S_ERROR,"Network Connection Failed.","",i); 114 } 115 pthread_mutex_lock(&mutex); 116 if(!exit_loop) 117 { 118 gettimeofday(&now, NULL); 119 outtime.tv_sec = now.tv_sec + 20; 120 outtime.tv_nsec = now.tv_usec * 1000; 121 pthread_cond_timedwait(&cond, &mutex, &outtime); 122 } 123 pthread_mutex_unlock(&mutex); 124 } 125 } 126 //2014.11.03 by sherry ip不对和网络连接有问题,不显示error信息 127 128 for(i = 0;i < ftp_config.dir_num;i++) 129 { 130 if(g_pSyncList[i]->IsNetWorkUnlink && !exit_loop) 131 { 132 write_log(S_ERROR,"check your ip or network","",i); 133 g_pSyncList[i]->IsNetWorkUnlink = 0; 134 } 135// else if(g_pSyncList[i]->IsPermission && !exit_loop)//2014.11.04 by sherry sever权限不足 136// { 137// write_log(S_ERROR,"Permission Denied.","",i); 138// g_pSyncList[i]->IsPermission = 0; 139// } 140 else 141 write_log(S_SYNC,"","",i); 142 143 } 144 145// for(i = 0;i < ftp_config.dir_num;i++)//原始的代码 146// { 147// write_log(S_SYNC,"","",i); 148// } 149// if(!exit_loop && IsNetWorkUnlink)//2014.10.31修改后 150// { 151// for(i = 0;i < ftp_config.dir_num;i++) 152// { 153// write_log(S_SYNC,"","",i); 154// IsNetWorkUnlink=0; 155// } 156// } 157} 158 159char *parse_nvram(char *nvram) 160{ 161 char *nv = nvram; 162 DEBUG("nv = %s\n",nv); 163 char *buf,*b; 164 char *new_nv = (char*)malloc(sizeof(char)); 165 //2014.10.30 by sherry sizeof(指针) 166 //memset(new_nv,'\0',sizeof(new_nv)); 167 memset(new_nv,'\0',sizeof(char));//对nvram进行清零操作 168 while((b = strsep(&nv, "<")) != NULL)//strsep(&nv, "<")分解字符串为一组字符串 169 { 170 buf = strdup(b);//strdup对串拷贝到新建的位置 171 buf = (char *)realloc(buf,strlen(buf) + 2);//realloc给已经分配了空间的地址重新分配空间 172 strcat(buf,">");//连接两个字符数组中的字符串 173 char *p = buf; 174 DEBUG("p = %s\n",p); 175 int count = 0; 176 while(strlen(p) != 0) 177 { 178 if(p[0] == '>') 179 { 180 count++; 181 //DEBUG("count = %d\n",count); 182 } 183 if(count == 6) 184 break; 185 p++; 186 } 187 188 char *same_part = (char*)malloc(sizeof(char)*(strlen(buf) - strlen(p) + 2)); 189 memset(same_part,'\0',strlen(buf) - strlen(p) + 2); 190 strncpy(same_part,buf,strlen(buf) - strlen(p) + 1); 191 //DEBUG("same_part = %s\n",same_part); 192 193 p = p + 3; 194 //DEBUG("p = %s\n",p); 195 char *q = p; 196 //DEBUG("q = %s\n",q); 197 int _count = 0; 198 while(strlen(q) != 0) 199 { 200 if(q[0] == '>') 201 _count++; 202 if(_count != 0 && _count%2 == 0) 203 { 204 char *temp = (char*)malloc(sizeof(char)*(strlen(p) - strlen(q) + 1)); 205 memset(temp,'\0',strlen(p) - strlen(q) + 1); 206 strncpy(temp,p,strlen(p) - strlen(q)); 207 //DEBUG("temp = %s\n",temp); 208 new_nv = (char *)realloc(new_nv,strlen(new_nv) + strlen(same_part) + strlen(temp) + 1 + 3); 209 strcat(new_nv,same_part); 210 strcat(new_nv,temp); 211 strcat(new_nv,"<"); 212 free(temp); 213 q = q + 1; 214 p = q; 215 _count = 0; 216 } 217 q++; 218 } 219 free(same_part); 220 free(buf); 221 } 222 new_nv[strlen(new_nv) - 1] = '\0'; 223 DEBUG("new_nv = %s\n",new_nv); 224 return new_nv; 225} 226 227#ifdef NVRAM_ 228/*Type>Desc>URL>Rule>capacha>LocalFolder*/ 229int convert_nvram_to_file_mutidir(char *file,Config *config) 230{ 231 DEBUG("enter convert_nvram_to_file_mutidir function\n"); 232 FILE *fp; 233 char *nv, *nvp, *b; 234 char *new_nv; 235 int i; 236 int j = 0; 237 //int status; 238 char *p; 239 char *buffer; 240 char *buf; 241 242 fp = fopen(file, "w"); 243 244 if (fp==NULL) return -1; 245 #ifdef USE_TCAPI 246 char tmp[MAXLEN_TCAPI_MSG] = {0}; 247 tcapi_get(AICLOUD,"cloud_sync",tmp); 248 nvp = my_str_malloc(strlen(tmp) + 1); 249 sprintf(nvp,"%s",tmp); 250 #else 251 nvp = strdup(nvram_safe_get("cloud_sync")); 252 #endif 253 new_nv = parse_nvram(nvp); 254 free(nvp); 255 nv = new_nv; 256 257 DEBUG("********nv = %s\n",nv); 258 259 if(nv) 260 { 261 while ((b = strsep(&new_nv, "<")) != NULL) 262 { 263 i = 0; 264 buf = buffer = strdup(b); 265 DEBUG("buffer = %s\n",buffer); 266 while((p = strsep(&buffer,">")) != NULL) 267 { 268 DEBUG("p = %s\n",p); 269 if (*p == 0) 270 { 271 fprintf(fp,"\n"); 272 i++; 273 continue; 274 } 275 if(i == 0) 276 { 277 if(atoi(p) != 2) 278 break; 279 if(j > 0) 280 { 281 fprintf(fp,"\n%s",p); 282 } 283 else 284 { 285 fprintf(fp,"%s",p); 286 } 287 j++; 288 } 289 else 290 { 291 fprintf(fp,"\n%s",p); 292 } 293 i++; 294 } 295 free(buf); 296 } 297 DEBUG("j = %d\n",j); 298 free(nv); 299 config->dir_num = j; 300 } 301 else 302 DEBUG("get nvram fail\n"); 303 fclose(fp); 304 DEBUG("end convert_nvram_to_file_mutidir function\n"); 305 return 0; 306} 307#endif 308int create_ftp_conf_file(Config *config) 309{ 310 DEBUG("enter create_ftp_conf_file function\n"); 311 FILE *fp; 312 char *nv, *nvp, *b; 313 char *new_nv; 314 int i; 315 int j = 0; 316 char *p; 317 char *buffer; 318 char *buf; 319 320#ifdef _PC 321 //fp = fopen(TMP_CONFIG,"r"); 322#else 323 fp = fopen(TMP_CONFIG,"r"); 324#endif 325 if (fp == NULL) 326 { 327 nvp = my_str_malloc(2); 328 sprintf(nvp,""); 329 } 330 else 331 { 332 fseek( fp , 0 , SEEK_END ); 333 int file_size; 334 file_size = ftell( fp );//文件所含字节数 335 fseek(fp , 0 , SEEK_SET); 336 nvp = (char *)malloc( file_size * sizeof( char ) ); 337 fread(nvp , file_size , sizeof(char) , fp); 338 fclose(fp); 339 } 340 new_nv = parse_nvram(nvp); 341 free(nvp); 342 nv = new_nv; 343 replace_char_in_str(new_nv,'\0','\n'); 344 DEBUG("**********nv = %s\n",nv); 345 fp = fopen(CONFIG_PATH, "w"); 346 if (fp==NULL) return -1; 347 if(nv) 348 { 349 while ((b = strsep(&new_nv, "<")) != NULL)//分解字符串为一组字符串 350 { 351 i = 0; 352 buf = buffer = strdup(b); 353 DEBUG("buffer = %s\n",buffer); 354 while((p = strsep(&buffer,">")) != NULL) 355 { 356 DEBUG("p = %s\n",p); 357 if (*p == 0) 358 { 359 fprintf(fp,"\n"); 360 i++; 361 continue; 362 } 363 if(i == 0) 364 { 365 if(atoi(p) != 2) 366 break; 367 if(j > 0) 368 { 369 fprintf(fp,"\n%s",p); 370 } 371 else 372 { 373 fprintf(fp,"%s",p); 374 } 375 j++; 376 } 377 else 378 { 379 fprintf(fp,"\n%s",p); 380 } 381 i++; 382 } 383 free(buf); 384 385 } 386 free(nv); 387 DEBUG("j:%d\n",j); 388 config->dir_num = j; 389 } 390 fclose(fp); 391 return 0; 392} 393 394#ifndef NVRAM_ 395int write_get_nvram_script(char *name,char *shell_dir,char *val_dir) 396{ 397 FILE *fp; 398 char contents[512]; 399 memset(contents,0,512); 400 sprintf(contents,"#! /bin/sh\n" \ 401 "NV=`nvram get %s`\n" \ 402 "if [ ! -f \"%s\" ]; then\n" \ 403 "touch %s\n" \ 404 "fi\n" \ 405 "echo \"$NV\" >%s",name,val_dir,val_dir,val_dir); 406 407 if(( fp = fopen(shell_dir,"w"))==NULL) 408 { 409 fprintf(stderr,"create ftpclient_get_nvram file error\n"); 410 return -1; 411 } 412 413 fprintf(fp,"%s",contents); 414 fclose(fp); 415 416 return 0; 417} 418#endif 419 420int create_shell_file() 421{ 422 DEBUG("create shell file\n"); 423 FILE *fp; 424 char contents[256]; 425 memset(contents,0,256); 426#ifndef USE_TCAPI 427 strcpy(contents,"#! /bin/sh\n" \ 428 "nvram set $2=\"$1\"\n" \ 429 "nvram commit"); 430#else 431 strcpy(contents,"#! /bin/sh\n" \ 432 "tcapi set AiCloud_Entry $2 \"$1\"\n" \ 433 "tcapi commit AiCloud\n" \ 434 "tcapi save"); 435#endif 436#ifdef _PC 437// if(( fp = fopen(SHELL_FILE,"w"))==NULL) 438// { 439// fprintf(stderr,"create shell file error"); 440// return -1; 441// } 442 443// fprintf(fp,"%s",contents); 444// fclose(fp); 445// return 0; 446#else 447 if(( fp = fopen(SHELL_FILE,"w"))==NULL) 448 { 449 fprintf(stderr,"create shell file error"); 450 return -1; 451 } 452 453 fprintf(fp,"%s",contents); 454 fclose(fp); 455 return 0; 456#endif 457} 458 459int detect_process(char * process_name) 460{ 461 FILE *ptr; 462 char buff[512]; 463 char ps[128]; 464 sprintf(ps,"ps | grep -c %s",process_name); 465 strcpy(buff,"ABNORMAL"); 466 if((ptr=popen(ps, "r")) != NULL) 467 { 468 while (fgets(buff, 512, ptr) != NULL) 469 { 470 if(atoi(buff)>2) 471 { 472 pclose(ptr); 473 return 1; 474 } 475 } 476 } 477 if(strcmp(buff,"ABNORMAL")==0) /*ps command error*/ 478 { 479 return 0; 480 } 481 pclose(ptr); 482 return 0; 483} 484 485#ifdef NVRAM_ 486int write_to_nvram(char *contents,char *nv_name) 487{ 488 char *command; 489 command = my_str_malloc(strlen(contents)+strlen(SHELL_FILE)+strlen(nv_name)+8); 490 sprintf(command,"sh %s \"%s\" %s",SHELL_FILE,contents,nv_name);//8是:字符sh,3个空格,两个转义字符\" 以及\0 491 DEBUG("command : [%s]\n",command); 492 system(command); 493 free(command); 494 495 return 0; 496} 497#else 498int write_to_ftp_tokenfile(char *contents) 499{ 500 if(contents == NULL || contents == "") 501 { 502 unlink(TOKENFILE_RECORD); 503 return 0; 504 } 505 FILE *fp; 506 if( ( fp = fopen(TOKENFILE_RECORD,"w") ) == NULL ) 507 { 508 DEBUG("open ftp_tokenfile failed!\n"); 509 return -1; 510 } 511 fprintf(fp,"%s",contents); 512 fclose(fp); 513 //DEBUG("write_to_ftp_tokenfile end\n"); 514 return 0; 515} 516#endif 517 518int delete_tokenfile_info(char *url,char *folder){ 519 520 struct tokenfile_info_tag *tmp; 521 522 tmp = tokenfile_info_start; 523 tokenfile_info_tmp = tokenfile_info_start->next; 524 525 while(tokenfile_info_tmp != NULL) 526 { 527 if( !strcmp(tokenfile_info_tmp->url,url) && 528 !strcmp(tokenfile_info_tmp->folder,folder)) 529 { 530 tmp->next = tokenfile_info_tmp->next; 531 free(tokenfile_info_tmp->folder); 532 free(tokenfile_info_tmp->url); 533 free(tokenfile_info_tmp->mountpath); 534 free(tokenfile_info_tmp); 535 tokenfile_info_tmp = tmp->next; 536 } 537 else 538 { 539 tmp = tokenfile_info_tmp; 540 tokenfile_info_tmp = tokenfile_info_tmp->next; 541 } 542 } 543 544 return 0; 545} 546 547char *delete_nvram_contents(char *url,char *folder) 548{ 549 //write_debug_log("delete_nvram_contents start"); 550 551 char *nv; 552 char *nvp; 553 char *p,*b; 554 //char buffer; 555 //const char *split = "<"; 556 char *new_nv; 557 int n; 558 int i=0; 559#ifdef NVRAM_ 560 #ifdef USE_TCAPI 561 char tmp[MAXLEN_TCAPI_MSG] = {0}; 562 tcapi_get(AICLOUD,"ftp_tokenfile",tmp); 563 nv = my_str_malloc(strlen(tmp) + 1); 564 sprintf(nv,"%s",tmp); 565 p = nv; 566 #else 567 p = nv = strdup(nvram_safe_get("ftp_tokenfile")); 568 #endif 569#else 570 FILE *fp; 571 fp = fopen("/opt/etc/ftp_tokenfile","r"); 572 if (fp==NULL) 573 { 574 nv = my_str_malloc(2); 575 sprintf(nv,""); 576 } 577 else 578 { 579 fseek( fp , 0 , SEEK_END ); 580 int file_size; 581 file_size = ftell( fp ); 582 fseek(fp , 0 , SEEK_SET); 583 //nv = (char *)malloc( file_size * sizeof( char ) ); 584 nv = my_str_malloc(file_size+2); 585 //fread(nv , file_size , sizeof(char) , fp); 586 fscanf(fp,"%[^\n]%*c",nv);//[^\n]字符串的分隔符是 "\n",中括号里可以写分隔符表 587 p = nv; 588 fclose(fp); 589 } 590 591#endif 592 593 nvp = my_str_malloc(strlen(url)+strlen(folder)+2); 594 sprintf(nvp,"%s>%s",url,folder); 595 596 //write_debug_log(nv); 597 598 if(strstr(nv,nvp) == NULL) 599 { 600 free(nvp); 601 return nv; 602 } 603 604 //write_debug_log("first if"); 605 606 if(!strcmp(nv,nvp)) 607 { 608 free(nvp); 609 memset(nv,0,sizeof(nv)); 610 sprintf(nv,""); 611 return nv; 612 } 613 614 //write_debug_log("second if"); 615 616 if(nv) 617 { 618 //write_debug_log("if nv"); 619 while((b = strsep(&p, "<")) != NULL) 620 { 621 //write_debug_log(b); 622 if(strcmp(b,nvp)) 623 { 624 n = strlen(b); 625 if(i == 0) 626 { 627 new_nv = my_str_malloc(n+1); 628 sprintf(new_nv,"%s",b); 629 ++i; 630 } 631 else 632 { 633 new_nv = (char*)realloc(new_nv, strlen(new_nv)+n+2); 634 sprintf(new_nv,"%s<%s",new_nv,b); 635 } 636 } 637 } 638 639 free(nv); 640 } 641 free(nvp); 642 return new_nv; 643} 644 645int write_to_tokenfile(char *mpath) 646{ 647 DEBUG("write_to_tokenfile\n"); 648 FILE *fp; 649 //int flag=0; 650 651 char *filename; 652 653 filename = my_str_malloc(strlen(mpath) + 21 + 1); 654 //filename = my_str_malloc(strlen(mpath) + strlen("/.ftpclient_tokenfile") + 1); 655 sprintf(filename,"%s/.ftpclient_tokenfile",mpath); 656 657 int i = 0; 658 if( ( fp = fopen(filename,"w") ) == NULL ) 659 { 660 DEBUG("open tokenfile failed!\n"); 661 return -1; 662 } 663 664 tokenfile_info_tmp = tokenfile_info_start->next; 665 while(tokenfile_info_tmp != NULL) 666 { 667 DEBUG("tokenfile_info_tmp->mountpath = %s\n",tokenfile_info_tmp->mountpath); 668 if(!strcmp(tokenfile_info_tmp->mountpath,mpath)) 669 { 670 //write_debug_log(tokenfile_info_tmp->folder); 671 if(i == 0) 672 { 673 fprintf(fp,"%s\n%s",tokenfile_info_tmp->url,tokenfile_info_tmp->folder); 674 i=1; 675 } 676 else 677 { 678 fprintf(fp,"\n%s\n%s",tokenfile_info_tmp->url,tokenfile_info_tmp->folder); 679 } 680 //flag = 1; 681 } 682 683 tokenfile_info_tmp = tokenfile_info_tmp->next; 684 } 685 686 fclose(fp); 687 if(!i) 688 remove(filename); 689 free(filename); 690 691 return 0; 692} 693 694int add_tokenfile_info(char *url,char *folder,char *mpath) 695{ 696 DEBUG("add_tokenfile_info start\n"); 697 if(initial_tokenfile_info_data(&tokenfile_info_tmp) == NULL) 698 { 699 return -1; 700 } 701 702 tokenfile_info_tmp->url = my_str_malloc(strlen(url)+1); 703 sprintf(tokenfile_info_tmp->url,"%s",url); 704 705 tokenfile_info_tmp->mountpath = my_str_malloc(strlen(mpath)+1); 706 sprintf(tokenfile_info_tmp->mountpath,"%s",mpath); 707 708 tokenfile_info_tmp->folder = my_str_malloc(strlen(folder)+1); 709 sprintf(tokenfile_info_tmp->folder,"%s",folder); 710 711 tokenfile_info->next = tokenfile_info_tmp; 712 tokenfile_info = tokenfile_info_tmp; 713 714 DEBUG("add_tokenfile_info end\n"); 715 return 0; 716} 717 718char *add_nvram_contents(char *url,char *folder) 719{ 720 DEBUG("add_nvram_contents start\n"); 721 char *nv; 722 int nv_len; 723 char *new_nv; 724 char *nvp; 725 726 nvp = my_str_malloc(strlen(url)+strlen(folder)+2); 727 //nvp = my_str_malloc(strlen(url)+strlen(folder)+strlen(">")+1); 728 sprintf(nvp,"%s>%s",url,folder); 729 730 DEBUG("add_nvram_contents nvp = %s\n",nvp); 731 732#ifdef NVRAM_ 733 #ifdef USE_TCAPI 734 char tmp[MAXLEN_TCAPI_MSG] = {0}; 735 tcapi_get(AICLOUD,"ftp_tokenfile",tmp); 736 nv = my_str_malloc(strlen(tmp) + 1); 737 sprintf(nv,"%s",tmp); 738 p = nv; 739 #else 740 nv = strdup(nvram_safe_get("ftp_tokenfile")); 741 #endif 742#else 743 FILE *fp; 744 fp = fopen("/opt/etc/ftp_tokenfile","r"); 745 if (fp==NULL) 746 { 747 nv = my_str_malloc(2); 748 sprintf(nv,""); 749 } 750 else 751 { 752 fseek( fp , 0 , SEEK_END ); 753 int file_size; 754 file_size = ftell( fp ); 755 fseek(fp , 0 , SEEK_SET); 756 DEBUG("add_nvram_contents file_size = %d\n",file_size); 757 //nv = (char *)malloc( file_size * sizeof( char ) ); 758 nv = my_str_malloc(file_size+2); 759 //fread(nv , file_size ,1, fp); 760 fscanf(fp,"%[^\n]%*c",nv); 761 fclose(fp); 762 } 763#endif 764 DEBUG("add_nvram_contents nv = %s\n",nv); 765 nv_len = strlen(nv); 766 767 if(nv_len) 768 { 769 new_nv = my_str_malloc(strlen(nv)+strlen(nvp)+2); 770 sprintf(new_nv,"%s<%s",nv,nvp); 771 772 } 773 else 774 { 775 new_nv = my_str_malloc(strlen(nvp)+1); 776 sprintf(new_nv,"%s",nvp); 777 } 778 779 DEBUG("add_nvram_contents new_nv = %s\n",new_nv); 780 free(nvp); 781 free(nv); 782 DEBUG("add_nvram_contents end\n"); 783 return new_nv; 784} 785 786int rewrite_tokenfile_and_nv() 787{ 788 int i,j; 789 int exist; 790 DEBUG("rewrite_tokenfile_and_nv start\n"); 791 if(ftp_config.dir_num > ftp_config_stop.dir_num) 792 { 793 DEBUG("ftp_config.dir_num > ftp_config_stop.dir_num\n"); 794 for(i=0;i<ftp_config.dir_num;i++) 795 { 796 exist = 0; 797 for(j=0;j<ftp_config_stop.dir_num;j++) 798 { 799 if(!strcmp(ftp_config_stop.multrule[j]->fullrooturl,ftp_config.multrule[i]->fullrooturl)) 800 { 801 exist = 1; 802 break; 803 } 804 } 805 if(!exist) 806 { 807 //write_debug_log("del form nv"); 808 char *new_nv; 809 del_acount = i; 810 delete_tokenfile_info(ftp_config.multrule[i]->fullrooturl,ftp_config.multrule[i]->base_folder); 811 DEBUG("%s\n",ftp_config.multrule[i]->fullrooturl); 812 DEBUG("%s\n",ftp_config.multrule[i]->base_folder); 813 new_nv = delete_nvram_contents(ftp_config.multrule[i]->fullrooturl,ftp_config.multrule[i]->base_folder); 814 write_to_tokenfile(ftp_config.multrule[i]->mount_path); 815#ifdef NVRAM_ 816 write_to_nvram(new_nv,"ftp_tokenfile"); 817#else 818 write_to_ftp_tokenfile(new_nv); 819#endif 820 free(new_nv); 821 } 822 } 823 } 824 else 825 { 826 for(i = 0;i < ftp_config_stop.dir_num;i++) 827 { 828 exist = 0; 829 for(j = 0;j < ftp_config.dir_num;j++) 830 { 831 if(!strcmp(ftp_config_stop.multrule[i]->fullrooturl,ftp_config.multrule[j]->fullrooturl)) 832 { 833 exist = 1; 834 break; 835 } 836 } 837 if(!exist) 838 { 839 DEBUG("add to nv\n"); 840 char *new_nv; 841 add_tokenfile_info(ftp_config_stop.multrule[i]->fullrooturl,ftp_config_stop.multrule[i]->base_folder,ftp_config_stop.multrule[i]->mount_path); 842 new_nv = add_nvram_contents(ftp_config_stop.multrule[i]->fullrooturl,ftp_config_stop.multrule[i]->base_folder); 843 write_to_tokenfile(ftp_config_stop.multrule[i]->mount_path); 844#ifdef NVRAM_ 845 write_to_nvram(new_nv,"ftp_tokenfile"); 846#else 847 write_to_ftp_tokenfile(new_nv); 848#endif 849 free(new_nv); 850 } 851 } 852 } 853 return 0; 854} 855 856int write_notify_file(char *path,int signal_num) 857{ 858 FILE *fp; 859 char fullname[64]; 860 memset(fullname,0,sizeof(fullname)); 861 862 my_local_mkdir("/tmp/notify"); 863 my_local_mkdir("/tmp/notify/usb"); 864 sprintf(fullname,"%s/ftpclient",path); 865 fp = fopen(fullname,"w"); 866 if(NULL == fp) 867 { 868 DEBUG("open notify %s file fail\n",fullname); 869 return -1; 870 } 871 fprintf(fp,"%d",signal_num); 872 fclose(fp); 873 return 0; 874} 875 876int free_tokenfile_info(struct tokenfile_info_tag *head) 877{ 878 struct tokenfile_info_tag *p = head->next; 879 while(p != NULL) 880 { 881 head->next = p->next; 882 if(p->folder != NULL) 883 { 884 //DEBUG("free CloudFile %s\n",p->href); 885 free(p->folder); 886 } 887 if(p->mountpath != NULL) 888 { 889 free(p->mountpath); 890 } 891 if(p->url != NULL) 892 { 893 free(p->url); 894 } 895 free(p); 896 p = head->next; 897 } 898 return 0; 899} 900 901/*检查同步目录所在的硬碟还是否存在*/ 902int check_sync_disk_removed() 903{ 904 DEBUG("check_sync_disk_removed start! \n"); 905 906 int ret; 907 908 free_tokenfile_info(tokenfile_info_start); 909 910 if(get_tokenfile_info()==-1) 911 { 912 DEBUG("\nget_tokenfile_info failed\n"); 913 exit(-1); 914 } 915 916 ret = check_config_path(0); 917 return ret; 918 919} 920 921int get_mounts_info(struct mounts_info_tag *info) 922{ 923 int len = 0; 924 FILE *fp; 925 int i = 0; 926 int num = 0; 927 //char *mount_path; 928 char **tmp_mount_list=NULL; 929 char **tmp_mount=NULL; 930 931 char buf[len+1]; 932 memset(buf,'\0',sizeof(buf)); 933 char a[1024]; 934 //char *temp; 935 char *p,*q; 936 fp = fopen("/proc/mounts","r"); 937 if(fp) 938 { 939 while(!feof(fp)) 940 { 941 memset(a,'\0',sizeof(a)); 942 fscanf(fp,"%[^\n]%*c",a); 943 if((strlen(a) != 0)&&((p=strstr(a,"/dev/sd")) != NULL)) 944 { 945 DEBUG("%s\n",p); 946 if((q=strstr(p,"/tmp")) != NULL) 947 { 948 if((p=strstr(q," ")) != NULL) 949 { 950 len = strlen(q) - strlen(p)+1; 951 952 tmp_mount = (char **)malloc(sizeof(char *)*(num+1)); 953 if(tmp_mount == NULL) 954 { 955 fclose(fp); 956 return -1; 957 } 958 959 tmp_mount[num] = my_str_malloc(len+1); 960 if(tmp_mount[num] == NULL) 961 { 962 free(tmp_mount); 963 fclose(fp); 964 return -1; 965 } 966 snprintf(tmp_mount[num],len,"%s",q); 967 968 if(num != 0) 969 { 970 for(i = 0; i < num; ++i) 971 tmp_mount[i] = tmp_mount_list[i]; 972 973 free(tmp_mount_list); 974 tmp_mount_list = tmp_mount; 975 } 976 else 977 tmp_mount_list = tmp_mount; 978 979 ++num; 980 } 981 } 982 } 983 } 984 } 985 fclose(fp); 986 info->paths = tmp_mount_list; 987 info->num = num; 988 return 0; 989} 990 991int get_tokenfile_info() 992{ 993 int i; 994 int j = 0; 995 struct mounts_info_tag *info = NULL; 996 char *tokenfile; 997 FILE *fp; 998 char buffer[1024]; 999 char *p; 1000 1001 tokenfile_info = tokenfile_info_start; 1002 1003 info = (struct mounts_info_tag *)malloc(sizeof(struct mounts_info_tag)); 1004 if(info == NULL) 1005 { 1006 DEBUG("obtain memory space fail\n"); 1007 return -1; 1008 } 1009 1010 memset(info,0,sizeof(struct mounts_info_tag)); 1011 memset(buffer,0,1024); 1012 1013 if(get_mounts_info(info) == -1) 1014 { 1015 DEBUG("get mounts info fail\n"); 1016 return -1; 1017 } 1018 1019 DEBUG("%d\n",info->num); 1020 for(i=0;i<info->num;i++) 1021 { 1022 DEBUG("info->paths[%d] = %s\n",i,info->paths[i]); 1023 1024 tokenfile = my_str_malloc(strlen(info->paths[i]) + 21 + 1); 1025 //tokenfile = my_str_malloc(strlen(info->paths[i]) + strlen("/.ftpclient_tokenfile") + 1); 1026 sprintf(tokenfile,"%s/.ftpclient_tokenfile",info->paths[i]); 1027 DEBUG("tokenfile = %s\n",tokenfile); 1028 if(!access(tokenfile,F_OK)) 1029 { 1030 DEBUG("tokenfile is exist!\n"); 1031 if((fp = fopen(tokenfile,"r"))==NULL) 1032 { 1033 fprintf(stderr,"read tokenfile error\n"); 1034 exit(-1); 1035 } 1036 while(fgets(buffer,1024,fp)!=NULL) 1037 { 1038 if( buffer[ strlen(buffer)-1 ] == '\n' ) 1039 buffer[ strlen(buffer)-1 ] = '\0'; 1040 p = buffer; 1041 DEBUG("p = %s\n",p); 1042 if(j == 0) 1043 { 1044 if(initial_tokenfile_info_data(&tokenfile_info_tmp) == NULL) 1045 { 1046 return -1; 1047 } 1048 tokenfile_info_tmp->url = my_str_malloc(strlen(p)+1); 1049 sprintf(tokenfile_info_tmp->url,"%s",p); 1050 tokenfile_info_tmp->mountpath = my_str_malloc(strlen(info->paths[i])+1); 1051 sprintf(tokenfile_info_tmp->mountpath,"%s",info->paths[i]); 1052 j++; 1053 } 1054 else 1055 { 1056 tokenfile_info_tmp->folder = my_str_malloc(strlen(p)+1); 1057 sprintf(tokenfile_info_tmp->folder,"%s",p); 1058 tokenfile_info->next = tokenfile_info_tmp; 1059 tokenfile_info = tokenfile_info_tmp; 1060 j = 0; 1061 } 1062 } 1063 fclose(fp); 1064 } 1065 free(tokenfile); 1066 } 1067 1068 for(i=0;i<info->num;++i) 1069 { 1070 free(info->paths[i]); 1071 } 1072 if(info->paths != NULL) 1073 free(info->paths); 1074 free(info); 1075 return 0; 1076} 1077 1078int check_config_path(int is_read_config) 1079{ 1080 DEBUG("check_config_path start\n"); 1081 int i; 1082 int flag; 1083 char *nv; 1084 char *nvp; 1085 char *new_nv; 1086 int nv_len; 1087 int is_path_change = 0; 1088 1089#ifdef NVRAM_ 1090 #ifdef USE_TCAPI 1091 char tmp[MAXLEN_TCAPI_MSG] = {0}; 1092 tcapi_get(AICLOUD,"ftp_tokenfile",tmp); 1093 nv = my_str_malloc(strlen(tmp) + 1); 1094 sprintf(nv,"%s",tmp); 1095 p = nv; 1096 #else 1097 nv = strdup(nvram_safe_get("ftp_tokenfile")); 1098 #endif 1099#else 1100 FILE *fp; 1101 fp = fopen(TOKENFILE_RECORD,"r"); 1102 if(fp==NULL) 1103 { 1104 nv = my_str_malloc(2); 1105 sprintf(nv,""); 1106 } 1107 else 1108 { 1109 fseek( fp , 0 , SEEK_END ); 1110 int file_size; 1111 file_size = ftell( fp ); 1112 fseek(fp , 0 , SEEK_SET); 1113 //nv = (char *)malloc( file_size * sizeof( char ) ); 1114 nv = my_str_malloc(file_size+2); 1115 //fread(nv , file_size , sizeof(char) , fp); 1116 fscanf(fp,"%[^\n]%*c",nv); 1117 fclose(fp); 1118 } 1119#endif 1120 nv_len = strlen(nv); 1121 1122 //write_debug_log("check_config_path"); 1123 DEBUG("nv_len = %d\n",nv_len); 1124 1125 for(i=0;i<ftp_config.dir_num;i++) 1126 { 1127 flag = 0; 1128 tokenfile_info_tmp = tokenfile_info_start->next; 1129 while(tokenfile_info_tmp != NULL) 1130 { 1131 if( !strcmp(tokenfile_info_tmp->url,ftp_config.multrule[i]->fullrooturl) && 1132 !strcmp(tokenfile_info_tmp->folder,ftp_config.multrule[i]->base_folder)) 1133 { 1134 if(strcmp(tokenfile_info_tmp->mountpath,ftp_config.multrule[i]->mount_path)) 1135 { 1136 memset(ftp_config.multrule[i]->mount_path,0,sizeof(ftp_config.multrule[i]->mount_path)); 1137 sprintf(ftp_config.multrule[i]->mount_path,"%s",tokenfile_info_tmp->mountpath); 1138 memset(ftp_config.multrule[i]->base_path,0,sizeof(ftp_config.multrule[i]->base_path)); 1139 sprintf(ftp_config.multrule[i]->base_path,"%s%s",tokenfile_info_tmp->mountpath,tokenfile_info_tmp->folder); 1140 ftp_config.multrule[i]->base_path_len = strlen(ftp_config.multrule[i]->base_path); 1141 is_path_change = 1; 1142 } 1143 if(!is_read_config) 1144 { 1145 if(g_pSyncList[i]->sync_disk_exist == 0) 1146 is_path_change = 1; //plug the disk and the mout_path not change 1147 } 1148 flag = 1; 1149 break; 1150 } 1151 tokenfile_info_tmp = tokenfile_info_tmp->next; 1152 } 1153 if(!flag) 1154 { 1155 1156 nvp = my_str_malloc(strlen(ftp_config.multrule[i]->fullrooturl)+strlen(ftp_config.multrule[i]->base_folder)+2); 1157 //nvp = my_str_malloc(strlen(ftp_config.multrule[i]->fullrooturl)+strlen(ftp_config.multrule[i]->base_folder)+strlen(">")+1); 1158 sprintf(nvp,"%s>%s",ftp_config.multrule[i]->fullrooturl,ftp_config.multrule[i]->base_folder); 1159 1160 //write_debug_log(nv); 1161 //write_debug_log(nvp); 1162 1163 if(!is_read_config) 1164 { 1165 if(g_pSyncList[i]->sync_disk_exist == 1) 1166 is_path_change = 2; //remove the disk and the mout_path not change 1167 } 1168 1169 DEBUG("write nvram and tokenfile if before\n"); 1170 1171 if(strstr(nv,nvp) == NULL) 1172 { 1173 DEBUG("write nvram and tokenfile if behind"); 1174 1175 if(initial_tokenfile_info_data(&tokenfile_info_tmp) == NULL) 1176 { 1177 return -1; 1178 } 1179 tokenfile_info_tmp->url = my_str_malloc(strlen(ftp_config.multrule[i]->fullrooturl) + 1); 1180 sprintf(tokenfile_info_tmp->url,"%s",ftp_config.multrule[i]->fullrooturl); 1181 1182 tokenfile_info_tmp->mountpath = my_str_malloc(strlen(ftp_config.multrule[i]->mount_path) + 1); 1183 sprintf(tokenfile_info_tmp->mountpath,"%s",ftp_config.multrule[i]->mount_path); 1184 1185 tokenfile_info_tmp->folder = my_str_malloc(strlen(ftp_config.multrule[i]->base_folder) + 1); 1186 sprintf(tokenfile_info_tmp->folder,"%s",ftp_config.multrule[i]->base_folder); 1187 1188 tokenfile_info->next = tokenfile_info_tmp; 1189 tokenfile_info = tokenfile_info_tmp; 1190 1191 write_to_tokenfile(ftp_config.multrule[i]->mount_path); 1192 1193 if(nv_len) 1194 { 1195 1196 new_nv = my_str_malloc(strlen(nv)+strlen(nvp) + 2); 1197 //new_nv = my_str_malloc(strlen(nv)+strlen(nvp) + strlen("<")+1); 1198 sprintf(new_nv,"%s<%s",nv,nvp); 1199 1200 } 1201 else 1202 { 1203 new_nv = my_str_malloc(strlen(nvp)+1); 1204 sprintf(new_nv,"%s",nvp); 1205 } 1206#ifdef NVRAM_ 1207 write_to_nvram(new_nv,"ftp_tokenfile"); 1208#else 1209 write_to_ftp_tokenfile(new_nv); 1210#endif 1211 free(new_nv); 1212 } 1213 free(nvp); 1214 } 1215 } 1216 free(nv); 1217 return is_path_change; 1218} 1219 1220struct tokenfile_info_tag *initial_tokenfile_info_data(struct tokenfile_info_tag **token) 1221{ 1222 struct tokenfile_info_tag *follow_token; 1223 1224 if(token == NULL) 1225 return *token; 1226 1227 *token = (struct tokenfile_info_tag *)malloc(sizeof(struct tokenfile_info_tag)); 1228 if(*token == NULL) 1229 return NULL; 1230 1231 follow_token = *token; 1232 1233 follow_token->url = NULL; 1234 follow_token->folder = NULL; 1235 follow_token->mountpath = NULL; 1236 follow_token->next = NULL; 1237 1238 return follow_token; 1239} 1240char *my_nstrchr(const char chr,char *str,int n){ 1241 1242 if(n<1) 1243 { 1244 DEBUG("my_nstrchr need n>=1\n"); 1245 return NULL; 1246 } 1247 1248 char *p1,*p2; 1249 int i = 1; 1250 p1 = str; 1251 1252 do{ 1253 p2 = strchr(p1,chr); 1254 p1 = p2; 1255 p1++; 1256 i++; 1257 }while(p2!=NULL && i<=n); 1258 1259 if(i<n) 1260 { 1261 return NULL; 1262 } 1263 1264 return p2; 1265} 1266 1267static void *xmalloc_fatal(size_t size) { 1268 if (size==0) return NULL; 1269 fprintf(stderr, "Out of memory."); 1270 exit(1); 1271} 1272 1273void *xmalloc (size_t size) { 1274 void *ptr = malloc (size); 1275 if (ptr == NULL) return xmalloc_fatal(size); 1276 return ptr; 1277} 1278 1279void *xrealloc (void *ptr, size_t size) { 1280 void *p = realloc (ptr, size); 1281 if (p == NULL) return xmalloc_fatal(size); 1282 return p; 1283} 1284 1285char *xstrdup (const char *s) { 1286 void *ptr = xmalloc(strlen(s)+1); 1287 strcpy ((char*)ptr, s); 1288 return (char*) ptr; 1289} 1290 1291/** 1292 * Escape 'string' according to RFC3986 and 1293 * http://oauth.net/core/1.0/#encoding_parameters. 1294 * 1295 * @param string The data to be encoded 1296 * @return encoded string otherwise NULL 1297 * The caller must free the returned string. 1298 */ 1299char *oauth_url_escape(const char *string) { 1300 size_t alloc, newlen; 1301 char *ns = NULL, *testing_ptr = NULL; 1302 unsigned char in; 1303 size_t strindex=0; 1304 size_t length; 1305 1306 if (!string) return xstrdup(""); 1307 1308 alloc = strlen(string)+1; 1309 newlen = alloc; 1310 1311 ns = (char*) xmalloc(alloc); 1312 1313 length = alloc-1; 1314 while(length--) { 1315 in = *string; 1316 1317 switch(in){ 1318 case '0': case '1': case '2': case '3': case '4': 1319 case '5': case '6': case '7': case '8': case '9': 1320 case 'a': case 'b': case 'c': case 'd': case 'e': 1321 case 'f': case 'g': case 'h': case 'i': case 'j': 1322 case 'k': case 'l': case 'm': case 'n': case 'o': 1323 case 'p': case 'q': case 'r': case 's': case 't': 1324 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': 1325 case 'A': case 'B': case 'C': case 'D': case 'E': 1326 case 'F': case 'G': case 'H': case 'I': case 'J': 1327 case 'K': case 'L': case 'M': case 'N': case 'O': 1328 case 'P': case 'Q': case 'R': case 'S': case 'T': 1329 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': 1330 case '_': case '~': case '.': case '-': 1331 ns[strindex++]=in; 1332 break; 1333 default: 1334 newlen += 2; /* this'll become a %XX */ 1335 if(newlen > alloc) { 1336 alloc *= 2; 1337 testing_ptr = (char*) xrealloc(ns, alloc); 1338 ns = testing_ptr; 1339 } 1340 //snprintf(&ns[strindex], 4, "%%%02X", in); 1341 snprintf(&ns[strindex], 4, "%%%02x", in); 1342 strindex+=3; 1343 break; 1344 } 1345 string++; 1346 } 1347 ns[strindex]=0; 1348 return ns; 1349} 1350 1351#ifndef ISXDIGIT 1352# define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x))) 1353#endif 1354 1355/** 1356 * Parse RFC3986 encoded 'string' back to unescaped version. 1357 * 1358 * @param string The data to be unescaped 1359 * @param olen unless NULL the length of the returned string is stored there. 1360 * @return decoded string or NULL 1361 * The caller must free the returned string. 1362 */ 1363char *oauth_url_unescape(const char *string, size_t *olen) { 1364 size_t alloc, strindex=0; 1365 char *ns = NULL; 1366 unsigned char in; 1367 long hex; 1368 1369 if (!string) return NULL; 1370 alloc = strlen(string)+1; 1371 ns = (char*) xmalloc(alloc); 1372 1373 while(--alloc > 0) { 1374 in = *string; 1375 if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) { 1376 char hexstr[3]; // '%XX' 1377 hexstr[0] = string[1]; 1378 hexstr[1] = string[2]; 1379 hexstr[2] = 0; 1380 hex = strtol(hexstr, NULL, 16); 1381 in = (unsigned char)hex; /* hex is always < 256 */ 1382 string+=2; 1383 alloc-=2; 1384 } 1385 ns[strindex++] = in; 1386 string++; 1387 } 1388 ns[strindex]=0; 1389 if(olen) *olen = strindex; 1390 return ns; 1391} 1392 1393int do_unfinished(int index) 1394{ 1395 if(exit_loop) 1396 { 1397 return 0; 1398 } 1399 DEBUG("*************do_unfinished*****************\n"); 1400 action_item *p,*p1; 1401 p = g_pSyncList[index]->unfinished_list->next; 1402 int ret; 1403 1404 while(p != NULL) 1405 { 1406 if(!strcmp(p->action,"download")) 1407 { 1408 CloudFile *filetmp = NULL; 1409 filetmp = get_CloudFile_node(g_pSyncList[index]->ServerRootNode,p->path,0x2); 1410 if(filetmp == NULL) //if filetmp == NULL,it means server has deleted filetmp 1411 { 1412 DEBUG("filetmp is NULL\n"); 1413 1414 p1 = p->next; 1415 del_action_item("download",p->path,g_pSyncList[index]->unfinished_list); 1416 p = p1; 1417 continue; 1418 } 1419 char *localpath = serverpath_to_localpath(filetmp->href,index); 1420 DEBUG("localpath = %s\n",localpath); 1421 if(is_local_space_enough(filetmp,index)) 1422 { 1423 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 1424 ret = download_(filetmp->href,index); 1425 if (ret == 0) 1426 { 1427 ChangeFile_modtime(localpath,filetmp->modtime,index); 1428 p1 = p->next; 1429 del_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 1430 p = p1; 1431 } 1432 else 1433 { 1434 DEBUG("download %s failed",filetmp->href); 1435 p = p->next; 1436 //return ret; 1437 } 1438 } 1439 else 1440 { 1441 write_log(S_ERROR,"local space is not enough!","",index); 1442 p = p->next; 1443 } 1444 free(localpath); 1445 } 1446 else if(!strcmp(p->action,"upload")) 1447 { 1448 p1 = p->next; 1449 char *serverpath; 1450 serverpath = localpath_to_serverpath(p->path,index); 1451 char *path_temp; 1452 path_temp = my_str_malloc(strlen(p->path)+1); 1453 sprintf(path_temp,"%s",p->path); 1454 ret = upload(p->path,index); 1455 DEBUG("********* upload ret = %d\n",ret); 1456 1457 if(ret == 0) 1458 { 1459 mod_time *modt = Getmodtime(serverpath,index); 1460 if(modt->modtime != -1) 1461 { 1462 ChangeFile_modtime(path_temp,modt->modtime,index); 1463 free(modt); 1464 } 1465 else 1466 { 1467 DEBUG("ChangeFile_modtime failed!\n"); 1468 } 1469 p = p1; 1470 } 1471 else if(ret == LOCAL_FILE_LOST) 1472 { 1473 del_action_item("upload",p->path,g_pSyncList[index]->unfinished_list); 1474 p = p1; 1475 } 1476 else 1477 { 1478 DEBUG("upload %s failed",p->path); 1479 p = p->next; 1480 } 1481 free(serverpath); 1482 free(path_temp); 1483 } 1484 else 1485 { 1486 p = p->next; 1487 } 1488 } 1489 return 0; 1490} 1491 1492char *get_currtm() 1493{ 1494 time_t rawtime; 1495 time(&rawtime); 1496 struct tm *timeinfo = localtime(&rawtime); 1497 //DEBUG("%s",ctime(&rawtime)); 1498 char *ret = my_str_malloc(22); 1499 sprintf(ret,"%d-%d-%d %d:%d:%d",1900+timeinfo->tm_year,1+timeinfo->tm_mon,timeinfo->tm_mday, 1500 timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec); 1501 return ret; 1502} 1503 1504char *write_error_message(char *format,...) 1505{ 1506 int size=256; 1507 char *p=(char *)malloc(size); 1508 memset(p,0,size);//2014.11.11 by sherry 1509 //memset(p,0,sizeof(p)); 1510 va_list ap; 1511 va_start(ap,format); 1512 vsnprintf(p,size,format,ap); 1513 va_end(ap); 1514 return p; 1515} 1516 1517int write_conflict_log(char *fullname, int type,char *msg) 1518{ 1519 FILE *fp = 0; 1520 //int len; 1521 char ctype[16] = {0}; 1522 1523 if(type == 1) 1524 strcpy(ctype,"Error"); 1525 else if(type == 2) 1526 strcpy(ctype,"Info"); 1527 else if(type == 3) 1528 strcpy(ctype,"Warning"); 1529 1530 1531 if(access(CONFLICT_DIR,0) == 0) 1532 fp = fopen(CONFLICT_DIR,"a"); 1533 else 1534 fp = fopen(CONFLICT_DIR,"w"); 1535 1536 1537 if(fp == NULL) 1538 { 1539 printf("open %s fail\n",CONFLICT_DIR); 1540 return -1; 1541 } 1542 1543 //len = strlen(mount_path); 1544 fprintf(fp,"TYPE:%s\nUSERNAME:%s\nFILENAME:%s\nMESSAGE:%s\n",ctype,"NULL",fullname,msg); 1545 //fprintf(fp,"ERR_CODE:%d\nMOUNT_PATH:%s\nFILENAME:%s\nRULENUM:%d\n", 1546 //err_code,mount_path,fullname+len,0); 1547 fclose(fp); 1548 return 0; 1549} 1550 1551int write_log(int status, char *message, char *filename,int index) 1552{ 1553 if(exit_loop) 1554 { 1555 return 0; 1556 } 1557 DEBUG("write log status = %d\n",status); 1558 pthread_mutex_lock(&mutex_log); 1559 struct timeval now; 1560 struct timespec outtime; 1561 FILE *fp; 1562 //int ret; 1563 Log_struc log_s; 1564 memset(&log_s,0,LOG_SIZE); 1565 1566 log_s.status = status; 1567 1568 fp = fopen(LOG_DIR,"w"); 1569 if(fp == NULL) 1570 { 1571 DEBUG("open logfile error.\n"); 1572 pthread_mutex_unlock(&mutex_log); 1573 return -1; 1574 } 1575 1576 if(log_s.status == S_ERROR) 1577 { 1578 DEBUG("******** status is ERROR *******\n"); 1579 //2014.10.31 by sherry 写入log的RuleNum的值出错为4527456 1580 //fprintf(fp,"STATUS:%d\nMESSAGE:%s\nTOTAL_SPACE:%lld\nUSED_SPACE:%lld\nRULENUM:%d\n",log_s.status,message,0,0,index); 1581 //字串写错 cookie抓不到值 1582 //fprintf(fp,"STATUS:%d\nMESSAGE:%s\nTOTAL_SPACE:0\nUSED_SPACE:0\nRULENUM:%d\n",log_s.status,message,0); 1583 fprintf(fp,"STATUS:%d\nERR_MSG:%s\nTOTAL_SPACE:0\nUSED_SPACE:0\nRULENUM:%d\n",log_s.status,message,0); 1584 DEBUG("Status:%d\tERR_MSG:%s\tRule:%d\n",log_s.status,message,index); 1585 } 1586 else if(log_s.status == S_DOWNLOAD) 1587 { 1588 DEBUG("******** status is DOWNLOAD *******\n"); 1589 strcpy(log_s.path,filename); 1590 fprintf(fp,"STATUS:%d\nMOUNT_PATH:%s\nFILENAME:%s\nTOTAL_SPACE:0\nUSED_SPACE:0\nRULENUM:%d\n",log_s.status,log_s.path,filename,index); 1591 DEBUG("Status:%d\tDownloading:%s\tRule:%d\n",log_s.status,log_s.path,index); 1592 } 1593 else if(log_s.status == S_UPLOAD) 1594 { 1595 DEBUG("******** status is UPLOAD *******\n"); 1596 strcpy(log_s.path,filename); 1597 fprintf(fp,"STATUS:%d\nMOUNT_PATH:%s\nFILENAME:%s\nTOTAL_SPACE:0\nUSED_SPACE:0\nRULENUM:%d\n",log_s.status,log_s.path,filename,index); 1598 DEBUG("Status:%d\tUploading:%s\tRule:%d\n",log_s.status,log_s.path,index); 1599 } 1600 else 1601 { 1602 if (log_s.status == S_INITIAL) 1603 DEBUG("******** other status is INIT *******\n"); 1604 else 1605 DEBUG("******** other status is SYNC *******\n"); 1606 fprintf(fp,"STATUS:%d\nTOTAL_SPACE:0\nUSED_SPACE:0\nRULENUM:%d\n",log_s.status,index); 1607 } 1608 1609 fclose(fp); 1610 1611 if(!exit_loop) 1612 { 1613 gettimeofday(&now, NULL); 1614 outtime.tv_sec = now.tv_sec + 3; 1615 outtime.tv_nsec = now.tv_usec * 1000; 1616 pthread_cond_timedwait(&cond_socket, &mutex_log, &outtime); 1617 } 1618 pthread_mutex_unlock(&mutex_log); 1619 return 0; 1620} 1621 1622int my_stat(_info *FILE_INFO) 1623{ 1624 char *prepath = get_prepath(FILE_INFO->path,strlen(FILE_INFO->path)); 1625 int exist = is_server_exist(prepath,FILE_INFO->path,FILE_INFO->index); 1626 if(exist == 1) 1627 { 1628 free(prepath); 1629 return 0; 1630 } 1631 else 1632 { 1633 free(prepath); 1634 return -1; 1635 } 1636} 1637 1638int debugFun(CURL *curl,curl_infotype type,char *str,size_t len,void *stream) 1639{ 1640 //if(type == CURLINFO_TEXT) 1641 fwrite(str,1,len,(FILE*)stream); 1642} 1643 1644size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) 1645{ 1646 if(exit_loop) 1647 { 1648 DEBUG("set download_break 1\n"); 1649 upload_break = 1; 1650 return -1; 1651 } 1652 1653 int len ; 1654 char *temp = (char*)malloc(sizeof(char)*(strlen(SERVER_FILE.path) + 9)); 1655 //char *temp = (char*)malloc(sizeof(char)*(strlen(SERVER_FILE.path) + strlen(".asus.td")+1)); 1656 sprintf(temp, "%s.asus.td", SERVER_FILE.path); 1657 struct stat info; 1658 if(stat(temp,&info) == -1) 1659 { 1660 DEBUG("set download_file_lost 1\n"); 1661 upload_file_lost = 1; 1662 free(temp);//valgrind 1663 return -1; 1664 } 1665 1666// if(my_stat(&SERVER_FILE) == -1) 1667// { 1668// DEBUG("break!\n"); 1669// return size*nmemb - 1; 1670// //return -1; 1671// } 1672 free(temp);//valgrind 1673 len = fwrite(ptr, size, nmemb, stream); 1674 return len; 1675 1676} 1677 1678size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) 1679{ 1680 if(exit_loop) 1681 { 1682 DEBUG("set upload_break 1\n"); 1683 upload_break = 1; 1684 return -1; 1685 } 1686 1687 struct stat info; 1688 char *ret = NULL; 1689 int status = stat(LOCAL_FILE.path,&info); 1690 if(status == -1) 1691 { 1692 usleep(1000*100); 1693 ret = search_newpath(LOCAL_FILE.path,LOCAL_FILE.index); 1694 if(ret == NULL) 1695 { 1696 DEBUG("set upload_file_lost 1\n"); 1697 upload_file_lost = 1; 1698 return -1; 1699 } 1700 else 1701 { 1702 free(LOCAL_FILE.path); 1703 LOCAL_FILE.path = (char *)malloc(sizeof(char)*(strlen(ret) + 1)); 1704 sprintf(LOCAL_FILE.path,"%s",ret); 1705 free(ret); 1706 } 1707 } 1708 1709 int len; 1710 len = fread(ptr, size, nmemb, stream); 1711 return len; 1712} 1713 1714int my_progress_func(char *progress_data,double t, double d,double ultotal,double ulnow) 1715{ 1716 if(exit_loop) 1717 return -1; 1718 1719 if(t > 1 && d > 10) // download 1720 { 1721 DEBUG("1\n"); 1722 DEBUG("\r%s %10.0f / %10.0f (%g %%)", progress_data, d, t, d*100.0/t); 1723 }else{ 1724 DEBUG("2\n"); 1725 //DEBUG("\r%s %10.0f / %10.0f (%g %%)", progress_data, ulnow, ultotal, ulnow*100.0/ultotal); 1726 DEBUG("\r%s %10.0f", progress_data,ulnow); 1727 } 1728 return 0; 1729 1730} 1731 1732/* 1733 *0,no local socket 1734 *1,have local socket 1735*/ 1736int wait_handle_socket(int index){ 1737 1738 //if(receve_socket) 1739 if(g_pSyncList[index]->receve_socket) 1740 { 1741 server_sync = 0; 1742 //while(receve_socket) 1743 while(g_pSyncList[index]->receve_socket || local_sync) 1744 { 1745 usleep(1000*100); 1746 } 1747 server_sync = 1; 1748 if(g_pSyncList[index]->have_local_socket) 1749 { 1750 g_pSyncList[index]->have_local_socket = 0; 1751 g_pSyncList[index]->first_sync = 1; 1752 return 1; 1753 } 1754 else 1755 { 1756 return 0; 1757 } 1758 } 1759 return 0; 1760} 1761 1762int deal_dragfolder_to_socketlist(char *dir,int index) 1763{ 1764 DEBUG("dir = %s\n",dir); 1765 1766 int status; 1767 struct dirent *ent = NULL; 1768 char info[512]; 1769 memset(info,0,sizeof(info)); 1770 DIR *pDir; 1771 pDir=opendir(dir); 1772 if(pDir != NULL) 1773 { 1774 while((ent=readdir(pDir)) != NULL) 1775 { 1776 if(ent->d_name[0] == '.') 1777 continue; 1778 if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) 1779 continue; 1780 char *fullname; 1781 1782 fullname = (char *)malloc(sizeof(char)*(strlen(dir)+strlen(ent->d_name)+2)); 1783 //fullname = (char *)malloc(sizeof(char)*(strlen(dir)+strlen(ent->d_name)+strlen("/")+1)); 1784 //2014.10.30 by sherry sizeof(指针)=4 1785 //memset(fullname,'\0',sizeof(fullname)); 1786 //memset(fullname,'\0',sizeof(char)*(strlen(dir)+strlen(ent->d_name)+strlen("/")+1)); 1787 memset(fullname,'\0',sizeof(char)*(strlen(dir)+strlen(ent->d_name)+2)); 1788 sprintf(fullname,"%s/%s",dir,ent->d_name); 1789 if(test_if_dir(fullname) == 1) 1790 { 1791 sprintf(info,"createfolder%s%s%s%s","\n",dir,"\n",ent->d_name); 1792 pthread_mutex_lock(&mutex_socket); 1793 add_socket_item(info,index); 1794 pthread_mutex_unlock(&mutex_socket); 1795 status = deal_dragfolder_to_socketlist(fullname,index); 1796 } 1797 else 1798 { 1799 sprintf(info,"createfile%s%s%s%s","\n",dir,"\n",ent->d_name); 1800 pthread_mutex_lock(&mutex_socket); 1801 add_socket_item(info,index); 1802 pthread_mutex_unlock(&mutex_socket); 1803 } 1804 free(fullname); 1805 } 1806 closedir(pDir); 1807 return 0; 1808 } 1809} 1810 1811int add_all_download_only_dragfolder_socket_list(const char *dir,int index) 1812{ 1813 struct dirent* ent = NULL; 1814 char *fullname; 1815 int fail_flag = 0; 1816 DIR *dp = opendir(dir); 1817 1818 if(dp == NULL) 1819 { 1820 DEBUG("opendir %s fail",dir); 1821 fail_flag = 1; 1822 return -1; 1823 } 1824 1825 while (NULL != (ent=readdir(dp))) 1826 { 1827 1828 if(ent->d_name[0] == '.') 1829 continue; 1830 if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) 1831 continue; 1832 1833 1834 fullname = my_str_malloc((size_t)(strlen(dir)+strlen(ent->d_name)+2)); 1835 //fullname = my_str_malloc((size_t)(strlen(dir)+strlen(ent->d_name)+strlen("/")+1)); 1836 1837 sprintf(fullname,"%s/%s",dir,ent->d_name); 1838 1839 if( test_if_dir(fullname) == 1) 1840 { 1841 add_action_item("createfolder",fullname,g_pSyncList[index]->dragfolder_action_list); 1842 add_action_item("createfolder",fullname,g_pSyncList[index]->download_only_socket_head); 1843 add_all_download_only_dragfolder_socket_list(fullname,index); 1844 } 1845 else 1846 { 1847 add_action_item("createfile",fullname,g_pSyncList[index]->dragfolder_action_list); 1848 add_action_item("createfile",fullname,g_pSyncList[index]->download_only_socket_head); 1849 } 1850 free(fullname); 1851 } 1852 1853 closedir(dp); 1854 return (fail_flag == 1) ? -1 : 0; 1855} 1856 1857mod_time *Getmodtime(char *href,int index) 1858{ 1859 DEBUG("\n********************get one file's modtime********************\n"); 1860 mod_time *mtime_1; 1861 1862 char *command = (char *)malloc(sizeof(char)*(strlen(href) + 6)); 1863 //char *command = (char *)malloc(sizeof(char)*(strlen(href) + strlen("MDTM ")+1)); 1864 //2014.10.29 by sherry sizeof(指针)=4 1865 //memset(command,'\0',sizeof(command)); 1866 //memset(command,'\0',sizeof(char)*(strlen(href) + strlen("MDTM ")+1)); 1867 memset(command,'\0',sizeof(char)*(strlen(href) + 6)); 1868 sprintf(command,"MDTM %s",href + 1); 1869 DEBUG("command:%s\n",command); 1870 char *temp = utf8_to(command,index); 1871 free(command); 1872 DEBUG("%s\n",temp); 1873 CURL *curl_handle; 1874 CURLcode res; 1875 curl_handle = curl_easy_init(); 1876 FILE *fp = fopen(TIME_ONE_DIR,"w+");; 1877 //fp=fopen("/tmp/ftpclient/time_one.txt","w+"); 1878 if(curl_handle) 1879 { 1880 curl_easy_setopt(curl_handle, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 1881 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 1882 curl_easy_setopt(curl_handle, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 1883 //curl_easy_setopt(curl_handle, CURLOPT_FTP_USE_EPSV, 0); 1884 curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER,fp); 1885 curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST,temp); 1886 curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_LIMIT,1); 1887 curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_TIME,30); 1888 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L); 1889 res = curl_easy_perform(curl_handle); 1890 DEBUG("%d\n",res); 1891 if(res != CURLE_OK && res != CURLE_FTP_COULDNT_RETR_FILE) 1892 { 1893 curl_easy_cleanup(curl_handle); 1894 fclose(fp); 1895 free(temp); 1896 //DEBUG("get %s mtime failed res = %d\n",href,res); 1897 if(res == CURLE_COULDNT_CONNECT) 1898 { 1899 write_log(S_ERROR,"Could not connect.","",index); 1900 } 1901 else if(res == CURLE_OPERATION_TIMEDOUT) 1902 { 1903 write_log(S_ERROR,"Connect timeout.","",index); 1904 } 1905 else if(res == CURLE_REMOTE_ACCESS_DENIED) 1906 { 1907 write_log(S_ERROR,"Connect refused.","",index); 1908 } 1909 mtime_1 = (mod_time *)malloc(sizeof(mod_time)); 1910 memset(mtime_1, 0, sizeof(mtime_1)); 1911 mtime_1->modtime = (time_t)-1; 1912 return mtime_1; 1913 } 1914 else 1915 { 1916 free(temp); 1917 curl_easy_cleanup(curl_handle); 1918 mtime_1 = get_mtime_1(fp); 1919 return mtime_1; 1920 } 1921 } 1922} 1923 1924//int add_socket_item_for_refresh(char *buf,int i) 1925//{ 1926// DEBUG("comeing add_socket_item_for_refresh\n"); 1927// 1928// pthread_mutex_lock(&mutex_receve_socket); 1929// g_pSyncList[i]->receve_socket = 1; 1930// pthread_mutex_unlock(&mutex_receve_socket); 1931// 1932// newNode = (Node *)malloc(sizeof(Node)); 1933// memset(newNode,0,sizeof(Node)); 1934// newNode->cmdName = (char *)malloc(sizeof(char)*(strlen(buf)+1)); 1935// memset(newNode->cmdName,'\0',sizeof(newNode->cmdName)); 1936// sprintf(newNode->cmdName,"%s",buf); 1937// 1938// //newNode->re_cmd = NULL; 1939// 1940// Node *pTemp = g_pSyncList[i]->SocketActionList_Refresh; 1941// //Node *pTemp = ActionList; 1942// while(pTemp->next!=NULL) 1943// pTemp=pTemp->next; 1944// pTemp->next=newNode; 1945// newNode->next=NULL; 1946// show(g_pSyncList[i]->SocketActionList_Refresh); 1947// DEBUG("end add_socket_item_for_rename\n"); 1948// return 0; 1949//} 1950 1951int add_socket_item_for_rename(char *buf,int i) 1952{ 1953 DEBUG("comeing add_socket_item_for_rename\n"); 1954 1955 pthread_mutex_lock(&mutex_receve_socket); 1956 g_pSyncList[i]->receve_socket = 1; 1957 pthread_mutex_unlock(&mutex_receve_socket); 1958 1959 newNode = (Node *)malloc(sizeof(Node)); 1960 memset(newNode,0,sizeof(Node)); 1961 newNode->cmdName = (char *)malloc(sizeof(char)*(strlen(buf)+1)); 1962 memset(newNode->cmdName,'\0',sizeof(newNode->cmdName)); 1963 sprintf(newNode->cmdName,"%s",buf); 1964 1965 //newNode->re_cmd = NULL; 1966 1967 Node *pTemp = g_pSyncList[i]->SocketActionList_Rename; 1968 //Node *pTemp = ActionList; 1969 while(pTemp->next!=NULL) 1970 pTemp=pTemp->next; 1971 pTemp->next=newNode; 1972 newNode->next=NULL; 1973 show(g_pSyncList[i]->SocketActionList_Rename); 1974 DEBUG("end add_socket_item_for_rename\n"); 1975 return 0; 1976} 1977 1978char *search_newpath(char *href,int i)//传入文件路径 1979{ 1980 char *ret = (char*)malloc(sizeof(char)*(strlen(href) + 1)); 1981 //2014.10.29 by sherry 未初始化ret 1982 memset(ret,0,sizeof(char)*(strlen(href) + 1)); 1983 sprintf(ret,"%s",href); 1984 Node *pTemp = g_pSyncList[i]->SocketActionList_Rename->next; 1985 while(pTemp != NULL) 1986 { 1987 char *oldpath; 1988 char *newpath; 1989 char cmd_name[512] = {0}; 1990 char oldname[512] = {0}; 1991 char newname[512] = {0}; 1992 char *path; 1993 char *temp; 1994 char *ch = pTemp->cmdName; 1995 int k = 0; 1996 while(*ch != '\n') 1997 { 1998 k++; 1999 ch++; 2000 } 2001 memcpy(cmd_name, pTemp->cmdName, k); 2002 char *p = NULL; 2003 ch++; 2004 k++; 2005 temp = my_str_malloc((size_t)(strlen(ch)+1)); 2006 strcpy(temp,ch); 2007 p = strchr(temp,'\n'); 2008 path = my_str_malloc((size_t)(strlen(temp)- strlen(p)+1)); 2009 if(p!=NULL) 2010 snprintf(path,strlen(temp)- strlen(p)+1,"%s",temp); 2011 p++; 2012 char *p1 = NULL; 2013 p1 = strchr(p,'\n'); 2014 if(p1 != NULL) 2015 strncpy(oldname,p,strlen(p)- strlen(p1)); 2016 p1++; 2017 strcpy(newname,p1); 2018 2019 oldpath = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 3)); 2020 newpath = my_str_malloc((size_t)(strlen(path) + strlen(newname) + 3)); 2021 //oldpath = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + strlen("/")+strlen("/")+1)); 2022 //newpath = my_str_malloc((size_t)(strlen(path) + strlen(newname) + strlen("/")+strlen("/")+1)); 2023 sprintf(oldpath,"%s/%s/",path,oldname); 2024 sprintf(newpath,"%s/%s/",path,newname); 2025 free(path); 2026 free(temp); 2027 2028 string temp1(ret),tmp; 2029 int m = strncmp(ret,oldpath,strlen(oldpath)); 2030 //char *m = strstr(ret,oldpath); 2031 int start,end; 2032 if(m == 0) 2033 { 2034 //start = strlen(ret) - strlen(m); 2035 end = strlen(oldpath); 2036 tmp = temp1.replace(0,end,newpath); 2037 free(ret); 2038 ret = (char*)malloc(sizeof(char)*(tmp.length() + 1)); 2039 sprintf(ret,"%s",tmp.c_str()); 2040 } 2041 pTemp = pTemp->next; 2042 } 2043 2044 if(strcmp(ret,href) == 0) 2045 { 2046 free(ret); 2047 return NULL; 2048 } 2049 else 2050 { 2051 return ret; 2052 } 2053} 2054 2055void set_re_cmd_(char *oldpath,char *oldpath_1,char *oldpath_2,char *newpath,char *newpath_1,char *newpath_2,int index) 2056{ 2057 int i; 2058 char *change_start; 2059 char *change_stop; 2060 char *old_buf; 2061 char new_buf[1024] = {0}; 2062 char *p = NULL; 2063 2064 Node *pTemp = g_pSyncList[index]->SocketActionList->next; 2065 while(pTemp != NULL) 2066 { 2067 change_start = NULL; 2068 change_stop = NULL; 2069 if((change_start = strstr(pTemp->cmdName,oldpath_1)) != NULL) 2070 { 2071 i = 0; 2072 old_buf = (char *)malloc(sizeof(char)*(strlen(pTemp->cmdName) + 1)); 2073 strcpy(old_buf,pTemp->cmdName); 2074 p = strtok(old_buf,ENTER); 2075 2076 while(p != NULL) 2077 { 2078 if(strcmp(p,oldpath)) //have no oldpath_full 2079 { 2080 if(i == 0) 2081 { 2082 i++; 2083 sprintf(new_buf,"%s",p); 2084 } 2085 else 2086 { 2087 sprintf(new_buf,"%s\n%s",new_buf,p); 2088 } 2089 } 2090 else //have oldpath_full 2091 { 2092 if(i == 0) 2093 { 2094 i++; 2095 sprintf(new_buf,"%s",newpath); 2096 } 2097 else 2098 { 2099 sprintf(new_buf,"%s\n%s",new_buf,newpath); 2100 } 2101 } 2102 2103 p=strtok(NULL,ENTER); 2104 } 2105 free(pTemp->cmdName); 2106 pTemp->cmdName = (char*)malloc(sizeof(char)*(strlen(new_buf) + 1)); 2107 sprintf(pTemp->cmdName,"%s",new_buf); 2108 } 2109 2110 else if((change_start = strstr(pTemp->cmdName,oldpath_2)) != NULL) 2111 { 2112 i = 0; 2113 old_buf = (char *)malloc(sizeof(char)*(strlen(pTemp->cmdName) + 1)); 2114 strcpy(old_buf,pTemp->cmdName); 2115 p = strtok(old_buf,ENTER); 2116 while(p != NULL) 2117 { 2118 if(strncmp(p,oldpath_2,strlen(oldpath_2))) //have no oldpath_part 2119 { 2120 if(i == 0) 2121 { 2122 i++; 2123 sprintf(new_buf,"%s",p); 2124 } 2125 else 2126 { 2127 sprintf(new_buf,"%s\n%s",new_buf,p); 2128 } 2129 } 2130 else //have oldpath_part 2131 { 2132 change_stop = p + strlen(oldpath_2); 2133 if(i == 0) 2134 { 2135 i++; 2136 sprintf(new_buf,"%s%s",newpath_2,change_stop); 2137 } 2138 else 2139 { 2140 sprintf(new_buf,"%s\n%s%s",new_buf,newpath_2,change_stop); 2141 } 2142 } 2143 p=strtok(NULL,ENTER); 2144 } 2145 free(pTemp->cmdName); 2146 pTemp->cmdName = (char*)malloc(sizeof(char)*(strlen(new_buf) + 1)); 2147 } 2148 pTemp = pTemp->next; 2149 } 2150 2151 int start,end; 2152 string socket_old,socket_new; 2153 action_item *item = g_pSyncList[index]->copy_file_list->next; 2154 while(item != NULL) 2155 { 2156 int res = strncmp(item->path,oldpath_2,strlen(oldpath_2)); 2157 socket_old = item->path; 2158 if(0 == res) 2159 { 2160 end = strlen(oldpath_2); 2161 socket_new = socket_old.replace(0,end,newpath_2); 2162 free(item->path); 2163 item->path = (char *)malloc(sizeof(char)*(socket_new.length() + 1)); 2164 sprintf(item->path,"%s",socket_new.c_str()); 2165 } 2166 item = item->next; 2167 } 2168} 2169 2170void set_re_cmd(char *oldpath,char *oldpath_1,char *oldpath_2,char *newpath,char *newpath_1,char *newpath_2,int i) 2171{ 2172 DEBUG("************************set_re_cmd***********************\n"); 2173 char *p = NULL; 2174 char *q = NULL; 2175 int start,end; 2176 string socket_old,socket_new; 2177 Node *pTemp = g_pSyncList[i]->SocketActionList->next; 2178 while(pTemp != NULL) 2179 { 2180 p = strstr(pTemp->cmdName,oldpath_1); 2181 q = strstr(pTemp->cmdName,oldpath_2); 2182 socket_old = pTemp->cmdName; 2183 if(p != NULL) 2184 { 2185 start = strlen(pTemp->cmdName) - strlen(p); 2186 end = strlen(oldpath_1); 2187 socket_new = socket_old.replace(start,end,newpath_1); 2188 free(pTemp->cmdName); 2189 pTemp->cmdName = (char *)malloc(sizeof(char)*(socket_new.length() + 1)); 2190 sprintf(pTemp->cmdName,"%s",socket_new.c_str()); 2191 } 2192 if(q != NULL) 2193 { 2194 start = strlen(pTemp->cmdName) - strlen(q); 2195 end = strlen(oldpath_2); 2196 socket_new = socket_old.replace(start,end,newpath_2); 2197 free(pTemp->cmdName); 2198 pTemp->cmdName = (char *)malloc(sizeof(char)*(socket_new.length() + 1)); 2199 sprintf(pTemp->cmdName,"%s",socket_new.c_str()); 2200 } 2201 2202 pTemp=pTemp->next; 2203 } 2204 2205 action_item *item = g_pSyncList[i]->copy_file_list->next; 2206 while(item != NULL) 2207 { 2208 int res = strncmp(item->path,oldpath_2,strlen(oldpath_2)); 2209 socket_old = item->path; 2210 if(0 == res) 2211 { 2212 end = strlen(oldpath_2); 2213 socket_new = socket_old.replace(0,end,newpath_2); 2214 free(item->path); 2215 item->path = (char *)malloc(sizeof(char)*(socket_new.length() + 1)); 2216 sprintf(item->path,"%s",socket_new.c_str()); 2217 } 2218 item = item->next; 2219 } 2220 2221 //show(g_pSyncList[i]->SocketActionList); 2222} 2223 2224int handle_rename_socket(char *buf,int i) 2225{ 2226 DEBUG("handle_rename_socket\n"); 2227 char *oldpath; 2228 char *oldpath_1; 2229 char *oldpath_2; 2230 char *newpath; 2231 char *newpath_1; 2232 char *newpath_2; 2233 char cmd_name[512] = {0}; 2234 char oldname[512] = {0}; 2235 char newname[512] = {0}; 2236 char *path; 2237 char *temp; 2238 char *ch = buf; 2239 int k = 0; 2240 while(*ch != '\n') 2241 { 2242 k++; 2243 ch++; 2244 } 2245 memcpy(cmd_name, buf, k); 2246 char *p = NULL; 2247 ch++; 2248 k++; 2249 temp = my_str_malloc((size_t)(strlen(ch)+1)); 2250 strcpy(temp,ch); 2251 //cout << temp << endl; 2252 p = strchr(temp,'\n'); 2253 path = my_str_malloc((size_t)(strlen(temp)- strlen(p) + 1)); 2254 //cout << path << endl; 2255 if(p!=NULL) 2256 snprintf(path,strlen(temp)- strlen(p)+1,"%s",temp); 2257 p++; 2258 char *p1 = NULL; 2259 p1 = strchr(p,'\n'); 2260 if(p1 != NULL) 2261 strncpy(oldname,p,strlen(p)- strlen(p1)); 2262 p1++; 2263 strcpy(newname,p1); 2264 2265 oldpath = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 2)); 2266 oldpath_1 = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 3)); 2267 oldpath_2 = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 3)); 2268 newpath = my_str_malloc((size_t)(strlen(path) + strlen(newname) + 2)); 2269 newpath_1 = my_str_malloc((size_t)(strlen(path) + strlen(newname) + 3)); 2270 newpath_2 = my_str_malloc((size_t)(strlen(path) + strlen(newname) + 3)); 2271 //oldpath = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + strlen("/")+1)); 2272 // oldpath_1 = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + strlen("/")+strlen("\n")+1)); 2273 //oldpath_2 = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + strlen("/")+strlen("/")+1)); 2274 //newpath = my_str_malloc((size_t)(strlen(path) + strlen(newname) + strlen("/")+1)); 2275 //newpath_1 = my_str_malloc((size_t)(strlen(path) + strlen(newname) + strlen("/")+strlen("\n")+1)); 2276 //newpath_2 = my_str_malloc((size_t)(strlen(path) + strlen(newname) + strlen("/")+strlen("/")+1)); 2277 sprintf(oldpath,"%s/%s",path,oldname); 2278 sprintf(oldpath_1,"%s/%s\n",path,oldname); 2279 sprintf(oldpath_2,"%s/%s/",path,oldname); 2280 sprintf(newpath,"%s/%s",path,newname); 2281 sprintf(newpath_1,"%s/%s\n",path,newname); 2282 sprintf(newpath_2,"%s/%s/",path,newname); 2283 free(path); 2284 free(temp); 2285 2286 set_re_cmd(oldpath,oldpath_1,oldpath_2,newpath,newpath_1,newpath_2,i); 2287 2288 free(oldpath); 2289 free(oldpath_1); 2290 free(oldpath_2); 2291 free(newpath); 2292 free(newpath_1); 2293 free(newpath_2); 2294 return 0; 2295} 2296 2297int add_socket_item(char *buf,int i) 2298{ 2299 DEBUG("comeing add_socket_item,i=%d\n",i); 2300 2301 pthread_mutex_lock(&mutex_receve_socket); 2302 g_pSyncList[i]->receve_socket = 1; 2303 pthread_mutex_unlock(&mutex_receve_socket); 2304 newNode = (Node *)malloc(sizeof(Node)); 2305 memset(newNode,0,sizeof(Node)); 2306 newNode->cmdName = (char *)malloc(sizeof(char)*(strlen(buf)+1)); 2307 //2014.10.29 by sherry sizeof(指针)=4 2308 //memset(newNode->cmdName,'\0',sizeof(newNode->cmdName)); 2309 memset(newNode->cmdName,'\0',sizeof(char)*(strlen(buf)+1)); 2310 sprintf(newNode->cmdName,"%s",buf); 2311 //newNode->re_cmd = NULL; 2312 Node *pTemp = g_pSyncList[i]->SocketActionList; 2313 while(pTemp->next != NULL) 2314 pTemp = pTemp->next; 2315 pTemp->next = newNode; 2316 newNode->next = NULL; 2317 show(g_pSyncList[i]->SocketActionList); 2318 DEBUG("end add_socket_item\n"); 2319 return 0; 2320} 2321 2322//char *search_xml_node(xmlNodePtr p) 2323//{ 2324// int count = 0; 2325// xmlChar *szAttr; 2326// char fold0[1024]; 2327// char *temp; 2328// char *res; 2329// res = (char*)malloc(sizeof(char)); 2330// memset(res,'\0',sizeof(res)); 2331// while(p != NULL) 2332// { 2333// szAttr = xmlGetProp(p,BAD_CAST p->properties->name); 2334// sprintf(fold0,"%s",szAttr); 2335// xmlNodePtr q = p->children; 2336// int _count = 0; 2337// while(q != NULL) 2338// { 2339// szAttr = xmlGetProp(q,BAD_CAST q->properties->name); 2340// q = q->next; 2341// _count++; 2342// } 2343// if(!xmlStrcmp(p->name,(const xmlChar*)("fold0"))) 2344// { 2345// temp = (char *)malloc(sizeof(char)*(strlen(fold0) + 12)); 2346// memset(temp,'\0',sizeof(char)*(strlen(fold0) + 12)); 2347// sprintf(temp,",\"%s#%d#%d\"",fold0,count,_count); 2348// int len = strlen(res) + strlen(temp) + 1; 2349// res = (char*)realloc(res,len); 2350// strncat(res,temp,strlen(temp)); 2351// free(temp); 2352// } 2353// p = p->next; 2354// count++; 2355// } 2356// if(count) 2357// { 2358// res = (char*)realloc(res,strlen(res) + 3); 2359// sprintf(res,"[%s]",res + 1); 2360// return res; 2361// } 2362// else 2363// { 2364// free(res); 2365// return NULL; 2366// } 2367//} 2368 2369void *Save_Socket(void *argc) 2370{ 2371 //写死socket 2372 // char buf1[256] ={0}; 2373 // char buf2[256] = {0}; 2374 // sprintf(buf1,"%s","createfolder\n/tmp/mnt/SNK/sync\n未命名文件夹"); 2375 // sprintf(buf2,"%s","rename0\n/tmp/mnt/SNK/sync\n未命名文件夹\na"); 2376 2377 // int i=0; 2378 // pthread_mutex_lock(&mutex_socket); 2379 // handle_rename_socket(buf2,i); 2380 // add_socket_item_for_rename(buf2,i); 2381 // add_socket_item(buf1,i); 2382 // pthread_mutex_unlock(&mutex_socket); 2383 2384 DEBUG("Save_Socket:%u\n",pthread_self()); 2385 int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd*/ 2386 int numbytes; 2387 char buf[MAXDATASIZE]; 2388 int yes; 2389 int ret; 2390 2391 fd_set read_fds; 2392 fd_set master; 2393 int fdmax; 2394 struct timeval timeout; 2395 2396 FD_ZERO(&read_fds); 2397 FD_ZERO(&master); 2398 2399 struct sockaddr_in my_addr; /* my address information */ 2400 struct sockaddr_in their_addr; /* connector's address information */ 2401 socklen_t sin_size; 2402 //int sin_size; 2403 2404 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 2405 perror("socket"); 2406 exit(1); 2407 } 2408 2409 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) 2410 { 2411 perror("Server-setsockopt() error lol!"); 2412 exit(1); 2413 } 2414 2415 my_addr.sin_family = AF_INET; /* host byte order */ 2416 my_addr.sin_port = htons(MYPORT); /* short, network byte order */ 2417 my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ 2418 bzero(&(my_addr.sin_zero), sizeof(my_addr.sin_zero)); /* zero the rest of the struct */ 2419 2420 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct 2421 sockaddr))== -1) { 2422 perror("bind"); 2423 exit(1); 2424 } 2425 if (listen(sockfd, BACKLOG) == -1) { 2426 perror("listen"); 2427 exit(1); 2428 } 2429 sin_size = sizeof(struct sockaddr_in); 2430 2431 FD_SET(sockfd,&master); 2432 fdmax = sockfd; 2433 2434 while(!exit_loop) 2435 { /* main accept() loop */ 2436 //DEBUG("listening!\n"); 2437 timeout.tv_sec = 5; 2438 timeout.tv_usec = 0; 2439 2440 read_fds = master; 2441 2442 ret = select(fdmax+1,&read_fds,NULL,NULL,&timeout); 2443 2444 switch (ret) 2445 { 2446 case 0: 2447 continue; 2448 break; 2449 case -1: 2450 perror("select"); 2451 continue; 2452 break; 2453 default: 2454 if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, 2455 &sin_size)) == -1) { 2456 perror("accept"); 2457 continue; 2458 } 2459 memset(buf, 0, sizeof(buf)); 2460 2461 if ((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) { 2462 perror("recv"); 2463 exit(1); 2464 } 2465 2466 if(buf[strlen(buf)] == '\n') 2467 { 2468 buf[strlen(buf)] = '\0'; 2469 } 2470 2471 2472 int i=0; 2473#ifdef _PC 2474// if(strncmp(buf,"refresh",7)) 2475// { 2476// char *r_path = get_socket_base_path(buf); 2477// DEBUG("r_path:%s\n",r_path); 2478// for(i=0;i<ftp_config.dir_num;i++) 2479// { 2480// DEBUG("r_path:%s,base_path=%s\n",r_path,ftp_config.multrule[i]->base_path); 2481// if(!strcmp(r_path,ftp_config.multrule[i]->base_path)) 2482// { 2483// free(r_path); 2484// break; 2485// } 2486// } 2487 2488// } 2489#else 2490 if(strncmp(buf,"refresh",7)) 2491 { 2492 char *r_path = get_socket_base_path(buf); 2493 DEBUG("r_path:%s\n",r_path); 2494 for(i=0;i<ftp_config.dir_num;i++) 2495 { 2496 if(!strcmp(r_path,ftp_config.multrule[i]->base_path)) 2497 { 2498 free(r_path); 2499 break; 2500 } 2501 } 2502 2503 } 2504#endif 2505 pthread_mutex_lock(&mutex_socket); 2506 if(!strncmp(buf,"rename0",7)) 2507 { 2508 DEBUG("it is renamed folder!\n"); 2509 handle_rename_socket(buf,i); 2510 add_socket_item_for_rename(buf,i); 2511 } 2512 else 2513 { 2514 add_socket_item(buf,i); 2515 } 2516 close(new_fd); 2517 pthread_mutex_unlock(&mutex_socket); 2518 } 2519 } 2520 close(sockfd); 2521 2522 DEBUG("stop FtpClient local sync\n"); 2523 2524 stop_down = 1; 2525 return NULL; 2526} 2527 2528void parseCloudInfo_forsize(char *parentherf) 2529{ 2530 FILE *fp; 2531 fp = fopen(LIST_SIZE_DIR,"r"); 2532 char tmp[512] = {0}; 2533 char buf[512] = {0}; 2534 const char *split = " "; 2535 const char *split_2 = "\n"; 2536 char *p; 2537 while(fgets(buf,sizeof(buf),fp)!=NULL) 2538 { 2539 int fail = 0; 2540 FolderTmp_size=(CloudFile *)malloc(sizeof(struct node)); 2541 memset(FolderTmp_size,0,sizeof(FolderTmp_size)); 2542 FolderTmp_size->next = NULL; //2016.2.25 by tina 2543 p = strtok(buf,split); 2544 int i=0; 2545 while(p != NULL && i <= 8) 2546 { 2547 switch(i) 2548 { 2549 case 0: 2550 strcpy(tmp,p); 2551 //strcpy(FolderTmp_size->auth,p); 2552 FolderTmp_size->isfile=is_file(tmp); 2553 break; 2554 case 4: 2555 FolderTmp_size->size=atoll(p); 2556 break; 2557 case 5: 2558 //strcpy(FolderTmp_size->month,p); 2559 break; 2560 case 6: 2561 //strcpy(FolderTmp_size->day,p); 2562 break; 2563 case 7: 2564 //strcpy(FolderTmp_size->lastmodifytime,p); 2565 break; 2566 case 8: 2567 if(!strcmp(p,".") || !strcmp(p,"..")) 2568 fail = 1; 2569 strcpy(FolderTmp_size->filename,p); 2570 break; 2571 default: 2572 break; 2573 } 2574 i++; 2575 if(i<=7) 2576 p=strtok(NULL,split); 2577 else 2578 p=strtok(NULL,split_2); 2579 } 2580 2581 FolderTmp_size->href = (char *)malloc(sizeof(char)*(strlen(parentherf)+1+1+strlen(FolderTmp_size->filename))); 2582 //FolderTmp_size->href = (char *)malloc(sizeof(char)*(strlen(parentherf)+strlen("/")+1+strlen(FolderTmp_size->filename))); 2583 memset(FolderTmp_size->href,'\0',sizeof(FolderTmp_size->href)); 2584 if(strcmp(parentherf," ")==0) 2585 sprintf(FolderTmp_size->href,"%s",FolderTmp_size->filename); 2586 else 2587 sprintf(FolderTmp_size->href,"%s/%s",parentherf,FolderTmp_size->filename); 2588 if(!fail) 2589 { 2590 if(FolderTmp_size->isfile==0) //文件夹链表 2591 { 2592 //FolderTail_size->next = FolderTmp_size; 2593 //FolderTail_size = FolderTmp_size; 2594 //FolderTail_size->next = NULL; 2595 } 2596 else if(FolderTmp_size->isfile==1) //文件链表 2597 { 2598 FileTail_size->next = FolderTmp_size; 2599 FileTail_size = FolderTmp_size; 2600 FileTail_size->next = NULL; 2601 } 2602 } 2603 else 2604 { 2605 free_CloudFile_item(FolderTmp_size); 2606 } 2607 } 2608 fclose(fp); 2609} 2610 2611long long int get_file_size(char *serverpath,int index) 2612{ 2613 char *url; 2614 long long int ret = 0; 2615 FileList_size = (CloudFile *)malloc(sizeof(CloudFile)); 2616 memset(FileList_size,0,sizeof(CloudFile)); 2617 2618 FileList_size->href = NULL; 2619 FileTail_size = FileList_size; 2620 FileTail_size->next = NULL; 2621 url = get_prepath(serverpath,strlen(serverpath)); 2622 2623 getCloudInfo_forsize(url,parseCloudInfo_forsize,index); 2624 CloudFile *de_filecurrent; 2625 de_filecurrent = FileList_size->next; 2626 2627 while(de_filecurrent != NULL) 2628 { 2629 if(de_filecurrent->href != NULL) 2630 { 2631 if(!(strcmp(de_filecurrent->href,serverpath))) 2632 { 2633 ret = de_filecurrent->size; 2634 free_CloudFile_item(FileList_size); 2635 free(url); 2636 return ret; 2637 } 2638 } 2639 de_filecurrent = de_filecurrent->next; 2640 } 2641 free(url); 2642 free_CloudFile_item(FileList_size); 2643} 2644 2645 2646 2647mod_time *ftp_MDTM(char *href,int index) 2648{ 2649 mod_time *mtime; 2650 2651 char *command = (char *)malloc(sizeof(char)*(strlen(href) + 6)); 2652 //char *command = (char *)malloc(sizeof(char)*(strlen(href) + strlen("MDTM ")+1)); 2653 memset(command,'\0',sizeof(command)); 2654 sprintf(command,"MDTM %s",href+1); 2655 DEBUG("%s\n",command); 2656 char *temp = utf8_to(command,index); 2657 free(command); 2658 CURL *curl_t; 2659 CURLcode res; 2660 curl_t = curl_easy_init(); 2661 FILE *fp = fopen(TIME_DIR,"w+"); 2662 //fp=fopen("/tmp/ftpclient/time.txt","w+"); 2663 if(curl_t){ 2664 curl_easy_setopt(curl_t, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 2665 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 2666 curl_easy_setopt(curl_t, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 2667 //curl_easy_setopt(curl_t, CURLOPT_FTP_USE_EPSV, 0); 2668 curl_easy_setopt(curl_t, CURLOPT_WRITEHEADER,fp); 2669 curl_easy_setopt(curl_t, CURLOPT_CUSTOMREQUEST,temp); 2670 curl_easy_setopt(curl_t, CURLOPT_LOW_SPEED_LIMIT,1); 2671 curl_easy_setopt(curl_t, CURLOPT_LOW_SPEED_TIME,30); 2672 curl_easy_setopt(curl_t, CURLOPT_VERBOSE, 0L); 2673 res = curl_easy_perform(curl_t); 2674 DEBUG("res = %d\n",res); 2675 if(res != CURLE_OK && res != CURLE_FTP_COULDNT_RETR_FILE) 2676 { 2677 curl_easy_cleanup(curl_t); 2678 fclose(fp); 2679 free(temp); 2680 //DEBUG("get %s mtime failed res =%d\n",href,res); 2681 if(res == CURLE_COULDNT_CONNECT) 2682 { 2683 write_log(S_ERROR,"Could not connect.","",index); 2684 } 2685 else if(res == CURLE_OPERATION_TIMEDOUT) 2686 { 2687 write_log(S_ERROR,"Connect timeout.","",index); 2688 } 2689 else if(res == CURLE_REMOTE_ACCESS_DENIED) 2690 { 2691 write_log(S_ERROR,"Connect refused.","",index); 2692 } 2693 mtime = (mod_time *)malloc(sizeof(mod_time)); 2694 memset(mtime,0,sizeof(mtime)); 2695 mtime->modtime = (time_t)-1; 2696 return mtime; 2697 } 2698 else 2699 { 2700 free(temp); 2701 curl_easy_cleanup(curl_t); 2702 mtime = get_mtime(fp); 2703 return mtime; 2704 } 2705 } 2706} 2707 2708int parseCloudInfo_tree(char *parentherf,int index) 2709{ 2710 FILE *fp; 2711 fp = fopen(LIST_DIR,"r"); 2712 char tmp[512] = {0}; 2713 char buf[512] = {0}; 2714 const char *split = " "; 2715 const char *split_1 = " \n"; 2716 const char *split_2 = "\n"; 2717 char *str,*temp; 2718 char *p; 2719 while(fgets(buf,sizeof(buf),fp)!=NULL) 2720 { 2721 int fail = 0; 2722 DEBUG("BUF:%s\n",buf); 2723 FolderTmp=(CloudFile *)malloc(sizeof(struct node)); 2724 memset(FolderTmp,0,sizeof(FolderTmp)); 2725 FolderTmp->next = NULL; //2016.2.25 by tina 2726 p=strtok(buf,split); 2727 int i=0; 2728 //DEBUG("p:%s\n",p); 2729 //string tmpStr; 2730 if(strlen(p) == 8) 2731 { 2732 int flag = 0; 2733 while(i <= 3)//p != NULL && 2734 { 2735 switch(i) 2736 { 2737 case 1: 2738 //strncpy(FolderTmp->lastmodifytime,p,5); 2739 break; 2740 case 2: 2741 strcpy(tmp,p); 2742 flag = is_folder(tmp); 2743 if(flag == 0) 2744 { 2745 FolderTmp->isfile = 0; 2746 } 2747 else 2748 { 2749 FolderTmp->isfile = 1; 2750 FolderTmp->size=atoll(p); 2751 } 2752 break; 2753 case 3: 2754 if(p == NULL) 2755 { 2756 free_CloudFile_item(FolderTmp); 2757 //fclose(fp); 2758 //return UNSUPPORT_ENCODING; 2759 break; 2760 } 2761 str = (char*)malloc(sizeof(char)*(strlen(p) + 1)); 2762 strcpy(str,p); 2763 temp = to_utf8(str,index); 2764 strcpy(FolderTmp->filename,temp); 2765 break; 2766 default: 2767 break; 2768 } 2769 i++; 2770 if(i<3) 2771 { 2772 p=strtok(NULL,split); 2773 } 2774 else 2775 { 2776 if(FolderTmp->isfile == 0) 2777 p=strtok(NULL,split_1); 2778 else 2779 p=strtok(NULL,split_2); 2780 } 2781 } 2782 } 2783 else 2784 { 2785 while(i <= 8)//p != NULL && 2786 { 2787 //DEBUG("p:%s %d\n",p,i); 2788 switch(i) 2789 { 2790 case 0: 2791 strcpy(tmp,p); 2792 //strcpy(FolderTmp->auth,p); 2793 FolderTmp->isfile=is_file(tmp); 2794 break; 2795 case 4: 2796 FolderTmp->size=atoll(p); 2797 break; 2798 case 5: 2799 //strcpy(FolderTmp->month,p); 2800 break; 2801 case 6: 2802 //strcpy(FolderTmp->day,p); 2803 break; 2804 case 7: 2805 //strcpy(FolderTmp->lastmodifytime,p); 2806 break; 2807 case 8: 2808 if(p == NULL || !strcmp(p,".") || !strcmp(p,"..")) 2809 { 2810 DEBUG("free item\n"); 2811 fail = 1; 2812 } 2813 str = (char*)malloc(sizeof(char)*(strlen(p) + 1)); 2814 strcpy(str,p); 2815 temp = to_utf8(str,index); 2816 free(str); 2817 strcpy(FolderTmp->filename,temp); 2818 break; 2819 default: 2820 break; 2821 } 2822 i++; 2823 if(i<=7) 2824 { 2825 p=strtok(NULL,split); 2826 //DEBUG("p:%s\n",p); 2827 } 2828 else 2829 { 2830 //DEBUG("p:%s\n",p); 2831 p=strtok(NULL,split_2); 2832 } 2833 } 2834 } 2835 free(temp); 2836 FolderTmp->href = (char *)malloc(sizeof(char)*(strlen(parentherf)+1+1+strlen(FolderTmp->filename)));//"1">strlen("/") 2837 2838 memset(FolderTmp->href,'\0',sizeof(FolderTmp->href)); 2839 if(strcmp(parentherf," ")==0) 2840 sprintf(FolderTmp->href,"%s",FolderTmp->filename); 2841 else 2842 sprintf(FolderTmp->href,"%s/%s",parentherf,FolderTmp->filename); 2843 if(!fail) 2844 { 2845 if(FolderTmp->isfile==0) //文件夹链表 2846 { 2847 TreeFolderTail->next=FolderTmp; 2848 TreeFolderTail=FolderTmp; 2849 TreeFolderTail->next=NULL; 2850 } 2851 else if(FolderTmp->isfile==1) //文件链表 2852 { 2853 mod_time *mtime; 2854 mtime = ftp_MDTM(FolderTmp->href,index); 2855 if(mtime->modtime == -1) 2856 { 2857 fclose(fp); 2858 return -1; 2859 } 2860 //DEBUG("mtime->modtime = %lu,mtime->mtime = %s\n",mtime->modtime,mtime->mtime); 2861 FolderTmp->modtime = mtime->modtime; 2862 //strcpy(FolderTmp->mtime,mtime->mtime); 2863 //DEBUG("FolderTmp->modtime = %lu,FolderTmp->mtime = %s\n",FolderTmp->modtime,FolderTmp->mtime); 2864 free(mtime); 2865 TreeFileTail->next=FolderTmp; 2866 TreeFileTail=FolderTmp; 2867 TreeFileTail->next=NULL; 2868 } 2869 } 2870 else 2871 { 2872 free_CloudFile_item(FolderTmp); 2873 //fclose(fp); //2016.2.25 by tina 2874 //return 912; //2016.2.25 by tina 2875 } 2876 } 2877 fclose(fp); 2878 return 0; 2879} 2880 2881int getCloudInfo(char *URL,int (* cmd_data)(char *,int),int index) 2882{ 2883 int status; 2884 char *command = (char *)malloc(sizeof(char)*(strlen(URL) + 7)); 2885 memset(command,'\0',sizeof(command)); 2886 sprintf(command,"LIST %s",URL); 2887 DEBUG("command = %s\n",command); 2888 char *temp = utf8_to(command,index); 2889 free(command); 2890 DEBUG("temp = %s\n",temp); 2891 CURL *curl; 2892 CURLcode res; 2893 FILE *fp = fopen(LIST_DIR,"wb"); 2894 curl = curl_easy_init(); 2895 if(curl) 2896 { 2897 curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip); 2898 if(strlen(ftp_config.multrule[index]->user_pwd) != 1) 2899 curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd); 2900 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,temp); 2901 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); 2902 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1); 2903 curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,30); 2904 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); 2905 res = curl_easy_perform(curl); 2906 DEBUG("getCloudInfo() - res = %d\n",res); 2907 curl_easy_cleanup(curl); 2908 fclose(fp); 2909 free(temp); 2910 if(res != CURLE_OK) 2911 { 2912 if(res == CURLE_COULDNT_CONNECT) 2913 { 2914 2915 write_log(S_ERROR,"Could not connect.","",index); 2916 2917 } 2918 else if(res == CURLE_OPERATION_TIMEDOUT) 2919 { 2920 write_log(S_ERROR,"Connect timeout.","",index); 2921 } 2922 else if(res == CURLE_REMOTE_ACCESS_DENIED) 2923 { 2924 write_log(S_ERROR,"Connect refused.","",index); 2925 } 2926 else if(res == CURLE_FTP_COULDNT_RETR_FILE) 2927 { 2928 write_log(S_ERROR, "Permission denied.", "", index); 2929 } 2930 2931 return -1; 2932 } 2933 else 2934 { 2935 status = cmd_data(URL,index); 2936 if(status == 912) 2937 { 2938 write_log(S_ERROR,"Unsuported encoding:BIG5 Contains Simplified.","",index); 2939 return UNSUPPORT_ENCODING; 2940 } 2941 return status; 2942 } 2943 } 2944 else 2945 { 2946 fclose(fp); 2947 free(temp); 2948 return 0; 2949 } 2950} 2951 2952Browse *browseFolder(char *URL,int index) 2953{ 2954 DEBUG("browseFolder URL = %s\n",URL); 2955 int status; 2956 int i=0; 2957 2958 Browse *browse = (Browse *)malloc(sizeof(Browse)); 2959 if( NULL == browse ) 2960 { 2961 DEBUG("create memery error\n"); 2962 exit(-1); 2963 } 2964 memset(browse,0,sizeof(Browse)); 2965 2966 TreeFolderList = (CloudFile *)malloc(sizeof(CloudFile)); 2967 memset(TreeFolderList,0,sizeof(CloudFile)); 2968 TreeFileList = (CloudFile *)malloc(sizeof(CloudFile)); 2969 memset(TreeFileList,0,sizeof(CloudFile)); 2970 2971 TreeFolderList->href = NULL; 2972 TreeFileList->href = NULL; 2973 2974 TreeFolderTail = TreeFolderList; 2975 TreeFileTail = TreeFileList; 2976 TreeFolderTail->next = NULL; 2977 TreeFileTail->next = NULL; 2978 2979 status = getCloudInfo(URL,parseCloudInfo_tree,index); 2980 DEBUG("end getCloudInfo,status = %d\n",status); 2981 if(status != 0) 2982 { 2983 free_CloudFile_item(TreeFolderList); 2984 free_CloudFile_item(TreeFileList); 2985 TreeFolderList = NULL; 2986 TreeFileList = NULL; 2987 free(browse); 2988 return NULL; 2989 } 2990 2991 browse->filelist = TreeFileList; 2992 browse->folderlist = TreeFolderList; 2993 2994 CloudFile *de_foldercurrent,*de_filecurrent; 2995 de_foldercurrent = TreeFolderList->next; 2996 de_filecurrent = TreeFileList->next; 2997 while(de_foldercurrent != NULL){ 2998 ++i; 2999 de_foldercurrent = de_foldercurrent->next; 3000 } 3001 browse->foldernumber = i; 3002 i = 0; 3003 while(de_filecurrent != NULL){ 3004 ++i; 3005 de_filecurrent = de_filecurrent->next; 3006 } 3007 browse->filenumber = i; 3008 return browse; 3009} 3010 3011int browse_to_tree(char *parenthref,Server_TreeNode *node,int index) 3012{ 3013 if(exit_loop) 3014 { 3015 return -1; 3016 } 3017 if(index == -1) 3018 { 3019 return -1; 3020 } 3021 Browse *br = NULL; 3022 int fail_flag = 0; 3023 3024 Server_TreeNode *tempnode = NULL, *p1 = NULL,*p2 = NULL; 3025 tempnode = create_server_treeroot(); 3026 tempnode->level = node->level + 1; 3027 3028 tempnode->parenthref = my_str_malloc((size_t)(strlen(parenthref)+1)); 3029 memset(tempnode->parenthref,'\0',sizeof(tempnode->parenthref)); 3030 3031 DEBUG("parenthref:%s\n",parenthref); 3032 br = browseFolder(parenthref,index); 3033 sprintf(tempnode->parenthref,"%s",parenthref); 3034 if(NULL == br) 3035 { 3036 free_server_tree(tempnode); 3037 DEBUG("browse folder failed\n"); 3038 return -1; 3039 } 3040 3041 tempnode->browse = br; 3042 3043 if(node->Child == NULL) 3044 { 3045 node->Child = tempnode; 3046 } 3047 else 3048 { 3049 DEBUG("have child\n"); 3050 p2 = node->Child; 3051 p1 = p2->NextBrother; 3052 3053 while(p1 != NULL) 3054 { 3055 DEBUG("p1 nextbrother have\n"); 3056 p2 = p1; 3057 p1 = p1->NextBrother; 3058 } 3059 3060 p2->NextBrother = tempnode; 3061 tempnode->NextBrother = NULL; 3062 } 3063 DEBUG("browse folder num is %d\n",br->foldernumber); 3064 CloudFile *de_foldercurrent; 3065 de_foldercurrent = br->folderlist->next; 3066 while(de_foldercurrent != NULL) 3067 { 3068 if(browse_to_tree(de_foldercurrent->href,tempnode,index) == -1) 3069 { 3070 fail_flag = 1; 3071 } 3072 de_foldercurrent = de_foldercurrent->next; 3073 } 3074 return (fail_flag == 1) ? -1 : 0 ; 3075 3076} 3077 3078/*获取某一文件夹下的所有文件和文件夹信息*/ 3079Local *Find_Floor_Dir(const char *path) 3080{ 3081 Local *local; 3082 int filenum; 3083 int foldernum; 3084 LocalFile *localfloorfile; 3085 LocalFolder *localfloorfolder; 3086 LocalFile *localfloorfiletmp; 3087 LocalFolder *localfloorfoldertmp; 3088 LocalFile *localfloorfiletail; 3089 LocalFolder *localfloorfoldertail; 3090 DIR *pDir; 3091 struct dirent *ent = NULL; 3092 3093 filenum = 0; 3094 foldernum = 0; 3095 local = (Local *)malloc(sizeof(Local)); 3096 memset(local,0,sizeof(Local)); 3097 localfloorfile = (LocalFile *)malloc(sizeof(LocalFile)); 3098 localfloorfolder = (LocalFolder *)malloc(sizeof(LocalFolder)); 3099 memset(localfloorfolder,0,sizeof(localfloorfolder)); 3100 memset(localfloorfile,0,sizeof(localfloorfile)); 3101 3102 localfloorfile->path = NULL; 3103 localfloorfolder->path = NULL; 3104 localfloorfiletail = localfloorfile; 3105 localfloorfoldertail = localfloorfolder; 3106 localfloorfiletail->next = NULL; 3107 localfloorfoldertail->next = NULL; 3108 3109 pDir = opendir(path); 3110 if(NULL == pDir) 3111 { 3112 return NULL; 3113 } 3114 3115 while(NULL != (ent = readdir(pDir))) 3116 { 3117 if(ent->d_name[0] == '.') 3118 continue; 3119 if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) 3120 continue; 3121 if(test_if_download_temp_file(ent->d_name)) //download temp files 3122 continue; 3123 3124 char *fullname; 3125 size_t len; 3126 len = strlen(path)+strlen(ent->d_name)+2; 3127 fullname = my_str_malloc(len); 3128 sprintf(fullname,"%s/%s",path,ent->d_name); 3129 3130 //DEBUG("folder fullname = %s\n",fullname); 3131 //DEBUG("ent->d_ino = %d\n",ent->d_ino); 3132 3133 if(test_if_dir(fullname) == 1) 3134 { 3135 localfloorfoldertmp = (LocalFolder *)malloc(sizeof(LocalFolder)); 3136 memset(localfloorfoldertmp,0,sizeof(localfloorfoldertmp)); 3137 localfloorfoldertmp->path = my_str_malloc((size_t)(strlen(fullname)+1)); 3138 3139 sprintf(localfloorfoldertmp->name,"%s",ent->d_name); 3140 sprintf(localfloorfoldertmp->path,"%s",fullname); 3141 3142 ++foldernum; 3143 3144 localfloorfoldertail->next = localfloorfoldertmp; 3145 localfloorfoldertail = localfloorfoldertmp; 3146 localfloorfoldertail->next = NULL; 3147 } 3148 else 3149 { 3150 struct stat buf; 3151 3152 if(stat(fullname,&buf) == -1) 3153 { 3154 perror("stat:"); 3155 continue; 3156 } 3157 3158 localfloorfiletmp = (LocalFile *)malloc(sizeof(LocalFile)); 3159 memset(localfloorfiletmp,0,sizeof(localfloorfiletmp)); 3160 localfloorfiletmp->path = my_str_malloc((size_t)(strlen(fullname)+1)); 3161 3162 //unsigned long asec = buf.st_atime; 3163 unsigned long msec = buf.st_mtime; 3164 //unsigned long csec = buf.st_ctime; 3165 3166 //sprintf(localfloorfiletmp->creationtime,"%lu",csec); 3167 //sprintf(localfloorfiletmp->lastaccesstime,"%lu",asec); 3168 //sprintf(localfloorfiletmp->lastwritetime,"%lu",msec); 3169 localfloorfiletmp->modtime = msec; 3170 3171 sprintf(localfloorfiletmp->name,"%s",ent->d_name); 3172 sprintf(localfloorfiletmp->path,"%s",fullname); 3173 3174 //localfloorfiletmp->size = buf.st_size; 3175 ++filenum; 3176 3177 localfloorfiletail->next = localfloorfiletmp; 3178 localfloorfiletail = localfloorfiletmp; 3179 localfloorfiletail->next = NULL; 3180 } 3181 free(fullname); 3182 } 3183 local->filelist = localfloorfile; 3184 local->folderlist = localfloorfolder; 3185 3186 local->filenumber = filenum; 3187 local->foldernumber = foldernum; 3188 3189 closedir(pDir); 3190 return local; 3191 3192} 3193 3194/*0,local file newer 3195 *1,server file newer 3196 *2,local time == server time 3197 *-1,get server modtime failed 3198**/ 3199int newer_file(char *localpath,int index,int flag)//比较server和loacal端的文件谁最新 3200{ 3201 char *serverpath = localpath_to_serverpath(localpath,index); 3202 3203 mod_time *modtime1; 3204 time_t modtime2,oldtime; 3205 modtime1 = Getmodtime(serverpath,index); 3206 if(modtime1->modtime == -1) 3207 { 3208 DEBUG("newer_file Getmodtime failed!\n"); 3209 free(modtime1); 3210 return -1; 3211 } 3212 3213 struct stat buf; 3214 if( stat(localpath,&buf) == -1) 3215 { 3216 perror("stat:"); 3217 free(modtime1); 3218 return -1; 3219 } 3220 modtime2 = buf.st_mtime; 3221 DEBUG("new local time:%ld\n",modtime2); 3222 DEBUG("new server time:%ld\n",modtime1->modtime); 3223 if(flag) 3224 { 3225 CloudFile *cloud_file = get_CloudFile_node(g_pSyncList[index]->OrigServerRootNode,serverpath,0x2); 3226 if(cloud_file != NULL) 3227 { 3228 DEBUG("old server time:%ld\n",cloud_file->modtime); 3229 oldtime = cloud_file->modtime; 3230 } 3231 else 3232 { 3233 oldtime = (time_t)-1; 3234 } 3235 free(serverpath); 3236 if(ftp_config.multrule[index]->rules == 1)//download only 3237 { 3238 if(modtime2 > modtime1->modtime) 3239 { 3240 free(modtime1); 3241 return 0; 3242 } 3243 else if(modtime2 == modtime1->modtime) 3244 { 3245 free(modtime1); 3246 return 2; 3247 } 3248 else 3249 { 3250 if(modtime2 == oldtime) 3251 { 3252 free(modtime1); 3253 return 1; 3254 } 3255 else 3256 { 3257 free(modtime1); 3258 return 0; 3259 } 3260 } 3261 } 3262 else 3263 { 3264 if(modtime2 > modtime1->modtime) 3265 { 3266 free(modtime1); 3267 return 0; 3268 } 3269 else if(modtime2 == modtime1->modtime) 3270 { 3271 free(modtime1); 3272 return 2; 3273 } 3274 else 3275 { 3276 free(modtime1); 3277 return 1; 3278 } 3279 } 3280 } 3281 free(serverpath); 3282 if(!flag)//for init 3283 { 3284 if(modtime1->modtime - modtime2 == 1 || modtime2 - modtime1->modtime == 1) 3285 { 3286 if(ChangeFile_modtime(localpath,modtime1->modtime,index)) 3287 { 3288 DEBUG("ChangeFile_modtime failed!\n"); 3289 } 3290 free(modtime1); 3291 return 2; 3292 } 3293 else if(modtime2 > modtime1->modtime) 3294 { 3295 free(modtime1); 3296 return 0; 3297 } 3298 else if(modtime2 == modtime1->modtime) 3299 { 3300 free(modtime1); 3301 return 2; 3302 } 3303 else 3304 { 3305 free(modtime1); 3306 return 1; 3307 } 3308 } 3309} 3310 3311int the_same_name_compare(LocalFile *localfiletmp,CloudFile *filetmp,int index,int flag) 3312{ 3313 int status = 0; 3314 int newer_file_ret = 0; 3315 3316 if(!flag) 3317 DEBUG("###################the same name compare...for init...####################\n"); 3318 else 3319 DEBUG("###################the same name compare...####################\n"); 3320 DEBUG("local:%s\t\tserver:%s\n",localfiletmp->name,filetmp->filename); 3321 DEBUG("local:%ld\t\tserver:%ld\n",localfiletmp->modtime,filetmp->modtime); 3322 3323 if(ftp_config.multrule[index]->rules == 1) 3324 { 3325 newer_file_ret = newer_file(localfiletmp->path,index,flag); 3326 DEBUG("newer_file_ret:%d\n",newer_file_ret); 3327 if(newer_file_ret != 2 && newer_file_ret != -1) 3328 { 3329 action_item *item1; 3330 item1 = get_action_item("download_only",localfiletmp->path, 3331 g_pSyncList[index]->download_only_socket_head,index); 3332 if(item1 != NULL) 3333 { 3334 char *mynewname = change_local_same_name(localfiletmp->path); 3335 rename(localfiletmp->path,mynewname); 3336 char *err_msg = write_error_message("%s is download from server,%s is local file and rename from %s",localfiletmp->path,mynewname,localfiletmp->path); 3337 write_conflict_log(localfiletmp->name,3,err_msg); //conflict 3338 free(mynewname); 3339 free(err_msg); 3340 add_action_item("rename",localfiletmp->path,g_pSyncList[index]->server_action_list); 3341 } 3342 char *localpath = serverpath_to_localpath(filetmp->href,index); 3343 3344 action_item *item; 3345 item = get_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list,index); 3346 3347 if(is_local_space_enough(filetmp,index)) 3348 { 3349 if(wait_handle_socket(index)) 3350 { 3351 return HAVE_LOCAL_SOCKET; 3352 } 3353 3354 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 3355 3356 status = download_(filetmp->href,index); 3357 if(status == 0) 3358 { 3359 DEBUG("do_cmd ok\n"); 3360 mod_time *onefiletime = Getmodtime(filetmp->href,index); 3361 if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 3362 { 3363 DEBUG("ChangeFile_modtime failed!\n"); 3364 } 3365 free(onefiletime); 3366 if(item != NULL) 3367 { 3368 del_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3369 } 3370 } 3371 } 3372 else 3373 { 3374 write_log(S_ERROR,"local space is not enough!","",index); 3375 if(item == NULL) 3376 { 3377 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3378 } 3379 } 3380 free(localpath); 3381 } 3382 } 3383 else if(ftp_config.multrule[index]->rules == 2) 3384 { 3385 newer_file_ret = newer_file(localfiletmp->path,index,flag); 3386 DEBUG("newer_file_ret:%d\n",newer_file_ret); 3387 if(newer_file_ret != 2 && newer_file_ret != -1) 3388 { 3389 if(wait_handle_socket(index)) 3390 { 3391 return HAVE_LOCAL_SOCKET; 3392 } 3393 char *temp = change_server_same_name(localfiletmp->path,index); 3394 my_rename_(localfiletmp->path,temp,index); 3395 //free(temp); 3396 status = upload(localfiletmp->path,index); 3397 if(status == 0) 3398 { 3399 char *err_msg = write_error_message("server file %s is renamed to %s",localfiletmp->path,temp); 3400 write_conflict_log(localfiletmp->path,3,err_msg); //conflict 3401 free(err_msg); 3402 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 3403 mod_time *onefiletime = Getmodtime(serverpath,index); 3404 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 3405 { 3406 DEBUG("ChangeFile_modtime failed!\n"); 3407 } 3408 free(onefiletime); 3409 free(serverpath); 3410 } 3411 else 3412 { 3413 return status; 3414 } 3415 } 3416 } 3417 else 3418 { 3419 newer_file_ret = newer_file(localfiletmp->path,index,flag); 3420 DEBUG("newer_file_ret:%d\n",newer_file_ret); 3421 if(!flag) 3422 { 3423 if(newer_file_ret != 2 && newer_file_ret != -1) 3424 { 3425 char *temp = change_server_same_name(localfiletmp->path,index); 3426 my_rename_(localfiletmp->path,temp,index); 3427 //free(temp); 3428 status = upload(localfiletmp->path,index); 3429 if(status == 0) 3430 { 3431 char *err_msg = write_error_message("server file %s is renamed to %s",localfiletmp->path,temp); 3432 write_conflict_log(localfiletmp->path,3,err_msg); //conflict 3433 free(err_msg); 3434 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 3435 mod_time *onefiletime = Getmodtime(serverpath,index); 3436 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 3437 { 3438 DEBUG("ChangeFile_modtime failed!\n"); 3439 } 3440 free(onefiletime); 3441 free(serverpath); 3442 } 3443 else 3444 { 3445 return status; 3446 } 3447 } 3448 } 3449 else 3450 { 3451 if(newer_file_ret == 1) 3452 { 3453 char *localpath = serverpath_to_localpath(filetmp->href,index); 3454 add_action_item("remove",localfiletmp->path,g_pSyncList[index]->server_action_list); 3455 unlink(localfiletmp->path); 3456 action_item *item; 3457 item = get_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list,index); 3458 3459 if(is_local_space_enough(filetmp,index)) 3460 { 3461 if(wait_handle_socket(index)) 3462 { 3463 return HAVE_LOCAL_SOCKET; 3464 } 3465 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 3466 status = download_(filetmp->href,index); 3467 if (status == 0) 3468 { 3469 mod_time *onefiletime = Getmodtime(filetmp->href,index); 3470 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 3471 { 3472 DEBUG("ChangeFile_modtime failed!\n"); 3473 } 3474 free(onefiletime); 3475 if(item != NULL) 3476 { 3477 del_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3478 } 3479 } 3480 else 3481 { 3482 free(localpath); 3483 return status; 3484 } 3485 } 3486 else 3487 { 3488 write_log(S_ERROR,"local space is not enough!","",index); 3489 if(item == NULL) 3490 { 3491 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3492 } 3493 3494 } 3495 free(localpath); 3496 } 3497 else if(newer_file_ret == 0 && ftp_config.multrule[index]->rules != 1) 3498 { 3499 if(wait_handle_socket(index)) 3500 { 3501 return HAVE_LOCAL_SOCKET; 3502 } 3503 if(!flag) 3504 { 3505 char *temp = change_server_same_name(localfiletmp->path,index); 3506 my_rename_(localfiletmp->path,temp,index); 3507 char *err_msg = write_error_message("server file %s is renamed to %s",localfiletmp->path,temp); 3508 write_conflict_log(localfiletmp->path,3,err_msg); //conflict 3509 free(err_msg); 3510 //free(temp); 3511 } 3512 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->server_action_list); //?? 3513 status = upload(localfiletmp->path,index); 3514 if(status == 0) 3515 { 3516 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 3517 mod_time *onefiletime = Getmodtime(serverpath,index); 3518 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 3519 { 3520 DEBUG("ChangeFile_modtime failed!\n"); 3521 } 3522 free(onefiletime); 3523 free(serverpath); 3524 } 3525 else 3526 { 3527 return status; 3528 } 3529 } 3530 } 3531 } 3532 return status; 3533} 3534 3535int sync_server_to_local_perform(Browse *perform_br,Local *perform_lo,int index) 3536{ 3537 if(perform_br == NULL || perform_lo == NULL) 3538 { 3539 return 0; 3540 } 3541 3542 CloudFile *foldertmp = NULL; 3543 CloudFile *filetmp = NULL; 3544 LocalFolder *localfoldertmp; 3545 LocalFile *localfiletmp; 3546 int ret = 0; 3547 //int wait_ret = 0; 3548 3549 if(perform_br->foldernumber > 0) 3550 foldertmp = perform_br->folderlist->next; 3551 if(perform_br->filenumber > 0) 3552 filetmp = perform_br->filelist->next; 3553 3554 localfoldertmp = perform_lo->folderlist->next; 3555 localfiletmp = perform_lo->filelist->next; 3556 3557 /****************handle files****************/ 3558 //DEBUG("##########handle files\n"); 3559 if(perform_br->filenumber == 0 && perform_lo->filenumber != 0) 3560 { 3561 DEBUG("serverfileNo:%d\t\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 3562 3563 while(localfiletmp != NULL && exit_loop == 0) 3564 { 3565 if(ftp_config.multrule[index]->rules == 1) 3566 { 3567 action_item *item; 3568 item = get_action_item("download_only",localfiletmp->path, 3569 g_pSyncList[index]->download_only_socket_head,index); 3570 if(item != NULL) 3571 { 3572 localfiletmp = localfiletmp->next; 3573 continue; 3574 } 3575 } 3576 if(wait_handle_socket(index)) 3577 { 3578 return HAVE_LOCAL_SOCKET; 3579 } 3580 add_action_item("remove",localfiletmp->path,g_pSyncList[index]->server_action_list); 3581 3582 action_item *pp; 3583 pp = get_action_item("upload",localfiletmp->path, 3584 g_pSyncList[index]->up_space_not_enough_list,index); 3585 if(pp == NULL) 3586 { 3587 unlink(localfiletmp->path); 3588 } 3589 localfiletmp = localfiletmp->next; 3590 } 3591 } 3592 else if(perform_br->filenumber != 0 && perform_lo->filenumber == 0) 3593 { 3594 DEBUG("serverfileNo:%d\t\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 3595 3596 while(filetmp != NULL && exit_loop == 0) 3597 { 3598 if(wait_handle_socket(index)) 3599 { 3600 return HAVE_LOCAL_SOCKET; 3601 } 3602 char *localpath = serverpath_to_localpath(filetmp->href,index); 3603 3604 action_item *item; 3605 item = get_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list,index); 3606 3607 int cp = 0; 3608 do{ 3609 if(exit_loop == 1) 3610 return -1; 3611 cp = is_ftp_file_copying(filetmp->href,index); 3612 }while(cp == 1); 3613 3614 usleep(100*100); 3615 if(is_local_space_enough(filetmp,index)) 3616 { 3617 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 3618 3619 int status = download_(filetmp->href,index); 3620// if(status == 0) 3621// { 3622// DEBUG("do_cmd ok\n"); 3623// mod_time *onefiletime = Getmodtime(filetmp->href,index); 3624// if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 3625// { 3626// DEBUG("ChangeFile_modtime failed!\n"); 3627// } 3628// free(onefiletime); 3629// } 3630// else 3631// { 3632 //DEBUG("do_cmd failed,try again~~\n"); 3633 //status = download_(filetmp->href,index); 3634 if(status == 0) 3635 { 3636 DEBUG("do_cmd ok\n"); 3637 mod_time *onefiletime = Getmodtime(filetmp->href,index); 3638 if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 3639 { 3640 DEBUG("ChangeFile_modtime failed!\n"); 3641 } 3642 free(onefiletime); 3643 if(item != NULL) 3644 { 3645 del_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3646 } 3647 } 3648 //} 3649 } 3650 else 3651 { 3652 write_log(S_ERROR,"local space is not enough!","",index); 3653 if(item == NULL) 3654 { 3655 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3656 } 3657 } 3658 free(localpath); 3659 filetmp = filetmp->next; 3660 } 3661 } 3662 else if(perform_br->filenumber != 0 && perform_lo->filenumber != 0) 3663 { 3664 DEBUG("serverfileNo:%d\t\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 3665 DEBUG("Ergodic ftp file while\n"); 3666 while(localfiletmp != NULL && exit_loop == 0) 3667 { 3668 int cmp = 1; 3669 char *localpathtmp = strstr(localfiletmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 3670 while(filetmp != NULL) 3671 { 3672 char *serverpathtmp = filetmp->href; 3673 serverpathtmp = serverpathtmp + strlen(ftp_config.multrule[index]->rooturl); 3674 DEBUG("%s\t%s\n",localpathtmp,serverpathtmp); 3675 3676 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 3677 { 3678 break; 3679 } 3680 else 3681 { 3682 filetmp = filetmp->next; 3683 } 3684 } 3685 if (cmp != 0) 3686 { 3687 DEBUG("cmp:%d\n",cmp); 3688 3689 if(wait_handle_socket(index)) 3690 { 3691 return HAVE_LOCAL_SOCKET; 3692 } 3693 if(ftp_config.multrule[index]->rules == 1) 3694 { 3695 action_item *item; 3696 item = get_action_item("download_only",localfiletmp->path, 3697 g_pSyncList[index]->download_only_socket_head,index); 3698 if(item != NULL) 3699 { 3700 DEBUG("item != NULL\n"); 3701 filetmp = perform_br->filelist->next; 3702 localfiletmp = localfiletmp->next; 3703 continue; 3704 } 3705 } 3706 action_item *pp; 3707 pp = get_action_item("upload",localfiletmp->path, 3708 g_pSyncList[index]->up_space_not_enough_list,index); 3709 if(pp == NULL) 3710 { 3711 unlink(localfiletmp->path); 3712 add_action_item("remove",localfiletmp->path,g_pSyncList[index]->server_action_list); 3713 } 3714 } 3715 else 3716 { 3717 if((ret = the_same_name_compare(localfiletmp,filetmp,index,1)) != 0) 3718 { 3719 return ret; 3720 } 3721 } 3722 filetmp = perform_br->filelist->next; 3723 localfiletmp = localfiletmp->next; 3724 } 3725 3726 filetmp = perform_br->filelist->next; 3727 localfiletmp = perform_lo->filelist->next; 3728 3729 DEBUG("Ergodic local file while\n"); 3730 while(filetmp != NULL && exit_loop == 0) 3731 { 3732 char *serverpathtmp = filetmp->href; 3733 serverpathtmp = serverpathtmp + strlen(ftp_config.multrule[index]->rooturl); 3734 int cmp = 1; 3735 while(localfiletmp != NULL) 3736 { 3737 char *localpathtmp = strstr(localfiletmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 3738 DEBUG("%s\t%s\n",serverpathtmp,localpathtmp); 3739 3740 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 3741 { 3742 break; 3743 } 3744 else 3745 { 3746 localfiletmp = localfiletmp->next; 3747 } 3748 } 3749 if (cmp != 0) 3750 { 3751 DEBUG("cmp:%d\n),cmp"); 3752 3753 if(wait_handle_socket(index)) 3754 { 3755 return HAVE_LOCAL_SOCKET; 3756 } 3757 char *localpath = serverpath_to_localpath(filetmp->href,index); 3758 action_item *item; 3759 item = get_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list,index); 3760 3761 int cp = 0; 3762 do{ 3763 if(exit_loop == 1) 3764 return -1; 3765 cp = is_ftp_file_copying(filetmp->href,index); 3766 }while(cp == 1); 3767 3768 if(is_local_space_enough(filetmp,index)) 3769 { 3770 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 3771 3772 int status = download_(filetmp->href,index); 3773 if(status == 0) 3774 { 3775 DEBUG("do_cmd ok\n"); 3776 mod_time *onefiletime = Getmodtime(filetmp->href,index); 3777 if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 3778 { 3779 DEBUG("ChangeFile_modtime failed!\n"); 3780 } 3781 free(onefiletime); 3782 if(item != NULL) 3783 { 3784 del_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3785 } 3786 } 3787 } 3788 else 3789 { 3790 write_log(S_ERROR,"local space is not enough!","",index); 3791 if(item == NULL) 3792 { 3793 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 3794 } 3795 } 3796 free(localpath); 3797 } 3798 filetmp = filetmp->next; 3799 localfiletmp = perform_lo->filelist->next; 3800 } 3801 } 3802 3803 /*************handle folders**************/ 3804 if(perform_br->foldernumber == 0 && perform_lo->foldernumber != 0) 3805 { 3806 DEBUG("serverfolderNo:%d\t\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 3807 while(localfoldertmp != NULL && exit_loop == 0) 3808 { 3809 if(ftp_config.multrule[index]->rules == 1) 3810 { 3811 action_item *item; 3812 item = get_action_item("download_only", 3813 localfoldertmp->path,g_pSyncList[index]->download_only_socket_head,index); 3814 if(item != NULL) 3815 { 3816 localfoldertmp = localfoldertmp->next; 3817 continue; 3818 } 3819 } 3820 if(wait_handle_socket(index)) 3821 { 3822 return HAVE_LOCAL_SOCKET; 3823 } 3824 del_all_items(localfoldertmp->path,index); 3825 localfoldertmp = localfoldertmp->next; 3826 } 3827 } 3828 else if(perform_br->foldernumber != 0 && perform_lo->foldernumber == 0) 3829 { 3830 DEBUG("serverfolderNo:%d\t\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 3831 while(foldertmp != NULL && exit_loop == 0) 3832 { 3833 if(wait_handle_socket(index)) 3834 { 3835 return HAVE_LOCAL_SOCKET; 3836 } 3837 char *localpath = (char *)malloc(sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(foldertmp->href) + 1)); 3838 memset(localpath,'\0',sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(foldertmp->href) + 1)); 3839 char *p = foldertmp->href; 3840 p = p + strlen(ftp_config.multrule[index]->rooturl); 3841 sprintf(localpath,"%s%s",ftp_config.multrule[index]->base_path,p); 3842 DEBUG("%s\n",localpath); 3843 3844 char *prePath = get_prepath(localpath,strlen(localpath)); 3845 int exist = is_server_exist(prePath,localpath,index); 3846 if(exist) 3847 { 3848 if(NULL == opendir(localpath)) 3849 { 3850 add_action_item("createfolder",localpath,g_pSyncList[index]->server_action_list); 3851 mkdir(localpath,0777); 3852 } 3853 } 3854 free(prePath); 3855 free(localpath); 3856 foldertmp = foldertmp->next; 3857 } 3858 } 3859 else if(perform_br->foldernumber != 0 && perform_lo->foldernumber != 0) 3860 { 3861 DEBUG("serverfolderNo:%d\t\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 3862 while(localfoldertmp != NULL && exit_loop == 0) 3863 { 3864 int cmp = 1; 3865 char *localpathtmp = strstr(localfoldertmp->path,ftp_config.multrule[index]->base_path) 3866 + ftp_config.multrule[index]->base_path_len; 3867 while(foldertmp != NULL) 3868 { 3869 char *serverpathtmp = foldertmp->href; 3870 serverpathtmp = serverpathtmp + strlen(ftp_config.multrule[index]->rooturl); 3871 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 3872 { 3873 break; 3874 } 3875 else 3876 { 3877 foldertmp = foldertmp->next; 3878 } 3879 } 3880 if (cmp != 0) 3881 { 3882 if(ftp_config.multrule[index]->rules == 1) 3883 { 3884 action_item *item; 3885 item = get_action_item("download_only", 3886 localfoldertmp->path,g_pSyncList[index]->download_only_socket_head,index); 3887 if(item != NULL) 3888 { 3889 foldertmp = perform_br->folderlist->next; 3890 localfoldertmp = localfoldertmp->next; 3891 continue; 3892 } 3893 } 3894 if(wait_handle_socket(index)) 3895 { 3896 return HAVE_LOCAL_SOCKET; 3897 } 3898 del_all_items(localfoldertmp->path,index); 3899 } 3900 foldertmp = perform_br->folderlist->next; 3901 localfoldertmp = localfoldertmp->next; 3902 } 3903 3904 foldertmp = perform_br->folderlist->next; 3905 localfoldertmp = perform_lo->folderlist->next; 3906 while(foldertmp != NULL && exit_loop == 0) 3907 { 3908 int cmp = 1; 3909 char *serverpathtmp = foldertmp->href; 3910 serverpathtmp = serverpathtmp + strlen(ftp_config.multrule[index]->rooturl); 3911 while(localfoldertmp != NULL) 3912 { 3913 char *localpathtmp = strstr(localfoldertmp->path,ftp_config.multrule[index]->base_path) 3914 + ftp_config.multrule[index]->base_path_len; 3915 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 3916 { 3917 break; 3918 } 3919 else 3920 { 3921 localfoldertmp = localfoldertmp->next; 3922 } 3923 } 3924 if (cmp != 0) 3925 { 3926 if(wait_handle_socket(index)) 3927 { 3928 return HAVE_LOCAL_SOCKET; 3929 } 3930 char *localpath = (char *)malloc(sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(foldertmp->href) + 1)); 3931 memset(localpath,'\0',sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(foldertmp->href) + 1)); 3932 sprintf(localpath,"%s%s",ftp_config.multrule[index]->base_path,serverpathtmp); 3933 DEBUG("%s\n",localpath); 3934 3935 char *prePath = get_prepath(localpath,strlen(localpath)); 3936 int exist = is_server_exist(prePath,localpath,index); 3937 if(exist) 3938 { 3939 if(NULL == opendir(localpath)) 3940 { 3941 add_action_item("createfolder",localpath,g_pSyncList[index]->server_action_list); 3942 mkdir(localpath,0777); 3943 } 3944 } 3945 free(prePath); 3946 free(localpath); 3947 } 3948 foldertmp = foldertmp->next; 3949 localfoldertmp = perform_lo->folderlist->next; 3950 } 3951 } 3952 return ret; 3953} 3954 3955int sync_server_to_local(Server_TreeNode *treenode,int (*sync_fuc)(Browse*,Local*,int),int index) 3956{ 3957 if(exit_loop) 3958 { 3959 return -1; 3960 } 3961 if(treenode->parenthref == NULL) 3962 { 3963 return 0; 3964 } 3965 Local *localnode; 3966 3967 int ret = 0; 3968 char *localpath = serverpath_to_localpath(treenode->parenthref,index); 3969 localnode = Find_Floor_Dir(localpath); 3970 3971 free(localpath); 3972 3973 if(NULL != localnode) 3974 { 3975 ret = sync_fuc(treenode->browse,localnode,index); 3976 if(ret == COULD_NOT_CONNECNT_TO_SERVER || ret == PERMISSION_DENIED || ret == CONNECNTION_TIMED_OUT 3977 || ret == COULD_NOT_READ_RESPONSE_BODY || ret == HAVE_LOCAL_SOCKET) 3978 { 3979 free_localfloor_node(localnode); 3980 return ret; 3981 } 3982 free_localfloor_node(localnode); 3983 } 3984 3985 if(treenode->Child != NULL) 3986 { 3987 ret = sync_server_to_local(treenode->Child,sync_fuc,index); 3988 if(ret != 0 && ret != SERVER_SPACE_NOT_ENOUGH 3989 && ret != LOCAL_FILE_LOST && ret != SERVER_FILE_DELETED) 3990 { 3991 return ret; 3992 } 3993 } 3994 if(treenode->NextBrother != NULL) 3995 { 3996 ret = sync_server_to_local(treenode->NextBrother,sync_fuc,index); 3997 if(ret != 0 && ret != SERVER_SPACE_NOT_ENOUGH 3998 && ret != LOCAL_FILE_LOST && ret != SERVER_FILE_DELETED) 3999 { 4000 return ret; 4001 } 4002 } 4003 4004 return ret; 4005} 4006 4007int compareLocalList(int index){ 4008 DEBUG("compareLocalList start!\n"); 4009 int ret = 0; 4010 4011 if(g_pSyncList[index]->ServerRootNode->Child != NULL) 4012 { 4013 ret = sync_server_to_local(g_pSyncList[index]->ServerRootNode->Child,sync_server_to_local_perform,index); 4014 } 4015 else 4016 { 4017 DEBUG("ServerRootNode->Child == NULL\n"); 4018 } 4019 4020 return ret; 4021} 4022 4023 4024/*ret = 0,server changed 4025 *ret = 1,server is no changed 4026*/ 4027int compareServerList(int index) 4028{ 4029 int ret; 4030 DEBUG("#########compareServerList\n"); 4031 4032 if(g_pSyncList[index]->ServerRootNode->Child != NULL && g_pSyncList[index]->OldServerRootNode->Child != NULL) 4033 { 4034 ret = isServerChanged(g_pSyncList[index]->ServerRootNode->Child,g_pSyncList[index]->OldServerRootNode->Child); 4035 return ret; 4036 } 4037 else if(g_pSyncList[index]->ServerRootNode->Child == NULL && g_pSyncList[index]->OldServerRootNode->Child == NULL) 4038 { 4039 ret = 1; 4040 return ret; 4041 } 4042 else 4043 { 4044 ret = 0; 4045 return ret; 4046 } 4047} 4048 4049void *SyncServer(void *argc) 4050{ 4051 DEBUG("SyncServer:%u\n",pthread_self()); 4052 struct timeval now; 4053 struct timespec outtime; 4054 int status; 4055 int i; 4056 4057 while (!exit_loop) 4058 { 4059#ifdef _PC 4060 //check_link_internet(); 4061#else 4062 check_link_internet(); 4063#endif 4064 for(i = 0; i < ftp_config.dir_num; i++) 4065 { 4066 //2014.10.30 by sherry Sockect_praser和Sync_server线程都卡着(处于在等待状态) 4067 //server_sync 和local_sync同时为1 4068// status = 0; 4069// server_sync = 1; //server sync starting 4070 4071// while (local_sync == 1 && exit_loop == 0) 4072// { 4073// usleep(1000*10); 4074// } 4075 4076 4077 status = 0; 4078 4079 while (local_sync == 1 && exit_loop == 0) 4080 { 4081 usleep(1000*10); 4082 } 4083 4084 server_sync = 1; //server sync starting 4085 4086 4087 if(exit_loop) 4088 break; 4089 4090 if(disk_change) 4091 { 4092 //disk_change = 0; 4093 check_disk_change(); 4094 } 4095 4096 status = usr_auth(ftp_config.multrule[i]->server_ip, ftp_config.multrule[i]->user_pwd); 4097 if(status == 7) 4098 { 4099 write_log(S_ERROR,"check your ip.","",ftp_config.multrule[i]->rules); 4100 continue; 4101 } 4102 else if(status == 67) 4103 { 4104 write_log(S_ERROR,"check your usrname or password.","",ftp_config.multrule[i]->rules); 4105 continue; 4106 } 4107 else if(status == 28) 4108 { 4109 write_log(S_ERROR,"connect timeout,check your ip or network.","",ftp_config.multrule[i]->rules); 4110 continue; 4111 } 4112 int flag = is_server_exist(ftp_config.multrule[i]->hardware, ftp_config.multrule[i]->rooturl, i); 4113 if(flag == CURLE_FTP_COULDNT_RETR_FILE) 4114 { 4115 write_log(S_ERROR,"Permission Denied.","",ftp_config.multrule[i]->rules); 4116 continue; 4117 } 4118 else if(flag == 0) 4119 { 4120 //write_log(S_ERROR,"remote root path lost OR write only.","",ftp_config.multrule[i]->rules); 4121 //continue; 4122 } 4123 4124 if(g_pSyncList[i]->sync_disk_exist == 0) 4125 { 4126 write_log(S_ERROR,"Sync disk unplug!","",i); 4127 continue; 4128 } 4129 4130 if(g_pSyncList[i]->no_local_root) 4131 { 4132 my_local_mkdir(ftp_config.multrule[i]->base_path); 4133 send_action(ftp_config.id,ftp_config.multrule[i]->base_path,INOTIFY_PORT); 4134 usleep(1000*10); 4135 g_pSyncList[i]->no_local_root = 0; 4136 g_pSyncList[i]->init_completed = 0; 4137 } 4138 4139 status = do_unfinished(i); 4140 4141 if(status != 0 && status != -1) 4142 { 4143 server_sync = 0; //server sync finished 4144 //sleep(2); 4145 usleep(1000*200); 4146 break; 4147 } 4148 4149 if(g_pSyncList[i]->init_completed == 0) 4150 { 4151 status = initialization(); 4152 if(status) 4153 continue; 4154 } 4155 4156 if(ftp_config.multrule[i]->rules == 2) 4157 { 4158 continue; 4159 } 4160 if(exit_loop == 0) 4161 { 4162 DEBUG("get ServerRootNode\n"); 4163 g_pSyncList[i]->ServerRootNode = create_server_treeroot(); 4164 status = browse_to_tree(ftp_config.multrule[i]->rooturl,g_pSyncList[i]->ServerRootNode,i); 4165 if (status != 0) 4166 { 4167 DEBUG("get ServerList ERROR! \n"); 4168 g_pSyncList[i]->first_sync = 1; 4169 usleep(1000*20); 4170 continue; 4171 } 4172 4173 if(g_pSyncList[i]->unfinished_list->next != NULL) 4174 { 4175 continue; 4176 } 4177 4178 if(g_pSyncList[i]->first_sync) 4179 { 4180 DEBUG("first sync!\n"); 4181 g_pSyncList[i]->first_sync = 0; 4182 g_pSyncList[i]->OrigServerRootNode = g_pSyncList[i]->OldServerRootNode; 4183 //free_server_tree(g_pSyncList[i]->OldServerRootNode); 4184 g_pSyncList[i]->OldServerRootNode = g_pSyncList[i]->ServerRootNode; 4185 status = compareLocalList(i); 4186 free_server_tree(g_pSyncList[i]->OrigServerRootNode); 4187 if(status == 0) 4188 g_pSyncList[i]->first_sync = 0; 4189 else 4190 g_pSyncList[i]->first_sync = 1; 4191 } 4192 else 4193 { 4194 if(ftp_config.multrule[i]->rules == 0) 4195 { 4196 status = compareServerList(i); 4197 } 4198 if(status == 0 || ftp_config.multrule[i]->rules == 1) 4199 { 4200 g_pSyncList[i]->OrigServerRootNode = g_pSyncList[i]->OldServerRootNode; 4201 //free_server_tree(g_pSyncList[i]->OldServerRootNode); 4202 g_pSyncList[i]->OldServerRootNode = g_pSyncList[i]->ServerRootNode; 4203 status = compareLocalList(i); 4204 free_server_tree(g_pSyncList[i]->OrigServerRootNode); 4205 if(status == 0) 4206 g_pSyncList[i]->first_sync = 0; 4207 else 4208 g_pSyncList[i]->first_sync = 1; 4209 } 4210 else 4211 { 4212 free_server_tree(g_pSyncList[i]->ServerRootNode); 4213 g_pSyncList[i]->ServerRootNode = NULL; 4214 } 4215 } 4216 } 4217 write_log(S_SYNC,"","",i); 4218 } 4219 server_sync = 0; //server_sync finished! 4220 pthread_mutex_lock(&mutex); 4221 if(!exit_loop) 4222 { 4223 gettimeofday(&now, NULL); 4224 outtime.tv_sec = now.tv_sec + 5; 4225 outtime.tv_nsec = now.tv_usec * 1000; 4226 pthread_cond_timedwait(&cond, &mutex, &outtime); 4227 } 4228 //DEBUG("server loop\n"); 4229 pthread_mutex_unlock(&mutex); 4230 } 4231 DEBUG("stop FtpClient server sync\n"); 4232 4233 stop_up = 1; 4234 4235} 4236 4237int cmd_parser(char *cmd,int index) 4238{ 4239 //对path,name,actionname进行解析 4240 if( strstr(cmd,"(conflict)") != NULL ) 4241 return 0; 4242 4243 DEBUG("socket command is %s \n",cmd); 4244 if( !strncmp(cmd,"exit",4)) 4245 { 4246 DEBUG("exit socket\n"); 4247 return 0; 4248 } 4249 4250 if(!strncmp(cmd,"rmroot",6)) 4251 { 4252 g_pSyncList[index]->no_local_root = 1; 4253 return 0; 4254 } 4255 4256 char cmd_name[64]; 4257 //char cmd_param[512]; 4258 char *path; 4259 char *temp; 4260 char filename[256]; 4261 char *fullname = NULL; 4262 char oldname[256],newname[256]; 4263 char *oldpath = NULL; 4264 char action[64]; 4265 char *cmp_name; 4266 char *mv_newpath; 4267 char *mv_oldpath; 4268 char *ch; 4269 int status; 4270 4271 //Asusconfig asusconfig; 4272 4273 memset(cmd_name, 0, sizeof(cmd_name)); 4274 memset(oldname,'\0',sizeof(oldname)); 4275 memset(newname,'\0',sizeof(newname)); 4276 memset(action,0,sizeof(action)); 4277 4278 ch = cmd; 4279 int i = 0; 4280 //while(*ch != '@') 4281 while(*ch != '\n') 4282 { 4283 i++; 4284 ch++; 4285 } 4286 4287 memcpy(cmd_name, cmd, i); 4288 4289 char *p = NULL; 4290 ch++; 4291 i++; 4292 4293 temp = my_str_malloc((size_t)(strlen(ch)+1)); 4294 4295 strcpy(temp,ch); 4296 //p = strchr(temp,'@'); 4297 p = strchr(temp,'\n'); 4298 4299 path = my_str_malloc((size_t)(strlen(temp)- strlen(p)+1)); 4300 4301 if(p!=NULL) 4302 snprintf(path,strlen(temp)- strlen(p)+1,"%s",temp); 4303 4304 //free(temp); 4305 4306 p++; 4307 if(strncmp(cmd_name, "rename",6) == 0)//是否是rename,是rename,strncmp(cmd_name, "rename",6)返回0,是走里面的 4308 { 4309 char *p1 = NULL; 4310 4311 //p1 = strchr(p,'@'); 4312 p1 = strchr(p,'\n'); 4313 4314 if(p1 != NULL) 4315 strncpy(oldname,p,strlen(p)- strlen(p1)); 4316 4317 p1++; 4318 4319 strcpy(newname,p1); 4320 4321 DEBUG("cmd_name: [%s],path: [%s],oldname: [%s],newname: [%s]\n",cmd_name,path,oldname,newname); 4322 if(newname[0] == '.' || (strstr(path,"/.")) != NULL) 4323 { 4324 free(temp); 4325 free(path); 4326 return 0; 4327 } 4328 } 4329 else if(strncmp(cmd_name, "move",4) == 0) 4330 { 4331 char *p1 = NULL; 4332 4333 //p1 = strchr(p,'@'); 4334 p1 = strchr(p,'\n'); 4335 4336 oldpath = my_str_malloc((size_t)(strlen(p)- strlen(p1)+1)); 4337 4338 if(p1 != NULL) 4339 snprintf(oldpath,strlen(p)- strlen(p1)+1,"%s",p); 4340 4341 p1++; 4342 4343 strcpy(oldname,p1); 4344 4345 DEBUG("cmd_name: [%s],path: [%s],oldpath: [%s],oldname: [%s]\n",cmd_name,path,oldpath,oldname); 4346 if(oldname[0] == '.' || (strstr(path,"/.")) != NULL) 4347 { 4348 free(temp); 4349 free(path); 4350 free(oldpath); 4351 return 0; 4352 } 4353 } 4354 else 4355 { 4356 strcpy(filename,p); 4357 fullname = my_str_malloc((size_t)(strlen(path)+strlen(filename)+2)); 4358 DEBUG("cmd_name: [%s],path: [%s],filename: [%s]\n",cmd_name,path,filename); 4359 if(filename[0] == '.' || (strstr(path,"/.")) != NULL) 4360 { 4361 free(temp); 4362 free(path); 4363 return 0; 4364 } 4365 } 4366 4367 free(temp); 4368 4369 if( !strncmp(cmd_name,"rename",6) )//是rename返回1,即:是rename走里面的内容 4370 { 4371 cmp_name = my_str_malloc((size_t)(strlen(path)+strlen(newname)+2)); 4372 sprintf(cmp_name,"%s/%s",path,newname); 4373 } 4374 else 4375 { 4376 cmp_name = my_str_malloc((size_t)(strlen(path)+strlen(filename)+2)); 4377 sprintf(cmp_name,"%s/%s",path,filename); 4378 } 4379 4380 if( strcmp(cmd_name, "createfile") == 0 ) 4381 { 4382 strcpy(action,"createfile"); 4383 del_action_item("copyfile",cmp_name,g_pSyncList[index]->copy_file_list); 4384 } 4385 else if( strcmp(cmd_name, "remove") == 0 || strcmp(cmd_name, "delete") == 0) 4386 { 4387 strcpy(action,"remove"); 4388 } 4389 else if( strcmp(cmd_name, "createfolder") == 0 ) 4390 { 4391 strcpy(action,"createfolder"); 4392 del_action_item("copyfile",cmp_name,g_pSyncList[index]->copy_file_list); 4393 } 4394 else if( strncmp(cmd_name, "rename",6) == 0 ) 4395 { 4396 strcpy(action,"rename"); 4397 } 4398 else if( strncmp(cmd_name, "move",4) == 0 ) 4399 { 4400 strcpy(action,"move"); 4401 } 4402 4403 if(g_pSyncList[index]->server_action_list->next != NULL) 4404 { 4405 action_item *item; 4406 4407 item = get_action_item(action,cmp_name,g_pSyncList[index]->server_action_list,index); 4408 4409 if(item != NULL) 4410 { 4411 DEBUG("##### %s %s by FTP Server self ######\n",action,cmp_name); 4412 del_action_item(action,cmp_name,g_pSyncList[index]->server_action_list); 4413 DEBUG("#### del action item success!\n"); 4414 free(path); 4415 free(fullname); 4416 free(cmp_name); 4417 return 0; 4418 } 4419 } 4420 4421 if(g_pSyncList[index]->dragfolder_action_list->next != NULL) 4422 { 4423 action_item *item; 4424 4425 item = get_action_item(action,cmp_name,g_pSyncList[index]->dragfolder_action_list,index); 4426 4427 if(item != NULL) 4428 { 4429 DEBUG("##### %s %s by dragfolder recursion self ######\n",action,cmp_name); 4430 del_action_item(action,cmp_name,g_pSyncList[index]->dragfolder_action_list); 4431 free(path); 4432 free(fullname); 4433 free(cmp_name); 4434 return 0; 4435 } 4436 } 4437 free(cmp_name); 4438//针对不同的操作进行,作出具体的反应 4439 DEBUG("###### %s is start ######\n",cmd_name); 4440 4441 if( strcmp(cmd_name, "copyfile") != 0 ) 4442 { 4443 g_pSyncList[index]->have_local_socket = 1; 4444 } 4445 4446 if( strcmp(cmd_name, "createfile") == 0 || strcmp(cmd_name, "dragfile") == 0 ) 4447 { 4448 sprintf(fullname,"%s/%s",path,filename); 4449 int exist = is_server_exist(path,fullname,index); 4450 //2014.11.7 by sherry 4451 //if(exist) 4452// if(exist==1) 4453// { 4454// char *temp = change_server_same_name(fullname,index);//名字要改成什么 4455// my_rename_(fullname,temp,index);//改名字 4456// free(temp); 4457// char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4458// write_conflict_log(fullname,3,err_msg); //conflict 4459// free(err_msg); 4460// } 4461 if(exist==1) 4462 { 4463// if((newer_file(fullname,index,1)) != 2 && (newer_file(fullname,index,1)) != -1) 4464// { 4465 char *temp = change_server_same_name(fullname,index);//名字要改成什么 4466 my_rename_(fullname,temp,index);//改名字 4467 char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4468 write_conflict_log(fullname,3,err_msg); //conflict 4469 free(err_msg); 4470 free(temp); 4471// } 4472 4473 } 4474 status = upload(fullname,index); 4475 if(status != 0) 4476 { 4477 DEBUG("upload %s failed\n",fullname); 4478 free(path); 4479 free(fullname); 4480 return status; 4481 } 4482 else 4483 { 4484 char *serverpath = localpath_to_serverpath(fullname,index); 4485 mod_time *onefiletime = Getmodtime(serverpath,index); 4486 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4487 { 4488 DEBUG("ChangeFile_modtime failed!\n"); 4489 } 4490 free(onefiletime); 4491 free(fullname); 4492 free(serverpath); 4493 } 4494 } 4495 else if( strcmp(cmd_name, "copyfile") == 0 ) 4496 { 4497 sprintf(fullname,"%s/%s",path,filename); 4498 add_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 4499 free(fullname); 4500 } 4501 else if( strncmp(cmd_name, "cancelcopy", 10) == 0) 4502 { 4503 sprintf(fullname,"%s/%s",path,filename); 4504 del_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 4505 free(fullname); 4506 } 4507 else if( strcmp(cmd_name, "modify") == 0 ) 4508 { 4509 sprintf(fullname,"%s/%s",path,filename); 4510 char *serverpath = localpath_to_serverpath(fullname,index); 4511 int exist; 4512 DEBUG("%s %s\n",path,fullname); 4513 exist = is_server_exist(path,fullname,index); 4514 if(!exist) //Server has no the same file 4515 { 4516 status = upload(fullname,index); 4517 if(status != 0) 4518 { 4519 DEBUG("upload %s failed\n",fullname); 4520 free(path); 4521 free(fullname); 4522 return status; 4523 } 4524 else 4525 { 4526 mod_time *onefiletime; 4527 onefiletime = Getmodtime(serverpath,index); 4528 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4529 { 4530 DEBUG("ChangeFile_modtime failed!\n"); 4531 } 4532 free(onefiletime); 4533 free(fullname); 4534 free(serverpath); 4535 } 4536 } 4537 else 4538 { 4539 //mod_time *onefiletime; 4540 //onefiletime = Getmodtime(serverpath,index); 4541 CloudFile *cloud_file = NULL; 4542 if(g_pSyncList[index]->init_completed) 4543 cloud_file = get_CloudFile_node(g_pSyncList[index]->OldServerRootNode,serverpath,0x2); 4544 else 4545 cloud_file = get_CloudFile_node(g_pSyncList[index]->ServerRootNode,serverpath,0x2); 4546 if(ftp_config.multrule[index]->rules == 2) 4547 { 4548 if(cloud_file == NULL) 4549 { 4550 char *temp = change_server_same_name(fullname,index); 4551 my_rename_(fullname,temp,index); 4552 //free(temp); 4553 status = upload(fullname,index); 4554 if(status != 0) 4555 { 4556 DEBUG("modify failed status = %d\n",status); 4557 free(path); 4558 return status; 4559 } 4560 else 4561 { 4562 4563 char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4564 write_conflict_log(fullname,3,err_msg); //conflict 4565 free(err_msg); 4566 DEBUG("serverpath = %s\n",serverpath); 4567 mod_time *onefiletime = Getmodtime(serverpath,index); 4568 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4569 { 4570 DEBUG("ChangeFile_modtime failed!\n"); 4571 } 4572 free(onefiletime); 4573 free(fullname); 4574 free(serverpath); 4575 } 4576 } 4577 else 4578 { 4579 if(cloud_file->ismodify) 4580 { 4581 char *temp = change_server_same_name(fullname,index); 4582 my_rename_(fullname,temp,index); 4583 free(temp); 4584 status = upload(fullname,index); 4585 cloud_file->ismodify = 0; 4586 if(status != 0) 4587 { 4588 DEBUG("modify failed status = %d\n",status); 4589 free(path); 4590 return status; 4591 } 4592 else 4593 { 4594 char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4595 write_conflict_log(fullname,3,err_msg); //conflict 4596 free(err_msg); 4597 DEBUG("serverpath = %s\n",serverpath); 4598 mod_time *onefiletime = Getmodtime(serverpath,index); 4599 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4600 { 4601 DEBUG("ChangeFile_modtime failed!\n"); 4602 } 4603 free(onefiletime); 4604 free(fullname); 4605 free(serverpath); 4606 } 4607 } 4608 else 4609 { 4610 mod_time *modtime1 = NULL; 4611 time_t modtime2; 4612 modtime1 = Getmodtime(serverpath,index); 4613 modtime2 = cloud_file->modtime; 4614 if(modtime1->modtime != modtime2) 4615 { 4616 char *temp = change_server_same_name(fullname,index); 4617 my_rename_(fullname,temp,index); 4618 free(temp); 4619 status = upload(fullname,index); 4620 free(modtime1); 4621 if(status != 0) 4622 { 4623 DEBUG("modify failed status = %d\n",status); 4624 free(path); 4625 return status; 4626 } 4627 else 4628 { 4629 char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4630 write_conflict_log(fullname,3,err_msg); //conflict 4631 free(err_msg); 4632 DEBUG("serverpath = %s\n",serverpath); 4633 mod_time *onefiletime = Getmodtime(serverpath,index); 4634 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4635 { 4636 DEBUG("ChangeFile_modtime failed!\n"); 4637 } 4638 free(onefiletime); 4639 free(fullname); 4640 free(serverpath); 4641 } 4642 } 4643 else 4644 { 4645 status = upload(fullname,index); 4646 if(status != 0) 4647 { 4648 DEBUG("modify failed status = %d\n",status); 4649 free(path); 4650 return status; 4651 } 4652 else 4653 { 4654 DEBUG("serverpath = %s\n",serverpath); 4655 mod_time *onefiletime; 4656 onefiletime = Getmodtime(serverpath,index); 4657 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4658 { 4659 DEBUG("ChangeFile_modtime failed!\n"); 4660 } 4661 free(onefiletime); 4662 free(fullname); 4663 free(serverpath); 4664 } 4665 } 4666 } 4667 } 4668 } 4669 else 4670 { 4671 mod_time *modtime1; 4672 time_t modtime2; 4673 modtime1 = Getmodtime(serverpath,index); 4674 if(cloud_file == NULL) 4675 { 4676 modtime2 = (time_t)-1; 4677 } 4678 else 4679 { 4680 modtime2 = cloud_file->modtime; 4681 } 4682 if(modtime1->modtime == modtime2) 4683 { 4684 status = upload(fullname,index); 4685 free(modtime1); 4686 if(status != 0) 4687 { 4688 DEBUG("modify failed status = %d\n",status); 4689 free(path); 4690 return status; 4691 } 4692 else 4693 { 4694 DEBUG("serverpath = %s\n",serverpath); 4695 mod_time *onefiletime = Getmodtime(serverpath,index); 4696 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4697 { 4698 DEBUG("ChangeFile_modtime failed!\n"); 4699 } 4700 free(onefiletime); 4701 free(fullname); 4702 free(serverpath); 4703 } 4704 } 4705 else 4706 { 4707 char *temp = change_server_same_name(fullname,index); 4708 my_rename_(fullname,temp,index); 4709 //free(temp); 4710 status = upload(fullname,index); 4711 if(status != 0) 4712 { 4713 DEBUG("modify failed status = %d\n",status); 4714 free(path); 4715 return status; 4716 } 4717 else 4718 { 4719 char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4720 write_conflict_log(fullname,3,err_msg); //conflict 4721 free(err_msg); 4722 DEBUG("serverpath = %s\n",serverpath); 4723 mod_time *onefiletime = Getmodtime(serverpath,index); 4724 if(ChangeFile_modtime(fullname,onefiletime->modtime,index)) 4725 { 4726 DEBUG("ChangeFile_modtime failed!\n"); 4727 } 4728 free(onefiletime); 4729 free(fullname); 4730 free(serverpath); 4731 } 4732 } 4733 } 4734 } 4735 } 4736 else if(strcmp(cmd_name, "delete") == 0 || strcmp(cmd_name, "remove") == 0) 4737 { 4738 sprintf(fullname,"%s/%s",path,filename); 4739 status = Delete(fullname,index); 4740 free(fullname); 4741 free(path); 4742 return status; 4743 } 4744 else if(strncmp(cmd_name, "move",4) == 0 || strncmp(cmd_name, "rename",6) == 0) 4745 { 4746 if(strncmp(cmd_name, "move",4) == 0) 4747 { 4748 4749 mv_newpath = my_str_malloc((size_t)(strlen(path)+strlen(oldname)+2)); 4750 mv_oldpath = my_str_malloc((size_t)(strlen(oldpath)+strlen(oldname)+2)); 4751 sprintf(mv_newpath,"%s/%s",path,oldname); 4752 sprintf(mv_oldpath,"%s/%s",oldpath,oldname); 4753 free(oldpath); 4754 } 4755 else 4756 { 4757 mv_newpath = my_str_malloc((size_t)(strlen(path)+strlen(newname)+2)); 4758 mv_oldpath = my_str_malloc((size_t)(strlen(path)+strlen(oldname)+2)); 4759 sprintf(mv_newpath,"%s/%s",path,newname); 4760 sprintf(mv_oldpath,"%s/%s",path,oldname); 4761 } 4762 if(strncmp(cmd_name,"rename",6) == 0) 4763 { 4764 4765 int exist = is_server_exist(path,mv_newpath,index); 4766 if(!exist) 4767 { 4768 //status = 0; 4769 status = my_rename_(mv_oldpath,newname,index); 4770 } 4771// else 4772// { 4773// char *temp = change_server_same_name(mv_newpath,index); 4774// my_rename_(mv_newpath,temp,index); 4775// //free(temp); 4776// char *err_msg = write_error_message("server file %s is renamed to %s",fullname,temp); 4777// write_conflict_log(fullname,3,err_msg); //conflict 4778// free(err_msg); 4779// status = my_rename_(mv_oldpath,newname,index); 4780// } 4781 } 4782 else //move 4783 { 4784 int exist = 0; 4785 int old_index; 4786 old_index = get_path_to_index(mv_oldpath); 4787// if(ftp_config.multrule[old_index]->rules == 1) 4788// { 4789// del_download_only_action_item("move",mv_oldpath,g_pSyncList[old_index]->download_only_socket_head); 4790// } 4791// 4792// if(test_if_dir(mv_newpath)) 4793// { 4794// exist = is_server_exist(path,mv_newpath,index); 4795// if(exist) 4796// { 4797// char *newname = change_server_same_name(mv_newpath,index); 4798// status = my_rename_(mv_newpath,newname,index); 4799// //free(newname); 4800// status = moveFolder(mv_oldpath,mv_newpath,index); 4801// } 4802// else 4803// { 4804// status = moveFolder(mv_oldpath,mv_newpath,index); 4805// } 4806// } 4807// else 4808// { 4809// Delete(mv_oldpath,index); 4810// exist = is_server_exist(path,mv_newpath,index); 4811// if(exist) 4812// { 4813// char *newname = change_server_same_name(mv_newpath,index); 4814// status = my_rename_(mv_newpath,newname,index); 4815// } 4816// status = upload(mv_newpath,index); 4817// if(status == 0) 4818// { 4819// char *serverpath = localpath_to_serverpath(mv_newpath,index); 4820// mod_time *onefiletime = Getmodtime(serverpath,index); 4821// free(serverpath); 4822// if(ChangeFile_modtime(mv_newpath,onefiletime->modtime,index)) 4823// { 4824// DEBUG("ChangeFile_modtime failed!\n"); 4825// } 4826// free(onefiletime); 4827// } 4828// } 4829 if(index == old_index) 4830 { 4831 if(test_if_dir(mv_newpath)) 4832 { 4833 exist = is_server_exist(path,mv_newpath,index); 4834 4835 if(exist) 4836 { 4837 char *newname; 4838 newname = change_server_same_name(mv_newpath,index); 4839 status = my_rename_(mv_newpath,newname,index); 4840 char *err_msg = write_error_message("server file %s is renamed to %s",mv_newpath,newname); 4841 write_conflict_log(mv_newpath,3,err_msg); //conflict 4842 free(err_msg); 4843 4844 free(newname); 4845 if(status == 0) 4846 { 4847 status = moveFolder(mv_oldpath,mv_newpath,index); 4848 } 4849 } 4850 else 4851 { 4852 status = moveFolder(mv_oldpath,mv_newpath,index); 4853 } 4854 } 4855 else 4856 { 4857 4858 Delete(mv_oldpath,index); 4859 exist = is_server_exist(path,mv_newpath,index); 4860 if(exist) 4861 { 4862 char *newname; 4863 newname = change_server_same_name(mv_newpath,index); 4864 status = my_rename_(mv_newpath,newname,index); 4865 char *err_msg = write_error_message("server file %s is renamed to %s",mv_newpath,newname); 4866 write_conflict_log(mv_newpath,3,err_msg); //conflict 4867 free(err_msg); 4868 4869 free(newname); 4870 if(status == 0) 4871 { 4872 status = upload(mv_newpath,index); 4873 if(status != 0) 4874 { 4875 DEBUG("move %S to %s failed\n",mv_oldpath,mv_newpath); 4876 free(path); 4877 free(mv_oldpath); 4878 free(mv_newpath); 4879 return status; 4880 } 4881 else 4882 { 4883 char *serverpath = localpath_to_serverpath(mv_newpath,index); 4884 mod_time *onefiletime = Getmodtime(serverpath,index); 4885 free(serverpath); 4886 if(ChangeFile_modtime(mv_newpath,onefiletime->modtime,index)) 4887 { 4888 DEBUG("ChangeFile_modtime failed!\n"); 4889 } 4890 free(onefiletime); 4891 } 4892 } 4893 4894 } 4895 else 4896 { 4897 status = upload(mv_newpath,index); 4898 if(status != 0) 4899 { 4900 DEBUG("move %S to %s failed\n",mv_oldpath,mv_newpath); 4901 free(path); 4902 free(mv_oldpath); 4903 free(mv_newpath); 4904 return status; 4905 } 4906 else 4907 { 4908 char *serverpath = localpath_to_serverpath(mv_newpath,index); 4909 mod_time *onefiletime = Getmodtime(serverpath,index); 4910 free(serverpath); 4911 if(ChangeFile_modtime(mv_newpath,onefiletime->modtime,index)) 4912 { 4913 DEBUG("ChangeFile_modtime failed!\n"); 4914 } 4915 free(onefiletime); 4916 } 4917 } 4918 4919 } 4920 } 4921 else 4922 { 4923 if(ftp_config.multrule[old_index]->rules == 1) 4924 { 4925 del_download_only_action_item("move",mv_oldpath,g_pSyncList[old_index]->download_only_socket_head); 4926 } 4927 else 4928 { 4929 Delete(mv_oldpath,old_index); 4930 } 4931 4932 if(test_if_dir(mv_newpath)) 4933 { 4934 status = moveFolder(mv_oldpath,mv_newpath,index); 4935 if(status != 0) 4936 { 4937 DEBUG("createFolder failed status = %d\n",status); 4938 free(path); 4939 free(mv_oldpath); 4940 free(mv_newpath); 4941 return status; 4942 } 4943 4944 } 4945 else 4946 { 4947 status = upload(mv_newpath,index); 4948 4949 if(status != 0) 4950 { 4951 DEBUG("move %S to %s failed\n",mv_oldpath,mv_newpath); 4952 free(path); 4953 free(mv_oldpath); 4954 free(mv_newpath); 4955 return status; 4956 } 4957 else 4958 { 4959 char *serverpath = localpath_to_serverpath(mv_newpath,index); 4960 mod_time *onefiletime = Getmodtime(serverpath,index); 4961 free(serverpath); 4962 if(ChangeFile_modtime(mv_newpath,onefiletime->modtime,index)) 4963 { 4964 DEBUG("ChangeFile_modtime failed!\n"); 4965 } 4966 free(onefiletime); 4967 } 4968 } 4969 } 4970 } 4971 4972 free(mv_oldpath); 4973 free(mv_newpath); 4974 4975 if(status != 0) 4976 { 4977 DEBUG("move/rename failed\n"); 4978 free(path); 4979 return status; 4980 } 4981 } 4982 4983 else if(strcmp(cmd_name, "dragfolder") == 0) 4984 { 4985 sprintf(fullname,"%s/%s",path,filename); 4986 4987 char info[512]; 4988 memset(info,0,sizeof(info)); 4989 sprintf(info,"createfolder%s%s%s%s","\n",path,"\n",filename); 4990 pthread_mutex_lock(&mutex_socket); 4991 add_socket_item(info,index); 4992 pthread_mutex_unlock(&mutex_socket); 4993 status = deal_dragfolder_to_socketlist(fullname,index); 4994 free(fullname); 4995// int exist = is_server_exist(path,fullname,index); 4996// if(exist) 4997// { 4998// my_rename_(fullname,change_server_same_name(fullname,index),index); 4999// } 5000// status = createFolder(fullname,index); 5001// free(fullname); 5002// if(status != 0) 5003// { 5004// DEBUG("createFolder failed status = %d\n",status); 5005// free(path); 5006// return status; 5007// } 5008 } 5009 5010 else if(strcmp(cmd_name, "createfolder") == 0) 5011 { 5012 sprintf(fullname,"%s/%s",path,filename); 5013 int exist = is_server_exist(path,fullname,index); 5014 if(exist) 5015 { 5016 return 0; 5017 //char *temp = change_server_same_name(fullname,index); 5018 //my_rename_(fullname,temp,index); 5019 } 5020 status = my_mkdir_(fullname,index); 5021 free(fullname); 5022 if(status != 0) 5023 { 5024 free(path); 5025 return status; 5026 } 5027 } 5028 5029 free(path); 5030 return 0; 5031} 5032int download_only_add_socket_item(char *cmd,int index) 5033{ 5034 DEBUG("running download_only_add_socket_item: %s\n",cmd); 5035 5036 if( strstr(cmd,"(conflict)") != NULL ) 5037 return 0; 5038 5039 if( !strncmp(cmd,"exit",4)) 5040 { 5041 DEBUG("exit socket\n"); 5042 return 0; 5043 } 5044 5045 if(!strncmp(cmd,"rmroot",6)) 5046 { 5047 g_pSyncList[index]->no_local_root = 1; 5048 return 0; 5049 } 5050 5051 char cmd_name[64]; 5052 char *path = NULL; 5053 char *temp = NULL; 5054 char filename[256]; 5055 char *fullname = NULL; 5056 char oldname[256],newname[256]; 5057 char *oldpath = NULL; 5058 char action[64]; 5059 //char *cmp_name = NULL; 5060 //char *mv_newpath; 5061 //char *mv_oldpath; 5062 char *ch = NULL; 5063 char *old_fullname = NULL; 5064 //int status; 5065 5066 memset(cmd_name,'\0',sizeof(cmd_name)); 5067 memset(oldname,'\0',sizeof(oldname)); 5068 memset(newname,'\0',sizeof(newname)); 5069 memset(action,'\0',sizeof(action)); 5070 5071 ch = cmd; 5072 int i = 0; 5073 //while(*ch != '@') 5074 while(*ch != '\n') 5075 { 5076 i++; 5077 ch++; 5078 } 5079 5080 memcpy(cmd_name, cmd, i); 5081 5082 char *p = NULL; 5083 ch++; 5084 i++; 5085 temp = my_str_malloc((size_t)(strlen(ch)+1)); 5086 5087 strcpy(temp,ch); 5088 //p = strchr(temp,'@'); 5089 p = strchr(temp,'\n'); 5090 5091 //DEBUG("temp = %s\n",temp); 5092 //DEBUG("p = %s\n",p); 5093 //DEBUG("strlen(temp)- strlen(p) = %d\n",strlen(temp)- strlen(p)); 5094 5095 path = my_str_malloc((size_t)(strlen(temp)- strlen(p)+1)); 5096 5097 //DEBUG("path = %s\n",path); 5098 5099 if(p!=NULL) 5100 snprintf(path,strlen(temp)- strlen(p)+1,"%s",temp); 5101 5102 //free(temp); 5103 5104 p++; 5105 if(strncmp(cmd_name, "rename",6) == 0) 5106 { 5107 char *p1 = NULL; 5108 5109 //p1 = strchr(p,'@'); 5110 p1 = strchr(p,'\n'); 5111 5112 if(p1 != NULL) 5113 strncpy(oldname,p,strlen(p)- strlen(p1)); 5114 5115 p1++; 5116 5117 strcpy(newname,p1); 5118 } 5119 else if(strncmp(cmd_name, "move",4) == 0) 5120 { 5121 char *p1 = NULL; 5122 5123 //p1 = strchr(p,'@'); 5124 p1 = strchr(p,'\n'); 5125 5126 oldpath = my_str_malloc((size_t)(strlen(p)- strlen(p1)+1)); 5127 5128 if(p1 != NULL) 5129 snprintf(oldpath,strlen(p)- strlen(p1)+1,"%s",p); 5130 5131 p1++; 5132 5133 strcpy(oldname,p1); 5134 5135 DEBUG("cmd_name: [%s],path: [%s],oldpath: [%s],oldname: [%s]\n",cmd_name,path,oldpath,oldname); 5136 } 5137 else 5138 { 5139 strcpy(filename,p); 5140 //fullname = my_str_malloc((size_t)(strlen(path)+strlen(filename)+2)); 5141 DEBUG("cmd_name: [%s],path: [%s],filename: [%s]\n",cmd_name,path,filename); 5142 } 5143 5144 free(temp); 5145 5146 if( !strncmp(cmd_name,"rename",strlen("rename")) ) 5147 { 5148 fullname = my_str_malloc((size_t)(strlen(path) + strlen(newname) + 2)); 5149 old_fullname = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 2)); 5150 sprintf(fullname,"%s/%s",path,newname); 5151 sprintf(old_fullname,"%s/%s",path,oldname); 5152 free(path); 5153 } 5154 else if( !strncmp(cmd_name,"move",strlen("move")) ) 5155 { 5156 fullname = my_str_malloc((size_t)(strlen(path) + strlen(oldname) + 2)); 5157 old_fullname = my_str_malloc((size_t)(strlen(oldpath) + strlen(oldname) + 2)); 5158 sprintf(fullname,"%s/%s",path,oldname); 5159 sprintf(old_fullname,"%s/%s",oldpath,oldname); 5160 free(oldpath); 5161 free(path); 5162 } 5163 else 5164 { 5165 fullname = my_str_malloc((size_t)(strlen(path) + strlen(filename) + 2)); 5166 sprintf(fullname,"%s/%s",path,filename); 5167 free(path); 5168 } 5169 5170 if( !strncmp(cmd_name,"copyfile",strlen("copyfile")) ) 5171 { 5172 add_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 5173 return 0; 5174 } 5175 if( !strncmp(cmd_name, "cancelcopy", 10)) 5176 { 5177 //sprintf(fullname,"%s/%s",path,filename); 5178 del_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 5179 free(fullname); 5180 return 0; 5181 } 5182 if( strcmp(cmd_name, "createfile") == 0 ) 5183 { 5184 strcpy(action,"createfile"); 5185 del_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 5186 } 5187 else if( strcmp(cmd_name, "remove") == 0 || strcmp(cmd_name, "delete") == 0) 5188 { 5189 strcpy(action,"remove"); 5190 del_download_only_action_item(action,fullname,g_pSyncList[index]->download_only_socket_head); 5191 } 5192 else if( strcmp(cmd_name, "createfolder") == 0 ) 5193 { 5194 strcpy(action,"createfolder"); 5195 del_action_item("copyfile",fullname,g_pSyncList[index]->copy_file_list); 5196 } 5197 else if( strncmp(cmd_name, "rename",6) == 0 ) 5198 { 5199 strcpy(action,"rename"); 5200 del_download_only_action_item(action,old_fullname,g_pSyncList[index]->download_only_socket_head); 5201 free(old_fullname); 5202 } 5203 else if( strncmp(cmd_name, "move",4) == 0 ) 5204 { 5205 strcpy(action,"move"); 5206 del_download_only_action_item(action,old_fullname,g_pSyncList[index]->download_only_socket_head); 5207 //free(old_fullname); 5208 } 5209 5210 if(g_pSyncList[index]->server_action_list->next != NULL) 5211 { 5212 action_item *item; 5213 DEBUG("%s:%s\n",action,fullname); 5214 item = get_action_item(action,fullname,g_pSyncList[index]->server_action_list,index); 5215 5216 if(item != NULL) 5217 { 5218 DEBUG("##### %s %s by FTP Server self ######\n",action,fullname); 5219 //pthread_mutex_lock(&mutex); 5220 del_action_item(action,fullname,g_pSyncList[index]->server_action_list); 5221 free(fullname); 5222 return 0; 5223 } 5224 } 5225 5226 if(g_pSyncList[index]->dragfolder_action_list->next != NULL) 5227 { 5228 action_item *item; 5229 5230 item = get_action_item(action,fullname,g_pSyncList[index]->dragfolder_action_list,index); 5231 5232 if(item != NULL) 5233 { 5234 DEBUG("##### %s %s by dragfolder recursion self ######\n",action,fullname); 5235 del_action_item(action,fullname,g_pSyncList[index]->dragfolder_action_list); 5236 free(fullname); 5237 return 0; 5238 } 5239 } 5240 5241 if( strcmp(cmd_name, "copyfile") != 0 ) 5242 { 5243 g_pSyncList[index]->have_local_socket = 1; 5244 } 5245 5246 if(strncmp(cmd_name, "rename",6) == 0) 5247 { 5248 if(test_if_dir(fullname)) 5249 { 5250 add_all_download_only_socket_list(cmd_name,fullname,index); 5251 } 5252 else 5253 { 5254 add_action_item(cmd_name,fullname,g_pSyncList[index]->download_only_socket_head); 5255 } 5256 } 5257 else if(strncmp(cmd_name, "move",4) == 0) 5258 { 5259 int old_index = get_path_to_index(old_fullname); 5260 if(old_index == index) 5261 { 5262 if(test_if_dir(fullname)) 5263 { 5264 add_all_download_only_socket_list(cmd_name,fullname,index); 5265 } 5266 else 5267 { 5268 add_action_item(cmd_name,fullname,g_pSyncList[index]->download_only_socket_head); 5269 } 5270 } 5271 else 5272 { 5273 if(ftp_config.multrule[old_index]->rules == 1) 5274 { 5275 del_download_only_action_item("",old_fullname,g_pSyncList[old_index]->download_only_socket_head); 5276 } 5277 else 5278 { 5279 Delete(old_fullname,old_index); 5280 } 5281 if(test_if_dir(fullname)) 5282 { 5283 add_all_download_only_socket_list(cmd_name,fullname,index); 5284 } 5285 else 5286 { 5287 add_action_item(cmd_name,fullname,g_pSyncList[index]->download_only_socket_head); 5288 } 5289 } 5290 5291 free(old_fullname); 5292 } 5293 else if(strcmp(cmd_name, "createfolder") == 0 || strcmp(cmd_name, "dragfolder") == 0) 5294 { 5295 add_action_item(cmd_name,fullname,g_pSyncList[index]->download_only_socket_head); 5296 add_all_download_only_dragfolder_socket_list(fullname,index); 5297 } 5298 else if( strcmp(cmd_name, "createfile") == 0 || strcmp(cmd_name, "dragfile") == 0 || strcmp(cmd_name, "modify") == 0) 5299 { 5300 add_action_item(cmd_name,fullname,g_pSyncList[index]->download_only_socket_head); 5301 } 5302 5303 free(fullname); 5304 return 0; 5305} 5306 5307int is_server_modify(Server_TreeNode *newNode,Server_TreeNode *oldNode) 5308{ 5309 5310 //DEBUG("########is_server_modify\n"); 5311 if(newNode->browse == NULL && oldNode->browse == NULL) 5312 { 5313 DEBUG("########Server is no modify1\n"); 5314 return 1; 5315 } 5316 else if(newNode->browse == NULL && oldNode->browse != NULL) 5317 { 5318 DEBUG("########Server is no modify2\n"); 5319 return 1; 5320 } 5321 else if(newNode->browse != NULL && oldNode->browse == NULL) 5322 { 5323 DEBUG("########Server is no modify3\n"); 5324 return 1; 5325 } 5326 else 5327 { 5328 int cmp; 5329 CloudFile *newfoldertmp = NULL; 5330 CloudFile *oldfoldertmp = NULL; 5331 CloudFile *newfiletmp = NULL; 5332 CloudFile *oldfiletmp = NULL; 5333 if(newNode->browse != NULL) 5334 { 5335 if(newNode->browse->foldernumber > 0) 5336 newfoldertmp = newNode->browse->folderlist->next; 5337 if(newNode->browse->filenumber > 0) 5338 { 5339 newfiletmp = newNode->browse->filelist->next; 5340 } 5341 else 5342 { 5343 DEBUG("########Server is no modify4\n"); 5344 return 1; 5345 } 5346 } 5347 if(oldNode->browse != NULL) 5348 { 5349 if(oldNode->browse->foldernumber > 0) 5350 oldfoldertmp = oldNode->browse->folderlist->next; 5351 if(oldNode->browse->filenumber <= 0) 5352 { 5353 DEBUG("########Server is no modify5\n"); 5354 return 1; 5355 } 5356 } 5357 5358 while(newfiletmp != NULL) 5359 { 5360 oldfiletmp = oldNode->browse->filelist->next; 5361 while (oldfiletmp != NULL) 5362 { 5363 if ((cmp = strcmp(newfiletmp->href,oldfiletmp->href)) == 0) 5364 { 5365 if((newfiletmp->modtime != oldfiletmp->modtime) || (oldfiletmp->ismodify == 1)) 5366 { 5367 DEBUG("########Server %s is modified\n",newfiletmp->href); 5368 newfiletmp->ismodify = 1; 5369 } 5370 break; 5371 } 5372 5373 oldfiletmp = oldfiletmp->next; 5374 } 5375 5376 newfiletmp = newfiletmp->next; 5377 } 5378 5379 } 5380 //DEBUG("########is_server_modify over\n"); 5381 return 1; 5382} 5383 5384int check_serverlist_modify(int index,Server_TreeNode *newnode) 5385{ 5386 int ret; 5387 //DEBUG("############check_serverlist_modify %s\n",newnode->parenthref); 5388 Server_TreeNode *oldnode; 5389 oldnode = getoldnode(g_pSyncList[index]->OldServerRootNode->Child,newnode->parenthref); 5390 5391// if(newnode == NULL) 5392// { 5393// DEBUG("newnode is NULL\n"); 5394// } 5395 5396// if(oldnode == NULL) 5397// { 5398// DEBUG("oldnode is NULL\n"); 5399// } 5400 5401 if(newnode != NULL && oldnode != NULL) 5402 { 5403 5404 ret = is_server_modify(newnode,oldnode); 5405 //return ret; 5406 } 5407 else if(newnode == NULL) 5408 { 5409 return 0; 5410 } 5411 5412 if(newnode->Child != NULL) 5413 { 5414 ret = check_serverlist_modify(index,newnode->Child); 5415 } 5416 5417 if(newnode->NextBrother != NULL) 5418 { 5419 ret = check_serverlist_modify(index,newnode->NextBrother); 5420 } 5421 5422 return ret; 5423} 5424 5425void *Socket_Parser(void *argc) 5426{ 5427 DEBUG("Socket_Parser:%u\n",pthread_self());//获取线程号 5428 Node *socket_execute; 5429 int status = 0; 5430 //int mysync = 1; 5431 int has_socket = 0; 5432 int i; 5433 struct timeval now; 5434 struct timespec outtime; 5435 int fail_flag; 5436 5437 while(!exit_loop) 5438 { 5439#ifdef _PC 5440 //check_link_internet(); 5441#else 5442 check_link_internet(); 5443#endif 5444 for(i = 0;i<ftp_config.dir_num;i++) 5445 { 5446 fail_flag = 0; 5447 while (server_sync == 1 && exit_loop ==0) 5448 { 5449 usleep(1000*10);//10毫秒 5450 } 5451 local_sync = 1; 5452 5453 if(exit_loop) 5454 break; 5455 5456 if(disk_change) 5457 { 5458 check_disk_change(); 5459 } 5460 status = usr_auth(ftp_config.multrule[i]->server_ip,ftp_config.multrule[i]->user_pwd); 5461 if(status == 7) 5462 { 5463 write_log(S_ERROR,"check your ip.","",ftp_config.multrule[i]->rules); 5464 continue; 5465 } 5466 else if(status == 67) 5467 { 5468 write_log(S_ERROR,"check your usrname or password.","",ftp_config.multrule[i]->rules); 5469 continue; 5470 } 5471 else if(status == 28) 5472 { 5473 write_log(S_ERROR,"connect timeout,check your ip or network.","",ftp_config.multrule[i]->rules); 5474 continue; 5475 } 5476 int flag = is_server_exist(ftp_config.multrule[i]->hardware, ftp_config.multrule[i]->rooturl, i); 5477 //is_server_exist返回1,硬碟存在,flag=1 5478 if(flag == CURLE_FTP_COULDNT_RETR_FILE)//判断flag=19? RETR' 命令收到了不正常的回复,或完成的传输尺寸为零字节 5479 { 5480 write_log(S_ERROR,"Permission Denied.","",ftp_config.multrule[i]->rules); 5481 continue; 5482 } 5483 else if(flag == 0)//curl_ok (All fine. Proceed as usual) 5484 { 5485 DEBUG("curl_OK\n"); 5486 //write_log(S_ERROR,"remote root path lost OR write only.","",ftp_config.multrule[i]->rules); 5487 //continue; 5488 } 5489 5490 5491 if(g_pSyncList[i]->sync_disk_exist == 0) 5492 continue; 5493 5494 if(ftp_config.multrule[i]->rules == 1) //download only 5495 { 5496 5497 while(exit_loop == 0) 5498 { 5499 while(g_pSyncList[i]->SocketActionList_Rename->next != NULL || g_pSyncList[i]->SocketActionList->next != NULL) 5500 { 5501 while(g_pSyncList[i]->SocketActionList_Rename->next != NULL) 5502 { 5503 has_socket = 1; 5504 socket_execute = g_pSyncList[i]->SocketActionList_Rename->next; 5505 status = download_only_add_socket_item(socket_execute->cmdName,i); 5506 if(status == 0) 5507 { 5508 DEBUG("########will del socket item##########\n"); 5509 pthread_mutex_lock(&mutex_socket); 5510 socket_execute = queue_dequeue(g_pSyncList[i]->SocketActionList_Rename); 5511 free(socket_execute); 5512 DEBUG("del socket item ok\n"); 5513 pthread_mutex_unlock(&mutex_socket); 5514 } 5515 else 5516 { 5517 fail_flag = 1; 5518 DEBUG("######## socket item fail########\n"); 5519 break; 5520 } 5521 //sleep(2); 5522 usleep(1000*20); 5523 } 5524 while(g_pSyncList[i]->SocketActionList->next != NULL) 5525 { 5526 has_socket = 1; 5527 socket_execute = g_pSyncList[i]->SocketActionList->next; 5528 status = download_only_add_socket_item(socket_execute->cmdName,i); 5529 if(status == 0) 5530 { 5531 DEBUG("########will del socket item##########\n"); 5532 pthread_mutex_lock(&mutex_socket); 5533 socket_execute = queue_dequeue(g_pSyncList[i]->SocketActionList); 5534 free(socket_execute); 5535 DEBUG("del socket item ok\n"); 5536 pthread_mutex_unlock(&mutex_socket); 5537 } 5538 else 5539 { 5540 fail_flag = 1; 5541 DEBUG("######## socket item fail########\n"); 5542 break; 5543 } 5544 //sleep(2); 5545 usleep(1000*20); 5546 5547 if(g_pSyncList[i]->SocketActionList_Rename->next != NULL) 5548 break; 5549 } 5550 } 5551 5552 if(fail_flag) 5553 { 5554 break; 5555 } 5556 if(g_pSyncList[i]->copy_file_list->next == NULL) 5557 { 5558 break; 5559 } 5560 else 5561 { 5562 usleep(1000*100); 5563 } 5564 } 5565 if(g_pSyncList[i]->dragfolder_action_list->next != NULL && g_pSyncList[i]->SocketActionList->next == NULL) 5566 { 5567 free_action_item(g_pSyncList[i]->dragfolder_action_list); 5568 g_pSyncList[i]->dragfolder_action_list = create_action_item_head(); 5569 } 5570 if(g_pSyncList[i]->server_action_list->next != NULL && g_pSyncList[i]->SocketActionList->next == NULL) 5571 { 5572 free_action_item(g_pSyncList[i]->server_action_list); 5573 g_pSyncList[i]->server_action_list = create_action_item_head(); 5574 } 5575 pthread_mutex_lock(&mutex_receve_socket); 5576 if(g_pSyncList[i]->SocketActionList->next == NULL) 5577 g_pSyncList[i]->receve_socket = 0; 5578 pthread_mutex_unlock(&mutex_receve_socket); 5579 } 5580 else //Sync 5581 { 5582 if(ftp_config.multrule[i]->rules == 2) //upload only规则时,处理未完成的上传动作 5583 { 5584 } 5585 5586 while(exit_loop ==0) 5587 { 5588 while(g_pSyncList[i]->SocketActionList_Rename->next != NULL || g_pSyncList[i]->SocketActionList->next != NULL) 5589 { 5590 while(g_pSyncList[i]->SocketActionList_Rename->next != NULL) 5591 { 5592 has_socket = 1; 5593 socket_execute = g_pSyncList[i]->SocketActionList_Rename->next; 5594 //socket_execute->cmdName=rename0 5595 // /tmp/mnt/SNK/sync 5596 // 未命名文件夹 5597 // aa 5598 status = cmd_parser(socket_execute->cmdName,i); 5599 if(status == 0 || status == LOCAL_FILE_LOST) 5600 { 5601 pthread_mutex_lock(&mutex_socket); 5602 socket_execute = queue_dequeue(g_pSyncList[i]->SocketActionList_Rename); 5603 free(socket_execute); 5604 pthread_mutex_unlock(&mutex_socket); 5605 } 5606 else 5607 { 5608 fail_flag = 1; 5609 DEBUG("######## socket item fail########\n"); 5610 break; 5611 } 5612 //pthread_mutex_unlock(&mutex_socket); 5613 //sleep(2); 5614 usleep(1000*20); 5615 } 5616 while(g_pSyncList[i]->SocketActionList->next != NULL && exit_loop == 0) 5617 { 5618 has_socket = 1; 5619 socket_execute = g_pSyncList[i]->SocketActionList->next; 5620 //pthread_mutex_lock(&mutex_socket); 5621 status = cmd_parser(socket_execute->cmdName,i); 5622 DEBUG("status:%d\n",status); 5623 if(status == 0 || status == LOCAL_FILE_LOST || status==SERVER_SPACE_NOT_ENOUGH || status == CURLE_READ_ERROR || status == CURLE_ABORTED_BY_CALLBACK) 5624 {//CURLE_ABORTED_BY_CALLBACK由回调中止, CURLE_READ_ERROR:couldn't open/read from file 5625 if(status == LOCAL_FILE_LOST && g_pSyncList[i]->SocketActionList_Rename->next != NULL) 5626 break; 5627 DEBUG("########will del socket item##########\n"); 5628 pthread_mutex_lock(&mutex_socket); 5629 socket_execute = queue_dequeue(g_pSyncList[i]->SocketActionList); 5630 free(socket_execute); 5631 DEBUG("del socket item ok\n"); 5632 pthread_mutex_unlock(&mutex_socket); 5633 } 5634 else 5635 { 5636 fail_flag = 1; 5637 DEBUG("######## socket item fail########\n"); 5638 break; 5639 } 5640 //pthread_mutex_unlock(&mutex_socket); 5641 //sleep(2); 5642 usleep(1000*20); 5643 5644 if(g_pSyncList[i]->SocketActionList_Rename->next != NULL) 5645 break; 5646 } 5647 } 5648 5649 if(fail_flag) 5650 break; 5651 5652 //show_item(g_pSyncList[i]->copy_file_list); 5653 if(g_pSyncList[i]->copy_file_list->next == NULL) 5654 { 5655 break; 5656 } 5657 else 5658 { 5659 usleep(1000*100); 5660 } 5661 } 5662 if(g_pSyncList[i]->dragfolder_action_list->next != NULL && g_pSyncList[i]->SocketActionList->next == NULL) 5663 { 5664 //DEBUG("################################################ dragfolder_action_list!\n"); 5665 free_action_item(g_pSyncList[i]->dragfolder_action_list); 5666 g_pSyncList[i]->dragfolder_action_list = create_action_item_head(); 5667 } 5668 if(g_pSyncList[i]->server_action_list->next != NULL && g_pSyncList[i]->SocketActionList->next == NULL) 5669 { 5670 //DEBUG("################################################ clear server_action_list!\n"); 5671 free_action_item(g_pSyncList[i]->server_action_list); 5672 g_pSyncList[i]->server_action_list = create_action_item_head(); 5673 } 5674 //DEBUG("#### clear server_action_list success!\n"); 5675 pthread_mutex_lock(&mutex_receve_socket); 5676 if(g_pSyncList[i]->SocketActionList->next == NULL) 5677 g_pSyncList[i]->receve_socket = 0; 5678 pthread_mutex_unlock(&mutex_receve_socket); 5679 } 5680 } 5681 pthread_mutex_lock(&mutex_socket); 5682 local_sync = 0; 5683 if(!exit_loop) 5684 { 5685 gettimeofday(&now, NULL); 5686 outtime.tv_sec = now.tv_sec + 2; 5687 outtime.tv_nsec = now.tv_usec * 1000; 5688 pthread_cond_timedwait(&cond_socket, &mutex_socket, &outtime); 5689 } 5690 pthread_mutex_unlock(&mutex_socket); 5691 //DEBUG("set local_sync %d!\n",local_sync); 5692 } 5693 5694 //DEBUG("stop FTP Socket_Parser\n"); 5695 stop_down = 1; 5696 return NULL; 5697} 5698 5699int send_action(int type, char *content,int port) 5700{ 5701 int sockfd, numbytes; 5702 char buf[MAXDATASIZE]; 5703 char str[1024]; 5704 //int port; 5705 //if(type == 1) 5706 //port = INOTIFY_PORT;//3678 5707 5708 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { //创建socket套接字 5709 perror("socket"); 5710 return -1; 5711 } 5712 5713 struct sockaddr_in their_addr; /* connector's address information */ 5714 bzero(&(their_addr), sizeof(their_addr)); /* zero the rest of the struct */ 5715 their_addr.sin_family = AF_INET; /* host byte order */ 5716 their_addr.sin_port = htons(port); /* short, network byte order */ 5717 their_addr.sin_addr.s_addr = htonl(INADDR_ANY); 5718 //their_addr.sin_addr.s_addr = ((struct in_addr *)(he->h_addr))->s_addr; 5719 bzero(&(their_addr.sin_zero), sizeof(their_addr.sin_zero)); /* zero the rest of the struct */ 5720 5721 if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) { 5722 perror("connect"); 5723 return -1; 5724 } 5725 5726 if(port == 80) 5727 { 5728 sprintf(str,"%s",content); 5729 DEBUG("to 80 port:%s\n",str); 5730 } 5731 else 5732 { 5733 sprintf(str,"%d@%s",type,content); 5734 } 5735 5736 if (send(sockfd, str, strlen(str), 0) == -1) { 5737 perror("send"); 5738 //exit(1); 5739 return -1; 5740 } 5741 5742// if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { 5743// perror("recv"); 5744// //exit(1); 5745// return -1; 5746// } 5747 5748 //buf[numbytes] = '\0'; 5749 close(sockfd); 5750 DEBUG("send_action finished!\n"); 5751 return 0; 5752} 5753 5754void send_to_inotify() 5755{ 5756#if PLAN_B 5757 if(browse_sev != -1) 5758 { 5759 DEBUG("send to inotify:selecting serverlist!\n"); 5760 return; 5761 } 5762#endif 5763 int i; 5764 5765 for(i=0;i<ftp_config.dir_num;i++) 5766 { 5767 DEBUG("send_action base_path = %s\n",ftp_config.multrule[i]->base_path); 5768 send_action(ftp_config.id,ftp_config.multrule[i]->base_path,INOTIFY_PORT); 5769 usleep(1000*500); 5770 } 5771} 5772 5773void read_config() 5774{ 5775#ifdef NVRAM_ 5776 if(convert_nvram_to_file_mutidir(CONFIG_PATH,&ftp_config) == -1) 5777 { 5778 DEBUG("convert_nvram_to_file fail\n"); 5779 exit(-1); 5780 } 5781#else 5782#ifdef _PC 5783// if(create_ftp_conf_file(&ftp_config) == -1) 5784// { 5785// DEBUG("create_ftp_conf_file fail\n"); 5786// exit(-1); 5787// } 5788#else 5789 if(create_ftp_conf_file(&ftp_config) == -1) 5790 { 5791 DEBUG("create_ftp_conf_file fail\n"); 5792 exit(-1); 5793 } 5794#endif 5795#endif 5796 DEBUG("#####read_config#####\n"); 5797 parse_config(CONFIG_PATH,&ftp_config); 5798#ifdef _PC 5799 ftp_config.dir_num = 1; 5800#else 5801 //exit(-1); 5802 if(ftp_config.dir_num == 0) 5803 { 5804 no_config = 1; 5805 return; 5806 } 5807#endif 5808 while(no_config == 1 && exit_loop != 1 ) 5809 { 5810 sleep(1); 5811 } 5812 5813 //if(!no_config && browse_sev == -1) 5814 if(!no_config) 5815 { 5816 exit_loop = 0; 5817 if(get_tokenfile_info()==-1) 5818 { 5819 DEBUG("\nget_tokenfile_info failed\n"); 5820 exit(-1); 5821 } 5822 DEBUG("get_tokenfile_info over\n"); 5823 check_config_path(1); 5824 } 5825} 5826 5827void init_global_var() 5828{ 5829#if PLAN_B 5830 if(browse_sev != -1) 5831 { 5832 DEBUG("init global var:selecting serverlist!\n"); 5833 return; 5834 } 5835#endif 5836 upload_break = 0; 5837 upload_file_lost = 0; 5838 del_acount = -1; 5839 5840 pthread_mutex_init(&mutex, NULL); 5841 pthread_mutex_init(&mutex_socket, NULL); 5842 pthread_mutex_init(&mutex_socket_1, NULL); 5843 pthread_mutex_init(&mutex_receve_socket, NULL); 5844 pthread_mutex_init(&mutex_rename, NULL); 5845 5846 int num = ftp_config.dir_num; 5847 g_pSyncList = (sync_list **)malloc(sizeof(sync_list *)*num); 5848 memset(g_pSyncList,0,sizeof(g_pSyncList)); 5849 5850 ServerNode_del = NULL; 5851 5852 for(int i=0;i<num;i++) 5853 { 5854 g_pSyncList[i] = (sync_list *)malloc(sizeof(sync_list)); 5855 memset(g_pSyncList[i],0,sizeof(sync_list)); 5856 5857 g_pSyncList[i]->index = i; 5858 g_pSyncList[i]->receve_socket = 0; 5859 g_pSyncList[i]->have_local_socket = 0; 5860 g_pSyncList[i]->first_sync = 1; 5861 g_pSyncList[i]->no_local_root = 0; 5862 g_pSyncList[i]->init_completed = 0; 5863 g_pSyncList[i]->IsNetWorkUnlink = 0;//2014.11.03 by sherry 5864 g_pSyncList[i]->IsPermission = 0;//2014.11.04 by sherry 5865 g_pSyncList[i]->ServerRootNode = NULL; 5866 g_pSyncList[i]->OldServerRootNode = NULL; 5867 g_pSyncList[i]->OrigServerRootNode = NULL; 5868 5869 g_pSyncList[i]->sync_disk_exist = 0; 5870 //create head 5871 g_pSyncList[i]->SocketActionList = new Node; 5872 g_pSyncList[i]->SocketActionList->cmdName = NULL; 5873 g_pSyncList[i]->SocketActionList->next = NULL; 5874 5875 sprintf(g_pSyncList[i]->temp_path,"%s/.smartsync/ftpclient",ftp_config.multrule[i]->mount_path); 5876 my_mkdir_r(g_pSyncList[i]->temp_path,i); 5877 5878 char *base_path_tmp = my_str_malloc(sizeof(ftp_config.multrule[i]->base_path)); 5879 sprintf(base_path_tmp,"%s",ftp_config.multrule[i]->base_path); 5880 replace_char_in_str(base_path_tmp,'_','/'); 5881 snprintf(g_pSyncList[i]->up_item_file,255,"%s/%s_up_item",g_pSyncList[i]->temp_path,base_path_tmp); 5882 //snprintf(g_pSyncList[i]->conflict_item,255,"%s/conflict_%s", 5883 //g_pSyncList[i]->temp_path,ftp_config.multrule[i]->base_folder + 1); 5884 free(base_path_tmp); 5885 5886 g_pSyncList[i]->SocketActionList_Rename = new Node; 5887 g_pSyncList[i]->SocketActionList_Rename->cmdName = NULL; 5888 g_pSyncList[i]->SocketActionList_Rename->next = NULL; 5889 5890 g_pSyncList[i]->server_action_list = create_action_item_head(); 5891 g_pSyncList[i]->dragfolder_action_list = create_action_item_head(); 5892 g_pSyncList[i]->unfinished_list = create_action_item_head(); 5893 g_pSyncList[i]->up_space_not_enough_list = create_action_item_head(); 5894 g_pSyncList[i]->copy_file_list = create_action_item_head(); 5895 if(ftp_config.multrule[i]->rules == 1) 5896 { 5897 g_pSyncList[i]->download_only_socket_head = create_action_item_head(); 5898 } 5899 5900 tokenfile_info_tmp = tokenfile_info_start->next; 5901 while(tokenfile_info_tmp != NULL) 5902 { 5903 DEBUG("tokenfile_info_tmp->mountpath = %s\n",tokenfile_info_tmp->mountpath); 5904 DEBUG("ftp_config.multrule[i]->mount_path = %s\n",ftp_config.multrule[i]->mount_path); 5905 //if(!strcmp(tokenfile_info_tmp->mountpath,ftp_config.multrule[i]->mount_path) && browse_sev == -1) 5906 if(!strcmp(tokenfile_info_tmp->mountpath,ftp_config.multrule[i]->mount_path)) 5907 { 5908 g_pSyncList[i]->sync_disk_exist = 1; 5909 break; 5910 } 5911 tokenfile_info_tmp = tokenfile_info_tmp->next; 5912 } 5913 } 5914} 5915 5916/*used for initial,local syncfolder is NULL*/ 5917int initMyLocalFolder(Server_TreeNode *servertreenode,int index) 5918{ 5919 int res = 0; 5920 if(servertreenode->browse != NULL) 5921 { 5922 CloudFile *init_foldercurrent = NULL; 5923 CloudFile *init_filecurrent = NULL; 5924 if(servertreenode->browse->foldernumber > 0) 5925 init_foldercurrent = servertreenode->browse->folderlist->next; 5926 if(servertreenode->browse->filenumber > 0) 5927 init_filecurrent = servertreenode->browse->filelist->next; 5928 5929 while(init_foldercurrent != NULL) 5930 { 5931 char *createpath = serverpath_to_localpath(init_foldercurrent->href,index); 5932 if(NULL == opendir(createpath)) 5933 { 5934 if(wait_handle_socket(index)) 5935 { 5936 return HAVE_LOCAL_SOCKET; 5937 } 5938 if(-1 == mkdir(createpath,0777)) 5939 { 5940 DEBUG("mkdir %s fail",createpath); 5941 exit(-1); 5942 } 5943 else 5944 { 5945 add_action_item("createfolder",createpath,g_pSyncList[index]->server_action_list); 5946 } 5947 } 5948 free(createpath); 5949 init_foldercurrent = init_foldercurrent->next; 5950 } 5951 5952 while(init_filecurrent != NULL) 5953 { 5954 if(is_local_space_enough(init_filecurrent,index)) 5955 { 5956 char *createpath = serverpath_to_localpath(init_filecurrent->href,index); 5957 if(wait_handle_socket(index)) 5958 { 5959 return HAVE_LOCAL_SOCKET; 5960 } 5961 add_action_item("createfile",createpath,g_pSyncList[index]->server_action_list); 5962 5963 int status = download_(init_filecurrent->href,index); 5964 if(status == 0) 5965 { 5966 DEBUG("do_cmd ok\n"); 5967 mod_time *onefiletime = Getmodtime(init_filecurrent->href,index); 5968 if(ChangeFile_modtime(createpath,onefiletime->modtime,index)) 5969 { 5970 DEBUG("ChangeFile_modtime failed!\n"); 5971 return status; 5972 } 5973 free(onefiletime); 5974 } 5975 free(createpath); 5976 } 5977 else 5978 { 5979 write_log(S_ERROR,"local space is not enough!","",index); 5980 add_action_item("download",init_filecurrent->href,g_pSyncList[index]->unfinished_list); 5981 } 5982 init_filecurrent = init_filecurrent->next; 5983 } 5984 } 5985 5986 if(servertreenode->Child != NULL) 5987 { 5988 res = initMyLocalFolder(servertreenode->Child,index); 5989 if(res != 0) 5990 { 5991 return res; 5992 } 5993 } 5994 5995 if(servertreenode->NextBrother != NULL) 5996 { 5997 res = initMyLocalFolder(servertreenode->NextBrother,index); 5998 if(res != 0) 5999 { 6000 return res; 6001 } 6002 } 6003 return res; 6004} 6005 6006int sync_local_to_server_init(Local *perform_lo,int index) 6007{ 6008 DEBUG("*******sync_local_to_server_init*******\n"); 6009 LocalFolder *localfoldertmp; 6010 LocalFile *localfiletmp; 6011 int ret = 0; 6012 6013 localfoldertmp = perform_lo->folderlist->next; 6014 localfiletmp = perform_lo->filelist->next; 6015 6016 //handle files 6017 while(localfiletmp != NULL) 6018 { 6019 if(ftp_config.multrule[index]->rules != 1) 6020 { 6021 if(wait_handle_socket(index)) 6022 { 6023 return HAVE_LOCAL_SOCKET; 6024 } 6025 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->server_action_list); 6026 ret = upload(localfiletmp->path,index); 6027 DEBUG("handle files,ret = %d\n",ret); 6028 if(ret == 0) 6029 { 6030 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 6031 //DEBUG("serverpath = %s\n",serverpath); 6032 mod_time *onefiletime = Getmodtime(serverpath,index); 6033 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 6034 { 6035 DEBUG("ChangeFile_modtime failed!\n"); 6036 } 6037 free(onefiletime); 6038 free(serverpath); 6039 } 6040 else 6041 { 6042 return ret; 6043 } 6044 } 6045 else 6046 { 6047 if(wait_handle_socket(index)) 6048 { 6049 return HAVE_LOCAL_SOCKET; 6050 } 6051 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->download_only_socket_head); 6052 } 6053 6054 localfiletmp = localfiletmp->next; 6055 } 6056 6057 //handle folders 6058 while(localfoldertmp != NULL) 6059 { 6060 if(ftp_config.multrule[index]->rules != 1) 6061 { 6062 if(wait_handle_socket(index)) 6063 { 6064 return HAVE_LOCAL_SOCKET; 6065 } 6066 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->server_action_list); 6067 ret = my_mkdir_(localfoldertmp->path,index); 6068 DEBUG("handle folders,ret = %d\n",ret); 6069 if(ret != 0) 6070 { 6071 return ret; 6072 } 6073 } 6074 else 6075 { 6076 if(wait_handle_socket(index)) 6077 { 6078 return HAVE_LOCAL_SOCKET; 6079 } 6080 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->download_only_socket_head); 6081 } 6082 6083 localfoldertmp = localfoldertmp->next; 6084 } 6085 6086 return ret; 6087} 6088 6089int sync_local_to_server(char *path,int index) 6090{ 6091 DEBUG("sync_local_to_server path = %s\n",path); 6092 6093 Local *localnode; 6094 int ret = 0; 6095 int res = 0; 6096 LocalFolder *localfoldertmp; 6097 6098 if(exit_loop) 6099 { 6100 return -1; 6101 } 6102 6103 localnode = Find_Floor_Dir(path); 6104 6105 if(localnode == NULL) 6106 { 6107 return 0; 6108 } 6109 6110 ret = is_server_have_localpath(path,g_pSyncList[index]->ServerRootNode->Child,index); 6111 6112 if(ret == 0) 6113 { 6114 res = sync_local_to_server_init(localnode,index); 6115 if(res != 0 && res != LOCAL_FILE_LOST)//&& res != SERVER_SPACE_NOT_ENOUGH 6116 { 6117 DEBUG("##########res = %d\n",res); 6118 free_localfloor_node(localnode); 6119 return res; 6120 } 6121 } 6122 6123 localfoldertmp = localnode->folderlist->next; 6124 while(localfoldertmp != NULL) 6125 { 6126 res = sync_local_to_server(localfoldertmp->path,index); 6127 if(res != 0 && res != LOCAL_FILE_LOST)// && res != SERVER_SPACE_NOT_ENOUGH 6128 { 6129 DEBUG("##########res = %d\n",res); 6130 free_localfloor_node(localnode); 6131 return res; 6132 } 6133 6134 localfoldertmp = localfoldertmp->next; 6135 } 6136 6137 free_localfloor_node(localnode); 6138 6139 return res; 6140} 6141 6142int sync_server_to_local_init(Browse *perform_br,Local *perform_lo,int index) 6143{ 6144 if(perform_br == NULL || perform_lo == NULL) 6145 { 6146 return 0; 6147 } 6148 6149 CloudFile *foldertmp = NULL; 6150 CloudFile *filetmp = NULL; 6151 LocalFolder *localfoldertmp; 6152 LocalFile *localfiletmp; 6153 int ret = 0; 6154 6155 if(perform_br->foldernumber > 0) 6156 foldertmp = perform_br->folderlist->next; 6157 if(perform_br->filenumber > 0) 6158 filetmp = perform_br->filelist->next; 6159 6160 localfoldertmp = perform_lo->folderlist->next; 6161 localfiletmp = perform_lo->filelist->next; 6162 6163 if(perform_br->filenumber == 0 && perform_lo->filenumber != 0) 6164 { 6165 DEBUG("serverfileNo:%d\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 6166 while(localfiletmp != NULL && exit_loop == 0) 6167 { 6168 if(ftp_config.multrule[index]->rules != 1) 6169 { 6170 if(wait_handle_socket(index)) 6171 { 6172 return HAVE_LOCAL_SOCKET; 6173 } 6174 6175 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->server_action_list); 6176 ret = upload(localfiletmp->path,index); 6177 if(ret == 0) 6178 { 6179 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 6180 //DEBUG("serverpath = %s\n",serverpath); 6181 mod_time *onefiletime = Getmodtime(serverpath,index); 6182 free(serverpath); 6183 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 6184 { 6185 DEBUG("ChangeFile_modtime failed!\n"); 6186 } 6187 free(onefiletime); 6188 } 6189 else 6190 { 6191 //DEBUG("upload failed!\n"); 6192 return ret; 6193 } 6194 } 6195 else 6196 { 6197 if(wait_handle_socket(index)) 6198 { 6199 return HAVE_LOCAL_SOCKET; 6200 } 6201 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->download_only_socket_head); 6202 } 6203 localfiletmp = localfiletmp->next; 6204 } 6205 } 6206 else if(perform_br->filenumber != 0 && perform_lo->filenumber == 0) 6207 { 6208 DEBUG("serverfileNo:%d\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 6209 if(ftp_config.multrule[index]->rules != 2) 6210 { 6211 while(filetmp != NULL && exit_loop == 0) 6212 { 6213 int cp = 0; 6214 do{ 6215 if(exit_loop == 1) 6216 return -1; 6217 cp = is_ftp_file_copying(filetmp->href,index); 6218 }while(cp == 1); 6219 6220 if(is_local_space_enough(filetmp,index)) 6221 { 6222 if(wait_handle_socket(index)) 6223 { 6224 return HAVE_LOCAL_SOCKET; 6225 } 6226 char *localpath = serverpath_to_localpath(filetmp->href,index); 6227 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 6228 6229 //wait_handle_socket(index); 6230 6231 ret = download_(filetmp->href,index); 6232 if (ret == 0) 6233 { 6234 //ChangeFile_modtime(localpath,filetmp->modtime); 6235 mod_time *onefiletime = Getmodtime(filetmp->href,index); 6236 if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 6237 { 6238 DEBUG("ChangeFile_modtime failed!\n"); 6239 } 6240 free(onefiletime); 6241 free(localpath); 6242 } 6243 else 6244 { 6245 free(localpath); 6246 return ret; 6247 } 6248 } 6249 else 6250 { 6251 write_log(S_ERROR,"local space is not enough!","",index); 6252 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 6253 } 6254 filetmp = filetmp->next; 6255 } 6256 } 6257 } 6258 else if(perform_br->filenumber != 0 && perform_lo->filenumber != 0) 6259 { 6260 DEBUG("serverfileNo:%d\tlocalfileNo:%d\n",perform_br->filenumber,perform_lo->filenumber); 6261 while(localfiletmp != NULL && exit_loop == 0) 6262 { 6263 DEBUG("localfiletmp->path = %s\n",localfiletmp->path); 6264 int cmp = 1; 6265 char *localpathtmp = strstr(localfiletmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 6266 while(filetmp != NULL) 6267 { 6268 char *serverpathtmp; 6269 serverpathtmp = strstr(filetmp->href,ftp_config.multrule[index]->rooturl) + strlen(ftp_config.multrule[index]->rooturl); 6270 DEBUG("%s\t%s\n",localpathtmp,serverpathtmp); 6271 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 6272 { 6273 break; 6274 } 6275 else 6276 { 6277 filetmp = filetmp->next; 6278 } 6279 } 6280 if (cmp != 0) 6281 { 6282 if(ftp_config.multrule[index]->rules != 1) 6283 { 6284 if(wait_handle_socket(index)) 6285 { 6286 return HAVE_LOCAL_SOCKET; 6287 } 6288 6289 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->server_action_list); 6290 ret = upload(localfiletmp->path,index); 6291 if(ret == 0) 6292 { 6293 char *serverpath = localpath_to_serverpath(localfiletmp->path,index); 6294 mod_time *onefiletime = Getmodtime(serverpath,index); 6295 if(ChangeFile_modtime(localfiletmp->path,onefiletime->modtime,index)) 6296 { 6297 DEBUG("ChangeFile_modtime failed!\n"); 6298 } 6299 free(onefiletime); 6300 free(serverpath); 6301 } 6302 else 6303 { 6304 return ret; 6305 } 6306 } 6307 else 6308 { 6309 if(wait_handle_socket(index)) 6310 { 6311 return HAVE_LOCAL_SOCKET; 6312 } 6313 add_action_item("createfile",localfiletmp->path,g_pSyncList[index]->download_only_socket_head); 6314 } 6315 } 6316 else 6317 { 6318 if((ret = the_same_name_compare(localfiletmp,filetmp,index,0)) != 0) 6319 { 6320 return ret; 6321 } 6322 } 6323 filetmp = perform_br->filelist->next; 6324 localfiletmp = localfiletmp->next; 6325 } 6326 6327 6328 filetmp = perform_br->filelist->next; 6329 localfiletmp = perform_lo->filelist->next; 6330 while(filetmp != NULL && exit_loop == 0) 6331 { 6332 int cmp = 1; 6333 char *serverpathtmp; 6334 serverpathtmp = strstr(filetmp->href,ftp_config.multrule[index]->rooturl) + strlen(ftp_config.multrule[index]->rooturl); 6335 //serverpathtmp = oauth_url_unescape(serverpathtmp,NULL); 6336 while(localfiletmp != NULL) 6337 { 6338 char *localpathtmp; 6339 localpathtmp = strstr(localfiletmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 6340 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 6341 { 6342 break; 6343 } 6344 else 6345 { 6346 localfiletmp = localfiletmp->next; 6347 } 6348 } 6349 if (cmp != 0) 6350 { 6351 if(ftp_config.multrule[index]->rules != 2) 6352 { 6353 int cp = 0; 6354 do{ 6355 if(exit_loop == 1) 6356 return -1; 6357 cp = is_ftp_file_copying(filetmp->href,index); 6358 }while(cp == 1); 6359 6360 if(is_local_space_enough(filetmp,index)) 6361 { 6362 char *localpath = serverpath_to_localpath(filetmp->href,index); 6363 6364 if(wait_handle_socket(index)) 6365 { 6366 return HAVE_LOCAL_SOCKET; 6367 } 6368 6369 add_action_item("createfile",localpath,g_pSyncList[index]->server_action_list); 6370 ret = download_(filetmp->href,index); 6371 6372 if (ret == 0) 6373 { 6374 mod_time *onefiletime = Getmodtime(filetmp->href,index); 6375 if(ChangeFile_modtime(localpath,onefiletime->modtime,index)) 6376 { 6377 DEBUG("ChangeFile_modtime failed!\n"); 6378 } 6379 free(onefiletime); 6380 free(localpath); 6381 } 6382 else 6383 { 6384 free(localpath); 6385 //free(serverpathtmp); 6386 return ret; 6387 } 6388 } 6389 else 6390 { 6391 write_log(S_ERROR,"local space is not enough!","",index); 6392 add_action_item("download",filetmp->href,g_pSyncList[index]->unfinished_list); 6393 } 6394 } 6395 } 6396 //free(serverpathtmp); 6397 filetmp = filetmp->next; 6398 localfiletmp = perform_lo->filelist->next; 6399 } 6400 } 6401 6402 if(perform_br->foldernumber == 0 && perform_lo->foldernumber != 0) 6403 { 6404 DEBUG("serverfolderNo:%d\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 6405 while(localfoldertmp != NULL && exit_loop == 0) 6406 { 6407 if(wait_handle_socket(index)) 6408 { 6409 return HAVE_LOCAL_SOCKET; 6410 } 6411 if(ftp_config.multrule[index]->rules != 1) 6412 { 6413 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->server_action_list); 6414 my_mkdir_(localfoldertmp->path,index); 6415 } 6416 else 6417 { 6418 if(wait_handle_socket(index)) 6419 { 6420 return HAVE_LOCAL_SOCKET; 6421 } 6422 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->download_only_socket_head); 6423 } 6424 localfoldertmp = localfoldertmp->next; 6425 } 6426 } 6427 else if(perform_br->foldernumber != 0 && perform_lo->foldernumber == 0) 6428 { 6429 DEBUG("serverfolderNo:%d\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 6430 if(ftp_config.multrule[index]->rules != 2) 6431 { 6432 while(foldertmp != NULL && exit_loop == 0) 6433 { 6434 if(wait_handle_socket(index)) 6435 { 6436 return HAVE_LOCAL_SOCKET; 6437 } 6438 char *localpath = serverpath_to_localpath(foldertmp->href,index); 6439 char *prePath = get_prepath(localpath,strlen(localpath)); 6440 int exist = is_server_exist(prePath,localpath,index); 6441 if(exist) 6442 { 6443 if(NULL == opendir(localpath)) 6444 { 6445 add_action_item("createfolder",localpath,g_pSyncList[index]->server_action_list); 6446 mkdir(localpath,0777); 6447 } 6448 } 6449 free(localpath); 6450 free(prePath); 6451 foldertmp = foldertmp->next; 6452 } 6453 } 6454 } 6455 else if(perform_br->foldernumber != 0 && perform_lo->foldernumber != 0) 6456 { 6457 DEBUG("serverfolderNo:%d\tlocalfolderNo:%d\n",perform_br->foldernumber,perform_lo->foldernumber); 6458 while(localfoldertmp != NULL && exit_loop == 0) 6459 { 6460 int cmp = 1; 6461 char *localpathtmp; 6462 localpathtmp = strstr(localfoldertmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 6463 while(foldertmp != NULL) 6464 { 6465 char *serverpathtmp; 6466 serverpathtmp = strstr(foldertmp->href,ftp_config.multrule[index]->rooturl) + strlen(ftp_config.multrule[index]->rooturl); 6467 DEBUG("%s\t%s\n",localpathtmp,serverpathtmp); 6468 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 6469 { 6470 //free(serverpathtmp); 6471 break; 6472 } 6473 else 6474 { 6475 foldertmp = foldertmp->next; 6476 } 6477 //free(serverpathtmp); 6478 } 6479 if (cmp != 0) 6480 { 6481 if(ftp_config.multrule[index]->rules != 1) 6482 { 6483 if(wait_handle_socket(index)) 6484 { 6485 return HAVE_LOCAL_SOCKET; 6486 } 6487 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->server_action_list); 6488 my_mkdir_(localfoldertmp->path,index); 6489 } 6490 else 6491 { 6492 if(wait_handle_socket(index)) 6493 { 6494 return HAVE_LOCAL_SOCKET; 6495 } 6496 add_action_item("createfolder",localfoldertmp->path,g_pSyncList[index]->download_only_socket_head); 6497 } 6498 } 6499 foldertmp = perform_br->folderlist->next; 6500 localfoldertmp = localfoldertmp->next; 6501 } 6502 6503 foldertmp = perform_br->folderlist->next; 6504 localfoldertmp = perform_lo->folderlist->next; 6505 while(foldertmp != NULL && exit_loop == 0) 6506 { 6507 int cmp = 1; 6508 char *serverpathtmp; 6509 serverpathtmp = strstr(foldertmp->href,ftp_config.multrule[index]->rooturl) + strlen(ftp_config.multrule[index]->rooturl); 6510 while(localfoldertmp != NULL) 6511 { 6512 char *localpathtmp; 6513 localpathtmp = strstr(localfoldertmp->path,ftp_config.multrule[index]->base_path) + ftp_config.multrule[index]->base_path_len; 6514 if ((cmp = strcmp(localpathtmp,serverpathtmp)) == 0) 6515 { 6516 break; 6517 } 6518 else 6519 { 6520 localfoldertmp = localfoldertmp->next; 6521 } 6522 } 6523 if (cmp != 0) 6524 { 6525 if(ftp_config.multrule[index]->rules != 2) 6526 { 6527 if(wait_handle_socket(index)) 6528 { 6529 return HAVE_LOCAL_SOCKET; 6530 } 6531 char *localpath = serverpath_to_localpath(foldertmp->href,index); 6532 char *prePath = get_prepath(localpath,strlen(localpath)); 6533 int exist = is_server_exist(prePath,localpath,index); 6534 if(exist) 6535 { 6536 if(NULL == opendir(localpath)) 6537 { 6538 add_action_item("createfolder",localpath,g_pSyncList[index]->server_action_list); 6539 mkdir(localpath,0777); 6540 } 6541 } 6542 free(prePath); 6543 free(localpath); 6544 } 6545 } 6546 //free(serverpathtmp); 6547 foldertmp = foldertmp->next; 6548 localfoldertmp = perform_lo->folderlist->next; 6549 } 6550 } 6551 6552 return ret; 6553} 6554 6555int parseCloudInfo_one(char *parentherf,int index) 6556{ 6557 FILE *fp; 6558 fp = fopen(LIST_ONE_DIR,"r"); 6559 char tmp[512]={0}; 6560 char buf[512]={0}; 6561 const char *split = " "; 6562 const char *split_2 = "\n"; 6563 char *str,*temp; 6564 char *p; 6565 while(fgets(buf,sizeof(buf),fp)!=NULL) 6566 { 6567 int fail = 0; 6568 DEBUG("%s\n",buf); 6569 FolderTmp_one=(CloudFile *)malloc(sizeof(struct node)); 6570 //memset(FolderTmp_one,0,sizeof(FolderTmp_one)); 6571 memset(FolderTmp_one,0,sizeof(struct node));//2014.10.28 by sherry sizeof(指针)=4 6572 p=strtok(buf,split); 6573 int i=0; 6574 while(p != NULL && i <= 8) 6575 { 6576 switch(i) 6577 { 6578 case 0: 6579 strcpy(tmp,p); 6580 //strcpy(FolderTmp_one->auth,p); 6581 FolderTmp_one->isfile=is_file(tmp); 6582 break; 6583 case 4: 6584 FolderTmp_one->size=atoll(p); 6585 break; 6586 case 5: 6587 //strcpy(FolderTmp_one->month,p); 6588 break; 6589 case 6: 6590 //strcpy(FolderTmp_one->day,p); 6591 break; 6592 case 7: 6593 //strcpy(FolderTmp_one->lastmodifytime,p); 6594 break; 6595 case 8: 6596// if(p == NULL || !strcmp(p,".") || !strcmp(p,"..")) 6597// { 6598// DEBUG("free iterm\n"); 6599// fail = 1; 6600// } 6601 str = (char*)malloc(sizeof(char)*(strlen(p) + 1)); 6602 strcpy(str,p); 6603 temp = to_utf8(str,index); 6604 free(str); 6605 strcpy(FolderTmp_one->filename,temp); 6606 break; 6607 default: 6608 break; 6609 } 6610 i++; 6611 if(i<=7) 6612 p=strtok(NULL,split); 6613 else 6614 p=strtok(NULL,split_2); 6615 } 6616 free(temp); 6617 FolderTmp_one->href = (char *)malloc(sizeof(char)*(strlen(parentherf)+1+1+strlen(FolderTmp_one->filename))); 6618 //memset(FolderTmp_one->href,'\0',sizeof(FolderTmp_one->href));//2014.10.28 by sherry sizeof(指针)=4 6619 memset(FolderTmp_one->href,'\0',sizeof(char)*(strlen(parentherf)+1+1+strlen(FolderTmp_one->filename))); 6620 if(strcmp(parentherf," ")==0){ 6621 sprintf(FolderTmp_one->href,"%s",FolderTmp_one->filename); 6622 } 6623 else{ 6624 sprintf(FolderTmp_one->href,"%s/%s",parentherf,FolderTmp_one->filename); 6625 } 6626 //if(!fail) 6627 //{ 6628 FileTail_one->next = FolderTmp_one; 6629 FileTail_one = FolderTmp_one; 6630 FileTail_one->next = NULL; 6631 //} 6632 //else 6633 //{ 6634 //free_CloudFile_item(FolderTmp_one); 6635 //} 6636 } 6637 fclose(fp); 6638} 6639 6640/* 6641 *1,server has the same file 6642 *0,server has no the same file 6643*/ 6644int is_server_exist(char *parentpath,char *filepath,int index) 6645{ 6646 DEBUG("####################is_server_exist...###############\n"); 6647 char *url = NULL; 6648 char *file_url = NULL; 6649 int status; 6650 FileList_one = (CloudFile *)malloc(sizeof(CloudFile)); 6651 memset(FileList_one,0,sizeof(CloudFile)); 6652 6653 FileList_one->href = NULL; 6654 FileTail_one = FileList_one; 6655 FileTail_one->next = NULL; 6656 6657 //2014.09.18 by sherry 6658 //未考虑深层目录的情况,出现指针乱指的现象 6659// if(!strcmp(parentpath,ftp_config.multrule[index]->hardware)) 6660// {// 如果传进的值就是hardware,那么走这里面的。 6661 6662// url = (char *)malloc(sizeof(char)*((strlen(parentpath) + 1))); 6663// sprintf(url,"%s",parentpath); 6664// } 6665// else 6666// { 6667// char *p = parentpath; 6668// p = p + ftp_config.multrule[index]->base_path_len; 6669// url = (char *)malloc(sizeof(char)*((strlen(ftp_config.multrule[index]->rooturl)+strlen(p) + 1))); 6670// sprintf(url,"%s%s",ftp_config.multrule[index]->rooturl,p); 6671 6672// } 6673 6674 if(!strcmp(parentpath,ftp_config.multrule[index]->hardware)) 6675 {// 如果传进的值就是hardware,那么走这里面的。 6676 6677 url = (char *)malloc(sizeof(char)*((strlen(parentpath) + 1))); 6678 sprintf(url,"%s",parentpath); 6679 } 6680 else 6681 { 6682 if(!strcmp(parentpath,ftp_config.multrule[index]->base_path)) 6683 { 6684 url = (char *)malloc(sizeof(char)*((strlen(ftp_config.multrule[index]->rooturl) + 1))); 6685 memset(url,0,sizeof(char)*((strlen(ftp_config.multrule[index]->rooturl) + 1))); 6686 sprintf(url,"%s",ftp_config.multrule[index]->rooturl); 6687 6688 } 6689 else 6690 { 6691 char *p = parentpath; 6692 p = p + ftp_config.multrule[index]->base_path_len; 6693 url = (char *)malloc(sizeof(char)*((strlen(ftp_config.multrule[index]->rooturl)+strlen(p) + 1))); 6694 memset(url,0,sizeof(char)*((strlen(ftp_config.multrule[index]->rooturl)+strlen(p) + 1)));//2014.10.28,by sherry 未初始化 6695 sprintf(url,"%s%s",ftp_config.multrule[index]->rooturl,p); 6696 6697 } 6698 } 6699 6700 do 6701 { 6702 status = getCloudInfo_one(url,parseCloudInfo_one,index); 6703 6704 }while(status == CURLE_COULDNT_CONNECT||status==CURLE_OPERATION_TIMEDOUT && exit_loop == 0); 6705 //while(status == CURLE_COULDNT_CONNECT && exit_loop == 0); //2014.11.07 by sherry 6706 6707 if(status != 0) 6708 { 6709 free(url); 6710 free_CloudFile_item(FileList_one); 6711 FileList_one = NULL; 6712 DEBUG("get Cloud Info One ERROR! \n"); 6713 //2014.11.07 by sherry 6714 //return status; 6715 return 0; 6716 } 6717 6718 CloudFile *de_filecurrent; 6719 de_filecurrent = FileList_one->next; 6720 6721 char *q = filepath; 6722 q = q + strlen(parentpath); 6723 file_url = (char*)malloc(sizeof(char)*(strlen(url) + strlen(q) + 1)); 6724 memset(file_url,'\0',sizeof(char)*(strlen(url) + strlen(q) + 1)); 6725 sprintf(file_url,"%s%s",url,q); 6726 DEBUG("file_url:%s\n",file_url); 6727 //DEBUG("filepath:%s\n",filepath); 6728 while(de_filecurrent != NULL) 6729 { 6730 if(de_filecurrent->href != NULL) 6731 { 6732 if(!(strcmp(de_filecurrent->href,file_url))) 6733 { 6734 DEBUG("get it.\n"); 6735 free(url); 6736 free(file_url); 6737 free_CloudFile_item(FileList_one); 6738 return 1; 6739 } 6740 } 6741 de_filecurrent = de_filecurrent->next; 6742 } 6743 DEBUG("not exist.\n"); 6744 free(url); 6745 free(file_url); 6746 free_CloudFile_item(FileList_one); 6747 return 0; 6748} 6749 6750void clear_global_var() 6751{ 6752 if(initial_tokenfile_info_data(&tokenfile_info_start) == NULL) 6753 { 6754 return; 6755 } 6756} 6757int check_disk_change() 6758{ 6759 int status = -1; 6760 disk_change = 0; 6761 status = check_sync_disk_removed(); 6762 6763 if(status == 2 || status == 1) 6764 { 6765 exit_loop = 1; 6766 //sync_up = 0; 6767 //sync_down = 0; 6768 sync_disk_removed = 1; 6769 } 6770 return 0; 6771} 6772 6773void sig_handler (int signum) 6774{ 6775 switch (signum) 6776 { 6777 case SIGTERM: 6778 case SIGUSR2: 6779 if(signum == SIGUSR2) 6780 { 6781 DEBUG("signal is SIGUSR2\n"); 6782 mountflag = 0; 6783 FILE *fp; 6784 fp = fopen("/proc/mounts","r"); 6785 if(fp == NULL) 6786 { 6787 DEBUG("open /proc/mounts fail\n"); 6788 } 6789 char a[1024]; 6790 while(!feof(fp))//检测流上的文件结束符,如果文件结束,则返回非0值,否则返回0 6791 {//当fp没有结束时 6792 memset(a,'\0',1024);//对a数组进行清零操作 6793 fscanf(fp,"%[^\n]%*c",a);//将fp读入到a数组, 6794 DEBUG("\nmount = %s\n",a); 6795 if(strstr(a,"/dev/sd")) 6796 { 6797 mountflag = 1; 6798 break; 6799 } 6800 } 6801 fclose(fp); 6802 } 6803 DEBUG("mountflag = %d\n",mountflag); 6804 if(signum == SIGTERM || mountflag == 0) 6805 { 6806 //sync_up = 0; 6807 //sync_down = 0; 6808 stop_progress = 1; 6809 //write_to_lock(stop_progress); 6810 exit_loop = 1; 6811 6812#ifndef NVRAM_ 6813 system("sh /tmp/smartsync/ftpclient/script/ftpclient_get_nvram"); 6814 sleep(2); 6815 if(create_ftp_conf_file(&ftp_config_stop) == -1) 6816 { 6817 DEBUG("create_webdav_conf_file fail\n"); 6818 return; 6819 } 6820#else 6821 if(convert_nvram_to_file_mutidir(CONFIG_PATH,&ftp_config_stop) == -1) 6822 { 6823 DEBUG("convert_nvram_to_file fail\n"); 6824 write_to_nvram("","ftp_tokenfile"); //the "" maybe cause errors 6825 return; 6826 } 6827#endif 6828 if(ftp_config_stop.dir_num == 0) 6829 { 6830 char *filename; 6831 del_acount = 0; 6832 filename = my_str_malloc(strlen(ftp_config.multrule[0]->mount_path) + 21 + 1); 6833 sprintf(filename,"%s/.ftpclient_tokenfile",ftp_config.multrule[0]->mount_path); 6834 remove(filename); 6835 free(filename); 6836#ifdef NVRAM_ 6837 write_to_nvram("","ftp_tokenfile"); //the "" maybe cause errors 6838#else 6839 write_to_ftp_tokenfile(""); 6840#endif 6841 } 6842 else 6843 { 6844 if(ftp_config_stop.dir_num != ftp_config.dir_num) 6845 { 6846 DEBUG("ftp_config_stop.dir_num = %d\n",ftp_config_stop.dir_num); 6847 DEBUG("ftp_config.dir_num = %d\n",ftp_config.dir_num); 6848 parse_config(CONFIG_PATH,&ftp_config_stop); 6849 //verify(ftp_config_stop); 6850 rewrite_tokenfile_and_nv(); 6851 } 6852 } 6853 sighandler_finished = 1; 6854 pthread_cond_signal(&cond); 6855 pthread_cond_signal(&cond_socket); 6856 pthread_cond_signal(&cond_log); 6857 } 6858 else 6859 { 6860 //pthread_mutex_lock(&mutex_checkdisk); 6861 disk_change = 1; 6862 //pthread_mutex_unlock(&mutex_checkdisk); 6863 sighandler_finished = 1; 6864 } 6865 break; 6866 case SIGUSR1: // add user 6867 DEBUG("signal is SIGUSER1\n"); 6868 click_apply = 1; 6869 break; 6870 default: 6871 break; 6872 } 6873} 6874 6875void* sigmgr_thread(void *argc) 6876{ 6877 DEBUG("\nsigmgr_thread:%u\n",pthread_self()); 6878 sigset_t waitset; 6879 int sig; 6880 int rc; 6881 6882 pthread_t ppid = pthread_self(); 6883 pthread_detach(ppid); 6884 6885 sigemptyset(&waitset); 6886 sigaddset(&waitset,SIGUSR1); 6887 sigaddset(&waitset,SIGUSR2); 6888 sigaddset(&waitset,SIGTERM); 6889 6890 while(1) 6891 { 6892 rc = sigwait(&waitset, &sig); 6893 if (rc != -1) 6894 { 6895 DEBUG("sigwait() fetch the signal - %d\n", sig); 6896 sig_handler(sig); 6897 }else{ 6898 DEBUG("sigwaitinfo() returned err: %d; %s\n", errno, strerror(errno)); 6899 } 6900 } 6901} 6902 6903long long stat_file(const char *filename) 6904{ 6905 //unsigned long size; 6906 struct stat filestat; 6907 if( stat(filename,&filestat) == -1) 6908 { 6909 perror("stat:"); 6910 return -1; 6911 //exit(1); 6912 } 6913 return filestat.st_size; 6914 6915 //return size; 6916 6917} 6918 6919void handle_quit_upload()//同一帐号,继续上传未上传完的文件 6920{ 6921 6922 DEBUG("###handle_quit_upload###\n"); 6923 int status; 6924 int i; 6925 for(i = 0; i<ftp_config.dir_num; i++) 6926 { 6927 status = usr_auth(ftp_config.multrule[i]->server_ip,ftp_config.multrule[i]->user_pwd); 6928 if(status == 7)//status=7表示couldn't connect to server 6929 { 6930 write_log(S_ERROR,"check your ip.","",ftp_config.multrule[i]->rules); 6931 continue; 6932 } 6933 else if(status == 67) 6934 { 6935 write_log(S_ERROR,"check your usrname or password.","",ftp_config.multrule[i]->rules); 6936 continue; 6937 } 6938 else if(status == 28) 6939 { 6940 write_log(S_ERROR,"connect timeout,check your ip or network.","",ftp_config.multrule[i]->rules); 6941 continue; 6942 } 6943 int flag = is_server_exist(ftp_config.multrule[i]->hardware, ftp_config.multrule[i]->rooturl, i); 6944 if(flag == CURLE_FTP_COULDNT_RETR_FILE) 6945 { 6946 write_log(S_ERROR,"Permission Denied.","",ftp_config.multrule[i]->rules); 6947 continue; 6948 } 6949 else if(flag == 0) 6950 { 6951 //write_log(S_ERROR,"remote root path lost OR write only.","",ftp_config.multrule[i]->rules); 6952 //continue; 6953 } 6954//#ifndef NVRAM_ 6955 if(g_pSyncList[i]->sync_disk_exist == 0) 6956 continue; 6957//#endif 6958 if(ftp_config.multrule[i]->rules != 1)//不是两条规则时 6959 { 6960 FILE *fp; 6961 long long filesize; 6962 char *buf; 6963 char *filepath; 6964 DEBUG("g_pSyncList[i]->up_item_file = %s\n",g_pSyncList[i]->up_item_file); 6965 if(access(g_pSyncList[i]->up_item_file,0) == 0) 6966 { 6967 filesize = stat_file(g_pSyncList[i]->up_item_file); 6968 fp = fopen(g_pSyncList[i]->up_item_file,"r"); 6969 if(fp == NULL) 6970 { 6971 DEBUG("open %s error\n",g_pSyncList[i]->up_item_file); 6972 return; 6973 } 6974 buf = my_str_malloc((size_t)(filesize + 1)); 6975 fscanf(fp,"%s",buf); 6976 fclose(fp); 6977 6978 if((strlen(buf)) <= 1) 6979 { 6980 return ; 6981 } 6982 DEBUG("buf:%s\n",buf); 6983 unlink(g_pSyncList[i]->up_item_file); 6984 6985 //filepath = my_str_malloc((size_t)(filesize + ftp_config.multrule[i]->base_path_len + strlen(buf) + 1)); 6986 //sprintf(filepath,"%s%s",ftp_config.multrule[i]->base_path,buf); 6987 //free(buf); 6988 //DEBUG("buf:%s\n",buf); 6989 Delete(buf,i); 6990 status = upload(buf,i); 6991 6992 if(status != 0) 6993 { 6994 DEBUG("upload %s failed\n",buf); 6995 } 6996 else 6997 { 6998 char *serverpath = localpath_to_serverpath(buf,i); 6999 DEBUG("serverpath = %s\n",serverpath); 7000 mod_time *onefiletime = Getmodtime(serverpath,i); 7001 if(ChangeFile_modtime(buf,onefiletime->modtime,i)) 7002 { 7003 DEBUG("ChangeFile_modtime failed!\n"); 7004 } 7005 free(serverpath); 7006 } 7007 7008 free(buf); 7009 } 7010 } 7011 } 7012 7013} 7014 7015int initialization() 7016{ 7017 int ret = 0; 7018 int status; 7019 int i; 7020 DEBUG("############################ftp_config.dir_num = %d\n",ftp_config.dir_num); 7021 for(i = 0; i < ftp_config.dir_num; i++) 7022 { 7023 if(exit_loop) 7024 break; 7025 if(disk_change) 7026 { 7027 //disk_change = 0; 7028 check_disk_change(); 7029 } 7030 7031 status = usr_auth(ftp_config.multrule[i]->server_ip,ftp_config.multrule[i]->user_pwd); 7032 if(status == 7) 7033 { 7034 write_log(S_ERROR,"check your ip.","",ftp_config.multrule[i]->rules); 7035 continue; 7036 } 7037 else if(status == 67) 7038 { 7039 write_log(S_ERROR,"check your usrname or password.","",ftp_config.multrule[i]->rules); 7040 continue; 7041 } 7042 else if(status == 28) 7043 { 7044 write_log(S_ERROR,"connect timeout,check your ip or network.","",ftp_config.multrule[i]->rules); 7045 continue; 7046 } 7047 int flag = is_server_exist(ftp_config.multrule[i]->hardware, ftp_config.multrule[i]->rooturl, i); 7048 if(flag == CURLE_FTP_COULDNT_RETR_FILE) 7049 { 7050 write_log(S_ERROR,"Permission Denied.","",ftp_config.multrule[i]->rules); 7051 continue; 7052 } 7053 else if(flag == 0) 7054 { 7055 //write_log(S_ERROR,"remote root path lost OR write only.","",ftp_config.multrule[i]->rules); 7056 //continue; 7057 } 7058 7059 if(g_pSyncList[i]->sync_disk_exist == 0) 7060 { 7061 write_log(S_ERROR,"Sync disk unplug!","",i); 7062 continue; 7063 } 7064 7065 if(g_pSyncList[i]->init_completed) 7066 continue; 7067 7068 //2104.11.04 by sherry server权限不足,error,finish,initial三种状态交替 7069 write_log(S_INITIAL,"","",i); 7070// if(!g_pSyncList[i]->IsPermission && !exit_loop)//有权限 7071// { 7072// write_log(S_INITIAL,"","",i); 7073// } 7074 ret = 1; 7075 DEBUG("it is %d init \n",i); 7076 7077 if(exit_loop == 0) 7078 { 7079 DEBUG("wd_initial ret = %d\n",ret); 7080 free_server_tree(g_pSyncList[i]->ServerRootNode); 7081 g_pSyncList[i]->ServerRootNode = NULL; 7082 7083 g_pSyncList[i]->ServerRootNode = create_server_treeroot(); 7084 //DEBUG("%s\n",ftp_config.multrule[i]->rooturl); 7085 status = browse_to_tree(ftp_config.multrule[i]->rooturl,g_pSyncList[i]->ServerRootNode,i); 7086 DEBUG("%d\n",status); 7087 7088 if(status != 0) 7089 continue; 7090 7091 if(exit_loop == 0) 7092 { 7093 if(test_if_dir_empty(ftp_config.multrule[i]->base_path)) 7094 { 7095 DEBUG("base_path is blank\n"); 7096 7097 if(ftp_config.multrule[i]->rules != 2) 7098 { 7099 if(g_pSyncList[i]->ServerRootNode->Child != NULL) 7100 { 7101 DEBUG("######## init sync folder,please wait......#######\n"); 7102 7103 ret = initMyLocalFolder(g_pSyncList[i]->ServerRootNode->Child,i); 7104 printf("ret1 = %d\n", ret); 7105 if(ret != 0) 7106 continue; 7107 g_pSyncList[i]->init_completed = 1; 7108 g_pSyncList[i]->OldServerRootNode = g_pSyncList[i]->ServerRootNode; 7109 DEBUG("######## init sync folder end#######\n"); 7110 } 7111 } 7112 else 7113 { 7114 g_pSyncList[i]->init_completed = 1; 7115 g_pSyncList[i]->OldServerRootNode = g_pSyncList[i]->ServerRootNode; 7116 ret = 0; 7117 } 7118 } 7119 else 7120 { 7121 DEBUG("######## init sync folder(have files),please wait......#######\n"); 7122 if(g_pSyncList[i]->ServerRootNode->Child != NULL) 7123 { 7124 DEBUG("########sync_server_to_local\n"); 7125 ret = sync_server_to_local(g_pSyncList[i]->ServerRootNode->Child,sync_server_to_local_init,i); 7126 printf("ret2 = %d\n", ret); 7127 if(ret != 0) 7128 continue; 7129 } 7130 ret = sync_local_to_server(ftp_config.multrule[i]->base_path,i); 7131 printf("ret3 = %d\n", ret); 7132 if(ret != 0) 7133 continue; 7134 DEBUG("#########ret = %d\n",ret); 7135 DEBUG("######## init sync folder end#######\n"); 7136 g_pSyncList[i]->init_completed = 1; 7137 g_pSyncList[i]->OldServerRootNode = g_pSyncList[i]->ServerRootNode; 7138 } 7139 } 7140 } 7141 if(ret == 0) 7142 { 7143 write_log(S_SYNC,"","",i); 7144 } 7145 } 7146 return ret; 7147} 7148 7149void run() 7150{ 7151 //init_global_var(); 7152 send_to_inotify(); 7153 handle_quit_upload();//处理上次上传中断的动作(估计是U盘拔掉啥的,程序挂掉) 7154 7155 int create_thid1 = 0; 7156 if(exit_loop == 0) 7157 { 7158 if(pthread_create(&newthid1,NULL,Save_Socket,NULL) != 0) 7159 { 7160 DEBUG("thread creation failed!\n"); 7161 exit(1); 7162 } 7163 usleep(1000*500); 7164 create_thid1 = 1; 7165 } 7166 7167 int create_thid2 = 0; 7168 //if(exit_loop == 0 && browse_sev == -1) 7169 if(exit_loop == 0) 7170 { 7171 if(pthread_create(&newthid2,NULL,Socket_Parser,NULL) != 0) 7172 { 7173 DEBUG("thread creation failed!\n"); 7174 exit(1); 7175 } 7176 usleep(1000*500); 7177 create_thid2 = 1; 7178 } 7179 //DEBUG("!!!!!!!!!!!!! start initialization()\n"); 7180 //by 7181 initialization(); 7182 // DEBUG("!!!!!!!!!!!!! end initialization()\n"); 7183 int create_thid3 = 0; 7184 //if(exit_loop == 0 && browse_sev == -1) 7185 7186//by 7187 if(exit_loop == 0) 7188 { 7189 if(pthread_create(&newthid3,NULL,SyncServer,NULL) != 0) 7190 { 7191 DEBUG("thread creation failed!\n"); 7192 exit(1); 7193 } 7194 //usleep(1000*500); 7195 create_thid3 = 1; 7196 } 7197 7198 if(create_thid1) 7199 { 7200 pthread_join(newthid1,NULL); 7201 DEBUG("newthid1 has stoped!\n"); 7202 } 7203 7204 if(create_thid2) 7205 { 7206 pthread_join(newthid2,NULL); 7207 DEBUG("newthid2 has stoped!\n"); 7208 } 7209 7210 if(create_thid3) 7211 { 7212 pthread_join(newthid3,NULL); 7213 DEBUG("newthid3 has stoped!\n"); 7214 } 7215 7216 usleep(1000); 7217 //clean_up(); 7218 7219 if(stop_progress != 1) 7220 { 7221 DEBUG("run again!\n"); 7222 DEBUG("disk_change:%d\n",disk_change); 7223 while(disk_change) 7224 { 7225 disk_change = 0; 7226 sync_disk_removed = check_sync_disk_removed(); 7227 7228 if(sync_disk_removed == 2) 7229 { 7230 DEBUG("sync path is change\n"); 7231 } 7232 else if(sync_disk_removed == 1) 7233 { 7234 DEBUG("sync disk is unmount\n"); 7235 } 7236 else if(sync_disk_removed == 0) 7237 { 7238 DEBUG("sync disk exists\n"); 7239 } 7240 } 7241 7242 exit_loop = 0; 7243 init_global_var(); 7244 run(); 7245 } 7246 7247} 7248 7249 7250void stop_process_clean_up() 7251{ 7252 //unlink("/tmp/smartsync_app/ftpclient_start"); 7253 7254 unlink("/tmp/notify/usb/ftp_client"); 7255 pthread_cond_destroy(&cond); 7256 pthread_cond_destroy(&cond_socket); 7257 pthread_cond_destroy(&cond_log); 7258 unlink(LOG_DIR); 7259} 7260 7261//int create_xml(int status) 7262//{ 7263// char buf1[512] = {0}; 7264// char buf2[512] = {0}; 7265// char LOG[512] = {0}; 7266// 7267// FILE *stream = fopen(SERVERLIST_TD,"a"); 7268// if(!access(SERVERLIST_LOG,F_OK)) 7269// { 7270// FILE *my_fp = fopen(SERVERLIST_LOG,"r"); 7271// if(fgets(LOG,sizeof(LOG),my_fp) != NULL) 7272// { 7273// if(LOG[ strlen(LOG)-1 ] == '\n') 7274// LOG[ strlen(LOG)-1 ] = '\0'; 7275// } 7276// fprintf(stream,"<root stat=\"%s\"></root>",LOG); 7277// fclose(stream); 7278// rename(SERVERLIST_TD,SERVERLIST_XML); 7279// return 0; 7280// } 7281//// if(status == -1) 7282//// { 7283//// fprintf(stream,"<root stat=\"Error\"></root>"); 7284//// fclose(stream); 7285//// rename(SERVERLIST_TD,SERVERLIST_XML); 7286//// return 0; 7287//// } 7288// FILE *fp1 = fopen(SERVERLIST_1,"r"); 7289// fprintf(stream,"<root stat=\"OK\">"); 7290// while(fgets(buf1,sizeof(buf1),fp1) != NULL) 7291// { 7292// if(buf1[ strlen(buf1)-1 ] == '\n') 7293// buf1[ strlen(buf1)-1 ] = '\0'; 7294// char *temp1 = (char*)malloc(sizeof(char)*(strlen(buf1) + 1)); 7295// memset(temp1,'\0',sizeof(char)*(strlen(buf1) + 1)); 7296// sprintf(temp1,"%s",buf1); 7297// char *p = temp1 + strlen(temp1) - 1; 7298// int i = 0; 7299// while(p[0] != '/' && i < (strlen(temp1)-1)) 7300// { 7301// i++; 7302// p--; 7303// } 7304// p = (p[0] == '/') ? (p + 1) : p; 7305// fprintf(stream,"<fold0 name=\"%s\">",p); 7306// FILE *fp2 = fopen(SERVERLIST_2,"r"); 7307// while(fgets(buf2,sizeof(buf2),fp2) != NULL) 7308// { 7309// if(buf2[ strlen(buf2)-1 ] == '\n') 7310// buf2[ strlen(buf2)-1 ] = '\0'; 7311// char *temp2 = (char*)malloc(sizeof(char)*(strlen(buf2) + 1)); 7312// memset(temp2,'\0',sizeof(char)*(strlen(buf2) + 1)); 7313// sprintf(temp2,"%s",buf2); 7314// if(strncmp(buf2,buf1,strlen(buf1)) == 0) 7315// { 7316// fprintf(stream,"<fold1 name=\"%s\">",temp2 + strlen(buf1) + 1); 7317// fprintf(stream,"</fold1>"); 7318// } 7319// free(temp2); 7320// } 7321// fclose(fp2); 7322// fprintf(stream,"</fold0>"); 7323// free(temp1); 7324// } 7325// fprintf(stream,"</root>"); 7326// fclose(fp1); 7327// fclose(stream); 7328// rename(SERVERLIST_TD,SERVERLIST_XML); 7329// return 0; 7330//} 7331 7332char *program_name; 7333char *login; 7334 7335int main(int argc,char **argv) 7336{ 7337 program_name=argv[0]; 7338 7339 //create_start_file(); 7340 no_config = 0; 7341 exit_loop = 0; 7342 stop_progress = 0; 7343 7344 my_local_mkdir("/tmp/smartsync"); 7345 my_local_mkdir("/tmp/smartsync/.logs"); 7346 my_local_mkdir("/tmp/smartsync/ftpclient"); 7347 my_local_mkdir("/tmp/smartsync/ftpclient/config"); 7348 my_local_mkdir("/tmp/smartsync/ftpclient/script"); 7349 my_local_mkdir("/tmp/smartsync/ftpclient/temp"); 7350 7351 sigset_t bset,oset; 7352 pthread_t sig_thread; 7353 7354 sigemptyset(&bset);//用来将参数bset信号集初始化并清空 7355 sigaddset(&bset,SIGUSR1);//用来将参数"SIGUSR1(此为信号种类)"信号加入至参数bset信号集里 7356 sigaddset(&bset,SIGUSR2); 7357 sigaddset(&bset,SIGTERM); 7358 7359 if( pthread_sigmask(SIG_BLOCK,&bset,&oset) == -1)//在多线程的程序里,希望只在主线程中处理信号, 7360 //用作在主调线程里控制信号掩码 7361 DEBUG("!! Set pthread mask failed\n"); 7362 7363 if( pthread_create(&sig_thread,NULL,sigmgr_thread,NULL) != 0)//创建信号的线程 7364 { 7365 DEBUG("thread creation failder\n"); 7366 exit(-1); 7367 } 7368 clear_global_var(); 7369 7370#ifndef _PC 7371 write_notify_file(NOTIFY_PATH,SIGUSR2); 7372 7373 while(detect_process("nvram")) 7374 { 7375 sleep(2); 7376 } 7377 create_shell_file(); 7378#ifndef NVRAM_ 7379 my_local_mkdir("/opt/etc/.smartsync"); 7380 write_get_nvram_script("cloud_sync",GET_NVRAM,TMP_CONFIG);// 7381 system("sh /tmp/smartsync/ftpclient/script/ftpclient_get_nvram"); 7382 sleep(2); 7383 write_get_nvram_script("link_internet",GET_INTERNET,VAL_INTERNET);// 7384#endif 7385#endif 7386 7387 read_config(); 7388 //DEBUG("browse_sev:%d\n",browse_sev); 7389 init_global_var(); 7390 7391#ifdef _PC 7392 7393#else 7394 check_link_internet(); 7395#endif 7396 if(no_config == 0) 7397 run(); 7398 7399 pthread_join(sig_thread,NULL); 7400 stop_process_clean_up(); 7401 7402 7403// if(detect_process_file()) 7404// { 7405// DEBUG("ftpclient did not kill inotify\n"); 7406// } 7407// else 7408// { 7409// system("killall -9 inotify &"); 7410// DEBUG("ftpclient kill inotify\n"); 7411// } 7412// if(!detect_process("asuswebstorage") && !detect_process("webdav_client") && !detect_process("dropbox_client")) 7413// { 7414// system("killall -9 inotify &"); 7415// DEBUG("ftpclient kill inotify\n"); 7416// } 7417// else 7418// { 7419// DEBUG("ftpclient did not kill inotify\n"); 7420// } 7421 DEBUG("FtpClient stop.\n"); 7422 return 0; 7423} 7424