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