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