Exemple #1
0
static void exec_delete(Pcs pcs, struct params *params)
{
	PcsPanApiRes *res;
	PcsSList *slist = NULL;
	int i;

	printf("\nDelete");
	for (i = 0; i < params->args_count; i++) {
		printf(" %s", params->args[i]);
		if (!slist) {
			slist = pcs_slist_create_ex(params->args[i], -1);
			if (!slist) {
				printf("Cannot create slist\n");
				return;
			}
		}
		else if (!pcs_slist_add_ex(slist, params->args[i], -1)) {
			printf("\nCannot create slist\n");
			pcs_slist_destroy(slist);
			return;
		}
	}
	putchar('\n');

	res = pcs_delete(pcs, slist);
	pcs_slist_destroy(slist);
	if (!res) {
		printf("Delete failed. %s\n", pcs_strerror(pcs));
		return;
	}
	putchar('\n');
	print_pcs_pan_api_res(res);
	pcs_pan_api_res_destroy(res);
}
Exemple #2
0
static void exec_rename(Pcs pcs, struct params *params)
{
	PcsPanApiRes *res;
	PcsSList2 slist;
	slist.string1 = params->args[0]; /* path */
	slist.string2 = params->args[1]; /* new_name */
	slist.next = NULL;
	printf("\nRename %s to %s\n", slist.string1, slist.string1);
	res = pcs_rename(pcs, &slist);
	if (!res) {
		printf("Rename failed: %s\n", pcs_strerror(pcs));
		return;
	}
	putchar('\n');
	print_pcs_pan_api_res(res);
	pcs_pan_api_res_destroy(res);
}
Exemple #3
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;
}
Exemple #4
0
/*
根据传入参数,执行api函数。
opera: delete, rename, move, copy
*/
static PcsPanApiRes *pcs_pan_api_filemanager(Pcs handle, const char *opera, const char *filelist, int file_count)
{
	struct pcs *pcs = (struct pcs *)handle;
	cJSON *json, *item, *list, *val;
	char *url, *html, *postdata;
	int error, cnt, i;
	PcsPanApiRes *res = NULL;
	PcsPanApiResInfoList *tail, *ri;

	PcsBool err_no_space = PcsFalse, //errno = -10 剩余空间不足
		err_target_not_exist = PcsFalse, //errno = -8 文件已存在于目标文件夹中
		err_src_file_not_exist = PcsFalse, //errno = -9 文件不存在
		err_has_succ_items = PcsFalse; //是否存在处理成功的文件项

	url = pcs_build_pan_api_url(handle, "filemanager",
		"opera", opera,
		NULL);
	if (!url) {
		pcs_set_errmsg(handle, "Can't build the url.");
		return NULL;
	}

	postdata = pcs_http_build_post_data(pcs->http, "filelist", filelist, NULL);
	if (!postdata) {
		pcs_set_errmsg(handle, "Can't build the post data.");
		pcs_free(url);
		return NULL;
	}
	html = pcs_http_post(pcs->http, url, postdata, PcsTrue);
	pcs_free(url);
	pcs_free(postdata);
	if (!html) {
		pcs_set_errmsg(handle, "Can't get response from the server.");
		return NULL;
	}
	json = cJSON_Parse(html);
	if (!json){
		pcs_set_errmsg(handle, "Can't parse the response as object. Response: %s", html);
		return NULL;
	}
	item = cJSON_GetObjectItem(json, "errno");
	if (!item) {
		pcs_set_errmsg(handle, "Can't read res.errno. Response: %s", html);
		cJSON_Delete(json);
		return NULL;
	}
	error = item->valueint;
	res = pcs_pan_api_res_create();
	if (!res) {
		pcs_set_errmsg(handle, "Can't create the object: PcsPanApiRes");
		cJSON_Delete(json);
		return NULL;
	}
	res->error = error;

	list = cJSON_GetObjectItem(json, "info");
	if (!list) {
		pcs_set_errmsg(handle, "Can't read res.info. Response: %s", html);
		cJSON_Delete(json);
		return NULL;
	}

	cnt = cJSON_GetArraySize(list);
	for (i = 0; i < cnt; i++) {
		item = cJSON_GetArrayItem(list, i);
		ri = pcs_pan_api_res_infolist_create();
		if (!ri) {
			pcs_set_errmsg(handle, "Can't create the object: PcsPanApiResInfoList");
			cJSON_Delete(json);
			if (res) pcs_pan_api_res_destroy(res);
			return NULL;
		}
		val = cJSON_GetObjectItem(item, "path");
		if (val)
			ri->info.path = pcs_utils_strdup(val->valuestring);
		val = cJSON_GetObjectItem(item, "errno");
		ri->info.error = val->valueint;
		if (!res->info_list) {
			res->info_list = tail = ri;
		}
		else {
			tail->next = ri;
			tail = ri;
		}
		switch (ri->info.error) {
		case 0: //I:处理成功
			err_has_succ_items = PcsTrue;
			break;
		case -8: //D:文件已存在于目标文件夹中
			err_target_not_exist = PcsTrue;
			break;
		case -9: //C:文件不存在
			err_src_file_not_exist = PcsTrue;
			break;
		case -10: //G:剩余空间不足
			err_no_space = PcsTrue;
			break;
		}
	}
	cJSON_Delete(json);
	if (res->error != 0) {
		int is_move = strcmp(opera, "move") == 0;
		if (is_move || strcmp(opera, "copy") == 0) {
			if (res->error == 12) {
				if (!err_no_space && !err_src_file_not_exist && !err_target_not_exist) {
					pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,请稍候重试");
				}
				else {
					if (cnt == file_count && !err_has_succ_items) {
						if (err_no_space)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,剩余空间不足");
						else if (err_target_not_exist)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,文件已存在于目标文件夹中");
						else if (err_src_file_not_exist)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,待处理文件不存在");
					}
					else {
						if (err_no_space)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,剩余空间不足");
						else if (err_target_not_exist)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,部分文件已存在于目标文件夹中");
						else if (err_src_file_not_exist)
							pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,部分待处理文件不存在");
					}
				}
			}
			else {
				pcs_set_errmsg(handle, "%s%s", is_move ? "移动" : "复制", "失败,未知错误");
			}
		}
		else if (strcmp(opera, "delete") == 0) {
			pcs_set_errmsg(handle, "删除文件失败,请稍候重试");
		}
		else if (strcmp(opera, "rename") == 0) {
			if (res->error == -8 || res->error == -9 || (cnt > 0 && (res->info_list->info.error == -8 || res->info_list->info.error == -9)))
				pcs_set_errmsg(handle, "名称已在使用,请使用其他名称");
			else if (res->error == 12 && cnt > 0 && res->info_list->info.error == -9)
				pcs_set_errmsg(handle, "重命名失败,文件已被删除或移走");
			else
				pcs_set_errmsg(handle, "文件(夹)重命名失败, 网络错误");
		}
	}
	return res;
}