Beispiel #1
0
/*
 * FtpXfer - issue a command and transfer data
 *
 * return 1 if successful, 0 otherwise
 */
static int FtpXfer(const char *localfile, const char *path,
                   netbuf *nControl, int typ, int mode)
{
    int l,c;
    char *dbuf;
    FILE *local = NULL;
    netbuf *nData;
    int rv=1;
    int writeResult = 0;

    if (localfile != NULL)
      {
          char ac[4] = "w";
          if (typ == FTPLIB_FILE_WRITE)
              ac[0] = 'r';
          if (mode == FTPLIB_IMAGE)
              ac[1] = 'b';
          local = fopen(localfile, ac);
          if (local == NULL)
            {
                strncpy(nControl->response, strerror(errno),
                        sizeof(nControl->response));
                return 0;
            }
      }
    if (local == NULL)
        local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
    if (!FtpAccess(path, typ, mode, nControl, &nData))
        return 0;
    dbuf = malloc(FTPLIB_BUFSIZ);
    if (typ == FTPLIB_FILE_WRITE)
      {
          while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
              if ((c = FtpWrite(dbuf, l, nData)) < l)
                {
                    if (ftplib_debug > 1)
                        printf("short write: passed %d, wrote %d\n", l, c);
                    rv = 0;
                    break;
                }
      }
    else
      {
          while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0) {
              writeResult = (nData->writercb) ? nData->writercb(nData, dbuf, l, nData->writerarg) : fwrite(dbuf, 1, l, local);
              if (writeResult <= 0)
                {
                    perror("localstore write");
                    rv = 0;
                    break;
                }
          }
      }
    free(dbuf);
    fflush(local);
    if (localfile != NULL)
        fclose(local);
    FtpClose(nData);
    return rv;
}
Beispiel #2
0
/*
 * FtpXfer - issue a command and transfer data
 *
 * return 1 if successful, 0 otherwise
 */
static int FtpXfer(const char *localfile, const char *path,
    netbuf *nControl, int typ, int mode)
{
    int l,c;
    char *dbuf;
    FILE *local = NULL;
    netbuf *nData;

    if (localfile != NULL)
    {
    local = fopen(localfile, (typ == FTPLIB_FILE_WRITE) ? "r" : "w");
    if (local == NULL)
    {
        strncpy(nControl->response, strerror(errno),
                    sizeof(nControl->response));
        return 0;
    }
    }
    if (local == NULL)
    local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
    if (!FtpAccess(path, typ, mode, nControl, &nData))
    return 0;
    dbuf = malloc(FTPLIB_BUFSIZ);
    if (typ == FTPLIB_FILE_WRITE)
    {
    while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
        if ((c = FtpWrite(dbuf, l, nData)) < l)
        printf("short write: passed %d, wrote %d\n", l, c);
    }
    else
    {
        while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
        if (fwrite(dbuf, 1, l, local) <= 0)
        {
        perror("localfile write");
        break;
        }
    }
    free(dbuf);
    fflush(local);
    if (localfile != NULL)
    fclose(local);
    return FtpClose(nData);
}
Beispiel #3
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;
}
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;
}