using libcurl to check if a file exists on a SFTP site
Posted
by Snazzer
on Stack Overflow
See other posts from Stack Overflow
or by Snazzer
Published on 2010-03-19T14:15:30Z
Indexed on
2010/06/01
14:03 UTC
Read the original article
Hit count: 256
I'm using C++ with libcurl to do SFTP/FTPS transfers. Before uploading a file, I need to check if the file exists without actually downloading it.
If the file doesn't exist, I run into the following problems:
//set up curlhandle for the public/private keys and whatever else first.
curl_easy_setopt(CurlHandle, CURLOPT_URL, "sftp://user@pass:host/nonexistent-file");
curl_easy_setopt(CurlHandle, CURLOPT_NOBODY, 1);
curl_easy_setopt(CurlHandle, CURLOPT_FILETIME, 1);
int result = curl_easy_perform(CurlHandle);
//result is CURLE_OK, not CURLE_REMOTE_FILE_NOT_FOUND
//using curl_easy_getinfo to get the file time will return -1 for filetime, regardless
//if the file is there or not.
If I don't use CURLOPT_NOBODY, it works, I get CURLE_REMOTE_FILE_NOT_FOUND.
However, if the file does exist, it gets downloaded, which wastes time for me, since I just want to know if it's there or not.
Any other techniques/options I'm missing? Note that it should work for ftps as well.
Edit: This error occurs with sftp. With FTPS/FTP I get CURLE_FTP_COULDNT_RETR_FILE, which I can work with.
© Stack Overflow or respective owner