static void exec_upload_file(Pcs pcs, struct params *params) { int ft = 0; PcsFileInfo *res, *meta; meta = pcs_meta(pcs, params->args[1]); if (meta) { if (meta->isdir) ft = 2; else ft = 1; pcs_fileinfo_destroy(meta); } if (ft == 2) { printf("The target %s exist and it is a folder.\n", params->args[1]); return; } else if (ft == 1 && !params->is_force) { printf("The target %s exist. You can use -f to force overwrite the file.\n", params->args[1]); return; } pcs_setopt(pcs, PCS_OPTION_PROGRESS_FUNCTION_DATE, params->args[0]); pcs_setopt(pcs, PCS_OPTION_PROGRESS, (void *)PcsTrue); res = pcs_upload(pcs, params->args[1], params->is_force, params->args[0]); pcs_setopt(pcs, PCS_OPTION_PROGRESS, (void *)PcsFalse); pcs_setopt(pcs, PCS_OPTION_PROGRESS_FUNCTION_DATE, NULL); if (!res) { printf("Execute upload command failed: %s\n", pcs_strerror(pcs)); return; } if (res->path[0]) { printf("Upload %s success, remote location is %s.\n", params->args[0], res->path); pcs_fileinfo_destroy(res); return; } pcs_fileinfo_destroy(res); }
PCS_API PcsRes pcs_setopts(Pcs handle, ...) { PcsRes res = PCS_OK; PcsOption opt; void *val; va_list args; va_start(args, handle); while((opt = va_arg(args, PcsOption)) != PCS_OPTION_END) { val = va_arg(args, void *); if ((res = pcs_setopt(handle, opt, val)) != PCS_OK) { break; } } va_end(args); return res; }
static void exec_download_file(Pcs pcs, const char *remote_path, const char *local_path, PcsFileInfo *meta, int force) { int ft; FILE *pf; PcsRes res; struct download_state ds; ft = is_dir_or_file(local_path); if (ft == 2) { printf("The file %s is directory.\n", local_path); return; } else if (ft == 1) { if (!force) { printf("The file %s is exist.\n", local_path); return; } } pf = fopen(local_path, "wb"); if (!pf) { printf("Cannot create the local file: %s\n", local_path); return; } putchar('\n'); ds.msg = NULL; ds.pf = pf; ds.size = 0; pcs_setopt(pcs, PCS_OPTION_DOWNLOAD_WRITE_FUNCTION_DATA, &ds); res = pcs_download(pcs, remote_path); fclose(pf); my_dirent_utime(local_path, meta->server_mtime); putchar('\n'); putchar('\n'); if (res != PCS_OK) { printf("Download %s fail.\n", remote_path); } else { printf("Download %s success. \nLocal Path: %s\n", remote_path, local_path); } }
static void exec_download(Pcs pcs, struct params *params) { PcsFileInfo *meta; pcs_setopt(pcs, PCS_OPTION_DOWNLOAD_WRITE_FUNCTION, &cb_download_write); printf("\nDownload %s to %s\n", params->args[0], params->args[1]); meta = pcs_meta(pcs, params->args[0]); if (!meta) { printf("File not exist: %s\n", params->args[0]); return; } if (meta->isdir) { if (exec_download_dir(pcs, params->args[0], params->args[1], params->is_force, params->is_recursion, params->is_synch)) { printf("\nFailed.\n", params->args[0]); } else { printf("\nAll Successfully\n"); } } else { exec_download_file(pcs, params->args[0], params->args[1], meta, params->is_force); } pcs_fileinfo_destroy(meta); }
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; }
int shell(struct params *params) { PcsRes pcsres; const char *cookie_file; Pcs pcs; if (params->cookie) cookie_file = params->cookie; else cookie_file = get_default_cookie_file(params->username); printf("COOKIE FILE: %s\n", cookie_file); /* 创建一个Pcs对象 */ pcs = pcs_create(cookie_file); /* 设定如何获取验证码 */ if (params->use_urlc) { pcs_setopt(pcs, PCS_OPTION_CAPTCHA_FUNCTION, cb_get_verify_code_byurlc); } else { pcs_setopt(pcs, PCS_OPTION_CAPTCHA_FUNCTION, cb_get_verify_code); } if (params->is_verbose) { pcs_setopt(pcs, PCS_OPTION_HTTP_RESPONSE_FUNCTION, cb_pcs_http_response); } pcs_setopts(pcs, PCS_OPTION_PROGRESS_FUNCTION, cb_upload_progress, PCS_OPTION_PROGRESS, PcsFalse, PCS_OPTION_END); if ((pcsres = pcs_islogin(pcs)) != PCS_LOGIN) { if (!params->username) { printf("Your session is time out, please restart with -u option\n"); pcs_destroy(pcs); return -1; } pcs_setopt(pcs, PCS_OPTION_USERNAME, params->username); if (!params->password) { char password[50]; printf("Password: "******"Login Failed: %s\n", pcs_strerror(pcs)); pcs_destroy(pcs); return -1; } } else { if (params->username && pcs_utils_strcmpi(pcs_sysUID(pcs), params->username) != 0) { char flag[8] = {0}; printf("You have been logged in with %s, but you specified %s,\ncontinue?(yes|no): \n", pcs_sysUID(pcs), params->username); get_string_from_std_input(flag, 4); if (pcs_utils_strcmpi(flag, "yes") && pcs_utils_strcmpi(flag, "y")) { pcs_destroy(pcs); return -1; } } } printf("UID: %s\n", pcs_sysUID(pcs)); show_quota(pcs); exec_cmd(pcs, params); pcs_destroy(pcs); return 0; }
static int exec_upload_dir(Pcs pcs, const char *local_path, const char *remote_path, int force, int recursion, int synch) { int ft = 0, res = 0, ric = 0; char *dest_path = NULL; PcsFileInfo *meta = NULL, *remote_file = NULL; PcsFileInfoList *remote_filelist = NULL; PcsFileInfoListIterater iterater = {0}; PcsRes pcsres = PCS_NONE; PcsPanApiRes *pcsapires = NULL; hashtable *ht = NULL; my_dirent *ents = NULL, *ent = NULL; printf("\nUpload %s to %s\n", local_path, remote_path); exec_upload_dir_label_1: meta = pcs_meta(pcs, remote_path); if (meta) { if (meta->isdir) ft = 2; else ft = 1; pcs_fileinfo_destroy(meta); meta = NULL; } else if (pcs_strerror(pcs)) { printf("Cannot get the meta for %s: %s\n", remote_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') goto exec_upload_dir_label_1; else return -1; } if (ft == 1) { if (force) { PcsSList slist = { (char *)remote_path, 0 }; printf("Delete the %s, since the target is not the directory and you specify -f.\n", remote_path); exec_upload_dir_label_2: pcsapires = pcs_delete(pcs, &slist); if (!pcsapires) { printf("[DELETE] [FAIL] - %s: %s\n", remote_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') goto exec_upload_dir_label_2; else return -1; } else { printf("[DELETE] [SUCC] - %s \n", remote_path); pcs_pan_api_res_destroy(pcsapires); } exec_upload_dir_label_3: pcsres = pcs_mkdir(pcs, remote_path); if (pcsres != PCS_OK) { printf("[FAIL] Cannot create the directory %s: %s\n", remote_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') goto exec_upload_dir_label_3; else return -1; } else { printf("[SUCC] Create directory %s\n", remote_path); goto exec_upload_dir_label_5; } } else { printf("The target %s is not the directory. You can use -f to force recreate the directory.\n", remote_path); } return -1; } else if (ft == 2) { } else { exec_upload_dir_label_4: pcsres = pcs_mkdir(pcs, remote_path); if (pcsres != PCS_OK) { printf("[FAIL] Cannot create the directory %s: %s\n", remote_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') goto exec_upload_dir_label_4; else return -1; } else { printf("[SUCC] Create directory %s\n", remote_path); goto exec_upload_dir_label_5; } } exec_upload_dir_label_5: printf("Get remote file list %s.\n", remote_path); remote_filelist = get_file_list(pcs, remote_path, NULL, PcsFalse, PcsFalse); if (!remote_filelist && pcs_strerror(pcs)) { printf("[FAIL] Cannot list the directory %s: %s\n", remote_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') goto exec_upload_dir_label_5; else return -1; } if (remote_filelist) { ht = hashtable_create(remote_filelist->count, 1, NULL); if (!ht) { printf("Cannot create hashtable.\n"); goto exec_upload_dir_label_00; } pcs_filist_iterater_init(remote_filelist, &iterater, PcsFalse); while(pcs_filist_iterater_next(&iterater)) { remote_file = iterater.current; if (hashtable_add(ht, remote_file->path, remote_file)) { printf("Cannot add object to hashtable.\n"); goto exec_upload_dir_label_00; } } } ents = list_dir(local_path, 0); if (!ents) { printf("[SKIP] d %s empty directory.\n", local_path); goto exec_upload_dir_label_0; } ent = ents; while(ent) { dest_path = combin_remote_path(remote_path, ent->filename); if (!dest_path) { printf("Cannot combin the path.\n"); goto exec_upload_dir_label_00; } if (ent->is_dir) { if (force || recursion) { remote_file = ht ? (PcsFileInfo *)hashtable_get(ht, dest_path) : NULL; if (remote_file) { remote_file->user_flag = 1; printf("[SKIP] d %s\n", ent->path); } else if (recursion) { exec_upload_dir_label_6: pcsres = pcs_mkdir(pcs, dest_path); if (pcsres != PCS_OK) { printf("[FAIL] d %s Cannot create the directory %s: %s\n", ent->path, dest_path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') { goto exec_upload_dir_label_6; } else { goto exec_upload_dir_label_00; } } else { printf("[SUCC] d %s => %s\n", ent->path, dest_path); } } } } else { remote_file = ht ? (PcsFileInfo *)hashtable_get(ht, dest_path) : NULL; if (remote_file) remote_file->user_flag = 1; if (force || !remote_file || ((time_t)remote_file->server_mtime) < my_dirent_get_mtime(ent)) { exec_upload_dir_label_7: pcs_setopt(pcs, PCS_OPTION_PROGRESS_FUNCTION_DATE, ent->path); pcs_setopt(pcs, PCS_OPTION_PROGRESS, (void *)PcsTrue); meta = pcs_upload(pcs, dest_path, PcsTrue, ent->path); pcs_setopt(pcs, PCS_OPTION_PROGRESS, (void *)PcsFalse); pcs_setopt(pcs, PCS_OPTION_PROGRESS_FUNCTION_DATE, NULL); if (!meta) { printf("[FAIL] - %s Cannot upload the file: %s\n", ent->path, pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') { goto exec_upload_dir_label_7; } else { goto exec_upload_dir_label_00; } } printf("[SUCC] - %s => %s\n", ent->path, meta->path); pcs_fileinfo_destroy(meta); meta = NULL; } else { printf("[SKIP] - %s\n", ent->path); } } ent = ent->next; pcs_free(dest_path); dest_path = NULL; } if (recursion) { ent = ents; while(ent) { dest_path = combin_remote_path(remote_path, ent->filename); if (!dest_path) { printf("Cannot combin the path.\n"); goto exec_upload_dir_label_00; } if (ent->is_dir) { if (exec_upload_dir(pcs, ent->path, dest_path, force, recursion, synch)) { goto exec_upload_dir_label_00; } } ent = ent->next; pcs_free(dest_path); dest_path = NULL; } } if (synch && ht) { hashtable_iterater *iterater; PcsSList slist = {0,0}; iterater = hashtable_iterater_create(ht); hashtable_iterater_reset(iterater); while(hashtable_iterater_next(iterater)) { remote_file = (PcsFileInfo *)hashtable_iterater_current(iterater); if (!remote_file->user_flag) { slist.string = remote_file->path; exec_upload_dir_label_8: pcsapires = pcs_delete(pcs, &slist); if (!pcsapires) { printf("[DELETE] [FAIL] %s %s Cannot delete the %s: %s\n", remote_file->isdir ? "d" : "-", remote_file->path, remote_file->isdir ? "directory" : "fire", pcs_strerror(pcs)); ric = retry_cancel(); if (ric == 'r') { goto exec_upload_dir_label_8; } else { goto exec_upload_dir_label_00; } } else { printf("[DELETE] [SUCC] %s %s \n", remote_file->isdir ? "d" : "-", remote_file->path); pcs_pan_api_res_destroy(pcsapires); } } } hashtable_iterater_destroy(iterater); } goto exec_upload_dir_label_0; exec_upload_dir_label_00: res = -1; exec_upload_dir_label_0: if (dest_path) pcs_free(dest_path); if (meta) pcs_fileinfo_destroy(meta); if (ht) hashtable_destroy(ht); if (remote_filelist) pcs_filist_destroy(remote_filelist); if (ents) my_dirent_destroy(ents); return res; }