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