int is_server_have_localpath(char *path, Server_TreeNode *treenode, int index) { if(treenode == NULL) return 0; char *localpath; int ret = 0; int cmp = 1; localpath = serverpath_to_localpath(treenode->parenthref, index); if((cmp = strcmp(localpath, path)) == 0) { ret = 1; free(localpath); return ret; } else { free(localpath); } if(treenode->Child != NULL) { ret = is_server_have_localpath(path, treenode->Child, index); if(ret == 1) { return ret; } } if(treenode->NextBrother != NULL) { ret = is_server_have_localpath(path,treenode->NextBrother, index); if(ret == 1) { return ret; } } return ret; }
/* download file */ int SMB_download(char *serverpath, int index) { char buffer[4096] = {0}; int buflen; char *localpath = serverpath_to_localpath(serverpath, index); //char *localpath_td = my_malloc(strlen(localpath) + strlen(".asus.td") + 1); char *localpath_td = my_malloc(strlen(localpath) + 9); //2014.10.20 by sherry malloc申请内存是否成功 //if(localpath_td==NULL) // return NULL; sprintf(localpath_td, "%s%s", localpath, ".asus.td"); write_log(S_DOWNLOAD, "", serverpath, index); unsigned long long halfsize = 0; int dst_fd; if(access(localpath_td, F_OK) != 0) { dst_fd = open(localpath_td, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if(dst_fd < 0){ printf("open() - %s failed\n", localpath_td); return -1; } } else { dst_fd = open(localpath_td, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if(dst_fd < 0){ printf("open() - %s failed\n", localpath_td); return -1; } halfsize = lseek(dst_fd, 0L, SEEK_END); lseek(dst_fd, halfsize, SEEK_SET); } SMB_init(index); int fd = -1; if((fd = smbc_open(serverpath, O_RDONLY, FILE_MODE)) > 0) { unsigned long long cli_filesize = 0; unsigned long long smb_filesize = 0; smb_filesize = smbc_lseek(fd, 0L, SEEK_END); smbc_lseek(fd, halfsize, SEEK_SET); while((buflen = smbc_read(fd, buffer, sizeof(buffer))) > 0 && exit_loop == 0) { //2014.11.20 by sherry 判断是否write成功 write(dst_fd, buffer, buflen); cli_filesize += buflen; printf("\rDownload [%s] percent - %f ", serverpath, (float)cli_filesize/(float)smb_filesize); } if(cli_filesize == smb_filesize) { rename(localpath_td, localpath); free(localpath); free(localpath_td); smbc_close(fd); close(dst_fd); } else { free(localpath); free(localpath_td); smbc_close(fd); close(dst_fd); return -1; } } else { printf("smbc_open() - %s failed\n", serverpath); close(dst_fd); free(localpath); free(localpath_td); return COULD_NOT_CONNECNT_TO_SERVER; } return 0; }
int initMyLocalFolder(Server_TreeNode *servertreenode,int index) { int res=0; if(servertreenode->browse != NULL) { CloudFile *init_folder=NULL,*init_file=NULL; if(servertreenode->browse->foldernumber > 0) init_folder=servertreenode->browse->folderlist->next; if(servertreenode->browse->filenumber > 0) init_file=servertreenode->browse->filelist->next; int ret; while(init_folder != NULL && !exit_loop) { char *createpath; createpath = serverpath_to_localpath(init_folder->href,index); if(NULL == opendir(createpath)) { if(wait_handle_socket(index)) { return HAVE_LOCAL_SOCKET; } if(-1 == mkdir(createpath,0777)) { wd_DEBUG("mkdir %s fail",createpath); return -1; } else { add_action_item("createfolder",createpath,g_pSyncList[index]->server_action_list); } } free(createpath); init_folder = init_folder->next; } while(init_file != NULL && !exit_loop) { if(is_local_space_enough(init_file,index)) { char *createpath; createpath = serverpath_to_localpath(init_file->href,index); add_action_item("createfile",createpath,g_pSyncList[index]->server_action_list); ret=api_download(createpath,init_file->href,index); if(ret == 0) { ChangeFile_modtime(createpath,init_file->mtime); } else return ret; free(createpath); } else { write_log(S_ERROR,"local space is not enough!","",index); add_action_item("download",init_file->href,g_pSyncList[index]->unfinished_list); } init_file = init_file->next; } } if(servertreenode->Child != NULL && !exit_loop) { res = initMyLocalFolder(servertreenode->Child,index); if(res != 0) { return res; } } if(servertreenode->NextBrother != NULL && !exit_loop) { res = initMyLocalFolder(servertreenode->NextBrother,index); if(res != 0) { return res; } } return res; }