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