]> sjero.net Git - wget/blobdiff - src/ftp.c
[svn] Fixes by Christian Biere:
[wget] / src / ftp.c
index 8d24355f94153532cb06e1b9a6bac4f73304347a..e4c3b621be6c7fae1dbe22281554edccaaca946a 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -66,6 +66,9 @@ extern LARGE_INT total_downloaded_bytes;
 
 extern char ftp_last_respline[];
 
+extern FILE *output_stream;
+extern int output_stream_regular;
+
 typedef struct
 {
   int st;                      /* connection status */
@@ -241,6 +244,9 @@ getftp (struct url *u, long *len, long restval, ccon *con)
   int cmd = con->cmd;
   int pasv_mode_open = 0;
   long expected_bytes = 0L;
+  int rest_failed = 0;
+  int flags;
+  long rd_size;
 
   assert (con != NULL);
   assert (con->target != NULL);
@@ -791,22 +797,8 @@ Error in server response, closing control connection.\n"));
          return err;
          break;
        case FTPRESTFAIL:
-         /* If `-c' is specified and the file already existed when
-            Wget was started, it would be a bad idea for us to start
-            downloading it from scratch, effectively truncating it.  */
-         if (opt.always_rest && (cmd & NO_TRUNCATE))
-           {
-             logprintf (LOG_NOTQUIET,
-                        _("\nREST failed; will not truncate `%s'.\n"),
-                        con->target);
-             fd_close (csock);
-             con->csock = -1;
-             fd_close (dtsock);
-             fd_close (local_sock);
-             return CONTNOTSUPPORTED;
-           }
          logputs (LOG_VERBOSE, _("\nREST failed, starting from scratch.\n"));
-         restval = 0L;
+         rest_failed = 1;
          break;
        case FTPOK:
          /* fine and dandy */
@@ -965,8 +957,8 @@ Error in server response, closing control connection.\n"));
        }
     }
 
-  /* Open the file -- if opt.dfp is set, use it instead.  */
-  if (!opt.dfp || con->cmd & DO_LIST)
+  /* Open the file -- if output_stream is set, use it instead.  */
+  if (!output_stream || con->cmd & DO_LIST)
     {
       mkalldirs (con->target);
       if (opt.backups)
@@ -986,24 +978,7 @@ Error in server response, closing control connection.\n"));
        }
     }
   else
-    {
-      extern int global_download_count;
-      fp = opt.dfp;
-
-      /* Rewind the output document if the download starts over and if
-        this is the first download.  See gethttp() for a longer
-        explanation.  */
-      if (!restval && global_download_count == 0 && opt.dfp != stdout)
-       {
-         /* This will silently fail for streams that don't correspond
-            to regular files, but that's OK.  */
-         rewind (fp);
-         /* ftruncate is needed because opt.dfp is opened in append
-            mode if opt.always_rest is set.  */
-         ftruncate (fileno (fp), 0);
-         clearerr (fp);
-       }
-    }
+    fp = output_stream;
 
   if (*len)
     {
@@ -1023,10 +998,17 @@ Error in server response, closing control connection.\n"));
     }
 
   /* Get the contents of the document.  */
-  res = fd_read_body (dtsock, fp, len, restval, expected_bytes, 0,
-                     &con->dltime);
+  flags = 0;
+  if (restval && rest_failed)
+    flags |= rb_skip_startpos;
+  *len = restval;
+  rd_size = 0;
+  res = fd_read_body (dtsock, fp,
+                     expected_bytes ? expected_bytes - restval : 0,
+                     restval, &rd_size, len, &con->dltime, flags);
+
   tms = time_str (NULL);
-  tmrate = retr_rate (*len - restval, con->dltime, 0);
+  tmrate = retr_rate (rd_size, con->dltime, 0);
   /* Close data connection socket.  */
   fd_close (dtsock);
   fd_close (local_sock);
@@ -1036,7 +1018,7 @@ Error in server response, closing control connection.\n"));
        error here.  Checking the result of fwrite() is not enough --
        errors could go unnoticed!  */
     int flush_res;
-    if (!opt.dfp || con->cmd & DO_LIST)
+    if (!output_stream || con->cmd & DO_LIST)
       flush_res = fclose (fp);
     else
       flush_res = fflush (fp);
@@ -1102,8 +1084,8 @@ Error in server response, closing control connection.\n"));
 
   if (!(cmd & LEAVE_PENDING))
     {
-      /* I should probably send 'QUIT' and check for a reply, but this
-        is faster.  #### Is it OK, though?  */
+      /* Closing the socket is faster than sending 'QUIT' and the
+        effect is the same.  */
       fd_close (csock);
       con->csock = -1;
     }
@@ -1141,7 +1123,7 @@ static uerr_t
 ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
 {
   int count, orig_lp;
-  long restval, len;
+  long restval, len = 0;
   char *tms, *locf;
   char *tmrate = NULL;
   uerr_t err;
@@ -1199,20 +1181,14 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
            con->cmd |= DO_CWD;
        }
 
-      /* Assume no restarting.  */
-      restval = 0L;
-      if ((count > 1 || opt.always_rest)
-         && !(con->cmd & DO_LIST)
-         && file_exists_p (locf))
-       if (stat (locf, &st) == 0 && S_ISREG (st.st_mode))
-         restval = st.st_size;
-
-      /* In `-c' is used, check whether the file we're writing to
-        exists and is of non-zero length.  If so, we'll refuse to
-        truncate it if the server doesn't support continued
-        downloads.  */
-      if (opt.always_rest && restval > 0)
-       con->cmd |= NO_TRUNCATE;
+      /* Decide whether or not to restart.  */
+      restval = 0;
+      if (count > 1)
+       restval = len;          /* start where the previous run left off */
+      else if (opt.always_rest
+              && stat (locf, &st) == 0
+              && S_ISREG (st.st_mode))
+       restval = st.st_size;
 
       /* Get the current time string.  */
       tms = time_str (NULL);
@@ -1220,7 +1196,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
       if (opt.verbose)
        {
          char *hurl = url_string (u, 1);
-         char tmp[15];
+         char tmp[256];
          strcpy (tmp, "        ");
          if (count > 1)
            sprintf (tmp, _("(try:%2d)"), count);
@@ -1273,7 +1249,6 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
          /* Not as great.  */
          abort ();
        }
-      /* Time?  */
       tms = time_str (NULL);
       if (!opt.spider)
         tmrate = retr_rate (len - restval, con->dltime, 0);
@@ -1598,7 +1573,7 @@ Already have correct symlink %s -> %s\n\n"),
          const char *fl = NULL;
          if (opt.output_document)
            {
-             if (opt.od_known_regular)
+             if (output_stream_regular)
                fl = opt.output_document;
            }
          else