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>
36 #include "repo_rpmdb.h"
38 #define RPMDB_COOKIE_VERSION 2
41 #define TAG_VERSION 1001
42 #define TAG_RELEASE 1002
43 #define TAG_EPOCH 1003
44 #define TAG_SUMMARY 1004
45 #define TAG_DESCRIPTION 1005
46 #define TAG_BUILDTIME 1006
47 #define TAG_BUILDHOST 1007
48 #define TAG_INSTALLTIME 1008
50 #define TAG_DISTRIBUTION 1010
51 #define TAG_VENDOR 1011
52 #define TAG_LICENSE 1014
53 #define TAG_PACKAGER 1015
54 #define TAG_GROUP 1016
57 #define TAG_FILESIZES 1028
58 #define TAG_FILEMODES 1030
59 #define TAG_FILEMD5S 1035
60 #define TAG_FILELINKTOS 1036
61 #define TAG_SOURCERPM 1044
62 #define TAG_PROVIDENAME 1047
63 #define TAG_REQUIREFLAGS 1048
64 #define TAG_REQUIRENAME 1049
65 #define TAG_REQUIREVERSION 1050
66 #define TAG_NOSOURCE 1051
67 #define TAG_NOPATCH 1052
68 #define TAG_CONFLICTFLAGS 1053
69 #define TAG_CONFLICTNAME 1054
70 #define TAG_CONFLICTVERSION 1055
71 #define TAG_OBSOLETENAME 1090
72 #define TAG_FILEDEVICES 1095
73 #define TAG_FILEINODES 1096
74 #define TAG_PROVIDEFLAGS 1112
75 #define TAG_PROVIDEVERSION 1113
76 #define TAG_OBSOLETEFLAGS 1114
77 #define TAG_OBSOLETEVERSION 1115
78 #define TAG_DIRINDEXES 1116
79 #define TAG_BASENAMES 1117
80 #define TAG_DIRNAMES 1118
81 #define TAG_PAYLOADFORMAT 1124
82 #define TAG_PATCHESNAME 1133
83 #define TAG_FILECOLORS 1140
84 #define TAG_SUGGESTSNAME 1156
85 #define TAG_SUGGESTSVERSION 1157
86 #define TAG_SUGGESTSFLAGS 1158
87 #define TAG_ENHANCESNAME 1159
88 #define TAG_ENHANCESVERSION 1160
89 #define TAG_ENHANCESFLAGS 1161
91 #define SIGTAG_SIZE 1000
92 #define SIGTAG_PGP 1002 /* RSA signature */
93 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
94 #define SIGTAG_GPG 1005 /* DSA signature */
96 #define DEP_LESS (1 << 1)
97 #define DEP_GREATER (1 << 2)
98 #define DEP_EQUAL (1 << 3)
99 #define DEP_STRONG (1 << 27)
100 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
108 typedef struct rpmhead {
112 unsigned char data[1];
116 static inline unsigned char *
117 headfindtag(RpmHead *h, int tag)
120 unsigned char *d, taga[4];
126 for (i = 0; i < h->cnt; i++, d -= 16)
127 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
133 headexists(RpmHead *h, int tag)
135 return headfindtag(h, tag) ? 1 : 0;
138 static unsigned int *
139 headint32array(RpmHead *h, int tag, int *cnt)
141 unsigned int i, o, *r;
142 unsigned char *d = headfindtag(h, tag);
144 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
146 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
147 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
148 if (o + 4 * i > h->dcnt)
151 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
154 for (o = 0; o < i; o++, d += 4)
155 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
159 /* returns the first entry of an integer array */
161 headint32(RpmHead *h, int tag)
164 unsigned char *d = headfindtag(h, tag);
166 if (!d || 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 = headfindtag(h, tag);
182 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
184 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
185 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
186 if (o + 4 * i > h->dcnt)
189 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
192 for (o = 0; o < i; o++, d += 2)
193 r[o] = d[0] << 8 | d[1];
198 headstring(RpmHead *h, int tag)
201 unsigned char *d = headfindtag(h, tag);
202 /* 6: STRING, 9: I18NSTRING */
203 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
205 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
208 return (char *)h->dp + o;
212 headstringarray(RpmHead *h, int tag, int *cnt)
215 unsigned char *d = headfindtag(h, tag);
218 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
220 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
221 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
222 r = sat_calloc(i ? i : 1, sizeof(char *));
226 for (o = 0; o < i; o++)
230 d += strlen((char *)d) + 1;
231 if (d >= h->dp + h->dcnt)
240 static unsigned char *
241 headbinary(RpmHead *h, int tag, unsigned int *sizep)
244 unsigned char *d = headfindtag(h, tag);
245 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
247 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
248 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
249 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
256 static char *headtoevr(RpmHead *h)
263 version = headstring(h, TAG_VERSION);
264 release = headstring(h, TAG_RELEASE);
265 epoch = headint32(h, TAG_EPOCH);
266 if (!version || !release)
268 fprintf(stderr, "headtoevr: bad rpm header\n");
271 for (v = version; *v >= 0 && *v <= '9'; v++)
273 if (epoch || (v != version && *v == ':'))
275 char epochbuf[11]; /* 32bit decimal will fit in */
276 sprintf(epochbuf, "%u", epoch);
277 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
278 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
282 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
283 sprintf(evr, "%s-%s", version, release);
290 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
292 const unsigned char *cp;
295 unsigned char *buf = 0, *bp;
297 /* check if it's already utf8, code taken from screen ;-) */
298 cp = (const unsigned char *)str;
299 while ((c = *cp++) != 0)
303 if ((c & 0xc0) != 0x80)
304 break; /* encoding error */
305 c = (c & 0x3f) | (state << 6);
306 if (!(state & 0x40000000))
308 /* check for overlong sequences */
309 if ((c & 0x820823e0) == 0x80000000)
311 else if ((c & 0x020821f0) == 0x02000000)
313 else if ((c & 0x000820f8) == 0x00080000)
315 else if ((c & 0x0000207c) == 0x00002000)
325 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
327 c = (c & 0x03) | 0xbfffff00; /* 4 */
329 c = (c & 0x07) | 0xbfffc000; /* 3 */
331 c = (c & 0x0f) | 0xbff00000; /* 2 */
333 c = (c & 0x1f) | 0xfc000000; /* 1 */
337 state = (c & 0x80000000) ? c : 0;
341 /* not utf8, assume latin1 */
342 buf = sat_malloc(2 * strlen(str) + 1);
343 cp = (const unsigned char *)str;
346 while ((c = *cp++) != 0)
359 repodata_set_str(repodata, handle, tag, str);
365 #define MAKEDEPS_FILTER_WEAK (1 << 0)
366 #define MAKEDEPS_FILTER_STRONG (1 << 1)
367 #define MAKEDEPS_NO_RPMLIB (1 << 2)
370 * strong: 0: ignore strongness
371 * 1: filter to strong
375 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
379 int i, cc, nc, vc, fc;
381 unsigned int olddeps;
385 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
386 n = headstringarray(rpmhead, tagn, &nc);
389 v = headstringarray(rpmhead, tagv, &vc);
395 f = headint32array(rpmhead, tagf, &fc);
402 if (nc != vc || nc != fc)
404 fprintf(stderr, "bad dependency entries\n");
409 haspre = 0; /* add no prereq marker */
412 /* we do filtering */
414 for (i = 0; i < nc; i++)
416 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
418 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
419 if (!strncmp(n[i], "rpmlib(", 7))
421 if ((f[i] & DEP_PRE) != 0)
426 else if (tagn == TAG_REQUIRENAME)
428 /* no filtering, just look for the first prereq */
429 for (i = 0; i < nc; i++)
430 if ((f[i] & DEP_PRE) != 0)
444 olddeps = repo_reserve_ids(repo, 0, cc);
445 ida = repo->idarraydata + olddeps;
452 haspre = 2; /* pass two: prereqs */
454 *ida++ = SOLVABLE_PREREQMARKER;
456 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
458 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
460 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
462 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
463 if (!strncmp(n[i], "rpmlib(", 7))
465 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
469 if ((f[i] & DEP_LESS) != 0)
471 if ((f[i] & DEP_EQUAL) != 0)
473 if ((f[i] & DEP_GREATER) != 0)
475 name = str2id(pool, n[i], 1);
476 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
477 evr = str2id(pool, v[i] + 2, 1);
479 evr = str2id(pool, v[i], 1);
480 *ida++ = rel2id(pool, name, evr, flags, 1);
483 *ida++ = str2id(pool, n[i], 1);
486 repo->idarraysize += cc + 1;
494 #ifdef USE_FILEFILTER
496 #define FILEFILTER_EXACT 0
497 #define FILEFILTER_STARTS 1
498 #define FILEFILTER_CONTAINS 2
506 static struct filefilter filefilters[] = {
507 { FILEFILTER_CONTAINS, "/bin/", 0},
508 { FILEFILTER_CONTAINS, "/sbin/", 0},
509 { FILEFILTER_CONTAINS, "/lib/", 0},
510 { FILEFILTER_CONTAINS, "/lib64/", 0},
511 { FILEFILTER_CONTAINS, "/etc/", 0},
512 { FILEFILTER_STARTS, "/usr/games/", 0},
513 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
514 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
515 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
521 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
525 unsigned int *fkb, *fn, *fsz, *fm, *fino;
526 unsigned int inotest[256], inotestok;
530 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
531 if (!fsz || fc != fszc)
536 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
537 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
538 if (!fm || fc != fszc)
544 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
545 if (!fino || fc != fszc)
553 if (fc < sizeof(inotest))
555 memset(inotest, 0, sizeof(inotest));
556 for (i = 0; i < fc; i++)
559 if (fsz[i] == 0 || !S_ISREG(fm[i]))
561 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
562 bit = 1 << (fino[i] & 31);
563 if ((inotest[off] & bit) != 0)
572 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
574 unsigned int mask, hash, hh;
575 if (!fdev || fc != fszc)
584 while ((mask & (mask - 1)) != 0)
585 mask = mask & (mask - 1);
587 if (mask > sizeof(inotest)/sizeof(*inotest))
588 fx = sat_calloc(mask, sizeof(unsigned int));
592 memset(fx, 0, mask * sizeof(unsigned int));
595 for (i = 0; i < fc; i++)
597 if (fsz[i] == 0 || !S_ISREG(fm[i]))
599 hash = (fino[i] + fdev[i] * 31) & mask;
601 while ((j = fx[hash]) != 0)
603 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
605 fsz[i] = 0; /* kill entry */
608 hash = (hash + hh++) & mask;
618 fn = sat_calloc(dic, sizeof(unsigned int));
619 fkb = sat_calloc(dic, sizeof(unsigned int));
620 for (i = 0; i < fc; i++)
622 if (fsz[i] == 0 || !S_ISREG(fm[i]))
627 fkb[di[i]] += fsz[i] / 1024 + 1;
632 handle = s - pool->solvables;
633 for (i = 0; i < fc; i++)
637 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
638 did = repodata_str2dir(data, "/usr/src", 1);
640 did = repodata_str2dir(data, dn[i], 1);
641 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
647 /* assumes last processed array is provides! */
649 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
656 #ifdef USE_FILEFILTER
658 struct filefilter *ff;
667 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
670 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
676 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
685 fprintf(stderr, "bad filelist\n");
690 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
692 for (i = 0; i < bnc; i++)
694 #ifdef USE_FILEFILTER
696 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
700 switch (ff->dirmatch)
702 case FILEFILTER_STARTS:
703 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
706 case FILEFILTER_CONTAINS:
707 if (!strstr(dn[di[i]], ff->dir))
710 case FILEFILTER_EXACT:
712 if (strcmp(dn[di[i]], ff->dir))
719 if (strcmp(bn[i], ff->base))
724 if (j == sizeof(filefilters)/sizeof(*filefilters))
728 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
732 fn = sat_realloc(fn, fna);
734 strcpy(fn, dn[di[i]]);
736 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
741 handle = s - pool->solvables;
742 did = repodata_str2dir(data, dn[di[i]], 1);
744 did = repodata_str2dir(data, "/", 1);
745 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
759 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
761 const char *p, *sevr, *sarch;
763 p = strrchr(sourcerpm, '.');
764 if (!p || strcmp(p, ".rpm") != 0)
767 while (p > sourcerpm && *p != '.')
769 if (*p != '.' || p == sourcerpm)
772 while (p > sourcerpm && *p != '-')
774 if (*p != '-' || p == sourcerpm)
777 while (p > sourcerpm && *p != '-')
779 if (*p != '-' || p == sourcerpm)
782 if (!strcmp(sarch, "src.rpm"))
783 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
784 else if (!strcmp(sarch, "nosrc.rpm"))
785 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
787 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
788 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
789 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
791 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
792 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
793 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
795 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
799 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
805 name = headstring(rpmhead, TAG_NAME);
806 if (!strcmp(name, "gpg-pubkey"))
808 s->name = str2id(pool, name, 1);
811 fprintf(stderr, "package has no name\n");
814 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
816 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
819 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
820 s->arch = ARCH_NOSRC;
825 s->arch = ARCH_NOARCH;
826 evr = headtoevr(rpmhead);
827 s->evr = str2id(pool, evr, 1);
828 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
830 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
831 if ((flags & RPM_ADD_NO_FILELIST) == 0)
832 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
833 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
834 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
835 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
836 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
837 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
839 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
840 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
841 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
842 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
843 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
844 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
852 handle = s - pool->solvables;
853 str = headstring(rpmhead, TAG_SUMMARY);
855 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
856 str = headstring(rpmhead, TAG_DESCRIPTION);
860 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
861 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
865 /* oh my, found SUSE special author section */
870 while (l > 0 && str[l - 1] == '\n')
873 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
875 aut = str; /* copy over */
876 while (*p == ' ' || *p == '\n')
889 while (aut != str && aut[-1] == '\n')
893 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
897 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
899 str = headstring(rpmhead, TAG_GROUP);
901 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
902 str = headstring(rpmhead, TAG_LICENSE);
904 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
905 str = headstring(rpmhead, TAG_URL);
907 repodata_set_str(data, handle, SOLVABLE_URL, str);
908 str = headstring(rpmhead, TAG_DISTRIBUTION);
910 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
911 str = headstring(rpmhead, TAG_PACKAGER);
913 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
914 u32 = headint32(rpmhead, TAG_BUILDTIME);
916 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
917 u32 = headint32(rpmhead, TAG_INSTALLTIME);
919 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
920 u32 = headint32(rpmhead, TAG_SIZE);
922 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
924 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
931 copyreldep(Pool *pool, Pool *frompool, Id id)
933 Reldep *rd = GETRELDEP(frompool, id);
934 Id name = rd->name, evr = rd->evr;
936 name = copyreldep(pool, frompool, name);
938 name = str2id(pool, id2str(frompool, name), 1);
940 evr = copyreldep(pool, frompool, evr);
942 evr = str2id(pool, id2str(frompool, evr), 1);
943 return rel2id(pool, name, evr, rd->flags, 1);
947 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
952 Pool *frompool = fromrepo->pool;
956 from = fromrepo->idarraydata + fromoff;
957 for (ida = from, cc = 0; *ida; ida++, cc++)
961 ido = repo_reserve_ids(repo, 0, cc);
962 ida = repo->idarraydata + ido;
963 if (frompool && pool != frompool)
969 id = copyreldep(pool, frompool, id);
971 id = str2id(pool, id2str(frompool, id), 1);
977 memcpy(ida, from, (cc + 1) * sizeof(Id));
978 repo->idarraysize += cc + 1;
982 #define COPYDIR_DIRCACHE_SIZE 512
984 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
987 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
989 if (cache && cache[did & 255] == did)
990 return cache[(did & 255) + 256];
991 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
995 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
997 Id parent = dirpool_parent(&fromdata->dirpool, did);
998 Id compid = dirpool_compid(&fromdata->dirpool, did);
1000 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1001 if (fromspool != &pool->ss)
1002 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1003 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1006 cache[did & 255] = did;
1007 cache[(did & 255) + 256] = compid;
1012 struct solvable_copy_cbdata {
1019 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1021 struct solvable_copy_cbdata *cbdata = vcbdata;
1023 Repodata *data = cbdata->data;
1024 Id handle = cbdata->handle;
1025 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1026 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1028 keyname = key->name;
1029 if (keyname >= ID_NUM_INTERNAL)
1030 keyname = str2id(pool, id2str(frompool, keyname), 1);
1033 case REPOKEY_TYPE_ID:
1034 case REPOKEY_TYPE_CONSTANTID:
1036 assert(!data->localpool); /* implement me! */
1037 if (pool != frompool || fromdata->localpool)
1040 id = copyreldep(pool, frompool, id);
1042 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1044 if (key->type == REPOKEY_TYPE_ID)
1045 repodata_set_id(data, handle, keyname, id);
1047 repodata_set_constantid(data, handle, keyname, id);
1049 case REPOKEY_TYPE_STR:
1050 repodata_set_str(data, handle, keyname, kv->str);
1052 case REPOKEY_TYPE_VOID:
1053 repodata_set_void(data, handle, keyname);
1055 case REPOKEY_TYPE_NUM:
1056 repodata_set_num(data, handle, keyname, kv->num);
1058 case REPOKEY_TYPE_CONSTANT:
1059 repodata_set_constant(data, handle, keyname, kv->num);
1061 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1063 assert(!data->localpool); /* implement me! */
1064 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1065 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1067 case REPOKEY_TYPE_DIRSTRARRAY:
1069 assert(!data->localpool); /* implement me! */
1070 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1071 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1080 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1082 Repo *repo = s->repo;
1083 Repo *fromrepo = r->repo;
1084 Pool *pool = repo->pool;
1085 struct solvable_copy_cbdata cbdata;
1087 /* copy solvable data */
1088 if (pool == fromrepo->pool)
1093 s->vendor = r->vendor;
1098 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1100 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1102 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1104 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1106 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1107 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1108 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1109 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1110 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1111 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1112 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1113 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1115 /* copy all attributes */
1119 cbdata.handle = s - pool->solvables;
1120 cbdata.dircache = dircache;
1121 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1124 /* used to sort entries returned in some database order */
1126 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1128 struct rpmid const *a = va, *b = vb;
1130 r = strcmp(a->name, b->name);
1133 return a->dbid - b->dbid;
1137 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1140 Pool *pool = repo->pool;
1141 Solvable *a = pool->solvables + *(Id *)va;
1142 Solvable *b = pool->solvables + *(Id *)vb;
1145 if (a->name != b->name)
1146 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1147 rpmdbid = repo->rpmdbid;
1148 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1152 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1154 Pool *pool = repo->pool;
1157 tmp = pool->solvables[pa];
1158 pool->solvables[pa] = pool->solvables[pb];
1159 pool->solvables[pb] = tmp;
1162 Id tmpid = repo->rpmdbid[pa - repo->start];
1163 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1164 repo->rpmdbid[pb - repo->start] = tmpid;
1166 /* only works if nothing is already internalized! */
1167 if (data && data->attrs)
1169 Id *tmpattrs = data->attrs[pa - data->start];
1170 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1171 data->attrs[pb - data->start] = tmpattrs;
1176 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1178 memset(cookie, 0, 32);
1179 cookie[3] = RPMDB_COOKIE_VERSION;
1180 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1181 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1185 count_headers(const char *rootdir, DB_ENV *dbenv)
1187 char dbpath[PATH_MAX];
1188 struct stat statbuf;
1196 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1197 if (stat(dbpath, &statbuf))
1199 memset(&dbkey, 0, sizeof(dbkey));
1200 memset(&dbdata, 0, sizeof(dbdata));
1201 if (db_create(&db, dbenv, 0))
1203 perror("db_create");
1206 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1208 perror("db->open Name index");
1211 if (db->get_byteswapped(db, &byteswapped))
1213 perror("db->get_byteswapped");
1216 if (db->cursor(db, NULL, &dbc, 0))
1218 perror("db->cursor");
1221 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1222 count += dbdata.size >> 3;
1229 * read rpm db as repo
1234 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1236 Pool *pool = repo->pool;
1237 unsigned char buf[16];
1242 unsigned char *dp, *dbidp;
1244 struct rpmid *rpmids, *rp;
1250 unsigned int refmask, h;
1251 char dbpath[PATH_MAX];
1255 struct stat packagesstat;
1256 unsigned char newcookie[32];
1257 const unsigned char *oldcookie = 0;
1258 Id oldcookietype = 0;
1260 int count = 0, done = 0;
1263 now = sat_timems(0);
1264 memset(&dbkey, 0, sizeof(dbkey));
1265 memset(&dbdata, 0, sizeof(dbdata));
1270 if (!(flags & REPO_REUSE_REPODATA))
1271 data = repo_add_repodata(repo, 0);
1273 data = repo_last_repodata(repo);
1275 if (ref && !(ref->nsolvables && ref->rpmdbid))
1278 if (db_env_create(&dbenv, 0))
1280 perror("db_env_create");
1283 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1284 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1286 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
1288 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1291 perror("dbenv open");
1295 /* XXX: should get ro lock of Packages database! */
1296 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1297 if (stat(dbpath, &packagesstat))
1302 mkrpmdbcookie(&packagesstat, newcookie);
1303 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1306 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1307 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1311 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1312 count = count_headers(rootdir, dbenv);
1313 if (db_create(&db, dbenv, 0))
1315 perror("db_create");
1318 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1320 perror("db->open Packages index");
1323 if (db->get_byteswapped(db, &byteswapped))
1325 perror("db->get_byteswapped");
1328 if (db->cursor(db, NULL, &dbc, 0))
1330 perror("db->cursor");
1333 dbidp = (unsigned char *)&dbid;
1338 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1341 s = pool_id2solvable(pool, repo_add_solvable(repo));
1343 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1344 if (dbkey.size != 4)
1346 fprintf(stderr, "corrupt Packages database (key size)\n");
1358 memcpy(dbidp, dp, 4);
1359 if (dbid == 0) /* the join key */
1361 if (dbdata.size < 8)
1363 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1366 if (dbdata.size > rpmheadsize)
1368 rpmheadsize = dbdata.size + 128;
1369 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1371 memcpy(buf, dbdata.data, 8);
1372 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1373 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1374 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1376 fprintf(stderr, "corrupt rpm database (data size)\n");
1379 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1380 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1381 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1382 if (rpm2solv(pool, repo, data, s, rpmhead, flags))
1389 /* We can reuse this solvable, but make sure it's still
1390 associated with this repo. */
1391 memset(s, 0, sizeof(*s));
1394 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1398 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1399 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1404 /* oops, could not reuse. free it instead */
1405 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1411 /* now sort all solvables */
1412 if (repo->end - repo->start > 1)
1414 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1415 for (i = repo->start; i < repo->end; i++)
1416 pkgids[i - repo->start] = i;
1417 sat_sort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp, repo);
1419 for (i = repo->start; i < repo->end; i++)
1421 int j = pkgids[i - repo->start];
1423 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1425 swap_solvables(repo, data, i, j);
1432 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1434 memset(dircache, 0, sizeof(dircache));
1435 if (db_create(&db, dbenv, 0))
1437 perror("db_create");
1440 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1442 perror("db->open Name index");
1445 if (db->get_byteswapped(db, &byteswapped))
1447 perror("db->get_byteswapped");
1450 if (db->cursor(db, NULL, &dbc, 0))
1452 perror("db->cursor");
1455 dbidp = (unsigned char *)&dbid;
1458 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1460 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1474 memcpy(dbidp, dp, 4);
1475 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1476 rpmids[nrpmids].dbid = dbid;
1477 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1478 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1479 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1490 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1492 dbidp = (unsigned char *)&dbid;
1496 /* create hash from dbid to ref */
1497 refmask = mkmask(ref->nsolvables);
1498 refhash = sat_calloc(refmask + 1, sizeof(Id));
1499 for (i = 0; i < ref->end - ref->start; i++)
1501 if (!ref->rpmdbid[i])
1503 h = ref->rpmdbid[i] & refmask;
1505 h = (h + 317) & refmask;
1506 refhash[h] = i + 1; /* make it non-zero */
1509 /* count the misses, they will cost us time */
1510 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1512 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1518 while ((id = refhash[h]))
1520 if (ref->rpmdbid[id - 1] == dbid)
1522 h = (h + 317) & refmask;
1531 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1533 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1535 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1538 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1542 while ((id = refhash[h]))
1544 if (ref->rpmdbid[id - 1] == dbid)
1546 h = (h + 317) & refmask;
1550 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1553 solvable_copy(s, r, data, dircache);
1560 if (db_create(&db, dbenv, 0))
1562 perror("db_create");
1565 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1567 perror("db->open var/lib/rpm/Packages");
1570 if (db->get_byteswapped(db, &byteswapped))
1572 perror("db->get_byteswapped");
1584 memcpy(buf, dbidp, 4);
1589 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1592 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1593 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1596 if (dbdata.size < 8)
1598 fprintf(stderr, "corrupt rpm database (size)\n");
1601 if (dbdata.size > rpmheadsize)
1603 rpmheadsize = dbdata.size + 128;
1604 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1606 memcpy(buf, dbdata.data, 8);
1607 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1608 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1609 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1611 fprintf(stderr, "corrupt rpm database (data size)\n");
1614 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1615 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1617 rpm2solv(pool, repo, data, s, rpmhead, flags);
1618 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1622 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1623 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1631 for (i = 0; i < nrpmids; i++)
1632 sat_free(rpmids[i].name);
1638 dbenv->close(dbenv, 0);
1641 if (!(flags & REPO_NO_INTERNALIZE))
1642 repodata_internalize(data);
1643 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1644 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1645 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1646 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1647 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1651 static inline unsigned int
1652 getu32(unsigned char *dp)
1654 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1659 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1661 int i, sigdsize, sigcnt, l;
1662 Pool *pool = repo->pool;
1664 RpmHead *rpmhead = 0;
1665 int rpmheadsize = 0;
1666 char *payloadformat;
1668 unsigned char lead[4096];
1669 int headerstart, headerend;
1672 unsigned char pkgid[16];
1675 if (!(flags & REPO_REUSE_REPODATA))
1676 data = repo_add_repodata(repo, 0);
1678 data = repo_last_repodata(repo);
1680 for (i = 0; i < nrpms; i++)
1682 if ((fp = fopen(rpms[i], "r")) == 0)
1687 if (fstat(fileno(fp), &stb))
1692 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1694 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1698 if (lead[78] != 0 || lead[79] != 5)
1700 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1704 if (getu32(lead + 96) != 0x8eade801)
1706 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1710 sigcnt = getu32(lead + 96 + 8);
1711 sigdsize = getu32(lead + 96 + 12);
1712 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1714 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1718 sigdsize += sigcnt * 16;
1719 sigdsize = (sigdsize + 7) & ~7;
1720 headerstart = 96 + 16 + sigdsize;
1722 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1724 unsigned char *chksum;
1725 unsigned int chksumsize;
1726 /* extract pkgid from the signature header */
1727 if (sigdsize > rpmheadsize)
1729 rpmheadsize = sigdsize + 128;
1730 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1732 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1734 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1738 rpmhead->cnt = sigcnt;
1739 rpmhead->dcnt = sigdsize - sigcnt * 16;
1740 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1741 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1742 if (chksum && chksumsize == 16)
1745 memcpy(pkgid, chksum, 16);
1750 /* just skip the signature header */
1753 l = sigdsize > 4096 ? 4096 : sigdsize;
1754 if (fread(lead, l, 1, fp) != 1)
1756 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1763 if (fread(lead, 16, 1, fp) != 1)
1765 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1769 if (getu32(lead) != 0x8eade801)
1771 fprintf(stderr, "%s: bad header\n", rpms[i]);
1775 sigcnt = getu32(lead + 8);
1776 sigdsize = getu32(lead + 12);
1777 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1779 fprintf(stderr, "%s: bad header\n", rpms[i]);
1783 l = sigdsize + sigcnt * 16;
1784 headerend = headerstart + 16 + l;
1785 if (l > rpmheadsize)
1787 rpmheadsize = l + 128;
1788 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1790 if (fread(rpmhead->data, l, 1, fp) != 1)
1792 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1796 rpmhead->cnt = sigcnt;
1797 rpmhead->dcnt = sigdsize;
1798 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1799 if (headexists(rpmhead, TAG_PATCHESNAME))
1801 /* this is a patch rpm, ignore */
1805 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1806 if (payloadformat && !strcmp(payloadformat, "drpm"))
1808 /* this is a delta rpm */
1813 s = pool_id2solvable(pool, repo_add_solvable(repo));
1814 rpm2solv(pool, repo, data, s, rpmhead, flags);
1817 Id handle = s - pool->solvables;
1818 repodata_set_location(data, handle, 0, 0, rpms[i]);
1819 if (S_ISREG(stb.st_mode))
1820 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1821 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1823 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1828 if (!(flags & REPO_NO_INTERNALIZE))
1829 repodata_internalize(data);
1833 linkhash(const char *lt, char *hash)
1836 const unsigned char *str = (const unsigned char *)lt;
1840 while ((c = *str++) != 0)
1842 sprintf(hash, "%08x", r);
1843 sprintf(hash + 8, "%08x", l);
1844 sprintf(hash + 16, "%08x", 0);
1845 sprintf(hash + 24, "%08x", 0);
1849 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1851 RpmHead *rpmhead = rpmhandle;
1856 unsigned int *di, diidx;
1857 unsigned int lastdir;
1860 int cnt, dcnt, cnt2;
1864 char md5[33], *md5p = 0;
1866 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1869 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1871 for (i = 0; i < dcnt; i++)
1872 (*cb)(cbdata, dn[i], 0, (char *)0);
1876 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1882 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1883 if (!di || cnt != cnt2)
1890 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1891 if (!fm || cnt != cnt2)
1899 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1901 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1902 if (!md || cnt != cnt2)
1914 for (i = 0; i < cnt; i++)
1919 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
1922 l = l1 + strlen(bn[i]) + 1;
1926 space = sat_realloc(space, spacen);
1928 if (lastdir != diidx)
1930 strcpy(space, dn[diidx]);
1934 strcpy(space + l1, bn[i]);
1943 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
1949 linkhash(lt[i], md5);
1956 (*cb)(cbdata, space, fm[i], md5p);
1968 struct rpm_by_state {
1979 rpm_installedrpmdbids(const char *rootdir, Queue *rpmdbidq)
1981 char dbpath[PATH_MAX];
1993 queue_empty(rpmdbidq);
1996 if (db_env_create(&dbenv, 0))
1998 perror("db_env_create");
2001 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
2003 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
2005 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
2008 perror("dbenv open");
2009 dbenv->close(dbenv, 0);
2012 if (db_create(&db, dbenv, 0))
2014 perror("db_create");
2015 dbenv->close(dbenv, 0);
2018 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2020 perror("db->open Name index");
2022 dbenv->close(dbenv, 0);
2025 if (db->get_byteswapped(db, &byteswapped))
2027 perror("db->get_byteswapped");
2029 dbenv->close(dbenv, 0);
2032 if (db->cursor(db, NULL, &dbc, 0))
2034 perror("db->cursor");
2036 dbenv->close(dbenv, 0);
2039 memset(&dbkey, 0, sizeof(dbkey));
2040 memset(&dbdata, 0, sizeof(dbdata));
2041 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
2043 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2051 ((char *)&rpmdbid)[0] = dp[3];
2052 ((char *)&rpmdbid)[1] = dp[2];
2053 ((char *)&rpmdbid)[2] = dp[1];
2054 ((char *)&rpmdbid)[3] = dp[0];
2057 memcpy((char *)&rpmdbid, dp, 4);
2059 queue_push(rpmdbidq, rpmdbid);
2067 dbenv->close(dbenv, 0);
2072 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2074 struct rpm_by_state *state = *statep;
2075 unsigned char buf[16];
2086 state->db->close(state->db, 0);
2088 state->dbenv->close(state->dbenv, 0);
2089 sat_free(state->rpmhead);
2091 *statep = (void *)0;
2097 state = sat_calloc(1, sizeof(*state));
2100 if (!state->dbopened)
2102 char dbpath[PATH_MAX];
2103 state->dbopened = 1;
2104 if (db_env_create(&state->dbenv, 0))
2106 perror("db_env_create");
2112 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
2114 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
2116 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
2119 perror("dbenv open");
2120 state->dbenv->close(state->dbenv, 0);
2124 if (db_create(&state->db, state->dbenv, 0))
2126 perror("db_create");
2128 state->dbenv->close(state->dbenv, 0);
2132 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2134 perror("db->open var/lib/rpm/Packages");
2135 state->db->close(state->db, 0);
2137 state->dbenv->close(state->dbenv, 0);
2141 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2143 perror("db->get_byteswapped");
2144 state->db->close(state->db, 0);
2146 state->dbenv->close(state->dbenv, 0);
2151 memcpy(buf, &rpmdbid, 4);
2152 if (state->byteswapped)
2155 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2156 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2158 memset(&dbkey, 0, sizeof(dbkey));
2159 memset(&dbdata, 0, sizeof(dbdata));
2164 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2169 if (dbdata.size < 8)
2171 fprintf(stderr, "corrupt rpm database (size)\n");
2174 if (dbdata.size > state->rpmheadsize)
2176 state->rpmheadsize = dbdata.size + 128;
2177 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2179 rpmhead = state->rpmhead;
2180 memcpy(buf, dbdata.data, 8);
2181 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2182 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2183 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2185 fprintf(stderr, "corrupt rpm database (data size)\n");
2188 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2189 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2194 rpm_byfp(FILE *fp, const char *name, void **statep)
2196 struct rpm_by_state *state = *statep;
2197 int headerstart, headerend;
2199 int sigdsize, sigcnt, l;
2200 unsigned char lead[4096];
2203 return rpm_byrpmdbid(0, 0, statep);
2206 state = sat_calloc(1, sizeof(*state));
2209 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2211 fprintf(stderr, "%s: not a rpm\n", name);
2214 if (lead[78] != 0 || lead[79] != 5)
2216 fprintf(stderr, "%s: not a V5 header\n", name);
2219 if (getu32(lead + 96) != 0x8eade801)
2221 fprintf(stderr, "%s: bad signature header\n", name);
2224 sigcnt = getu32(lead + 96 + 8);
2225 sigdsize = getu32(lead + 96 + 12);
2226 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2228 fprintf(stderr, "%s: bad signature header\n", name);
2231 sigdsize += sigcnt * 16;
2232 sigdsize = (sigdsize + 7) & ~7;
2233 headerstart = 96 + 16 + sigdsize;
2236 l = sigdsize > 4096 ? 4096 : sigdsize;
2237 if (fread(lead, l, 1, fp) != 1)
2239 fprintf(stderr, "%s: unexpected EOF\n", name);
2244 if (fread(lead, 16, 1, fp) != 1)
2246 fprintf(stderr, "%s: unexpected EOF\n", name);
2249 if (getu32(lead) != 0x8eade801)
2251 fprintf(stderr, "%s: bad header\n", name);
2255 sigcnt = getu32(lead + 8);
2256 sigdsize = getu32(lead + 12);
2257 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2259 fprintf(stderr, "%s: bad header\n", name);
2263 l = sigdsize + sigcnt * 16;
2264 headerend = headerstart + 16 + l;
2265 if (l > state->rpmheadsize)
2267 state->rpmheadsize = l + 128;
2268 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2270 rpmhead = state->rpmhead;
2271 if (fread(rpmhead->data, l, 1, fp) != 1)
2273 fprintf(stderr, "%s: unexpected EOF\n", name);
2277 rpmhead->cnt = sigcnt;
2278 rpmhead->dcnt = sigdsize;
2279 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;