| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) Daniel Stenberg, <daniel@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 https://curl.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 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | |
| 25 | #include "curl_setup.h" |
| 26 | |
| 27 | #include "urldata.h" |
| 28 | #include "sendf.h" |
| 29 | #include "multiif.h" |
| 30 | #include "progress.h" |
| 31 | #include "timeval.h" |
| 32 | #include "curl_printf.h" |
| 33 | |
| 34 | /* check rate limits within this many recent milliseconds, at minimum. */ |
| 35 | #define MIN_RATE_LIMIT_PERIOD 3000 |
| 36 | |
| 37 | #ifndef CURL_DISABLE_PROGRESS_METER |
| 38 | /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero |
| 39 | byte) */ |
| 40 | static void time2str(char *r, curl_off_t seconds) |
| 41 | { |
| 42 | curl_off_t h; |
| 43 | if(seconds <= 0) { |
| 44 | strcpy(dest: r, src: "--:--:--" ); |
| 45 | return; |
| 46 | } |
| 47 | h = seconds / CURL_OFF_T_C(3600); |
| 48 | if(h <= CURL_OFF_T_C(99)) { |
| 49 | curl_off_t m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60); |
| 50 | curl_off_t s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60)); |
| 51 | msnprintf(buffer: r, maxlength: 9, format: "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T |
| 52 | ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s); |
| 53 | } |
| 54 | else { |
| 55 | /* this equals to more than 99 hours, switch to a more suitable output |
| 56 | format to fit within the limits. */ |
| 57 | curl_off_t d = seconds / CURL_OFF_T_C(86400); |
| 58 | h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600); |
| 59 | if(d <= CURL_OFF_T_C(999)) |
| 60 | msnprintf(buffer: r, maxlength: 9, format: "%3" CURL_FORMAT_CURL_OFF_T |
| 61 | "d %02" CURL_FORMAT_CURL_OFF_T "h" , d, h); |
| 62 | else |
| 63 | msnprintf(buffer: r, maxlength: 9, format: "%7" CURL_FORMAT_CURL_OFF_T "d" , d); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* The point of this function would be to return a string of the input data, |
| 68 | but never longer than 5 columns (+ one zero byte). |
| 69 | Add suffix k, M, G when suitable... */ |
| 70 | static char *max5data(curl_off_t bytes, char *max5) |
| 71 | { |
| 72 | #define ONE_KILOBYTE CURL_OFF_T_C(1024) |
| 73 | #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE) |
| 74 | #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE) |
| 75 | #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE) |
| 76 | #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE) |
| 77 | |
| 78 | if(bytes < CURL_OFF_T_C(100000)) |
| 79 | msnprintf(buffer: max5, maxlength: 6, format: "%5" CURL_FORMAT_CURL_OFF_T, bytes); |
| 80 | |
| 81 | else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE) |
| 82 | msnprintf(buffer: max5, maxlength: 6, format: "%4" CURL_FORMAT_CURL_OFF_T "k" , bytes/ONE_KILOBYTE); |
| 83 | |
| 84 | else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE) |
| 85 | /* 'XX.XM' is good as long as we're less than 100 megs */ |
| 86 | msnprintf(buffer: max5, maxlength: 6, format: "%2" CURL_FORMAT_CURL_OFF_T ".%0" |
| 87 | CURL_FORMAT_CURL_OFF_T "M" , bytes/ONE_MEGABYTE, |
| 88 | (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) ); |
| 89 | |
| 90 | else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE) |
| 91 | /* 'XXXXM' is good until we're at 10000MB or above */ |
| 92 | msnprintf(buffer: max5, maxlength: 6, format: "%4" CURL_FORMAT_CURL_OFF_T "M" , bytes/ONE_MEGABYTE); |
| 93 | |
| 94 | else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE) |
| 95 | /* 10000 MB - 100 GB, we show it as XX.XG */ |
| 96 | msnprintf(buffer: max5, maxlength: 6, format: "%2" CURL_FORMAT_CURL_OFF_T ".%0" |
| 97 | CURL_FORMAT_CURL_OFF_T "G" , bytes/ONE_GIGABYTE, |
| 98 | (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) ); |
| 99 | |
| 100 | else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE) |
| 101 | /* up to 10000GB, display without decimal: XXXXG */ |
| 102 | msnprintf(buffer: max5, maxlength: 6, format: "%4" CURL_FORMAT_CURL_OFF_T "G" , bytes/ONE_GIGABYTE); |
| 103 | |
| 104 | else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE) |
| 105 | /* up to 10000TB, display without decimal: XXXXT */ |
| 106 | msnprintf(buffer: max5, maxlength: 6, format: "%4" CURL_FORMAT_CURL_OFF_T "T" , bytes/ONE_TERABYTE); |
| 107 | |
| 108 | else |
| 109 | /* up to 10000PB, display without decimal: XXXXP */ |
| 110 | msnprintf(buffer: max5, maxlength: 6, format: "%4" CURL_FORMAT_CURL_OFF_T "P" , bytes/ONE_PETABYTE); |
| 111 | |
| 112 | /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number can |
| 113 | hold, but our data type is signed so 8192PB will be the maximum. */ |
| 114 | |
| 115 | return max5; |
| 116 | } |
| 117 | #endif |
| 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 Curl_easy *data) |
| 134 | { |
| 135 | int rc; |
| 136 | data->progress.lastshow = 0; |
| 137 | rc = Curl_pgrsUpdate(data); /* the final (forced) update */ |
| 138 | if(rc) |
| 139 | return rc; |
| 140 | |
| 141 | if(!(data->progress.flags & PGRS_HIDE) && |
| 142 | !data->progress.callback) |
| 143 | /* only output if we don't use a progress callback and we're not |
| 144 | * hidden */ |
| 145 | fprintf(fd: data->set.err, format: "\n" ); |
| 146 | |
| 147 | data->progress.speeder_c = 0; /* reset the progress meter display */ |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* reset the known transfer sizes */ |
| 152 | void Curl_pgrsResetTransferSizes(struct Curl_easy *data) |
| 153 | { |
| 154 | Curl_pgrsSetDownloadSize(data, size: -1); |
| 155 | Curl_pgrsSetUploadSize(data, size: -1); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * |
| 160 | * Curl_pgrsTimeWas(). Store the timestamp time at the given label. |
| 161 | */ |
| 162 | void Curl_pgrsTimeWas(struct Curl_easy *data, timerid timer, |
| 163 | struct curltime timestamp) |
| 164 | { |
| 165 | timediff_t *delta = NULL; |
| 166 | |
| 167 | switch(timer) { |
| 168 | default: |
| 169 | case TIMER_NONE: |
| 170 | /* mistake filter */ |
| 171 | break; |
| 172 | case TIMER_STARTOP: |
| 173 | /* This is set at the start of a transfer */ |
| 174 | data->progress.t_startop = timestamp; |
| 175 | break; |
| 176 | case TIMER_STARTSINGLE: |
| 177 | /* This is set at the start of each single fetch */ |
| 178 | data->progress.t_startsingle = timestamp; |
| 179 | data->progress.is_t_startransfer_set = false; |
| 180 | break; |
| 181 | case TIMER_STARTACCEPT: |
| 182 | data->progress.t_acceptdata = timestamp; |
| 183 | break; |
| 184 | case TIMER_NAMELOOKUP: |
| 185 | delta = &data->progress.t_nslookup; |
| 186 | break; |
| 187 | case TIMER_CONNECT: |
| 188 | delta = &data->progress.t_connect; |
| 189 | break; |
| 190 | case TIMER_APPCONNECT: |
| 191 | delta = &data->progress.t_appconnect; |
| 192 | break; |
| 193 | case TIMER_PRETRANSFER: |
| 194 | delta = &data->progress.t_pretransfer; |
| 195 | break; |
| 196 | case TIMER_STARTTRANSFER: |
| 197 | delta = &data->progress.t_starttransfer; |
| 198 | /* prevent updating t_starttransfer unless: |
| 199 | * 1) this is the first time we're setting t_starttransfer |
| 200 | * 2) a redirect has occurred since the last time t_starttransfer was set |
| 201 | * This prevents repeated invocations of the function from incorrectly |
| 202 | * changing the t_starttransfer time. |
| 203 | */ |
| 204 | if(data->progress.is_t_startransfer_set) { |
| 205 | return; |
| 206 | } |
| 207 | else { |
| 208 | data->progress.is_t_startransfer_set = true; |
| 209 | break; |
| 210 | } |
| 211 | case TIMER_POSTRANSFER: |
| 212 | /* this is the normal end-of-transfer thing */ |
| 213 | break; |
| 214 | case TIMER_REDIRECT: |
| 215 | data->progress.t_redirect = Curl_timediff_us(newer: timestamp, |
| 216 | older: data->progress.start); |
| 217 | break; |
| 218 | } |
| 219 | if(delta) { |
| 220 | timediff_t us = Curl_timediff_us(newer: timestamp, older: data->progress.t_startsingle); |
| 221 | if(us < 1) |
| 222 | us = 1; /* make sure at least one microsecond passed */ |
| 223 | *delta += us; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * |
| 229 | * Curl_pgrsTime(). Store the current time at the given label. This fetches a |
| 230 | * fresh "now" and returns it. |
| 231 | * |
| 232 | * @unittest: 1399 |
| 233 | */ |
| 234 | struct curltime Curl_pgrsTime(struct Curl_easy *data, timerid timer) |
| 235 | { |
| 236 | struct curltime now = Curl_now(); |
| 237 | |
| 238 | Curl_pgrsTimeWas(data, timer, timestamp: now); |
| 239 | return now; |
| 240 | } |
| 241 | |
| 242 | void (struct Curl_easy *data) |
| 243 | { |
| 244 | data->progress.speeder_c = 0; /* reset the progress meter display */ |
| 245 | data->progress.start = Curl_now(); |
| 246 | data->progress.is_t_startransfer_set = false; |
| 247 | data->progress.ul_limit_start = data->progress.start; |
| 248 | data->progress.dl_limit_start = data->progress.start; |
| 249 | data->progress.ul_limit_size = 0; |
| 250 | data->progress.dl_limit_size = 0; |
| 251 | data->progress.downloaded = 0; |
| 252 | data->progress.uploaded = 0; |
| 253 | /* clear all bits except HIDE and HEADERS_OUT */ |
| 254 | data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT; |
| 255 | Curl_ratelimit(data, now: data->progress.start); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * This is used to handle speed limits, calculating how many milliseconds to |
| 260 | * wait until we're back under the speed limit, if needed. |
| 261 | * |
| 262 | * The way it works is by having a "starting point" (time & amount of data |
| 263 | * transferred by then) used in the speed computation, to be used instead of |
| 264 | * the start of the transfer. This starting point is regularly moved as |
| 265 | * transfer goes on, to keep getting accurate values (instead of average over |
| 266 | * the entire transfer). |
| 267 | * |
| 268 | * This function takes the current amount of data transferred, the amount at |
| 269 | * the starting point, the limit (in bytes/s), the time of the starting point |
| 270 | * and the current time. |
| 271 | * |
| 272 | * Returns 0 if no waiting is needed or when no waiting is needed but the |
| 273 | * starting point should be reset (to current); or the number of milliseconds |
| 274 | * to wait to get back under the speed limit. |
| 275 | */ |
| 276 | timediff_t Curl_pgrsLimitWaitTime(curl_off_t cursize, |
| 277 | curl_off_t startsize, |
| 278 | curl_off_t limit, |
| 279 | struct curltime start, |
| 280 | struct curltime now) |
| 281 | { |
| 282 | curl_off_t size = cursize - startsize; |
| 283 | timediff_t minimum; |
| 284 | timediff_t actual; |
| 285 | |
| 286 | if(!limit || !size) |
| 287 | return 0; |
| 288 | |
| 289 | /* |
| 290 | * 'minimum' is the number of milliseconds 'size' should take to download to |
| 291 | * stay below 'limit'. |
| 292 | */ |
| 293 | if(size < CURL_OFF_T_MAX/1000) |
| 294 | minimum = (timediff_t) (CURL_OFF_T_C(1000) * size / limit); |
| 295 | else { |
| 296 | minimum = (timediff_t) (size / limit); |
| 297 | if(minimum < TIMEDIFF_T_MAX/1000) |
| 298 | minimum *= 1000; |
| 299 | else |
| 300 | minimum = TIMEDIFF_T_MAX; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * 'actual' is the time in milliseconds it took to actually download the |
| 305 | * last 'size' bytes. |
| 306 | */ |
| 307 | actual = Curl_timediff(newer: now, older: start); |
| 308 | if(actual < minimum) { |
| 309 | /* if it downloaded the data faster than the limit, make it wait the |
| 310 | difference */ |
| 311 | return (minimum - actual); |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Set the number of downloaded bytes so far. |
| 319 | */ |
| 320 | CURLcode (struct Curl_easy *data, curl_off_t size) |
| 321 | { |
| 322 | if(data->set.max_filesize && (size > data->set.max_filesize)) { |
| 323 | failf(data, fmt: "Exceeded the maximum allowed file size " |
| 324 | "(%" CURL_FORMAT_CURL_OFF_T ")" , |
| 325 | data->set.max_filesize); |
| 326 | return CURLE_FILESIZE_EXCEEDED; |
| 327 | } |
| 328 | data->progress.downloaded = size; |
| 329 | return CURLE_OK; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Update the timestamp and sizestamp to use for rate limit calculations. |
| 334 | */ |
| 335 | void Curl_ratelimit(struct Curl_easy *data, struct curltime now) |
| 336 | { |
| 337 | /* don't set a new stamp unless the time since last update is long enough */ |
| 338 | if(data->set.max_recv_speed) { |
| 339 | if(Curl_timediff(newer: now, older: data->progress.dl_limit_start) >= |
| 340 | MIN_RATE_LIMIT_PERIOD) { |
| 341 | data->progress.dl_limit_start = now; |
| 342 | data->progress.dl_limit_size = data->progress.downloaded; |
| 343 | } |
| 344 | } |
| 345 | if(data->set.max_send_speed) { |
| 346 | if(Curl_timediff(newer: now, older: data->progress.ul_limit_start) >= |
| 347 | MIN_RATE_LIMIT_PERIOD) { |
| 348 | data->progress.ul_limit_start = now; |
| 349 | data->progress.ul_limit_size = data->progress.uploaded; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Set the number of uploaded bytes so far. |
| 356 | */ |
| 357 | void (struct Curl_easy *data, curl_off_t size) |
| 358 | { |
| 359 | data->progress.uploaded = size; |
| 360 | } |
| 361 | |
| 362 | void (struct Curl_easy *data, curl_off_t size) |
| 363 | { |
| 364 | if(size >= 0) { |
| 365 | data->progress.size_dl = size; |
| 366 | data->progress.flags |= PGRS_DL_SIZE_KNOWN; |
| 367 | } |
| 368 | else { |
| 369 | data->progress.size_dl = 0; |
| 370 | data->progress.flags &= ~PGRS_DL_SIZE_KNOWN; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void (struct Curl_easy *data, curl_off_t size) |
| 375 | { |
| 376 | if(size >= 0) { |
| 377 | data->progress.size_ul = size; |
| 378 | data->progress.flags |= PGRS_UL_SIZE_KNOWN; |
| 379 | } |
| 380 | else { |
| 381 | data->progress.size_ul = 0; |
| 382 | data->progress.flags &= ~PGRS_UL_SIZE_KNOWN; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* returns the average speed in bytes / second */ |
| 387 | static curl_off_t trspeed(curl_off_t size, /* number of bytes */ |
| 388 | curl_off_t us) /* microseconds */ |
| 389 | { |
| 390 | if(us < 1) |
| 391 | return size * 1000000; |
| 392 | else if(size < CURL_OFF_T_MAX/1000000) |
| 393 | return (size * 1000000) / us; |
| 394 | else if(us >= 1000000) |
| 395 | return size / (us / 1000000); |
| 396 | else |
| 397 | return CURL_OFF_T_MAX; |
| 398 | } |
| 399 | |
| 400 | /* returns TRUE if it's time to show the progress meter */ |
| 401 | static bool progress_calc(struct Curl_easy *data, struct curltime now) |
| 402 | { |
| 403 | bool timetoshow = FALSE; |
| 404 | struct Progress * const p = &data->progress; |
| 405 | |
| 406 | /* The time spent so far (from the start) in microseconds */ |
| 407 | p->timespent = Curl_timediff_us(newer: now, older: p->start); |
| 408 | p->dlspeed = trspeed(size: p->downloaded, us: p->timespent); |
| 409 | p->ulspeed = trspeed(size: p->uploaded, us: p->timespent); |
| 410 | |
| 411 | /* Calculations done at most once a second, unless end is reached */ |
| 412 | if(p->lastshow != now.tv_sec) { |
| 413 | int countindex; /* amount of seconds stored in the speeder array */ |
| 414 | int nowindex = p->speeder_c% CURR_TIME; |
| 415 | p->lastshow = now.tv_sec; |
| 416 | timetoshow = TRUE; |
| 417 | |
| 418 | /* Let's do the "current speed" thing, with the dl + ul speeds |
| 419 | combined. Store the speed at entry 'nowindex'. */ |
| 420 | p->speeder[ nowindex ] = p->downloaded + p->uploaded; |
| 421 | |
| 422 | /* remember the exact time for this moment */ |
| 423 | p->speeder_time [ nowindex ] = now; |
| 424 | |
| 425 | /* advance our speeder_c counter, which is increased every time we get |
| 426 | here and we expect it to never wrap as 2^32 is a lot of seconds! */ |
| 427 | p->speeder_c++; |
| 428 | |
| 429 | /* figure out how many index entries of data we have stored in our speeder |
| 430 | array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of |
| 431 | transfer. Imagine, after one second we have filled in two entries, |
| 432 | after two seconds we've filled in three entries etc. */ |
| 433 | countindex = ((p->speeder_c >= CURR_TIME)? CURR_TIME:p->speeder_c) - 1; |
| 434 | |
| 435 | /* first of all, we don't do this if there's no counted seconds yet */ |
| 436 | if(countindex) { |
| 437 | int checkindex; |
| 438 | timediff_t span_ms; |
| 439 | curl_off_t amount; |
| 440 | |
| 441 | /* Get the index position to compare with the 'nowindex' position. |
| 442 | Get the oldest entry possible. While we have less than CURR_TIME |
| 443 | entries, the first entry will remain the oldest. */ |
| 444 | checkindex = (p->speeder_c >= CURR_TIME)? p->speeder_c%CURR_TIME:0; |
| 445 | |
| 446 | /* Figure out the exact time for the time span */ |
| 447 | span_ms = Curl_timediff(newer: now, older: p->speeder_time[checkindex]); |
| 448 | if(0 == span_ms) |
| 449 | span_ms = 1; /* at least one millisecond MUST have passed */ |
| 450 | |
| 451 | /* Calculate the average speed the last 'span_ms' milliseconds */ |
| 452 | amount = p->speeder[nowindex]- p->speeder[checkindex]; |
| 453 | |
| 454 | if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */) |
| 455 | /* the 'amount' value is bigger than would fit in 32 bits if |
| 456 | multiplied with 1000, so we use the double math for this */ |
| 457 | p->current_speed = (curl_off_t) |
| 458 | ((double)amount/((double)span_ms/1000.0)); |
| 459 | else |
| 460 | /* the 'amount' value is small enough to fit within 32 bits even |
| 461 | when multiplied with 1000 */ |
| 462 | p->current_speed = amount*CURL_OFF_T_C(1000)/span_ms; |
| 463 | } |
| 464 | else |
| 465 | /* the first second we use the average */ |
| 466 | p->current_speed = p->ulspeed + p->dlspeed; |
| 467 | |
| 468 | } /* Calculations end */ |
| 469 | return timetoshow; |
| 470 | } |
| 471 | |
| 472 | #ifndef CURL_DISABLE_PROGRESS_METER |
| 473 | static void progress_meter(struct Curl_easy *data) |
| 474 | { |
| 475 | char max5[6][10]; |
| 476 | curl_off_t dlpercen = 0; |
| 477 | curl_off_t ulpercen = 0; |
| 478 | curl_off_t total_percen = 0; |
| 479 | curl_off_t total_transfer; |
| 480 | curl_off_t total_expected_transfer; |
| 481 | char time_left[10]; |
| 482 | char time_total[10]; |
| 483 | char time_spent[10]; |
| 484 | curl_off_t ulestimate = 0; |
| 485 | curl_off_t dlestimate = 0; |
| 486 | curl_off_t total_estimate; |
| 487 | curl_off_t timespent = |
| 488 | (curl_off_t)data->progress.timespent/1000000; /* seconds */ |
| 489 | |
| 490 | if(!(data->progress.flags & PGRS_HEADERS_OUT)) { |
| 491 | if(data->state.resume_from) { |
| 492 | fprintf(fd: data->set.err, |
| 493 | format: "** Resuming transfer from byte position %" |
| 494 | CURL_FORMAT_CURL_OFF_T "\n" , data->state.resume_from); |
| 495 | } |
| 496 | fprintf(fd: data->set.err, |
| 497 | format: " %% Total %% Received %% Xferd Average Speed " |
| 498 | "Time Time Time Current\n" |
| 499 | " Dload Upload " |
| 500 | "Total Spent Left Speed\n" ); |
| 501 | data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */ |
| 502 | } |
| 503 | |
| 504 | /* Figure out the estimated time of arrival for the upload */ |
| 505 | if((data->progress.flags & PGRS_UL_SIZE_KNOWN) && |
| 506 | (data->progress.ulspeed > CURL_OFF_T_C(0))) { |
| 507 | ulestimate = data->progress.size_ul / data->progress.ulspeed; |
| 508 | |
| 509 | if(data->progress.size_ul > CURL_OFF_T_C(10000)) |
| 510 | ulpercen = data->progress.uploaded / |
| 511 | (data->progress.size_ul/CURL_OFF_T_C(100)); |
| 512 | else if(data->progress.size_ul > CURL_OFF_T_C(0)) |
| 513 | ulpercen = (data->progress.uploaded*100) / |
| 514 | data->progress.size_ul; |
| 515 | } |
| 516 | |
| 517 | /* ... and the download */ |
| 518 | if((data->progress.flags & PGRS_DL_SIZE_KNOWN) && |
| 519 | (data->progress.dlspeed > CURL_OFF_T_C(0))) { |
| 520 | dlestimate = data->progress.size_dl / data->progress.dlspeed; |
| 521 | |
| 522 | if(data->progress.size_dl > CURL_OFF_T_C(10000)) |
| 523 | dlpercen = data->progress.downloaded / |
| 524 | (data->progress.size_dl/CURL_OFF_T_C(100)); |
| 525 | else if(data->progress.size_dl > CURL_OFF_T_C(0)) |
| 526 | dlpercen = (data->progress.downloaded*100) / |
| 527 | data->progress.size_dl; |
| 528 | } |
| 529 | |
| 530 | /* Now figure out which of them is slower and use that one for the |
| 531 | total estimate! */ |
| 532 | total_estimate = ulestimate>dlestimate?ulestimate:dlestimate; |
| 533 | |
| 534 | /* create the three time strings */ |
| 535 | time2str(r: time_left, seconds: total_estimate > 0?(total_estimate - timespent):0); |
| 536 | time2str(r: time_total, seconds: total_estimate); |
| 537 | time2str(r: time_spent, seconds: timespent); |
| 538 | |
| 539 | /* Get the total amount of data expected to get transferred */ |
| 540 | total_expected_transfer = |
| 541 | ((data->progress.flags & PGRS_UL_SIZE_KNOWN)? |
| 542 | data->progress.size_ul:data->progress.uploaded)+ |
| 543 | ((data->progress.flags & PGRS_DL_SIZE_KNOWN)? |
| 544 | data->progress.size_dl:data->progress.downloaded); |
| 545 | |
| 546 | /* We have transferred this much so far */ |
| 547 | total_transfer = data->progress.downloaded + data->progress.uploaded; |
| 548 | |
| 549 | /* Get the percentage of data transferred so far */ |
| 550 | if(total_expected_transfer > CURL_OFF_T_C(10000)) |
| 551 | total_percen = total_transfer / |
| 552 | (total_expected_transfer/CURL_OFF_T_C(100)); |
| 553 | else if(total_expected_transfer > CURL_OFF_T_C(0)) |
| 554 | total_percen = (total_transfer*100) / total_expected_transfer; |
| 555 | |
| 556 | fprintf(fd: data->set.err, |
| 557 | format: "\r" |
| 558 | "%3" CURL_FORMAT_CURL_OFF_T " %s " |
| 559 | "%3" CURL_FORMAT_CURL_OFF_T " %s " |
| 560 | "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s" , |
| 561 | total_percen, /* 3 letters */ /* total % */ |
| 562 | max5data(bytes: total_expected_transfer, max5: max5[2]), /* total size */ |
| 563 | dlpercen, /* 3 letters */ /* rcvd % */ |
| 564 | max5data(bytes: data->progress.downloaded, max5: max5[0]), /* rcvd size */ |
| 565 | ulpercen, /* 3 letters */ /* xfer % */ |
| 566 | max5data(bytes: data->progress.uploaded, max5: max5[1]), /* xfer size */ |
| 567 | max5data(bytes: data->progress.dlspeed, max5: max5[3]), /* avrg dl speed */ |
| 568 | max5data(bytes: data->progress.ulspeed, max5: max5[4]), /* avrg ul speed */ |
| 569 | time_total, /* 8 letters */ /* total time */ |
| 570 | time_spent, /* 8 letters */ /* time spent */ |
| 571 | time_left, /* 8 letters */ /* time left */ |
| 572 | max5data(bytes: data->progress.current_speed, max5: max5[5]) |
| 573 | ); |
| 574 | |
| 575 | /* we flush the output stream to make it appear as soon as possible */ |
| 576 | fflush(stream: data->set.err); |
| 577 | } |
| 578 | #else |
| 579 | /* progress bar disabled */ |
| 580 | #define progress_meter(x) Curl_nop_stmt |
| 581 | #endif |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | * Curl_pgrsUpdate() returns 0 for success or the value returned by the |
| 586 | * progress callback! |
| 587 | */ |
| 588 | int Curl_pgrsUpdate(struct Curl_easy *data) |
| 589 | { |
| 590 | struct curltime now = Curl_now(); /* what time is it */ |
| 591 | bool showprogress = progress_calc(data, now); |
| 592 | if(!(data->progress.flags & PGRS_HIDE)) { |
| 593 | if(data->set.fxferinfo) { |
| 594 | int result; |
| 595 | /* There's a callback set, call that */ |
| 596 | Curl_set_in_callback(data, true); |
| 597 | result = data->set.fxferinfo(data->set.progress_client, |
| 598 | data->progress.size_dl, |
| 599 | data->progress.downloaded, |
| 600 | data->progress.size_ul, |
| 601 | data->progress.uploaded); |
| 602 | Curl_set_in_callback(data, false); |
| 603 | if(result != CURL_PROGRESSFUNC_CONTINUE) { |
| 604 | if(result) |
| 605 | failf(data, fmt: "Callback aborted" ); |
| 606 | return result; |
| 607 | } |
| 608 | } |
| 609 | else if(data->set.fprogress) { |
| 610 | int result; |
| 611 | /* The older deprecated callback is set, call that */ |
| 612 | Curl_set_in_callback(data, true); |
| 613 | result = data->set.fprogress(data->set.progress_client, |
| 614 | (double)data->progress.size_dl, |
| 615 | (double)data->progress.downloaded, |
| 616 | (double)data->progress.size_ul, |
| 617 | (double)data->progress.uploaded); |
| 618 | Curl_set_in_callback(data, false); |
| 619 | if(result != CURL_PROGRESSFUNC_CONTINUE) { |
| 620 | if(result) |
| 621 | failf(data, fmt: "Callback aborted" ); |
| 622 | return result; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if(showprogress) |
| 627 | progress_meter(data); |
| 628 | } |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |