-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogress.c
116 lines (101 loc) · 3.35 KB
/
progress.c
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
/*
* zsync - client side rsync over http
* Copyright (C) 2004,2005,2007,2009 Colin Phipps <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Artistic License v2 (see the accompanying
* file COPYING for the full license terms), or, at your option, any later
* version of the same license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* COPYING file for details.
*/
#include "zsglobal.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef WITH_DMALLOC
# include <dmalloc.h>
#endif
#include "progress.h"
struct progress {
time_t starttime;
time_t lasttime;
float lastpcnt;
long long lastdl;
};
/* progbar(chars, percent)
* (Re)print progress bar with chars out of 20 shown and followed by the given
* percentage done. */
static void progbar(int j, float pcnt) {
int i;
char buf[21];
for (i = 0; i < j && i < 20; i++)
buf[i] = '#';
for (; i < 20; i++)
buf[i] = '-';
buf[i] = 0;
printf("\r%s %.1f%%", buf, pcnt);
}
/* state=start_progress(url)
* Returns a handle to a progress state used to track progress downloading
* from the given URL.*/
void *start_progress(const char *url)
{
struct progress *p = calloc(sizeof(struct progress), 1);
return p;
}
/* do_progress(progress, percent, total_bytes_retrieved
* Updates the supplied progress structure with the new % done given, and
* recalculates the rolling download rate given the supplied
* total_bytes_retrieved (and the current time) */
void do_progress(void *pv, float pcnt, long long newdl) {
struct progress *p = pv;
/* If new or if time has passed, update progress display & data */
time_t newtime = time(NULL);
if (p->lasttime != newtime) {
int passed = p->lasttime ? newtime - p->lasttime : 0;
if (!p->lasttime)
p->starttime = newtime;
p->lasttime = newtime;
/* Update progress bar displayed */
progbar(pcnt * (20.0 / 100.0), pcnt);
/* Each time 1s has passed, we update and redisplay our download rate */
if (passed) {
float rate = newdl - p->lastdl;
int sleft = (100.0f - pcnt) / (pcnt - p->lastpcnt);
if (passed != 1) {
rate /= passed;
sleft *= passed;
}
printf(" %.1f kBps ", rate / 1000.0);
if (sleft < 60 * 1000)
printf("%d:%02d ETA ", sleft / 60, sleft % 60);
else
puts(" ");
}
p->lastdl = newdl;
p->lastpcnt = pcnt;
fflush(stdout);
}
}
/* end_progress(progress, done)
* Do one final update of the progress display (set to 100% if done is set to
* true), and then release the progress data structures.
*/
void end_progress(void *pv, int done) {
struct progress *p = pv;
if (done == 2)
progbar(20, 100.0);
else
progbar(p->lastpcnt * (20.0 / 100.0), p->lastpcnt);
{
float rate = ((float)p->lastdl) / (p->lasttime - p->starttime + 0.5);
printf(" %.1f kBps ", rate / 1000.0);
}
puts(done == 2 ? "DONE \n" : !done ? "aborted \n" : " \n");
fflush(stdout);
free(p);
}