libcurl sftp 文件上传恢复问题

libcurl sftp file upload resume issue

我正在尝试开发一个简单的 PoC(概念验证)来执行支持恢复部分传输文件的 sftp 文件上传。

环境:

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

我以代码示例作为起点:https://curl.haxx.se/libcurl/c/ftpuploadresume.html 并将其修改为:

  1. 使用协议字符串 sftp
  2. 用于重试的 for() 循环中的强制恢复分支 (c=1)

我正在验证的行为是,一旦连接完成:

  1. 远程文件大小在第一个 curl_perform() 调用后被截断为零(据我所知, 应该检索远程文件大小 代替)
  2. 回调 getcontentlengfunc() 从未被调用

我也对评论有点困惑:

  /*
   * 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);

假设评论是正确的,我希望将选项 CURLOPT_HEADER 设置为 0L

我也试过了,但行为与上述相同。

更新

刚在github上发现上面的行为是设计的,因为SFTP协议不支持HEADER概念,所以我的问题是,如何使用libcurl检索远程文件大小?

经过多次阅读和与 curl 维护者的互动后,我解决了这个问题。 一个完整的工作示例已贡献给 curl 项目并被善意接受。 详情在这里:https://curl.haxx.se/libcurl/c/sftpuploadresume.html