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>
29 #include "repo_rpmdb.h"
31 #include "tools_util.h"
35 #define TAG_VERSION 1001
36 #define TAG_RELEASE 1002
37 #define TAG_EPOCH 1003
38 #define TAG_SUMMARY 1004
39 #define TAG_DESCRIPTION 1005
40 #define TAG_BUILDTIME 1006
41 #define TAG_VENDOR 1011
42 #define TAG_GROUP 1016
44 #define TAG_FILESIZES 1028
45 #define TAG_FILEMODES 1030
46 #define TAG_PROVIDENAME 1047
47 #define TAG_REQUIREFLAGS 1048
48 #define TAG_REQUIRENAME 1049
49 #define TAG_REQUIREVERSION 1050
50 #define TAG_CONFLICTFLAGS 1053
51 #define TAG_CONFLICTNAME 1054
52 #define TAG_CONFLICTVERSION 1055
53 #define TAG_OBSOLETENAME 1090
54 #define TAG_PROVIDEFLAGS 1112
55 #define TAG_PROVIDEVERSION 1113
56 #define TAG_OBSOLETEFLAGS 1114
57 #define TAG_OBSOLETEVERSION 1115
58 #define TAG_DIRINDEXES 1116
59 #define TAG_BASENAMES 1117
60 #define TAG_DIRNAMES 1118
61 #define TAG_SUGGESTSNAME 1156
62 #define TAG_SUGGESTSVERSION 1157
63 #define TAG_SUGGESTSFLAGS 1158
64 #define TAG_ENHANCESNAME 1159
65 #define TAG_ENHANCESVERSION 1160
66 #define TAG_ENHANCESFLAGS 1161
68 #define DEP_LESS (1 << 1)
69 #define DEP_GREATER (1 << 2)
70 #define DEP_EQUAL (1 << 3)
71 #define DEP_STRONG (1 << 27)
72 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
83 typedef struct rpmhead {
87 unsigned char data[1];
91 headint32array(RpmHead *h, int tag, int *cnt)
93 unsigned int i, o, *r;
94 unsigned char *d, taga[4];
101 for (i = 0; i < h->cnt; i++, d -= 16)
102 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
106 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
108 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
109 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
110 if (o + 4 * i > h->dcnt)
113 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
116 for (o = 0; o < i; o++, d += 4)
117 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
122 headint32(RpmHead *h, int tag)
125 unsigned char *d, taga[4];
132 for (i = 0; i < h->cnt; i++, d -= 16)
133 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
137 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
139 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
140 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
141 if (i == 0 || o + 4 * i > h->dcnt)
144 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
147 static unsigned int *
148 headint16array(RpmHead *h, int tag, int *cnt)
150 unsigned int i, o, *r;
151 unsigned char *d, taga[4];
158 for (i = 0; i < h->cnt; i++, d -= 16)
159 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
163 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
165 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
166 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
167 if (o + 4 * i > h->dcnt)
170 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
173 for (o = 0; o < i; o++, d += 2)
174 r[o] = d[0] << 8 | d[1];
179 headstring(RpmHead *h, int tag)
182 unsigned char *d, taga[4];
188 for (i = 0; i < h->cnt; i++, d -= 16)
189 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
193 /* 6: STRING, 9: I18NSTRING */
194 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
196 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
197 return (char *)h->dp + o;
201 headstringarray(RpmHead *h, int tag, int *cnt)
204 unsigned char *d, taga[4];
212 for (i = 0; i < h->cnt; i++, d -= 16)
213 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
217 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
219 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
220 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
221 r = sat_calloc(i ? i : 1, sizeof(char *));
225 for (o = 0; o < i; o++)
229 d += strlen((char *)d) + 1;
230 if (d >= h->dp + h->dcnt)
239 static char *headtoevr(RpmHead *h)
246 version = headstring(h, TAG_VERSION);
247 release = headstring(h, TAG_RELEASE);
248 epoch = headint32(h, TAG_EPOCH);
249 if (!version || !release)
251 fprintf(stderr, "headtoevr: bad rpm header\n");
254 for (v = version; *v >= 0 && *v <= '9'; v++)
256 if (epoch || (v != version && *v == ':'))
258 char epochbuf[11]; /* 32bit decimal will fit in */
259 sprintf(epochbuf, "%u", epoch);
260 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
261 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
265 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
266 sprintf(evr, "%s-%s", version, release);
272 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
276 int i, cc, nc, vc, fc;
278 unsigned int olddeps;
281 n = headstringarray(rpmhead, tagn, &nc);
284 v = headstringarray(rpmhead, tagv, &vc);
290 f = headint32array(rpmhead, tagf, &fc);
297 if (nc != vc || nc != fc)
299 fprintf(stderr, "bad dependency entries\n");
307 for (i = 0; i < nc; i++)
308 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
311 if ((f[i] & DEP_PRE) != 0)
317 for (i = 0; i < nc; i++)
318 if ((f[i] & DEP_PRE) != 0)
324 if (tagn != TAG_REQUIRENAME)
334 olddeps = repo_reserve_ids(repo, 0, cc);
335 ida = repo->idarraydata + olddeps;
344 *ida++ = SOLVABLE_PREREQMARKER;
346 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
348 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
350 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
352 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
356 if ((f[i] & DEP_LESS) != 0)
358 if ((f[i] & DEP_EQUAL) != 0)
360 if ((f[i] & DEP_GREATER) != 0)
362 name = str2id(pool, n[i], 1);
363 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
364 evr = str2id(pool, v[i] + 2, 1);
366 evr = str2id(pool, v[i], 1);
367 *ida++ = rel2id(pool, name, evr, flags, 1);
370 *ida++ = str2id(pool, n[i], 1);
373 repo->idarraysize += cc + 1;
381 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
386 Pool *frompool = fromrepo->pool;
390 from = fromrepo->idarraydata + fromoff;
391 for (ida = from, cc = 0; *ida; ida++, cc++)
395 ido = repo_reserve_ids(repo, 0, cc);
396 ida = repo->idarraydata + ido;
397 if (frompool && pool != frompool)
404 Reldep *rd = GETRELDEP(frompool, id);
405 Id name = str2id(pool, id2str(frompool, rd->name), 1);
406 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
407 id = rel2id(pool, name, evr, rd->flags, 1);
410 id = str2id(pool, id2str(frompool, id), 1);
416 memcpy(ida, from, (cc + 1) * sizeof(Id));
417 repo->idarraysize += cc + 1;
422 #define FILEFILTER_EXACT 0
423 #define FILEFILTER_STARTS 1
424 #define FILEFILTER_CONTAINS 2
432 static struct filefilter filefilters[] = {
433 { FILEFILTER_CONTAINS, "/bin/", 0},
434 { FILEFILTER_CONTAINS, "/sbin/", 0},
435 { FILEFILTER_CONTAINS, "/lib/", 0},
436 { FILEFILTER_CONTAINS, "/lib64/", 0},
437 { FILEFILTER_CONTAINS, "/etc/", 0},
438 { FILEFILTER_STARTS, "/usr/games/", 0},
439 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
440 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
441 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
445 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
449 unsigned int *fkb, *fn, *fsz, *fm;
451 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
452 if (!fsz || fc != fszc)
457 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
458 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
459 if (!fm || fc != fszc)
465 fn = sat_calloc(dic, sizeof(unsigned int));
466 fkb = sat_calloc(dic, sizeof(unsigned int));
467 for (i = 0; i < fc; i++)
469 if (fsz[i] == 0 || !S_ISREG(fm[i]))
474 /* does not consider hard links. tough luck. */
475 fkb[di[i]] += fsz[i] / 1024 + 1;
480 repodata_extend(repodata, s - pool->solvables);
481 entry = (s - pool->solvables) - repodata->start;
482 for (i = 0; i < fc; i++)
486 did = repodata_str2dir(repodata, dn[i], 1);
487 repodata_add_dirnumnum(repodata, entry, id_diskusage, did, fkb[i], fn[i]);
493 /* assumes last processed array is provides! */
495 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
502 struct filefilter *ff;
508 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
511 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
517 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
526 fprintf(stderr, "bad filelist\n");
531 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
533 for (i = 0; i < bnc; i++)
536 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
540 switch (ff->dirmatch)
542 case FILEFILTER_STARTS:
543 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
546 case FILEFILTER_CONTAINS:
547 if (!strstr(dn[di[i]], ff->dir))
550 case FILEFILTER_EXACT:
552 if (strcmp(dn[di[i]], ff->dir))
559 if (strcmp(bn[i], ff->base))
564 if (j == sizeof(filefilters)/sizeof(*filefilters))
566 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
570 fn = sat_realloc(fn, fna);
572 strcpy(fn, dn[di[i]]);
575 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
580 repodata_extend(repodata, s - pool->solvables);
581 entry = (s - pool->solvables) - repodata->start;
582 did = repodata_str2dir(repodata, dn[di[i]], 1);
583 repodata_add_dirstr(repodata, entry, id_filelist, did, bn[i]);
595 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
600 name = headstring(rpmhead, TAG_NAME);
601 if (!strcmp(name, "gpg-pubkey"))
603 s->name = str2id(pool, name, 1);
606 fprintf(stderr, "package has no name\n");
609 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
611 s->arch = ARCH_NOARCH;
612 evr = headtoevr(rpmhead);
613 s->evr = str2id(pool, evr, 1);
615 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
617 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
618 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
619 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
620 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
621 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
622 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
624 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
625 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
626 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
627 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
629 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
637 repodata_extend(repodata, s - pool->solvables);
638 entry = (s - pool->solvables) - repodata->start;
639 str = headstring(rpmhead, TAG_SUMMARY);
641 repodata_set_str(repodata, entry, id_summary, str);
642 str = headstring(rpmhead, TAG_DESCRIPTION);
646 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
647 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
651 /* oh my, found SUSE special author section */
656 while (l > 0 && str[l - 1] == '\n')
659 repodata_set_str(repodata, entry, id_description, str);
661 aut = str; /* copy over */
662 while (*p == ' ' || *p == '\n')
675 while (aut != str && aut[-1] == '\n')
679 repodata_set_str(repodata, entry, id_authors, str);
683 repodata_set_str(repodata, entry, id_description, str);
685 str = headstring(rpmhead, TAG_GROUP);
687 repodata_set_poolstr(repodata, entry, id_group, str);
688 u32 = headint32(rpmhead, TAG_BUILDTIME);
690 repodata_set_num(repodata, entry, id_time, u32);
697 * read rpm db as repo
702 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
704 Pool *pool = repo->pool;
705 unsigned char buf[16];
710 unsigned char *dp, *dbidp;
712 struct rpmid *rpmids, *rp;
718 unsigned int refmask, h;
722 if (repo->start != repo->end)
723 abort(); /* FIXME: rpmdbid */
725 repodata = repo_add_repodata(repo);
726 init_attr_ids(repo->pool);
728 if (ref && !(ref->nsolvables && ref->rpmdbid))
731 if (db_create(&db, 0, 0))
739 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
741 perror("db->open /var/lib/rpm/Packages");
744 if (db->get_byteswapped(db, &byteswapped))
746 perror("db->get_byteswapped");
749 if (db->cursor(db, NULL, &dbc, 0))
751 perror("db->cursor");
754 dbidp = (unsigned char *)&dbid;
755 repo->rpmdbid = sat_calloc(256, sizeof(unsigned int));
761 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
764 s = pool_id2solvable(pool, repo_add_solvable(repo));
767 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
768 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
773 fprintf(stderr, "corrupt Packages database (key size)\n");
785 memcpy(dbidp, dp, 4);
786 if (dbid == 0) /* the join key */
790 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
793 if (data.size > rpmheadsize)
794 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + data.size);
795 memcpy(buf, data.data, 8);
796 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
797 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
798 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
800 fprintf(stderr, "corrupt rpm database (data size)\n");
803 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
804 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
805 repo->rpmdbid[i] = dbid;
806 if (rpm2solv(pool, repo, repodata, s, rpmhead))
813 /* We can reuse this solvable, but make sure it's still
814 associated with this repo. */
815 memset(s, 0, sizeof(*s));
821 /* oops, could not reuse. free it instead */
822 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
831 if (db->open(db, 0, "/var/lib/rpm/Name", 0, DB_HASH, DB_RDONLY, 0664))
833 perror("db->open /var/lib/rpm/Name");
836 if (db->get_byteswapped(db, &byteswapped))
838 perror("db->get_byteswapped");
841 if (db->cursor(db, NULL, &dbc, 0))
843 perror("db->cursor");
846 dbidp = (unsigned char *)&dbid;
849 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
851 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
865 memcpy(dbidp, dp, 4);
866 if ((nrpmids & 255) == 0)
867 rpmids = sat_realloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
868 rpmids[nrpmids].dbid = dbid;
869 rpmids[nrpmids].name = sat_malloc((int)key.size + 1);
870 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
871 rpmids[nrpmids].name[(int)key.size] = 0;
882 dbidp = (unsigned char *)&dbid;
890 refmask = mkmask(ref->nsolvables);
891 refhash = sat_calloc(refmask + 1, sizeof(Id));
892 for (i = 0; i < ref->nsolvables; i++)
894 h = ref->rpmdbid[i] & refmask;
896 h = (h + 317) & refmask;
897 refhash[h] = i + 1; /* make it non-zero */
901 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
903 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
905 for (i = 0; i < nrpmids; i++, rp++, s++)
908 repo->rpmdbid[i] = dbid;
912 while ((id = refhash[h]))
914 if (ref->rpmdbid[id - 1] == dbid)
916 h = (h + 317) & refmask;
920 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
921 if (pool == ref->pool)
926 s->vendor = r->vendor;
931 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
933 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
935 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
937 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
939 s->provides = copydeps(pool, repo, r->provides, ref);
940 s->requires = copydeps(pool, repo, r->requires, ref);
941 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
942 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
943 s->recommends = copydeps(pool, repo, r->recommends, ref);
944 s->suggests = copydeps(pool, repo, r->suggests, ref);
945 s->supplements = copydeps(pool, repo, r->supplements, ref);
946 s->enhances = copydeps(pool, repo, r->enhances, ref);
947 s->freshens = copydeps(pool, repo, r->freshens, ref);
953 if (db_create(&db, 0, 0))
958 if (db->open(db, 0, "/var/lib/rpm/Packages", 0, DB_HASH, DB_RDONLY, 0664))
960 perror("db->open /var/lib/rpm/Packages");
963 if (db->get_byteswapped(db, &byteswapped))
965 perror("db->get_byteswapped");
977 memcpy(buf, dbidp, 4);
982 if (db->get(db, NULL, &key, &data, 0))
985 fprintf(stderr, "corrupt rpm database\n");
990 fprintf(stderr, "corrupt rpm database (size)\n");
993 if (data.size > rpmheadsize)
994 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + data.size);
995 memcpy(buf, data.data, 8);
996 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
997 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
998 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
1000 fprintf(stderr, "corrupt rpm database (data size)\n");
1003 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1004 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1006 rpm2solv(pool, repo, repodata, s, rpmhead);
1013 for (i = 0; i < nrpmids; i++)
1014 sat_free(rpmids[i].name);
1023 repodata_internalize(repodata);
1026 /* XXX: delete me! */