summaryrefslogtreecommitdiffstats
path: root/sbin/camcontrol/progress.c
blob: f76305c335252eaf1227729872029027da27f6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*	$NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $	*/

/*-
 * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "progress.h"

static const char * const suffixes[] = {
	"",	/* 2^0  (byte) */
	"KiB",	/* 2^10 Kibibyte */
	"MiB",	/* 2^20 Mebibyte */
	"GiB",	/* 2^30 Gibibyte */
	"TiB",	/* 2^40 Tebibyte */
	"PiB",	/* 2^50 Pebibyte */
	"EiB",	/* 2^60 Exbibyte */
};

#define NSUFFIXES	nitems(suffixes)
#define SECSPERHOUR	(60 * 60)
#define DEFAULT_TTYWIDTH	80

/* initialise progress meter structure */
int
progress_init(progress_t *prog, const char *prefix, uint64_t total)
{
        struct winsize	winsize;
        int		oerrno = errno;

	(void) memset(prog, 0x0, sizeof(*prog));
	prog->size = total;
	prog->prefix = strdup(prefix);
	prog->start = time(NULL);
        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
            winsize.ws_col != 0) {
                prog->ttywidth = winsize.ws_col;
        } else {
                prog->ttywidth = DEFAULT_TTYWIDTH;
	}
        errno = oerrno;
	return 1;
}

/* update the values in the progress meter */
int
progress_update(progress_t *prog, uint64_t done)
{
	prog->done = done;
	prog->percent = (prog->done * 100) / prog->size;
	prog->now = time(NULL);
	prog->elapsed = prog->now - prog->start;
	if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) {
		prog->eta = 0;
	} else {
		prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed;
	}
	return 1;
}

/* update the values in the progress meter */
int
progress_reset_size(progress_t *prog, uint64_t size)
{
	prog->size = size;
	return 1;
}

/* make it look pretty at the end - display done bytes (usually total) */
int
progress_complete(progress_t *prog, uint64_t done)
{
	progress_update(prog, done);
	progress_draw(prog);
	printf("\n");
	return 1;
}

/* draw the progress meter */
int
progress_draw(progress_t *prog)
{
#define	BAROVERHEAD	45		/* non `*' portion of progress bar */
					/*
					 * stars should contain at least
					 * sizeof(buf) - BAROVERHEAD entries
					 */
#define	MIN_BAR_LEN	10
	static const char	stars[] =
"*****************************************************************************"
"*****************************************************************************"
"*****************************************************************************";
	unsigned		bytesabbrev;
	unsigned		bpsabbrev;
	int64_t			secs;
	uint64_t		bytespersec;
	uint64_t		abbrevsize;
	int64_t			secsleft;
	ssize_t			barlength;
	size_t			starc;
	char			hours[12];
	char			buf[256];
	int			len;
	int			prefix_len;

	prefix_len = strlen(prog->prefix);
	barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) -
		BAROVERHEAD - prefix_len;
	if (barlength < MIN_BAR_LEN) {
		int tmp_prefix_len;
		/*
		 * In this case the TTY width is too small or the prefix is
		 * too large for a progress bar.  We'll try decreasing the
		 * prefix length.
		 */
		barlength = MIN_BAR_LEN;
		tmp_prefix_len = MIN(sizeof(buf) - 1,(unsigned)prog->ttywidth) -
		    BAROVERHEAD - MIN_BAR_LEN;
		if (tmp_prefix_len > 0)
			prefix_len = tmp_prefix_len;
		else
			prefix_len = 0;
	}
	starc = (barlength * prog->percent) / 100;
	abbrevsize = prog->done;
	for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) {
		abbrevsize >>= 10;
	}
	if (bytesabbrev == NSUFFIXES) {
		bytesabbrev--;
	}
	bytespersec = 0;
	if (prog->done > 0) {
		bytespersec = prog->done;
		if (prog->elapsed > 0) {
			bytespersec /= prog->elapsed;
		}
	}
	for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) {
		bytespersec >>= 10;
	}
	if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) {
		secsleft = 0;
	} else {
		secsleft = prog->eta;
	}
	if ((secs = secsleft / SECSPERHOUR) > 0) {
		(void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs);
	} else {
		(void) snprintf(hours, sizeof(hours), "   ");
	}
	secs = secsleft % SECSPERHOUR;
	len = snprintf(buf, sizeof(buf),
		"\r%.*s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA",
		prefix_len, (prog->prefix) ? prog->prefix : "",
		(long long)prog->percent,
		(int)starc, stars, (int)(barlength - starc), "",
		(long long)abbrevsize,
		suffixes[bytesabbrev],
		(long long)(bytespersec / 1024),
		(int)((bytespersec % 1024) * 100 / 1024),
		suffixes[bpsabbrev],
		hours,
		(int)secs / 60, (int)secs % 60);
	return (int)write(STDOUT_FILENO, buf, len);
}
OpenPOWER on IntegriCloud