cURL / Mailing Lists / curl-library / Single Mail

curl-library

Problem with CURLOPT_RESUME_FROM and CURLOPT_APPEND

From: Daniele Pianu <muogoro_at_gmail.com>
Date: Wed, 27 Aug 2008 18:43:33 +0200

Hi to all,
I'm working to a library that simulates Unix I/O read/write/seek operations
using different streams/protocols. The plugin which uses FTP protocol is
built using libcURL. While I'm doing consecutive writes there's no problems:
I set the CURLOPT_APPEND and all data is appended beyond the EOF. I've some
problems when I seek back and want to overwrite some data inside the file.
My writing function code is something like this:

  /* Setup curl library for upload */
  curl_easy_setopt( handle, CURLOPT_UPLOAD, 1L );

  /* If file size is equal to file offset and this isn't the first write
    (channelSize != 0), append data to the end of file .....*/
  if( channelSize )
  {
    if( channelPos == channelSize )
    {
      curl_easy_setopt( handle, CURLOPT_APPEND, 1L );
    }
    else
    {
      /* ... else, set resume */
      curl_easy_setopt( handle, CURLOPT_RESUME_FROM,
                                     channelPos );
    }

    /* Check if the buffer write need to write data beyond the EOF */
    if( channelPos + size > channelSize )
    {
      /* Write up to channelSize - curl->channelPos bytes */
      retVal = Curl_write( libraryInstance, buffer,
                                         channelSize - channelPos );
      /* Append remaining bytes */
      curl_easy_setopt( handle, CURLOPT_RESUME_FROM, 0L );
      curl_easy_setopt( handle, CURLOPT_APPEND, 1L );
      retVal = Curl_write( libraryInstance, (unsigned char*)buffer +
retVal,
                                         size - retVal );
    }
    else
    {
      /* overwrite data inside the file */
      Curl_write( libraryInstance, buffer, size );
    }

  }
  else
  {
    /* First write */
    Curl_write( libraryInstance, buffer, size );
  }

  /* Reset curl library options for next transfers */
 curl_easy_setopt( handle, CURLOPT_UPLOAD, 0L );
 curl_easy_setopt( handle, CURLOPT_APPEND, 0L );
 curl_easy_setopt( handle, CURLOPT_RESUME_FROM, 0L );

  return retVal;

channelSize is the size of the file, channelPos is the offset.
libraryInstance is the library instance, Curl_write is the plugin write
function, which use my own defined callbacks. A callback that prevents seek
operation in the input stream is also provided, so resume operations move
the offset only inside the ftp server file ( that is, transfer starts ALWAYS
from the begin of the file ). Supposing I've this file:

foo!!!
foobar!!!

and I seek back to 7-th byte (the second line), if I want to write the
string "bar!!!\n", I've this output file in the server

foo!!!
foobar!!!
bar!!!

and not this

foo!!!
bar!!!
bar!!!

seek ( and, so, resume) operations are ignored: if I enable verbose mode I
see the every write operation is resolved to a APPEND command (except for
the first write).

Every help is appreciated,
Daniele

P.S.: excuse me for my bad english :)
Received on 2008-08-27