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;
34 char *pathcat(char *dir, char *path) {
35 size_t dirlen = 0, pathlen = 0;
38 /* chop off trailing / on dir */
41 while (dirlen && dir[dirlen-1] == '/') {
47 pathlen = strlen(path);
48 while (*path && *path == '/') {
54 cat = malloc(dirlen + pathlen + 2);
56 strncpy(cat, dir, dirlen);
58 strcpy(cat+dirlen+1, path);
63 char *checkfile(char *pkgstr, char *path) {
67 if (!zpm_open(&pkgfile, path)) {
71 pkgid = zpm_findpkg(&pkgfile, pkgstr, NULL);
77 char *checkfileforlib(char *soname, char *path) {
81 if (!zpm_open(&pkgfile, path)) {
85 pkgid = zpm_findlib(&pkgfile, soname, NULL);
87 fprintf(stderr, "sql error: %s\n", pkgfile.errmsg);
94 int find_globs(struct search_ctl *opt) {
104 char *globstr = pathcat(opt->repodir, "*.repo");
105 rv = glob(globstr, globopts, NULL, repos);
108 fprintf(stderr, "glob no mem\n");
111 fprintf(stderr, "glob abort\n");
120 globopts = GLOB_APPEND;
125 char *globstr = pathcat(opt->pkgdir, "*.zpm");
126 rv = glob(globstr, globopts, NULL, repos);
129 fprintf(stderr, "glob no mem\n");
132 fprintf(stderr, "glob abort\n");
147 int find_lib(char *soname, struct search_ctl *opt, struct pkgloc *pkg) {
149 char *installed = 0, *found = 0;
155 installed = zpm_findlib(opt->zpmdb, soname, "status = 'installed'");
157 if (opt->verbose > 1) {
158 fprintf(stderr, "library %s installed via %s\n", soname, installed);
160 /* we're done, the lib is installed */
165 for (i = 0; i < opt->repos.gl_pathc; i++) {
166 if (opt->verbose > 1) {
167 fprintf(stderr, "checking %s for %s\n", opt->repos.gl_pathv[i], soname);
169 found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
171 if (opt->verbose > 1) {
172 fprintf(stderr, "found %s\n", found);
174 rv = zpm_vercmp(found, latest);
178 pkgfile = strdup(opt->repos.gl_pathv[i]);
180 } else if (opt->verbose > 1) {
181 fprintf(stderr, "not found\n");
185 if (latest && pkgfile) {
186 pkg->id = strdup(latest);
194 struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
196 char *installed = 0, *found = 0;
198 struct pkgloc *pkg = 0;
203 installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
206 pkgfile = opt->zpmdb->path;
208 found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
210 rv = zpm_vercmp(found, latest);
213 pkgfile = opt->zpmdb->path;
219 zpm_foreach_repo(opt->zpmdb, search_repo, pkg)
224 for (i = 0; i < opt->repos.gl_pathc; i++) {
225 if (!opt->matchallpkgfile
226 && !strstr(opt->repos.gl_pathv[i], pkgstr)
227 && !strstr(opt->repos.gl_pathv[i], ".repo")
231 found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
233 rv = zpm_vercmp(found, latest);
236 pkgfile = strdup(opt->repos.gl_pathv[i]);
241 if (latest && pkgfile) {
242 pkg = malloc(sizeof *pkg);
243 pkg->id = strdup(latest);
247 pkg->installed = !strcmp(latest, installed);
254 char *pkglibsneeded = "\
256 select distinct EN.needed as soname, PF.pkgid\
258 join packagefiles_pkgid PF on PF.hash = EN.file\
261 select distinct EL.soname, PF.pkgid\
262 from elflibraries EL\
263 join packagefiles_pkgid PF on PF.hash = EL.file\
265 select distinct PL.soname, PP.soname is not null as selfsatisfied\
267 left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
271 void checklibs(struct search_ctl *opts,
272 jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
273 char *pkgid = 0, *pkgfile = 0;
276 /* checked_libs is a list of checked sonames
277 * checked is a list of checked packages
278 * needed is list of libs for package
280 jsw_atree_t *needed = 0, *checked, *checked_libs;
286 checked = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
287 checked_libs = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
289 while (jsw_hsize(check) > 0) {
295 pkgid = strdup(jsw_hkey(check));
296 pkgfile = strdup(jsw_hitem(check));
298 if (!pkgid || !pkgfile) {
302 jsw_herase(check, pkgid);
304 if (jsw_afind(checked, pkgid)) {
305 /* already checked this one */
309 /* we do this now so we catch self deps */
310 jsw_ainsert(checked, pkgid);
312 /* get the libraries needed by this package */
313 if (!zpm_open(&src, pkgfile)) {
314 fprintf(stderr, "can't zpm open %s\n", pkgfile);
318 if (needed) jsw_adelete(needed);
319 needed = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
320 libs = zpm_libraries_needed(&src, pkgid, needed);
329 for (soname = jsw_atfirst(i, needed); soname; soname = jsw_atnext(i)) {
331 struct pkgloc pkginfo;
333 if (opts->verbose > 1) {
334 fprintf(stderr, "checking for %s\n", soname);
336 /* if it's in checked_libs, we've already looked at this one */
337 if (jsw_afind(checked_libs, soname)) {
338 if (opts->verbose > 1) {
339 fprintf(stderr, "already checked for %s\n", soname);
344 /* haven't found this soname */
345 jsw_ainsert(checked_libs, soname);
347 found = find_lib(soname, opts, &pkginfo);
349 if (!jsw_hinsert(nfound, soname, pkgid)) {
350 fprintf(stderr,"insert error\n");
356 /* if not alreacy checked, add to check */
357 if (!jsw_afind(checked, pkginfo.file)) {
358 jsw_hinsert(check,pkginfo.id,pkginfo.file);
360 /* shouldn't be in there already */
361 jsw_hinsert(forlibs, pkginfo.id, pkginfo.file);
365 /* otherwise found == 2, so just continue */
372 void print_pkghash(jsw_hash_t *hash, int json) {
373 const char *pkgid, *file;
378 fmt = json ? "\"%s\": \"%s\"" : "%s:%s";
379 sep = json ? ", " : "\n";
385 if (jsw_hsize(hash) > 0) {
386 for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) {
387 pkgid = jsw_hkey(hash);
388 file = jsw_hitem(hash);
392 printf(fmt, pkgid, file);
395 if (json) printf(" }");
399 int main(int ac, char *av[]) {
401 int findlibs = 0, json = 0;
402 struct pkgloc *found;
403 jsw_hash_t *packages, *check, *forlibs, *nolib;
405 struct search_ctl opt = { 0 };
410 opt.localdb = getenv("ZPMDB");
411 if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
412 opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
413 if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
414 opt.repodir = getenv("ZPM_REPO_DIR");
415 if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo";
418 /* -l also find packages needed for libs
420 * -q quiet - suppress output
422 * -d debug - trace each step
425 * ZPMDB - path to localdb, /var/lib/zpm/local.db
426 * ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repo'
427 * ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
428 * '/var/lib/zpm/packages'
429 * ZPM_ROOT_DIR :- prepends to above paths
431 * -M (missing) invert the output and only output missing
433 * packages found (suppress with -P)
434 * package strings not found (suppress with -S)
435 * library dependency packages needed and found (suppress with -N)
436 * library dependency packages already installed (suppress with -H)
437 * library dependency packages in install set (suppress with -I)
438 * library dependency sonames not found (suppress with -L)
440 * exit value is 0 if no missing libs and no bad package strings
441 * exit value is non zero otherwise
445 while ((option = getopt(ac, av, "ljqPRDvp:r:d:MiI")) != -1) {
447 case 'l': findlibs = 1; break;
448 case 'j': json = 1; break;
449 case 'q': output = 0; break;
450 /* show installed files */
451 case 'i': opt.matchinstalled = 1; break;
452 case 'I': opt.suppressinstalled = 1; break;
453 case 'd': opt.localdb = optarg; break;
454 case 'p': opt.pkgdir = optarg; break;
455 case 'r': opt.repodir = optarg; break;
456 case 'P': opt.pkgdir = 0; break;
457 case 'R': opt.repodir = 0; break;
458 case 'D': opt.localdb = 0; break;
459 /* matchallpkgfile means look at
460 * all .zpm files, not just ones
461 * that have the pkgid string prefix
463 case 'M': opt.matchallpkgfile = 1; break;
464 case 'v': opt.verbose++; break;
473 if (zpm_open(&localdb, NULL)) {
474 opt.zpmdb = &localdb;
476 fprintf(stderr, "can't open localdb\n");
481 if (!find_globs(&opt)) {
482 fprintf(stderr, "bad globs\n");
486 if (opt.verbose > 1) {
488 fprintf(stderr, "globs:");
489 for (i = 0; i < opt.repos.gl_pathc; i++) {
490 fprintf(stderr, " %s", opt.repos.gl_pathv[i]);
492 fprintf(stderr, "\n");
495 packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
496 (itemdup_f)strdup,free,free);
497 check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
498 (itemdup_f)strdup,free,free);
499 forlibs = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
500 (itemdup_f)strdup,free,free);
501 nolib = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
502 (itemdup_f)strdup,free,free);
503 nfound = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
506 for (arg = argn; arg < ac; arg++) {
507 found = find_package(av[arg], &opt);
508 if (found && (opt.matchinstalled || !found->installed)) {
509 if (found->installed && opt.suppressinstalled) {
512 jsw_hinsert(packages, found->id, found->file);
513 jsw_hinsert(check, found->id, found->file);
518 jsw_ainsert(nfound, av[arg]);
524 checklibs(&opt, check, forlibs, nolib);
528 if (jsw_hsize(packages)) {
529 print_pkghash(packages, json);
531 if (jsw_hsize(forlibs)) {
532 print_pkghash(forlibs, json);
536 if (jsw_hsize(nolib) > 0) {
537 for (jsw_hreset(nolib); jsw_hitem(nolib); jsw_hnext(nolib)) {
538 const char *pkgid, *file;
539 pkgid = jsw_hkey(nolib);
540 file = jsw_hitem(nolib);
541 fprintf(stderr, "no lib found %s:%s\n", pkgid, file);
545 if (jsw_asize(nfound) > 0) {
550 for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
551 fprintf(stderr, "%s: not found\n", pkgstr);
555 return jsw_asize(nfound) > 0 || jsw_hsize(nolib) > 0 ? 1 : 0;