]> pd.if.org Git - zpackage/blob - zpm-note.c
ignore non-empty directories at unlink
[zpackage] / zpm-note.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <unistd.h>
8
9 #include "zpm.h"
10
11 void usage(void) {
12         fprintf(stderr, "zpm-note [-I] [-s <status> ...] [-S <status>] [package]\n");
13 }
14
15 void print_note(struct zpm_note *n) {
16         printf("%" PRId64 " %s %s\n", n->id, n->pkgid ? n->pkgid : "-", n->note);
17 }
18
19 int main(int ac, char **av){
20         int opt;
21         struct zpm pkg;
22         char *dbfile;
23         int fail, echo = 0;
24
25         int select = 1;
26
27         struct zpm_note n = { 0 };
28         n.pkgid = 0;
29         n.hash = 0;
30         n.path = 0;
31
32         dbfile = getenv("ZPMDB");
33         if (!dbfile) {
34                 dbfile = "/var/lib/zpm/local.db";
35         }
36
37         /* -l list
38          * -p packageid
39          * -A ack
40          * -n note id
41          * -D delete
42          * -H hash
43          * -a all
44          * -m note message, otherwise EDITOR/vi to edit a text file
45          * -e echo note after creating
46          * zpm note -p pkgid -P path -H hash -m 'foo'
47          * zpm note -l
48          * zpm note -D <n>
49          * zpm note -A <n>
50          */
51         int list = 0, delete = 0, ack = 0;
52         while ((opt = getopt(ac, av, "f:p:P:H:m:n:lDAae")) != -1) {
53                 switch (opt) {
54                         case 'f': dbfile = optarg; break;
55                         case 'p': n.pkgid = optarg; break;
56                         case 'P': n.path = optarg; break;
57                         case 'H': n.hash = optarg; break;
58                         case 'm': n.note = optarg; break;
59                         case 'n': n.id = strtoll(optarg, NULL, 10); break;
60                         case 'l': list = 1; break;
61                         case 'e': echo = 1; break;
62                         case 'D': delete = 1; break;
63                         case 'A': ack = 1; break;
64                         case 'a': select = 3; break;
65
66                         default:
67                                   usage();
68                                   exit(EXIT_FAILURE);
69                                   break;
70                 }
71         }
72
73         if (!dbfile) {
74                 fprintf(stderr, "must specify db\n");
75                 return 1;
76         }
77
78         /* given a package name, get the packages */
79         /* no package name, get all */
80
81         if (zpm_open(&pkg, dbfile)) {
82                 if (list) {
83                         while (zpm_note(&pkg, &n, select)) {
84                                 print_note(&n);
85                                 zpm_note_free(&n);
86                         }
87                 } else if (n.note) {
88                         n.id = zpm_note_add(&pkg, n.pkgid, n.path, n.hash, "%s", n.note);
89                         if (n.id) {
90                                 if (echo) {
91                                         print_note(&n);
92                                 } else {
93                                         printf("%" PRId64 "\n", n.id);
94                                 }
95                         } else {
96                                 fprintf(stderr, "unable to create note\n");
97                                 pkg.error = 1;
98                         }
99                 } else if (delete) {
100                         zpm_note_del(&pkg, n.id);
101                 } else if (ack) {
102                         zpm_note_ack(&pkg, n.id);
103                 }
104         } else {
105                 fprintf(stderr, "unable to open %s\n", dbfile);
106         }
107         fail = pkg.error;
108         zpm_close(&pkg);
109         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
110 }