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/>.
27 #include <sys/types.h> /* off_t */
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
)
61 static struct timeval last_time
= {0, 0};
63 /* on first call, simply set the last time and return */
65 gettimeofday(&last_time
, NULL
);
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
) {
80 last_time
= this_time
;
87 /* refactored from cb_trans_progress */
88 static void fill_progress(const int bar_percent
, const int disp_percent
,
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;
97 if(bar_percent
== 0) {
104 for(i
= hashlen
; i
> 0; --i
) {
105 /* if special progress bar enabled */
107 if(i
> hashlen
- hash
) {
109 } else if(i
== hashlen
- hash
) {
110 if(lasthash
== hash
) {
112 printf("\033[1;33mC\033[m");
114 printf("\033[1;33mc\033[m");
118 mouth
= mouth
== 1 ? 0 : 1;
120 printf("\033[1;33mC\033[m");
122 printf("\033[1;33mc\033[m");
125 } else if(i
%3 == 0) {
126 printf("\033[0;37mo\033[m");
128 printf("\033[0;37m \033[m");
130 } /* else regular progress bar */
131 else if(i
> hashlen
- hash
) {
139 /* print display percent after progress bar */
140 /* 5 = 1 space + 3 digits + 1 % */
142 printf(" %3d%%", disp_percent
);
145 if(bar_percent
== 100) {
155 /* callback to handle messages/notifications from libalpm transactions */
156 void cb_trans_evt(alpm_transevt_t event
, void *data1
, void *data2
)
159 case ALPM_TRANS_EVT_CHECKDEPS_START
:
160 printf(_("checking dependencies...\n"));
162 case ALPM_TRANS_EVT_FILECONFLICTS_START
:
163 if(config
->noprogressbar
) {
164 printf(_("checking for file conflicts...\n"));
167 case ALPM_TRANS_EVT_RESOLVEDEPS_START
:
168 printf(_("resolving dependencies...\n"));
170 case ALPM_TRANS_EVT_INTERCONFLICTS_START
:
171 printf(_("looking for inter-conflicts...\n"));
173 case ALPM_TRANS_EVT_ADD_START
:
174 if(config
->noprogressbar
) {
175 printf(_("installing %s...\n"), alpm_pkg_get_name(data1
));
178 case ALPM_TRANS_EVT_ADD_DONE
:
179 alpm_logaction(config
->handle
, "installed %s (%s)\n",
180 alpm_pkg_get_name(data1
),
181 alpm_pkg_get_version(data1
));
182 display_optdepends(data1
);
184 case ALPM_TRANS_EVT_REMOVE_START
:
185 if(config
->noprogressbar
) {
186 printf(_("removing %s...\n"), alpm_pkg_get_name(data1
));
189 case ALPM_TRANS_EVT_REMOVE_DONE
:
190 alpm_logaction(config
->handle
, "removed %s (%s)\n",
191 alpm_pkg_get_name(data1
),
192 alpm_pkg_get_version(data1
));
194 case ALPM_TRANS_EVT_UPGRADE_START
:
195 if(config
->noprogressbar
) {
196 printf(_("upgrading %s...\n"), alpm_pkg_get_name(data1
));
199 case ALPM_TRANS_EVT_UPGRADE_DONE
:
200 alpm_logaction(config
->handle
, "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
);
206 case ALPM_TRANS_EVT_INTEGRITY_START
:
207 if(config
->noprogressbar
) {
208 printf(_("checking package integrity...\n"));
211 case ALPM_TRANS_EVT_DELTA_INTEGRITY_START
:
212 printf(_("checking delta integrity...\n"));
214 case ALPM_TRANS_EVT_DELTA_PATCHES_START
:
215 printf(_("applying deltas...\n"));
217 case ALPM_TRANS_EVT_DELTA_PATCH_START
:
218 printf(_("generating %s with %s... "), (char *)data1
, (char *)data2
);
220 case ALPM_TRANS_EVT_DELTA_PATCH_DONE
:
221 printf(_("success!\n"));
223 case ALPM_TRANS_EVT_DELTA_PATCH_FAILED
:
224 printf(_("failed.\n"));
226 case ALPM_TRANS_EVT_SCRIPTLET_INFO
:
227 printf("%s", (char *)data1
);
229 case ALPM_TRANS_EVT_RETRIEVE_START
:
230 printf(_(":: Retrieving packages from %s...\n"), (char *)data1
);
232 case ALPM_TRANS_EVT_DISKSPACE_START
:
233 if(config
->noprogressbar
) {
234 printf(_("checking available disk space...\n"));
237 /* all the simple done events, with fallthrough for each */
238 case ALPM_TRANS_EVT_FILECONFLICTS_DONE
:
239 case ALPM_TRANS_EVT_CHECKDEPS_DONE
:
240 case ALPM_TRANS_EVT_RESOLVEDEPS_DONE
:
241 case ALPM_TRANS_EVT_INTERCONFLICTS_DONE
:
242 case ALPM_TRANS_EVT_INTEGRITY_DONE
:
243 case ALPM_TRANS_EVT_DELTA_INTEGRITY_DONE
:
244 case ALPM_TRANS_EVT_DELTA_PATCHES_DONE
:
245 case ALPM_TRANS_EVT_DISKSPACE_DONE
:
252 /* callback to handle questions from libalpm transactions (yes/no) */
253 /* TODO this is one of the worst ever functions written. void *data ? wtf */
254 void cb_trans_conv(alpm_transconv_t event
, void *data1
, void *data2
,
255 void *data3
, int *response
)
258 case PM_TRANS_CONV_INSTALL_IGNOREPKG
:
259 if(!config
->op_s_downloadonly
) {
260 *response
= yesno(_(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
261 alpm_pkg_get_name(data1
));
266 case PM_TRANS_CONV_REPLACE_PKG
:
267 *response
= yesno(_(":: Replace %s with %s/%s?"),
268 alpm_pkg_get_name(data1
),
270 alpm_pkg_get_name(data2
));
272 case PM_TRANS_CONV_CONFLICT_PKG
:
273 /* data parameters: target package, local package, conflict (strings) */
274 /* print conflict only if it contains new information */
275 if(strcmp(data1
, data3
) == 0 || strcmp(data2
, data3
) == 0) {
276 *response
= noyes(_(":: %s and %s are in conflict. Remove %s?"),
281 *response
= noyes(_(":: %s and %s are in conflict (%s). Remove %s?"),
288 case PM_TRANS_CONV_REMOVE_PKGS
:
290 alpm_list_t
*unresolved
= (alpm_list_t
*) data1
;
291 alpm_list_t
*namelist
= NULL
, *i
;
293 for (i
= unresolved
; i
; i
= i
->next
) {
294 namelist
= alpm_list_add(namelist
,
295 (char *)alpm_pkg_get_name(i
->data
));
299 ":: The following package cannot be upgraded due to unresolvable dependencies:\n",
300 ":: The following packages cannot be upgraded due to unresolvable dependencies:\n",
302 list_display(" ", namelist
);
304 *response
= noyes(_n(
305 "Do you want to skip the above package for this upgrade?",
306 "Do you want to skip the above packages for this upgrade?",
308 alpm_list_free(namelist
);
311 case PM_TRANS_CONV_SELECT_PROVIDER
:
313 alpm_list_t
*providers
= (alpm_list_t
*)data1
;
314 int count
= alpm_list_count(providers
);
315 char *depstring
= alpm_dep_compute_string((alpm_depend_t
*)data2
);
316 printf(_(":: There are %d providers available for %s:\n"), count
,
319 select_display(providers
);
320 *response
= select_question(count
);
323 case PM_TRANS_CONV_LOCAL_NEWER
:
324 if(!config
->op_s_downloadonly
) {
325 *response
= yesno(_(":: %s-%s: local version is newer. Upgrade anyway?"),
326 alpm_pkg_get_name(data1
),
327 alpm_pkg_get_version(data1
));
332 case PM_TRANS_CONV_CORRUPTED_PKG
:
333 *response
= yesno(_(":: File %s is corrupted. Do you want to delete it?"),
338 if(config
->ask
& event
) {
339 /* inverse the default answer */
340 *response
= !*response
;
345 /* callback to handle display of transaction progress */
346 void cb_trans_progress(alpm_transprog_t event
, const char *pkgname
, int percent
,
347 size_t howmany
, size_t current
)
349 /* size of line to allocate for text printing (e.g. not progressbar) */
354 /* used for wide character width determination and printing */
355 int len
, wclen
, wcwid
, padwid
;
358 const int cols
= getcols();
360 if(config
->noprogressbar
|| cols
== 0) {
365 get_update_timediff(1);
366 } else if(percent
== 100) {
367 /* no need for timediff update, but unconditionally continue unless we
368 * already completed on a previous call */
369 if(prevpercent
== 100) {
373 if(!pkgname
|| percent
== prevpercent
|| get_update_timediff(0) < UPDATE_SPEED_SEC
) {
374 /* only update the progress bar when we have a package name, the
375 * percentage has changed, and it has been long enough. */
380 prevpercent
= percent
;
382 /* set text of message to display */
384 case PM_TRANS_PROGRESS_ADD_START
:
385 opr
= _("installing");
387 case PM_TRANS_PROGRESS_UPGRADE_START
:
388 opr
= _("upgrading");
390 case PM_TRANS_PROGRESS_REMOVE_START
:
393 case PM_TRANS_PROGRESS_CONFLICTS_START
:
394 opr
= _("checking for file conflicts");
396 case PM_TRANS_PROGRESS_DISKSPACE_START
:
397 opr
= _("checking available disk space");
399 case PM_TRANS_PROGRESS_INTEGRITY_START
:
400 opr
= _("checking package integrity");
406 infolen
= cols
* 6 / 10;
411 /* find # of digits in package counts to scale output */
417 /* determine room left for non-digits text [not ( 1/12) part] */
418 textlen
= infolen
- 3 /* (/) */ - (2 * digits
) - 1 /* space */;
420 /* In order to deal with characters from all locales, we have to worry
421 * about wide characters and their column widths. A lot of stuff is
422 * done here to figure out the actual number of screen columns used
423 * by the output, and then pad it accordingly so we fill the terminal.
425 /* len = opr len + pkgname len (if available) + space + null */
426 len
= strlen(opr
) + ((pkgname
) ? strlen(pkgname
) : 0) + 2;
427 wcstr
= calloc(len
, sizeof(wchar_t));
428 /* print our strings to the alloc'ed memory */
429 #if defined(HAVE_SWPRINTF)
430 wclen
= swprintf(wcstr
, len
, L
"%s %s", opr
, pkgname
);
432 /* because the format string was simple, we can easily do this without
433 * using swprintf, although it is probably not as safe/fast. The max
434 * chars we can copy is decremented each time by subtracting the length
435 * of the already printed/copied wide char string. */
436 wclen
= mbstowcs(wcstr
, opr
, len
);
437 wclen
+= mbstowcs(wcstr
+ wclen
, " ", len
- wclen
);
438 wclen
+= mbstowcs(wcstr
+ wclen
, pkgname
, len
- wclen
);
440 wcwid
= wcswidth(wcstr
, wclen
);
441 padwid
= textlen
- wcwid
;
442 /* if padwid is < 0, we need to trim the string so padwid = 0 */
446 /* grab the max number of char columns we can fill */
447 while(i
> 0 && wcwidth(*p
) < i
) {
451 /* then add the ellipsis and fill out any extra padding */
457 printf("(%*ld/%*ld) %ls%-*s", digits
, (unsigned long)current
,
458 digits
, (unsigned long)howmany
, wcstr
, padwid
, "");
462 /* call refactored fill progress function */
463 fill_progress(percent
, percent
, cols
- infolen
);
466 alpm_list_t
*i
= NULL
;
468 for(i
= output
; i
; i
= i
->next
) {
469 printf("%s", (char *)i
->data
);
478 /* callback to handle receipt of total download value */
479 void cb_dl_total(off_t total
)
482 /* if we get a 0 value, it means this list has finished downloading,
483 * so clear out our list_xfered as well */
489 /* callback to handle display of download progress */
490 void cb_dl_progress(const char *filename
, off_t file_xfered
, off_t file_total
)
495 /* used for wide character width determination and printing */
496 int len
, wclen
, wcwid
, padwid
;
499 int totaldownload
= 0;
501 double rate
= 0.0, timediff
= 0.0;
502 unsigned int eta_h
= 0, eta_m
= 0, eta_s
= 0;
503 double rate_human
, xfered_human
;
504 const char *rate_label
, *xfered_label
;
505 int file_percent
= 0, total_percent
= 0;
507 const int cols
= getcols();
509 if(config
->noprogressbar
|| cols
== 0 || file_total
== -1) {
510 if(file_xfered
== 0) {
511 printf(_("downloading %s...\n"), filename
);
517 infolen
= cols
* 6 / 10;
521 /* explanation of magic 28 number at the end */
522 filenamelen
= infolen
- 28;
524 /* only use TotalDownload if enabled and we have a callback value */
525 if(config
->totaldownload
&& list_total
) {
527 if(list_xfered
+ file_total
<= list_total
) {
530 /* bogus values : don't enable totaldownload and reset */
537 xfered
= list_xfered
+ file_xfered
;
540 xfered
= file_xfered
;
544 /* bogus values : stop here */
549 /* this is basically a switch on xfered: 0, total, and
551 if(file_xfered
== 0) {
552 /* set default starting values, ensure we only call this once
553 * if TotalDownload is enabled */
554 if(!totaldownload
|| (totaldownload
&& list_xfered
== 0)) {
555 gettimeofday(&initial_time
, NULL
);
556 xfered_last
= (off_t
)0;
558 get_update_timediff(1);
560 } else if(file_xfered
== file_total
) {
561 /* compute final values */
562 struct timeval current_time
;
563 double diff_sec
, diff_usec
;
565 gettimeofday(¤t_time
, NULL
);
566 diff_sec
= current_time
.tv_sec
- initial_time
.tv_sec
;
567 diff_usec
= current_time
.tv_usec
- initial_time
.tv_usec
;
568 timediff
= diff_sec
+ (diff_usec
/ 1000000.0);
569 rate
= xfered
/ timediff
;
571 /* round elapsed time to the nearest second */
572 eta_s
= (int)(timediff
+ 0.5);
574 /* compute current average values */
575 timediff
= get_update_timediff(0);
577 if(timediff
< UPDATE_SPEED_SEC
) {
578 /* return if the calling interval was too short */
581 rate
= (xfered
- xfered_last
) / timediff
;
582 /* average rate to reduce jumpiness */
583 rate
= (rate
+ 2 * rate_last
) / 3;
584 eta_s
= (total
- xfered
) / rate
;
586 xfered_last
= xfered
;
589 file_percent
= (file_xfered
* 100) / file_total
;
592 total_percent
= ((list_xfered
+ file_xfered
) * 100) /
595 /* if we are at the end, add the completed file to list_xfered */
596 if(file_xfered
== file_total
) {
597 list_xfered
+= file_total
;
601 /* fix up time for display */
602 eta_h
= eta_s
/ 3600;
603 eta_s
-= eta_h
* 3600;
607 fname
= strdup(filename
);
608 /* strip package or DB extension for cleaner look */
609 if((p
= strstr(fname
, ".pkg")) || (p
= strstr(fname
, ".db"))) {
612 /* In order to deal with characters from all locales, we have to worry
613 * about wide characters and their column widths. A lot of stuff is
614 * done here to figure out the actual number of screen columns used
615 * by the output, and then pad it accordingly so we fill the terminal.
617 /* len = filename len + null */
618 len
= strlen(filename
) + 1;
619 wcfname
= calloc(len
, sizeof(wchar_t));
620 wclen
= mbstowcs(wcfname
, fname
, len
);
621 wcwid
= wcswidth(wcfname
, wclen
);
622 padwid
= filenamelen
- wcwid
;
623 /* if padwid is < 0, we need to trim the string so padwid = 0 */
625 int i
= filenamelen
- 3;
626 wchar_t *wcp
= wcfname
;
627 /* grab the max number of char columns we can fill */
628 while(i
> 0 && wcwidth(*wcp
) < i
) {
632 /* then add the ellipsis and fill out any extra padding */
638 rate_human
= humanize_size((off_t
)rate
, '\0', 0, &rate_label
);
639 xfered_human
= humanize_size(xfered
, '\0', 0, &xfered_label
);
641 /* 1 space + filenamelen + 1 space + 7 for size + 1 + 7 for rate + 2 for /s + 1 space + 8 for eta */
642 printf(" %ls%-*s %6.1f%s %#6.1f%s/s %02u:%02u:%02u", wcfname
,
643 padwid
, "", xfered_human
, xfered_label
, rate_human
, rate_label
,
644 eta_h
, eta_m
, eta_s
);
650 fill_progress(file_percent
, total_percent
, cols
- infolen
);
652 fill_progress(file_percent
, file_percent
, cols
- infolen
);
657 /* Callback to handle notifications from the library */
658 void cb_log(alpm_loglevel_t level
, const char *fmt
, va_list args
)
660 if(!fmt
|| strlen(fmt
) == 0) {
666 pm_vasprintf(&string
, level
, fmt
, args
);
668 output
= alpm_list_add(output
, string
);
671 pm_vfprintf(stdout
, level
, fmt
, args
);
675 /* vim: set ts=2 sw=2 noet: */