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