Coverage Report

Created: 2026-05-30 06:06

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