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"
33 #define RPMDB_COOKIE_VERSION 2
36 #define TAG_VERSION 1001
37 #define TAG_RELEASE 1002
38 #define TAG_EPOCH 1003
39 #define TAG_SUMMARY 1004
40 #define TAG_DESCRIPTION 1005
41 #define TAG_BUILDTIME 1006
42 #define TAG_BUILDHOST 1007
43 #define TAG_INSTALLTIME 1008
45 #define TAG_DISTRIBUTION 1010
46 #define TAG_VENDOR 1011
47 #define TAG_LICENSE 1014
48 #define TAG_PACKAGER 1015
49 #define TAG_GROUP 1016
52 #define TAG_FILESIZES 1028
53 #define TAG_FILEMODES 1030
54 #define TAG_SOURCERPM 1044
55 #define TAG_PROVIDENAME 1047
56 #define TAG_REQUIREFLAGS 1048
57 #define TAG_REQUIRENAME 1049
58 #define TAG_REQUIREVERSION 1050
59 #define TAG_NOSOURCE 1051
60 #define TAG_NOPATCH 1052
61 #define TAG_CONFLICTFLAGS 1053
62 #define TAG_CONFLICTNAME 1054
63 #define TAG_CONFLICTVERSION 1055
64 #define TAG_OBSOLETENAME 1090
65 #define TAG_FILEDEVICES 1095
66 #define TAG_FILEINODES 1096
67 #define TAG_PROVIDEFLAGS 1112
68 #define TAG_PROVIDEVERSION 1113
69 #define TAG_OBSOLETEFLAGS 1114
70 #define TAG_OBSOLETEVERSION 1115
71 #define TAG_DIRINDEXES 1116
72 #define TAG_BASENAMES 1117
73 #define TAG_DIRNAMES 1118
74 #define TAG_PAYLOADFORMAT 1124
75 #define TAG_PATCHESNAME 1133
76 #define TAG_SUGGESTSNAME 1156
77 #define TAG_SUGGESTSVERSION 1157
78 #define TAG_SUGGESTSFLAGS 1158
79 #define TAG_ENHANCESNAME 1159
80 #define TAG_ENHANCESVERSION 1160
81 #define TAG_ENHANCESFLAGS 1161
83 #define DEP_LESS (1 << 1)
84 #define DEP_GREATER (1 << 2)
85 #define DEP_EQUAL (1 << 3)
86 #define DEP_STRONG (1 << 27)
87 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
95 typedef struct rpmhead {
99 unsigned char data[1];
103 headexists(RpmHead *h, int tag)
106 unsigned char *d, taga[4];
113 for (i = 0; i < h->cnt; i++, d -= 16)
114 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
119 static unsigned int *
120 headint32array(RpmHead *h, int tag, int *cnt)
122 unsigned int i, o, *r;
123 unsigned char *d, taga[4];
130 for (i = 0; i < h->cnt; i++, d -= 16)
131 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
135 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
137 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
138 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
139 if (o + 4 * i > h->dcnt)
142 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
145 for (o = 0; o < i; o++, d += 4)
146 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
151 headint32(RpmHead *h, int tag)
154 unsigned char *d, taga[4];
161 for (i = 0; i < h->cnt; i++, d -= 16)
162 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
166 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
168 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
169 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
170 if (i == 0 || o + 4 * i > h->dcnt)
173 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
176 static unsigned int *
177 headint16array(RpmHead *h, int tag, int *cnt)
179 unsigned int i, o, *r;
180 unsigned char *d, taga[4];
187 for (i = 0; i < h->cnt; i++, d -= 16)
188 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
192 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
194 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
195 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
196 if (o + 4 * i > h->dcnt)
199 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
202 for (o = 0; o < i; o++, d += 2)
203 r[o] = d[0] << 8 | d[1];
208 headstring(RpmHead *h, int tag)
211 unsigned char *d, taga[4];
217 for (i = 0; i < h->cnt; i++, d -= 16)
218 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
222 /* 6: STRING, 9: I18NSTRING */
223 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
225 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
226 return (char *)h->dp + o;
230 headstringarray(RpmHead *h, int tag, int *cnt)
233 unsigned char *d, taga[4];
241 for (i = 0; i < h->cnt; i++, d -= 16)
242 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
246 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
248 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
249 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
250 r = sat_calloc(i ? i : 1, sizeof(char *));
254 for (o = 0; o < i; o++)
258 d += strlen((char *)d) + 1;
259 if (d >= h->dp + h->dcnt)
268 static char *headtoevr(RpmHead *h)
275 version = headstring(h, TAG_VERSION);
276 release = headstring(h, TAG_RELEASE);
277 epoch = headint32(h, TAG_EPOCH);
278 if (!version || !release)
280 fprintf(stderr, "headtoevr: bad rpm header\n");
283 for (v = version; *v >= 0 && *v <= '9'; v++)
285 if (epoch || (v != version && *v == ':'))
287 char epochbuf[11]; /* 32bit decimal will fit in */
288 sprintf(epochbuf, "%u", epoch);
289 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
290 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
294 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
295 sprintf(evr, "%s-%s", version, release);
302 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
304 const unsigned char *cp;
309 /* check if it's already utf8, code taken from screen ;-) */
310 cp = (const unsigned char *)str;
311 while ((c = *cp++) != 0)
315 if ((c & 0xc0) != 0x80)
316 break; /* encoding error */
317 c = (c & 0x3f) | (state << 6);
318 if (!(state & 0x40000000))
320 /* check for overlong sequences */
321 if ((c & 0x820823e0) == 0x80000000)
323 else if ((c & 0x020821f0) == 0x02000000)
325 else if ((c & 0x000820f8) == 0x00080000)
327 else if ((c & 0x0000207c) == 0x00002000)
337 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
339 c = (c & 0x03) | 0xbfffff00; /* 4 */
341 c = (c & 0x07) | 0xbfffc000; /* 3 */
343 c = (c & 0x0f) | 0xbff00000; /* 2 */
345 c = (c & 0x1f) | 0xfc000000; /* 1 */
347 c = 0xfdffffff; /* overlong */
351 state = (c & 0x80000000) ? c : 0;
355 /* not utf8, assume latin1 */
356 buf = sat_malloc(2 * strlen(str) + 1);
357 cp = (const unsigned char *)str;
360 while ((c = *cp++) != 0)
373 repodata_set_str(repodata, handle, tag, str);
379 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
383 int i, cc, nc, vc, fc;
385 unsigned int olddeps;
388 n = headstringarray(rpmhead, tagn, &nc);
391 v = headstringarray(rpmhead, tagv, &vc);
397 f = headint32array(rpmhead, tagf, &fc);
404 if (nc != vc || nc != fc)
406 fprintf(stderr, "bad dependency entries\n");
414 for (i = 0; i < nc; i++)
415 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
418 if ((f[i] & DEP_PRE) != 0)
424 for (i = 0; i < nc; i++)
425 if ((f[i] & DEP_PRE) != 0)
431 if (tagn != TAG_REQUIRENAME)
441 olddeps = repo_reserve_ids(repo, 0, cc);
442 ida = repo->idarraydata + olddeps;
451 *ida++ = SOLVABLE_PREREQMARKER;
453 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
455 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
457 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
459 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
463 if ((f[i] & DEP_LESS) != 0)
465 if ((f[i] & DEP_EQUAL) != 0)
467 if ((f[i] & DEP_GREATER) != 0)
469 name = str2id(pool, n[i], 1);
470 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
471 evr = str2id(pool, v[i] + 2, 1);
473 evr = str2id(pool, v[i], 1);
474 *ida++ = rel2id(pool, name, evr, flags, 1);
477 *ida++ = str2id(pool, n[i], 1);
480 repo->idarraysize += cc + 1;
488 #ifdef USE_FILEFILTER
490 #define FILEFILTER_EXACT 0
491 #define FILEFILTER_STARTS 1
492 #define FILEFILTER_CONTAINS 2
500 static struct filefilter filefilters[] = {
501 { FILEFILTER_CONTAINS, "/bin/", 0},
502 { FILEFILTER_CONTAINS, "/sbin/", 0},
503 { FILEFILTER_CONTAINS, "/lib/", 0},
504 { FILEFILTER_CONTAINS, "/lib64/", 0},
505 { FILEFILTER_CONTAINS, "/etc/", 0},
506 { FILEFILTER_STARTS, "/usr/games/", 0},
507 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
508 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
509 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
515 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
519 unsigned int *fkb, *fn, *fsz, *fm, *fino;
520 unsigned int inotest[256], inotestok;
524 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
525 if (!fsz || fc != fszc)
530 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
531 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
532 if (!fm || fc != fszc)
538 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
539 if (!fino || fc != fszc)
547 if (fc < sizeof(inotest))
549 memset(inotest, 0, sizeof(inotest));
550 for (i = 0; i < fc; i++)
553 if (fsz[i] == 0 || !S_ISREG(fm[i]))
555 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
556 bit = 1 << (fino[i] & 31);
557 if ((inotest[off] & bit) != 0)
566 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
568 unsigned int mask, hash, hh;
569 if (!fdev || fc != fszc)
578 while ((mask & (mask - 1)) != 0)
579 mask = mask & (mask - 1);
581 if (mask > sizeof(inotest)/sizeof(*inotest))
582 fx = sat_calloc(mask, sizeof(unsigned int));
586 memset(fx, 0, mask * sizeof(unsigned int));
589 for (i = 0; i < fc; i++)
591 if (fsz[i] == 0 || !S_ISREG(fm[i]))
593 hash = (fino[i] + fdev[i] * 31) & mask;
595 while ((j = fx[hash]) != 0)
597 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
599 fsz[i] = 0; /* kill entry */
602 hash = (hash + hh++) & mask;
612 fn = sat_calloc(dic, sizeof(unsigned int));
613 fkb = sat_calloc(dic, sizeof(unsigned int));
614 for (i = 0; i < fc; i++)
616 if (fsz[i] == 0 || !S_ISREG(fm[i]))
621 fkb[di[i]] += fsz[i] / 1024 + 1;
626 repodata_extend(repodata, s - pool->solvables);
627 handle = (s - pool->solvables) - repodata->start;
628 handle = repodata_get_handle(repodata, handle);
629 for (i = 0; i < fc; i++)
633 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
634 did = repodata_str2dir(repodata, "/usr/src", 1);
636 did = repodata_str2dir(repodata, dn[i], 1);
637 repodata_add_dirnumnum(repodata, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
643 /* assumes last processed array is provides! */
645 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
652 #ifdef USE_FILEFILTER
654 struct filefilter *ff;
663 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
666 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
672 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
681 fprintf(stderr, "bad filelist\n");
686 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
688 for (i = 0; i < bnc; i++)
690 #ifdef USE_FILEFILTER
692 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
696 switch (ff->dirmatch)
698 case FILEFILTER_STARTS:
699 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
702 case FILEFILTER_CONTAINS:
703 if (!strstr(dn[di[i]], ff->dir))
706 case FILEFILTER_EXACT:
708 if (strcmp(dn[di[i]], ff->dir))
715 if (strcmp(bn[i], ff->base))
720 if (j == sizeof(filefilters)/sizeof(*filefilters))
724 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
728 fn = sat_realloc(fn, fna);
730 strcpy(fn, dn[di[i]]);
732 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
737 repodata_extend(repodata, s - pool->solvables);
738 handle = (s - pool->solvables) - repodata->start;
739 handle = repodata_get_handle(repodata, handle);
740 did = repodata_str2dir(repodata, dn[di[i]], 1);
742 did = repodata_str2dir(repodata, "/", 1);
743 repodata_add_dirstr(repodata, handle, SOLVABLE_FILELIST, did, bn[i]);
757 addsourcerpm(Pool *pool, Repodata *repodata, Id handle, char *sourcerpm, char *name, char *evr)
759 const char *p, *sevr, *sarch;
761 p = strrchr(sourcerpm, '.');
762 if (!p || strcmp(p, ".rpm") != 0)
765 while (p > sourcerpm && *p != '.')
767 if (*p != '.' || p == sourcerpm)
770 while (p > sourcerpm && *p != '-')
772 if (*p != '-' || p == sourcerpm)
775 while (p > sourcerpm && *p != '-')
777 if (*p != '-' || p == sourcerpm)
780 if (!strcmp(sarch, "src.rpm"))
781 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
782 else if (!strcmp(sarch, "nosrc.rpm"))
783 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
785 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
786 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
787 repodata_set_void(repodata, handle, SOLVABLE_SOURCEEVR);
789 repodata_set_id(repodata, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
790 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
791 repodata_set_void(repodata, handle, SOLVABLE_SOURCENAME);
793 repodata_set_id(repodata, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
797 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
803 name = headstring(rpmhead, TAG_NAME);
804 if (!strcmp(name, "gpg-pubkey"))
806 s->name = str2id(pool, name, 1);
809 fprintf(stderr, "package has no name\n");
812 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
814 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
817 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
818 s->arch = ARCH_NOSRC;
823 s->arch = ARCH_NOARCH;
824 evr = headtoevr(rpmhead);
825 s->evr = str2id(pool, evr, 1);
826 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
828 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
829 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
830 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
831 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
832 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
833 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
834 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
836 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
837 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
838 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
839 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
840 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
841 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
849 repodata_extend(repodata, s - pool->solvables);
850 handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
851 str = headstring(rpmhead, TAG_SUMMARY);
853 setutf8string(repodata, handle, SOLVABLE_SUMMARY, str);
854 str = headstring(rpmhead, TAG_DESCRIPTION);
858 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
859 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
863 /* oh my, found SUSE special author section */
868 while (l > 0 && str[l - 1] == '\n')
871 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
873 aut = str; /* copy over */
874 while (*p == ' ' || *p == '\n')
887 while (aut != str && aut[-1] == '\n')
891 setutf8string(repodata, handle, SOLVABLE_AUTHORS, str);
895 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
897 str = headstring(rpmhead, TAG_GROUP);
899 repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
900 str = headstring(rpmhead, TAG_LICENSE);
902 repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
903 str = headstring(rpmhead, TAG_URL);
905 repodata_set_str(repodata, handle, SOLVABLE_URL, str);
906 str = headstring(rpmhead, TAG_DISTRIBUTION);
908 repodata_set_poolstr(repodata, handle, SOLVABLE_DISTRIBUTION, str);
909 str = headstring(rpmhead, TAG_PACKAGER);
911 repodata_set_poolstr(repodata, handle, SOLVABLE_PACKAGER, str);
912 u32 = headint32(rpmhead, TAG_BUILDTIME);
914 repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
915 u32 = headint32(rpmhead, TAG_INSTALLTIME);
917 repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
918 u32 = headint32(rpmhead, TAG_SIZE);
920 repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
922 addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
929 copyreldep(Pool *pool, Pool *frompool, Id id)
931 Reldep *rd = GETRELDEP(frompool, id);
932 Id name = rd->name, evr = rd->evr;
934 name = copyreldep(pool, frompool, name);
936 name = str2id(pool, id2str(frompool, name), 1);
938 evr = copyreldep(pool, frompool, evr);
940 evr = str2id(pool, id2str(frompool, evr), 1);
941 return rel2id(pool, name, evr, rd->flags, 1);
945 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
950 Pool *frompool = fromrepo->pool;
954 from = fromrepo->idarraydata + fromoff;
955 for (ida = from, cc = 0; *ida; ida++, cc++)
959 ido = repo_reserve_ids(repo, 0, cc);
960 ida = repo->idarraydata + ido;
961 if (frompool && pool != frompool)
967 id = copyreldep(pool, frompool, id);
969 id = str2id(pool, id2str(frompool, id), 1);
975 memcpy(ida, from, (cc + 1) * sizeof(Id));
976 repo->idarraysize += cc + 1;
980 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
983 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
985 if (cache && cache[did & 255] == did)
986 return cache[(did & 255) + 256];
987 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
991 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
993 Id parent = dirpool_parent(&fromdata->dirpool, did);
994 Id compid = dirpool_compid(&fromdata->dirpool, did);
996 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
997 if (fromspool != &pool->ss)
998 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
999 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1002 cache[did & 255] = did;
1003 cache[(did & 255) + 256] = compid;
1008 struct solvable_copy_cbdata {
1015 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1017 struct solvable_copy_cbdata *cbdata = vcbdata;
1019 Repodata *data = cbdata->data;
1020 Id handle = cbdata->handle;
1021 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1022 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1024 keyname = key->name;
1025 if (keyname >= ID_NUM_INTERNAL)
1026 keyname = str2id(pool, id2str(frompool, keyname), 1);
1029 case REPOKEY_TYPE_ID:
1030 case REPOKEY_TYPE_CONSTANTID:
1032 assert(!data->localpool); /* implement me! */
1033 if (pool != frompool || fromdata->localpool)
1036 id = copyreldep(pool, frompool, id);
1038 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1040 if (key->type == REPOKEY_TYPE_ID)
1041 repodata_set_id(data, handle, keyname, id);
1043 repodata_set_constantid(data, handle, keyname, id);
1045 case REPOKEY_TYPE_STR:
1046 repodata_set_str(data, handle, keyname, kv->str);
1048 case REPOKEY_TYPE_VOID:
1049 repodata_set_void(data, handle, keyname);
1051 case REPOKEY_TYPE_NUM:
1052 repodata_set_num(data, handle, keyname, kv->num);
1054 case REPOKEY_TYPE_CONSTANT:
1055 repodata_set_constant(data, handle, keyname, kv->num);
1057 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1059 assert(!data->localpool); /* implement me! */
1060 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1061 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1063 case REPOKEY_TYPE_DIRSTRARRAY:
1065 assert(!data->localpool); /* implement me! */
1066 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1067 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1076 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1078 Repo *repo = s->repo;
1079 Repo *fromrepo = r->repo;
1080 Pool *pool = repo->pool;
1081 struct solvable_copy_cbdata cbdata;
1083 /* copy solvable data */
1084 if (pool == fromrepo->pool)
1089 s->vendor = r->vendor;
1094 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1096 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1098 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1100 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1102 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1103 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1104 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1105 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1106 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1107 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1108 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1109 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1111 /* copy all attributes */
1114 repodata_extend(data, s - pool->solvables);
1116 cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
1117 cbdata.dircache = dircache;
1118 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1121 /* used to sort entries returned in some database order */
1123 rpmids_sort_cmp(const void *va, const void *vb)
1125 struct rpmid const *a = va, *b = vb;
1127 r = strcmp(a->name, b->name);
1130 return a->dbid - b->dbid;
1133 static Repo *pkgids_sort_cmp_data;
1136 pkgids_sort_cmp(const void *va, const void *vb)
1138 Pool *pool = pkgids_sort_cmp_data->pool;
1139 Solvable *a = pool->solvables + *(Id *)va;
1140 Solvable *b = pool->solvables + *(Id *)vb;
1143 if (a->name != b->name)
1144 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1145 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1146 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1150 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1152 Pool *pool = repo->pool;
1155 tmp = pool->solvables[pa];
1156 pool->solvables[pa] = pool->solvables[pb];
1157 pool->solvables[pb] = tmp;
1160 Id tmpid = repo->rpmdbid[pa - repo->start];
1161 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1162 repo->rpmdbid[pb - repo->start] = tmpid;
1164 /* only works if nothing is already internalized! */
1165 if (data && data->attrs)
1167 Id tmpattrs = data->attrs[pa - data->start];
1168 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1169 data->attrs[pb - data->start] = tmpattrs;
1174 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1176 memset(cookie, 0, 32);
1177 cookie[3] = RPMDB_COOKIE_VERSION;
1178 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1179 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1183 * read rpm db as repo
1188 repo_add_rpmdb(Repo *repo, Repodata *repodata, Repo *ref, const char *rootdir)
1190 Pool *pool = repo->pool;
1191 unsigned char buf[16];
1196 unsigned char *dp, *dbidp;
1198 struct rpmid *rpmids, *rp;
1204 unsigned int refmask, h;
1205 char dbpath[PATH_MAX];
1209 struct stat packagesstat;
1210 int repodata_self = 0;
1212 memset(&dbkey, 0, sizeof(dbkey));
1213 memset(&dbdata, 0, sizeof(dbdata));
1220 repodata = repo_add_repodata(repo, 0);
1224 if (ref && !(ref->nsolvables && ref->rpmdbid))
1227 if (db_env_create(&dbenv, 0))
1229 perror("db_env_create");
1232 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1233 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1235 perror("dbenv open");
1238 if (db_create(&db, dbenv, 0))
1240 perror("db_create");
1244 /* XXX: should get ro lock of Packages database! */
1245 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1246 if (stat(dbpath, &packagesstat))
1251 mkrpmdbcookie(&packagesstat, repo->rpmdbcookie);
1253 if (!ref || memcmp(repo->rpmdbcookie, ref->rpmdbcookie, 32) != 0)
1256 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1258 perror("db->open var/lib/rpm/Packages");
1261 if (db->get_byteswapped(db, &byteswapped))
1263 perror("db->get_byteswapped");
1266 if (db->cursor(db, NULL, &dbc, 0))
1268 perror("db->cursor");
1271 dbidp = (unsigned char *)&dbid;
1276 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1279 s = pool_id2solvable(pool, repo_add_solvable(repo));
1281 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1282 if (dbkey.size != 4)
1284 fprintf(stderr, "corrupt Packages database (key size)\n");
1296 memcpy(dbidp, dp, 4);
1297 if (dbid == 0) /* the join key */
1299 if (dbdata.size < 8)
1301 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1304 if (dbdata.size > rpmheadsize)
1305 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1306 memcpy(buf, dbdata.data, 8);
1307 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1308 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1309 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1311 fprintf(stderr, "corrupt rpm database (data size)\n");
1314 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1315 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1316 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1317 if (rpm2solv(pool, repo, repodata, s, rpmhead))
1324 /* We can reuse this solvable, but make sure it's still
1325 associated with this repo. */
1326 memset(s, 0, sizeof(*s));
1332 /* oops, could not reuse. free it instead */
1333 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1339 /* now sort all solvables */
1340 if (repo->end - repo->start > 1)
1342 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1343 for (i = repo->start; i < repo->end; i++)
1344 pkgids[i - repo->start] = i;
1345 pkgids_sort_cmp_data = repo;
1346 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1348 for (i = repo->start; i < repo->end; i++)
1350 int j = pkgids[i - repo->start];
1352 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1354 swap_solvables(repo, repodata, i, j);
1363 memset(dircache, 0, sizeof(dircache));
1364 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1365 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1367 perror("db->open var/lib/rpm/Name");
1370 if (db->get_byteswapped(db, &byteswapped))
1372 perror("db->get_byteswapped");
1375 if (db->cursor(db, NULL, &dbc, 0))
1377 perror("db->cursor");
1380 dbidp = (unsigned char *)&dbid;
1383 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1385 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1399 memcpy(dbidp, dp, 4);
1400 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1401 rpmids[nrpmids].dbid = dbid;
1402 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1403 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1404 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1415 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1418 dbidp = (unsigned char *)&dbid;
1422 /* create hash from dbid to ref */
1423 refmask = mkmask(ref->nsolvables);
1424 refhash = sat_calloc(refmask + 1, sizeof(Id));
1425 for (i = 0; i < ref->end - ref->start; i++)
1427 if (!ref->rpmdbid[i])
1429 h = ref->rpmdbid[i] & refmask;
1431 h = (h + 317) & refmask;
1432 refhash[h] = i + 1; /* make it non-zero */
1435 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1437 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1439 for (i = 0; i < nrpmids; i++, rp++, s++)
1442 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1446 while ((id = refhash[h]))
1448 if (ref->rpmdbid[id - 1] == dbid)
1450 h = (h + 317) & refmask;
1454 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1457 solvable_copy(s, r, repodata, dircache);
1464 if (db_create(&db, 0, 0))
1466 perror("db_create");
1469 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1470 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1472 perror("db->open var/lib/rpm/Packages");
1475 if (db->get_byteswapped(db, &byteswapped))
1477 perror("db->get_byteswapped");
1489 memcpy(buf, dbidp, 4);
1494 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1497 fprintf(stderr, "corrupt rpm database\n");
1500 if (dbdata.size < 8)
1502 fprintf(stderr, "corrupt rpm database (size)\n");
1505 if (dbdata.size > rpmheadsize)
1506 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1507 memcpy(buf, dbdata.data, 8);
1508 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1509 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1510 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1512 fprintf(stderr, "corrupt rpm database (data size)\n");
1515 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1516 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1518 rpm2solv(pool, repo, repodata, s, rpmhead);
1525 for (i = 0; i < nrpmids; i++)
1526 sat_free(rpmids[i].name);
1530 if (repodata && repodata_self)
1531 repodata_internalize(repodata);
1536 dbenv->close(dbenv, 0);
1540 static inline unsigned int
1541 getu32(unsigned char *dp)
1543 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1548 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1550 Pool *pool = s->repo->pool;
1551 const char *name, *n1, *n2;
1554 repodata_extend(data, s - pool->solvables);
1556 /* skip ./ prefix */
1557 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1560 name = strrchr(location, '/');
1566 n2 = id2str(pool, s->arch);
1568 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1570 /* too bad, need to store directory */
1571 char *dir = strdup(location);
1572 dir[name - location - 1] = 0;
1573 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1577 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1580 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1583 if (*n2 || *n1 != '-')
1586 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1589 if (*n2 || *n1 != '.')
1592 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1595 if (*n2 || strcmp (n1, ".rpm"))
1597 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1601 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1606 repo_add_rpms(Repo *repo, Repodata *repodata, const char **rpms, int nrpms)
1608 int i, sigdsize, sigcnt, l;
1609 Pool *pool = repo->pool;
1611 RpmHead *rpmhead = 0;
1612 int rpmheadsize = 0;
1613 char *payloadformat;
1615 unsigned char lead[4096];
1616 int headerstart, headerend;
1622 for (i = 0; i < nrpms; i++)
1624 if ((fp = fopen(rpms[i], "r")) == 0)
1629 if (fstat(fileno(fp), &stb))
1634 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1636 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1640 if (lead[78] != 0 || lead[79] != 5)
1642 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1646 if (getu32(lead + 96) != 0x8eade801)
1648 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1652 sigcnt = getu32(lead + 96 + 8);
1653 sigdsize = getu32(lead + 96 + 12);
1654 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1656 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1660 sigdsize += sigcnt * 16;
1661 sigdsize = (sigdsize + 7) & ~7;
1662 headerstart = 96 + 16 + sigdsize;
1665 l = sigdsize > 4096 ? 4096 : sigdsize;
1666 if (fread(lead, l, 1, fp) != 1)
1668 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1674 if (fread(lead, 16, 1, fp) != 1)
1676 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1680 if (getu32(lead) != 0x8eade801)
1682 fprintf(stderr, "%s: bad header\n", rpms[i]);
1686 sigcnt = getu32(lead + 8);
1687 sigdsize = getu32(lead + 12);
1688 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1690 fprintf(stderr, "%s: bad header\n", rpms[i]);
1694 l = sigdsize + sigcnt * 16;
1695 headerend = headerstart + 16 + l;
1696 if (l > rpmheadsize)
1697 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1698 if (fread(rpmhead->data, l, 1, fp) != 1)
1700 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1704 rpmhead->cnt = sigcnt;
1705 rpmhead->dcnt = sigdsize;
1706 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1707 if (headexists(rpmhead, TAG_PATCHESNAME))
1709 /* this is a patch rpm, ignore */
1713 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1714 if (payloadformat && !strcmp(payloadformat, "drpm"))
1716 /* this is a delta rpm */
1721 s = pool_id2solvable(pool, repo_add_solvable(repo));
1722 rpm2solv(pool, repo, repodata, s, rpmhead);
1725 Id handle = (s - pool->solvables) - repodata->start;
1726 handle = repodata_get_handle(repodata, handle);
1727 add_location(repodata, s, handle, rpms[i]);
1728 repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1729 repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);