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

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.12    ! markus     26: RCSID("$OpenBSD: progressmeter.c,v 1.11 2003/07/30 07:44:14 markus 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.1       fgsch      42:
1.9       markus     43: /* updates the progressmeter to reflect the current state of the transfer */
                     44: void refresh_progress_meter(void);
1.1       fgsch      45:
1.9       markus     46: /* signal handler for updating the progress meter */
1.1       fgsch      47: static void update_progress_meter(int);
                     48:
1.9       markus     49: static time_t start;           /* start progress */
                     50: static time_t last_update;     /* last progress update */
                     51: static char *file;             /* name of the file being transferred */
                     52: static off_t end_pos;          /* ending position of transfer */
                     53: static off_t cur_pos;          /* transfer position as of last refresh */
                     54: static volatile off_t *counter;        /* progress counter */
                     55: static long stalled;           /* how long we have been stalled */
                     56: static int bytes_per_second;   /* current speed in bytes per second */
                     57: static int win_size;           /* terminal window size */
                     58:
                     59: static int
                     60: can_output(void)
                     61: {
                     62:        return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
                     63: }
                     64:
                     65: static void
                     66: format_size(char *buf, int size, off_t bytes)
                     67: {
                     68:        static const char unit[] = " KMGT";
                     69:        int i;
1.1       fgsch      70:
1.11      markus     71:        for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
1.9       markus     72:                bytes /= 1024;
1.11      markus     73:        snprintf(buf, size, "%4lld%c%s",
1.9       markus     74:            (long long) bytes,
                     75:            unit[i],
                     76:            i ? "B" : " ");
                     77: }
1.1       fgsch      78:
1.9       markus     79: void
                     80: refresh_progress_meter(void)
                     81: {
                     82:        char buf[MAX_WINSIZE + 1];
                     83:        time_t now;
                     84:        off_t transferred;
                     85:        double elapsed;
                     86:        int percent;
                     87:        int bytes_left;
                     88:        int cur_speed;
                     89:        int hours, minutes, seconds;
                     90:        int i, len;
                     91:        int file_len;
                     92:
                     93:        transferred = *counter - cur_pos;
                     94:        cur_pos = *counter;
                     95:        now = time(NULL);
                     96:        bytes_left = end_pos - cur_pos;
1.1       fgsch      97:
1.9       markus     98:        if (bytes_left > 0)
                     99:                elapsed = now - last_update;
                    100:        else
                    101:                elapsed = now - start;
1.1       fgsch     102:
1.9       markus    103:        /* calculate speed */
                    104:        if (elapsed != 0)
                    105:                cur_speed = (transferred / elapsed);
                    106:        else
                    107:                cur_speed = 0;
1.1       fgsch     108:
1.9       markus    109: #define AGE_FACTOR 0.9
                    110:        if (bytes_per_second != 0) {
                    111:                bytes_per_second = (bytes_per_second * AGE_FACTOR) +
                    112:                    (cur_speed * (1.0 - AGE_FACTOR));
                    113:        } else
                    114:                bytes_per_second = cur_speed;
1.1       fgsch     115:
1.9       markus    116:        /* filename */
                    117:        buf[0] = '\0';
1.11      markus    118:        file_len = win_size - 34;
1.9       markus    119:        if (file_len > 0) {
                    120:                len = snprintf(buf, file_len, "\r%s", file);
                    121:                for (i = len;  i < file_len; i++ )
                    122:                        buf[i] = ' ';
                    123:                buf[file_len] = '\0';
                    124:        }
1.1       fgsch     125:
1.9       markus    126:        /* percent of transfer done */
                    127:        if (end_pos != 0)
                    128:                percent = ((float)cur_pos / end_pos) * 100;
                    129:        else
                    130:                percent = 100;
                    131:        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    132:            " %3d%% ", percent);
                    133:
                    134:        /* amount transferred */
                    135:        format_size(buf + strlen(buf), win_size - strlen(buf),
                    136:            cur_pos);
                    137:        strlcat(buf, " ", win_size);
                    138:
                    139:        /* bandwidth usage */
                    140:        format_size(buf + strlen(buf), win_size - strlen(buf),
                    141:            bytes_per_second);
                    142:        strlcat(buf, "/s ", win_size);
                    143:
                    144:        /* ETA */
                    145:        if (!transferred)
                    146:                stalled += elapsed;
                    147:        else
                    148:                stalled = 0;
1.1       fgsch     149:
1.9       markus    150:        if (stalled >= STALL_TIME)
1.10      markus    151:                strlcat(buf, "- stalled -", win_size);
1.9       markus    152:        else if (bytes_per_second == 0 && bytes_left)
                    153:                strlcat(buf, "  --:-- ETA", win_size);
                    154:        else {
                    155:                if (bytes_left > 0)
                    156:                        seconds = bytes_left / bytes_per_second;
                    157:                else
                    158:                        seconds = elapsed;
1.1       fgsch     159:
1.9       markus    160:                hours = seconds / 3600;
                    161:                seconds -= hours * 3600;
                    162:                minutes = seconds / 60;
                    163:                seconds -= minutes * 60;
                    164:
                    165:                if (hours != 0)
                    166:                        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    167:                            "%d:%02d:%02d", hours, minutes, seconds);
                    168:                else
                    169:                        snprintf(buf + strlen(buf), win_size - strlen(buf),
                    170:                            "  %02d:%02d", minutes, seconds);
1.1       fgsch     171:
1.9       markus    172:                if (bytes_left > 0)
                    173:                        strlcat(buf, " ETA", win_size);
                    174:                else
                    175:                        strlcat(buf, "    ", win_size);
                    176:        }
1.1       fgsch     177:
1.9       markus    178:        atomicio(vwrite, STDOUT_FILENO, buf, win_size);
                    179:        last_update = now;
1.1       fgsch     180: }
                    181:
                    182: static void
                    183: update_progress_meter(int ignore)
                    184: {
1.9       markus    185:        int save_errno;
                    186:
                    187:        save_errno = errno;
                    188:
                    189:        if (can_output())
                    190:                refresh_progress_meter();
1.1       fgsch     191:
                    192:        signal(SIGALRM, update_progress_meter);
1.9       markus    193:        alarm(UPDATE_INTERVAL);
1.1       fgsch     194:        errno = save_errno;
                    195: }
                    196:
1.9       markus    197: void
                    198: start_progress_meter(char *f, off_t filesize, off_t *stat)
1.1       fgsch     199: {
1.9       markus    200:        struct winsize winsize;
1.1       fgsch     201:
1.9       markus    202:        start = last_update = time(NULL);
                    203:        file = f;
                    204:        end_pos = filesize;
                    205:        cur_pos = 0;
                    206:        counter = stat;
                    207:        stalled = 0;
                    208:        bytes_per_second = 0;
                    209:
                    210:        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
                    211:            winsize.ws_col != 0) {
                    212:                if (winsize.ws_col > MAX_WINSIZE)
                    213:                        win_size = MAX_WINSIZE;
                    214:                else
                    215:                        win_size = winsize.ws_col;
                    216:        } else
                    217:                win_size = DEFAULT_WINSIZE;
                    218:        win_size += 1;                                  /* trailing \0 */
1.12    ! markus    219:
1.9       markus    220:        if (can_output())
                    221:                refresh_progress_meter();
1.1       fgsch     222:
1.9       markus    223:        signal(SIGALRM, update_progress_meter);
                    224:        alarm(UPDATE_INTERVAL);
1.1       fgsch     225: }
                    226:
1.9       markus    227: void
                    228: stop_progress_meter(void)
1.1       fgsch     229: {
1.9       markus    230:        alarm(0);
1.1       fgsch     231:
1.9       markus    232:        if (!can_output())
1.1       fgsch     233:                return;
                    234:
1.9       markus    235:        /* Ensure we complete the progress */
                    236:        if (cur_pos != end_pos)
                    237:                refresh_progress_meter();
1.12    ! markus    238:
1.9       markus    239:        atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.1       fgsch     240: }