[pacman-dev] [PATCH] src/pacman : rework the display_targets function.

Xavier Chantry shiningxc at gmail.com
Thu Jul 24 20:02:36 EDT 2008


We had a lot of duplicated code here. The code handling the showsize option
needed to be there three times :
1) for install part of -S
2) for remove part of -S (conflict removal)
3) for -R

This patch introduce a new display_targets(pkglist, install) function which
can handle the 3 cases above. We pass install == 1 for case 1), and install
== 0 for case 2) and 3).

Now we can finally get the benefit of an old patch which handled the
ShowSize option consistently in the 3 cases above, without an awful lot of
duplicated code :
http://www.archlinux.org/pipermail/pacman-dev/2008-January/011029.html

Signed-off-by: Xavier Chantry <shiningxc at gmail.com>
---
 src/pacman/query.c  |    2 +-
 src/pacman/remove.c |   15 +++-----
 src/pacman/sync.c   |    2 +-
 src/pacman/util.c   |  100 ++++++++++++++++++++++++++++-----------------------
 src/pacman/util.h   |    3 +-
 5 files changed, 64 insertions(+), 58 deletions(-)

diff --git a/src/pacman/query.c b/src/pacman/query.c
index bd2d8c5..d899539 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -261,7 +261,7 @@ static int query_upgrades(void)
 		return(-1);
 	}
 	if(syncpkgs) {
-		display_targets(syncpkgs, db_local);
+		display_synctargets(syncpkgs);
 		return(0);
 	}
 
diff --git a/src/pacman/remove.c b/src/pacman/remove.c
index 83947ca..f091fa4 100644
--- a/src/pacman/remove.c
+++ b/src/pacman/remove.c
@@ -125,21 +125,16 @@ int pacman_remove(alpm_list_t *targets)
 	if(config->flags & PM_TRANS_FLAG_RECURSE ||
 	   config->flags & PM_TRANS_FLAG_CASCADE) {
 		/* list transaction targets */
-		alpm_list_t *lst = NULL;
-		/* create a new list of package names only */
-		for(i = alpm_trans_get_pkgs(); i; i = alpm_list_next(i)) {
-			pmpkg_t *pkg = alpm_list_getdata(i);
-			lst = alpm_list_add(lst, strdup(alpm_pkg_get_name(pkg)));
-		}
+		alpm_list_t *pkglist = alpm_trans_get_pkgs();
+
+		display_targets(pkglist, 0);
 		printf("\n");
-		list_display(_("Targets:"), lst);
-		FREELIST(lst);
+
 		/* get confirmation */
-		if(yesno(1, _("\nDo you want to remove these packages?")) == 0) {
+		if(yesno(1, _("Do you want to remove these packages?")) == 0) {
 			retval = 1;
 			goto cleanup;
 		}
-		printf("\n");
 	}
 
 	/* Step 3: actually perform the removal */
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 87bd5be..8d0c529 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -669,7 +669,7 @@ static int sync_trans(alpm_list_t *targets)
 	if(!(alpm_trans_get_flags() & PM_TRANS_FLAG_PRINTURIS)) {
 		int confirm;
 
-		display_targets(packages, db_local);
+		display_synctargets(packages);
 		printf("\n");
 
 		if(config->op_s_downloadonly) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index d7ac9e3..efe8e6a 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -480,49 +480,33 @@ void list_display(const char *title, const alpm_list_t *list)
 	}
 }
 
-/* Display a list of transaction targets.
- * `pkgs` should be a list of pmsyncpkg_t's,
- * retrieved from a transaction object
- */
-/* TODO move to output.c? or just combine util and output */
-void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
+/* prepare a list of pkgs to display */
+void display_targets(const alpm_list_t *pkgs, int install)
 {
 	char *str;
-	const alpm_list_t *i, *j;
-	alpm_list_t *targets = NULL, *to_remove = NULL;
-	/* TODO these are some messy variable names */
-	off_t isize = 0, rsize = 0, dispsize = 0, dlsize = 0;
-	double mbisize = 0.0, mbrsize = 0.0, mbdispsize = 0.0, mbdlsize = 0.0;
-
-	for(i = syncpkgs; i; i = alpm_list_next(i)) {
-		pmsyncpkg_t *sync = alpm_list_getdata(i);
-		pmpkg_t *pkg = alpm_sync_get_pkg(sync);
-
-		/* The removes member contains a list of packages to be removed
-		 * due to the package that is being installed. */
-		alpm_list_t *to_replace = alpm_sync_get_removes(sync);
+	const alpm_list_t *i;
+	off_t isize = 0, dlsize = 0;
+	double mbisize = 0.0, mbdlsize = 0.0;
+	alpm_list_t *targets = NULL;
 
-		for(j = to_replace; j; j = alpm_list_next(j)) {
-			pmpkg_t *rp = alpm_list_getdata(j);
-			const char *name = alpm_pkg_get_name(rp);
+	if(!pkgs) {
+		return;
+	}
 
-			if(!alpm_list_find_str(to_remove, name)) {
-				rsize += alpm_pkg_get_isize(rp);
-				to_remove = alpm_list_add(to_remove, strdup(name));
-			}
-		}
+	printf("\n");
+	for(i = pkgs; i; i = alpm_list_next(i)) {
+		pmpkg_t *pkg = alpm_list_getdata(i);
 
-		dispsize = alpm_pkg_get_size(pkg);
 		dlsize += alpm_pkg_download_size(pkg);
 		isize += alpm_pkg_get_isize(pkg);
 
 		/* print the package size with the output if ShowSize option set */
 		if(config->showsize) {
-			/* Convert byte size to MB */
-			mbdispsize = dispsize / (1024.0 * 1024.0);
+			double mbsize = 0.0;
+			mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
 
 			asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
-					alpm_pkg_get_version(pkg), mbdispsize);
+					alpm_pkg_get_version(pkg), mbsize);
 		} else {
 			asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
 					alpm_pkg_get_version(pkg));
@@ -531,29 +515,55 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
 	}
 
 	/* Convert byte sizes to MB */
-	mbisize = isize / (1024.0 * 1024.0);
-	mbrsize = rsize / (1024.0 * 1024.0);
 	mbdlsize = dlsize / (1024.0 * 1024.0);
+	mbisize = isize / (1024.0 * 1024.0);
 
-	/* start displaying information */
-	printf("\n");
-
-	if(to_remove) {
-		list_display(_("Remove:"), to_remove);
+	if(install) {
+		list_display(_("Targets:"), targets);
 		printf("\n");
-		FREELIST(to_remove);
 
-		printf(_("Total Removed Size:   %.2f MB\n"), mbrsize);
+		printf(_("Total Download Size:    %.2f MB\n"), mbdlsize);
+		printf(_("Total Installed Size:   %.2f MB\n"), mbisize);
+	} else {
+		list_display(_("Remove:"), targets);
 		printf("\n");
+
+		printf(_("Total Removed Size:   %.2f MB\n"), mbisize);
 	}
 
-	list_display(_("Targets:"), targets);
-	printf("\n");
+	FREELIST(targets);
+}
 
-	printf(_("Total Download Size:    %.2f MB\n"), mbdlsize);
-	printf(_("Total Installed Size:   %.2f MB\n"), mbisize);
+/* Display a list of transaction targets.
+ * `pkgs` should be a list of pmsyncpkg_t's,
+ * retrieved from a transaction object
+ */
+void display_synctargets(const alpm_list_t *syncpkgs)
+{
+	const alpm_list_t *i, *j;
+	alpm_list_t *pkglist = NULL, *rpkglist = NULL;
 
-	FREELIST(targets);
+	for(i = syncpkgs; i; i = alpm_list_next(i)) {
+		pmsyncpkg_t *sync = alpm_list_getdata(i);
+		pmpkg_t *pkg = alpm_sync_get_pkg(sync);
+		pkglist = alpm_list_add(pkglist, pkg);
+
+		/* The removes member contains a list of packages to be removed
+		 * due to the package that is being installed. */
+		alpm_list_t *to_replace = alpm_sync_get_removes(sync);
+
+		for(j = to_replace; j; j = alpm_list_next(j)) {
+			pmpkg_t *rp = alpm_list_getdata(j);
+			rpkglist = alpm_list_add(rpkglist, rp);
+		}
+	}
+
+	/* start displaying information */
+	display_targets(rpkglist, 0);
+	display_targets(pkglist, 1);
+
+	alpm_list_free(pkglist);
+	alpm_list_free(rpkglist);
 }
 
 /* presents a prompt and gets a Y/N answer */
diff --git a/src/pacman/util.h b/src/pacman/util.h
index 722e4ab..2ddc1b5 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -51,7 +51,8 @@ char *strreplace(const char *str, const char *needle, const char *replace);
 alpm_list_t *strsplit(const char *str, const char splitchar);
 void string_display(const char *title, const char *string);
 void list_display(const char *title, const alpm_list_t *list);
-void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local);
+void display_targets(const alpm_list_t *pkgs, int install);
+void display_synctargets(const alpm_list_t *syncpkgs);
 int yesno(short preset, char *fmt, ...);
 int pm_printf(pmloglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
 int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...) __attribute__((format(printf,3,4)));
-- 
1.5.6.4





More information about the pacman-dev mailing list