progress.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "urldata.h"
  26. #include "curl_trc.h"
  27. #include "multiif.h"
  28. #include "progress.h"
  29. #include "transfer.h"
  30. #include "curlx/strcopy.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 7 letters long (plus the zero byte).
  35. Unit test 1636.
  36. */
  37. UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds);
  38. UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds)
  39. {
  40. curl_off_t h;
  41. if(seconds <= 0) {
  42. curlx_strcopy(r, rsize, " ", 7);
  43. return;
  44. }
  45. h = seconds / 3600;
  46. if(h <= 99) {
  47. curl_off_t m = (seconds - (h * 3600)) / 60;
  48. if(h <= 9) {
  49. curl_off_t s = (seconds - (h * 3600)) - (m * 60);
  50. if(h)
  51. curl_msnprintf(r, rsize, "%" FMT_OFF_T ":%02" FMT_OFF_T ":"
  52. "%02" FMT_OFF_T, h, m, s);
  53. else
  54. curl_msnprintf(r, rsize, " %02" FMT_OFF_T ":%02" FMT_OFF_T, m, s);
  55. }
  56. else
  57. curl_msnprintf(r, rsize, "%" FMT_OFF_T "h %02" FMT_OFF_T "m", h, m);
  58. }
  59. else {
  60. curl_off_t d = seconds / 86400;
  61. h = (seconds - (d * 86400)) / 3600;
  62. if(d <= 99)
  63. curl_msnprintf(r, rsize, "%2" FMT_OFF_T "d %02" FMT_OFF_T "h", d, h);
  64. else if(d <= 999)
  65. curl_msnprintf(r, rsize, "%6" FMT_OFF_T "d", d);
  66. else { /* more than 999 days */
  67. curl_off_t m = d / 30;
  68. if(m <= 999)
  69. curl_msnprintf(r, rsize, "%6" FMT_OFF_T "m", m);
  70. else { /* more than 999 months */
  71. curl_off_t y = d / 365;
  72. if(y <= 99999)
  73. curl_msnprintf(r, rsize, "%6" FMT_OFF_T "y", y);
  74. else
  75. curlx_strcopy(r, rsize, ">99999y", 7);
  76. }
  77. }
  78. }
  79. }
  80. /* The point of this function would be to return a string of the input data,
  81. but never longer than 6 columns (+ one zero byte).
  82. Add suffix k, M, G when suitable...
  83. Unit test 1636
  84. */
  85. UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen);
  86. UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen)
  87. {
  88. /* a signed 64-bit value is 8192 petabytes maximum, shown as
  89. 8.0E (exabytes)*/
  90. if(bytes < 100000)
  91. curl_msnprintf(max6, mlen, "%6" CURL_FORMAT_CURL_OFF_T, bytes);
  92. else {
  93. const char unit[] = { 'k', 'M', 'G', 'T', 'P', 'E', 0 };
  94. int k = 0;
  95. curl_off_t nbytes;
  96. curl_off_t rest;
  97. do {
  98. nbytes = bytes / 1024;
  99. if(nbytes < 1000)
  100. break;
  101. bytes = nbytes;
  102. k++;
  103. DEBUGASSERT(unit[k]);
  104. } while(unit[k]);
  105. rest = bytes % 1024;
  106. if(nbytes <= 99)
  107. /* xx.yyU */
  108. curl_msnprintf(max6, mlen, "%2" CURL_FORMAT_CURL_OFF_T
  109. ".%02" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
  110. rest * 100 / 1024, unit[k]);
  111. else
  112. /* xxx.yU */
  113. curl_msnprintf(max6, mlen, "%3" CURL_FORMAT_CURL_OFF_T
  114. ".%" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
  115. rest * 10 / 1024, unit[k]);
  116. }
  117. return max6;
  118. }
  119. #endif
  120. static void pgrs_speedinit(struct Curl_easy *data)
  121. {
  122. memset(&data->state.keeps_speed, 0, sizeof(struct curltime));
  123. }
  124. /*
  125. * @unittest: 1606
  126. */
  127. UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
  128. const struct curltime *pnow)
  129. {
  130. if(!data->set.low_speed_time || !data->set.low_speed_limit ||
  131. Curl_xfer_recv_is_paused(data) || Curl_xfer_send_is_paused(data))
  132. /* A paused transfer is not qualified for speed checks */
  133. return CURLE_OK;
  134. if(data->progress.current_speed >= 0) {
  135. if(data->progress.current_speed < data->set.low_speed_limit) {
  136. if(!data->state.keeps_speed.tv_sec)
  137. /* under the limit at this moment */
  138. data->state.keeps_speed = *pnow;
  139. else {
  140. /* how long has it been under the limit */
  141. timediff_t howlong =
  142. curlx_ptimediff_ms(pnow, &data->state.keeps_speed);
  143. if(howlong >= data->set.low_speed_time * 1000) {
  144. /* too long */
  145. failf(data,
  146. "Operation too slow. "
  147. "Less than %ld bytes/sec transferred the last %ld seconds",
  148. data->set.low_speed_limit,
  149. data->set.low_speed_time);
  150. return CURLE_OPERATION_TIMEDOUT;
  151. }
  152. }
  153. }
  154. else
  155. /* faster right now */
  156. data->state.keeps_speed.tv_sec = 0;
  157. }
  158. /* since low speed limit is enabled, set the expire timer to make this
  159. connection's speed get checked again in a second */
  160. Curl_expire(data, 1000, EXPIRE_SPEEDCHECK);
  161. return CURLE_OK;
  162. }
  163. const struct curltime *Curl_pgrs_now(struct Curl_easy *data)
  164. {
  165. struct curltime *pnow = data->multi ?
  166. &data->multi->now : &data->progress.now;
  167. curlx_pnow(pnow);
  168. return pnow;
  169. }
  170. /*
  171. New proposed interface, 9th of February 2000:
  172. pgrsStartNow() - sets start time
  173. pgrsSetDownloadSize(x) - known expected download size
  174. pgrsSetUploadSize(x) - known expected upload size
  175. pgrsSetDownloadCounter() - amount of data currently downloaded
  176. pgrsSetUploadCounter() - amount of data currently uploaded
  177. pgrsUpdate() - show progress
  178. pgrsDone() - transfer complete
  179. */
  180. int Curl_pgrsDone(struct Curl_easy *data)
  181. {
  182. int rc;
  183. data->progress.lastshow = 0;
  184. rc = Curl_pgrsUpdate(data); /* the final (forced) update */
  185. if(rc)
  186. return rc;
  187. if(!data->progress.hide && !data->progress.callback)
  188. /* only output if we do not use a progress callback and we are not
  189. * hidden */
  190. curl_mfprintf(data->set.err, "\n");
  191. return 0;
  192. }
  193. void Curl_pgrsReset(struct Curl_easy *data)
  194. {
  195. Curl_pgrsSetUploadCounter(data, 0);
  196. data->progress.dl.cur_size = 0;
  197. Curl_pgrsSetUploadSize(data, -1);
  198. Curl_pgrsSetDownloadSize(data, -1);
  199. data->progress.speeder_c = 0; /* reset speed records */
  200. pgrs_speedinit(data);
  201. }
  202. /* reset the known transfer sizes */
  203. void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
  204. {
  205. Curl_pgrsSetDownloadSize(data, -1);
  206. Curl_pgrsSetUploadSize(data, -1);
  207. }
  208. void Curl_pgrsRecvPause(struct Curl_easy *data, bool enable)
  209. {
  210. if(!enable) {
  211. data->progress.speeder_c = 0; /* reset speed records */
  212. pgrs_speedinit(data); /* reset low speed measurements */
  213. }
  214. }
  215. void Curl_pgrsSendPause(struct Curl_easy *data, bool enable)
  216. {
  217. if(!enable) {
  218. data->progress.speeder_c = 0; /* reset speed records */
  219. pgrs_speedinit(data); /* reset low speed measurements */
  220. }
  221. }
  222. /*
  223. *
  224. * Curl_pgrsTimeWas(). Store the timestamp time at the given label.
  225. */
  226. void Curl_pgrsTimeWas(struct Curl_easy *data, timerid timer,
  227. struct curltime timestamp)
  228. {
  229. timediff_t *delta = NULL;
  230. switch(timer) {
  231. default:
  232. case TIMER_NONE:
  233. /* mistake filter */
  234. break;
  235. case TIMER_STARTOP:
  236. /* This is set at the start of a transfer */
  237. data->progress.t_startop = timestamp;
  238. data->progress.t_startqueue = timestamp;
  239. data->progress.t_postqueue = 0;
  240. break;
  241. case TIMER_STARTSINGLE:
  242. /* This is set at the start of each single transfer */
  243. data->progress.t_startsingle = timestamp;
  244. data->progress.is_t_startransfer_set = FALSE;
  245. break;
  246. case TIMER_POSTQUEUE:
  247. /* Queue time is accumulative from all involved redirects */
  248. data->progress.t_postqueue +=
  249. curlx_ptimediff_us(&timestamp, &data->progress.t_startqueue);
  250. break;
  251. case TIMER_STARTACCEPT:
  252. data->progress.t_acceptdata = timestamp;
  253. break;
  254. case TIMER_NAMELOOKUP:
  255. delta = &data->progress.t_nslookup;
  256. break;
  257. case TIMER_CONNECT:
  258. delta = &data->progress.t_connect;
  259. break;
  260. case TIMER_APPCONNECT:
  261. delta = &data->progress.t_appconnect;
  262. break;
  263. case TIMER_PRETRANSFER:
  264. delta = &data->progress.t_pretransfer;
  265. break;
  266. case TIMER_STARTTRANSFER:
  267. delta = &data->progress.t_starttransfer;
  268. /* prevent updating t_starttransfer unless:
  269. * 1) this is the first time we are setting t_starttransfer
  270. * 2) a redirect has occurred since the last time t_starttransfer was set
  271. * This prevents repeated invocations of the function from incorrectly
  272. * changing the t_starttransfer time.
  273. */
  274. if(data->progress.is_t_startransfer_set) {
  275. return;
  276. }
  277. else {
  278. data->progress.is_t_startransfer_set = TRUE;
  279. break;
  280. }
  281. case TIMER_POSTRANSFER:
  282. delta = &data->progress.t_posttransfer;
  283. break;
  284. case TIMER_REDIRECT:
  285. data->progress.t_redirect = curlx_ptimediff_us(&timestamp,
  286. &data->progress.start);
  287. data->progress.t_startqueue = timestamp;
  288. break;
  289. }
  290. if(delta) {
  291. timediff_t us = curlx_ptimediff_us(&timestamp,
  292. &data->progress.t_startsingle);
  293. if(us < 1)
  294. us = 1; /* make sure at least one microsecond passed */
  295. *delta += us;
  296. }
  297. }
  298. /*
  299. *
  300. * Curl_pgrsTime(). Store the current time at the given label. This fetches a
  301. * fresh "now" and returns it.
  302. *
  303. * @unittest: 1399
  304. */
  305. void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
  306. {
  307. Curl_pgrsTimeWas(data, timer, *Curl_pgrs_now(data));
  308. }
  309. void Curl_pgrsStartNow(struct Curl_easy *data)
  310. {
  311. struct Progress *p = &data->progress;
  312. p->speeder_c = 0; /* reset the progress meter display */
  313. p->start = *Curl_pgrs_now(data);
  314. p->is_t_startransfer_set = FALSE;
  315. p->dl.cur_size = 0;
  316. p->ul.cur_size = 0;
  317. /* the sizes are unknown at start */
  318. p->dl_size_known = FALSE;
  319. p->ul_size_known = FALSE;
  320. }
  321. void Curl_pgrs_download_inc(struct Curl_easy *data, size_t delta)
  322. {
  323. if(delta) {
  324. data->progress.dl.cur_size += delta;
  325. Curl_rlimit_drain(&data->progress.dl.rlimit, delta, Curl_pgrs_now(data));
  326. }
  327. }
  328. void Curl_pgrs_upload_inc(struct Curl_easy *data, size_t delta)
  329. {
  330. if(delta) {
  331. data->progress.ul.cur_size += delta;
  332. Curl_rlimit_drain(&data->progress.ul.rlimit, delta, Curl_pgrs_now(data));
  333. }
  334. }
  335. /*
  336. * Set the number of uploaded bytes so far.
  337. */
  338. void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
  339. {
  340. data->progress.ul.cur_size = size;
  341. }
  342. void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
  343. {
  344. if(size >= 0) {
  345. data->progress.dl.total_size = size;
  346. data->progress.dl_size_known = TRUE;
  347. }
  348. else {
  349. data->progress.dl.total_size = 0;
  350. data->progress.dl_size_known = FALSE;
  351. }
  352. }
  353. void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
  354. {
  355. if(size >= 0) {
  356. data->progress.ul.total_size = size;
  357. data->progress.ul_size_known = TRUE;
  358. }
  359. else {
  360. data->progress.ul.total_size = 0;
  361. data->progress.ul_size_known = FALSE;
  362. }
  363. }
  364. void Curl_pgrsEarlyData(struct Curl_easy *data, curl_off_t sent)
  365. {
  366. data->progress.earlydata_sent = sent;
  367. }
  368. /* returns the average speed in bytes / second */
  369. static curl_off_t trspeed(curl_off_t size, /* number of bytes */
  370. curl_off_t us) /* microseconds */
  371. {
  372. if(us < 1)
  373. return size * 1000000;
  374. else if(size < CURL_OFF_T_MAX / 1000000)
  375. return (size * 1000000) / us;
  376. else if(us >= 1000000)
  377. return size / (us / 1000000);
  378. else
  379. return CURL_OFF_T_MAX;
  380. }
  381. /* returns TRUE if it is time to show the progress meter */
  382. static bool progress_calc(struct Curl_easy *data,
  383. const struct curltime *pnow)
  384. {
  385. struct Progress * const p = &data->progress;
  386. int i_next, i_oldest, i_latest;
  387. timediff_t duration_ms;
  388. curl_off_t amount;
  389. /* The time spent so far (from the start) in microseconds */
  390. p->timespent = curlx_ptimediff_us(pnow, &p->start);
  391. p->dl.speed = trspeed(p->dl.cur_size, p->timespent);
  392. p->ul.speed = trspeed(p->ul.cur_size, p->timespent);
  393. if(!p->speeder_c) { /* no previous record exists */
  394. p->speed_amount[0] = p->dl.cur_size + p->ul.cur_size;
  395. p->speed_time[0] = *pnow;
  396. p->speeder_c++;
  397. /* use the overall average at the start */
  398. p->current_speed = p->ul.speed + p->dl.speed;
  399. p->lastshow = pnow->tv_sec;
  400. return TRUE;
  401. }
  402. /* We have at least one record now. Where to put the next and
  403. * where is the latest one? */
  404. i_next = p->speeder_c % CURL_SPEED_RECORDS;
  405. i_latest = (i_next > 0) ? (i_next - 1) : (CURL_SPEED_RECORDS - 1);
  406. /* Make a new record only when some time has passed.
  407. * Too frequent calls otherwise ruin the history. */
  408. if(curlx_ptimediff_ms(pnow, &p->speed_time[i_latest]) >= 1000) {
  409. p->speeder_c++;
  410. i_latest = i_next;
  411. p->speed_amount[i_latest] = p->dl.cur_size + p->ul.cur_size;
  412. p->speed_time[i_latest] = *pnow;
  413. }
  414. else if(data->req.done) {
  415. /* When a transfer is done, and we did not have a current speed
  416. * already, update the last record. Otherwise, stay at the speed
  417. * we have. The last chunk of data, when rate limiting, would increase
  418. * reported speed since it no longer measures a full second. */
  419. if(!p->current_speed) {
  420. p->speed_amount[i_latest] = p->dl.cur_size + p->ul.cur_size;
  421. p->speed_time[i_latest] = *pnow;
  422. }
  423. }
  424. else {
  425. /* transfer ongoing, wait for more time to pass. */
  426. return FALSE;
  427. }
  428. i_oldest = (p->speeder_c < CURL_SPEED_RECORDS) ? 0 :
  429. ((i_latest + 1) % CURL_SPEED_RECORDS);
  430. /* How much we transferred between oldest and current records */
  431. amount = p->speed_amount[i_latest] - p->speed_amount[i_oldest];
  432. /* How long this took */
  433. duration_ms = curlx_ptimediff_ms(&p->speed_time[i_latest],
  434. &p->speed_time[i_oldest]);
  435. if(duration_ms <= 0)
  436. duration_ms = 1;
  437. if(amount > (CURL_OFF_T_MAX / 1000)) {
  438. /* the 'amount' value is bigger than would fit in 64 bits if
  439. multiplied with 1000, so we use the double math for this */
  440. p->current_speed =
  441. (curl_off_t)(((double)amount * 1000.0) / (double)duration_ms);
  442. }
  443. else {
  444. /* the 'amount' value is small enough to fit within 32 bits even
  445. when multiplied with 1000 */
  446. p->current_speed = amount * 1000 / duration_ms;
  447. }
  448. if((p->lastshow == pnow->tv_sec) && !data->req.done)
  449. return FALSE;
  450. p->lastshow = pnow->tv_sec;
  451. return TRUE;
  452. }
  453. #ifndef CURL_DISABLE_PROGRESS_METER
  454. struct pgrs_estimate {
  455. curl_off_t secs;
  456. curl_off_t percent;
  457. };
  458. static curl_off_t pgrs_est_percent(curl_off_t total, curl_off_t cur)
  459. {
  460. if(total > 10000)
  461. return cur / (total / 100);
  462. else if(total > 0)
  463. return (cur * 100) / total;
  464. return 0;
  465. }
  466. static void pgrs_estimates(struct pgrs_dir *d,
  467. bool total_known,
  468. struct pgrs_estimate *est)
  469. {
  470. est->secs = 0;
  471. est->percent = 0;
  472. if(total_known && (d->speed > 0)) {
  473. est->secs = d->total_size / d->speed;
  474. est->percent = pgrs_est_percent(d->total_size, d->cur_size);
  475. }
  476. }
  477. static void progress_meter(struct Curl_easy *data)
  478. {
  479. struct Progress *p = &data->progress;
  480. char max6[6][7];
  481. struct pgrs_estimate dl_estm;
  482. struct pgrs_estimate ul_estm;
  483. struct pgrs_estimate total_estm;
  484. curl_off_t total_cur_size;
  485. curl_off_t total_expected_size;
  486. curl_off_t dl_size;
  487. char time_left[8];
  488. char time_total[8];
  489. char time_spent[8];
  490. curl_off_t cur_secs = (curl_off_t)p->timespent / 1000000; /* seconds */
  491. if(!p->headers_out) {
  492. if(data->state.resume_from) {
  493. curl_mfprintf(data->set.err,
  494. "** Resuming transfer from byte position %" FMT_OFF_T "\n",
  495. data->state.resume_from);
  496. }
  497. curl_mfprintf(data->set.err,
  498. " %% Total %% Received %% Xferd Average Speed "
  499. "Time Time Time Current\n"
  500. " Dload Upload "
  501. "Total Spent Left Speed\n");
  502. p->headers_out = TRUE; /* headers are shown */
  503. }
  504. /* Figure out the estimated time of arrival for upload and download */
  505. pgrs_estimates(&p->ul, (bool)p->ul_size_known, &ul_estm);
  506. pgrs_estimates(&p->dl, (bool)p->dl_size_known, &dl_estm);
  507. /* Since both happen at the same time, total expected duration is max. */
  508. total_estm.secs = CURLMAX(ul_estm.secs, dl_estm.secs);
  509. /* create the three time strings */
  510. time2str(time_left, sizeof(time_left),
  511. total_estm.secs > 0 ? (total_estm.secs - cur_secs) : 0);
  512. time2str(time_total, sizeof(time_total), total_estm.secs);
  513. time2str(time_spent, sizeof(time_spent), cur_secs);
  514. /* Get the total amount of data expected to get transferred */
  515. total_expected_size = p->ul_size_known ? p->ul.total_size : p->ul.cur_size;
  516. dl_size = p->dl_size_known ? p->dl.total_size : p->dl.cur_size;
  517. /* integer overflow check */
  518. if((CURL_OFF_T_MAX - total_expected_size) < dl_size)
  519. total_expected_size = CURL_OFF_T_MAX; /* capped */
  520. else
  521. total_expected_size += dl_size;
  522. /* We have transferred this much so far */
  523. total_cur_size = p->dl.cur_size + p->ul.cur_size;
  524. /* Get the percentage of data transferred so far */
  525. total_estm.percent = pgrs_est_percent(total_expected_size, total_cur_size);
  526. curl_mfprintf(data->set.err,
  527. "\r"
  528. "%3" FMT_OFF_T " %s "
  529. "%3" FMT_OFF_T " %s "
  530. "%3" FMT_OFF_T " %s %s %s %s %s %s %s",
  531. total_estm.percent, /* 3 letters */ /* total % */
  532. max6out(total_expected_size, max6[2],
  533. sizeof(max6[2])), /* total size */
  534. dl_estm.percent, /* 3 letters */ /* rcvd % */
  535. max6out(p->dl.cur_size, max6[0],
  536. sizeof(max6[0])), /* rcvd size */
  537. ul_estm.percent, /* 3 letters */ /* xfer % */
  538. max6out(p->ul.cur_size, max6[1],
  539. sizeof(max6[1])), /* xfer size */
  540. max6out(p->dl.speed, max6[3],
  541. sizeof(max6[3])), /* avrg dl speed */
  542. max6out(p->ul.speed, max6[4],
  543. sizeof(max6[4])), /* avrg ul speed */
  544. time_total, /* 7 letters */ /* total time */
  545. time_spent, /* 7 letters */ /* time spent */
  546. time_left, /* 7 letters */ /* time left */
  547. max6out(p->current_speed, max6[5],
  548. sizeof(max6[5])) /* current speed */
  549. );
  550. /* we flush the output stream to make it appear as soon as possible */
  551. fflush(data->set.err);
  552. }
  553. #else
  554. /* progress bar disabled */
  555. #define progress_meter(x) Curl_nop_stmt
  556. #endif
  557. /*
  558. * Curl_pgrsUpdate() returns 0 for success or the value returned by the
  559. * progress callback!
  560. */
  561. static CURLcode pgrsupdate(struct Curl_easy *data, bool showprogress)
  562. {
  563. if(!data->progress.hide) {
  564. if(data->set.fxferinfo) {
  565. int result;
  566. /* There is a callback set, call that */
  567. Curl_set_in_callback(data, TRUE);
  568. result = data->set.fxferinfo(data->set.progress_client,
  569. data->progress.dl.total_size,
  570. data->progress.dl.cur_size,
  571. data->progress.ul.total_size,
  572. data->progress.ul.cur_size);
  573. Curl_set_in_callback(data, FALSE);
  574. if(result != CURL_PROGRESSFUNC_CONTINUE) {
  575. if(result) {
  576. failf(data, "Callback aborted");
  577. return CURLE_ABORTED_BY_CALLBACK;
  578. }
  579. return CURLE_OK;
  580. }
  581. }
  582. else if(data->set.fprogress) {
  583. int result;
  584. /* The older deprecated callback is set, call that */
  585. Curl_set_in_callback(data, TRUE);
  586. result = data->set.fprogress(data->set.progress_client,
  587. (double)data->progress.dl.total_size,
  588. (double)data->progress.dl.cur_size,
  589. (double)data->progress.ul.total_size,
  590. (double)data->progress.ul.cur_size);
  591. Curl_set_in_callback(data, FALSE);
  592. if(result != CURL_PROGRESSFUNC_CONTINUE) {
  593. if(result) {
  594. failf(data, "Callback aborted");
  595. return CURLE_ABORTED_BY_CALLBACK;
  596. }
  597. return CURLE_OK;
  598. }
  599. }
  600. if(showprogress)
  601. progress_meter(data);
  602. }
  603. return CURLE_OK;
  604. }
  605. static CURLcode pgrs_update(struct Curl_easy *data,
  606. const struct curltime *pnow)
  607. {
  608. bool showprogress = progress_calc(data, pnow);
  609. return pgrsupdate(data, showprogress);
  610. }
  611. CURLcode Curl_pgrsUpdate(struct Curl_easy *data)
  612. {
  613. return pgrs_update(data, Curl_pgrs_now(data));
  614. }
  615. CURLcode Curl_pgrsCheck(struct Curl_easy *data)
  616. {
  617. CURLcode result;
  618. result = pgrs_update(data, Curl_pgrs_now(data));
  619. if(!result && !data->req.done)
  620. result = pgrs_speedcheck(data, Curl_pgrs_now(data));
  621. return result;
  622. }
  623. /*
  624. * Update all progress, do not do progress meter/callbacks.
  625. */
  626. void Curl_pgrsUpdate_nometer(struct Curl_easy *data)
  627. {
  628. (void)progress_calc(data, Curl_pgrs_now(data));
  629. }