Skip to content

Commit a1abb1b

Browse files
committed
Also implement scp upload.
1 parent ce79f7d commit a1abb1b

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

BUGS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ known BUGS
1111
* using a slash ('/') in a bookmarks alias name will lead yafc into thinking
1212
there is a directory to change into
1313
* issuing a "ls /usr /var" gives a listing of both directories mixed together
14-
* slow SFTP transfers
1514
* Stalled transfers never timeout
1615

1716
--

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
TODO: Yafc 1.2.0
22
-----------------
33

4-
* Fix SFTP slow transfers
54
* Add support for stalled transfers to timeout
65
> ability to specify preferred protocol (ssh/ftp) (Steve Grecni)
76
> Enhance Documentation

src/ftp/ssh_cmd.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,96 @@ int ssh_do_receive(const char *infile, FILE *fp, getmode_t mode,
805805
return (r == 0 && !ftp->ti.ioerror && !ftp->ti.interrupted) ? 0 : -1;
806806
}
807807

808+
static int do_scp_write(ssh_scp scp, const char* path, FILE* fp,
809+
ftp_transfer_func hookf)
810+
{
811+
time_t then = time(NULL) - 1;
812+
ftp_set_close_handler();
813+
814+
if (hookf)
815+
hookf(&ftp->ti);
816+
ftp->ti.begin = false;
817+
818+
struct stat sb;
819+
errno = 0;
820+
if (fstat(fileno(fp), &sb) == -1)
821+
{
822+
ftp_err(_("Couldn't fstat local file: %s\n"), strerror(errno));
823+
ssh_scp_free(scp);
824+
return -1;
825+
}
826+
827+
int rc = ssh_scp_push_file(scp, path, sb.st_size, sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
828+
if (rc != SSH_OK)
829+
{
830+
ftp_err(_("Failed to start scp upload: %s\n"),
831+
ssh_get_error(ftp->session));
832+
ssh_scp_close(scp);
833+
ssh_scp_free(scp);
834+
return -1;
835+
}
836+
837+
/* read file */
838+
char buffer[SSH_BUFSIZ];
839+
ssize_t nbytes = 0;
840+
errno = 0;
841+
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
842+
{
843+
if (ftp_sigints() > 0)
844+
{
845+
ftp_trace("break due to sigint\n");
846+
break;
847+
}
848+
849+
rc = ssh_scp_write(scp, buffer, nbytes);
850+
if (rc != SSH_OK)
851+
{
852+
ftp_err(_("Error while writing to file: %s\n"), ssh_get_error(ftp->session));
853+
ssh_scp_close(scp);
854+
ssh_scp_free(scp);
855+
return -1;
856+
}
857+
858+
ftp->ti.size += nbytes;
859+
if (hookf)
860+
{
861+
time_t now = time(NULL);
862+
if (now > then)
863+
{
864+
hookf(&ftp->ti);
865+
then = now;
866+
}
867+
}
868+
errno = 0;
869+
}
870+
871+
if (ferror(fp))
872+
{
873+
ftp_err(_("Failed to read from file: %s\n"), strerror(errno));
874+
rc = -1;
875+
}
876+
877+
ssh_scp_close(scp);
878+
ssh_scp_free(scp);
879+
return rc;
880+
}
881+
808882
static int do_write(const char* path, FILE* fp, ftp_transfer_func hookf,
809883
uint64_t offset)
810884
{
885+
/* try to set up a scp connection */
886+
if (!offset)
887+
{
888+
ssh_scp scp = ssh_scp_new(ftp->session, SSH_SCP_WRITE, path);
889+
if (scp != NULL)
890+
{
891+
int rc = ssh_scp_init(scp);
892+
if (rc == SSH_OK)
893+
return do_scp_write(scp, path, fp, hookf);
894+
ssh_scp_free(scp);
895+
}
896+
}
897+
811898
time_t then = time(NULL) - 1;
812899
ftp_set_close_handler();
813900

0 commit comments

Comments
 (0)