Пример #1
0
static int ftp_send_ext(ftp_host_info_t *server, FILE *control_stream, char *local_path, char *local_data, unsigned long data_size, char *server_path)
{
	int ret = -1;
	char buf[512];
	int fd_data = -1;
	int response = 0;
	FTP_PARAM ftp_param;

	// Connect to the data socket
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) 
	{
		return -1;
	}

	fd_data = connect_ftpdata(server, buf);
	if (fd_data < 0)
	{
		return -1;
	}

	//printf("Entry ftp_send_ext function ftp_param.strPath = %s\n", ftp_param.strPath);
	//if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) // [zhb][delete][2006-09-18]
	#if 1
	//改变ftp 工作目录
	ret = getFtpParam(&ftp_param);
	if (ret < 0)
	{
		return -1;
	}

#if 1
	if (ftpcmd("MKD ", ftp_param.strPath, control_stream, buf) != 250) 
	{
		printf("%s\n", buf);
		//close(fd_data);
		//return -1;
	}
#endif

	if (ftpcmd("CWD ", ftp_param.strPath, control_stream, buf) != 250) 
	{
		printf("%s\n", buf);
		close(fd_data);
		return -1;
	}
	#endif

	#if 0
	if (ftpcmd("CWD ", "./", control_stream, buf) != 250) 
	{
		printf("%s\n", buf);
		close(fd_data);
		return -1;
	}
	#endif
	
	
	
	// get the local file
	sprintf(buf, "ALLO %lu", (unsigned long)data_size);
	response = ftpcmd(buf, NULL, control_stream, buf);	
	switch (response) 
	{
	case 200:
	case 202:
		break;
	default:
		printf("ALLO error(%s)!\n", buf);
		break;
	}
	
	response = ftpcmd("STOR ", local_path, control_stream, buf);
	switch (response) 
	{
	case 125:
	case 150:
		break;
	default:
		printf("STOR error!\n");
	}

	// transfer the file
	if ((ret = copyfd_size_ext(local_data, fd_data, data_size)) == -1) 
	{
		//printf("copyfd_size_ext() Failed!\n");
		// Add the code by lvjh, 2009-04-02
		close(fd_data);
		
		return -1;
	}	

	// close it all down
	close(fd_data);
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) 
	{
		printf("ftpcmd(%s) Failed!\n", buf);
		return -1;
	}
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}
Пример #2
0
static int ftp_send(ftp_host_info_t *server, FILE *control_stream, char *local_path, char *server_path)
{
	int ret = -1;
	off_t filesize = 0;
	struct stat sbuf;
	char buf[512];
	int fd_data = -1;
	int fd_local = -1;
	int response = 0;
	FTP_PARAM ftp_param;

	// Connect to the data socket
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) 
	{
		return -1;
	}

	fd_data = connect_ftpdata(server, buf);
	if (fd_data < 0)
	{
		return -1;
	}
	
	//if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) // [zhb][delete][2006-09-18]
	//改变ftp 工作目录
	ret = getFtpParam(&ftp_param);
	if (ret < 0)
	{
		return -1;
	}

	if (ftpcmd("CWD ", ftp_param.strPath, control_stream, buf) != 250) 
	{
		printf("%s\n", buf);
		close(fd_data);
		return -1;
	}
	
	// get the local file
	if ((local_path[0] == '-') && (local_path[1] == '\0')) 
	{
		fd_local = STDIN_FILENO;
	} 

	else 
	{		
		fd_local = open(local_path, O_RDONLY);
		fstat(fd_local, &sbuf);

		sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
		filesize = sbuf.st_size;
		response = ftpcmd(buf, NULL, control_stream, buf);
		
		switch (response) 
		{
		case 200:
		case 202:
			break;
		default:
			//close(fd_local); // [zhb][delete][2006-09-18]
			printf("ALLO error(%s)!\n", buf);
			break;
		}
	}
	response = ftpcmd("STOR ", local_path, control_stream, buf);
	switch (response) 
	{
	case 125:
	case 150:
		break;
	default:
		printf("STOR error!\n");
		close(fd_local);		
	}

	// transfer the file
	if ((ret = copyfd_size(fd_local, fd_data, filesize)) == -1) 
	{
		//printf("copyfd_size() Failed!\n");
		return -1;
	}	

	// close it all down
	close(fd_data);
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) 
	{
		printf("ftpcmd(%s) Failed!\n", buf);
		return -1;
	}
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}
Пример #3
0
int ftp_msend2(ftp_host_info_t *server, FILE *control_stream,
		file_path_t *file_path, int file_cnt)
{
	struct stat sbuf;
	char buf[512];
	int fd_data;
	int* fd_local = NULL;
	int response; 
	int i = 0;

	if(file_cnt == 0)
		return -1;

	fd_local = (int*)malloc(sizeof(int)*file_cnt);
	if(fd_local == NULL){
		lib_error("[%s] malloc err!!", __FUNCTION__);
		return -1;
	}

	for(i=0;i<file_cnt;i++){

		memset(buf, 0, sizeof(buf));

		/*  Connect to the data socket */
		if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
			ftp_die("PASV", buf);
		}

		fd_data = connect_ftpdata(server, buf);
		if(fd_data < 0){
			lib_error("[%s] connect_ftpdata err!!", __FUNCTION__);
			return -1;
		}

		/* get the local file */
		fd_local[i] = STDIN_FILENO;
		if (NOT_LONE_DASH(file_path[i].local_path)) {

			//printf("%d: lc_path : %s, rt_path:%s\n",i, file_path[i].local_path, file_path[i].server_path);
			fd_local[i] = open(file_path[i].local_path, O_RDONLY, 0666);
			if(fd_local[i] < 0){
				lib_error("[%s] open err!!", __FUNCTION__);
				close(fd_data);
				return -1;
			}
			fstat(fd_local[i], &sbuf);

			sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
			response = ftpcmd(buf, NULL, control_stream, buf);
			switch (response) {
			case 200:
			case 202:
				break;
			default:
				ftp_die("ALLO", buf);
				break;
			}
		}

		response = ftpcmd("STOR", file_path[i].server_path, control_stream, buf);
		switch (response) {
		case 125:
		case 150:
			break;
		default:
			ftp_die("STOR", buf);
			close(fd_local[i]);
			close(fd_data);
			goto ftp_msend_quit_error;
		}

		/* transfer the file  */
		if (copyfd_eof(fd_local[i], fd_data, 0) == -1) {
			close(fd_data);
			close(fd_local[i]);
			goto ftp_msend_quit_error;
		}

		/* close it all down */
		close(fd_local[i]);

		close(fd_data);
		if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
			ftp_die("close", buf);
		}

	}

	ftpcmd("QUIT", NULL, control_stream, buf);
	return 0;

ftp_msend_quit_error:
	ftpcmd("QUIT", NULL, control_stream, buf);
	return -1;
}
Пример #4
0
static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, char *local_path, char *server_path)
{
	int ret = -1;
	char buf[512];
	off_t filesize = 0;
	int fd_data = -1;
	int fd_local = -1;
	off_t beg_range = 0;

	/* Connect to the data socket */
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) 
	{
		printf("ftpcmd(PASV) Failed!\n");
		return -1;
	}
	
	fd_data = connect_ftpdata(server, buf);
	if (fd_data == -1)
	{
		printf("connect_ftpdata(%s) Failed!\n", buf);
		return -1;
	}

	if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) 
	{
		unsigned long value=filesize;
		//if (strtoul(buf + 4, &value, 10))
		if (safe_strtoul(buf + 4, &value))
		{			
			close(fd_data);
			return -1;
		}
		filesize = value;
	}

	if ((local_path[0] == '-') && (local_path[1] == '\0')) 
	{
		fd_local = STDOUT_FILENO;
		do_continue = 0;
	}

	if (do_continue) 
	{
		struct stat sbuf;
		if (lstat(local_path, &sbuf) < 0) 
		{
			close(fd_data);
			return -1;
		}
		if (sbuf.st_size > 0) 
		{
			beg_range = sbuf.st_size;
		} 
		else 
		{
			do_continue = 0;
		}
	}

	if (do_continue) 
	{
		sprintf(buf, "REST %ld", (long)beg_range);
		if (ftpcmd(buf, NULL, control_stream, buf) != 350) 
		{
			do_continue = 0;
		} 
		else 
		{
			filesize -= beg_range;
		}
	}

	if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) 
	{
		close(fd_data);
		return -1;
	}
	
	/* only make a local file if we know that one exists on the remote server */
	if (fd_local == -1) 
	{
		if (do_continue) 
		{
			fd_local = open(local_path, O_APPEND | O_WRONLY, 0777);
		} 
		else 
		{
			fd_local = open(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0777);
		}
	}

	if (fd_local < 0)
	{
		close(fd_data);
		return -1;
	}
	
	// Copy the file
	if (copyfd_size(fd_data, fd_local, filesize) == -1) 
	{		
		close(fd_data);
		close(fd_local);
		return -1;
	}	

	// close it all down
	close(fd_data);
	close(fd_local);
	
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) 
	{
		return -1;
	}
	
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}
Пример #5
0
int ftp_send(ftp_host_info_t *server, FILE *control_stream,
		const char *server_path, char *local_path)
{
	struct stat sbuf;
	char buf[512];
	int fd_data;
	int fd_local;
	int response; 
	
	/*  Connect to the data socket */
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
		ftp_die("PASV", buf);
	}

	fd_data = connect_ftpdata(server, buf);
	if(fd_data < 0){
		lib_error("[%s] connect_ftpdata err!!", __FUNCTION__);
		return -1;
	}

	/* get the local file */
	fd_local = STDIN_FILENO;
	if (NOT_LONE_DASH(local_path)) {
		fd_local = open(local_path, O_RDONLY, 0666);
		if(fd_local < 0){
			lib_error("[%s] open err!!", __FUNCTION__);
			close(fd_data);
			return -1;
		}
		fstat(fd_local, &sbuf);

		sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
		response = ftpcmd(buf, NULL, control_stream, buf);
		switch (response) {
		case 200:
		case 202:
			break;
		default:
			ftp_die("ALLO", buf);
			break;
		}
	}
	response = ftpcmd("STOR", server_path, control_stream, buf);
	switch (response) {
	case 125:
	case 150:
		break;
	default:
		ftp_die("STOR", buf);
		close(fd_local);
		close(fd_data);
		return -1;

	}

	/* transfer the file  */
	if (copyfd_eof(fd_local, fd_data, 0) == -1) {
		close(fd_data);
		close(fd_local);
		return -1;
	}

	/* close it all down */
	close(fd_local);
	close(fd_data);
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
		ftp_die("close", buf);
	}
	//ftpcmd("NOOP", NULL, control_stream, buf);
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}