2
0

progress.c 21 KB

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