c27d488318e61e33f5dcc80755774c17de5d2ae4
[platform/upstream/libsolv.git] / tools / repo_rpmdb.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_rpmdb
10  * 
11  * convert rpm db to repo
12  * 
13  */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <assert.h>
24
25 #include <rpm/db.h>
26
27 #include "pool.h"
28 #include "repo.h"
29 #include "hash.h"
30 #include "util.h"
31 #include "repo_rpmdb.h"
32
33 #define RPMDB_COOKIE_VERSION 2
34
35 #define TAG_NAME                1000
36 #define TAG_VERSION             1001
37 #define TAG_RELEASE             1002
38 #define TAG_EPOCH               1003
39 #define TAG_SUMMARY             1004
40 #define TAG_DESCRIPTION         1005
41 #define TAG_BUILDTIME           1006
42 #define TAG_BUILDHOST           1007
43 #define TAG_INSTALLTIME         1008
44 #define TAG_SIZE                1009
45 #define TAG_DISTRIBUTION        1010
46 #define TAG_VENDOR              1011
47 #define TAG_LICENSE             1014
48 #define TAG_PACKAGER            1015
49 #define TAG_GROUP               1016
50 #define TAG_URL                 1020
51 #define TAG_ARCH                1022
52 #define TAG_FILESIZES           1028
53 #define TAG_FILEMODES           1030
54 #define TAG_SOURCERPM           1044
55 #define TAG_PROVIDENAME         1047
56 #define TAG_REQUIREFLAGS        1048
57 #define TAG_REQUIRENAME         1049
58 #define TAG_REQUIREVERSION      1050
59 #define TAG_NOSOURCE            1051
60 #define TAG_NOPATCH             1052
61 #define TAG_CONFLICTFLAGS       1053
62 #define TAG_CONFLICTNAME        1054
63 #define TAG_CONFLICTVERSION     1055
64 #define TAG_OBSOLETENAME        1090
65 #define TAG_FILEDEVICES         1095
66 #define TAG_FILEINODES          1096
67 #define TAG_PROVIDEFLAGS        1112
68 #define TAG_PROVIDEVERSION      1113
69 #define TAG_OBSOLETEFLAGS       1114
70 #define TAG_OBSOLETEVERSION     1115
71 #define TAG_DIRINDEXES          1116
72 #define TAG_BASENAMES           1117
73 #define TAG_DIRNAMES            1118
74 #define TAG_PAYLOADFORMAT       1124
75 #define TAG_PATCHESNAME         1133
76 #define TAG_SUGGESTSNAME        1156
77 #define TAG_SUGGESTSVERSION     1157
78 #define TAG_SUGGESTSFLAGS       1158
79 #define TAG_ENHANCESNAME        1159
80 #define TAG_ENHANCESVERSION     1160
81 #define TAG_ENHANCESFLAGS       1161
82
83 #define DEP_LESS                (1 << 1)
84 #define DEP_GREATER             (1 << 2)
85 #define DEP_EQUAL               (1 << 3)
86 #define DEP_STRONG              (1 << 27)
87 #define DEP_PRE                 ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
88
89
90 struct rpmid {
91   unsigned int dbid;
92   char *name;
93 };
94
95 typedef struct rpmhead {
96   int cnt;
97   int dcnt;
98   unsigned char *dp;
99   unsigned char data[1];
100 } RpmHead;
101
102 static int
103 headexists(RpmHead *h, int tag)
104 {
105   unsigned int i;
106   unsigned char *d, taga[4];
107
108   d = h->dp - 16;
109   taga[0] = tag >> 24;
110   taga[1] = tag >> 16;
111   taga[2] = tag >> 8;
112   taga[3] = tag;
113   for (i = 0; i < h->cnt; i++, d -= 16)
114     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
115       return 1;
116   return 0;
117 }
118
119 static unsigned int *
120 headint32array(RpmHead *h, int tag, int *cnt)
121 {
122   unsigned int i, o, *r;
123   unsigned char *d, taga[4];
124
125   d = h->dp - 16;
126   taga[0] = tag >> 24;
127   taga[1] = tag >> 16;
128   taga[2] = tag >> 8;
129   taga[3] = tag;
130   for (i = 0; i < h->cnt; i++, d -= 16)
131     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
132       break;
133   if (i >= h->cnt)
134     return 0;
135   if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
136     return 0;
137   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
138   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
139   if (o + 4 * i > h->dcnt)
140     return 0;
141   d = h->dp + o;
142   r = sat_calloc(i ? i : 1, sizeof(unsigned int));
143   if (cnt)
144     *cnt = i;
145   for (o = 0; o < i; o++, d += 4)
146     r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
147   return r;
148 }
149
150 static unsigned int
151 headint32(RpmHead *h, int tag)
152 {
153   unsigned int i, o;
154   unsigned char *d, taga[4];
155
156   d = h->dp - 16; 
157   taga[0] = tag >> 24; 
158   taga[1] = tag >> 16; 
159   taga[2] = tag >> 8;
160   taga[3] = tag;
161   for (i = 0; i < h->cnt; i++, d -= 16) 
162     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
163       break;
164   if (i >= h->cnt)
165     return 0;
166   if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
167     return 0;
168   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
169   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
170   if (i == 0 || o + 4 * i > h->dcnt)
171     return 0;
172   d = h->dp + o;
173   return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
174 }
175
176 static unsigned int *
177 headint16array(RpmHead *h, int tag, int *cnt)
178 {
179   unsigned int i, o, *r;
180   unsigned char *d, taga[4];
181
182   d = h->dp - 16;
183   taga[0] = tag >> 24;
184   taga[1] = tag >> 16;
185   taga[2] = tag >> 8;
186   taga[3] = tag;
187   for (i = 0; i < h->cnt; i++, d -= 16)
188     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
189       break;
190   if (i >= h->cnt)
191     return 0;
192   if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
193     return 0;
194   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
195   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
196   if (o + 4 * i > h->dcnt)
197     return 0;
198   d = h->dp + o;
199   r = sat_calloc(i ? i : 1, sizeof(unsigned int));
200   if (cnt)
201     *cnt = i;
202   for (o = 0; o < i; o++, d += 2)
203     r[o] = d[0] << 8 | d[1];
204   return r;
205 }
206
207 static char *
208 headstring(RpmHead *h, int tag)
209 {
210   unsigned int i, o;
211   unsigned char *d, taga[4];
212   d = h->dp - 16;
213   taga[0] = tag >> 24;
214   taga[1] = tag >> 16;
215   taga[2] = tag >> 8;
216   taga[3] = tag;
217   for (i = 0; i < h->cnt; i++, d -= 16)
218     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
219       break;
220   if (i >= h->cnt)
221     return 0;
222   /* 6: STRING, 9: I18NSTRING */
223   if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
224     return 0;
225   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
226   return (char *)h->dp + o;
227 }
228
229 static char **
230 headstringarray(RpmHead *h, int tag, int *cnt)
231 {
232   unsigned int i, o;
233   unsigned char *d, taga[4];
234   char **r;
235
236   d = h->dp - 16;
237   taga[0] = tag >> 24;
238   taga[1] = tag >> 16;
239   taga[2] = tag >> 8;
240   taga[3] = tag;
241   for (i = 0; i < h->cnt; i++, d -= 16)
242     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
243       break;
244   if (i >= h->cnt)
245     return 0;
246   if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
247     return 0;
248   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
249   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
250   r = sat_calloc(i ? i : 1, sizeof(char *));
251   if (cnt)
252     *cnt = i;
253   d = h->dp + o;
254   for (o = 0; o < i; o++)
255     {
256       r[o] = (char *)d;
257       if (o + 1 < i)
258         d += strlen((char *)d) + 1;
259       if (d >= h->dp + h->dcnt)
260         {
261           sat_free(r);
262           return 0;
263         }
264     }
265   return r;
266 }
267
268 static char *headtoevr(RpmHead *h)
269 {
270   unsigned int epoch;
271   char *version, *v;
272   char *release;
273   char *evr;
274
275   version  = headstring(h, TAG_VERSION);
276   release  = headstring(h, TAG_RELEASE);
277   epoch = headint32(h, TAG_EPOCH);
278   if (!version || !release)
279     {
280       fprintf(stderr, "headtoevr: bad rpm header\n");
281       exit(1);
282     }
283   for (v = version; *v >= 0 && *v <= '9'; v++)
284     ;
285   if (epoch || (v != version && *v == ':'))
286     {
287       char epochbuf[11];        /* 32bit decimal will fit in */
288       sprintf(epochbuf, "%u", epoch);
289       evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
290       sprintf(evr, "%s:%s-%s", epochbuf, version, release);
291     }
292   else
293     {
294       evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
295       sprintf(evr, "%s-%s", version, release);
296     }
297   return evr;
298 }
299
300
301 static void
302 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
303 {
304   const unsigned char *cp;
305   int state = 0;
306   int c;
307   char *buf = 0, *bp;
308
309   /* check if it's already utf8, code taken from screen ;-) */
310   cp = (const unsigned char *)str;
311   while ((c = *cp++) != 0)
312     {
313       if (state)
314         {
315           if ((c & 0xc0) != 0x80)
316             break; /* encoding error */
317           c = (c & 0x3f) | (state << 6);
318           if (!(state & 0x40000000))
319             {
320               /* check for overlong sequences */
321               if ((c & 0x820823e0) == 0x80000000)
322                 c = 0xfdffffff;
323               else if ((c & 0x020821f0) == 0x02000000)
324                 c = 0xfff7ffff;
325               else if ((c & 0x000820f8) == 0x00080000)
326                 c = 0xffffd000;
327               else if ((c & 0x0000207c) == 0x00002000)
328                 c = 0xffffff70;
329             }
330         }
331       else
332         {
333           /* new sequence */
334           if (c >= 0xfe)
335             c = 0xfffd;
336           else if (c >= 0xfc)
337             c = (c & 0x01) | 0xbffffffc;    /* 5 bytes to follow */
338           else if (c >= 0xf8)
339             c = (c & 0x03) | 0xbfffff00;    /* 4 */
340           else if (c >= 0xf0)
341             c = (c & 0x07) | 0xbfffc000;    /* 3 */
342           else if (c >= 0xe0)
343             c = (c & 0x0f) | 0xbff00000;    /* 2 */
344           else if (c >= 0xc2)
345             c = (c & 0x1f) | 0xfc000000;    /* 1 */
346           else if (c >= 0xc0)
347             c = 0xfdffffff;         /* overlong */
348           else if (c >= 0x80)
349             c = 0xfffd;
350         }
351       state = (c & 0x80000000) ? c : 0;
352     }
353   if (c)
354     {
355       /* not utf8, assume latin1 */
356       buf = sat_malloc(2 * strlen(str) + 1);
357       cp = (const unsigned char *)str;
358       str = buf;
359       bp = buf;
360       while ((c = *cp++) != 0)
361         {
362           if (c >= 0xc0)
363             {
364               *bp++ = 0xc3;
365               c ^= 0x80;
366             }
367           else if (c >= 0x80)
368             *bp++ = 0xc2;
369           *bp++ = c;
370         }
371       *bp++ = 0;
372     }
373   repodata_set_str(repodata, handle, tag, str);
374   if (buf)
375     sat_free(buf);
376 }
377
378 static unsigned int
379 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
380 {
381   char **n, **v;
382   unsigned int *f;
383   int i, cc, nc, vc, fc;
384   int haspre = 0;
385   unsigned int olddeps;
386   Id *ida;
387
388   n = headstringarray(rpmhead, tagn, &nc);
389   if (!n)
390     return 0;
391   v = headstringarray(rpmhead, tagv, &vc);
392   if (!v)
393     {
394       sat_free(n);
395       return 0;
396     }
397   f = headint32array(rpmhead, tagf, &fc);
398   if (!f)
399     {
400       sat_free(n);
401       free(v);
402       return 0;
403     }
404   if (nc != vc || nc != fc)
405     {
406       fprintf(stderr, "bad dependency entries\n");
407       exit(1);
408     }
409
410   cc = nc;
411   if (strong)
412     {
413       cc = 0;
414       for (i = 0; i < nc; i++)
415         if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
416           {
417             cc++;
418             if ((f[i] & DEP_PRE) != 0)
419               haspre = 1;
420           }
421     }
422   else
423     {
424       for (i = 0; i < nc; i++)
425         if ((f[i] & DEP_PRE) != 0)
426           {
427             haspre = 1;
428             break;
429           }
430     }
431   if (tagn != TAG_REQUIRENAME)
432      haspre = 0;
433   if (cc == 0)
434     {
435       sat_free(n);
436       sat_free(v);
437       sat_free(f);
438       return 0;
439     }
440   cc += haspre;
441   olddeps = repo_reserve_ids(repo, 0, cc);
442   ida = repo->idarraydata + olddeps;
443   for (i = 0; ; i++)
444     {
445       if (i == nc)
446         {
447           if (haspre != 1)
448             break;
449           haspre = 2;
450           i = 0;
451           *ida++ = SOLVABLE_PREREQMARKER;
452         }
453       if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
454         continue;
455       if (haspre == 1 && (f[i] & DEP_PRE) != 0)
456         continue;
457       if (haspre == 2 && (f[i] & DEP_PRE) == 0)
458         continue;
459       if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
460         {
461           Id name, evr;
462           int flags = 0;
463           if ((f[i] & DEP_LESS) != 0)
464             flags |= 4;
465           if ((f[i] & DEP_EQUAL) != 0)
466             flags |= 2;
467           if ((f[i] & DEP_GREATER) != 0)
468             flags |= 1;
469           name = str2id(pool, n[i], 1);
470           if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
471             evr = str2id(pool, v[i] + 2, 1);
472           else
473             evr = str2id(pool, v[i], 1);
474           *ida++ = rel2id(pool, name, evr, flags, 1);
475         }
476       else
477         *ida++ = str2id(pool, n[i], 1);
478     }
479   *ida++ = 0;
480   repo->idarraysize += cc + 1;
481   sat_free(n);
482   sat_free(v);
483   sat_free(f);
484   return olddeps;
485 }
486
487
488 #ifdef USE_FILEFILTER
489
490 #define FILEFILTER_EXACT    0
491 #define FILEFILTER_STARTS   1
492 #define FILEFILTER_CONTAINS 2
493
494 struct filefilter {
495   int dirmatch;
496   char *dir;
497   char *base;
498 };
499
500 static struct filefilter filefilters[] = {
501   { FILEFILTER_CONTAINS, "/bin/", 0},
502   { FILEFILTER_CONTAINS, "/sbin/", 0},
503   { FILEFILTER_CONTAINS, "/lib/", 0},
504   { FILEFILTER_CONTAINS, "/lib64/", 0},
505   { FILEFILTER_CONTAINS, "/etc/", 0},
506   { FILEFILTER_STARTS, "/usr/games/", 0},
507   { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
508   { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
509   { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
510 };
511
512 #endif
513
514 static void
515 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
516 {
517   Id handle, did;
518   int i, fszc;
519   unsigned int *fkb, *fn, *fsz, *fm, *fino;
520   unsigned int inotest[256], inotestok;
521
522   if (!fc)
523     return;
524   fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
525   if (!fsz || fc != fszc)
526     {
527       sat_free(fsz);
528       return;
529     }
530   /* stupid rpm recodrs sizes of directories, so we have to check the mode */
531   fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
532   if (!fm || fc != fszc)
533     {
534       sat_free(fsz);
535       sat_free(fm);
536       return;
537     }
538   fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
539   if (!fino || fc != fszc)
540     {
541       sat_free(fsz);
542       sat_free(fm);
543       sat_free(fino);
544       return;
545     }
546   inotestok = 0;
547   if (fc < sizeof(inotest))
548     {
549       memset(inotest, 0, sizeof(inotest));
550       for (i = 0; i < fc; i++)
551         {
552           int off, bit;
553           if (fsz[i] == 0 || !S_ISREG(fm[i]))
554             continue;
555           off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
556           bit = 1 << (fino[i] & 31);
557           if ((inotest[off] & bit) != 0)
558             break;
559           inotest[off] |= bit;
560         }
561       if (i == fc)
562         inotestok = 1;
563     }
564   if (!inotestok)
565     {
566       unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
567       unsigned int *fx, j;
568       unsigned int mask, hash, hh;
569       if (!fdev || fc != fszc)
570         {
571           sat_free(fsz);
572           sat_free(fm);
573           sat_free(fdev);
574           sat_free(fino);
575           return;
576         }
577       mask = fc;
578       while ((mask & (mask - 1)) != 0)
579         mask = mask & (mask - 1);
580       mask <<= 2;
581       if (mask > sizeof(inotest)/sizeof(*inotest))
582         fx = sat_calloc(mask, sizeof(unsigned int));
583       else
584         {
585           fx = inotest;
586           memset(fx, 0, mask * sizeof(unsigned int));
587         }
588       mask--;
589       for (i = 0; i < fc; i++)
590         {
591           if (fsz[i] == 0 || !S_ISREG(fm[i]))
592             continue;
593           hash = (fino[i] + fdev[i] * 31) & mask;
594           hh = 7;
595           while ((j = fx[hash]) != 0)
596             {
597               if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
598                 {
599                   fsz[i] = 0;   /* kill entry */
600                   break;
601                 }
602               hash = (hash + hh++) & mask;
603             }
604           if (!j)
605             fx[hash] = i + 1;
606         }
607       if (fx != inotest)
608         sat_free(fx);
609       sat_free(fdev);
610     }
611   sat_free(fino);
612   fn = sat_calloc(dic, sizeof(unsigned int));
613   fkb = sat_calloc(dic, sizeof(unsigned int));
614   for (i = 0; i < fc; i++)
615     {
616       if (fsz[i] == 0 || !S_ISREG(fm[i]))
617         continue;
618       if (di[i] >= dic)
619         continue;
620       fn[di[i]]++;
621       fkb[di[i]] += fsz[i] / 1024 + 1;
622     }
623   sat_free(fsz);
624   sat_free(fm);
625   /* commit */
626   repodata_extend(repodata, s - pool->solvables);
627   handle = (s - pool->solvables) - repodata->start;
628   handle = repodata_get_handle(repodata, handle);
629   for (i = 0; i < fc; i++)
630     {
631       if (!fn[i])
632         continue;
633       if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
634         did = repodata_str2dir(repodata, "/usr/src", 1);
635       else
636         did = repodata_str2dir(repodata, dn[i], 1);
637       repodata_add_dirnumnum(repodata, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
638     }
639   sat_free(fn);
640   sat_free(fkb);
641 }
642
643 /* assumes last processed array is provides! */
644 static unsigned int
645 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
646 {
647   char **bn;
648   char **dn;
649   unsigned int *di;
650   int bnc, dnc, dic;
651   int i;
652 #ifdef USE_FILEFILTER
653   int j;
654   struct filefilter *ff;
655 #endif
656 #if 0
657   char *fn = 0;
658   int fna = 0;
659 #endif
660
661   if (!repodata)
662     return olddeps;
663   bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
664   if (!bn)
665     return olddeps;
666   dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
667   if (!dn)
668     {
669       sat_free(bn);
670       return olddeps;
671     }
672   di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
673   if (!di)
674     {
675       sat_free(bn);
676       sat_free(dn);
677       return olddeps;
678     }
679   if (bnc != dic)
680     {
681       fprintf(stderr, "bad filelist\n");
682       exit(1);
683     }
684
685   if (repodata)
686     adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
687
688   for (i = 0; i < bnc; i++)
689     {
690 #ifdef USE_FILEFILTER
691       ff = filefilters;
692       for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
693         {
694           if (ff->dir)
695             {
696               switch (ff->dirmatch)
697                 {
698                 case FILEFILTER_STARTS:
699                   if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
700                     continue;
701                   break;
702                 case FILEFILTER_CONTAINS:
703                   if (!strstr(dn[di[i]], ff->dir))
704                     continue;
705                   break;
706                 case FILEFILTER_EXACT:
707                 default:
708                   if (strcmp(dn[di[i]], ff->dir))
709                     continue;
710                   break;
711                 }
712             }
713           if (ff->base)
714             {
715               if (strcmp(bn[i], ff->base))
716                 continue;
717             }
718           break;
719         }
720       if (j == sizeof(filefilters)/sizeof(*filefilters))
721         continue;
722 #endif
723 #if 0
724       j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
725       if (j > fna)
726         {
727           fna = j + 256;
728           fn = sat_realloc(fn, fna);
729         }
730       strcpy(fn, dn[di[i]]);
731       strcat(fn, bn[i]);
732       olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
733 #endif
734       if (repodata)
735         {
736           Id handle, did;
737           repodata_extend(repodata, s - pool->solvables);
738           handle = (s - pool->solvables) - repodata->start;
739           handle = repodata_get_handle(repodata, handle);
740           did = repodata_str2dir(repodata, dn[di[i]], 1);
741           if (!did)
742             did = repodata_str2dir(repodata, "/", 1);
743           repodata_add_dirstr(repodata, handle, SOLVABLE_FILELIST, did, bn[i]);
744         }
745     }
746 #if 0
747   if (fn)
748     sat_free(fn);
749 #endif
750   sat_free(bn);
751   sat_free(dn);
752   sat_free(di);
753   return olddeps;
754 }
755
756 static void
757 addsourcerpm(Pool *pool, Repodata *repodata, Id handle, char *sourcerpm, char *name, char *evr)
758 {
759   const char *p, *sevr, *sarch;
760
761   p = strrchr(sourcerpm, '.');
762   if (!p || strcmp(p, ".rpm") != 0)
763     return;
764   p--;
765   while (p > sourcerpm && *p != '.')
766     p--;
767   if (*p != '.' || p == sourcerpm)
768     return;
769   sarch = p-- + 1;
770   while (p > sourcerpm && *p != '-')
771     p--;
772   if (*p != '-' || p == sourcerpm)
773     return;
774   p--;
775   while (p > sourcerpm && *p != '-')
776     p--;
777   if (*p != '-' || p == sourcerpm)
778     return;
779   sevr = p + 1;
780   if (!strcmp(sarch, "src.rpm"))
781     repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
782   else if (!strcmp(sarch, "nosrc.rpm"))
783     repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
784   else
785     repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
786   if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
787     repodata_set_void(repodata, handle, SOLVABLE_SOURCEEVR);
788   else
789     repodata_set_id(repodata, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
790   if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
791     repodata_set_void(repodata, handle, SOLVABLE_SOURCENAME);
792   else
793     repodata_set_id(repodata, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
794 }
795
796 static int
797 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
798 {
799   char *name;
800   char *evr;
801   char *sourcerpm;
802
803   name = headstring(rpmhead, TAG_NAME);
804   if (!strcmp(name, "gpg-pubkey"))
805     return 0;
806   s->name = str2id(pool, name, 1);
807   if (!s->name)
808     {
809       fprintf(stderr, "package has no name\n");
810       exit(1);
811     }
812   sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
813   if (sourcerpm)
814     s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
815   else
816     {
817       if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
818         s->arch = ARCH_NOSRC;
819       else
820         s->arch = ARCH_SRC;
821     }
822   if (!s->arch)
823     s->arch = ARCH_NOARCH;
824   evr = headtoevr(rpmhead);
825   s->evr = str2id(pool, evr, 1);
826   s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
827
828   s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
829   s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
830   if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
831     s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
832   s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
833   s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
834   s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
835
836   s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
837   s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
838   s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
839   s->enhances  = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
840   s->supplements = repo_fix_legacy(repo, s->provides, s->supplements, 0);
841
842   if (repodata)
843     {
844       Id handle;
845       char *str;
846       unsigned int u32;
847
848       repodata_extend(repodata, s - pool->solvables);
849       handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
850       str = headstring(rpmhead, TAG_SUMMARY);
851       if (str)
852         setutf8string(repodata, handle, SOLVABLE_SUMMARY, str);
853       str = headstring(rpmhead, TAG_DESCRIPTION);
854       if (str)
855         {
856           char *aut, *p;
857           for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
858             if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
859               break;
860           if (aut)
861             {
862               /* oh my, found SUSE special author section */
863               int l = aut - str;
864               str = strdup(str);
865               aut = str + l;
866               str[l] = 0;
867               while (l > 0 && str[l - 1] == '\n')
868                 str[--l] = 0;
869               if (l)
870                 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
871               p = aut + 19;
872               aut = str;        /* copy over */
873               while (*p == ' ' || *p == '\n')
874                 p++;
875               while (*p)
876                 {
877                   if (*p == '\n')
878                     {
879                       *aut++ = *p++;
880                       while (*p == ' ')
881                         p++;
882                       continue;
883                     }
884                   *aut++ = *p++;
885                 }
886               while (aut != str && aut[-1] == '\n')
887                 aut--;
888               *aut = 0;
889               if (*str)
890                 setutf8string(repodata, handle, SOLVABLE_AUTHORS, str);
891               free(str);
892             }
893           else if (*str)
894             setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
895         }
896       str = headstring(rpmhead, TAG_GROUP);
897       if (str)
898         repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
899       str = headstring(rpmhead, TAG_LICENSE);
900       if (str)
901         repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
902       str = headstring(rpmhead, TAG_URL);
903       if (str)
904         repodata_set_str(repodata, handle, SOLVABLE_URL, str);
905       str = headstring(rpmhead, TAG_DISTRIBUTION);
906       if (str)
907         repodata_set_poolstr(repodata, handle, SOLVABLE_DISTRIBUTION, str);
908       str = headstring(rpmhead, TAG_PACKAGER);
909       if (str)
910         repodata_set_poolstr(repodata, handle, SOLVABLE_PACKAGER, str);
911       u32 = headint32(rpmhead, TAG_BUILDTIME);
912       if (u32)
913         repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
914       u32 = headint32(rpmhead, TAG_INSTALLTIME);
915       if (u32)
916         repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
917       u32 = headint32(rpmhead, TAG_SIZE);
918       if (u32)
919         repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
920       if (sourcerpm)
921         addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
922     }
923   sat_free(evr);
924   return 1;
925 }
926
927 static Id
928 copyreldep(Pool *pool, Pool *frompool, Id id)
929 {
930   Reldep *rd = GETRELDEP(frompool, id);
931   Id name = rd->name, evr = rd->evr;
932   if (ISRELDEP(name))
933     name = copyreldep(pool, frompool, name);
934   else
935     name = str2id(pool, id2str(frompool, name), 1);
936   if (ISRELDEP(evr))
937     evr = copyreldep(pool, frompool, evr);
938   else
939     evr = str2id(pool, id2str(frompool, evr), 1);
940   return rel2id(pool, name, evr, rd->flags, 1);
941 }
942
943 static Offset
944 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
945 {
946   int cc;
947   Id id, *ida, *from;
948   Offset ido;
949   Pool *frompool = fromrepo->pool;
950
951   if (!fromoff)
952     return 0;
953   from = fromrepo->idarraydata + fromoff;
954   for (ida = from, cc = 0; *ida; ida++, cc++)
955     ;
956   if (cc == 0)
957     return 0;
958   ido = repo_reserve_ids(repo, 0, cc);
959   ida = repo->idarraydata + ido;
960   if (frompool && pool != frompool)
961     {
962       while (*from)
963         {
964           id = *from++;
965           if (ISRELDEP(id))
966             id = copyreldep(pool, frompool, id);
967           else
968             id = str2id(pool, id2str(frompool, id), 1);
969           *ida++ = id;
970         }
971       *ida = 0;
972     }
973   else
974     memcpy(ida, from, (cc + 1) * sizeof(Id));
975   repo->idarraysize += cc + 1;
976   return ido;
977 }
978
979 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
980
981 static inline Id
982 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
983 {
984   if (cache && cache[did & 255] == did)
985     return cache[(did & 255) + 256];
986   return copydir_complex(pool, data, fromspool, fromdata, did, cache);
987 }
988
989 static Id
990 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
991 {
992   Id parent = dirpool_parent(&fromdata->dirpool, did);
993   Id compid = dirpool_compid(&fromdata->dirpool, did);
994   if (parent)
995     parent = copydir(pool, data, fromspool, fromdata, parent, cache);
996   if (fromspool != &pool->ss)
997     compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
998   compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
999   if (cache)
1000     {
1001       cache[did & 255] = did;
1002       cache[(did & 255) + 256] = compid;
1003     }
1004   return compid;
1005 }
1006
1007 struct solvable_copy_cbdata {
1008   Repodata *data;
1009   Id handle;
1010   Id *dircache;
1011 };
1012
1013 static int
1014 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1015 {
1016   struct solvable_copy_cbdata *cbdata = vcbdata;
1017   Id id, keyname;
1018   Repodata *data = cbdata->data;
1019   Id handle = cbdata->handle;
1020   Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1021   Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1022
1023   keyname = key->name;
1024   if (keyname >= ID_NUM_INTERNAL)
1025     keyname = str2id(pool, id2str(frompool, keyname), 1);
1026   switch(key->type)
1027     {
1028     case REPOKEY_TYPE_ID:
1029     case REPOKEY_TYPE_CONSTANTID:
1030       id = kv->id;
1031       assert(!data->localpool); /* implement me! */
1032       if (pool != frompool || fromdata->localpool)
1033         {
1034           if (ISRELDEP(id))
1035             id = copyreldep(pool, frompool, id);
1036           else
1037             id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1038         }
1039       if (key->type == REPOKEY_TYPE_ID)
1040         repodata_set_id(data, handle, keyname, id);
1041       else
1042         repodata_set_constantid(data, handle, keyname, id);
1043       break;
1044     case REPOKEY_TYPE_STR:
1045       repodata_set_str(data, handle, keyname, kv->str);
1046       break;
1047     case REPOKEY_TYPE_VOID:
1048       repodata_set_void(data, handle, keyname);
1049       break;
1050     case REPOKEY_TYPE_NUM:
1051       repodata_set_num(data, handle, keyname, kv->num);
1052       break;
1053     case REPOKEY_TYPE_CONSTANT:
1054       repodata_set_constant(data, handle, keyname, kv->num);
1055       break;
1056     case REPOKEY_TYPE_DIRNUMNUMARRAY:
1057       id = kv->id;
1058       assert(!data->localpool); /* implement me! */
1059       id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1060       repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1061       break;
1062     case REPOKEY_TYPE_DIRSTRARRAY:
1063       id = kv->id;
1064       assert(!data->localpool); /* implement me! */
1065       id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1066       repodata_add_dirstr(data, handle, keyname, id, kv->str);
1067       break;
1068     default:
1069       break;
1070     }
1071   return 0;
1072 }
1073
1074 static void
1075 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1076 {
1077   Repo *repo = s->repo;
1078   Repo *fromrepo = r->repo;
1079   Pool *pool = repo->pool;
1080   struct solvable_copy_cbdata cbdata;
1081
1082   /* copy solvable data */
1083   if (pool == fromrepo->pool)
1084     {
1085       s->name = r->name;
1086       s->evr = r->evr;
1087       s->arch = r->arch;
1088       s->vendor = r->vendor;
1089     }
1090   else
1091     {
1092       if (r->name)
1093         s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1094       if (r->evr)
1095         s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1096       if (r->arch)
1097         s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1098       if (r->vendor)
1099         s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1100     }
1101   s->provides = copydeps(pool, repo, r->provides, fromrepo);
1102   s->requires = copydeps(pool, repo, r->requires, fromrepo);
1103   s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1104   s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1105   s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1106   s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1107   s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1108   s->enhances  = copydeps(pool, repo, r->enhances, fromrepo);
1109
1110   /* copy all attributes */
1111   if (!data)
1112     return;
1113   repodata_extend(data, s - pool->solvables);
1114   cbdata.data = data;
1115   cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
1116   cbdata.dircache = dircache;
1117   repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1118 }
1119
1120 /* used to sort entries returned in some database order */
1121 static int
1122 rpmids_sort_cmp(const void *va, const void *vb)
1123 {
1124   struct rpmid const *a = va, *b = vb;
1125   int r;
1126   r = strcmp(a->name, b->name);
1127   if (r)
1128     return r;
1129   return a->dbid - b->dbid;
1130 }
1131
1132 static Repo *pkgids_sort_cmp_data;
1133
1134 static int
1135 pkgids_sort_cmp(const void *va, const void *vb)
1136 {
1137   Pool *pool = pkgids_sort_cmp_data->pool;
1138   Solvable *a = pool->solvables + *(Id *)va;
1139   Solvable *b = pool->solvables + *(Id *)vb;
1140   Id *rpmdbid;
1141
1142   if (a->name != b->name)
1143     return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1144   rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1145   return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1146 }
1147
1148 static void
1149 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1150 {
1151   Pool *pool = repo->pool;
1152   Solvable tmp;
1153
1154   tmp = pool->solvables[pa];
1155   pool->solvables[pa] = pool->solvables[pb];
1156   pool->solvables[pb] = tmp;
1157   if (repo->rpmdbid)
1158     {
1159       Id tmpid = repo->rpmdbid[pa - repo->start];
1160       repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1161       repo->rpmdbid[pb - repo->start] = tmpid;
1162     }
1163   /* only works if nothing is already internalized! */
1164   if (data && data->attrs)
1165     {
1166       Id tmpattrs = data->attrs[pa - data->start];
1167       data->attrs[pa - data->start] = data->attrs[pb - data->start];
1168       data->attrs[pb - data->start] = tmpattrs;
1169     }
1170 }
1171
1172 static void
1173 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1174 {
1175   memset(cookie, 0, 32);
1176   cookie[3] = RPMDB_COOKIE_VERSION;
1177   memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1178   memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1179 }
1180
1181 /*
1182  * read rpm db as repo
1183  * 
1184  */
1185
1186 void
1187 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
1188 {
1189   Pool *pool = repo->pool;
1190   unsigned char buf[16];
1191   DB *db = 0;
1192   DBC *dbc = 0;
1193   int byteswapped;
1194   unsigned int dbid;
1195   unsigned char *dp, *dbidp;
1196   int dl, nrpmids;
1197   struct rpmid *rpmids, *rp;
1198   int i;
1199   int rpmheadsize;
1200   RpmHead *rpmhead;
1201   Solvable *s;
1202   Id id, *refhash;
1203   unsigned int refmask, h;
1204   int asolv;
1205   Repodata *repodata;
1206   char dbpath[PATH_MAX];
1207   DB_ENV *dbenv = 0;
1208   DBT dbkey;
1209   DBT dbdata;
1210   struct stat packagesstat;
1211
1212   memset(&dbkey, 0, sizeof(dbkey));
1213   memset(&dbdata, 0, sizeof(dbdata));
1214
1215   if (repo->start != repo->end)
1216     abort();            /* FIXME: rpmdbid */
1217
1218   if (!rootdir)
1219     rootdir = "";
1220
1221   repodata = repo_add_repodata(repo, 0);
1222
1223   if (ref && !(ref->nsolvables && ref->rpmdbid))
1224     ref = 0;
1225
1226   if (db_env_create(&dbenv, 0))
1227     {
1228       perror("db_env_create");
1229       exit(1);
1230     }
1231   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1232   if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1233     {
1234       perror("dbenv open");
1235       exit(1);
1236     }
1237   if (db_create(&db, dbenv, 0))
1238     {
1239       perror("db_create");
1240       exit(1);
1241     }
1242
1243   /* XXX: should get ro lock of Packages database! */
1244   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1245   if (stat(dbpath, &packagesstat))
1246     {
1247       perror(dbpath);
1248       exit(1);
1249     }
1250   mkrpmdbcookie(&packagesstat, repo->rpmdbcookie);
1251
1252   if (!ref || memcmp(repo->rpmdbcookie, ref->rpmdbcookie, 32) != 0)
1253     {
1254       Id *pkgids;
1255       if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1256         {
1257           perror("db->open var/lib/rpm/Packages");
1258           exit(1);
1259         }
1260       if (db->get_byteswapped(db, &byteswapped))
1261         {
1262           perror("db->get_byteswapped");
1263           exit(1);
1264         }
1265       if (db->cursor(db, NULL, &dbc, 0))
1266         {
1267           perror("db->cursor");
1268           exit(1);
1269         }
1270       dbidp = (unsigned char *)&dbid;
1271       repo->rpmdbid = sat_calloc(256, sizeof(Id));
1272       asolv = 256;
1273       rpmheadsize = 0;
1274       rpmhead = 0;
1275       i = 0;
1276       s = 0;
1277       while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1278         {
1279           if (!s)
1280             s = pool_id2solvable(pool, repo_add_solvable(repo));
1281           if (i >= asolv)
1282             {
1283               repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(Id));
1284               memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
1285               asolv += 256;
1286             }
1287           if (dbkey.size != 4)
1288             {
1289               fprintf(stderr, "corrupt Packages database (key size)\n");
1290               exit(1);
1291             }
1292           dp = dbkey.data;
1293           if (byteswapped)
1294             {
1295               dbidp[0] = dp[3];
1296               dbidp[1] = dp[2];
1297               dbidp[2] = dp[1];
1298               dbidp[3] = dp[0];
1299             }
1300           else
1301             memcpy(dbidp, dp, 4);
1302           if (dbid == 0)                /* the join key */
1303             continue;
1304           if (dbdata.size < 8)
1305             {
1306               fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1307               exit(1);
1308             }
1309           if (dbdata.size > rpmheadsize)
1310             rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1311           memcpy(buf, dbdata.data, 8);
1312           rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1313           rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1314           if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1315             {
1316               fprintf(stderr, "corrupt rpm database (data size)\n");
1317               exit(1);
1318             }
1319           memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1320           rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1321           repo->rpmdbid[i] = dbid;
1322           if (rpm2solv(pool, repo, repodata, s, rpmhead))
1323             {
1324               i++;
1325               s = 0;
1326             }
1327           else
1328             {
1329               /* We can reuse this solvable, but make sure it's still
1330                  associated with this repo.  */
1331               memset(s, 0, sizeof(*s));
1332               s->repo = repo;
1333             }
1334         }
1335       if (s)
1336         {
1337           /* oops, could not reuse. free it instead */
1338           repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1339           s = 0;
1340         }
1341       dbc->c_close(dbc);
1342       db->close(db, 0);
1343       db = 0;
1344       /* now sort all solvables */
1345       if (repo->end - repo->start > 1)
1346         {
1347           pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1348           for (i = repo->start; i < repo->end; i++)
1349             pkgids[i - repo->start] = i;
1350           pkgids_sort_cmp_data = repo;
1351           qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1352           /* adapt order */
1353           for (i = repo->start; i < repo->end; i++)
1354             {
1355               int j = pkgids[i - repo->start];
1356               while (j < i)
1357                 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1358               if (j != i)
1359                 swap_solvables(repo, repodata, i, j);
1360             }
1361           sat_free(pkgids);
1362         }
1363     }
1364   else
1365     {
1366       Id dircache[512];
1367
1368       memset(dircache, 0, sizeof(dircache));
1369       snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1370       if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1371         {
1372           perror("db->open var/lib/rpm/Name");
1373           exit(1);
1374         }
1375       if (db->get_byteswapped(db, &byteswapped))
1376         {
1377           perror("db->get_byteswapped");
1378           exit(1);
1379         }
1380       if (db->cursor(db, NULL, &dbc, 0))
1381         {
1382           perror("db->cursor");
1383           exit(1);
1384         }
1385       dbidp = (unsigned char *)&dbid;
1386       nrpmids = 0;
1387       rpmids = 0;
1388       while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1389         {
1390           if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1391             continue;
1392           dl = dbdata.size;
1393           dp = dbdata.data;
1394           while(dl >= 8)
1395             {
1396               if (byteswapped)
1397                 {
1398                   dbidp[0] = dp[3];
1399                   dbidp[1] = dp[2];
1400                   dbidp[2] = dp[1];
1401                   dbidp[3] = dp[0];
1402                 }
1403               else
1404                 memcpy(dbidp, dp, 4);
1405               rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1406               rpmids[nrpmids].dbid = dbid;
1407               rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1408               memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1409               rpmids[nrpmids].name[(int)dbkey.size] = 0;
1410               nrpmids++;
1411               dp += 8;
1412               dl -= 8;
1413             }
1414         }
1415       dbc->c_close(dbc);
1416       db->close(db, 0);
1417       db = 0;
1418
1419       /* sort rpmids */
1420       qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1421
1422       rp = rpmids;
1423       dbidp = (unsigned char *)&dbid;
1424       rpmheadsize = 0;
1425       rpmhead = 0;
1426
1427       /* create hash from dbid to ref */
1428       refmask = mkmask(ref->nsolvables);
1429       refhash = sat_calloc(refmask + 1, sizeof(Id));
1430       for (i = 0; i < ref->nsolvables; i++)
1431         {
1432           h = ref->rpmdbid[i] & refmask;
1433           while (refhash[h])
1434             h = (h + 317) & refmask;
1435           refhash[h] = i + 1;   /* make it non-zero */
1436         }
1437
1438       repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
1439       s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1440
1441       for (i = 0; i < nrpmids; i++, rp++, s++)
1442         {
1443           dbid = rp->dbid;
1444           repo->rpmdbid[i] = dbid;
1445           if (refhash)
1446             {
1447               h = dbid & refmask;
1448               while ((id = refhash[h]))
1449                 {
1450                   if (ref->rpmdbid[id - 1] == dbid)
1451                     break;
1452                   h = (h + 317) & refmask;
1453                 }
1454               if (id)
1455                 {
1456                   Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1457                   solvable_copy(s, r, repodata, dircache);
1458                   continue;
1459                 }
1460             }
1461           if (!db)
1462             {
1463               if (db_create(&db, 0, 0))
1464                 {
1465                   perror("db_create");
1466                   exit(1);
1467                 }
1468               snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1469               if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1470                 {
1471                   perror("db->open var/lib/rpm/Packages");
1472                   exit(1);
1473                 }
1474               if (db->get_byteswapped(db, &byteswapped))
1475                 {
1476                   perror("db->get_byteswapped");
1477                   exit(1);
1478                 }
1479             }
1480           if (byteswapped)
1481             {
1482               buf[0] = dbidp[3];
1483               buf[1] = dbidp[2];
1484               buf[2] = dbidp[1];
1485               buf[3] = dbidp[0];
1486             }
1487           else
1488             memcpy(buf, dbidp, 4);
1489           dbkey.data = buf;
1490           dbkey.size = 4;
1491           dbdata.data = 0;
1492           dbdata.size = 0;
1493           if (db->get(db, NULL, &dbkey, &dbdata, 0))
1494             {
1495               perror("db->get");
1496               fprintf(stderr, "corrupt rpm database\n");
1497               exit(1);
1498             }
1499           if (dbdata.size < 8)
1500             {
1501               fprintf(stderr, "corrupt rpm database (size)\n");
1502               exit(1);
1503             }
1504           if (dbdata.size > rpmheadsize)
1505             rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1506           memcpy(buf, dbdata.data, 8);
1507           rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1508           rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1509           if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1510             {
1511               fprintf(stderr, "corrupt rpm database (data size)\n");
1512               exit(1);
1513             }
1514           memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1515           rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1516
1517           rpm2solv(pool, repo, repodata, s, rpmhead);
1518         }
1519
1520       if (refhash)
1521         sat_free(refhash);
1522       if (rpmids)
1523         {
1524           for (i = 0; i < nrpmids; i++)
1525             sat_free(rpmids[i].name);
1526           sat_free(rpmids);
1527         }
1528     }
1529   if (rpmhead)
1530     sat_free(rpmhead);
1531   if (db)
1532     db->close(db, 0);
1533   dbenv->close(dbenv, 0);
1534   if (repodata)
1535     repodata_internalize(repodata);
1536 }
1537
1538 static inline unsigned int
1539 getu32(unsigned char *dp)
1540 {
1541   return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1542 }
1543
1544 static void
1545 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1546 {
1547   Pool *pool = s->repo->pool;
1548   const char *name, *n1, *n2;
1549   int l;
1550
1551   repodata_extend(data, s - pool->solvables);
1552
1553   /* skip ./ prefix */
1554   if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1555     location += 2;
1556
1557   name = strrchr(location, '/');
1558   if (!name)
1559     name = location;
1560   else
1561     {
1562       name++;
1563       n2 = id2str(pool, s->arch);
1564       l = strlen(n2);
1565       if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1566         {
1567           /* too bad, need to store directory */
1568           char *dir = strdup(location);
1569           dir[name - location - 1] = 0;
1570           repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1571           free(dir);
1572         }
1573       else
1574         repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1575     }
1576   n1 = name;
1577   for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1578     if (*n1 != *n2)
1579       break;
1580   if (*n2 || *n1 != '-')
1581     goto nontrivial;
1582   n1++;
1583   for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1584     if (*n1 != *n2)
1585       break;
1586   if (*n2 || *n1 != '.')
1587     goto nontrivial;
1588   n1++;
1589   for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1590     if (*n1 != *n2)
1591       break;
1592   if (*n2 || strcmp (n1, ".rpm"))
1593     goto nontrivial;
1594   repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1595   return;
1596
1597 nontrivial:
1598   repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1599   return;
1600 }
1601
1602 void
1603 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1604 {
1605   int i, sigdsize, sigcnt, l;
1606   Pool *pool = repo->pool;
1607   Solvable *s;
1608   Repodata *repodata;
1609   RpmHead *rpmhead = 0;
1610   int rpmheadsize = 0;
1611   char *payloadformat;
1612   FILE *fp;
1613   unsigned char lead[4096];
1614   int headerstart, headerend;
1615   struct stat stb;
1616
1617   if (nrpms <= 0)
1618     return;
1619   repodata = repo_add_repodata(repo, 0);
1620   for (i = 0; i < nrpms; i++)
1621     {
1622       if ((fp = fopen(rpms[i], "r")) == 0)
1623         {
1624           perror(rpms[i]);
1625           continue;
1626         }
1627       if (fstat(fileno(fp), &stb))
1628         {
1629           perror("stat");
1630           continue;
1631         }
1632       if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1633         {
1634           fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1635           fclose(fp);
1636           continue;
1637         }
1638       if (lead[78] != 0 || lead[79] != 5)
1639         {
1640           fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1641           fclose(fp);
1642           continue;
1643         }
1644       if (getu32(lead + 96) != 0x8eade801)
1645         {
1646           fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1647           fclose(fp);
1648           continue;
1649         }
1650       sigcnt = getu32(lead + 96 + 8);
1651       sigdsize = getu32(lead + 96 + 12);
1652       if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1653         {
1654           fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1655           fclose(fp);
1656           continue;
1657         }
1658       sigdsize += sigcnt * 16;
1659       sigdsize = (sigdsize + 7) & ~7;
1660       headerstart = 96 + 16 + sigdsize;
1661       while (sigdsize)
1662         {
1663           l = sigdsize > 4096 ? 4096 : sigdsize;
1664           if (fread(lead, l, 1, fp) != 1)
1665             {
1666               fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1667               fclose(fp);
1668               continue;
1669             }
1670           sigdsize -= l;
1671         }
1672       if (fread(lead, 16, 1, fp) != 1)
1673         {
1674           fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1675           fclose(fp);
1676           continue;
1677         }
1678       if (getu32(lead) != 0x8eade801)
1679         {
1680           fprintf(stderr, "%s: bad header\n", rpms[i]);
1681           fclose(fp);
1682           continue;
1683         }
1684       sigcnt = getu32(lead + 8);
1685       sigdsize = getu32(lead + 12);
1686       if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1687         {
1688           fprintf(stderr, "%s: bad header\n", rpms[i]);
1689           fclose(fp);
1690           continue;
1691         }
1692       l = sigdsize + sigcnt * 16;
1693       headerend = headerstart + 16 + l;
1694       if (l > rpmheadsize)
1695         rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1696       if (fread(rpmhead->data, l, 1, fp) != 1)
1697         {
1698           fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1699           fclose(fp);
1700           continue;
1701         }
1702       rpmhead->cnt = sigcnt;
1703       rpmhead->dcnt = sigdsize;
1704       rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1705       if (headexists(rpmhead, TAG_PATCHESNAME))
1706         {
1707           /* this is a patch rpm, ignore */
1708           fclose(fp);
1709           continue;
1710         }
1711       payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1712       if (payloadformat && !strcmp(payloadformat, "drpm"))
1713         {
1714           /* this is a delta rpm */
1715           fclose(fp);
1716           continue;
1717         }
1718       fclose(fp);
1719       s = pool_id2solvable(pool, repo_add_solvable(repo));
1720       rpm2solv(pool, repo, repodata, s, rpmhead);
1721       if (repodata)
1722         {
1723           Id handle = (s - pool->solvables) - repodata->start;
1724           handle = repodata_get_handle(repodata, handle);
1725           add_location(repodata, s, handle, rpms[i]);
1726           repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1727           repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);
1728         }
1729     }
1730   if (rpmhead)
1731     sat_free(rpmhead);
1732   if (repodata)
1733     repodata_internalize(repodata);
1734 }