使用 libcurl 检查 SFTP 站点上是否存在文件
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 libcurl 7.38.0 中对此进行了测试
但是,SFTP 的 CURLOPT_NOBODY 和 CURLOPT_HEADER 不会返回错误
如果 libcurl 的某些早期版本中不存在该文件。解决此问题的替代解决方案:
Tested this in libcurl 7.38.0
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:
我找到了一种方法来完成这项工作。基本概念是尝试读取文件,如果文件存在则中止读取操作,以避免下载整个文件。因此,它要么从 cURL 收到“文件不存在”返回的错误,要么“写入数据时出错”:
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":