refactor: split off openpkgdb()
[platform/upstream/libsolv.git] / ext / repo_rpmdb.c
1 /*
2  * Copyright (c) 2007-2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_rpmdb
10  *
11  * convert rpm db to repo
12  *
13  */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <stdint.h>
25 #include <errno.h>
26
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
29 #ifndef RPM5
30 #include <rpm/header.h>
31 #endif
32 #include <rpm/rpmdb.h>
33
34 #ifndef DB_CREATE
35 # if defined(SUSE) || defined(HAVE_RPM_DB_H)
36 #  include <rpm/db.h>
37 # else
38 #  include <db.h>
39 # endif
40 #endif
41
42 #include "pool.h"
43 #include "repo.h"
44 #include "hash.h"
45 #include "util.h"
46 #include "queue.h"
47 #include "chksum.h"
48 #include "repo_rpmdb.h"
49
50 /* 3: added triggers */
51 /* 4: fixed triggers */
52 #define RPMDB_COOKIE_VERSION 4
53
54 #define TAG_NAME                1000
55 #define TAG_VERSION             1001
56 #define TAG_RELEASE             1002
57 #define TAG_EPOCH               1003
58 #define TAG_SUMMARY             1004
59 #define TAG_DESCRIPTION         1005
60 #define TAG_BUILDTIME           1006
61 #define TAG_BUILDHOST           1007
62 #define TAG_INSTALLTIME         1008
63 #define TAG_SIZE                1009
64 #define TAG_DISTRIBUTION        1010
65 #define TAG_VENDOR              1011
66 #define TAG_LICENSE             1014
67 #define TAG_PACKAGER            1015
68 #define TAG_GROUP               1016
69 #define TAG_URL                 1020
70 #define TAG_ARCH                1022
71 #define TAG_FILESIZES           1028
72 #define TAG_FILEMODES           1030
73 #define TAG_FILEMD5S            1035
74 #define TAG_FILELINKTOS         1036
75 #define TAG_FILEFLAGS           1037
76 #define TAG_SOURCERPM           1044
77 #define TAG_PROVIDENAME         1047
78 #define TAG_REQUIREFLAGS        1048
79 #define TAG_REQUIRENAME         1049
80 #define TAG_REQUIREVERSION      1050
81 #define TAG_NOSOURCE            1051
82 #define TAG_NOPATCH             1052
83 #define TAG_CONFLICTFLAGS       1053
84 #define TAG_CONFLICTNAME        1054
85 #define TAG_CONFLICTVERSION     1055
86 #define TAG_TRIGGERNAME         1066
87 #define TAG_TRIGGERVERSION      1067
88 #define TAG_TRIGGERFLAGS        1068
89 #define TAG_CHANGELOGTIME       1080
90 #define TAG_CHANGELOGNAME       1081
91 #define TAG_CHANGELOGTEXT       1082
92 #define TAG_OBSOLETENAME        1090
93 #define TAG_FILEDEVICES         1095
94 #define TAG_FILEINODES          1096
95 #define TAG_SOURCEPACKAGE       1106
96 #define TAG_PROVIDEFLAGS        1112
97 #define TAG_PROVIDEVERSION      1113
98 #define TAG_OBSOLETEFLAGS       1114
99 #define TAG_OBSOLETEVERSION     1115
100 #define TAG_DIRINDEXES          1116
101 #define TAG_BASENAMES           1117
102 #define TAG_DIRNAMES            1118
103 #define TAG_PAYLOADFORMAT       1124
104 #define TAG_PATCHESNAME         1133
105 #define TAG_FILECOLORS          1140
106 #define TAG_SUGGESTSNAME        1156
107 #define TAG_SUGGESTSVERSION     1157
108 #define TAG_SUGGESTSFLAGS       1158
109 #define TAG_ENHANCESNAME        1159
110 #define TAG_ENHANCESVERSION     1160
111 #define TAG_ENHANCESFLAGS       1161
112
113 /* rpm5 tags */
114 #define TAG_DISTEPOCH           1218
115
116 /* rpm4 tags */
117 #define TAG_LONGFILESIZES       5008
118 #define TAG_LONGSIZE            5009
119
120 /* signature tags */
121 #define TAG_SIGBASE             256
122 #define TAG_SIGMD5              (TAG_SIGBASE + 5)
123 #define TAG_SHA1HEADER          (TAG_SIGBASE + 13)
124
125 #define SIGTAG_SIZE             1000
126 #define SIGTAG_PGP              1002    /* RSA signature */
127 #define SIGTAG_MD5              1004    /* header+payload md5 checksum */
128 #define SIGTAG_GPG              1005    /* DSA signature */
129
130 #define DEP_LESS                (1 << 1)
131 #define DEP_GREATER             (1 << 2)
132 #define DEP_EQUAL               (1 << 3)
133 #define DEP_STRONG              (1 << 27)
134 #define DEP_PRE                 ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
135
136 #define FILEFLAG_GHOST          (1 <<  6)
137
138
139 #ifdef RPM5
140 # define RPM_INDEX_SIZE 4
141 #else
142 # define RPM_INDEX_SIZE 8
143 #endif
144
145 struct rpmid {
146   unsigned int dbid;
147   char *name;
148 };
149
150 typedef struct rpmhead {
151   int cnt;
152   int dcnt;
153   unsigned char *dp;
154   int forcebinary;              /* sigh */
155   unsigned char data[1];
156 } RpmHead;
157
158
159 static inline unsigned char *
160 headfindtag(RpmHead *h, int tag)
161 {
162   unsigned int i;
163   unsigned char *d, taga[4];
164   d = h->dp - 16;
165   taga[0] = tag >> 24;
166   taga[1] = tag >> 16;
167   taga[2] = tag >> 8;
168   taga[3] = tag;
169   for (i = 0; i < h->cnt; i++, d -= 16)
170     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
171       return d;
172   return 0;
173 }
174
175 static int
176 headexists(RpmHead *h, int tag)
177 {
178   return headfindtag(h, tag) ? 1 : 0;
179 }
180
181 static unsigned int *
182 headint32array(RpmHead *h, int tag, int *cnt)
183 {
184   unsigned int i, o, *r;
185   unsigned char *d = headfindtag(h, tag);
186
187   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
188     return 0;
189   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
190   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
191   if (o + 4 * i > h->dcnt)
192     return 0;
193   d = h->dp + o;
194   r = solv_calloc(i ? i : 1, sizeof(unsigned int));
195   if (cnt)
196     *cnt = i;
197   for (o = 0; o < i; o++, d += 4)
198     r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
199   return r;
200 }
201
202 /* returns the first entry of an integer array */
203 static unsigned int
204 headint32(RpmHead *h, int tag)
205 {
206   unsigned int i, o;
207   unsigned char *d = headfindtag(h, tag);
208
209   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
210     return 0;
211   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
212   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
213   if (i == 0 || o + 4 * i > h->dcnt)
214     return 0;
215   d = h->dp + o;
216   return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
217 }
218
219 /* returns the first entry of an 64bit integer array */
220 static unsigned long long
221 headint64(RpmHead *h, int tag)
222 {
223   unsigned int i, o;
224   unsigned char *d = headfindtag(h, tag);
225   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
226     return 0;
227   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
228   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
229   if (i == 0 || o + 8 * i > h->dcnt)
230     return 0;
231   d = h->dp + o;
232   i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
233   return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
234 }
235
236 static unsigned int *
237 headint16array(RpmHead *h, int tag, int *cnt)
238 {
239   unsigned int i, o, *r;
240   unsigned char *d = headfindtag(h, tag);
241
242   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
243     return 0;
244   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
245   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
246   if (o + 4 * i > h->dcnt)
247     return 0;
248   d = h->dp + o;
249   r = solv_calloc(i ? i : 1, sizeof(unsigned int));
250   if (cnt)
251     *cnt = i;
252   for (o = 0; o < i; o++, d += 2)
253     r[o] = d[0] << 8 | d[1];
254   return r;
255 }
256
257 static char *
258 headstring(RpmHead *h, int tag)
259 {
260   unsigned int o;
261   unsigned char *d = headfindtag(h, tag);
262   /* 6: STRING, 9: I18NSTRING */
263   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
264     return 0;
265   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
266   if (o >= h->dcnt)
267     return 0;
268   return (char *)h->dp + o;
269 }
270
271 static char **
272 headstringarray(RpmHead *h, int tag, int *cnt)
273 {
274   unsigned int i, o;
275   unsigned char *d = headfindtag(h, tag);
276   char **r;
277
278   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
279     return 0;
280   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
281   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
282   r = solv_calloc(i ? i : 1, sizeof(char *));
283   if (cnt)
284     *cnt = i;
285   d = h->dp + o;
286   for (o = 0; o < i; o++)
287     {
288       r[o] = (char *)d;
289       if (o + 1 < i)
290         d += strlen((char *)d) + 1;
291       if (d >= h->dp + h->dcnt)
292         {
293           solv_free(r);
294           return 0;
295         }
296     }
297   return r;
298 }
299
300 static unsigned char *
301 headbinary(RpmHead *h, int tag, unsigned int *sizep)
302 {
303   unsigned int i, o;
304   unsigned char *d = headfindtag(h, tag);
305   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
306     return 0;
307   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
308   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
309   if (o > h->dcnt || o + i < o || o + i > h->dcnt)
310     return 0;
311   if (sizep)
312     *sizep = i;
313   return h->dp + o;
314 }
315
316 static char *headtoevr(RpmHead *h)
317 {
318   unsigned int epoch;
319   char *version, *v;
320   char *release;
321   char *evr;
322   char *distepoch;
323
324   version  = headstring(h, TAG_VERSION);
325   release  = headstring(h, TAG_RELEASE);
326   epoch = headint32(h, TAG_EPOCH);
327   if (!version || !release)
328     {
329       fprintf(stderr, "headtoevr: bad rpm header\n");
330       return 0;
331     }
332   for (v = version; *v >= '0' && *v <= '9'; v++)
333     ;
334   if (epoch || (v != version && *v == ':'))
335     {
336       char epochbuf[11];        /* 32bit decimal will fit in */
337       sprintf(epochbuf, "%u", epoch);
338       evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
339       sprintf(evr, "%s:%s-%s", epochbuf, version, release);
340     }
341   else
342     {
343       evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
344       sprintf(evr, "%s-%s", version, release);
345     }
346   distepoch = headstring(h, TAG_DISTEPOCH);
347   if (distepoch && *distepoch)
348     {
349       int l = strlen(evr);
350       evr = solv_realloc(evr, l + strlen(distepoch) + 2);
351       evr[l++] = ':';
352       strcpy(evr + l, distepoch);
353     }
354   return evr;
355 }
356
357
358 static void
359 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
360 {
361   const unsigned char *cp;
362   int state = 0;
363   int c;
364   unsigned char *buf = 0, *bp;
365
366   /* check if it's already utf8, code taken from screen ;-) */
367   cp = (const unsigned char *)str;
368   while ((c = *cp++) != 0)
369     {
370       if (state)
371         {
372           if ((c & 0xc0) != 0x80)
373             break; /* encoding error */
374           c = (c & 0x3f) | (state << 6);
375           if (!(state & 0x40000000))
376             {
377               /* check for overlong sequences */
378               if ((c & 0x820823e0) == 0x80000000)
379                 c = 0xfdffffff;
380               else if ((c & 0x020821f0) == 0x02000000)
381                 c = 0xfff7ffff;
382               else if ((c & 0x000820f8) == 0x00080000)
383                 c = 0xffffd000;
384               else if ((c & 0x0000207c) == 0x00002000)
385                 c = 0xffffff70;
386             }
387         }
388       else
389         {
390           /* new sequence */
391           if (c >= 0xfe)
392             break;
393           else if (c >= 0xfc)
394             c = (c & 0x01) | 0xbffffffc;    /* 5 bytes to follow */
395           else if (c >= 0xf8)
396             c = (c & 0x03) | 0xbfffff00;    /* 4 */
397           else if (c >= 0xf0)
398             c = (c & 0x07) | 0xbfffc000;    /* 3 */
399           else if (c >= 0xe0)
400             c = (c & 0x0f) | 0xbff00000;    /* 2 */
401           else if (c >= 0xc2)
402             c = (c & 0x1f) | 0xfc000000;    /* 1 */
403           else if (c >= 0x80)
404             break;
405         }
406       state = (c & 0x80000000) ? c : 0;
407     }
408   if (c)
409     {
410       /* not utf8, assume latin1 */
411       buf = solv_malloc(2 * strlen(str) + 1);
412       cp = (const unsigned char *)str;
413       str = (char *)buf;
414       bp = buf;
415       while ((c = *cp++) != 0)
416         {
417           if (c >= 0xc0)
418             {
419               *bp++ = 0xc3;
420               c ^= 0x80;
421             }
422           else if (c >= 0x80)
423             *bp++ = 0xc2;
424           *bp++ = c;
425         }
426       *bp++ = 0;
427     }
428   repodata_set_str(repodata, handle, tag, str);
429   if (buf)
430     solv_free(buf);
431 }
432
433
434 #define MAKEDEPS_FILTER_WEAK    (1 << 0)
435 #define MAKEDEPS_FILTER_STRONG  (1 << 1)
436 #define MAKEDEPS_NO_RPMLIB      (1 << 2)
437
438 /*
439  * strong: 0: ignore strongness
440  *         1: filter to strong
441  *         2: filter to weak
442  */
443 static unsigned int
444 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
445 {
446   char **n, **v;
447   unsigned int *f;
448   int i, cc, nc, vc, fc;
449   int haspre;
450   unsigned int olddeps;
451   Id *ida;
452   int strong;
453
454   strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
455   n = headstringarray(rpmhead, tagn, &nc);
456   if (!n || !nc)
457     return 0;
458   vc = fc = 0;
459   v = headstringarray(rpmhead, tagv, &vc);
460   f = headint32array(rpmhead, tagf, &fc);
461   if (!v || !f || nc != vc || nc != fc)
462     {
463       char *pkgname = rpm_query(rpmhead, 0);
464       fprintf(stderr, "bad dependency entries for %s: %d %d %d\n", pkgname ? pkgname : "<NULL>", nc, vc, fc);
465       solv_free(pkgname);
466       solv_free(n);
467       solv_free(v);
468       solv_free(f);
469       return 0;
470     }
471
472   cc = nc;
473   haspre = 0;   /* add no prereq marker */
474   if (flags)
475     {
476       /* we do filtering */
477       cc = 0;
478       for (i = 0; i < nc; i++)
479         {
480           if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
481             continue;
482           if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
483             if (!strncmp(n[i], "rpmlib(", 7))
484               continue;
485           if ((f[i] & DEP_PRE) != 0)
486             haspre = 1;
487           cc++;
488         }
489     }
490   else if (tagn == TAG_REQUIRENAME)
491     {
492       /* no filtering, just look for the first prereq */
493       for (i = 0; i < nc; i++)
494         if ((f[i] & DEP_PRE) != 0)
495           {
496             haspre = 1;
497             break;
498           }
499     }
500   if (cc == 0)
501     {
502       solv_free(n);
503       solv_free(v);
504       solv_free(f);
505       return 0;
506     }
507   cc += haspre;
508   olddeps = repo_reserve_ids(repo, 0, cc);
509   ida = repo->idarraydata + olddeps;
510   for (i = 0; ; i++)
511     {
512       if (i == nc)
513         {
514           if (haspre != 1)
515             break;
516           haspre = 2;   /* pass two: prereqs */
517           i = 0;
518           *ida++ = SOLVABLE_PREREQMARKER;
519         }
520       if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
521         continue;
522       if (haspre == 1 && (f[i] & DEP_PRE) != 0)
523         continue;
524       if (haspre == 2 && (f[i] & DEP_PRE) == 0)
525         continue;
526       if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
527         if (!strncmp(n[i], "rpmlib(", 7))
528           continue;
529       if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
530         {
531           Id name, evr;
532           int flags = 0;
533           if ((f[i] & DEP_LESS) != 0)
534             flags |= 4;
535           if ((f[i] & DEP_EQUAL) != 0)
536             flags |= 2;
537           if ((f[i] & DEP_GREATER) != 0)
538             flags |= 1;
539           name = pool_str2id(pool, n[i], 1);
540           if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
541             evr = pool_str2id(pool, v[i] + 2, 1);
542           else
543             evr = pool_str2id(pool, v[i], 1);
544           *ida++ = pool_rel2id(pool, name, evr, flags, 1);
545         }
546       else
547         *ida++ = pool_str2id(pool, n[i], 1);
548     }
549   *ida++ = 0;
550   repo->idarraysize += cc + 1;
551   solv_free(n);
552   solv_free(v);
553   solv_free(f);
554   return olddeps;
555 }
556
557
558 #ifdef USE_FILEFILTER
559
560 #define FILEFILTER_EXACT    0
561 #define FILEFILTER_STARTS   1
562 #define FILEFILTER_CONTAINS 2
563
564 struct filefilter {
565   int dirmatch;
566   char *dir;
567   char *base;
568 };
569
570 static struct filefilter filefilters[] = {
571   { FILEFILTER_CONTAINS, "/bin/", 0},
572   { FILEFILTER_CONTAINS, "/sbin/", 0},
573   { FILEFILTER_CONTAINS, "/lib/", 0},
574   { FILEFILTER_CONTAINS, "/lib64/", 0},
575   { FILEFILTER_CONTAINS, "/etc/", 0},
576   { FILEFILTER_STARTS, "/usr/games/", 0},
577   { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
578   { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
579   { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
580 };
581
582 #endif
583
584 static void
585 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
586 {
587   Id handle, did;
588   int i, fszc;
589   unsigned int *fkb, *fn, *fsz, *fm, *fino;
590   unsigned int inotest[256], inotestok;
591
592   if (!fc)
593     return;
594   /* XXX: use TAG_LONGFILESIZES if available */
595   fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
596   if (!fsz || fc != fszc)
597     {
598       solv_free(fsz);
599       return;
600     }
601   /* stupid rpm records sizes of directories, so we have to check the mode */
602   fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
603   if (!fm || fc != fszc)
604     {
605       solv_free(fsz);
606       solv_free(fm);
607       return;
608     }
609   fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
610   if (!fino || fc != fszc)
611     {
612       solv_free(fsz);
613       solv_free(fm);
614       solv_free(fino);
615       return;
616     }
617   inotestok = 0;
618   if (fc < sizeof(inotest))
619     {
620       memset(inotest, 0, sizeof(inotest));
621       for (i = 0; i < fc; i++)
622         {
623           int off, bit;
624           if (fsz[i] == 0 || !S_ISREG(fm[i]))
625             continue;
626           off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
627           bit = 1 << (fino[i] & 31);
628           if ((inotest[off] & bit) != 0)
629             break;
630           inotest[off] |= bit;
631         }
632       if (i == fc)
633         inotestok = 1;
634     }
635   if (!inotestok)
636     {
637       unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
638       unsigned int *fx, j;
639       unsigned int mask, hash, hh;
640       if (!fdev || fc != fszc)
641         {
642           solv_free(fsz);
643           solv_free(fm);
644           solv_free(fdev);
645           solv_free(fino);
646           return;
647         }
648       mask = fc;
649       while ((mask & (mask - 1)) != 0)
650         mask = mask & (mask - 1);
651       mask <<= 2;
652       if (mask > sizeof(inotest)/sizeof(*inotest))
653         fx = solv_calloc(mask, sizeof(unsigned int));
654       else
655         {
656           fx = inotest;
657           memset(fx, 0, mask * sizeof(unsigned int));
658         }
659       mask--;
660       for (i = 0; i < fc; i++)
661         {
662           if (fsz[i] == 0 || !S_ISREG(fm[i]))
663             continue;
664           hash = (fino[i] + fdev[i] * 31) & mask;
665           hh = 7;
666           while ((j = fx[hash]) != 0)
667             {
668               if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
669                 {
670                   fsz[i] = 0;   /* kill entry */
671                   break;
672                 }
673               hash = (hash + hh++) & mask;
674             }
675           if (!j)
676             fx[hash] = i + 1;
677         }
678       if (fx != inotest)
679         solv_free(fx);
680       solv_free(fdev);
681     }
682   solv_free(fino);
683   fn = solv_calloc(dc, sizeof(unsigned int));
684   fkb = solv_calloc(dc, sizeof(unsigned int));
685   for (i = 0; i < fc; i++)
686     {
687       if (di[i] >= dc)
688         continue;
689       fn[di[i]]++;
690       if (fsz[i] == 0 || !S_ISREG(fm[i]))
691         continue;
692       fkb[di[i]] += fsz[i] / 1024 + 1;
693     }
694   solv_free(fsz);
695   solv_free(fm);
696   /* commit */
697   handle = s - pool->solvables;
698   for (i = 0; i < dc; i++)
699     {
700       if (!fn[i])
701         continue;
702       if (!*dn[i])
703         {
704           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
705             did = repodata_str2dir(data, "/usr/src", 1);
706           else
707             continue;   /* work around rpm bug */
708         }
709       else
710         did = repodata_str2dir(data, dn[i], 1);
711       repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
712     }
713   solv_free(fn);
714   solv_free(fkb);
715 }
716
717 /* assumes last processed array is provides! */
718 static unsigned int
719 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
720 {
721   char **bn;
722   char **dn;
723   unsigned int *di;
724   int bnc, dnc, dic;
725   int i;
726   Id lastdid = 0;
727   int lastdii = -1;
728 #ifdef USE_FILEFILTER
729   int j;
730   struct filefilter *ff;
731 #endif
732 #if 0
733   char *fn = 0;
734   int fna = 0;
735 #endif
736
737   if (!data)
738     return olddeps;
739   bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
740   if (!bn)
741     return olddeps;
742   dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
743   if (!dn)
744     {
745       solv_free(bn);
746       return olddeps;
747     }
748   di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
749   if (!di)
750     {
751       solv_free(bn);
752       solv_free(dn);
753       return olddeps;
754     }
755   if (bnc != dic)
756     {
757       fprintf(stderr, "bad filelist\n");
758       return olddeps;
759     }
760
761   if (data)
762     adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
763
764   for (i = 0; i < bnc; i++)
765     {
766 #ifdef USE_FILEFILTER
767       ff = filefilters;
768       for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
769         {
770           if (ff->dir)
771             {
772               switch (ff->dirmatch)
773                 {
774                 case FILEFILTER_STARTS:
775                   if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
776                     continue;
777                   break;
778                 case FILEFILTER_CONTAINS:
779                   if (!strstr(dn[di[i]], ff->dir))
780                     continue;
781                   break;
782                 case FILEFILTER_EXACT:
783                 default:
784                   if (strcmp(dn[di[i]], ff->dir))
785                     continue;
786                   break;
787                 }
788             }
789           if (ff->base)
790             {
791               if (strcmp(bn[i], ff->base))
792                 continue;
793             }
794           break;
795         }
796       if (j == sizeof(filefilters)/sizeof(*filefilters))
797         continue;
798 #endif
799 #if 0
800       j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
801       if (j > fna)
802         {
803           fna = j + 256;
804           fn = solv_realloc(fn, fna);
805         }
806       strcpy(fn, dn[di[i]]);
807       strcat(fn, bn[i]);
808       olddeps = repo_addid_dep(repo, olddeps, pool_str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
809 #endif
810       if (data)
811         {
812           Id did;
813           char *b = bn[i];
814
815           if (di[i] == lastdii)
816             did = lastdid;
817           else
818             {
819               did = repodata_str2dir(data, dn[di[i]], 1);
820               if (!did)
821                 did = repodata_str2dir(data, "/", 1);
822               lastdid = did;
823               lastdii = di[i];
824             }
825           if (b && *b == '/')   /* work around rpm bug */
826             b++;
827           repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, b);
828         }
829     }
830 #if 0
831   if (fn)
832     solv_free(fn);
833 #endif
834   solv_free(bn);
835   solv_free(dn);
836   solv_free(di);
837   return olddeps;
838 }
839
840 static void
841 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
842 {
843   char **cn;
844   char **cx;
845   unsigned int *ct;
846   int i, cnc, cxc, ctc;
847   Queue hq;
848
849   ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
850   cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
851   cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
852   if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
853     {
854       solv_free(ct);
855       solv_free(cx);
856       solv_free(cn);
857       return;
858     }
859   queue_init(&hq);
860   for (i = 0; i < ctc; i++)
861     {
862       Id h = repodata_new_handle(data);
863       if (ct[i])
864         repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
865       if (cn[i])
866         repodata_set_str(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
867       if (cx[i])
868         repodata_set_str(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
869       queue_push(&hq, h);
870     }
871   for (i = 0; i < hq.count; i++)
872     repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
873   queue_free(&hq);
874   solv_free(ct);
875   solv_free(cx);
876   solv_free(cn);
877 }
878
879
880 static int
881 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
882 {
883   char *name;
884   char *evr;
885   char *sourcerpm;
886
887   name = headstring(rpmhead, TAG_NAME);
888   if (!name)
889     {
890       fprintf(stderr, "package has no name\n");
891       return 0;
892     }
893   if (!strcmp(name, "gpg-pubkey"))
894     return 0;
895   s->name = pool_str2id(pool, name, 1);
896   sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
897   if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
898     s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
899   else
900     {
901       if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
902         s->arch = ARCH_NOSRC;
903       else
904         s->arch = ARCH_SRC;
905     }
906   if (!s->arch)
907     s->arch = ARCH_NOARCH;
908   evr = headtoevr(rpmhead);
909   s->evr = pool_str2id(pool, evr, 1);
910   s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
911
912   s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
913   if ((flags & RPM_ADD_NO_FILELIST) == 0)
914     s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
915   if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
916     s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
917   s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
918   s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
919   s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
920
921   s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
922   s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
923   s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
924   s->enhances  = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
925   s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
926   s->conflicts = repo_fix_conflicts(repo, s->conflicts);
927
928   if (data)
929     {
930       Id handle;
931       char *str;
932       unsigned int u32;
933       unsigned long long u64;
934
935       handle = s - pool->solvables;
936       str = headstring(rpmhead, TAG_SUMMARY);
937       if (str)
938         setutf8string(data, handle, SOLVABLE_SUMMARY, str);
939       str = headstring(rpmhead, TAG_DESCRIPTION);
940       if (str)
941         {
942           char *aut, *p;
943           for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
944             if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
945               break;
946           if (aut)
947             {
948               /* oh my, found SUSE special author section */
949               int l = aut - str;
950               str = solv_strdup(str);
951               aut = str + l;
952               str[l] = 0;
953               while (l > 0 && str[l - 1] == '\n')
954                 str[--l] = 0;
955               if (l)
956                 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
957               p = aut + 19;
958               aut = str;        /* copy over */
959               while (*p == ' ' || *p == '\n')
960                 p++;
961               while (*p)
962                 {
963                   if (*p == '\n')
964                     {
965                       *aut++ = *p++;
966                       while (*p == ' ')
967                         p++;
968                       continue;
969                     }
970                   *aut++ = *p++;
971                 }
972               while (aut != str && aut[-1] == '\n')
973                 aut--;
974               *aut = 0;
975               if (*str)
976                 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
977               free(str);
978             }
979           else if (*str)
980             setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
981         }
982       str = headstring(rpmhead, TAG_GROUP);
983       if (str)
984         repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
985       str = headstring(rpmhead, TAG_LICENSE);
986       if (str)
987         repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
988       str = headstring(rpmhead, TAG_URL);
989       if (str)
990         repodata_set_str(data, handle, SOLVABLE_URL, str);
991       str = headstring(rpmhead, TAG_DISTRIBUTION);
992       if (str)
993         repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
994       str = headstring(rpmhead, TAG_PACKAGER);
995       if (str)
996         repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
997       if ((flags & RPM_ADD_WITH_PKGID) != 0)
998         {
999           unsigned char *chksum;
1000           unsigned int chksumsize;
1001           chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
1002           if (chksum && chksumsize == 16)
1003             repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
1004         }
1005       if ((flags & RPM_ADD_WITH_HDRID) != 0)
1006         {
1007           str = headstring(rpmhead, TAG_SHA1HEADER);
1008           if (str && strlen(str) == 40)
1009             repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
1010           else if (str && strlen(str) == 64)
1011             repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
1012         }
1013       u32 = headint32(rpmhead, TAG_BUILDTIME);
1014       if (u32)
1015         repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1016       u32 = headint32(rpmhead, TAG_INSTALLTIME);
1017       if (u32)
1018         repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1019       u64 = headint64(rpmhead, TAG_LONGSIZE);
1020       if (u64)
1021         repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1022       else
1023         {
1024           u32 = headint32(rpmhead, TAG_SIZE);
1025           if (u32)
1026             repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1027         }
1028       if (sourcerpm)
1029         repodata_set_sourcepkg(data, handle, sourcerpm);
1030       if ((flags & RPM_ADD_TRIGGERS) != 0)
1031         {
1032           Id id, lastid;
1033           unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1034
1035           lastid = 0;
1036           for (; (id = repo->idarraydata[ida]) != 0; ida++)
1037             {
1038               /* we currently do not support rel ids in incore data, so
1039                * strip off versioning information */
1040               while (ISRELDEP(id))
1041                 {
1042                   Reldep *rd = GETRELDEP(pool, id);
1043                   id = rd->name;
1044                 }
1045               if (id == lastid)
1046                 continue;
1047               repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1048               lastid = id;
1049             }
1050         }
1051       if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1052         addchangelog(data, handle, rpmhead);
1053     }
1054   solv_free(evr);
1055   return 1;
1056 }
1057
1058 static Id
1059 copyreldep(Pool *pool, Pool *frompool, Id id)
1060 {
1061   Reldep *rd = GETRELDEP(frompool, id);
1062   Id name = rd->name, evr = rd->evr;
1063   if (ISRELDEP(name))
1064     name = copyreldep(pool, frompool, name);
1065   else
1066     name = pool_str2id(pool, pool_id2str(frompool, name), 1);
1067   if (ISRELDEP(evr))
1068     evr = copyreldep(pool, frompool, evr);
1069   else
1070     evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1071   return pool_rel2id(pool, name, evr, rd->flags, 1);
1072 }
1073
1074 static Offset
1075 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1076 {
1077   int cc;
1078   Id id, *ida, *from;
1079   Offset ido;
1080   Pool *frompool = fromrepo->pool;
1081
1082   if (!fromoff)
1083     return 0;
1084   from = fromrepo->idarraydata + fromoff;
1085   for (ida = from, cc = 0; *ida; ida++, cc++)
1086     ;
1087   if (cc == 0)
1088     return 0;
1089   ido = repo_reserve_ids(repo, 0, cc);
1090   ida = repo->idarraydata + ido;
1091   if (frompool && pool != frompool)
1092     {
1093       while (*from)
1094         {
1095           id = *from++;
1096           if (ISRELDEP(id))
1097             id = copyreldep(pool, frompool, id);
1098           else
1099             id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1100           *ida++ = id;
1101         }
1102       *ida = 0;
1103     }
1104   else
1105     memcpy(ida, from, (cc + 1) * sizeof(Id));
1106   repo->idarraysize += cc + 1;
1107   return ido;
1108 }
1109
1110 #define COPYDIR_DIRCACHE_SIZE 512
1111
1112 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1113
1114 static inline Id
1115 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1116 {
1117   if (cache && cache[did & 255] == did)
1118     return cache[(did & 255) + 256];
1119   return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1120 }
1121
1122 static Id
1123 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1124 {
1125   Id parent = dirpool_parent(&fromdata->dirpool, did);
1126   Id compid = dirpool_compid(&fromdata->dirpool, did);
1127   if (parent)
1128     parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1129   if (fromspool != &pool->ss)
1130     compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1131   compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1132   if (cache)
1133     {
1134       cache[did & 255] = did;
1135       cache[(did & 255) + 256] = compid;
1136     }
1137   return compid;
1138 }
1139
1140 struct solvable_copy_cbdata {
1141   Repodata *data;
1142   Id handle;
1143   Id subhandle;
1144   Id *dircache;
1145 };
1146
1147 static int
1148 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1149 {
1150   struct solvable_copy_cbdata *cbdata = vcbdata;
1151   Id id, keyname;
1152   Repodata *data = cbdata->data;
1153   Id handle = cbdata->handle;
1154   Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1155   Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1156
1157   keyname = key->name;
1158   if (keyname >= ID_NUM_INTERNAL && pool != frompool)
1159     keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1160   switch(key->type)
1161     {
1162     case REPOKEY_TYPE_ID:
1163     case REPOKEY_TYPE_CONSTANTID:
1164     case REPOKEY_TYPE_IDARRAY:  /* used for triggers */
1165       id = kv->id;
1166       assert(!data->localpool); /* implement me! */
1167       if (pool != frompool || fromdata->localpool)
1168         {
1169           if (ISRELDEP(id))
1170             id = copyreldep(pool, frompool, id);
1171           else
1172             id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1173         }
1174       if (key->type == REPOKEY_TYPE_ID)
1175         repodata_set_id(data, handle, keyname, id);
1176       else if (key->type == REPOKEY_TYPE_CONSTANTID)
1177         repodata_set_constantid(data, handle, keyname, id);
1178       else
1179         repodata_add_idarray(data, handle, keyname, id);
1180       break;
1181     case REPOKEY_TYPE_STR:
1182       repodata_set_str(data, handle, keyname, kv->str);
1183       break;
1184     case REPOKEY_TYPE_VOID:
1185       repodata_set_void(data, handle, keyname);
1186       break;
1187     case REPOKEY_TYPE_NUM:
1188       repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1189       break;
1190     case REPOKEY_TYPE_CONSTANT:
1191       repodata_set_constant(data, handle, keyname, kv->num);
1192       break;
1193     case REPOKEY_TYPE_DIRNUMNUMARRAY:
1194       id = kv->id;
1195       assert(!data->localpool); /* implement me! */
1196       id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1197       repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1198       break;
1199     case REPOKEY_TYPE_DIRSTRARRAY:
1200       id = kv->id;
1201       assert(!data->localpool); /* implement me! */
1202       id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1203       repodata_add_dirstr(data, handle, keyname, id, kv->str);
1204       break;
1205     case REPOKEY_TYPE_FLEXARRAY:
1206       if (kv->eof == 2)
1207         {
1208           assert(cbdata->subhandle);
1209           cbdata->handle = cbdata->subhandle;
1210           cbdata->subhandle = 0;
1211           break;
1212         }
1213       if (!kv->entry)
1214         {
1215           assert(!cbdata->subhandle);
1216           cbdata->subhandle = cbdata->handle;
1217         }
1218       cbdata->handle = repodata_new_handle(data);
1219       repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1220       break;
1221     default:
1222       break;
1223     }
1224   return 0;
1225 }
1226
1227 static void
1228 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1229 {
1230   Repo *repo = s->repo;
1231   Repo *fromrepo = r->repo;
1232   Pool *pool = repo->pool;
1233   struct solvable_copy_cbdata cbdata;
1234
1235   /* copy solvable data */
1236   if (pool == fromrepo->pool)
1237     {
1238       s->name = r->name;
1239       s->evr = r->evr;
1240       s->arch = r->arch;
1241       s->vendor = r->vendor;
1242     }
1243   else
1244     {
1245       if (r->name)
1246         s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1247       if (r->evr)
1248         s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1249       if (r->arch)
1250         s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1251       if (r->vendor)
1252         s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1253     }
1254   s->provides = copydeps(pool, repo, r->provides, fromrepo);
1255   s->requires = copydeps(pool, repo, r->requires, fromrepo);
1256   s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1257   s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1258   s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1259   s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1260   s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1261   s->enhances  = copydeps(pool, repo, r->enhances, fromrepo);
1262
1263   /* copy all attributes */
1264   if (!data)
1265     return;
1266   cbdata.data = data;
1267   cbdata.handle = s - pool->solvables;
1268   cbdata.subhandle = 0;
1269   cbdata.dircache = dircache;
1270   repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1271 }
1272
1273 /* used to sort entries returned in some database order */
1274 static int
1275 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1276 {
1277   struct rpmid const *a = va, *b = vb;
1278   int r;
1279   r = strcmp(a->name, b->name);
1280   if (r)
1281     return r;
1282   return a->dbid - b->dbid;
1283 }
1284
1285 static int
1286 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1287 {
1288   Repo *repo = dp;
1289   Pool *pool = repo->pool;
1290   Solvable *a = pool->solvables + *(Id *)va;
1291   Solvable *b = pool->solvables + *(Id *)vb;
1292   Id *rpmdbid;
1293
1294   if (a->name != b->name)
1295     return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1296   rpmdbid = repo->rpmdbid;
1297   return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1298 }
1299
1300 static void
1301 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1302 {
1303   Pool *pool = repo->pool;
1304   Solvable tmp;
1305
1306   tmp = pool->solvables[pa];
1307   pool->solvables[pa] = pool->solvables[pb];
1308   pool->solvables[pb] = tmp;
1309   if (repo->rpmdbid)
1310     {
1311       Id tmpid = repo->rpmdbid[pa - repo->start];
1312       repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1313       repo->rpmdbid[pb - repo->start] = tmpid;
1314     }
1315   /* only works if nothing is already internalized! */
1316   if (data)
1317     repodata_swap_attrs(data, pa, pb);
1318 }
1319
1320
1321 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1322 {
1323 #ifdef RPM5
1324   return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1325 #else
1326 # if defined(WORDS_BIGENDIAN)
1327   if (!byteswapped)
1328 # else
1329   if (byteswapped)
1330 # endif
1331     return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1332   else
1333     return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1334 #endif
1335 }
1336
1337 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1338 {
1339 #ifdef RPM5
1340   db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1341 #else
1342 # if defined(WORDS_BIGENDIAN)
1343   if (!byteswapped)
1344 # else
1345   if (byteswapped)
1346 # endif
1347     db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1348   else
1349     db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1350 #endif
1351 }
1352
1353 static void
1354 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1355 {
1356   memset(cookie, 0, 32);
1357   cookie[3] = RPMDB_COOKIE_VERSION;
1358   memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1359   memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1360 }
1361
1362 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1363 static DB_ENV *
1364 opendbenv(const char *rootdir)
1365 {
1366   char dbpath[PATH_MAX];
1367   DB_ENV *dbenv = 0;
1368   int r;
1369
1370   if (db_env_create(&dbenv, 0))
1371     {
1372       perror("db_env_create");
1373       return 0;
1374     }
1375 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1376   dbenv->set_thread_count(dbenv, 8);
1377 #endif
1378   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1379   if (access(dbpath, W_OK) == -1)
1380     {
1381       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1382     }
1383   else
1384     {
1385 #ifdef FEDORA
1386       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1387 #else
1388       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1389 #endif
1390     }
1391   if (r)
1392     {
1393       perror("dbenv open");
1394       dbenv->close(dbenv, 0);
1395       return 0;
1396     }
1397   return dbenv;
1398 }
1399
1400
1401 static int
1402 count_headers(const char *rootdir, DB_ENV *dbenv)
1403 {
1404   char dbpath[PATH_MAX];
1405   struct stat statbuf;
1406   DB *db = 0;
1407   DBC *dbc = 0;
1408   int count = 0;
1409   DBT dbkey;
1410   DBT dbdata;
1411
1412   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1413   if (stat(dbpath, &statbuf))
1414     return 0;
1415   memset(&dbkey, 0, sizeof(dbkey));
1416   memset(&dbdata, 0, sizeof(dbdata));
1417   if (db_create(&db, dbenv, 0))
1418     {
1419       perror("db_create");
1420       return 0;
1421     }
1422   if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1423     {
1424       perror("db->open Name index");
1425       return 0;
1426     }
1427   if (db->cursor(db, NULL, &dbc, 0))
1428     {
1429       perror("db->cursor");
1430       return 0;
1431     }
1432   while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1433     count += dbdata.size / RPM_INDEX_SIZE;
1434   dbc->c_close(dbc);
1435   db->close(db, 0);
1436   return count;
1437 }
1438
1439 /*
1440  * read rpm db as repo
1441  *
1442  */
1443
1444 int
1445 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1446 {
1447   Pool *pool = repo->pool;
1448   unsigned char buf[16];
1449   DB *db = 0;
1450   DBC *dbc = 0;
1451   int byteswapped;
1452   unsigned int dbid;
1453   unsigned char *dp;
1454   int dl, nrpmids;
1455   struct rpmid *rpmids, *rp;
1456   int i;
1457   int rpmheadsize;
1458   RpmHead *rpmhead;
1459   Solvable *s;
1460   Id id, *refhash;
1461   unsigned int refmask, h;
1462   char dbpath[PATH_MAX];
1463   DB_ENV *dbenv = 0;
1464   DBT dbkey;
1465   DBT dbdata;
1466   struct stat packagesstat;
1467   unsigned char newcookie[32];
1468   const unsigned char *oldcookie = 0;
1469   Id oldcookietype = 0;
1470   Repodata *data;
1471   int count = 0, done = 0;
1472   unsigned int now;
1473   const char *rootdir = 0;
1474
1475   now = solv_timems(0);
1476   memset(&dbkey, 0, sizeof(dbkey));
1477   memset(&dbdata, 0, sizeof(dbdata));
1478
1479   data = repo_add_repodata(repo, flags);
1480
1481   if (ref && !(ref->nsolvables && ref->rpmdbid))
1482     ref = 0;
1483
1484   if (flags & REPO_USE_ROOTDIR)
1485     rootdir = pool_get_rootdir(pool);
1486   if (!(dbenv = opendbenv(rootdir)))
1487     {
1488       return pool_error(pool, -1, "repo_add_rpmdb: opendbenv failed");
1489     }
1490
1491   /* XXX: should get ro lock of Packages database! */
1492   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1493   if (stat(dbpath, &packagesstat))
1494     {
1495       pool_error(pool, -1, "repo_add_rpmdb: %s: %s", dbpath, strerror(errno));
1496       dbenv->close(dbenv, 0);
1497       return -1;
1498     }
1499   mkrpmdbcookie(&packagesstat, newcookie);
1500   repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1501
1502   if (ref)
1503     oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1504   if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1505     {
1506       Id *pkgids;
1507       int solvstart = 0, solvend = 0;
1508
1509       if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1510         count = count_headers(rootdir, dbenv);
1511       if (db_create(&db, dbenv, 0))
1512         {
1513           pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1514           dbenv->close(dbenv, 0);
1515           return -1;
1516         }
1517       if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1518         {
1519           pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1520           db->close(db, 0);
1521           dbenv->close(dbenv, 0);
1522           return -1;
1523         }
1524       if (db->get_byteswapped(db, &byteswapped))
1525         {
1526           db->close(db, 0);
1527           dbenv->close(dbenv, 0);
1528           return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1529         }
1530       if (db->cursor(db, NULL, &dbc, 0))
1531         {
1532           db->close(db, 0);
1533           dbenv->close(dbenv, 0);
1534           return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1535         }
1536       rpmheadsize = 0;
1537       rpmhead = 0;
1538       i = 0;
1539       s = 0;
1540       while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1541         {
1542           if (!s)
1543             {
1544               s = pool_id2solvable(pool, repo_add_solvable(repo));
1545               if (!solvstart)
1546                 solvstart = s - pool->solvables;
1547               solvend = s - pool->solvables + 1;
1548             }
1549           if (!repo->rpmdbid)
1550             repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1551           if (dbkey.size != 4)
1552             {
1553               dbc->c_close(dbc);
1554               db->close(db, 0);
1555               dbenv->close(dbenv, 0);
1556               return pool_error(pool, -1, "corrupt Packages database (key size)");
1557             }
1558           dbid = db2rpmdbid(dbkey.data, byteswapped);
1559           if (dbid == 0)                /* the join key */
1560             continue;
1561           if (dbdata.size < 8)
1562             {
1563               dbc->c_close(dbc);
1564               db->close(db, 0);
1565               dbenv->close(dbenv, 0);
1566               return pool_error(pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1567             }
1568           if (dbdata.size > rpmheadsize)
1569             {
1570               rpmheadsize = dbdata.size + 128;
1571               rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1572             }
1573           memcpy(buf, dbdata.data, 8);
1574           rpmhead->forcebinary = 1;
1575           rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1576           rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1577           if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1578             {
1579               dbc->c_close(dbc);
1580               db->close(db, 0);
1581               dbenv->close(dbenv, 0);
1582               return pool_error(pool, -1, "corrupt rpm database (data size)\n");
1583             }
1584           memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1585           rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1586           repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1587           if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1588             {
1589               i++;
1590               s = 0;
1591             }
1592           else
1593             {
1594               /* We can reuse this solvable, but make sure it's still
1595                  associated with this repo.  */
1596               memset(s, 0, sizeof(*s));
1597               s->repo = repo;
1598             }
1599           if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1600             {
1601               if (done < count)
1602                 done++;
1603               if (done < count && (done - 1) * 100 / count != done * 100 / count)
1604                 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1605             }
1606         }
1607       if (s)
1608         {
1609           /* oops, could not reuse. free it instead */
1610           repo_free_solvable(repo, s - pool->solvables, 1);
1611           solvend--;
1612           s = 0;
1613         }
1614       dbc->c_close(dbc);
1615       db->close(db, 0);
1616       db = 0;
1617       /* now sort all solvables in the new solvstart..solvend block */
1618       if (solvend - solvstart > 1)
1619         {
1620           pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1621           for (i = solvstart; i < solvend; i++)
1622             pkgids[i - solvstart] = i;
1623           solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1624           /* adapt order */
1625           for (i = solvstart; i < solvend; i++)
1626             {
1627               int j = pkgids[i - solvstart];
1628               while (j < i)
1629                 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1630               if (j != i)
1631                 swap_solvables(repo, data, i, j);
1632             }
1633           solv_free(pkgids);
1634         }
1635     }
1636   else
1637     {
1638       Id dircache[COPYDIR_DIRCACHE_SIZE];               /* see copydir */
1639
1640       memset(dircache, 0, sizeof(dircache));
1641       if (db_create(&db, dbenv, 0))
1642         {
1643           pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1644           dbenv->close(dbenv, 0);
1645           return -1;
1646         }
1647       if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1648         {
1649           pool_error(pool, -1, "repo_add_rpmdb: db->open Name index failed: %s", strerror(errno));
1650           db->close(db, 0);
1651           dbenv->close(dbenv, 0);
1652           return -1;
1653         }
1654       if (db->get_byteswapped(db, &byteswapped))
1655         {
1656           db->close(db, 0);
1657           dbenv->close(dbenv, 0);
1658           return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1659         }
1660       if (db->cursor(db, NULL, &dbc, 0))
1661         {
1662           db->close(db, 0);
1663           dbenv->close(dbenv, 0);
1664           return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1665         }
1666       nrpmids = 0;
1667       rpmids = 0;
1668       while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1669         {
1670           if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1671             continue;
1672           dl = dbdata.size;
1673           dp = dbdata.data;
1674           while(dl >= RPM_INDEX_SIZE)
1675             {
1676               rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1677               rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1678               rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1679               memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1680               rpmids[nrpmids].name[(int)dbkey.size] = 0;
1681               nrpmids++;
1682               dp += RPM_INDEX_SIZE;
1683               dl -= RPM_INDEX_SIZE;
1684             }
1685         }
1686       dbc->c_close(dbc);
1687       db->close(db, 0);
1688       db = 0;
1689
1690       /* sort rpmids */
1691       solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1692
1693       rpmheadsize = 0;
1694       rpmhead = 0;
1695
1696       /* create hash from dbid to ref */
1697       refmask = mkmask(ref->nsolvables);
1698       refhash = solv_calloc(refmask + 1, sizeof(Id));
1699       for (i = 0; i < ref->end - ref->start; i++)
1700         {
1701           if (!ref->rpmdbid[i])
1702             continue;
1703           h = ref->rpmdbid[i] & refmask;
1704           while (refhash[h])
1705             h = (h + 317) & refmask;
1706           refhash[h] = i + 1;   /* make it non-zero */
1707         }
1708
1709       /* count the misses, they will cost us time */
1710       if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1711         {
1712           for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1713             {
1714               dbid = rp->dbid;
1715               if (refhash)
1716                 {
1717                   h = dbid & refmask;
1718                   while ((id = refhash[h]))
1719                     {
1720                       if (ref->rpmdbid[id - 1] == dbid)
1721                         break;
1722                       h = (h + 317) & refmask;
1723                     }
1724                   if (id)
1725                     continue;
1726                 }
1727               count++;
1728             }
1729         }
1730
1731       s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1732       if (!repo->rpmdbid)
1733         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1734
1735       for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1736         {
1737           dbid = rp->dbid;
1738           repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1739           if (refhash)
1740             {
1741               h = dbid & refmask;
1742               while ((id = refhash[h]))
1743                 {
1744                   if (ref->rpmdbid[id - 1] == dbid)
1745                     break;
1746                   h = (h + 317) & refmask;
1747                 }
1748               if (id)
1749                 {
1750                   Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1751                   if (r->repo == ref)
1752                     {
1753                       solvable_copy(s, r, data, dircache);
1754                       continue;
1755                     }
1756                 }
1757             }
1758           if (!db)
1759             {
1760               if (db_create(&db, dbenv, 0))
1761                 {
1762                   pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1763                   dbenv->close(dbenv, 0);
1764                   return -1;
1765                 }
1766               if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1767                 {
1768                   pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1769                   db->close(db, 0);
1770                   dbenv->close(dbenv, 0);
1771                   return -1;
1772                 }
1773               if (db->get_byteswapped(db, &byteswapped))
1774                 {
1775                   db->close(db, 0);
1776                   dbenv->close(dbenv, 0);
1777                   return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1778                 }
1779             }
1780           rpmdbid2db(buf, rp->dbid, byteswapped);
1781           dbkey.data = buf;
1782           dbkey.size = 4;
1783           dbdata.data = 0;
1784           dbdata.size = 0;
1785           if (db->get(db, NULL, &dbkey, &dbdata, 0))
1786             {
1787               db->close(db, 0);
1788               dbenv->close(dbenv, 0);
1789               return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1790             }
1791           if (dbdata.size < 8)
1792             {
1793               db->close(db, 0);
1794               dbenv->close(dbenv, 0);
1795               return pool_error(pool, -1, "corrupt Packages database (size)");
1796             }
1797           if (dbdata.size > rpmheadsize)
1798             {
1799               rpmheadsize = dbdata.size + 128;
1800               rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1801             }
1802           memcpy(buf, dbdata.data, 8);
1803           rpmhead->forcebinary = 1;
1804           rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1805           rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1806           if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1807             {
1808               db->close(db, 0);
1809               dbenv->close(dbenv, 0);
1810               return pool_error(pool, -1, "corrupt Packages database (data size)");
1811             }
1812           memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1813           rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1814
1815           rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1816           if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1817             {
1818               if (done < count)
1819                 done++;
1820               if (done < count && (done - 1) * 100 / count != done * 100 / count)
1821                 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1822             }
1823         }
1824
1825       if (refhash)
1826         solv_free(refhash);
1827       if (rpmids)
1828         {
1829           for (i = 0; i < nrpmids; i++)
1830             solv_free(rpmids[i].name);
1831           solv_free(rpmids);
1832         }
1833     }
1834   if (db)
1835     db->close(db, 0);
1836   dbenv->close(dbenv, 0);
1837   if (rpmhead)
1838     solv_free(rpmhead);
1839   if (!(flags & REPO_NO_INTERNALIZE))
1840     repodata_internalize(data);
1841   if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1842     pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1843   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1844   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1845   POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1846   return 0;
1847 }
1848
1849
1850 static inline unsigned int
1851 getu32(const unsigned char *dp)
1852 {
1853   return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1854 }
1855
1856
1857 Id
1858 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1859 {
1860   unsigned int sigdsize, sigcnt, l;
1861   Pool *pool = repo->pool;
1862   Solvable *s;
1863   RpmHead *rpmhead = 0;
1864   int rpmheadsize = 0;
1865   char *payloadformat;
1866   FILE *fp;
1867   unsigned char lead[4096];
1868   int headerstart, headerend;
1869   struct stat stb;
1870   Repodata *data;
1871   unsigned char pkgid[16];
1872   unsigned char leadsigid[16];
1873   unsigned char hdrid[32];
1874   int pkgidtype, leadsigidtype, hdridtype;
1875   Id chksumtype = 0;
1876   void *chksumh = 0;
1877   void *leadsigchksumh = 0;
1878   int forcebinary = 0;
1879
1880   data = repo_add_repodata(repo, flags);
1881
1882   if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1883     chksumtype = REPOKEY_TYPE_SHA256;
1884   else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1885     chksumtype = REPOKEY_TYPE_SHA1;
1886
1887   if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1888     {
1889       pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1890       return 0;
1891     }
1892   if (fstat(fileno(fp), &stb))
1893     {
1894       pool_error(pool, -1, "fstat: %s", strerror(errno));
1895       fclose(fp);
1896       return 0;
1897     }
1898   if (chksumtype)
1899     chksumh = solv_chksum_create(chksumtype);
1900   if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1901     leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1902   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1903     {
1904       pool_error(pool, -1, "%s: not a rpm", rpm);
1905       fclose(fp);
1906       return 0;
1907     }
1908   forcebinary = lead[6] != 0 || lead[7] != 1;
1909   if (chksumh)
1910     solv_chksum_add(chksumh, lead, 96 + 16);
1911   if (leadsigchksumh)
1912     solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1913   if (lead[78] != 0 || lead[79] != 5)
1914     {
1915       pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1916       fclose(fp);
1917       return 0;
1918     }
1919   if (getu32(lead + 96) != 0x8eade801)
1920     {
1921       pool_error(pool, -1, "%s: bad signature header", rpm);
1922       fclose(fp);
1923       return 0;
1924     }
1925   sigcnt = getu32(lead + 96 + 8);
1926   sigdsize = getu32(lead + 96 + 12);
1927   if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1928     {
1929       pool_error(pool, -1, "%s: bad signature header", rpm);
1930       fclose(fp);
1931       return 0;
1932     }
1933   sigdsize += sigcnt * 16;
1934   sigdsize = (sigdsize + 7) & ~7;
1935   headerstart = 96 + 16 + sigdsize;
1936   pkgidtype = leadsigidtype = hdridtype = 0;
1937   if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1938     {
1939       /* extract pkgid or hdrid from the signature header */
1940       if (sigdsize > rpmheadsize)
1941         {
1942           rpmheadsize = sigdsize + 128;
1943           rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1944         }
1945       if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1946         {
1947           pool_error(pool, -1, "%s: unexpected EOF", rpm);
1948           fclose(fp);
1949           return 0;
1950         }
1951       if (chksumh)
1952         solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1953       if (leadsigchksumh)
1954         solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1955       rpmhead->forcebinary = 0;
1956       rpmhead->cnt = sigcnt;
1957       rpmhead->dcnt = sigdsize - sigcnt * 16;
1958       rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1959       if ((flags & RPM_ADD_WITH_PKGID) != 0)
1960         {
1961           unsigned char *chksum;
1962           unsigned int chksumsize;
1963           chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1964           if (chksum && chksumsize == 16)
1965             {
1966               pkgidtype = REPOKEY_TYPE_MD5;
1967               memcpy(pkgid, chksum, 16);
1968             }
1969         }
1970       if ((flags & RPM_ADD_WITH_HDRID) != 0)
1971         {
1972           const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1973           if (str && strlen(str) == 40)
1974             {
1975               if (solv_hex2bin(&str, hdrid, 20) == 20)
1976                 hdridtype = REPOKEY_TYPE_SHA1;
1977             }
1978           else if (str && strlen(str) == 64)
1979             {
1980               if (solv_hex2bin(&str, hdrid, 32) == 32)
1981                 hdridtype = REPOKEY_TYPE_SHA256;
1982             }
1983         }
1984     }
1985   else
1986     {
1987       /* just skip the signature header */
1988       while (sigdsize)
1989         {
1990           l = sigdsize > 4096 ? 4096 : sigdsize;
1991           if (fread(lead, l, 1, fp) != 1)
1992             {
1993               pool_error(pool, -1, "%s: unexpected EOF", rpm);
1994               fclose(fp);
1995               return 0;
1996             }
1997           if (chksumh)
1998             solv_chksum_add(chksumh, lead, l);
1999           if (leadsigchksumh)
2000             solv_chksum_add(leadsigchksumh, lead, l);
2001           sigdsize -= l;
2002         }
2003     }
2004   if (leadsigchksumh)
2005     {
2006       leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2007       leadsigidtype = REPOKEY_TYPE_MD5;
2008     }
2009   if (fread(lead, 16, 1, fp) != 1)
2010     {
2011       pool_error(pool, -1, "%s: unexpected EOF", rpm);
2012       fclose(fp);
2013       return 0;
2014     }
2015   if (chksumh)
2016     solv_chksum_add(chksumh, lead, 16);
2017   if (getu32(lead) != 0x8eade801)
2018     {
2019       pool_error(pool, -1, "%s: bad header", rpm);
2020       fclose(fp);
2021       return 0;
2022     }
2023   sigcnt = getu32(lead + 8);
2024   sigdsize = getu32(lead + 12);
2025   if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2026     {
2027       pool_error(pool, -1, "%s: bad header", rpm);
2028       fclose(fp);
2029       return 0;
2030     }
2031   l = sigdsize + sigcnt * 16;
2032   headerend = headerstart + 16 + l;
2033   if (l > rpmheadsize)
2034     {
2035       rpmheadsize = l + 128;
2036       rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2037     }
2038   if (fread(rpmhead->data, l, 1, fp) != 1)
2039     {
2040       pool_error(pool, -1, "%s: unexpected EOF", rpm);
2041       fclose(fp);
2042       return 0;
2043     }
2044   if (chksumh)
2045     solv_chksum_add(chksumh, rpmhead->data, l);
2046   rpmhead->forcebinary = forcebinary;
2047   rpmhead->cnt = sigcnt;
2048   rpmhead->dcnt = sigdsize;
2049   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2050   if (headexists(rpmhead, TAG_PATCHESNAME))
2051     {
2052       /* this is a patch rpm, ignore */
2053       pool_error(pool, -1, "%s: is patch rpm", rpm);
2054       fclose(fp);
2055       solv_chksum_free(chksumh, 0);
2056       solv_free(rpmhead);
2057       return 0;
2058     }
2059   payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2060   if (payloadformat && !strcmp(payloadformat, "drpm"))
2061     {
2062       /* this is a delta rpm */
2063       pool_error(pool, -1, "%s: is delta rpm", rpm);
2064       fclose(fp);
2065       solv_chksum_free(chksumh, 0);
2066       solv_free(rpmhead);
2067       return 0;
2068     }
2069   if (chksumh)
2070     while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2071       solv_chksum_add(chksumh, lead, l);
2072   fclose(fp);
2073   s = pool_id2solvable(pool, repo_add_solvable(repo));
2074   if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2075     {
2076       repo_free_solvable(repo, s - pool->solvables, 1);
2077       solv_chksum_free(chksumh, 0);
2078       solv_free(rpmhead);
2079       return 0;
2080     }
2081   if (!(flags & REPO_NO_LOCATION))
2082     repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2083   if (S_ISREG(stb.st_mode))
2084     repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2085   repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2086   if (pkgidtype)
2087     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2088   if (hdridtype)
2089     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2090   if (leadsigidtype)
2091     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2092   if (chksumh)
2093     {
2094       repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2095       chksumh = solv_chksum_free(chksumh, 0);
2096     }
2097   if (rpmhead)
2098     solv_free(rpmhead);
2099   if (!(flags & REPO_NO_INTERNALIZE))
2100     repodata_internalize(data);
2101   return s - pool->solvables;
2102 }
2103
2104 Id
2105 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2106 {
2107   Pool *pool = repo->pool;
2108   Repodata *data;
2109   RpmHead *rpmhead = rpmhandle;
2110   Solvable *s;
2111   char *payloadformat;
2112
2113   data = repo_add_repodata(repo, flags);
2114   if (headexists(rpmhead, TAG_PATCHESNAME))
2115     {
2116       pool_error(pool, -1, "is a patch rpm");
2117       return 0;
2118     }
2119   payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2120   if (payloadformat && !strcmp(payloadformat, "drpm"))
2121     {
2122       /* this is a delta rpm */
2123       pool_error(pool, -1, "is a delta rpm");
2124       return 0;
2125     }
2126   s = pool_id2solvable(pool, repo_add_solvable(repo));
2127   if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2128     {
2129       repo_free_solvable(repo, s - pool->solvables, 1);
2130       return 0;
2131     }
2132   if (!(flags & REPO_NO_INTERNALIZE))
2133     repodata_internalize(data);
2134   return s - pool->solvables;
2135 }
2136
2137 static inline void
2138 linkhash(const char *lt, char *hash)
2139 {
2140   unsigned int r = 0;
2141   const unsigned char *str = (const unsigned char *)lt;
2142   int l, c;
2143
2144   l = strlen(lt);
2145   while ((c = *str++) != 0)
2146     r += (r << 3) + c;
2147   sprintf(hash, "%08x", r);
2148   sprintf(hash + 8, "%08x", l);
2149   sprintf(hash + 16, "%08x", 0);
2150   sprintf(hash + 24, "%08x", 0);
2151 }
2152
2153 void
2154 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
2155 {
2156   RpmHead *rpmhead = rpmhandle;
2157   char **bn;
2158   char **dn;
2159   char **md = 0;
2160   char **lt = 0;
2161   unsigned int *di, diidx;
2162   unsigned int *co = 0;
2163   unsigned int *ff = 0;
2164   unsigned int lastdir;
2165   int lastdirl;
2166   unsigned int *fm;
2167   int cnt, dcnt, cnt2;
2168   int i, l1, l;
2169   char *space = 0;
2170   int spacen = 0;
2171   char md5[33], *md5p = 0;
2172
2173   dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2174   if (!dn)
2175     return;
2176   if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2177     {
2178       for (i = 0; i < dcnt; i++)
2179         (*cb)(cbdata, dn[i], 0, (char *)0);
2180       solv_free(dn);
2181       return;
2182     }
2183   bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2184   if (!bn)
2185     {
2186       solv_free(dn);
2187       return;
2188     }
2189   di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2190   if (!di || cnt != cnt2)
2191     {
2192       solv_free(di);
2193       solv_free(bn);
2194       solv_free(dn);
2195       return;
2196     }
2197   fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2198   if (!fm || cnt != cnt2)
2199     {
2200       solv_free(fm);
2201       solv_free(di);
2202       solv_free(bn);
2203       solv_free(dn);
2204       return;
2205     }
2206   if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2207     {
2208       md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2209       if (!md || cnt != cnt2)
2210         {
2211           solv_free(md);
2212           solv_free(fm);
2213           solv_free(di);
2214           solv_free(bn);
2215           solv_free(dn);
2216           return;
2217         }
2218     }
2219   if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2220     {
2221       co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2222       if (!co || cnt != cnt2)
2223         {
2224           solv_free(co);
2225           solv_free(md);
2226           solv_free(fm);
2227           solv_free(di);
2228           solv_free(bn);
2229           solv_free(dn);
2230           return;
2231         }
2232     }
2233   if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2234     {
2235       ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2236       if (!ff || cnt != cnt2)
2237         {
2238           solv_free(ff);
2239           solv_free(co);
2240           solv_free(md);
2241           solv_free(fm);
2242           solv_free(di);
2243           solv_free(bn);
2244           solv_free(dn);
2245           return;
2246         }
2247     }
2248   lastdir = dcnt;
2249   lastdirl = 0;
2250   for (i = 0; i < cnt; i++)
2251     {
2252       if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2253         continue;
2254       diidx = di[i];
2255       if (diidx >= dcnt)
2256         continue;
2257       l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2258       if (l1 == 0)
2259         continue;
2260       l = l1 + strlen(bn[i]) + 1;
2261       if (l > spacen)
2262         {
2263           spacen = l + 16;
2264           space = solv_realloc(space, spacen);
2265         }
2266       if (lastdir != diidx)
2267         {
2268           strcpy(space, dn[diidx]);
2269           lastdir = diidx;
2270           lastdirl = l1;
2271         }
2272       strcpy(space + l1, bn[i]);
2273       if (md)
2274         {
2275           md5p = md[i];
2276           if (S_ISLNK(fm[i]))
2277             {
2278               md5p = 0;
2279               if (!lt)
2280                 {
2281                   lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2282                   if (cnt != cnt2)
2283                     lt = solv_free(lt);
2284                 }
2285               if (lt)
2286                 {
2287                   linkhash(lt[i], md5);
2288                   md5p = md5;
2289                 }
2290             }
2291           if (!md5p)
2292             {
2293               sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2294               md5p = md5;
2295             }
2296         }
2297       (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2298     }
2299   solv_free(space);
2300   solv_free(lt);
2301   solv_free(md);
2302   solv_free(fm);
2303   solv_free(di);
2304   solv_free(bn);
2305   solv_free(dn);
2306   solv_free(co);
2307   solv_free(ff);
2308 }
2309
2310 char *
2311 rpm_query(void *rpmhandle, Id what)
2312 {
2313   const char *name, *arch, *sourcerpm;
2314   char *evr, *r;
2315   int l;
2316
2317   RpmHead *rpmhead = rpmhandle;
2318   r = 0;
2319   switch (what)
2320     {
2321     case 0:
2322       name = headstring(rpmhead, TAG_NAME);
2323       if (!name)
2324         name = "";
2325       sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2326       if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2327         arch = headstring(rpmhead, TAG_ARCH);
2328       else
2329         {
2330           if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2331             arch = "nosrc";
2332           else
2333             arch = "src";
2334         }
2335       if (!arch)
2336         arch = "noarch";
2337       evr = headtoevr(rpmhead);
2338       l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2339       r = solv_malloc(l);
2340       sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2341       solv_free(evr);
2342       break;
2343     case SOLVABLE_NAME:
2344       name = headstring(rpmhead, TAG_NAME);
2345       r = solv_strdup(name);
2346       break;
2347     case SOLVABLE_EVR:
2348       r = headtoevr(rpmhead);
2349       break;
2350     }
2351   return r;
2352 }
2353
2354
2355 struct rpm_by_state {
2356   RpmHead *rpmhead;
2357   int rpmheadsize;
2358
2359   int dbopened;
2360   DB_ENV *dbenv;
2361   DB *db;
2362   int byteswapped;
2363 };
2364
2365 struct rpmdbentry {
2366   Id rpmdbid;
2367   Id nameoff;
2368 };
2369
2370 #define ENTRIES_BLOCK 255
2371 #define NAMEDATA_BLOCK 1023
2372
2373 static struct rpmdbentry *
2374 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2375 {
2376   DB_ENV *dbenv = 0;
2377   DB *db = 0;
2378   DBC *dbc = 0;
2379   int byteswapped;
2380   DBT dbkey;
2381   DBT dbdata;
2382   unsigned char *dp;
2383   int dl;
2384   Id nameoff;
2385
2386   char *namedata = 0;
2387   int namedatal = 0;
2388   struct rpmdbentry *entries = 0;
2389   int nentries = 0;
2390
2391   *nentriesp = 0;
2392   *namedatap = 0;
2393
2394   dbenv = state->dbenv;
2395   if (db_create(&db, dbenv, 0))
2396     {
2397       perror("db_create");
2398       return 0;
2399     }
2400   if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2401     {
2402       perror("db->open index");
2403       db->close(db, 0);
2404       return 0;
2405     }
2406   if (db->get_byteswapped(db, &byteswapped))
2407     {
2408       perror("db->get_byteswapped");
2409       db->close(db, 0);
2410       return 0;
2411     }
2412   if (db->cursor(db, NULL, &dbc, 0))
2413     {
2414       perror("db->cursor");
2415       db->close(db, 0);
2416       return 0;
2417     }
2418   memset(&dbkey, 0, sizeof(dbkey));
2419   memset(&dbdata, 0, sizeof(dbdata));
2420   if (match)
2421     {
2422       dbkey.data = (void *)match;
2423       dbkey.size = strlen(match);
2424     }
2425   while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2426     {
2427       if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2428         continue;
2429       dl = dbdata.size;
2430       dp = dbdata.data;
2431       nameoff = namedatal;
2432       if (namedatap)
2433         {
2434           namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2435           memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2436           namedata[namedatal + dbkey.size] = 0;
2437           namedatal += dbkey.size + 1;
2438         }
2439       while(dl >= RPM_INDEX_SIZE)
2440         {
2441           entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2442           entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2443           entries[nentries].nameoff = nameoff;
2444           nentries++;
2445           dp += RPM_INDEX_SIZE;
2446           dl -= RPM_INDEX_SIZE;
2447         }
2448       if (match)
2449         break;
2450     }
2451   dbc->c_close(dbc);
2452   db->close(db, 0);
2453   *nentriesp = nentries;
2454   if (namedatap)
2455     *namedatap = namedata;
2456   return entries;
2457 }
2458
2459 static int
2460 openpkgdb(struct rpm_by_state *state, const char *rootdir)
2461 {
2462   if (state->dbopened)
2463     return state->dbopened > 0 ? 1 : 0;
2464   state->dbopened = -1;
2465   if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2466     return 0;
2467   if (db_create(&state->db, state->dbenv, 0))
2468     {
2469       perror("db_create");
2470       state->db = 0;
2471       state->dbenv->close(state->dbenv, 0);
2472       state->dbenv = 0;
2473       return 0;
2474     }
2475   if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2476     {
2477       perror("db->open var/lib/rpm/Packages");
2478       state->db->close(state->db, 0);
2479       state->db = 0;
2480       state->dbenv->close(state->dbenv, 0);
2481       state->dbenv = 0;
2482       return 0;
2483     }
2484   if (state->db->get_byteswapped(state->db, &state->byteswapped))
2485     {
2486       perror("db->get_byteswapped");
2487       state->db->close(state->db, 0);
2488       state->db = 0;
2489       state->dbenv->close(state->dbenv, 0);
2490       state->dbenv = 0;
2491       return 0;
2492     }
2493   state->dbopened = 1;
2494   return 1;
2495 }
2496
2497 static void
2498 freestate(struct rpm_by_state *state)
2499 {
2500   /* close down */
2501   if (!state)
2502     return;
2503   if (state->db)
2504     state->db->close(state->db, 0);
2505   if (state->dbenv)
2506     state->dbenv->close(state->dbenv, 0);
2507   solv_free(state->rpmhead);
2508 }
2509
2510 int
2511 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2512 {
2513   struct rpm_by_state state;
2514   struct rpmdbentry *entries;
2515   int nentries, i;
2516   char *namedata;
2517
2518   if (!index)
2519     index = "Name";
2520   if (rpmdbidq)
2521     queue_empty(rpmdbidq);
2522   memset(&state, 0, sizeof(state));
2523   if (!(state.dbenv = opendbenv(rootdir)))
2524     return 0;
2525   entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2526   if (rpmdbidq)
2527     for (i = 0; i < nentries; i++)
2528       queue_push(rpmdbidq, entries[i].rpmdbid);
2529   solv_free(entries);
2530   solv_free(namedata);
2531   freestate(&state);
2532   return nentries;
2533 }
2534
2535 void *
2536 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2537 {
2538   struct rpm_by_state *state = *statep;
2539   unsigned char buf[16];
2540   DBT dbkey;
2541   DBT dbdata;
2542   RpmHead *rpmhead;
2543
2544   if (!rpmdbid)
2545     {
2546       /* close down */
2547       freestate(state);
2548       solv_free(state);
2549       *statep = (void *)0;
2550       return 0;
2551     }
2552
2553   if (!state)
2554     {
2555       state = solv_calloc(1, sizeof(*state));
2556       *statep = state;
2557     }
2558   if (!state->dbopened && !openpkgdb(state, rootdir))
2559     return 0;
2560   rpmdbid2db(buf, rpmdbid, state->byteswapped);
2561   memset(&dbkey, 0, sizeof(dbkey));
2562   memset(&dbdata, 0, sizeof(dbdata));
2563   dbkey.data = buf;
2564   dbkey.size = 4;
2565   dbdata.data = 0;
2566   dbdata.size = 0;
2567   if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2568     {
2569       perror("db->get");
2570       return 0;
2571     }
2572   if (dbdata.size < 8)
2573     {
2574       fprintf(stderr, "corrupt rpm database (size)\n");
2575       return 0;
2576     }
2577   if (dbdata.size > state->rpmheadsize)
2578     {
2579       state->rpmheadsize = dbdata.size + 128;
2580       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2581     }
2582   rpmhead = state->rpmhead;
2583   memcpy(buf, dbdata.data, 8);
2584   rpmhead->forcebinary = 1;
2585   rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
2586   rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
2587   if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2588     {
2589       fprintf(stderr, "corrupt rpm database (data size)\n");
2590       return 0;
2591     }
2592   memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2593   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2594   return rpmhead;
2595 }
2596
2597 void *
2598 rpm_byfp(FILE *fp, const char *name, void **statep)
2599 {
2600   struct rpm_by_state *state = *statep;
2601   /* int headerstart, headerend; */
2602   RpmHead *rpmhead;
2603   unsigned int sigdsize, sigcnt, l;
2604   unsigned char lead[4096];
2605   int forcebinary = 0;
2606
2607   if (!fp)
2608     return rpm_byrpmdbid(0, 0, statep);
2609   if (!state)
2610     {
2611       state = solv_calloc(1, sizeof(*state));
2612       *statep = state;
2613     }
2614   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2615     {
2616       fprintf(stderr, "%s: not a rpm\n", name);
2617       return 0;
2618     }
2619   forcebinary = lead[6] != 0 || lead[7] != 1;
2620   if (lead[78] != 0 || lead[79] != 5)
2621     {
2622       fprintf(stderr, "%s: not a V5 header\n", name);
2623       return 0;
2624     }
2625   if (getu32(lead + 96) != 0x8eade801)
2626     {
2627       fprintf(stderr, "%s: bad signature header\n", name);
2628       return 0;
2629     }
2630   sigcnt = getu32(lead + 96 + 8);
2631   sigdsize = getu32(lead + 96 + 12);
2632   if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2633     {
2634       fprintf(stderr, "%s: bad signature header\n", name);
2635       return 0;
2636     }
2637   sigdsize += sigcnt * 16;
2638   sigdsize = (sigdsize + 7) & ~7;
2639   /* headerstart = 96 + 16 + sigdsize; */
2640   while (sigdsize)
2641     {
2642       l = sigdsize > 4096 ? 4096 : sigdsize;
2643       if (fread(lead, l, 1, fp) != 1)
2644         {
2645           fprintf(stderr, "%s: unexpected EOF\n", name);
2646           return 0;
2647         }
2648       sigdsize -= l;
2649     }
2650   if (fread(lead, 16, 1, fp) != 1)
2651     {
2652       fprintf(stderr, "%s: unexpected EOF\n", name);
2653       return 0;
2654     }
2655   if (getu32(lead) != 0x8eade801)
2656     {
2657       fprintf(stderr, "%s: bad header\n", name);
2658       fclose(fp);
2659       return 0;
2660     }
2661   sigcnt = getu32(lead + 8);
2662   sigdsize = getu32(lead + 12);
2663   if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2664     {
2665       fprintf(stderr, "%s: bad header\n", name);
2666       fclose(fp);
2667       return 0;
2668     }
2669   l = sigdsize + sigcnt * 16;
2670   /* headerend = headerstart + 16 + l; */
2671   if (l > state->rpmheadsize)
2672     {
2673       state->rpmheadsize = l + 128;
2674       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2675     }
2676   rpmhead = state->rpmhead;
2677   if (fread(rpmhead->data, l, 1, fp) != 1)
2678     {
2679       fprintf(stderr, "%s: unexpected EOF\n", name);
2680       fclose(fp);
2681       return 0;
2682     }
2683   rpmhead->forcebinary = forcebinary;
2684   rpmhead->cnt = sigcnt;
2685   rpmhead->dcnt = sigdsize;
2686   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2687   return rpmhead;
2688 }
2689
2690 #ifdef ENABLE_RPMDB_BYRPMHEADER
2691
2692 void *
2693 rpm_byrpmh(Header h, void **statep)
2694 {
2695   struct rpm_by_state *state = *statep;
2696   const unsigned char *uh;
2697   unsigned int sigdsize, sigcnt, l;
2698   RpmHead *rpmhead;
2699
2700 #ifndef RPM5
2701   uh = headerUnload(h);
2702 #else
2703   uh = headerUnload(h, NULL);
2704 #endif
2705   if (!uh)
2706     return 0;
2707   sigcnt = getu32(uh);
2708   sigdsize = getu32(uh + 4);
2709   l = sigdsize + sigcnt * 16;
2710   if (!state)
2711     {
2712       state = solv_calloc(1, sizeof(*state));
2713       *statep = state;
2714     }
2715   if (l > state->rpmheadsize)
2716     {
2717       state->rpmheadsize = l + 128;
2718       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2719     }
2720   rpmhead = state->rpmhead;
2721   memcpy(rpmhead->data, uh + 8, l - 8);
2722   free((void *)uh);
2723   rpmhead->forcebinary = 0;
2724   rpmhead->cnt = sigcnt;
2725   rpmhead->dcnt = sigdsize;
2726   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2727   return rpmhead;
2728 }
2729
2730 #endif
2731
2732
2733 #ifdef ENABLE_RPMDB_PUBKEY
2734
2735 static char *
2736 r64dec1(char *p, unsigned int *vp, int *eofp)
2737 {
2738   int i, x;
2739   unsigned int v = 0;
2740
2741   for (i = 0; i < 4; )
2742     {
2743       x = *p++;
2744       if (!x)
2745         return 0;
2746       if (x >= 'A' && x <= 'Z')
2747         x -= 'A';
2748       else if (x >= 'a' && x <= 'z')
2749         x -= 'a' - 26;
2750       else if (x >= '0' && x <= '9')
2751         x -= '0' - 52;
2752       else if (x == '+')
2753         x = 62;
2754       else if (x == '/')
2755         x = 63;
2756       else if (x == '=')
2757         {
2758           x = 0;
2759           if (i == 0)
2760             {
2761               *eofp = 3;
2762               *vp = 0;
2763               return p - 1;
2764             }
2765           *eofp += 1;
2766         }
2767       else
2768         continue;
2769       v = v << 6 | x;
2770       i++;
2771     }
2772   *vp = v;
2773   return p;
2774 }
2775
2776 static unsigned int
2777 crc24(unsigned char *p, int len)
2778 {
2779   unsigned int crc = 0xb704ceL;
2780   int i;
2781
2782   while (len--)
2783     {
2784       crc ^= (*p++) << 16;
2785       for (i = 0; i < 8; i++)
2786         if ((crc <<= 1) & 0x1000000)
2787           crc ^= 0x1864cfbL;
2788     }
2789   return crc & 0xffffffL;
2790 }
2791
2792 static unsigned char *
2793 unarmor(char *pubkey, int *pktlp)
2794 {
2795   char *p;
2796   int l, eof;
2797   unsigned char *buf, *bp;
2798   unsigned int v;
2799
2800   *pktlp = 0;
2801   while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2802     {
2803       pubkey = strchr(pubkey, '\n');
2804       if (!pubkey)
2805         return 0;
2806       pubkey++;
2807     }
2808   pubkey = strchr(pubkey, '\n');
2809   if (!pubkey++)
2810     return 0;
2811   /* skip header lines */
2812   for (;;)
2813     {
2814       while (*pubkey == ' ' || *pubkey == '\t')
2815         pubkey++;
2816       if (*pubkey == '\n')
2817         break;
2818       pubkey = strchr(pubkey, '\n');
2819       if (!pubkey++)
2820         return 0;
2821     }
2822   pubkey++;
2823   p = strchr(pubkey, '=');
2824   if (!p)
2825     return 0;
2826   l = p - pubkey;
2827   bp = buf = solv_malloc(l * 3 / 4 + 4);
2828   eof = 0;
2829   while (!eof)
2830     {
2831       pubkey = r64dec1(pubkey, &v, &eof);
2832       if (!pubkey)
2833         {
2834           solv_free(buf);
2835           return 0;
2836         }
2837       *bp++ = v >> 16;
2838       *bp++ = v >> 8;
2839       *bp++ = v;
2840     }
2841   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2842     pubkey++;
2843   bp -= eof;
2844   if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2845     {
2846       solv_free(buf);
2847       return 0;
2848     }
2849   if (v != crc24(buf, bp - buf))
2850     {
2851       solv_free(buf);
2852       return 0;
2853     }
2854   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2855     pubkey++;
2856   if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2857     {
2858       solv_free(buf);
2859       return 0;
2860     }
2861   *pktlp = bp - buf;
2862   return buf;
2863 }
2864
2865 static void
2866 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2867 {
2868   int x, tag, l;
2869   unsigned char keyid[8];
2870   unsigned int kcr = 0, maxex = 0;
2871   unsigned char *pubkey = 0;
2872   unsigned char *userid = 0;
2873 #if 0
2874   int pubkeyl = 0;
2875   int useridl = 0;
2876 #endif
2877
2878   for (; pl; p += l, pl -= l)
2879     {
2880       x = *p++;
2881       pl--;
2882       if (!(x & 128) || pl <= 0)
2883         return;
2884       if ((x & 64) == 0)
2885         {
2886           /* old format */
2887           tag = (x & 0x3c) >> 2;
2888           x &= 3;
2889           if (x == 3)
2890             return;
2891           l = 1 << x;
2892           if (pl < l)
2893             return;
2894           x = 0;
2895           while (l--)
2896             {
2897               x = x << 8 | *p++;
2898               pl--;
2899             }
2900           l = x;
2901         }
2902       else
2903         {
2904           tag = (x & 0x3f);
2905           x = *p++;
2906           pl--;
2907           if (x < 192)
2908             l = x;
2909           else if (x >= 192 && x < 224)
2910             {
2911               if (pl <= 0)
2912                 return;
2913               l = ((x - 192) << 8) + *p++ + 192;
2914               pl--;
2915             }
2916           else if (x == 255)
2917             {
2918               /* sanity: p[0] must be zero */
2919               if (pl <= 4 || p[0] != 0)
2920                 return;
2921               l = p[1] << 16 | p[2] << 8 | p[3];
2922               p += 4;
2923               pl -= 4;
2924             }
2925           else
2926             return;
2927         }
2928       if (pl < l)
2929         return;
2930       if (tag == 6)
2931         {
2932           pubkey = solv_realloc(pubkey, l);
2933           if (l)
2934             memcpy(pubkey, p, l);
2935 #if 0
2936           pubkeyl = l;
2937 #endif
2938           kcr = 0;
2939           if (p[0] == 3)
2940             {
2941               unsigned int ex;
2942               void *h;
2943               kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2944               ex = 0;
2945               if (p[5] || p[6])
2946                 {
2947                   ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2948                   if (ex > maxex)
2949                     maxex = ex;
2950                 }
2951               memset(keyid, 0, 8);
2952               if (p[7] == 1)    /* RSA */
2953                 {
2954                   int i, ql;
2955                   unsigned char fp[16];
2956                   char fpx[32 + 1];
2957                   unsigned char *q;
2958
2959                   ql = ((p[8] << 8 | p[9]) + 7) / 8;
2960                   memcpy(keyid, p + 10 + ql - 8, 8);
2961                   h = solv_chksum_create(REPOKEY_TYPE_MD5);
2962                   solv_chksum_add(h, p + 10, ql);
2963                   q = p + 10 + ql;
2964                   ql = ((q[0] << 8 | q[1]) + 7) / 8;
2965                   solv_chksum_add(h, q + 2, ql);
2966                   solv_chksum_free(h, fp);
2967                   for (i = 0; i < 16; i++)
2968                     sprintf(fpx + i * 2, "%02x", fp[i]);
2969                   repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2970                 }
2971             }
2972           else if (p[0] == 4)
2973             {
2974               int i;
2975               void *h;
2976               unsigned char hdr[3];
2977               unsigned char fp[20];
2978               char fpx[40 + 1];
2979
2980               kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2981               hdr[0] = 0x99;
2982               hdr[1] = l >> 8;
2983               hdr[2] = l;
2984               h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2985               solv_chksum_add(h, hdr, 3);
2986               solv_chksum_add(h, p, l);
2987               solv_chksum_free(h, fp);
2988               for (i = 0; i < 20; i++)
2989                 sprintf(fpx + i * 2, "%02x", fp[i]);
2990               repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2991               memcpy(keyid, fp + 12, 8);
2992             }
2993         }
2994       if (tag == 2)
2995         {
2996           if (p[0] == 3 && p[1] == 5)
2997             {
2998 #if 0
2999               Id htype = 0;
3000 #endif
3001               /* printf("V3 signature packet\n"); */
3002               if (l < 17)
3003                 continue;
3004               if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
3005                 continue;
3006               if (!memcmp(keyid, p + 6, 8))
3007                 {
3008                   /* printf("SELF SIG\n"); */
3009                 }
3010               else
3011                 {
3012                   /* printf("OTHER SIG\n"); */
3013                 }
3014 #if 0
3015               if (p[16] == 1)
3016                 htype = REPOKEY_TYPE_MD5;
3017               else if (p[16] == 2)
3018                 htype = REPOKEY_TYPE_SHA1;
3019               else if (p[16] == 8)
3020                 htype = REPOKEY_TYPE_SHA256;
3021               if (htype)
3022                 {
3023                   void *h = solv_chksum_create(htype);
3024                   unsigned char b[3], *cs;
3025
3026                   b[0] = 0x99;
3027                   b[1] = pubkeyl >> 8;
3028                   b[2] = pubkeyl;
3029                   solv_chksum_add(h, b, 3);
3030                   solv_chksum_add(h, pubkey, pubkeyl);
3031                   if (p[2] >= 0x10 && p[2] <= 0x13)
3032                     solv_chksum_add(h, userid, useridl);
3033                   solv_chksum_add(h, p + 2, 5);
3034                   cs = solv_chksum_get(h, 0);
3035                   solv_chksum_free(h, 0);
3036                 }
3037 #endif
3038             }
3039           if (p[0] == 4)
3040             {
3041               int j, ql, haveissuer;
3042               unsigned char *q;
3043               unsigned int ex = 0;
3044 #if 0
3045               unsigned int scr = 0;
3046 #endif
3047               unsigned char issuer[8];
3048
3049               /* printf("V4 signature packet\n"); */
3050               if (l < 6)
3051                 continue;
3052               if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
3053                 continue;
3054               haveissuer = 0;
3055               ex = 0;
3056               q = p + 4;
3057               for (j = 0; q && j < 2; j++)
3058                 {
3059                   if (q + 2 > p + l)
3060                     {
3061                       q = 0;
3062                       break;
3063                     }
3064                   ql = q[0] << 8 | q[1];
3065                   q += 2;
3066                   if (q + ql > p + l)
3067                     {
3068                       q = 0;
3069                       break;
3070                     }
3071                   while (ql)
3072                     {
3073                       int sl;
3074                       x = *q++;
3075                       ql--;
3076                       if (x < 192)
3077                         sl = x;
3078                       else if (x == 255)
3079                         {
3080                           if (ql < 4 || q[0] != 0)
3081                             {
3082                               q = 0;
3083                               break;
3084                             }
3085                           sl = q[1] << 16 | q[2] << 8 | q[3];
3086                           q += 4;
3087                           ql -= 4;
3088                         }
3089                       else
3090                         {
3091                           if (ql < 1)
3092                             {
3093                               q = 0;
3094                               break;
3095                             }
3096                           sl = ((x - 192) << 8) + *q++ + 192;
3097                           ql--;
3098                         }
3099                       if (ql < sl)
3100                         {
3101                           q = 0;
3102                           break;
3103                         }
3104                       x = q[0] & 127;
3105                       /* printf("%d SIGSUB %d %d\n", j, x, sl); */
3106                       if (x == 16 && sl == 9 && !haveissuer)
3107                         {
3108                           memcpy(issuer, q + 1, 8);
3109                           haveissuer = 1;
3110                         }
3111 #if 0
3112                       if (x == 2 && j == 0)
3113                         scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3114 #endif
3115                       if (x == 9 && j == 0)
3116                         ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3117                       q += sl;
3118                       ql -= sl;
3119                     }
3120                 }
3121               if (ex)
3122                 ex += kcr;
3123               if (haveissuer)
3124                 {
3125 #if 0
3126                   Id htype = 0;
3127                   if (p[3] == 1)
3128                     htype = REPOKEY_TYPE_MD5;
3129                   else if (p[3] == 2)
3130                     htype = REPOKEY_TYPE_SHA1;
3131                   else if (p[3] == 8)
3132                     htype = REPOKEY_TYPE_SHA256;
3133                   if (htype && pubkeyl)
3134                     {
3135                       void *h = solv_chksum_create(htype);
3136                       unsigned char b[6], *cs;
3137                       unsigned int hl;
3138
3139                       b[0] = 0x99;
3140                       b[1] = pubkeyl >> 8;
3141                       b[2] = pubkeyl;
3142                       solv_chksum_add(h, b, 3);
3143                       solv_chksum_add(h, pubkey, pubkeyl);
3144                       if (p[1] >= 0x10 && p[1] <= 0x13)
3145                         {
3146                           b[0] = 0xb4;
3147                           b[1] = useridl >> 24;
3148                           b[2] = useridl >> 16;
3149                           b[3] = useridl >> 8;
3150                           b[4] = useridl;
3151                           solv_chksum_add(h, b, 5);
3152                           solv_chksum_add(h, userid, useridl);
3153                         }
3154                       hl = 6 + (p[4] << 8 | p[5]);
3155                       solv_chksum_add(h, p, hl);
3156                       b[0] = 4;
3157                       b[1] = 0xff;
3158                       b[2] = hl >> 24;
3159                       b[3] = hl >> 16;
3160                       b[4] = hl >> 8;
3161                       b[5] = hl;
3162                       solv_chksum_add(h, b, 6);
3163                       cs = solv_chksum_get(h, 0);
3164                       solv_chksum_free(h, 0);
3165                     }
3166 #endif
3167                   if (!memcmp(keyid, issuer, 8))
3168                     {
3169                       /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
3170                       if (ex > maxex)
3171                         maxex = ex;
3172                     }
3173                   else
3174                     {
3175                       /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
3176                     }
3177                 }
3178             }
3179         }
3180       if (tag == 13)
3181         {
3182           userid = solv_realloc(userid, l);
3183           if (l)
3184             memcpy(userid, p, l);
3185 #if 0
3186           useridl = l;
3187 #endif
3188         }
3189     }
3190   if (maxex)
3191     repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3192   solv_free(pubkey);
3193   solv_free(userid);
3194 }
3195
3196 /* this is private to rpm, but rpm lacks an interface to retrieve
3197  * the values. Sigh. */
3198 struct pgpDigParams_s {
3199     const char * userid;
3200     const unsigned char * hash;
3201     const char * params[4];
3202     unsigned char tag;
3203     unsigned char version;               /*!< version number. */
3204     unsigned char time[4];               /*!< time that the key was created. */
3205     unsigned char pubkey_algo;           /*!< public key algorithm. */
3206     unsigned char hash_algo;
3207     unsigned char sigtype;
3208     unsigned char hashlen;
3209     unsigned char signhash16[2];
3210     unsigned char signid[8];
3211     unsigned char saved;
3212 };
3213
3214 struct pgpDig_s {
3215     struct pgpDigParams_s signature;
3216     struct pgpDigParams_s pubkey;
3217 };
3218
3219 static int
3220 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3221 {
3222   Pool *pool = s->repo->pool;
3223   unsigned char *pkts;
3224   unsigned int btime;
3225   int pktsl, i;
3226   pgpDig dig = 0;
3227   char keyid[16 + 1];
3228   char evrbuf[8 + 1 + 8 + 1];
3229
3230   pkts = unarmor(pubkey, &pktsl);
3231   if (!pkts)
3232     return 0;
3233   setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3234   parsekeydata(s, data, pkts, pktsl);
3235   /* only rpm knows how to do the release calculation, we don't dare
3236    * to recreate all the bugs */
3237 #ifndef RPM5
3238   dig = pgpNewDig();
3239 #else
3240   dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3241 #endif
3242   (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3243   btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3244   sprintf(evrbuf, "%02x%02x%02x%02x-%02x%02x%02x%02x", dig->pubkey.signid[4], dig->pubkey.signid[5], dig->pubkey.signid[6], dig->pubkey.signid[7], dig->pubkey.time[0], dig->pubkey.time[1], dig->pubkey.time[2], dig->pubkey.time[3]);
3245   repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3246
3247   s->name = pool_str2id(pool, "gpg-pubkey", 1);
3248   s->evr = pool_str2id(pool, evrbuf, 1);
3249   s->arch = 1;
3250   for (i = 0; i < 8; i++)
3251     sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3252   repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3253   if (dig->pubkey.userid)
3254     setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3255 #ifndef RPM5
3256   (void)pgpFreeDig(dig);
3257 #else
3258   (void)pgpDigFree(dig);
3259 #endif
3260   solv_free((void *)pkts);
3261   return 1;
3262 }
3263
3264 int
3265 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
3266 {
3267   Pool *pool = repo->pool;
3268   struct rpm_by_state state;
3269   struct rpmdbentry *entries;
3270   int nentries, i;
3271   char *namedata, *str;
3272   unsigned int u32;
3273   Repodata *data;
3274   Solvable *s;
3275   const char *rootdir = 0;
3276
3277   data = repo_add_repodata(repo, flags);
3278   if (flags & REPO_USE_ROOTDIR)
3279     rootdir = pool_get_rootdir(pool);
3280
3281   memset(&state, 0, sizeof(state));
3282   if (!(state.dbenv = opendbenv(rootdir)))
3283     return 0;
3284   entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3285   for (i = 0 ; i < nentries; i++)
3286     {
3287       void *statep = &state;
3288       RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3289       if (!rpmhead)
3290         continue;
3291       str = headstring(rpmhead, TAG_DESCRIPTION);
3292       if (!str)
3293         continue;
3294       s = pool_id2solvable(pool, repo_add_solvable(repo));
3295       pubkey2solvable(s, data, str);
3296       u32 = headint32(rpmhead, TAG_INSTALLTIME);
3297       if (u32)
3298         repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3299       if (!repo->rpmdbid)
3300         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3301       repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3302     }
3303   solv_free(entries);
3304   solv_free(namedata);
3305   freestate(&state);
3306   if (!(flags & REPO_NO_INTERNALIZE))
3307     repodata_internalize(data);
3308   return 0;
3309 }
3310
3311 Id
3312 repo_add_pubkey(Repo *repo, const char *key, int flags)
3313 {
3314   Pool *pool = repo->pool;
3315   Repodata *data;
3316   Solvable *s;
3317   char *buf;
3318   int bufl, l, ll;
3319   FILE *fp;
3320
3321   data = repo_add_repodata(repo, flags);
3322   buf = 0;
3323   bufl = 0;
3324   if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
3325     {
3326       pool_error(pool, -1, "%s: %s", key, strerror(errno));
3327       return 0;
3328     }
3329   for (l = 0; ;)
3330     {
3331       if (bufl - l < 4096)
3332         {
3333           bufl += 4096;
3334           buf = solv_realloc(buf, bufl);
3335         }
3336       ll = fread(buf, 1, bufl - l, fp);
3337       if (ll < 0)
3338         {
3339           fclose(fp);
3340           pool_error(pool, -1, "%s: %s", key, strerror(errno));
3341           return 0;
3342         }
3343       if (ll == 0)
3344         break;
3345       l += ll;
3346     }
3347   buf[l] = 0;
3348   fclose(fp);
3349   s = pool_id2solvable(pool, repo_add_solvable(repo));
3350   if (!pubkey2solvable(s, data, buf))
3351     {
3352       repo_free_solvable(repo, s - pool->solvables, 1);
3353       solv_free(buf);
3354       return 0;
3355     }
3356   solv_free(buf);
3357   if (!(flags & REPO_NO_INTERNALIZE))
3358     repodata_internalize(data);
3359   return s - pool->solvables;
3360 }
3361
3362 #endif /* ENABLE_RPMDB_PUBKEY */