curl / Mailing Lists / curl-library / Single Mail

curl-library

SMB: unable to upload file to a subfolder: remote file not found

From: Ivan Pilipenko <ivan.pilipenko_at_matrix-vision.de>
Date: Fri, 18 May 2018 13:34:44 +0200 (CEST)

Greetings,

I have an implementation of a tool that uses libcurl 7.60.0 to upload report files to an FTP server, creating subdirectories as needed. So far this has been working fine, but now we also got a request to do the same using SMB.

The implementation I got so far manages to upload a file to a share's root folder, i.e. "smb://myserver/share/test.txt", but fails when attempting to upload the file to an _existing_ subfolder, i.e. "smb://myserver/share/testfolder/test.txt".

This is my code so far (relevant excerpt):

// reset all parameters
curl_easy_reset(curl);

// set username and password
string userPwd = user + ":" + password;
setCurlOption(CURLOPT_USERPWD, userPwd);
setCurlOption(CURLOPT_NOPROGRESS, 1);

// we want to use our own read function
setCurlOption(CurlOption(CURLOPT_READFUNCTION, readCallbackFunction));

// enable uploading
setCurlOption(CurlOption(CURLOPT_UPLOAD, 1L));

// iterate over all NamedDataElements
for(const auto& namedData : namedDataElements) {
    // create pair<NamedData*, uint32> with read position for this NamedData
    if(namedData == nullptr) {
        continue;
    }

    pair<NamedData*, uint32> namedDataReadPos = make_pair(namedData.get(), 0);

    // now specify which namedData to upload
    setCurlOption(CurlOption(CURLOPT_READDATA, &namedDataReadPos));

    // set the size of the namedData
    curl_off_t dataLength = static_cast<curl_off_t>(namedData->getLength());
    setCurlOption(CurlOption(CURLOPT_INFILESIZE_LARGE, dataLength));

    string filename = "smb://server/share/testfolder/test.txt";
    setCurlOption(CurlOption(CURLOPT_URL, filename));

    // provide a buffer to store errors in and set the error buffer to empty before performing a request
    std::fill_n(errorBuffer, CURL_ERROR_SIZE + 1, '\0');
    try {
        setCurlOption(CURLOPT_ERRORBUFFER, errorBuffer);
    }
    catch(const MvException&) {
        if(doThrow) {
            // rethrow
            throw;
        }
    }

    auto result = curl_easy_perform(curl);
    if(CURLE_OK != result && doThrow == true) {
        throwCurlStatus(result, __FUNCTION__, __LINE__, errorBuffer);
    }
}

Executing results in "CURL error: Remote file not found", errorBuffer is empty. I am not quite sure why this error message is returned by libcurl. Obviously, the remote file might not exist when I'm attempting to upload one?

Any help is appreciated.

Regards,
Ivan
-----------------------------------------------------------------
Upcoming Events:
19.-22. June: AUTOMATICA, Munich, Germany
06.-08. November: VISION, Stuttgart, Germany
-----------------------------------------------------------------

MATRIX VISION GmbH, Talstrasse 16, DE-71570 Oppenweiler
Registergericht: Amtsgericht Stuttgart, HRB 271090
Geschaeftsfuehrer: Uwe Furtner
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2018-05-18