Wget patch to delete remote files after downloading

"David Dorsey" <[email protected]> Thu, 15 Sep 2005 19:43:48 -0600
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------040707040208070509080002
Content-Type: text/plain;
 charset=iso-8859-1;
 format=flowed
Content-Transfer-Encoding: 7bit

This patch adds the --delete-remote option.  It deletes the file on the 
server after you download it.  I needed this so I could move files 
rather than merely copy them.  Patch and changelog are attached.


Dave


--------------040707040208070509080002
Content-Type: text/plain;
 name=ChangeLog-newentries
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename=ChangeLog-newentries

to src/ChangeLog

2005-09-15  David Dorsey   <[email protected]>

    * options.h: Added variable bool delete_remote.

    * init.c:  Added deleteremote to commands struct.

    * ftp.h:  Added function prototype for ftp_dele.
    
    * ftp.c:  Added conditional that will delete the file
    on the remote server if the --delete-remote option is set
    and if we just retrieved a file from the server.
    
    * ftp-basic.c:  Added function ftp_dele that sends the dele
    command to the server to delete the file on the remote
    server.
    
    * main.c: Added entries in options for delete-remote option.

to doc/ChangeLog

2005-09-15  David Dorsey  <[email protected]>

	* wget.texi: Added documentation for option --delete-remote



--------------040707040208070509080002
Content-Type: text/x-patch;
 name=deleteremote.patch
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename=deleteremote.patch

Index: src/options.h
===================================================================
--- src/options.h	(revision 2079)
+++ src/options.h	(working copy)
@@ -156,6 +156,9 @@
   bool delete_after;		/* Whether the files will be deleted
 				   after download. */
 
+  bool delete_remote;	    /* Whether the files on the remote sever
+				   will be deleted after download. */
+
   bool html_extension;		/* Use ".html" extension on all text/html? */
 
   bool page_requisites;		/* Whether we need to download all files
Index: src/init.c
===================================================================
--- src/init.c	(revision 2079)
+++ src/init.c	(working copy)
@@ -132,6 +132,7 @@
   { "debug",		&opt.debug,		cmd_boolean },
 #endif
   { "deleteafter",	&opt.delete_after,	cmd_boolean },
+  { "deleteremote",	&opt.delete_remote,	cmd_boolean },
   { "dirprefix",	&opt.dir_prefix,	cmd_directory },
   { "dirstruct",	NULL,			cmd_spec_dirstruct },
   { "dnscache",		&opt.dns_cache,		cmd_boolean },
Index: src/ftp.c
===================================================================
--- src/ftp.c	(revision 2079)
+++ src/ftp.c	(working copy)
@@ -1033,6 +1033,47 @@
       return FTPRETRINT;
     }
 
+  /* If we should delete the remmote file, do it now */
+  if (cmd & DO_RETR && opt.delete_remote)
+    {
+      err = ftp_dele (csock, u->file);
+      /* FTPRERR, WRITEFAILED, FTPNSFOD */
+      switch (err)
+      {
+      case FTPRERR:
+        logputs (LOG_VERBOSE, "\n");
+        logputs (LOG_NOTQUIET, _("\
+Error in server response, closing control connection.\n"));
+        fd_close (csock);
+        con->csock = -1;
+        fd_close (dtsock);
+        fd_close (local_sock);
+        return err;
+      case WRITEFAILED:
+        logputs (LOG_VERBOSE, "\n");
+        logputs (LOG_NOTQUIET,
+           _("Write failed, closing control connection.\n"));
+        fd_close (csock);
+        con->csock = -1;
+        fd_close (dtsock);
+        fd_close (local_sock);
+        return err;
+      case FTPNSFOD:
+        logputs (LOG_VERBOSE, "\n");
+        logprintf (LOG_NOTQUIET, _("No such file to delete `%s'.\n\n"),
+             escnonprint (u->file));
+        fd_close (dtsock);
+        fd_close (local_sock);
+        return err;
+      case FTPOK:
+        break;
+      default:
+        abort ();
+      }
+    logprintf (LOG_VERBOSE, "\nFile %s deleted on remote server because of \
+--delete-remote.\n", u->file);
+    }
+
   if (!(cmd & LEAVE_PENDING))
     {
       /* Closing the socket is faster than sending 'QUIT' and the
Index: src/ftp.h
===================================================================
--- src/ftp.h	(revision 2079)
+++ src/ftp.h	(working copy)
@@ -58,6 +58,7 @@
 uerr_t ftp_type (int, int);
 uerr_t ftp_cwd (int, const char *);
 uerr_t ftp_retr (int, const char *);
+uerr_t ftp_dele (int, const char *);
 uerr_t ftp_rest (int, wgint);
 uerr_t ftp_list (int, const char *);
 uerr_t ftp_syst (int, enum stype *);
Index: src/ftp-basic.c
===================================================================
--- src/ftp-basic.c	(revision 2079)
+++ src/ftp-basic.c	(working copy)
@@ -954,6 +954,44 @@
   return FTPOK;
 }
 
+
+/* Sends DELE command to the FTP server.  */
+uerr_t
+ftp_dele (int csock, const char *file)
+{
+  char *request, *respline;
+  int nwritten;
+  uerr_t err;
+
+  /* Send DELE request.  */
+  request = ftp_request ("DELE", file);
+  nwritten = fd_write (csock, request, strlen (request), -1);
+  if (nwritten < 0)
+    {
+      xfree (request);
+      return WRITEFAILED;
+    }
+  xfree (request);
+  /* Get appropriate response.  */
+  err = ftp_response (csock, &respline);
+  if (err != FTPOK)
+    return err;
+  if (*respline == '5')
+    {
+      xfree (respline);
+      return FTPNSFOD;
+    }
+  if (*respline != '2')
+    {
+      xfree (respline);
+      return FTPRERR;
+    }
+  xfree (respline);
+  /* All OK.  */
+  return FTPOK;
+}
+
+
 /* Sends the LIST command to the server.  If FILE is NULL, send just
    `LIST' (no space).  */
 uerr_t
Index: src/main.c
===================================================================
--- src/main.c	(revision 2079)
+++ src/main.c	(working copy)
@@ -147,6 +147,7 @@
     { "cut-dirs", 0, OPT_VALUE, "cutdirs", -1 },
     { WHEN_DEBUG ("debug"), 'd', OPT_BOOLEAN, "debug", -1 },
     { "delete-after", 0, OPT_BOOLEAN, "deleteafter", -1 },
+    { "delete-remote", 0, OPT_BOOLEAN, "deleteremote", -1 },
     { "directories", 0, OPT_BOOLEAN, "dirstruct", -1 },
     { "directory-prefix", 'P', OPT_VALUE, "dirprefix", -1 },
     { "dns-cache", 0, OPT_BOOLEAN, "dnscache", -1 },
@@ -574,6 +575,8 @@
     N_("\
        --delete-after       delete files locally after downloading them.\n"),
     N_("\
+       --delete-remote      delete files on the remote server after downloading them.\n"),
+    N_("\
   -k,  --convert-links      make links in downloaded HTML point to local files.\n"),
     N_("\
   -K,  --backup-converted   before converting file X, back up as X.orig.\n"),
Index: doc/wget.texi
===================================================================
--- doc/wget.texi	(revision 2079)
+++ doc/wget.texi	(working copy)
@@ -1575,6 +1575,19 @@
 This option is useful when, for some reason, persistent (keep-alive)
 connections don't work for you, for example due to a server bug or due
 to the inability of server-side scripts to cope with the connections.
+
+@cindex delete remote after retrieval
+@item --delete-remote
+This option tells Wget to delete every single file it downloads,
+@emph{after} having done so @emph{on the remote server}.
+
+@example
+wget -r --delete-remote http://whatever.com/~popular/page/
+@end example
+
+Note that @samp{--delete-remote} deletes files on the remote machine.
+It does issue the @samp{DELE} command to remote FTP sites.
+
 @end table
 
 @node Recursive Retrieval Options

--------------040707040208070509080002--