[pacman-dev] [PATCH 01/16] refactor common code in query_search/sync_search

Simon Gomizelj simongmzlj at gmail.com
Fri Mar 1 16:32:30 EST 2013


Signed-off-by: Simon Gomizelj <simongmzlj at gmail.com>
---
 src/pacman/package.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/pacman/package.h |  3 ++
 src/pacman/query.c   | 56 +-------------------------------------
 src/pacman/sync.c    | 76 +++------------------------------------------------
 4 files changed, 85 insertions(+), 127 deletions(-)

diff --git a/src/pacman/package.c b/src/pacman/package.c
index 330f7ab..6daf745 100644
--- a/src/pacman/package.c
+++ b/src/pacman/package.c
@@ -341,4 +341,81 @@ void dump_pkg_changelog(alpm_pkg_t *pkg)
 	}
 }
 
+void print_installed(alpm_db_t *db_local, alpm_pkg_t *pkg)
+{
+	const char *pkgname = alpm_pkg_get_name(pkg);
+	const char *pkgver = alpm_pkg_get_version(pkg);
+	alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
+	if(lpkg) {
+		const char *lpkgver = alpm_pkg_get_version(lpkg);
+		if(strcmp(lpkgver, pkgver) == 0) {
+			printf(" [%s]", _("installed"));
+		} else {
+			printf(" [%s: %s]", _("installed"), lpkgver);
+		}
+	}
+}
+
+/* search a database for a matching package */
+int dump_pkg_search(alpm_db_t *db, alpm_list_t *targets, alpm_db_t *installed_in)
+{
+	int freelist = 0;
+	alpm_list_t *i, *searchlist;
+	unsigned short cols;
+
+	/* if we have a targets list, search for packages matching it */
+	if(targets) {
+		searchlist = alpm_db_search(db, targets);
+		freelist = 1;
+	} else {
+		searchlist = alpm_db_get_pkgcache(db);
+		freelist = 0;
+	}
+	if(searchlist == NULL) {
+		return 1;
+	}
+
+	cols = getcols(fileno(stdout));
+	for(i = searchlist; i; i = alpm_list_next(i)) {
+		alpm_list_t *grp;
+		alpm_pkg_t *pkg = i->data;
+
+		if(config->quiet) {
+			fputs(alpm_pkg_get_name(pkg), stdout);
+		} else {
+			printf("%s/%s %s", alpm_db_get_name(db),
+					alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
+
+			if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
+				alpm_list_t *k;
+				fputs(" (", stdout);
+				for(k = grp; k; k = alpm_list_next(k)) {
+					const char *group = k->data;
+					fputs(group, stdout);
+					if(alpm_list_next(k)) {
+						/* only print a spacer if there are more groups */
+						putchar(' ');
+					}
+				}
+				putchar(')');
+			}
+
+			if (installed_in)
+				print_installed(installed_in, pkg);
+
+			/* we need a newline and initial indent first */
+			fputs("\n    ", stdout);
+			indentprint(alpm_pkg_get_desc(pkg), 4, cols);
+		}
+		fputc('\n', stdout);
+	}
+
+	/* we only want to free if the list was a search list */
+	if(freelist) {
+		alpm_list_free(searchlist);
+	}
+
+	return 0;
+}
+
 /* vim: set ts=2 sw=2 noet: */
diff --git a/src/pacman/package.h b/src/pacman/package.h
index 47fbcad..635d5f7 100644
--- a/src/pacman/package.h
+++ b/src/pacman/package.h
@@ -28,6 +28,9 @@ void dump_pkg_backups(alpm_pkg_t *pkg);
 void dump_pkg_files(alpm_pkg_t *pkg, int quiet);
 void dump_pkg_changelog(alpm_pkg_t *pkg);
 
+void print_installed(alpm_db_t *db_local, alpm_pkg_t *pkg);
+int dump_pkg_search(alpm_db_t *db, alpm_list_t *targets, alpm_db_t *installed_in);
+
 #endif /* _PM_PACKAGE_H */
 
 /* vim: set ts=2 sw=2 noet: */
diff --git a/src/pacman/query.c b/src/pacman/query.c
index 1247c3d..416f92c 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -262,62 +262,8 @@ targcleanup:
 /* search the local database for a matching package */
 static int query_search(alpm_list_t *targets)
 {
-	alpm_list_t *i, *searchlist;
-	int freelist;
 	alpm_db_t *db_local = alpm_get_localdb(config->handle);
-	unsigned short cols;
-
-	/* if we have a targets list, search for packages matching it */
-	if(targets) {
-		searchlist = alpm_db_search(db_local, targets);
-		freelist = 1;
-	} else {
-		searchlist = alpm_db_get_pkgcache(db_local);
-		freelist = 0;
-	}
-	if(searchlist == NULL) {
-		return 1;
-	}
-
-	cols = getcols(fileno(stdout));
-	for(i = searchlist; i; i = alpm_list_next(i)) {
-		alpm_list_t *grp;
-		alpm_pkg_t *pkg = i->data;
-
-		if(!config->quiet) {
-			printf(LOCAL_PREFIX "%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
-		} else {
-			fputs(alpm_pkg_get_name(pkg), stdout);
-		}
-
-
-		if(!config->quiet) {
-			if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
-				alpm_list_t *k;
-				fputs(" (", stdout);
-				for(k = grp; k; k = alpm_list_next(k)) {
-					const char *group = k->data;
-					fputs(group, stdout);
-					if(alpm_list_next(k)) {
-						/* only print a spacer if there are more groups */
-						putchar(' ');
-					}
-				}
-				putchar(')');
-			}
-
-			/* we need a newline and initial indent first */
-			fputs("\n    ", stdout);
-			indentprint(alpm_pkg_get_desc(pkg), 4, cols);
-		}
-		fputc('\n', stdout);
-	}
-
-	/* we only want to free if the list was a search list */
-	if(freelist) {
-		alpm_list_free(searchlist);
-	}
-	return 0;
+	return dump_pkg_search(db_local, targets, NULL);
 }
 
 static int query_group(alpm_list_t *targets)
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 924cdf5..b1cc3fe 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -352,87 +352,19 @@ static int sync_synctree(int level, alpm_list_t *syncs)
 	return (success > 0);
 }
 
-static void print_installed(alpm_db_t *db_local, alpm_pkg_t *pkg)
-{
-	const char *pkgname = alpm_pkg_get_name(pkg);
-	const char *pkgver = alpm_pkg_get_version(pkg);
-	alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, pkgname);
-	if(lpkg) {
-		const char *lpkgver = alpm_pkg_get_version(lpkg);
-		if(strcmp(lpkgver, pkgver) == 0) {
-			printf(" [%s]", _("installed"));
-		} else {
-			printf(" [%s: %s]", _("installed"), lpkgver);
-		}
-	}
-}
-
 /* search the sync dbs for a matching package */
 static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
 {
-	alpm_list_t *i, *j, *ret;
-	int freelist;
-	int found = 0;
+	alpm_list_t *i;
 	alpm_db_t *db_local = alpm_get_localdb(config->handle);
+	int found = 0;
 
 	for(i = syncs; i; i = alpm_list_next(i)) {
 		alpm_db_t *db = i->data;
-		unsigned short cols;
-		/* if we have a targets list, search for packages matching it */
-		if(targets) {
-			ret = alpm_db_search(db, targets);
-			freelist = 1;
-		} else {
-			ret = alpm_db_get_pkgcache(db);
-			freelist = 0;
-		}
-		if(ret == NULL) {
-			continue;
-		} else {
-			found = 1;
-		}
-		cols = getcols(fileno(stdout));
-		for(j = ret; j; j = alpm_list_next(j)) {
-			alpm_list_t *grp;
-			alpm_pkg_t *pkg = j->data;
-
-			if(!config->quiet) {
-				printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
-							 alpm_pkg_get_version(pkg));
-			} else {
-				fputs(alpm_pkg_get_name(pkg), stdout);
-			}
-
-			if(!config->quiet) {
-				if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
-					alpm_list_t *k;
-					fputs(" (", stdout);
-					for(k = grp; k; k = alpm_list_next(k)) {
-						const char *group = k->data;
-						fputs(group, stdout);
-						if(alpm_list_next(k)) {
-							/* only print a spacer if there are more groups */
-							putchar(' ');
-						}
-					}
-					putchar(')');
-				}
-
-				print_installed(db_local, pkg);
-
-				/* we need a newline and initial indent first */
-				fputs("\n    ", stdout);
-				indentprint(alpm_pkg_get_desc(pkg), 4, cols);
-			}
-			fputc('\n', stdout);
-		}
-		/* we only want to free if the list was a search list */
-		if(freelist) {
-			alpm_list_free(ret);
-		}
+		found += !dump_pkg_search(db, targets, db_local);
 	}
 
-	return !found;
+	return (found == 0);
 }
 
 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
-- 
1.8.1.4



More information about the pacman-dev mailing list