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>
30 #include "repo_rpmdb.h"
34 #define TAG_VERSION 1001
35 #define TAG_RELEASE 1002
36 #define TAG_EPOCH 1003
37 #define TAG_SUMMARY 1004
38 #define TAG_DESCRIPTION 1005
39 #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_SOURCERPM 1044
47 #define TAG_PROVIDENAME 1047
48 #define TAG_REQUIREFLAGS 1048
49 #define TAG_REQUIRENAME 1049
50 #define TAG_REQUIREVERSION 1050
51 #define TAG_NOSOURCE 1051
52 #define TAG_NOPATCH 1052
53 #define TAG_CONFLICTFLAGS 1053
54 #define TAG_CONFLICTNAME 1054
55 #define TAG_CONFLICTVERSION 1055
56 #define TAG_OBSOLETENAME 1090
57 #define TAG_PROVIDEFLAGS 1112
58 #define TAG_PROVIDEVERSION 1113
59 #define TAG_OBSOLETEFLAGS 1114
60 #define TAG_OBSOLETEVERSION 1115
61 #define TAG_DIRINDEXES 1116
62 #define TAG_BASENAMES 1117
63 #define TAG_DIRNAMES 1118
64 #define TAG_SUGGESTSNAME 1156
65 #define TAG_SUGGESTSVERSION 1157
66 #define TAG_SUGGESTSFLAGS 1158
67 #define TAG_ENHANCESNAME 1159
68 #define TAG_ENHANCESVERSION 1160
69 #define TAG_ENHANCESFLAGS 1161
71 #define DEP_LESS (1 << 1)
72 #define DEP_GREATER (1 << 2)
73 #define DEP_EQUAL (1 << 3)
74 #define DEP_STRONG (1 << 27)
75 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
86 typedef struct rpmhead {
90 unsigned char data[1];
94 headexists(RpmHead *h, int tag)
97 unsigned char *d, taga[4];
104 for (i = 0; i < h->cnt; i++, d -= 16)
105 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
110 static unsigned int *
111 headint32array(RpmHead *h, int tag, int *cnt)
113 unsigned int i, o, *r;
114 unsigned char *d, taga[4];
121 for (i = 0; i < h->cnt; i++, d -= 16)
122 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
126 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
128 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
129 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
130 if (o + 4 * i > h->dcnt)
133 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
136 for (o = 0; o < i; o++, d += 4)
137 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
142 headint32(RpmHead *h, int tag)
145 unsigned char *d, taga[4];
152 for (i = 0; i < h->cnt; i++, d -= 16)
153 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
157 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
159 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
160 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
161 if (i == 0 || o + 4 * i > h->dcnt)
164 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
167 static unsigned int *
168 headint16array(RpmHead *h, int tag, int *cnt)
170 unsigned int i, o, *r;
171 unsigned char *d, taga[4];
178 for (i = 0; i < h->cnt; i++, d -= 16)
179 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
183 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
185 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
186 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
187 if (o + 4 * i > h->dcnt)
190 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
193 for (o = 0; o < i; o++, d += 2)
194 r[o] = d[0] << 8 | d[1];
199 headstring(RpmHead *h, int tag)
202 unsigned char *d, taga[4];
208 for (i = 0; i < h->cnt; i++, d -= 16)
209 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
213 /* 6: STRING, 9: I18NSTRING */
214 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
216 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
217 return (char *)h->dp + o;
221 headstringarray(RpmHead *h, int tag, int *cnt)
224 unsigned char *d, taga[4];
232 for (i = 0; i < h->cnt; i++, d -= 16)
233 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
237 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
239 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
240 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
241 r = sat_calloc(i ? i : 1, sizeof(char *));
245 for (o = 0; o < i; o++)
249 d += strlen((char *)d) + 1;
250 if (d >= h->dp + h->dcnt)
259 static char *headtoevr(RpmHead *h)
266 version = headstring(h, TAG_VERSION);
267 release = headstring(h, TAG_RELEASE);
268 epoch = headint32(h, TAG_EPOCH);
269 if (!version || !release)
271 fprintf(stderr, "headtoevr: bad rpm header\n");
274 for (v = version; *v >= 0 && *v <= '9'; v++)
276 if (epoch || (v != version && *v == ':'))
278 char epochbuf[11]; /* 32bit decimal will fit in */
279 sprintf(epochbuf, "%u", epoch);
280 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
281 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
285 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
286 sprintf(evr, "%s-%s", version, release);
292 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
296 int i, cc, nc, vc, fc;
298 unsigned int olddeps;
301 n = headstringarray(rpmhead, tagn, &nc);
304 v = headstringarray(rpmhead, tagv, &vc);
310 f = headint32array(rpmhead, tagf, &fc);
317 if (nc != vc || nc != fc)
319 fprintf(stderr, "bad dependency entries\n");
327 for (i = 0; i < nc; i++)
328 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
331 if ((f[i] & DEP_PRE) != 0)
337 for (i = 0; i < nc; i++)
338 if ((f[i] & DEP_PRE) != 0)
344 if (tagn != TAG_REQUIRENAME)
354 olddeps = repo_reserve_ids(repo, 0, cc);
355 ida = repo->idarraydata + olddeps;
364 *ida++ = SOLVABLE_PREREQMARKER;
366 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
368 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
370 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
372 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
376 if ((f[i] & DEP_LESS) != 0)
378 if ((f[i] & DEP_EQUAL) != 0)
380 if ((f[i] & DEP_GREATER) != 0)
382 name = str2id(pool, n[i], 1);
383 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
384 evr = str2id(pool, v[i] + 2, 1);
386 evr = str2id(pool, v[i], 1);
387 *ida++ = rel2id(pool, name, evr, flags, 1);
390 *ida++ = str2id(pool, n[i], 1);
393 repo->idarraysize += cc + 1;
401 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
406 Pool *frompool = fromrepo->pool;
410 from = fromrepo->idarraydata + fromoff;
411 for (ida = from, cc = 0; *ida; ida++, cc++)
415 ido = repo_reserve_ids(repo, 0, cc);
416 ida = repo->idarraydata + ido;
417 if (frompool && pool != frompool)
424 Reldep *rd = GETRELDEP(frompool, id);
425 Id name = str2id(pool, id2str(frompool, rd->name), 1);
426 Id evr = str2id(pool, id2str(frompool, rd->evr), 1);
427 id = rel2id(pool, name, evr, rd->flags, 1);
430 id = str2id(pool, id2str(frompool, id), 1);
436 memcpy(ida, from, (cc + 1) * sizeof(Id));
437 repo->idarraysize += cc + 1;
442 #define FILEFILTER_EXACT 0
443 #define FILEFILTER_STARTS 1
444 #define FILEFILTER_CONTAINS 2
452 static struct filefilter filefilters[] = {
453 { FILEFILTER_CONTAINS, "/bin/", 0},
454 { FILEFILTER_CONTAINS, "/sbin/", 0},
455 { FILEFILTER_CONTAINS, "/lib/", 0},
456 { FILEFILTER_CONTAINS, "/lib64/", 0},
457 { FILEFILTER_CONTAINS, "/etc/", 0},
458 { FILEFILTER_STARTS, "/usr/games/", 0},
459 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
460 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
461 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
465 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
469 unsigned int *fkb, *fn, *fsz, *fm;
471 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
472 if (!fsz || fc != fszc)
477 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
478 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
479 if (!fm || fc != fszc)
485 fn = sat_calloc(dic, sizeof(unsigned int));
486 fkb = sat_calloc(dic, sizeof(unsigned int));
487 for (i = 0; i < fc; i++)
489 if (fsz[i] == 0 || !S_ISREG(fm[i]))
494 /* does not consider hard links. tough luck. */
495 fkb[di[i]] += fsz[i] / 1024 + 1;
500 repodata_extend(repodata, s - pool->solvables);
501 entry = (s - pool->solvables) - repodata->start;
502 for (i = 0; i < fc; i++)
506 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
507 did = repodata_str2dir(repodata, "/usr/src", 1);
509 did = repodata_str2dir(repodata, dn[i], 1);
510 repodata_add_dirnumnum(repodata, entry, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
516 /* assumes last processed array is provides! */
518 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
525 struct filefilter *ff;
531 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
534 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
540 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
549 fprintf(stderr, "bad filelist\n");
554 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
556 for (i = 0; i < bnc; i++)
559 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
563 switch (ff->dirmatch)
565 case FILEFILTER_STARTS:
566 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
569 case FILEFILTER_CONTAINS:
570 if (!strstr(dn[di[i]], ff->dir))
573 case FILEFILTER_EXACT:
575 if (strcmp(dn[di[i]], ff->dir))
582 if (strcmp(bn[i], ff->base))
587 if (j == sizeof(filefilters)/sizeof(*filefilters))
589 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
593 fn = sat_realloc(fn, fna);
595 strcpy(fn, dn[di[i]]);
598 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
603 repodata_extend(repodata, s - pool->solvables);
604 entry = (s - pool->solvables) - repodata->start;
605 did = repodata_str2dir(repodata, dn[di[i]], 1);
606 repodata_add_dirstr(repodata, entry, SOLVABLE_FILELIST, did, bn[i]);
618 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
623 name = headstring(rpmhead, TAG_NAME);
624 if (!strcmp(name, "gpg-pubkey"))
626 s->name = str2id(pool, name, 1);
629 fprintf(stderr, "package has no name\n");
632 if (headstring(rpmhead, TAG_SOURCERPM))
633 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
636 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
637 s->arch = ARCH_NOSRC;
642 s->arch = ARCH_NOARCH;
643 evr = headtoevr(rpmhead);
644 s->evr = str2id(pool, evr, 1);
646 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
648 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
649 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
650 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
651 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
652 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
653 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
655 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
656 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
657 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
658 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
660 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
668 repodata_extend(repodata, s - pool->solvables);
669 entry = (s - pool->solvables) - repodata->start;
670 str = headstring(rpmhead, TAG_SUMMARY);
672 repodata_set_str(repodata, entry, SOLVABLE_SUMMARY, str);
673 str = headstring(rpmhead, TAG_DESCRIPTION);
677 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
678 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
682 /* oh my, found SUSE special author section */
687 while (l > 0 && str[l - 1] == '\n')
690 repodata_set_str(repodata, entry, SOLVABLE_DESCRIPTION, str);
692 aut = str; /* copy over */
693 while (*p == ' ' || *p == '\n')
706 while (aut != str && aut[-1] == '\n')
710 repodata_set_str(repodata, entry, SOLVABLE_AUTHORS, str);
714 repodata_set_str(repodata, entry, SOLVABLE_DESCRIPTION, str);
716 str = headstring(rpmhead, TAG_GROUP);
718 repodata_set_poolstr(repodata, entry, SOLVABLE_GROUP, str);
719 u32 = headint32(rpmhead, TAG_BUILDTIME);
721 repodata_set_num(repodata, entry, SOLVABLE_BUILDTIME, u32);
722 u32 = headint32(rpmhead, TAG_SIZE);
724 repodata_set_num(repodata, entry, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
731 * read rpm db as repo
736 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
738 Pool *pool = repo->pool;
739 unsigned char buf[16];
744 unsigned char *dp, *dbidp;
746 struct rpmid *rpmids, *rp;
752 unsigned int refmask, h;
755 char dbpath[PATH_MAX];
757 if (repo->start != repo->end)
758 abort(); /* FIXME: rpmdbid */
760 repodata = repo_add_repodata(repo);
762 if (ref && !(ref->nsolvables && ref->rpmdbid))
765 if (db_create(&db, 0, 0))
773 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
774 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
776 perror("db->open var/lib/rpm/Packages");
779 if (db->get_byteswapped(db, &byteswapped))
781 perror("db->get_byteswapped");
784 if (db->cursor(db, NULL, &dbc, 0))
786 perror("db->cursor");
789 dbidp = (unsigned char *)&dbid;
790 repo->rpmdbid = sat_calloc(256, sizeof(unsigned int));
796 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
799 s = pool_id2solvable(pool, repo_add_solvable(repo));
802 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(unsigned int));
803 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
808 fprintf(stderr, "corrupt Packages database (key size)\n");
820 memcpy(dbidp, dp, 4);
821 if (dbid == 0) /* the join key */
825 fprintf(stderr, "corrupt rpm database (size %u)\n", data.size);
828 if (data.size > rpmheadsize)
829 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + data.size);
830 memcpy(buf, data.data, 8);
831 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
832 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
833 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
835 fprintf(stderr, "corrupt rpm database (data size)\n");
838 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
839 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
840 repo->rpmdbid[i] = dbid;
841 if (rpm2solv(pool, repo, repodata, s, rpmhead))
848 /* We can reuse this solvable, but make sure it's still
849 associated with this repo. */
850 memset(s, 0, sizeof(*s));
856 /* oops, could not reuse. free it instead */
857 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
866 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
867 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
869 perror("db->open var/lib/rpm/Name");
872 if (db->get_byteswapped(db, &byteswapped))
874 perror("db->get_byteswapped");
877 if (db->cursor(db, NULL, &dbc, 0))
879 perror("db->cursor");
882 dbidp = (unsigned char *)&dbid;
885 while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0)
887 if (key.size == 10 && !memcmp(key.data, "gpg-pubkey", 10))
901 memcpy(dbidp, dp, 4);
902 if ((nrpmids & 255) == 0)
903 rpmids = sat_realloc(rpmids, sizeof(*rpmids) * (nrpmids + 256));
904 rpmids[nrpmids].dbid = dbid;
905 rpmids[nrpmids].name = sat_malloc((int)key.size + 1);
906 memcpy(rpmids[nrpmids].name, key.data, (int)key.size);
907 rpmids[nrpmids].name[(int)key.size] = 0;
918 dbidp = (unsigned char *)&dbid;
926 refmask = mkmask(ref->nsolvables);
927 refhash = sat_calloc(refmask + 1, sizeof(Id));
928 for (i = 0; i < ref->nsolvables; i++)
930 h = ref->rpmdbid[i] & refmask;
932 h = (h + 317) & refmask;
933 refhash[h] = i + 1; /* make it non-zero */
937 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
939 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
941 for (i = 0; i < nrpmids; i++, rp++, s++)
944 repo->rpmdbid[i] = dbid;
948 while ((id = refhash[h]))
950 if (ref->rpmdbid[id - 1] == dbid)
952 h = (h + 317) & refmask;
956 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
957 if (pool == ref->pool)
962 s->vendor = r->vendor;
967 s->name = str2id(pool, id2str(ref->pool, r->name), 1);
969 s->evr = str2id(pool, id2str(ref->pool, r->evr), 1);
971 s->arch = str2id(pool, id2str(ref->pool, r->arch), 1);
973 s->vendor = str2id(pool, id2str(ref->pool, r->vendor), 1);
975 s->provides = copydeps(pool, repo, r->provides, ref);
976 s->requires = copydeps(pool, repo, r->requires, ref);
977 s->conflicts = copydeps(pool, repo, r->conflicts, ref);
978 s->obsoletes = copydeps(pool, repo, r->obsoletes, ref);
979 s->recommends = copydeps(pool, repo, r->recommends, ref);
980 s->suggests = copydeps(pool, repo, r->suggests, ref);
981 s->supplements = copydeps(pool, repo, r->supplements, ref);
982 s->enhances = copydeps(pool, repo, r->enhances, ref);
983 s->freshens = copydeps(pool, repo, r->freshens, ref);
989 if (db_create(&db, 0, 0))
994 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
995 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
997 perror("db->open var/lib/rpm/Packages");
1000 if (db->get_byteswapped(db, &byteswapped))
1002 perror("db->get_byteswapped");
1014 memcpy(buf, dbidp, 4);
1019 if (db->get(db, NULL, &key, &data, 0))
1022 fprintf(stderr, "corrupt rpm database\n");
1027 fprintf(stderr, "corrupt rpm database (size)\n");
1030 if (data.size > rpmheadsize)
1031 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + data.size);
1032 memcpy(buf, data.data, 8);
1033 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1034 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1035 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > data.size)
1037 fprintf(stderr, "corrupt rpm database (data size)\n");
1040 memcpy(rpmhead->data, (unsigned char *)data.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1041 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1043 rpm2solv(pool, repo, repodata, s, rpmhead);
1050 for (i = 0; i < nrpmids; i++)
1051 sat_free(rpmids[i].name);
1060 repodata_internalize(repodata);
1063 static inline unsigned int
1064 getu32(unsigned char *dp)
1066 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1070 add_location(Repodata *data, Solvable *s, const char *location)
1072 Pool *pool = s->repo->pool;
1073 const char *name, *n1, *n2;
1077 repodata_extend(data, s - pool->solvables);
1078 entry = (s - pool->solvables) - data->start;
1080 /* skip ./ prefix */
1081 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1084 name = strrchr(location, '/');
1090 n2 = id2str(pool, s->arch);
1092 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1094 /* too bad, need to store directory */
1095 char *dir = strdup(location);
1096 dir[name - location - 1] = 0;
1097 repodata_set_str(data, entry, SOLVABLE_MEDIADIR, dir);
1101 repodata_set_void(data, entry, SOLVABLE_MEDIADIR);
1104 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1107 if (*n2 || *n1 != '-')
1110 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1113 if (*n2 || *n1 != '.')
1116 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1119 if (*n2 || strcmp (n1, ".rpm"))
1121 repodata_set_void(data, entry, SOLVABLE_MEDIAFILE);
1125 repodata_set_str(data, entry, SOLVABLE_MEDIAFILE, name);
1131 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1133 int i, sigdsize, sigcnt, l;
1134 Pool *pool = repo->pool;
1137 RpmHead *rpmhead = 0;
1138 int rpmheadsize = 0;
1140 unsigned char lead[4096];
1144 repodata = repo_add_repodata(repo);
1145 for (i = 0; i < nrpms; i++)
1147 if ((fp = fopen(rpms[i], "r")) == 0)
1152 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1154 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1158 if (lead[78] != 0 || lead[79] != 5)
1160 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1164 if (getu32(lead + 96) != 0x8eade801)
1166 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1170 sigcnt = getu32(lead + 96 + 8);
1171 sigdsize = getu32(lead + 96 + 12);
1172 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1174 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1178 sigdsize += sigcnt * 16;
1179 sigdsize = (sigdsize + 7) & ~7;
1182 l = sigdsize > 4096 ? 4096 : sigdsize;
1183 if (fread(lead, l, 1, fp) != 1)
1185 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1191 if (fread(lead, 16, 1, fp) != 1)
1193 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1197 if (getu32(lead) != 0x8eade801)
1199 fprintf(stderr, "%s: bad header\n", rpms[i]);
1203 sigcnt = getu32(lead + 8);
1204 sigdsize = getu32(lead + 12);
1205 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1207 fprintf(stderr, "%s: bad header\n", rpms[i]);
1211 l = sigdsize + sigcnt * 16;
1212 if (l > rpmheadsize)
1213 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1214 if (fread(rpmhead->data, l, 1, fp) != 1)
1216 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1220 rpmhead->cnt = sigcnt;
1221 rpmhead->dcnt = sigdsize;
1222 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1224 s = pool_id2solvable(pool, repo_add_solvable(repo));
1225 rpm2solv(pool, repo, repodata, s, rpmhead);
1226 add_location(repodata, s, rpms[i]);
1231 repodata_internalize(repodata);