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