scp patch to delete source files after copy

adam at skullslayer.rod.org adam at skullslayer.rod.org
Sun May 14 19:02:04 EST 2006


At work we have a large collection of scripts to move log
and config files around. These depend on commercial (F-Secure)
ssh/scp, as it supports the -u option to delete the source file
after (successful) copying. That is the sole reason we can't
run openssh on our 150+ unix boxes.

I have attached a patch below, which adds the -u option to
delete the source file after copying, provided there were no
errors. I would appreciate any feedback on this patch, with
an aim to seeing it included in future releases.

    Adam



*** scp.c	2006-01-31 22:11:38.000000000 +1100
--- scp.c.new	2006-05-14 18:11:14.000000000 +1000
***************
*** 99,104 ****
--- 99,107 ----
  /* This is set to zero if the progressmeter is not desired. */
  int showprogress = 1;
  
+ /* Delete the source after copying. */
+ int delete_source = 0;
+ 
  /* This is the program to execute for the secured connection. ("ssh" or -S) */
  char *ssh_program = _PATH_SSH_PROGRAM;
  
***************
*** 278,284 ****
  	addargs(&args, "-oClearAllForwardings yes");
  
  	fflag = tflag = 0;
! 	while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
  		switch (ch) {
  		/* User-visible flags. */
  		case '1':
--- 281,287 ----
  	addargs(&args, "-oClearAllForwardings yes");
  
  	fflag = tflag = 0;
! 	while ((ch = getopt(argc, argv, "dfl:prtuvBCc:i:P:q1246S:o:F:")) != -1)
  		switch (ch) {
  		/* User-visible flags. */
  		case '1':
***************
*** 315,320 ****
--- 318,326 ----
  		case 'S':
  			ssh_program = xstrdup(optarg);
  			break;
+ 		case 'u':
+ 			delete_source = 1;
+ 			break;
  		case 'v':
  			addargs(&args, "-v");
  			verbose_mode = 1;
***************
*** 373,381 ****
  	remin = remout = -1;
  	do_cmd_pid = -1;
  	/* Command to be executed on remote system using "ssh". */
! 	(void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
  	    verbose_mode ? " -v" : "",
  	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
  	    targetshouldbedirectory ? " -d" : "");
  
  	(void) signal(SIGPIPE, lostconn);
--- 379,388 ----
  	remin = remout = -1;
  	do_cmd_pid = -1;
  	/* Command to be executed on remote system using "ssh". */
! 	(void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s%s",
  	    verbose_mode ? " -v" : "",
  	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
+ 	    delete_source ? " -u" : "",
  	    targetshouldbedirectory ? " -d" : "");
  
  	(void) signal(SIGPIPE, lostconn);
***************
*** 651,659 ****
  				haderr = errno;
  			fd = -1;
  		}
! 		if (!haderr)
  			(void) atomicio(vwrite, remout, "", 1);
! 		else
  			run_err("%s: %s", name, strerror(haderr));
  		(void) response();
  	}
--- 658,672 ----
  				haderr = errno;
  			fd = -1;
  		}
! 		if (!haderr) {
! 			if (delete_source) {
! 				if (verbose_mode) {
! 					fprintf(stderr, "Deleting source: %s\n", name);
! 				}
! 				unlink(name);
! 			}
  			(void) atomicio(vwrite, remout, "", 1);
! 		} else
  			run_err("%s: %s", name, strerror(haderr));
  		(void) response();
  	}
***************
*** 1084,1090 ****
  usage(void)
  {
  	(void) fprintf(stderr,
! 	    "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
  	    "           [-l limit] [-o ssh_option] [-P port] [-S program]\n"
  	    "           [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
  	exit(1);
--- 1097,1103 ----
  usage(void)
  {
  	(void) fprintf(stderr,
! 	    "usage: scp [-1246BCpqruv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
  	    "           [-l limit] [-o ssh_option] [-P port] [-S program]\n"
  	    "           [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
  	exit(1);
*** scp.1	2006-01-20 11:31:47.000000000 +1100
--- scp.1.new	2006-05-14 18:59:35.000000000 +1000
***************
*** 20,26 ****
  .Sh SYNOPSIS
  .Nm scp
  .Bk -words
! .Op Fl 1246BCpqrv
  .Op Fl c Ar cipher
  .Op Fl F Ar ssh_config
  .Op Fl i Ar identity_file
--- 20,26 ----
  .Sh SYNOPSIS
  .Nm scp
  .Bk -words
! .Op Fl 1246BCpqruv
  .Op Fl c Ar cipher
  .Op Fl F Ar ssh_config
  .Op Fl i Ar identity_file
***************
*** 188,193 ****
--- 188,195 ----
  The program must understand
  .Xr ssh 1
  options.
+ .It Fl u
+ Deletes the source file after copying.
  .It Fl v
  Verbose mode.
  Causes




More information about the openssh-unix-dev mailing list