]> pd.if.org Git - zpackage/blobdiff - zpm-packagehash.c
add program to generate a hash of package contents
[zpackage] / zpm-packagehash.c
diff --git a/zpm-packagehash.c b/zpm-packagehash.c
new file mode 100644 (file)
index 0000000..b158780
--- /dev/null
@@ -0,0 +1,60 @@
+#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");
+}
+
+int main(int ac, char **av){
+       int opt;
+       struct zpm pkg;
+       char *dbfile;
+
+       int set = 0;
+
+       dbfile = getenv("ZPMDB");
+       if (!dbfile) {
+               dbfile = "/var/lib/zpm/local.db";
+       }
+
+       while ((opt = getopt(ac, av, "f:s")) != -1) {
+               switch (opt) {
+                       case 'f': dbfile = optarg; break;
+                       case 's': set = 1; break;
+                       default:
+                                 usage();
+                                 exit(EXIT_FAILURE);
+                                 break;
+               }
+       }
+       int argn = optind;
+
+       if (!dbfile) {
+               fprintf(stderr, "must specify db\n");
+               return 1;
+       }
+
+       char *pkgid = av[argn];
+       char hash[ZPM_HASH_STRLEN+1];
+
+       if (zpm_open(&pkg, dbfile)) {
+               if (set) {
+                       found = zpm_package_sethash(&pkg, pkgid, hash);
+               } else {
+                       found = zpm_package_hash(&pkg, pkgid, hash);
+               }
+       }
+       zpm_close(&pkg);
+       if (found) {
+               printf("%s\n", hash);
+       }
+       return found ? 0 : 1;
+}