int ftp_receive(const char *path, FILE *fp, transfer_mode_t mode, ftp_transfer_func hookf) { if(ftp->ssh_pid) return ssh_do_receive(path, fp, mode, hookf); if(ftp_init_receive(path, mode, hookf) != 0) return -1; return ftp_do_receive(fp, mode, hookf); }
int ftp_receive(const char *path, FILE *fp, transfer_mode_t mode, ftp_transfer_func hookf) { #ifdef HAVE_LIBSSH if(ftp->session) return ssh_do_receive(path, fp, mode, hookf); #endif if(ftp_init_receive(path, mode, hookf) != 0) return -1; return ftp_do_receive(fp, mode, hookf); }
int ftp_getfile(const char *infile, const char *outfile, getmode_t how, transfer_mode_t mode, ftp_transfer_func hookf) { FILE *fp; int r; struct stat statbuf; long rp = 0; int (*close_func)(FILE *fp); if(stat(outfile, &statbuf) == 0) { if(S_ISDIR(statbuf.st_mode)) { ftp_err(_("%s: is a directory\n"), outfile); return -1; } if(!(statbuf.st_mode & S_IWRITE)) { ftp_err(_("%s: permission denied\n"), outfile); return -1; } if(how == getResume) ftp->restart_offset = statbuf.st_size; } else ftp->restart_offset = 0L; ftp->ti.total_size = -1; /* we need to save this, because ftp_init_receive() sets it to zero */ rp = ftp->restart_offset; reset_transfer_info(); #ifdef HAVE_LIBSSH if (ftp->session) { /* we need to stat the remote file, so we are sure we can read it * this needs to be done before we call ssh_do_receive, because by * then, the file is created, and would leave a zero-size file opon * failure */ sftp_attributes a = sftp_stat(ftp->sftp_session, infile); if(!a) { ftp_err(_("Unable to stat file '%s': %s\n"), infile, ssh_get_error(ftp->session)); return -1; } sftp_attributes_free(a); /* FIXME: how can we check if it will be possible to transfer * the specified file? */ } else #endif if (ftp_init_receive(infile, mode, hookf) != 0) return -1; if(how == getPipe) { fp = popen(outfile, "w"); close_func = pclose; } else { fp = fopen(outfile, (rp > 0L || (how == getAppend)) ? "a" : "w"); close_func = fclose; } if(!fp) { ftp_err("%s: %s\n", outfile, strerror(errno)); ftp->restart_offset = 0L; return -1; } if(rp > 0L) { if(fseek(fp, rp, SEEK_SET) != 0) { ftp_err(_("%s: %s, transfer cancelled\n"), outfile, strerror(errno)); close_func(fp); ftp->restart_offset = 0L; return -1; } } free(ftp->ti.remote_name); free(ftp->ti.local_name); ftp->ti.remote_name = xstrdup(infile); ftp->ti.local_name = xstrdup(outfile); foo_hookf = hookf; #ifdef HAVE_LIBSSH if(ftp->session) r = ssh_do_receive(infile, fp, mode, hookf); else #endif r = ftp_do_receive(fp, mode, hookf); close_func(fp); return r; }