Cleanup usages of alpm_list_find and alpm_list_remove.

* remove obsolete and unused *_cmp helper functions like deppkg_cmp and
_alpm_grp_cmp

* new alpm_list_remove_str function, used 6 times in handle.c

* remove _alpm_prov_cmp / _alpm_db_whatprovides and replace them by
a more general alpm_find_pkg_satisfiers with a cleaner implementation.
before: alpm_db_whatprovides(db, targ)
after: alpm_find_pkg_satisfiers(alpm_db_getpkgcache(db), targ)

* remove satisfycmp and replace alpm_list_find + satisfycmp usage by
_alpm_find_dep_satisfiers.
before : alpm_list_find(_alpm_db_get_pkgcache(db), dep, satisfycmp)
after : _alpm_find_dep_satisfiers(_alpm_db_get_pkgcache(db), dep)

* remove _alpm_pkgname_pkg_cmp, which was used with alpm_list_remove, and
use _alpm_pkg_find + alpm_list_remove with _alpm_pkg_cmp instead.

This commit actually get rids of all complicated and asymmetric _cmp
functions. I first thought these functions were worth it, be caused it
allowed us to reuse list_find and list_remove. But this was at the detriment
of the clarity and also the ease of use of these functions, dangerous
because of their asymmetricity.

Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Chantry Xavier 2008-05-10 18:47:42 +02:00 committed by Dan McGee
parent 8248b4bfb1
commit f43805d875
16 changed files with 90 additions and 143 deletions

View File

@ -96,15 +96,6 @@ error:
return(-1);
}
/* This is still messy. We have a lot of compare functions, and we should
* try to consolidate them as much as we can (between add and sync) */
/*static int deppkg_cmp(const void *p1, const void *p2)
{
return(strcmp(((pmdepmissing_t *)p1)->target,
((pmdepmissing_t *)p2)->target));
}*/
int _alpm_add_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
{
alpm_list_t *lp = NULL;

View File

@ -167,7 +167,6 @@ int alpm_db_update(int level, pmdb_t *db);
pmpkg_t *alpm_db_get_pkg(pmdb_t *db, const char *name);
alpm_list_t *alpm_db_getpkgcache(pmdb_t *db);
alpm_list_t *alpm_db_whatprovides(pmdb_t *db, const char *name);
pmgrp_t *alpm_db_readgrp(pmdb_t *db, const char *name);
alpm_list_t *alpm_db_getgrpcache(pmdb_t *db);
@ -432,6 +431,7 @@ int alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep);
alpm_list_t *alpm_checkdeps(pmdb_t *db, int reversedeps,
alpm_list_t *remove, alpm_list_t *upgrade);
alpm_list_t *alpm_deptest(pmdb_t *db, alpm_list_t *targets);
alpm_list_t *alpm_find_pkg_satisfiers(alpm_list_t *pkgs, const char *pkgname);
const char *alpm_miss_get_target(const pmdepmissing_t *miss);
pmdepend_t *alpm_miss_get_dep(pmdepmissing_t *miss);

View File

@ -307,7 +307,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack, const void *needl
continue;
}
tmp = i->next;
if(fn(needle, i->data) == 0) {
if(fn(i->data, needle) == 0) {
/* we found a matching item */
if(i == haystack) {
/* Special case: removing the head node which has a back reference to
@ -350,6 +350,22 @@ alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack, const void *needl
return(haystack);
}
/**
* @brief Remove a string from a list.
*
* @param haystack the list to remove the item from
* @param needle the data member of the item we're removing
* @param data output parameter containing data of the removed item
*
* @return the resultant list
*/
alpm_list_t SYMEXPORT *alpm_list_remove_str(alpm_list_t *haystack,
const char *needle, char **data)
{
return(alpm_list_remove(haystack, (const void *)needle,
(alpm_list_fn_cmp)strcmp, (void **)data));
}
/**
* @brief Create a new list without any duplicates.
*
@ -586,7 +602,7 @@ void SYMEXPORT *alpm_list_find(const alpm_list_t *haystack, const void *needle,
}
/* trivial helper function for alpm_list_find_ptr */
static int ptrcmp(const void *p, const void *q)
static int ptr_cmp(const void *p, const void *q)
{
return(p != q);
}
@ -603,7 +619,7 @@ static int ptrcmp(const void *p, const void *q)
*/
void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle)
{
return(alpm_list_find(haystack, needle, ptrcmp));
return(alpm_list_find(haystack, needle, ptr_cmp));
}
/**
@ -614,9 +630,11 @@ void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack, const void *need
*
* @return `needle` if found, NULL otherwise
*/
char SYMEXPORT *alpm_list_find_str(const alpm_list_t *haystack, const char *needle)
char SYMEXPORT *alpm_list_find_str(const alpm_list_t *haystack,
const char *needle)
{
return((char *)alpm_list_find(haystack, (const void*)needle, (alpm_list_fn_cmp)strcmp));
return((char *)alpm_list_find(haystack, (const void*)needle,
(alpm_list_fn_cmp)strcmp));
}
/**

View File

@ -57,6 +57,7 @@ alpm_list_t *alpm_list_join(alpm_list_t *first, alpm_list_t *second);
alpm_list_t *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, alpm_list_fn_cmp fn);
alpm_list_t *alpm_list_msort(alpm_list_t *list, int n, alpm_list_fn_cmp fn);
alpm_list_t *alpm_list_remove(alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn, void **data);
alpm_list_t *alpm_list_remove_str(alpm_list_t *haystack, const char *needle, char **data);
alpm_list_t *alpm_list_remove_dupes(const alpm_list_t *list);
alpm_list_t *alpm_list_strdup(const alpm_list_t *list);
alpm_list_t *alpm_list_copy(const alpm_list_t *list);

View File

@ -266,23 +266,6 @@ alpm_list_t SYMEXPORT *alpm_db_getpkgcache(pmdb_t *db)
return(_alpm_db_get_pkgcache(db));
}
/** Get the list of packages that a package provides
* @param db pointer to the package database to get the package from
* @param name name of the package
* @return the list of packages on success, NULL on error
*/
alpm_list_t SYMEXPORT *alpm_db_whatprovides(pmdb_t *db, const char *name)
{
ALPM_LOG_FUNC;
/* Sanity checks */
ASSERT(handle != NULL, return(NULL));
ASSERT(db != NULL, return(NULL));
ASSERT(name != NULL && strlen(name) != 0, return(NULL));
return(_alpm_db_whatprovides(db, name));
}
/** Get a group entry from a package database
* @param db pointer to the package database to get the group from
* @param name of the group
@ -364,10 +347,11 @@ void _alpm_db_free(pmdb_t *db)
return;
}
int _alpm_db_cmp(const void *db1, const void *db2)
int _alpm_db_cmp(const void *d1, const void *d2)
{
ALPM_LOG_FUNC;
return(strcmp(((pmdb_t *)db1)->treename, ((pmdb_t *)db2)->treename));
pmdb_t *db1 = (pmdb_t *)db1;
pmdb_t *db2 = (pmdb_t *)db2;
return(strcmp(db1->treename, db2->treename));
}
alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
@ -533,47 +517,4 @@ pmdb_t *_alpm_db_register_sync(const char *treename)
return(db);
}
/* helper function for alpm_list_find and _alpm_db_whatprovides
*
* @return "provision.name" == needle (as string)
*/
int _alpm_prov_cmp(const void *provision, const void *needle)
{
char *tmpptr;
char *provname = strdup(provision);
int retval = 0;
tmpptr = strchr(provname, '=');
if(tmpptr != NULL) { /* provision-version */
*tmpptr='\0';
}
retval = strcmp(provname, needle);
free(provname);
return(retval);
}
/* return a alpm_list_t of packages in "db" that provide "package"
*/
alpm_list_t *_alpm_db_whatprovides(pmdb_t *db, const char *package)
{
alpm_list_t *pkgs = NULL;
alpm_list_t *lp;
ALPM_LOG_FUNC;
if(db == NULL || package == NULL || strlen(package) == 0) {
return(NULL);
}
for(lp = _alpm_db_get_pkgcache(db); lp; lp = lp->next) {
pmpkg_t *info = lp->data;
if(alpm_list_find(alpm_pkg_get_provides(info), (const void *)package, _alpm_prov_cmp)) {
pkgs = alpm_list_add(pkgs, info);
}
}
return(pkgs);
}
/* vim: set ts=2 sw=2 noet: */

View File

@ -55,10 +55,6 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles);
pmdb_t *_alpm_db_register_local(void);
pmdb_t *_alpm_db_register_sync(const char *treename);
/* Provision */
int _alpm_prov_cmp(const void *provision, const void *needle);
alpm_list_t *_alpm_db_whatprovides(pmdb_t *db, const char *package);
/* be.c, backend specific calls */
int _alpm_db_open(pmdb_t *db);
void _alpm_db_close(pmdb_t *db);

View File

@ -194,10 +194,30 @@ alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, int reverse)
return(newtargs);
}
/* Little helper function for alpm_list_find */
static int satisfycmp(const void *pkg, const void *depend)
alpm_list_t *_alpm_find_dep_satisfiers(alpm_list_t *pkgs, pmdepend_t *dep)
{
return(!alpm_depcmp((pmpkg_t*) pkg, (pmdepend_t*) depend));
alpm_list_t *i, *ret = NULL;
for(i = pkgs; i; i = alpm_list_next(i)) {
pmpkg_t *pkg = i->data;
if(alpm_depcmp(pkg, dep)) {
ret = alpm_list_add(ret, pkg);
}
}
return(ret);
}
/** Find packages in a list that provide a given package.
* @param pkgs an alpm_list_t* of package to search
* @param pkgname the name of the package
* @return an alpm_list_t* of packages that provide pkgname
*/
alpm_list_t SYMEXPORT *alpm_find_pkg_satisfiers(alpm_list_t *pkgs, const char *pkgname)
{
pmdepend_t *dep = _alpm_splitdep(pkgname);
alpm_list_t *res = _alpm_find_dep_satisfiers(pkgs, dep);
_alpm_dep_free(dep);
return(res);
}
/** Checks dependencies and returns missing ones in a list.
@ -217,7 +237,7 @@ alpm_list_t SYMEXPORT *alpm_deptest(pmdb_t *db, alpm_list_t *targets)
target = alpm_list_getdata(i);
dep = _alpm_splitdep(target);
if(!alpm_list_find(_alpm_db_get_pkgcache(db), dep, satisfycmp)) {
if(!_alpm_find_dep_satisfiers(_alpm_db_get_pkgcache(db), dep)) {
ret = alpm_list_add(ret, target);
}
_alpm_dep_free(dep);
@ -268,8 +288,8 @@ alpm_list_t SYMEXPORT *alpm_checkdeps(pmdb_t *db, int reversedeps,
pmdepend_t *depend = j->data;
/* 1. we check the upgrade list */
/* 2. we check database for untouched satisfying packages */
if(!alpm_list_find(upgrade, depend, satisfycmp) &&
!alpm_list_find(dblist, depend, satisfycmp)) {
if(!_alpm_find_dep_satisfiers(upgrade, depend) &&
!_alpm_find_dep_satisfiers(dblist, depend)) {
/* Unsatisfied dependency in the upgrade list */
char *missdepstring = alpm_dep_get_string(depend);
_alpm_log(PM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
@ -288,13 +308,13 @@ alpm_list_t SYMEXPORT *alpm_checkdeps(pmdb_t *db, int reversedeps,
pmpkg_t *lp = i->data;
for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
pmdepend_t *depend = j->data;
pmpkg_t *causingpkg = alpm_list_find(modified, depend, satisfycmp);
pmpkg_t *causingpkg = alpm_list_getdata(_alpm_find_dep_satisfiers(modified, depend));
/* we won't break this depend, if it is already broken, we ignore it */
/* 1. check upgrade list for satisfiers */
/* 2. check dblist for satisfiers */
if(causingpkg &&
!alpm_list_find(upgrade, depend, satisfycmp) &&
!alpm_list_find(dblist, depend, satisfycmp)) {
!_alpm_find_dep_satisfiers(upgrade, depend) &&
!_alpm_find_dep_satisfiers(dblist, depend)) {
char *missdepstring = alpm_dep_get_string(depend);
_alpm_log(PM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
missdepstring, alpm_pkg_get_name(lp));

View File

@ -51,6 +51,7 @@ int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,
alpm_list_t **list, alpm_list_t *remove, pmtrans_t *trans, alpm_list_t
**data);
pmdepend_t *_alpm_splitdep(const char *depstring);
alpm_list_t *_alpm_find_dep_satisfiers(alpm_list_t *pkgs, pmdepend_t *dep);
#endif /* _ALPM_DEPS_H */

View File

@ -56,16 +56,6 @@ void _alpm_grp_free(pmgrp_t *grp)
FREE(grp);
}
/* Helper function for sorting groups
*/
int _alpm_grp_cmp(const void *g1, const void *g2)
{
pmgrp_t *grp1 = (pmgrp_t *)g1;
pmgrp_t *grp2 = (pmgrp_t *)g2;
return(strcmp(grp1->name, grp2->name));
}
const char SYMEXPORT *alpm_grp_get_name(const pmgrp_t *grp)
{
ALPM_LOG_FUNC;

View File

@ -30,7 +30,6 @@ struct __pmgrp_t {
pmgrp_t *_alpm_grp_new(const char *name);
void _alpm_grp_free(pmgrp_t *grp);
int _alpm_grp_cmp(const void *g1, const void *g2);
#endif /* _ALPM_GROUP_H */

View File

@ -384,7 +384,7 @@ void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs)
int SYMEXPORT alpm_option_remove_cachedir(const char *cachedir)
{
void *vdata = NULL;
char *vdata = NULL;
char *newcachedir;
size_t cachedirlen;
/* verify cachedir ends in a '/' */
@ -395,8 +395,7 @@ int SYMEXPORT alpm_option_remove_cachedir(const char *cachedir)
newcachedir = calloc(cachedirlen + 1, sizeof(char));
strncpy(newcachedir, cachedir, cachedirlen);
newcachedir[cachedirlen-1] = '/';
handle->cachedirs = alpm_list_remove(handle->cachedirs, newcachedir,
_alpm_str_cmp, &vdata);
handle->cachedirs = alpm_list_remove_str(handle->cachedirs, newcachedir, &vdata);
FREE(newcachedir);
if(vdata != NULL) {
FREE(vdata);
@ -449,9 +448,8 @@ void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade)
int SYMEXPORT alpm_option_remove_noupgrade(const char *pkg)
{
void *vdata = NULL;
handle->noupgrade = alpm_list_remove(handle->noupgrade, pkg,
_alpm_str_cmp, &vdata);
char *vdata = NULL;
handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
if(vdata != NULL) {
FREE(vdata);
return(1);
@ -472,9 +470,8 @@ void SYMEXPORT alpm_option_set_noextracts(alpm_list_t *noextract)
int SYMEXPORT alpm_option_remove_noextract(const char *pkg)
{
void *vdata = NULL;
handle->noextract = alpm_list_remove(handle->noextract, pkg,
_alpm_str_cmp, &vdata);
char *vdata = NULL;
handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
if(vdata != NULL) {
FREE(vdata);
return(1);
@ -495,9 +492,8 @@ void SYMEXPORT alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
int SYMEXPORT alpm_option_remove_ignorepkg(const char *pkg)
{
void *vdata = NULL;
handle->ignorepkg = alpm_list_remove(handle->ignorepkg, pkg,
_alpm_str_cmp, &vdata);
char *vdata = NULL;
handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
if(vdata != NULL) {
FREE(vdata);
return(1);
@ -518,9 +514,8 @@ void SYMEXPORT alpm_option_set_holdpkgs(alpm_list_t *holdpkgs)
int SYMEXPORT alpm_option_remove_holdpkg(const char *pkg)
{
void *vdata = NULL;
handle->holdpkg = alpm_list_remove(handle->holdpkg, pkg,
_alpm_str_cmp, &vdata);
char *vdata = NULL;
handle->holdpkg = alpm_list_remove_str(handle->holdpkg, pkg, &vdata);
if(vdata != NULL) {
FREE(vdata);
return(1);
@ -541,9 +536,8 @@ void SYMEXPORT alpm_option_set_ignoregrps(alpm_list_t *ignoregrps)
int SYMEXPORT alpm_option_remove_ignoregrp(const char *grp)
{
void *vdata = NULL;
handle->ignoregrp = alpm_list_remove(handle->ignoregrp, grp,
_alpm_str_cmp, &vdata);
char *vdata = NULL;
handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
if(vdata != NULL) {
FREE(vdata);
return(1);

View File

@ -837,15 +837,9 @@ int _alpm_pkg_compare_versions(pmpkg_t *local_pkg, pmpkg_t *pkg)
*/
int _alpm_pkg_cmp(const void *p1, const void *p2)
{
pmpkg_t *pk1 = (pmpkg_t *)p1;
pmpkg_t *pk2 = (pmpkg_t *)p2;
return(strcmp(alpm_pkg_get_name(pk1), alpm_pkg_get_name(pk2)));
}
int _alpm_pkgname_pkg_cmp(const void *pkgname, const void *package)
{
return(strcmp(alpm_pkg_get_name((pmpkg_t *) package), (char *) pkgname));
pmpkg_t *pkg1 = (pmpkg_t *)p1;
pmpkg_t *pkg2 = (pmpkg_t *)p2;
return(strcmp(alpm_pkg_get_name(pkg1), alpm_pkg_get_name(pkg2)));
}
/* Test for existence of a package in a alpm_list_t*

View File

@ -79,7 +79,6 @@ pmpkg_t* _alpm_pkg_new(const char *name, const char *version);
pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg);
void _alpm_pkg_free(pmpkg_t *pkg);
int _alpm_pkg_cmp(const void *p1, const void *p2);
int _alpm_pkgname_pkg_cmp(const void *pkgname, const void *package);
int _alpm_pkg_compare_versions(pmpkg_t *local_pkg, pmpkg_t *pkg);
pmpkg_t *_alpm_pkg_find(alpm_list_t *haystack, const char *needle);
int _alpm_pkg_should_ignore(pmpkg_t *pkg);

View File

@ -119,9 +119,9 @@ static void remove_prepare_keep_needed(pmtrans_t *trans, pmdb_t *db,
for(i = lp; i; i = i->next) {
pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
void *vpkg;
pmpkg_t *pkg;
trans->packages = alpm_list_remove(trans->packages, miss->causingpkg,
_alpm_pkgname_pkg_cmp, &vpkg);
pmpkg_t *pkg = _alpm_pkg_find(trans->packages, miss->causingpkg);
trans->packages = alpm_list_remove(trans->packages, pkg, _alpm_pkg_cmp,
&vpkg);
pkg = vpkg;
if(pkg) {
_alpm_log(PM_LOG_WARNING, "removing %s from the target-list\n",

View File

@ -372,11 +372,8 @@ static int syncpkg_cmp(const void *s1, const void *s2)
{
const pmsyncpkg_t *sp1 = s1;
const pmsyncpkg_t *sp2 = s2;
pmpkg_t *p1, *p2;
p1 = alpm_sync_get_pkg(sp1);
p2 = alpm_sync_get_pkg(sp2);
pmpkg_t *p1 = alpm_sync_get_pkg(sp1);
pmpkg_t *p2 = alpm_sync_get_pkg(sp2);
return(strcmp(alpm_pkg_get_name(p1), alpm_pkg_get_name(p2)));
}
@ -531,12 +528,12 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
conflict->package1, conflict->package2);
/* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
if(alpm_list_find(alpm_pkg_get_provides(sync1->pkg),
conflict->package2, _alpm_prov_cmp)) {
pmdepend_t *dep1 = _alpm_splitdep(conflict->package1);
pmdepend_t *dep2 = _alpm_splitdep(conflict->package2);
if(alpm_depcmp(sync1->pkg, dep2)) {
rsync = sync2;
sync = sync1;
} else if(alpm_list_find(alpm_pkg_get_provides(sync2->pkg),
conflict->package1, _alpm_prov_cmp)) {
} else if(alpm_depcmp(sync2->pkg, dep1)) {
rsync = sync1;
sync = sync2;
} else {
@ -551,8 +548,12 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
}
alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
alpm_list_free(deps);
_alpm_dep_free(dep1);
_alpm_dep_free(dep2);
goto cleanup;
}
_alpm_dep_free(dep1);
_alpm_dep_free(dep2);
/* Prints warning */
_alpm_log(PM_LOG_WARNING,

View File

@ -641,7 +641,9 @@ static int sync_trans(alpm_list_t *targets)
alpm_list_t *prov = NULL;
for(j = sync_dbs; j; j = alpm_list_next(j)) {
pmdb_t *db = alpm_list_getdata(j);
prov = alpm_list_join(prov, alpm_db_whatprovides(db, targ));
alpm_list_t *dblist = alpm_db_getpkgcache(db);
alpm_list_t *satisfiers = alpm_find_pkg_satisfiers(dblist, targ);
prov = alpm_list_join(prov, satisfiers);
}
if(prov != NULL) {
if(alpm_list_count(prov) == 1) {