diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-11 19:09:30 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-11 19:09:30 +0100 |
commit | ab8d00d64fc23602875952c08c030f05f206686c (patch) | |
tree | 62832c02ad648d25a9395f26d59220ba932b8f88 | |
parent | d55e13964916af6a083be881bffdb493af287c1d (diff) | |
download | busybox-w32-ab8d00d64fc23602875952c08c030f05f206686c.tar.gz busybox-w32-ab8d00d64fc23602875952c08c030f05f206686c.tar.bz2 busybox-w32-ab8d00d64fc23602875952c08c030f05f206686c.zip |
progress meter: fix bugs found in stall detection and unknown size logic
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 5 | ||||
-rw-r--r-- | libbb/progress.c | 55 | ||||
-rw-r--r-- | networking/tftp.c | 2 |
3 files changed, 36 insertions, 26 deletions
diff --git a/include/libbb.h b/include/libbb.h index 7581cd4c4..65c319402 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1584,8 +1584,9 @@ int print_flags_separated(const int *masks, const char *labels, | |||
1584 | int print_flags(const masks_labels_t *ml, int flags) FAST_FUNC; | 1584 | int print_flags(const masks_labels_t *ml, int flags) FAST_FUNC; |
1585 | 1585 | ||
1586 | typedef struct bb_progress_t { | 1586 | typedef struct bb_progress_t { |
1587 | off_t lastsize; | 1587 | unsigned last_size; |
1588 | unsigned lastupdate_sec; | 1588 | unsigned last_update_sec; |
1589 | unsigned last_change_sec; | ||
1589 | unsigned start_sec; | 1590 | unsigned start_sec; |
1590 | const char *curfile; | 1591 | const char *curfile; |
1591 | } bb_progress_t; | 1592 | } bb_progress_t; |
diff --git a/libbb/progress.c b/libbb/progress.c index 1d260dd08..df43dad5c 100644 --- a/libbb/progress.c +++ b/libbb/progress.c | |||
@@ -61,8 +61,9 @@ void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile) | |||
61 | p->curfile = curfile; | 61 | p->curfile = curfile; |
62 | #endif | 62 | #endif |
63 | p->start_sec = monotonic_sec(); | 63 | p->start_sec = monotonic_sec(); |
64 | p->lastupdate_sec = p->start_sec; | 64 | p->last_update_sec = p->start_sec; |
65 | p->lastsize = 0; | 65 | p->last_change_sec = p->start_sec; |
66 | p->last_size = 0; | ||
66 | } | 67 | } |
67 | 68 | ||
68 | /* File already had beg_size bytes. | 69 | /* File already had beg_size bytes. |
@@ -79,12 +80,15 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
79 | { | 80 | { |
80 | uoff_t beg_and_transferred; | 81 | uoff_t beg_and_transferred; |
81 | unsigned since_last_update, elapsed; | 82 | unsigned since_last_update, elapsed; |
82 | unsigned ratio; | ||
83 | int barlength; | 83 | int barlength; |
84 | int kiloscale; | 84 | int kiloscale; |
85 | 85 | ||
86 | //transferred = 1234; /* use for stall detection testing */ | ||
87 | //totalsize = 0; /* use for unknown size download testing */ | ||
88 | |||
86 | elapsed = monotonic_sec(); | 89 | elapsed = monotonic_sec(); |
87 | since_last_update = elapsed - p->lastupdate_sec; | 90 | since_last_update = elapsed - p->last_update_sec; |
91 | p->last_update_sec = elapsed; | ||
88 | 92 | ||
89 | if (totalsize != 0 && transferred >= totalsize - beg_size) { | 93 | if (totalsize != 0 && transferred >= totalsize - beg_size) { |
90 | /* Last call. Do not skip this update */ | 94 | /* Last call. Do not skip this update */ |
@@ -131,23 +135,27 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
131 | } | 135 | } |
132 | } | 136 | } |
133 | 137 | ||
134 | beg_and_transferred = beg_size + transferred; | ||
135 | |||
136 | ratio = 100 * beg_and_transferred / totalsize; | ||
137 | if (ENABLE_UNICODE_SUPPORT) | 138 | if (ENABLE_UNICODE_SUPPORT) |
138 | fprintf(stderr, "\r%s%4u%% ", p->curfile, ratio); | 139 | fprintf(stderr, "\r%s", p->curfile); |
139 | else | 140 | else |
140 | fprintf(stderr, "\r%-20.20s%4u%% ", p->curfile, ratio); | 141 | fprintf(stderr, "\r%-20.20s", p->curfile); |
141 | 142 | ||
142 | barlength = get_tty2_width() - 49; | 143 | beg_and_transferred = beg_size + transferred; |
143 | if (barlength > 0) { | 144 | |
144 | /* god bless gcc for variable arrays :) */ | 145 | if (totalsize != 0) { |
145 | char buf[barlength + 1]; | 146 | unsigned ratio = 100 * beg_and_transferred / totalsize; |
146 | unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize; | 147 | fprintf(stderr, "%4u%%", ratio); |
147 | memset(buf, ' ', barlength); | 148 | |
148 | buf[barlength] = '\0'; | 149 | barlength = get_tty2_width() - 49; |
149 | memset(buf, '*', stars); | 150 | if (barlength > 0) { |
150 | fprintf(stderr, "|%s|", buf); | 151 | /* god bless gcc for variable arrays :) */ |
152 | char buf[barlength + 1]; | ||
153 | unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize; | ||
154 | memset(buf, ' ', barlength); | ||
155 | buf[barlength] = '\0'; | ||
156 | memset(buf, '*', stars); | ||
157 | fprintf(stderr, " |%s|", buf); | ||
158 | } | ||
151 | } | 159 | } |
152 | 160 | ||
153 | while (beg_and_transferred >= 100000) { | 161 | while (beg_and_transferred >= 100000) { |
@@ -158,9 +166,10 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
158 | fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]); | 166 | fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]); |
159 | #define beg_and_transferred dont_use_beg_and_transferred_below() | 167 | #define beg_and_transferred dont_use_beg_and_transferred_below() |
160 | 168 | ||
161 | if (transferred != p->lastsize) { | 169 | since_last_update = elapsed - p->last_change_sec; |
162 | p->lastupdate_sec = elapsed; | 170 | if ((unsigned)transferred != p->last_size) { |
163 | p->lastsize = transferred; | 171 | p->last_change_sec = elapsed; |
172 | p->last_size = (unsigned)transferred; | ||
164 | if (since_last_update >= STALLTIME) { | 173 | if (since_last_update >= STALLTIME) { |
165 | /* We "cut out" these seconds from elapsed time | 174 | /* We "cut out" these seconds from elapsed time |
166 | * by adjusting start time */ | 175 | * by adjusting start time */ |
@@ -173,7 +182,7 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
173 | 182 | ||
174 | if (since_last_update >= STALLTIME) { | 183 | if (since_last_update >= STALLTIME) { |
175 | fprintf(stderr, " - stalled -"); | 184 | fprintf(stderr, " - stalled -"); |
176 | } else if (!totalsize || !transferred || (int)elapsed <= 0) { | 185 | } else if (!totalsize || !transferred || (int)elapsed < 0) { |
177 | fprintf(stderr, " --:--:-- ETA"); | 186 | fprintf(stderr, " --:--:-- ETA"); |
178 | } else { | 187 | } else { |
179 | unsigned eta, secs, hours; | 188 | unsigned eta, secs, hours; |
diff --git a/networking/tftp.c b/networking/tftp.c index 2a3991755..35cf0dbd9 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -119,7 +119,7 @@ static void tftp_progress_done(void) | |||
119 | if (is_bb_progress_inited(&G.pmt)) { | 119 | if (is_bb_progress_inited(&G.pmt)) { |
120 | tftp_progress_update(); | 120 | tftp_progress_update(); |
121 | bb_putchar_stderr('\n'); | 121 | bb_putchar_stderr('\n'); |
122 | bb_progress_free(p); | 122 | bb_progress_free(&G.pmt); |
123 | } | 123 | } |
124 | } | 124 | } |
125 | #else | 125 | #else |