Pantek Library
Hosting Provided By
CybrHost
High Speed Hosting

sftp - reput, reget

From: Dmitry Lohansky <sq(at)oganer.net>
Date: Sat Sep 13 2003 - 06:27:53 EDT


Hello, tech@

I would like to give my patches for sftp-client. I have added -c option for get/put commands, whith it 'get' will download file restarting at the end of local file (like ftp's 'reget' command). Similar for 'put'. Modified files are: sftp-client.c, sftp-int.c, sftp-int.h

For the future.. is it tech@ or misc@ stuff?

  • sftp-client.c.orig Sun Jun 29 00:23:06 2003 +++ sftp-client.c Sat Sep 13 17:51:55 2003
    @@ -44,4 +44,5 @@
    #include "sftp-common.h" #include "sftp-client.h" +#include "sftp-int.h"

 extern int showprogress;
@@ -508,5 +509,5 @@

 			debug("Server version does not support lstat operation");
 		else
-			logit("Server version does not support lstat operation");
+ debug("Server version does not support lstat operation"); return(do_stat(conn, path, quiet)); }
@@ -737,5 +738,5 @@

 int
 do_download(struct sftp_conn *conn, char *remote_path, char *local_path, - int pflag)
+ int pgflags)
 {

         Attrib junk, *a;
@@ -743,8 +744,9 @@

 	char *handle;
 	int local_fd, status, num_req, max_req, write_error;
-	int read_error, write_errno;
+	int read_error, write_errno, open_flags;
 	u_int64_t offset, size;
 	u_int handle_len, mode, type, id, buflen;
 	off_t progress_counter;
+	struct stat sb;
 	struct request {
 		u_int id;

@@ -799,6 +801,9 @@
} - local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, - mode | S_IWRITE); + open_flags = O_WRONLY | O_CREAT; + if (!(pgflags & PG_CONT)) + open_flags |= O_TRUNC; + + local_fd = open(local_path, open_flags, mode | S_IWRITE); if (local_fd == -1) { error("Couldn't open local file \"%s\" for writing: %s",
@@ -808,9 +813,19 @@
return(-1); } + if (pgflags & PG_CONT) { + if (fstat(local_fd, &sb) == -1) { + error("Couldn't fstat local file \"%s\": %s", + local_path, strerror(errno)); + close(local_fd); + return(-1); + } + offset = sb.st_size; + } else + offset = 0; /* Read from remote and write to local */ - write_error = read_error = write_errno = num_req = offset = 0; + write_error = read_error = write_errno = num_req = 0; max_req = 1; - progress_counter = 0; + progress_counter = offset; if (showprogress) {

@@ -945,8 +960,8 @@
 
 		/* Override umask and utimes if asked */
-		if (pflag && fchmod(local_fd, mode) == -1)
+		if ((pgflags & PG_PERM) && fchmod(local_fd, mode) == -1)
 			error("Couldn't set mode on \"%s\": %s", local_path,
 			    strerror(errno));
-		if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
+		if ((pgflags & PG_PERM) && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
 			struct timeval tv[2];
 			tv[0].tv_sec = a->atime;

@@ -967,7 +982,7 @@

 int
 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path, - int pflag)
+ int pgflags)
 {
-	int local_fd, status;
+	int local_fd, status, open_flags;
 	u_int handle_len, id, type;
 	u_int64_t offset;

@@ -975,5 +990,5 @@
Buffer msg; struct stat sb; - Attrib a; + Attrib a, *ar; u_int32_t startid; u_int32_t ackid;
Do you need help?X

@@ -1010,8 +1025,12 @@
a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; a.perm &= 0777; - if (!pflag) + if (!(pgflags & PG_PERM)) a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; buffer_init(&msg); + + open_flags = SSH2_FXF_WRITE | SSH2_FXF_CREAT; + if (!(pgflags & PG_CONT)) + open_flags |= SSH2_FXF_TRUNC; /* Send open request */
@@ -1020,5 +1039,5 @@
buffer_put_int(&msg, id); buffer_put_cstring(&msg, remote_path); - buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC); + buffer_put_int(&msg, open_flags); encode_attrib(&msg, &a); send_msg(conn->fd_out, &msg);

@@ -1026,5 +1045,5 @@
 

         buffer_clear(&msg);
-
+

 	handle = get_handle(conn->fd_in, id, &handle_len);
 	if (handle == NULL) {

@@ -1033,4 +1052,19 @@
return(-1); } + + if (pgflags & PG_CONT) { + ar = do_stat(conn, remote_path, 0); + if (ar == NULL) { + error("Couldn't stat remote file \"%s\"", + remote_path); + do_close(conn, handle, handle_len); + close(local_fd); + buffer_free(&msg); + return(-1); + } + offset = ar->size; + lseek(local_fd, offset, SEEK_SET); + } else + offset = 0; startid = ackid = id + 1;

@@ -1038,5 +1072,4 @@
 
 	/* Read from local and write to remote */
-	offset = 0;
 	if (showprogress)
 		start_progress_meter(local_path, sb.st_size, &offset);

@@ -1135,5 +1168,5 @@
 
 	/* Override umask and utimes if asked */
-	if (pflag)
+	if (pgflags & PG_PERM)
 		do_fsetstat(conn, handle, handle_len, &a);
 

--- sftp-int.c.orig	Mon Aug 25 16:13:09 2003
+++ sftp-int.c	Sat Sep 13 18:07:00 2003

@@ -282,5 +282,5 @@
 

 static int
-parse_getput_flags(const char **cpp, int *pflag) +parse_getput_flags(const char **cpp, int *pgflags)  {

         const char *cp = *cpp;
@@ -287,17 +287,26 @@
 

 	/* Check for flags */
-	if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
-		switch (cp[1]) {
-		case 'p':
-		case 'P':
-			*pflag = 1;
-			break;
-		default:
-			error("Invalid flag -%c", cp[1]);
-			return(-1);
-		}
-		cp += 2;
-		*cpp = cp + strspn(cp, WHITESPACE);
+	while (cp[0] == '-') {
+		cp++;
+		while (cp[0] && !strchr(WHITESPACE, cp[0]))
Can we help you?X
+ switch (cp[0]) { + case 'p': + case 'P': + *pgflags += PG_PERM; + cp++; + break; + case 'c': + case 'C': + *pgflags += PG_CONT; + cp++; + break; + default: + error("Invalid flag -%c", cp[0]); + return(-1); + } + cp += strspn(cp, WHITESPACE); } + + *cpp = cp; return(0);

@@ -432,5 +441,5 @@
 
Do you need more help?X

 static int
-process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) +process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pgflags)  {

         char *abs_src = NULL;
@@ -484,5 +493,5 @@
 

 		printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
-		if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
+		if (do_download(conn, g.gl_pathv[i], abs_dst, pgflags) == -1)
 			err = -1;
 		xfree(abs_dst);

@@ -499,5 +508,5 @@
 

 static int
-process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) +process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pgflags)  {

         char *tmp_dst = NULL;
@@ -559,5 +568,5 @@
 

 		printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
-		if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
+		if (do_upload(conn, g.gl_pathv[i], abs_dst, pgflags) == -1)
 			err = -1;
 	}

@@ -738,5 +747,5 @@
 

 static int
-parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, +parse_args(const char **cpp, int *pgflags, int *lflag, int *iflag,

     unsigned long *n_arg, char **path1, char **path2)  {
@@ -786,5 +795,5 @@
 

 	/* Get arguments and parse flags */
-	*lflag = *pflag = *n_arg = 0;
+	*lflag = *pgflags = *n_arg = 0;
 	*path1 = *path2 = NULL;
 	switch (cmdnum) {

@@ -791,5 +800,5 @@
case I_GET: case I_PUT: - if (parse_getput_flags(&cp, pflag)) + if (parse_getput_flags(&cp, pgflags)) return(-1); /* Get first pathname (mandatory) */

@@ -897,5 +906,5 @@

 {
 	char *path1, *path2, *tmp;
-	int pflag, lflag, iflag, cmdnum, i;
+	int pgflags, lflag, iflag, cmdnum, i;
 	unsigned long n_arg;
 	Attrib a, *aa;

@@ -905,5 +914,5 @@
 
 	path1 = path2 = NULL;
-	cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
+	cmdnum = parse_args(&cmd, &pgflags, &lflag, &iflag, &n_arg,
 	    &path1, &path2);
 

@@ -923,8 +932,8 @@
break; case I_GET: - err = process_get(conn, path1, path2, *pwd, pflag); + err = process_get(conn, path1, path2, *pwd, pgflags); break; case I_PUT: - err = process_put(conn, path1, path2, *pwd, pflag); + err = process_put(conn, path1, path2, *pwd, pgflags); break; case I_RENAME: --- sftp-int.h.orig Thu Jan 9 06:53:26 2003 +++ sftp-int.h Sat Sep 13 18:02:40 2003
Can't find what you're looking for?X

@@ -25,3 +25,11 @@

  */  
+#ifndef _SFTP_INT_H
+#define _SFTP_INT_H
+
+#define PG_PERM 1
+#define PG_CONT 2
+
 int	 interactive_loop(int, int, char *, char *);
+
+#endif /* _SFTP_INT_H */ Received on Sat Sep 13 06:34:34 2003

This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 13:48:44 EDT


Contact Us  Legal Notices  Order Services Online 
Pantek Home  Privacy Policy  IT news  Site Map  Pantek Library