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