From 1b60df648a9fefa9702fd9f95f08f92e00b8aa4f Mon Sep 17 00:00:00 2001
From: Chantry Xavier <shiningxc@gmail.com>
Date: Sat, 2 Jun 2007 16:25:22 +0200
Subject: [PATCH] Moves some parts of pacman_query into several subfunctions (query_search, query_group, query_isfile)

Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
---
 src/pacman/query.c |  158 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 101 insertions(+), 57 deletions(-)

diff --git a/src/pacman/query.c b/src/pacman/query.c
index a4a4207..aafbe09 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -59,39 +59,46 @@ static char *resolve_path(const char* file)
 }
 
 
-static void query_fileowner(pmdb_t *db, char *filename)
+static int query_fileowner(alpm_list_t *targets)
 {
 	struct stat buf;
-	int found = 0;
+	int ret = 0;
+	int found;
 	char *rpath;
-	alpm_list_t *i, *j;
+	alpm_list_t *i, *j, *t;
+	char *filename;
 
-	if(db == NULL) {
-		return;
-	}
-	if(filename == NULL || strlen(filename) == 0) {
+	if(targets == NULL) {
 		fprintf(stderr, _("error: no file was specified for --owns\n"));
-		return;
+		return(1);
 	}
 
+	for(t = targets; t; t = alpm_list_next(t)) {
+		filename = alpm_list_getdata(t);
+
+
 	if(stat(filename, &buf) == -1) {
 		fprintf(stderr, _("error: failed to read file '%s': %s\n"),
 		        filename, strerror(errno));
-		return;
+			ret++;
+			continue;
 	}
 	
 	if(S_ISDIR(buf.st_mode)) {
 		fprintf(stderr, _("error: cannot determine ownership of a directory\n"));
-		return;
+			ret++;
+			continue;
 	}
 
 	if(!(rpath = resolve_path(filename))) {
 		fprintf(stderr, _("error: cannot determine real path for '%s': %s\n"),
 		        filename, strerror(errno));
-		return;
+			ret++;
+			continue;
 	}
 
-	for(i = alpm_db_getpkgcache(db); i && !found; i = alpm_list_next(i)) {
+		found = 0;
+		for(i = alpm_db_getpkgcache(db_local); i && !found; i = alpm_list_next(i)) {
 		pmpkg_t *info = alpm_list_getdata(i);
 
 		for(j = alpm_pkg_get_files(info); j && !found; j = alpm_list_next(j)) {
@@ -110,19 +117,18 @@ static void query_fileowner(pmdb_t *db, char *filename)
 	}
 	if(!found) {
 		fprintf(stderr, _("error: No package owns %s\n"), filename);
+			ret++;
 	}
 
 	free(rpath);
+	}
+	return ret;
 }
 
-int pacman_query(alpm_list_t *targets)
-{
-	alpm_list_t *sync_dbs = NULL, *i, *j, *k;
-	pmpkg_t *info = NULL;
-	char *package = NULL;
-	int ret = 0;
 
-	if(config->op_q_search) {
+int query_search(alpm_list_t *targets)
+{
+	alpm_list_t *i;
 		alpm_list_t *searchlist = alpm_db_search(db_local, targets);
 		if(searchlist == NULL) {
 			return(0);
@@ -156,35 +162,13 @@ int pacman_query(alpm_list_t *targets)
 		}
 		alpm_list_free(searchlist);
 		return(0);
-	}
-
-	if(config->op_q_foreign) {
-		sync_dbs = alpm_option_get_syncdbs();
-
-		if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
-			fprintf(stderr, _("error: no usable package repositories configured.\n"));
-			return(1);
-		}
-	}
-
-	if(config->op_q_upgrade) {
-		printf(_("Checking for package upgrades..."));
-		alpm_list_t *syncpkgs;
-
-		if((syncpkgs = alpm_get_upgrades()) != NULL) {
-				display_targets(syncpkgs);
-				return(0);
-		} else {
-			printf(_("no upgrades found"));
-			return(1);
-		}
-	}
-
-	for(i = targets; i; i = alpm_list_next(i)) {
-		package = alpm_list_getdata(i);
+}
 
-		/* looking for groups */
-		if(config->group) {
+int query_group(alpm_list_t *targets)
+{
+	alpm_list_t *i, *j;
+	char *package = NULL;
+	int ret = 0;
 			if(targets == NULL) {
 				for(j = alpm_db_getgrpcache(db_local); j; j = alpm_list_next(j)) {
 					pmgrp_t *grp = alpm_list_getdata(j);
@@ -199,6 +183,8 @@ int pacman_query(alpm_list_t *targets)
 					}
 				}
 			} else {
+		for(i = targets; i; i = alpm_list_next(i)) {
+			package = alpm_list_getdata(i);
 				pmgrp_t *grp = alpm_db_readgrp(db_local, package);
 				if(grp) {
 					alpm_list_t *p, *pkgnames = alpm_grp_get_pkgs(grp);
@@ -210,19 +196,27 @@ int pacman_query(alpm_list_t *targets)
 					ret++;
 				}
 			}
-			continue;
 		}
+	return ret;
+}
 
-		/* output info for a .tar.gz package */
-		if(config->op_q_isfile) {
-			if(package == NULL) {
+int query_isfile(alpm_list_t *targets)
+{
+	int ret = 0;
+	char *package = NULL;
+	alpm_list_t *i;
+	pmpkg_t *info = NULL;
+	if(targets == NULL) {
 				fprintf(stderr, _("error: no package file was specified for --file\n"));
 				return(1);
-			}
+	} else {
+		for(i = targets; i; i = alpm_list_next(i)) {
+			package = alpm_list_getdata(i);
 			if(alpm_pkg_load(package, &info) == -1) {
 				fprintf(stderr, _("error: failed to load package '%s' (%s)\n"),
 				        package, alpm_strerror(pm_errno));
-				return(1);
+				ret++;
+				continue;
 			}
 			if(config->op_q_info) {
 				dump_pkg_full(info, config->op_q_info);
@@ -236,17 +230,65 @@ int pacman_query(alpm_list_t *targets)
 			}
 			alpm_pkg_free(info);
 			info = NULL;
-			continue;
+		}
+	}
+	return(ret);
+}
+
+int pacman_query(alpm_list_t *targets)
+{
+	alpm_list_t *sync_dbs = NULL, *i, *j, *k;
+	pmpkg_t *info = NULL;
+	char *package = NULL;
+	int ret = 0;
+
+	if(config->op_q_search) {
+		ret = query_search(targets);
+		return(ret);
+	}
+
+	if(config->op_q_foreign) {
+		sync_dbs = alpm_option_get_syncdbs();
+
+		if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
+			fprintf(stderr, _("error: no usable package repositories configured.\n"));
+			return(1);
+		}
+	}
+
+	if(config->op_q_upgrade) {
+		printf(_("Checking for package upgrades..."));
+		alpm_list_t *syncpkgs;
+
+		if((syncpkgs = alpm_get_upgrades()) != NULL) {
+			display_targets(syncpkgs);
+			return(0);
+		} else {
+			printf(_("no upgrades found"));
+			return(1);
+		}
+	}
+
+	/* looking for groups */
+	if(config->group) {
+		ret = query_group(targets);
+		return(ret);
+	}
+
+	/* output info for a .tar.gz package */
+	if(config->op_q_isfile) {
+		ret = query_isfile(targets);
+		return(ret);
 		}
 
 		/* determine the owner of a file */
 		if(config->op_q_owns) {
-			query_fileowner(db_local, package);
-			continue;
+		ret = query_fileowner(targets);
+		return(ret);
 		}
 
 		/* find packages in the db */
-		if(package == NULL) {
+	if(targets == NULL) {
 			/* no target */
 			for(i = alpm_db_getpkgcache(db_local); i; i = alpm_list_next(i)) {
 				pmpkg_t *tmpp = alpm_list_getdata(i);
@@ -291,6 +333,8 @@ int pacman_query(alpm_list_t *targets)
 				}
 			}
 		} else {
+		for(i = targets; i; i = alpm_list_next(i)) {
+			package = alpm_list_getdata(i);
 			info = alpm_db_get_pkg(db_local, package);
 			if(info == NULL) {
 				fprintf(stderr, _("error: package \"%s\" not found\n"), package);
-- 
1.5.2

