]> pd.if.org Git - zpackage/blob - zpm-packagehash.c
add where clause argument to findpkg
[zpackage] / zpm-packagehash.c
1 #define _POSIX_C_SOURCE 2 
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <unistd.h>
7 #include <string.h>
8
9 #include "zpm.h"
10
11 static int found = 0;
12
13 void usage(void) {
14         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
15 }
16
17 int main(int ac, char **av){
18         int opt;
19         struct zpm pkg;
20         char *dbfile;
21
22         int set = 0;
23         int check = 0;
24         int quiet = 0;
25
26         dbfile = getenv("ZPMDB");
27         if (!dbfile) {
28                 dbfile = "/var/lib/zpm/local.db";
29         }
30
31         while ((opt = getopt(ac, av, "f:scq")) != -1) {
32                 switch (opt) {
33                         case 'f': dbfile = optarg; break;
34                         case 's': set = 1; break;
35                         case 'c': check = 1; break;
36                         case 'q': quiet = 1; break;
37                         default:
38                                   usage();
39                                   exit(EXIT_FAILURE);
40                                   break;
41                 }
42         }
43         int argn = optind;
44
45         if (!dbfile) {
46                 fprintf(stderr, "must specify db\n");
47                 return 1;
48         }
49
50         char *pkgid = av[argn];
51         char hash[ZPM_HASH_STRLEN+1];
52
53         if (zpm_open(&pkg, dbfile)) {
54                 if (check) {
55                         found = zpm_package_hash(&pkg, pkgid, hash);
56                         char *ehash = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid);
57                         if (ehash && found && !strcmp(ehash, hash)) {
58                                 found = 1;
59                         } else {
60                                 found = 0;
61                         }
62                 } else if (set) {
63                         found = zpm_package_sethash(&pkg, pkgid, hash);
64                 } else {
65                         found = zpm_package_hash(&pkg, pkgid, hash);
66                 }
67         }
68         zpm_close(&pkg);
69         if (found && !quiet) {
70                 printf("%s\n", hash);
71         }
72         return found ? 0 : 1;
73 }