]> pd.if.org Git - zpackage/blob - zpm-search.c
add search options for installs
[zpackage] / zpm-search.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include <glob.h>
7
8 #include "zpm.h"
9 #include "lib/jsw/jsw_hlib.h"
10 #include "lib/jsw/jsw_atree.h"
11
12 /*
13  * find packages, and lib deps
14  */
15
16 struct pkgloc {
17         char *id;
18         char *file;
19         int info;
20         int installed;
21 };
22
23 struct search_ctl {
24         char *localdb, *repodir, *pkgdir;
25         struct zpm *zpmdb;
26         glob_t repos;
27         int matchallpkgfile;
28         int matchinstalled;
29         int suppressinstalled;
30         int onlylocalinstalled;
31         int verbose;
32         int dbrepos;
33 };
34
35 char *pathcat(char *dir, char *path) {
36         size_t dirlen = 0, pathlen = 0;
37         char *cat;
38
39         /* chop off trailing / on dir */
40         if (dir) {
41                 dirlen = strlen(dir);
42                 while (dirlen && dir[dirlen-1] == '/') {
43                         dirlen--;
44                 }
45         }
46
47         if (path) {
48                 pathlen = strlen(path);
49                 while (*path && *path == '/') {
50                         path++;
51                         pathlen--;
52                 }
53         }
54
55         cat = malloc(dirlen + pathlen + 2);
56         if (cat) {
57                 strncpy(cat, dir, dirlen);
58                 cat[dirlen] = '/';
59                 strcpy(cat+dirlen+1, path);
60         }
61         return cat;
62 }
63
64 char *checkfile(char *pkgstr, char *path) {
65         struct zpm pkgfile;
66         char *pkgid = 0;
67
68         if (!zpm_open(&pkgfile, path)) {
69                 return NULL;
70         }
71         
72         pkgid = zpm_findpkg(&pkgfile, pkgstr, NULL);
73         zpm_close(&pkgfile);
74
75         return pkgid;
76 }
77
78 char *checkfileforlib(char *soname, char *path) {
79         struct zpm pkgfile;
80         char *pkgid = 0;
81
82         if (!zpm_open(&pkgfile, path)) {
83                 return NULL;
84         }
85         
86         pkgid = zpm_findlib(&pkgfile, soname, NULL);
87         if (pkgfile.error) {
88                 fprintf(stderr, "sql error: %s\n", pkgfile.errmsg);
89         }
90         zpm_close(&pkgfile);
91
92         return pkgid;
93 }
94
95 int find_globs(struct search_ctl *opt) {
96         glob_t *repos;
97         int rv;
98
99         repos = &opt->repos;
100         repos->gl_offs = 0;
101
102         int globopts = 0;
103
104         if (opt->repodir) {
105                 char *globstr = pathcat(opt->repodir, "*.repo");
106                 rv = glob(globstr, globopts, NULL, repos);
107                 switch (rv) {
108                         case GLOB_NOSPACE:
109                                 fprintf(stderr, "glob no mem\n");
110                                 return 0;
111                         case GLOB_ABORTED:
112                                 fprintf(stderr, "glob abort\n");
113                                 return 0;
114                         case GLOB_NOMATCH:
115                                 break;
116                         case 0:
117                                 break;
118                         default:
119                                 break;
120                 }
121                 globopts = GLOB_APPEND;
122                 free(globstr);
123         }
124
125         if (opt->pkgdir) {
126                 char *globstr = pathcat(opt->pkgdir, "*.zpm");
127                 rv = glob(globstr, globopts, NULL, repos);
128                 switch (rv) {
129                         case GLOB_NOSPACE:
130                                 fprintf(stderr, "glob no mem\n");
131                                 return 0;
132                         case GLOB_ABORTED:
133                                 fprintf(stderr, "glob abort\n");
134                                 return 0;
135                         case GLOB_NOMATCH:
136                                 break;
137                         case 0:
138                                 break;
139                         default:
140                                 break;
141                 }
142                 free(globstr);
143         }
144
145         return 1;
146 }
147
148 int find_lib(char *soname, struct search_ctl *opt, struct pkgloc *pkg) {
149         char *latest = 0;
150         char *installed = 0, *found = 0;
151         char *pkgfile = 0;
152         int rv;
153         unsigned int i;
154
155         if (opt->localdb) {
156                 installed = zpm_findlib(opt->zpmdb, soname, "status = 'installed'");
157                 if (installed) {
158                         if (opt->verbose > 1) {
159                                 fprintf(stderr, "library %s installed via %s\n", soname, installed);
160                         }
161                         /* we're done, the lib is installed */
162                         return 2;
163                 }
164         }
165
166         for (i = 0; i < opt->repos.gl_pathc; i++) {
167                 if (opt->verbose > 1) {
168                         fprintf(stderr, "checking %s for %s\n", opt->repos.gl_pathv[i], soname);
169                 }
170                 found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
171                 if (found) {
172                         if (opt->verbose > 1) {
173                                 fprintf(stderr, "found %s\n", found);
174                         }
175                         rv = zpm_vercmp(found, latest);
176                         if (rv == 1) {
177                                 latest = found;
178                                 free(pkgfile);
179                                 pkgfile = strdup(opt->repos.gl_pathv[i]);
180                         }
181                 } else if (opt->verbose > 1) {
182                         fprintf(stderr, "not found\n");
183                 }
184         }
185
186         if (latest && pkgfile) {
187                 pkg->id = strdup(latest);
188                 pkg->file = pkgfile;
189                 return 1;
190         }
191
192         return 0;
193 }
194
195 struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
196         char *latest = 0;
197         char *installed = 0, *found = 0;
198         char *pkgfile = 0;
199         struct pkgloc *pkg = 0;
200         int rv;
201         unsigned int i;
202
203         if (opt->localdb) {
204                 installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
205                 if (installed) {
206                         latest = installed;
207                         pkgfile = opt->zpmdb->path;
208                 }
209                 if (!opt->onlylocalinstalled) {
210                         found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
211                         if (found) {
212                                 rv = zpm_vercmp(found, latest);
213                                 if (rv == 1) {
214                                         latest = found;
215                                         pkgfile = opt->zpmdb->path;
216                                 }
217                         }
218                 }
219
220 #if 0
221                 if (opt->dbrepos) {
222                         zpm_foreach_repo(opt->zpmdb, search_repo, pkg)
223                 }
224 #endif
225         }
226
227         for (i = 0; i < opt->repos.gl_pathc; i++) {
228                 if (!opt->matchallpkgfile
229                                 && !strstr(opt->repos.gl_pathv[i], pkgstr)
230                                 && !strstr(opt->repos.gl_pathv[i], ".repo")
231                                 ) {
232                         continue;
233                 }
234                 found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
235                 if (found) {
236                         rv = zpm_vercmp(found, latest);
237                         if (rv == 1) {
238                                 latest = found;
239                                 pkgfile = strdup(opt->repos.gl_pathv[i]);
240                         }
241                 }
242         }
243
244         if (latest && pkgfile) {
245                 pkg = malloc(sizeof *pkg);
246                 pkg->id = strdup(latest);
247                 pkg->file = pkgfile;
248                 pkg->installed = 0;
249                 if (installed) {
250                        pkg->installed = !strcmp(latest, installed);
251                 }
252         }
253
254         return pkg;
255 }
256
257 char *pkglibsneeded = "\
258 with pkglibs as (\
259                 select distinct EN.needed as soname, PF.pkgid\
260                 from elfneeded EN\
261                 join packagefiles_pkgid PF on PF.hash = EN.file\
262                 ),\
263      pkgprovides as (\
264                      select distinct EL.soname, PF.pkgid\
265                      from elflibraries EL\
266                      join packagefiles_pkgid PF on PF.hash = EL.file\
267                     )\
268      select distinct PL.soname, PP.soname is not null as selfsatisfied\
269      from pkglibs PL\
270      left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
271      where PL.pkgid = %Q\
272      ";
273
274 void checklibs(struct search_ctl *opts,
275                 jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
276         char *pkgid = 0, *pkgfile = 0;
277         struct zpm src;
278
279         /* checked_libs is a list of checked sonames
280          * checked is a list of checked packages
281          * needed is list of libs for package
282          */
283         jsw_atree_t *needed = 0, *checked, *checked_libs;
284
285         int libs;
286         jsw_atrav_t *i;
287         char *soname;
288         
289         checked = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
290         checked_libs = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
291
292         while (jsw_hsize(check) > 0) {
293                 free(pkgid);
294                 free(pkgfile);
295
296                 jsw_hreset(check);
297
298                 pkgid = strdup(jsw_hkey(check));
299                 pkgfile = strdup(jsw_hitem(check));
300
301                 if (!pkgid || !pkgfile) {
302                         break;
303                 }
304
305                 jsw_herase(check, pkgid);
306
307                 if (jsw_afind(checked, pkgid)) {
308                         /* already checked this one */
309                         continue;
310                 }
311
312                 /* we do this now so we catch self deps */
313                 jsw_ainsert(checked, pkgid);
314
315                 /* get the libraries needed by this package */
316                 if (!zpm_open(&src, pkgfile)) {
317                         fprintf(stderr, "can't zpm open %s\n", pkgfile);
318                         break;
319                 }
320
321                 if (needed) jsw_adelete(needed);
322                 needed = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
323                 libs = zpm_libraries_needed(&src, pkgid, needed);
324                 zpm_close(&src);
325
326                 if (libs == 0) {
327                         continue;
328                 }
329
330                 i = jsw_atnew();
331
332                 for (soname = jsw_atfirst(i, needed); soname; soname = jsw_atnext(i)) {
333                         int found;
334                         struct pkgloc pkginfo;
335
336                         if (opts->verbose > 1) {
337                                 fprintf(stderr, "checking for %s\n", soname);
338                         }
339                         /* if it's in checked_libs, we've already looked at this one */
340                         if (jsw_afind(checked_libs, soname)) {
341                                 if (opts->verbose > 1) {
342                                         fprintf(stderr, "already checked for %s\n", soname);
343                                 }
344                                 continue;
345                         }
346
347                         /* haven't found this soname */
348                         jsw_ainsert(checked_libs, soname);
349
350                         found = find_lib(soname, opts, &pkginfo);
351                         if (!found) {
352                                 if (!jsw_hinsert(nfound, soname, pkgid)) {
353                                         fprintf(stderr,"insert error\n");
354                                 }
355                                 continue;
356                         }
357
358                         if (found == 1) {
359                                 /* if not alreacy checked, add to check */
360                                 if (!jsw_afind(checked, pkginfo.file)) {
361                                         jsw_hinsert(check,pkginfo.id,pkginfo.file);
362                                 }
363                                 /* shouldn't be in there already */
364                                 jsw_hinsert(forlibs, pkginfo.id, pkginfo.file);
365                                 free(pkginfo.file);
366                                 free(pkginfo.id);
367                         } 
368                         /* otherwise found == 2, so just continue */
369                 }
370         }
371         free(pkgid);
372         free(pkgfile);
373 }
374
375 void print_pkghash(jsw_hash_t *hash, int json) {
376         const char *pkgid, *file;
377         char *fmt;
378         char *sep;
379         int count = 0;
380
381         fmt = json ? "\"%s\": \"%s\"" : "%s:%s";
382         sep = json ? ", " : "\n";
383
384         if (json) {
385                 printf("{ ");
386         }
387
388         if (jsw_hsize(hash) > 0) {
389                 for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) {
390                         pkgid = jsw_hkey(hash);
391                         file = jsw_hitem(hash);
392                         if (count++) {
393                                 printf("%s", sep);
394                         }
395                         printf(fmt, pkgid, file);
396                 }
397         }
398         if (json) printf(" }");
399         printf("\n");
400 }
401
402 int main(int ac, char *av[]) {
403         int option, argn;
404         int findlibs = 0, json = 0;
405         struct pkgloc *found;
406         jsw_hash_t *packages, *check, *forlibs, *nolib;
407         jsw_atree_t *nfound;
408         struct search_ctl opt = { 0 };
409         struct zpm localdb;
410
411         opt.dbrepos = 1;
412
413         opt.localdb = getenv("ZPMDB");
414         if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
415         opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
416         if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
417         opt.repodir = getenv("ZPM_REPO_DIR");
418         if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo";
419
420
421         /* -l also find packages needed for libs
422          * -j output json
423          *  -q quiet - suppress output
424          *  -v verbose - 
425          *  -d debug - trace each step
426          *
427          *  environment:
428          *  ZPMDB - path to localdb, /var/lib/zpm/local.db
429          *  ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repo'
430          *  ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
431          *  '/var/lib/zpm/packages'
432          *  ZPM_ROOT_DIR :- prepends to above paths
433          *
434          *  -M (missing) invert the output and only output missing
435          *  outputs
436          *  packages found (suppress with -P)
437          *  package strings not found (suppress with -S)
438          *  library dependency packages needed and found (suppress with -N)
439          *  library dependency packages already installed (suppress with -H)
440          *  library dependency packages in install set (suppress with -I)
441          *  library dependency sonames not found (suppress with -L)
442          *
443          *  exit value is 0 if no missing libs and no bad package strings
444          *  exit value is non zero otherwise
445          */
446
447         int output = 1;
448         while ((option = getopt(ac, av, "ljqPRDvp:r:d:MiIO")) != -1) {
449                 switch (option) {
450                         case 'l': findlibs = 1; break;
451                         case 'j': json = 1; break;
452                         case 'q': output = 0; break;
453                                   /* show installed files */
454                         case 'i': opt.matchinstalled = 1; break;
455                         case 'I': opt.suppressinstalled = 1; break;
456                                   /* only find localdb pkgs if installed */
457                         case 'O': opt.onlylocalinstalled = 1; break;
458                         case 'd': opt.localdb = optarg; break;
459                         case 'p': opt.pkgdir = optarg; break;
460                         case 'r': opt.repodir = optarg; break;
461                         case 'P': opt.pkgdir = 0; break;
462                         case 'R': opt.repodir = 0; break;
463                         case 'D': opt.localdb = 0; break;
464                                   /* matchallpkgfile means look at
465                                    * all .zpm files, not just ones
466                                    * that have the pkgid string prefix
467                                    */
468                         case 'M': opt.matchallpkgfile = 1; break;
469                         case 'v': opt.verbose++; break;
470                         default:
471                                   exit(EXIT_FAILURE);
472                                   break;
473                 }
474         }
475         argn = optind;
476
477         if (opt.localdb) {
478                 if (zpm_open(&localdb, NULL)) {
479                         opt.zpmdb = &localdb;
480                 } else {
481                         fprintf(stderr, "can't open localdb\n");
482                         return 2;
483                 }
484         }
485
486         if (!find_globs(&opt)) {
487                 fprintf(stderr, "bad globs\n");
488                 return 3;
489         }
490
491         if (opt.verbose > 1) {
492                 unsigned int i;
493                 fprintf(stderr, "globs:");
494                 for (i = 0; i < opt.repos.gl_pathc; i++) {
495                         fprintf(stderr, " %s", opt.repos.gl_pathv[i]);
496                 }
497                 fprintf(stderr, "\n");
498         }
499
500         packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
501                         (itemdup_f)strdup,free,free);
502         check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
503                         (itemdup_f)strdup,free,free);
504         forlibs = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
505                         (itemdup_f)strdup,free,free);
506         nolib = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
507                         (itemdup_f)strdup,free,free);
508         nfound = jsw_anew((cmp_f)strcmp, (dup_f)strdup, (rel_f)free);
509
510         int arg;
511         for (arg = argn; arg < ac; arg++) {
512                 found = find_package(av[arg], &opt);
513                 if (found && (opt.matchinstalled || !found->installed)) {
514                         if (found->installed && opt.suppressinstalled) {
515                                 continue;
516                         }
517                         jsw_hinsert(packages, found->id, found->file);
518                         jsw_hinsert(check, found->id, found->file);
519                         free(found->id);
520                         free(found->file);
521                         free(found);
522                 } else {
523                         jsw_ainsert(nfound, av[arg]);
524                 }
525
526         }
527
528         if (findlibs) {
529                 checklibs(&opt, check, forlibs, nolib);
530                 /* remove from forlibs anything already explicitly
531                  * in packages
532                  */
533         }
534
535         if (output) {
536                 if (jsw_hsize(packages)) {
537                         print_pkghash(packages, json);
538                 }
539                 if (jsw_hsize(forlibs)) {
540                         print_pkghash(forlibs, json);
541                 }
542         }
543
544         if (jsw_hsize(nolib) > 0) {
545                 for (jsw_hreset(nolib); jsw_hitem(nolib); jsw_hnext(nolib)) {
546                         const char *pkgid, *file;
547                         pkgid = jsw_hkey(nolib);
548                         file = jsw_hitem(nolib);
549                         fprintf(stderr, "no lib found %s:%s\n", pkgid, file);
550                 }
551         }
552
553         if (jsw_asize(nfound) > 0) {
554                 jsw_atrav_t *i;
555                 i = jsw_atnew();
556                 char *pkgstr;
557
558                 for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
559                         fprintf(stderr, "%s: not found\n", pkgstr);
560                 }
561         }
562
563         return jsw_asize(nfound) > 0 || jsw_hsize(nolib) > 0 ? 1 : 0;
564 }