我正在编写一个小应用程序,它使用 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
。
您将在
mtime
的
sftp_attributes_struct
字段中获得完整时间戳。
libssh2_sftp_opendir
,然后重复libssh2_sftp_readdir
,最后libssh2_sftp_closedir
。
您将在
mtime
的 LIBSSH2_SFTP_ATTRIBUTES
字段中获得完整时间戳。
通过获取文件列表,解析到相关文件列表并分别获取每个文件时间戳来解决。