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>
30 #include <rpm/rpmio.h>
31 #include <rpm/rpmpgp.h>
32 #include <rpm/header.h>
40 #include "repo_rpmdb.h"
42 #define RPMDB_COOKIE_VERSION 2
45 #define TAG_VERSION 1001
46 #define TAG_RELEASE 1002
47 #define TAG_EPOCH 1003
48 #define TAG_SUMMARY 1004
49 #define TAG_DESCRIPTION 1005
50 #define TAG_BUILDTIME 1006
51 #define TAG_BUILDHOST 1007
52 #define TAG_INSTALLTIME 1008
54 #define TAG_DISTRIBUTION 1010
55 #define TAG_VENDOR 1011
56 #define TAG_LICENSE 1014
57 #define TAG_PACKAGER 1015
58 #define TAG_GROUP 1016
61 #define TAG_FILESIZES 1028
62 #define TAG_FILEMODES 1030
63 #define TAG_FILEMD5S 1035
64 #define TAG_FILELINKTOS 1036
65 #define TAG_SOURCERPM 1044
66 #define TAG_PROVIDENAME 1047
67 #define TAG_REQUIREFLAGS 1048
68 #define TAG_REQUIRENAME 1049
69 #define TAG_REQUIREVERSION 1050
70 #define TAG_NOSOURCE 1051
71 #define TAG_NOPATCH 1052
72 #define TAG_CONFLICTFLAGS 1053
73 #define TAG_CONFLICTNAME 1054
74 #define TAG_CONFLICTVERSION 1055
75 #define TAG_OBSOLETENAME 1090
76 #define TAG_FILEDEVICES 1095
77 #define TAG_FILEINODES 1096
78 #define TAG_PROVIDEFLAGS 1112
79 #define TAG_PROVIDEVERSION 1113
80 #define TAG_OBSOLETEFLAGS 1114
81 #define TAG_OBSOLETEVERSION 1115
82 #define TAG_DIRINDEXES 1116
83 #define TAG_BASENAMES 1117
84 #define TAG_DIRNAMES 1118
85 #define TAG_PAYLOADFORMAT 1124
86 #define TAG_PATCHESNAME 1133
87 #define TAG_FILECOLORS 1140
88 #define TAG_SUGGESTSNAME 1156
89 #define TAG_SUGGESTSVERSION 1157
90 #define TAG_SUGGESTSFLAGS 1158
91 #define TAG_ENHANCESNAME 1159
92 #define TAG_ENHANCESVERSION 1160
93 #define TAG_ENHANCESFLAGS 1161
95 #define SIGTAG_SIZE 1000
96 #define SIGTAG_PGP 1002 /* RSA signature */
97 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
98 #define SIGTAG_GPG 1005 /* DSA signature */
100 #define DEP_LESS (1 << 1)
101 #define DEP_GREATER (1 << 2)
102 #define DEP_EQUAL (1 << 3)
103 #define DEP_STRONG (1 << 27)
104 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
112 typedef struct rpmhead {
116 unsigned char data[1];
120 static inline unsigned char *
121 headfindtag(RpmHead *h, int tag)
124 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])
137 headexists(RpmHead *h, int tag)
139 return headfindtag(h, tag) ? 1 : 0;
142 static unsigned int *
143 headint32array(RpmHead *h, int tag, int *cnt)
145 unsigned int i, o, *r;
146 unsigned char *d = headfindtag(h, tag);
148 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
150 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
151 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
152 if (o + 4 * i > h->dcnt)
155 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
158 for (o = 0; o < i; o++, d += 4)
159 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
163 /* returns the first entry of an integer array */
165 headint32(RpmHead *h, int tag)
168 unsigned char *d = headfindtag(h, tag);
170 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
172 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
173 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
174 if (i == 0 || o + 4 * i > h->dcnt)
177 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
180 static unsigned int *
181 headint16array(RpmHead *h, int tag, int *cnt)
183 unsigned int i, o, *r;
184 unsigned char *d = headfindtag(h, tag);
186 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
188 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
189 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
190 if (o + 4 * i > h->dcnt)
193 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
196 for (o = 0; o < i; o++, d += 2)
197 r[o] = d[0] << 8 | d[1];
202 headstring(RpmHead *h, int tag)
205 unsigned char *d = headfindtag(h, tag);
206 /* 6: STRING, 9: I18NSTRING */
207 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
209 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
212 return (char *)h->dp + o;
216 headstringarray(RpmHead *h, int tag, int *cnt)
219 unsigned char *d = headfindtag(h, tag);
222 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
224 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
225 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
226 r = sat_calloc(i ? i : 1, sizeof(char *));
230 for (o = 0; o < i; o++)
234 d += strlen((char *)d) + 1;
235 if (d >= h->dp + h->dcnt)
244 static unsigned char *
245 headbinary(RpmHead *h, int tag, unsigned int *sizep)
248 unsigned char *d = headfindtag(h, tag);
249 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
251 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
252 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
253 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
260 static char *headtoevr(RpmHead *h)
267 version = headstring(h, TAG_VERSION);
268 release = headstring(h, TAG_RELEASE);
269 epoch = headint32(h, TAG_EPOCH);
270 if (!version || !release)
272 fprintf(stderr, "headtoevr: bad rpm header\n");
275 for (v = version; *v >= 0 && *v <= '9'; v++)
277 if (epoch || (v != version && *v == ':'))
279 char epochbuf[11]; /* 32bit decimal will fit in */
280 sprintf(epochbuf, "%u", epoch);
281 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
282 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
286 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
287 sprintf(evr, "%s-%s", version, release);
294 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
296 const unsigned char *cp;
299 unsigned char *buf = 0, *bp;
301 /* check if it's already utf8, code taken from screen ;-) */
302 cp = (const unsigned char *)str;
303 while ((c = *cp++) != 0)
307 if ((c & 0xc0) != 0x80)
308 break; /* encoding error */
309 c = (c & 0x3f) | (state << 6);
310 if (!(state & 0x40000000))
312 /* check for overlong sequences */
313 if ((c & 0x820823e0) == 0x80000000)
315 else if ((c & 0x020821f0) == 0x02000000)
317 else if ((c & 0x000820f8) == 0x00080000)
319 else if ((c & 0x0000207c) == 0x00002000)
329 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
331 c = (c & 0x03) | 0xbfffff00; /* 4 */
333 c = (c & 0x07) | 0xbfffc000; /* 3 */
335 c = (c & 0x0f) | 0xbff00000; /* 2 */
337 c = (c & 0x1f) | 0xfc000000; /* 1 */
341 state = (c & 0x80000000) ? c : 0;
345 /* not utf8, assume latin1 */
346 buf = sat_malloc(2 * strlen(str) + 1);
347 cp = (const unsigned char *)str;
350 while ((c = *cp++) != 0)
363 repodata_set_str(repodata, handle, tag, str);
369 #define MAKEDEPS_FILTER_WEAK (1 << 0)
370 #define MAKEDEPS_FILTER_STRONG (1 << 1)
371 #define MAKEDEPS_NO_RPMLIB (1 << 2)
374 * strong: 0: ignore strongness
375 * 1: filter to strong
379 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
383 int i, cc, nc, vc, fc;
385 unsigned int olddeps;
389 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
390 n = headstringarray(rpmhead, tagn, &nc);
393 v = headstringarray(rpmhead, tagv, &vc);
399 f = headint32array(rpmhead, tagf, &fc);
406 if (nc != vc || nc != fc)
408 fprintf(stderr, "bad dependency entries\n");
413 haspre = 0; /* add no prereq marker */
416 /* we do filtering */
418 for (i = 0; i < nc; i++)
420 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
422 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
423 if (!strncmp(n[i], "rpmlib(", 7))
425 if ((f[i] & DEP_PRE) != 0)
430 else if (tagn == TAG_REQUIRENAME)
432 /* no filtering, just look for the first prereq */
433 for (i = 0; i < nc; i++)
434 if ((f[i] & DEP_PRE) != 0)
448 olddeps = repo_reserve_ids(repo, 0, cc);
449 ida = repo->idarraydata + olddeps;
456 haspre = 2; /* pass two: prereqs */
458 *ida++ = SOLVABLE_PREREQMARKER;
460 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
462 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
464 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
466 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
467 if (!strncmp(n[i], "rpmlib(", 7))
469 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
473 if ((f[i] & DEP_LESS) != 0)
475 if ((f[i] & DEP_EQUAL) != 0)
477 if ((f[i] & DEP_GREATER) != 0)
479 name = str2id(pool, n[i], 1);
480 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
481 evr = str2id(pool, v[i] + 2, 1);
483 evr = str2id(pool, v[i], 1);
484 *ida++ = rel2id(pool, name, evr, flags, 1);
487 *ida++ = str2id(pool, n[i], 1);
490 repo->idarraysize += cc + 1;
498 #ifdef USE_FILEFILTER
500 #define FILEFILTER_EXACT 0
501 #define FILEFILTER_STARTS 1
502 #define FILEFILTER_CONTAINS 2
510 static struct filefilter filefilters[] = {
511 { FILEFILTER_CONTAINS, "/bin/", 0},
512 { FILEFILTER_CONTAINS, "/sbin/", 0},
513 { FILEFILTER_CONTAINS, "/lib/", 0},
514 { FILEFILTER_CONTAINS, "/lib64/", 0},
515 { FILEFILTER_CONTAINS, "/etc/", 0},
516 { FILEFILTER_STARTS, "/usr/games/", 0},
517 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
518 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
519 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
525 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
529 unsigned int *fkb, *fn, *fsz, *fm, *fino;
530 unsigned int inotest[256], inotestok;
534 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
535 if (!fsz || fc != fszc)
540 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
541 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
542 if (!fm || fc != fszc)
548 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
549 if (!fino || fc != fszc)
557 if (fc < sizeof(inotest))
559 memset(inotest, 0, sizeof(inotest));
560 for (i = 0; i < fc; i++)
563 if (fsz[i] == 0 || !S_ISREG(fm[i]))
565 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
566 bit = 1 << (fino[i] & 31);
567 if ((inotest[off] & bit) != 0)
576 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
578 unsigned int mask, hash, hh;
579 if (!fdev || fc != fszc)
588 while ((mask & (mask - 1)) != 0)
589 mask = mask & (mask - 1);
591 if (mask > sizeof(inotest)/sizeof(*inotest))
592 fx = sat_calloc(mask, sizeof(unsigned int));
596 memset(fx, 0, mask * sizeof(unsigned int));
599 for (i = 0; i < fc; i++)
601 if (fsz[i] == 0 || !S_ISREG(fm[i]))
603 hash = (fino[i] + fdev[i] * 31) & mask;
605 while ((j = fx[hash]) != 0)
607 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
609 fsz[i] = 0; /* kill entry */
612 hash = (hash + hh++) & mask;
622 fn = sat_calloc(dic, sizeof(unsigned int));
623 fkb = sat_calloc(dic, sizeof(unsigned int));
624 for (i = 0; i < fc; i++)
626 if (fsz[i] == 0 || !S_ISREG(fm[i]))
631 fkb[di[i]] += fsz[i] / 1024 + 1;
636 handle = s - pool->solvables;
637 for (i = 0; i < fc; i++)
641 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
642 did = repodata_str2dir(data, "/usr/src", 1);
644 did = repodata_str2dir(data, dn[i], 1);
645 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
651 /* assumes last processed array is provides! */
653 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
660 #ifdef USE_FILEFILTER
662 struct filefilter *ff;
671 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
674 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
680 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
689 fprintf(stderr, "bad filelist\n");
694 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
696 for (i = 0; i < bnc; i++)
698 #ifdef USE_FILEFILTER
700 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
704 switch (ff->dirmatch)
706 case FILEFILTER_STARTS:
707 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
710 case FILEFILTER_CONTAINS:
711 if (!strstr(dn[di[i]], ff->dir))
714 case FILEFILTER_EXACT:
716 if (strcmp(dn[di[i]], ff->dir))
723 if (strcmp(bn[i], ff->base))
728 if (j == sizeof(filefilters)/sizeof(*filefilters))
732 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
736 fn = sat_realloc(fn, fna);
738 strcpy(fn, dn[di[i]]);
740 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
745 handle = s - pool->solvables;
746 did = repodata_str2dir(data, dn[di[i]], 1);
748 did = repodata_str2dir(data, "/", 1);
749 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
763 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
765 const char *p, *sevr, *sarch;
767 p = strrchr(sourcerpm, '.');
768 if (!p || strcmp(p, ".rpm") != 0)
771 while (p > sourcerpm && *p != '.')
773 if (*p != '.' || p == sourcerpm)
776 while (p > sourcerpm && *p != '-')
778 if (*p != '-' || p == sourcerpm)
781 while (p > sourcerpm && *p != '-')
783 if (*p != '-' || p == sourcerpm)
786 if (!strcmp(sarch, "src.rpm"))
787 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
788 else if (!strcmp(sarch, "nosrc.rpm"))
789 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
791 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
792 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
793 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
795 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
796 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
797 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
799 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
803 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
809 name = headstring(rpmhead, TAG_NAME);
810 if (!strcmp(name, "gpg-pubkey"))
812 s->name = str2id(pool, name, 1);
815 fprintf(stderr, "package has no name\n");
818 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
820 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
823 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
824 s->arch = ARCH_NOSRC;
829 s->arch = ARCH_NOARCH;
830 evr = headtoevr(rpmhead);
831 s->evr = str2id(pool, evr, 1);
832 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
834 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
835 if ((flags & RPM_ADD_NO_FILELIST) == 0)
836 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
837 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
838 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
839 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
840 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
841 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
843 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
844 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
845 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
846 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
847 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
848 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
856 handle = s - pool->solvables;
857 str = headstring(rpmhead, TAG_SUMMARY);
859 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
860 str = headstring(rpmhead, TAG_DESCRIPTION);
864 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
865 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
869 /* oh my, found SUSE special author section */
874 while (l > 0 && str[l - 1] == '\n')
877 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
879 aut = str; /* copy over */
880 while (*p == ' ' || *p == '\n')
893 while (aut != str && aut[-1] == '\n')
897 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
901 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
903 str = headstring(rpmhead, TAG_GROUP);
905 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
906 str = headstring(rpmhead, TAG_LICENSE);
908 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
909 str = headstring(rpmhead, TAG_URL);
911 repodata_set_str(data, handle, SOLVABLE_URL, str);
912 str = headstring(rpmhead, TAG_DISTRIBUTION);
914 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
915 str = headstring(rpmhead, TAG_PACKAGER);
917 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
918 u32 = headint32(rpmhead, TAG_BUILDTIME);
920 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
921 u32 = headint32(rpmhead, TAG_INSTALLTIME);
923 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
924 u32 = headint32(rpmhead, TAG_SIZE);
926 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
928 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
935 copyreldep(Pool *pool, Pool *frompool, Id id)
937 Reldep *rd = GETRELDEP(frompool, id);
938 Id name = rd->name, evr = rd->evr;
940 name = copyreldep(pool, frompool, name);
942 name = str2id(pool, id2str(frompool, name), 1);
944 evr = copyreldep(pool, frompool, evr);
946 evr = str2id(pool, id2str(frompool, evr), 1);
947 return rel2id(pool, name, evr, rd->flags, 1);
951 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
956 Pool *frompool = fromrepo->pool;
960 from = fromrepo->idarraydata + fromoff;
961 for (ida = from, cc = 0; *ida; ida++, cc++)
965 ido = repo_reserve_ids(repo, 0, cc);
966 ida = repo->idarraydata + ido;
967 if (frompool && pool != frompool)
973 id = copyreldep(pool, frompool, id);
975 id = str2id(pool, id2str(frompool, id), 1);
981 memcpy(ida, from, (cc + 1) * sizeof(Id));
982 repo->idarraysize += cc + 1;
986 #define COPYDIR_DIRCACHE_SIZE 512
988 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
991 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
993 if (cache && cache[did & 255] == did)
994 return cache[(did & 255) + 256];
995 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
999 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1001 Id parent = dirpool_parent(&fromdata->dirpool, did);
1002 Id compid = dirpool_compid(&fromdata->dirpool, did);
1004 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1005 if (fromspool != &pool->ss)
1006 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1007 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1010 cache[did & 255] = did;
1011 cache[(did & 255) + 256] = compid;
1016 struct solvable_copy_cbdata {
1023 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1025 struct solvable_copy_cbdata *cbdata = vcbdata;
1027 Repodata *data = cbdata->data;
1028 Id handle = cbdata->handle;
1029 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1030 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1032 keyname = key->name;
1033 if (keyname >= ID_NUM_INTERNAL)
1034 keyname = str2id(pool, id2str(frompool, keyname), 1);
1037 case REPOKEY_TYPE_ID:
1038 case REPOKEY_TYPE_CONSTANTID:
1040 assert(!data->localpool); /* implement me! */
1041 if (pool != frompool || fromdata->localpool)
1044 id = copyreldep(pool, frompool, id);
1046 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1048 if (key->type == REPOKEY_TYPE_ID)
1049 repodata_set_id(data, handle, keyname, id);
1051 repodata_set_constantid(data, handle, keyname, id);
1053 case REPOKEY_TYPE_STR:
1054 repodata_set_str(data, handle, keyname, kv->str);
1056 case REPOKEY_TYPE_VOID:
1057 repodata_set_void(data, handle, keyname);
1059 case REPOKEY_TYPE_NUM:
1060 repodata_set_num(data, handle, keyname, kv->num);
1062 case REPOKEY_TYPE_CONSTANT:
1063 repodata_set_constant(data, handle, keyname, kv->num);
1065 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1067 assert(!data->localpool); /* implement me! */
1068 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1069 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1071 case REPOKEY_TYPE_DIRSTRARRAY:
1073 assert(!data->localpool); /* implement me! */
1074 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1075 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1084 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1086 Repo *repo = s->repo;
1087 Repo *fromrepo = r->repo;
1088 Pool *pool = repo->pool;
1089 struct solvable_copy_cbdata cbdata;
1091 /* copy solvable data */
1092 if (pool == fromrepo->pool)
1097 s->vendor = r->vendor;
1102 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1104 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1106 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1108 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1110 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1111 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1112 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1113 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1114 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1115 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1116 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1117 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1119 /* copy all attributes */
1123 cbdata.handle = s - pool->solvables;
1124 cbdata.dircache = dircache;
1125 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1128 /* used to sort entries returned in some database order */
1130 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1132 struct rpmid const *a = va, *b = vb;
1134 r = strcmp(a->name, b->name);
1137 return a->dbid - b->dbid;
1141 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1144 Pool *pool = repo->pool;
1145 Solvable *a = pool->solvables + *(Id *)va;
1146 Solvable *b = pool->solvables + *(Id *)vb;
1149 if (a->name != b->name)
1150 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1151 rpmdbid = repo->rpmdbid;
1152 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1156 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1158 Pool *pool = repo->pool;
1161 tmp = pool->solvables[pa];
1162 pool->solvables[pa] = pool->solvables[pb];
1163 pool->solvables[pb] = tmp;
1166 Id tmpid = repo->rpmdbid[pa - repo->start];
1167 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1168 repo->rpmdbid[pb - repo->start] = tmpid;
1170 /* only works if nothing is already internalized! */
1171 if (data && data->attrs)
1173 Id *tmpattrs = data->attrs[pa - data->start];
1174 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1175 data->attrs[pb - data->start] = tmpattrs;
1180 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1182 memset(cookie, 0, 32);
1183 cookie[3] = RPMDB_COOKIE_VERSION;
1184 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1185 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1188 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1190 opendbenv(const char *rootdir)
1192 char dbpath[PATH_MAX];
1196 if (db_env_create(&dbenv, 0))
1198 perror("db_env_create");
1201 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1202 if (access(dbpath, W_OK) == -1)
1204 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1209 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1211 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1216 perror("dbenv open");
1217 dbenv->close(dbenv, 0);
1225 count_headers(const char *rootdir, DB_ENV *dbenv)
1227 char dbpath[PATH_MAX];
1228 struct stat statbuf;
1236 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1237 if (stat(dbpath, &statbuf))
1239 memset(&dbkey, 0, sizeof(dbkey));
1240 memset(&dbdata, 0, sizeof(dbdata));
1241 if (db_create(&db, dbenv, 0))
1243 perror("db_create");
1246 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1248 perror("db->open Name index");
1251 if (db->get_byteswapped(db, &byteswapped))
1253 perror("db->get_byteswapped");
1256 if (db->cursor(db, NULL, &dbc, 0))
1258 perror("db->cursor");
1261 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1262 count += dbdata.size >> 3;
1269 * read rpm db as repo
1274 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1276 Pool *pool = repo->pool;
1277 unsigned char buf[16];
1282 unsigned char *dp, *dbidp;
1284 struct rpmid *rpmids, *rp;
1290 unsigned int refmask, h;
1291 char dbpath[PATH_MAX];
1295 struct stat packagesstat;
1296 unsigned char newcookie[32];
1297 const unsigned char *oldcookie = 0;
1298 Id oldcookietype = 0;
1300 int count = 0, done = 0;
1303 now = sat_timems(0);
1304 memset(&dbkey, 0, sizeof(dbkey));
1305 memset(&dbdata, 0, sizeof(dbdata));
1310 data = repo_add_repodata(repo, flags);
1312 if (ref && !(ref->nsolvables && ref->rpmdbid))
1315 if (!(dbenv = opendbenv(rootdir)))
1318 /* XXX: should get ro lock of Packages database! */
1319 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1320 if (stat(dbpath, &packagesstat))
1325 mkrpmdbcookie(&packagesstat, newcookie);
1326 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1329 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1330 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1333 int solvstart = 0, solvend = 0;
1335 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1336 count = count_headers(rootdir, dbenv);
1337 if (db_create(&db, dbenv, 0))
1339 perror("db_create");
1342 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1344 perror("db->open Packages index");
1347 if (db->get_byteswapped(db, &byteswapped))
1349 perror("db->get_byteswapped");
1352 if (db->cursor(db, NULL, &dbc, 0))
1354 perror("db->cursor");
1357 dbidp = (unsigned char *)&dbid;
1362 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1366 s = pool_id2solvable(pool, repo_add_solvable(repo));
1368 solvstart = s - pool->solvables;
1369 solvend = s - pool->solvables + 1;
1372 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1373 if (dbkey.size != 4)
1375 fprintf(stderr, "corrupt Packages database (key size)\n");
1387 memcpy(dbidp, dp, 4);
1388 if (dbid == 0) /* the join key */
1390 if (dbdata.size < 8)
1392 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1395 if (dbdata.size > rpmheadsize)
1397 rpmheadsize = dbdata.size + 128;
1398 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1400 memcpy(buf, dbdata.data, 8);
1401 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1402 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1403 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1405 fprintf(stderr, "corrupt rpm database (data size)\n");
1408 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1409 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1410 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1411 if (rpm2solv(pool, repo, data, s, rpmhead, flags))
1418 /* We can reuse this solvable, but make sure it's still
1419 associated with this repo. */
1420 memset(s, 0, sizeof(*s));
1423 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1427 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1428 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1433 /* oops, could not reuse. free it instead */
1434 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1441 /* now sort all solvables in the new solvstart..solvend block */
1442 if (solvend - solvstart > 1)
1444 pkgids = sat_malloc2(solvend - solvstart, sizeof(Id));
1445 for (i = solvstart; i < solvend; i++)
1446 pkgids[i - solvstart] = i;
1447 sat_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1449 for (i = solvstart; i < solvend; i++)
1451 int j = pkgids[i - solvstart];
1453 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1455 swap_solvables(repo, data, i, j);
1462 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1464 memset(dircache, 0, sizeof(dircache));
1465 if (db_create(&db, dbenv, 0))
1467 perror("db_create");
1470 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1472 perror("db->open Name index");
1475 if (db->get_byteswapped(db, &byteswapped))
1477 perror("db->get_byteswapped");
1480 if (db->cursor(db, NULL, &dbc, 0))
1482 perror("db->cursor");
1485 dbidp = (unsigned char *)&dbid;
1488 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1490 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1504 memcpy(dbidp, dp, 4);
1505 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1506 rpmids[nrpmids].dbid = dbid;
1507 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1508 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1509 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1520 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1522 dbidp = (unsigned char *)&dbid;
1526 /* create hash from dbid to ref */
1527 refmask = mkmask(ref->nsolvables);
1528 refhash = sat_calloc(refmask + 1, sizeof(Id));
1529 for (i = 0; i < ref->end - ref->start; i++)
1531 if (!ref->rpmdbid[i])
1533 h = ref->rpmdbid[i] & refmask;
1535 h = (h + 317) & refmask;
1536 refhash[h] = i + 1; /* make it non-zero */
1539 /* count the misses, they will cost us time */
1540 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1542 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1548 while ((id = refhash[h]))
1550 if (ref->rpmdbid[id - 1] == dbid)
1552 h = (h + 317) & refmask;
1561 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1563 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1565 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1568 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1572 while ((id = refhash[h]))
1574 if (ref->rpmdbid[id - 1] == dbid)
1576 h = (h + 317) & refmask;
1580 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1583 solvable_copy(s, r, data, dircache);
1590 if (db_create(&db, dbenv, 0))
1592 perror("db_create");
1595 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1597 perror("db->open var/lib/rpm/Packages");
1600 if (db->get_byteswapped(db, &byteswapped))
1602 perror("db->get_byteswapped");
1614 memcpy(buf, dbidp, 4);
1619 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1622 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1623 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1626 if (dbdata.size < 8)
1628 fprintf(stderr, "corrupt rpm database (size)\n");
1631 if (dbdata.size > rpmheadsize)
1633 rpmheadsize = dbdata.size + 128;
1634 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1636 memcpy(buf, dbdata.data, 8);
1637 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1638 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1639 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1641 fprintf(stderr, "corrupt rpm database (data size)\n");
1644 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1645 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1647 rpm2solv(pool, repo, data, s, rpmhead, flags);
1648 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1652 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1653 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1661 for (i = 0; i < nrpmids; i++)
1662 sat_free(rpmids[i].name);
1668 dbenv->close(dbenv, 0);
1671 if (!(flags & REPO_NO_INTERNALIZE))
1672 repodata_internalize(data);
1673 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1674 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1675 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1676 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1677 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1681 static inline unsigned int
1682 getu32(const unsigned char *dp)
1684 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1689 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1691 int i, sigdsize, sigcnt, l;
1692 Pool *pool = repo->pool;
1694 RpmHead *rpmhead = 0;
1695 int rpmheadsize = 0;
1696 char *payloadformat;
1698 unsigned char lead[4096];
1699 int headerstart, headerend;
1702 unsigned char pkgid[16];
1707 data = repo_add_repodata(repo, flags);
1709 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1710 chksumtype = REPOKEY_TYPE_SHA256;
1711 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1712 chksumtype = REPOKEY_TYPE_SHA1;
1713 for (i = 0; i < nrpms; i++)
1715 if ((fp = fopen(rpms[i], "r")) == 0)
1720 if (fstat(fileno(fp), &stb))
1726 chksumh = sat_chksum_free(chksumh, 0);
1728 chksumh = sat_chksum_create(chksumtype);
1729 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1731 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1736 sat_chksum_add(chksumh, lead, 96 + 16);
1737 if (lead[78] != 0 || lead[79] != 5)
1739 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1743 if (getu32(lead + 96) != 0x8eade801)
1745 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1749 sigcnt = getu32(lead + 96 + 8);
1750 sigdsize = getu32(lead + 96 + 12);
1751 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1753 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1757 sigdsize += sigcnt * 16;
1758 sigdsize = (sigdsize + 7) & ~7;
1759 headerstart = 96 + 16 + sigdsize;
1761 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1763 unsigned char *chksum;
1764 unsigned int chksumsize;
1765 /* extract pkgid from the signature header */
1766 if (sigdsize > rpmheadsize)
1768 rpmheadsize = sigdsize + 128;
1769 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1771 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1773 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1778 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1779 rpmhead->cnt = sigcnt;
1780 rpmhead->dcnt = sigdsize - sigcnt * 16;
1781 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1782 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1783 if (chksum && chksumsize == 16)
1786 memcpy(pkgid, chksum, 16);
1791 /* just skip the signature header */
1794 l = sigdsize > 4096 ? 4096 : sigdsize;
1795 if (fread(lead, l, 1, fp) != 1)
1797 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1802 sat_chksum_add(chksumh, lead, l);
1806 if (fread(lead, 16, 1, fp) != 1)
1808 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1813 sat_chksum_add(chksumh, lead, 16);
1814 if (getu32(lead) != 0x8eade801)
1816 fprintf(stderr, "%s: bad header\n", rpms[i]);
1820 sigcnt = getu32(lead + 8);
1821 sigdsize = getu32(lead + 12);
1822 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1824 fprintf(stderr, "%s: bad header\n", rpms[i]);
1828 l = sigdsize + sigcnt * 16;
1829 headerend = headerstart + 16 + l;
1830 if (l > rpmheadsize)
1832 rpmheadsize = l + 128;
1833 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1835 if (fread(rpmhead->data, l, 1, fp) != 1)
1837 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1842 sat_chksum_add(chksumh, rpmhead->data, l);
1843 rpmhead->cnt = sigcnt;
1844 rpmhead->dcnt = sigdsize;
1845 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1846 if (headexists(rpmhead, TAG_PATCHESNAME))
1848 /* this is a patch rpm, ignore */
1852 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1853 if (payloadformat && !strcmp(payloadformat, "drpm"))
1855 /* this is a delta rpm */
1860 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1861 sat_chksum_add(chksumh, lead, l);
1863 s = pool_id2solvable(pool, repo_add_solvable(repo));
1864 rpm2solv(pool, repo, data, s, rpmhead, flags);
1867 Id handle = s - pool->solvables;
1868 repodata_set_location(data, handle, 0, 0, rpms[i]);
1869 if (S_ISREG(stb.st_mode))
1870 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1871 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1873 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1875 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1879 chksumh = sat_chksum_free(chksumh, 0);
1882 if (!(flags & REPO_NO_INTERNALIZE))
1883 repodata_internalize(data);
1887 linkhash(const char *lt, char *hash)
1890 const unsigned char *str = (const unsigned char *)lt;
1894 while ((c = *str++) != 0)
1896 sprintf(hash, "%08x", r);
1897 sprintf(hash + 8, "%08x", l);
1898 sprintf(hash + 16, "%08x", 0);
1899 sprintf(hash + 24, "%08x", 0);
1903 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1905 RpmHead *rpmhead = rpmhandle;
1910 unsigned int *di, diidx;
1911 unsigned int lastdir;
1914 int cnt, dcnt, cnt2;
1918 char md5[33], *md5p = 0;
1920 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1923 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1925 for (i = 0; i < dcnt; i++)
1926 (*cb)(cbdata, dn[i], 0, (char *)0);
1930 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1936 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1937 if (!di || cnt != cnt2)
1944 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1945 if (!fm || cnt != cnt2)
1953 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1955 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1956 if (!md || cnt != cnt2)
1968 for (i = 0; i < cnt; i++)
1973 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
1976 l = l1 + strlen(bn[i]) + 1;
1980 space = sat_realloc(space, spacen);
1982 if (lastdir != diidx)
1984 strcpy(space, dn[diidx]);
1988 strcpy(space + l1, bn[i]);
1997 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2003 linkhash(lt[i], md5);
2010 (*cb)(cbdata, space, fm[i], md5p);
2022 rpm_query(void *rpmhandle, Id what)
2024 const char *name, *arch, *sourcerpm;
2028 RpmHead *rpmhead = rpmhandle;
2033 name = headstring(rpmhead, TAG_NAME);
2036 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2038 arch = headstring(rpmhead, TAG_ARCH);
2041 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2048 evr = headtoevr(rpmhead);
2049 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2051 sprintf(r, "%s-%s.%s", name, evr, arch);
2055 name = headstring(rpmhead, TAG_NAME);
2059 r = headtoevr(rpmhead);
2066 struct rpm_by_state {
2081 #define ENTRIES_BLOCK 255
2082 #define NAMEDATA_BLOCK 1023
2084 #define FLAGS_GET_PUBKEYS 1
2086 static struct rpmdbentry *
2087 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap, int flags)
2101 struct rpmdbentry *entries = 0;
2107 dbenv = state->dbenv;
2108 if (db_create(&db, dbenv, 0))
2110 perror("db_create");
2113 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2115 perror("db->open index");
2119 if (db->get_byteswapped(db, &byteswapped))
2121 perror("db->get_byteswapped");
2125 if (db->cursor(db, NULL, &dbc, 0))
2127 perror("db->cursor");
2131 memset(&dbkey, 0, sizeof(dbkey));
2132 memset(&dbdata, 0, sizeof(dbdata));
2135 dbkey.data = (void *)match;
2136 dbkey.size = strlen(match);
2138 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2142 if ((flags & FLAGS_GET_PUBKEYS))
2144 if (dbkey.size != 10 || memcmp(dbkey.data, "gpg-pubkey", 10))
2149 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2159 ((char *)&rpmdbid)[0] = dp[3];
2160 ((char *)&rpmdbid)[1] = dp[2];
2161 ((char *)&rpmdbid)[2] = dp[1];
2162 ((char *)&rpmdbid)[3] = dp[0];
2165 memcpy((char *)&rpmdbid, dp, 4);
2166 entries = sat_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2167 entries[nentries].rpmdbid = rpmdbid;
2168 entries[nentries].nameoff = namedatal;
2170 namedata = sat_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2171 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2172 namedata[namedatal + dbkey.size] = 0;
2173 namedatal += dbkey.size + 1;
2182 *nentriesp = nentries;
2183 *namedatap = namedata;
2188 rpm_installedrpmdbids(const char *rootdir, Queue *rpmdbidq)
2190 struct rpm_by_state state;
2191 struct rpmdbentry *entries;
2196 queue_empty(rpmdbidq);
2197 memset(&state, 0, sizeof(state));
2198 if (!(state.dbenv = opendbenv(rootdir)))
2200 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata, 0);
2202 for (i = 0; i < nentries; i++)
2203 queue_push(rpmdbidq, entries[i].rpmdbid);
2206 rpm_byrpmdbid(0, 0, (void **)&state);
2211 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2213 struct rpm_by_state *state = *statep;
2214 unsigned char buf[16];
2225 state->db->close(state->db, 0);
2227 state->dbenv->close(state->dbenv, 0);
2228 sat_free(state->rpmhead);
2230 *statep = (void *)0;
2236 state = sat_calloc(1, sizeof(*state));
2239 if (!state->dbopened)
2241 state->dbopened = 1;
2242 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2244 if (db_create(&state->db, state->dbenv, 0))
2246 perror("db_create");
2248 state->dbenv->close(state->dbenv, 0);
2252 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2254 perror("db->open var/lib/rpm/Packages");
2255 state->db->close(state->db, 0);
2257 state->dbenv->close(state->dbenv, 0);
2261 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2263 perror("db->get_byteswapped");
2264 state->db->close(state->db, 0);
2266 state->dbenv->close(state->dbenv, 0);
2271 memcpy(buf, &rpmdbid, 4);
2272 if (state->byteswapped)
2275 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2276 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2278 memset(&dbkey, 0, sizeof(dbkey));
2279 memset(&dbdata, 0, sizeof(dbdata));
2284 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2289 if (dbdata.size < 8)
2291 fprintf(stderr, "corrupt rpm database (size)\n");
2294 if (dbdata.size > state->rpmheadsize)
2296 state->rpmheadsize = dbdata.size + 128;
2297 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2299 rpmhead = state->rpmhead;
2300 memcpy(buf, dbdata.data, 8);
2301 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2302 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2303 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2305 fprintf(stderr, "corrupt rpm database (data size)\n");
2308 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2309 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2314 rpm_byfp(FILE *fp, const char *name, void **statep)
2316 struct rpm_by_state *state = *statep;
2317 int headerstart, headerend;
2319 int sigdsize, sigcnt, l;
2320 unsigned char lead[4096];
2323 return rpm_byrpmdbid(0, 0, statep);
2326 state = sat_calloc(1, sizeof(*state));
2329 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2331 fprintf(stderr, "%s: not a rpm\n", name);
2334 if (lead[78] != 0 || lead[79] != 5)
2336 fprintf(stderr, "%s: not a V5 header\n", name);
2339 if (getu32(lead + 96) != 0x8eade801)
2341 fprintf(stderr, "%s: bad signature header\n", name);
2344 sigcnt = getu32(lead + 96 + 8);
2345 sigdsize = getu32(lead + 96 + 12);
2346 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2348 fprintf(stderr, "%s: bad signature header\n", name);
2351 sigdsize += sigcnt * 16;
2352 sigdsize = (sigdsize + 7) & ~7;
2353 headerstart = 96 + 16 + sigdsize;
2356 l = sigdsize > 4096 ? 4096 : sigdsize;
2357 if (fread(lead, l, 1, fp) != 1)
2359 fprintf(stderr, "%s: unexpected EOF\n", name);
2364 if (fread(lead, 16, 1, fp) != 1)
2366 fprintf(stderr, "%s: unexpected EOF\n", name);
2369 if (getu32(lead) != 0x8eade801)
2371 fprintf(stderr, "%s: bad header\n", name);
2375 sigcnt = getu32(lead + 8);
2376 sigdsize = getu32(lead + 12);
2377 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2379 fprintf(stderr, "%s: bad header\n", name);
2383 l = sigdsize + sigcnt * 16;
2384 headerend = headerstart + 16 + l;
2385 if (l > state->rpmheadsize)
2387 state->rpmheadsize = l + 128;
2388 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2390 rpmhead = state->rpmhead;
2391 if (fread(rpmhead->data, l, 1, fp) != 1)
2393 fprintf(stderr, "%s: unexpected EOF\n", name);
2397 rpmhead->cnt = sigcnt;
2398 rpmhead->dcnt = sigdsize;
2399 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2404 rpm_byrpmh(Header h, void **statep)
2406 struct rpm_by_state *state = *statep;
2407 const unsigned char *uh;
2408 int sigdsize, sigcnt, l;
2411 uh = headerUnload(h);
2414 sigcnt = getu32(uh);
2415 sigdsize = getu32(uh + 4);
2416 l = sigdsize + sigcnt * 16;
2419 state = sat_calloc(1, sizeof(*state));
2422 if (l > state->rpmheadsize)
2424 state->rpmheadsize = l + 128;
2425 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2427 rpmhead = state->rpmhead;
2428 memcpy(rpmhead->data, uh + 8, l - 8);
2430 rpmhead->cnt = sigcnt;
2431 rpmhead->dcnt = sigdsize;
2432 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2438 r64dec1(char *p, unsigned int *vp, int *eofp)
2443 for (i = 0; i < 4; )
2448 if (x >= 'A' && x <= 'Z')
2450 else if (x >= 'a' && x <= 'z')
2452 else if (x >= '0' && x <= '9')
2479 crc24(unsigned char *p, int len)
2481 unsigned int crc = 0xb704ceL;
2486 crc ^= (*p++) << 16;
2487 for (i = 0; i < 8; i++)
2488 if ((crc <<= 1) & 0x1000000)
2491 return crc & 0xffffffL;
2494 static unsigned char *
2495 unarmor(char *pubkey, int *pktlp)
2499 unsigned char *buf, *bp;
2503 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2505 pubkey = strchr(pubkey, '\n');
2510 pubkey = strchr(pubkey, '\n');
2513 /* skip header lines */
2516 while (*pubkey == ' ' || *pubkey == '\t')
2518 if (*pubkey == '\n')
2520 pubkey = strchr(pubkey, '\n');
2525 p = strchr(pubkey, '=');
2529 bp = buf = sat_malloc(l * 3 / 4 + 4);
2533 pubkey = r64dec1(pubkey, &v, &eof);
2543 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2546 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2551 if (v != crc24(buf, bp - buf))
2556 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2558 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2568 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2571 unsigned char keyid[8];
2572 unsigned int kcr = 0, maxex = 0;
2573 unsigned char *pubkey = 0;
2575 unsigned char *userid = 0;
2578 for (; pl; p += l, pl -= l)
2582 if (!(x & 128) || pl <= 0)
2587 tag = (x & 0x3c) >> 2;
2609 else if (x >= 192 && x < 224)
2613 l = ((x - 192) << 8) + *p++ + 192;
2620 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2631 pubkey = sat_realloc(pubkey, l);
2633 memcpy(pubkey, p, l);
2640 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2644 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2648 memset(keyid, 0, 8);
2649 if (p[7] == 1) /* RSA */
2652 unsigned char fp[16];
2656 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2657 memcpy(keyid, p + 10 + ql - 8, 8);
2658 h = sat_chksum_create(REPOKEY_TYPE_MD5);
2659 sat_chksum_add(h, p + 10, ql);
2661 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2662 sat_chksum_add(h, q + 2, ql);
2663 sat_chksum_free(h, fp);
2664 for (i = 0; i < 16; i++)
2665 sprintf(fpx + i * 2, "%02x", fp[i]);
2666 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2673 unsigned char hdr[3];
2674 unsigned char fp[20];
2677 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2681 h = sat_chksum_create(REPOKEY_TYPE_SHA1);
2682 sat_chksum_add(h, hdr, 3);
2683 sat_chksum_add(h, p, l);
2684 sat_chksum_free(h, fp);
2685 for (i = 0; i < 20; i++)
2686 sprintf(fpx + i * 2, "%02x", fp[i]);
2687 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2688 memcpy(keyid, fp + 12, 8);
2693 if (p[0] == 3 && p[1] == 5)
2698 // printf("V3 signature packet\n");
2699 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2701 if (!memcmp(keyid, p + 6, 8))
2703 // printf("SELF SIG\n");
2707 // printf("OTHER SIG\n");
2711 htype = REPOKEY_TYPE_MD5;
2712 else if (p[16] == 2)
2713 htype = REPOKEY_TYPE_SHA1;
2714 else if (p[16] == 8)
2715 htype = REPOKEY_TYPE_SHA256;
2718 void *h = sat_chksum_create(htype);
2719 unsigned char b[3], *cs;
2722 b[1] = pubkeyl >> 8;
2724 sat_chksum_add(h, b, 3);
2725 sat_chksum_add(h, pubkey, pubkeyl);
2726 if (p[2] >= 0x10 && p[2] <= 0x13)
2727 sat_chksum_add(h, userid, useridl);
2728 sat_chksum_add(h, p + 2, 5);
2729 cs = sat_chksum_get(h, 0);
2730 sat_chksum_free(h, 0);
2736 int j, ql, haveissuer;
2738 unsigned int ex = 0, scr = 0;
2739 unsigned char issuer[8];
2741 // printf("V4 signature packet\n");
2742 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2747 for (j = 0; q && j < 2; j++)
2749 ql = q[0] << 8 | q[1];
2765 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2776 sl = ((x - 192) << 8) + *q++ + 192;
2785 // printf("%d SIGSUB %d %d\n", j, x, sl);
2786 if (x == 16 && sl == 9 && !haveissuer)
2788 memcpy(issuer, q + 1, 8);
2791 if (x == 2 && j == 0)
2792 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2793 if (x == 9 && j == 0)
2794 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2806 htype = REPOKEY_TYPE_MD5;
2808 htype = REPOKEY_TYPE_SHA1;
2810 htype = REPOKEY_TYPE_SHA256;
2811 if (htype && pubkeyl)
2813 void *h = sat_chksum_create(htype);
2814 unsigned char b[6], *cs;
2818 b[1] = pubkeyl >> 8;
2820 sat_chksum_add(h, b, 3);
2821 sat_chksum_add(h, pubkey, pubkeyl);
2822 if (p[1] >= 0x10 && p[1] <= 0x13)
2825 b[1] = useridl >> 24;
2826 b[2] = useridl >> 16;
2827 b[3] = useridl >> 8;
2829 sat_chksum_add(h, b, 5);
2830 sat_chksum_add(h, userid, useridl);
2832 hl = 6 + (p[4] << 8 | p[5]);
2833 sat_chksum_add(h, p, hl);
2840 sat_chksum_add(h, b, 6);
2841 cs = sat_chksum_get(h, 0);
2842 sat_chksum_free(h, 0);
2845 if (!memcmp(keyid, issuer, 8))
2847 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2853 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2860 userid = sat_realloc(userid, l);
2862 memcpy(userid, p, l);
2867 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2872 /* this is private to rpm, but rpm lacks an interface to retrieve
2873 * the values. Sigh. */
2874 struct pgpDigParams_s {
2875 const char * userid;
2876 const unsigned char * hash;
2877 const char * params[4];
2879 unsigned char version; /*!< version number. */
2880 unsigned char time[4]; /*!< time that the key was created. */
2881 unsigned char pubkey_algo; /*!< public key algorithm. */
2882 unsigned char hash_algo;
2883 unsigned char sigtype;
2884 unsigned char hashlen;
2885 unsigned char signhash16[2];
2886 unsigned char signid[8];
2887 unsigned char saved;
2891 struct pgpDigParams_s signature;
2892 struct pgpDigParams_s pubkey;
2896 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
2898 Pool *pool = s->repo->pool;
2899 unsigned char *pkts;
2904 char evrbuf[8 + 1 + 8 + 1];
2906 pkts = unarmor(pubkey, &pktsl);
2909 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
2910 parsekeydata(s, data, pkts, pktsl);
2912 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
2913 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
2914 sprintf(evrbuf, "%02x%02x%02x%02x-%02x%02x%02x%02x", dig->pubkey.signid[4], dig->pubkey.signid[5], dig->pubkey.signid[6], dig->pubkey.signid[7], dig->pubkey.time[0], dig->pubkey.time[1], dig->pubkey.time[2], dig->pubkey.time[3]);
2915 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
2916 s->name = str2id(pool, "gpg-pubkey", 1);
2917 s->evr = str2id(pool, evrbuf, 1);
2919 for (i = 0; i < 8; i++)
2920 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
2921 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
2922 if (dig->pubkey.userid)
2923 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
2925 sat_free((void *)pkts);
2930 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
2932 Pool *pool = repo->pool;
2933 struct rpm_by_state state;
2934 struct rpmdbentry *entries;
2936 char *namedata, *str;
2941 data = repo_add_repodata(repo, flags);
2943 memset(&state, 0, sizeof(state));
2944 if (!(state.dbenv = opendbenv(rootdir)))
2946 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata, FLAGS_GET_PUBKEYS);
2947 for (i = 0 ; i < nentries; i++)
2949 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, (void **)&state);
2952 str = headstring(rpmhead, TAG_DESCRIPTION);
2955 s = pool_id2solvable(pool, repo_add_solvable(repo));
2956 pubkey2solvable(s, data, str);
2957 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2959 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
2961 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
2962 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
2964 rpm_byrpmdbid(0, 0, (void **)&state);
2965 if (!(flags & REPO_NO_INTERNALIZE))
2966 repodata_internalize(data);
2970 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
2972 Pool *pool = repo->pool;
2979 data = repo_add_repodata(repo, flags);
2982 for (i = 0; i < nkeys; i++)
2984 if ((fp = fopen(keys[i], "r")) == 0)
2991 if (bufl - l < 4096)
2993 buf = sat_realloc(buf, bufl + 4096);
2996 ll = fread(buf, 1, bufl - l, fp);
3002 s = pool_id2solvable(pool, repo_add_solvable(repo));
3003 pubkey2solvable(s, data, buf);
3006 if (!(flags & REPO_NO_INTERNALIZE))
3007 repodata_internalize(data);