X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=zpm-packagehash.c;h=6d5b9128fabb5e4a007305f10637111af24dd649;hb=62f6ff407bc4f2cf03d1fa7cf3dc9a3f4026624a;hp=b1587801a1a5bed5254c8b1d6b59c2edb474a40b;hpb=5774fe14b7203c06873e29d13c9520c9a32321cb;p=zpackage diff --git a/zpm-packagehash.c b/zpm-packagehash.c index b158780..6d5b912 100644 --- a/zpm-packagehash.c +++ b/zpm-packagehash.c @@ -4,11 +4,10 @@ #include #include #include +#include #include "zpm.h" -static int found = 0; - void usage(void) { fprintf(stderr, "zpm-findpkg [-I] [-s ...] [-S ] [package]\n"); } @@ -18,17 +17,28 @@ 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; dbfile = getenv("ZPMDB"); if (!dbfile) { dbfile = "/var/lib/zpm/local.db"; } - while ((opt = getopt(ac, av, "f:s")) != -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); @@ -44,17 +54,37 @@ 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 (set) { - found = zpm_package_sethash(&pkg, pkgid, hash); + if (check || showcurrent) { + current = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid); + } + + if (check) { + checkfail = 1; + if (current) { + zpm_package_hash(&pkg, pkgid, hash); + checkfail = strcmp(current, hash); + } + } else if (set) { + 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 (display && !quiet) { + printf("%s\n", display); + } + free(current); } - zpm_close(&pkg); - if (found) { - printf("%s\n", hash); - } - return found ? 0 : 1; + + return checkfail ? EXIT_FAILURE : 0; }