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 foreach-path [-fncC] args ...\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 pkglist = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
155 struct config conf = { 0 };
157 conf.dbfile = "/var/lib/zpm/local.db";
159 if ((s = getenv("ZPMDB"))) {
160 /* TODO does this need to be copied ? */
164 while ((opt = getopt(ac, av, "f:eF")) != -1) {
166 case 'f': conf.dbfile = optarg; break;
167 case 'e': conf.skipuninstalled = 1; break;
168 case 'F': conf.failed_only = 1; break;
178 if (!zpm_open(&pkg, conf.dbfile)) {
179 fprintf(stderr, "can't open zpm db %s\n", conf.dbfile);
186 fprintf(stderr, "must specify pkgid\n");
191 for (i = argn; i < ac; i++) {
192 pkgid = zpm_findpkg(&pkg, av[i], "status = 'installed'");
194 if (conf.skipuninstalled) {
197 fprintf(stderr, "no installed package: %s\n", av[i]);
204 if (!zpm_foreach_path(&pkg, pkgid, 0, count_plan, &conf, &errmsg)) {
206 fprintf(stderr, "database error: %s\n", errmsg);
209 if (pkg.error == 1) {
210 fprintf(stderr, "unable to allocate memory\n");
212 fprintf(stderr, "unable to plan\n");
215 if (!jsw_ainsert(pkglist, pkgid)) {
216 fprintf(stderr, "pkglist insert failed\n");
229 for (pkgid = jsw_atfirst(list, pkglist); pkgid; pkgid = jsw_atnext(list)) {
232 if (!zpm_foreach_path(&pkg, pkgid, 0, verify, &conf, &errmsg)) {
234 fprintf(stderr, "database error: %s\n", errmsg);
237 if (pkg.error == 1) {
238 fprintf(stderr, "unable to allocate memory\n");
244 if (pkglist) jsw_adelete(pkglist);
247 return conf.failed ? 1 : 0;