Esempio n. 1
0
int ftp_put(st_ftp *s)
{
	int ret,error;
	char src_file[512],dst_file[512],send_buff[512],put_cmd[512];
	struct stat file_info;
	int local_file;
	int file_put_sock,new_sock,count=0,i=0;
	int set=sizeof(local_host);
	
	memset(src_file,0,sizeof(src_file));
	memset(dst_file,0,sizeof(dst_file));	
	memset(send_buff,0,sizeof(send_buff));
	memset(put_cmd,0,sizeof(put_cmd));
	sprintf(put_cmd,"put %s",s->putFileName);
	
	if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1) // 登录ftp
	{
		printf("Password error!\n");
		return -1;
	}
	else if(ret == -2)
	{
		printf("User error!\n");
		return -1;
	}
	
	if(local_cd(s->localDir) != 0) // 切换到本地目录
	{
		printf("local cd error !\n");		
		return -1;
	}

	if(ftp_cd(s->ftpDir) != 0)// 改变ftp服务器的目录
	{
		printf("ftp cd error !\n");		
		return -1;
	}
	
	ftp_cmd_filename(put_cmd,src_file,dst_file);// ftp操作命令
	if((stat(src_file,&file_info))<0) // 获取文件信息
	{
		printf("local file %s doesn't exist!\n",src_file);
		close(sock_control);
		return -1;
	}
	local_file=open(src_file,O_RDONLY);
	if(local_file<0)
	{		
		printf("Open file error\n");
		close(sock_control);
		return -2;
	}
	file_put_sock=ftp_connect_data();
	if(file_put_sock<0)
	{
		ftp_get_reply(sock_control);
		printf("Creat data socket error!\n");		
		close(local_file);
		close(sock_control);
		return -3;
	}	
	ftp_send_cmd("TYPE I",NULL,sock_control);/* 数据类型(A=ASCII,E=EBCDIC,I=binary) */
	error = ftp_get_reply(sock_control);
	printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__);	
	if(error != 200)
	{
		printf("commond :<TYPE I> error!,line<%d>\n",__LINE__);
		return -1;
	}
	
	ftp_send_cmd("STOR ",dst_file,sock_control);/* 储存(复制)文件到服务器上  */
	error = ftp_get_reply(sock_control);
	printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__);	
	if(error != 150)
	{
		printf("commond :<STOR> error!,line<%d>\n",__LINE__);
		return -1;		
	}

	printf("put file begin,please wait ...\n");
	while(1)
	{
		count=read(local_file,send_buff,sizeof(send_buff));
		if(count<=0)
		{
			break;
		}
		else
		{
			if((write(file_put_sock,send_buff,count)) == -1)
			{
				printf("write err!\n");
				break;
			}
		}
	}	
	printf("put file succ!\n");
	close(local_file);
	close(file_put_sock);
	ftp_get_reply(sock_control);	
	ftp_quit();
	return 0;
}
Esempio n. 2
0
void main(int argc, char *argv[])
{
	ifstream bcpInfile;		// local import file holding records to bcp to x_import table
	int	hndl;				// file handle
	char szXfrFile[128];	// holds name of delimited import file
	char szTmpFile[128];	// holds name of renamed delimited import file	
	char szXfrLocalFile[128]; // holds name of delimited import file with local dir prepended
	int	 intBcpCount;		// number of rows bcp'd

	// Open import log and import error log files
	if ( !OpenLogFiles() )
		return;

	if ( argc >= 7 )
		szSystem = argv[7];
	else 
		szSystem = "";

	// Check if user wants help or if incorrect args entered
	if ( !CheckArgs(argc, argv) )
		return;

	// Create transfer filenames
	wsprintf(szXfrFile, "%s_cags.xfr", argv[7] );
	wsprintf(szTmpFile, "%s_cags.yyy", argv[7] ); 

	// Connect to database and init db structures
	if ( !DBConnect(argv[1], argv[2], argv[3]) )
		return;

	//
	// Check if local wms_cags.xfr exists to BCP records into import table
	//
	wsprintf(szXfrLocalFile, "%s%s", "./transfer/", szXfrFile ); 
	if ( FileSize(szXfrLocalFile) > -1 )  // file exists
	{
		// Open local wms_cags.xfr 
		bcpInfile.open(szXfrLocalFile, ios::nocreate, filebuf::sh_none);
		if ( !bcpInfile.is_open() )
		{
			// Failed open so get out and retry on next run
			errfile << ERROR_PREFIX  << "failed to open bcp input file" << endl;
			return;
		}
		else
		{
			// Call bcp routines to move data from import file into x_import table
			// Note: If migrated to another RDBMS most changes will be to this function
			if ( (intBcpCount = BCPInRecords(bcpInfile, argv[7])) != -1 )
			{
				// Delete local import file now that its records are in import table
				bcpInfile.close();
				f_deletefile(szXfrLocalFile);
				logfile << INFO_PREFIX << "successfull BCP of " << intBcpCount
						<< " records into import table" << endl;
			}
			else
			{
				// Failed bcp so don't delete and get out to retry on next run
				bcpInfile.close();
				errfile << ERROR_PREFIX  << "failed BCP of import file to import table" << endl;
				return;
			}
		}
	}
	else
	{
		if ( FileSize(szXfrLocalFile) == -1 )
			logfile << WARNING_PREFIX << "no records to import from " << szXfrLocalFile << " this run" << endl;
		else
			errfile << ERROR_PREFIX << "error opening local import file " << szXfrLocalFile << endl;
	}

	//
	// Logon to ftp server to get remote wms_cags.xfr to be bcp'd next import run
	//
	hndl = ftp_login(argv[4], argv[5], argv[6], "");
	if ( hndl == -1 )
	{
		errfile << ERROR_PREFIX << "failed ftp_login" << endl;
		return;
	}

	// Set current remote transfer directory
	if ( ftp_cd(hndl, argv[8]) == -1 )
	{
		errfile << ERROR_PREFIX << "failed ftp_cd to " << argv[8] << endl;
		return;
	}

	// Check for no left over records from prior import and new records to import
	if ( !ftp_exist(hndl, szTmpFile) && ftp_exist(hndl, szXfrFile) )
	{
		// If so, then rename prior to ftp_get to prevent contention with remote site
		if ( ftp_rename(hndl, szXfrFile, szTmpFile) == -1 )
		{
			// ftp_rename failed so just log message and try again next invocation
			errfile << ERROR_PREFIX << "failed ftp_remame of " 
					<< szXfrFile << " to " << szTmpFile
					<< " will retry next run" << endl;
			return;
		}
	}

	// Check for either left over records or new records in tmp file to import	
	if ( ftp_exist(hndl, szTmpFile) )
	{
		// If so, then ftp them to local directory
		if ( ftp_get(hndl, szTmpFile, szXfrLocalFile) == -1 )
		{
			// ftp_get failed so do nothing here and retry next invovation
			errfile << ERROR_PREFIX << "failed ftp_get of " << szTmpFile 
					<< " will retry next run" << endl;
		}
		else
		{
			if ( ftp_delete(hndl, szTmpFile) == -1 )
			{
				// ftp_delete failed so delete local to prevent re-importing next invocation
				f_deletefile(szXfrLocalFile);
				errfile << ERROR_PREFIX << "failed ftp_delete of " << szTmpFile << endl;
			}
			else
			{
				// successfull FTP
				logfile << INFO_PREFIX << "successfull FTP from remote site to " 
						<< szXfrLocalFile << endl;
			}
		}
	}						
	else
			logfile << WARNING_PREFIX << "no records to ftp from remote site this run" << endl;

	// Close opened objects
	if ( !logfile.is_open() )
		logfile.close();
	if ( !errfile.is_open() )
		errfile.close();
	dbexit();		// close database connection
	ftp_quit(hndl); // close ftp connection
}
Esempio n. 3
0
int ftp_get(st_ftp *s)
{
	int ret,error;
	int get_sock, set, new_sock, i = 0;
	char src_file[512],dst_file[512],rcv_buf[512],get_cmd[512];	
	int local_file;
	int count = 0;
	struct stat file_info;
	memset(src_file,0,sizeof(src_file));
	memset(dst_file,0,sizeof(dst_file));	
	memset(rcv_buf,0,sizeof(rcv_buf));
	memset(get_cmd,0,sizeof(get_cmd));
	sprintf(get_cmd,"get %s",s->getFileName);

	if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1)
	{
		printf("Password error!\n");
		return -1;
	}
	else if(ret == -2)
	{
		printf("User error!\n");
		return -1;
	}
	
	if(local_cd(s->localDir) != 0)
	{
		printf("local cd error !\n");		
		return -1;
	}

	if(ftp_cd(s->ftpDir) != 0)
	{
		printf("ftp cd error !\n");		
		return -1;
	}

	ftp_cmd_filename(get_cmd, src_file, dst_file);
	
	if(!stat(dst_file, &file_info))
	{
		printf("local file %s exists: %d bytes\n", dst_file, (int)file_info.st_size);
		if(!s->getCoverFalgs)
		{
			close(sock_control);
			return -1;
		}
	}
	
	local_file = open(dst_file, O_CREAT|O_TRUNC|O_WRONLY);
	if(local_file < 0)
	{
		printf("creat local file %s error!\n", dst_file);
		close(sock_control);
		return -2;
	}
	get_sock = ftp_connect_data();
	if(get_sock < 0)
	{
		printf("socket error!\n");
		close(sock_control);
		return -3;
	}
	set = sizeof(local_host);
	
	ftp_send_cmd("TYPE I", NULL, sock_control);
	error = ftp_get_reply(sock_control);
	printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__);
	if(error != 200)
	{
		printf("commond :<TYPE I> error!,line<%d>\n",__LINE__);
		return -1;
	}
	
	ftp_send_cmd("RETR ", src_file, sock_control);/* 从服务器上找回(复制)文件  */
	error = ftp_get_reply(sock_control);
	printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__);	
	if(error != 150)
	{
		printf("commond :<RETR> error!,line<%d>\n",__LINE__);
		return -1;
	}
	
	printf("get file begin,please wait ...\n");
	while(1)
	{
		count = read(get_sock, rcv_buf, sizeof(rcv_buf));
		if(count <= 0)
		{
			break;
		}	
		else
		{
			if((write(local_file, rcv_buf, count)) == -1)
			{
				printf("write err!\n");
				break;
			}
		}
	}
	printf("get file succ!\n");
	close(local_file);
	close(get_sock);
	ftp_get_reply(sock_control); 	
	ftp_quit();
	return 0;
}