progress.c 21 KB

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