]> sjero.net Git - wget/blobdiff - src/ftp.c
[svn] --delete-after wasn't implemented for files retrieved by FTP or corresponding to
[wget] / src / ftp.c
index 9a7f33d81a13627d4c6135355a64bc09c21a174e..ab70114b33220b00005bc22e428d730bbeed8005 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -337,7 +337,7 @@ Error in server response, closing control connection.\n"));
   /* If anything is to be retrieved, PORT (or PASV) must be sent.  */
   if (cmd & (DO_LIST | DO_RETR))
     {
-      if (opt.ftp_pasv)
+      if (opt.ftp_pasv > 0)
        {
          char thost[256];
          unsigned short tport;
@@ -686,6 +686,15 @@ Error in server response, closing control connection.\n"));
     }
   else
     fp = opt.dfp;
+  
+  /* Some FTP servers return the total length of file after REST command,
+     others just return the remaining size. */  
+  if (*len && restval && expected_bytes
+      && (expected_bytes == *len - restval))
+  {
+    DEBUGP (("Lying FTP server found, adjusting.\n"));
+    expected_bytes = *len;
+  }
 
   if (*len)
     {
@@ -711,10 +720,18 @@ Error in server response, closing control connection.\n"));
   /* Close data connection socket.  */
   closeport (dtsock);
   /* Close the local file.  */
-  if (!opt.dfp || con->cmd & DO_LIST)
-    fclose (fp);
-  else
-    fflush (fp);
+  {
+    /* Close or flush the file.  We have to be careful to check for
+       error here.  Checking the result of fwrite() is not enough --
+       errors could go unnoticed!  */
+    int flush_res;
+    if (!opt.dfp || con->cmd & DO_LIST)
+      flush_res = fclose (fp);
+    else
+      flush_res = fflush (fp);
+    if (flush_res == EOF)
+      res = -2;
+  }
   /* If get_contents couldn't write to fp, bail out.  */
   if (res == -2)
     {
@@ -961,7 +978,7 @@ ftp_loop_internal (struct urlinfo *u, struct fileinfo *f, ccon *con)
 
       /* If we get out of the switch above without continue'ing, we've
         successfully downloaded a file.  Remember this fact. */
-      downloaded_file(ADD_FILE, locf);
+      downloaded_file(FILE_DOWNLOADED_NORMALLY, locf);
 
       if (con->st & ON_YOUR_OWN)
        {
@@ -972,13 +989,42 @@ ftp_loop_internal (struct urlinfo *u, struct fileinfo *f, ccon *con)
                 tms, tmrate, locf, len);
       logprintf (LOG_NONVERBOSE, "%s URL: %s [%ld] -> \"%s\" [%d]\n",
                 tms, u->url, len, locf, count);
-      /* Do not count listings among the downloaded stuff, since they
-        will get deleted anyway.  */
-      if (!(con->cmd & DO_LIST))
+
+      if ((con->cmd & DO_LIST))
+       /* This is a directory listing file. */
+       {
+         if (!opt.remove_listing)
+           /* --dont-remove-listing was specified, so do count this towards the
+              number of bytes and files downloaded. */
+           {
+             opt.downloaded += len;
+             opt.numurls++;
+           }
+
+         /* Deletion of listing files is not controlled by --delete-after, but
+            by the more specific option --dont-remove-listing, and the code
+            to do this deletion is in another function. */
+       }
+      else
+       /* This is not a directory listing file. */
        {
-         ++opt.numurls;
+         /* Unlike directory listing files, don't pretend normal files weren't
+            downloaded if they're going to be deleted.  People seeding proxies,
+            for instance, may want to know how many bytes and files they've
+            downloaded through it. */
          opt.downloaded += len;
+         opt.numurls++;
+
+         if (opt.delete_after)
+           {
+             DEBUGP (("Removing file due to --delete-after in"
+                      " ftp_loop_internal():\n"));
+             logprintf (LOG_VERBOSE, _("Removing %s.\n"), locf);
+             if (unlink (locf))
+               logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
+           }
        }
+      
       /* Restore the original leave-pendingness.  */
       if (orig_lp)
        con->cmd |= LEAVE_PENDING;
@@ -1060,7 +1106,7 @@ ftp_retrieve_list (struct urlinfo *u, struct fileinfo *f, ccon *con)
 
   /* Increase the depth.  */
   ++depth;
-  if (opt.reclevel && depth > opt.reclevel)
+  if (opt.reclevel != INFINITE_RECURSION && depth > opt.reclevel)
     {
       DEBUGP ((_("Recursion depth %d exceeded max. depth %d.\n"),
               depth, opt.reclevel));
@@ -1213,7 +1259,7 @@ Already have correct symlink %s -> %s\n\n"),
       else if (f->tstamp == -1)
        logprintf (LOG_NOTQUIET, _("%s: corrupt time-stamp.\n"), u->local);
 
-      if (f->perms && dlthis)
+      if (f->perms && f->type == FT_PLAINFILE && dlthis)
        chmod (u->local, f->perms);
       else
        DEBUGP (("Unrecognized permissions for %s.\n", u->local));
@@ -1228,7 +1274,8 @@ Already have correct symlink %s -> %s\n\n"),
       f = f->next;
     } /* while */
   /* We do not want to call ftp_retrieve_dirs here */
-  if (opt.recursive && !(opt.reclevel && depth >= opt.reclevel))
+  if (opt.recursive &&
+      !(opt.reclevel != INFINITE_RECURSION && depth >= opt.reclevel))
     err = ftp_retrieve_dirs (u, orig, con);
   else if (opt.recursive)
     DEBUGP ((_("Will not retrieve dirs since depth is %d (max %d).\n"),