]> pd.if.org Git - zpackage/commitdiff
improve contents and packagehash
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 2 Nov 2018 11:59:26 +0000 (11:59 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 3 Nov 2018 12:39:53 +0000 (12:39 +0000)
Make contents have options more like ls.

Added options to packagehash to show the existing hash.

db.sql
zpm-add
zpm-contents
zpm-packagehash.c

diff --git a/db.sql b/db.sql
index c7040349b38dc2004ed9970eb3e9ca9a414da33a..00c5df0f6f1e316c964dd380fe84570f95cbc169 100644 (file)
--- a/db.sql
+++ b/db.sql
@@ -194,6 +194,23 @@ begin
 end
 ;
 
+create trigger packagefiles_delete_trigger instead of
+delete on packagefiles_pkgid
+begin
+       delete from packagefiles
+       where package = OLD.package
+       and version = OLD.version
+       and release = OLD.release
+       and path = OLD.path
+       ;
+       update packages set hash = null
+       where package = OLD.package
+       and version = OLD.version
+       and release = OLD.release
+       ;
+end
+;
+
 create view installed_ref_count as
 select I.path, count(*) as refcount
 from installedfiles I
diff --git a/zpm-add b/zpm-add
index 28c12f96fef2cd82f6af30b67e79a227f689b788..6dc08b7d1dc09bce85dd4241bbe4eb56f55648e1 100755 (executable)
--- a/zpm-add
+++ b/zpm-add
@@ -199,8 +199,12 @@ for path in $*; do
 
        # TODO check that we have such a package,version,release
        #cat <<EOS
+       if [ $verbose -gt 2 ]; then
+               shellecho=".echo on"
+       fi
        zpm shell $pkgfile <<EOS
 begin;
+$shellecho
 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
 commit;
index 23c07ae29a1d327050f8c5a06e953c5b141083c0..7522c183a647f0f8f9ab34647963e3954e2021db 100755 (executable)
@@ -2,43 +2,87 @@
 
 pkgfile=${ZPMDB:-/var/lib/zpm/db.zpm}
 
-verbose=0
+long=0
+pkgonly=0
+quiet=0
 
-while getopts f:v opt; do
+while getopts f:qln opt; do
        case $opt in
                f) pkgfile="$OPTARG" ;;
-               v) verbose=1
+               l) long=1 ;;
+               n) pkgonly=1 ;;
+               q) quiet=1 ;;
        esac
 done
 shift $((OPTIND - 1))
 
-set -e
-
 if [ ! -f $pkgfile ]; then
        echo cannot find $pkgfile
        exit 1
 fi
 
-cols=
-if [ $verbose -gt 0 ]; then
-       cols="filetype,printf('%4.4s', mode) as mode,username,groupname,"
+if [ $long -gt 0 ]; then
+       cols="filetype,printf('%3.3s', mode) as mode,username,groupname,"
+fi
+
+if [ $pkgonly -eq 1 ]; then
+       cols="pkgid"
+elif [ $quiet -eq 0 ]; then
+       cols="pkgid,$cols"
 fi
 
-for pkg in "$@"; do
-       pkgid=$(zpm findpkg -f $pkgfile $pkg)
-
-       zpm shell $pkgfile <<EOS
-.separator " "
-select pkgid, $cols
-case when filetype = 'd' then
-       rtrim(path,'/') || '/'
-else
-       path
-end as path
-from packagefiles_pkgid
-where pkgid = '$pkgid'
-order by path
-;
-EOS
+pkglist=
+while [ $# -gt 0 ]; do
+       pkg=$1
+       shift
+       if [ "$pkg" = '--' ]; then
+               break;
+       fi
+
+       pkgid=$(zpm findpkg -f $pkgfile "$pkg")
+       if [ -n "$pkgid" ]; then
+               q=$(zpm quote -q "$pkgid")
+               pkglist=",$q"
+       else
+               warn "package $pkg not found, ignoring"
+       fi
+done
+pkglist=${pkglist#,}
 
+globlist=
+for glob in "$@"; do
+       q=$(zpm quote -q "$glob")
+       globlist="or path glob $q"
 done
+globlist=${globlist#'or '}
+
+cols=${cols%,}
+
+{
+       printf '.separator " "\n'
+       printf 'select %s\n' "$cols"
+
+       if [ $pkgonly -eq 0 ]; then
+               if [ -n "$cols" ]; then
+                       printf ', '
+               fi
+
+               cat <<-EOC
+               case when filetype = 'd' then
+                       rtrim(path,'/') || '/'
+               else
+                       path
+               end as path
+               EOC
+       fi
+       printf 'from packagefiles_pkgid\nwhere true\n'
+       if [ -n "$globlist" ]; then
+               printf "and (%s)\n" "$globlist"
+       fi
+       if [ -n "$pkglist" ]; then
+               printf "and pkgid in (%s)\n" "$pkglist"
+       fi
+       printf 'order by pkgid,path\n'
+       printf ';\n'
+} | zpm shell $pkgfile
+#} | cat
index 534f6ad7de7b4eb4d958f48a6436ccb0a25d2587..6ca53af3c57274fda0b22c2d8ee272ad70387bc4 100644 (file)
@@ -8,8 +8,6 @@
 
 #include "zpm.h"
 
-static int found = 0;
-
 void usage(void) {
        fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
 }
@@ -19,7 +17,7 @@ int main(int ac, char **av){
        struct zpm pkg;
        char *dbfile;
 
-       int set = 0;
+       int set = 0, clear = 0, showcurrent = 0;
        int check = 0;
        int quiet = 0;
 
@@ -28,12 +26,19 @@ int main(int ac, char **av){
                dbfile = "/var/lib/zpm/local.db";
        }
 
-       while ((opt = getopt(ac, av, "f:scq")) != -1) {
+       /* set -s
+        * clear -S
+        * show current -e
+        * check -c
+        */ 
+       while ((opt = getopt(ac, av, "f:sScqe")) != -1) {
                switch (opt) {
                        case 'f': dbfile = optarg; break;
                        case 's': set = 1; break;
+                       case 'S': clear = 1; break;
                        case 'c': check = 1; break;
                        case 'q': quiet = 1; break;
+                       case 'e': showcurrent = 1; break;
                        default:
                                  usage();
                                  exit(EXIT_FAILURE);
@@ -49,25 +54,39 @@ int main(int ac, char **av){
 
        char *pkgid = av[argn];
        char hash[ZPM_HASH_STRLEN+1];
+       char *current = 0, *display = hash;
+       int checkfail = 0;
 
        if (zpm_open(&pkg, dbfile)) {
+               if (check || showcurrent) {
+                       current = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid);
+               }
+
                if (check) {
-                       found = zpm_package_hash(&pkg, pkgid, hash);
-                       char *ehash = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid);
-                       if (ehash && found && !strcmp(ehash, hash)) {
-                               found = 1;
-                       } else {
-                               found = 0;
+                       checkfail = 1;
+                       if (current) {
+                               zpm_package_hash(&pkg, pkgid, hash);
+                               checkfail = strcmp(current, hash);
                        }
                } else if (set) {
-                       found = zpm_package_sethash(&pkg, pkgid, hash);
+                       zpm_package_sethash(&pkg, pkgid, hash);
+               } else if (clear) {
+                       zpm_package_sethash(&pkg, pkgid, NULL);
+                       display = NULL;
+               } else if (showcurrent) {
+                       display = current;
                } else {
-                       found = zpm_package_hash(&pkg, pkgid, hash);
+                       zpm_package_hash(&pkg, pkgid, hash);
+               }
+
+               zpm_close(&pkg);
+               if (current) {
+                       free(current);
+               }
+               if (display && !quiet) {
+                       printf("%s\n", display);
                }
        }
-       zpm_close(&pkg);
-       if (found && !quiet) {
-               printf("%s\n", hash);
-       }
-       return found ? 0 : 1;
+
+       return checkfail ? EXIT_FAILURE : 0;
 }