4 * convert rpm db to repo
20 #include "repo_rpmdb.h"
23 #define TAG_VERSION 1001
24 #define TAG_RELEASE 1002
25 #define TAG_EPOCH 1003
26 #define TAG_SUMMARY 1004
27 #define TAG_DESCRIPTION 1005
28 #define TAG_BUILDTIME 1006
29 #define TAG_VENDOR 1011
31 #define TAG_PROVIDENAME 1047
32 #define TAG_REQUIREFLAGS 1048
33 #define TAG_REQUIRENAME 1049
34 #define TAG_REQUIREVERSION 1050
35 #define TAG_CONFLICTFLAGS 1053
36 #define TAG_CONFLICTNAME 1054
37 #define TAG_CONFLICTVERSION 1055
38 #define TAG_OBSOLETENAME 1090
39 #define TAG_PROVIDEFLAGS 1112
40 #define TAG_PROVIDEVERSION 1113
41 #define TAG_OBSOLETEFLAGS 1114
42 #define TAG_OBSOLETEVERSION 1115
43 #define TAG_DIRINDEXES 1116
44 #define TAG_BASENAMES 1117
45 #define TAG_DIRNAMES 1118
46 #define TAG_SUGGESTSNAME 1156
47 #define TAG_SUGGESTSVERSION 1157
48 #define TAG_SUGGESTSFLAGS 1158
49 #define TAG_ENHANCESNAME 1159
50 #define TAG_ENHANCESVERSION 1160
51 #define TAG_ENHANCESFLAGS 1161
53 #define DEP_LESS (1 << 1)
54 #define DEP_GREATER (1 << 2)
55 #define DEP_EQUAL (1 << 3)
56 #define DEP_STRONG (1 << 27)
57 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
68 typedef struct rpmhead {
72 unsigned char data[1];
76 headint32(RpmHead *h, int tag, int *cnt)
78 unsigned int i, o, *r;
79 unsigned char *d, taga[4];
86 for (i = 0; i < h->cnt; i++, d -= 16)
87 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
91 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
93 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
94 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
95 if (o + 4 * i > h->dcnt)
98 r = xcalloc(i ? i : 1, sizeof(unsigned int));
101 for (o = 0; o < i; o++, d += 4)
102 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
107 headstring(RpmHead *h, int tag)
110 unsigned char *d, taga[4];
116 for (i = 0; i < h->cnt; i++, d -= 16)
117 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
121 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 6)
123 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
124 return (char *)h->dp + o;
128 headstringarray(RpmHead *h, int tag, int *cnt)
131 unsigned char *d, taga[4];
139 for (i = 0; i < h->cnt; i++, d -= 16)
140 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
144 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
146 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
147 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
148 r = xcalloc(i ? i : 1, sizeof(char *));
152 for (o = 0; o < i; o++)
156 d += strlen((char *)d) + 1;
157 if (d >= h->dp + h->dcnt)
166 static char *headtoevr(RpmHead *h)
168 unsigned int epoch, *epochp;
174 version = headstring(h, TAG_VERSION);
175 release = headstring(h, TAG_RELEASE);
176 epochp = headint32(h, TAG_EPOCH, &epochcnt);
177 if (!version || !release)
179 fprintf(stderr, "headtoevr: bad rpm header\n");
182 for (v = version; *v >= 0 && *v <= '9'; v++)
184 epoch = epochp && epochcnt ? *epochp : 0;
185 if (epoch || (v != version && *v == ':'))
187 char epochbuf[11]; /* 32bit decimal will fit in */
188 sprintf(epochbuf, "%u", epoch);
189 evr = xmalloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
190 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
194 evr = xmalloc(strlen(version) + 1 + strlen(release) + 1);
195 sprintf(evr, "%s-%s", version, release);
203 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
207 int i, cc, nc, vc, fc;
209 unsigned int olddeps;
212 n = headstringarray(rpmhead, tagn, &nc);
215 v = headstringarray(rpmhead, tagv, &vc);
221 f = headint32(rpmhead, tagf, &fc);
228 if (nc != vc || nc != fc)
230 fprintf(stderr, "bad dependency entries\n");
238 for (i = 0; i < nc; i++)
239 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
242 if ((f[i] & DEP_PRE) != 0)
248 for (i = 0; i < nc; i++)
249 if ((f[i] & DEP_PRE) != 0)
255 if (tagn != TAG_REQUIRENAME)
265 olddeps = repo_reserve_ids(repo, 0, cc);
266 ida = repo->idarraydata + olddeps;
275 *ida++ = SOLVABLE_PREREQMARKER;
277 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
279 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
281 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
283 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
287 if ((f[i] & DEP_LESS) != 0)
289 if ((f[i] & DEP_EQUAL) != 0)
291 if ((f[i] & DEP_GREATER) != 0)
293 name = str2id(pool, n[i], 1);
294 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
295 evr = str2id(pool, v[i] + 2, 1);
297 evr = str2id(pool, v[i], 1);
298 *ida++ = rel2id(pool, name, evr, flags, 1);
301 *ida++ = str2id(pool, n[i], 1);
304 repo->idarraysize += cc + 1;
312 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
317 Pool *frompool = fromrepo->pool;
321 from = fromrepo->idarraydata + fromoff;
322 for (ida = from, cc = 0; *ida; ida++, cc++)
326 ido = repo_reserve_ids(repo, 0, cc);
327 ida = repo->idarraydata + ido;
328 if (frompool && pool != frompool)
335 Reldep *rd = GETRELDEP(frompool, id);
336 Id name = str2id(pool, id2str(frompool, rd->name), 1);
337 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
338 id = rel2id(pool, name, evr, rd->flags, 1);
341 id = str2id(pool, id2str(frompool, id), 1);
347 memcpy(ida, from, (cc + 1) * sizeof(Id));
348 repo->idarraysize += cc + 1;
353 #define FILEFILTER_EXACT 0
354 #define FILEFILTER_STARTS 1
355 #define FILEFILTER_CONTAINS 2
363 static struct filefilter filefilters[] = {
364 { FILEFILTER_CONTAINS, "/bin/", 0},
365 { FILEFILTER_CONTAINS, "/sbin/", 0},
366 { FILEFILTER_CONTAINS, "/lib/", 0},
367 { FILEFILTER_CONTAINS, "/lib64/", 0},
368 { FILEFILTER_CONTAINS, "/etc/", 0},
369 { FILEFILTER_STARTS, "/usr/games/", 0},
370 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
371 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
372 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
375 /* assumes last processed array is provides! */
377 addfileprovides(Pool *pool, Repo *repo, RpmHead *rpmhead, unsigned int olddeps)
384 struct filefilter *ff;
388 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
391 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
397 di = headint32(rpmhead, TAG_DIRINDEXES, &dic);
406 fprintf(stderr, "bad filelist\n");
409 for (i = 0; i < bnc; i++)
412 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
416 switch (ff->dirmatch)
418 case FILEFILTER_STARTS:
419 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
422 case FILEFILTER_CONTAINS:
423 if (!strstr(dn[di[i]], ff->dir))
426 case FILEFILTER_EXACT:
428 if (strcmp(dn[di[i]], ff->dir))
435 if (strcmp(bn[i], ff->base))
440 if (j == sizeof(filefilters)/sizeof(*filefilters))
442 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
446 fn = xrealloc(fn, fna);
448 strcpy(fn, dn[di[i]]);
450 olddeps = repo_addid(repo, olddeps, str2id(pool, fn, 1));
461 rpm2solv(Pool *pool, Repo *repo, Solvable *s, RpmHead *rpmhead)
466 name = headstring(rpmhead, TAG_NAME);
467 if (!strcmp(name, "gpg-pubkey"))
469 s->name = str2id(pool, name, 1);
472 fprintf(stderr, "package has no name\n");
475 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
477 s->arch = ARCH_NOARCH;
478 evr = headtoevr(rpmhead);
479 s->evr = str2id(pool, evr, 1);
481 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
483 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
484 s->provides = addfileprovides(pool, repo, rpmhead, s->provides);
485 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
486 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
487 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
488 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
490 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
491 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
492 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
493 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
495 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
501 * read rpm db as repo
506 pool_addrepo_rpmdb(Pool *pool, Repo *ref)
508 unsigned char buf[16];
513 unsigned char *dp, *dbidp;
515 struct rpmid *rpmids, *rp;
522 unsigned int refmask, h;
525 repo = pool_addrepo_empty(pool);
527 if (ref && !(ref->nsolvables && ref->rpmdbid))
530 if (db_create(&db, 0, 0))
538 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
540 perror("db->open /var/lib/rpm/Packages");
543 if (db->get_byteswapped(db, &byteswapped))
545 perror("db->get_byteswapped");
548 if (db->cursor(db, NULL, &dbc, 0))
550 perror("db->cursor");
553 dbidp = (unsigned char *)&dbid;
554 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + 256) * sizeof(Solvable));
555 memset(pool->solvables + repo->start, 0, 256 * sizeof(Solvable));
556 repo->rpmdbid = xcalloc(256, sizeof(unsigned int));
561 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
565 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + asolv + 256) * sizeof(Solvable));
566 memset(pool->solvables + repo->start + asolv, 0, 256 * sizeof(Solvable));
567 repo->rpmdbid = xrealloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
568 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
571 pool->solvables[repo->start + i].repo = repo;
574 fprintf(stderr, "corrupt Packages database (key size)\n");
586 memcpy(dbidp, dp, 4);
587 if (dbid == 0) /* the join key */
591 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
594 if (data.size > rpmheadsize)
595 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
596 memcpy(buf, data.data, 8);
597 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
598 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
599 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
601 fprintf(stderr, "corrupt rpm database (data size)\n");
604 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
605 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
606 repo->rpmdbid[i] = dbid;
607 if (rpm2solv(pool, repo, pool->solvables + repo->start + i, rpmhead))
617 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
619 perror("db->open /var/lib/rpm/Name");
622 if (db->get_byteswapped(db, &byteswapped))
624 perror("db->get_byteswapped");
627 if (db->cursor(db, NULL, &dbc, 0))
629 perror("db->cursor");
632 dbidp = (unsigned char *)&dbid;
635 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
637 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
651 memcpy(dbidp, dp, 4);
652 if ((nrpmids & 255) == 0)
653 rpmids = xrealloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
654 rpmids[nrpmids].dbid = dbid;
655 rpmids[nrpmids].name = malloc((int)key.size + 1);
656 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
657 rpmids[nrpmids].name[(int)key.size] = 0;
668 dbidp = (unsigned char *)&dbid;
672 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + nrpmids) * sizeof(Solvable));
673 memset(pool->solvables + repo->start, 0, nrpmids * sizeof(Solvable));
674 repo->rpmdbid = calloc(nrpmids, sizeof(unsigned int));
680 refmask = mkmask(ref->nsolvables);
681 refhash = xcalloc(refmask + 1, sizeof(Id));
682 for (i = 0; i < ref->nsolvables; i++)
684 h = ref->rpmdbid[i] & refmask;
686 h = (h + 317) & refmask;
687 refhash[h] = i + 1; /* make it non-zero */
690 s = pool->solvables + repo->start;
691 for (i = 0; i < nrpmids; i++, rp++, s++)
695 repo->rpmdbid[i] = dbid;
699 while ((id = refhash[h]))
701 if (ref->rpmdbid[id - 1] == dbid)
703 h = (h + 317) & refmask;
707 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
708 if (pool == ref->pool)
713 s->vendor = r->vendor;
718 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
720 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
722 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
724 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
726 s->provides = copydeps(pool, repo, r->provides, ref);
727 s->requires = copydeps(pool, repo, r->requires, ref);
728 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
729 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
730 s->recommends = copydeps(pool, repo, r->recommends, ref);
731 s->suggests = copydeps(pool, repo, r->suggests, ref);
732 s->supplements = copydeps(pool, repo, r->supplements, ref);
733 s->enhances = copydeps(pool, repo, r->enhances, ref);
734 s->freshens = copydeps(pool, repo, r->freshens, ref);
740 if (db_create(&db, 0, 0))
745 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
747 perror("db->open /var/lib/rpm/Packages");
750 if (db->get_byteswapped(db, &byteswapped))
752 perror("db->get_byteswapped");
764 memcpy(buf, dbidp, 4);
769 if (db->get(db, NULL, &key, &data, 0))
772 fprintf(stderr, "corrupt rpm database\n");
777 fprintf(stderr, "corrupt rpm database (size)\n");
780 if (data.size > rpmheadsize)
781 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
782 memcpy(buf, data.data, 8);
783 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
784 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
785 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
787 fprintf(stderr, "corrupt rpm database (data size)\n");
790 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
791 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
793 rpm2solv(pool, repo, s, rpmhead);
800 for (i = 0; i < nrpmids; i++)
801 free(rpmids[i].name);
807 pool->nsolvables += nrpmids;
808 repo->nsolvables = nrpmids;