progressmeter.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifdef PROGRESS_METER
  2. /*
  3. * Copyright (c) 2003 Nils Nordman. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. /*RCSID("$OpenBSD: progressmeter.c,v 1.24 2005/06/07 13:25:23 jaredy Exp $");*/
  27. #include "progressmeter.h"
  28. #include "atomicio.h"
  29. #include "scpmisc.h"
  30. #define DEFAULT_WINSIZE 80
  31. #define MAX_WINSIZE 512
  32. #define PADDING 1 /* padding between the progress indicators */
  33. #define UPDATE_INTERVAL 1 /* update the progress meter every second */
  34. #define STALL_TIME 5 /* we're stalled after this many seconds */
  35. /* determines whether we can output to the terminal */
  36. static int can_output(void);
  37. /* formats and inserts the specified size into the given buffer */
  38. static void format_size(char *, int, off_t);
  39. static void format_rate(char *, int, off_t);
  40. /* window resizing */
  41. static void sig_winch(int);
  42. static void setscreensize(void);
  43. /* updates the progressmeter to reflect the current state of the transfer */
  44. void refresh_progress_meter(void);
  45. /* signal handler for updating the progress meter */
  46. static void update_progress_meter(int);
  47. static time_t start; /* start progress */
  48. static time_t last_update; /* last progress update */
  49. static char *file; /* name of the file being transferred */
  50. static off_t end_pos; /* ending position of transfer */
  51. static off_t cur_pos; /* transfer position as of last refresh */
  52. static volatile off_t *counter; /* progress counter */
  53. static long stalled; /* how long we have been stalled */
  54. static int bytes_per_second; /* current speed in bytes per second */
  55. static int win_size; /* terminal window size */
  56. static volatile sig_atomic_t win_resized; /* for window resizing */
  57. /* units for format_size */
  58. static const char unit[] = " KMGT";
  59. static int
  60. can_output(void)
  61. {
  62. return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
  63. }
  64. static void
  65. format_rate(char *buf, int size, off_t bytes)
  66. {
  67. int i;
  68. bytes *= 100;
  69. for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
  70. bytes = (bytes + 512) / 1024;
  71. if (i == 0) {
  72. i++;
  73. bytes = (bytes + 512) / 1024;
  74. }
  75. snprintf(buf, size, "%3lld.%1lld%c%s",
  76. (long long) (bytes + 5) / 100,
  77. (long long) (bytes + 5) / 10 % 10,
  78. unit[i],
  79. i ? "B" : " ");
  80. }
  81. static void
  82. format_size(char *buf, int size, off_t bytes)
  83. {
  84. int i;
  85. for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
  86. bytes = (bytes + 512) / 1024;
  87. snprintf(buf, size, "%4lld%c%s",
  88. (long long) bytes,
  89. unit[i],
  90. i ? "B" : " ");
  91. }
  92. void
  93. refresh_progress_meter(void)
  94. {
  95. char buf[MAX_WINSIZE + 1];
  96. time_t now;
  97. off_t transferred;
  98. double elapsed;
  99. int percent;
  100. off_t bytes_left;
  101. int cur_speed;
  102. int hours, minutes, seconds;
  103. int i, len;
  104. int file_len;
  105. transferred = *counter - cur_pos;
  106. cur_pos = *counter;
  107. now = time(NULL);
  108. bytes_left = end_pos - cur_pos;
  109. if (bytes_left > 0)
  110. elapsed = now - last_update;
  111. else {
  112. elapsed = now - start;
  113. /* Calculate true total speed when done */
  114. transferred = end_pos;
  115. bytes_per_second = 0;
  116. }
  117. /* calculate speed */
  118. if (elapsed != 0)
  119. cur_speed = (transferred / elapsed);
  120. else
  121. cur_speed = transferred;
  122. #define AGE_FACTOR 0.9
  123. if (bytes_per_second != 0) {
  124. bytes_per_second = (bytes_per_second * AGE_FACTOR) +
  125. (cur_speed * (1.0 - AGE_FACTOR));
  126. } else
  127. bytes_per_second = cur_speed;
  128. /* filename */
  129. buf[0] = '\0';
  130. file_len = win_size - 35;
  131. if (file_len > 0) {
  132. len = snprintf(buf, file_len + 1, "\r%s", file);
  133. if (len < 0)
  134. len = 0;
  135. if (len >= file_len + 1)
  136. len = file_len;
  137. for (i = len; i < file_len; i++ )
  138. buf[i] = ' ';
  139. buf[file_len] = '\0';
  140. }
  141. /* percent of transfer done */
  142. if (end_pos != 0)
  143. percent = ((float)cur_pos / end_pos) * 100;
  144. else
  145. percent = 100;
  146. snprintf(buf + strlen(buf), win_size - strlen(buf),
  147. " %3d%% ", percent);
  148. /* amount transferred */
  149. format_size(buf + strlen(buf), win_size - strlen(buf),
  150. cur_pos);
  151. strlcat(buf, " ", win_size);
  152. /* bandwidth usage */
  153. format_rate(buf + strlen(buf), win_size - strlen(buf),
  154. (off_t)bytes_per_second);
  155. strlcat(buf, "/s ", win_size);
  156. /* ETA */
  157. if (!transferred)
  158. stalled += elapsed;
  159. else
  160. stalled = 0;
  161. if (stalled >= STALL_TIME)
  162. strlcat(buf, "- stalled -", win_size);
  163. else if (bytes_per_second == 0 && bytes_left)
  164. strlcat(buf, " --:-- ETA", win_size);
  165. else {
  166. if (bytes_left > 0)
  167. seconds = bytes_left / bytes_per_second;
  168. else
  169. seconds = elapsed;
  170. hours = seconds / 3600;
  171. seconds -= hours * 3600;
  172. minutes = seconds / 60;
  173. seconds -= minutes * 60;
  174. if (hours != 0)
  175. snprintf(buf + strlen(buf), win_size - strlen(buf),
  176. "%d:%02d:%02d", hours, minutes, seconds);
  177. else
  178. snprintf(buf + strlen(buf), win_size - strlen(buf),
  179. " %02d:%02d", minutes, seconds);
  180. if (bytes_left > 0)
  181. strlcat(buf, " ETA", win_size);
  182. else
  183. strlcat(buf, " ", win_size);
  184. }
  185. atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
  186. last_update = now;
  187. }
  188. static void
  189. update_progress_meter(int ignore)
  190. {
  191. int save_errno;
  192. save_errno = errno;
  193. if (win_resized) {
  194. setscreensize();
  195. win_resized = 0;
  196. }
  197. if (can_output())
  198. refresh_progress_meter();
  199. signal(SIGALRM, update_progress_meter);
  200. alarm(UPDATE_INTERVAL);
  201. errno = save_errno;
  202. }
  203. void
  204. start_progress_meter(char *f, off_t filesize, off_t *ctr)
  205. {
  206. start = last_update = time(NULL);
  207. file = f;
  208. end_pos = filesize;
  209. cur_pos = 0;
  210. counter = ctr;
  211. stalled = 0;
  212. bytes_per_second = 0;
  213. setscreensize();
  214. if (can_output())
  215. refresh_progress_meter();
  216. signal(SIGALRM, update_progress_meter);
  217. signal(SIGWINCH, sig_winch);
  218. alarm(UPDATE_INTERVAL);
  219. }
  220. void
  221. stop_progress_meter(void)
  222. {
  223. alarm(0);
  224. if (!can_output())
  225. return;
  226. /* Ensure we complete the progress */
  227. if (cur_pos != end_pos)
  228. refresh_progress_meter();
  229. atomicio(vwrite, STDOUT_FILENO, "\n", 1);
  230. }
  231. static void
  232. sig_winch(int sig)
  233. {
  234. win_resized = 1;
  235. }
  236. static void
  237. setscreensize(void)
  238. {
  239. struct winsize winsize;
  240. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
  241. winsize.ws_col != 0) {
  242. if (winsize.ws_col > MAX_WINSIZE)
  243. win_size = MAX_WINSIZE;
  244. else
  245. win_size = winsize.ws_col;
  246. } else
  247. win_size = DEFAULT_WINSIZE;
  248. win_size += 1; /* trailing \0 */
  249. }
  250. #endif /* PROGRESS_METER */