示例#1
0
char FTPLibFTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {

    char retVal = 0;

    // assert we can login
    retVal = assureLoggedIn();
    if (retVal) return retVal;

    SWBuf sourcePath = sourceURL;
    SWBuf outFile = (!destBuf) ? destPath : "swftplib.tmp";
    sourcePath << (6 + host.length()); // shift << "ftp://hostname";
    SWLog::getSystemLog()->logDebug("getting file %s to %s\n", sourcePath.c_str(), outFile.c_str());
    if (passive)
        FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, ftpConnection);
    else
        FtpOptions(FTPLIB_CONNMODE, FTPLIB_PORT, ftpConnection);
    // !!!WDG also want to set callback options
    if (sourcePath.endsWith("/") || sourcePath.endsWith("\\")) {
        SWLog::getSystemLog()->logDebug("getting test directory %s\n", sourcePath.c_str());
        FtpDir(NULL, sourcePath, ftpConnection);
        SWLog::getSystemLog()->logDebug("getting real directory %s\n", sourcePath.c_str());
        retVal = FtpDir(outFile.c_str(), sourcePath, ftpConnection) - 1;
    }
    else {
        SWLog::getSystemLog()->logDebug("getting file %s\n", sourcePath.c_str());
        retVal = FtpGet(outFile.c_str(), sourcePath, FTPLIB_IMAGE, ftpConnection) - 1;
    }

    // Is there a way to FTPGet directly to a buffer?
    // If not, we probably want to add x-platform way to open a tmp file with FileMgr
    // This wreaks and will easily fail if a user's CWD is not writable.
    if (destBuf) {
        FileDesc *fd = FileMgr::getSystemFileMgr()->open("swftplib.tmp", FileMgr::RDONLY);
        long size = fd->seek(0, SEEK_END);
        fd->seek(0, SEEK_SET);
        destBuf->size(size);
        fd->read(destBuf->getRawData(), size);
        FileMgr::getSystemFileMgr()->close(fd);
        FileMgr::removeFile("swftplib.tmp");
    }

    return retVal;
}
示例#2
0
void process_file(char *fnm)
{
    int sts=0;
    fsz_t fsz;
    struct REMFILE *filelist = NULL;
    struct REMFILE rem;

    ftp_connect();
    FtpClearCallback(conn);
    if ((action == FTP_SEND) || (action == FTP_GET) || (action == FTP_RM))
    {
	if (action == FTP_SEND)
	{
	    struct stat info;
	    if (stat(fnm,&info) == -1)
	    {
	    	perror(fnm);
		return;
	    }
	    if (S_ISDIR(info.st_mode))
	    {
		if (!FtpMkdir(fnm, conn))
		    fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn));
		else
		    if (ftplib_debug)
			fprintf(stderr,"Directory %s created\n",fnm);
		return;
	    }
            fsz = info.st_size;
	}
        else
        {
	    if (!wildcard)
	    {
		struct REMFILE *f;
		f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
		memset(f,0,sizeof(struct REMFILE));
		f->next = filelist;
		filelist = f;
		f->fnm = strdup(fnm);
	    } else {
		netbuf *dir;
		char *buf;
		if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir))
		{
		    fprintf(stderr,"error requesting directory of %s\n%s\n",
			    fnm, FtpLastResponse(conn));
		    return;
		}
		buf = malloc(DIRBUF_SIZE);
		while (FtpRead(buf, DIRBUF_SIZE, dir) > 0)
		{
		    struct REMFILE *f;
		    char *p;
		    f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
		    memset(f,0,sizeof(struct REMFILE));
		    f->next = filelist;
		    p = strchr(buf,'\n');
		    if (p)
			*p = '\0';
		    f->fnm = strdup(buf);
		    filelist = f;
		}
		free(buf);
		FtpClose(dir);
	    }
        }
    }
    switch (action)
    {
      case FTP_DIR :
	sts = FtpDir(NULL, fnm, conn);
	break;
      case FTP_LIST :
	sts = FtpNlst(NULL, fnm, conn);
	break;
      case FTP_SEND :
	rem.next = NULL;
	rem.fnm = fnm;
	rem.fsz = fsz;
	fsz /= 100;
	if (fsz > 100000)
	    fsz = 100000;
        if (ftplib_debug && fsz)
        {
	    FtpCallbackOptions opt;
	    opt.cbFunc = log_progress;
	    opt.cbArg = &rem;
	    opt.idleTime = 1000;
	    opt.bytesXferred = fsz;
	    FtpSetCallback(&opt,conn);
        }
	sts = FtpPut(fnm,strippath ? basename(fnm) : fnm,mode,conn);
	if (ftplib_debug && sts)
	    printf("%s sent\n",fnm);
	break;
      case FTP_GET :
	while (filelist)
	{
	    struct REMFILE *f = filelist;
	    filelist = f->next;
#if defined(__UINT64_MAX)
	    if (!FtpSizeLong(f->fnm, &fsz, mode, conn))
#else
	    if (!FtpSize(f->fnm, &fsz, mode, conn))
#endif
		fsz = 0;
	    f->fsz = fsz;
	    fsz /= 100;
	    if (fsz > 100000)
		fsz = 100000;
	    if ( fsz == 0 )
		fsz = 32768;
	    if (ftplib_debug)
	    {
		FtpCallbackOptions opt;
		opt.cbFunc = log_progress;
		opt.cbArg = f;
		opt.idleTime = 1000;
		opt.bytesXferred = fsz;
		FtpSetCallback(&opt,conn);
	    }
	    sts = FtpGet(f->fnm,f->fnm,mode,conn);
	    if (ftplib_debug && sts)
		printf("%s retrieved\n",f->fnm);
	    free(f->fnm);
	    free(f);
	}
	break;
      case FTP_RM :
	while (filelist)
	{
	    struct REMFILE *f = filelist;
	    filelist = f->next;
	    sts = FtpDelete(f->fnm,conn);
	    if (ftplib_debug && sts)
		printf("%s deleted\n", f->fnm);
	    free(f->fnm);
	    free(f);
	}
	break;
    }
    if (!sts)
	printf("ftp error\n%s\n",FtpLastResponse(conn));
    return;
}
示例#3
0
void process_file(char *fnm)
{
    int i;
    int fsz;

    ftp_connect();
    FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
    if ((action == FTP_SEND) || (action == FTP_GET))
    {
	if (action == FTP_SEND)
	{
	    struct stat info;
	    if (stat(fnm,&info) == -1)
	    {
	    	perror(fnm);
		return;
	    }
	    if (S_ISDIR(info.st_mode))
	    {
		if (!ftpMkdir(fnm))
		    fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn));
		else
		    if (ftplib_debug)
			fprintf(stderr,"Directory %s created\n",fnm);
		return;
	    }
            fsz = info.st_size;
	}
        else
        {
            if (!FtpSize(fnm, &fsz, mode, conn))
                fsz = 0;
        }
        if (ftplib_debug && fsz)
        {
            FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
            FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
            FtpOptions(FTPLIB_CALLBACKARG, (long) &fsz, conn);
            FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz/10, conn);
        }
    }
    switch (action)
    {
      case FTP_DIR :
	i = FtpDir(NULL, fnm, conn);
	break;
      case FTP_LIST :
	i = FtpNlst(NULL, fnm, conn);
	break;
      case FTP_SEND :
	i = FtpPut(fnm,fnm,mode,conn);
	if (ftplib_debug && i)
	    printf("%s sent\n",fnm);
	break;
      case FTP_GET :
	i = FtpGet(fnm,fnm,mode,conn);
	if (ftplib_debug && i)
	    printf("%s retrieved\n",fnm);
	break;
      case FTP_RM :
	i = FtpDelete(fnm,conn);
	if (ftplib_debug && i)
	    printf("%s deleted\n", fnm);
	break;
    }
    if (!i)
	printf("ftp error\n%s\n",FtpLastResponse(conn));
}
void process_file(char *fnm) {
	int sts = 0;
	int fsz;
	struct REMFILE *filelist = NULL;
	struct REMFILE rem;

	ftp_connect();
	FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
	if ((action == FTP_SEND) || (action == FTP_GET)) {
		if (action == FTP_SEND) {
			struct stat info;
			if (stat(fnm, &info) == -1) {
				perror(fnm);
				return;
			}
			if (S_ISDIR(info.st_mode)) {
				if (!ftpMkdir(fnm))
					fprintf(stderr, "mkdir %s failed\n%s", fnm,
							FtpLastResponse(conn));
				else if (ftplib_debug)
					fprintf(stderr, "Directory %s created\n", fnm);
				return;
			}
			fsz = info.st_size;
		} else {
			if (!wildcard) {
				struct REMFILE *f;
				f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
				memset(f, 0, sizeof(struct REMFILE));
				f->next = filelist;
				filelist = f;
				f->fnm = strdup(fnm);
			} else {
				netbuf * dir;
				char *buf;
				if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir)) {
					fprintf(stderr, "error requesting directory of %s\n%s\n",
							fnm, FtpLastResponse(conn));
					return;
				}
				buf = malloc(DIRBUF_SIZE);
				while (FtpRead(buf, DIRBUF_SIZE, dir) > 0) {
					struct REMFILE *f;
					char *p;
					f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
					memset(f, 0, sizeof(struct REMFILE));
					f->next = filelist;
					p = strchr(buf, '\n');
					if (p)
						*p = '\0';
					f->fnm = strdup(buf);
					filelist = f;
				}
				free(buf);
				FtpClose(dir);
			}
		}
	}
	switch (action) {
	case FTP_DIR:
		sts = FtpDir(NULL, fnm, conn);
		break;
	case FTP_LIST:
		sts = FtpNlst(NULL, fnm, conn);
		break;
	case FTP_SEND:
		rem.next = NULL;
		rem.fnm = fnm;
		rem.fsz = fsz;
		fsz /= 10;
		if (fsz > 100000)
			fsz = 100000;
		if (ftplib_debug && fsz) {
			FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
			FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
			FtpOptions(FTPLIB_CALLBACKARG, (long) &rem, conn);
			FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);
		}
		sts = FtpPut(fnm, fnm, mode, conn);
		if (ftplib_debug && sts)
			printf("%s sent\n", fnm);
		break;
	case FTP_GET:
		while (filelist) {
			struct REMFILE *f = filelist;
			filelist = f->next;
			if (!FtpSize(f->fnm, &fsz, mode, conn))
				fsz = 0;
			f->fsz = fsz;
			fsz /= 10;
			if (fsz > 100000)
				fsz = 100000;
			if (ftplib_debug && fsz) {
				FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
				FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
				FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn);
				FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);
			}
			sts = FtpGet(f->fnm, f->fnm, mode, conn);
			if (ftplib_debug && sts)
				printf("%s retrieved\n", f->fnm);
			free(f->fnm);
			free(f);
		}
		break;
	case FTP_RM:
		while (filelist) {
			struct REMFILE *f = filelist;
			filelist = f->next;
			sts = FtpDelete(f->fnm, conn);
			if (ftplib_debug && sts)
				printf("%s deleted\n", f->fnm);
			free(f->fnm);
			free(f);
		}
		break;
	}
	if (!sts)
		printf("ftp error\n%s\n", FtpLastResponse(conn));
	return;
}