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 repo_add_rpmdb(Repo *repo, Repo *ref)
515 Pool *pool = repo->pool;
516 unsigned char buf[16];
521 unsigned char *dp, *dbidp;
523 struct rpmid *rpmids, *rp;
529 unsigned int refmask, h;
532 if (repo->start && repo->start + repo->nsolvables != pool->nsolvables)
535 repo->start = pool->nsolvables;
537 if (ref && !(ref->nsolvables && ref->rpmdbid))
540 if (db_create(&db, 0, 0))
548 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
550 perror("db->open /var/lib/rpm/Packages");
553 if (db->get_byteswapped(db, &byteswapped))
555 perror("db->get_byteswapped");
558 if (db->cursor(db, NULL, &dbc, 0))
560 perror("db->cursor");
563 dbidp = (unsigned char *)&dbid;
564 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + 256) * sizeof(Solvable));
565 memset(pool->solvables + pool->nsolvables, 0, 256 * sizeof(Solvable));
566 repo->rpmdbid = xcalloc(256, sizeof(unsigned int));
571 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
575 pool->solvables = xrealloc(pool->solvables, (pool->nsolvables + asolv + 256) * sizeof(Solvable));
576 memset(pool->solvables + pool->nsolvables + asolv, 0, 256 * sizeof(Solvable));
577 repo->rpmdbid = xrealloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
578 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
581 pool->solvables[pool->nsolvables + i].repo = repo;
584 fprintf(stderr, "corrupt Packages database (key size)\n");
596 memcpy(dbidp, dp, 4);
597 if (dbid == 0) /* the join key */
601 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
604 if (data.size > rpmheadsize)
605 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
606 memcpy(buf, data.data, 8);
607 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
608 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
609 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
611 fprintf(stderr, "corrupt rpm database (data size)\n");
614 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
615 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
616 repo->rpmdbid[i] = dbid;
617 if (rpm2solv(pool, repo, pool->solvables + pool->nsolvables + i, rpmhead))
627 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
629 perror("db->open /var/lib/rpm/Name");
632 if (db->get_byteswapped(db, &byteswapped))
634 perror("db->get_byteswapped");
637 if (db->cursor(db, NULL, &dbc, 0))
639 perror("db->cursor");
642 dbidp = (unsigned char *)&dbid;
645 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
647 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
661 memcpy(dbidp, dp, 4);
662 if ((nrpmids & 255) == 0)
663 rpmids = xrealloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
664 rpmids[nrpmids].dbid = dbid;
665 rpmids[nrpmids].name = malloc((int)key.size + 1);
666 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
667 rpmids[nrpmids].name[(int)key.size] = 0;
678 dbidp = (unsigned char *)&dbid;
682 pool->solvables = xrealloc2(pool->solvables, (pool->nsolvables + nrpmids), sizeof(Solvable));
683 memset(pool->solvables + pool->nsolvables, 0, nrpmids * sizeof(Solvable));
684 repo->rpmdbid = calloc(nrpmids, sizeof(unsigned int));
690 refmask = mkmask(ref->nsolvables);
691 refhash = xcalloc(refmask + 1, sizeof(Id));
692 for (i = 0; i < ref->nsolvables; i++)
694 h = ref->rpmdbid[i] & refmask;
696 h = (h + 317) & refmask;
697 refhash[h] = i + 1; /* make it non-zero */
700 s = pool->solvables + pool->nsolvables;
701 for (i = 0; i < nrpmids; i++, rp++, s++)
705 repo->rpmdbid[i] = dbid;
709 while ((id = refhash[h]))
711 if (ref->rpmdbid[id - 1] == dbid)
713 h = (h + 317) & refmask;
717 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
718 if (pool == ref->pool)
723 s->vendor = r->vendor;
728 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
730 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
732 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
734 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
736 s->provides = copydeps(pool, repo, r->provides, ref);
737 s->requires = copydeps(pool, repo, r->requires, ref);
738 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
739 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
740 s->recommends = copydeps(pool, repo, r->recommends, ref);
741 s->suggests = copydeps(pool, repo, r->suggests, ref);
742 s->supplements = copydeps(pool, repo, r->supplements, ref);
743 s->enhances = copydeps(pool, repo, r->enhances, ref);
744 s->freshens = copydeps(pool, repo, r->freshens, ref);
750 if (db_create(&db, 0, 0))
755 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
757 perror("db->open /var/lib/rpm/Packages");
760 if (db->get_byteswapped(db, &byteswapped))
762 perror("db->get_byteswapped");
774 memcpy(buf, dbidp, 4);
779 if (db->get(db, NULL, &key, &data, 0))
782 fprintf(stderr, "corrupt rpm database\n");
787 fprintf(stderr, "corrupt rpm database (size)\n");
790 if (data.size > rpmheadsize)
791 rpmhead = xrealloc(rpmhead, sizeof(*rpmhead) + data.size);
792 memcpy(buf, data.data, 8);
793 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
794 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
795 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
797 fprintf(stderr, "corrupt rpm database (data size)\n");
800 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
801 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
803 rpm2solv(pool, repo, s, rpmhead);
810 for (i = 0; i < nrpmids; i++)
811 free(rpmids[i].name);
817 pool->nsolvables += nrpmids;
818 repo->nsolvables += nrpmids;