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_legacy(repo, s->provides, s->supplements, 0);
848 repodata_extend(repodata, s - pool->solvables);
849 handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
850 str = headstring(rpmhead, TAG_SUMMARY);
852 setutf8string(repodata, handle, SOLVABLE_SUMMARY, str);
853 str = headstring(rpmhead, TAG_DESCRIPTION);
857 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
858 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
862 /* oh my, found SUSE special author section */
867 while (l > 0 && str[l - 1] == '\n')
870 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
872 aut = str; /* copy over */
873 while (*p == ' ' || *p == '\n')
886 while (aut != str && aut[-1] == '\n')
890 setutf8string(repodata, handle, SOLVABLE_AUTHORS, str);
894 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
896 str = headstring(rpmhead, TAG_GROUP);
898 repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
899 str = headstring(rpmhead, TAG_LICENSE);
901 repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
902 str = headstring(rpmhead, TAG_URL);
904 repodata_set_str(repodata, handle, SOLVABLE_URL, str);
905 str = headstring(rpmhead, TAG_DISTRIBUTION);
907 repodata_set_poolstr(repodata, handle, SOLVABLE_DISTRIBUTION, str);
908 str = headstring(rpmhead, TAG_PACKAGER);
910 repodata_set_poolstr(repodata, handle, SOLVABLE_PACKAGER, str);
911 u32 = headint32(rpmhead, TAG_BUILDTIME);
913 repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
914 u32 = headint32(rpmhead, TAG_INSTALLTIME);
916 repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
917 u32 = headint32(rpmhead, TAG_SIZE);
919 repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
921 addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
928 copyreldep(Pool *pool, Pool *frompool, Id id)
930 Reldep *rd = GETRELDEP(frompool, id);
931 Id name = rd->name, evr = rd->evr;
933 name = copyreldep(pool, frompool, name);
935 name = str2id(pool, id2str(frompool, name), 1);
937 evr = copyreldep(pool, frompool, evr);
939 evr = str2id(pool, id2str(frompool, evr), 1);
940 return rel2id(pool, name, evr, rd->flags, 1);
944 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
949 Pool *frompool = fromrepo->pool;
953 from = fromrepo->idarraydata + fromoff;
954 for (ida = from, cc = 0; *ida; ida++, cc++)
958 ido = repo_reserve_ids(repo, 0, cc);
959 ida = repo->idarraydata + ido;
960 if (frompool && pool != frompool)
966 id = copyreldep(pool, frompool, id);
968 id = str2id(pool, id2str(frompool, id), 1);
974 memcpy(ida, from, (cc + 1) * sizeof(Id));
975 repo->idarraysize += cc + 1;
979 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
982 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
984 if (cache && cache[did & 255] == did)
985 return cache[(did & 255) + 256];
986 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
990 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
992 Id parent = dirpool_parent(&fromdata->dirpool, did);
993 Id compid = dirpool_compid(&fromdata->dirpool, did);
995 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
996 if (fromspool != &pool->ss)
997 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
998 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1001 cache[did & 255] = did;
1002 cache[(did & 255) + 256] = compid;
1007 struct solvable_copy_cbdata {
1014 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1016 struct solvable_copy_cbdata *cbdata = vcbdata;
1018 Repodata *data = cbdata->data;
1019 Id handle = cbdata->handle;
1020 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1021 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1023 keyname = key->name;
1024 if (keyname >= ID_NUM_INTERNAL)
1025 keyname = str2id(pool, id2str(frompool, keyname), 1);
1028 case REPOKEY_TYPE_ID:
1029 case REPOKEY_TYPE_CONSTANTID:
1031 assert(!data->localpool); /* implement me! */
1032 if (pool != frompool || fromdata->localpool)
1035 id = copyreldep(pool, frompool, id);
1037 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1039 if (key->type == REPOKEY_TYPE_ID)
1040 repodata_set_id(data, handle, keyname, id);
1042 repodata_set_constantid(data, handle, keyname, id);
1044 case REPOKEY_TYPE_STR:
1045 repodata_set_str(data, handle, keyname, kv->str);
1047 case REPOKEY_TYPE_VOID:
1048 repodata_set_void(data, handle, keyname);
1050 case REPOKEY_TYPE_NUM:
1051 repodata_set_num(data, handle, keyname, kv->num);
1053 case REPOKEY_TYPE_CONSTANT:
1054 repodata_set_constant(data, handle, keyname, kv->num);
1056 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1058 assert(!data->localpool); /* implement me! */
1059 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1060 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1062 case REPOKEY_TYPE_DIRSTRARRAY:
1064 assert(!data->localpool); /* implement me! */
1065 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1066 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1075 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1077 Repo *repo = s->repo;
1078 Repo *fromrepo = r->repo;
1079 Pool *pool = repo->pool;
1080 struct solvable_copy_cbdata cbdata;
1082 /* copy solvable data */
1083 if (pool == fromrepo->pool)
1088 s->vendor = r->vendor;
1093 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1095 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1097 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1099 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1101 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1102 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1103 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1104 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1105 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1106 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1107 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1108 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1110 /* copy all attributes */
1113 repodata_extend(data, s - pool->solvables);
1115 cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
1116 cbdata.dircache = dircache;
1117 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1120 /* used to sort entries returned in some database order */
1122 rpmids_sort_cmp(const void *va, const void *vb)
1124 struct rpmid const *a = va, *b = vb;
1126 r = strcmp(a->name, b->name);
1129 return a->dbid - b->dbid;
1132 static Repo *pkgids_sort_cmp_data;
1135 pkgids_sort_cmp(const void *va, const void *vb)
1137 Pool *pool = pkgids_sort_cmp_data->pool;
1138 Solvable *a = pool->solvables + *(Id *)va;
1139 Solvable *b = pool->solvables + *(Id *)vb;
1142 if (a->name != b->name)
1143 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1144 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1145 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1149 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1151 Pool *pool = repo->pool;
1154 tmp = pool->solvables[pa];
1155 pool->solvables[pa] = pool->solvables[pb];
1156 pool->solvables[pb] = tmp;
1159 Id tmpid = repo->rpmdbid[pa - repo->start];
1160 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1161 repo->rpmdbid[pb - repo->start] = tmpid;
1163 /* only works if nothing is already internalized! */
1164 if (data && data->attrs)
1166 Id tmpattrs = data->attrs[pa - data->start];
1167 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1168 data->attrs[pb - data->start] = tmpattrs;
1173 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1175 memset(cookie, 0, 32);
1176 cookie[3] = RPMDB_COOKIE_VERSION;
1177 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1178 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1182 * read rpm db as repo
1187 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
1189 Pool *pool = repo->pool;
1190 unsigned char buf[16];
1195 unsigned char *dp, *dbidp;
1197 struct rpmid *rpmids, *rp;
1203 unsigned int refmask, h;
1206 char dbpath[PATH_MAX];
1210 struct stat packagesstat;
1212 memset(&dbkey, 0, sizeof(dbkey));
1213 memset(&dbdata, 0, sizeof(dbdata));
1215 if (repo->start != repo->end)
1216 abort(); /* FIXME: rpmdbid */
1221 repodata = repo_add_repodata(repo, 0);
1223 if (ref && !(ref->nsolvables && ref->rpmdbid))
1226 if (db_env_create(&dbenv, 0))
1228 perror("db_env_create");
1231 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1232 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1234 perror("dbenv open");
1237 if (db_create(&db, dbenv, 0))
1239 perror("db_create");
1243 /* XXX: should get ro lock of Packages database! */
1244 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1245 if (stat(dbpath, &packagesstat))
1250 mkrpmdbcookie(&packagesstat, repo->rpmdbcookie);
1252 if (!ref || memcmp(repo->rpmdbcookie, ref->rpmdbcookie, 32) != 0)
1255 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1257 perror("db->open var/lib/rpm/Packages");
1260 if (db->get_byteswapped(db, &byteswapped))
1262 perror("db->get_byteswapped");
1265 if (db->cursor(db, NULL, &dbc, 0))
1267 perror("db->cursor");
1270 dbidp = (unsigned char *)&dbid;
1271 repo->rpmdbid = sat_calloc(256, sizeof(Id));
1277 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1280 s = pool_id2solvable(pool, repo_add_solvable(repo));
1283 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(Id));
1284 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
1287 if (dbkey.size != 4)
1289 fprintf(stderr, "corrupt Packages database (key size)\n");
1301 memcpy(dbidp, dp, 4);
1302 if (dbid == 0) /* the join key */
1304 if (dbdata.size < 8)
1306 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1309 if (dbdata.size > rpmheadsize)
1310 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1311 memcpy(buf, dbdata.data, 8);
1312 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1313 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1314 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1316 fprintf(stderr, "corrupt rpm database (data size)\n");
1319 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1320 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1321 repo->rpmdbid[i] = dbid;
1322 if (rpm2solv(pool, repo, repodata, s, rpmhead))
1329 /* We can reuse this solvable, but make sure it's still
1330 associated with this repo. */
1331 memset(s, 0, sizeof(*s));
1337 /* oops, could not reuse. free it instead */
1338 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1344 /* now sort all solvables */
1345 if (repo->end - repo->start > 1)
1347 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1348 for (i = repo->start; i < repo->end; i++)
1349 pkgids[i - repo->start] = i;
1350 pkgids_sort_cmp_data = repo;
1351 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1353 for (i = repo->start; i < repo->end; i++)
1355 int j = pkgids[i - repo->start];
1357 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1359 swap_solvables(repo, repodata, i, j);
1368 memset(dircache, 0, sizeof(dircache));
1369 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1370 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1372 perror("db->open var/lib/rpm/Name");
1375 if (db->get_byteswapped(db, &byteswapped))
1377 perror("db->get_byteswapped");
1380 if (db->cursor(db, NULL, &dbc, 0))
1382 perror("db->cursor");
1385 dbidp = (unsigned char *)&dbid;
1388 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1390 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1404 memcpy(dbidp, dp, 4);
1405 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1406 rpmids[nrpmids].dbid = dbid;
1407 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1408 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1409 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1420 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1423 dbidp = (unsigned char *)&dbid;
1427 /* create hash from dbid to ref */
1428 refmask = mkmask(ref->nsolvables);
1429 refhash = sat_calloc(refmask + 1, sizeof(Id));
1430 for (i = 0; i < ref->nsolvables; i++)
1432 h = ref->rpmdbid[i] & refmask;
1434 h = (h + 317) & refmask;
1435 refhash[h] = i + 1; /* make it non-zero */
1438 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
1439 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1441 for (i = 0; i < nrpmids; i++, rp++, s++)
1444 repo->rpmdbid[i] = dbid;
1448 while ((id = refhash[h]))
1450 if (ref->rpmdbid[id - 1] == dbid)
1452 h = (h + 317) & refmask;
1456 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1457 solvable_copy(s, r, repodata, dircache);
1463 if (db_create(&db, 0, 0))
1465 perror("db_create");
1468 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1469 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1471 perror("db->open var/lib/rpm/Packages");
1474 if (db->get_byteswapped(db, &byteswapped))
1476 perror("db->get_byteswapped");
1488 memcpy(buf, dbidp, 4);
1493 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1496 fprintf(stderr, "corrupt rpm database\n");
1499 if (dbdata.size < 8)
1501 fprintf(stderr, "corrupt rpm database (size)\n");
1504 if (dbdata.size > rpmheadsize)
1505 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1506 memcpy(buf, dbdata.data, 8);
1507 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1508 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1509 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1511 fprintf(stderr, "corrupt rpm database (data size)\n");
1514 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1515 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1517 rpm2solv(pool, repo, repodata, s, rpmhead);
1524 for (i = 0; i < nrpmids; i++)
1525 sat_free(rpmids[i].name);
1533 dbenv->close(dbenv, 0);
1535 repodata_internalize(repodata);
1538 static inline unsigned int
1539 getu32(unsigned char *dp)
1541 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1545 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1547 Pool *pool = s->repo->pool;
1548 const char *name, *n1, *n2;
1551 repodata_extend(data, s - pool->solvables);
1553 /* skip ./ prefix */
1554 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1557 name = strrchr(location, '/');
1563 n2 = id2str(pool, s->arch);
1565 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1567 /* too bad, need to store directory */
1568 char *dir = strdup(location);
1569 dir[name - location - 1] = 0;
1570 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1574 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1577 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1580 if (*n2 || *n1 != '-')
1583 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1586 if (*n2 || *n1 != '.')
1589 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1592 if (*n2 || strcmp (n1, ".rpm"))
1594 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1598 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1603 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1605 int i, sigdsize, sigcnt, l;
1606 Pool *pool = repo->pool;
1609 RpmHead *rpmhead = 0;
1610 int rpmheadsize = 0;
1611 char *payloadformat;
1613 unsigned char lead[4096];
1614 int headerstart, headerend;
1619 repodata = repo_add_repodata(repo, 0);
1620 for (i = 0; i < nrpms; i++)
1622 if ((fp = fopen(rpms[i], "r")) == 0)
1627 if (fstat(fileno(fp), &stb))
1632 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1634 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1638 if (lead[78] != 0 || lead[79] != 5)
1640 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1644 if (getu32(lead + 96) != 0x8eade801)
1646 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1650 sigcnt = getu32(lead + 96 + 8);
1651 sigdsize = getu32(lead + 96 + 12);
1652 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1654 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1658 sigdsize += sigcnt * 16;
1659 sigdsize = (sigdsize + 7) & ~7;
1660 headerstart = 96 + 16 + sigdsize;
1663 l = sigdsize > 4096 ? 4096 : sigdsize;
1664 if (fread(lead, l, 1, fp) != 1)
1666 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1672 if (fread(lead, 16, 1, fp) != 1)
1674 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1678 if (getu32(lead) != 0x8eade801)
1680 fprintf(stderr, "%s: bad header\n", rpms[i]);
1684 sigcnt = getu32(lead + 8);
1685 sigdsize = getu32(lead + 12);
1686 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1688 fprintf(stderr, "%s: bad header\n", rpms[i]);
1692 l = sigdsize + sigcnt * 16;
1693 headerend = headerstart + 16 + l;
1694 if (l > rpmheadsize)
1695 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1696 if (fread(rpmhead->data, l, 1, fp) != 1)
1698 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1702 rpmhead->cnt = sigcnt;
1703 rpmhead->dcnt = sigdsize;
1704 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1705 if (headexists(rpmhead, TAG_PATCHESNAME))
1707 /* this is a patch rpm, ignore */
1711 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1712 if (payloadformat && !strcmp(payloadformat, "drpm"))
1714 /* this is a delta rpm */
1719 s = pool_id2solvable(pool, repo_add_solvable(repo));
1720 rpm2solv(pool, repo, repodata, s, rpmhead);
1723 Id handle = (s - pool->solvables) - repodata->start;
1724 handle = repodata_get_handle(repodata, handle);
1725 add_location(repodata, s, handle, rpms[i]);
1726 repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1727 repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);
1733 repodata_internalize(repodata);