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