libcurl console progress bar for file download

2019-04-15 04:52发布

问题:

Here I implemented code for file download from server. its working fine. Now I want to make my own progress bar function which calculates some data like remaining seconds data Rate per second etc. So from here I found one way to use curl progress bar option. how we can enable this option. I completely done with this.

I put my code below. here in this code my_progress_func calls frequently as per curl library time interval. I want to change this interval time and make it to 1 second. is it possible in curl library using to set some options for curl library?

I want to call this my_progress_func function after every 1 second.

Code :

#include <stdio.h>
#include <curl/curl.h>

long test =0;

struct FtpFile {
  const char *filename;
  FILE *stream;
  long iAppend;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
      out->stream=fopen(out->filename, out->iAppend ? "ab":"wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  out->iAppend += nmemb;
  return fwrite(buffer, size, nmemb, out->stream);
}

int my_progress_func(void *bar,
                     double t, /* dltotal */ 
                     double d, /* dlnow */ 
                     double ultotal,
                     double ulnow)
{
    printf("%f : %f \n", d, t);

  return 0;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  int c;

  struct FtpFile ftpfile={
    "dev.zip", /* name to store the file as if succesful */
    NULL,
  };

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
                     "sftp://root:xyz_@192.170.10.1/mnt/xyz.tar");


    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L);


    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);


    curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");

    /* Switch on full protocol/debug output */

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);

    res = curl_easy_perform(curl);

    printf("res is %d\n, data get %ld\n", res, ftpfile.iAppend);


    ///Retry upto 100 times it timeout or connection drop occur
    for (c = 0; (res != CURLE_OK) && (c < 100); c++) {



        curl_easy_setopt(curl, CURLOPT_RESUME_FROM , ftpfile.iAppend);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK) c =0;
        printf("%d res is %d\n, data get %ld\n",c, res, ftpfile.iAppend);

    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */
  curl_global_cleanup();
   return 0;
}

回答1:

According to the curl documentation: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

Function pointer that should match the curl_progress_callback prototype found in . This function gets called by libcurl instead of its internal equivalent with a frequent interval during operation (roughly once per second or sooner) no matter if data is being transfered or not. Unknown/unused argument values passed to the callback will be set to zero (like if you only download data, the upload size will remain 0). Returning a non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.

If it's calling too frequently then you can use time() and a static var to limit this, something like this:

static time_t prevtime;
time_t currtime;
double dif;
static int first = 1;
if(first) {
    time(&prevtime);
    first = 0;
}
time(&currtime);
dif = difftime(currtime, prevtime);
if(dif < 1.0)
    return;
prevtime = currtime;

Obviously, you run the risk that curl might not call this function again for fully another second.



标签: c libcurl