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