1#include "base.h"
2
3extern Config ftp_config,ftp_config_stop;
4extern sync_list **g_pSyncList;
5extern int browse_sev;
6extern int my_index;
7
8void replace_char_in_str(char *str,char newchar,char oldchar){
9
10    int i;
11    int len;
12    len = strlen(str);
13    for(i=0;i<len;i++)
14    {
15        if(str[i] == oldchar)
16        {
17            str[i] = newchar;
18        }
19    }
20
21}
22
23void my_local_mkdir(char *path)//���������������(path)������������������������
24{
25    //char error_message[256];
26    DIR *dir;
27    if(NULL == (dir = opendir(path)))
28    {
29        if(-1 == mkdir(path,0777)) //mkdir(path,0777)���������������path������������������
30            //0777���������������������������������������������������������������������������������������������������������
31        {
32            DEBUG("please check disk can write or dir has exist???");
33            DEBUG("mkdir %s fail\n",path);
34            return;
35        }
36    }
37    else
38        closedir(dir);
39}
40
41int is_server_have_localpath(char *path,Server_TreeNode *treenode,int index)
42{
43    if(treenode == NULL)
44        return 0;
45
46    //char *hreftmp;
47    char *localpath;
48    int ret = 0;
49    int cmp = 1;
50
51    localpath = serverpath_to_localpath(treenode->parenthref,index);
52
53    if((cmp = strcmp(localpath,path)) == 0)
54    {
55        ret = 1;
56        free(localpath);
57        return ret;
58    }
59    else
60    {
61        free(localpath);
62    }
63
64    if(treenode->Child != NULL)
65    {
66        ret = is_server_have_localpath(path,treenode->Child,index);
67        if(ret == 1)
68        {
69            return ret;
70        }
71    }
72    if(treenode->NextBrother != NULL)
73    {
74        ret = is_server_have_localpath(path,treenode->NextBrother,index);
75        if(ret == 1)
76        {
77            return ret;
78        }
79    }
80
81    return ret;
82}
83
84Server_TreeNode * getoldnode(Server_TreeNode *tempoldnode,char *href)
85{
86    //Server_TreeNode *tempoldnode;
87    Server_TreeNode *retnode;
88    //tempoldnode = NULL;
89    retnode = NULL;
90
91    //DEBUG("getoldnode tempoldnode->parenthref -> %s   %d\n",tempoldnode->parenthref,strlen(tempoldnode->parenthref));
92    //DEBUG("                              href -> %s   %d\n",href,strlen(href));
93
94    if(strcmp(href,tempoldnode->parenthref) == 0)
95    {
96        //DEBUG("*****get the retnode\n");
97        retnode = tempoldnode;
98        return retnode;
99    }
100
101    if(tempoldnode->Child != NULL)
102    {
103        retnode = getoldnode(tempoldnode->Child,href);
104        if(retnode != NULL)
105        {
106            return retnode;
107        }
108    }
109    if(tempoldnode->NextBrother != NULL)
110    {
111        retnode = getoldnode(tempoldnode->NextBrother,href);
112        if(retnode != NULL)
113        {
114            return retnode;
115        }
116    }
117    return retnode;
118}
119
120int get_path_to_index(char *path)
121{
122    DEBUG("%s\n",path);
123    int i;
124    char *root_path = NULL;
125    char *temp = NULL;
126    root_path = my_str_malloc(512);
127
128    temp = my_nstrchr('/',path,5);
129    if(temp == NULL)
130    {
131        sprintf(root_path,"%s",path);
132    }
133    else
134    {
135        snprintf(root_path,strlen(path)-strlen(temp)+1,"%s",path);
136    }
137
138    for(i=0;i<ftp_config.dir_num;i++)
139    {
140        if(!strcmp(root_path,ftp_config.multrule[i]->base_path))
141            break;
142    }
143    DEBUG("get_path_to_index root_path = %s\t%d\n",root_path,i);
144
145    free(root_path);
146
147    return i;
148}
149
150char *change_server_same_name(char *fullname,int index)
151{
152    DEBUG("###################change server same name...##################\n");
153    int i = 1;
154    int exist;
155    char *filename = NULL;
156    char *temp_name = NULL;
157    int len = 0;
158    char *path;
159    char newfilename[512];
160    int exit = 1;
161    int lens = 0;
162    char tail[512];
163
164    char *fullname_tmp = NULL;
165    fullname_tmp = my_str_malloc(strlen(fullname)+1);
166    sprintf(fullname_tmp,"%s",fullname);
167
168    DEBUG("%s\n",fullname);
169    char *temp = get_prepath(fullname_tmp,strlen(fullname_tmp));
170    filename = (char *)malloc(sizeof(char)*(strlen(fullname_tmp) - strlen(temp) + 1));
171    sprintf(filename,"%s",fullname_tmp + strlen(temp));
172    //filename = fullname_tmp + strlen(temp);
173    free(temp);
174    //len = strlen(fullname_tmp);//2014.11.11 by sherry
175    path = get_prepath(fullname,strlen(fullname));
176
177    free(fullname_tmp);
178
179    while(exit)
180    {
181        int n = i;
182        int j = 0;
183        while((n=(n/10)))
184        {
185            j++;
186        }
187        memset(newfilename,'\0',sizeof(newfilename));
188        snprintf(newfilename,252-j,"%s",filename);
189        lens = get_prefix(newfilename,strlen(newfilename));
190        sprintf(tail,"(%d)",i);
191
192        string newname(newfilename),temp(tail);//������������������
193        newname = newname.insert(lens,temp);
194        //sprintf(newfilename,"%s(%d)",newfilename,i);
195        memset(newfilename,'\0',sizeof(newfilename));
196        //strcpy(newfilename,newname.c_str());
197        sprintf(newfilename,"%s",newname.c_str());
198
199        //newname.~string();
200        //temp.~string();
201        DEBUG("newfilename = %s\n",newfilename);
202        i++;
203
204        temp_name = (char *)malloc(sizeof(char)*(strlen(path) + strlen(newfilename) + 1));
205        sprintf(temp_name,"%s%s",path,newfilename);
206        DEBUG("temp_name = %s\n",temp_name);
207
208        exist = is_server_exist(path,temp_name,index);
209
210        if(exist)
211        {
212            //free(path);
213            free(temp_name);
214        }
215        else
216        {
217            exit = 0;
218            //return temp_name;
219        }
220    }
221    free(path);
222    char *tmp = get_prepath(temp_name,strlen(temp_name));
223    //2014.11.11 by sherry  free���������������������������������������������������
224    //temp_name = temp_name + strlen(tmp) + 1;
225    char *temp_name1=NULL;
226    temp_name1=my_str_malloc(strlen(temp_name)-strlen(tmp)+1);
227    sprintf(temp_name1,"%s",temp_name + strlen(tmp) + 1);
228
229    free(tmp);
230    free(filename);
231    free(temp_name);//2014.11.11 by sherry
232
233//    temp_name = temp_name + strlen(get_prepath(temp_name,strlen(temp_name))) + 1;
234    return temp_name1;//2014.11.11 by sherry
235}
236
237char *change_local_same_name(char *fullname)
238{
239    int i = 1;
240    char *temp_name = NULL;
241    int len = 0;
242    char *path;
243    char newfilename[256];
244    int lens = 0;
245    char tail[512];
246
247    char *fullname_tmp = NULL;
248    fullname_tmp = my_str_malloc(strlen(fullname)+1);
249    sprintf(fullname_tmp,"%s",fullname);
250
251    char *temp = get_prepath(fullname_tmp,strlen(fullname_tmp));
252    char *filename = fullname_tmp + strlen(temp);
253    free(temp);
254    len = strlen(filename);
255    DEBUG("filename len is %d\n",len);
256    path = my_str_malloc((size_t)(strlen(fullname)-len+1));
257    DEBUG("fullname = %s\n",fullname);
258    snprintf(path,strlen(fullname)-len+1,"%s",fullname);
259    DEBUG("path = %s\n",path);
260    //free(fullname_tmp);
261
262    while(1)
263    {
264        int n = i;
265        int j = 0;
266        while((n=(n/10)))
267        {
268            j++;
269        }
270        memset(newfilename,'\0',sizeof(newfilename));
271        snprintf(newfilename,252-j,"%s",filename);
272        lens = get_prefix(newfilename,strlen(newfilename));
273        sprintf(tail,"(%d)",i);
274        string newname(newfilename),temp(tail);
275        newname = newname.insert(lens,temp);
276        //sprintf(newfilename,"%s(%d)",newfilename,i);
277        memset(newfilename,'\0',sizeof(newfilename));
278        sprintf(newfilename,"%s",newname.c_str());
279
280        DEBUG("newfilename = %s\n",newfilename);
281        i++;
282
283        temp_name = my_str_malloc((size_t)(strlen(path)+strlen(newfilename)+1));
284        sprintf(temp_name,"%s%s",path,newfilename);
285
286        if(access(temp_name,F_OK) != 0)
287        {
288            //free(path);
289            //return temp_name;
290            break;
291        }
292        else
293            free(temp_name);
294    }
295    free(path);
296    free(fullname_tmp);
297    //free(filename);
298    return temp_name;
299}
300
301/*
302 *judge is server changed
303 *0:server changed
304 *1:server is not changed
305*/
306int isServerChanged(Server_TreeNode *newNode,Server_TreeNode *oldNode)
307{
308    int res = 1;
309    int serverchanged = 0;
310    if(newNode->browse == NULL && oldNode->browse == NULL)
311    {
312        DEBUG("########Server is not change\n");
313        return 1;
314    }
315    else if(newNode->browse == NULL && oldNode->browse != NULL)
316    {
317        DEBUG("########Server changed1\n");
318        return 0;
319    }
320    else if(newNode->browse != NULL && oldNode->browse == NULL)
321    {
322        DEBUG("########Server changed2\n");
323        return 0;
324    }
325    else
326    {
327        if(newNode->browse->filenumber != oldNode->browse->filenumber || newNode->browse->foldernumber != oldNode->browse->foldernumber)
328        {
329            DEBUG("########Server changed3\n");
330            return 0;
331        }
332        else
333        {
334            int cmp;
335            CloudFile *newfoldertmp = NULL;
336            CloudFile *oldfoldertmp = NULL;
337            CloudFile *newfiletmp = NULL;
338            CloudFile *oldfiletmp = NULL;
339            if(newNode->browse != NULL)
340            {
341                if(newNode->browse->foldernumber > 0)
342                    newfoldertmp = newNode->browse->folderlist->next;
343                if(newNode->browse->filenumber > 0)
344                    newfiletmp = newNode->browse->filelist->next;
345            }
346            if(oldNode->browse != NULL)
347            {
348                if(oldNode->browse->foldernumber > 0)
349                    oldfoldertmp = oldNode->browse->folderlist->next;
350                if(oldNode->browse->filenumber > 0)
351                    oldfiletmp = oldNode->browse->filelist->next;
352            }
353
354            while (newfoldertmp != NULL || oldfoldertmp != NULL)
355            {
356                if ((cmp = strcmp(newfoldertmp->href,oldfoldertmp->href)) != 0){
357                    DEBUG("########Server changed4\n");
358                    return 0;
359                }
360                newfoldertmp = newfoldertmp->next;
361                oldfoldertmp = oldfoldertmp->next;
362            }
363            while (newfiletmp != NULL || oldfiletmp != NULL)
364            {
365                if ((cmp = strcmp(newfiletmp->href,oldfiletmp->href)) != 0)
366                {
367                    DEBUG("########Server changed5\n");
368                    return 0;
369                }
370                if (newfiletmp->modtime != oldfiletmp->modtime)
371                {
372                    DEBUG("########Server changed6\n");
373                    return 0;
374                }
375                newfiletmp = newfiletmp->next;
376                oldfiletmp = oldfiletmp->next;
377            }
378        }
379
380        if((newNode->Child == NULL && oldNode->Child != NULL) || (newNode->Child != NULL && oldNode->Child == NULL))
381        {
382            DEBUG("########Server changed7\n");
383            return 0;
384        }
385        if((newNode->NextBrother == NULL && oldNode->NextBrother != NULL) || (newNode->NextBrother!= NULL && oldNode->NextBrother == NULL))
386        {
387            DEBUG("########Server changed8\n");
388            return 0;
389        }
390
391        if(newNode->Child != NULL && oldNode->Child != NULL)
392        {
393            res = isServerChanged(newNode->Child,oldNode->Child);
394            if(res == 0)
395            {
396                serverchanged = 1;
397            }
398        }
399        if(newNode->NextBrother != NULL && oldNode->NextBrother != NULL)
400        {
401            res = isServerChanged(newNode->NextBrother,oldNode->NextBrother);
402            if(res == 0)
403            {
404                serverchanged = 1;
405            }
406        }
407    }
408    if(serverchanged == 1)
409    {
410        DEBUG("########Server changed9\n");
411        return 0;
412    }
413    else
414    {
415        return 1;
416    }
417}
418
419/*
420 *if a = 0x1,find in folderlist
421 *if a = 0x2,find in filelist
422 *if a = 0x3,find in folderlist and filelist
423*/
424CloudFile *get_CloudFile_node(Server_TreeNode* treeRoot,const char *dofile_href,int a)
425{
426    DEBUG("****get_CloudFile_node****dofile_href = %s\n",dofile_href);
427    int href_len = strlen(dofile_href);
428    CloudFile *finded_file = NULL;
429    if(treeRoot == NULL)
430    {
431        return NULL;
432    }
433
434    if((treeRoot->Child!=NULL))
435    {
436        finded_file = get_CloudFile_node(treeRoot->Child,dofile_href,a);
437        if(finded_file != NULL)
438        {
439            return finded_file;
440        }
441    }
442
443    if(treeRoot->NextBrother != NULL)
444    {
445        finded_file = get_CloudFile_node(treeRoot->NextBrother,dofile_href,a);
446        if(finded_file != NULL)
447        {
448            return finded_file;
449        }
450    }
451
452    if(treeRoot->browse != NULL)
453    {
454        int int_folder = 0x1;
455        int int_file = 0x2;
456        CloudFile *de_foldercurrent = NULL;
457        CloudFile *de_filecurrent = NULL;
458        DEBUG("111111folder = %d,file = %d\n",treeRoot->browse->foldernumber,treeRoot->browse->filenumber);
459        if(treeRoot->browse->foldernumber > 0)
460            de_foldercurrent = treeRoot->browse->folderlist->next;
461        if(treeRoot->browse->filenumber > 0)
462            de_filecurrent = treeRoot->browse->filelist->next;
463        if((a&int_folder) && de_foldercurrent != NULL)
464        {
465            while(de_foldercurrent != NULL)
466            {
467                if(de_foldercurrent->href != NULL)
468                {
469                    DEBUG("de_foldercurrent->href = %s\n",de_foldercurrent->href);
470                    if(!(strncmp(de_foldercurrent->href,dofile_href,href_len)))
471                    {
472                        return de_foldercurrent;
473                    }
474                }
475                de_foldercurrent = de_foldercurrent->next;
476            }
477        }
478        if((a&int_file) && de_filecurrent != NULL)
479        {
480            while(de_filecurrent != NULL)
481            {
482                if(de_filecurrent->href != NULL)
483                {
484                    DEBUG("de_filecurrent->href = %s\n",de_filecurrent->href);
485                    if(!(strncmp(de_filecurrent->href,dofile_href,href_len)))
486                    {
487                        DEBUG("get it\n");
488                        return de_filecurrent;
489                    }
490                }
491                de_filecurrent = de_filecurrent->next;
492            }
493        }
494    }
495    return finded_file;
496}
497
498void del_all_items(char *dir,int index)
499{
500    struct dirent* ent = NULL;
501    DIR *pDir;
502    pDir=opendir(dir);
503
504    if(pDir != NULL )
505    {
506        while (NULL != (ent=readdir(pDir)))
507        {
508            if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,".."))
509                continue;
510
511            char *fullname;
512            size_t len;
513            len = strlen(dir)+strlen(ent->d_name)+2;
514            fullname = my_str_malloc(len);
515            sprintf(fullname,"%s/%s",dir,ent->d_name);
516
517            if(test_if_dir(fullname) == 1)
518            {
519                wait_handle_socket(index);
520                del_all_items(fullname,index);
521            }
522            else
523            {
524                wait_handle_socket(index);
525                add_action_item("remove",fullname,g_pSyncList[index]->server_action_list);
526                remove(fullname);
527            }
528
529            free(fullname);
530        }
531        closedir(pDir);
532
533        add_action_item("remove",dir,g_pSyncList[index]->server_action_list);
534        remove(dir);
535    }
536    else
537        DEBUG("open %s fail \n",dir);
538}
539
540char *get_prepath(char *temp,int len)
541{
542    //2014.09.26 by sherry
543    //������������������������
544//    char *p = temp;
545//    char *q = temp;
546//    int count = 0,count_1 = 0;
547//    while(strlen(p) != 0) //p ���������������������������
548//    {
549//        if(p[0] == '/')
550//        {
551//            count++;
552//        }
553//        p++;
554//    }
555//    while(strlen(q) != 0) //q ���������������������'/'
556//    {
557//        if(q[0] == '/')
558//        {
559//            count_1++;
560//            if(count_1 == count)
561//            {
562//                break;
563//            }
564//        }
565//        q++;
566//    }
567    char *q;
568    q=strrchr(temp,'/');
569    if( q == NULL)
570    {
571        return NULL;
572    }
573
574    char *ret = (char *)malloc(sizeof(char)*(len - strlen(q) + 1));
575    memset(ret,'\0',sizeof(char)*(len - strlen(q) + 1));
576    strncpy(ret,temp,len-strlen(q));
577    return ret;
578}
579
580int get_prefix(char *temp,int len)
581{
582//    char *p = temp;
583//    char *q = temp;
584//    int count = 0,count_1 = 0;
585//    while(strlen(p) != 0)
586//    {
587//        if(p[0] == '.')
588//        {
589//            count++;
590//        }
591//        p++;
592//    }
593//    while(strlen(q) != 0)
594//    {
595//        if(q[0] == '.')
596//        {
597//            count_1++;
598//            if(count_1 == count)
599//            {
600//                break;
601//            }
602//        }
603//        q++;
604//    }
605    //2014.11.06 by sherry ������������
606    char *q;
607    q=strrchr(temp,'.');
608    if(q==NULL)
609        return len;
610    return len-strlen(q);
611}
612
613/*free������������������������������������������*/
614void free_localfloor_node(Local *local)
615{
616    free_LocalFile_item(local->filelist);
617    free_LocalFolder_item(local->folderlist);
618    free(local);
619}
620
621void free_LocalFolder_item(LocalFolder *head)
622{
623    LocalFolder *p = head;
624    while(p != NULL)
625    {
626        head = head->next;
627        if(p->path != NULL)
628        {
629            //DEBUG("free LocalFolder %s\n",point->path);
630            free(p->path);
631        }
632        free(p);
633        p = head;
634    }
635}
636
637void free_LocalFile_item(LocalFile *head)
638{
639    LocalFile *p = head;
640    while(p != NULL)
641    {
642        head = head->next;
643        if(p->path != NULL)
644        {
645            //DEBUG("free LocalFile %s\n",point->path);
646            free(p->path);
647        }
648        free(p);
649        p = head;
650    }
651}
652
653void free_server_tree(Server_TreeNode *node)
654{
655    //DEBUG("free_server_tree\n");
656    if(node != NULL)
657    {
658        //DEBUG("free tree node\n");
659
660        //free_server_list(node->browse);
661
662        if(node->NextBrother != NULL)
663            free_server_tree(node->NextBrother);
664        if(node->Child != NULL)
665            free_server_tree(node->Child);
666        if(node->parenthref != NULL)
667        {
668            free(node->parenthref);
669        }
670        if(node->browse != NULL)
671        {
672            free_CloudFile_item(node->browse->filelist);
673            free_CloudFile_item(node->browse->folderlist);
674            free(node->browse);
675        }
676        free(node);
677    }
678}
679
680void free_CloudFile_item(CloudFile *head)
681{
682    //DEBUG("***************free_CloudFile_item*********************\n");
683
684    CloudFile *p = head;
685    while(p != NULL)
686    {
687        head = head->next;
688        if(p->href != NULL)
689        {
690            //DEBUG("free CloudFile %s\n",p->href);
691            free(p->href);
692        }
693        free(p);
694        p = head;
695    }
696    //head=NULL;
697}
698
699int is_folder(char *p){
700    return (p[0]=='<') ? 0 : 1;
701}
702
703int is_file(char *p){
704    return (p[0]=='d')?0:1;
705}
706
707Server_TreeNode *create_server_treeroot()
708{
709    Server_TreeNode *TreeRoot = NULL;
710    TreeRoot = (Server_TreeNode *)malloc(sizeof (Server_TreeNode));
711    memset(TreeRoot,0,sizeof(Server_TreeNode));
712    if(TreeRoot == NULL)
713    {
714        DEBUG("create memory error!\n");
715        exit(-1);
716    }
717    TreeRoot->level=0;
718    TreeRoot->NextBrother = NULL;
719    //TreeRoot->browse = NULL;
720    //sprintf(TreeRoot->parenthref,"%s%s/",HOST,ROOTFOLDER);
721    TreeRoot->parenthref = NULL;
722    TreeRoot->browse = NULL;
723    TreeRoot->Child = NULL;
724
725    return TreeRoot;
726}
727
728
729int getCloudInfo_forsize(char *URL,void (* cmd_data)(char *),int index)
730{
731    char *command = (char *)malloc(sizeof(char)*(strlen(URL) + 7));
732    memset(command,'\0',sizeof(command));
733    sprintf(command,"LIST %s",URL);
734    char *temp = utf8_to(command,index);
735    free(command);
736    CURL *curl;
737    CURLcode res;
738    FILE *fp = fopen(LIST_SIZE_DIR,"w");
739    //fp=fopen("/tmp/ftpclient/list_size.txt","w");
740    curl=curl_easy_init();
741    if(curl){
742        curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip);
743        if(strlen(ftp_config.multrule[index]->user_pwd) != 1)
744            curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd);
745        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,temp);
746        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
747        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1);
748        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,30);
749        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
750        res=curl_easy_perform(curl);
751        //DEBUG("%d\n",res);
752
753        if(res != CURLE_OK && res != CURLE_FTP_COULDNT_RETR_FILE){
754            curl_easy_cleanup(curl);
755            fclose(fp);
756            //free(command);
757            free(temp);
758            if(res == CURLE_COULDNT_CONNECT)
759            {
760                write_log(S_ERROR,"Could not connect.","",index);
761            }
762            else if(res == CURLE_OPERATION_TIMEDOUT)
763            {
764                write_log(S_ERROR,"Connect timeout.","",index);
765            }
766            else if(res == CURLE_REMOTE_ACCESS_DENIED)
767            {
768                write_log(S_ERROR,"Connect refused.","",index);
769            }
770            return -1;
771        }
772        curl_easy_cleanup(curl);
773        fclose(fp);
774        //free(command);
775        free(temp);
776    }
777    else
778    {
779        curl_easy_cleanup(curl);
780        fclose(fp);
781        //free(command);
782        free(temp);
783    }
784    cmd_data(URL);
785    return 0;
786}
787
788
789
790int is_ftp_file_copying(char *serverhref,int index)
791{
792    //DEBUG("########check copying#######\n");
793    //DEBUG("\rFtp file copying...");
794    long long int old_length;
795    long long int new_length;
796    long long int d_value;
797
798    old_length = get_file_size(serverhref,index);
799    usleep(1000*1000);
800    new_length = get_file_size(serverhref,index);
801    d_value = new_length - old_length;
802    if(d_value == 0)
803        return 0;
804    else
805        return 1;
806}
807
808int ChangeFile_modtime(char *filepath,time_t servermodtime,int index)
809{
810    DEBUG("**************ChangeFile_modtime**********\n");
811    char *newpath_re = search_newpath(filepath,index);
812
813    struct utimbuf *ub;
814    ub = (struct utimbuf *)malloc(sizeof(struct utimbuf));
815    if(newpath_re != NULL)
816
817    if(servermodtime == -1)
818    {
819        return -1;
820    }
821
822    ub->actime = servermodtime;
823    ub->modtime = servermodtime;
824
825    if(newpath_re != NULL)
826    {
827        DEBUG("newpath_re = %s\n",newpath_re);
828        utime(newpath_re,ub);//������������filename���������������inode������������
829    }
830    else
831    {
832        DEBUG("filepath = %s\n",filepath);
833        utime(filepath,ub);
834    }
835
836    free(newpath_re);
837    free(ub);
838    return 0;
839}
840
841char *localpath_to_serverpath(char *from_localpath,int index)
842{
843    //2014.10.27 by sherry
844    //���������������������������������������
845//    char *p = from_localpath;
846//    p = p + strlen(ftp_config.multrule[index]->base_path);
847//    char *serverpath = (char*)malloc(sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + strlen(p) + 1));
848//    memset(serverpath,'\0',sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + strlen(p) + 1));
849//    sprintf(serverpath,"%s%s",ftp_config.multrule[index]->rooturl,p);
850//    return serverpath;
851
852    char *p = from_localpath;
853    char *serverpath;
854
855    if(!strcmp(from_localpath,ftp_config.multrule[index]->base_path))
856    {
857        serverpath = (char*)malloc(sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + 1));
858        memset(serverpath,'\0',sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + 1));
859        sprintf(serverpath,"%s",ftp_config.multrule[index]->rooturl);
860        return serverpath;
861    }
862    else
863    {
864        p = p + strlen(ftp_config.multrule[index]->base_path);
865        serverpath = (char*)malloc(sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + strlen(p) + 1));
866        memset(serverpath,'\0',sizeof(char)*(strlen(ftp_config.multrule[index]->rooturl) + strlen(p) + 1));
867        sprintf(serverpath,"%s%s",ftp_config.multrule[index]->rooturl,p);
868        return serverpath;
869    }
870}
871
872char *serverpath_to_localpath(char *from_serverpath,int index)
873{
874    //2014.10.28 by sherry
875    //���������������������������������������
876//    char *p = from_serverpath;
877//    p = p + strlen(ftp_config.multrule[index]->rooturl);
878//    char *localpath = (char*)malloc(sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(p) + 1));
879//    memset(localpath,'\0',sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(p) + 1));
880//    sprintf(localpath,"%s%s",ftp_config.multrule[index]->base_path,p);
881//    return localpath;
882
883    char *p = from_serverpath;
884    char *localpath;
885    if(!strcmp(from_serverpath,ftp_config.multrule[index]->rooturl))
886    {
887        localpath=(char*)malloc(sizeof(char)*(ftp_config.multrule[index]->base_path_len + 1));
888        memset(localpath,'\0',sizeof(char)*(ftp_config.multrule[index]->base_path_len + 1));
889        sprintf(localpath,"%s",ftp_config.multrule[index]->base_path);
890        return localpath;
891    }
892    else
893    {
894        p = p + strlen(ftp_config.multrule[index]->rooturl);
895        localpath = (char*)malloc(sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(p) + 1));
896        memset(localpath,'\0',sizeof(char)*(ftp_config.multrule[index]->base_path_len + strlen(p) + 1));
897        sprintf(localpath,"%s%s",ftp_config.multrule[index]->base_path,p);
898        return localpath;
899    }
900
901}
902
903int test_if_dir_empty(char *dir)
904{
905    struct dirent* ent = NULL;
906    DIR *pDir;
907    int i = 0;
908    pDir=opendir(dir);
909    if(pDir != NULL )
910    {
911        while (NULL != (ent=readdir(pDir)))
912        {
913            if(ent->d_name[0] == '.')
914                continue;
915            if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,".."))
916                continue;
917            i++;
918        }
919        closedir(pDir);
920    }
921    return  (i == 0) ? 1 : 0;
922}
923
924Node *queue_dequeue (Node *q)
925{
926    Node *first = q->next;
927    Node *newfirst = q->next->next;
928    q->next = newfirst;
929    first->next = NULL;
930    return first;
931}
932
933//void SearchServerTree(Server_TreeNode* treeRoot)
934//{
935//    FILE *fp = NULL;
936//
937//    if(treeRoot->level == 0)
938//    {
939//        fp = fopen(SERVERLIST_0,"a");
940//        //fprintf(fp,"%d\n",treeRoot->level);
941//    }
942//
943//    if(treeRoot->level == 1)
944//    {
945//        fp = fopen(SERVERLIST_1,"a");
946//        //fprintf(fp,"%d\n",treeRoot->level);
947//    }
948//
949//    if(treeRoot->level == 2)
950//    {
951//        fp = fopen(SERVERLIST_2,"a");
952//        //fprintf(fp,"%d\n",treeRoot->level);
953//    }
954//
955//    if(treeRoot->browse != NULL)
956//    {
957//
958//        CloudFile *de_foldercurrent;
959//        de_foldercurrent = treeRoot->browse->folderlist->next;
960//        while(de_foldercurrent != NULL){
961//            //DEBUG("serverfolder->href = %s\n",de_foldercurrent->href);
962//            fprintf(fp,"%s\n",de_foldercurrent->href);
963//            de_foldercurrent = de_foldercurrent->next;
964//        }
965//    }
966//    fclose(fp);
967//    if((treeRoot->Child != NULL))
968//        SearchServerTree(treeRoot->Child);
969//
970//    if(treeRoot->NextBrother != NULL)
971//        SearchServerTree(treeRoot->NextBrother);
972//}
973
974void free_action_item(action_item *head)
975{
976    action_item *point;
977    point = head->next;
978
979    while(point != NULL)
980    {
981        head->next = point->next;
982        free(point->action);
983        free(point->path);
984        free(point);
985        point = head->next;
986    }
987    free(head);
988}
989
990void del_download_only_action_item(const char *action,const char *path,action_item *head)
991{
992    //DEBUG("del_sync_item action=%s,path=%s\n",action,path);
993    action_item *p1, *p2;
994    char *cmp_name;
995    char *p1_cmp_name;
996    p1 = head->next;
997    p2 = head;
998
999    cmp_name = my_str_malloc((size_t)(strlen(path)+2));
1000    sprintf(cmp_name,"%s/",path);    //add for delete folder and subfolder in download only socket list
1001
1002    while(p1 != NULL)
1003    {
1004        p1_cmp_name = my_str_malloc((size_t)(strlen(p1->path)+2));
1005        sprintf(p1_cmp_name,"%s/",p1->path);      //add for delete folder and subfolder in download only socket list
1006        //DEBUG("del_download_only_sync_item  p1->name = %s\n",p1->name);
1007        //DEBUG("del_download_only_sync_item  cmp_name = %s\n",cmp_name);
1008        if(strstr(p1_cmp_name,cmp_name) != NULL)
1009        {
1010            p2->next = p1->next;
1011            free(p1->action);
1012            free(p1->path);
1013            free(p1);
1014            //DEBUG("del sync item ok\n");
1015            //break;
1016            p1 = p2->next;
1017        }
1018        else
1019        {
1020            p2 = p1;
1021            p1 = p1->next;
1022        }
1023        free(p1_cmp_name);
1024    }
1025
1026    free(cmp_name);
1027    //DEBUG("del sync item fail\n");
1028}
1029
1030int add_all_download_only_socket_list(char *cmd,const char *dir,int index)
1031{
1032    struct dirent* ent = NULL;
1033    char *fullname;
1034    int fail_flag = 0;
1035    //char error_message[256];
1036
1037    DIR *dp = opendir(dir);
1038
1039    if(dp == NULL)
1040    {
1041        DEBUG("opendir %s fail",dir);
1042        fail_flag = 1;
1043        return -1;
1044    }
1045
1046    add_action_item(cmd,dir,g_pSyncList[index]->download_only_socket_head);
1047
1048    while (NULL != (ent=readdir(dp)))
1049    {
1050
1051        if(ent->d_name[0] == '.')
1052            continue;
1053        if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,".."))
1054            continue;
1055
1056        fullname = my_str_malloc((size_t)(strlen(dir)+strlen(ent->d_name)+2));
1057
1058        sprintf(fullname,"%s/%s",dir,ent->d_name);
1059
1060        if( test_if_dir(fullname) == 1)
1061        {
1062            add_all_download_only_socket_list("createfolder",fullname,index);
1063        }
1064        else
1065        {
1066            add_action_item("createfile",fullname,g_pSyncList[index]->download_only_socket_head);
1067        }
1068        free(fullname);
1069    }
1070
1071    closedir(dp);
1072
1073    return (fail_flag == 1) ? -1 : 0;
1074}
1075
1076int test_if_download_temp_file(char *filename)
1077{
1078    char file_suffix[9];
1079    //char *temp_suffix = ".asus.td";
1080    char *temp_suffix = (char*)malloc(sizeof(char)*9);
1081    memset(temp_suffix,'\0',sizeof(char)*9);
1082    strcpy(temp_suffix,".asus.td");
1083
1084    memset(file_suffix,0,sizeof(file_suffix));
1085    char *p = filename;
1086    if(strstr(filename,temp_suffix))
1087    {
1088        strcpy(file_suffix,p+(strlen(filename)-strlen(temp_suffix)));
1089        if(!strcmp(file_suffix,temp_suffix))
1090        {
1091            free(temp_suffix);
1092            return 1;
1093        }
1094    }
1095    free(temp_suffix);
1096    return 0;
1097}
1098
1099int test_if_dir(const char *dir)
1100{
1101    DIR *dp = opendir(dir);
1102    if(dp == NULL)
1103    {
1104        //DEBUG("file\n");
1105        return 0;
1106    }
1107    closedir(dp);
1108    //DEBUG("dir\n");
1109    return 1;
1110}
1111
1112void show(Node* head)
1113{
1114    DEBUG("##################Sockets from inotify...###################\n");
1115    Node *pTemp = head->next;
1116    while(pTemp!=NULL)
1117    {
1118        DEBUG(">>>>%s\n",pTemp->cmdName);
1119        pTemp=pTemp->next;
1120    }
1121}
1122
1123void show_item(action_item* head)
1124{
1125    action_item *pTemp = head->next;
1126    //DEBUG(">>>>Chain:%s\n",head->cmdName);
1127    while(pTemp!=NULL)
1128    {
1129        DEBUG("*****%s:%s\n",pTemp->action,pTemp->path);
1130        pTemp=pTemp->next;
1131    }
1132}
1133
1134int del_action_item(const char *action,const char *path,action_item *head)
1135{
1136    action_item *p1,*p2;
1137    p1 = head->next;
1138    p2 = head;
1139    while(p1 != NULL)
1140    {
1141        if( !strcmp(p1->action,action) && !strcmp(p1->path,path))
1142        {
1143            p2->next = p1->next;
1144            free(p1->action);
1145            free(p1->path);
1146            free(p1);
1147            return 0;
1148        }
1149        p2 = p1;
1150        p1 = p1->next;
1151    }
1152    DEBUG("can not find action item\n");
1153
1154    return 1;
1155}
1156
1157int is_local_space_enough(CloudFile *do_file,int index)
1158{
1159    long long int freespace;
1160    freespace = get_local_freespace(index);
1161    DEBUG("freespace = %lld,do_file->size = %lld\n",freespace,do_file->size);
1162    if(freespace <= do_file->size){
1163        DEBUG("local freespace is not enough!\n");
1164        return 0;
1165    }
1166    else
1167    {
1168        //DEBUG("local freespace is enough!\n");
1169        return 1;
1170    }
1171}
1172
1173action_item *create_action_item_head()
1174{
1175    action_item *head;
1176
1177    head = (action_item *)malloc(sizeof(action_item));
1178    if(head == NULL)
1179    {
1180        DEBUG("create memory error!\n");
1181        exit(-1);
1182    }
1183    memset(head,'\0',sizeof(action_item));
1184    head->next = NULL;
1185
1186    return head;
1187}
1188
1189int add_action_item(const char *action,const char *path,action_item *head)
1190{
1191    DEBUG("add_action_item,action = %s,path = %s\n",action,path);
1192
1193    action_item *p1,*p2;
1194    p1 = head;
1195    p2 = (action_item *)malloc(sizeof(action_item));
1196    memset(p2,'\0',sizeof(action_item));
1197    p2->action = (char *)malloc(sizeof(char)*(strlen(action)+1));
1198    p2->path = (char *)malloc(sizeof(char)*(strlen(path)+1));
1199    memset(p2->action,'\0',sizeof(p2->action));
1200    memset(p2->path,'\0',sizeof(p2->path));
1201    sprintf(p2->action,"%s",action);
1202    sprintf(p2->path,"%s",path);
1203    while(p1->next != NULL)
1204        p1 = p1->next;
1205    p1->next = p2;
1206    p2->next = NULL;
1207    DEBUG("add action item OK!\n");
1208    return 0;
1209}
1210
1211action_item *get_action_item(const char *action,const char *path,action_item *head,int index)
1212{
1213    action_item *p;
1214    p = head->next;
1215    while(p != NULL)
1216    {
1217        if(ftp_config.multrule[index]->rules == 1)
1218        {
1219            if(!strcmp(p->path,path))
1220            {
1221                return p;
1222            }
1223        }
1224        else
1225        {
1226            if(!strcmp(p->action,action) && !strcmp(p->path,path))
1227            {
1228                return p;
1229            }
1230        }
1231        p = p->next;
1232    }
1233    DEBUG("can not find action item\n");
1234    return NULL;
1235}
1236
1237char *my_str_malloc(size_t len)
1238{
1239    char *s;
1240    s = (char *)malloc(sizeof(char)*len);
1241    if(s == NULL)
1242    {
1243        DEBUG("Out of memory.\n");
1244        exit(1);
1245    }
1246
1247    //memset(s,'\0',sizeof(s));//2014.10.28 by sherry sizeof(������)������������4
1248    memset(s,'\0',sizeof(char)*len);
1249    return s;
1250}
1251
1252long long int get_local_freespace(int index)
1253{
1254    DEBUG("***********get %s freespace!***********\n",ftp_config.multrule[index]->base_path);
1255    long long int freespace = 0;
1256    struct statvfs diskdata;
1257    if(!statvfs(ftp_config.multrule[index]->base_path,&diskdata))
1258    {
1259        freespace = (long long)diskdata.f_bsize * (long long)diskdata.f_bavail;
1260        return freespace;
1261    }
1262    else
1263    {
1264        return 0;
1265    }
1266}
1267
1268char *get_socket_base_path(char *cmd)
1269{
1270
1271    DEBUG("get_socket_base_path cmd : %s\n",cmd);
1272
1273    char *temp = NULL;
1274    char *temp1 = NULL;
1275    char path[1024];
1276    char *root_path = NULL;
1277
1278    if(!strncmp(cmd,"rmroot",6))
1279    {
1280        temp = strchr(cmd,'/');
1281        root_path = my_str_malloc(512);
1282        sprintf(root_path,"%s",temp);
1283    }
1284    else
1285    {
1286        temp = strchr(cmd,'/');
1287        temp1 = strchr(temp,'\n');
1288        memset(path,0,sizeof(path));
1289        strncpy(path,temp,strlen(temp)-strlen(temp1));
1290        root_path = my_str_malloc(512);
1291        temp = my_nstrchr('/',path,5);
1292        if(temp == NULL)
1293        {
1294            sprintf(root_path,"%s",path);
1295        }
1296        else
1297        {
1298            snprintf(root_path,strlen(path)-strlen(temp) + 1,"%s",path);
1299        }
1300    }
1301    //DEBUG("get_socket_base_path root_path = %s\n",root_path);
1302    return root_path;
1303}
1304
1305mod_time *get_mtime_1(FILE *fp)
1306{
1307    if(fp != NULL)
1308    {
1309        mod_time *modtime_1;
1310        modtime_1=(mod_time *)malloc(sizeof(mod_time));
1311        memset(modtime_1,0,sizeof(modtime_1));
1312        char buff[512]={0};
1313        int res=0;
1314        const char *split=" ";
1315        char *p;
1316        rewind(fp);
1317        while(fgets(buff,sizeof(buff),fp) != NULL)
1318        {
1319            p=strtok(buff,split);
1320            int i=0;
1321            while(p!=NULL)
1322            {
1323                switch(i)
1324                {
1325                case 0:
1326                    res=atoi(p);
1327                    break;
1328                case 1:
1329                    if(res == 213)
1330                    {
1331                        strcpy(modtime_1->mtime, p);
1332                        DEBUG("%s", modtime_1->mtime);
1333                        modtime_1->modtime = change_time_to_sec(modtime_1->mtime);
1334                    }
1335                    if(res == 500)
1336                    {
1337                        modtime_1->modtime = (time_t)-1;
1338                    }
1339                    break;
1340                default:
1341                    break;
1342                }
1343                i++;
1344                p=strtok(NULL,split);
1345            }
1346            res=0;
1347        }
1348        fclose(fp);
1349        DEBUG1("@@@@@@@@@@@@modtime_1=%s\n",modtime_1);
1350        return modtime_1;
1351    }
1352    else
1353        DEBUG("fp is NULL \n");
1354}
1355
1356int getCloudInfo_one(char *URL,int (* cmd_data)(char *,int),int index)
1357{
1358    DEBUG("%s\n",URL);
1359    int status;
1360    char *command = (char *)malloc(sizeof(char)*(strlen(URL) + 7));
1361    //memset(command,'\0',sizeof(command));//2014.10.28 by sherry sizeof(������)=4
1362    memset(command,'\0',sizeof(char)*(strlen(URL) + 7));
1363    sprintf(command,"LIST %s",URL);
1364    DEBUG("command = %s\n",command);
1365    char *temp = utf8_to(command,index);
1366    free(command);
1367    CURL *curl;
1368    CURLcode res;
1369    FILE *fp = fopen(LIST_ONE_DIR,"w");
1370    //fp=fopen("/tmp/ftpclient/list_one.txt","w");
1371    curl=curl_easy_init();
1372    if(curl){
1373        curl_easy_setopt(curl, CURLOPT_URL, ftp_config.multrule[index]->server_ip);
1374        if(strlen(ftp_config.multrule[index]->user_pwd) != 1)
1375            curl_easy_setopt(curl, CURLOPT_USERPWD, ftp_config.multrule[index]->user_pwd);
1376        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,temp);
1377        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
1378        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1);
1379        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,30);
1380        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
1381        res = curl_easy_perform(curl);
1382        DEBUG("getCloudInfo_one() - res = %d\n",res);
1383        if(res != CURLE_OK)
1384        {
1385            curl_easy_cleanup(curl);
1386            fclose(fp);
1387            free(temp);
1388            return res;
1389        }
1390        curl_easy_cleanup(curl);
1391        fclose(fp);
1392        free(temp);//free(command);
1393    }
1394    else
1395    {
1396        curl_easy_cleanup(curl);
1397        fclose(fp);
1398        free(temp);//free(command);
1399    }
1400    status = cmd_data(URL,index);
1401    if(status)
1402    {
1403        return UNSUPPORT_ENCODING;
1404    }
1405    return 0;
1406}
1407
1408time_t change_time_to_sec(char *time)
1409{
1410    char *p = time;
1411    char buf[12];
1412    memset(buf,0,sizeof(buf));
1413    struct timeval tv;
1414    struct timezone tz;
1415    gettimeofday(&tv,&tz);
1416    int zone_time = tz.tz_minuteswest / 60;
1417    time_t sec;
1418    struct tm *timeptr = (struct tm *)malloc(sizeof(struct tm));
1419
1420    memset(timeptr,'\0',sizeof(timeptr));
1421    strncpy(buf,p,4);
1422    timeptr->tm_year = atoi(buf) - 1900;
1423
1424    p = p + 4;
1425    memset(buf,0,sizeof(buf));
1426    strncpy(buf,p,2);
1427    timeptr->tm_mon = atoi(buf) - 1;
1428
1429    p = p + 2;
1430    memset(buf,0,sizeof(buf));
1431    strncpy(buf,p,2);
1432    timeptr->tm_mday = atoi(buf);
1433
1434    p = p + 2;
1435    memset(buf,0,sizeof(buf));
1436    strncpy(buf,p,2);
1437    timeptr->tm_hour = atoi(buf) - zone_time;
1438
1439    p = p + 2;
1440    memset(buf,0,sizeof(buf));
1441    strncpy(buf,p,2);
1442    timeptr->tm_min = atoi(buf);
1443
1444    p = p + 2;
1445    memset(buf,0,sizeof(buf));
1446    strncpy(buf,p,2);
1447    timeptr->tm_sec = atoi(buf);
1448
1449    sec = mktime(timeptr);
1450    p = NULL;
1451    free(timeptr);
1452    return sec;
1453}
1454
1455mod_time *get_mtime(FILE *fp)
1456{
1457    //DEBUG("*********************get_mtime***********************\n");
1458    if(fp != NULL){
1459        mod_time *modtime;
1460        modtime=(mod_time *)malloc(sizeof(mod_time));
1461        memset(modtime,0,sizeof(modtime));
1462        char buff[512] = {0};
1463        int res = 0;
1464        const char *split = " ";
1465        char *p;
1466        rewind(fp);
1467        while(fgets(buff,sizeof(buff),fp) != NULL)
1468        {
1469            p=strtok(buff,split);
1470            int i = 0;
1471            while(p != NULL)
1472            {
1473                switch(i)
1474                {
1475                case 0:
1476                    res = atoi(p);
1477                    break;
1478                case 1:
1479                    if(res == 213)
1480                    {
1481                        strcpy(modtime->mtime,p);
1482                        modtime->modtime = change_time_to_sec(modtime->mtime);
1483                    }
1484                    break;
1485                default:
1486                    break;
1487                }
1488                i++;
1489                p = strtok(NULL,split);
1490            }
1491            res = 0;
1492        }
1493        fclose(fp);
1494        return modtime;
1495    }
1496    else
1497    {
1498        DEBUG("fp is NULL \n");
1499    }
1500}
1501
1502void my_mkdir_r(char *path,int index)
1503{
1504    int i,len;
1505    char str[512];
1506    char fullname[512];
1507    char *temp;
1508    DEBUG("****************my_mkdir_r*******************\n");
1509    DEBUG("%s\n",path);
1510    memset(str,0,sizeof(str));
1511
1512    temp = strstr(path,ftp_config.multrule[index]->mount_path);
1513
1514    len = strlen(ftp_config.multrule[index]->mount_path);
1515    strcpy(str,temp + len);
1516
1517    //strncpy(str,path,512);
1518    len = strlen(str);
1519    for(i=0; i < len ; i++)
1520    {
1521        if(str[i] == '/' && i != 0)
1522        {
1523            str[i] = '\0';
1524            memset(fullname,0,sizeof(fullname));
1525            sprintf(fullname,"%s%s",ftp_config.multrule[index]->mount_path,str);
1526            if(access(fullname,F_OK) != 0)
1527            {
1528                DEBUG("%s\n",fullname);
1529                my_local_mkdir(fullname);
1530            }
1531            str[i] = '/';
1532        }
1533    }
1534
1535
1536    memset(fullname,0,sizeof(fullname));
1537    sprintf(fullname,"%s%s",ftp_config.multrule[index]->mount_path,str);
1538
1539    if(len > 0 && access(fullname,F_OK) != 0)
1540    {
1541        DEBUG("%s\n",fullname);
1542        my_local_mkdir(fullname);
1543    }
1544}
1545
1546int parse_config(const char *path,Config *config)
1547{
1548    DEBUG("#####parse_config####\n");
1549    FILE *fp;
1550    //DIR *dir;
1551    int status;
1552    char buffer[256];
1553    char *p;
1554
1555    int i = 0;
1556    int k = 0;
1557    int len = 0;
1558
1559    memset(buffer, '\0', sizeof(buffer));
1560
1561    if (access(path,0) == 0)
1562    {
1563        if(( fp = fopen(path,"rb"))==NULL)
1564        {
1565            fprintf(stderr,"read Cloud error");
1566            exit(-1);
1567        }
1568        config->multrule = (MultRule **)malloc(sizeof(MultRule*)*config->dir_num);
1569        while(fgets(buffer,256,fp) != NULL)
1570        {
1571            if( buffer[ strlen(buffer)-1 ] == '\n' )
1572                buffer[ strlen(buffer)-1 ] = '\0';
1573            p = buffer;
1574            DEBUG("p:%s\n",p);
1575            if(i == (k*8))
1576            {
1577                config->id = atoi(p);
1578                DEBUG("config->id = %d\n",config->id);
1579            }
1580            else if(i == (k*8 + 1))
1581            {
1582                config->multrule[k] = (MultRule*)malloc(sizeof(MultRule));
1583                memset(config->multrule[k],0,sizeof(MultRule));
1584                config->multrule[k]->encoding = atoi(p);
1585                DEBUG("config->multrule[%d]->encoding = %d\n",k,config->multrule[k]->encoding);
1586            }
1587            else if(i == (k*8 + 2))
1588            {
1589                strcpy(config->multrule[k]->f_usr,p);
1590            }
1591            else if(i == (k*8 + 3))
1592            {
1593                strcpy(config->multrule[k]->f_pwd,p);
1594                sprintf(config->multrule[k]->user_pwd,"%s:%s",config->multrule[k]->f_usr,config->multrule[k]->f_pwd);
1595                DEBUG("config->multrule[%d]->user_pwd = %s len:%d\n",k,config->multrule[k]->user_pwd,strlen(config->multrule[k]->user_pwd));
1596            }
1597            else if(i == (k*8 + 4))
1598            {
1599                strcpy(config->multrule[k]->server_ip,p);
1600                len = strlen(config->multrule[k]->server_ip);
1601                config->multrule[k]->server_ip_len = len;
1602                DEBUG("config->multrule[%d]->server_ip = %s\n",k,config->multrule[k]->server_ip);
1603            }
1604            else if(i == (k*8 + 5))
1605            {
1606                strcpy(config->multrule[k]->rooturl,p);
1607                char *p1 = config->multrule[k]->rooturl + strlen(config->multrule[k]->rooturl);
1608                while(p1[0] != '/')
1609                    p1--;
1610                DEBUG("config->multrule[%d]->rooturl = %s\n",k,config->multrule[k]->rooturl);
1611                snprintf(config->multrule[k]->hardware,strlen(config->multrule[k]->rooturl) - strlen(p1) + 1,"%s",config->multrule[k]->rooturl);
1612                DEBUG("config->multrule[%d]->hardware:%s\n",k,config->multrule[k]->hardware);
1613            }
1614            else if(i == (k*8 + 6))
1615            {
1616                config->multrule[k]->rules = atoi(p);
1617                DEBUG("config->multrule[%d]->rules = %d\n",k,config->multrule[k]->rules);
1618            }
1619            else if(i == (k*8 + 7))
1620            {
1621                len = strlen(p);
1622                if(p[len-1] == '\n')
1623                    p[len-1] = '\0';
1624                strcpy(config->multrule[k]->base_path,p);
1625                len = strlen(config->multrule[k]->base_path);
1626                config->multrule[k]->base_path_len = len;
1627                if(config->multrule[k]->base_path[len-1] == '/')
1628                    config->multrule[k]->base_path[len-1] = '\0';
1629
1630                char *p2 = config->multrule[k]->base_path + config->multrule[k]->base_path_len;
1631                while(p2[0] != '/')
1632                    p2--;
1633                snprintf(config->multrule[k]->mount_path,config->multrule[k]->base_path_len - strlen(p2) + 1,"%s",config->multrule[k]->base_path);
1634                sprintf(config->multrule[k]->base_folder,"%s",p2);
1635                //sprintf(config->multrule[k]->base_path_parentref,"%s",config->multrule[k]->mount_path);
1636                sprintf(config->multrule[k]->fullrooturl,"%s%s",config->multrule[k]->server_ip,config->multrule[k]->rooturl);
1637
1638                DEBUG("config->multrule[%d]->mount_path = %s\n",k,config->multrule[k]->mount_path);
1639                DEBUG("config->multrule[%d]->base_path = %s\n",k,config->multrule[k]->base_path);
1640                //DEBUG("config->multrule[%d]->base_path_parentref = %s\n",k,config->multrule[k]->base_path_parentref);
1641                DEBUG("config->multrule[%d]->base_folder = %s\n",k,config->multrule[k]->base_folder);
1642                DEBUG("config->multrule[%d]->rooturl = %s\n",k,config->multrule[k]->rooturl);
1643                DEBUG("config->multrule[%d]->fullrooturl = %s\n",k,config->multrule[k]->fullrooturl);
1644                k++;
1645            }
1646            i++;
1647        }
1648
1649        //DEBUG("%s\n",buffer);
1650        //int x;
1651        //x = config->dir_num;
1652        //for(x=0;)
1653
1654        fclose(fp);
1655    }
1656    int m;
1657    for(m = 0; m < config->dir_num; m++)
1658    {
1659        DEBUG("m = %d\n",m);
1660        my_mkdir_r(config->multrule[m]->base_path, m);  //have mountpath
1661    }
1662    return 0;
1663}
1664
1665int usr_auth(char *ip,char *user_pwd)
1666{
1667    CURL *curl;
1668    CURLcode res;
1669    curl = curl_easy_init();
1670    if(curl)
1671    {
1672        curl_easy_setopt(curl, CURLOPT_URL, ip);
1673        if(strlen(user_pwd) != 1)
1674            curl_easy_setopt(curl, CURLOPT_USERPWD, user_pwd);
1675        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
1676        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,1);
1677        curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,10);
1678        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
1679        res = curl_easy_perform(curl);// 28:a timeout was reached  7:couldn't connect to server
1680        curl_easy_cleanup(curl);
1681        return res;
1682    }
1683}
1684
1685//void create_start_file()
1686//{
1687//    my_local_mkdir("/tmp/smartsync_app");
1688//    FILE *fp;
1689//    fp = fopen("/tmp/smartsync_app/ftpclient_start","w");
1690//    fclose(fp);
1691//}
1692//
1693///*
1694// *0,no file
1695// *1,have file
1696//*/
1697//int detect_process_file()
1698//{
1699//    struct dirent *ent = NULL;
1700//    DIR *pdir;
1701//    int num = 0;
1702//    pdir = opendir("/tmp/smartsync_app");
1703//
1704//    if(pdir != NULL)
1705//    {
1706//        while (NULL != (ent=readdir(pdir)))
1707//        {
1708//            //printf("%s is ent->d_name\n",ent->d_name);
1709//            if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,".."))
1710//                continue;
1711//            num++;
1712//        }
1713//        closedir(pdir);
1714//    }
1715//    else
1716//        return 0;
1717//
1718//    if(num)
1719//        return 1;
1720//
1721//    return 0;
1722//}
1723