int my_dirent_remove(const char *path) { my_dirent root, *i; int res = 0; int ft = is_dir_or_file(path); memset(&root, 0, sizeof(my_dirent)); if (ft == 2) { if (filesearch(path, &root, 0)) { res = -1; } else { i = root.next; while(i) { if (my_dirent_remove(i->path)) { res = -1; break; } i = i->next; } } my_dirent_destroy(root.next); if (!i) { res = remove(path); } } else { res = remove(path); } return res; }
static int exec_download_dir(Pcs pcs, const char *remote_path, const char *local_path, int force, int recursion, int synch) { int ft; hashtable *ht; my_dirent *ents, *ent, *local_file; PcsFileInfoList *remote_files; PcsFileInfoListIterater iterater; PcsFileInfo *file; PcsRes dres; char *dest_path; struct download_state ddstat; FILE *pf; printf("\nDownload %s to %s\n", remote_path, local_path); ft = is_dir_or_file(local_path); if (ft == 1) { printf("Invalidate target: %s is not the directory.\n", local_path); return -1; } else if (ft == 2) { //if (!force) { // printf("execute upload command failed: The target %s exist.\n", remote_path); // return -1; //} } else { if (mkdir(local_path)) { printf("Cannot create the directory %s.\n", local_path); return -1; } } ents = list_dir(local_path, 0); ht = hashtable_create(100, 1, NULL); if (!ht) { printf("Cannot create hashtable.\n"); my_dirent_destroy(ents); return -1; } ent = ents; while(ent) { if (hashtable_add(ht, ent->path, ent)) { printf("Cannot add item into hashtable.\n"); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } ent = ent->next; } remote_files = get_file_list(pcs, remote_path, NULL, PcsFalse, PcsFalse); if (!remote_files) { hashtable_destroy(ht); my_dirent_destroy(ents); if (pcs_strerror(pcs)) { printf("Cannot list the remote files.\n"); return -1; } return 0; } pcs_filist_iterater_init(remote_files, &iterater, PcsFalse); while(pcs_filist_iterater_next(&iterater)) { file = iterater.current; dest_path = combin_local_path(local_path, file->server_filename); if (!dest_path) { printf("Cannot alloca memory. 0%s, %s\n", local_path, file->server_filename); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } if (file->isdir) { if (force || recursion) { local_file = (my_dirent *)hashtable_get(ht, dest_path); if (local_file) { local_file->user_flag = 1; printf("[SKIP] d %s\n", file->path); } else if (recursion) { if (mkdir(dest_path)) { printf("[FAIL] d %s Cannot create the folder %s\n", file->path, dest_path); pcs_free(dest_path); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } else { printf("[SUCC] d %s => %s\n", file->path, dest_path); } } } } else { local_file = (my_dirent *)hashtable_get(ht, dest_path); if (local_file) local_file->user_flag = 1; if (force || !local_file || my_dirent_get_mtime(local_file) < ((time_t)file->server_mtime)) { ddstat.msg = (char *) pcs_malloc(strlen(dest_path) + 12); if (!ddstat.msg) { printf("Cannot alloca memory. 1%s, %s\n", local_path, file->server_filename); pcs_free(dest_path); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } sprintf(ddstat.msg, "Download %s ", dest_path); pf = fopen(dest_path, "wb"); if (!pf) { printf("Cannot create the local file: %s\n", dest_path); pcs_free(dest_path); pcs_free(ddstat.msg); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } ddstat.pf = pf; ddstat.size = 0; pcs_setopt(pcs, PCS_OPTION_DOWNLOAD_WRITE_FUNCTION_DATA, &ddstat); dres = pcs_download(pcs, file->path); pcs_free(ddstat.msg); fclose(ddstat.pf); if (dres != PCS_OK) { printf("[FAIL] - %s => %s\n", file->path, dest_path); pcs_free(dest_path); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } my_dirent_utime(dest_path, file->server_mtime); printf("[SUCC] - %s => %s\n", file->path, dest_path); } else { printf("[SKIP] - %s\n", file->path); } } pcs_free(dest_path); } if (recursion) { pcs_filist_iterater_init(remote_files, &iterater, PcsFalse); while(pcs_filist_iterater_next(&iterater)) { file = iterater.current; dest_path = combin_local_path(local_path, file->server_filename); if (!dest_path) { printf("Cannot alloca memory. 2%s, %s\n", local_path, file->server_filename); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } if (file->isdir) { if (exec_download_dir(pcs, file->path, dest_path, force, recursion, synch)) { pcs_free(dest_path); pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return -1; } } pcs_free(dest_path); } } if (synch) { hashtable_iterater *iterater; iterater = hashtable_iterater_create(ht); hashtable_iterater_reset(iterater); while(hashtable_iterater_next(iterater)) { local_file = (my_dirent *)hashtable_iterater_current(iterater); if (!local_file->user_flag) { if (my_dirent_remove(local_file->path)) { printf("[DELETE] [FAIL] %s %s \n", local_file->is_dir ? "d" : "-", local_file->path); } else { printf("[DELETE] [SUCC] %s %s \n", local_file->is_dir ? "d" : "-", local_file->path); } } } hashtable_iterater_destroy(iterater); } pcs_filist_destroy(remote_files); hashtable_destroy(ht); my_dirent_destroy(ents); return 0; }