cURL / Mailing Lists / curl-users / Single Mail

curl-users

libcurl crashing at fwrite while downloading http file

From: Sukhdev Singh <sukhdev.hbti_at_gmail.com>
Date: Fri, 24 May 2013 10:51:23 -0700

I am using libcurl to dwonload file using http URL. File is very big, it
can be around 5 GB.

I tried two ways;-

1. Using libcurl's internal WRITEFUNCTION
2. Wrote my own WRITEFUNCTION

In both above cases my application crashes in the middle of download at
fwrite. Below is the BT. I am running on linuxplatform.

#0 0xffffe410 in __kernel_vsyscall ()
#1 0xf44f609b in write () from /lib/libc.so.6
#2 0xf4495e34 in _IO_new_file_write () from /lib/libc.so.6
#3 0xf4495af5 in new_do_write () from /lib/libc.so.6
#4 0xf4495ddf in _IO_new_do_write () from /lib/libc.so.6
#5 0xf44966ce in _IO_new_file_overflow () from /lib/libc.so.6
#6 0xf4495ccc in _IO_new_file_xsputn () from /lib/libc.so.6
#7 0xf448c28f in fwrite () from /lib/libc.so.6
#8 0xf7e37fab in writeData (ptr=0xf2a1ae30, size=1, nmemb=16384,
stream=0xf2a19390)
#9 0xf7051f34 in Curl_client_write () from
/opt/cisco/resource-mgr/sam/lib/libcurl.so.4
#10 0xf7062b6c in Curl_httpchunk_read () from
/opt/cisco/resource-mgr/sam/lib/libcurl.so.4
#11 0xf70603f9 in Curl_readwrite () from
/opt/cisco/resource-mgr/sam/lib/libcurl.so.4
#12 0xf7061b95 in Curl_perform () from
/opt/cisco/resource-mgr/sam/lib/libcurl.so.4
#13 0xf70620eb in curl_easy_perform () from
/opt/cisco/resource-mgr/sam/lib/libcurl.so.4
#14 0xf7e37e04 in HttpFileDownloader::download ()

Please find below code sample. Please let me know if I am doing anything
wrong. Any help will be highly appreciated.

writeData(void *ptr, size_t size, size_t nmemb, FILE *stream){
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

download(std::string srcUrl, std::string targetFile){
    CURL *curl = NULL;
    FILE *fp = NULL;
    curl = curl_easy_init();
    if (curl != NULL){
        fp = fopen(targetFile.c_str(), "wb");
        if (fp == NULL){
            LOG(LOG_INFO, "unable to open output file %s for http
download",
                    targetFile.c_str());
            return -1;
        }

        curl_easy_setopt(curl, CURLOPT_URL, srcUrl.c_str());
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)fp);
        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
        fp = NULL;

        LOG(LOG_INFO, "curl http download return with code %d", res);
        if (res == CURLE_WRITE_ERROR && isAborted == true){
            //download aborted
            LOG(LOG_INFO, "curl http file download aborted");
            return -1;
        }else if(res != CURLE_OK){
            //error in download
            LOG(LOG_INFO, "curl error in http file download");
            return -1;
        }
    }else{
        return -1;
    }
    return 0;
}

Cheers

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-05-24