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