示例#1
0
int main(int argc, char **argv) {
	skod_t skod;
	ftp_t ftp;

	skod_init(&skod);
	skod_parse_cla(argc, argv, &skod);		

	signal(SIGINT, signal_handler);

	/* Init FTP*/
	ftp.user = skod.user;
	ftp.password = skod.password;
	ftp.server = skod.server;
	ftp.port = skod.port;
	ftp.alarm_sec = 1; /* High risk */

	ftp_mkcon(&ftp);

	/* --dest, -e*/
	if ( skod.dest != NULL )
		ftp_cwd(&ftp, skod.dest);
	else if ( skod.dest == NULL && flag == 4 )
		die("You need to pass --dest (destination folder) with --upload.");

	switch(flag) {
		case 1:
			ftp_list(&ftp, skod.path, 1);
			break;
		case 2:
			ftp_remove(&ftp, skod.path);
			break;
		case 3:
			ftp_download_single(&ftp, skod.path);
			break;
		case 4:
			ftp_upload_single(&ftp, skod.path);
			break;
		case 5:
			ftp_cat(&ftp, skod.path);
			break;
		case 6:
			printf("%d\n", ftp_size(&ftp, skod.path));
			break;
		case 7:
			printf("%s\n", ftp_pwd(&ftp));
			break;
		case 8:
			ftp_delete(&ftp, skod.path);
			break;
		case 10:
			ftp_mdtm(&ftp, skod.path);
			break;
		case 99:
			skod_scan(&skod, &ftp);
			break;
	}

	ftp_close(&ftp);
	return 0;
}
示例#2
0
文件: test1.c 项目: andypc/lib_ftp
int main ()
{
  int ret;

  /* Allocate app's struct */
  ftp_s *ftp;
  ftp = malloc (1 * sizeof (ftp_s));

  /* Define settings */
  ftp->server           = "server"; /* es: "oneandy.altervista.org" */
  ftp->username         = "******";
  ftp->password         = "******";
  ftp->path_file_upload = "data.txt";
  ftp->name_file_upload = "data_uploaded.txt";
  ftp->use_proxy        = 0;
  ftp->proxy            = NULL;
  ftp->proxy_port       = NULL;

  printf ("* explore server...\n");

  /*
    EXPLORE SERVER saving the content in "data.txt"
    No iterative protocol - It doesn't explore the sub-directories
  */
  ret = ftp_explore (ftp, "data.txt");

  printf ("* start backup...\n");

  /*
    BACKUP FILE
    You can download the file and save it with another name.
    No iterative prototocol - One file at the time
  */
  ret = ftp_backup (ftp, "file.pdf", "saved_file.pdf");

  printf ("* start upload...\n");

  /*
    UPLOAD FILE
    No iterative prototocol - One file at the time
  */
  /*
  ret = ftp_upload (ftp);
  */
  /* or */
  ret = ftp_upload_set_files (ftp, "data.txt", "uploaded_data.txt");


  /*
    DELETE FILE
    No iterative prototocol - One file at the time
  */
  ret = ftp_delete (ftp, "uploaded_data.txt");


  return 0;
}
示例#3
0
int main(int argc, char** argv){
	if(argc != 2){
		printUsage();
		return 1;
	}

	int val = validateURL(argv[1], sizeof(argv[1]));
	int anon = 1;
	char user[MAX_STRING_SIZE];
	char password[MAX_STRING_SIZE];
	char host[MAX_STRING_SIZE];
	char path[MAX_STRING_SIZE];
	switch(val){
		case 0:
			anon = 0;
		case 1:
			parseURL(argv[1], sizeof(argv[1]), host, user, password, path, anon);
			break;
		case 2:
			printUsage();
			return 1;
		default:
			return 1;
	}

	ftp_t* ftp = ftp_init(host, user, password, path);
	if(ftp == NULL)
		return 1;

	if(ftp_connect(ftp) < 0)
		return 1;

	if(ftp_login_host(ftp) < 0)
		return 1;

	if(ftp_set_passive_mode(ftp) < 0)
		return 1;

	if(ftp_retr_file(ftp) < 0)
		return 1;

	if(ftp_download_file(ftp) < 0)
		return 1;

	if(ftp_disconnect(ftp) < 0)
		return 1;

	ftp_delete(ftp);

	return 0;
}
示例#4
0
int ftp_put(struct obex_session *os, obex_object_t *obj, void *user_data)
{
	struct ftp_session *ftp = user_data;
	const char *name = obex_get_name(os);
	ssize_t size = obex_get_size(os);

	DBG("%p name %s size %zd", ftp, name, size);

	if (ftp->folder == NULL)
		return -EPERM;

	if (name == NULL)
		return -EBADR;

	if (size == OBJECT_SIZE_DELETE)
		return ftp_delete(ftp, name);

	return 0;
}
示例#5
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
}
示例#6
0
void libmftp_main_test(char *host, unsigned int port, char *user, char *pw, char *workingdirectory)
{
	int success = 0;
	ftp_file *f = NULL, *g = NULL;
	ftp_content_listing *cl = NULL, *cl2 = NULL;
	ftp_date d;
	char *buf = NULL;
	ftp_connection *c = ftp_open(host, port, tls == 0 ? ftp_security_none : ftp_security_always);

	/*c->_current_features->use_mlsd = ftp_bfalse;*/

	if (!c) {
		printf("Could not connect. Error: %i\n",ftp_error);
		if (ftp_error == FTP_ESECURITY) {
			printf("This server does not support a TLS connection. FTP_ESECURITY\n");
		}
		goto end;
	}

	if (ftp_auth(c, user, pw, ftp_btrue) != FTP_OK) {
		printf("Could not authenticate. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_reload_cur_directory(c) != FTP_OK) {
		printf("Could not reload wd. Error: %i\n", c->error);
		goto end;
	}

	if (strcmp(workingdirectory, c->cur_directory) != 0) {
		printf("Warning: Working dir (%s) does not match ftp dir (%s)! Continue? (y/n)\n",workingdirectory,c->cur_directory);
		if (getchar() != 'y') {
			goto end;
		}
	}

	char *test = "This is a test string that will be written to the server.";
	size_t test_len = strlen(test);

	//TEST UPLOAD 1

	f = ftp_fopen(c, "testfile.test", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	//TEST CONTENT LISTING

	int entry_count;
	cl = ftp_contents_of_directory(c, &entry_count);

	if (!cl) {
		printf("Could not get content listing. Error: %i\n", c->error);
		goto end;
	}

	if (entry_count != 1 || cl->next) {
		printf("More than 1 entry in content listing.\n");
		goto end;
	}

	if (!ftp_item_exists_in_content_listing(cl, "testfile.test", &cl2)) {
		printf("Could not find previously generated file in content listing.\n");
		goto end;
	}

	if (!cl2) {
		printf("Did not get a pointer to the content listing entry.\n");
		goto end;
	}

	if (!cl2->facts.given.modify) {
		printf("Warning: Remote host does not provide modification date information! Continue? (y/n)\n");
		if (getchar() != 'y') {
			goto end;
		}
	} else {
		time_t cur = time(NULL);
		struct tm *curtm = localtime(&cur);
		if (curtm->tm_year + 1900 != cl2->facts.modify.year) {
			printf("Warning: Modification year of remote file (%i) does not equal local year (%i). Continue? (y/n)\n",cl2->facts.modify.year,curtm->tm_year + 1900);
			if (getchar() != 'y') {
				goto end;
			}
		}
	}

	//TEST SIZE

	size_t srv_size;
	if (ftp_size(c, "testfile.test", &srv_size) != FTP_OK) {
		printf("Could not get file size. Error: %i\n", c->error);
		goto end;
	}

	if (srv_size != test_len) {
		printf("Remote file size (%lu) differs from local file size (%lu).\n",srv_size,test_len);
		goto end;
	}

	//TEST READ

	f = ftp_fopen(c, "testfile.test", FTP_READ, 0);
	if (!f) {
		printf("Could not fopen to read. Error: %i\n", c->error);
		goto end;
	}

	buf = malloc(srv_size + 1);

	if (ftp_fread(buf, 1, srv_size, f) != srv_size) {
		printf("Could not read file. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	*(buf+srv_size) = '\0';
	if (strcmp(test, buf) != 0) {
		printf("Remote file content differs from local file content.\n");
		goto end;
	}

	//TEST UPLOAD 2 (SIMULTANEOUS)

	f = ftp_fopen(c, "testfile1.txt", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write 2 (1). Error: %i\n", c->error);
		goto end;
	}
	g = ftp_fopen(c, "testfile2.txt", FTP_WRITE, 0);
	if (!g) {
		printf("Could not fopen to write 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string 2 (1). Error: %i\n", *(f->error));
		goto end;
	}
	if (ftp_fwrites(test, g) != test_len) {
		printf("Could not write test string 2 (2). Error: %i\n", *(g->error));
		goto end;
	}
	ftp_fclose(f);
	ftp_fclose(g);
	f = g = NULL;

	//TEST SIZE 2

	size_t srv_size2;
	if (ftp_size(c, "testfile1.txt", &srv_size) != FTP_OK) {
		printf("Could not get file size 2 (1). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_size(c, "testfile2.txt", &srv_size2) != FTP_OK) {
		printf("Could not get file size 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (srv_size != test_len || srv_size2 != test_len) {
		printf("Remote file size 2 differs from local file size.\n");
		goto end;
	}

	//TEST FOLDERS

	if (ftp_create_folder(c, "testfolder") != FTP_OK) {
		printf("Could not create folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_move(c, "testfile1.txt", "testfolder/testfile1.txt")) {
		printf("Could not move file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, "testfolder") != FTP_OK) {
		printf("Could not cwd 2. Error: %i\n", c->error);
		goto end;
	}

	if (!ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("File does not exist where it was moved to.");
		goto end;
	}

	//TEST DELETE

	if (ftp_delete(c, "testfile1.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not delete file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("Deleted file still exists.");
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd 3. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_delete(c, "testfolder", ftp_btrue) != FTP_OK) {
		printf("Could not delete folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfolder", NULL)) {
		printf("Deleted folder still exists.");
		goto end;
	}

	if (ftp_delete(c, "testfile.test", ftp_bfalse) != FTP_OK || ftp_delete(c, "testfile2.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not clean up. Error: %i\n", c->error);
		goto end;
	}

	success = 1;
end:
	if (cl) ftp_free(cl);
	if (f) ftp_fclose(f);
	if (g) ftp_fclose(g);
	if (c) ftp_close(c);
	if (buf) free(buf);
	if (!success) {
		printf("Test was NOT successful. :-(\n");
	} else {
		printf("Test was successful! :-)\n");
	}
}