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