]> pd.if.org Git - zpackage/commitdiff
change findpkg to use -f option
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 28 Sep 2018 17:54:24 +0000 (17:54 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 28 Sep 2018 17:54:24 +0000 (17:54 +0000)
db.sql
t/install.t
zpm-add
zpm-contents
zpm-findpkg.c
zpm-install
zpm-list
zpm-merge
zpm-pkg
zpm-pkginfo
zpm-setscript

diff --git a/db.sql b/db.sql
index bf6fd22a07c01b886d9d6e26a9dc1557d5132c03..94ffce308a19b726172ff4e9e987d9ef24053750 100644 (file)
--- a/db.sql
+++ b/db.sql
@@ -52,6 +52,8 @@ create table packages (
 without rowid
 ;
 
+create index package_status_index on packages (status);
+
 create view packages_pkgid as
 select printf('%s-%s-%s', package, version, release) as pkgid, *
 from packages;
@@ -153,6 +155,13 @@ from installedfiles I
 group by I.path
 ;
 
+create view sync_status_ref_count as
+select path, status, count(*) as refcount
+from packagefiles_status
+where status in ('installed', 'installing', 'removing')
+group by path, status
+;
+
 create view packagefiles_status as
 select P.status, PF.*
 from packagefiles_pkgid PF
@@ -197,6 +206,16 @@ left join packagefiles_status PN
        and PI.pkgid != PN.pkgid
 where PN.path is null
 and PI.package in (select package from packages where status = 'installing')
+
+union all
+-- remove files in removing, but not installing
+select distinct 'remove' as op, PR.*
+from packagefiles_status PR
+left join packagefiles_status PN
+on PR.path = PN.path
+and PR.pkgid != PN.pkgid and PN.status in ('installing', 'installed')
+where PN.path is null
+and PR.status = 'removing'
 ;
 
 create table pathtags (
index 9b8d5eebfee8810539318fe4da4cb9ce4bdf588d..00287a9ddab2886fe9a651498103020487f9c6d9 100755 (executable)
@@ -6,7 +6,7 @@
 
 . tap.sh
 
-plan 51
+plan 53
 
 newpkg() {
        pkgid=$1
@@ -186,6 +186,10 @@ skiponfail 3 "can't create $pkgid" || {
        }
 }
 
+tryrun zpm uninstall -d local.db third-1.0-1
+test -f bin/echo
+failsok bin/echo removed
+ls -l bin | diagstdin
 
 cd .. || bailout
 require rm -rf tmp
diff --git a/zpm-add b/zpm-add
index 2b4dda692f822361ccc032aef314d7156404a724..1d68800a9c2faf5d68befb55620a393f47340142 100755 (executable)
--- a/zpm-add
+++ b/zpm-add
@@ -71,7 +71,7 @@ if [ -z "$release" ]; then
        if [ -z "$pkgfile" ]; then
                die "cannot determine package file"
        else
-               pkgstr=$(zpm findpkg $pkgfile $pkgid)
+               pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
                if [ -z "$pkgstr" ]; then
                        die "unable to find package id for $pkgid in $pkgfile"
                fi
index 0d1a027bae73110f937b3dec01151331bd63eaf2..3d1d13fc29ca93f08fb63bc9e1d91942f068afc1 100755 (executable)
@@ -18,7 +18,7 @@ fi
 
 
 for pkg in "$@"; do
-       pkgid=$(zpm findpkg $pkgfile $pkg)
+       pkgid=$(zpm findpkg -f $pkgfile $pkg)
 
        zpm shell $pkgfile <<EOS
 .separator "\t"
index 95085d5590f3516ba21911189a2f3c5696cd40a8..3fd608c56327f5ed1653fb6fbbbef31811b18c79 100644 (file)
@@ -1,11 +1,18 @@
+#define _POSIX_C_SOURCE 2 
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <unistd.h>
 
 #include "zpm.h"
 
 static int found = 0;
 
+void usage(void) {
+       fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
+}
+
 static int prow(void *f, int ncols, char **vals, char **cols) {
        FILE *out = f;
        int i;
@@ -68,60 +75,105 @@ void parse_package(char *pstr, char *name, char *ver, int *rel) {
 }
 
 int main(int ac, char **av){
+       int opt;
        struct zpm pkg;
        char *dbfile, *pkgstr;
        char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages";
        char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp";
 
-//     char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages";
-//     char *order = "order by package, version collate vercmp, cast(release as integer)";
-       char sql[2048];
-
-       if (ac < 2) {
-               fprintf(stderr, "usage: db path\n");
-               return 1;
-       }
+       char *sql;
+       sqlite3_str *include;
+       sqlite3_str *exclude;
+       sqlite3_str *query;
 
        dbfile = getenv("ZPMDB");
 
-       if (ac > 1) {
-               dbfile = av[1];
+       query = sqlite3_str_new(NULL);
+       include = sqlite3_str_new(NULL);
+       exclude = sqlite3_str_new(NULL);
+
+       while ((opt = getopt(ac, av, "f:s:S:I")) != -1) {
+               switch (opt) {
+                       case 'f': dbfile = optarg; break;
+                       case 's': sqlite3_str_appendf(include,",%Q", optarg);
+                                 break;
+                       case 'S': sqlite3_str_appendf(exclude,",%Q", optarg);
+                                 break;
+                       case 'I': sqlite3_str_appendall(include,",'installed'");
+                                 break;
+                       default:
+                                 usage();
+                                 exit(EXIT_FAILURE);
+                                 break;
+               }
+       }
+       int argn = optind;
+
+       if (!dbfile) {
+               fprintf(stderr, "must specify db\n");
+               return 1;
        }
 
        /* given a package name, get the packages */
        /* no package name, get all */
 
+#ifdef DEBUG
+       fprintf(stderr, "opening db %s\n", dbfile);
+#endif
+
        if (zpm_open(&pkg, dbfile)) {
                char *errmsg;
+               char *excludes, *includes;
 
-               if (ac >= 2) {
+               excludes = sqlite3_str_value(exclude);
+               includes = sqlite3_str_value(include);
+
+               sqlite3_str_appendall(query, " ");
+               sqlite3_str_appendall(query, select);
+               sqlite3_str_appendall(query, " where true");
+
+               if (includes) {
+                       sqlite3_str_appendf(query, " and status in (%s)", includes+1);
+               }
+               if (excludes) {
+                       sqlite3_str_appendf(query, " and status not in (%s)", excludes+1);
+               }
+
+               if (ac >= argn) {
                        int release;
                        char version[32];
                        char package[32];
 
-                       pkgstr = av[2];
+                       pkgstr = av[argn];
 
                        parse_package(pkgstr, package, version, &release);
+                       if (*package != 0) {
+                               sqlite3_str_appendf(query, " and package = %Q",
+                                               package);
+                       }
+                       if (*version != 0) {
+                               sqlite3_str_appendf(query, " and version = %Q",
+                                               version);
+                       }
                        if (release != -1) {
-                               /* all three */
-                       sprintf(sql, "%s where package = '%s' and version = "
-                                       "'%s' and release = %d %s;", select,
-                                       package, version, release, group);
-                       } else if (*version != 0) {
-                       sprintf(sql, "%s where package = '%s' and version = "
-                                       "'%s' %s;", select,
-                                       package, version, group);
-
-                       } else {
-
-                       sprintf(sql, "%s where package = '%s' %s;",
-                                       select, av[2], group);
+                               sqlite3_str_appendf(query, " and release = %d",
+                                               release);
                        }
-               } else {
-                       sprintf(sql, "%s %s;", select, group);
                }
 
-               zpm_exec(&pkg, sql, prow, stdout, &errmsg);
+               sqlite3_str_appendall(query, " ");
+               sqlite3_str_appendall(query, group);
+               sqlite3_str_appendall(query, ";");
+
+               sql = sqlite3_str_value(query);
+               if (sql) {
+#ifdef DEBUG
+                       fprintf(stderr, "q: %s\n", sql);
+#endif
+                       zpm_exec(&pkg, sql, prow, stdout, &errmsg);
+               } else {
+                       fprintf(stderr, "unable to form database query\n");
+               }
        }
        zpm_close(&pkg);
        return found ? 0 : 1;
index 0bbfed5d9e4bc6d08a862a482fcd92315d95de33..47681d3c56d3dea0d2590fdb121a65efabc33514 100755 (executable)
@@ -98,6 +98,17 @@ if [ $var -gt 0 ]; then
        zpm list -v -f $localdb -s installing 
        die "already ($localdb) installing $var package(s)"
 fi
+# check if we're installing something already
+var=$(zpm list -f $localdb -s removing | wc -l)
+if [ $var -gt 0 ]; then
+       zpm list -v -f $localdb -s removing 
+       die "already ($localdb) removing $var package(s)"
+fi
+var=$(zpm list -f $localdb -s updating | wc -l)
+if [ $var -gt 0 ]; then
+       zpm list -v -f $localdb -s updating 
+       die "already ($localdb) updating $var package(s)"
+fi
 
 if [ -n "$rootdir" ]; then
        ZPM_ROOT_DIR="$rootdir"
@@ -106,7 +117,7 @@ fi
 
 # TODO mark already installed packages as updating?
 for pkgstr in "$@"; do
-       pkgid=$(zpm findpkg $pkgfile $pkgstr)
+       pkgid=$(zpm findpkg -f $pkgfile $pkgstr)
        if [ $? -ne 0 ]; then
                # TODO log
                die "can't find package $pkgstr in $pkgfile"
index 5868ef2d02b5ccdf34ac9a88ecc5039e940d449e..0dda9daa68223dfd0ec231a6c8e014e5d1f41d65 100755 (executable)
--- a/zpm-list
+++ b/zpm-list
@@ -60,4 +60,3 @@ zpm shell $pkgfile <<EOS
 select $cols from packages_pkgid $where
 ;
 EOS
-
index d9833733b09baeda9bbea2099b02e1d7dc6246e7..ada48aacfefcc6a0859908a38ae632a41f6d25bb 100755 (executable)
--- a/zpm-merge
+++ b/zpm-merge
@@ -67,7 +67,7 @@ fi
 
 # try to get from package file
 if [ -z "$release" ]; then
-       pkgid=$(zpm findpkg $pkgfile $pkgid)
+       pkgid=$(zpm findpkg -f $pkgfile $pkgid)
        if [ -z "$pkgid" ]; then
                die cannot find package id
        fi
diff --git a/zpm-pkg b/zpm-pkg
index 14a01654ea598e79152aabd9ac7eb701cac4ce6d..7a0fc77c6759d72f4b372d3e9ce3f746065eef69 100755 (executable)
--- a/zpm-pkg
+++ b/zpm-pkg
@@ -41,7 +41,7 @@ fi
 item=$1
 shift
 #printf "pkg: %s\n" $pkg
-pkgid=$(zpm findpkg $pkgfile "$item")
+pkgid=$(zpm findpkg -f $pkgfile "$item")
 
 if [ -z "$pkgid" ]; then
        die "can't find pkgid for $item in $pkgfile"
@@ -58,7 +58,7 @@ while [ $# -gt 0 ]; do
        show=0
        case "$item" in
                :*)
-                       pkgid=$(zpm findpkg $pkgfile "${item#:}")
+                       pkgid=$(zpm findpkg -f $pkgfile "${item#:}")
                        continue
                        ;;
                *=*)
index daef64246f8223b8e0d591fc91adf37ba85e009d..ff55481c27e4022e66cd80caaabe6d1e9189c7b9 100755 (executable)
@@ -34,7 +34,7 @@ if [ -z "$pkgfile" ]; then
 fi
 
 zpm-test -v $pkgfile
-pkg=$(zpm-findpkg $pkgfile)
+pkg=$(zpm-findpkg -f $pkgfile)
 
 #.mode line
 {
index 1b055fd0ddef50bb84983547f214b487c4101d95..09e3b4a6d9853b523f4d09bcdaf24b81bc5eb542 100755 (executable)
@@ -42,7 +42,7 @@ fi
 
 # try to get from package file
 if [ -z "$release" ]; then
-       pkgid=$(zpm findpkg $pkgfile $pkgid)
+       pkgid=$(zpm findpkg -f $pkgfile $pkgid)
        if [ -z "$pkgid" ]; then
                die cannot find package id
        fi