Use double rather than float everywhere
[pacman-ng.git] / src / pacman / callback.c
blobda07b16181e15f03c575a22d0ec20c1718ef79fb
1 /*
2 * callback.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <sys/types.h> /* off_t */
28 #include <unistd.h>
29 #include <wchar.h>
31 #include <alpm.h>
33 /* pacman */
34 #include "callback.h"
35 #include "util.h"
36 #include "conf.h"
38 /* download progress bar */
39 static double rate_last;
40 static off_t xfered_last;
41 static off_t list_xfered = 0.0;
42 static off_t list_total = 0.0;
43 static struct timeval initial_time;
45 /* transaction progress bar */
46 static int prevpercent = 0; /* for less progressbar output */
48 /* delayed output during progress bar */
49 static int on_progress = 0;
50 static alpm_list_t *output = NULL;
52 /* Silly little helper function, determines if the caller needs a visual update
53 * since the last time this function was called.
54 * This is made for the two progress bar functions, to prevent flicker
56 * first_call indicates if this is the first time it is called, for
57 * initialization purposes */
58 static double get_update_timediff(int first_call)
60 double retval = 0.0;
61 static struct timeval last_time = {0, 0};
63 /* on first call, simply set the last time and return */
64 if(first_call) {
65 gettimeofday(&last_time, NULL);
66 } else {
67 struct timeval this_time;
68 double diff_sec, diff_usec;
70 gettimeofday(&this_time, NULL);
71 diff_sec = this_time.tv_sec - last_time.tv_sec;
72 diff_usec = this_time.tv_usec - last_time.tv_usec;
74 retval = diff_sec + (diff_usec / 1000000.0);
76 /* return 0 and do not update last_time if interval was too short */
77 if(retval < UPDATE_SPEED_SEC) {
78 retval = 0.0;
79 } else {
80 last_time = this_time;
84 return(retval);
87 /* refactored from cb_trans_progress */
88 static void fill_progress(const int bar_percent, const int disp_percent,
89 const int proglen)
91 /* 8 = 1 space + 1 [ + 1 ] + 5 for percent */
92 const int hashlen = proglen - 8;
93 const int hash = bar_percent * hashlen / 100;
94 static int lasthash = 0, mouth = 0;
95 int i;
97 if(bar_percent == 0) {
98 lasthash = 0;
99 mouth = 0;
102 if(hashlen > 0) {
103 printf(" [");
104 for(i = hashlen; i > 0; --i) {
105 /* if special progress bar enabled */
106 if(config->chomp) {
107 if(i > hashlen - hash) {
108 printf("-");
109 } else if(i == hashlen - hash) {
110 if(lasthash == hash) {
111 if(mouth) {
112 printf("\033[1;33mC\033[m");
113 } else {
114 printf("\033[1;33mc\033[m");
116 } else {
117 lasthash = hash;
118 mouth = mouth == 1 ? 0 : 1;
119 if(mouth) {
120 printf("\033[1;33mC\033[m");
121 } else {
122 printf("\033[1;33mc\033[m");
125 } else if(i%3 == 0) {
126 printf("\033[0;37mo\033[m");
127 } else {
128 printf("\033[0;37m \033[m");
130 } /* else regular progress bar */
131 else if(i > hashlen - hash) {
132 printf("#");
133 } else {
134 printf("-");
137 printf("]");
139 /* print display percent after progress bar */
140 /* 5 = 1 space + 3 digits + 1 % */
141 if(proglen >= 5) {
142 printf(" %3d%%", disp_percent);
145 if(bar_percent == 100) {
146 printf("\n");
147 } else {
148 printf("\r");
150 fflush(stdout);
155 /* callback to handle messages/notifications from libalpm transactions */
156 void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
158 switch(event) {
159 case PM_TRANS_EVT_CHECKDEPS_START:
160 printf(_("checking dependencies...\n"));
161 break;
162 case PM_TRANS_EVT_FILECONFLICTS_START:
163 if(config->noprogressbar) {
164 printf(_("checking for file conflicts...\n"));
166 break;
167 case PM_TRANS_EVT_RESOLVEDEPS_START:
168 printf(_("resolving dependencies...\n"));
169 break;
170 case PM_TRANS_EVT_INTERCONFLICTS_START:
171 printf(_("looking for inter-conflicts...\n"));
172 break;
173 case PM_TRANS_EVT_ADD_START:
174 if(config->noprogressbar) {
175 printf(_("installing %s...\n"), alpm_pkg_get_name(data1));
177 break;
178 case PM_TRANS_EVT_ADD_DONE:
179 alpm_logaction("installed %s (%s)\n",
180 alpm_pkg_get_name(data1),
181 alpm_pkg_get_version(data1));
182 display_optdepends(data1);
183 break;
184 case PM_TRANS_EVT_REMOVE_START:
185 if(config->noprogressbar) {
186 printf(_("removing %s...\n"), alpm_pkg_get_name(data1));
188 break;
189 case PM_TRANS_EVT_REMOVE_DONE:
190 alpm_logaction("removed %s (%s)\n",
191 alpm_pkg_get_name(data1),
192 alpm_pkg_get_version(data1));
193 break;
194 case PM_TRANS_EVT_UPGRADE_START:
195 if(config->noprogressbar) {
196 printf(_("upgrading %s...\n"), alpm_pkg_get_name(data1));
198 break;
199 case PM_TRANS_EVT_UPGRADE_DONE:
200 alpm_logaction("upgraded %s (%s -> %s)\n",
201 (char *)alpm_pkg_get_name(data1),
202 (char *)alpm_pkg_get_version(data2),
203 (char *)alpm_pkg_get_version(data1));
204 display_new_optdepends(data2,data1);
205 break;
206 case PM_TRANS_EVT_INTEGRITY_START:
207 printf(_("checking package integrity...\n"));
208 break;
209 case PM_TRANS_EVT_DELTA_INTEGRITY_START:
210 printf(_("checking delta integrity...\n"));
211 break;
212 case PM_TRANS_EVT_DELTA_PATCHES_START:
213 printf(_("applying deltas...\n"));
214 break;
215 case PM_TRANS_EVT_DELTA_PATCH_START:
216 printf(_("generating %s with %s... "), (char *)data1, (char *)data2);
217 break;
218 case PM_TRANS_EVT_DELTA_PATCH_DONE:
219 printf(_("success!\n"));
220 break;
221 case PM_TRANS_EVT_DELTA_PATCH_FAILED:
222 printf(_("failed.\n"));
223 break;
224 case PM_TRANS_EVT_SCRIPTLET_INFO:
225 printf("%s", (char*)data1);
226 break;
227 case PM_TRANS_EVT_RETRIEVE_START:
228 printf(_(":: Retrieving packages from %s...\n"), (char*)data1);
229 break;
230 case PM_TRANS_EVT_DISKSPACE_START:
231 if(config->noprogressbar) {
232 printf(_("checking available disk space...\n"));
234 break;
235 /* all the simple done events, with fallthrough for each */
236 case PM_TRANS_EVT_FILECONFLICTS_DONE:
237 case PM_TRANS_EVT_CHECKDEPS_DONE:
238 case PM_TRANS_EVT_RESOLVEDEPS_DONE:
239 case PM_TRANS_EVT_INTERCONFLICTS_DONE:
240 case PM_TRANS_EVT_INTEGRITY_DONE:
241 case PM_TRANS_EVT_DELTA_INTEGRITY_DONE:
242 case PM_TRANS_EVT_DELTA_PATCHES_DONE:
243 case PM_TRANS_EVT_DISKSPACE_DONE:
244 /* nothing */
245 break;
247 fflush(stdout);
250 /* callback to handle questions from libalpm transactions (yes/no) */
251 /* TODO this is one of the worst ever functions written. void *data ? wtf */
252 void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
253 void *data3, int *response)
255 switch(event) {
256 case PM_TRANS_CONV_INSTALL_IGNOREPKG:
257 *response = yesno(_(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
258 alpm_pkg_get_name(data1));
259 break;
260 case PM_TRANS_CONV_REPLACE_PKG:
261 *response = yesno(_(":: Replace %s with %s/%s?"),
262 alpm_pkg_get_name(data1),
263 (char *)data3,
264 alpm_pkg_get_name(data2));
265 break;
266 case PM_TRANS_CONV_CONFLICT_PKG:
267 /* data parameters: target package, local package, conflict (strings) */
268 /* print conflict only if it contains new information */
269 if(strcmp(data1, data3) == 0 || strcmp(data2, data3) == 0) {
270 *response = noyes(_(":: %s and %s are in conflict. Remove %s?"),
271 (char *)data1,
272 (char *)data2,
273 (char *)data2);
274 } else {
275 *response = noyes(_(":: %s and %s are in conflict (%s). Remove %s?"),
276 (char *)data1,
277 (char *)data2,
278 (char *)data3,
279 (char *)data2);
281 break;
282 case PM_TRANS_CONV_REMOVE_PKGS:
284 alpm_list_t *unresolved = (alpm_list_t *) data1;
285 alpm_list_t *namelist = NULL, *i;
286 for (i = unresolved; i; i = i->next) {
287 namelist = alpm_list_add(namelist,
288 (char *)alpm_pkg_get_name(i->data));
290 printf(_n(
291 ":: The following package cannot be upgraded due to unresolvable dependencies:\n",
292 ":: The following packages cannot be upgraded due to unresolvable dependencies:\n",
293 alpm_list_count(namelist)));
294 list_display(" ", namelist);
295 printf("\n");
296 *response = noyes(_n(
297 "Do you want to skip the above package for this upgrade?",
298 "Do you want to skip the above packages for this upgrade?",
299 alpm_list_count(namelist)));
300 alpm_list_free(namelist);
302 break;
303 case PM_TRANS_CONV_LOCAL_NEWER:
304 if(!config->op_s_downloadonly) {
305 *response = yesno(_(":: %s-%s: local version is newer. Upgrade anyway?"),
306 alpm_pkg_get_name(data1),
307 alpm_pkg_get_version(data1));
308 } else {
309 *response = 1;
311 break;
312 case PM_TRANS_CONV_CORRUPTED_PKG:
313 *response = yesno(_(":: File %s is corrupted. Do you want to delete it?"),
314 (char *)data1);
315 break;
317 if(config->noask) {
318 if(config->ask & event) {
319 /* inverse the default answer */
320 *response = !*response;
325 /* callback to handle display of transaction progress */
326 void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
327 size_t howmany, size_t current)
329 /* size of line to allocate for text printing (e.g. not progressbar) */
330 int infolen;
331 int digits, textlen;
332 size_t tmp;
333 char *opr = NULL;
334 /* used for wide character width determination and printing */
335 int len, wclen, wcwid, padwid;
336 wchar_t *wcstr;
338 if(config->noprogressbar) {
339 return;
342 if(percent == 0) {
343 get_update_timediff(1);
344 } else if(percent == 100) {
345 /* no need for timediff update, but unconditionally continue unless we
346 * already completed on a previous call */
347 if(prevpercent == 100) {
348 return;
350 } else {
351 if(!pkgname || percent == prevpercent || get_update_timediff(0) < UPDATE_SPEED_SEC) {
352 /* only update the progress bar when we have a package name, the
353 * percentage has changed, and it has been long enough. */
354 return;
358 prevpercent = percent;
360 /* set text of message to display */
361 switch (event) {
362 case PM_TRANS_PROGRESS_ADD_START:
363 opr = _("installing");
364 break;
365 case PM_TRANS_PROGRESS_UPGRADE_START:
366 opr = _("upgrading");
367 break;
368 case PM_TRANS_PROGRESS_REMOVE_START:
369 opr = _("removing");
370 break;
371 case PM_TRANS_PROGRESS_CONFLICTS_START:
372 opr = _("checking for file conflicts");
373 break;
374 case PM_TRANS_PROGRESS_DISKSPACE_START:
375 opr = _("checking available disk space");
376 break;
377 default:
378 return;
381 infolen = getcols() * 6 / 10;
382 if (infolen < 50) {
383 infolen = 50;
386 /* find # of digits in package counts to scale output */
387 digits = 1;
388 tmp = howmany;
389 while((tmp /= 10)) {
390 ++digits;
392 /* determine room left for non-digits text [not ( 1/12) part] */
393 textlen = infolen - 3 /* (/) */ - (2 * digits) - 1 /* space */;
395 /* In order to deal with characters from all locales, we have to worry
396 * about wide characters and their column widths. A lot of stuff is
397 * done here to figure out the actual number of screen columns used
398 * by the output, and then pad it accordingly so we fill the terminal.
400 /* len = opr len + pkgname len (if available) + space + null */
401 len = strlen(opr) + ((pkgname) ? strlen(pkgname) : 0) + 2;
402 wcstr = calloc(len, sizeof(wchar_t));
403 /* print our strings to the alloc'ed memory */
404 #if defined(HAVE_SWPRINTF)
405 wclen = swprintf(wcstr, len, L"%s %s", opr, pkgname);
406 #else
407 /* because the format string was simple, we can easily do this without
408 * using swprintf, although it is probably not as safe/fast. The max
409 * chars we can copy is decremented each time by subtracting the length
410 * of the already printed/copied wide char string. */
411 wclen = mbstowcs(wcstr, opr, len);
412 wclen += mbstowcs(wcstr + wclen, " ", len - wclen);
413 wclen += mbstowcs(wcstr + wclen, pkgname, len - wclen);
414 #endif
415 wcwid = wcswidth(wcstr, wclen);
416 padwid = textlen - wcwid;
417 /* if padwid is < 0, we need to trim the string so padwid = 0 */
418 if(padwid < 0) {
419 int i = textlen - 3;
420 wchar_t *p = wcstr;
421 /* grab the max number of char columns we can fill */
422 while(i > 0 && wcwidth(*p) < i) {
423 i -= wcwidth(*p);
424 p++;
426 /* then add the ellipsis and fill out any extra padding */
427 wcscpy(p, L"...");
428 padwid = i;
432 printf("(%*ld/%*ld) %ls%-*s", digits, (unsigned long)current,
433 digits, (unsigned long)howmany, wcstr, padwid, "");
435 free(wcstr);
437 /* call refactored fill progress function */
438 fill_progress(percent, percent, getcols() - infolen);
440 if(percent == 100) {
441 alpm_list_t *i = NULL;
442 on_progress = 0;
443 for(i = output; i; i = i->next) {
444 printf("%s", (char *)i->data);
446 fflush(stdout);
447 FREELIST(output);
448 } else {
449 on_progress = 1;
453 /* callback to handle receipt of total download value */
454 void cb_dl_total(off_t total)
456 list_total = total;
457 /* if we get a 0 value, it means this list has finished downloading,
458 * so clear out our list_xfered as well */
459 if(total == 0) {
460 list_xfered = 0;
464 /* callback to handle display of download progress */
465 void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
467 int infolen;
468 int filenamelen;
469 char *fname, *p;
470 /* used for wide character width determination and printing */
471 int len, wclen, wcwid, padwid;
472 wchar_t *wcfname;
474 int totaldownload = 0;
475 off_t xfered, total;
476 double rate = 0.0, timediff = 0.0, f_xfered = 0.0;
477 unsigned int eta_h = 0, eta_m = 0, eta_s = 0;
478 int file_percent = 0, total_percent = 0;
479 char rate_size = 'K', xfered_size = 'K';
481 if(config->noprogressbar || file_total == -1) {
482 if(file_xfered == 0) {
483 printf(_("downloading %s...\n"), filename);
484 fflush(stdout);
486 return;
489 infolen = getcols() * 6 / 10;
490 if (infolen < 50) {
491 infolen = 50;
493 /* explanation of magic 28 number at the end */
494 filenamelen = infolen - 28;
496 /* only use TotalDownload if enabled and we have a callback value */
497 if(config->totaldownload && list_total) {
498 /* sanity check */
499 if(list_xfered + file_total <= list_total) {
500 totaldownload = 1;
501 } else {
502 /* bogus values : don't enable totaldownload and reset */
503 list_xfered = 0;
504 list_total = 0;
508 if(totaldownload) {
509 xfered = list_xfered + file_xfered;
510 total = list_total;
511 } else {
512 xfered = file_xfered;
513 total = file_total;
516 /* bogus values : stop here */
517 if(xfered > total) {
518 return;
521 /* this is basically a switch on xfered: 0, total, and
522 * anything else */
523 if(file_xfered == 0) {
524 /* set default starting values, ensure we only call this once
525 * if TotalDownload is enabled */
526 if(!totaldownload || (totaldownload && list_xfered == 0)) {
527 gettimeofday(&initial_time, NULL);
528 xfered_last = (off_t)0;
529 rate_last = 0.0;
530 get_update_timediff(1);
532 } else if(file_xfered == file_total) {
533 /* compute final values */
534 struct timeval current_time;
535 double diff_sec, diff_usec;
537 gettimeofday(&current_time, NULL);
538 diff_sec = current_time.tv_sec - initial_time.tv_sec;
539 diff_usec = current_time.tv_usec - initial_time.tv_usec;
540 timediff = diff_sec + (diff_usec / 1000000.0);
541 rate = xfered / (timediff * 1024.0);
543 /* round elapsed time to the nearest second */
544 eta_s = (int)(timediff + 0.5);
545 } else {
546 /* compute current average values */
547 timediff = get_update_timediff(0);
549 if(timediff < UPDATE_SPEED_SEC) {
550 /* return if the calling interval was too short */
551 return;
553 rate = (xfered - xfered_last) / (timediff * 1024.0);
554 /* average rate to reduce jumpiness */
555 rate = (rate + 2 * rate_last) / 3;
556 eta_s = (total - xfered) / (rate * 1024.0);
557 rate_last = rate;
558 xfered_last = xfered;
561 file_percent = (file_xfered * 100) / file_total;
563 if(totaldownload) {
564 total_percent = ((list_xfered + file_xfered) * 100) /
565 list_total;
567 /* if we are at the end, add the completed file to list_xfered */
568 if(file_xfered == file_total) {
569 list_xfered += file_total;
573 /* fix up time for display */
574 eta_h = eta_s / 3600;
575 eta_s -= eta_h * 3600;
576 eta_m = eta_s / 60;
577 eta_s -= eta_m * 60;
579 fname = strdup(filename);
580 /* strip package or DB extension for cleaner look */
581 if((p = strstr(fname, ".pkg")) || (p = strstr(fname, ".db"))) {
582 *p = '\0';
584 /* In order to deal with characters from all locales, we have to worry
585 * about wide characters and their column widths. A lot of stuff is
586 * done here to figure out the actual number of screen columns used
587 * by the output, and then pad it accordingly so we fill the terminal.
589 /* len = filename len + null */
590 len = strlen(filename) + 1;
591 wcfname = calloc(len, sizeof(wchar_t));
592 wclen = mbstowcs(wcfname, fname, len);
593 wcwid = wcswidth(wcfname, wclen);
594 padwid = filenamelen - wcwid;
595 /* if padwid is < 0, we need to trim the string so padwid = 0 */
596 if(padwid < 0) {
597 int i = filenamelen - 3;
598 wchar_t *p = wcfname;
599 /* grab the max number of char columns we can fill */
600 while(i > 0 && wcwidth(*p) < i) {
601 i -= wcwidth(*p);
602 p++;
604 /* then add the ellipsis and fill out any extra padding */
605 wcscpy(p, L"...");
606 padwid = i;
610 /* Awesome formatting for progress bar. We need a mess of Kb->Mb->Gb stuff
611 * here. We'll use limit of 2048 for each until we get some empirical */
612 /* rate_size = 'K'; was set above */
613 if(rate > 2048.0) {
614 rate /= 1024.0;
615 rate_size = 'M';
616 if(rate > 2048.0) {
617 rate /= 1024.0;
618 rate_size = 'G';
619 /* we should not go higher than this for a few years (9999.9 Gb/s?)*/
623 f_xfered = xfered / 1024.0; /* convert to K by default */
624 /* xfered_size = 'K'; was set above */
625 if(f_xfered > 2048.0) {
626 f_xfered /= 1024.0;
627 xfered_size = 'M';
628 if(f_xfered > 2048.0) {
629 f_xfered /= 1024.0;
630 xfered_size = 'G';
631 /* I should seriously hope that archlinux packages never break
632 * the 9999.9GB mark... we'd have more serious problems than the progress
633 * bar in pacman */
637 /* 1 space + filenamelen + 1 space + 7 for size + 1 + 7 for rate + 2 for /s + 1 space + 8 for eta */
638 printf(" %ls%-*s %6.1f%c %#6.1f%c/s %02u:%02u:%02u", wcfname,
639 padwid, "", f_xfered, xfered_size,
640 rate, rate_size, eta_h, eta_m, eta_s);
642 free(fname);
643 free(wcfname);
645 if(totaldownload) {
646 fill_progress(file_percent, total_percent, getcols() - infolen);
647 } else {
648 fill_progress(file_percent, file_percent, getcols() - infolen);
650 return;
653 /* Callback to handle notifications from the library */
654 void cb_log(pmloglevel_t level, char *fmt, va_list args)
656 if(!fmt || strlen(fmt) == 0) {
657 return;
660 if(on_progress) {
661 char *string = NULL;
662 pm_vasprintf(&string, level, fmt, args);
663 if(string != NULL) {
664 output = alpm_list_add(output, string);
666 } else {
667 pm_vfprintf(stdout, level, fmt, args);
671 /* vim: set ts=2 sw=2 noet: */