progress.c 21 KB

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