将 sftp 协议与 libcurl 结合使用 — 如何列出目录的内容?
我有一点疑问。我需要获取 SFTP 服务器上特定目录内的文件列表。我将使用 CUROPT_DIRLISTONLY 来获取名称,但我不确定如何获取它们。这是我现在拥有的和平代码:
string baseUrl(serverAddr + "/" + __destDir);
curl_easy_setopt(anEasyHandle, CURLOPT_URL, (baseUrl).c_str());
curl_easy_setopt(anEasyHandle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(anEasyHandle, CURLOPT_DIRLISTONLY, 1);
curl_easy_setopt(anEasyHandle, CURLOPT_QUOTE, commandList);
curl_easy_perform(anEasyHandle);
curl_easy_reset(anEasyHandle);
如果我是对的,curl_easy_perform只会返回一个ERROR_CODE(它适用的成功或错误),对吗?那么哪里可以获取文件列表呢?
如果我在终端上运行它:curl -u user:pass sftp://server/path/ -l 我得到我想要的列表....
任何帮助将不胜感激。
提前致谢, 乔治
I have a little doubt. I need to get a list of the files inside a specific directory on a SFTP server. I will use CUROPT_DIRLISTONLY in order to get just the names, but I'm not sure how to get them. This is the peace of code I have by now:
string baseUrl(serverAddr + "/" + __destDir);
curl_easy_setopt(anEasyHandle, CURLOPT_URL, (baseUrl).c_str());
curl_easy_setopt(anEasyHandle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(anEasyHandle, CURLOPT_DIRLISTONLY, 1);
curl_easy_setopt(anEasyHandle, CURLOPT_QUOTE, commandList);
curl_easy_perform(anEasyHandle);
curl_easy_reset(anEasyHandle);
If I'm right, curl_easy_perform just returns an ERROR_CODE (success or error ir it applies), correct? So where can I get the files list?
If I run it on a terminal: curl -u user:pass sftp://server/path/ -l I get the list I want....
Any help would be appreciated.
Thanks in advance,
George
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用以下签名创建一个“回调函数”:
然后调用 curl_easy_setopt 将回调函数设置为 CURLOPT_WRITEFUNCTION。将从远程服务器接收到的每个数据块都会调用您的函数,这些数据应该是某种形式的目录列表。
免责声明:我只阅读了关于此的文档 ,没试过。
You need to create a "callback function" with the following signature:
You then call curl_easy_setopt to set the callback function as your CURLOPT_WRITEFUNCTION. Your function will be called for each chunk of data recieved from the remote server, which should be some form of directory listing.
Disclaimer: I have only read the documentation on this, not tried it.