[BACK]Return to progressmeter.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/progressmeter.c, Revision 1.33

1.33    ! stevesk     1: /* $OpenBSD: progressmeter.c,v 1.32 2006/07/21 21:26:55 stevesk Exp $ */
1.1       fgsch       2: /*
1.9       markus      3:  * Copyright (c) 2003 Nils Nordman.  All rights reserved.
1.1       fgsch       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:
                     26: #include "includes.h"
1.25      stevesk    27:
1.31      stevesk    28: #include <sys/types.h>
1.25      stevesk    29: #include <sys/ioctl.h>
1.26      stevesk    30:
1.30      stevesk    31: #include <errno.h>
1.26      stevesk    32: #include <signal.h>
1.33    ! stevesk    33: #include <time.h>
1.31      stevesk    34: #include <unistd.h>
1.1       fgsch      35:
1.9       markus     36: #include "progressmeter.h"
                     37: #include "atomicio.h"
                     38:
                     39: #define DEFAULT_WINSIZE 80
                     40: #define MAX_WINSIZE 512
                     41: #define PADDING 1              /* padding between the progress indicators */
                     42: #define UPDATE_INTERVAL 1      /* update the progress meter every second */
                     43: #define STALL_TIME 5           /* we're stalled after this many seconds */
                     44:
                     45: /* determines whether we can output to the terminal */
                     46: static int can_output(void);
1.1       fgsch      47:
1.9       markus     48: /* formats and inserts the specified size into the given buffer */
                     49: static void format_size(char *, int, off_t);
1.13      markus     50: static void format_rate(char *, int, off_t);
1.1       fgsch      51:
1.24      jaredy     52: /* window resizing */
                     53: static void sig_winch(int);
                     54: static void setscreensize(void);
                     55:
1.9       markus     56: /* updates the progressmeter to reflect the current state of the transfer */
                     57: void refresh_progress_meter(void);
1.1       fgsch      58:
1.9       markus     59: /* signal handler for updating the progress meter */
1.1       fgsch      60: static void update_progress_meter(int);
                     61:
1.22      deraadt    62: static time_t start;           /* start progress */
                     63: static time_t last_update;     /* last progress update */
                     64: static char *file;             /* name of the file being transferred */
                     65: static off_t end_pos;          /* ending position of transfer */
                     66: static off_t cur_pos;          /* transfer position as of last refresh */
1.9       markus     67: static volatile off_t *counter;        /* progress counter */
1.22      deraadt    68: static long stalled;           /* how long we have been stalled */
                     69: static int bytes_per_second;   /* current speed in bytes per second */
                     70: static int win_size;           /* terminal window size */
1.24      jaredy     71: static volatile sig_atomic_t win_resized; /* for window resizing */
1.9       markus     72:
1.13      markus     73: /* units for format_size */
                     74: static const char unit[] = " KMGT";
                     75:
1.9       markus     76: static int
                     77: can_output(void)
                     78: {
                     79:        return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
                     80: }
                     81:
                     82: static void
1.13      markus     83: format_rate(char *buf, int size, off_t bytes)
                     84: {
                     85:        int i;
                     86:
                     87:        bytes *= 100;
                     88:        for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
                     89:                bytes = (bytes + 512) / 1024;
                     90:        if (i == 0) {
                     91:                i++;
                     92:                bytes = (bytes + 512) / 1024;
                     93:        }
                     94:        snprintf(buf, size, "%3lld.%1lld%c%s",
1.17      markus     95:            (long long) (bytes + 5) / 100,
1.13      markus     96:            (long long) (bytes + 5) / 10 % 10,
                     97:            unit[i],
                     98:            i ? "B" : " ");
                     99: }
                    100:
                    101: static void
1.9       markus    102: format_size(char *buf, int size, off_t bytes)
                    103: {
                    104:        int i;
1.1       fgsch     105:
1.11      markus    106:        for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
1.13      markus    107:                bytes = (bytes + 512) / 1024;
1.11      markus    108:        snprintf(buf, size, "%4lld%c%s",
1.9       markus    109:            (long long) bytes,
                    110:            unit[i],
                    111:            i ? "B" : " ");
                    112: }
1.1       fgsch     113:
1.9       markus    114: void
                    115: refresh_progress_meter(void)
                    116: {
                    117:        char buf[MAX_WINSIZE + 1];
                    118:        time_t now;
                    119:        off_t transferred;
                    120:        double elapsed;
                    121:        int percent;
1.19      markus    122:        off_t bytes_left;
1.9       markus    123:        int cur_speed;
                    124:        int hours, minutes, seconds;
                    125:        int i, len;
                    126:        int file_len;
                    127:
                    128:        transferred = *counter - cur_pos;
                    129:        cur_pos = *counter;
                    130:        now = time(NULL);
                    131:        bytes_left = end_pos - cur_pos;
1.1       fgsch     132:
1.9       markus    133:        if (bytes_left > 0)
                    134:                elapsed = now - last_update;
1.18      markus    135:        else {
1.9       markus    136:                elapsed = now - start;
1.18      markus    137:                /* Calculate true total speed when done */
                    138:                transferred = end_pos;
                    139:                bytes_per_second = 0;
                    140:        }
1.1       fgsch     141:
1.9       markus    142:        /* calculate speed */
                    143:        if (elapsed != 0)
                    144:                cur_speed = (transferred / elapsed);
                    145:        else
1.18      markus    146:                cur_speed = transferred;
1.1       fgsch     147:
1.9       markus    148: #define AGE_FACTOR 0.9
                    149:        if (bytes_per_second != 0) {
                    150:                bytes_per_second = (bytes_per_second * AGE_FACTOR) +
                    151:                    (cur_speed * (1.0 - AGE_FACTOR));
                    152:        } else
                    153:                bytes_per_second = cur_speed;
1.1       fgsch     154:
1.9       markus    155:        /* filename */
                    156:        buf[0] = '\0';
1.13      markus    157:        file_len = win_size - 35;
1.9       markus    158:        if (file_len > 0) {
1.14      markus    159:                len = snprintf(buf, file_len + 1, "\r%s", file);
1.15      markus    160:                if (len < 0)
                    161:                        len = 0;
1.23      moritz    162:                if (len >= file_len + 1)
                    163:                        len = file_len;
1.9       markus    164:                for (i = len;  i < file_len; i++ )
                    165:                        buf[i] = ' ';
                    166:                buf[file_len] = '\0';
                    167:        }
1.1       fgsch     168:
1.9       markus    169:        /* percent of transfer done */
                    170:        if (end_pos != 0)
                    171:                percent = ((float)cur_pos / end_pos) * 100;
                    172:        else
                    173:                percent = 100;
                    174:        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    175:            " %3d%% ", percent);
                    176:
                    177:        /* amount transferred */
                    178:        format_size(buf + strlen(buf), win_size - strlen(buf),
                    179:            cur_pos);
                    180:        strlcat(buf, " ", win_size);
                    181:
                    182:        /* bandwidth usage */
1.13      markus    183:        format_rate(buf + strlen(buf), win_size - strlen(buf),
1.20      deraadt   184:            (off_t)bytes_per_second);
1.9       markus    185:        strlcat(buf, "/s ", win_size);
                    186:
                    187:        /* ETA */
                    188:        if (!transferred)
                    189:                stalled += elapsed;
                    190:        else
                    191:                stalled = 0;
1.1       fgsch     192:
1.9       markus    193:        if (stalled >= STALL_TIME)
1.10      markus    194:                strlcat(buf, "- stalled -", win_size);
1.9       markus    195:        else if (bytes_per_second == 0 && bytes_left)
                    196:                strlcat(buf, "  --:-- ETA", win_size);
                    197:        else {
                    198:                if (bytes_left > 0)
                    199:                        seconds = bytes_left / bytes_per_second;
                    200:                else
                    201:                        seconds = elapsed;
1.1       fgsch     202:
1.9       markus    203:                hours = seconds / 3600;
                    204:                seconds -= hours * 3600;
                    205:                minutes = seconds / 60;
                    206:                seconds -= minutes * 60;
                    207:
                    208:                if (hours != 0)
                    209:                        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    210:                            "%d:%02d:%02d", hours, minutes, seconds);
                    211:                else
                    212:                        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    213:                            "  %02d:%02d", minutes, seconds);
1.1       fgsch     214:
1.9       markus    215:                if (bytes_left > 0)
                    216:                        strlcat(buf, " ETA", win_size);
                    217:                else
                    218:                        strlcat(buf, "    ", win_size);
                    219:        }
1.1       fgsch     220:
1.16      markus    221:        atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
1.9       markus    222:        last_update = now;
1.1       fgsch     223: }
                    224:
1.32      stevesk   225: /*ARGSUSED*/
1.1       fgsch     226: static void
                    227: update_progress_meter(int ignore)
                    228: {
1.9       markus    229:        int save_errno;
                    230:
                    231:        save_errno = errno;
                    232:
1.24      jaredy    233:        if (win_resized) {
                    234:                setscreensize();
                    235:                win_resized = 0;
                    236:        }
1.9       markus    237:        if (can_output())
                    238:                refresh_progress_meter();
1.1       fgsch     239:
                    240:        signal(SIGALRM, update_progress_meter);
1.9       markus    241:        alarm(UPDATE_INTERVAL);
1.1       fgsch     242:        errno = save_errno;
                    243: }
                    244:
1.9       markus    245: void
1.21      avsm      246: start_progress_meter(char *f, off_t filesize, off_t *ctr)
1.1       fgsch     247: {
1.9       markus    248:        start = last_update = time(NULL);
                    249:        file = f;
                    250:        end_pos = filesize;
                    251:        cur_pos = 0;
1.21      avsm      252:        counter = ctr;
1.9       markus    253:        stalled = 0;
                    254:        bytes_per_second = 0;
                    255:
1.24      jaredy    256:        setscreensize();
1.9       markus    257:        if (can_output())
                    258:                refresh_progress_meter();
1.1       fgsch     259:
1.9       markus    260:        signal(SIGALRM, update_progress_meter);
1.24      jaredy    261:        signal(SIGWINCH, sig_winch);
1.9       markus    262:        alarm(UPDATE_INTERVAL);
1.1       fgsch     263: }
                    264:
1.9       markus    265: void
                    266: stop_progress_meter(void)
1.1       fgsch     267: {
1.9       markus    268:        alarm(0);
1.1       fgsch     269:
1.9       markus    270:        if (!can_output())
1.1       fgsch     271:                return;
                    272:
1.9       markus    273:        /* Ensure we complete the progress */
                    274:        if (cur_pos != end_pos)
                    275:                refresh_progress_meter();
1.12      markus    276:
1.9       markus    277:        atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.24      jaredy    278: }
                    279:
1.28      deraadt   280: /*ARGSUSED*/
1.24      jaredy    281: static void
                    282: sig_winch(int sig)
                    283: {
                    284:        win_resized = 1;
                    285: }
                    286:
                    287: static void
                    288: setscreensize(void)
                    289: {
                    290:        struct winsize winsize;
                    291:
                    292:        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
                    293:            winsize.ws_col != 0) {
                    294:                if (winsize.ws_col > MAX_WINSIZE)
                    295:                        win_size = MAX_WINSIZE;
                    296:                else
                    297:                        win_size = winsize.ws_col;
                    298:        } else
                    299:                win_size = DEFAULT_WINSIZE;
                    300:        win_size += 1;                                  /* trailing \0 */
1.1       fgsch     301: }