]> pd.if.org Git - zpackage/commitdiff
add packagehash options
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 12 Oct 2018 23:36:11 +0000 (23:36 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 3 Nov 2018 12:39:27 +0000 (12:39 +0000)
add -c option to check package hash
add -q option to to suppress output

zpm-packagehash.c

index b1587801a1a5bed5254c8b1d6b59c2edb474a40b..534f6ad7de7b4eb4d958f48a6436ccb0a25d2587 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <unistd.h>
+#include <string.h>
 
 #include "zpm.h"
 
@@ -19,16 +20,20 @@ int main(int ac, char **av){
        char *dbfile;
 
        int set = 0;
+       int check = 0;
+       int quiet = 0;
 
        dbfile = getenv("ZPMDB");
        if (!dbfile) {
                dbfile = "/var/lib/zpm/local.db";
        }
 
-       while ((opt = getopt(ac, av, "f:s")) != -1) {
+       while ((opt = getopt(ac, av, "f:scq")) != -1) {
                switch (opt) {
                        case 'f': dbfile = optarg; break;
                        case 's': set = 1; break;
+                       case 'c': check = 1; break;
+                       case 'q': quiet = 1; break;
                        default:
                                  usage();
                                  exit(EXIT_FAILURE);
@@ -46,14 +51,22 @@ int main(int ac, char **av){
        char hash[ZPM_HASH_STRLEN+1];
 
        if (zpm_open(&pkg, dbfile)) {
-               if (set) {
+               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;
+                       }
+               } else if (set) {
                        found = zpm_package_sethash(&pkg, pkgid, hash);
                } else {
                        found = zpm_package_hash(&pkg, pkgid, hash);
                }
        }
        zpm_close(&pkg);
-       if (found) {
+       if (found && !quiet) {
                printf("%s\n", hash);
        }
        return found ? 0 : 1;