[pacman-dev] [PATCH] libalpm: add alpm_dbs_get_pkg

morganamilo morganamilo at archlinux.org
Thu Jun 3 01:57:55 UTC 2021


This is a common function that pretty much every front end ends up
implementing, searching for a package in the syncdbs. May as well add it
here for every one to use.

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index a5f4a6ae..5fa34faa 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -1381,6 +1381,17 @@ int alpm_db_update(alpm_handle_t *handle, alpm_list_t *dbs, int force);
  */
 alpm_pkg_t *alpm_db_get_pkg(alpm_db_t *db, const char *name);
 
+/** Get a package entry from a list of databases.
+ * This is similar to \link alpm_dbs_get_pkg() get pkg \endlink except it will
+ * look through a list of databases instead of just one. The first matching
+ * package found will be returned.
+ * @param handle the context handle
+ * @param dbs list of databases to search through
+ * @param name name of the package
+ * @return the package entry on success, NULL on error
+ */
+alpm_pkg_t *alpm_dbs_get_pkg(alpm_handle_t *handle, alpm_list_t *dbs, const char *name);
+
 /** Get the package cache of a package database.
  * This is a list of all packages the db contains.
  * @param db pointer to the package database to get the package from
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index b8d1b157..c3cfb13a 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -257,6 +257,16 @@ alpm_list_t SYMEXPORT *alpm_db_get_pkgcache(alpm_db_t *db)
 	return _alpm_db_get_pkgcache(db);
 }
 
+alpm_pkg_t SYMEXPORT *alpm_dbs_get_pkg(alpm_handle_t *handle, alpm_list_t *dbs, const char *name)
+{
+	CHECK_HANDLE(handle, return NULL);
+	ASSERT(name != NULL && strlen(name) != 0,
+			RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
+	alpm_pkg_t *pkg = _alpm_dbs_get_pkgfromcache(dbs, name);
+	ASSERT(pkg, RET_ERR(handle, ALPM_ERR_PKG_NOT_FOUND, NULL));
+	return pkg;
+}
+
 alpm_group_t SYMEXPORT *alpm_db_get_group(alpm_db_t *db, const char *name)
 {
 	ASSERT(db != NULL, return NULL);
@@ -363,6 +373,16 @@ const char *_alpm_db_path(alpm_db_t *db)
 	return db->_path;
 }
 
+alpm_pkg_t *_alpm_dbs_get_pkgfromcache(alpm_list_t *dbs, const char *name)
+{
+	alpm_pkg_t *pkg = NULL;
+	for(alpm_list_t *i = dbs; !pkg && i; i = i->next) {
+		alpm_db_t *db = i->data;
+		pkg = _alpm_db_get_pkgfromcache(db, name);
+	}
+	return pkg;
+}
+
 int _alpm_db_cmp(const void *d1, const void *d2)
 {
 	const alpm_db_t *db1 = d1;
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index 92c69ba7..dfb3212c 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -108,6 +108,7 @@ int _alpm_db_remove_pkgfromcache(alpm_db_t *db, alpm_pkg_t *pkg);
 alpm_pkghash_t *_alpm_db_get_pkgcache_hash(alpm_db_t *db);
 alpm_list_t *_alpm_db_get_pkgcache(alpm_db_t *db);
 alpm_pkg_t *_alpm_db_get_pkgfromcache(alpm_db_t *db, const char *target);
+alpm_pkg_t *_alpm_dbs_get_pkgfromcache(alpm_list_t *dbs, const char *name);
 /* groups */
 alpm_list_t *_alpm_db_get_groupcache(alpm_db_t *db);
 alpm_group_t *_alpm_db_get_groupfromcache(alpm_db_t *db, const char *target);
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index 7ef558c4..d934de73 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -55,16 +55,9 @@ struct keyinfo_t {
 
 alpm_pkg_t SYMEXPORT *alpm_sync_get_new_version(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
 {
-	alpm_list_t *i;
-	alpm_pkg_t *spkg = NULL;
-
 	ASSERT(pkg != NULL, return NULL);
 	pkg->handle->pm_errno = ALPM_ERR_OK;
-
-	for(i = dbs_sync; !spkg && i; i = i->next) {
-		alpm_db_t *db = i->data;
-		spkg = _alpm_db_get_pkgfromcache(db, pkg->name);
-	}
+	alpm_pkg_t *spkg = _alpm_dbs_get_pkgfromcache(dbs_sync, pkg->name);
 
 	if(spkg == NULL) {
 		_alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
diff --git a/src/pacman/query.c b/src/pacman/query.c
index e3e7d844..2fc2afd1 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -247,15 +247,9 @@ static int query_search(alpm_list_t *targets)
 static unsigned short pkg_get_locality(alpm_pkg_t *pkg)
 {
 	const char *pkgname = alpm_pkg_get_name(pkg);
-	alpm_list_t *j;
 	alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle);
-
-	for(j = sync_dbs; j; j = alpm_list_next(j)) {
-		if(alpm_db_get_pkg(j->data, pkgname)) {
-			return PKG_LOCALITY_NATIVE;
-		}
-	}
-	return PKG_LOCALITY_FOREIGN;
+	alpm_pkg_t *repo_pkg = alpm_dbs_get_pkg(config->handle, sync_dbs, pkgname);
+	return repo_pkg ? PKG_LOCALITY_NATIVE : PKG_LOCALITY_FOREIGN;
 }
 
 static int is_unrequired(alpm_pkg_t *pkg, unsigned short level)
-- 
2.31.1


More information about the pacman-dev mailing list