Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libcurl sftp file upload resume issue

Tags:

c

sftp

libcurl

I'm trying to develop a simple PoC (Proof of Concept) to perform an sftp file upload that supports resume of partially transferred files.

Environemnt:

GNU/Linux Ubuntu 16.04 64b
libcurl 7.58.0 built with libssh2 support
gcc 5.4.0

I took as a starting point the code example: https://curl.haxx.se/libcurl/c/ftpuploadresume.html and modified it to:

  1. use the protocol string sftp
  2. force resume branch (c=1) in the for() loop used to do the re-tries

The behavior I'm verifying is that once the connection is done:

  1. the remote file size is truncated to zero after the first curl_perform() call (that I understand is supposed to retrieve the remote file size instead)
  2. callback getcontentlengthfunc() is never called

I'm a bit confused by a comment as well:

  /*
   * With NOBODY and NOHEADER, libcurl will issue a SIZE
   * command, but the only way to retrieve the result is
   * to parse the returned Content-Length header. Thus,
   * getcontentlengthfunc(). We need discardfunc() above
   * because HEADER will dump the headers to stdout
   * without it.
   */
  curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
  curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);

assuming the comment is correct, I would expect the option CURLOPT_HEADER to be set to 0L instead.

I tried it as well but the behavior is the same described above.

Update

Just discovered on github that the above behavior is by design, because the SFTP protocol does not support the HEADER concept, so my question is, how can I use libcurl to retrieve the remote file size?

like image 574
sergico Avatar asked Feb 01 '26 04:02

sergico


1 Answers

After several reading and interaction with curl mainteiner I solved the issue. A fully working example has been contributed to the curl project and kindly accepted. Here the details: https://curl.haxx.se/libcurl/c/sftpuploadresume.html

like image 96
sergico Avatar answered Feb 03 '26 09:02

sergico