Use access() instead of stat() when possible
[pacman-ng.git] / src / util / testdb.c
blob87bfcf96ca6933411347f7eebd80ec78d89a3276
1 /*
2 * testdb.c : Test a pacman local database for validity
4 * Copyright (c) 2007 by Aaron Griffin <aaronmgriffin@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <libgen.h>
29 #include <alpm.h>
30 #include <alpm_list.h>
32 #define BASENAME "testdb"
34 int str_cmp(const void *s1, const void *s2)
36 return(strcmp(s1, s2));
39 static void cleanup(int signum) {
40 if(alpm_release() == -1) {
41 fprintf(stderr, "error releasing alpm: %s\n", alpm_strerrorlast());
44 exit(signum);
47 void output_cb(pmloglevel_t level, char *fmt, va_list args)
49 if(strlen(fmt)) {
50 switch(level) {
51 case PM_LOG_ERROR: printf("error: "); break;
52 case PM_LOG_WARNING: printf("warning: "); break;
53 default: return;
55 vprintf(fmt, args);
59 static int db_test(char *dbpath)
61 struct dirent *ent;
62 char path[PATH_MAX];
63 int ret = 0;
65 DIR *dir;
67 if(!(dir = opendir(dbpath))) {
68 fprintf(stderr, "error : %s : %s\n", dbpath, strerror(errno));
69 return(1);
72 while ((ent = readdir(dir)) != NULL) {
73 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
74 continue;
76 /* check for desc, depends, and files */
77 snprintf(path, PATH_MAX, "%s/%s/desc", dbpath, ent->d_name);
78 if(access(path, F_OK)) {
79 printf("%s: description file is missing\n", ent->d_name);
80 ret++;
82 snprintf(path, PATH_MAX, "%s/%s/depends", dbpath, ent->d_name);
83 if(access(path, F_OK)) {
84 printf("%s: dependency file is missing\n", ent->d_name);
85 ret++;
87 snprintf(path, PATH_MAX, "%s/%s/files", dbpath, ent->d_name);
88 if(access(path, F_OK)) {
89 printf("%s: file list is missing\n", ent->d_name);
90 ret++;
93 return(ret);
96 int main(int argc, char **argv)
98 int retval = 0; /* default = false */
99 pmdb_t *db = NULL;
100 char *dbpath;
101 char localdbpath[PATH_MAX];
102 alpm_list_t *i;
104 if(argc == 1) {
105 dbpath = DBPATH;
106 } else if(argc == 3 && strcmp(argv[1], "-b") == 0) {
107 dbpath = argv[2];
108 } else {
109 fprintf(stderr, "usage: %s -b <pacman db>\n", BASENAME);
110 return(1);
113 snprintf(localdbpath, PATH_MAX, "%s/local", dbpath);
114 retval = db_test(localdbpath);
115 if(retval) {
116 return(retval);
119 if(alpm_initialize() == -1) {
120 fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerrorlast());
121 return(1);
124 /* let us get log messages from libalpm */
125 alpm_option_set_logcb(output_cb);
127 alpm_option_set_dbpath(dbpath);
129 db = alpm_db_register_local();
130 if(db == NULL) {
131 fprintf(stderr, "error: could not register 'local' database (%s)\n",
132 alpm_strerrorlast());
133 cleanup(EXIT_FAILURE);
136 /* check dependencies */
137 alpm_list_t *data;
138 data = alpm_checkdeps(db, 0, NULL, alpm_db_getpkgcache(db));
139 for(i = data; i; i = alpm_list_next(i)) {
140 pmdepmissing_t *miss = alpm_list_getdata(i);
141 pmdepend_t *dep = alpm_miss_get_dep(miss);
142 char *depstring = alpm_dep_get_string(dep);
143 printf("missing dependency for %s : %s\n", alpm_miss_get_target(miss),
144 depstring);
145 free(depstring);
148 /* check conflicts */
149 data = alpm_checkdbconflicts(db);
150 for(i = data; i; i = i->next) {
151 pmconflict_t *conflict = alpm_list_getdata(i);
152 printf("%s conflicts with %s\n", alpm_conflict_get_package1(conflict),
153 alpm_conflict_get_package2(conflict));
156 cleanup(retval);
159 /* vim: set ts=2 sw=2 noet: */