]> pd.if.org Git - zpackage/blob - src/packagehash.c
allow partial package ids in packagehash
[zpackage] / src / 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 void usage(void) {
12         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
13 }
14
15 int main(int ac, char **av){
16         int opt, argn;
17         struct zpm pkg;
18         char *dbfile;
19
20         int set = 0, clear = 0, showcurrent = 0;
21         int check = 0, quiet = 0, checkfail = 0, verbose = 0;
22         char hash[ZPM_HASH_STRLEN+1];
23         char *pkgid = 0, *current = 0, *display = hash;
24
25         dbfile = getenv("ZPMDB");
26         if (!dbfile) {
27                 dbfile = "/var/lib/zpm/local.db";
28         }
29
30         /* set -s
31          * clear -S
32          * show current -e
33          * check -c
34          */ 
35         while ((opt = getopt(ac, av, "f:sScqev")) != -1) {
36                 switch (opt) {
37                         case 'f': dbfile = optarg; break;
38                         case 's': set = 1; break;
39                         case 'S': clear = 1; break;
40                         case 'c': check = 1; break;
41                         case 'q': quiet = 1; break;
42                         case 'e': showcurrent = 1; break;
43                         case 'v': verbose = 1; break;
44                         default:
45                                   usage();
46                                   exit(EXIT_FAILURE);
47                                   break;
48                 }
49         }
50         argn = optind;
51
52         if (!dbfile) {
53                 fprintf(stderr, "must specify db\n");
54                 return 1;
55         }
56
57         if (zpm_open(&pkg, dbfile)) {
58                 pkgid = zpm_findpkg(&pkg, av[argn], NULL);
59                 if (!pkgid) {
60                         exit(EXIT_FAILURE);
61                 }
62                 if (check || showcurrent) {
63                         current = zpm_db_string(&pkg, "select hash from packages_pkgid where pkgid = %Q", pkgid);
64                 }
65
66                 if (check) {
67                         checkfail = 1;
68                         if (current) {
69                                 zpm_package_hash(&pkg, pkgid, hash);
70                                 checkfail = strcmp(current, hash);
71                         }
72                 } else if (set) {
73                         zpm_package_sethash(&pkg, pkgid, hash);
74                 } else if (clear) {
75                         zpm_package_sethash(&pkg, pkgid, NULL);
76                         display = NULL;
77                 } else if (showcurrent) {
78                         display = current;
79                 } else {
80                         zpm_package_hash(&pkg, pkgid, hash);
81                 }
82
83                 zpm_close(&pkg);
84                 if (display && !quiet) {
85                         if (verbose) {
86                                 printf("%s ", pkgid);
87                         }
88                         printf("%s\n", display);
89                 }
90                 free(current);
91         }
92
93         return checkfail ? EXIT_FAILURE : 0;
94 }