progress.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "urldata.h"
  24. #include "sendf.h"
  25. #include "multiif.h"
  26. #include "progress.h"
  27. #include "timeval.h"
  28. #include "curl_printf.h"
  29. /* check rate limits within this many recent milliseconds, at minimum. */
  30. #define MIN_RATE_LIMIT_PERIOD 3000
  31. #ifndef CURL_DISABLE_PROGRESS_METER
  32. /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
  33. byte) */
  34. static void time2str(char *r, curl_off_t seconds)
  35. {
  36. curl_off_t h;
  37. if(seconds <= 0) {
  38. strcpy(r, "--:--:--");
  39. return;
  40. }
  41. h = seconds / CURL_OFF_T_C(3600);
  42. if(h <= CURL_OFF_T_C(99)) {
  43. curl_off_t m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
  44. curl_off_t s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
  45. msnprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
  46. ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
  47. }
  48. else {
  49. /* this equals to more than 99 hours, switch to a more suitable output
  50. format to fit within the limits. */
  51. curl_off_t d = seconds / CURL_OFF_T_C(86400);
  52. h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
  53. if(d <= CURL_OFF_T_C(999))
  54. msnprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
  55. "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
  56. else
  57. msnprintf(r, 9, "%7" CURL_FORMAT_CURL_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" CURL_FORMAT_CURL_OFF_T, bytes);
  72. else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
  73. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_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're less than 100 megs */
  76. msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
  77. CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
  78. (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
  79. #if (SIZEOF_CURL_OFF_T > 4)
  80. else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
  81. /* 'XXXXM' is good until we're at 10000MB or above */
  82. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
  83. else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
  84. /* 10000 MB - 100 GB, we show it as XX.XG */
  85. msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
  86. CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
  87. (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
  88. else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
  89. /* up to 10000GB, display without decimal: XXXXG */
  90. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
  91. else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
  92. /* up to 10000TB, display without decimal: XXXXT */
  93. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
  94. else
  95. /* up to 10000PB, display without decimal: XXXXP */
  96. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
  97. /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
  98. can hold, but our data type is signed so 8192PB will be the maximum. */
  99. #else
  100. else
  101. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
  102. #endif
  103. return max5;
  104. }
  105. #endif
  106. /*
  107. New proposed interface, 9th of February 2000:
  108. pgrsStartNow() - sets start time
  109. pgrsSetDownloadSize(x) - known expected download size
  110. pgrsSetUploadSize(x) - known expected upload size
  111. pgrsSetDownloadCounter() - amount of data currently downloaded
  112. pgrsSetUploadCounter() - amount of data currently uploaded
  113. pgrsUpdate() - show progress
  114. pgrsDone() - transfer complete
  115. */
  116. int Curl_pgrsDone(struct Curl_easy *data)
  117. {
  118. int rc;
  119. data->progress.lastshow = 0;
  120. rc = Curl_pgrsUpdate(data); /* the final (forced) update */
  121. if(rc)
  122. return rc;
  123. if(!(data->progress.flags & PGRS_HIDE) &&
  124. !data->progress.callback)
  125. /* only output if we don't use a progress callback and we're not
  126. * hidden */
  127. fprintf(data->set.err, "\n");
  128. data->progress.speeder_c = 0; /* reset the progress meter display */
  129. return 0;
  130. }
  131. /* reset the known transfer sizes */
  132. void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
  133. {
  134. Curl_pgrsSetDownloadSize(data, -1);
  135. Curl_pgrsSetUploadSize(data, -1);
  136. }
  137. /*
  138. *
  139. * Curl_pgrsTime(). Store the current time at the given label. This fetches a
  140. * fresh "now" and returns it.
  141. *
  142. * @unittest: 1399
  143. */
  144. struct curltime Curl_pgrsTime(struct Curl_easy *data, timerid timer)
  145. {
  146. struct curltime now = Curl_now();
  147. timediff_t *delta = NULL;
  148. switch(timer) {
  149. default:
  150. case TIMER_NONE:
  151. /* mistake filter */
  152. break;
  153. case TIMER_STARTOP:
  154. /* This is set at the start of a transfer */
  155. data->progress.t_startop = now;
  156. break;
  157. case TIMER_STARTSINGLE:
  158. /* This is set at the start of each single fetch */
  159. data->progress.t_startsingle = now;
  160. data->progress.is_t_startransfer_set = false;
  161. break;
  162. case TIMER_STARTACCEPT:
  163. data->progress.t_acceptdata = now;
  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're 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 now;
  187. }
  188. else {
  189. data->progress.is_t_startransfer_set = true;
  190. break;
  191. }
  192. case TIMER_POSTRANSFER:
  193. /* this is the normal end-of-transfer thing */
  194. break;
  195. case TIMER_REDIRECT:
  196. data->progress.t_redirect = Curl_timediff_us(now, data->progress.start);
  197. break;
  198. }
  199. if(delta) {
  200. timediff_t us = Curl_timediff_us(now, data->progress.t_startsingle);
  201. if(us < 1)
  202. us = 1; /* make sure at least one microsecond passed */
  203. *delta += us;
  204. }
  205. return now;
  206. }
  207. void Curl_pgrsStartNow(struct Curl_easy *data)
  208. {
  209. data->progress.speeder_c = 0; /* reset the progress meter display */
  210. data->progress.start = Curl_now();
  211. data->progress.is_t_startransfer_set = false;
  212. data->progress.ul_limit_start = data->progress.start;
  213. data->progress.dl_limit_start = data->progress.start;
  214. data->progress.ul_limit_size = 0;
  215. data->progress.dl_limit_size = 0;
  216. data->progress.downloaded = 0;
  217. data->progress.uploaded = 0;
  218. /* clear all bits except HIDE and HEADERS_OUT */
  219. data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
  220. Curl_ratelimit(data, data->progress.start);
  221. }
  222. /*
  223. * This is used to handle speed limits, calculating how many milliseconds to
  224. * wait until we're back under the speed limit, if needed.
  225. *
  226. * The way it works is by having a "starting point" (time & amount of data
  227. * transferred by then) used in the speed computation, to be used instead of
  228. * the start of the transfer. This starting point is regularly moved as
  229. * transfer goes on, to keep getting accurate values (instead of average over
  230. * the entire transfer).
  231. *
  232. * This function takes the current amount of data transferred, the amount at
  233. * the starting point, the limit (in bytes/s), the time of the starting point
  234. * and the current time.
  235. *
  236. * Returns 0 if no waiting is needed or when no waiting is needed but the
  237. * starting point should be reset (to current); or the number of milliseconds
  238. * to wait to get back under the speed limit.
  239. */
  240. timediff_t Curl_pgrsLimitWaitTime(curl_off_t cursize,
  241. curl_off_t startsize,
  242. curl_off_t limit,
  243. struct curltime start,
  244. struct curltime now)
  245. {
  246. curl_off_t size = cursize - startsize;
  247. timediff_t minimum;
  248. timediff_t actual;
  249. if(!limit || !size)
  250. return 0;
  251. /*
  252. * 'minimum' is the number of milliseconds 'size' should take to download to
  253. * stay below 'limit'.
  254. */
  255. if(size < CURL_OFF_T_MAX/1000)
  256. minimum = (timediff_t) (CURL_OFF_T_C(1000) * size / limit);
  257. else {
  258. minimum = (timediff_t) (size / limit);
  259. if(minimum < TIMEDIFF_T_MAX/1000)
  260. minimum *= 1000;
  261. else
  262. minimum = TIMEDIFF_T_MAX;
  263. }
  264. /*
  265. * 'actual' is the time in milliseconds it took to actually download the
  266. * last 'size' bytes.
  267. */
  268. actual = Curl_timediff(now, start);
  269. if(actual < minimum) {
  270. /* if it downloaded the data faster than the limit, make it wait the
  271. difference */
  272. return (minimum - actual);
  273. }
  274. return 0;
  275. }
  276. /*
  277. * Set the number of downloaded bytes so far.
  278. */
  279. void Curl_pgrsSetDownloadCounter(struct Curl_easy *data, curl_off_t size)
  280. {
  281. data->progress.downloaded = size;
  282. }
  283. /*
  284. * Update the timestamp and sizestamp to use for rate limit calculations.
  285. */
  286. void Curl_ratelimit(struct Curl_easy *data, struct curltime now)
  287. {
  288. /* don't set a new stamp unless the time since last update is long enough */
  289. if(data->set.max_recv_speed) {
  290. if(Curl_timediff(now, data->progress.dl_limit_start) >=
  291. MIN_RATE_LIMIT_PERIOD) {
  292. data->progress.dl_limit_start = now;
  293. data->progress.dl_limit_size = data->progress.downloaded;
  294. }
  295. }
  296. if(data->set.max_send_speed) {
  297. if(Curl_timediff(now, data->progress.ul_limit_start) >=
  298. MIN_RATE_LIMIT_PERIOD) {
  299. data->progress.ul_limit_start = now;
  300. data->progress.ul_limit_size = data->progress.uploaded;
  301. }
  302. }
  303. }
  304. /*
  305. * Set the number of uploaded bytes so far.
  306. */
  307. void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
  308. {
  309. data->progress.uploaded = size;
  310. }
  311. void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
  312. {
  313. if(size >= 0) {
  314. data->progress.size_dl = size;
  315. data->progress.flags |= PGRS_DL_SIZE_KNOWN;
  316. }
  317. else {
  318. data->progress.size_dl = 0;
  319. data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
  320. }
  321. }
  322. void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
  323. {
  324. if(size >= 0) {
  325. data->progress.size_ul = size;
  326. data->progress.flags |= PGRS_UL_SIZE_KNOWN;
  327. }
  328. else {
  329. data->progress.size_ul = 0;
  330. data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
  331. }
  332. }
  333. /* returns the average speed in bytes / second */
  334. static curl_off_t trspeed(curl_off_t size, /* number of bytes */
  335. curl_off_t us) /* microseconds */
  336. {
  337. if(us < 1)
  338. return size * 1000000;
  339. return (curl_off_t)((long double)size/us * 1000000);
  340. }
  341. /* returns TRUE if it's time to show the progress meter */
  342. static bool progress_calc(struct Curl_easy *data, struct curltime now)
  343. {
  344. bool timetoshow = FALSE;
  345. struct Progress * const p = &data->progress;
  346. /* The time spent so far (from the start) in microseconds */
  347. p->timespent = Curl_timediff_us(now, p->start);
  348. p->dlspeed = trspeed(p->downloaded, p->timespent);
  349. p->ulspeed = trspeed(p->uploaded, p->timespent);
  350. /* Calculations done at most once a second, unless end is reached */
  351. if(p->lastshow != now.tv_sec) {
  352. int countindex; /* amount of seconds stored in the speeder array */
  353. int nowindex = p->speeder_c% CURR_TIME;
  354. p->lastshow = now.tv_sec;
  355. timetoshow = TRUE;
  356. /* Let's do the "current speed" thing, with the dl + ul speeds
  357. combined. Store the speed at entry 'nowindex'. */
  358. p->speeder[ nowindex ] = p->downloaded + p->uploaded;
  359. /* remember the exact time for this moment */
  360. p->speeder_time [ nowindex ] = now;
  361. /* advance our speeder_c counter, which is increased every time we get
  362. here and we expect it to never wrap as 2^32 is a lot of seconds! */
  363. p->speeder_c++;
  364. /* figure out how many index entries of data we have stored in our speeder
  365. array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
  366. transfer. Imagine, after one second we have filled in two entries,
  367. after two seconds we've filled in three entries etc. */
  368. countindex = ((p->speeder_c >= CURR_TIME)? CURR_TIME:p->speeder_c) - 1;
  369. /* first of all, we don't do this if there's no counted seconds yet */
  370. if(countindex) {
  371. int checkindex;
  372. timediff_t span_ms;
  373. curl_off_t amount;
  374. /* Get the index position to compare with the 'nowindex' position.
  375. Get the oldest entry possible. While we have less than CURR_TIME
  376. entries, the first entry will remain the oldest. */
  377. checkindex = (p->speeder_c >= CURR_TIME)? p->speeder_c%CURR_TIME:0;
  378. /* Figure out the exact time for the time span */
  379. span_ms = Curl_timediff(now, p->speeder_time[checkindex]);
  380. if(0 == span_ms)
  381. span_ms = 1; /* at least one millisecond MUST have passed */
  382. /* Calculate the average speed the last 'span_ms' milliseconds */
  383. amount = p->speeder[nowindex]- p->speeder[checkindex];
  384. if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
  385. /* the 'amount' value is bigger than would fit in 32 bits if
  386. multiplied with 1000, so we use the double math for this */
  387. p->current_speed = (curl_off_t)
  388. ((double)amount/((double)span_ms/1000.0));
  389. else
  390. /* the 'amount' value is small enough to fit within 32 bits even
  391. when multiplied with 1000 */
  392. p->current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
  393. }
  394. else
  395. /* the first second we use the average */
  396. p->current_speed = p->ulspeed + p->dlspeed;
  397. } /* Calculations end */
  398. return timetoshow;
  399. }
  400. #ifndef CURL_DISABLE_PROGRESS_METER
  401. static void progress_meter(struct Curl_easy *data)
  402. {
  403. char max5[6][10];
  404. curl_off_t dlpercen = 0;
  405. curl_off_t ulpercen = 0;
  406. curl_off_t total_percen = 0;
  407. curl_off_t total_transfer;
  408. curl_off_t total_expected_transfer;
  409. char time_left[10];
  410. char time_total[10];
  411. char time_spent[10];
  412. curl_off_t ulestimate = 0;
  413. curl_off_t dlestimate = 0;
  414. curl_off_t total_estimate;
  415. curl_off_t timespent =
  416. (curl_off_t)data->progress.timespent/1000000; /* seconds */
  417. if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
  418. if(data->state.resume_from) {
  419. fprintf(data->set.err,
  420. "** Resuming transfer from byte position %"
  421. CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
  422. }
  423. fprintf(data->set.err,
  424. " %% Total %% Received %% Xferd Average Speed "
  425. "Time Time Time Current\n"
  426. " Dload Upload "
  427. "Total Spent Left Speed\n");
  428. data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
  429. }
  430. /* Figure out the estimated time of arrival for the upload */
  431. if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
  432. (data->progress.ulspeed > CURL_OFF_T_C(0))) {
  433. ulestimate = data->progress.size_ul / data->progress.ulspeed;
  434. if(data->progress.size_ul > CURL_OFF_T_C(10000))
  435. ulpercen = data->progress.uploaded /
  436. (data->progress.size_ul/CURL_OFF_T_C(100));
  437. else if(data->progress.size_ul > CURL_OFF_T_C(0))
  438. ulpercen = (data->progress.uploaded*100) /
  439. data->progress.size_ul;
  440. }
  441. /* ... and the download */
  442. if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
  443. (data->progress.dlspeed > CURL_OFF_T_C(0))) {
  444. dlestimate = data->progress.size_dl / data->progress.dlspeed;
  445. if(data->progress.size_dl > CURL_OFF_T_C(10000))
  446. dlpercen = data->progress.downloaded /
  447. (data->progress.size_dl/CURL_OFF_T_C(100));
  448. else if(data->progress.size_dl > CURL_OFF_T_C(0))
  449. dlpercen = (data->progress.downloaded*100) /
  450. data->progress.size_dl;
  451. }
  452. /* Now figure out which of them is slower and use that one for the
  453. total estimate! */
  454. total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
  455. /* create the three time strings */
  456. time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
  457. time2str(time_total, total_estimate);
  458. time2str(time_spent, timespent);
  459. /* Get the total amount of data expected to get transferred */
  460. total_expected_transfer =
  461. ((data->progress.flags & PGRS_UL_SIZE_KNOWN)?
  462. data->progress.size_ul:data->progress.uploaded)+
  463. ((data->progress.flags & PGRS_DL_SIZE_KNOWN)?
  464. data->progress.size_dl:data->progress.downloaded);
  465. /* We have transferred this much so far */
  466. total_transfer = data->progress.downloaded + data->progress.uploaded;
  467. /* Get the percentage of data transferred so far */
  468. if(total_expected_transfer > CURL_OFF_T_C(10000))
  469. total_percen = total_transfer /
  470. (total_expected_transfer/CURL_OFF_T_C(100));
  471. else if(total_expected_transfer > CURL_OFF_T_C(0))
  472. total_percen = (total_transfer*100) / total_expected_transfer;
  473. fprintf(data->set.err,
  474. "\r"
  475. "%3" CURL_FORMAT_CURL_OFF_T " %s "
  476. "%3" CURL_FORMAT_CURL_OFF_T " %s "
  477. "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
  478. total_percen, /* 3 letters */ /* total % */
  479. max5data(total_expected_transfer, max5[2]), /* total size */
  480. dlpercen, /* 3 letters */ /* rcvd % */
  481. max5data(data->progress.downloaded, max5[0]), /* rcvd size */
  482. ulpercen, /* 3 letters */ /* xfer % */
  483. max5data(data->progress.uploaded, max5[1]), /* xfer size */
  484. max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
  485. max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
  486. time_total, /* 8 letters */ /* total time */
  487. time_spent, /* 8 letters */ /* time spent */
  488. time_left, /* 8 letters */ /* time left */
  489. max5data(data->progress.current_speed, max5[5])
  490. );
  491. /* we flush the output stream to make it appear as soon as possible */
  492. fflush(data->set.err);
  493. }
  494. #else
  495. /* progress bar disabled */
  496. #define progress_meter(x) Curl_nop_stmt
  497. #endif
  498. /*
  499. * Curl_pgrsUpdate() returns 0 for success or the value returned by the
  500. * progress callback!
  501. */
  502. int Curl_pgrsUpdate(struct Curl_easy *data)
  503. {
  504. struct curltime now = Curl_now(); /* what time is it */
  505. bool showprogress = progress_calc(data, now);
  506. if(!(data->progress.flags & PGRS_HIDE)) {
  507. if(data->set.fxferinfo) {
  508. int result;
  509. /* There's a callback set, call that */
  510. Curl_set_in_callback(data, true);
  511. result = data->set.fxferinfo(data->set.progress_client,
  512. data->progress.size_dl,
  513. data->progress.downloaded,
  514. data->progress.size_ul,
  515. data->progress.uploaded);
  516. Curl_set_in_callback(data, false);
  517. if(result != CURL_PROGRESSFUNC_CONTINUE) {
  518. if(result)
  519. failf(data, "Callback aborted");
  520. return result;
  521. }
  522. }
  523. else if(data->set.fprogress) {
  524. int result;
  525. /* The older deprecated callback is set, call that */
  526. Curl_set_in_callback(data, true);
  527. result = data->set.fprogress(data->set.progress_client,
  528. (double)data->progress.size_dl,
  529. (double)data->progress.downloaded,
  530. (double)data->progress.size_ul,
  531. (double)data->progress.uploaded);
  532. Curl_set_in_callback(data, false);
  533. if(result != CURL_PROGRESSFUNC_CONTINUE) {
  534. if(result)
  535. failf(data, "Callback aborted");
  536. return result;
  537. }
  538. }
  539. if(showprogress)
  540. progress_meter(data);
  541. }
  542. return 0;
  543. }