>From c0f445808507029391522e7a1524cec82dfc59f0 Mon Sep 17 00:00:00 2001 From: Chantry Xavier Date: Fri, 20 Jul 2007 17:02:59 +0200 Subject: [PATCH] testdb : slightly more efficient implementation. This also detects wrongly duplicated requiredby entries. --- lib/libalpm/alpm.h | 2 + lib/libalpm/package.c | 20 +++++++++ lib/libalpm/package.h | 1 + src/util/testdb.c | 109 +++++++++++++++++++++++++++++++++++++------------ 4 files changed, 105 insertions(+), 27 deletions(-) diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index 9e641f3..cdd6311 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -211,6 +211,8 @@ alpm_list_t *alpm_pkg_get_licenses(pmpkg_t *pkg); alpm_list_t *alpm_pkg_get_groups(pmpkg_t *pkg); alpm_list_t *alpm_pkg_get_depends(pmpkg_t *pkg); alpm_list_t *alpm_pkg_get_requiredby(pmpkg_t *pkg); +alpm_list_t *alpm_pkg_get_newrequiredby(pmpkg_t *pkg); +void alpm_pkg_set_newrequiredby(pmpkg_t *pkg, alpm_list_t *newrequiredby); alpm_list_t *alpm_pkg_get_conflicts(pmpkg_t *pkg); alpm_list_t *alpm_pkg_get_provides(pmpkg_t *pkg); alpm_list_t *alpm_pkg_get_replaces(pmpkg_t *pkg); diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index d5eca20..437647f 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -519,6 +519,25 @@ alpm_list_t SYMEXPORT *alpm_pkg_get_requiredby(pmpkg_t *pkg) return pkg->requiredby; } +void SYMEXPORT alpm_pkg_set_newrequiredby(pmpkg_t *pkg, alpm_list_t *newrequiredby) +{ + pkg->newrequiredby = newrequiredby; +} + +alpm_list_t SYMEXPORT *alpm_pkg_get_newrequiredby(pmpkg_t *pkg) +{ + ALPM_LOG_FUNC; + + /* Sanity checks */ + ASSERT(handle != NULL, return(NULL)); + ASSERT(pkg != NULL, return(NULL)); + + if(pkg->origin == PKG_FROM_CACHE && !(pkg->infolevel & INFRQ_DEPENDS)) { + _alpm_db_read(pkg->data, pkg, INFRQ_DEPENDS); + } + return pkg->newrequiredby; +} + alpm_list_t SYMEXPORT *alpm_pkg_get_conflicts(pmpkg_t *pkg) { ALPM_LOG_FUNC; @@ -746,6 +765,7 @@ pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg) newpkg->licenses = alpm_list_strdup(alpm_pkg_get_licenses(pkg)); /*newpkg->desc_localized = alpm_list_strdup(pkg->desc_localized);*/ newpkg->requiredby = alpm_list_strdup(alpm_pkg_get_requiredby(pkg)); + newpkg->newrequiredby = alpm_list_strdup(alpm_pkg_get_newrequiredby(pkg)); newpkg->conflicts = alpm_list_strdup(alpm_pkg_get_conflicts(pkg)); newpkg->files = alpm_list_strdup(alpm_pkg_get_files(pkg)); newpkg->backup = alpm_list_strdup(alpm_pkg_get_backup(pkg)); diff --git a/lib/libalpm/package.h b/lib/libalpm/package.h index f704ab9..8c89807 100644 --- a/lib/libalpm/package.h +++ b/lib/libalpm/package.h @@ -75,6 +75,7 @@ struct __pmpkg_t { alpm_list_t *backup; alpm_list_t *depends; alpm_list_t *requiredby; + alpm_list_t *newrequiredby; alpm_list_t *conflicts; alpm_list_t *provides; /* internal */ diff --git a/src/util/testdb.c b/src/util/testdb.c index 9d7a8d8..8040894 100644 --- a/src/util/testdb.c +++ b/src/util/testdb.c @@ -92,27 +92,17 @@ int db_test(char *dbpath) return(ret); } -int check_requiredby(pmpkg_t *pkg) +static void display_list(alpm_list_t *list) { - int retval = 0; - alpm_list_t *reqs, *deps; - const char *pkgname = alpm_pkg_get_name(pkg); - - for(reqs = alpm_pkg_get_requiredby(pkg); reqs; reqs = reqs->next) { - int found = 0; - char *reqname = reqs->data; - pmpkg_t *reqpkg = alpm_db_get_pkg(alpm_option_get_localdb(), reqname); - for(deps = alpm_pkg_get_depends(reqpkg); deps && !found; deps = deps->next) { - pmdepend_t *dep = alpm_splitdep(deps->data); - found = alpm_depcmp(pkg, dep); - free(dep); - } - if(!found) { - printf("%s : wrong requiredby field %s\n", pkgname, reqname); - retval++; + alpm_list_t *i; + for(i = list; i; i = i->next) { + char *s = i->data; + if(i->next) { + printf("%s, ", s); + } else { + printf("%s\n", s); } } - return(retval); } int check_depends(pmpkg_t *pkg) @@ -123,6 +113,7 @@ int check_depends(pmpkg_t *pkg) const char *pkgname = alpm_pkg_get_name(pkg); alpm_list_t *depends = alpm_pkg_get_depends(pkg); + alpm_list_t *missing_deps = NULL; for(i = depends; i; i = i->next) { if(!i->data) { @@ -138,23 +129,66 @@ int check_depends(pmpkg_t *pkg) pmpkg_t *deppkg = j->data; if(deppkg && alpm_depcmp(deppkg, dep)) { found = 1; - alpm_list_t *rqdby = alpm_pkg_get_requiredby(deppkg); - const char *deppkgname = alpm_pkg_get_name(deppkg); - if(!alpm_list_find_str(rqdby, pkgname)) { - printf("%s : missing requiredby field %s\n", deppkgname, pkgname); - retval++; - } + alpm_list_t *newrqdby = alpm_pkg_get_newrequiredby(deppkg); + newrqdby = alpm_list_add(newrqdby, (char *)pkgname); + alpm_pkg_set_newrequiredby(deppkg, newrqdby); } } if(!found) { - printf("%s : missing dependency %s\n", pkgname, depname); - retval++; + missing_deps = alpm_list_add(missing_deps, depname); } free(dep); } + if(missing_deps) { + retval++; + printf("missing deps for %s : ", pkgname); + display_list(missing_deps); + } return(retval); } +int _alpm_str_cmp(const void *s1, const void *s2) +{ + return(strcmp(s1, s2)); +} + +static void diff(alpm_list_t *list1, alpm_list_t *list2, + alpm_list_t **result1, alpm_list_t **result2) +{ + list1 = alpm_list_msort(list1, alpm_list_count(list1), _alpm_str_cmp); + list2 = alpm_list_msort(list2, alpm_list_count(list2), _alpm_str_cmp); + + alpm_list_t *i = list1; + alpm_list_t *j = list2; + + while(i || j) { + char *s1 = NULL; + char *s2 = NULL; + int n; + if(i && !j) { + n = -1; + } else if(!i && j) { + n = 1; + } else { + s1 = i->data; + s2 = j->data; + n = strcmp(s1, s2); + } + if(n < 0) { + s1 = i->data; + *result1 = alpm_list_add(*result1, s1); + i = i->next; + } else if (n > 0) { + s2 = j->data; + *result2 = alpm_list_add(*result2, s2); + j = j->next; + } else { + i = i->next; + j = j->next; + } + } +} + int main(int argc, char **argv) { int retval = 0; /* default = false */ @@ -191,11 +225,32 @@ int main(int argc, char **argv) cleanup(EXIT_FAILURE); } + printf("CHECKING DEPENDENCIES\n\n"); for(i = alpm_db_getpkgcache(db); i; i = alpm_list_next(i)) { pmpkg_t *pkg = alpm_list_getdata(i); - retval += check_requiredby(pkg); retval += check_depends(pkg); } + printf("\nCHECKING REQUIREDBY\n\n"); + for(i = alpm_db_getpkgcache(db); i; i = alpm_list_next(i)) { + alpm_list_t *extra = NULL, *missing = NULL; + pmpkg_t *pkg = alpm_list_getdata(i); + alpm_list_t *rqdby = alpm_pkg_get_requiredby(pkg); + alpm_list_t *newrqdby = alpm_pkg_get_newrequiredby(pkg); + diff(rqdby, newrqdby, &extra, &missing); + if(extra || missing) { + retval++; + const char *pkgname = alpm_pkg_get_name(pkg); + if(extra) { + printf("wrong requiredby for %s : ", pkgname); + display_list(extra); + } + if(missing) { + printf("missing requiredby for %s : ", pkgname); + display_list(missing); + } + } + } + cleanup(retval); } -- 1.5.2.4