Home | History | Annotate | Download | only in lib
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at http://curl.haxx.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 
     23 #include "curl_setup.h"
     24 
     25 #include "urldata.h"
     26 #include "sendf.h"
     27 #include "progress.h"
     28 #include "curl_printf.h"
     29 
     30 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
     31    byte) */
     32 static void time2str(char *r, curl_off_t seconds)
     33 {
     34   curl_off_t d, h, m, s;
     35   if(seconds <= 0) {
     36     strcpy(r, "--:--:--");
     37     return;
     38   }
     39   h = seconds / CURL_OFF_T_C(3600);
     40   if(h <= CURL_OFF_T_C(99)) {
     41     m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
     42     s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
     43     snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
     44              ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
     45   }
     46   else {
     47     /* this equals to more than 99 hours, switch to a more suitable output
     48        format to fit within the limits. */
     49     d = seconds / CURL_OFF_T_C(86400);
     50     h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
     51     if(d <= CURL_OFF_T_C(999))
     52       snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
     53                "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
     54     else
     55       snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
     56   }
     57 }
     58 
     59 /* The point of this function would be to return a string of the input data,
     60    but never longer than 5 columns (+ one zero byte).
     61    Add suffix k, M, G when suitable... */
     62 static char *max5data(curl_off_t bytes, char *max5)
     63 {
     64 #define ONE_KILOBYTE  CURL_OFF_T_C(1024)
     65 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
     66 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
     67 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
     68 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
     69 
     70   if(bytes < CURL_OFF_T_C(100000))
     71     snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
     72 
     73   else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
     74     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
     75 
     76   else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
     77     /* 'XX.XM' is good as long as we're less than 100 megs */
     78     snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
     79              CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
     80              (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
     81 
     82 #if (CURL_SIZEOF_CURL_OFF_T > 4)
     83 
     84   else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
     85     /* 'XXXXM' is good until we're at 10000MB or above */
     86     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
     87 
     88   else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
     89     /* 10000 MB - 100 GB, we show it as XX.XG */
     90     snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
     91              CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
     92              (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
     93 
     94   else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
     95     /* up to 10000GB, display without decimal: XXXXG */
     96     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
     97 
     98   else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
     99     /* up to 10000TB, display without decimal: XXXXT */
    100     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
    101 
    102   else
    103     /* up to 10000PB, display without decimal: XXXXP */
    104     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
    105 
    106     /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
    107        can hold, but our data type is signed so 8192PB will be the maximum. */
    108 
    109 #else
    110 
    111   else
    112     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
    113 
    114 #endif
    115 
    116   return max5;
    117 }
    118 
    119 /*
    120 
    121    New proposed interface, 9th of February 2000:
    122 
    123    pgrsStartNow() - sets start time
    124    pgrsSetDownloadSize(x) - known expected download size
    125    pgrsSetUploadSize(x) - known expected upload size
    126    pgrsSetDownloadCounter() - amount of data currently downloaded
    127    pgrsSetUploadCounter() - amount of data currently uploaded
    128    pgrsUpdate() - show progress
    129    pgrsDone() - transfer complete
    130 
    131 */
    132 
    133 int Curl_pgrsDone(struct connectdata *conn)
    134 {
    135   int rc;
    136   struct SessionHandle *data = conn->data;
    137   data->progress.lastshow=0;
    138   rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
    139   if(rc)
    140     return rc;
    141 
    142   if(!(data->progress.flags & PGRS_HIDE) &&
    143      !data->progress.callback)
    144     /* only output if we don't use a progress callback and we're not
    145      * hidden */
    146     fprintf(data->set.err, "\n");
    147 
    148   data->progress.speeder_c = 0; /* reset the progress meter display */
    149   return 0;
    150 }
    151 
    152 /* reset all times except redirect, and reset the known transfer sizes */
    153 void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
    154 {
    155   data->progress.t_nslookup = 0.0;
    156   data->progress.t_connect = 0.0;
    157   data->progress.t_pretransfer = 0.0;
    158   data->progress.t_starttransfer = 0.0;
    159 
    160   Curl_pgrsSetDownloadSize(data, -1);
    161   Curl_pgrsSetUploadSize(data, -1);
    162 }
    163 
    164 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
    165 {
    166   struct timeval now = Curl_tvnow();
    167 
    168   switch(timer) {
    169   default:
    170   case TIMER_NONE:
    171     /* mistake filter */
    172     break;
    173   case TIMER_STARTOP:
    174     /* This is set at the start of a transfer */
    175     data->progress.t_startop = now;
    176     break;
    177   case TIMER_STARTSINGLE:
    178     /* This is set at the start of each single fetch */
    179     data->progress.t_startsingle = now;
    180     break;
    181 
    182   case TIMER_STARTACCEPT:
    183     data->progress.t_acceptdata = Curl_tvnow();
    184     break;
    185 
    186   case TIMER_NAMELOOKUP:
    187     data->progress.t_nslookup =
    188       Curl_tvdiff_secs(now, data->progress.t_startsingle);
    189     break;
    190   case TIMER_CONNECT:
    191     data->progress.t_connect =
    192       Curl_tvdiff_secs(now, data->progress.t_startsingle);
    193     break;
    194   case TIMER_APPCONNECT:
    195     data->progress.t_appconnect =
    196       Curl_tvdiff_secs(now, data->progress.t_startsingle);
    197     break;
    198   case TIMER_PRETRANSFER:
    199     data->progress.t_pretransfer =
    200       Curl_tvdiff_secs(now, data->progress.t_startsingle);
    201     break;
    202   case TIMER_STARTTRANSFER:
    203     data->progress.t_starttransfer =
    204       Curl_tvdiff_secs(now, data->progress.t_startsingle);
    205     break;
    206   case TIMER_POSTRANSFER:
    207     /* this is the normal end-of-transfer thing */
    208     break;
    209   case TIMER_REDIRECT:
    210     data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
    211     break;
    212   }
    213 }
    214 
    215 void Curl_pgrsStartNow(struct SessionHandle *data)
    216 {
    217   data->progress.speeder_c = 0; /* reset the progress meter display */
    218   data->progress.start = Curl_tvnow();
    219   /* clear all bits except HIDE and HEADERS_OUT */
    220   data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
    221 }
    222 
    223 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
    224 {
    225   data->progress.downloaded = size;
    226 }
    227 
    228 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
    229 {
    230   data->progress.uploaded = size;
    231 }
    232 
    233 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
    234 {
    235   if(size >= 0) {
    236     data->progress.size_dl = size;
    237     data->progress.flags |= PGRS_DL_SIZE_KNOWN;
    238   }
    239   else {
    240     data->progress.size_dl = 0;
    241     data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
    242   }
    243 }
    244 
    245 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
    246 {
    247   if(size >= 0) {
    248     data->progress.size_ul = size;
    249     data->progress.flags |= PGRS_UL_SIZE_KNOWN;
    250   }
    251   else {
    252     data->progress.size_ul = 0;
    253     data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
    254   }
    255 }
    256 
    257 /*
    258  * Curl_pgrsUpdate() returns 0 for success or the value returned by the
    259  * progress callback!
    260  */
    261 int Curl_pgrsUpdate(struct connectdata *conn)
    262 {
    263   struct timeval now;
    264   int result;
    265   char max5[6][10];
    266   curl_off_t dlpercen=0;
    267   curl_off_t ulpercen=0;
    268   curl_off_t total_percen=0;
    269   curl_off_t total_transfer;
    270   curl_off_t total_expected_transfer;
    271   curl_off_t timespent;
    272   struct SessionHandle *data = conn->data;
    273   int nowindex = data->progress.speeder_c% CURR_TIME;
    274   int checkindex;
    275   int countindex; /* amount of seconds stored in the speeder array */
    276   char time_left[10];
    277   char time_total[10];
    278   char time_spent[10];
    279   curl_off_t ulestimate=0;
    280   curl_off_t dlestimate=0;
    281   curl_off_t total_estimate;
    282   bool shownow=FALSE;
    283 
    284   now = Curl_tvnow(); /* what time is it */
    285 
    286   /* The time spent so far (from the start) */
    287   data->progress.timespent =
    288     (double)(now.tv_sec - data->progress.start.tv_sec) +
    289     (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
    290   timespent = (curl_off_t)data->progress.timespent;
    291 
    292   /* The average download speed this far */
    293   data->progress.dlspeed = (curl_off_t)
    294     ((double)data->progress.downloaded/
    295      (data->progress.timespent>0?data->progress.timespent:1));
    296 
    297   /* The average upload speed this far */
    298   data->progress.ulspeed = (curl_off_t)
    299     ((double)data->progress.uploaded/
    300      (data->progress.timespent>0?data->progress.timespent:1));
    301 
    302   /* Calculations done at most once a second, unless end is reached */
    303   if(data->progress.lastshow != (long)now.tv_sec) {
    304     shownow = TRUE;
    305 
    306     data->progress.lastshow = now.tv_sec;
    307 
    308     /* Let's do the "current speed" thing, which should use the fastest
    309        of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
    310     data->progress.speeder[ nowindex ] =
    311       data->progress.downloaded>data->progress.uploaded?
    312       data->progress.downloaded:data->progress.uploaded;
    313 
    314     /* remember the exact time for this moment */
    315     data->progress.speeder_time [ nowindex ] = now;
    316 
    317     /* advance our speeder_c counter, which is increased every time we get
    318        here and we expect it to never wrap as 2^32 is a lot of seconds! */
    319     data->progress.speeder_c++;
    320 
    321     /* figure out how many index entries of data we have stored in our speeder
    322        array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
    323        transfer. Imagine, after one second we have filled in two entries,
    324        after two seconds we've filled in three entries etc. */
    325     countindex = ((data->progress.speeder_c>=CURR_TIME)?
    326                   CURR_TIME:data->progress.speeder_c) - 1;
    327 
    328     /* first of all, we don't do this if there's no counted seconds yet */
    329     if(countindex) {
    330       long span_ms;
    331 
    332       /* Get the index position to compare with the 'nowindex' position.
    333          Get the oldest entry possible. While we have less than CURR_TIME
    334          entries, the first entry will remain the oldest. */
    335       checkindex = (data->progress.speeder_c>=CURR_TIME)?
    336         data->progress.speeder_c%CURR_TIME:0;
    337 
    338       /* Figure out the exact time for the time span */
    339       span_ms = Curl_tvdiff(now,
    340                             data->progress.speeder_time[checkindex]);
    341       if(0 == span_ms)
    342         span_ms=1; /* at least one millisecond MUST have passed */
    343 
    344       /* Calculate the average speed the last 'span_ms' milliseconds */
    345       {
    346         curl_off_t amount = data->progress.speeder[nowindex]-
    347           data->progress.speeder[checkindex];
    348 
    349         if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
    350           /* the 'amount' value is bigger than would fit in 32 bits if
    351              multiplied with 1000, so we use the double math for this */
    352           data->progress.current_speed = (curl_off_t)
    353             ((double)amount/((double)span_ms/1000.0));
    354         else
    355           /* the 'amount' value is small enough to fit within 32 bits even
    356              when multiplied with 1000 */
    357           data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
    358       }
    359     }
    360     else
    361       /* the first second we use the main average */
    362       data->progress.current_speed =
    363         (data->progress.ulspeed>data->progress.dlspeed)?
    364         data->progress.ulspeed:data->progress.dlspeed;
    365 
    366   } /* Calculations end */
    367 
    368   if(!(data->progress.flags & PGRS_HIDE)) {
    369     /* progress meter has not been shut off */
    370 
    371     if(data->set.fxferinfo) {
    372       /* There's a callback set, call that */
    373       result= data->set.fxferinfo(data->set.progress_client,
    374                                   data->progress.size_dl,
    375                                   data->progress.downloaded,
    376                                   data->progress.size_ul,
    377                                   data->progress.uploaded);
    378       if(result)
    379         failf(data, "Callback aborted");
    380       return result;
    381     }
    382     else if(data->set.fprogress) {
    383       /* The older deprecated callback is set, call that */
    384       result= data->set.fprogress(data->set.progress_client,
    385                                   (double)data->progress.size_dl,
    386                                   (double)data->progress.downloaded,
    387                                   (double)data->progress.size_ul,
    388                                   (double)data->progress.uploaded);
    389       if(result)
    390         failf(data, "Callback aborted");
    391       return result;
    392     }
    393 
    394     if(!shownow)
    395       /* only show the internal progress meter once per second */
    396       return 0;
    397 
    398     /* If there's no external callback set, use internal code to show
    399        progress */
    400 
    401     if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
    402       if(data->state.resume_from) {
    403         fprintf(data->set.err,
    404                 "** Resuming transfer from byte position %"
    405                 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
    406       }
    407       fprintf(data->set.err,
    408               "  %% Total    %% Received %% Xferd  Average Speed   "
    409               "Time    Time     Time  Current\n"
    410               "                                 Dload  Upload   "
    411               "Total   Spent    Left  Speed\n");
    412       data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
    413     }
    414 
    415     /* Figure out the estimated time of arrival for the upload */
    416     if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
    417        (data->progress.ulspeed > CURL_OFF_T_C(0))) {
    418       ulestimate = data->progress.size_ul / data->progress.ulspeed;
    419 
    420       if(data->progress.size_ul > CURL_OFF_T_C(10000))
    421         ulpercen = data->progress.uploaded /
    422           (data->progress.size_ul/CURL_OFF_T_C(100));
    423       else if(data->progress.size_ul > CURL_OFF_T_C(0))
    424         ulpercen = (data->progress.uploaded*100) /
    425           data->progress.size_ul;
    426     }
    427 
    428     /* ... and the download */
    429     if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
    430        (data->progress.dlspeed > CURL_OFF_T_C(0))) {
    431       dlestimate = data->progress.size_dl / data->progress.dlspeed;
    432 
    433       if(data->progress.size_dl > CURL_OFF_T_C(10000))
    434         dlpercen = data->progress.downloaded /
    435           (data->progress.size_dl/CURL_OFF_T_C(100));
    436       else if(data->progress.size_dl > CURL_OFF_T_C(0))
    437         dlpercen = (data->progress.downloaded*100) /
    438           data->progress.size_dl;
    439     }
    440 
    441     /* Now figure out which of them is slower and use that one for the
    442        total estimate! */
    443     total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
    444 
    445     /* create the three time strings */
    446     time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
    447     time2str(time_total, total_estimate);
    448     time2str(time_spent, timespent);
    449 
    450     /* Get the total amount of data expected to get transferred */
    451     total_expected_transfer =
    452       (data->progress.flags & PGRS_UL_SIZE_KNOWN?
    453        data->progress.size_ul:data->progress.uploaded)+
    454       (data->progress.flags & PGRS_DL_SIZE_KNOWN?
    455        data->progress.size_dl:data->progress.downloaded);
    456 
    457     /* We have transferred this much so far */
    458     total_transfer = data->progress.downloaded + data->progress.uploaded;
    459 
    460     /* Get the percentage of data transferred so far */
    461     if(total_expected_transfer > CURL_OFF_T_C(10000))
    462       total_percen = total_transfer /
    463         (total_expected_transfer/CURL_OFF_T_C(100));
    464     else if(total_expected_transfer > CURL_OFF_T_C(0))
    465       total_percen = (total_transfer*100) / total_expected_transfer;
    466 
    467     fprintf(data->set.err,
    468             "\r"
    469             "%3" CURL_FORMAT_CURL_OFF_T " %s  "
    470             "%3" CURL_FORMAT_CURL_OFF_T " %s  "
    471             "%3" CURL_FORMAT_CURL_OFF_T " %s  %s  %s %s %s %s %s",
    472             total_percen,  /* 3 letters */                /* total % */
    473             max5data(total_expected_transfer, max5[2]),   /* total size */
    474             dlpercen,      /* 3 letters */                /* rcvd % */
    475             max5data(data->progress.downloaded, max5[0]), /* rcvd size */
    476             ulpercen,      /* 3 letters */                /* xfer % */
    477             max5data(data->progress.uploaded, max5[1]),   /* xfer size */
    478             max5data(data->progress.dlspeed, max5[3]),    /* avrg dl speed */
    479             max5data(data->progress.ulspeed, max5[4]),    /* avrg ul speed */
    480             time_total,    /* 8 letters */                /* total time */
    481             time_spent,    /* 8 letters */                /* time spent */
    482             time_left,     /* 8 letters */                /* time left */
    483             max5data(data->progress.current_speed, max5[5]) /* current speed */
    484             );
    485 
    486     /* we flush the output stream to make it appear as soon as possible */
    487     fflush(data->set.err);
    488 
    489   } /* !(data->progress.flags & PGRS_HIDE) */
    490 
    491   return 0;
    492 }
    493