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>
31 #include "repo_rpmdb.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_INSTALLTIME 1008
43 #define TAG_VENDOR 1011
44 #define TAG_LICENSE 1014
45 #define TAG_GROUP 1016
47 #define TAG_FILESIZES 1028
48 #define TAG_FILEMODES 1030
49 #define TAG_SOURCERPM 1044
50 #define TAG_PROVIDENAME 1047
51 #define TAG_REQUIREFLAGS 1048
52 #define TAG_REQUIRENAME 1049
53 #define TAG_REQUIREVERSION 1050
54 #define TAG_NOSOURCE 1051
55 #define TAG_NOPATCH 1052
56 #define TAG_CONFLICTFLAGS 1053
57 #define TAG_CONFLICTNAME 1054
58 #define TAG_CONFLICTVERSION 1055
59 #define TAG_OBSOLETENAME 1090
60 #define TAG_FILEDEVICES 1095
61 #define TAG_FILEINODES 1096
62 #define TAG_PROVIDEFLAGS 1112
63 #define TAG_PROVIDEVERSION 1113
64 #define TAG_OBSOLETEFLAGS 1114
65 #define TAG_OBSOLETEVERSION 1115
66 #define TAG_DIRINDEXES 1116
67 #define TAG_BASENAMES 1117
68 #define TAG_DIRNAMES 1118
69 #define TAG_PAYLOADFORMAT 1124
70 #define TAG_PATCHESNAME 1133
71 #define TAG_SUGGESTSNAME 1156
72 #define TAG_SUGGESTSVERSION 1157
73 #define TAG_SUGGESTSFLAGS 1158
74 #define TAG_ENHANCESNAME 1159
75 #define TAG_ENHANCESVERSION 1160
76 #define TAG_ENHANCESFLAGS 1161
78 #define DEP_LESS (1 << 1)
79 #define DEP_GREATER (1 << 2)
80 #define DEP_EQUAL (1 << 3)
81 #define DEP_STRONG (1 << 27)
82 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
90 typedef struct rpmhead {
94 unsigned char data[1];
98 headexists(RpmHead *h, int tag)
101 unsigned char *d, taga[4];
108 for (i = 0; i < h->cnt; i++, d -= 16)
109 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
114 static unsigned int *
115 headint32array(RpmHead *h, int tag, int *cnt)
117 unsigned int i, o, *r;
118 unsigned char *d, taga[4];
125 for (i = 0; i < h->cnt; i++, d -= 16)
126 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
130 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
132 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
133 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
134 if (o + 4 * i > h->dcnt)
137 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
140 for (o = 0; o < i; o++, d += 4)
141 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
146 headint32(RpmHead *h, int tag)
149 unsigned char *d, taga[4];
156 for (i = 0; i < h->cnt; i++, d -= 16)
157 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
161 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
163 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
164 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
165 if (i == 0 || o + 4 * i > h->dcnt)
168 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
171 static unsigned int *
172 headint16array(RpmHead *h, int tag, int *cnt)
174 unsigned int i, o, *r;
175 unsigned char *d, taga[4];
182 for (i = 0; i < h->cnt; i++, d -= 16)
183 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
187 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
189 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
190 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
191 if (o + 4 * i > h->dcnt)
194 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
197 for (o = 0; o < i; o++, d += 2)
198 r[o] = d[0] << 8 | d[1];
203 headstring(RpmHead *h, int tag)
206 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 /* 6: STRING, 9: I18NSTRING */
218 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
220 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
221 return (char *)h->dp + o;
225 headstringarray(RpmHead *h, int tag, int *cnt)
228 unsigned char *d, taga[4];
236 for (i = 0; i < h->cnt; i++, d -= 16)
237 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
241 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
243 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
244 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
245 r = sat_calloc(i ? i : 1, sizeof(char *));
249 for (o = 0; o < i; o++)
253 d += strlen((char *)d) + 1;
254 if (d >= h->dp + h->dcnt)
263 static char *headtoevr(RpmHead *h)
270 version = headstring(h, TAG_VERSION);
271 release = headstring(h, TAG_RELEASE);
272 epoch = headint32(h, TAG_EPOCH);
273 if (!version || !release)
275 fprintf(stderr, "headtoevr: bad rpm header\n");
278 for (v = version; *v >= 0 && *v <= '9'; v++)
280 if (epoch || (v != version && *v == ':'))
282 char epochbuf[11]; /* 32bit decimal will fit in */
283 sprintf(epochbuf, "%u", epoch);
284 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
285 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
289 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
290 sprintf(evr, "%s-%s", version, release);
297 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
299 const unsigned char *cp;
304 /* check if it's already utf8, code taken from screen ;-) */
305 cp = (const unsigned char *)str;
306 while ((c = *cp++) != 0)
310 if ((c & 0xc0) != 0x80)
311 break; /* encoding error */
312 c = (c & 0x3f) | (state << 6);
313 if (!(state & 0x40000000))
315 /* check for overlong sequences */
316 if ((c & 0x820823e0) == 0x80000000)
318 else if ((c & 0x020821f0) == 0x02000000)
320 else if ((c & 0x000820f8) == 0x00080000)
322 else if ((c & 0x0000207c) == 0x00002000)
332 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
334 c = (c & 0x03) | 0xbfffff00; /* 4 */
336 c = (c & 0x07) | 0xbfffc000; /* 3 */
338 c = (c & 0x0f) | 0xbff00000; /* 2 */
340 c = (c & 0x1f) | 0xfc000000; /* 1 */
342 c = 0xfdffffff; /* overlong */
346 state = (c & 0x80000000) ? c : 0;
350 /* not utf8, assume latin1 */
351 buf = sat_malloc(2 * strlen(str) + 1);
352 cp = (const unsigned char *)str;
355 while ((c = *cp++) != 0)
368 repodata_set_str(repodata, handle, tag, str);
374 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
378 int i, cc, nc, vc, fc;
380 unsigned int olddeps;
383 n = headstringarray(rpmhead, tagn, &nc);
386 v = headstringarray(rpmhead, tagv, &vc);
392 f = headint32array(rpmhead, tagf, &fc);
399 if (nc != vc || nc != fc)
401 fprintf(stderr, "bad dependency entries\n");
409 for (i = 0; i < nc; i++)
410 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
413 if ((f[i] & DEP_PRE) != 0)
419 for (i = 0; i < nc; i++)
420 if ((f[i] & DEP_PRE) != 0)
426 if (tagn != TAG_REQUIRENAME)
436 olddeps = repo_reserve_ids(repo, 0, cc);
437 ida = repo->idarraydata + olddeps;
446 *ida++ = SOLVABLE_PREREQMARKER;
448 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
450 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
452 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
454 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
458 if ((f[i] & DEP_LESS) != 0)
460 if ((f[i] & DEP_EQUAL) != 0)
462 if ((f[i] & DEP_GREATER) != 0)
464 name = str2id(pool, n[i], 1);
465 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
466 evr = str2id(pool, v[i] + 2, 1);
468 evr = str2id(pool, v[i], 1);
469 *ida++ = rel2id(pool, name, evr, flags, 1);
472 *ida++ = str2id(pool, n[i], 1);
475 repo->idarraysize += cc + 1;
483 #define FILEFILTER_EXACT 0
484 #define FILEFILTER_STARTS 1
485 #define FILEFILTER_CONTAINS 2
493 static struct filefilter filefilters[] = {
494 { FILEFILTER_CONTAINS, "/bin/", 0},
495 { FILEFILTER_CONTAINS, "/sbin/", 0},
496 { FILEFILTER_CONTAINS, "/lib/", 0},
497 { FILEFILTER_CONTAINS, "/lib64/", 0},
498 { FILEFILTER_CONTAINS, "/etc/", 0},
499 { FILEFILTER_STARTS, "/usr/games/", 0},
500 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
501 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
502 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
506 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
510 unsigned int *fkb, *fn, *fsz, *fm, *fino;
511 unsigned int inotest[256], inotestok;
515 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
516 if (!fsz || fc != fszc)
521 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
522 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
523 if (!fm || fc != fszc)
529 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
530 if (!fino || fc != fszc)
538 if (fc < sizeof(inotest))
540 memset(inotest, 0, sizeof(inotest));
541 for (i = 0; i < fc; i++)
544 if (fsz[i] == 0 || !S_ISREG(fm[i]))
546 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
547 bit = 1 << (fino[i] & 31);
548 if ((inotest[off] & bit) != 0)
557 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
559 unsigned int mask, hash, hh;
560 if (!fdev || fc != fszc)
569 while ((mask & (mask - 1)) != 0)
570 mask = mask & (mask - 1);
572 if (mask > sizeof(inotest)/sizeof(*inotest))
573 fx = sat_calloc(mask, sizeof(unsigned int));
577 memset(fx, 0, mask * sizeof(unsigned int));
580 for (i = 0; i < fc; i++)
582 if (fsz[i] == 0 || !S_ISREG(fm[i]))
584 hash = (fino[i] + fdev[i] * 31) & mask;
586 while ((j = fx[hash]) != 0)
588 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
590 fsz[i] = 0; /* kill entry */
593 hash = (hash + hh++) & mask;
603 fn = sat_calloc(dic, sizeof(unsigned int));
604 fkb = sat_calloc(dic, sizeof(unsigned int));
605 for (i = 0; i < fc; i++)
607 if (fsz[i] == 0 || !S_ISREG(fm[i]))
612 fkb[di[i]] += fsz[i] / 1024 + 1;
617 repodata_extend(repodata, s - pool->solvables);
618 handle = (s - pool->solvables) - repodata->start;
619 handle = repodata_get_handle(repodata, handle);
620 for (i = 0; i < fc; i++)
624 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
625 did = repodata_str2dir(repodata, "/usr/src", 1);
627 did = repodata_str2dir(repodata, dn[i], 1);
628 repodata_add_dirnumnum(repodata, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
634 /* assumes last processed array is provides! */
636 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
643 struct filefilter *ff;
651 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
654 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
660 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
669 fprintf(stderr, "bad filelist\n");
674 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
676 for (i = 0; i < bnc; i++)
679 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
683 switch (ff->dirmatch)
685 case FILEFILTER_STARTS:
686 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
689 case FILEFILTER_CONTAINS:
690 if (!strstr(dn[di[i]], ff->dir))
693 case FILEFILTER_EXACT:
695 if (strcmp(dn[di[i]], ff->dir))
702 if (strcmp(bn[i], ff->base))
707 if (j == sizeof(filefilters)/sizeof(*filefilters))
710 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
714 fn = sat_realloc(fn, fna);
716 strcpy(fn, dn[di[i]]);
718 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
723 repodata_extend(repodata, s - pool->solvables);
724 handle = (s - pool->solvables) - repodata->start;
725 handle = repodata_get_handle(repodata, handle);
726 did = repodata_str2dir(repodata, dn[di[i]], 1);
728 did = repodata_str2dir(repodata, "/", 1);
729 repodata_add_dirstr(repodata, handle, SOLVABLE_FILELIST, did, bn[i]);
743 addsourcerpm(Pool *pool, Repodata *repodata, Id handle, char *sourcerpm, char *name, char *evr)
745 const char *p, *sevr, *sarch;
747 p = strrchr(sourcerpm, '.');
748 if (!p || strcmp(p, ".rpm") != 0)
751 while (p > sourcerpm && *p != '.')
753 if (*p != '.' || p == sourcerpm)
756 while (p > sourcerpm && *p != '-')
758 if (*p != '-' || p == sourcerpm)
761 while (p > sourcerpm && *p != '-')
763 if (*p != '-' || p == sourcerpm)
766 if (!strcmp(sarch, "src.rpm"))
767 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
768 else if (!strcmp(sarch, "nosrc.rpm"))
769 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
771 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
772 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
773 repodata_set_void(repodata, handle, SOLVABLE_SOURCEEVR);
775 repodata_set_id(repodata, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
776 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
777 repodata_set_void(repodata, handle, SOLVABLE_SOURCENAME);
779 repodata_set_id(repodata, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
783 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
789 name = headstring(rpmhead, TAG_NAME);
790 if (!strcmp(name, "gpg-pubkey"))
792 s->name = str2id(pool, name, 1);
795 fprintf(stderr, "package has no name\n");
798 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
800 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
803 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
804 s->arch = ARCH_NOSRC;
809 s->arch = ARCH_NOARCH;
810 evr = headtoevr(rpmhead);
811 s->evr = str2id(pool, evr, 1);
812 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
814 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
815 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
816 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
817 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
818 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
819 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
821 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
822 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
823 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
824 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
826 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
834 repodata_extend(repodata, s - pool->solvables);
835 handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
836 str = headstring(rpmhead, TAG_SUMMARY);
838 setutf8string(repodata, handle, SOLVABLE_SUMMARY, str);
839 str = headstring(rpmhead, TAG_DESCRIPTION);
843 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
844 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
848 /* oh my, found SUSE special author section */
853 while (l > 0 && str[l - 1] == '\n')
856 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
858 aut = str; /* copy over */
859 while (*p == ' ' || *p == '\n')
872 while (aut != str && aut[-1] == '\n')
876 setutf8string(repodata, handle, SOLVABLE_AUTHORS, str);
880 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
882 str = headstring(rpmhead, TAG_GROUP);
884 repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
885 str = headstring(rpmhead, TAG_LICENSE);
887 repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
888 u32 = headint32(rpmhead, TAG_BUILDTIME);
890 repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
891 u32 = headint32(rpmhead, TAG_INSTALLTIME);
893 repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
894 u32 = headint32(rpmhead, TAG_SIZE);
896 repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
898 addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
905 copyreldep(Pool *pool, Pool *frompool, Id id)
907 Reldep *rd = GETRELDEP(frompool, id);
908 Id name = rd->name, evr = rd->evr;
910 name = copyreldep(pool, frompool, name);
912 name = str2id(pool, id2str(frompool, name), 1);
914 evr = copyreldep(pool, frompool, evr);
916 evr = str2id(pool, id2str(frompool, evr), 1);
917 return rel2id(pool, name, evr, rd->flags, 1);
921 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
926 Pool *frompool = fromrepo->pool;
930 from = fromrepo->idarraydata + fromoff;
931 for (ida = from, cc = 0; *ida; ida++, cc++)
935 ido = repo_reserve_ids(repo, 0, cc);
936 ida = repo->idarraydata + ido;
937 if (frompool && pool != frompool)
943 id = copyreldep(pool, frompool, id);
945 id = str2id(pool, id2str(frompool, id), 1);
951 memcpy(ida, from, (cc + 1) * sizeof(Id));
952 repo->idarraysize += cc + 1;
957 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did)
959 Id parent = dirpool_parent(&fromdata->dirpool, did);
960 Id compid = dirpool_compid(&fromdata->dirpool, did);
962 parent = copydir(pool, data, fromspool, fromdata, parent);
963 if (fromspool != &pool->ss)
964 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
965 return dirpool_add_dir(&data->dirpool, parent, compid, 1);
968 struct solvable_copy_cbdata {
974 solvable_copy_cb(void *cbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
977 Repodata *data = ((struct solvable_copy_cbdata *)cbdata)->data;
978 Id handle = ((struct solvable_copy_cbdata *)cbdata)->handle;
979 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
980 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
983 if (keyname >= ID_NUM_INTERNAL)
984 keyname = str2id(pool, id2str(frompool, keyname), 1);
987 case REPOKEY_TYPE_ID:
988 case REPOKEY_TYPE_CONSTANTID:
990 assert(!data->localpool); /* implement me! */
991 if (pool != frompool || fromdata->localpool)
994 id = copyreldep(pool, frompool, id);
996 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
998 if (key->type == REPOKEY_TYPE_ID)
999 repodata_set_id(data, handle, keyname, id);
1001 repodata_set_constantid(data, handle, keyname, id);
1003 case REPOKEY_TYPE_STR:
1004 repodata_set_str(data, handle, keyname, kv->str);
1006 case REPOKEY_TYPE_VOID:
1007 repodata_set_void(data, handle, keyname);
1009 case REPOKEY_TYPE_NUM:
1010 repodata_set_num(data, handle, keyname, kv->num);
1012 case REPOKEY_TYPE_CONSTANT:
1013 repodata_set_constant(data, handle, keyname, kv->num);
1015 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1017 assert(!data->localpool); /* implement me! */
1018 id = copydir(pool, data, fromspool, fromdata, id);
1019 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1021 case REPOKEY_TYPE_DIRSTRARRAY:
1023 assert(!data->localpool); /* implement me! */
1024 id = copydir(pool, data, fromspool, fromdata, id);
1025 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1034 solvable_copy(Solvable *s, Solvable *r, Repodata *data)
1036 Repo *repo = s->repo;
1037 Repo *fromrepo = r->repo;
1038 Pool *pool = repo->pool;
1039 struct solvable_copy_cbdata cbdata;
1041 /* copy solvable data */
1042 if (pool == fromrepo->pool)
1047 s->vendor = r->vendor;
1052 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1054 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1056 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1058 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1060 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1061 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1062 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1063 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1064 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1065 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1066 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1067 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1068 s->freshens = copydeps(pool, repo, r->freshens, fromrepo);
1070 /* copy all attributes */
1073 repodata_extend(data, s - pool->solvables);
1075 cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
1076 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1079 /* used to sort entries returned in some database order */
1081 rpmids_sort_cmp(const void *va, const void *vb)
1083 struct rpmid const *a = va, *b = vb;
1085 r = strcmp(a->name, b->name);
1088 return a->dbid - b->dbid;
1091 static Repo *pkgids_sort_cmp_data;
1094 pkgids_sort_cmp(const void *va, const void *vb)
1096 Pool *pool = pkgids_sort_cmp_data->pool;
1097 Solvable *a = pool->solvables + *(Id *)va;
1098 Solvable *b = pool->solvables + *(Id *)vb;
1101 if (a->name != b->name)
1102 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1103 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1104 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1108 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1110 Pool *pool = repo->pool;
1113 tmp = pool->solvables[pa];
1114 pool->solvables[pa] = pool->solvables[pb];
1115 pool->solvables[pb] = tmp;
1118 Id tmpid = repo->rpmdbid[pa - repo->start];
1119 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1120 repo->rpmdbid[pb - repo->start] = tmpid;
1122 /* only works if nothing is already internalized! */
1123 if (data && data->attrs)
1125 Id tmpattrs = data->attrs[pa - data->start];
1126 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1127 data->attrs[pb - data->start] = tmpattrs;
1132 * read rpm db as repo
1137 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
1139 Pool *pool = repo->pool;
1140 unsigned char buf[16];
1145 unsigned char *dp, *dbidp;
1147 struct rpmid *rpmids, *rp;
1153 unsigned int refmask, h;
1156 char dbpath[PATH_MAX];
1161 memset(&dbkey, 0, sizeof(dbkey));
1162 memset(&dbdata, 0, sizeof(dbdata));
1164 if (repo->start != repo->end)
1165 abort(); /* FIXME: rpmdbid */
1170 repodata = repo_add_repodata(repo, 0);
1172 if (ref && !(ref->nsolvables && ref->rpmdbid))
1175 if (db_env_create(&dbenv, 0))
1177 perror("db_env_create");
1180 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1181 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1183 perror("dbenv open");
1186 if (db_create(&db, dbenv, 0))
1188 perror("db_create");
1195 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1196 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1198 perror("db->open var/lib/rpm/Packages");
1201 if (db->get_byteswapped(db, &byteswapped))
1203 perror("db->get_byteswapped");
1206 if (db->cursor(db, NULL, &dbc, 0))
1208 perror("db->cursor");
1211 dbidp = (unsigned char *)&dbid;
1212 repo->rpmdbid = sat_calloc(256, sizeof(Id));
1218 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1221 s = pool_id2solvable(pool, repo_add_solvable(repo));
1224 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(Id));
1225 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
1228 if (dbkey.size != 4)
1230 fprintf(stderr, "corrupt Packages database (key size)\n");
1242 memcpy(dbidp, dp, 4);
1243 if (dbid == 0) /* the join key */
1245 if (dbdata.size < 8)
1247 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1250 if (dbdata.size > rpmheadsize)
1251 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1252 memcpy(buf, dbdata.data, 8);
1253 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1254 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1255 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1257 fprintf(stderr, "corrupt rpm database (data size)\n");
1260 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1261 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1262 repo->rpmdbid[i] = dbid;
1263 if (rpm2solv(pool, repo, repodata, s, rpmhead))
1270 /* We can reuse this solvable, but make sure it's still
1271 associated with this repo. */
1272 memset(s, 0, sizeof(*s));
1278 /* oops, could not reuse. free it instead */
1279 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1285 /* now sort all solvables */
1286 if (repo->end - repo->start > 1)
1288 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1289 for (i = repo->start; i < repo->end; i++)
1290 pkgids[i - repo->start] = i;
1291 pkgids_sort_cmp_data = repo;
1292 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1294 for (i = repo->start; i < repo->end; i++)
1296 int j = pkgids[i - repo->start];
1298 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1300 swap_solvables(repo, repodata, i, j);
1307 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1308 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1310 perror("db->open var/lib/rpm/Name");
1313 if (db->get_byteswapped(db, &byteswapped))
1315 perror("db->get_byteswapped");
1318 if (db->cursor(db, NULL, &dbc, 0))
1320 perror("db->cursor");
1323 dbidp = (unsigned char *)&dbid;
1326 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1328 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1342 memcpy(dbidp, dp, 4);
1343 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1344 rpmids[nrpmids].dbid = dbid;
1345 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1346 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1347 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1358 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1361 dbidp = (unsigned char *)&dbid;
1365 /* create hash from dbid to ref */
1366 refmask = mkmask(ref->nsolvables);
1367 refhash = sat_calloc(refmask + 1, sizeof(Id));
1368 for (i = 0; i < ref->nsolvables; i++)
1370 h = ref->rpmdbid[i] & refmask;
1372 h = (h + 317) & refmask;
1373 refhash[h] = i + 1; /* make it non-zero */
1376 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
1377 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1379 for (i = 0; i < nrpmids; i++, rp++, s++)
1382 repo->rpmdbid[i] = dbid;
1386 while ((id = refhash[h]))
1388 if (ref->rpmdbid[id - 1] == dbid)
1390 h = (h + 317) & refmask;
1394 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1395 solvable_copy(s, r, repodata);
1401 if (db_create(&db, 0, 0))
1403 perror("db_create");
1406 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1407 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1409 perror("db->open var/lib/rpm/Packages");
1412 if (db->get_byteswapped(db, &byteswapped))
1414 perror("db->get_byteswapped");
1426 memcpy(buf, dbidp, 4);
1431 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1434 fprintf(stderr, "corrupt rpm database\n");
1437 if (dbdata.size < 8)
1439 fprintf(stderr, "corrupt rpm database (size)\n");
1442 if (dbdata.size > rpmheadsize)
1443 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1444 memcpy(buf, dbdata.data, 8);
1445 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1446 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1447 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1449 fprintf(stderr, "corrupt rpm database (data size)\n");
1452 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1453 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1455 rpm2solv(pool, repo, repodata, s, rpmhead);
1462 for (i = 0; i < nrpmids; i++)
1463 sat_free(rpmids[i].name);
1471 dbenv->close(dbenv, 0);
1473 repodata_internalize(repodata);
1476 static inline unsigned int
1477 getu32(unsigned char *dp)
1479 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1483 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1485 Pool *pool = s->repo->pool;
1486 const char *name, *n1, *n2;
1489 repodata_extend(data, s - pool->solvables);
1491 /* skip ./ prefix */
1492 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1495 name = strrchr(location, '/');
1501 n2 = id2str(pool, s->arch);
1503 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1505 /* too bad, need to store directory */
1506 char *dir = strdup(location);
1507 dir[name - location - 1] = 0;
1508 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1512 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1515 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1518 if (*n2 || *n1 != '-')
1521 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1524 if (*n2 || *n1 != '.')
1527 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1530 if (*n2 || strcmp (n1, ".rpm"))
1532 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1536 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1541 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1543 int i, sigdsize, sigcnt, l;
1544 Pool *pool = repo->pool;
1547 RpmHead *rpmhead = 0;
1548 int rpmheadsize = 0;
1549 char *payloadformat;
1551 unsigned char lead[4096];
1552 int headerstart, headerend;
1557 repodata = repo_add_repodata(repo, 0);
1558 for (i = 0; i < nrpms; i++)
1560 if ((fp = fopen(rpms[i], "r")) == 0)
1565 if (fstat(fileno(fp), &stb))
1570 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1572 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1576 if (lead[78] != 0 || lead[79] != 5)
1578 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1582 if (getu32(lead + 96) != 0x8eade801)
1584 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1588 sigcnt = getu32(lead + 96 + 8);
1589 sigdsize = getu32(lead + 96 + 12);
1590 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1592 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1596 sigdsize += sigcnt * 16;
1597 sigdsize = (sigdsize + 7) & ~7;
1598 headerstart = 96 + 16 + sigdsize;
1601 l = sigdsize > 4096 ? 4096 : sigdsize;
1602 if (fread(lead, l, 1, fp) != 1)
1604 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1610 if (fread(lead, 16, 1, fp) != 1)
1612 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1616 if (getu32(lead) != 0x8eade801)
1618 fprintf(stderr, "%s: bad header\n", rpms[i]);
1622 sigcnt = getu32(lead + 8);
1623 sigdsize = getu32(lead + 12);
1624 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1626 fprintf(stderr, "%s: bad header\n", rpms[i]);
1630 l = sigdsize + sigcnt * 16;
1631 headerend = headerstart + 16 + l;
1632 if (l > rpmheadsize)
1633 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1634 if (fread(rpmhead->data, l, 1, fp) != 1)
1636 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1640 rpmhead->cnt = sigcnt;
1641 rpmhead->dcnt = sigdsize;
1642 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1643 if (headexists(rpmhead, TAG_PATCHESNAME))
1645 /* this is a patch rpm, ignore */
1649 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1650 if (payloadformat && !strcmp(payloadformat, "drpm"))
1652 /* this is a delta rpm */
1657 s = pool_id2solvable(pool, repo_add_solvable(repo));
1658 rpm2solv(pool, repo, repodata, s, rpmhead);
1661 Id handle = (s - pool->solvables) - repodata->start;
1662 handle = repodata_get_handle(repodata, handle);
1663 add_location(repodata, s, handle, rpms[i]);
1664 repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1665 repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);
1671 repodata_internalize(repodata);