1 #define _POSIX_C_SOURCE 200809L
9 #include "lib/jsw/jsw_hlib.h"
10 #include "lib/jsw/jsw_atree.h"
13 * find packages, and lib deps
23 char *localdb, *repodir, *pkgdir;
30 char *pathcat(char *dir, char *path) {
31 size_t dirlen = 0, pathlen = 0;
34 /* chop off trailing / on dir */
37 while (dirlen && dir[dirlen-1] == '/') {
43 pathlen = strlen(path);
44 while (*path && *path == '/') {
50 cat = malloc(dirlen + pathlen + 2);
52 strncpy(cat, dir, dirlen);
54 strcpy(cat+dirlen+1, path);
59 char *checkfile(char *pkgstr, char *path) {
63 if (!zpm_open(&pkgfile, path)) {
67 pkgid = zpm_findpkg(&pkgfile, pkgstr, NULL);
73 char *checkfileforlib(char *soname, char *path) {
77 if (!zpm_open(&pkgfile, path)) {
81 pkgid = zpm_findlib(&pkgfile, soname, NULL);
83 fprintf(stderr, "sql error: %s\n", pkgfile.errmsg);
90 int find_globs(struct search_ctl *opt) {
100 char *globstr = pathcat(opt->repodir, "*.repo");
101 rv = glob(globstr, globopts, NULL, repos);
104 fprintf(stderr, "glob no mem\n");
107 fprintf(stderr, "glob abort\n");
116 globopts = GLOB_APPEND;
121 char *globstr = pathcat(opt->pkgdir, "*.zpm");
122 rv = glob(globstr, globopts, NULL, repos);
125 fprintf(stderr, "glob no mem\n");
128 fprintf(stderr, "glob abort\n");
143 int find_lib(char *soname, struct search_ctl *opt, struct pkgloc *pkg) {
145 char *installed = 0, *found = 0;
151 installed = zpm_findlib(opt->zpmdb, soname, "status = 'installed'");
153 if (opt->verbose > 1) {
154 fprintf(stderr, "library %s installed via %s\n", soname, installed);
156 /* we're done, the lib is installed */
161 for (i = 0; i < opt->repos.gl_pathc; i++) {
162 if (opt->verbose > 1) {
163 fprintf(stderr, "checking %s for %s\n", opt->repos.gl_pathv[i], soname);
165 found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
167 if (opt->verbose > 1) {
168 fprintf(stderr, "found %s\n", found);
170 rv = zpm_vercmp(found, latest);
174 pkgfile = strdup(opt->repos.gl_pathv[i]);
176 } else if (opt->verbose > 1) {
177 fprintf(stderr, "not found\n");
181 if (latest && pkgfile) {
182 pkg->id = strdup(latest);
190 struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
192 char *installed = 0, *found = 0;
194 struct pkgloc *pkg = 0;
199 installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
203 found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
205 rv = zpm_vercmp(found, latest);
208 pkgfile = opt->zpmdb->path;
213 for (i = 0; i < opt->repos.gl_pathc; i++) {
214 if (!opt->matchallpkgfile
215 && !strstr(opt->repos.gl_pathv[i], pkgstr)
216 && !strstr(opt->repos.gl_pathv[i], ".repo")
220 found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
222 rv = zpm_vercmp(found, latest);
225 pkgfile = strdup(opt->repos.gl_pathv[i]);
230 if (latest && pkgfile) {
231 pkg = malloc(sizeof *pkg);
232 pkg->id = strdup(latest);
239 char *pkglibsneeded = "\
241 select distinct EN.needed as soname, PF.pkgid\
243 join packagefiles_pkgid PF on PF.hash = EN.file\
246 select distinct EL.soname, PF.pkgid\
247 from elflibraries EL\
248 join packagefiles_pkgid PF on PF.hash = EL.file\
250 select distinct PL.soname, PP.soname is not null as selfsatisfied\
252 left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
256 void checklibs(struct search_ctl *opts,
257 jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
258 char *pkgid = 0, *pkgfile = 0;
261 /* checked_libs is a list of checked sonames
262 * checked is a list of checked packages
263 * needed is list of libs for package
265 jsw_atree_t *needed = 0, *checked, *checked_libs;
271 checked = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
272 checked_libs = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
274 while (jsw_hsize(check) > 0) {
280 pkgid = strdup(jsw_hkey(check));
281 pkgfile = strdup(jsw_hitem(check));
283 if (!pkgid || !pkgfile) {
287 jsw_herase(check, pkgid);
289 if (jsw_afind(checked, pkgid)) {
290 /* already checked this one */
294 /* we do this now so we catch self deps */
295 jsw_ainsert(checked, pkgid);
297 /* get the libraries needed by this package */
298 if (!zpm_open(&src, pkgfile)) {
299 fprintf(stderr, "can't zpm open %s\n", pkgfile);
303 if (needed) jsw_adelete(needed);
304 needed = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
305 libs = zpm_libraries_needed(&src, pkgid, needed);
314 for (soname = jsw_atfirst(i, needed); soname; soname = jsw_atnext(i)) {
316 struct pkgloc pkginfo;
318 if (opts->verbose > 1) {
319 fprintf(stderr, "checking for %s\n", soname);
321 /* if it's in checked_libs, we've already looked at this one */
322 if (jsw_afind(checked_libs, soname)) {
323 if (opts->verbose > 1) {
324 fprintf(stderr, "already checked for %s\n", soname);
329 /* haven't found this soname */
330 jsw_ainsert(checked_libs, soname);
332 found = find_lib(soname, opts, &pkginfo);
334 if (!jsw_hinsert(nfound, soname, pkgid)) {
335 fprintf(stderr,"insert error\n");
341 /* if not alreacy checked, add to check */
342 if (!jsw_afind(checked, pkginfo.file)) {
343 jsw_hinsert(check,pkginfo.id,pkginfo.file);
345 /* shouldn't be in there already */
346 jsw_hinsert(forlibs, pkginfo.id, pkginfo.file);
350 /* otherwise found == 2, so just continue */
357 void print_pkghash(jsw_hash_t *hash, int json) {
358 const char *pkgid, *file;
363 fmt = json ? "\"%s\": \"%s\"" : "%s:%s";
364 sep = json ? ", " : "\n";
370 if (jsw_hsize(hash) > 0) {
371 for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) {
372 pkgid = jsw_hkey(hash);
373 file = jsw_hitem(hash);
377 printf(fmt, pkgid, file);
380 if (json) printf(" }");
384 int main(int ac, char *av[]) {
386 int findlibs = 0, json = 0;
387 struct pkgloc *found;
388 jsw_hash_t *packages, *check, *forlibs, *nolib;
390 struct search_ctl opt = { 0 };
393 opt.localdb = getenv("ZPMDB");
394 if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
395 opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
396 if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
397 opt.repodir = getenv("ZPM_REPO_DIR");
398 if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo/";
400 /* -l also find packages needed for libs
402 * -q quiet - suppress output
404 * -d debug - trace each step
407 * ZPMDB - path to localdb, /var/lib/zpm/local.db
408 * ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repos'
409 * ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
410 * '/var/lib/zpm/packages'
411 * ZPM_ROOT_DIR :- prepends to above paths
413 * -M (missing) invert the output and only output missing
415 * packages found (suppress with -P)
416 * package strings not found (suppress with -S)
417 * library dependency packages needed and found (suppress with -N)
418 * library dependency packages already installed (suppress with -H)
419 * library dependency packages in install set (suppress with -I)
420 * library dependency sonames not found (suppress with -L)
422 * exit value is 0 if no missing libs and no bad package strings
423 * exit value is non zero otherwise
427 while ((option = getopt(ac, av, "ljqPRDvp:r:d:M")) != -1) {
429 case 'l': findlibs = 1; break;
430 case 'j': json = 1; break;
431 case 'q': output = 0;
432 case 'd': opt.localdb = optarg; break;
433 case 'p': opt.pkgdir = optarg; break;
434 case 'r': opt.repodir = optarg; break;
435 case 'P': opt.pkgdir = 0; break;
436 case 'R': opt.repodir = 0; break;
437 case 'D': opt.localdb = 0; break;
438 case 'M': opt.matchallpkgfile = 1; break;
439 case 'v': opt.verbose++; break;
448 if (zpm_open(&localdb, NULL)) {
449 opt.zpmdb = &localdb;
451 fprintf(stderr, "can't open localdb\n");
457 if (!find_globs(&opt)) {
458 fprintf(stderr, "bad globs\n");
462 if (opt.verbose > 1) {
464 fprintf(stderr, "globs:");
465 for (i = 0; i < opt.repos.gl_pathc; i++) {
466 fprintf(stderr, " %s", opt.repos.gl_pathv[i]);
468 fprintf(stderr, "\n");
471 packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
472 (itemdup_f)strdup,free,free);
473 check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
474 (itemdup_f)strdup,free,free);
475 forlibs = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
476 (itemdup_f)strdup,free,free);
477 nolib = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
478 (itemdup_f)strdup,free,free);
479 nfound = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
482 for (arg = argn; arg < ac; arg++) {
483 found = find_package(av[arg], &opt);
485 jsw_hinsert(packages, found->id, found->file);
486 jsw_hinsert(check, found->id, found->file);
491 jsw_ainsert(nfound, av[arg]);
497 checklibs(&opt, check, forlibs, nolib);
501 print_pkghash(packages, json);
502 if (jsw_hsize(forlibs)) {
503 print_pkghash(forlibs, json);
507 if (jsw_hsize(nolib) > 0) {
508 for (jsw_hreset(nolib); jsw_hitem(nolib); jsw_hnext(nolib)) {
509 const char *pkgid, *file;
510 pkgid = jsw_hkey(nolib);
511 file = jsw_hitem(nolib);
512 fprintf(stderr, "no lib found %s:%s\n", pkgid, file);
516 if (jsw_asize(nfound) > 0) {
521 for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
522 fprintf(stderr, "%s: not found\n", pkgstr);
526 return jsw_asize(nfound) > 0 || jsw_hsize(nolib) > 0 ? 1 : 0;