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