X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/34580230cecf68453395e5698bb0176ab473748f..23e4df365f332a92d97fae27212b2cf777d5b8e0:/psftp.c diff --git a/psftp.c b/psftp.c index db3ae7f5..65d52c3b 100644 --- a/psftp.c +++ b/psftp.c @@ -16,6 +16,8 @@ #include "sftp.h" #include "int64.h" +const char *const appname = "PSFTP"; + /* * Since SFTP is a request-response oriented protocol, it requires * no buffer management: when we send data, we stop and wait for an @@ -128,8 +130,12 @@ char *canonify(char *name) assert(rreq == req); canonname = fxp_realpath_recv(pktin, rreq); - if (!canonname) - return fullname; /* even that failed; give up */ + if (!canonname) { + /* Even that failed. Restore our best guess at the + * constructed filename and give up */ + fullname[i] = '/'; /* restore slash and last component */ + return fullname; + } /* * We have a canonical name for all but the last path @@ -317,22 +323,24 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart) * If none of them exists, of course, we start at 0. */ i = 0; - while (i < nnames) { - char *nextoutfname; - int ret; - if (outfname) - nextoutfname = dir_file_cat(outfname, - ournames[i]->filename); - else - nextoutfname = dupstr(ournames[i]->filename); - ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT); - sfree(nextoutfname); - if (ret) - break; - i++; - } - if (i > 0) - i--; + if (restart) { + while (i < nnames) { + char *nextoutfname; + int ret; + if (outfname) + nextoutfname = dir_file_cat(outfname, + ournames[i]->filename); + else + nextoutfname = dupstr(ournames[i]->filename); + ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT); + sfree(nextoutfname); + if (ret) + break; + i++; + } + if (i > 0) + i--; + } /* * Now we're ready to recurse. Starting at ournames[i] @@ -405,14 +413,15 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart) if (restart) { char decbuf[30]; if (seek_file(file, uint64_make(0,0) , FROM_END) == -1) { + close_wfile(file); printf("reget: cannot restart %s - file too large\n", outfname); - sftp_register(req = fxp_close_send(fh)); - rreq = sftp_find_request(pktin = sftp_recv()); - assert(rreq == req); - fxp_close_recv(pktin, rreq); + sftp_register(req = fxp_close_send(fh)); + rreq = sftp_find_request(pktin = sftp_recv()); + assert(rreq == req); + fxp_close_recv(pktin, rreq); - return 0; + return 0; } offset = get_file_posn(file); @@ -457,6 +466,7 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart) printf("error while writing local file\n"); ret = 0; xfer_set_error(xfer); + break; } wpos += wlen; } @@ -564,23 +574,25 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart) * If none of them exists, of course, we start at 0. */ i = 0; - while (i < nnames) { - char *nextoutfname; - nextoutfname = dupcat(outfname, "/", ournames[i], NULL); - sftp_register(req = fxp_stat_send(nextoutfname)); - rreq = sftp_find_request(pktin = sftp_recv()); - assert(rreq == req); - result = fxp_stat_recv(pktin, rreq, &attrs); - sfree(nextoutfname); - if (!result) - break; - i++; - } - if (i > 0) - i--; + if (restart) { + while (i < nnames) { + char *nextoutfname; + nextoutfname = dupcat(outfname, "/", ournames[i], NULL); + sftp_register(req = fxp_stat_send(nextoutfname)); + rreq = sftp_find_request(pktin = sftp_recv()); + assert(rreq == req); + result = fxp_stat_recv(pktin, rreq, &attrs); + sfree(nextoutfname); + if (!result) + break; + i++; + } + if (i > 0) + i--; + } - /* - * Now we're ready to recurse. Starting at ournames[i] + /* + * Now we're ready to recurse. Starting at ournames[i] * and continuing on to the end of the list, we * construct a new source and target file name, and * call sftp_put_file again. @@ -634,6 +646,7 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart) fh = fxp_open_recv(pktin, rreq); if (!fh) { + close_rfile(file); printf("%s: open for write: %s\n", outfname, fxp_error()); return 0; } @@ -649,10 +662,12 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart) ret = fxp_fstat_recv(pktin, rreq, &attrs); if (!ret) { + close_rfile(file); printf("read size of %s: %s\n", outfname, fxp_error()); return 0; } if (!(attrs.flags & SSH_FILEXFER_ATTR_SIZE)) { + close_rfile(file); printf("read size of %s: size was not given\n", outfname); return 0; } @@ -694,7 +709,7 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart) if (!xfer_done(xfer)) { pktin = sftp_recv(); ret = xfer_upload_gotpkt(xfer, pktin); - if (!ret) { + if (ret <= 0 && !err) { printf("error while writing: %s\n", fxp_error()); err = 1; } @@ -952,7 +967,7 @@ int sftp_cmd_close(struct sftp_command *cmd) return 0; } - if (back != NULL && back->socket(backhandle) != NULL) { + if (back != NULL && back->connected(backhandle)) { char ch; back->special(backhandle, TS_EOF); sftp_recvdata(&ch, 1); @@ -2227,6 +2242,11 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) cmd->words = sresize(cmd->words, cmd->wordssize, char *); cmd->words[0] = dupstr("!"); cmd->words[1] = dupstr(p+1); + } else if (*p == '#') { + /* + * Special case: comment. Entire line is ignored. + */ + cmd->nwords = cmd->wordssize = 0; } else { /* @@ -2498,7 +2518,8 @@ int from_backend(void *frontend, int is_stderr, const char *data, int datalen) */ if (is_stderr) { if (len > 0) - fwrite(data, 1, len, stderr); + if (fwrite(data, 1, len, stderr) < len) + /* oh well */; return 0; } @@ -2743,6 +2764,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber) cfg.x11_forward = 0; cfg.agentfwd = 0; cfg.portfwd[0] = cfg.portfwd[1] = '\0'; + cfg.ssh_simple = TRUE; /* Set up subsystem name. */ strcpy(cfg.remote_cmd, "sftp"); @@ -2784,6 +2806,8 @@ static int psftp_connect(char *userhost, char *user, int portnumber) back->provide_logctx(backhandle, logctx); console_provide_logctx(logctx); while (!back->sendok(backhandle)) { + if (back->exitcode(backhandle) >= 0) + return 1; if (ssh_sftp_loop_iteration() < 0) { fprintf(stderr, "ssh_init: error during SSH connection setup\n"); return 1; @@ -2909,7 +2933,7 @@ int psftp_main(int argc, char *argv[]) do_sftp(mode, modeflags, batchfile); - if (back != NULL && back->socket(backhandle) != NULL) { + if (back != NULL && back->connected(backhandle)) { char ch; back->special(backhandle, TS_EOF); sftp_recvdata(&ch, 1);