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

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