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