int test_of_var_files(const char *const mount_path) { doSystem("rm -f /%s/.__MediaServer_var.txt", mount_path); create_if_no_var_files(mount_path); // According to the old folder_list, add the new folder. initial_folder_list_in_mount_path(mount_path); // get the new folder_list. create_if_no_var_files(mount_path); // According to the new folder_list, add the new var file. return 0; }
int initial_all_var_file_in_mount_path(const char *const mount_path) { int i; int acc_num = 0; char **account_list = NULL; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } // delete all var files doSystem("rm -f /%s/.__*.txt", mount_path); // initial the var file for ftp anonymous initial_one_var_file_in_mount_path(FTP_ANONYMOUS_USER, mount_path); // get the account number and account_list get_account_list(&acc_num, &account_list); // initial the var file for (i = 0; i < acc_num; ++i) initial_one_var_file_in_mount_path(account_list[i], mount_path); free_2_dimension_list(&acc_num, &account_list); // initial the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: %s MOUNT_PATH\n", argv[0]); return -1; } initial_folder_list_in_mount_path(argv[1]); return 0; }
int get_folder_list_in_mount_path(const char *const mount_path, int *sh_num, char ***folder_list) { char **tmp_folder_list, target[16]; int len, i; char *list_file, *list_info; char *follow_info, *follow_info_end, backup; // 1. get list file len = strlen(mount_path)+strlen("/.__folder_list.txt"); list_file = (char *)malloc(sizeof(char)*(len+1)); if (list_file == NULL) { return -1; } sprintf(list_file, "%s/.__folder_list.txt", mount_path); list_file[len] = 0; // 2. read if the list file is existed if (!test_if_file(list_file)) initial_folder_list_in_mount_path(mount_path); list_info = read_whole_file(list_file); if (list_info == NULL) { free(list_file); return -1; } free(list_file); // 3. find sh_num follow_info = strstr(list_info, "sh_num="); if (follow_info == NULL) { free(list_info); return -1; } follow_info += strlen("sh_num="); follow_info_end = follow_info; while (*follow_info_end != 0 && *follow_info_end != '\n') ++follow_info_end; if (*follow_info_end == 0) { free(list_info); return -1; } backup = *follow_info_end; *follow_info_end = 0; *sh_num = atoi(follow_info); *follow_info_end = backup; if (*sh_num <= 0) return 0; // 4. get folder list from the folder list file tmp_folder_list = (char **)malloc(sizeof(char *)*((*sh_num)+1)); if (tmp_folder_list == NULL) { free(list_info); return -1; } for (i = 0; i < *sh_num; ++i) { // 5. get folder name memset(target, 0, 16); sprintf(target, "\nsh_name%d=", i); follow_info = strstr(list_info, target); if (follow_info == NULL) { free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } follow_info += strlen(target); follow_info_end = follow_info; while (*follow_info_end != 0 && *follow_info_end != '\n') ++follow_info_end; if (*follow_info_end == 0) { free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } backup = *follow_info_end; *follow_info_end = 0; len = strlen(follow_info); tmp_folder_list[i] = (char *)malloc(sizeof(char)*(len+1)); if (tmp_folder_list == NULL) { *follow_info_end = backup; free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } strcpy(tmp_folder_list[i], follow_info); tmp_folder_list[i][len] = 0; *follow_info_end = backup; } if (*sh_num > 0) *folder_list = tmp_folder_list; return 0; }
int add_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 *target, *var_info; FILE *fp; int samba_right, ftp_right, dms_right; char *full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } // test if creatting the folder result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result != 0) { return -1; } // create 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; umask(0000); result = mkdir(full_path, 0777); free(full_path); if (result != 0) { return -1; } // get the samba right, ftp right and target samba_right = DEFAULT_SAMBA_RIGHT; ftp_right = DEFAULT_FTP_RIGHT; dms_right = DEFAULT_DMS_RIGHT; len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; // 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) { ; } else if (strstr(var_info, target) != NULL) { free(var_info); continue; } else free(var_info); // 7. add the folder's info in the var file fp = fopen(var_file, "a+"); if (fp) { fprintf(fp, "%s%d%d%d\n", target, samba_right, ftp_right, dms_right); fclose(fp); } } 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); fp = fopen(var_file, "a+"); if (fp) { fprintf(fp, "%s%d%d%d\n", target, 1, 1, 1); fclose(fp); } free(target); // 9. add the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
int set_permission(const char *account, const char *mount_path, const char *folder, const char *protocol, const int flag) { FILE *fp; char var_file[256]; char *target, *follow_info, *var_info; int len, result; if (flag < 0 || flag > 3) { return -1; } // 2. get the content of the var_file of the account snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account); var_info = read_whole_file(var_file); if (var_info == NULL) { initial_one_var_file_in_mount_path(account, mount_path); sleep(1); var_info = read_whole_file(var_file); if (var_info == NULL) { return -1; } } // 3. get the target in the content len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { free(var_info); return -1; } sprintf(target, "*%s=", folder); target[len] = 0; // 4. judge if the target is in the var file. follow_info = upper_strstr(var_info, target); if (follow_info == NULL) { free(var_info); result = initial_folder_list_in_mount_path(mount_path); fp = fopen(var_file, "a+"); if (fp == NULL) { free(target); return -1; } fprintf(fp, "%s", target); free(target); // 5.1 change the right of folder if (!strcmp(protocol, "cifs")) fprintf(fp, "%d%d%d\n", flag, 0, DEFAULT_DMS_RIGHT); else if (!strcmp(protocol, "ftp")) fprintf(fp, "%d%d%d\n", 0, flag, DEFAULT_DMS_RIGHT); else if (!strcmp(protocol, "dms")) fprintf(fp, "%d%d%d\n", 0, 0, flag); else{ fclose(fp); return -1; } fclose(fp); if (!strcmp(protocol, "cifs")) system("/sbin/run_samba"); return 0; } free(target); follow_info += len; if (follow_info[3] != '\n') { free(var_info); return -1; } // 5.2. change the right of folder if (!strcmp(protocol, "cifs")) { if (follow_info[0] == '0'+flag) { free(var_info); return 0; } follow_info[0] = '0'+flag; } else if (!strcmp(protocol, "ftp")) { if (follow_info[1] == '0'+flag) { free(var_info); return 0; } follow_info[1] = '0'+flag; } else if (!strcmp(protocol, "dms")) { if (follow_info[2] == '0'+flag) { free(var_info); return 0; } follow_info[2] = '0'+flag; } else{ free(var_info); return -1; } // 6. rewrite the var file. fp = fopen(var_file, "w"); if (fp == NULL) { free(var_info); return -1; } fprintf(fp, "%s", var_info); fclose(fp); free(var_info); if (!strcmp(protocol, "cifs")) system("/sbin/run_samba"); 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; }