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
24 char *localdb, *repodir, *pkgdir;
29 int suppressinstalled;
30 int onlylocalinstalled;
36 char *pathcat(char *dir, char *path) {
37 size_t dirlen = 0, pathlen = 0;
40 /* chop off trailing / on dir */
43 while (dirlen && dir[dirlen-1] == '/') {
49 pathlen = strlen(path);
50 while (*path && *path == '/') {
56 cat = malloc(dirlen + pathlen + 2);
58 strncpy(cat, dir, dirlen);
60 strcpy(cat+dirlen+1, path);
65 char *checkfile(char *pkgstr, char *path) {
69 if (!zpm_open(&pkgfile, path)) {
73 pkgid = zpm_findpkg(&pkgfile, pkgstr, NULL);
79 char *checkfileforlib(char *soname, char *path) {
83 if (!zpm_open(&pkgfile, path)) {
87 pkgid = zpm_findlib(&pkgfile, soname, NULL);
89 fprintf(stderr, "sql error: %s\n", pkgfile.errmsg);
96 int find_globs(struct search_ctl *opt) {
106 char *globstr = pathcat(opt->repodir, "*.repo");
107 rv = glob(globstr, globopts, NULL, repos);
110 fprintf(stderr, "glob no mem\n");
113 fprintf(stderr, "glob abort\n");
122 globopts = GLOB_APPEND;
127 char *globstr = pathcat(opt->pkgdir, "*.zpm");
128 rv = glob(globstr, globopts, NULL, repos);
131 fprintf(stderr, "glob no mem\n");
134 fprintf(stderr, "glob abort\n");
149 int find_lib(char *soname, struct search_ctl *opt, struct pkgloc *pkg) {
151 char *installed = 0, *found = 0;
157 installed = zpm_findlib(opt->zpmdb, soname, "status = 'installed'");
159 if (opt->verbose > 1) {
160 fprintf(stderr, "library %s installed via %s\n", soname, installed);
162 /* we're done, the lib is installed */
167 for (i = 0; i < opt->repos.gl_pathc; i++) {
168 if (opt->verbose > 1) {
169 fprintf(stderr, "checking %s for %s\n", opt->repos.gl_pathv[i], soname);
171 found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
173 if (opt->verbose > 1) {
174 fprintf(stderr, "found %s\n", found);
176 rv = zpm_vercmp(found, latest);
180 pkgfile = strdup(opt->repos.gl_pathv[i]);
182 } else if (opt->verbose > 1) {
183 fprintf(stderr, "not found\n");
187 if (latest && pkgfile) {
188 pkg->id = strdup(latest);
196 struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
198 char *installed = 0, *found = 0;
200 struct pkgloc *pkg = 0;
205 installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
208 pkgfile = opt->zpmdb->path;
210 if (!opt->onlylocalinstalled) {
211 found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
213 rv = zpm_vercmp(found, latest);
216 pkgfile = opt->zpmdb->path;
223 zpm_foreach_repo(opt->zpmdb, search_repo, pkg)
228 for (i = 0; i < opt->repos.gl_pathc; i++) {
229 if (!opt->matchallpkgfile
230 && !strstr(opt->repos.gl_pathv[i], pkgstr)
231 && !strstr(opt->repos.gl_pathv[i], ".repo")
235 found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
237 rv = zpm_vercmp(found, latest);
240 pkgfile = strdup(opt->repos.gl_pathv[i]);
245 if (latest && pkgfile) {
246 pkg = malloc(sizeof *pkg);
247 pkg->id = strdup(latest);
251 pkg->installed = !strcmp(latest, installed);
258 char *pkglibsneeded = "\
260 select distinct EN.needed as soname, PF.pkgid\
262 join packagefiles_pkgid PF on PF.hash = EN.file\
265 select distinct EL.soname, PF.pkgid\
266 from elflibraries EL\
267 join packagefiles_pkgid PF on PF.hash = EL.file\
269 select distinct PL.soname, PP.soname is not null as selfsatisfied\
271 left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
275 static int afind_strcmp(const void *a, const void *b) {
279 void checklibs(struct search_ctl *opts,
280 jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
281 char *pkgid = 0, *pkgfile = 0;
284 /* checked_libs is a list of checked sonames
285 * checked is a list of checked packages
286 * needed is list of libs for package
288 jsw_atree_t *needed = 0, *checked, *checked_libs;
294 checked = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
295 checked_libs = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
297 while (jsw_hsize(check) > 0) {
303 pkgid = strdup(jsw_hkey(check));
304 pkgfile = strdup(jsw_hitem(check));
306 if (!pkgid || !pkgfile) {
310 jsw_herase(check, pkgid);
312 if (jsw_afind(checked, pkgid)) {
313 /* already checked this one */
314 /* fprintf(stderr, "already checked %s\n", pkgid); */
318 fprintf(stderr, "checking libs for %s\n", pkgid);
319 /* we do this now so we catch self deps */
320 if (!jsw_ainsert(checked, pkgid)) {
321 fprintf(stderr, "checked insert failed\n");
325 if (!jsw_afind(checked, pkgid)) {
326 /* already checked this one */
327 fprintf(stderr, "checked find failed\n");
331 /* get the libraries needed by this package */
332 if (!zpm_open(&src, pkgfile)) {
333 fprintf(stderr, "can't zpm open %s\n", pkgfile);
337 if (needed) jsw_adelete(needed);
338 needed = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
339 libs = zpm_libraries_needed(&src, pkgid, needed);
348 for (soname = jsw_atfirst(i, needed); soname; soname = jsw_atnext(i)) {
350 struct pkgloc pkginfo;
352 /* if it's in checked_libs, we've already looked at this one */
353 if (jsw_afind(checked_libs, soname)) {
354 if (opts->verbose > 1) {
355 fprintf(stderr, "already checked %s\n", soname);
360 if (opts->verbose > 1) {
361 fprintf(stderr, "checking for %s\n", soname);
365 /* haven't found this soname */
366 jsw_ainsert(checked_libs, soname);
368 found = find_lib(soname, opts, &pkginfo);
370 if (!jsw_hinsert(nfound, soname, pkgid)) {
371 fprintf(stderr,"insert error\n");
377 /* if not alreacy checked, add to check */
378 if (!jsw_afind(checked, pkginfo.file)) {
379 jsw_hinsert(check,pkginfo.id,pkginfo.file);
381 /* shouldn't be in there already */
382 jsw_hinsert(forlibs, pkginfo.id, pkginfo.file);
386 /* otherwise found == 2, so just continue */
393 void print_pkghash(jsw_hash_t *hash, int json, int idonly) {
394 const char *pkgid, *file;
408 fmt = "\"%s\": \"%s\"";
426 if (jsw_hsize(hash) > 0) {
427 for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) {
428 pkgid = jsw_hkey(hash);
429 file = jsw_hitem(hash);
436 printf(fmt, pkgid, file);
439 if (!json) printf("\n");
444 int main(int ac, char *av[]) {
445 int option, argn, rv = 0;
446 int findlibs = 0, json = 0, idonly = 0;
447 struct pkgloc *found;
448 jsw_hash_t *packages, *check, *forlibs, *nolib;
450 struct search_ctl opt = { 0 };
455 opt.localdb = getenv("ZPMDB");
456 if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
457 opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
458 if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
459 opt.repodir = getenv("ZPM_REPO_DIR");
460 if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo";
462 /* -l also find packages needed for libs
464 * -q quiet - suppress output
466 * -d debug - trace each step
469 * ZPMDB - path to localdb, /var/lib/zpm/local.db
470 * ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repo'
471 * ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
472 * '/var/lib/zpm/packages'
473 * ZPM_ROOT_DIR :- prepends to above paths
475 * -M (missing) invert the output and only output missing
477 * packages found (suppress with -P)
478 * package strings not found (suppress with -S)
479 * library dependency packages needed and found (suppress with -N)
480 * library dependency packages already installed (suppress with -H)
481 * library dependency packages in install set (suppress with -I)
482 * library dependency sonames not found (suppress with -L)
484 * exit value is 0 if no missing libs and no bad package strings
485 * exit value is non zero otherwise
489 while ((option = getopt(ac, av, "fljqPRDvp:r:d:MiIOn")) != -1) {
491 case 'l': findlibs = 1; break;
492 case 'j': json = 1; break;
493 case 'q': output = 0; break;
494 /* show installed files */
495 case 'i': opt.matchinstalled = 1; break;
496 case 'I': opt.suppressinstalled = 1; break;
497 /* only find localdb pkgs if installed */
498 case 'O': opt.onlylocalinstalled = 1; break;
499 case 'f': opt.foundonly = 1; break;
500 case 'd': opt.localdb = optarg; break;
501 case 'p': opt.pkgdir = optarg; break;
502 case 'r': opt.repodir = optarg; break;
503 case 'P': opt.pkgdir = 0; break;
504 case 'R': opt.repodir = 0; break;
505 case 'D': opt.localdb = 0; break;
506 /* matchallpkgfile means look at
507 * all .zpm files, not just ones
508 * that have the pkgid string prefix
510 case 'M': opt.matchallpkgfile = 1; break;
511 case 'v': opt.verbose++; break;
512 case 'n': idonly = 1; break;
521 if (zpm_open(&localdb, NULL)) {
522 opt.zpmdb = &localdb;
524 fprintf(stderr, "can't open localdb\n");
529 if (!find_globs(&opt)) {
530 fprintf(stderr, "bad globs\n");
534 if (opt.verbose > 1) {
536 fprintf(stderr, "globs:");
537 for (i = 0; i < opt.repos.gl_pathc; i++) {
538 fprintf(stderr, " %s", opt.repos.gl_pathv[i]);
540 fprintf(stderr, "\n");
543 /* direct packages asked for */
544 packages = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
545 (itemdup_f)strdup,free,free);
547 /* packages we need to check for libs */
548 check = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
549 (itemdup_f)strdup,free,free);
551 /* packages we will also need for library dependences */
552 forlibs = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
553 (itemdup_f)strdup,free,free);
555 /* libraries we didn't find */
556 nolib = jsw_hnew(ac,NULL,afind_strcmp,(keydup_f)strdup,
557 (itemdup_f)strdup,free,free);
559 /* packages asked for that we can't find */
560 nfound = jsw_anew(afind_strcmp, (dup_f)strdup, (rel_f)free);
563 for (arg = argn; arg < ac; arg++) {
564 found = find_package(av[arg], &opt);
565 if (found && (opt.matchinstalled || !found->installed)) {
566 if (found->installed && opt.suppressinstalled) {
569 jsw_hinsert(packages, found->id, found->file);
570 jsw_hinsert(check, found->id, found->file);
575 jsw_ainsert(nfound, av[arg]);
580 checklibs(&opt, check, forlibs, nolib);
581 /* remove from forlibs anything already explicitly
587 if (jsw_hsize(packages)) {
588 print_pkghash(packages, json, idonly);
590 if (jsw_hsize(forlibs)) {
591 print_pkghash(forlibs, json, idonly);
595 if (jsw_hsize(nolib) > 0) {
596 for (jsw_hreset(nolib); jsw_hitem(nolib); jsw_hnext(nolib)) {
597 const char *pkgid, *file;
598 pkgid = jsw_hkey(nolib);
599 file = jsw_hitem(nolib);
600 fprintf(stderr, "no lib found %s:%s\n", pkgid, file);
605 if (jsw_asize(nfound) > 0 && !opt.foundonly) {
610 for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
611 fprintf(stderr, "%s: not found\n", pkgstr);