[pacman-dev] [PATCH 1/2] Combine add and removal package list display

Dan McGee dan at archlinux.org
Fri Jul 22 13:19:58 EDT 2011


There was no real reason for these to be done separately.

Signed-off-by: Dan McGee <dan at archlinux.org>
---

A bit of WIP here. Try it out to see what it does, but it addresses Allan's
issue with upgrade size being all whacky when replaces are involved, among
other things. http://paste.pocoo.org/show/444372/ for some example output with
VerbosePkgLists on, http://paste.pocoo.org/show/444368/ with them off. Not sure
what to do with the corny '[removal]' tag bit though.

Notes:
* The size column is now net changed size per package, so only new packages or
  totally removed packages will usually have big values (wireshark-cli is buggy
  in the package itself, as noticed by Dave and I).
* Packages are now always sorted in alpha order, rather than the half-sorted,
  dep resolved mess we had before.
* -R ops will show the same format as -U, even though the new version column
  will now always be blank. I think the consistency win is worth it. They will
  also always show the '[removal]' tag bit, so suggestions for that are
  welcome.
* Yes, you get 4 different totals now on -S operations, but since the damn
  package list is so long anyway, who cares.

Feedback/thoughts definitely welcome. I'll improve the commit message too
before any final version goes live.

-Dan

 src/pacman/remove.c  |    2 +-
 src/pacman/sync.c    |    3 +-
 src/pacman/upgrade.c |    3 +-
 src/pacman/util.c    |  163 +++++++++++++++++++++++++++++++------------------
 src/pacman/util.h    |    7 ++-
 5 files changed, 112 insertions(+), 66 deletions(-)

diff --git a/src/pacman/remove.c b/src/pacman/remove.c
index f0ac04e..95a728e 100644
--- a/src/pacman/remove.c
+++ b/src/pacman/remove.c
@@ -156,7 +156,7 @@ int pacman_remove(alpm_list_t *targets)
 	}
 
 	/* print targets and ask user confirmation */
-	display_targets(pkglist, 0);
+	display_targets();
 	printf("\n");
 	if(yesno(_("Do you want to remove these packages?")) == 0) {
 		retval = 1;
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 5e1643e..0facd6c 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -795,8 +795,7 @@ static int sync_trans(alpm_list_t *targets)
 		goto cleanup;
 	}
 
-	display_targets(alpm_trans_get_remove(config->handle), 0);
-	display_targets(alpm_trans_get_add(config->handle), 1);
+	display_targets();
 	printf("\n");
 
 	int confirm;
diff --git a/src/pacman/upgrade.c b/src/pacman/upgrade.c
index a99b137..55b5fa9 100644
--- a/src/pacman/upgrade.c
+++ b/src/pacman/upgrade.c
@@ -152,8 +152,7 @@ int pacman_upgrade(alpm_list_t *targets)
 		trans_release();
 		return retval;
 	}
-	display_targets(alpm_trans_get_remove(config->handle), 0);
-	display_targets(alpm_trans_get_add(config->handle), 1);
+	display_targets();
 	printf("\n");
 	int confirm = yesno(_("Proceed with installation?"));
 	if(!confirm) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 7065abd..a300f14 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -711,7 +711,7 @@ void signature_display(const char *title, alpm_sigresult_t *result)
 }
 
 /* creates a header row for use with table_display */
-static alpm_list_t *create_verbose_header(int install)
+static alpm_list_t *create_verbose_header(void)
 {
 	alpm_list_t *res = NULL;
 	char *str;
@@ -720,10 +720,8 @@ static alpm_list_t *create_verbose_header(int install)
 	res = alpm_list_add(res, str);
 	pm_asprintf(&str, "%s", _("Old Version"));
 	res = alpm_list_add(res, str);
-	if(install) {
-		pm_asprintf(&str, "%s", _("New Version"));
-		res = alpm_list_add(res, str);
-	}
+	pm_asprintf(&str, "%s", _("New Version"));
+	res = alpm_list_add(res, str);
 	pm_asprintf(&str, "%s", _("Size"));
 	res = alpm_list_add(res, str);
 
@@ -731,125 +729,170 @@ static alpm_list_t *create_verbose_header(int install)
 }
 
 /* returns package info as list of strings */
-static alpm_list_t *create_verbose_row(alpm_pkg_t *pkg, int install)
+static alpm_list_t *create_verbose_row(pm_target_t *target)
 {
 	char *str;
-	double size;
+	off_t size = 0;
+	double human_size;
 	const char *label;
 	alpm_list_t *ret = NULL;
-	alpm_db_t *ldb = alpm_option_get_localdb(config->handle);
 
 	/* a row consists of the package name, */
-	pm_asprintf(&str, "%s", alpm_pkg_get_name(pkg));
+	if(target->install) {
+		pm_asprintf(&str, "%s", alpm_pkg_get_name(target->install));
+	} else {
+		pm_asprintf(&str, "%s", alpm_pkg_get_name(target->remove));
+	}
 	ret = alpm_list_add(ret, str);
 
 	/* old and new versions */
-	if(install) {
-		alpm_pkg_t *oldpkg = alpm_db_get_pkg(ldb, alpm_pkg_get_name(pkg));
-		pm_asprintf(&str, "%s",
-				oldpkg != NULL ? alpm_pkg_get_version(oldpkg) : "");
-		ret = alpm_list_add(ret, str);
-	}
+	pm_asprintf(&str, "%s",
+			target->remove != NULL ? alpm_pkg_get_version(target->remove) : "");
+	ret = alpm_list_add(ret, str);
 
-	pm_asprintf(&str, "%s", alpm_pkg_get_version(pkg));
+	pm_asprintf(&str, "%s",
+			target->install != NULL ? alpm_pkg_get_version(target->install) : "");
 	ret = alpm_list_add(ret, str);
 
 	/* and size */
-	size = humanize_size(alpm_pkg_get_size(pkg), 'M', 1, &label);
-	pm_asprintf(&str, "%.2f %s", size, label);
+	size -= target->remove ? alpm_pkg_get_isize(target->remove) : 0;
+	size += target->install ? alpm_pkg_get_isize(target->install) : 0;
+	human_size = humanize_size(size, 'M', 1, &label);
+	pm_asprintf(&str, "%.2f %s", human_size, label);
 	ret = alpm_list_add(ret, str);
 
 	return ret;
 }
 
 /* prepare a list of pkgs to display */
-void display_targets(const alpm_list_t *pkgs, int install)
+static void _display_targets(alpm_list_t *targets)
 {
 	char *str;
-	const char *title, *label;
+	const char *label;
 	double size;
-	const alpm_list_t *i;
 	off_t isize = 0, rsize = 0, dlsize = 0;
-	alpm_list_t *j, *lp, *header = NULL, *targets = NULL;
-	alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
+	alpm_list_t *i, *header = NULL, *rows = NULL;
 
-	if(!pkgs) {
+	if(!targets) {
 		return;
 	}
 
-	/* gather pkg infos */
-	for(i = pkgs; i; i = alpm_list_next(i)) {
-		alpm_pkg_t *pkg = alpm_list_getdata(i);
+	/* gather package info */
+	for(i = targets; i; i = alpm_list_next(i)) {
+		pm_target_t *target = alpm_list_getdata(i);
 
-		if(install) {
-			alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, alpm_pkg_get_name(pkg));
-			dlsize += alpm_pkg_download_size(pkg);
-			if(lpkg) {
-				/* add up size of all removed packages */
-				rsize += alpm_pkg_get_isize(lpkg);
-			}
+		if(target->install) {
+			dlsize += alpm_pkg_download_size(target->install);
+			isize += alpm_pkg_get_isize(target->install);
+		}
+		if(target->remove) {
+			/* add up size of all removed packages */
+			rsize += alpm_pkg_get_isize(target->remove);
 		}
-		isize += alpm_pkg_get_isize(pkg);
 
 		if(config->verbosepkglists) {
-			targets = alpm_list_add(targets, create_verbose_row(pkg, install));
+			rows = alpm_list_add(rows, create_verbose_row(target));
 		} else {
-			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg));
-			targets = alpm_list_add(targets, str);
+			if(target->install) {
+				pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->install),
+						alpm_pkg_get_version(target->install));
+			} else {
+				pm_asprintf(&str, "%s-%s [removal]", alpm_pkg_get_name(target->remove),
+						alpm_pkg_get_version(target->remove));
+			}
+			rows = alpm_list_add(rows, str);
 		}
 	}
 
 	/* print to screen */
-	title = install ? _("Targets (%d):") : _("Remove (%d):");
-	pm_asprintf(&str, title, alpm_list_count(pkgs));
+	pm_asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
 
 	printf("\n");
 	if(config->verbosepkglists) {
-		header = create_verbose_header(install);
-		if(table_display(str, header, targets) != 0) {
+		header = create_verbose_header();
+		if(table_display(str, header, rows) != 0) {
 			config->verbosepkglists = 0;
-			display_targets(pkgs, install);
+			_display_targets(targets);
 			goto out;
 		}
 	} else {
-		list_display(str, targets);
+		list_display(str, rows);
 	}
 	printf("\n");
 
-	if(install) {
+	if(dlsize > 0) {
 		size = humanize_size(dlsize, 'M', 1, &label);
 		printf(_("Total Download Size:    %.2f %s\n"), size, label);
-		if(!(config->flags & ALPM_TRANS_FLAG_DOWNLOADONLY)) {
+	}
+	if(!(config->flags & ALPM_TRANS_FLAG_DOWNLOADONLY)) {
+		if(isize > 0) {
 			size = humanize_size(isize, 'M', 1, &label);
 			printf(_("Total Installed Size:   %.2f %s\n"), size, label);
-			/* only show this net value if different from raw installed size */
-			if(rsize > 0) {
-				size = humanize_size(isize - rsize, 'M', 1, &label);
-				printf(_("Net Upgrade Size:       %.2f %s\n"), size, label);
-			}
 		}
-	} else {
-		size = humanize_size(isize, 'M', 1, &label);
-		printf(_("Total Removed Size:   %.2f %s\n"), size, label);
+		if(rsize > 0) {
+			size = humanize_size(rsize, 'M', 1, &label);
+			printf(_("Total Removed Size:     %.2f %s\n"), size, label);
+		}
+		/* only show this net value if different from raw installed size */
+		if(isize > 0 && rsize > 0) {
+			size = humanize_size(isize - rsize, 'M', 1, &label);
+			printf(_("Net Upgrade Size:       %.2f %s\n"), size, label);
+		}
 	}
 
 out:
 	/* cleanup */
 	if(config->verbosepkglists) {
-		/* targets is a list of lists of strings, free inner lists here */
-		for(j = targets; j; j = alpm_list_next(j)) {
-			lp = alpm_list_getdata(j);
+		/* rows is a list of lists of strings, free inner lists here */
+		for(i = rows; i; i = alpm_list_next(i)) {
+			alpm_list_t *lp = alpm_list_getdata(i);
 			FREELIST(lp);
 		}
-		alpm_list_free(targets);
+		alpm_list_free(rows);
 		FREELIST(header);
 	} else {
-		FREELIST(targets);
+		FREELIST(rows);
 	}
 	free(str);
 }
 
+static int target_cmp(const void *p1, const void *p2)
+{
+	const pm_target_t *targ1 = p1;
+	const pm_target_t *targ2 = p2;
+	const char *name1 = targ1->install ?
+		alpm_pkg_get_name(targ1->install) : alpm_pkg_get_name(targ1->remove);
+	const char *name2 = targ2->install ?
+		alpm_pkg_get_name(targ2->install) : alpm_pkg_get_name(targ2->remove);
+	return strcmp(name1, name2);
+}
+
+void display_targets(void)
+{
+	alpm_list_t *i, *targets = NULL;
+	alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
+
+	for(i = alpm_trans_get_add(config->handle); i; i = alpm_list_next(i)) {
+		alpm_pkg_t *pkg = alpm_list_getdata(i);
+		pm_target_t *targ = calloc(1, sizeof(pm_target_t));
+		if(!targ) return;
+		targ->install = pkg;
+		targ->remove = alpm_db_get_pkg(db_local, alpm_pkg_get_name(pkg));
+		targets = alpm_list_add(targets, targ);
+	}
+	for(i = alpm_trans_get_remove(config->handle); i; i = alpm_list_next(i)) {
+		alpm_pkg_t *pkg = alpm_list_getdata(i);
+		pm_target_t *targ = calloc(1, sizeof(pm_target_t));
+		if(!targ) return;
+		targ->remove = pkg;
+		targets = alpm_list_add(targets, targ);
+	}
+
+	targets = alpm_list_msort(targets, alpm_list_count(targets), target_cmp);
+	_display_targets(targets);
+	FREELIST(targets);
+}
+
 static off_t pkg_get_size(alpm_pkg_t *pkg)
 {
 	switch(config->op) {
diff --git a/src/pacman/util.h b/src/pacman/util.h
index ee3dbd1..54c941d 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -39,6 +39,11 @@
 /* update speed for the fill_progress based functions */
 #define UPDATE_SPEED_SEC 0.2f
 
+typedef struct _pm_target_t {
+	alpm_pkg_t *remove;
+	alpm_pkg_t *install;
+} pm_target_t;
+
 int trans_init(alpm_transflag_t flags, int check_valid);
 int trans_release(void);
 int needs_root(void);
@@ -58,7 +63,7 @@ int table_display(const char *title, const alpm_list_t *header, const alpm_list_
 void list_display(const char *title, const alpm_list_t *list);
 void list_display_linebreak(const char *title, const alpm_list_t *list);
 void signature_display(const char *title, alpm_sigresult_t *result);
-void display_targets(const alpm_list_t *pkgs, int install);
+void display_targets(void);
 int str_cmp(const void *s1, const void *s2);
 void display_new_optdepends(alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg);
 void display_optdepends(alpm_pkg_t *pkg);
-- 
1.7.6



More information about the pacman-dev mailing list