progress.c 21 KB

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