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