如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表

How to get the file list with from a remote SFTP server in HH-MM-SS timestamp format

本文关键字:时间戳 HH-MM-SS 格式 列表 文件 获取 服务器 SFTP      更新时间:2023-10-16

我正在编写一个小应用程序,它使用 Curl 从远程 sftp 服务器读取文件列表。在获取文件列表并解析它时,这不是问题:

curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, Callback);
curl_easy_setopt(m_pCurlSession, CURLOPT_URL, "sftp://xxxx.xx:22/");
curl_easy_setopt(m_pCurlSession, CURLOPT_USERNAME, "xxxx");
curl_easy_setopt(m_pCurlSession, CURLOPT_PASSWORD, "xxxx");
curl_easy_setopt(m_pCurlSession, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
curl_easy_setopt(m_pCurlSession, CURLOPT_VERBOSE, 1);
curl_easy_perform(m_pCurlSession);

我得到的输出是这样的

drwxrwxr-x    3 root     root       4096 Jun 23 16:21 .
drwxr-xr-x    4 root     root       4096 Jun 15 08:59 ..
drwxrwxr-x    2 user     user      4096 Jun 23 17:11 IMAGES
-rw-rw-r--    1 user     user    152069 Jun 23 16:20 test.xml

如您所见,上次修改文件的时间戳是 HH:MM,但由于我的应用程序需要比较本地和远程文件的上次修改时间,我还需要查看秒数,以便进行比较。

使用 FileZilla 客户端进行测试时,我可以看到正确的时间戳,所以我很确定这不是服务器端问题。

在将 libcurl 与 SFTP 一起使用时LIST,如何获得正确的时间戳?

我不认为libcurl是很好的库。

尝试 libssh 或 libssh2:

  • libssh:调用sftp_opendir,然后反复sftp_readdir,最后sftp_closedir

    您将在sftp_attributes_structmtime字段中获得完整的时间戳。

  • libssh2:调用libssh2_sftp_opendir,然后反复libssh2_sftp_readdir,最后libssh2_sftp_closedir

    您将在mtime字段中获得完整的时间戳LIBSSH2_SFTP_ATTRIBUTES.

通过获取文件列表、解析到相关文件列表并分别获取每个文件时间戳来解决。