void cmd_switch(int argc, char **argv) { OPT_HELP("Switch between active connections. Usage:\n" " switch [options] [number | name]\n" "Options:\n" " -h, --help show this help\n" "The argument can either be the connection number, host name" " or its alias\n" "Without argument, switch to the next active connection\n"); maxargs(optind); if(argc > optind) { listitem *tmp = ftplist_search(argv[optind]); if(tmp) gvCurrentFtp = tmp; } else { if(gvCurrentFtp == gvFtpList->last) gvCurrentFtp = gvFtpList->first; else { if(gvCurrentFtp->next) gvCurrentFtp = gvCurrentFtp->next; } } ftp_use((Ftp *)gvCurrentFtp->data); }
void ftp_quit_all(void) { listitem *li; /* nicely close all open connections */ for(li=gvFtpList->first; li; li=li->next) { ftp_use((Ftp *)li->data); ftp_quit(); } }
void cmd_close(int argc, char **argv) { if(argv) { OPT_HELP_NEW(_("Close open connection"), "close [options]", NULL); maxargs(optind - 1); } ftp_quit(); if(list_numitem(gvFtpList) > 1) { list_delitem(gvFtpList, gvCurrentFtp); gvCurrentFtp = gvFtpList->first; ftp_use((Ftp *)gvCurrentFtp->data); } }
void cmd_close(int argc, char **argv) { if(argv) { OPT_HELP("Close open connection. Usage:\n" " close [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); } ftp_quit(); if(list_numitem(gvFtpList) > 1) { list_delitem(gvFtpList, gvCurrentFtp); gvCurrentFtp = gvFtpList->first; ftp_use((Ftp *)gvCurrentFtp->data); } }
void cmd_switch(int argc, char **argv) { OPT_HELP_NEW(_("Switch between active connections."), "switch [options] [number | name]", _("The argument can either be the connection number, host name or its alias.\n" "Without argument, switch to the next active connection\n")); maxargs(optind); if(argc > optind) { listitem *tmp = ftplist_search(argv[optind]); if(tmp) gvCurrentFtp = tmp; } else { if(gvCurrentFtp == gvFtpList->last) gvCurrentFtp = gvFtpList->first; else { if(gvCurrentFtp->next) gvCurrentFtp = gvCurrentFtp->next; } } ftp_use((Ftp *)gvCurrentFtp->data); }
/* transfers SRCFILE on SRCFTP to DESTFILE on DESTFTP * using pasv mode on SRCFTP and port mode on DESTFTP * */ int ftp_fxpfile(Ftp *srcftp, const char *srcfile, Ftp *destftp, const char *destfile, fxpmode_t how, transfer_mode_t mode) { int r; unsigned int old_reply_timeout; Ftp *thisftp; unsigned char addr[6]; /* printf("FxP: %s -> %s\n", srcftp->url->hostname, destftp->url->hostname);*/ if(srcftp == destftp) { ftp_err(_("FxP between same hosts\n")); return -1; } #ifdef HAVE_LIBSSH if(ftp->session) { ftp_err("FxP with SSH not implemented\n"); return -1; } #endif thisftp = ftp; /* save currently active connection */ /* setup source side */ ftp_use(srcftp); ftp_type(mode); // TODO: IPv6 support if(!ftp_pasv(false, addr, NULL)) { ftp_use(thisftp); return -1; } ftp->ti.total_size = -1; /* setup destination side */ ftp_use(destftp); ftp_type(mode); ftp_cmd("PORT %d,%d,%d,%d,%d,%d", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); if(ftp->code != ctComplete) { ftp_use(thisftp); return -1; } ftp->ti.total_size = -1; ftp_use(destftp); if(how == fxpResume) { rfile *f; f = ftp_get_file(destfile); if(f && f->size != (unsigned long long)-1) ftp->restart_offset = f->size; else { ftp->restart_offset = ftp_filesize(destfile); if(ftp->restart_offset == (unsigned long long)-1) { ftp_err(_("unable to get remote filesize of '%s'," " unable to resume\n"), destfile); ftp->restart_offset = 0L; } } } else ftp->restart_offset = 0L; if(ftp->restart_offset) { /* RESTart on destftp */ ftp_cmd("REST %ld", ftp->restart_offset); if(ftp->code != ctContinue) return -1; ftp_use(srcftp); ftp_cmd("REST %ld", ftp->restart_offset); if(ftp->code != ctContinue) return -1; } /* issue a STOR command on DESTFTP */ ftp_use(destftp); switch(how) { case fxpUnique: if(ftp->has_stou_command) { ftp_cmd("STOU %s", destfile); if(ftp->fullcode == 502) ftp->has_stou_command = false; } else { ftp->code = ctError; ftp->fullcode = 502; } break; case fxpAppend: ftp_cmd("APPE %s", destfile); break; case fxpNormal: default: ftp_cmd("STOR %s", destfile); break; } ftp_cache_flush_mark_for(destfile); if(ftp->code != ctPrelim) { ftp_use(thisftp); return -1; } /* issue a RETR command on SRCFTP */ ftp_use(srcftp); ftp_cmd("RETR %s", srcfile); if(ftp->code != ctPrelim) { ftp_use(destftp); ftp_abort(NULL); ftp_use(thisftp); return -1; } ftp_use(destftp); old_reply_timeout = ftp->reply_timeout; ftp_reply_timeout(0); ftp_read_reply(); ftp_reply_timeout(old_reply_timeout); r = (ftp->code == ctComplete ? 0 : -1); ftp_use(srcftp); old_reply_timeout = ftp->reply_timeout; ftp_reply_timeout(0); ftp_read_reply(); ftp_reply_timeout(old_reply_timeout); ftp_use(thisftp); return r; }
static int fxpfile(const rfile *fi, unsigned int opt, const char *output, const char *destname) { fxpmode_t how = fxpNormal; bool file_exists = false; char *dest, *dpath; bool dir_created; char *dest_dir, *q_dest_dir; Ftp *thisftp = ftp; if((fxp_glob_mask && fnmatch(fxp_glob_mask, base_name_ptr(fi->path), 0) == FNM_NOMATCH) #ifdef HAVE_REGEX || (fxp_rx_mask_set && regexec(&fxp_rx_mask, base_name_ptr(fi->path), 0, 0, 0) == REG_NOMATCH) #endif ) return 0; if(!output) output = "."; if(test(opt, FXP_PARENTS)) { char *p = base_dir_xptr(fi->path); if (asprintf(&dest, "%s/%s/%s", output, p, base_name_ptr(fi->path)) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); free(p); return -1; } free(p); } else if(test(opt, FXP_OUTPUT_FILE)) dest = xstrdup(output); else if (asprintf(&dest, "%s/%s", output, base_name_ptr(fi->path)) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); return -1; } path_collapse(dest); ftp_use(fxp_target); /* make sure destination directory exists */ dpath = base_dir_xptr(dest); dest_dir = ftp_path_absolute(dpath); q_dest_dir = backslash_quote(dest_dir); int r = ftp_mkpath(q_dest_dir); free(q_dest_dir); free(dest_dir); if(r == -1) { transfer_mail_msg(_("Couldn't create directory: %s\n"), dest_dir); free(dpath); free(dest); ftp_use(thisftp); return -1; } dir_created = (r == 1); if(!dir_created && !test(opt, FXP_UNIQUE) && !test(opt, FXP_FORCE)) { rfile *f; f = ftp_get_file(dest); file_exists = (f != 0); if(f && risdir(f)) { /* can't overwrite a directory */ printf(_("%s: is a directory\n"), dest); free(dest); return 0; } } if(test(opt, FXP_APPEND)) { how = fxpAppend; } else if(file_exists) { if(test(opt, FXP_SKIP_EXISTING)) { char* sp = shortpath(dest, 42, ftp->homedir); printf(_("Remote file '%s' exists, skipping...\n"), sp); free(sp); free(dest); ftp_use(thisftp); return 0; } else if(test(opt, FXP_NEWER)) { time_t src_ft; time_t dst_ft; ftp_use(thisftp); src_ft = ftp_filetime(fi->path, test(opt, FXP_FORCE_NEWER)); ftp_use(fxp_target); dst_ft = ftp_filetime(dest, test(opt, FXP_FORCE_NEWER)); if(src_ft != (time_t)-1 && dst_ft != (time_t)-1 && dst_ft >= src_ft) { char* sp = shortpath(dest, 42, ftp->homedir); printf(_("Remote file '%s' is newer than local, skipping...\n"), sp); free(sp); free(dest); ftp_use(thisftp); return 0; } } else if(!test(opt, FXP_RESUME)) { if(!fxp_owbatch) { char* sp = shortpath(dest, 42, ftp->homedir); int a = ask(ASKYES|ASKNO|ASKUNIQUE|ASKCANCEL|ASKALL|ASKRESUME, ASKRESUME, _("File '%s' exists, overwrite?"), sp); free(sp); if(a == ASKCANCEL) { fxp_quit = true; free(dest); ftp_use(thisftp); return 0; } else if(a == ASKNO) { free(dest); ftp_use(thisftp); return 0; } else if(a == ASKUNIQUE) opt |= FXP_UNIQUE; /* for this file only */ else if(a == ASKALL) fxp_owbatch = true; else if(a == ASKRESUME) opt |= FXP_RESUME; /* for this file only */ /* else a == ASKYES */ } } } if(test(opt, FXP_RESUME)) how = fxpResume; if(test(opt, FXP_UNIQUE)) how = fxpUnique; r = do_the_fxp(thisftp, fi->path, fxp_target, dest, how, opt); free(dest); if(r != 0) { ftp_use(thisftp); return -1; } if(test(opt, FXP_PRESERVE)) fxp_preserve_attribs(fi, dest); if(test(opt, FXP_DELETE_AFTER)) { bool dodel = false; ftp_use(thisftp); if(!test(opt, FXP_FORCE) && !fxp_delbatch && !gvSighupReceived) { char* sp = shortpath(fi->path, 42, ftp->homedir); int a = ask(ASKYES|ASKNO|ASKCANCEL|ASKALL, ASKYES, _("Delete remote file '%s'?"), sp); free(sp); if(a == ASKALL) { fxp_delbatch = true; dodel = true; } else if(a == ASKCANCEL) fxp_quit = true; else if(a != ASKNO) dodel = true; } else dodel = true; if(dodel) { ftp_unlink(fi->path); char* sp = shortpath(fi->path, 42, ftp->homedir); if(ftp->code == ctComplete) fprintf(stderr, _("%s: deleted\n"), sp); else fprintf(stderr, _("error deleting '%s': %s\n"), sp, ftp_getreply(false)); free(sp); } } ftp_use(thisftp); return 0; }