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