Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Support glob patterns in pkg-which.
Vsevolod Stakhov committed 13 years ago
commit 1da7bc9c5d7003d914a33fefffbbc7486a8bf4d8
parent 6170a96
5 files changed +37 -20
modified libpkg/pkg.h.in
@@ -929,7 +929,7 @@ struct pkgdb_it * pkgdb_search(struct pkgdb *db, const char *pattern,
/**
 * @todo Return directly the struct pkg?
 */
-
struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path);
+
struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path, bool glob);

struct pkgdb_it * pkgdb_query_shlib_required(struct pkgdb *db, const char *shlib);
struct pkgdb_it * pkgdb_query_shlib_provided(struct pkgdb *db, const char *shlib);
modified libpkg/pkg_elf.c
@@ -152,7 +152,7 @@ test_depends(void *actdata, struct pkg *pkg, const char *fpath,
	if (shlibs)
		pkg_addshlib_required(pkg, name);

-
	if ((it = pkgdb_query_which(db, pathbuf)) == NULL)
+
	if ((it = pkgdb_query_which(db, pathbuf, false)) == NULL)
		return (EPKG_OK);

	d = NULL;
modified libpkg/pkgdb.c
@@ -1424,18 +1424,20 @@ pkgdb_query(struct pkgdb *db, const char *pattern, match_t match)
}

struct pkgdb_it *
-
pkgdb_query_which(struct pkgdb *db, const char *path)
+
pkgdb_query_which(struct pkgdb *db, const char *path, bool glob)
{
	sqlite3_stmt	*stmt;
-
	const char	 sql[] = ""
-
		"SELECT p.id, p.origin, p.name, p.version, p.comment, p.desc, "
-
			"p.message, p.arch, p.maintainer, p.www, "
-
			"p.prefix, p.flatsize, p.time, p.infos "
-
			"FROM packages AS p, files AS f "
-
			"WHERE p.id = f.package_id "
-
				"AND f.path = ?1;";
+
	char	sql[BUFSIZ];
+


	assert(db != NULL);
+
	sqlite3_snprintf(sizeof(sql), sql,
+
			"SELECT p.id, p.origin, p.name, p.version, p.comment, p.desc, "
+
			"p.message, p.arch, p.maintainer, p.www, "
+
			"p.prefix, p.flatsize, p.time, p.infos "
+
			"FROM packages AS p "
+
			"LEFT JOIN files AS f ON p.id = f.package_id "
+
			"WHERE f.path %s ?1 GROUP BY p.id;", glob ? "GLOB" : "=");

	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
		ERROR_SQLITE(db->sqlite);
@@ -2338,7 +2340,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete)
			ERROR_SQLITE(s);
			goto cleanup;
		}
-
		it = pkgdb_query_which(db, pkg_path);
+
		it = pkgdb_query_which(db, pkg_path, false);
		if (it == NULL) {
			ERROR_SQLITE(s);
			goto cleanup;
modified pkg/pkg-which.8
@@ -23,7 +23,7 @@
.Nd displays which package installed a specific file
.Sh SYNOPSIS
.Nm
-
.Op Fl qo
+
.Op Fl qog
.Ar <file>
.Sh DESCRIPTION
.Nm
@@ -37,6 +37,10 @@ The following options are supported by
Be quiet
.It Fl o
Shows the origin of the package instead of name-version
+
.It Fl g
+
Treat
+
.Ao file Ac
+
as a glob pattern.
.El
.Sh ENVIRONMENT
The following environment variables affect the execution of
modified pkg/which.c
@@ -39,7 +39,7 @@
void
usage_which(void)
{
-
	fprintf(stderr, "usage: pkg which [-qo] <file>\n\n");
+
	fprintf(stderr, "usage: pkg which [-qgo] <file>\n\n");
	fprintf(stderr, "For more information see 'pkg help which'.\n");
}

@@ -54,12 +54,16 @@ exec_which(int argc, char **argv)
	const char *name, *version, *origin;
	int ch;
	bool orig = false;
+
	bool glob = false;

-
	while ((ch = getopt(argc, argv, "qo")) != -1) {
+
	while ((ch = getopt(argc, argv, "qgo")) != -1) {
		switch (ch) {
		case 'q':
			quiet = true;
			break;
+
		case 'g':
+
			glob = true;
+
			break;
		case 'o':
			orig = true;
			break;
@@ -79,13 +83,15 @@ exec_which(int argc, char **argv)
		return (EX_IOERR);
	}

-
	absolutepath(argv[0], pathabs, sizeof(pathabs));
+
	if (!glob)
+
		absolutepath(argv[0], pathabs, sizeof(pathabs));
+
	else
+
		strlcpy(pathabs, argv[0], sizeof(pathabs));

-
	if ((it = pkgdb_query_which(db, pathabs)) == NULL) {
+
	if ((it = pkgdb_query_which(db, pathabs, glob)) == NULL)
		return (EX_IOERR);
-
	}

-
	if ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
	while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
		pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version, PKG_ORIGIN, &origin);
		if (quiet && orig)
			printf("%s\n", origin);
@@ -95,9 +101,14 @@ exec_which(int argc, char **argv)
			printf("%s was installed by package %s\n", pathabs, origin);
		else if (!quiet && !orig)
			printf("%s was installed by package %s-%s\n", pathabs, name, version);
-
	} else if (ret != EPKG_END) {
+
		if (!glob)
+
			break;
+
	}
+

+
	if (ret != EPKG_END) {
		retcode = EX_SOFTWARE;
-
	} else {
+
	}
+
	else if (!glob) {
		if (!quiet)
			printf("%s was not found in the database\n", pathabs);
		retcode = EX_DATAERR;