]> pd.if.org Git - zpackage/commitdiff
general search improvements
authorNathan Wagner <nw@hydaspes.if.org>
Tue, 13 Nov 2018 19:32:04 +0000 (19:32 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Tue, 13 Nov 2018 19:32:04 +0000 (19:32 +0000)
zpm-search.c

index ff6d7301950ff3171c4cc1e69fe37a80b218adc0..06439253c8b38f757bc479b428b75f318372223d 100644 (file)
@@ -19,6 +19,42 @@ struct pkgloc {
        int info;
 };
 
+struct search_ctl {
+       char *localdb, *repodir, *pkgdir;
+       struct zpm *zpmdb;
+       glob_t repos;
+       int matchallpkgfile;
+};
+
+char *pathcat(char *dir, char *path) {
+       size_t dirlen = 0, pathlen = 0;
+       char *cat;
+
+       /* chop off trailing / on dir */
+       if (dir) {
+               dirlen = strlen(dir);
+               while (dirlen && dir[dirlen-1] == '/') {
+                       dirlen--;
+               }
+       }
+
+       if (path) {
+               pathlen = strlen(path);
+               while (*path && *path == '/') {
+                       path++;
+                       pathlen--;
+               }
+       }
+
+       cat = malloc(dirlen + pathlen + 2);
+       if (cat) {
+               strncpy(cat, dir, dirlen);
+               cat[dirlen] = '/';
+               strcpy(cat+dirlen+1, path);
+       }
+       return cat;
+}
+
 char *checkfile(char *pkgstr, char *path) {
        struct zpm pkgfile;
        char *pkgid = 0;
@@ -50,88 +86,83 @@ char *checkfileforlib(char *soname, char *path) {
        return pkgid;
 }
 
-int find_lib(char *soname, struct pkgloc *pkg) {
+int find_globs(struct search_ctl *opt) {
+       glob_t *repos;
+       int rv;
+
+       repos = &opt->repos;
+       repos->gl_offs = 0;
+
+       int globopts = 0;
+
+       if (opt->repodir) {
+               char *globstr = pathcat(opt->repodir, "*.repo");
+               rv = glob(globstr, globopts, NULL, repos);
+               switch (rv) {
+                       case GLOB_NOSPACE:
+                               fprintf(stderr, "glob no mem\n");
+                               return 0;
+                       case GLOB_ABORTED:
+                               fprintf(stderr, "glob abort\n");
+                               return 0;
+                       case GLOB_NOMATCH:
+                               break;
+                       case 0:
+                               break;
+                       default:
+                               break;
+               }
+               globopts = GLOB_APPEND;
+               free(globstr);
+       }
+
+       if (opt->pkgdir) {
+               char *globstr = pathcat(opt->pkgdir, "*.zpm");
+               rv = glob(globstr, globopts, NULL, repos);
+               switch (rv) {
+                       case GLOB_NOSPACE:
+                               fprintf(stderr, "glob no mem\n");
+                               return 0;
+                       case GLOB_ABORTED:
+                               fprintf(stderr, "glob abort\n");
+                               return 0;
+                       case GLOB_NOMATCH:
+                               break;
+                       case 0:
+                               break;
+                       default:
+                               break;
+               }
+               globopts = GLOB_APPEND;
+               free(globstr);
+       }
+
+       return 1;
+}
+
+int find_lib(char *soname, struct search_ctl *opt, struct pkgloc *pkg) {
        char *latest = 0;
        char *installed = 0, *found = 0;
        char *pkgfile = 0;
        int rv;
-       struct zpm localdb;
-
-       if (zpm_open(&localdb, NULL)) {
-               installed = zpm_findlib(&localdb, soname, "status = 'installed'");
-               zpm_close(&localdb);
+       unsigned int i;
 
+       if (opt->localdb) {
+               installed = zpm_findlib(opt->zpmdb, soname, "status = 'installed'");
                if (installed) {
                        /* we're done, the lib is installed */
                        return 2;
                }
-       } else {
-               fprintf(stderr, "can't open localdb\n");
-       }
-
-       glob_t repos;
-       repos.gl_offs = 0;
-       rv = glob("/var/lib/zpm/repos/*.repo", 0, NULL, &repos);
-       switch (rv) {
-               case GLOB_NOSPACE:
-                       fprintf(stderr, "glob no mem\n");
-                       exit(EXIT_FAILURE); break;
-               case GLOB_ABORTED:
-                       fprintf(stderr, "glob abort\n");
-                       exit(EXIT_FAILURE); break;
-               case GLOB_NOMATCH:
-                       break;
-               case 0:
-                                  break;
-               default:
-                                  break;
-       }
-
-       unsigned i;
-       for (i = 0; i < repos.gl_pathc; i++) {
-               found = checkfileforlib(soname, repos.gl_pathv[i]);
-               if (found) {
-                       rv = zpm_vercmp(found, latest);
-                       if (rv == 1) {
-                               latest = found;
-                               free(pkgfile);
-                               pkgfile = strdup(repos.gl_pathv[i]);
-                       }
-               }
-       }
-
-       globfree(&repos);
-       repos.gl_offs = 0;
-
-       char *pkgdir = "/home/zoranix/zrepo/packages";
-       char *pkgglob = 0;
-
-       size_t globlen = strlen(pkgdir)+7;
-       pkgglob = malloc(globlen+1);
-       snprintf(pkgglob, globlen, "%s/*.zpm", pkgdir);
-       rv = glob(pkgglob, 0, NULL, &repos);
-       free(pkgglob);
-
-       switch (rv) {
-               case GLOB_NOSPACE:
-                       exit(EXIT_FAILURE); break;
-               case GLOB_ABORTED:
-                       exit(EXIT_FAILURE); break;
-               case GLOB_NOMATCH: break;
-               case 0:
-                                  break;
-               default:
-                                  break;
        }
 
-       for (i = 0; i < repos.gl_pathc; i++) {
-               found = checkfileforlib(soname, repos.gl_pathv[i]);
+       for (i = 0; i < opt->repos.gl_pathc; i++) {
+               found = checkfileforlib(soname, opt->repos.gl_pathv[i]);
                if (found) {
                        rv = zpm_vercmp(found, latest);
                        if (rv == 1) {
                                latest = found;
                                free(pkgfile);
-                               pkgfile = strdup(repos.gl_pathv[i]);
+                               pkgfile = strdup(opt->repos.gl_pathv[i]);
                        }
                }
        }
@@ -145,92 +176,42 @@ int find_lib(char *soname, struct pkgloc *pkg) {
        return 0;
 }
 
-struct pkgloc *find_package(char *pkgstr) {
+struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
        char *latest = 0;
        char *installed = 0, *found = 0;
        char *pkgfile;
        struct pkgloc *pkg = 0;
        int rv;
-       struct zpm localdb;
+       unsigned int i;
 
-       if (zpm_open(&localdb, NULL)) {
-               installed = zpm_findpkg(&localdb, pkgstr, "status = 'installed'");
+       if (opt->localdb) {
+               installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
                if (installed) {
                        latest = installed;
                }
-               found = zpm_findpkg(&localdb, pkgstr, NULL);
+               found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
                if (found) {
                        rv = zpm_vercmp(found, latest);
                        if (rv == 1) {
                                latest = found;
-                               pkgfile = localdb.path;
+                               pkgfile = opt->zpmdb->path;
                        }
                }
-               zpm_close(&localdb);
-       } else {
-               fprintf(stderr, "can't open localdb\n");
        }
 
-       glob_t repos;
-       repos.gl_offs = 0;
-       rv = glob("/var/lib/zpm/repos/*.repo", 0, NULL, &repos);
-       switch (rv) {
-               case GLOB_NOSPACE:
-                       fprintf(stderr, "glob no mem\n");
-                       exit(EXIT_FAILURE); break;
-               case GLOB_ABORTED:
-                       fprintf(stderr, "glob abort\n");
-                       exit(EXIT_FAILURE); break;
-               case GLOB_NOMATCH:
-                       break;
-               case 0:
-                                  break;
-               default:
-                                  break;
-       }
-
-       unsigned i;
-       for (i = 0; i < repos.gl_pathc; i++) {
-               found = checkfile(pkgstr, repos.gl_pathv[i]);
-               if (found) {
-                       rv = zpm_vercmp(found, latest);
-                       if (rv == 1) {
-                               latest = found;
-                               pkgfile = repos.gl_pathv[i];
-                       }
+       for (i = 0; i < opt->repos.gl_pathc; i++) {
+               if (!opt->matchallpkgfile
+                               && !strstr(opt->repos.gl_pathv[i], pkgstr)
+                               && !strstr(opt->repos.gl_pathv[i], ".repo")
+                               ) {
+                       continue;
                }
-       }
-
-       globfree(&repos);
-       repos.gl_offs = 0;
-
-       char *pkgdir = "/home/zoranix/zrepo/packages";
-       char *pkgglob = 0;
-
-       size_t globlen = strlen(pkgdir)+strlen(pkgstr)+7;
-       pkgglob = malloc(globlen+1);
-       snprintf(pkgglob, globlen, "%s/%s*.zpm", pkgdir, pkgstr);
-
-       rv = glob(pkgglob, 0, NULL, &repos);
-       switch (rv) {
-               case GLOB_NOSPACE:
-                       exit(EXIT_FAILURE); break;
-               case GLOB_ABORTED:
-                       exit(EXIT_FAILURE); break;
-               case GLOB_NOMATCH: break;
-               case 0:
-                                  break;
-               default:
-                                  break;
-       }
-
-       for (i = 0; i < repos.gl_pathc; i++) {
-               found = checkfile(pkgstr, repos.gl_pathv[i]);
+               found = checkfile(pkgstr, opt->repos.gl_pathv[i]);
                if (found) {
                        rv = zpm_vercmp(found, latest);
                        if (rv == 1) {
                                latest = found;
-                               pkgfile = strdup(repos.gl_pathv[i]);
+                               pkgfile = strdup(opt->repos.gl_pathv[i]);
                        }
                }
        }
@@ -261,7 +242,8 @@ with pkglibs as (\
      where PL.pkgid = %Q\
      ";
 
-void checklibs(jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
+void checklibs(struct search_ctl *opts,
+               jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
        char *pkgid = 0, *pkgfile = 0;
        struct zpm src;
 
@@ -330,7 +312,7 @@ void checklibs(jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
                        /* haven't found this soname */
                        jsw_ainsert(checked_libs, soname);
 
-                       found = find_lib(soname, &pkginfo);
+                       found = find_lib(soname, opts, &pkginfo);
                        if (!found) {
                                if (!jsw_hinsert(nfound, soname, pkgid)) {
                                        fprintf(stderr,"insert error\n");
@@ -357,35 +339,6 @@ void checklibs(jsw_hash_t *check, jsw_hash_t *forlibs, jsw_hash_t *nfound) {
        free(pkgfile);
 }
 
-char *pathcat(char *dir, char *path) {
-       size_t dirlen = 0, pathlen = 0;
-       char *cat;
-
-       /* chop off trailing / on dir */
-       if (dir) {
-               dirlen = strlen(dir);
-               while (dirlen && dir[dirlen-1] == '/') {
-                       dirlen--;
-               }
-       }
-
-       if (path) {
-               pathlen = strlen(path);
-               while (*path && *path == '/') {
-                       path++;
-                       pathlen--;
-               }
-       }
-
-       cat = malloc(dirlen + pathlen + 2);
-       if (cat) {
-               strncpy(cat, dir, dirlen);
-               cat[dirlen] = '/';
-               strcpy(cat+dirlen+1, path);
-       }
-       return cat;
-}
-
 void print_pkghash(jsw_hash_t *hash, int json) {
        const char *pkgid, *file;
        char *fmt;
@@ -414,11 +367,20 @@ void print_pkghash(jsw_hash_t *hash, int json) {
 }
 
 int main(int ac, char *av[]) {
-       int opt, argn;
+       int option, argn;
        int findlibs = 0, json = 0;
        struct pkgloc *found;
        jsw_hash_t *packages, *check, *forlibs, *nolib;
        jsw_atree_t *nfound;
+       struct search_ctl opt = { 0 };
+       struct zpm localdb;
+
+       opt.localdb = getenv("ZPMDB");
+       if (!opt.localdb) opt.localdb = "/var/lib/zpm/local.db";
+       opt.pkgdir = getenv("ZPM_PACKAGE_DIR");
+       if (!opt.pkgdir) opt.pkgdir = "/var/lib/zpm/packages";
+       opt.repodir = getenv("ZPM_REPO_DIR");
+       if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo/";
 
        /* -l also find packages needed for libs
         * -j output json
@@ -447,11 +409,18 @@ int main(int ac, char *av[]) {
         */
 
        int output = 1;
-       while ((opt = getopt(ac, av, "lj")) != -1) {
-               switch (opt) {
+       while ((option = getopt(ac, av, "ljqPRDp:r:d:M")) != -1) {
+               switch (option) {
                        case 'l': findlibs = 1; break;
                        case 'j': json = 1; break;
                        case 'q': output = 0;
+                       case 'd': opt.localdb = optarg; break;
+                       case 'p': opt.pkgdir = optarg; break;
+                       case 'r': opt.repodir = optarg; break;
+                       case 'P': opt.pkgdir = 0; break;
+                       case 'R': opt.repodir = 0; break;
+                       case 'D': opt.localdb = 0; break;
+                       case 'M': opt.matchallpkgfile = 1; break;
                        default:
                                  exit(EXIT_FAILURE);
                                  break;
@@ -459,6 +428,21 @@ int main(int ac, char *av[]) {
        }
        argn = optind;
 
+       if (opt.localdb) {
+               if (zpm_open(&localdb, NULL)) {
+                       opt.zpmdb = &localdb;
+               } else {
+                       fprintf(stderr, "can't open localdb\n");
+                       return 2;
+               }
+       }
+
+
+       if (!find_globs(&opt)) {
+               fprintf(stderr, "bad globs\n");
+               return 3;
+       }
+
        packages = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
                        (itemdup_f)strdup,free,free);
        check = jsw_hnew(ac,NULL,(cmp_f)strcmp,(keydup_f)strdup,
@@ -471,7 +455,7 @@ int main(int ac, char *av[]) {
 
        int arg;
        for (arg = argn; arg < ac; arg++) {
-               found = find_package(av[arg]);
+               found = find_package(av[arg], &opt);
                if (found) {
                        jsw_hinsert(packages, found->id, found->file);
                        jsw_hinsert(check, found->id, found->file);
@@ -485,12 +469,14 @@ int main(int ac, char *av[]) {
        }
 
        if (findlibs) {
-               checklibs(check, forlibs, nolib);
+               checklibs(&opt, check, forlibs, nolib);
        }
 
        if (output) {
                print_pkghash(packages, json);
-               print_pkghash(forlibs, json);
+               if (jsw_hsize(forlibs)) {
+                       print_pkghash(forlibs, json);
+               }
        }
 
        if (jsw_hsize(nolib) > 0) {
@@ -508,7 +494,7 @@ int main(int ac, char *av[]) {
                char *pkgstr;
 
                for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) {
-                       fprintf(stderr, "%s:\n", pkgstr);
+                       fprintf(stderr, "%s: not found\n", pkgstr);
                }
        }