I have been trying to create a function that returns true if a file exists
or not on a TFTP server. I had a function for this for SFTP/FTP using
CURLOPT_NOBODY, and that has been for those protocols. However, did not
work for TFTP. I have managed to make a function that checks if a file
exists on a TFTP server, I am just uncertain if this is the right way to go
about it.

size_t failIfWrite(char* buffer, size_t size, size_t nmemb, void* _) {
    return 0;
}
bool ServerClass::fileExistOnServer(CURL* curl, const std::string&
fileOnServer) const {
    bool exist = false;
    setGenericCurlData(curl, fileOnServer);
    if (m_protocol == "tftp") {
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, failIfWrite);
    }
    curl_easy_setopt(curl, CURLOPT_NOBODY, true);
    CURLcode res = curl_easy_perform(curl);
    if (res == CURLE_OK) {
        exist = true;
    }
    else if (res == CURLE_WRITE_ERROR && m_protocol == "tftp") {
        exist = true;
    }
    else if (res != CURLE_REMOTE_FILE_NOT_FOUND || res != CURLE_TFTP_NOTFOUND) {
        spdlog::doSpdLog(m_logger, spdlog::level::info, "could not
search for file due to '{}'", curl_easy_strerror(res));
    }
    return exist;
}

Note:
setGenericCurlData just set server address, username, ports, protocol etc..

It simply calls a Write error when curl starts to call my custom write
function, and if it does, I know there is data to be written, which means
that the file exists.
I do something very similar to get file size on TFTP, simply put, I use my
own progression call back, and abort that call back when I get a value in "
dltotal", and use that as the file size.

I am just wondering if there is an easier way to do this or if there is
some CURLOPT for this?


-- Tomas Berger
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html

Reply via email to