blob: 266177b6ecd9fe5e407771b7971731bb91ec1d67 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "tool_setup.h"
25#include "tool_operate.h"
26#include "tool_progress.h"
27#include "tool_util.h"
28
29#define ENABLE_CURLX_PRINTF
30/* use our own printf() functions */
31#include "curlx.h"
32
33/* The point of this function would be to return a string of the input data,
34 but never longer than 5 columns (+ one zero byte).
35 Add suffix k, M, G when suitable... */
36static char *max5data(curl_off_t bytes, char *max5)
37{
38#define ONE_KILOBYTE CURL_OFF_T_C(1024)
39#define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
40#define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
41#define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
42#define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
43
44 if(bytes < CURL_OFF_T_C(100000))
45 msnprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
46
47 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
48 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
49
50 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
51 /* 'XX.XM' is good as long as we're less than 100 megs */
52 msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
53 CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
54 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
55
56#if (SIZEOF_CURL_OFF_T > 4)
57
58 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
59 /* 'XXXXM' is good until we're at 10000MB or above */
60 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
61
62 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
63 /* 10000 MB - 100 GB, we show it as XX.XG */
64 msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
65 CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
66 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
67
68 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
69 /* up to 10000GB, display without decimal: XXXXG */
70 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
71
72 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
73 /* up to 10000TB, display without decimal: XXXXT */
74 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
75
76 else
77 /* up to 10000PB, display without decimal: XXXXP */
78 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
79
80 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
81 can hold, but our data type is signed so 8192PB will be the maximum. */
82
83#else
84
85 else
86 msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
87
88#endif
89
90 return max5;
91}
92
93int xferinfo_cb(void *clientp,
94 curl_off_t dltotal,
95 curl_off_t dlnow,
96 curl_off_t ultotal,
97 curl_off_t ulnow)
98{
99 struct per_transfer *per = clientp;
100 struct OperationConfig *config = per->config;
101 per->dltotal = dltotal;
102 per->dlnow = dlnow;
103 per->ultotal = ultotal;
104 per->ulnow = ulnow;
105
106 if(per->abort)
107 return 1;
108
109 if(config->readbusy) {
110 config->readbusy = FALSE;
111 curl_easy_pause(per->curl, CURLPAUSE_CONT);
112 }
113
114 return 0;
115}
116
117/* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
118 byte) */
119static void time2str(char *r, curl_off_t seconds)
120{
121 curl_off_t h;
122 if(seconds <= 0) {
123 strcpy(r, "--:--:--");
124 return;
125 }
126 h = seconds / CURL_OFF_T_C(3600);
127 if(h <= CURL_OFF_T_C(99)) {
128 curl_off_t m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
129 curl_off_t s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
130 msnprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
131 ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
132 }
133 else {
134 /* this equals to more than 99 hours, switch to a more suitable output
135 format to fit within the limits. */
136 curl_off_t d = seconds / CURL_OFF_T_C(86400);
137 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
138 if(d <= CURL_OFF_T_C(999))
139 msnprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
140 "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
141 else
142 msnprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
143 }
144}
145
146static curl_off_t all_dltotal = 0;
147static curl_off_t all_ultotal = 0;
148static curl_off_t all_dlalready = 0;
149static curl_off_t all_ulalready = 0;
150
151curl_off_t all_xfers = 0; /* current total */
152
153struct speedcount {
154 curl_off_t dl;
155 curl_off_t ul;
156 struct timeval stamp;
157};
158#define SPEEDCNT 10
159static unsigned int speedindex;
160static bool indexwrapped;
161static struct speedcount speedstore[SPEEDCNT];
162
163/*
164 |DL% UL% Dled Uled Xfers Live Total Current Left Speed
165 | 6 -- 9.9G 0 2 2 0:00:40 0:00:02 0:00:37 4087M
166*/
167bool progress_meter(struct GlobalConfig *global,
168 struct timeval *start,
169 bool final)
170{
171 static struct timeval stamp;
172 static bool header = FALSE;
173 struct timeval now;
174 long diff;
175
176 if(global->noprogress)
177 return FALSE;
178
179 now = tvnow();
180 diff = tvdiff(now, stamp);
181
182 if(!header) {
183 header = TRUE;
184 fputs("DL% UL% Dled Uled Xfers Live "
185 "Total Current Left Speed\n",
186 global->errors);
187 }
188 if(final || (diff > 500)) {
189 char time_left[10];
190 char time_total[10];
191 char time_spent[10];
192 char buffer[3][6];
193 curl_off_t spent = tvdiff(now, *start)/1000;
194 char dlpercen[4]="--";
195 char ulpercen[4]="--";
196 struct per_transfer *per;
197 curl_off_t all_dlnow = 0;
198 curl_off_t all_ulnow = 0;
199 bool dlknown = TRUE;
200 bool ulknown = TRUE;
201 curl_off_t all_running = 0; /* in progress */
202 curl_off_t speed = 0;
203 unsigned int i;
204 stamp = now;
205
206 /* first add the amounts of the already completed transfers */
207 all_dlnow += all_dlalready;
208 all_ulnow += all_ulalready;
209
210 for(per = transfers; per; per = per->next) {
211 all_dlnow += per->dlnow;
212 all_ulnow += per->ulnow;
213 if(!per->dltotal)
214 dlknown = FALSE;
215 else if(!per->dltotal_added) {
216 /* only add this amount once */
217 all_dltotal += per->dltotal;
218 per->dltotal_added = TRUE;
219 }
220 if(!per->ultotal)
221 ulknown = FALSE;
222 else if(!per->ultotal_added) {
223 /* only add this amount once */
224 all_ultotal += per->ultotal;
225 per->ultotal_added = TRUE;
226 }
227 if(per->added)
228 all_running++;
229 }
230 if(dlknown && all_dltotal)
231 /* TODO: handle integer overflow */
232 msnprintf(dlpercen, sizeof(dlpercen), "%3" CURL_FORMAT_CURL_OFF_T,
233 all_dlnow * 100 / all_dltotal);
234 if(ulknown && all_ultotal)
235 /* TODO: handle integer overflow */
236 msnprintf(ulpercen, sizeof(ulpercen), "%3" CURL_FORMAT_CURL_OFF_T,
237 all_ulnow * 100 / all_ultotal);
238
239 /* get the transfer speed, the higher of the two */
240
241 i = speedindex;
242 speedstore[i].dl = all_dlnow;
243 speedstore[i].ul = all_ulnow;
244 speedstore[i].stamp = now;
245 if(++speedindex >= SPEEDCNT) {
246 indexwrapped = TRUE;
247 speedindex = 0;
248 }
249
250 {
251 long deltams;
252 curl_off_t dl;
253 curl_off_t ul;
254 curl_off_t dls;
255 curl_off_t uls;
256 if(indexwrapped) {
257 /* 'speedindex' is the oldest stored data */
258 deltams = tvdiff(now, speedstore[speedindex].stamp);
259 dl = all_dlnow - speedstore[speedindex].dl;
260 ul = all_ulnow - speedstore[speedindex].ul;
261 }
262 else {
263 /* since the beginning */
264 deltams = tvdiff(now, *start);
265 dl = all_dlnow;
266 ul = all_ulnow;
267 }
268 if(!deltams) /* no division by zero please */
269 deltams++;
270 dls = (curl_off_t)((double)dl / ((double)deltams/1000.0));
271 uls = (curl_off_t)((double)ul / ((double)deltams/1000.0));
272 speed = dls > uls ? dls : uls;
273 }
274
275
276 if(dlknown && speed) {
277 curl_off_t est = all_dltotal / speed;
278 curl_off_t left = (all_dltotal - all_dlnow) / speed;
279 time2str(time_left, left);
280 time2str(time_total, est);
281 }
282 else {
283 time2str(time_left, 0);
284 time2str(time_total, 0);
285 }
286 time2str(time_spent, spent);
287
288 fprintf(global->errors,
289 "\r"
290 "%-3s " /* percent downloaded */
291 "%-3s " /* percent uploaded */
292 "%s " /* Dled */
293 "%s " /* Uled */
294 "%5" CURL_FORMAT_CURL_OFF_T " " /* Xfers */
295 "%5" CURL_FORMAT_CURL_OFF_T " " /* Live */
296 " %s " /* Total time */
297 "%s " /* Current time */
298 "%s " /* Time left */
299 "%s " /* Speed */
300 "%5s" /* final newline */,
301
302 dlpercen, /* 3 letters */
303 ulpercen, /* 3 letters */
304 max5data(all_dlnow, buffer[0]),
305 max5data(all_ulnow, buffer[1]),
306 all_xfers,
307 all_running,
308 time_total,
309 time_spent,
310 time_left,
311 max5data(speed, buffer[2]), /* speed */
312 final ? "\n" :"");
313 return TRUE;
314 }
315 return FALSE;
316}
317
318void progress_finalize(struct per_transfer *per)
319{
320 /* get the numbers before this transfer goes away */
321 all_dlalready += per->dlnow;
322 all_ulalready += per->ulnow;
323 if(!per->dltotal_added) {
324 all_dltotal += per->dltotal;
325 per->dltotal_added = TRUE;
326 }
327 if(!per->ultotal_added) {
328 all_ultotal += per->ultotal;
329 per->ultotal_added = TRUE;
330 }
331}