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