我正在编写一个小应用程序,该应用程序使用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,但是由于我的应用需要比较本地和远程文件的最后修改的时间戳,因此我还需要查看秒数,以便进行比较。 / p>
在使用Filezilla客户端进行测试时,我可以看到正确的时间戳记,因此我可以确定这不是服务器端的问题。
使用带有SFTP的libcurl发出LIST时,如何获得正确的时间戳?
答案 0 :(得分:1)
我不认为libcurl是适合此的库。
尝试libssh或libssh2:
libssh :呼叫sftp_opendir
,然后反复sftp_readdir
,最后是sftp_closedir
。
您将在sftp_attributes_struct
的mtime
字段中获得完整的时间戳。
libssh2 :呼叫libssh2_sftp_opendir
,然后反复libssh2_sftp_readdir
,最后是libssh2_sftp_closedir
。
您将在mtime
的{{1}}字段中获得完整的时间戳。
答案 1 :(得分:0)
通过获取文件列表,解析到相关的文件列表并分别获取每个文件的时间戳来解决。