4 * convert rpm db to repo
19 #include "repo_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, Repo *repo, 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 = repo_reserve_ids(repo, 0, cc);
265 ida = repo->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 repo->idarraysize += cc + 1;
311 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
316 Pool *frompool = fromrepo->pool;
320 from = fromrepo->idarraydata + fromoff;
321 for (ida = from, cc = 0; *ida; ida++, cc++)
325 ido = repo_reserve_ids(repo, 0, cc);
326 ida = repo->idarraydata + ido;
327 if (frompool && pool != frompool)
334 Reldep *rd = GETRELDEP(frompool, id);
335 Id name = str2id(pool, id2str(frompool, rd->name), 1);
336 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
337 id = rel2id(pool, name, evr, rd->flags, 1);
340 id = str2id(pool, id2str(frompool, id), 1);
346 memcpy(ida, from, (cc + 1) * sizeof(Id));
347 repo->idarraysize += cc + 1;
352 #define FILEFILTER_EXACT 0
353 #define FILEFILTER_STARTS 1
354 #define FILEFILTER_CONTAINS 2
362 static struct filefilter filefilters[] = {
363 { FILEFILTER_CONTAINS, "/bin/", 0},
364 { FILEFILTER_CONTAINS, "/sbin/", 0},
365 { FILEFILTER_CONTAINS, "/lib/", 0},
366 { FILEFILTER_CONTAINS, "/lib64/", 0},
367 { FILEFILTER_CONTAINS, "/etc/", 0},
368 { FILEFILTER_STARTS, "/usr/games/", 0},
369 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
370 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
371 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
374 /* assumes last processed array is provides! */
376 addfileprovides(Pool *pool, Repo *repo, RpmHead *rpmhead, unsigned int olddeps)
383 struct filefilter *ff;
387 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
390 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
396 di = headint32(rpmhead, TAG_DIRINDEXES, &dic);
405 fprintf(stderr, "bad filelist\n");
408 for (i = 0; i < bnc; i++)
411 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
415 switch (ff->dirmatch)
417 case FILEFILTER_STARTS:
418 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
421 case FILEFILTER_CONTAINS:
422 if (!strstr(dn[di[i]], ff->dir))
425 case FILEFILTER_EXACT:
427 if (strcmp(dn[di[i]], ff->dir))
434 if (strcmp(bn[i], ff->base))
439 if (j == sizeof(filefilters)/sizeof(*filefilters))
441 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
445 fn = realloc(fn, j + 256);
447 fn = malloc(j + 256);
450 strcpy(fn, dn[di[i]]);
452 olddeps = repo_addid(repo, olddeps, str2id(pool, fn, 1));
463 rpm2solv(Pool *pool, Repo *repo, Solvable *s, RpmHead *rpmhead)
468 name = headstring(rpmhead, TAG_NAME);
469 if (!strcmp(name, "gpg-pubkey"))
471 s->name = str2id(pool, name, 1);
474 fprintf(stderr, "package has no name\n");
477 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
480 fprintf(stderr, "package %s has no arch\n", id2str(pool, s->name));
483 evr = headtoevr(rpmhead);
484 s->evr = str2id(pool, evr, 1);
487 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
488 s->provides = addfileprovides(pool, repo, rpmhead, s->provides);
489 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
490 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
491 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
492 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
494 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
495 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
496 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
497 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
499 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
505 * read rpm db as repo
510 pool_addrepo_rpmdb(Pool *pool, Repo *ref)
512 unsigned char buf[16];
517 unsigned char *dp, *dbidp;
519 struct rpmid *rpmids, *rp;
526 unsigned int refmask, h;
529 repo = pool_addrepo_empty(pool);
531 if (ref && !(ref->nsolvables && ref->rpmdbid))
534 if (db_create(&db, 0, 0))
542 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
544 perror("db->open /var/lib/rpm/Packages");
547 if (db->get_byteswapped(db, &byteswapped))
549 perror("db->get_byteswapped");
552 if (db->cursor(db, NULL, &dbc, 0))
554 perror("db->cursor");
557 dbidp = (unsigned char *)&dbid;
558 pool->solvables = realloc(pool->solvables, (pool->nsolvables + 256) * sizeof(Solvable));
559 memset(pool->solvables + repo->start, 0, 256 * sizeof(Solvable));
560 repo->rpmdbid = calloc(256, sizeof(unsigned int));
565 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
569 pool->solvables = realloc(pool->solvables, (pool->nsolvables + asolv + 256) * sizeof(Solvable));
570 memset(pool->solvables + repo->start + asolv, 0, 256 * sizeof(Solvable));
571 repo->rpmdbid = realloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
572 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
575 pool->solvables[repo->start + i].repo = repo;
578 fprintf(stderr, "corrupt Packages database (key size)\n");
590 memcpy(dbidp, dp, 4);
591 if (dbid == 0) /* the join key */
595 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
599 rpmhead = malloc(sizeof(*rpmhead) + data.size);
600 else if (data.size > rpmheadsize)
601 rpmhead = realloc(rpmhead, sizeof(*rpmhead) + data.size);
602 memcpy(buf, data.data, 8);
603 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
604 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
605 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
607 fprintf(stderr, "corrupt rpm database (data size)\n");
610 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
611 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
612 repo->rpmdbid[i] = dbid;
613 if (rpm2solv(pool, repo, pool->solvables + repo->start + i, rpmhead))
623 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
625 perror("db->open /var/lib/rpm/Name");
628 if (db->get_byteswapped(db, &byteswapped))
630 perror("db->get_byteswapped");
633 if (db->cursor(db, NULL, &dbc, 0))
635 perror("db->cursor");
638 dbidp = (unsigned char *)&dbid;
641 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
643 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
657 memcpy(dbidp, dp, 4);
658 if ((nrpmids & 255) == 0)
661 rpmids = realloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
663 rpmids = malloc(sizeof(*rpmids) * 256);
665 rpmids[nrpmids].dbid = dbid;
666 rpmids[nrpmids].name = malloc((int)key.size + 1);
667 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
668 rpmids[nrpmids].name[(int)key.size] = 0;
680 dbidp = (unsigned char *)&dbid;
684 pool->solvables = realloc(pool->solvables, (pool->nsolvables + nrpmids) * sizeof(Solvable));
685 memset(pool->solvables + repo->start, 0, nrpmids * sizeof(Solvable));
686 repo->rpmdbid = calloc(nrpmids, sizeof(unsigned int));
692 refmask = mkmask(ref->nsolvables);
693 refhash = calloc(refmask + 1, sizeof(Id));
694 for (i = 0; i < ref->nsolvables; i++)
696 h = ref->rpmdbid[i] & refmask;
698 h = (h + 317) & refmask;
699 refhash[h] = i + 1; /* make it non-zero */
702 s = pool->solvables + repo->start;
703 for (i = 0; i < nrpmids; i++, rp++, s++)
707 repo->rpmdbid[i] = dbid;
711 while ((id = refhash[h]))
713 if (ref->rpmdbid[id - 1] == dbid)
715 h = (h + 317) & refmask;
719 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
720 if (pool == ref->pool)
729 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
731 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
733 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
735 s->provides = copydeps(pool, repo, r->provides, ref);
736 s->requires = copydeps(pool, repo, r->requires, ref);
737 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
738 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
739 s->recommends = copydeps(pool, repo, r->recommends, ref);
740 s->suggests = copydeps(pool, repo, r->suggests, ref);
741 s->supplements = copydeps(pool, repo, r->supplements, ref);
742 s->enhances = copydeps(pool, repo, r->enhances, ref);
743 s->freshens = copydeps(pool, repo, r->freshens, ref);
749 if (db_create(&db, 0, 0))
754 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
756 perror("db->open /var/lib/rpm/Packages");
759 if (db->get_byteswapped(db, &byteswapped))
761 perror("db->get_byteswapped");
773 memcpy(buf, dbidp, 4);
778 if (db->get(db, NULL, &key, &data, 0))
781 fprintf(stderr, "corrupt rpm database\n");
786 fprintf(stderr, "corrupt rpm database (size)\n");
790 rpmhead = malloc(sizeof(*rpmhead) + data.size);
791 else if (data.size > rpmheadsize)
792 rpmhead = realloc(rpmhead, sizeof(*rpmhead) + data.size);
793 memcpy(buf, data.data, 8);
794 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
795 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
796 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
798 fprintf(stderr, "corrupt rpm database (data size)\n");
801 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
802 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
804 rpm2solv(pool, repo, s, rpmhead);
811 for (i = 0; i < nrpmids; i++)
812 free(rpmids[i].name);
818 pool->nsolvables += nrpmids;
819 repo->nsolvables = nrpmids;