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