progress.c 21 KB

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