curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

TFTP get filesize/file exist

From: Tomas Berger via curl-library <>
Date: Wed, 24 Mar 2021 15:45:02 +0100

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;

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

Received on 2021-03-24