>From 87a2748822e95d65d358fd81f31825f10e1c256c Mon Sep 17 00:00:00 2001 From: Chantry Xavier Date: Tue, 17 Jul 2007 14:21:01 +0200 Subject: [PATCH] Add testdb util for finding inconsistencies in the database. Signed-off-by: Chantry Xavier --- src/util/.gitignore | 1 + src/util/Makefile.am | 5 +- src/util/testdb.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 1 deletions(-) create mode 100644 src/util/testdb.c diff --git a/src/util/.gitignore b/src/util/.gitignore index 96ef10d..3668880 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore @@ -2,3 +2,4 @@ .libs vercmp testpkg +testdb diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 0c48f10..676b442 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = vercmp testpkg +bin_PROGRAMS = vercmp testpkg testdb INCLUDES = -I$(top_srcdir)/lib/libalpm @@ -10,4 +10,7 @@ vercmp_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la testpkg_SOURCES = testpkg.c testpkg_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la +testdb_SOURCES = testdb.c +testdb_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la + # vim:set ts=2 sw=2 noet: diff --git a/src/util/testdb.c b/src/util/testdb.c new file mode 100644 index 0000000..9d7a8d8 --- /dev/null +++ b/src/util/testdb.c @@ -0,0 +1,201 @@ +/* + * testdb.c : Test a pacman local database for validity + * + * Copyright (c) 2007 by Aaron Griffin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define PATH_MAX 1024 + +static void cleanup(int signum) { + if(alpm_release() == -1) { + fprintf(stderr, "error releasing alpm: %s\n", alpm_strerror(pm_errno)); + } + + exit(signum); +} + +void output_cb(pmloglevel_t level, char *fmt, va_list args) +{ + if(strlen(fmt)) { + switch(level) { + case PM_LOG_ERROR: printf("error: "); break; + case PM_LOG_WARNING: printf("warning: "); break; + default: return; + } + vprintf(fmt, args); + printf("\n"); + } +} + +int db_test(char *dbpath) +{ + struct dirent *ent; + char path[PATH_MAX]; + struct stat buf; + int ret = 0; + + DIR *dir; + + if(!(dir = opendir(dbpath))) { + fprintf(stderr, "error : %s : %s\n", dbpath, strerror(errno)); + return(1); + } + + while ((ent = readdir(dir)) != NULL) { + if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + continue; + } + /* check for desc, depends, and files */ + snprintf(path, PATH_MAX, "%s/%s/desc", dbpath, ent->d_name); + if(stat(path, &buf)) { + printf("%s: description file is missing\n", ent->d_name); + ret++; + } + snprintf(path, PATH_MAX, "%s/%s/depends", dbpath, ent->d_name); + if(stat(path, &buf)) { + printf("%s: dependency file is missing\n", ent->d_name); + ret++; + } + snprintf(path, PATH_MAX, "%s/%s/files", dbpath, ent->d_name); + if(stat(path, &buf)) { + printf("%s: file list is missing\n", ent->d_name); + ret++; + } + } + return(ret); +} + +int check_requiredby(pmpkg_t *pkg) +{ + 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++; + } + } + return(retval); +} + +int check_depends(pmpkg_t *pkg) +{ + int retval = 0; + alpm_list_t *i, *j; + pmdb_t *db = alpm_option_get_localdb(); + + const char *pkgname = alpm_pkg_get_name(pkg); + alpm_list_t *depends = alpm_pkg_get_depends(pkg); + + for(i = depends; i; i = i->next) { + if(!i->data) { + continue; + } + char *depname = i->data; + pmdepend_t* dep = alpm_splitdep(depname); + if(dep == NULL) { + continue; + } + int found = 0; + for(j = alpm_db_getpkgcache(db); j; j = j->next) { + 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++; + } + } + } + if(!found) { + printf("%s : missing dependency %s\n", pkgname, depname); + retval++; + } + free(dep); + } + return(retval); +} + +int main(int argc, char **argv) +{ + int retval = 0; /* default = false */ + pmdb_t *db = NULL; + char dbpath[PATH_MAX]; + alpm_list_t *i; + + if(argc != 2) { + fprintf(stderr, "usage: %s \n", basename(argv[0])); + return(1); + } + + snprintf(dbpath, PATH_MAX, "%s/local", argv[1]); + + retval = db_test(dbpath); + if(retval) { + exit(retval); + } + + if(alpm_initialize() == -1) { + fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(pm_errno)); + return(1); + } + + /* let us get log messages from libalpm */ + alpm_option_set_logcb(output_cb); + + alpm_option_set_dbpath(argv[1]); + + db = alpm_db_register("local"); + if(db == NULL) { + fprintf(stderr, "error: could not register 'local' database (%s)\n", + alpm_strerror(pm_errno)); + cleanup(EXIT_FAILURE); + } + + 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); + } + + cleanup(retval); +} -- 1.5.2.4