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);
296 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
300 int i, cc, nc, vc, fc;
302 unsigned int olddeps;
305 n = headstringarray(rpmhead, tagn, &nc);
308 v = headstringarray(rpmhead, tagv, &vc);
314 f = headint32array(rpmhead, tagf, &fc);
321 if (nc != vc || nc != fc)
323 fprintf(stderr, "bad dependency entries\n");
331 for (i = 0; i < nc; i++)
332 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
335 if ((f[i] & DEP_PRE) != 0)
341 for (i = 0; i < nc; i++)
342 if ((f[i] & DEP_PRE) != 0)
348 if (tagn != TAG_REQUIRENAME)
358 olddeps = repo_reserve_ids(repo, 0, cc);
359 ida = repo->idarraydata + olddeps;
368 *ida++ = SOLVABLE_PREREQMARKER;
370 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
372 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
374 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
376 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
380 if ((f[i] & DEP_LESS) != 0)
382 if ((f[i] & DEP_EQUAL) != 0)
384 if ((f[i] & DEP_GREATER) != 0)
386 name = str2id(pool, n[i], 1);
387 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
388 evr = str2id(pool, v[i] + 2, 1);
390 evr = str2id(pool, v[i], 1);
391 *ida++ = rel2id(pool, name, evr, flags, 1);
394 *ida++ = str2id(pool, n[i], 1);
397 repo->idarraysize += cc + 1;
405 #define FILEFILTER_EXACT 0
406 #define FILEFILTER_STARTS 1
407 #define FILEFILTER_CONTAINS 2
415 static struct filefilter filefilters[] = {
416 { FILEFILTER_CONTAINS, "/bin/", 0},
417 { FILEFILTER_CONTAINS, "/sbin/", 0},
418 { FILEFILTER_CONTAINS, "/lib/", 0},
419 { FILEFILTER_CONTAINS, "/lib64/", 0},
420 { FILEFILTER_CONTAINS, "/etc/", 0},
421 { FILEFILTER_STARTS, "/usr/games/", 0},
422 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
423 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
424 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
428 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
432 unsigned int *fkb, *fn, *fsz, *fm, *fino;
433 unsigned int inotest[256], inotestok;
437 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
438 if (!fsz || fc != fszc)
443 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
444 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
445 if (!fm || fc != fszc)
451 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
452 if (!fino || fc != fszc)
460 if (fc < sizeof(inotest))
462 memset(inotest, 0, sizeof(inotest));
463 for (i = 0; i < fc; i++)
466 if (fsz[i] == 0 || !S_ISREG(fm[i]))
468 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
469 bit = 1 << (fino[i] & 31);
470 if ((inotest[off] & bit) != 0)
479 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
481 unsigned int mask, hash, hh;
482 if (!fdev || fc != fszc)
491 while ((mask & (mask - 1)) != 0)
492 mask = mask & (mask - 1);
494 if (mask > sizeof(inotest)/sizeof(*inotest))
495 fx = sat_calloc(mask, sizeof(unsigned int));
499 memset(fx, 0, mask * sizeof(unsigned int));
502 for (i = 0; i < fc; i++)
504 if (fsz[i] == 0 || !S_ISREG(fm[i]))
506 hash = (fino[i] + fdev[i] * 31) & mask;
508 while ((j = fx[hash]) != 0)
510 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
512 fsz[i] = 0; /* kill entry */
515 hash = (hash + hh++) & mask;
525 fn = sat_calloc(dic, sizeof(unsigned int));
526 fkb = sat_calloc(dic, sizeof(unsigned int));
527 for (i = 0; i < fc; i++)
529 if (fsz[i] == 0 || !S_ISREG(fm[i]))
534 fkb[di[i]] += fsz[i] / 1024 + 1;
539 repodata_extend(repodata, s - pool->solvables);
540 handle = (s - pool->solvables) - repodata->start;
541 handle = repodata_get_handle(repodata, handle);
542 for (i = 0; i < fc; i++)
546 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
547 did = repodata_str2dir(repodata, "/usr/src", 1);
549 did = repodata_str2dir(repodata, dn[i], 1);
550 repodata_add_dirnumnum(repodata, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
556 /* assumes last processed array is provides! */
558 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
565 struct filefilter *ff;
573 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
576 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
582 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
591 fprintf(stderr, "bad filelist\n");
596 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
598 for (i = 0; i < bnc; i++)
601 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
605 switch (ff->dirmatch)
607 case FILEFILTER_STARTS:
608 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
611 case FILEFILTER_CONTAINS:
612 if (!strstr(dn[di[i]], ff->dir))
615 case FILEFILTER_EXACT:
617 if (strcmp(dn[di[i]], ff->dir))
624 if (strcmp(bn[i], ff->base))
629 if (j == sizeof(filefilters)/sizeof(*filefilters))
632 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
636 fn = sat_realloc(fn, fna);
638 strcpy(fn, dn[di[i]]);
640 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
645 repodata_extend(repodata, s - pool->solvables);
646 handle = (s - pool->solvables) - repodata->start;
647 handle = repodata_get_handle(repodata, handle);
648 did = repodata_str2dir(repodata, dn[di[i]], 1);
649 repodata_add_dirstr(repodata, handle, SOLVABLE_FILELIST, did, bn[i]);
663 addsourcerpm(Pool *pool, Repodata *repodata, Id handle, char *sourcerpm, char *name, char *evr)
665 const char *p, *sevr, *sarch;
667 p = strrchr(sourcerpm, '.');
668 if (!p || strcmp(p, ".rpm") != 0)
671 while (p > sourcerpm && *p != '.')
673 if (*p != '.' || p == sourcerpm)
676 while (p > sourcerpm && *p != '-')
678 if (*p != '-' || p == sourcerpm)
681 while (p > sourcerpm && *p != '-')
683 if (*p != '-' || p == sourcerpm)
686 if (!strcmp(sarch, "src.rpm"))
687 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
688 else if (!strcmp(sarch, "nosrc.rpm"))
689 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
691 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
692 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
693 repodata_set_void(repodata, handle, SOLVABLE_SOURCEEVR);
695 repodata_set_id(repodata, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
696 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
697 repodata_set_void(repodata, handle, SOLVABLE_SOURCENAME);
699 repodata_set_id(repodata, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
703 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
709 name = headstring(rpmhead, TAG_NAME);
710 if (!strcmp(name, "gpg-pubkey"))
712 s->name = str2id(pool, name, 1);
715 fprintf(stderr, "package has no name\n");
718 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
720 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
723 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
724 s->arch = ARCH_NOSRC;
729 s->arch = ARCH_NOARCH;
730 evr = headtoevr(rpmhead);
731 s->evr = str2id(pool, evr, 1);
732 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
734 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
735 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
736 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
737 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
738 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
739 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
741 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
742 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
743 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
744 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
746 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
754 repodata_extend(repodata, s - pool->solvables);
755 handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
756 str = headstring(rpmhead, TAG_SUMMARY);
758 repodata_set_str(repodata, handle, SOLVABLE_SUMMARY, str);
759 str = headstring(rpmhead, TAG_DESCRIPTION);
763 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
764 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
768 /* oh my, found SUSE special author section */
773 while (l > 0 && str[l - 1] == '\n')
776 repodata_set_str(repodata, handle, SOLVABLE_DESCRIPTION, str);
778 aut = str; /* copy over */
779 while (*p == ' ' || *p == '\n')
792 while (aut != str && aut[-1] == '\n')
796 repodata_set_str(repodata, handle, SOLVABLE_AUTHORS, str);
800 repodata_set_str(repodata, handle, SOLVABLE_DESCRIPTION, str);
802 str = headstring(rpmhead, TAG_GROUP);
804 repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
805 str = headstring(rpmhead, TAG_LICENSE);
807 repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
808 u32 = headint32(rpmhead, TAG_BUILDTIME);
810 repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
811 u32 = headint32(rpmhead, TAG_INSTALLTIME);
813 repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
814 u32 = headint32(rpmhead, TAG_SIZE);
816 repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
818 addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
825 copyreldep(Pool *pool, Pool *frompool, Id id)
827 Reldep *rd = GETRELDEP(frompool, id);
828 Id name = rd->name, evr = rd->evr;
830 name = copyreldep(pool, frompool, name);
832 name = str2id(pool, id2str(frompool, name), 1);
834 evr = copyreldep(pool, frompool, evr);
836 evr = str2id(pool, id2str(frompool, evr), 1);
837 return rel2id(pool, name, evr, rd->flags, 1);
841 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
846 Pool *frompool = fromrepo->pool;
850 from = fromrepo->idarraydata + fromoff;
851 for (ida = from, cc = 0; *ida; ida++, cc++)
855 ido = repo_reserve_ids(repo, 0, cc);
856 ida = repo->idarraydata + ido;
857 if (frompool && pool != frompool)
863 id = copyreldep(pool, frompool, id);
865 id = str2id(pool, id2str(frompool, id), 1);
871 memcpy(ida, from, (cc + 1) * sizeof(Id));
872 repo->idarraysize += cc + 1;
877 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did)
879 Id parent = dirpool_parent(&fromdata->dirpool, did);
880 Id compid = dirpool_compid(&fromdata->dirpool, did);
882 parent = copydir(pool, data, fromspool, fromdata, parent);
883 if (fromspool != &pool->ss)
884 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
885 return dirpool_add_dir(&data->dirpool, parent, compid, 1);
888 struct solvable_copy_cbdata {
894 solvable_copy_cb(void *cbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
897 Repodata *data = ((struct solvable_copy_cbdata *)cbdata)->data;
898 Id handle = ((struct solvable_copy_cbdata *)cbdata)->handle;
899 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
900 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
903 if (keyname >= ID_NUM_INTERNAL)
904 keyname = str2id(pool, id2str(frompool, keyname), 1);
907 case REPOKEY_TYPE_ID:
908 case REPOKEY_TYPE_CONSTANTID:
910 assert(!data->localpool); /* implement me! */
911 if (pool != frompool || fromdata->localpool)
914 id = copyreldep(pool, frompool, id);
916 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
918 if (key->type == REPOKEY_TYPE_ID)
919 repodata_set_id(data, handle, keyname, id);
921 repodata_set_constantid(data, handle, keyname, id);
923 case REPOKEY_TYPE_STR:
924 repodata_set_str(data, handle, keyname, kv->str);
926 case REPOKEY_TYPE_VOID:
927 repodata_set_void(data, handle, keyname);
929 case REPOKEY_TYPE_NUM:
930 repodata_set_num(data, handle, keyname, kv->num);
932 case REPOKEY_TYPE_CONSTANT:
933 repodata_set_constant(data, handle, keyname, kv->num);
935 case REPOKEY_TYPE_DIRNUMNUMARRAY:
937 assert(!data->localpool); /* implement me! */
938 id = copydir(pool, data, fromspool, fromdata, id);
939 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
941 case REPOKEY_TYPE_DIRSTRARRAY:
943 assert(!data->localpool); /* implement me! */
944 id = copydir(pool, data, fromspool, fromdata, id);
945 repodata_add_dirstr(data, handle, keyname, id, kv->str);
954 solvable_copy(Solvable *s, Solvable *r, Repodata *data)
956 Repo *repo = s->repo;
957 Repo *fromrepo = r->repo;
958 Pool *pool = repo->pool;
959 struct solvable_copy_cbdata cbdata;
961 /* copy solvable data */
962 if (pool == fromrepo->pool)
967 s->vendor = r->vendor;
972 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
974 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
976 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
978 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
980 s->provides = copydeps(pool, repo, r->provides, fromrepo);
981 s->requires = copydeps(pool, repo, r->requires, fromrepo);
982 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
983 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
984 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
985 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
986 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
987 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
988 s->freshens = copydeps(pool, repo, r->freshens, fromrepo);
990 /* copy all attributes */
993 repodata_extend(data, s - pool->solvables);
995 cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
996 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
999 /* used to sort entries returned in some database order */
1001 rpmids_sort_cmp(const void *va, const void *vb)
1003 struct rpmid const *a = va, *b = vb;
1005 r = strcmp(a->name, b->name);
1008 return a->dbid - b->dbid;
1011 static Repo *pkgids_sort_cmp_data;
1014 pkgids_sort_cmp(const void *va, const void *vb)
1016 Pool *pool = pkgids_sort_cmp_data->pool;
1017 Solvable *a = pool->solvables + *(Id *)va;
1018 Solvable *b = pool->solvables + *(Id *)vb;
1021 if (a->name != b->name)
1022 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1023 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1024 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1028 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1030 Pool *pool = repo->pool;
1033 tmp = pool->solvables[pa];
1034 pool->solvables[pa] = pool->solvables[pb];
1035 pool->solvables[pb] = tmp;
1038 Id tmpid = repo->rpmdbid[pa - repo->start];
1039 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1040 repo->rpmdbid[pb - repo->start] = tmpid;
1042 /* only works if nothing is already internalized! */
1043 if (data && data->attrs)
1045 Id tmpattrs = data->attrs[pa - data->start];
1046 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1047 data->attrs[pb - data->start] = tmpattrs;
1052 * read rpm db as repo
1057 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
1059 Pool *pool = repo->pool;
1060 unsigned char buf[16];
1065 unsigned char *dp, *dbidp;
1067 struct rpmid *rpmids, *rp;
1073 unsigned int refmask, h;
1076 char dbpath[PATH_MAX];
1081 memset(&dbkey, 0, sizeof(dbkey));
1082 memset(&dbdata, 0, sizeof(dbdata));
1084 if (repo->start != repo->end)
1085 abort(); /* FIXME: rpmdbid */
1090 repodata = repo_add_repodata(repo, 0);
1092 if (ref && !(ref->nsolvables && ref->rpmdbid))
1095 if (db_env_create(&dbenv, 0))
1097 perror("db_env_create");
1100 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1101 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1103 perror("dbenv open");
1106 if (db_create(&db, dbenv, 0))
1108 perror("db_create");
1115 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1116 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1118 perror("db->open var/lib/rpm/Packages");
1121 if (db->get_byteswapped(db, &byteswapped))
1123 perror("db->get_byteswapped");
1126 if (db->cursor(db, NULL, &dbc, 0))
1128 perror("db->cursor");
1131 dbidp = (unsigned char *)&dbid;
1132 repo->rpmdbid = sat_calloc(256, sizeof(Id));
1138 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1141 s = pool_id2solvable(pool, repo_add_solvable(repo));
1144 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(Id));
1145 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
1148 if (dbkey.size != 4)
1150 fprintf(stderr, "corrupt Packages database (key size)\n");
1162 memcpy(dbidp, dp, 4);
1163 if (dbid == 0) /* the join key */
1165 if (dbdata.size < 8)
1167 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1170 if (dbdata.size > rpmheadsize)
1171 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1172 memcpy(buf, dbdata.data, 8);
1173 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1174 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1175 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1177 fprintf(stderr, "corrupt rpm database (data size)\n");
1180 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1181 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1182 repo->rpmdbid[i] = dbid;
1183 if (rpm2solv(pool, repo, repodata, s, rpmhead))
1190 /* We can reuse this solvable, but make sure it's still
1191 associated with this repo. */
1192 memset(s, 0, sizeof(*s));
1198 /* oops, could not reuse. free it instead */
1199 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1205 /* now sort all solvables */
1206 if (repo->end - repo->start > 1)
1208 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1209 for (i = repo->start; i < repo->end; i++)
1210 pkgids[i - repo->start] = i;
1211 pkgids_sort_cmp_data = repo;
1212 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1214 for (i = repo->start; i < repo->end; i++)
1216 int j = pkgids[i - repo->start];
1218 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1220 swap_solvables(repo, repodata, i, j);
1227 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1228 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1230 perror("db->open var/lib/rpm/Name");
1233 if (db->get_byteswapped(db, &byteswapped))
1235 perror("db->get_byteswapped");
1238 if (db->cursor(db, NULL, &dbc, 0))
1240 perror("db->cursor");
1243 dbidp = (unsigned char *)&dbid;
1246 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1248 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1262 memcpy(dbidp, dp, 4);
1263 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1264 rpmids[nrpmids].dbid = dbid;
1265 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1266 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1267 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1278 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1281 dbidp = (unsigned char *)&dbid;
1285 /* create hash from dbid to ref */
1286 refmask = mkmask(ref->nsolvables);
1287 refhash = sat_calloc(refmask + 1, sizeof(Id));
1288 for (i = 0; i < ref->nsolvables; i++)
1290 h = ref->rpmdbid[i] & refmask;
1292 h = (h + 317) & refmask;
1293 refhash[h] = i + 1; /* make it non-zero */
1296 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
1297 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1299 for (i = 0; i < nrpmids; i++, rp++, s++)
1302 repo->rpmdbid[i] = dbid;
1306 while ((id = refhash[h]))
1308 if (ref->rpmdbid[id - 1] == dbid)
1310 h = (h + 317) & refmask;
1314 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1315 solvable_copy(s, r, repodata);
1321 if (db_create(&db, 0, 0))
1323 perror("db_create");
1326 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1327 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1329 perror("db->open var/lib/rpm/Packages");
1332 if (db->get_byteswapped(db, &byteswapped))
1334 perror("db->get_byteswapped");
1346 memcpy(buf, dbidp, 4);
1351 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1354 fprintf(stderr, "corrupt rpm database\n");
1357 if (dbdata.size < 8)
1359 fprintf(stderr, "corrupt rpm database (size)\n");
1362 if (dbdata.size > rpmheadsize)
1363 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1364 memcpy(buf, dbdata.data, 8);
1365 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1366 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1367 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1369 fprintf(stderr, "corrupt rpm database (data size)\n");
1372 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1373 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1375 rpm2solv(pool, repo, repodata, s, rpmhead);
1382 for (i = 0; i < nrpmids; i++)
1383 sat_free(rpmids[i].name);
1391 dbenv->close(dbenv, 0);
1393 repodata_internalize(repodata);
1396 static inline unsigned int
1397 getu32(unsigned char *dp)
1399 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1403 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1405 Pool *pool = s->repo->pool;
1406 const char *name, *n1, *n2;
1409 repodata_extend(data, s - pool->solvables);
1411 /* skip ./ prefix */
1412 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1415 name = strrchr(location, '/');
1421 n2 = id2str(pool, s->arch);
1423 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1425 /* too bad, need to store directory */
1426 char *dir = strdup(location);
1427 dir[name - location - 1] = 0;
1428 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1432 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1435 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1438 if (*n2 || *n1 != '-')
1441 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1444 if (*n2 || *n1 != '.')
1447 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1450 if (*n2 || strcmp (n1, ".rpm"))
1452 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1456 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1461 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1463 int i, sigdsize, sigcnt, l;
1464 Pool *pool = repo->pool;
1467 RpmHead *rpmhead = 0;
1468 int rpmheadsize = 0;
1469 char *payloadformat;
1471 unsigned char lead[4096];
1472 int headerstart, headerend;
1477 repodata = repo_add_repodata(repo, 0);
1478 for (i = 0; i < nrpms; i++)
1480 if ((fp = fopen(rpms[i], "r")) == 0)
1485 if (fstat(fileno(fp), &stb))
1490 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1492 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1496 if (lead[78] != 0 || lead[79] != 5)
1498 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1502 if (getu32(lead + 96) != 0x8eade801)
1504 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1508 sigcnt = getu32(lead + 96 + 8);
1509 sigdsize = getu32(lead + 96 + 12);
1510 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1512 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1516 sigdsize += sigcnt * 16;
1517 sigdsize = (sigdsize + 7) & ~7;
1518 headerstart = 96 + 16 + sigdsize;
1521 l = sigdsize > 4096 ? 4096 : sigdsize;
1522 if (fread(lead, l, 1, fp) != 1)
1524 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1530 if (fread(lead, 16, 1, fp) != 1)
1532 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1536 if (getu32(lead) != 0x8eade801)
1538 fprintf(stderr, "%s: bad header\n", rpms[i]);
1542 sigcnt = getu32(lead + 8);
1543 sigdsize = getu32(lead + 12);
1544 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1546 fprintf(stderr, "%s: bad header\n", rpms[i]);
1550 l = sigdsize + sigcnt * 16;
1551 headerend = headerstart + 16 + l;
1552 if (l > rpmheadsize)
1553 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1554 if (fread(rpmhead->data, l, 1, fp) != 1)
1556 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1560 rpmhead->cnt = sigcnt;
1561 rpmhead->dcnt = sigdsize;
1562 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1563 if (headexists(rpmhead, TAG_PATCHESNAME))
1565 /* this is a patch rpm, ignore */
1569 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1570 if (payloadformat && !strcmp(payloadformat, "drpm"))
1572 /* this is a delta rpm */
1577 s = pool_id2solvable(pool, repo_add_solvable(repo));
1578 rpm2solv(pool, repo, repodata, s, rpmhead);
1581 Id handle = (s - pool->solvables) - repodata->start;
1582 handle = repodata_get_handle(repodata, handle);
1583 add_location(repodata, s, handle, rpms[i]);
1584 repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1585 repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);
1591 repodata_internalize(repodata);