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