- Bug report #2709004 (http://curl.haxx.se/bug/view.cgi?id=2709004) by Tim
authorDaniel Stenberg <daniel@haxx.se>
Tue, 28 Apr 2009 11:19:10 +0000 (11:19 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 28 Apr 2009 11:19:10 +0000 (11:19 +0000)
  Chen pointed out how curl couldn't upload with resume when reading from a
  pipe.

  This ended up with the introduction of a new return code for the
  CURLOPT_SEEKFUNCTION callback that basically says that the seek failed but
  that libcurl may try to resolve the situation anyway. In our case this means
  libcurl will attempt to instead read that much data from the stream instead
  of seeking and that way curl can now upload with resume when data is read
  from a stream!

CHANGES
RELEASE-NOTES
docs/libcurl/curl_easy_setopt.3
include/curl/curl.h
lib/ftp.c
lib/http.c
lib/ssh.c
src/main.c

diff --git a/CHANGES b/CHANGES
index 76d7470..744422a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,18 @@
 
                                   Changelog
 
+Daniel Stenberg (28 Apr 2009)
+- Bug report #2709004 (http://curl.haxx.se/bug/view.cgi?id=2709004) by Tim
+  Chen pointed out how curl couldn't upload with resume when reading from a
+  pipe.
+
+  This ended up with the introduction of a new return code for the
+  CURLOPT_SEEKFUNCTION callback that basically says that the seek failed but
+  that libcurl may try to resolve the situation anyway. In our case this means
+  libcurl will attempt to instead read that much data from the stream instead
+  of seeking and that way curl can now upload with resume when data is read
+  from a stream!
+
 Daniel Stenberg (26 Apr 2009)
 - Bug report #2779733 (http://curl.haxx.se/bug/view.cgi?id=2779733) by Sven
   Wegener pointed out that CURLINFO_APPCONNECT_TIME didn't work with the multi
index 243d7ae..77ad07f 100644 (file)
@@ -13,6 +13,8 @@ This release includes the following changes:
    connection
  o libssh2's version number can now be figured out run-time instead of using
    the build-time fixed number
+ o CURLOPT_SEEKFUNCTION may now return CURL_SEEKFUNC_CANTSEEK
+ o curl can now upload with resume even when reading from a pipe
 
 This release includes the following bugfixes:
 
@@ -43,6 +45,7 @@ advice from friends like these:
  Daniel Fandrich, Yang Tse, David James, Chris Deidun, Bill Egert,
  Andre Guibert de Bruet, Andreas Farber, Frank Hempel, Pierre Brico,
  Kamil Dudka, Jim Freeman, Daniel Johnson, Toshio Kuratomi, Martin Storsjo,
- Pramod Sharma, Gisle Vanem, Leanic Lefever, Rainer Koenig, Sven Wegener
+ Pramod Sharma, Gisle Vanem, Leanic Lefever, Rainer Koenig, Sven Wegener,
+ Tim Chen
 
         Thanks! (and sorry if I forgot to mention someone)
index 0c93e7f..06e3a11 100644 (file)
@@ -189,8 +189,11 @@ uploaded bytes with the normal read function/callback). It is also called to
 rewind a stream when doing a HTTP PUT or POST with a multi-pass authentication
 method. The function shall work like "fseek" or "lseek" and accepted SEEK_SET,
 SEEK_CUR and SEEK_END as argument for origin, although (in 7.18.0) libcurl
-only passes SEEK_SET. The callback must return 0 on success as returning
-something else will cause the upload operation to fail.
+only passes SEEK_SET. The callback must return 0 (CURL_SEEKFUNC_OK) on
+success, 1 (CURL_SEEKFUNC_FAIL) to cause the upload operation to fail or 2
+(CURL_SEEKFUNC_CANTSEEK) to indicate that while the seek failed, libcurl is
+free to work around the problem if possible. The latter can sometimes be done
+by instead reading from the input or similar.
 
 If you forward the input arguments directly to "fseek" or "lseek", note that
 the data type for \fIoffset\fP is not the same as defined for curl_off_t on
index 7ecb631..9c8cd40 100644 (file)
@@ -186,15 +186,21 @@ typedef size_t (*curl_write_callback)(char *buffer,
                                       size_t nitems,
                                       void *outstream);
 
+/* this is the return codes for the seek callbacks */
+#define CURL_SEEKFUNC_OK       0
+#define CURL_SEEKFUNC_FAIL     1 /* fail the entire transfer */
+#define CURL_SEEKFUNC_CANTSEEK 2 /* tell libcurl seeking can't be done, so
+                                    libcurl might try other means instead */
+typedef int (*curl_seek_callback)(void *instream,
+                                  curl_off_t offset,
+                                  int origin); /* 'whence' */
+
 /* This is a return code for the read callback that, when returned, will
    signal libcurl to immediately abort the current transfer. */
 #define CURL_READFUNC_ABORT 0x10000000
 /* This is a return code for the read callback that, when returned, will
    signal libcurl to pause sending data on the current transfer. */
 #define CURL_READFUNC_PAUSE 0x10000001
-typedef int (*curl_seek_callback)(void *instream,
-                                  curl_off_t offset,
-                                  int origin); /* 'whence' */
 
 typedef size_t (*curl_read_callback)(char *buffer,
                                       size_t size,
index c9991f1..ca14b97 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -1520,6 +1520,7 @@ static CURLcode ftp_state_ul_setup(struct connectdata *conn,
   struct FTP *ftp = conn->data->state.proto.ftp;
   struct SessionHandle *data = conn->data;
   struct ftp_conn *ftpc = &conn->proto.ftpc;
+  int seekerr = CURL_SEEKFUNC_OK;
 
   if((data->state.resume_from && !sizechecked) ||
      ((data->state.resume_from > 0) && sizechecked)) {
@@ -1548,38 +1549,39 @@ static CURLcode ftp_state_ul_setup(struct connectdata *conn,
 
     /* Let's read off the proper amount of bytes from the input. */
     if(conn->seek_func) {
-      curl_off_t readthisamountnow = data->state.resume_from;
+      seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
+                                SEEK_SET);
+    }
 
-      if(conn->seek_func(conn->seek_client,
-                         readthisamountnow, SEEK_SET) != 0) {
+    if(seekerr != CURL_SEEKFUNC_OK) {
+      if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
         failf(data, "Could not seek stream");
         return CURLE_FTP_COULDNT_USE_REST;
       }
+      /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
+      else {
+        curl_off_t passed=0;
+        do {
+          curl_off_t readthisamountnow = (data->state.resume_from - passed);
+          curl_off_t actuallyread;
+
+          if(readthisamountnow > BUFSIZE)
+            readthisamountnow = BUFSIZE;
+
+          actuallyread = (curl_off_t)
+            conn->fread_func(data->state.buffer, 1, (size_t)readthisamountnow,
+                             conn->fread_in);
+
+          passed += actuallyread;
+          if((actuallyread <= 0) || (actuallyread > readthisamountnow)) {
+            /* this checks for greater-than only to make sure that the
+               CURL_READFUNC_ABORT return code still aborts */
+            failf(data, "Failed to read data");
+            return CURLE_FTP_COULDNT_USE_REST;
+          }
+        } while(passed < data->state.resume_from);
+      }
     }
-
-    else {
-      curl_off_t passed=0;
-      do {
-        curl_off_t readthisamountnow = (data->state.resume_from - passed);
-        curl_off_t actuallyread;
-
-        if(readthisamountnow > BUFSIZE)
-          readthisamountnow = BUFSIZE;
-
-        actuallyread = (curl_off_t)
-          conn->fread_func(data->state.buffer, 1, (size_t)readthisamountnow,
-                      conn->fread_in);
-
-        passed += actuallyread;
-        if((actuallyread <= 0) || (actuallyread > readthisamountnow)) {
-          /* this checks for greater-than only to make sure that the
-             CURL_READFUNC_ABORT return code still aborts */
-          failf(data, "Failed to read data");
-          return CURLE_FTP_COULDNT_USE_REST;
-        }
-      } while(passed < data->state.resume_from);
-    }
-
     /* now, decrease the size of the read */
     if(data->set.infilesize>0) {
       data->set.infilesize -= data->state.resume_from;
index 2e3798f..c6ff5f5 100644 (file)
@@ -2054,7 +2054,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
   const char *httpstring;
   send_buffer *req_buffer;
   curl_off_t postsize; /* off_t type to be able to hold a large file size */
-
+  int seekerr = CURL_SEEKFUNC_OK;
 
   /* Always consider the DO phase done after this function call, even if there
      may be parts of the request that is not yet sent, since we can deal with
@@ -2335,36 +2335,40 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
       /* Now, let's read off the proper amount of bytes from the
          input. */
       if(conn->seek_func) {
-        curl_off_t readthisamountnow = data->state.resume_from;
+        seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
+                                  SEEK_SET);
+      }
 
-        if(conn->seek_func(conn->seek_client,
-                           readthisamountnow, SEEK_SET) != 0) {
+      if(seekerr != CURL_SEEKFUNC_OK) {
+        if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
           failf(data, "Could not seek stream");
           return CURLE_READ_ERROR;
         }
-      }
-      else {
-        curl_off_t passed=0;
-
-        do {
-          size_t readthisamountnow = (size_t)(data->state.resume_from - passed);
-          size_t actuallyread;
-
-          if(readthisamountnow > BUFSIZE)
-            readthisamountnow = BUFSIZE;
-
-          actuallyread = data->set.fread_func(data->state.buffer, 1,
-                                              (size_t)readthisamountnow,
-                                              data->set.in);
-
-          passed += actuallyread;
-          if(actuallyread != readthisamountnow) {
-            failf(data, "Could only read %" FORMAT_OFF_T
-                  " bytes from the input",
-                  passed);
-            return CURLE_READ_ERROR;
-          }
-        } while(passed != data->state.resume_from); /* loop until done */
+        /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
+        else {
+          curl_off_t passed=0;
+
+          do {
+            size_t readthisamountnow = (size_t)(data->state.resume_from -
+                                                passed);
+            size_t actuallyread;
+
+            if(readthisamountnow > BUFSIZE)
+              readthisamountnow = BUFSIZE;
+
+            actuallyread = data->set.fread_func(data->state.buffer, 1,
+                                                (size_t)readthisamountnow,
+                                                data->set.in);
+
+            passed += actuallyread;
+            if(actuallyread != readthisamountnow) {
+              failf(data, "Could only read %" FORMAT_OFF_T
+                    " bytes from the input",
+                    passed);
+              return CURLE_READ_ERROR;
+            }
+          } while(passed != data->state.resume_from); /* loop until done */
+        }
       }
 
       /* now, decrease the size of the read */
index 2de1cf8..d473d14 100644 (file)
--- a/lib/ssh.c
+++ b/lib/ssh.c
@@ -463,6 +463,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
   const char *host_public_key_md5;
   int rc = LIBSSH2_ERROR_NONE, i;
   int err;
+  int seekerr = CURL_SEEKFUNC_OK;
   *block = 0; /* we're not blocking by default */
 
   switch(sshc->state) {
@@ -1315,37 +1316,41 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
     if(data->state.resume_from > 0) {
       /* Let's read off the proper amount of bytes from the input. */
       if(conn->seek_func) {
-        curl_off_t readthisamountnow = data->state.resume_from;
+        seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
+                                  SEEK_SET);
+      }
+
+      if(seekerr != CURL_SEEKFUNC_OK){
 
-        if(conn->seek_func(conn->seek_client,
-                           readthisamountnow, SEEK_SET) != 0) {
+        if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
           failf(data, "Could not seek stream");
           return CURLE_FTP_COULDNT_USE_REST;
         }
-      }
-      else {
-        curl_off_t passed=0;
-        curl_off_t readthisamountnow;
-        curl_off_t actuallyread;
-        do {
-          readthisamountnow = (data->state.resume_from - passed);
-
-          if(readthisamountnow > BUFSIZE)
-            readthisamountnow = BUFSIZE;
-
-          actuallyread =
-            (curl_off_t) conn->fread_func(data->state.buffer, 1,
-                                          (size_t)readthisamountnow,
-                                          conn->fread_in);
-
-          passed += actuallyread;
-          if((actuallyread <= 0) || (actuallyread > readthisamountnow)) {
-            /* this checks for greater-than only to make sure that the
-               CURL_READFUNC_ABORT return code still aborts */
-             failf(data, "Failed to read data");
-            return CURLE_FTP_COULDNT_USE_REST;
-          }
-        } while(passed < data->state.resume_from);
+        /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
+        else {
+          curl_off_t passed=0;
+          curl_off_t readthisamountnow;
+          curl_off_t actuallyread;
+          do {
+            readthisamountnow = (data->state.resume_from - passed);
+
+            if(readthisamountnow > BUFSIZE)
+              readthisamountnow = BUFSIZE;
+
+            actuallyread =
+              (curl_off_t) conn->fread_func(data->state.buffer, 1,
+                                            (size_t)readthisamountnow,
+                                            conn->fread_in);
+
+            passed += actuallyread;
+            if((actuallyread <= 0) || (actuallyread > readthisamountnow)) {
+              /* this checks for greater-than only to make sure that the
+                 CURL_READFUNC_ABORT return code still aborts */
+              failf(data, "Failed to read data");
+              return CURLE_FTP_COULDNT_USE_REST;
+            }
+          } while(passed < data->state.resume_from);
+        }
       }
 
       /* now, decrease the size of the read */
index 06bfaed..1efa3fe 100644 (file)
@@ -3302,9 +3302,10 @@ static int my_seek(void *stream, curl_off_t offset, int whence)
   }
 #endif
   if(-1 == lseek(in->fd, offset, whence))
-    /* couldn't rewind, the reason is in errno but errno is just not
-       portable enough and we don't actually care that much why we failed. */
-    return 1;
+    /* couldn't rewind, the reason is in errno but errno is just not portable
+       enough and we don't actually care that much why we failed. We'll let
+       libcurl know that it may try other means if it wants to. */
+    return CURL_SEEKFUNC_CANTSEEK;
 
   return 0;
 }