2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * convert rpm db to repo
15 #include <sys/types.h>
28 #include "repo_rpmdb.h"
31 #define TAG_VERSION 1001
32 #define TAG_RELEASE 1002
33 #define TAG_EPOCH 1003
34 #define TAG_SUMMARY 1004
35 #define TAG_DESCRIPTION 1005
36 #define TAG_BUILDTIME 1006
37 #define TAG_VENDOR 1011
39 #define TAG_PROVIDENAME 1047
40 #define TAG_REQUIREFLAGS 1048
41 #define TAG_REQUIRENAME 1049
42 #define TAG_REQUIREVERSION 1050
43 #define TAG_CONFLICTFLAGS 1053
44 #define TAG_CONFLICTNAME 1054
45 #define TAG_CONFLICTVERSION 1055
46 #define TAG_OBSOLETENAME 1090
47 #define TAG_PROVIDEFLAGS 1112
48 #define TAG_PROVIDEVERSION 1113
49 #define TAG_OBSOLETEFLAGS 1114
50 #define TAG_OBSOLETEVERSION 1115
51 #define TAG_DIRINDEXES 1116
52 #define TAG_BASENAMES 1117
53 #define TAG_DIRNAMES 1118
54 #define TAG_SUGGESTSNAME 1156
55 #define TAG_SUGGESTSVERSION 1157
56 #define TAG_SUGGESTSFLAGS 1158
57 #define TAG_ENHANCESNAME 1159
58 #define TAG_ENHANCESVERSION 1160
59 #define TAG_ENHANCESFLAGS 1161
61 #define DEP_LESS (1 << 1)
62 #define DEP_GREATER (1 << 2)
63 #define DEP_EQUAL (1 << 3)
64 #define DEP_STRONG (1 << 27)
65 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
76 typedef struct rpmhead {
80 unsigned char data[1];
84 headint32(RpmHead *h, int tag, int *cnt)
86 unsigned int i, o, *r;
87 unsigned char *d, taga[4];
94 for (i = 0; i < h->cnt; i++, d -= 16)
95 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
99 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
101 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
102 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
103 if (o + 4 * i > h->dcnt)
106 r = xcalloc(i ? i : 1, sizeof(unsigned int));
109 for (o = 0; o < i; o++, d += 4)
110 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
115 headstring(RpmHead *h, int tag)
118 unsigned char *d, taga[4];
124 for (i = 0; i < h->cnt; i++, d -= 16)
125 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
129 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 6)
131 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
132 return (char *)h->dp + o;
136 headstringarray(RpmHead *h, int tag, int *cnt)
139 unsigned char *d, taga[4];
147 for (i = 0; i < h->cnt; i++, d -= 16)
148 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
152 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
154 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
155 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
156 r = xcalloc(i ? i : 1, sizeof(char *));
160 for (o = 0; o < i; o++)
164 d += strlen((char *)d) + 1;
165 if (d >= h->dp + h->dcnt)
174 static char *headtoevr(RpmHead *h)
176 unsigned int epoch, *epochp;
182 version = headstring(h, TAG_VERSION);
183 release = headstring(h, TAG_RELEASE);
184 epochp = headint32(h, TAG_EPOCH, &epochcnt);
185 if (!version || !release)
187 fprintf(stderr, "headtoevr: bad rpm header\n");
190 for (v = version; *v >= 0 && *v <= '9'; v++)
192 epoch = epochp && epochcnt ? *epochp : 0;
193 if (epoch || (v != version && *v == ':'))
195 char epochbuf[11]; /* 32bit decimal will fit in */
196 sprintf(epochbuf, "%u", epoch);
197 evr = xmalloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
198 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
202 evr = xmalloc(strlen(version) + 1 + strlen(release) + 1);
203 sprintf(evr, "%s-%s", version, release);
211 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
215 int i, cc, nc, vc, fc;
217 unsigned int olddeps;
220 n = headstringarray(rpmhead, tagn, &nc);
223 v = headstringarray(rpmhead, tagv, &vc);
229 f = headint32(rpmhead, tagf, &fc);
236 if (nc != vc || nc != fc)
238 fprintf(stderr, "bad dependency entries\n");
246 for (i = 0; i < nc; i++)
247 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
250 if ((f[i] & DEP_PRE) != 0)
256 for (i = 0; i < nc; i++)
257 if ((f[i] & DEP_PRE) != 0)
263 if (tagn != TAG_REQUIRENAME)
273 olddeps = repo_reserve_ids(repo, 0, cc);
274 ida = repo->idarraydata + olddeps;
283 *ida++ = SOLVABLE_PREREQMARKER;
285 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
287 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
289 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
291 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
295 if ((f[i] & DEP_LESS) != 0)
297 if ((f[i] & DEP_EQUAL) != 0)
299 if ((f[i] & DEP_GREATER) != 0)
301 name = str2id(pool, n[i], 1);
302 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
303 evr = str2id(pool, v[i] + 2, 1);
305 evr = str2id(pool, v[i], 1);
306 *ida++ = rel2id(pool, name, evr, flags, 1);
309 *ida++ = str2id(pool, n[i], 1);
312 repo->idarraysize += cc + 1;
320 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
325 Pool *frompool = fromrepo->pool;
329 from = fromrepo->idarraydata + fromoff;
330 for (ida = from, cc = 0; *ida; ida++, cc++)
334 ido = repo_reserve_ids(repo, 0, cc);
335 ida = repo->idarraydata + ido;
336 if (frompool && pool != frompool)
343 Reldep *rd = GETRELDEP(frompool, id);
344 Id name = str2id(pool, id2str(frompool, rd->name), 1);
345 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
346 id = rel2id(pool, name, evr, rd->flags, 1);
349 id = str2id(pool, id2str(frompool, id), 1);
355 memcpy(ida, from, (cc + 1) * sizeof(Id));
356 repo->idarraysize += cc + 1;
361 #define FILEFILTER_EXACT 0
362 #define FILEFILTER_STARTS 1
363 #define FILEFILTER_CONTAINS 2
371 static struct filefilter filefilters[] = {
372 { FILEFILTER_CONTAINS, "/bin/", 0},
373 { FILEFILTER_CONTAINS, "/sbin/", 0},
374 { FILEFILTER_CONTAINS, "/lib/", 0},
375 { FILEFILTER_CONTAINS, "/lib64/", 0},
376 { FILEFILTER_CONTAINS, "/etc/", 0},
377 { FILEFILTER_STARTS, "/usr/games/", 0},
378 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
379 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
380 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
383 /* assumes last processed array is provides! */
385 addfileprovides(Pool *pool, Repo *repo, RpmHead *rpmhead, unsigned int olddeps)
392 struct filefilter *ff;
396 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
399 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
405 di = headint32(rpmhead, TAG_DIRINDEXES, &dic);
414 fprintf(stderr, "bad filelist\n");
417 for (i = 0; i < bnc; i++)
420 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
424 switch (ff->dirmatch)
426 case FILEFILTER_STARTS:
427 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
430 case FILEFILTER_CONTAINS:
431 if (!strstr(dn[di[i]], ff->dir))
434 case FILEFILTER_EXACT:
436 if (strcmp(dn[di[i]], ff->dir))
443 if (strcmp(bn[i], ff->base))
448 if (j == sizeof(filefilters)/sizeof(*filefilters))
450 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
454 fn = xrealloc(fn, fna);
456 strcpy(fn, dn[di[i]]);
458 olddeps = repo_addid(repo, olddeps, str2id(pool, fn, 1));
469 rpm2solv(Pool *pool, Repo *repo, Solvable *s, RpmHead *rpmhead)
474 name = headstring(rpmhead, TAG_NAME);
475 if (!strcmp(name, "gpg-pubkey"))
477 s->name = str2id(pool, name, 1);
480 fprintf(stderr, "package has no name\n");
483 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
485 s->arch = ARCH_NOARCH;
486 evr = headtoevr(rpmhead);
487 s->evr = str2id(pool, evr, 1);
489 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
491 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
492 s->provides = addfileprovides(pool, repo, rpmhead, s->provides);
493 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
494 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
495 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
496 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
498 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
499 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
500 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
501 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
503 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
509 * read rpm db as repo
514 repo_add_rpmdb(Repo *repo, Repo *ref)
516 Pool *pool = repo->pool;
517 unsigned char buf[16];
522 unsigned char *dp, *dbidp;
524 struct rpmid *rpmids, *rp;
530 unsigned int refmask, h;
533 if (repo->start != repo->end)
534 abort(); /* FIXME: rpmdbid */
536 if (ref && !(ref->nsolvables && ref->rpmdbid))
539 if (db_create(&db, 0, 0))
547 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
549 perror("db->open /var/lib/rpm/Packages");
552 if (db->get_byteswapped(db, &byteswapped))
554 perror("db->get_byteswapped");
557 if (db->cursor(db, NULL, &dbc, 0))
559 perror("db->cursor");
562 dbidp = (unsigned char *)&dbid;
563 repo->rpmdbid = xcalloc(256, sizeof(unsigned int));
569 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
572 s = pool_id2solvable(pool, repo_add_solvable(repo));
575 repo->rpmdbid = xrealloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
576 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
581 fprintf(stderr, "corrupt Packages database (key size)\n");
593 memcpy(dbidp, dp, 4);
594 if (dbid == 0) /* the join key */
598 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
601 if (data.size > rpmheadsize)
602 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
603 memcpy(buf, data.data, 8);
604 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
605 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
606 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
608 fprintf(stderr, "corrupt rpm database (data size)\n");
611 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
612 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
613 repo->rpmdbid[i] = dbid;
614 if (rpm2solv(pool, repo, s, rpmhead))
621 /* We can reuse this solvable, but make sure it's still
622 associated with this repo. */
623 memset(s, 0, sizeof(*s));
629 /* oops, could not reuse. free it instead */
630 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
639 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
641 perror("db->open /var/lib/rpm/Name");
644 if (db->get_byteswapped(db, &byteswapped))
646 perror("db->get_byteswapped");
649 if (db->cursor(db, NULL, &dbc, 0))
651 perror("db->cursor");
654 dbidp = (unsigned char *)&dbid;
657 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
659 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
673 memcpy(dbidp, dp, 4);
674 if ((nrpmids & 255) == 0)
675 rpmids = xrealloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
676 rpmids[nrpmids].dbid = dbid;
677 rpmids[nrpmids].name = malloc((int)key.size + 1);
678 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
679 rpmids[nrpmids].name[(int)key.size] = 0;
690 dbidp = (unsigned char *)&dbid;
698 refmask = mkmask(ref->nsolvables);
699 refhash = xcalloc(refmask + 1, sizeof(Id));
700 for (i = 0; i < ref->nsolvables; i++)
702 h = ref->rpmdbid[i] & refmask;
704 h = (h + 317) & refmask;
705 refhash[h] = i + 1; /* make it non-zero */
709 repo->rpmdbid = xcalloc(nrpmids, sizeof(unsigned int));
711 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
713 for (i = 0; i < nrpmids; i++, rp++, s++)
716 repo->rpmdbid[i] = dbid;
720 while ((id = refhash[h]))
722 if (ref->rpmdbid[id - 1] == dbid)
724 h = (h + 317) & refmask;
728 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
729 if (pool == ref->pool)
734 s->vendor = r->vendor;
739 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
741 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
743 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
745 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
747 s->provides = copydeps(pool, repo, r->provides, ref);
748 s->requires = copydeps(pool, repo, r->requires, ref);
749 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
750 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
751 s->recommends = copydeps(pool, repo, r->recommends, ref);
752 s->suggests = copydeps(pool, repo, r->suggests, ref);
753 s->supplements = copydeps(pool, repo, r->supplements, ref);
754 s->enhances = copydeps(pool, repo, r->enhances, ref);
755 s->freshens = copydeps(pool, repo, r->freshens, ref);
761 if (db_create(&db, 0, 0))
766 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
768 perror("db->open /var/lib/rpm/Packages");
771 if (db->get_byteswapped(db, &byteswapped))
773 perror("db->get_byteswapped");
785 memcpy(buf, dbidp, 4);
790 if (db->get(db, NULL, &key, &data, 0))
793 fprintf(stderr, "corrupt rpm database\n");
798 fprintf(stderr, "corrupt rpm database (size)\n");
801 if (data.size > rpmheadsize)
802 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
803 memcpy(buf, data.data, 8);
804 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
805 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
806 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
808 fprintf(stderr, "corrupt rpm database (data size)\n");
811 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
812 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
814 rpm2solv(pool, repo, s, rpmhead);
821 for (i = 0; i < nrpmids; i++)
822 free(rpmids[i].name);