Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Honor PACKAGESITE during 'pkg search' if defined
Marin Atanasov Nikolov committed 14 years ago
commit 0178c468efd1fb8f68bf33bb29371c649ed4e3d6
parent 206442c
1 file changed +62 -45
modified pkg/search.c
@@ -11,6 +11,9 @@

#include "search.h"

+
static int search_remote_repo(const char *pattern, match_t match, 
+
		unsigned int field, const char *dbname);
+

void
usage_search(void)
{
@@ -23,13 +26,9 @@ exec_search(int argc, char **argv)
{
	char *pattern = NULL;
	match_t match = MATCH_EXACT;
-
	int  retcode = EPKG_OK;
	unsigned int field = REPO_SEARCH_NAME;
+
	int retcode = EPKG_OK;
	int ch;
-
	char size[7], dbfile[MAXPATHLEN];
-
	struct pkgdb *db = NULL;
-
	struct pkgdb_it *it = NULL;
-
	struct pkg *pkg = NULL;
	struct pkg_remote_repo *repo = NULL;

	while ((ch = getopt(argc, argv, "gxXcd")) != -1) {
@@ -65,52 +64,70 @@ exec_search(int argc, char **argv)

	pattern = argv[0];

-
	pkg_remote_repo_init();
-
	pkg_remote_repo_load();
-

-

	/*
	 * TODO: Implement a feature to search only
-
	 * in a given repository instead of all
+
	 * in a given repository specified in the argument list
	 */
-
	while ((repo = pkg_remote_repo_next()) != NULL) {
-
		snprintf(dbfile, MAXPATHLEN, "%s.sqlite", repo->name);
-

-
		if ((retcode = pkgdb_open(&db, PKGDB_REMOTE, dbfile)) != EPKG_OK) {
-
			warnx("cannot open repository database: %s/%s\n", 
-
					pkg_config("PKG_DBDIR"), dbfile);
-
			warnx("skipping repository %s\n", repo->name);
-
			pkgdb_close(db);
-
			continue;
-
		}

-
		if ((it = pkgdb_rquery(db, pattern, match, field)) == NULL) {
-
			warnx("cannot query repository database: %s/%s\n",
-
					pkg_config("PKG_DBDIR"), dbfile);
-
			warnx("skipping repository %s\n", repo->name);
-
			pkgdb_it_free(it);
-
			pkgdb_close(db);
-
			continue;
-
		}
+
	/* 
+
	 * Honor PACKAGESITE if specified
+
	 */
+
	if (pkg_config("PACKAGESITE") != NULL) {
+
		retcode = search_remote_repo(pattern, match, field, "repo");
+
	} else {
+
		pkg_remote_repo_init();
+
		pkg_remote_repo_load();
+
	
+
		while ((repo = pkg_remote_repo_next()) != NULL)
+
			retcode = search_remote_repo(pattern, match, field, repo->name);
+

+
		pkg_remote_repo_free();
+
	}

-
		while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
-
			printf("Name:       %s\n", pkg_get(pkg, PKG_NAME));
-
			printf("Version:    %s\n", pkg_get(pkg, PKG_VERSION));
-
			printf("Origin:     %s\n", pkg_get(pkg, PKG_ORIGIN));
-
			printf("Arch:       %s\n", pkg_get(pkg, PKG_ARCH));
-
			printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER));
-
			printf("WWW:        %s\n", pkg_get(pkg, PKG_WWW));
-
			printf("Comment:    %s\n", pkg_get(pkg, PKG_COMMENT));
-
			printf("Repository: %s\n", repo->name);
-
			humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0);
-
			printf("Flat size:  %s\n", size);
-
			humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0);
-
			printf("Pkg size:   %s\n", size);
-
			printf("\n");
-
		}
+
	return (retcode);
+
}
+

+
static int
+
search_remote_repo(const char *pattern, match_t match, unsigned int field, const char *dbname)
+
{
+
	char size[7];
+
	char dbfile[MAXPATHLEN];
+
	int  retcode = EPKG_OK;
+
	struct pkgdb *db = NULL;
+
	struct pkgdb_it *it = NULL;
+
	struct pkg *pkg = NULL;
+

+
	snprintf(dbfile, MAXPATHLEN, "%s.sqlite", dbname);
+

+
	if (pkgdb_open(&db, PKGDB_REMOTE, dbfile) != EPKG_OK) {
+
		warnx("cannot open repository database: %s/%s\n", 
+
				pkg_config("PKG_DBDIR"), dbfile);
+
		return(EPKG_FATAL);
+
	}
+

+
	if ((it = pkgdb_rquery(db, pattern, match, field)) == NULL) {
+
		warnx("cannot query repository database: %s/%s\n",
+
				pkg_config("PKG_DBDIR"), dbfile);
+
		pkgdb_it_free(it);
+
		pkgdb_close(db);
+
		return(EPKG_FATAL);
	}

-
	pkg_remote_repo_free();
+
	while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
		printf("Name:       %s\n", pkg_get(pkg, PKG_NAME));
+
		printf("Version:    %s\n", pkg_get(pkg, PKG_VERSION));
+
		printf("Origin:     %s\n", pkg_get(pkg, PKG_ORIGIN));
+
		printf("Arch:       %s\n", pkg_get(pkg, PKG_ARCH));
+
		printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER));
+
		printf("WWW:        %s\n", pkg_get(pkg, PKG_WWW));
+
		printf("Comment:    %s\n", pkg_get(pkg, PKG_COMMENT));
+
		printf("Repository: %s\n", dbname);
+
		humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0);
+
		printf("Flat size:  %s\n", size);
+
		humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0);
+
		printf("Pkg size:   %s\n", size);
+
		printf("\n");
+
	}

-
	return (retcode);
+
	return(retcode);
}