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_FILECOLORS 1140
77 #define TAG_SUGGESTSNAME 1156
78 #define TAG_SUGGESTSVERSION 1157
79 #define TAG_SUGGESTSFLAGS 1158
80 #define TAG_ENHANCESNAME 1159
81 #define TAG_ENHANCESVERSION 1160
82 #define TAG_ENHANCESFLAGS 1161
84 #define DEP_LESS (1 << 1)
85 #define DEP_GREATER (1 << 2)
86 #define DEP_EQUAL (1 << 3)
87 #define DEP_STRONG (1 << 27)
88 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
96 typedef struct rpmhead {
100 unsigned char data[1];
104 headexists(RpmHead *h, int tag)
107 unsigned char *d, taga[4];
114 for (i = 0; i < h->cnt; i++, d -= 16)
115 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
120 static unsigned int *
121 headint32array(RpmHead *h, int tag, int *cnt)
123 unsigned int i, o, *r;
124 unsigned char *d, taga[4];
131 for (i = 0; i < h->cnt; i++, d -= 16)
132 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
136 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
138 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
139 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
140 if (o + 4 * i > h->dcnt)
143 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
146 for (o = 0; o < i; o++, d += 4)
147 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
152 headint32(RpmHead *h, int tag)
155 unsigned char *d, taga[4];
162 for (i = 0; i < h->cnt; i++, d -= 16)
163 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
167 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
169 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
170 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
171 if (i == 0 || o + 4 * i > h->dcnt)
174 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
177 static unsigned int *
178 headint16array(RpmHead *h, int tag, int *cnt)
180 unsigned int i, o, *r;
181 unsigned char *d, taga[4];
188 for (i = 0; i < h->cnt; i++, d -= 16)
189 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
193 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
195 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
196 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
197 if (o + 4 * i > h->dcnt)
200 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
203 for (o = 0; o < i; o++, d += 2)
204 r[o] = d[0] << 8 | d[1];
209 headstring(RpmHead *h, int tag)
212 unsigned char *d, taga[4];
218 for (i = 0; i < h->cnt; i++, d -= 16)
219 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
223 /* 6: STRING, 9: I18NSTRING */
224 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
226 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
227 return (char *)h->dp + o;
231 headstringarray(RpmHead *h, int tag, int *cnt)
234 unsigned char *d, taga[4];
242 for (i = 0; i < h->cnt; i++, d -= 16)
243 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
247 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
249 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
250 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
251 r = sat_calloc(i ? i : 1, sizeof(char *));
255 for (o = 0; o < i; o++)
259 d += strlen((char *)d) + 1;
260 if (d >= h->dp + h->dcnt)
269 static char *headtoevr(RpmHead *h)
276 version = headstring(h, TAG_VERSION);
277 release = headstring(h, TAG_RELEASE);
278 epoch = headint32(h, TAG_EPOCH);
279 if (!version || !release)
281 fprintf(stderr, "headtoevr: bad rpm header\n");
284 for (v = version; *v >= 0 && *v <= '9'; v++)
286 if (epoch || (v != version && *v == ':'))
288 char epochbuf[11]; /* 32bit decimal will fit in */
289 sprintf(epochbuf, "%u", epoch);
290 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
291 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
295 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
296 sprintf(evr, "%s-%s", version, release);
303 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
305 const unsigned char *cp;
310 /* check if it's already utf8, code taken from screen ;-) */
311 cp = (const unsigned char *)str;
312 while ((c = *cp++) != 0)
316 if ((c & 0xc0) != 0x80)
317 break; /* encoding error */
318 c = (c & 0x3f) | (state << 6);
319 if (!(state & 0x40000000))
321 /* check for overlong sequences */
322 if ((c & 0x820823e0) == 0x80000000)
324 else if ((c & 0x020821f0) == 0x02000000)
326 else if ((c & 0x000820f8) == 0x00080000)
328 else if ((c & 0x0000207c) == 0x00002000)
338 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
340 c = (c & 0x03) | 0xbfffff00; /* 4 */
342 c = (c & 0x07) | 0xbfffc000; /* 3 */
344 c = (c & 0x0f) | 0xbff00000; /* 2 */
346 c = (c & 0x1f) | 0xfc000000; /* 1 */
348 c = 0xfdffffff; /* overlong */
352 state = (c & 0x80000000) ? c : 0;
356 /* not utf8, assume latin1 */
357 buf = sat_malloc(2 * strlen(str) + 1);
358 cp = (const unsigned char *)str;
361 while ((c = *cp++) != 0)
374 repodata_set_str(repodata, handle, tag, str);
380 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
384 int i, cc, nc, vc, fc;
386 unsigned int olddeps;
389 n = headstringarray(rpmhead, tagn, &nc);
392 v = headstringarray(rpmhead, tagv, &vc);
398 f = headint32array(rpmhead, tagf, &fc);
405 if (nc != vc || nc != fc)
407 fprintf(stderr, "bad dependency entries\n");
415 for (i = 0; i < nc; i++)
416 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
419 if ((f[i] & DEP_PRE) != 0)
425 for (i = 0; i < nc; i++)
426 if ((f[i] & DEP_PRE) != 0)
432 if (tagn != TAG_REQUIRENAME)
442 olddeps = repo_reserve_ids(repo, 0, cc);
443 ida = repo->idarraydata + olddeps;
452 *ida++ = SOLVABLE_PREREQMARKER;
454 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
456 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
458 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
460 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
464 if ((f[i] & DEP_LESS) != 0)
466 if ((f[i] & DEP_EQUAL) != 0)
468 if ((f[i] & DEP_GREATER) != 0)
470 name = str2id(pool, n[i], 1);
471 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
472 evr = str2id(pool, v[i] + 2, 1);
474 evr = str2id(pool, v[i], 1);
475 *ida++ = rel2id(pool, name, evr, flags, 1);
478 *ida++ = str2id(pool, n[i], 1);
481 repo->idarraysize += cc + 1;
489 #ifdef USE_FILEFILTER
491 #define FILEFILTER_EXACT 0
492 #define FILEFILTER_STARTS 1
493 #define FILEFILTER_CONTAINS 2
501 static struct filefilter filefilters[] = {
502 { FILEFILTER_CONTAINS, "/bin/", 0},
503 { FILEFILTER_CONTAINS, "/sbin/", 0},
504 { FILEFILTER_CONTAINS, "/lib/", 0},
505 { FILEFILTER_CONTAINS, "/lib64/", 0},
506 { FILEFILTER_CONTAINS, "/etc/", 0},
507 { FILEFILTER_STARTS, "/usr/games/", 0},
508 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
509 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
510 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
516 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
520 unsigned int *fkb, *fn, *fsz, *fm, *fino;
521 unsigned int inotest[256], inotestok;
525 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
526 if (!fsz || fc != fszc)
531 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
532 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
533 if (!fm || fc != fszc)
539 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
540 if (!fino || fc != fszc)
548 if (fc < sizeof(inotest))
550 memset(inotest, 0, sizeof(inotest));
551 for (i = 0; i < fc; i++)
554 if (fsz[i] == 0 || !S_ISREG(fm[i]))
556 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
557 bit = 1 << (fino[i] & 31);
558 if ((inotest[off] & bit) != 0)
567 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
569 unsigned int mask, hash, hh;
570 if (!fdev || fc != fszc)
579 while ((mask & (mask - 1)) != 0)
580 mask = mask & (mask - 1);
582 if (mask > sizeof(inotest)/sizeof(*inotest))
583 fx = sat_calloc(mask, sizeof(unsigned int));
587 memset(fx, 0, mask * sizeof(unsigned int));
590 for (i = 0; i < fc; i++)
592 if (fsz[i] == 0 || !S_ISREG(fm[i]))
594 hash = (fino[i] + fdev[i] * 31) & mask;
596 while ((j = fx[hash]) != 0)
598 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
600 fsz[i] = 0; /* kill entry */
603 hash = (hash + hh++) & mask;
613 fn = sat_calloc(dic, sizeof(unsigned int));
614 fkb = sat_calloc(dic, sizeof(unsigned int));
615 for (i = 0; i < fc; i++)
617 if (fsz[i] == 0 || !S_ISREG(fm[i]))
622 fkb[di[i]] += fsz[i] / 1024 + 1;
627 handle = s - pool->solvables;
628 for (i = 0; i < fc; i++)
632 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
633 did = repodata_str2dir(data, "/usr/src", 1);
635 did = repodata_str2dir(data, dn[i], 1);
636 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
642 /* assumes last processed array is provides! */
644 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
651 #ifdef USE_FILEFILTER
653 struct filefilter *ff;
662 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
665 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
671 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
680 fprintf(stderr, "bad filelist\n");
685 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
687 for (i = 0; i < bnc; i++)
689 #ifdef USE_FILEFILTER
691 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
695 switch (ff->dirmatch)
697 case FILEFILTER_STARTS:
698 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
701 case FILEFILTER_CONTAINS:
702 if (!strstr(dn[di[i]], ff->dir))
705 case FILEFILTER_EXACT:
707 if (strcmp(dn[di[i]], ff->dir))
714 if (strcmp(bn[i], ff->base))
719 if (j == sizeof(filefilters)/sizeof(*filefilters))
723 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
727 fn = sat_realloc(fn, fna);
729 strcpy(fn, dn[di[i]]);
731 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
736 handle = s - pool->solvables;
737 did = repodata_str2dir(data, dn[di[i]], 1);
739 did = repodata_str2dir(data, "/", 1);
740 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
754 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
756 const char *p, *sevr, *sarch;
758 p = strrchr(sourcerpm, '.');
759 if (!p || strcmp(p, ".rpm") != 0)
762 while (p > sourcerpm && *p != '.')
764 if (*p != '.' || p == sourcerpm)
767 while (p > sourcerpm && *p != '-')
769 if (*p != '-' || p == sourcerpm)
772 while (p > sourcerpm && *p != '-')
774 if (*p != '-' || p == sourcerpm)
777 if (!strcmp(sarch, "src.rpm"))
778 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
779 else if (!strcmp(sarch, "nosrc.rpm"))
780 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
782 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
783 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
784 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
786 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
787 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
788 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
790 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
794 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead)
800 name = headstring(rpmhead, TAG_NAME);
801 if (!strcmp(name, "gpg-pubkey"))
803 s->name = str2id(pool, name, 1);
806 fprintf(stderr, "package has no name\n");
809 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
811 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
814 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
815 s->arch = ARCH_NOSRC;
820 s->arch = ARCH_NOARCH;
821 evr = headtoevr(rpmhead);
822 s->evr = str2id(pool, evr, 1);
823 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
825 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
826 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
827 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
828 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
829 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
830 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
831 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
833 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
834 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
835 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
836 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
837 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
838 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
846 handle = s - pool->solvables;
847 str = headstring(rpmhead, TAG_SUMMARY);
849 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
850 str = headstring(rpmhead, TAG_DESCRIPTION);
854 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
855 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
859 /* oh my, found SUSE special author section */
864 while (l > 0 && str[l - 1] == '\n')
867 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
869 aut = str; /* copy over */
870 while (*p == ' ' || *p == '\n')
883 while (aut != str && aut[-1] == '\n')
887 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
891 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
893 str = headstring(rpmhead, TAG_GROUP);
895 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
896 str = headstring(rpmhead, TAG_LICENSE);
898 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
899 str = headstring(rpmhead, TAG_URL);
901 repodata_set_str(data, handle, SOLVABLE_URL, str);
902 str = headstring(rpmhead, TAG_DISTRIBUTION);
904 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
905 str = headstring(rpmhead, TAG_PACKAGER);
907 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
908 u32 = headint32(rpmhead, TAG_BUILDTIME);
910 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
911 u32 = headint32(rpmhead, TAG_INSTALLTIME);
913 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
914 u32 = headint32(rpmhead, TAG_SIZE);
916 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
918 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
925 copyreldep(Pool *pool, Pool *frompool, Id id)
927 Reldep *rd = GETRELDEP(frompool, id);
928 Id name = rd->name, evr = rd->evr;
930 name = copyreldep(pool, frompool, name);
932 name = str2id(pool, id2str(frompool, name), 1);
934 evr = copyreldep(pool, frompool, evr);
936 evr = str2id(pool, id2str(frompool, evr), 1);
937 return rel2id(pool, name, evr, rd->flags, 1);
941 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
946 Pool *frompool = fromrepo->pool;
950 from = fromrepo->idarraydata + fromoff;
951 for (ida = from, cc = 0; *ida; ida++, cc++)
955 ido = repo_reserve_ids(repo, 0, cc);
956 ida = repo->idarraydata + ido;
957 if (frompool && pool != frompool)
963 id = copyreldep(pool, frompool, id);
965 id = str2id(pool, id2str(frompool, id), 1);
971 memcpy(ida, from, (cc + 1) * sizeof(Id));
972 repo->idarraysize += cc + 1;
976 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
979 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
981 if (cache && cache[did & 255] == did)
982 return cache[(did & 255) + 256];
983 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
987 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
989 Id parent = dirpool_parent(&fromdata->dirpool, did);
990 Id compid = dirpool_compid(&fromdata->dirpool, did);
992 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
993 if (fromspool != &pool->ss)
994 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
995 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
998 cache[did & 255] = did;
999 cache[(did & 255) + 256] = compid;
1004 struct solvable_copy_cbdata {
1011 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1013 struct solvable_copy_cbdata *cbdata = vcbdata;
1015 Repodata *data = cbdata->data;
1016 Id handle = cbdata->handle;
1017 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1018 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1020 keyname = key->name;
1021 if (keyname >= ID_NUM_INTERNAL)
1022 keyname = str2id(pool, id2str(frompool, keyname), 1);
1025 case REPOKEY_TYPE_ID:
1026 case REPOKEY_TYPE_CONSTANTID:
1028 assert(!data->localpool); /* implement me! */
1029 if (pool != frompool || fromdata->localpool)
1032 id = copyreldep(pool, frompool, id);
1034 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1036 if (key->type == REPOKEY_TYPE_ID)
1037 repodata_set_id(data, handle, keyname, id);
1039 repodata_set_constantid(data, handle, keyname, id);
1041 case REPOKEY_TYPE_STR:
1042 repodata_set_str(data, handle, keyname, kv->str);
1044 case REPOKEY_TYPE_VOID:
1045 repodata_set_void(data, handle, keyname);
1047 case REPOKEY_TYPE_NUM:
1048 repodata_set_num(data, handle, keyname, kv->num);
1050 case REPOKEY_TYPE_CONSTANT:
1051 repodata_set_constant(data, handle, keyname, kv->num);
1053 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1055 assert(!data->localpool); /* implement me! */
1056 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1057 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1059 case REPOKEY_TYPE_DIRSTRARRAY:
1061 assert(!data->localpool); /* implement me! */
1062 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1063 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1072 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1074 Repo *repo = s->repo;
1075 Repo *fromrepo = r->repo;
1076 Pool *pool = repo->pool;
1077 struct solvable_copy_cbdata cbdata;
1079 /* copy solvable data */
1080 if (pool == fromrepo->pool)
1085 s->vendor = r->vendor;
1090 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1092 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1094 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1096 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1098 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1099 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1100 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1101 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1102 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1103 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1104 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1105 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1107 /* copy all attributes */
1111 cbdata.handle = s - pool->solvables;
1112 cbdata.dircache = dircache;
1113 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1116 /* used to sort entries returned in some database order */
1118 rpmids_sort_cmp(const void *va, const void *vb)
1120 struct rpmid const *a = va, *b = vb;
1122 r = strcmp(a->name, b->name);
1125 return a->dbid - b->dbid;
1128 static Repo *pkgids_sort_cmp_data;
1131 pkgids_sort_cmp(const void *va, const void *vb)
1133 Pool *pool = pkgids_sort_cmp_data->pool;
1134 Solvable *a = pool->solvables + *(Id *)va;
1135 Solvable *b = pool->solvables + *(Id *)vb;
1138 if (a->name != b->name)
1139 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1140 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1141 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1145 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1147 Pool *pool = repo->pool;
1150 tmp = pool->solvables[pa];
1151 pool->solvables[pa] = pool->solvables[pb];
1152 pool->solvables[pb] = tmp;
1155 Id tmpid = repo->rpmdbid[pa - repo->start];
1156 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1157 repo->rpmdbid[pb - repo->start] = tmpid;
1159 /* only works if nothing is already internalized! */
1160 if (data && data->attrs)
1162 Id *tmpattrs = data->attrs[pa - data->start];
1163 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1164 data->attrs[pb - data->start] = tmpattrs;
1169 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1171 memset(cookie, 0, 32);
1172 cookie[3] = RPMDB_COOKIE_VERSION;
1173 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1174 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1178 * read rpm db as repo
1183 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1185 Pool *pool = repo->pool;
1186 unsigned char buf[16];
1191 unsigned char *dp, *dbidp;
1193 struct rpmid *rpmids, *rp;
1199 unsigned int refmask, h;
1200 char dbpath[PATH_MAX];
1204 struct stat packagesstat;
1205 unsigned char newcookie[32];
1206 const unsigned char *oldcookie = 0;
1207 Id oldcookietype = 0;
1210 memset(&dbkey, 0, sizeof(dbkey));
1211 memset(&dbdata, 0, sizeof(dbdata));
1217 if (!(flags & REPO_REUSE_REPODATA))
1218 data = repo_add_repodata(repo, 0);
1220 data = repo_last_repodata(repo);
1222 if (ref && !(ref->nsolvables && ref->rpmdbid))
1225 if (db_env_create(&dbenv, 0))
1227 perror("db_env_create");
1230 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1231 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1233 perror("dbenv open");
1236 if (db_create(&db, dbenv, 0))
1238 perror("db_create");
1242 /* XXX: should get ro lock of Packages database! */
1243 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1244 if (stat(dbpath, &packagesstat))
1249 mkrpmdbcookie(&packagesstat, newcookie);
1250 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1253 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1254 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1257 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1259 perror("db->open var/lib/rpm/Packages");
1262 if (db->get_byteswapped(db, &byteswapped))
1264 perror("db->get_byteswapped");
1267 if (db->cursor(db, NULL, &dbc, 0))
1269 perror("db->cursor");
1272 dbidp = (unsigned char *)&dbid;
1277 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1280 s = pool_id2solvable(pool, repo_add_solvable(repo));
1282 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1283 if (dbkey.size != 4)
1285 fprintf(stderr, "corrupt Packages database (key size)\n");
1297 memcpy(dbidp, dp, 4);
1298 if (dbid == 0) /* the join key */
1300 if (dbdata.size < 8)
1302 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1305 if (dbdata.size > rpmheadsize)
1306 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1307 memcpy(buf, dbdata.data, 8);
1308 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1309 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1310 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1312 fprintf(stderr, "corrupt rpm database (data size)\n");
1315 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1316 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1317 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1318 if (rpm2solv(pool, repo, data, s, rpmhead))
1325 /* We can reuse this solvable, but make sure it's still
1326 associated with this repo. */
1327 memset(s, 0, sizeof(*s));
1333 /* oops, could not reuse. free it instead */
1334 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1340 /* now sort all solvables */
1341 if (repo->end - repo->start > 1)
1343 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1344 for (i = repo->start; i < repo->end; i++)
1345 pkgids[i - repo->start] = i;
1346 pkgids_sort_cmp_data = repo;
1347 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1349 for (i = repo->start; i < repo->end; i++)
1351 int j = pkgids[i - repo->start];
1353 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1355 swap_solvables(repo, data, i, j);
1364 memset(dircache, 0, sizeof(dircache));
1365 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1366 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1368 perror("db->open var/lib/rpm/Name");
1371 if (db->get_byteswapped(db, &byteswapped))
1373 perror("db->get_byteswapped");
1376 if (db->cursor(db, NULL, &dbc, 0))
1378 perror("db->cursor");
1381 dbidp = (unsigned char *)&dbid;
1384 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1386 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1400 memcpy(dbidp, dp, 4);
1401 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1402 rpmids[nrpmids].dbid = dbid;
1403 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1404 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1405 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1416 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1419 dbidp = (unsigned char *)&dbid;
1423 /* create hash from dbid to ref */
1424 refmask = mkmask(ref->nsolvables);
1425 refhash = sat_calloc(refmask + 1, sizeof(Id));
1426 for (i = 0; i < ref->end - ref->start; i++)
1428 if (!ref->rpmdbid[i])
1430 h = ref->rpmdbid[i] & refmask;
1432 h = (h + 317) & refmask;
1433 refhash[h] = i + 1; /* make it non-zero */
1436 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1438 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1440 for (i = 0; i < nrpmids; i++, rp++, s++)
1443 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1447 while ((id = refhash[h]))
1449 if (ref->rpmdbid[id - 1] == dbid)
1451 h = (h + 317) & refmask;
1455 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1458 solvable_copy(s, r, data, dircache);
1465 if (db_create(&db, 0, 0))
1467 perror("db_create");
1470 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1471 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1473 perror("db->open var/lib/rpm/Packages");
1476 if (db->get_byteswapped(db, &byteswapped))
1478 perror("db->get_byteswapped");
1490 memcpy(buf, dbidp, 4);
1495 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1498 fprintf(stderr, "corrupt rpm database\n");
1501 if (dbdata.size < 8)
1503 fprintf(stderr, "corrupt rpm database (size)\n");
1506 if (dbdata.size > rpmheadsize)
1507 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1508 memcpy(buf, dbdata.data, 8);
1509 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1510 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1511 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1513 fprintf(stderr, "corrupt rpm database (data size)\n");
1516 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1517 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1519 rpm2solv(pool, repo, data, s, rpmhead);
1526 for (i = 0; i < nrpmids; i++)
1527 sat_free(rpmids[i].name);
1531 if (!(flags & REPO_NO_INTERNALIZE))
1532 repodata_internalize(data);
1537 dbenv->close(dbenv, 0);
1541 static inline unsigned int
1542 getu32(unsigned char *dp)
1544 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1549 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1551 Pool *pool = s->repo->pool;
1552 const char *name, *n1, *n2;
1555 /* skip ./ prefix */
1556 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1559 name = strrchr(location, '/');
1565 n2 = id2str(pool, s->arch);
1567 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1569 /* too bad, need to store directory */
1570 char *dir = strdup(location);
1571 dir[name - location - 1] = 0;
1572 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1576 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1579 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1582 if (*n2 || *n1 != '-')
1585 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1588 if (*n2 || *n1 != '.')
1591 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1594 if (*n2 || strcmp (n1, ".rpm"))
1596 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1600 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1605 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1607 int i, sigdsize, sigcnt, l;
1608 Pool *pool = repo->pool;
1610 RpmHead *rpmhead = 0;
1611 int rpmheadsize = 0;
1612 char *payloadformat;
1614 unsigned char lead[4096];
1615 int headerstart, headerend;
1619 if (!(flags & REPO_REUSE_REPODATA))
1620 data = repo_add_repodata(repo, 0);
1622 data = repo_last_repodata(repo);
1627 for (i = 0; i < nrpms; i++)
1629 if ((fp = fopen(rpms[i], "r")) == 0)
1634 if (fstat(fileno(fp), &stb))
1639 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1641 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1645 if (lead[78] != 0 || lead[79] != 5)
1647 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1651 if (getu32(lead + 96) != 0x8eade801)
1653 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1657 sigcnt = getu32(lead + 96 + 8);
1658 sigdsize = getu32(lead + 96 + 12);
1659 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1661 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1665 sigdsize += sigcnt * 16;
1666 sigdsize = (sigdsize + 7) & ~7;
1667 headerstart = 96 + 16 + sigdsize;
1670 l = sigdsize > 4096 ? 4096 : sigdsize;
1671 if (fread(lead, l, 1, fp) != 1)
1673 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1679 if (fread(lead, 16, 1, fp) != 1)
1681 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1685 if (getu32(lead) != 0x8eade801)
1687 fprintf(stderr, "%s: bad header\n", rpms[i]);
1691 sigcnt = getu32(lead + 8);
1692 sigdsize = getu32(lead + 12);
1693 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1695 fprintf(stderr, "%s: bad header\n", rpms[i]);
1699 l = sigdsize + sigcnt * 16;
1700 headerend = headerstart + 16 + l;
1701 if (l > rpmheadsize)
1702 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1703 if (fread(rpmhead->data, l, 1, fp) != 1)
1705 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1709 rpmhead->cnt = sigcnt;
1710 rpmhead->dcnt = sigdsize;
1711 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1712 if (headexists(rpmhead, TAG_PATCHESNAME))
1714 /* this is a patch rpm, ignore */
1718 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1719 if (payloadformat && !strcmp(payloadformat, "drpm"))
1721 /* this is a delta rpm */
1726 s = pool_id2solvable(pool, repo_add_solvable(repo));
1727 rpm2solv(pool, repo, data, s, rpmhead);
1730 Id handle = s - pool->solvables;
1731 add_location(data, s, handle, rpms[i]);
1732 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1733 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1738 if (!(flags & REPO_NO_INTERNALIZE))
1739 repodata_internalize(data);