使用 libcurl 检查 SFTP 站点上是否存在文件

发布于 2024-08-26 08:02:49 字数 837 浏览 9 评论 0原文

我使用 C++ 和 libcurl 进行 SFTP/FTPS 传输。在上传文件之前,我需要检查文件是否存在而不实际下载它。

如果该文件不存在,我会遇到以下问题:

//set up curlhandle for the public/private keys and whatever else first.
curl_easy_setopt(CurlHandle, CURLOPT_URL, "sftp://user@pass:host/nonexistent-file");
curl_easy_setopt(CurlHandle, CURLOPT_NOBODY, 1);
curl_easy_setopt(CurlHandle, CURLOPT_FILETIME, 1);
int result = curl_easy_perform(CurlHandle); 
//result is CURLE_OK, not CURLE_REMOTE_FILE_NOT_FOUND
//using curl_easy_getinfo to get the file time will return -1 for filetime, regardless
//if the file is there or not.

如果我不使用 CURLOPT_NOBODY,它会起作用,我会得到 CURLE_REMOTE_FILE_NOT_FOUND。

但是,如果该文件确实存在,则会下载它,这对我来说浪费时间,因为我只想知道它是否存在。

我还缺少其他技术/选项吗?请注意,它也适用于 ftps。


编辑: sftp 会发生此错误。通过 FTPS/FTP,我得到了 CURLE_FTP_COULDNT_RETR_FILE,我可以使用它。

I'm using C++ with libcurl to do SFTP/FTPS transfers. Before uploading a file, I need to check if the file exists without actually downloading it.

If the file doesn't exist, I run into the following problems:

//set up curlhandle for the public/private keys and whatever else first.
curl_easy_setopt(CurlHandle, CURLOPT_URL, "sftp://user@pass:host/nonexistent-file");
curl_easy_setopt(CurlHandle, CURLOPT_NOBODY, 1);
curl_easy_setopt(CurlHandle, CURLOPT_FILETIME, 1);
int result = curl_easy_perform(CurlHandle); 
//result is CURLE_OK, not CURLE_REMOTE_FILE_NOT_FOUND
//using curl_easy_getinfo to get the file time will return -1 for filetime, regardless
//if the file is there or not.

If I don't use CURLOPT_NOBODY, it works, I get CURLE_REMOTE_FILE_NOT_FOUND.

However, if the file does exist, it gets downloaded, which wastes time for me, since I just want to know if it's there or not.

Any other techniques/options I'm missing? Note that it should work for ftps as well.


Edit: This error occurs with sftp. With FTPS/FTP I get CURLE_FTP_COULDNT_RETR_FILE, which I can work with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-09-02 08:02:50

在 libcurl 7.38.0 中对此进行了测试

curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);

CURLcode iRc = curl_easy_perform(curl);

if (iRc == CURLE_REMOTE_FILE_NOT_FOUND)
  // File doesn't exist
else if (iRc == CURLE_OK)
  // File exists

但是,SFTP 的 CURLOPT_NOBODY 和 CURLOPT_HEADER 不会返回错误
如果 libcurl 的某些早期版本中不存在该文件。解决此问题的替代解决方案:

// Ask for the first byte
curl_easy_setopt(curl, CURLOPT_RANGE,
    (const char *)"0-0");

CURLcode iRc = curl_easy_perform(curl);

if (iRc == CURLE_REMOTE_FILE_NOT_FOUND)
  // File doesn't exist
else if (iRc == CURLE_OK || iRc == CURLE_BAD_DOWNLOAD_RESUME)
  // File exists

Tested this in libcurl 7.38.0

curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);

CURLcode iRc = curl_easy_perform(curl);

if (iRc == CURLE_REMOTE_FILE_NOT_FOUND)
  // File doesn't exist
else if (iRc == CURLE_OK)
  // File exists

However, CURLOPT_NOBODY and CURLOPT_HEADER for SFTP doesn't return an error
if file doesn't exist in some previous versions of libcurl. An alternative solution to resolve this:

// Ask for the first byte
curl_easy_setopt(curl, CURLOPT_RANGE,
    (const char *)"0-0");

CURLcode iRc = curl_easy_perform(curl);

if (iRc == CURLE_REMOTE_FILE_NOT_FOUND)
  // File doesn't exist
else if (iRc == CURLE_OK || iRc == CURLE_BAD_DOWNLOAD_RESUME)
  // File exists
ゃ懵逼小萝莉 2024-09-02 08:02:50

我找到了一种方法来完成这项工作。基本概念是尝试读取文件,如果文件存在则中止读取操作,以避免下载整个文件。因此,它要么从 cURL 收到“文件不存在”返回的错误,要么“写入数据时出错”:

static size_t abort_read(void *ptr, size_t size, size_t nmemb, void *data)
{
  (void)ptr;
  (void)data;
  /* we are not interested in the data itself,
     so we abort operation ... */ 
  return (size_t)(-1); // produces CURLE_WRITE_ERROR
}
....
curl_easy_setopt(curl,CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, abort_read);
CURLcode res = curl_easy_perform(curl);
/* restore options */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_URL, NULL);
return (res==CURLE_WRITE_ERROR);

I found a way to make this work. The basic concept is to attempt to read the file, then abort the read operation if the file exists, to avoid downloading the whole file. So it will either get a returned error from cURL for "file doesn't exist", or "error writing data":

static size_t abort_read(void *ptr, size_t size, size_t nmemb, void *data)
{
  (void)ptr;
  (void)data;
  /* we are not interested in the data itself,
     so we abort operation ... */ 
  return (size_t)(-1); // produces CURLE_WRITE_ERROR
}
....
curl_easy_setopt(curl,CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, abort_read);
CURLcode res = curl_easy_perform(curl);
/* restore options */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_URL, NULL);
return (res==CURLE_WRITE_ERROR);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文