88e26a77cfa61ae56e1897cba99ff0688d60700e
[platform/upstream/rpm.git] / lib / idtx.c
1 #include "system.h"
2
3 #include <idtx.h>
4 #include <rpmlib.h>
5 #include <rpmdb.h>
6 #include <rpmts.h>
7 #include <rpmfileutil.h>
8 #include <rpmlog.h>
9
10 static int reverse = -1;
11
12 /**
13  */
14 static int IDTintcmp(const void * a, const void * b)
15 {
16     return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
17 }
18
19 IDTX IDTXfree(IDTX idtx)
20 {
21     if (idtx) {
22         int i;
23         if (idtx->idt)
24         for (i = 0; i < idtx->nidt; i++) {
25             IDT idt = idtx->idt + i;
26             idt->h = headerFree(idt->h);
27             idt->key = _free(idt->key);
28         }
29         idtx->idt = _free(idtx->idt);
30         idtx = _free(idtx);
31     }
32     return NULL;
33 }
34
35 IDTX IDTXnew(void)
36 {
37     IDTX idtx = xcalloc(1, sizeof(*idtx));
38     idtx->delta = 10;
39     idtx->size = sizeof(*((IDT)0));
40     return idtx;
41 }
42
43 IDTX IDTXgrow(IDTX idtx, int need)
44 {
45     if (need < 0) return NULL;
46     if (idtx == NULL)
47         idtx = IDTXnew();
48     if (need == 0) return idtx;
49
50     if ((idtx->nidt + need) > idtx->alloced) {
51         while (need > 0) {
52             idtx->alloced += idtx->delta;
53             need -= idtx->delta;
54         }
55         idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
56     }
57     return idtx;
58 }
59
60 IDTX IDTXsort(IDTX idtx)
61 {
62     if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
63         qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
64     return idtx;
65 }
66
67 IDTX IDTXload(rpmts ts, rpmTag tag)
68 {
69     IDTX idtx = NULL;
70     rpmdbMatchIterator mi;
71     HGE_t hge = (HGE_t) headerGetEntry;
72     Header h;
73
74     mi = rpmtsInitIterator(ts, tag, NULL, 0);
75 #ifdef  NOTYET
76     (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey');
77 #endif
78     while ((h = rpmdbNextIterator(mi)) != NULL) {
79         rpmTagType type = RPM_NULL_TYPE;
80         int32_t count = 0;
81         int32_t * tidp;
82
83         tidp = NULL;
84         if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
85             continue;
86
87         if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
88             continue;
89
90         idtx = IDTXgrow(idtx, 1);
91         if (idtx == NULL)
92             continue;
93         if (idtx->idt == NULL)
94             continue;
95
96         {   IDT idt;
97             idt = idtx->idt + idtx->nidt;
98             idt->h = headerLink(h);
99             idt->key = NULL;
100             idt->instance = rpmdbGetIteratorOffset(mi);
101             idt->val.u32 = *tidp;
102         }
103         idtx->nidt++;
104     }
105     mi = rpmdbFreeIterator(mi);
106
107     return IDTXsort(idtx);
108 }
109
110 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
111 {
112     IDTX idtx = NULL;
113     HGE_t hge = (HGE_t) headerGetEntry;
114     Header h;
115     int32_t * tidp;
116     FD_t fd;
117     const char ** av = NULL;
118     int ac = 0;
119     rpmRC rpmrc;
120     int xx;
121     int i;
122
123     av = NULL;  ac = 0;
124     xx = rpmGlob(globstr, &ac, &av);
125
126     if (xx == 0)
127     for (i = 0; i < ac; i++) {
128         rpmTagType type;
129         int32_t count;
130         int isSource;
131
132         fd = Fopen(av[i], "r.ufdio");
133         if (fd == NULL || Ferror(fd)) {
134             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), av[i],
135                         Fstrerror(fd));
136             if (fd != NULL) (void) Fclose(fd);
137             continue;
138         }
139
140         rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
141         (void) Fclose(fd);
142         switch (rpmrc) {
143         default:
144             goto bottom;
145             break;
146         case RPMRC_NOTTRUSTED:
147         case RPMRC_NOKEY:
148         case RPMRC_OK:
149             isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
150             if (isSource)
151                 goto bottom;
152             break;
153         }
154
155         tidp = NULL;
156         if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) {
157
158             idtx = IDTXgrow(idtx, 1);
159             if (idtx == NULL || idtx->idt == NULL)
160                 goto bottom;
161
162             {   IDT idt;
163                 idt = idtx->idt + idtx->nidt;
164                 idt->h = headerLink(h);
165                 idt->key = av[i];
166                 av[i] = NULL;
167                 idt->instance = 0;
168                 idt->val.u32 = *tidp;
169             }
170             idtx->nidt++;
171         }
172 bottom:
173         h = headerFree(h);
174     }
175
176     for (i = 0; i < ac; i++)
177         av[i] = _free(av[i]);
178     av = _free(av);     ac = 0;
179
180     return IDTXsort(idtx);
181 }
182