将 sftp 协议与 libcurl 结合使用 — 如何列出目录的内容?

发布于 2024-08-28 00:53:04 字数 680 浏览 5 评论 0原文

我有一点疑问。我需要获取 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 技术交流群。

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

发布评论

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

评论(1

南冥有猫 2024-09-04 00:53:04

您需要使用以下签名创建一个“回调函数”:

size_t function( void *ptr, size_t size, size_t nmemb, void *stream);

然后调用 curl_easy_setopt 将回调函数设置为 CURLOPT_WRITEFUNCTION。将从远程服务器接收到的每个数据块都会调用您的函数,这些数据应该是某种形式的目录列表。

免责声明:我只阅读了关于此的文档 ,没试过。

You need to create a "callback function" with the following signature:

size_t function( void *ptr, size_t size, size_t nmemb, void *stream);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文