]> sjero.net Git - wget/blobdiff - src/ftp.c
[svn] Gracefully handle opt.downloaded overflowing.
[wget] / src / ftp.c
index 1f65c254dd4b6bd804a36e52e7bd339f281ab009..d2399311e6a56a2f0863b40668aa7b4109ad8bda 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;
@@ -648,6 +648,15 @@ Error in server response, closing control connection.\n"));
       expected_bytes = ftp_expected_bytes (ftp_last_respline);
     } /* cmd & DO_LIST */
 
+  /* 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 no transmission was required, then everything is OK.  */
   if (!(cmd & (DO_LIST | DO_RETR)))
     return RETRFINISHED;
@@ -685,7 +694,16 @@ Error in server response, closing control connection.\n"));
        }
     }
   else
-    fp = opt.dfp;
+    {
+      fp = opt.dfp;
+      if (!restval)
+       {
+         /* This will silently fail for streams that don't correspond
+            to regular files, but that's OK.  */
+         rewind (fp);
+         clearerr (fp);
+       }
+    }
 
   if (*len)
     {
@@ -980,13 +998,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. */
        {
-         ++opt.numurls;
-         opt.downloaded += len;
+         if (!opt.remove_listing)
+           /* --dont-remove-listing was specified, so do count this towards the
+              number of bytes and files downloaded. */
+           {
+             downloaded_increase (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. */
+       {
+         /* 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. */
+         downloaded_increase (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;
@@ -1095,7 +1142,7 @@ ftp_retrieve_list (struct urlinfo *u, struct fileinfo *f, ccon *con)
 
   while (f)
     {
-      if (opt.quota && opt.downloaded > opt.quota)
+      if (downloaded_exceeds_quota ())
        {
          --depth;
          return QUOTEXC;
@@ -1261,7 +1308,7 @@ ftp_retrieve_dirs (struct urlinfo *u, struct fileinfo *f, ccon *con)
     {
       int len;
 
-      if (opt.quota && opt.downloaded > opt.quota)
+      if (downloaded_exceeds_quota ())
        break;
       if (f->type != FT_DIRECTORY)
        continue;
@@ -1382,7 +1429,7 @@ ftp_retrieve_glob (struct urlinfo *u, ccon *con, int action)
        }
     }
   freefileinfo (start);
-  if (opt.quota && opt.downloaded > opt.quota)
+  if (downloaded_exceeds_quota ())
     return QUOTEXC;
   else
     /* #### Should we return `res' here?  */