4 * convert rpm db to source
19 #include "source_rpmdb.h"
22 #define TAG_VERSION 1001
23 #define TAG_RELEASE 1002
24 #define TAG_EPOCH 1003
25 #define TAG_SUMMARY 1004
26 #define TAG_DESCRIPTION 1005
27 #define TAG_BUILDTIME 1006
28 #define TAG_VENDOR 1011
30 #define TAG_PROVIDENAME 1047
31 #define TAG_REQUIREFLAGS 1048
32 #define TAG_REQUIRENAME 1049
33 #define TAG_REQUIREVERSION 1050
34 #define TAG_CONFLICTFLAGS 1053
35 #define TAG_CONFLICTNAME 1054
36 #define TAG_CONFLICTVERSION 1055
37 #define TAG_OBSOLETENAME 1090
38 #define TAG_PROVIDEFLAGS 1112
39 #define TAG_PROVIDEVERSION 1113
40 #define TAG_OBSOLETEFLAGS 1114
41 #define TAG_OBSOLETEVERSION 1115
42 #define TAG_DIRINDEXES 1116
43 #define TAG_BASENAMES 1117
44 #define TAG_DIRNAMES 1118
45 #define TAG_SUGGESTSNAME 1156
46 #define TAG_SUGGESTSVERSION 1157
47 #define TAG_SUGGESTSFLAGS 1158
48 #define TAG_ENHANCESNAME 1159
49 #define TAG_ENHANCESVERSION 1160
50 #define TAG_ENHANCESFLAGS 1161
52 #define DEP_LESS (1 << 1)
53 #define DEP_GREATER (1 << 2)
54 #define DEP_EQUAL (1 << 3)
55 #define DEP_STRONG (1 << 27)
56 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
67 typedef struct rpmhead {
71 unsigned char data[1];
75 headint32(RpmHead *h, int tag, int *cnt)
77 unsigned int i, o, *r;
78 unsigned char *d, taga[4];
85 for (i = 0; i < h->cnt; i++, d -= 16)
86 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
90 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
92 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
93 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
94 if (o + 4 * i > h->dcnt)
97 r = calloc(i ? i : 1, sizeof(unsigned int));
100 for (o = 0; o < i; o++, d += 4)
101 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
106 headstring(RpmHead *h, int tag)
109 unsigned char *d, taga[4];
115 for (i = 0; i < h->cnt; i++, d -= 16)
116 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
120 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 6)
122 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
123 return (char *)h->dp + o;
127 headstringarray(RpmHead *h, int tag, int *cnt)
130 unsigned char *d, taga[4];
138 for (i = 0; i < h->cnt; i++, d -= 16)
139 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
143 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
145 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
146 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
147 r = calloc(i ? i : 1, sizeof(char *));
151 for (o = 0; o < i; o++)
155 d += strlen((char *)d) + 1;
156 if (d >= h->dp + h->dcnt)
165 static char *headtoevr(RpmHead *h)
167 unsigned int epoch, *epochp;
173 version = headstring(h, TAG_VERSION);
174 release = headstring(h, TAG_RELEASE);
175 epochp = headint32(h, TAG_EPOCH, &epochcnt);
176 if (!version || !release)
178 fprintf(stderr, "headtoevr: bad rpm header\n");
181 for (v = version; *v >= 0 && *v <= '9'; v++)
183 epoch = epochp && epochcnt ? *epochp : 0;
184 if (epoch || (v != version && *v == ':'))
186 char epochbuf[11]; /* 32bit decimal will fit in */
187 sprintf(epochbuf, "%u", epoch);
188 evr = malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
189 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
193 evr = malloc(strlen(version) + 1 + strlen(release) + 1);
194 sprintf(evr, "%s-%s", version, release);
202 makedeps(Pool *pool, Source *source, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
206 int i, cc, nc, vc, fc;
208 unsigned int olddeps;
211 n = headstringarray(rpmhead, tagn, &nc);
214 v = headstringarray(rpmhead, tagv, &vc);
220 f = headint32(rpmhead, tagf, &fc);
227 if (nc != vc || nc != fc)
229 fprintf(stderr, "bad dependency entries\n");
237 for (i = 0; i < nc; i++)
238 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
241 if ((f[i] & DEP_PRE) != 0)
247 for (i = 0; i < nc; i++)
248 if ((f[i] & DEP_PRE) != 0)
254 if (tagn != TAG_REQUIRENAME)
264 olddeps = source_reserve_ids(source, 0, cc);
265 ida = source->idarraydata + olddeps;
274 *ida++ = SOLVABLE_PREREQMARKER;
276 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
278 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
280 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
282 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
286 if ((f[i] & DEP_LESS) != 0)
288 if ((f[i] & DEP_EQUAL) != 0)
290 if ((f[i] & DEP_GREATER) != 0)
292 name = str2id(pool, n[i], 1);
293 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
294 evr = str2id(pool, v[i] + 2, 1);
296 evr = str2id(pool, v[i], 1);
297 *ida++ = rel2id(pool, name, evr, flags, 1);
300 *ida++ = str2id(pool, n[i], 1);
303 source->idarraysize += cc + 1;
311 copydeps(Pool *pool, Source *source, Id *from, Pool *frompool)
315 unsigned int olddeps;
319 for (ida = from, cc = 0; *ida; ida++, cc++)
323 olddeps = source_reserve_ids(source, 0, cc);
324 ida = source->idarraydata + olddeps;
325 if (frompool && pool != frompool)
332 Reldep *rd = GETRELDEP(frompool, id);
333 Id name = str2id(pool, id2str(frompool, rd->name), 1);
334 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
335 id = rel2id(pool, name, evr, rd->flags, 1);
338 id = str2id(pool, id2str(frompool, id), 1);
344 memcpy(ida, from, (cc + 1) * sizeof(Id));
345 source->idarraysize += cc + 1;
350 #define FILEFILTER_EXACT 0
351 #define FILEFILTER_STARTS 1
352 #define FILEFILTER_CONTAINS 2
360 static struct filefilter filefilters[] = {
361 { FILEFILTER_CONTAINS, "/bin/", 0},
362 { FILEFILTER_CONTAINS, "/sbin/", 0},
363 { FILEFILTER_CONTAINS, "/lib/", 0},
364 { FILEFILTER_CONTAINS, "/lib64/", 0},
365 { FILEFILTER_CONTAINS, "/etc/", 0},
366 { FILEFILTER_STARTS, "/usr/games/", 0},
367 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
368 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
369 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
372 /* assumes last processed array is provides! */
374 addfileprovides(Pool *pool, Source *source, RpmHead *rpmhead, unsigned int olddeps)
381 struct filefilter *ff;
385 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
388 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
394 di = headint32(rpmhead, TAG_DIRINDEXES, &dic);
403 fprintf(stderr, "bad filelist\n");
406 for (i = 0; i < bnc; i++)
409 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
413 switch (ff->dirmatch)
415 case FILEFILTER_STARTS:
416 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
419 case FILEFILTER_CONTAINS:
420 if (!strstr(dn[di[i]], ff->dir))
423 case FILEFILTER_EXACT:
425 if (strcmp(dn[di[i]], ff->dir))
432 if (strcmp(bn[i], ff->base))
437 if (j == sizeof(filefilters)/sizeof(*filefilters))
439 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
443 fn = realloc(fn, j + 256);
445 fn = malloc(j + 256);
448 strcpy(fn, dn[di[i]]);
450 olddeps = source_addid(source, olddeps, str2id(pool, fn, 1));
461 unsigned int provides;
462 unsigned int requires;
463 unsigned int obsoletes;
464 unsigned int conflicts;
465 unsigned int recommends;
466 unsigned int supplements;
467 unsigned int enhances;
468 unsigned int suggests;
469 unsigned int freshens;
473 rpm2solv(Pool *pool, Source *source, Solvable *s, struct deps *deps, RpmHead *rpmhead)
478 name = headstring(rpmhead, TAG_NAME);
479 if (!strcmp(name, "gpg-pubkey"))
481 s->name = str2id(pool, name, 1);
484 fprintf(stderr, "package has no name\n");
487 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
490 fprintf(stderr, "package %s has no arch\n", id2str(pool, s->name));
493 evr = headtoevr(rpmhead);
494 s->evr = str2id(pool, evr, 1);
497 deps->provides = makedeps(pool, source, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
498 deps->provides = addfileprovides(pool, source, rpmhead, deps->provides);
499 deps->provides = source_addid_dep(source, deps->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
500 deps->requires = makedeps(pool, source, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
501 deps->conflicts = makedeps(pool, source, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
502 deps->obsoletes = makedeps(pool, source, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
504 deps->recommends = makedeps(pool, source, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
505 deps->suggests = makedeps(pool, source, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
506 deps->supplements = makedeps(pool, source, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
507 deps->enhances = makedeps(pool, source, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
509 deps->supplements = source_fix_legacy(source, deps->provides, deps->supplements);
515 * read rpm db as source
520 pool_addsource_rpmdb(Pool *pool, Source *ref)
522 unsigned char buf[16];
527 unsigned char *dp, *dbidp;
529 struct rpmid *rpmids, *rp;
537 unsigned int refmask, h;
540 source = pool_addsource_empty(pool);
542 if (ref && !(ref->nsolvables && ref->rpmdbid))
545 if (db_create(&db, 0, 0))
553 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
555 perror("db->open /var/lib/rpm/Packages");
558 if (db->get_byteswapped(db, &byteswapped))
560 perror("db->get_byteswapped");
563 if (db->cursor(db, NULL, &dbc, 0))
565 perror("db->cursor");
568 dbidp = (unsigned char *)&dbid;
569 pool->solvables = realloc(pool->solvables, (pool->nsolvables + 256) * sizeof(Solvable));
570 memset(pool->solvables + source->start, 0, 256 * sizeof(Solvable));
571 source->rpmdbid = calloc(256, sizeof(unsigned int));
572 deps = calloc(256, sizeof(*deps));
577 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
581 pool->solvables = realloc(pool->solvables, (pool->nsolvables + asolv + 256) * sizeof(Solvable));
582 memset(pool->solvables + source->start + asolv, 0, 256 * sizeof(Solvable));
583 source->rpmdbid = realloc(source->rpmdbid, (asolv + 256) * sizeof(unsigned int));
584 memset(source->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
585 deps = realloc(deps, (asolv + 256) * sizeof(*deps));
586 memset(deps + asolv, 0, 256 * sizeof(*deps));
591 fprintf(stderr, "corrupt Packages database (key size)\n");
603 memcpy(dbidp, dp, 4);
604 if (dbid == 0) /* the join key */
608 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
612 rpmhead = malloc(sizeof(*rpmhead) + data.size);
613 else if (data.size > rpmheadsize)
614 rpmhead = realloc(rpmhead, sizeof(*rpmhead) + data.size);
615 memcpy(buf, data.data, 8);
616 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
617 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
618 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
620 fprintf(stderr, "corrupt rpm database (data size)\n");
623 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
624 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
625 source->rpmdbid[i] = dbid;
626 if (rpm2solv(pool, source, pool->solvables + source->start + i, deps + i, rpmhead))
636 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
638 perror("db->open /var/lib/rpm/Name");
641 if (db->get_byteswapped(db, &byteswapped))
643 perror("db->get_byteswapped");
646 if (db->cursor(db, NULL, &dbc, 0))
648 perror("db->cursor");
651 dbidp = (unsigned char *)&dbid;
654 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
656 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
670 memcpy(dbidp, dp, 4);
671 if ((nrpmids & 255) == 0)
674 rpmids = realloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
676 rpmids = malloc(sizeof(*rpmids) * 256);
678 rpmids[nrpmids].dbid = dbid;
679 rpmids[nrpmids].name = malloc((int)key.size + 1);
680 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
681 rpmids[nrpmids].name[(int)key.size] = 0;
693 dbidp = (unsigned char *)&dbid;
697 pool->solvables = realloc(pool->solvables, (pool->nsolvables + nrpmids) * sizeof(Solvable));
698 memset(pool->solvables + source->start, 0, nrpmids * sizeof(Solvable));
699 source->rpmdbid = calloc(nrpmids, sizeof(unsigned int));
700 deps = calloc(nrpmids, sizeof(*deps));
706 refmask = mkmask(ref->nsolvables);
707 refhash = calloc(refmask + 1, sizeof(Id));
708 for (i = 0; i < ref->nsolvables; i++)
710 h = ref->rpmdbid[i] & refmask;
712 h = (h + 317) & refmask;
713 refhash[h] = i + 1; /* make it non-zero */
716 s = pool->solvables + source->start;
717 for (i = 0; i < nrpmids; i++, rp++, s++)
720 source->rpmdbid[i] = dbid;
724 while ((id = refhash[h]))
726 if (ref->rpmdbid[id - 1] == dbid)
728 h = (h + 317) & refmask;
732 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
733 if (pool == ref->pool)
742 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
744 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
746 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
748 deps[i].provides = copydeps(pool, source, r->provides, ref->pool);
749 deps[i].requires = copydeps(pool, source, r->requires, ref->pool);
750 deps[i].conflicts = copydeps(pool, source, r->conflicts, ref->pool);
751 deps[i].obsoletes = copydeps(pool, source, r->obsoletes, ref->pool);
752 deps[i].recommends = copydeps(pool, source, r->recommends, ref->pool);
753 deps[i].suggests = copydeps(pool, source, r->suggests, ref->pool);
754 deps[i].supplements = copydeps(pool, source, r->supplements, ref->pool);
755 deps[i].enhances = copydeps(pool, source, r->enhances, ref->pool);
756 deps[i].freshens = copydeps(pool, source, r->freshens, ref->pool);
762 if (db_create(&db, 0, 0))
767 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
769 perror("db->open /var/lib/rpm/Packages");
772 if (db->get_byteswapped(db, &byteswapped))
774 perror("db->get_byteswapped");
786 memcpy(buf, dbidp, 4);
791 if (db->get(db, NULL, &key, &data, 0))
794 fprintf(stderr, "corrupt rpm database\n");
799 fprintf(stderr, "corrupt rpm database (size)\n");
803 rpmhead = malloc(sizeof(*rpmhead) + data.size);
804 else if (data.size > rpmheadsize)
805 rpmhead = realloc(rpmhead, sizeof(*rpmhead) + data.size);
806 memcpy(buf, data.data, 8);
807 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
808 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
809 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
811 fprintf(stderr, "corrupt rpm database (data size)\n");
814 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
815 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
817 rpm2solv(pool, source, s, deps + i, rpmhead);
824 for (i = 0; i < nrpmids; i++)
825 free(rpmids[i].name);
831 pool->nsolvables += nrpmids;
832 source->nsolvables = nrpmids;
834 // copy solvable data to pool
835 s = pool->solvables + source->start;
836 for (i = 0; i < nrpmids; i++, s++)
838 if (deps[i].provides)
839 s->provides = source->idarraydata + deps[i].provides;
840 if (deps[i].requires)
841 s->requires = source->idarraydata + deps[i].requires;
842 if (deps[i].conflicts)
843 s->conflicts = source->idarraydata + deps[i].conflicts;
844 if (deps[i].obsoletes)
845 s->obsoletes = source->idarraydata + deps[i].obsoletes;
846 if (deps[i].recommends)
847 s->recommends = source->idarraydata + deps[i].recommends;
848 if (deps[i].supplements)
849 s->supplements = source->idarraydata + deps[i].supplements;
850 if (deps[i].suggests)
851 s->suggests = source->idarraydata + deps[i].suggests;
852 if (deps[i].enhances)
853 s->enhances = source->idarraydata + deps[i].enhances;
854 if (deps[i].freshens)
855 s->freshens = source->idarraydata + deps[i].freshens;