progress.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Progress bar code.
  4. */
  5. /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
  6. * much of which was blatantly stolen from openssh.
  7. */
  8. /*-
  9. * Copyright (c) 1992, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
  22. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
  23. *
  24. * 4. Neither the name of the University nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. #include "libbb.h"
  41. #include "unicode.h"
  42. enum {
  43. /* Seconds when xfer considered "stalled" */
  44. STALLTIME = 5
  45. };
  46. void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile)
  47. {
  48. #if ENABLE_UNICODE_SUPPORT
  49. init_unicode();
  50. p->curfile = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20);
  51. #else
  52. p->curfile = curfile;
  53. #endif
  54. p->start_sec = monotonic_sec();
  55. p->last_update_sec = p->start_sec;
  56. p->last_change_sec = p->start_sec;
  57. p->last_size = 0;
  58. #if 0
  59. p->last_eta = INT_MAX;
  60. #endif
  61. }
  62. /* File already had beg_size bytes.
  63. * Then we started downloading.
  64. * We downloaded "transferred" bytes so far.
  65. * Download is expected to stop when total size (beg_size + transferred)
  66. * will be "totalsize" bytes.
  67. * If totalsize == 0, then it is unknown.
  68. */
  69. int FAST_FUNC bb_progress_update(bb_progress_t *p,
  70. uoff_t beg_size,
  71. uoff_t transferred,
  72. uoff_t totalsize)
  73. {
  74. char numbuf5[6]; /* 5 + 1 for NUL */
  75. unsigned since_last_update, elapsed;
  76. int notty;
  77. //transferred = 1234; /* use for stall detection testing */
  78. //totalsize = 0; /* use for unknown size download testing */
  79. elapsed = monotonic_sec();
  80. since_last_update = elapsed - p->last_update_sec;
  81. p->last_update_sec = elapsed;
  82. if (totalsize != 0 && transferred >= totalsize - beg_size) {
  83. /* Last call. Do not skip this update */
  84. transferred = totalsize - beg_size; /* sanitize just in case */
  85. }
  86. else if (since_last_update == 0) {
  87. /*
  88. * Do not update on every call
  89. * (we can be called on every network read!)
  90. */
  91. return -1;
  92. }
  93. /* Before we lose real, unscaled sizes, produce human-readable size string */
  94. smart_ulltoa5(beg_size + transferred, numbuf5, " kMGTPEZY")[0] = '\0';
  95. /*
  96. * Scale sizes down if they are close to overflowing.
  97. * This allows calculations like (100 * transferred / totalsize)
  98. * without risking overflow: we guarantee 10 highest bits to be 0.
  99. * Introduced error is less than 1 / 2^12 ~= 0.025%
  100. */
  101. while (totalsize >= (1 << 20)) {
  102. totalsize >>= 8;
  103. beg_size >>= 8;
  104. transferred >>= 8;
  105. }
  106. /* If they were huge, now they are scaled down to [1048575,4096] range.
  107. * (N * totalsize) won't overflow 32 bits for N up to 4096.
  108. */
  109. #if ULONG_MAX == 0xffffffff
  110. /* 32-bit CPU, uoff_t arithmetic is complex on it, cast variables to narrower types */
  111. # define totalsize ((unsigned)totalsize)
  112. # define beg_size ((unsigned)beg_size)
  113. # define transferred ((unsigned)transferred)
  114. #endif
  115. notty = !isatty(STDERR_FILENO);
  116. if (ENABLE_UNICODE_SUPPORT)
  117. fprintf(stderr, "\r%s " + notty, p->curfile);
  118. else
  119. fprintf(stderr, "\r%-20.20s " + notty, p->curfile);
  120. if (totalsize != 0) {
  121. int barlength;
  122. unsigned beg_and_transferred; /* does not need uoff_t, see scaling code */
  123. unsigned ratio;
  124. beg_and_transferred = beg_size + transferred;
  125. ratio = 100 * beg_and_transferred / totalsize;
  126. /* can't overflow ^^^^^^^^^^^^^^^ */
  127. fprintf(stderr, "%3u%% ", ratio);
  128. barlength = get_terminal_width(2) - 48;
  129. /*
  130. * Must reject barlength <= 0 (terminal too narrow). While at it,
  131. * also reject: 1-char bar (useless), 2-char bar (ridiculous).
  132. */
  133. if (barlength > 2) {
  134. if (barlength > 999)
  135. barlength = 999;
  136. {
  137. /* god bless gcc for variable arrays :) */
  138. char buf[barlength + 1];
  139. unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
  140. /* can't overflow ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  141. memset(buf, ' ', barlength);
  142. buf[barlength] = '\0';
  143. memset(buf, '*', stars);
  144. fprintf(stderr, "|%s| ", buf);
  145. }
  146. }
  147. }
  148. fputs(numbuf5, stderr); /* "NNNNk" */
  149. since_last_update = elapsed - p->last_change_sec;
  150. if ((unsigned)transferred != p->last_size) {
  151. p->last_change_sec = elapsed;
  152. p->last_size = (unsigned)transferred;
  153. if (since_last_update >= STALLTIME) {
  154. /* We "cut out" these seconds from elapsed time
  155. * by adjusting start time */
  156. p->start_sec += since_last_update;
  157. }
  158. since_last_update = 0; /* we are un-stalled now */
  159. }
  160. elapsed -= p->start_sec; /* now it's "elapsed since start" */
  161. if (since_last_update >= STALLTIME) {
  162. fprintf(stderr, " - stalled -");
  163. } else if (!totalsize || !transferred || (int)elapsed < 0) {
  164. fprintf(stderr, " --:--:-- ETA");
  165. } else {
  166. unsigned eta, secs, hours;
  167. unsigned bytes;
  168. bytes = totalsize - beg_size;
  169. /* Estimated remaining time =
  170. * estimated_sec_to_dl_bytes - elapsed_sec =
  171. * bytes / average_bytes_sec_so_far - elapsed =
  172. * bytes / (transferred/elapsed) - elapsed =
  173. * bytes * elapsed / transferred - elapsed
  174. */
  175. eta = (unsigned long)bytes * elapsed / transferred - elapsed;
  176. /* if 32bit, can overflow ^^^^^^^^^^, but this would only show bad ETA */
  177. if (eta >= 1000*60*60)
  178. eta = 1000*60*60 - 1;
  179. #if 0
  180. /* To prevent annoying "back-and-forth" estimation jitter,
  181. * if new ETA is larger than the last just by a few seconds,
  182. * disregard it, and show last one. The end result is that
  183. * ETA usually only decreases, unless download slows down a lot.
  184. */
  185. if ((unsigned)(eta - p->last_eta) < 10)
  186. eta = p->last_eta;
  187. p->last_eta = eta;
  188. #endif
  189. secs = eta % 3600;
  190. hours = eta / 3600;
  191. fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
  192. }
  193. if (notty)
  194. fputc('\n', stderr);
  195. return notty;
  196. }