add pool parameter to rpm_state_create so that we can use pool_error for error reporting
[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   Pool *pool; 
2272   RpmHead *rpmhead;
2273   int rpmheadsize;
2274
2275   int dbopened;
2276   char *rootdir;
2277   DB_ENV *dbenv;
2278   DB *db;
2279   int byteswapped;
2280 };
2281
2282 struct rpmdbentry {
2283   Id rpmdbid;
2284   Id nameoff;
2285 };
2286
2287 #define ENTRIES_BLOCK 255
2288 #define NAMEDATA_BLOCK 1023
2289
2290 static struct rpmdbentry *
2291 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2292 {
2293   DB_ENV *dbenv = 0;
2294   DB *db = 0;
2295   DBC *dbc = 0;
2296   int byteswapped;
2297   DBT dbkey;
2298   DBT dbdata;
2299   unsigned char *dp;
2300   int dl;
2301   Id nameoff;
2302
2303   char *namedata = 0;
2304   int namedatal = 0;
2305   struct rpmdbentry *entries = 0;
2306   int nentries = 0;
2307
2308   *nentriesp = 0;
2309   if (namedatap)
2310     *namedatap = 0;
2311
2312   if (!state->dbenv && !(state->dbenv = opendbenv(state->rootdir)))
2313     return 0;
2314   dbenv = state->dbenv;
2315   if (db_create(&db, dbenv, 0))
2316     {
2317       perror("db_create");
2318       return 0;
2319     }
2320   if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2321     {
2322       perror("db->open index");
2323       db->close(db, 0);
2324       return 0;
2325     }
2326   if (db->get_byteswapped(db, &byteswapped))
2327     {
2328       perror("db->get_byteswapped");
2329       db->close(db, 0);
2330       return 0;
2331     }
2332   if (db->cursor(db, NULL, &dbc, 0))
2333     {
2334       perror("db->cursor");
2335       db->close(db, 0);
2336       return 0;
2337     }
2338   memset(&dbkey, 0, sizeof(dbkey));
2339   memset(&dbdata, 0, sizeof(dbdata));
2340   if (match)
2341     {
2342       dbkey.data = (void *)match;
2343       dbkey.size = strlen(match);
2344     }
2345   while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2346     {
2347       if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2348         continue;
2349       dl = dbdata.size;
2350       dp = dbdata.data;
2351       nameoff = namedatal;
2352       if (namedatap)
2353         {
2354           namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2355           memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2356           namedata[namedatal + dbkey.size] = 0;
2357           namedatal += dbkey.size + 1;
2358         }
2359       while(dl >= RPM_INDEX_SIZE)
2360         {
2361           entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2362           entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2363           entries[nentries].nameoff = nameoff;
2364           nentries++;
2365           dp += RPM_INDEX_SIZE;
2366           dl -= RPM_INDEX_SIZE;
2367         }
2368       if (match)
2369         break;
2370     }
2371   dbc->c_close(dbc);
2372   db->close(db, 0);
2373   *nentriesp = nentries;
2374   if (namedatap)
2375     *namedatap = namedata;
2376   return entries;
2377 }
2378
2379 static int
2380 openpkgdb(struct rpm_by_state *state, const char *rootdir)
2381 {
2382   if (state->dbopened)
2383     return state->dbopened > 0 ? 1 : 0;
2384   state->dbopened = -1;
2385   if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2386     return 0;
2387   if (db_create(&state->db, state->dbenv, 0))
2388     {
2389       perror("db_create");
2390       state->db = 0;
2391       state->dbenv->close(state->dbenv, 0);
2392       state->dbenv = 0;
2393       return 0;
2394     }
2395   if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2396     {
2397       perror("db->open var/lib/rpm/Packages");
2398       state->db->close(state->db, 0);
2399       state->db = 0;
2400       state->dbenv->close(state->dbenv, 0);
2401       state->dbenv = 0;
2402       return 0;
2403     }
2404   if (state->db->get_byteswapped(state->db, &state->byteswapped))
2405     {
2406       perror("db->get_byteswapped");
2407       state->db->close(state->db, 0);
2408       state->db = 0;
2409       state->dbenv->close(state->dbenv, 0);
2410       state->dbenv = 0;
2411       return 0;
2412     }
2413   state->dbopened = 1;
2414   return 1;
2415 }
2416
2417 static void
2418 freestate(struct rpm_by_state *state)
2419 {
2420   /* close down */
2421   if (!state)
2422     return;
2423   if (state->rootdir)
2424     solv_free(state->rootdir);
2425   if (state->db)
2426     state->db->close(state->db, 0);
2427   if (state->dbenv)
2428     state->dbenv->close(state->dbenv, 0);
2429   solv_free(state->rpmhead);
2430 }
2431
2432 void *
2433 rpm_state_create(Pool *pool, const char *rootdir)
2434 {
2435   struct rpm_by_state *state;
2436   state = solv_calloc(1, sizeof(*state));
2437   state->pool = pool;
2438   if (rootdir)
2439     state->rootdir = solv_strdup(rootdir);
2440   return state;
2441 }
2442
2443 void *
2444 rpm_state_free(void *state)
2445 {
2446   freestate(state);
2447   return solv_free(state);
2448 }
2449
2450 int
2451 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2452 {
2453   struct rpmdbentry *entries;
2454   int nentries, i;
2455
2456   entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2457   if (rpmdbidq)
2458     {
2459       queue_empty(rpmdbidq);
2460       for (i = 0; i < nentries; i++)
2461         queue_push(rpmdbidq, entries[i].rpmdbid);
2462     }
2463   solv_free(entries);
2464   return nentries;
2465 }
2466
2467 void *
2468 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2469 {
2470   struct rpm_by_state *state = rpmstate;
2471   unsigned char buf[16];
2472   DBT dbkey;
2473   DBT dbdata;
2474   RpmHead *rpmhead;
2475
2476   if (state->dbopened != 1 && !openpkgdb(state, state->rootdir))
2477     return 0;
2478   rpmdbid2db(buf, rpmdbid, state->byteswapped);
2479   memset(&dbkey, 0, sizeof(dbkey));
2480   memset(&dbdata, 0, sizeof(dbdata));
2481   dbkey.data = buf;
2482   dbkey.size = 4;
2483   dbdata.data = 0;
2484   dbdata.size = 0;
2485   if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2486     {
2487       perror("db->get");
2488       return 0;
2489     }
2490   if (dbdata.size < 8)
2491     {
2492       fprintf(stderr, "corrupt rpm database (size)\n");
2493       return 0;
2494     }
2495   if (dbdata.size > state->rpmheadsize)
2496     {
2497       state->rpmheadsize = dbdata.size + 128;
2498       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2499     }
2500   rpmhead = state->rpmhead;
2501   memcpy(buf, dbdata.data, 8);
2502   rpmhead->forcebinary = 1;
2503   rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
2504   rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
2505   if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2506     {
2507       fprintf(stderr, "corrupt rpm database (data size)\n");
2508       return 0;
2509     }
2510   memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2511   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2512   return rpmhead;
2513 }
2514
2515 void *
2516 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2517 {
2518   struct rpm_by_state *state = rpmstate;
2519   /* int headerstart, headerend; */
2520   RpmHead *rpmhead;
2521   unsigned int sigdsize, sigcnt, l;
2522   unsigned char lead[4096];
2523   int forcebinary = 0;
2524
2525   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2526     {
2527       fprintf(stderr, "%s: not a rpm\n", name);
2528       return 0;
2529     }
2530   forcebinary = lead[6] != 0 || lead[7] != 1;
2531   if (lead[78] != 0 || lead[79] != 5)
2532     {
2533       fprintf(stderr, "%s: not a V5 header\n", name);
2534       return 0;
2535     }
2536   if (getu32(lead + 96) != 0x8eade801)
2537     {
2538       fprintf(stderr, "%s: bad signature header\n", name);
2539       return 0;
2540     }
2541   sigcnt = getu32(lead + 96 + 8);
2542   sigdsize = getu32(lead + 96 + 12);
2543   if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2544     {
2545       fprintf(stderr, "%s: bad signature header\n", name);
2546       return 0;
2547     }
2548   sigdsize += sigcnt * 16;
2549   sigdsize = (sigdsize + 7) & ~7;
2550   /* headerstart = 96 + 16 + sigdsize; */
2551   while (sigdsize)
2552     {
2553       l = sigdsize > 4096 ? 4096 : sigdsize;
2554       if (fread(lead, l, 1, fp) != 1)
2555         {
2556           fprintf(stderr, "%s: unexpected EOF\n", name);
2557           return 0;
2558         }
2559       sigdsize -= l;
2560     }
2561   if (fread(lead, 16, 1, fp) != 1)
2562     {
2563       fprintf(stderr, "%s: unexpected EOF\n", name);
2564       return 0;
2565     }
2566   if (getu32(lead) != 0x8eade801)
2567     {
2568       fprintf(stderr, "%s: bad header\n", name);
2569       fclose(fp);
2570       return 0;
2571     }
2572   sigcnt = getu32(lead + 8);
2573   sigdsize = getu32(lead + 12);
2574   if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2575     {
2576       fprintf(stderr, "%s: bad header\n", name);
2577       fclose(fp);
2578       return 0;
2579     }
2580   l = sigdsize + sigcnt * 16;
2581   /* headerend = headerstart + 16 + l; */
2582   if (l > state->rpmheadsize)
2583     {
2584       state->rpmheadsize = l + 128;
2585       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2586     }
2587   rpmhead = state->rpmhead;
2588   if (fread(rpmhead->data, l, 1, fp) != 1)
2589     {
2590       fprintf(stderr, "%s: unexpected EOF\n", name);
2591       fclose(fp);
2592       return 0;
2593     }
2594   rpmhead->forcebinary = forcebinary;
2595   rpmhead->cnt = sigcnt;
2596   rpmhead->dcnt = sigdsize;
2597   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2598   return rpmhead;
2599 }
2600
2601 #ifdef ENABLE_RPMDB_BYRPMHEADER
2602
2603 void *
2604 rpm_byrpmh(void *rpmstate, Header h)
2605 {
2606   struct rpm_by_state *state = rpmstate;
2607   const unsigned char *uh;
2608   unsigned int sigdsize, sigcnt, l;
2609   RpmHead *rpmhead;
2610
2611 #ifndef RPM5
2612   uh = headerUnload(h);
2613 #else
2614   uh = headerUnload(h, NULL);
2615 #endif
2616   if (!uh)
2617     return 0;
2618   sigcnt = getu32(uh);
2619   sigdsize = getu32(uh + 4);
2620   l = sigdsize + sigcnt * 16;
2621   if (l > state->rpmheadsize)
2622     {
2623       state->rpmheadsize = l + 128;
2624       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2625     }
2626   rpmhead = state->rpmhead;
2627   memcpy(rpmhead->data, uh + 8, l - 8);
2628   free((void *)uh);
2629   rpmhead->forcebinary = 0;
2630   rpmhead->cnt = sigcnt;
2631   rpmhead->dcnt = sigdsize;
2632   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2633   return rpmhead;
2634 }
2635
2636 #endif
2637
2638
2639 #ifdef ENABLE_RPMDB_PUBKEY
2640
2641 static char *
2642 r64dec1(char *p, unsigned int *vp, int *eofp)
2643 {
2644   int i, x;
2645   unsigned int v = 0;
2646
2647   for (i = 0; i < 4; )
2648     {
2649       x = *p++;
2650       if (!x)
2651         return 0;
2652       if (x >= 'A' && x <= 'Z')
2653         x -= 'A';
2654       else if (x >= 'a' && x <= 'z')
2655         x -= 'a' - 26;
2656       else if (x >= '0' && x <= '9')
2657         x -= '0' - 52;
2658       else if (x == '+')
2659         x = 62;
2660       else if (x == '/')
2661         x = 63;
2662       else if (x == '=')
2663         {
2664           x = 0;
2665           if (i == 0)
2666             {
2667               *eofp = 3;
2668               *vp = 0;
2669               return p - 1;
2670             }
2671           *eofp += 1;
2672         }
2673       else
2674         continue;
2675       v = v << 6 | x;
2676       i++;
2677     }
2678   *vp = v;
2679   return p;
2680 }
2681
2682 static unsigned int
2683 crc24(unsigned char *p, int len)
2684 {
2685   unsigned int crc = 0xb704ceL;
2686   int i;
2687
2688   while (len--)
2689     {
2690       crc ^= (*p++) << 16;
2691       for (i = 0; i < 8; i++)
2692         if ((crc <<= 1) & 0x1000000)
2693           crc ^= 0x1864cfbL;
2694     }
2695   return crc & 0xffffffL;
2696 }
2697
2698 static unsigned char *
2699 unarmor(char *pubkey, int *pktlp)
2700 {
2701   char *p;
2702   int l, eof;
2703   unsigned char *buf, *bp;
2704   unsigned int v;
2705
2706   *pktlp = 0;
2707   while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2708     {
2709       pubkey = strchr(pubkey, '\n');
2710       if (!pubkey)
2711         return 0;
2712       pubkey++;
2713     }
2714   pubkey = strchr(pubkey, '\n');
2715   if (!pubkey++)
2716     return 0;
2717   /* skip header lines */
2718   for (;;)
2719     {
2720       while (*pubkey == ' ' || *pubkey == '\t')
2721         pubkey++;
2722       if (*pubkey == '\n')
2723         break;
2724       pubkey = strchr(pubkey, '\n');
2725       if (!pubkey++)
2726         return 0;
2727     }
2728   pubkey++;
2729   p = strchr(pubkey, '=');
2730   if (!p)
2731     return 0;
2732   l = p - pubkey;
2733   bp = buf = solv_malloc(l * 3 / 4 + 4);
2734   eof = 0;
2735   while (!eof)
2736     {
2737       pubkey = r64dec1(pubkey, &v, &eof);
2738       if (!pubkey)
2739         {
2740           solv_free(buf);
2741           return 0;
2742         }
2743       *bp++ = v >> 16;
2744       *bp++ = v >> 8;
2745       *bp++ = v;
2746     }
2747   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2748     pubkey++;
2749   bp -= eof;
2750   if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2751     {
2752       solv_free(buf);
2753       return 0;
2754     }
2755   if (v != crc24(buf, bp - buf))
2756     {
2757       solv_free(buf);
2758       return 0;
2759     }
2760   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2761     pubkey++;
2762   if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2763     {
2764       solv_free(buf);
2765       return 0;
2766     }
2767   *pktlp = bp - buf;
2768   return buf;
2769 }
2770
2771 static void
2772 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2773 {
2774   int x, tag, l;
2775   unsigned char keyid[8];
2776   unsigned int kcr = 0, maxex = 0;
2777   unsigned char *pubkey = 0;
2778   unsigned char *userid = 0;
2779 #if 0
2780   int pubkeyl = 0;
2781   int useridl = 0;
2782 #endif
2783
2784   for (; pl; p += l, pl -= l)
2785     {
2786       x = *p++;
2787       pl--;
2788       if (!(x & 128) || pl <= 0)
2789         return;
2790       if ((x & 64) == 0)
2791         {
2792           /* old format */
2793           tag = (x & 0x3c) >> 2;
2794           x &= 3;
2795           if (x == 3)
2796             return;
2797           l = 1 << x;
2798           if (pl < l)
2799             return;
2800           x = 0;
2801           while (l--)
2802             {
2803               x = x << 8 | *p++;
2804               pl--;
2805             }
2806           l = x;
2807         }
2808       else
2809         {
2810           tag = (x & 0x3f);
2811           x = *p++;
2812           pl--;
2813           if (x < 192)
2814             l = x;
2815           else if (x >= 192 && x < 224)
2816             {
2817               if (pl <= 0)
2818                 return;
2819               l = ((x - 192) << 8) + *p++ + 192;
2820               pl--;
2821             }
2822           else if (x == 255)
2823             {
2824               /* sanity: p[0] must be zero */
2825               if (pl <= 4 || p[0] != 0)
2826                 return;
2827               l = p[1] << 16 | p[2] << 8 | p[3];
2828               p += 4;
2829               pl -= 4;
2830             }
2831           else
2832             return;
2833         }
2834       if (pl < l)
2835         return;
2836       if (tag == 6)
2837         {
2838           pubkey = solv_realloc(pubkey, l);
2839           if (l)
2840             memcpy(pubkey, p, l);
2841 #if 0
2842           pubkeyl = l;
2843 #endif
2844           kcr = 0;
2845           if (p[0] == 3)
2846             {
2847               unsigned int ex;
2848               void *h;
2849               kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2850               ex = 0;
2851               if (p[5] || p[6])
2852                 {
2853                   ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2854                   if (ex > maxex)
2855                     maxex = ex;
2856                 }
2857               memset(keyid, 0, 8);
2858               if (p[7] == 1)    /* RSA */
2859                 {
2860                   int i, ql;
2861                   unsigned char fp[16];
2862                   char fpx[32 + 1];
2863                   unsigned char *q;
2864
2865                   ql = ((p[8] << 8 | p[9]) + 7) / 8;
2866                   memcpy(keyid, p + 10 + ql - 8, 8);
2867                   h = solv_chksum_create(REPOKEY_TYPE_MD5);
2868                   solv_chksum_add(h, p + 10, ql);
2869                   q = p + 10 + ql;
2870                   ql = ((q[0] << 8 | q[1]) + 7) / 8;
2871                   solv_chksum_add(h, q + 2, ql);
2872                   solv_chksum_free(h, fp);
2873                   for (i = 0; i < 16; i++)
2874                     sprintf(fpx + i * 2, "%02x", fp[i]);
2875                   repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2876                 }
2877             }
2878           else if (p[0] == 4)
2879             {
2880               int i;
2881               void *h;
2882               unsigned char hdr[3];
2883               unsigned char fp[20];
2884               char fpx[40 + 1];
2885
2886               kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2887               hdr[0] = 0x99;
2888               hdr[1] = l >> 8;
2889               hdr[2] = l;
2890               h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2891               solv_chksum_add(h, hdr, 3);
2892               solv_chksum_add(h, p, l);
2893               solv_chksum_free(h, fp);
2894               for (i = 0; i < 20; i++)
2895                 sprintf(fpx + i * 2, "%02x", fp[i]);
2896               repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2897               memcpy(keyid, fp + 12, 8);
2898             }
2899         }
2900       if (tag == 2)
2901         {
2902           if (p[0] == 3 && p[1] == 5)
2903             {
2904 #if 0
2905               Id htype = 0;
2906 #endif
2907               /* printf("V3 signature packet\n"); */
2908               if (l < 17)
2909                 continue;
2910               if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2911                 continue;
2912               if (!memcmp(keyid, p + 6, 8))
2913                 {
2914                   /* printf("SELF SIG\n"); */
2915                 }
2916               else
2917                 {
2918                   /* printf("OTHER SIG\n"); */
2919                 }
2920 #if 0
2921               if (p[16] == 1)
2922                 htype = REPOKEY_TYPE_MD5;
2923               else if (p[16] == 2)
2924                 htype = REPOKEY_TYPE_SHA1;
2925               else if (p[16] == 8)
2926                 htype = REPOKEY_TYPE_SHA256;
2927               if (htype)
2928                 {
2929                   void *h = solv_chksum_create(htype);
2930                   unsigned char b[3], *cs;
2931
2932                   b[0] = 0x99;
2933                   b[1] = pubkeyl >> 8;
2934                   b[2] = pubkeyl;
2935                   solv_chksum_add(h, b, 3);
2936                   solv_chksum_add(h, pubkey, pubkeyl);
2937                   if (p[2] >= 0x10 && p[2] <= 0x13)
2938                     solv_chksum_add(h, userid, useridl);
2939                   solv_chksum_add(h, p + 2, 5);
2940                   cs = solv_chksum_get(h, 0);
2941                   solv_chksum_free(h, 0);
2942                 }
2943 #endif
2944             }
2945           if (p[0] == 4)
2946             {
2947               int j, ql, haveissuer;
2948               unsigned char *q;
2949               unsigned int ex = 0;
2950 #if 0
2951               unsigned int scr = 0;
2952 #endif
2953               unsigned char issuer[8];
2954
2955               /* printf("V4 signature packet\n"); */
2956               if (l < 6)
2957                 continue;
2958               if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2959                 continue;
2960               haveissuer = 0;
2961               ex = 0;
2962               q = p + 4;
2963               for (j = 0; q && j < 2; j++)
2964                 {
2965                   if (q + 2 > p + l)
2966                     {
2967                       q = 0;
2968                       break;
2969                     }
2970                   ql = q[0] << 8 | q[1];
2971                   q += 2;
2972                   if (q + ql > p + l)
2973                     {
2974                       q = 0;
2975                       break;
2976                     }
2977                   while (ql)
2978                     {
2979                       int sl;
2980                       x = *q++;
2981                       ql--;
2982                       if (x < 192)
2983                         sl = x;
2984                       else if (x == 255)
2985                         {
2986                           if (ql < 4 || q[0] != 0)
2987                             {
2988                               q = 0;
2989                               break;
2990                             }
2991                           sl = q[1] << 16 | q[2] << 8 | q[3];
2992                           q += 4;
2993                           ql -= 4;
2994                         }
2995                       else
2996                         {
2997                           if (ql < 1)
2998                             {
2999                               q = 0;
3000                               break;
3001                             }
3002                           sl = ((x - 192) << 8) + *q++ + 192;
3003                           ql--;
3004                         }
3005                       if (ql < sl)
3006                         {
3007                           q = 0;
3008                           break;
3009                         }
3010                       x = q[0] & 127;
3011                       /* printf("%d SIGSUB %d %d\n", j, x, sl); */
3012                       if (x == 16 && sl == 9 && !haveissuer)
3013                         {
3014                           memcpy(issuer, q + 1, 8);
3015                           haveissuer = 1;
3016                         }
3017 #if 0
3018                       if (x == 2 && j == 0)
3019                         scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3020 #endif
3021                       if (x == 9 && j == 0)
3022                         ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3023                       q += sl;
3024                       ql -= sl;
3025                     }
3026                 }
3027               if (ex)
3028                 ex += kcr;
3029               if (haveissuer)
3030                 {
3031 #if 0
3032                   Id htype = 0;
3033                   if (p[3] == 1)
3034                     htype = REPOKEY_TYPE_MD5;
3035                   else if (p[3] == 2)
3036                     htype = REPOKEY_TYPE_SHA1;
3037                   else if (p[3] == 8)
3038                     htype = REPOKEY_TYPE_SHA256;
3039                   if (htype && pubkeyl)
3040                     {
3041                       void *h = solv_chksum_create(htype);
3042                       unsigned char b[6], *cs;
3043                       unsigned int hl;
3044
3045                       b[0] = 0x99;
3046                       b[1] = pubkeyl >> 8;
3047                       b[2] = pubkeyl;
3048                       solv_chksum_add(h, b, 3);
3049                       solv_chksum_add(h, pubkey, pubkeyl);
3050                       if (p[1] >= 0x10 && p[1] <= 0x13)
3051                         {
3052                           b[0] = 0xb4;
3053                           b[1] = useridl >> 24;
3054                           b[2] = useridl >> 16;
3055                           b[3] = useridl >> 8;
3056                           b[4] = useridl;
3057                           solv_chksum_add(h, b, 5);
3058                           solv_chksum_add(h, userid, useridl);
3059                         }
3060                       hl = 6 + (p[4] << 8 | p[5]);
3061                       solv_chksum_add(h, p, hl);
3062                       b[0] = 4;
3063                       b[1] = 0xff;
3064                       b[2] = hl >> 24;
3065                       b[3] = hl >> 16;
3066                       b[4] = hl >> 8;
3067                       b[5] = hl;
3068                       solv_chksum_add(h, b, 6);
3069                       cs = solv_chksum_get(h, 0);
3070                       solv_chksum_free(h, 0);
3071                     }
3072 #endif
3073                   if (!memcmp(keyid, issuer, 8))
3074                     {
3075                       /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
3076                       if (ex > maxex)
3077                         maxex = ex;
3078                     }
3079                   else
3080                     {
3081                       /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
3082                     }
3083                 }
3084             }
3085         }
3086       if (tag == 13)
3087         {
3088           userid = solv_realloc(userid, l);
3089           if (l)
3090             memcpy(userid, p, l);
3091 #if 0
3092           useridl = l;
3093 #endif
3094         }
3095     }
3096   if (maxex)
3097     repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3098   solv_free(pubkey);
3099   solv_free(userid);
3100 }
3101
3102 /* this is private to rpm, but rpm lacks an interface to retrieve
3103  * the values. Sigh. */
3104 struct pgpDigParams_s {
3105     const char * userid;
3106     const unsigned char * hash;
3107 #ifndef HAVE_PGPDIGGETPARAMS
3108     const char * params[4];
3109 #endif
3110     unsigned char tag;
3111     unsigned char version;               /*!< version number. */
3112     unsigned char time[4];               /*!< time that the key was created. */
3113     unsigned char pubkey_algo;           /*!< public key algorithm. */
3114     unsigned char hash_algo;
3115     unsigned char sigtype;
3116     unsigned char hashlen;
3117     unsigned char signhash16[2];
3118     unsigned char signid[8];
3119     unsigned char saved;
3120 };
3121
3122 #ifndef HAVE_PGPDIGGETPARAMS
3123 struct pgpDig_s {
3124     struct pgpDigParams_s signature;
3125     struct pgpDigParams_s pubkey;
3126 };
3127 #endif
3128
3129 static int
3130 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3131 {
3132   Pool *pool = s->repo->pool;
3133   unsigned char *pkts;
3134   unsigned int btime;
3135   int pktsl, i;
3136   pgpDig dig = 0;
3137   char keyid[16 + 1];
3138   char evrbuf[8 + 1 + 8 + 1];
3139   struct pgpDigParams_s *digpubkey;
3140
3141   pkts = unarmor(pubkey, &pktsl);
3142   if (!pkts)
3143     return 0;
3144   setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3145   parsekeydata(s, data, pkts, pktsl);
3146   /* only rpm knows how to do the release calculation, we don't dare
3147    * to recreate all the bugs */
3148 #ifndef RPM5
3149   dig = pgpNewDig();
3150 #else
3151   dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3152 #endif
3153   (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3154
3155 #ifdef HAVE_PGPDIGGETPARAMS
3156   digpubkey = pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY);
3157 #else
3158   digpubkey = &dig->pubkey;
3159 #endif
3160   btime = digpubkey->time[0] << 24 | digpubkey->time[1] << 16 | digpubkey->time[2] << 8 | digpubkey->signid[3];
3161   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]);
3162
3163   repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3164
3165   s->name = pool_str2id(pool, "gpg-pubkey", 1);
3166   s->evr = pool_str2id(pool, evrbuf, 1);
3167   s->arch = 1;
3168   for (i = 0; i < 8; i++)
3169     sprintf(keyid + 2 * i, "%02x", digpubkey->signid[i]);
3170   repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3171   if (digpubkey->userid)
3172     setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, digpubkey->userid);
3173 #ifndef RPM5
3174   (void)pgpFreeDig(dig);
3175 #else
3176   (void)pgpDigFree(dig);
3177 #endif
3178   solv_free((void *)pkts);
3179   return 1;
3180 }
3181
3182 int
3183 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
3184 {
3185   Pool *pool = repo->pool;
3186   struct rpm_by_state state;
3187   struct rpmdbentry *entries;
3188   int nentries, i;
3189   char *str;
3190   unsigned int u32;
3191   Repodata *data;
3192   Solvable *s;
3193   const char *rootdir = 0;
3194
3195   data = repo_add_repodata(repo, flags);
3196   if (flags & REPO_USE_ROOTDIR)
3197     rootdir = pool_get_rootdir(pool);
3198
3199   memset(&state, 0, sizeof(state));
3200   state.pool = pool;
3201   if (!(state.dbenv = opendbenv(rootdir)))
3202     return 0;
3203   entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, 0);
3204   for (i = 0 ; i < nentries; i++)
3205     {
3206       RpmHead *rpmhead = rpm_byrpmdbid(&state, entries[i].rpmdbid);
3207       if (!rpmhead)
3208         continue;
3209       str = headstring(rpmhead, TAG_DESCRIPTION);
3210       if (!str)
3211         continue;
3212       s = pool_id2solvable(pool, repo_add_solvable(repo));
3213       pubkey2solvable(s, data, str);
3214       u32 = headint32(rpmhead, TAG_INSTALLTIME);
3215       if (u32)
3216         repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3217       if (!repo->rpmdbid)
3218         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3219       repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3220     }
3221   solv_free(entries);
3222   freestate(&state);
3223   if (!(flags & REPO_NO_INTERNALIZE))
3224     repodata_internalize(data);
3225   return 0;
3226 }
3227
3228 Id
3229 repo_add_pubkey(Repo *repo, const char *key, int flags)
3230 {
3231   Pool *pool = repo->pool;
3232   Repodata *data;
3233   Solvable *s;
3234   char *buf;
3235   int bufl, l, ll;
3236   FILE *fp;
3237
3238   data = repo_add_repodata(repo, flags);
3239   buf = 0;
3240   bufl = 0;
3241   if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
3242     {
3243       pool_error(pool, -1, "%s: %s", key, strerror(errno));
3244       return 0;
3245     }
3246   for (l = 0; ;)
3247     {
3248       if (bufl - l < 4096)
3249         {
3250           bufl += 4096;
3251           buf = solv_realloc(buf, bufl);
3252         }
3253       ll = fread(buf, 1, bufl - l, fp);
3254       if (ll < 0)
3255         {
3256           fclose(fp);
3257           pool_error(pool, -1, "%s: %s", key, strerror(errno));
3258           return 0;
3259         }
3260       if (ll == 0)
3261         break;
3262       l += ll;
3263     }
3264   buf[l] = 0;
3265   fclose(fp);
3266   s = pool_id2solvable(pool, repo_add_solvable(repo));
3267   if (!pubkey2solvable(s, data, buf))
3268     {
3269       repo_free_solvable(repo, s - pool->solvables, 1);
3270       solv_free(buf);
3271       return 0;
3272     }
3273   solv_free(buf);
3274   if (!(flags & REPO_NO_INTERNALIZE))
3275     repodata_internalize(data);
3276   return s - pool->solvables;
3277 }
3278
3279 #endif /* ENABLE_RPMDB_PUBKEY */