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;
29 char *pathcat(char *dir, char *path) {
30 size_t dirlen = 0, pathlen = 0;
33 /* chop off trailing / on dir */
36 while (dirlen && dir[dirlen-1] == '/') {
42 pathlen = strlen(path);
43 while (*path && *path == '/') {
49 cat = malloc(dirlen + pathlen + 2);
51 strncpy(cat, dir, dirlen);
53 strcpy(cat+dirlen+1, path);
58 char *checkfile(char *pkgstr, char *path) {
62 if (!zpm_open(&pkgfile, path)) {
66 pkgid = zpm_findpkg(&pkgfile, pkgstr, "hash is not null");
72 char *checkfileforlib(char *soname, char *path) {
76 if (!zpm_open(&pkgfile, path)) {
80 pkgid = zpm_findlib(&pkgfile, soname, "hash is not null");
82 fprintf(stderr, "sql error: %s\n", pkgfile.errmsg);
89 int find_globs(struct search_ctl *opt) {
99 char *globstr = pathcat(opt->repodir, "*.repo");
100 rv = glob(globstr, globopts, NULL, repos);
103 fprintf(stderr, "glob no mem\n");
106 fprintf(stderr, "glob abort\n");
115 globopts = GLOB_APPEND;
120 char *globstr = pathcat(opt->pkgdir, "*.zpm");
121 rv = glob(globstr, globopts, NULL, repos);
124 fprintf(stderr, "glob no mem\n");
127 fprintf(stderr, "glob abort\n");
136 globopts = GLOB_APPEND;
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 /* we're done, the lib is installed */
158 for (i = 0; i < opt->repos.gl_pathc; i++) {
159 found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
161 rv = zpm_vercmp(found, latest);
165 pkgfile = strdup(opt->repos.gl_pathv[i]);
170 if (latest && pkgfile) {
171 pkg->id = strdup(latest);
179 struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
181 char *installed = 0, *found = 0;
183 struct pkgloc *pkg = 0;
188 installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
192 found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
194 rv = zpm_vercmp(found, latest);
197 pkgfile = opt->zpmdb->path;
202 for (i = 0; i < opt->repos.gl_pathc; i++) {
203 if (!opt->matchallpkgfile
204 && !strstr(opt->repos.gl_pathv[i], pkgstr)
205 && !strstr(opt->repos.gl_pathv[i], ".repo")
209 found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
211 rv = zpm_vercmp(found, latest);
214 pkgfile = strdup(opt->repos.gl_pathv[i]);
219 if (latest && pkgfile) {
220 pkg = malloc(sizeof *pkg);
221 pkg->id = strdup(latest);
228 char *pkglibsneeded = "\
230 select distinct EN.needed as soname, PF.pkgid\
232 join packagefiles_pkgid PF on PF.hash = EN.file\
235 select distinct EL.soname, PF.pkgid\
236 from elflibraries EL\
237 join packagefiles_pkgid PF on PF.hash = EL.file\
239 select distinct PL.soname, PP.soname is not null as selfsatisfied\
241 left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
245 void checklibs(struct search_ctl *opts,
246 jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
247 char *pkgid = 0, *pkgfile = 0;
250 /* checked_libs is a list of checked sonames
251 * checked is a list of checked packages
252 * needed is list of libs for package
254 jsw_atree_t *needed = 0, *checked, *checked_libs;
260 checked = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
261 checked_libs = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
263 while (jsw_hsize(check) > 0) {
269 pkgid = strdup(jsw_hkey(check));
270 pkgfile = strdup(jsw_hitem(check));
272 if (!pkgid || !pkgfile) {
276 jsw_herase(check, pkgid);
278 if (jsw_afind(checked, pkgid)) {
279 /* already checked this one */
283 /* we do this now so we catch self deps */
284 jsw_ainsert(checked, pkgid);
286 /* get the libraries needed by this package */
287 if (!zpm_open(&src, pkgfile)) {
288 fprintf(stderr, "can't zpm open %s\n", pkgfile);
292 if (needed) jsw_adelete(needed);
293 needed = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
294 libs = zpm_libraries_needed(&src, pkgid, needed);
303 for (soname = jsw_atfirst(i, needed); soname; soname = jsw_atnext(i)) {
305 struct pkgloc pkginfo;
307 /* if it's in checked_libs, we've already looked at this one */
308 if (jsw_afind(checked_libs, soname)) {
312 /* haven't found this soname */
313 jsw_ainsert(checked_libs, soname);
315 found = find_lib(soname, opts, &pkginfo);
317 if (!jsw_hinsert(nfound, soname, pkgid)) {
318 fprintf(stderr,"insert error\n");
324 /* if not alreacy checked, add to check */
325 if (!jsw_afind(checked, pkginfo.file)) {
326 jsw_hinsert(check,pkginfo.id,pkginfo.file);
328 /* shouldn't be in there already */
329 jsw_hinsert(forlibs, pkginfo.id, pkginfo.file);
331 /* otherwise found == 2, so just continue */
342 void print_pkghash(jsw_hash_t *hash, int json) {
343 const char *pkgid, *file;
348 fmt = json ? "\"%s\": \"%s\"" : "%s:%s";
349 sep = json ? ", " : "\n";
355 if (jsw_hsize(hash) > 0) {
356 for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) {
357 pkgid = jsw_hkey(hash);
358 file = jsw_hitem(hash);
362 printf(fmt, pkgid, file);
365 if (json) printf(" }");
369 int main(int ac, char *av[]) {
371 int findlibs = 0, json = 0;
372 struct pkgloc *found;
373 jsw_hash_t *packages, *check, *forlibs, *nolib;
375 struct search_ctl opt = { 0 };
378 opt.localdb = getenv("ZPMDB");
379 if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
380 opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
381 if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
382 opt.repodir = getenv("ZPM_REPO_DIR");
383 if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo/";
385 /* -l also find packages needed for libs
387 * -q quiet - suppress output
389 * -d debug - trace each step
392 * ZPMDB - path to localdb, /var/lib/zpm/local.db
393 * ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repos'
394 * ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
395 * '/var/lib/zpm/packages'
396 * ZPM_ROOT_DIR :- prepends to above paths
398 * -M (missing) invert the output and only output missing
400 * packages found (suppress with -P)
401 * package strings not found (suppress with -S)
402 * library dependency packages needed and found (suppress with -N)
403 * library dependency packages already installed (suppress with -H)
404 * library dependency packages in install set (suppress with -I)
405 * library dependency sonames not found (suppress with -L)
407 * exit value is 0 if no missing libs and no bad package strings
408 * exit value is non zero otherwise
412 while ((option = getopt(ac, av, "ljqPRDp:r:d:M")) != -1) {
414 case 'l': findlibs = 1; break;
415 case 'j': json = 1; break;
416 case 'q': output = 0;
417 case 'd': opt.localdb = optarg; break;
418 case 'p': opt.pkgdir = optarg; break;
419 case 'r': opt.repodir = optarg; break;
420 case 'P': opt.pkgdir = 0; break;
421 case 'R': opt.repodir = 0; break;
422 case 'D': opt.localdb = 0; break;
423 case 'M': opt.matchallpkgfile = 1; break;
432 if (zpm_open(&localdb, NULL)) {
433 opt.zpmdb = &localdb;
435 fprintf(stderr, "can't open localdb\n");
441 if (!find_globs(&opt)) {
442 fprintf(stderr, "bad globs\n");
446 packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
447 (itemdup_f)strdup,free,free);
448 check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
449 (itemdup_f)strdup,free,free);
450 forlibs = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
451 (itemdup_f)strdup,free,free);
452 nolib = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
453 (itemdup_f)strdup,free,free);
454 nfound = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
457 for (arg = argn; arg < ac; arg++) {
458 found = find_package(av[arg], &opt);
460 jsw_hinsert(packages, found->id, found->file);
461 jsw_hinsert(check, found->id, found->file);
466 jsw_ainsert(nfound, av[arg]);
472 checklibs(&opt, check, forlibs, nolib);
476 print_pkghash(packages, json);
477 if (jsw_hsize(forlibs)) {
478 print_pkghash(forlibs, json);
482 if (jsw_hsize(nolib) > 0) {
483 for (jsw_hreset(nolib); jsw_hitem(nolib); jsw_hnext(nolib)) {
484 const char *pkgid, *file;
485 pkgid = jsw_hkey(nolib);
486 file = jsw_hitem(nolib);
487 fprintf(stderr, "no lib found %s:%s\n", pkgid, file);
491 if (jsw_asize(nfound) > 0) {
496 for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
497 fprintf(stderr, "%s: not found\n", pkgstr);
501 return jsw_asize(nfound) > 0 || jsw_hsize(nolib) > 0 ? 1 : 0;