X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=src%2Fpackagehash.c;fp=src%2Fpackagehash.c;h=6d5b9128fabb5e4a007305f10637111af24dd649;hb=5dd3c3e64a9574112dda77a5afc167f5daa53fd8;hp=0000000000000000000000000000000000000000;hpb=bd21f0a1265b43ad5f05353a39db31c16826f05c;p=zpackage diff --git a/src/packagehash.c b/src/packagehash.c new file mode 100644 index 0000000..6d5b912 --- /dev/null +++ b/src/packagehash.c @@ -0,0 +1,90 @@ +#define _POSIX_C_SOURCE 2 + +#include +#include +#include +#include +#include + +#include "zpm.h" + +void usage(void) { + fprintf(stderr, "zpm-findpkg [-I] [-s ...] [-S ] [package]\n"); +} + +int main(int ac, char **av){ + int opt; + struct zpm pkg; + char *dbfile; + + int set = 0, clear = 0, showcurrent = 0; + int check = 0; + int quiet = 0; + + dbfile = getenv("ZPMDB"); + if (!dbfile) { + dbfile = "/var/lib/zpm/local.db"; + } + + /* 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); + break; + } + } + int argn = optind; + + if (!dbfile) { + fprintf(stderr, "must specify db\n"); + return 1; + } + + 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) { + 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 { + zpm_package_hash(&pkg, pkgid, hash); + } + + zpm_close(&pkg); + if (display && !quiet) { + printf("%s\n", display); + } + free(current); + } + + return checkfail ? EXIT_FAILURE : 0; +}