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