]> pd.if.org Git - zpackage/commitdiff
add options to search installed packages
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 30 Nov 2018 12:33:52 +0000 (12:33 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 30 Nov 2018 12:33:52 +0000 (12:33 +0000)
zpm-search.c

index b431152e690fe8ec451ed488290ad7a93cd2afe0..defbe14fd09173cf243461442b05986b3427ef7d 100644 (file)
@@ -17,6 +17,7 @@ struct pkgloc {
        char *id;
        char *file;
        int info;
+       int installed;
 };
 
 struct search_ctl {
@@ -24,7 +25,10 @@ struct search_ctl {
        struct zpm *zpmdb;
        glob_t repos;
        int matchallpkgfile;
+       int matchinstalled;
+       int suppressinstalled;
        int verbose;
+       int dbrepos;
 };
 
 char *pathcat(char *dir, char *path) {
@@ -199,6 +203,7 @@ struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
                installed = zpm_findpkg(opt->zpmdb, pkgstr, "status = 'installed'");
                if (installed) {
                        latest = installed;
+                       pkgfile = opt->zpmdb->path;
                }
                found = zpm_findpkg(opt->zpmdb, pkgstr, NULL);
                if (found) {
@@ -208,6 +213,12 @@ struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
                                pkgfile = opt->zpmdb->path;
                        }
                }
+
+#if 0
+               if (opt->dbrepos) {
+                       zpm_foreach_repo(opt->zpmdb, search_repo, pkg)
+               }
+#endif
        }
 
        for (i = 0; i < opt->repos.gl_pathc; i++) {
@@ -231,6 +242,10 @@ struct pkgloc *find_package(char *pkgstr, struct search_ctl *opt) {
                pkg = malloc(sizeof *pkg);
                pkg->id = strdup(latest);
                pkg->file = pkgfile;
+               pkg->installed = 0;
+               if (installed) {
+                      pkg->installed = !strcmp(latest, installed);
+               }
        }
 
        return pkg;
@@ -390,12 +405,15 @@ int main(int ac, char *av[]) {
        struct search_ctl opt = { 0 };
        struct zpm localdb;
 
+       opt.dbrepos = 1;
+
        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/";
+       if (!opt.repodir) opt.repodir = "/var/lib/zpm/repo";
+
 
        /* -l also find packages needed for libs
         * -j output json
@@ -405,7 +423,7 @@ int main(int ac, char *av[]) {
         *
         *  environment:
         *  ZPMDB - path to localdb, /var/lib/zpm/local.db
-        *  ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repos'
+        *  ZPM_REPO_DIR - path to *.repo files, '/var/lib/zpm/repo'
         *  ZPM_PACKAGE_DIRS - : separated paths to *.zpm files,
         *  '/var/lib/zpm/packages'
         *  ZPM_ROOT_DIR :- prepends to above paths
@@ -424,17 +442,24 @@ int main(int ac, char *av[]) {
         */
 
        int output = 1;
-       while ((option = getopt(ac, av, "ljqPRDvp:r:d:M")) != -1) {
+       while ((option = getopt(ac, av, "ljqPRDvp:r:d:MiI")) != -1) {
                switch (option) {
                        case 'l': findlibs = 1; break;
                        case 'j': json = 1; break;
-                       case 'q': output = 0;
+                       case 'q': output = 0; break;
+                                 /* show installed files */
+                       case 'i': opt.matchinstalled = 1; break;
+                       case 'I': opt.suppressinstalled = 1; break;
                        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;
+                                 /* matchallpkgfile means look at
+                                  * all .zpm files, not just ones
+                                  * that have the pkgid string prefix
+                                  */
                        case 'M': opt.matchallpkgfile = 1; break;
                        case 'v': opt.verbose++; break;
                        default:
@@ -453,7 +478,6 @@ int main(int ac, char *av[]) {
                }
        }
 
-
        if (!find_globs(&opt)) {
                fprintf(stderr, "bad globs\n");
                return 3;
@@ -481,7 +505,10 @@ int main(int ac, char *av[]) {
        int arg;
        for (arg = argn; arg < ac; arg++) {
                found = find_package(av[arg], &opt);
-               if (found) {
+               if (found && (opt.matchinstalled || !found->installed)) {
+                       if (found->installed && opt.suppressinstalled) {
+                               continue;
+                       }
                        jsw_hinsert(packages, found->id, found->file);
                        jsw_hinsert(check, found->id, found->file);
                        free(found->id);
@@ -498,7 +525,9 @@ int main(int ac, char *av[]) {
        }
 
        if (output) {
-               print_pkghash(packages, json);
+               if (jsw_hsize(packages)) {
+                       print_pkghash(packages, json);
+               }
                if (jsw_hsize(forlibs)) {
                        print_pkghash(forlibs, json);
                }