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>
27 #include "repo_rpmdb.h"
30 #define TAG_VERSION 1001
31 #define TAG_RELEASE 1002
32 #define TAG_EPOCH 1003
33 #define TAG_SUMMARY 1004
34 #define TAG_DESCRIPTION 1005
35 #define TAG_BUILDTIME 1006
36 #define TAG_VENDOR 1011
38 #define TAG_PROVIDENAME 1047
39 #define TAG_REQUIREFLAGS 1048
40 #define TAG_REQUIRENAME 1049
41 #define TAG_REQUIREVERSION 1050
42 #define TAG_CONFLICTFLAGS 1053
43 #define TAG_CONFLICTNAME 1054
44 #define TAG_CONFLICTVERSION 1055
45 #define TAG_OBSOLETENAME 1090
46 #define TAG_PROVIDEFLAGS 1112
47 #define TAG_PROVIDEVERSION 1113
48 #define TAG_OBSOLETEFLAGS 1114
49 #define TAG_OBSOLETEVERSION 1115
50 #define TAG_DIRINDEXES 1116
51 #define TAG_BASENAMES 1117
52 #define TAG_DIRNAMES 1118
53 #define TAG_SUGGESTSNAME 1156
54 #define TAG_SUGGESTSVERSION 1157
55 #define TAG_SUGGESTSFLAGS 1158
56 #define TAG_ENHANCESNAME 1159
57 #define TAG_ENHANCESVERSION 1160
58 #define TAG_ENHANCESFLAGS 1161
60 #define DEP_LESS (1 << 1)
61 #define DEP_GREATER (1 << 2)
62 #define DEP_EQUAL (1 << 3)
63 #define DEP_STRONG (1 << 27)
64 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
75 typedef struct rpmhead {
79 unsigned char data[1];
83 headint32(RpmHead *h, int tag, int *cnt)
85 unsigned int i, o, *r;
86 unsigned char *d, taga[4];
93 for (i = 0; i < h->cnt; i++, d -= 16)
94 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
98 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
100 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
101 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
102 if (o + 4 * i > h->dcnt)
105 r = xcalloc(i ? i : 1, sizeof(unsigned int));
108 for (o = 0; o < i; o++, d += 4)
109 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
114 headstring(RpmHead *h, int tag)
117 unsigned char *d, taga[4];
123 for (i = 0; i < h->cnt; i++, d -= 16)
124 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
128 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 6)
130 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
131 return (char *)h->dp + o;
135 headstringarray(RpmHead *h, int tag, int *cnt)
138 unsigned char *d, taga[4];
146 for (i = 0; i < h->cnt; i++, d -= 16)
147 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
151 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
153 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
154 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
155 r = xcalloc(i ? i : 1, sizeof(char *));
159 for (o = 0; o < i; o++)
163 d += strlen((char *)d) + 1;
164 if (d >= h->dp + h->dcnt)
173 static char *headtoevr(RpmHead *h)
175 unsigned int epoch, *epochp;
181 version = headstring(h, TAG_VERSION);
182 release = headstring(h, TAG_RELEASE);
183 epochp = headint32(h, TAG_EPOCH, &epochcnt);
184 if (!version || !release)
186 fprintf(stderr, "headtoevr: bad rpm header\n");
189 for (v = version; *v >= 0 && *v <= '9'; v++)
191 epoch = epochp && epochcnt ? *epochp : 0;
192 if (epoch || (v != version && *v == ':'))
194 char epochbuf[11]; /* 32bit decimal will fit in */
195 sprintf(epochbuf, "%u", epoch);
196 evr = xmalloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
197 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
201 evr = xmalloc(strlen(version) + 1 + strlen(release) + 1);
202 sprintf(evr, "%s-%s", version, release);
210 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
214 int i, cc, nc, vc, fc;
216 unsigned int olddeps;
219 n = headstringarray(rpmhead, tagn, &nc);
222 v = headstringarray(rpmhead, tagv, &vc);
228 f = headint32(rpmhead, tagf, &fc);
235 if (nc != vc || nc != fc)
237 fprintf(stderr, "bad dependency entries\n");
245 for (i = 0; i < nc; i++)
246 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
249 if ((f[i] & DEP_PRE) != 0)
255 for (i = 0; i < nc; i++)
256 if ((f[i] & DEP_PRE) != 0)
262 if (tagn != TAG_REQUIRENAME)
272 olddeps = repo_reserve_ids(repo, 0, cc);
273 ida = repo->idarraydata + olddeps;
282 *ida++ = SOLVABLE_PREREQMARKER;
284 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
286 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
288 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
290 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
294 if ((f[i] & DEP_LESS) != 0)
296 if ((f[i] & DEP_EQUAL) != 0)
298 if ((f[i] & DEP_GREATER) != 0)
300 name = str2id(pool, n[i], 1);
301 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
302 evr = str2id(pool, v[i] + 2, 1);
304 evr = str2id(pool, v[i], 1);
305 *ida++ = rel2id(pool, name, evr, flags, 1);
308 *ida++ = str2id(pool, n[i], 1);
311 repo->idarraysize += cc + 1;
319 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
324 Pool *frompool = fromrepo->pool;
328 from = fromrepo->idarraydata + fromoff;
329 for (ida = from, cc = 0; *ida; ida++, cc++)
333 ido = repo_reserve_ids(repo, 0, cc);
334 ida = repo->idarraydata + ido;
335 if (frompool && pool != frompool)
342 Reldep *rd = GETRELDEP(frompool, id);
343 Id name = str2id(pool, id2str(frompool, rd->name), 1);
344 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
345 id = rel2id(pool, name, evr, rd->flags, 1);
348 id = str2id(pool, id2str(frompool, id), 1);
354 memcpy(ida, from, (cc + 1) * sizeof(Id));
355 repo->idarraysize += cc + 1;
360 #define FILEFILTER_EXACT 0
361 #define FILEFILTER_STARTS 1
362 #define FILEFILTER_CONTAINS 2
370 static struct filefilter filefilters[] = {
371 { FILEFILTER_CONTAINS, "/bin/", 0},
372 { FILEFILTER_CONTAINS, "/sbin/", 0},
373 { FILEFILTER_CONTAINS, "/lib/", 0},
374 { FILEFILTER_CONTAINS, "/lib64/", 0},
375 { FILEFILTER_CONTAINS, "/etc/", 0},
376 { FILEFILTER_STARTS, "/usr/games/", 0},
377 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
378 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
379 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
382 /* assumes last processed array is provides! */
384 addfileprovides(Pool *pool, Repo *repo, RpmHead *rpmhead, unsigned int olddeps)
391 struct filefilter *ff;
395 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
398 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
404 di = headint32(rpmhead, TAG_DIRINDEXES, &dic);
413 fprintf(stderr, "bad filelist\n");
416 for (i = 0; i < bnc; i++)
419 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
423 switch (ff->dirmatch)
425 case FILEFILTER_STARTS:
426 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
429 case FILEFILTER_CONTAINS:
430 if (!strstr(dn[di[i]], ff->dir))
433 case FILEFILTER_EXACT:
435 if (strcmp(dn[di[i]], ff->dir))
442 if (strcmp(bn[i], ff->base))
447 if (j == sizeof(filefilters)/sizeof(*filefilters))
449 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
453 fn = xrealloc(fn, fna);
455 strcpy(fn, dn[di[i]]);
457 olddeps = repo_addid(repo, olddeps, str2id(pool, fn, 1));
468 rpm2solv(Pool *pool, Repo *repo, Solvable *s, RpmHead *rpmhead)
473 name = headstring(rpmhead, TAG_NAME);
474 if (!strcmp(name, "gpg-pubkey"))
476 s->name = str2id(pool, name, 1);
479 fprintf(stderr, "package has no name\n");
482 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
484 s->arch = ARCH_NOARCH;
485 evr = headtoevr(rpmhead);
486 s->evr = str2id(pool, evr, 1);
488 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
490 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
491 s->provides = addfileprovides(pool, repo, rpmhead, s->provides);
492 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
493 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
494 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
495 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
497 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
498 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
499 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
500 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
502 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
508 * read rpm db as repo
513 pool_addrepo_rpmdb(Pool *pool, Repo *ref)
515 unsigned char buf[16];
520 unsigned char *dp, *dbidp;
522 struct rpmid *rpmids, *rp;
529 unsigned int refmask, h;
532 repo = pool_addrepo_empty(pool);
534 if (ref && !(ref->nsolvables && ref->rpmdbid))
537 if (db_create(&db, 0, 0))
545 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
547 perror("db->open /var/lib/rpm/Packages");
550 if (db->get_byteswapped(db, &byteswapped))
552 perror("db->get_byteswapped");
555 if (db->cursor(db, NULL, &dbc, 0))
557 perror("db->cursor");
560 dbidp = (unsigned char *)&dbid;
561 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + 256) * sizeof(Solvable));
562 memset(pool->solvables + repo->start, 0, 256 * sizeof(Solvable));
563 repo->rpmdbid = xcalloc(256, sizeof(unsigned int));
568 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
572 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + asolv + 256) * sizeof(Solvable));
573 memset(pool->solvables + repo->start + asolv, 0, 256 * sizeof(Solvable));
574 repo->rpmdbid = xrealloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
575 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
578 pool->solvables[repo->start + i].repo = repo;
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, pool->solvables + repo->start + i, rpmhead))
624 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
626 perror("db->open /var/lib/rpm/Name");
629 if (db->get_byteswapped(db, &byteswapped))
631 perror("db->get_byteswapped");
634 if (db->cursor(db, NULL, &dbc, 0))
636 perror("db->cursor");
639 dbidp = (unsigned char *)&dbid;
642 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
644 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
658 memcpy(dbidp, dp, 4);
659 if ((nrpmids & 255) == 0)
660 rpmids = xrealloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
661 rpmids[nrpmids].dbid = dbid;
662 rpmids[nrpmids].name = malloc((int)key.size + 1);
663 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
664 rpmids[nrpmids].name[(int)key.size] = 0;
675 dbidp = (unsigned char *)&dbid;
679 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + nrpmids) * sizeof(Solvable));
680 memset(pool->solvables + repo->start, 0, nrpmids * sizeof(Solvable));
681 repo->rpmdbid = calloc(nrpmids, sizeof(unsigned int));
687 refmask = mkmask(ref->nsolvables);
688 refhash = xcalloc(refmask + 1, sizeof(Id));
689 for (i = 0; i < ref->nsolvables; i++)
691 h = ref->rpmdbid[i] & refmask;
693 h = (h + 317) & refmask;
694 refhash[h] = i + 1; /* make it non-zero */
697 s = pool->solvables + repo->start;
698 for (i = 0; i < nrpmids; i++, rp++, s++)
702 repo->rpmdbid[i] = dbid;
706 while ((id = refhash[h]))
708 if (ref->rpmdbid[id - 1] == dbid)
710 h = (h + 317) & refmask;
714 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
715 if (pool == ref->pool)
720 s->vendor = r->vendor;
725 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
727 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
729 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
731 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
733 s->provides = copydeps(pool, repo, r->provides, ref);
734 s->requires = copydeps(pool, repo, r->requires, ref);
735 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
736 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
737 s->recommends = copydeps(pool, repo, r->recommends, ref);
738 s->suggests = copydeps(pool, repo, r->suggests, ref);
739 s->supplements = copydeps(pool, repo, r->supplements, ref);
740 s->enhances = copydeps(pool, repo, r->enhances, ref);
741 s->freshens = copydeps(pool, repo, r->freshens, ref);
747 if (db_create(&db, 0, 0))
752 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
754 perror("db->open /var/lib/rpm/Packages");
757 if (db->get_byteswapped(db, &byteswapped))
759 perror("db->get_byteswapped");
771 memcpy(buf, dbidp, 4);
776 if (db->get(db, NULL, &key, &data, 0))
779 fprintf(stderr, "corrupt rpm database\n");
784 fprintf(stderr, "corrupt rpm database (size)\n");
787 if (data.size > rpmheadsize)
788 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
789 memcpy(buf, data.data, 8);
790 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
791 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
792 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
794 fprintf(stderr, "corrupt rpm database (data size)\n");
797 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
798 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
800 rpm2solv(pool, repo, s, rpmhead);
807 for (i = 0; i < nrpmids; i++)
808 free(rpmids[i].name);
814 pool->nsolvables += nrpmids;
815 repo->nsolvables = nrpmids;