1 #define _POSIX_C_SOURCE 200112L
10 #include <sys/types.h>
16 #include "t/ctap/ctap.h"
17 #include "lib/jsw/jsw_atree.h"
32 int check_content; /* ignored for config files */
37 printf("usage: zpm verify [-f file] -eF [pkgid ...]\n");
40 static char *column(char *col, int ncols, char **vals, char **cols) {
44 for (i=0; i < ncols; i++) {
45 if (!strcmp(col, cols[i])) {
53 static int count_plan(void *f, int ncols, char **vals, char **cols) {
54 struct config *conf = f;
56 int ftype, configfile = 0;
59 v = column("filetype", ncols, vals, cols);
61 v = column("configuration", ncols, vals, cols);
66 if (ftype == 'r' && !configfile) {
75 static int verify(void *f, int ncols, char **vals, char **cols) {
76 struct config *conf = f;
80 int ftype, configfile = 0;
81 char *v, *hash, ehash[ZPM_HASH_STRLEN+1];
83 path = column("path", ncols, vals, cols);
84 hash = column("hash", ncols, vals, cols);
85 v = column("filetype", ncols, vals, cols);
87 v = column("configuration", ncols, vals, cols);
94 rv = lstat(path, &st);
97 case ENOENT: ok(0, "%s does not exist", path);
100 default: ok(0, "%s%scannot stat %s: %s",
101 conf->pkgid ? conf->pkgid : "",
102 conf->pkgid ? " " : "",
103 path, strerror(errno));
107 if (ftype == 'r' && !configfile) {
113 if (!conf->failed_only) {
114 ok(1, "%s%s%s exists",
115 conf->pkgid ? conf->pkgid : "",
116 conf->pkgid ? " " : "",
124 if (ftype == 'r' && !configfile) {
125 zpm_hash(path, ehash, 0);
126 rv = strcmp(ehash, hash);
130 if (rv != 0 || !conf->failed_only) {
131 is_string(ehash, hash, "%s%shash %s",
132 conf->pkgid ? conf->pkgid : "",
133 conf->pkgid ? " " : "",
141 static int afind_strcmp(const void *a, const void *b) {
145 int main(int ac, char **av) {
150 jsw_atree_t *pkglist;
154 struct config conf = { 0 };
156 /* TODO could do nothing for the dup */
157 pkglist = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
159 conf.dbfile = "/var/lib/zpm/local.db";
161 if ((s = getenv("ZPMDB"))) {
162 /* TODO does this need to be copied ? */
166 while ((opt = getopt(ac, av, "f:eF")) != -1) {
168 case 'f': conf.dbfile = optarg; break;
169 case 'e': conf.skipuninstalled = 1; break;
170 case 'F': conf.failed_only = 1; break;
180 if (!zpm_open(&pkg, conf.dbfile)) {
181 fprintf(stderr, "can't open zpm db %s\n", conf.dbfile);
187 for (i = argn; i < ac; i++) {
188 pkgid = zpm_findpkg(&pkg, av[i], "status = 'installed'");
190 if (conf.skipuninstalled) {
193 fprintf(stderr, "no installed package: %s\n", av[i]);
199 if (!jsw_ainsert(pkglist, pkgid)) {
200 fprintf(stderr, "pkglist insert failed\n");
206 fprintf(stderr, "must specify pkgid\n");
213 for (pkgid = jsw_atfirst(list, pkglist); pkgid; pkgid = jsw_atnext(list)) {
216 if (!zpm_foreach_path(&pkg, pkgid, 0, count_plan, &conf, &errmsg)) {
218 fprintf(stderr, "database error: %s\n", errmsg);
221 if (pkg.error == 1) {
222 fprintf(stderr, "unable to allocate memory\n");
236 for (pkgid = jsw_atfirst(list, pkglist); pkgid; pkgid = jsw_atnext(list)) {
239 if (!zpm_foreach_path(&pkg, pkgid, 0, verify, &conf, &errmsg)) {
241 fprintf(stderr, "database error: %s\n", errmsg);
244 if (pkg.error == 1) {
245 fprintf(stderr, "unable to allocate memory\n");
251 if (pkglist) jsw_adelete(pkglist);
254 return conf.failed ? 1 : 0;