[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.22.2.1

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