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/>.
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());
47 void output_cb(pmloglevel_t level
, char *fmt
, va_list args
)
51 case PM_LOG_ERROR
: printf("error: "); break;
52 case PM_LOG_WARNING
: printf("warning: "); break;
59 static int db_test(char *dbpath
)
67 if(!(dir
= opendir(dbpath
))) {
68 fprintf(stderr
, "error : %s : %s\n", dbpath
, strerror(errno
));
72 while ((ent
= readdir(dir
)) != NULL
) {
73 if(!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, "..")) {
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
);
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
);
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
);
96 int main(int argc
, char **argv
)
98 int retval
= 0; /* default = false */
101 char localdbpath
[PATH_MAX
];
106 } else if(argc
== 3 && strcmp(argv
[1], "-b") == 0) {
109 fprintf(stderr
, "usage: %s -b <pacman db>\n", BASENAME
);
113 snprintf(localdbpath
, PATH_MAX
, "%s/local", dbpath
);
114 retval
= db_test(localdbpath
);
119 if(alpm_initialize() == -1) {
120 fprintf(stderr
, "cannot initialize alpm: %s\n", alpm_strerrorlast());
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();
131 fprintf(stderr
, "error: could not register 'local' database (%s)\n",
132 alpm_strerrorlast());
133 cleanup(EXIT_FAILURE
);
136 /* check dependencies */
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
),
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
));
159 /* vim: set ts=2 sw=2 noet: */