Coverage Report

Created: 2026-09-04 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/progress.c
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
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "curl_trc.h"
28
#include "multiif.h"
29
#include "progress.h"
30
#include "transfer.h"
31
#include "curlx/strcopy.h"
32
33
#ifndef CURL_DISABLE_PROGRESS_METER
34
/* Provide a string that is 7 letters long (plus the zero byte).
35
36
   @unittest 1636 */
37
UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds);
38
UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds)
39
8.83k
{
40
8.83k
  curl_off_t h;
41
8.83k
  if(seconds <= 0) {
42
8.83k
    curlx_strcopy(r, rsize, STRCONST("       "));
43
8.83k
    return;
44
8.83k
  }
45
8
  h = seconds / 3600;
46
8
  if(h <= 99) {
47
8
    curl_off_t m = (seconds - (h * 3600)) / 60;
48
8
    if(h <= 9) {
49
8
      curl_off_t s = (seconds - (h * 3600)) - (m * 60);
50
8
      if(h)
51
0
        curl_msnprintf(r, rsize, "%" FMT_OFF_T ":%02" FMT_OFF_T ":"
52
0
                       "%02" FMT_OFF_T, h, m, s);
53
8
      else
54
8
        curl_msnprintf(r, rsize, "  %02" FMT_OFF_T ":%02" FMT_OFF_T, m, s);
55
8
    }
56
0
    else
57
0
      curl_msnprintf(r, rsize, "%" FMT_OFF_T "h %02" FMT_OFF_T "m", h, m);
58
8
  }
59
0
  else {
60
0
    curl_off_t d = seconds / 86400;
61
0
    h = (seconds - (d * 86400)) / 3600;
62
0
    if(d <= 99)
63
0
      curl_msnprintf(r, rsize, "%2" FMT_OFF_T "d %02" FMT_OFF_T "h", d, h);
64
0
    else if(d <= 999)
65
0
      curl_msnprintf(r, rsize, "%6" FMT_OFF_T "d", d);
66
0
    else { /* more than 999 days */
67
0
      curl_off_t m = d / 30;
68
0
      if(m <= 999)
69
0
        curl_msnprintf(r, rsize, "%6" FMT_OFF_T "m", m);
70
0
      else { /* more than 999 months */
71
0
        curl_off_t y = d / 365;
72
0
        if(y <= 99999)
73
0
          curl_msnprintf(r, rsize, "%6" FMT_OFF_T "y", y);
74
0
        else
75
0
          curlx_strcopy(r, rsize, STRCONST(">99999y"));
76
0
      }
77
0
    }
78
0
  }
79
8
}
80
81
/* The point of this function would be to return a string of the input data,
82
   but never longer than 6 columns (+ one zero byte).
83
   Add suffix k, M, G when suitable...
84
85
   @unittest 1636 */
86
UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen);
87
UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen)
88
17.6k
{
89
  /* a signed 64-bit value is 8192 petabytes maximum, shown as
90
     8.0E (exabytes)*/
91
17.6k
  if(bytes < 100000)
92
16.7k
    curl_msnprintf(max6, mlen, "%6" CURL_FORMAT_CURL_OFF_T, bytes);
93
895
  else {
94
895
    static const char unit[] = { 'k', 'M', 'G', 'T', 'P', 'E', 0 };
95
895
    int k = 0;
96
895
    curl_off_t nbytes;
97
895
    curl_off_t rest;
98
1.79k
    do {
99
1.79k
      nbytes = bytes / 1024;
100
1.79k
      if(nbytes < 1000)
101
895
        break;
102
900
      bytes = nbytes;
103
900
      k++;
104
900
      DEBUGASSERT(unit[k]);
105
900
    } while(unit[k]);
106
895
    rest = bytes % 1024;
107
895
    if(nbytes <= 99)
108
      /* xx.yyU */
109
387
      curl_msnprintf(max6, mlen, "%2" CURL_FORMAT_CURL_OFF_T
110
387
                     ".%02" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
111
387
                     rest * 100 / 1024, unit[k]);
112
508
    else
113
      /* xxx.yU */
114
508
      curl_msnprintf(max6, mlen, "%3" CURL_FORMAT_CURL_OFF_T
115
508
                     ".%" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
116
508
                     rest * 10 / 1024, unit[k]);
117
895
  }
118
17.6k
  return max6;
119
17.6k
}
120
#endif
121
122
static void pgrs_speedinit(struct Curl_easy *data)
123
29.4k
{
124
29.4k
  memset(&data->state.keeps_speed, 0, sizeof(struct curltime));
125
29.4k
}
126
127
/*
128
 * @unittest 1606
129
 */
130
UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
131
                                  const struct curltime *pnow);
132
UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
133
                                  const struct curltime *pnow)
134
11.6k
{
135
11.6k
  if(!data->set.low_speed_time || !data->set.low_speed_limit ||
136
143
     Curl_xfer_recv_is_paused(data) || Curl_xfer_send_is_paused(data))
137
    /* A paused transfer is not qualified for speed checks */
138
11.5k
    return CURLE_OK;
139
140
143
  if(data->progress.current_speed >= 0) {
141
143
    if(data->progress.current_speed < data->set.low_speed_limit) {
142
143
      if(!data->state.keeps_speed.tv_sec)
143
        /* under the limit at this moment */
144
110
        data->state.keeps_speed = *pnow;
145
33
      else {
146
        /* how long has it been under the limit */
147
33
        timediff_t howlong =
148
33
          curlx_ptimediff_ms(pnow, &data->state.keeps_speed);
149
150
33
        if(howlong >= data->set.low_speed_time * 1000) {
151
          /* too long */
152
0
          failf(data, "Operation too slow. Less than %" FMT_OFF_T
153
0
                " bytes/sec transferred the last %u seconds",
154
0
                data->set.low_speed_limit, data->set.low_speed_time);
155
0
          return CURLE_OPERATION_TIMEDOUT;
156
0
        }
157
33
      }
158
143
    }
159
0
    else
160
      /* faster right now */
161
0
      data->state.keeps_speed.tv_sec = 0;
162
143
  }
163
164
  /* since low speed limit is enabled, set the expire timer to make this
165
     connection's speed get checked again in a second */
166
143
  Curl_expire_set(data, EXPIRE_SPEEDCHECK, 1000, pnow);
167
168
143
  return CURLE_OK;
169
143
}
170
171
const struct curltime *Curl_pgrs_now(struct Curl_easy *data)
172
1.35M
{
173
1.35M
  curlx_pnow(&data->progress.now);
174
1.35M
  return &data->progress.now;
175
1.35M
}
176
177
/* New proposed interface, 9th of February 2000:
178
179
   pgrsStartNow() - sets start time
180
   pgrsSetDownloadSize(x) - known expected download size
181
   pgrsSetUploadSize(x) - known expected upload size
182
   pgrsSetDownloadCounter() - amount of data currently downloaded
183
   pgrsSetUploadCounter() - amount of data currently uploaded
184
   pgrsUpdate() - show progress
185
   pgrsDone() - transfer complete
186
 */
187
188
int Curl_pgrsDone(struct Curl_easy *data)
189
27.0k
{
190
27.0k
  int rc;
191
27.0k
  data->progress.delta.lastshow_us = -1;
192
27.0k
  rc = Curl_pgrsUpdate(data); /* the final (forced) update */
193
27.0k
  if(rc)
194
0
    return rc;
195
196
27.0k
  if(!data->progress.hide && !data->progress.callback)
197
    /* only output if we do not use a progress callback and we are not
198
     * hidden */
199
974
    curl_mfprintf(data->set.err, "\n");
200
201
27.0k
  return 0;
202
27.0k
}
203
204
void Curl_pgrsReset(struct Curl_easy *data)
205
29.4k
{
206
29.4k
  Curl_pgrsSetUploadCounter(data, 0);
207
29.4k
  data->progress.dl.cur_size = 0;
208
29.4k
  Curl_pgrsSetUploadSize(data, -1);
209
29.4k
  Curl_pgrsSetDownloadSize(data, -1);
210
29.4k
  data->progress.speeder_c = 0; /* reset speed records */
211
29.4k
  data->progress.deliver = 0;
212
29.4k
  pgrs_speedinit(data);
213
29.4k
}
214
215
/* reset the known transfer sizes */
216
void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
217
29.9k
{
218
29.9k
  Curl_pgrsSetDownloadSize(data, -1);
219
29.9k
  Curl_pgrsSetUploadSize(data, -1);
220
29.9k
}
221
222
void Curl_pgrsRecvPause(struct Curl_easy *data, bool enable)
223
0
{
224
0
  if(!enable) {
225
0
    data->progress.speeder_c = 0; /* reset speed records */
226
0
    pgrs_speedinit(data); /* reset low speed measurements */
227
0
  }
228
0
}
229
230
void Curl_pgrsSendPause(struct Curl_easy *data, bool enable)
231
0
{
232
0
  if(!enable) {
233
0
    data->progress.speeder_c = 0; /* reset speed records */
234
0
    pgrs_speedinit(data); /* reset low speed measurements */
235
0
  }
236
0
}
237
238
#ifdef CURLVERBOSE
239
static const char * const pgrs_timer_names[] = {
240
  "PGRS-NONE",
241
  "PGRS-STARTOP",
242
  "PGRS-STARTSINGLE",
243
  "PGRS-POSTQUEUE",
244
  "PGRS-NAMELOOKUP",
245
  "PGRS-CONNECT",
246
  "PGRS-APPCONNECT",
247
  "PGRS-PRETRANSFER",
248
  "PGRS-STARTTRANSFER",
249
  "PGRS-POSTRANSFER",
250
  "PGRS-REDIRECT",
251
};
252
253
static const char *pgrs_timer_name(timerid timer)
254
0
{
255
0
  if((size_t)timer < CURL_ARRAYSIZE(pgrs_timer_names))
256
0
    return pgrs_timer_names[(size_t)timer];
257
0
  return "?";
258
0
}
259
#endif /* CURLVERBOSE */
260
261
/*
262
 * Curl_pgrsTimeWas(). Store the timestamp time at the given label.
263
 */
264
void Curl_pgrsTimeWas(struct Curl_easy *data, timerid timer,
265
                      struct curltime timestamp)
266
216k
{
267
216k
  timediff_t *delta = NULL;
268
269
216k
  switch(timer) {
270
0
  default:
271
0
  case TIMER_NONE:
272
    /* mistake filter */
273
0
    break;
274
17.9k
  case TIMER_STARTOP:
275
    /* This is set at the start of a transfer */
276
17.9k
    data->progress.delta.startop_us =
277
17.9k
      curlx_ptimediff_us(&timestamp, &data->progress.start);
278
17.9k
    data->progress.delta.startqueue_us = data->progress.delta.startop_us;
279
17.9k
    break;
280
29.9k
  case TIMER_STARTSINGLE:
281
    /* This is set at the start of each single transfer */
282
29.9k
    data->progress.delta.startsingle_us =
283
29.9k
      curlx_ptimediff_us(&timestamp, &data->progress.start);
284
29.9k
    data->progress.startransfer_added = FALSE;
285
29.9k
    break;
286
28.9k
  case TIMER_POSTQUEUE:
287
    /* Queue time is accumulative from all involved redirects */
288
28.9k
    data->progress.total.queued_us +=
289
28.9k
      curlx_ptimediff_us(&timestamp, &data->progress.start) -
290
28.9k
      data->progress.delta.startqueue_us;
291
28.9k
    break;
292
21.7k
  case TIMER_NAMELOOKUP:
293
21.7k
    delta = &data->progress.total.nslookup_us;
294
21.7k
    break;
295
20.5k
  case TIMER_CONNECT:
296
20.5k
    delta = &data->progress.total.connect_us;
297
20.5k
    break;
298
0
  case TIMER_APPCONNECT:
299
0
    delta = &data->progress.total.appconnect_us;
300
0
    break;
301
32.8k
  case TIMER_PRETRANSFER:
302
32.8k
    delta = &data->progress.total.pretransfer_us;
303
32.8k
    break;
304
19.8k
  case TIMER_STARTTRANSFER:
305
    /* prevent updating t_starttransfer unless:
306
     *   1. this is the first time we are setting t_starttransfer
307
     *   2. a redirect has occurred since the last time t_starttransfer was set
308
     * This prevents repeated invocations of the function from incorrectly
309
     * changing the t_starttransfer time.
310
     */
311
19.8k
    if(data->progress.startransfer_added) {
312
0
      CURL_TRC_M(data, "[%s] ignored", pgrs_timer_name(timer));
313
0
      return;
314
0
    }
315
19.8k
    data->progress.startransfer_added = TRUE;
316
19.8k
    delta = &data->progress.total.starttransfer_us;
317
19.8k
    break;
318
32.3k
  case TIMER_POSTRANSFER:
319
32.3k
    delta = &data->progress.total.posttransfer_us;
320
32.3k
    break;
321
12.0k
  case TIMER_REDIRECT:
322
12.0k
    data->progress.delta.startredirect_us =
323
12.0k
      curlx_ptimediff_us(&timestamp, &data->progress.start);
324
    /* transfer starts queueing again */
325
12.0k
    data->progress.delta.startqueue_us =
326
12.0k
      data->progress.delta.startredirect_us;
327
12.0k
    break;
328
216k
  }
329
216k
  if(delta) {
330
127k
    timediff_t us = curlx_ptimediff_us(&timestamp, &data->progress.start) -
331
127k
                    data->progress.delta.startsingle_us;
332
127k
    if(us < 1)
333
0
      us = 1; /* make sure at least one microsecond passed */
334
127k
    *delta += us;
335
127k
    CURL_TRC_M(data, "[%s] added %" FMT_TIMEDIFF_T "us",
336
127k
               pgrs_timer_name(timer), us);
337
127k
  }
338
88.8k
  else
339
88.8k
    CURL_TRC_M(data, "[%s] set", pgrs_timer_name(timer));
340
216k
}
341
342
/*
343
 * Curl_pgrsTime(). Store the current time at the given label. This fetches a
344
 * fresh "now" and returns it.
345
 *
346
 * @unittest: 1399
347
 */
348
void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
349
136k
{
350
136k
  Curl_pgrsTimeWas(data, timer, *Curl_pgrs_now(data));
351
136k
}
352
353
void Curl_pgrsStart(struct Curl_easy *data, const struct curltime *pnow)
354
17.9k
{
355
17.9k
  struct Progress *p = &data->progress;
356
357
17.9k
  if(!pnow)
358
17.9k
    pnow = Curl_pgrs_now(data);
359
17.9k
  p->speeder_c = 0; /* reset the progress meter display */
360
17.9k
  p->start = *pnow;
361
17.9k
  memset(&p->delta, 0, sizeof(p->delta));
362
17.9k
  memset(&p->total, 0, sizeof(p->total));
363
17.9k
  p->startransfer_added = FALSE;
364
17.9k
  p->dl.cur_size = 0;
365
17.9k
  p->ul.cur_size = 0;
366
  /* the sizes are unknown at start */
367
17.9k
  p->dl_size_known = FALSE;
368
17.9k
  p->ul_size_known = FALSE;
369
17.9k
}
370
371
/* check that the 'delta' amount of bytes are okay to deliver to the
372
   application, or return error if not. */
373
CURLcode Curl_pgrs_deliver_check(struct Curl_easy *data, size_t delta)
374
296k
{
375
296k
  if(data->set.max_filesize &&
376
1.05k
     ((curl_off_t)delta > data->set.max_filesize - data->progress.deliver)) {
377
9
    failf(data, "Would have exceeded max file size");
378
9
    return CURLE_FILESIZE_EXCEEDED;
379
9
  }
380
296k
  return CURLE_OK;
381
296k
}
382
383
/* this counts how much data is delivered to the application, which
384
   in compressed cases may differ from downloaded amount */
385
void Curl_pgrs_deliver_inc(struct Curl_easy *data, size_t delta)
386
296k
{
387
296k
  data->progress.deliver += delta;
388
296k
}
389
390
void Curl_pgrs_download_inc(struct Curl_easy *data, size_t delta)
391
2.15k
{
392
2.15k
  if(delta) {
393
2.15k
    data->progress.dl.cur_size += delta;
394
2.15k
    Curl_rlimit_drain(&data->progress.dl.rlimit, delta, NULL);
395
2.15k
  }
396
2.15k
}
397
398
void Curl_pgrs_upload_inc(struct Curl_easy *data, size_t delta)
399
5.63k
{
400
5.63k
  if(delta) {
401
5.63k
    data->progress.ul.cur_size += delta;
402
5.63k
    Curl_rlimit_drain(&data->progress.ul.rlimit, delta, NULL);
403
5.63k
  }
404
5.63k
}
405
406
/*
407
 * Set the number of uploaded bytes so far.
408
 */
409
void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
410
29.4k
{
411
29.4k
  data->progress.ul.cur_size = size;
412
29.4k
}
413
414
void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
415
66.7k
{
416
66.7k
  if(size >= 0) {
417
7.32k
    data->progress.dl.total_size = size;
418
7.32k
    data->progress.dl_size_known = TRUE;
419
7.32k
  }
420
59.4k
  else {
421
59.4k
    data->progress.dl.total_size = 0;
422
59.4k
    data->progress.dl_size_known = FALSE;
423
59.4k
  }
424
66.7k
}
425
426
void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
427
86.2k
{
428
86.2k
  if(size >= 0) {
429
25.8k
    data->progress.ul.total_size = size;
430
25.8k
    data->progress.ul_size_known = TRUE;
431
25.8k
  }
432
60.3k
  else {
433
60.3k
    data->progress.ul.total_size = 0;
434
60.3k
    data->progress.ul_size_known = FALSE;
435
60.3k
  }
436
86.2k
}
437
438
void Curl_pgrsEarlyData(struct Curl_easy *data, curl_off_t sent)
439
0
{
440
0
  data->progress.earlydata_sent = sent;
441
0
}
442
443
/* returns the average speed in bytes / second */
444
static curl_off_t trspeed(curl_off_t size, /* number of bytes */
445
                          curl_off_t us)   /* microseconds */
446
873k
{
447
873k
  if(us < 1)
448
0
    return size * 1000000;
449
873k
  else if(size < CURL_OFF_T_MAX / 1000000)
450
873k
    return (size * 1000000) / us;
451
0
  else if(us >= 1000000)
452
0
    return size / (us / 1000000);
453
0
  else
454
0
    return CURL_OFF_T_MAX;
455
873k
}
456
457
/* returns TRUE if it is time to show the progress meter */
458
static bool progress_calc(struct Curl_easy *data,
459
                          const struct curltime *pnow)
460
436k
{
461
436k
  struct Progress * const p = &data->progress;
462
436k
  int i_next, i_oldest, i_latest;
463
436k
  timediff_t duration_us, elapsed_us;
464
436k
  curl_off_t amount;
465
466
  /* The time spent so far (from the start) in microseconds */
467
436k
  elapsed_us = curlx_ptimediff_us(pnow, &p->start);
468
436k
  p->total.spent_us = elapsed_us;
469
436k
  p->dl.speed = trspeed(p->dl.cur_size, p->total.spent_us);
470
436k
  p->ul.speed = trspeed(p->ul.cur_size, p->total.spent_us);
471
472
436k
  if(!p->speeder_c) { /* no previous record exists */
473
29.2k
    p->speed_amount[0] = p->dl.cur_size + p->ul.cur_size;
474
29.2k
    p->speed_time[0] = elapsed_us;
475
29.2k
    p->speeder_c++;
476
    /* use the overall average at the start */
477
29.2k
    p->current_speed = p->ul.speed + p->dl.speed;
478
29.2k
    p->delta.lastshow_us = elapsed_us;
479
29.2k
    return TRUE;
480
29.2k
  }
481
  /* We have at least one record now. Where to put the next and
482
   * where is the latest one? */
483
407k
  i_next = p->speeder_c % CURL_SPEED_RECORDS;
484
407k
  i_latest = (i_next > 0) ? (i_next - 1) : (CURL_SPEED_RECORDS - 1);
485
486
  /* Make a new record only when some time has passed.
487
   * Too frequent calls otherwise ruin the history. */
488
407k
  if((elapsed_us - p->speed_time[i_latest]) >= (1000 * 1000)) {
489
0
    p->speeder_c++;
490
0
    i_latest = i_next;
491
0
    p->speed_amount[i_latest] = p->dl.cur_size + p->ul.cur_size;
492
0
    p->speed_time[i_latest] = elapsed_us;
493
0
  }
494
407k
  else if(data->req.done) {
495
    /* When a transfer is done, and we did not have a current speed
496
     * already, update the last record. Otherwise, stay at the speed
497
     * we have. The last chunk of data, when rate limiting, would increase
498
     * reported speed since it no longer measures a full second. */
499
41.8k
    if(!p->current_speed) {
500
41.8k
      p->speed_amount[i_latest] = p->dl.cur_size + p->ul.cur_size;
501
41.8k
      p->speed_time[i_latest] = elapsed_us;
502
41.8k
    }
503
41.8k
  }
504
365k
  else {
505
    /* transfer ongoing, wait for more time to pass. */
506
365k
    return FALSE;
507
365k
  }
508
509
41.8k
  i_oldest = (p->speeder_c < CURL_SPEED_RECORDS) ? 0 :
510
41.8k
             ((i_latest + 1) % CURL_SPEED_RECORDS);
511
512
  /* How much we transferred between oldest and current records */
513
41.8k
  amount = p->speed_amount[i_latest] - p->speed_amount[i_oldest];
514
  /* How long this took */
515
41.8k
  duration_us = p->speed_time[i_latest] - p->speed_time[i_oldest];
516
41.8k
  if(duration_us <= 0)
517
41.8k
    duration_us = 1;
518
519
41.8k
  if(amount > (CURL_OFF_T_MAX / 1000000)) {
520
    /* the 'amount' value is bigger than would fit in 64 bits if
521
       multiplied with 1000000, so we use the double math for this */
522
0
    p->current_speed =
523
0
      (curl_off_t)(((double)amount * 1000000.0) / (double)duration_us);
524
0
  }
525
41.8k
  else {
526
41.8k
    p->current_speed = amount * 1000000 / duration_us;
527
41.8k
  }
528
529
41.8k
  if((p->delta.lastshow_us >= 0) && !data->req.done &&
530
0
     ((elapsed_us - p->delta.lastshow_us) < (1000 * 1000)))
531
0
    return FALSE;
532
41.8k
  p->delta.lastshow_us = elapsed_us;
533
41.8k
  return TRUE;
534
41.8k
}
535
536
#ifndef CURL_DISABLE_PROGRESS_METER
537
538
struct pgrs_estimate {
539
  curl_off_t secs;
540
  curl_off_t percent;
541
};
542
543
static curl_off_t pgrs_est_percent(curl_off_t total, curl_off_t cur)
544
3.69k
{
545
3.69k
  if(total > 10000)
546
400
    return cur / (total / 100);
547
3.29k
  else if(total > 0)
548
1.48k
    return (cur * 100) / total;
549
1.80k
  return 0;
550
3.69k
}
551
552
static void pgrs_estimates(struct pgrs_dir *d,
553
                           bool total_known,
554
                           struct pgrs_estimate *est)
555
5.89k
{
556
5.89k
  est->secs = 0;
557
5.89k
  est->percent = 0;
558
5.89k
  if(total_known && (d->speed > 0)) {
559
746
    est->secs = d->total_size / d->speed;
560
746
    est->percent = pgrs_est_percent(d->total_size, d->cur_size);
561
746
  }
562
5.89k
}
563
564
static void progress_meter(struct Curl_easy *data)
565
2.94k
{
566
2.94k
  struct Progress *p = &data->progress;
567
2.94k
  char max6[6][7];
568
2.94k
  struct pgrs_estimate dl_estm;
569
2.94k
  struct pgrs_estimate ul_estm;
570
2.94k
  struct pgrs_estimate total_estm;
571
2.94k
  curl_off_t total_cur_size;
572
2.94k
  curl_off_t total_expected_size;
573
2.94k
  curl_off_t dl_size;
574
2.94k
  char time_left[8];
575
2.94k
  char time_total[8];
576
2.94k
  char time_spent[8];
577
2.94k
  curl_off_t cur_secs = (curl_off_t)p->total.spent_us / 1000000;
578
579
2.94k
  if(!p->headers_out) {
580
292
    if(data->state.resume_from) {
581
43
      curl_mfprintf(data->set.err,
582
43
                    "** Resuming transfer from byte position %" FMT_OFF_T "\n",
583
43
                    data->state.resume_from);
584
43
    }
585
292
    curl_mfprintf(data->set.err,
586
292
                  "  %% Total    %% Received %% Xferd  Average Speed  "
587
292
                  "Time    Time    Time   Current\n"
588
292
                  "                                 Dload  Upload  "
589
292
                  "Total   Spent   Left   Speed\n");
590
292
    p->headers_out = TRUE; /* headers are shown */
591
292
  }
592
593
  /* Figure out the estimated time of arrival for upload and download */
594
2.94k
  pgrs_estimates(&p->ul, (bool)p->ul_size_known, &ul_estm);
595
2.94k
  pgrs_estimates(&p->dl, (bool)p->dl_size_known, &dl_estm);
596
597
  /* Since both happen at the same time, total expected duration is max. */
598
2.94k
  total_estm.secs = CURLMAX(ul_estm.secs, dl_estm.secs);
599
  /* create the three time strings */
600
2.94k
  time2str(time_left, sizeof(time_left),
601
2.94k
           total_estm.secs > 0 ? (total_estm.secs - cur_secs) : 0);
602
2.94k
  time2str(time_total, sizeof(time_total), total_estm.secs);
603
2.94k
  time2str(time_spent, sizeof(time_spent), cur_secs);
604
605
  /* Get the total amount of data expected to get transferred */
606
2.94k
  total_expected_size = p->ul_size_known ? p->ul.total_size : p->ul.cur_size;
607
608
2.94k
  dl_size = p->dl_size_known ? p->dl.total_size : p->dl.cur_size;
609
610
  /* integer overflow check */
611
2.94k
  if((CURL_OFF_T_MAX - total_expected_size) < dl_size)
612
1
    total_expected_size = CURL_OFF_T_MAX; /* capped */
613
2.94k
  else
614
2.94k
    total_expected_size += dl_size;
615
616
  /* We have transferred this much so far */
617
2.94k
  total_cur_size = p->dl.cur_size + p->ul.cur_size;
618
619
  /* Get the percentage of data transferred so far */
620
2.94k
  total_estm.percent = pgrs_est_percent(total_expected_size, total_cur_size);
621
622
2.94k
  curl_mfprintf(data->set.err,
623
2.94k
                "\r"
624
2.94k
                "%3" FMT_OFF_T " %s "
625
2.94k
                "%3" FMT_OFF_T " %s "
626
2.94k
                "%3" FMT_OFF_T " %s %s %s %s %s %s %s",
627
2.94k
                total_estm.percent, /* 3 letters */    /* total % */
628
2.94k
                max6out(total_expected_size, max6[2],
629
2.94k
                        sizeof(max6[2])),              /* total size */
630
2.94k
                dl_estm.percent, /* 3 letters */       /* rcvd % */
631
2.94k
                max6out(p->dl.cur_size, max6[0],
632
2.94k
                        sizeof(max6[0])),              /* rcvd size */
633
2.94k
                ul_estm.percent, /* 3 letters */       /* xfer % */
634
2.94k
                max6out(p->ul.cur_size, max6[1],
635
2.94k
                        sizeof(max6[1])),              /* xfer size */
636
2.94k
                max6out(p->dl.speed, max6[3],
637
2.94k
                        sizeof(max6[3])),              /* avrg dl speed */
638
2.94k
                max6out(p->ul.speed, max6[4],
639
2.94k
                        sizeof(max6[4])),              /* avrg ul speed */
640
2.94k
                time_total,    /* 7 letters */         /* total time */
641
2.94k
                time_spent,    /* 7 letters */         /* time spent */
642
2.94k
                time_left,     /* 7 letters */         /* time left */
643
2.94k
                max6out(p->current_speed, max6[5],
644
2.94k
                        sizeof(max6[5]))               /* current speed */
645
2.94k
    );
646
647
  /* we flush the output stream to make it appear as soon as possible */
648
2.94k
  fflush(data->set.err);
649
2.94k
}
650
#else /* CURL_DISABLE_PROGRESS_METER */
651
#define progress_meter(x) Curl_nop_stmt
652
#endif
653
654
/*
655
 * Curl_pgrsUpdate() returns 0 for success or the value returned by the
656
 * progress callback!
657
 */
658
static CURLcode pgrsupdate(struct Curl_easy *data, bool showprogress)
659
435k
{
660
435k
  if(!data->progress.hide) {
661
8.14k
    int rc;
662
8.14k
    if(data->set.fxferinfo) {
663
      /* There is a callback set, call that */
664
0
      struct Curl_mapi_guard guard;
665
0
      CURL_CBAPI_START(&guard, data, easy_fxferinfo);
666
0
      rc = data->set.fxferinfo(data->set.progress_client,
667
0
                               data->progress.dl.total_size,
668
0
                               data->progress.dl.cur_size,
669
0
                               data->progress.ul.total_size,
670
0
                               data->progress.ul.cur_size);
671
0
      CURL_CBAPI_END(&guard);
672
0
      if(rc != CURL_PROGRESSFUNC_CONTINUE) {
673
0
        if(rc) {
674
0
          failf(data, "Callback aborted");
675
0
          return CURLE_ABORTED_BY_CALLBACK;
676
0
        }
677
0
        return CURLE_OK;
678
0
      }
679
0
    }
680
8.14k
    else if(data->set.fprogress) {
681
      /* The older deprecated callback is set, call that */
682
0
      struct Curl_mapi_guard guard;
683
0
      CURL_CBAPI_START(&guard, data, easy_fprogress);
684
0
      rc = data->set.fprogress(data->set.progress_client,
685
0
                               (double)data->progress.dl.total_size,
686
0
                               (double)data->progress.dl.cur_size,
687
0
                               (double)data->progress.ul.total_size,
688
0
                               (double)data->progress.ul.cur_size);
689
0
      CURL_CBAPI_END(&guard);
690
0
      if(rc != CURL_PROGRESSFUNC_CONTINUE) {
691
0
        if(rc) {
692
0
          failf(data, "Callback aborted");
693
0
          return CURLE_ABORTED_BY_CALLBACK;
694
0
        }
695
0
        return CURLE_OK;
696
0
      }
697
0
    }
698
699
8.14k
    if(showprogress)
700
2.94k
      progress_meter(data);
701
8.14k
  }
702
703
435k
  return CURLE_OK;
704
435k
}
705
706
static CURLcode pgrs_update(struct Curl_easy *data,
707
                            const struct curltime *pnow)
708
435k
{
709
435k
  bool showprogress = progress_calc(data, pnow);
710
435k
  return pgrsupdate(data, showprogress);
711
435k
}
712
713
CURLcode Curl_pgrsUpdate(struct Curl_easy *data)
714
274k
{
715
274k
  return pgrs_update(data, Curl_pgrs_now(data));
716
274k
}
717
718
CURLcode Curl_pgrsUpdateX(struct Curl_easy *data,
719
                          const struct curltime *pnow)
720
132k
{
721
132k
  return pgrs_update(data, pnow);
722
132k
}
723
724
CURLcode Curl_pgrsCheckX(struct Curl_easy *data,
725
                         const struct curltime *pnow)
726
28.5k
{
727
28.5k
  CURLcode result;
728
729
28.5k
  result = pgrs_update(data, pnow);
730
28.5k
  if(!result && !data->req.done)
731
11.6k
    result = pgrs_speedcheck(data, pnow);
732
28.5k
  return result;
733
28.5k
}
734
735
CURLcode Curl_pgrsCheck(struct Curl_easy *data)
736
227
{
737
227
  return Curl_pgrsCheckX(data, Curl_pgrs_now(data));
738
227
}
739
740
/*
741
 * Update all progress, do not do progress meter/callbacks.
742
 */
743
void Curl_pgrsUpdate_nometer(struct Curl_easy *data)
744
1.01k
{
745
1.01k
  (void)progress_calc(data, Curl_pgrs_now(data));
746
1.01k
}
747
748
void Curl_pgrsCompleted(struct Curl_easy *data)
749
20.4k
{
750
20.4k
  struct Progress * const p = &data->progress;
751
20.4k
  p->total.spent_us = curlx_ptimediff_us(Curl_pgrs_now(data), &p->start);
752
20.4k
}
753
754
timediff_t Curl_pgrs_since_ms(struct Curl_easy *data,
755
                              const struct curltime *pnow,
756
                              timerid timer)
757
378k
{
758
378k
  if(!pnow)
759
1.09k
    pnow = Curl_pgrs_now(data);
760
378k
  switch(timer) {
761
245k
  case TIMER_STARTOP:
762
245k
    return (curlx_ptimediff_us(pnow, &data->progress.start) -
763
245k
            data->progress.delta.startop_us) / 1000;
764
133k
  case TIMER_STARTSINGLE:
765
133k
    return (curlx_ptimediff_us(pnow, &data->progress.start) -
766
133k
            data->progress.delta.startsingle_us) / 1000;
767
0
  default:
768
0
    DEBUGASSERT(0);
769
0
    return 0;
770
378k
  }
771
378k
}