/* rename or move (file&folder) */ int SMB_remo(char *localoldpath, char *localnewpath, int index) { SMB_init(index); char *serveroldpath, *servernewpath; serveroldpath = localpath_to_serverpath(localoldpath, index); servernewpath = localpath_to_serverpath(localnewpath, index); int res = smbc_rename(serveroldpath, servernewpath); if(res != 0) { if(!test_if_dir(localnewpath)){ res = SMB_upload(localnewpath, index); if(res == 0) { time_t modtime = Getmodtime(servernewpath, index); if(ChangeFile_modtime(localnewpath, modtime, index)) { printf("ChangeFile_modtime failed!\n"); } } }else{ res = moveFolder(localoldpath, localnewpath, index); } } free(serveroldpath); free(servernewpath); return res; }
int check_process_exist(int pid){ char path[32]; memset(path, 0, 32); sprintf(path, "/proc/%d", pid); return test_if_dir(path); }
int add_all_download_only_dragfolder_socket_list(const char *dir,int index) { struct dirent* ent = NULL; char *fullname; int fail_flag = 0; DIR *dp = opendir(dir); if(dp == NULL) { DEBUG("opendir %s fail",dir); fail_flag = 1; return -1; } while (NULL != (ent=readdir(dp))) { if(ent->d_name[0] == '.') continue; if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) continue; fullname = my_malloc((size_t)(strlen(dir)+strlen(ent->d_name)+2)); sprintf(fullname,"%s/%s",dir,ent->d_name); if( test_if_dir(fullname) == 1) { add_action_item("createfolder", fullname, g_pSyncList[index]->dragfolder_action_list); add_action_item("createfolder", fullname, g_pSyncList[index]->download_only_socket_head); add_all_download_only_dragfolder_socket_list(fullname,index); } else { add_action_item("createfile",fullname,g_pSyncList[index]->dragfolder_action_list); add_action_item("createfile",fullname,g_pSyncList[index]->download_only_socket_head); } free(fullname); } closedir(dp); return (fail_flag == 1) ? -1 : 0; }
void del_all_items(char *dir,int index) { struct dirent* ent = NULL; DIR *pDir; pDir=opendir(dir); if(pDir != NULL ) { while (NULL != (ent=readdir(pDir))) { if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) continue; char *fullname; size_t len; len = strlen(dir)+strlen(ent->d_name)+2; fullname = my_malloc(len); sprintf(fullname,"%s/%s",dir,ent->d_name); if(test_if_dir(fullname) == 1) { wait_handle_socket(index); del_all_items(fullname,index); } else { wait_handle_socket(index); add_action_item("remove",fullname,g_pSyncList[index]->server_action_list); remove(fullname); } free(fullname); } closedir(pDir); add_action_item("remove",dir,g_pSyncList[index]->server_action_list); remove(dir); } else DEBUG("open %s fail \n",dir); }
/*获取某一文件夹下的所有文件和文件夹信息*/ Local *Find_Floor_Dir(const char *path) { Local *local; int filenum; int foldernum; LocalFile *localfloorfile; LocalFolder *localfloorfolder; LocalFile *localfloorfiletmp; LocalFolder *localfloorfoldertmp; LocalFile *localfloorfiletail; LocalFolder *localfloorfoldertail; DIR *pDir; struct dirent *ent = NULL; filenum = 0; foldernum = 0; local = (Local *)malloc(sizeof(Local)); memset(local,0,sizeof(Local)); localfloorfile = (LocalFile *)malloc(sizeof(LocalFile)); localfloorfolder = (LocalFolder *)malloc(sizeof(LocalFolder)); memset(localfloorfolder,0,sizeof(localfloorfolder)); memset(localfloorfile,0,sizeof(localfloorfile)); localfloorfile->path = NULL; localfloorfolder->path = NULL; localfloorfiletail = localfloorfile; localfloorfoldertail = localfloorfolder; localfloorfiletail->next = NULL; localfloorfoldertail->next = NULL; pDir = opendir(path); if(NULL == pDir) { return NULL; } while(NULL != (ent = readdir(pDir))) { /* fix :accept the begin of '.' files; */ // if(ent->d_name[0] == '.') // continue; if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) continue; if(test_if_download_temp_file(ent->d_name)) //xxx.asus.td filename will not get continue; char *fullname; size_t len; len = strlen(path)+strlen(ent->d_name)+2; fullname = my_str_malloc(len); sprintf(fullname,"%s/%s",path,ent->d_name); //printf("folder fullname = %s\n",fullname); //printf("ent->d_ino = %d\n",ent->d_ino); if(test_if_dir(fullname) == 1) { localfloorfoldertmp = (LocalFolder *)malloc(sizeof(LocalFolder)); memset(localfloorfoldertmp,0,sizeof(localfloorfoldertmp)); localfloorfoldertmp->path = my_str_malloc((size_t)(strlen(fullname)+1)); sprintf(localfloorfoldertmp->name,"%s",ent->d_name); sprintf(localfloorfoldertmp->path,"%s",fullname); ++foldernum; localfloorfoldertail->next = localfloorfoldertmp; localfloorfoldertail = localfloorfoldertmp; localfloorfoldertail->next = NULL; } else { struct stat buf; if(stat(fullname,&buf) == -1) { perror("stat:"); continue; } localfloorfiletmp = (LocalFile *)malloc(sizeof(LocalFile)); memset(localfloorfiletmp,0,sizeof(localfloorfiletmp)); localfloorfiletmp->path = my_str_malloc((size_t)(strlen(fullname)+1)); unsigned long asec = buf.st_atime; unsigned long msec = buf.st_mtime; unsigned long csec = buf.st_ctime; sprintf(localfloorfiletmp->creationtime,"%lu",csec); sprintf(localfloorfiletmp->lastaccesstime,"%lu",asec); sprintf(localfloorfiletmp->lastwritetime,"%lu",msec); sprintf(localfloorfiletmp->name,"%s",ent->d_name); sprintf(localfloorfiletmp->path,"%s",fullname); localfloorfiletmp->size = buf.st_size; ++filenum; localfloorfiletail->next = localfloorfiletmp; localfloorfiletail = localfloorfiletmp; localfloorfiletail->next = NULL; } //printf("free fullname\n"); free(fullname); //printf("free fullname over\n"); } local->filelist = localfloorfile; local->folderlist = localfloorfolder; local->filenumber = filenum; local->foldernumber = foldernum; closedir(pDir); return local; }
int get_all_folder_in_mount_path(const char *const mount_path, int *sh_num, char ***folder_list) { DIR *pool_to_open; struct dirent *dp; char *testdir; char **tmp_folder_list, **tmp_folder; int len, i; tmp_folder_list = NULL; pool_to_open = opendir(mount_path); if (pool_to_open == NULL) { return -1; } *sh_num = 0; while ((dp = readdir(pool_to_open)) != NULL) { if (dp->d_name[0] == '.') continue; if (test_if_System_folder(dp->d_name) == 1) continue; len = strlen(mount_path)+strlen("/")+strlen(dp->d_name); testdir = (char *)malloc(sizeof(char)*(len+1)); if (testdir == NULL) { closedir(pool_to_open); return -1; } sprintf(testdir, "%s/%s", mount_path, dp->d_name); testdir[len] = 0; if (!test_if_dir(testdir)) { free(testdir); continue; } free(testdir); tmp_folder = (char **)malloc(sizeof(char *)*(*sh_num+1)); if (tmp_folder == NULL) { return -1; } len = strlen(dp->d_name); tmp_folder[*sh_num] = (char *)malloc(sizeof(char)*(len+1)); if (tmp_folder[*sh_num] == NULL) { free(tmp_folder); return -1; } strcpy(tmp_folder[*sh_num], dp->d_name); if (*sh_num != 0) { for (i = 0; i < *sh_num; ++i) tmp_folder[i] = tmp_folder_list[i]; free(tmp_folder_list); tmp_folder_list = tmp_folder; } else tmp_folder_list = tmp_folder; ++(*sh_num); } closedir(pool_to_open); *folder_list = tmp_folder_list; return 0; }
int mod_folder(const char *const mount_path, const char *const folder, const char *const new_folder) { int result, i, len; int acc_num = 0; char **account_list = NULL; char var_file[256]; char *target, *new_target, *var_info; FILE *fp; char *follow_info, backup; char *full_path, *new_full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } if (new_folder == NULL || strlen(new_folder) <= 0) { return -1; } // 1. test if modifying the folder len = strlen(mount_path)+strlen("/")+strlen(folder); full_path = (char *)malloc(sizeof(char)*(len+1)); if (full_path == NULL) { return -1; } sprintf(full_path, "%s/%s", mount_path, folder); full_path[len] = 0; len = strlen(mount_path)+strlen("/")+strlen(new_folder); new_full_path = (char *)malloc(sizeof(char)*(len+1)); if (new_full_path == NULL) { return -1; } sprintf(new_full_path, "%s/%s", mount_path, new_folder); new_full_path[len] = 0; result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result == 0) { result = test_if_dir(full_path); if (result != 1) { free(full_path); free(new_full_path); return -1; } // the folder is existed but not in .__folder_list.txt add_folder(mount_path, folder); } // modify the folder result = rename(full_path, new_full_path); free(full_path); free(new_full_path); if (result != 0) { return -1; } len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; len = strlen("*")+strlen(new_folder)+strlen("="); new_target = (char *)malloc(sizeof(char)*(len+1)); if (new_target == NULL) { free(target); return -1; } sprintf(new_target, "*%s=", new_folder); new_target[len] = 0; // 3. add folder's right to every var file get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { // check if the created target is exist in the var file snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); var_info = read_whole_file(var_file); if (var_info == NULL) { continue; } if ((follow_info = strstr(var_info, target)) == NULL) { free(var_info); continue; } // 7. modify the folder's info in the var file fp = fopen(var_file, "w"); if (fp) { // write the info before target backup = *follow_info; *follow_info = 0; fprintf(fp, "%s", var_info); *follow_info = backup; // write the info before new_target fprintf(fp, "%s", new_target); // write the info after target follow_info += strlen(target); fprintf(fp, "%s", follow_info); fclose(fp); } free(var_info); } free_2_dimension_list(&acc_num, &account_list); // get the var_file for anonymous ftp snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); var_info = read_whole_file(var_file); if (var_info) { if ((follow_info = strstr(var_info, target))) { fp = fopen(var_file, "w"); if (fp) { // write the info before target backup = *follow_info; *follow_info = 0; fprintf(fp, "%s", var_info); *follow_info = backup; // write the info before new_target fprintf(fp, "%s", new_target); // write the info after target follow_info += strlen(target); fprintf(fp, "%s", follow_info); fclose(fp); } } free(var_info); } free(target); free(new_target); // 9. modify the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
int del_folder(const char *const mount_path, const char *const folder) { int result, i, len; int acc_num = 0; char **account_list = NULL; char var_file[256]; char *follow_info, backup; char *target, *var_info; FILE *fp; char *full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } // 1. test if deleting the folder len = strlen(mount_path)+strlen("/")+strlen(folder); full_path = (char *)malloc(sizeof(char)*(len+1)); if (full_path == NULL) { return -1; } sprintf(full_path, "%s/%s", mount_path, folder); full_path[len] = 0; result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result == 0) { result = test_if_dir(full_path); if (result != 1) { free(full_path); return -1; } } // 2. delete the folder result = rmdir(full_path); free(full_path); if (result != 0) { return -1; } // 4. get the target which is deleted in every var file len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; // 3. del folder's right to every var file get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { // delete the content about the folder snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); var_info = read_whole_file(var_file); if (var_info == NULL) { continue; } follow_info = upper_strstr(var_info, target); if (follow_info == NULL) { free(var_info); continue; } backup = *follow_info; *follow_info = 0; fp = fopen(var_file, "w"); if (fp == NULL) { *follow_info = backup; free(var_info); continue; } fprintf(fp, "%s", var_info); *follow_info = backup; while (*follow_info != 0 && *follow_info != '\n') ++follow_info; if (*follow_info != 0 && *(follow_info+1) != 0) { ++follow_info; fprintf(fp, "%s", follow_info); } fclose(fp); free(var_info); } free_2_dimension_list(&acc_num, &account_list); // get the var_file for anonymous ftp snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); var_info = read_whole_file(var_file); if (var_info == NULL) { goto MOD_FOLDER_END; } follow_info = upper_strstr(var_info, target); if (follow_info == NULL) { free(var_info); goto MOD_FOLDER_END; } backup = *follow_info; *follow_info = 0; fp = fopen(var_file, "w"); if (fp == NULL) { *follow_info = backup; free(var_info); goto MOD_FOLDER_END; } fprintf(fp, "%s", var_info); *follow_info = backup; while (*follow_info != 0 && *follow_info != '\n') ++follow_info; if (*follow_info != 0 && *(follow_info+1) != 0) { ++follow_info; fprintf(fp, "%s", follow_info); } fclose(fp); free(var_info); MOD_FOLDER_END: free(target); // 9. modify the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }