curl / Mailing Lists / curl-library / Single Mail

curl-library

Unable to copy files to windows shared drive using libCurl and FILE protocol

From: Krishna Chaithanya B via curl-library <curl-library_at_cool.haxx.se>
Date: Thu, 3 Jan 2019 12:55:25 +0530

I am using the curl-7.62.0 built for VC17 X64 machine to develop a file
uploader utility. The file uploader uploads the file to a network drive.
The curl build directory has a .exe and a .dll files

I am able to run this command from windows shell and was able to upload the
file to the network drive successfully.

<path_to_curl>\bin\curl --upload-file C:\file.txt
file:////<ip_address>/ShareDrive/
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 1403 0 0 100 1403 0 87687 --:--:-- --:--:-- --:--:-- 87687

Now I want to achieve the same programmatically and I am writing my
component in C++. I have an upload function as below:

int Uploader::upload(const std::string& url) {
    CURLcode res;
    FILE *hd_src;
    struct stat file_info;
    curl_off_t fsize;

    if (stat(LOCAL_FILE, &file_info)) {
        printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
            return -1;
        }
        fsize = (curl_off_t)file_info.st_size;

        printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);

        hd_src = fopen(LOCAL_FILE, "rb");

        curl_global_init(CURL_GLOBAL_ALL);

        curl_ = curl_easy_init();
        if (curl_) {
        curl_easy_setopt(curl_, CURLOPT_UPLOAD, 1L);

            curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());

            curl_easy_setopt(curl_, CURLOPT_READDATA, hd_src);

            curl_easy_setopt(curl_, CURLOPT_READFUNCTION, read_callback);

            curl_easy_setopt(curl_, CURLOPT_INFILESIZE_LARGE,
                    (curl_off_t)fsize);

            res = curl_easy_perform(curl_);
        }

        fclose(hd_src);
        return 0;}

And my callback is in global scope:

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream){

    printf("Called...\n");

    size_t retcode = fread(ptr, size, nmemb, (FILE*)stream);
    printf("*** We read %lld bytes from file\n", retcode);
    return retcode;}

And my main looks like this:

int main(int argc, char** argv) {
    Uploader uploader;
    uploader.upload("file:////<ip_address>/ShareDrive/"); }

The control never goes to the callback method at all and hence the upload
never happens. Does CURL support the FILE Protocol ? I can see the examples
of FTPUpload, HTTPupload but there are no examples with the FILE protocol
on Windows.

Am I doing it right?

-- 
Regards,
Krishna Chaithanya B

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2019-01-03