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>
25 #include <rpm/rpmio.h>
26 #include <rpm/rpmpgp.h>
27 #include <rpm/header.h>
28 #include <rpm/rpmdb.h>
44 #include "repo_rpmdb.h"
46 #define RPMDB_COOKIE_VERSION 2
49 #define TAG_VERSION 1001
50 #define TAG_RELEASE 1002
51 #define TAG_EPOCH 1003
52 #define TAG_SUMMARY 1004
53 #define TAG_DESCRIPTION 1005
54 #define TAG_BUILDTIME 1006
55 #define TAG_BUILDHOST 1007
56 #define TAG_INSTALLTIME 1008
58 #define TAG_DISTRIBUTION 1010
59 #define TAG_VENDOR 1011
60 #define TAG_LICENSE 1014
61 #define TAG_PACKAGER 1015
62 #define TAG_GROUP 1016
65 #define TAG_FILESIZES 1028
66 #define TAG_FILEMODES 1030
67 #define TAG_FILEMD5S 1035
68 #define TAG_FILELINKTOS 1036
69 #define TAG_SOURCERPM 1044
70 #define TAG_PROVIDENAME 1047
71 #define TAG_REQUIREFLAGS 1048
72 #define TAG_REQUIRENAME 1049
73 #define TAG_REQUIREVERSION 1050
74 #define TAG_NOSOURCE 1051
75 #define TAG_NOPATCH 1052
76 #define TAG_CONFLICTFLAGS 1053
77 #define TAG_CONFLICTNAME 1054
78 #define TAG_CONFLICTVERSION 1055
79 #define TAG_OBSOLETENAME 1090
80 #define TAG_FILEDEVICES 1095
81 #define TAG_FILEINODES 1096
82 #define TAG_PROVIDEFLAGS 1112
83 #define TAG_PROVIDEVERSION 1113
84 #define TAG_OBSOLETEFLAGS 1114
85 #define TAG_OBSOLETEVERSION 1115
86 #define TAG_DIRINDEXES 1116
87 #define TAG_BASENAMES 1117
88 #define TAG_DIRNAMES 1118
89 #define TAG_PAYLOADFORMAT 1124
90 #define TAG_PATCHESNAME 1133
91 #define TAG_FILECOLORS 1140
92 #define TAG_SUGGESTSNAME 1156
93 #define TAG_SUGGESTSVERSION 1157
94 #define TAG_SUGGESTSFLAGS 1158
95 #define TAG_ENHANCESNAME 1159
96 #define TAG_ENHANCESVERSION 1160
97 #define TAG_ENHANCESFLAGS 1161
99 #define SIGTAG_SIZE 1000
100 #define SIGTAG_PGP 1002 /* RSA signature */
101 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
102 #define SIGTAG_GPG 1005 /* DSA signature */
104 #define DEP_LESS (1 << 1)
105 #define DEP_GREATER (1 << 2)
106 #define DEP_EQUAL (1 << 3)
107 #define DEP_STRONG (1 << 27)
108 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
116 typedef struct rpmhead {
120 unsigned char data[1];
124 static inline unsigned char *
125 headfindtag(RpmHead *h, int tag)
128 unsigned char *d, taga[4];
134 for (i = 0; i < h->cnt; i++, d -= 16)
135 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
141 headexists(RpmHead *h, int tag)
143 return headfindtag(h, tag) ? 1 : 0;
146 static unsigned int *
147 headint32array(RpmHead *h, int tag, int *cnt)
149 unsigned int i, o, *r;
150 unsigned char *d = headfindtag(h, tag);
152 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
154 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
155 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
156 if (o + 4 * i > h->dcnt)
159 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
162 for (o = 0; o < i; o++, d += 4)
163 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
167 /* returns the first entry of an integer array */
169 headint32(RpmHead *h, int tag)
172 unsigned char *d = headfindtag(h, tag);
174 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
176 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
177 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
178 if (i == 0 || o + 4 * i > h->dcnt)
181 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
184 static unsigned int *
185 headint16array(RpmHead *h, int tag, int *cnt)
187 unsigned int i, o, *r;
188 unsigned char *d = headfindtag(h, tag);
190 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
192 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
193 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
194 if (o + 4 * i > h->dcnt)
197 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
200 for (o = 0; o < i; o++, d += 2)
201 r[o] = d[0] << 8 | d[1];
206 headstring(RpmHead *h, int tag)
209 unsigned char *d = headfindtag(h, tag);
210 /* 6: STRING, 9: I18NSTRING */
211 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
213 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
216 return (char *)h->dp + o;
220 headstringarray(RpmHead *h, int tag, int *cnt)
223 unsigned char *d = headfindtag(h, tag);
226 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
228 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
229 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
230 r = sat_calloc(i ? i : 1, sizeof(char *));
234 for (o = 0; o < i; o++)
238 d += strlen((char *)d) + 1;
239 if (d >= h->dp + h->dcnt)
248 static unsigned char *
249 headbinary(RpmHead *h, int tag, unsigned int *sizep)
252 unsigned char *d = headfindtag(h, tag);
253 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
255 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
256 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
257 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
264 static char *headtoevr(RpmHead *h)
271 version = headstring(h, TAG_VERSION);
272 release = headstring(h, TAG_RELEASE);
273 epoch = headint32(h, TAG_EPOCH);
274 if (!version || !release)
276 fprintf(stderr, "headtoevr: bad rpm header\n");
279 for (v = version; *v >= 0 && *v <= '9'; v++)
281 if (epoch || (v != version && *v == ':'))
283 char epochbuf[11]; /* 32bit decimal will fit in */
284 sprintf(epochbuf, "%u", epoch);
285 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
286 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
290 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
291 sprintf(evr, "%s-%s", version, release);
298 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
300 const unsigned char *cp;
303 unsigned char *buf = 0, *bp;
305 /* check if it's already utf8, code taken from screen ;-) */
306 cp = (const unsigned char *)str;
307 while ((c = *cp++) != 0)
311 if ((c & 0xc0) != 0x80)
312 break; /* encoding error */
313 c = (c & 0x3f) | (state << 6);
314 if (!(state & 0x40000000))
316 /* check for overlong sequences */
317 if ((c & 0x820823e0) == 0x80000000)
319 else if ((c & 0x020821f0) == 0x02000000)
321 else if ((c & 0x000820f8) == 0x00080000)
323 else if ((c & 0x0000207c) == 0x00002000)
333 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
335 c = (c & 0x03) | 0xbfffff00; /* 4 */
337 c = (c & 0x07) | 0xbfffc000; /* 3 */
339 c = (c & 0x0f) | 0xbff00000; /* 2 */
341 c = (c & 0x1f) | 0xfc000000; /* 1 */
345 state = (c & 0x80000000) ? c : 0;
349 /* not utf8, assume latin1 */
350 buf = sat_malloc(2 * strlen(str) + 1);
351 cp = (const unsigned char *)str;
354 while ((c = *cp++) != 0)
367 repodata_set_str(repodata, handle, tag, str);
373 #define MAKEDEPS_FILTER_WEAK (1 << 0)
374 #define MAKEDEPS_FILTER_STRONG (1 << 1)
375 #define MAKEDEPS_NO_RPMLIB (1 << 2)
378 * strong: 0: ignore strongness
379 * 1: filter to strong
383 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
387 int i, cc, nc, vc, fc;
389 unsigned int olddeps;
393 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
394 n = headstringarray(rpmhead, tagn, &nc);
397 v = headstringarray(rpmhead, tagv, &vc);
403 f = headint32array(rpmhead, tagf, &fc);
410 if (nc != vc || nc != fc)
412 fprintf(stderr, "bad dependency entries\n");
417 haspre = 0; /* add no prereq marker */
420 /* we do filtering */
422 for (i = 0; i < nc; i++)
424 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
426 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
427 if (!strncmp(n[i], "rpmlib(", 7))
429 if ((f[i] & DEP_PRE) != 0)
434 else if (tagn == TAG_REQUIRENAME)
436 /* no filtering, just look for the first prereq */
437 for (i = 0; i < nc; i++)
438 if ((f[i] & DEP_PRE) != 0)
452 olddeps = repo_reserve_ids(repo, 0, cc);
453 ida = repo->idarraydata + olddeps;
460 haspre = 2; /* pass two: prereqs */
462 *ida++ = SOLVABLE_PREREQMARKER;
464 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
466 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
468 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
470 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
471 if (!strncmp(n[i], "rpmlib(", 7))
473 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
477 if ((f[i] & DEP_LESS) != 0)
479 if ((f[i] & DEP_EQUAL) != 0)
481 if ((f[i] & DEP_GREATER) != 0)
483 name = str2id(pool, n[i], 1);
484 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
485 evr = str2id(pool, v[i] + 2, 1);
487 evr = str2id(pool, v[i], 1);
488 *ida++ = rel2id(pool, name, evr, flags, 1);
491 *ida++ = str2id(pool, n[i], 1);
494 repo->idarraysize += cc + 1;
502 #ifdef USE_FILEFILTER
504 #define FILEFILTER_EXACT 0
505 #define FILEFILTER_STARTS 1
506 #define FILEFILTER_CONTAINS 2
514 static struct filefilter filefilters[] = {
515 { FILEFILTER_CONTAINS, "/bin/", 0},
516 { FILEFILTER_CONTAINS, "/sbin/", 0},
517 { FILEFILTER_CONTAINS, "/lib/", 0},
518 { FILEFILTER_CONTAINS, "/lib64/", 0},
519 { FILEFILTER_CONTAINS, "/etc/", 0},
520 { FILEFILTER_STARTS, "/usr/games/", 0},
521 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
522 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
523 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
529 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
533 unsigned int *fkb, *fn, *fsz, *fm, *fino;
534 unsigned int inotest[256], inotestok;
538 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
539 if (!fsz || fc != fszc)
544 /* stupid rpm records sizes of directories, so we have to check the mode */
545 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
546 if (!fm || fc != fszc)
552 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
553 if (!fino || fc != fszc)
561 if (fc < sizeof(inotest))
563 memset(inotest, 0, sizeof(inotest));
564 for (i = 0; i < fc; i++)
567 if (fsz[i] == 0 || !S_ISREG(fm[i]))
569 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
570 bit = 1 << (fino[i] & 31);
571 if ((inotest[off] & bit) != 0)
580 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
582 unsigned int mask, hash, hh;
583 if (!fdev || fc != fszc)
592 while ((mask & (mask - 1)) != 0)
593 mask = mask & (mask - 1);
595 if (mask > sizeof(inotest)/sizeof(*inotest))
596 fx = sat_calloc(mask, sizeof(unsigned int));
600 memset(fx, 0, mask * sizeof(unsigned int));
603 for (i = 0; i < fc; i++)
605 if (fsz[i] == 0 || !S_ISREG(fm[i]))
607 hash = (fino[i] + fdev[i] * 31) & mask;
609 while ((j = fx[hash]) != 0)
611 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
613 fsz[i] = 0; /* kill entry */
616 hash = (hash + hh++) & mask;
626 fn = sat_calloc(dic, sizeof(unsigned int));
627 fkb = sat_calloc(dic, sizeof(unsigned int));
628 for (i = 0; i < fc; i++)
633 if (fsz[i] == 0 || !S_ISREG(fm[i]))
635 fkb[di[i]] += fsz[i] / 1024 + 1;
640 handle = s - pool->solvables;
641 for (i = 0; i < fc; i++)
645 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
646 did = repodata_str2dir(data, "/usr/src", 1);
648 did = repodata_str2dir(data, dn[i], 1);
649 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
655 /* assumes last processed array is provides! */
657 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
664 #ifdef USE_FILEFILTER
666 struct filefilter *ff;
675 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
678 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
684 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
693 fprintf(stderr, "bad filelist\n");
698 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
700 for (i = 0; i < bnc; i++)
702 #ifdef USE_FILEFILTER
704 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
708 switch (ff->dirmatch)
710 case FILEFILTER_STARTS:
711 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
714 case FILEFILTER_CONTAINS:
715 if (!strstr(dn[di[i]], ff->dir))
718 case FILEFILTER_EXACT:
720 if (strcmp(dn[di[i]], ff->dir))
727 if (strcmp(bn[i], ff->base))
732 if (j == sizeof(filefilters)/sizeof(*filefilters))
736 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
740 fn = sat_realloc(fn, fna);
742 strcpy(fn, dn[di[i]]);
744 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
749 handle = s - pool->solvables;
750 did = repodata_str2dir(data, dn[di[i]], 1);
752 did = repodata_str2dir(data, "/", 1);
753 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
767 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
769 const char *p, *sevr, *sarch;
771 p = strrchr(sourcerpm, '.');
772 if (!p || strcmp(p, ".rpm") != 0)
775 while (p > sourcerpm && *p != '.')
777 if (*p != '.' || p == sourcerpm)
780 while (p > sourcerpm && *p != '-')
782 if (*p != '-' || p == sourcerpm)
785 while (p > sourcerpm && *p != '-')
787 if (*p != '-' || p == sourcerpm)
790 if (!strcmp(sarch, "src.rpm"))
791 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
792 else if (!strcmp(sarch, "nosrc.rpm"))
793 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
795 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
796 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
797 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
799 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
800 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
801 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
803 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
807 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
813 name = headstring(rpmhead, TAG_NAME);
814 if (!strcmp(name, "gpg-pubkey"))
816 s->name = str2id(pool, name, 1);
819 fprintf(stderr, "package has no name\n");
822 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
824 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
827 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
828 s->arch = ARCH_NOSRC;
833 s->arch = ARCH_NOARCH;
834 evr = headtoevr(rpmhead);
835 s->evr = str2id(pool, evr, 1);
836 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
838 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
839 if ((flags & RPM_ADD_NO_FILELIST) == 0)
840 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
841 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
842 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
843 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
844 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
845 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
847 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
848 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
849 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
850 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
851 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
852 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
860 handle = s - pool->solvables;
861 str = headstring(rpmhead, TAG_SUMMARY);
863 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
864 str = headstring(rpmhead, TAG_DESCRIPTION);
868 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
869 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
873 /* oh my, found SUSE special author section */
878 while (l > 0 && str[l - 1] == '\n')
881 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
883 aut = str; /* copy over */
884 while (*p == ' ' || *p == '\n')
897 while (aut != str && aut[-1] == '\n')
901 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
905 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
907 str = headstring(rpmhead, TAG_GROUP);
909 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
910 str = headstring(rpmhead, TAG_LICENSE);
912 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
913 str = headstring(rpmhead, TAG_URL);
915 repodata_set_str(data, handle, SOLVABLE_URL, str);
916 str = headstring(rpmhead, TAG_DISTRIBUTION);
918 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
919 str = headstring(rpmhead, TAG_PACKAGER);
921 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
922 u32 = headint32(rpmhead, TAG_BUILDTIME);
924 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
925 u32 = headint32(rpmhead, TAG_INSTALLTIME);
927 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
928 u32 = headint32(rpmhead, TAG_SIZE);
930 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
932 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
939 copyreldep(Pool *pool, Pool *frompool, Id id)
941 Reldep *rd = GETRELDEP(frompool, id);
942 Id name = rd->name, evr = rd->evr;
944 name = copyreldep(pool, frompool, name);
946 name = str2id(pool, id2str(frompool, name), 1);
948 evr = copyreldep(pool, frompool, evr);
950 evr = str2id(pool, id2str(frompool, evr), 1);
951 return rel2id(pool, name, evr, rd->flags, 1);
955 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
960 Pool *frompool = fromrepo->pool;
964 from = fromrepo->idarraydata + fromoff;
965 for (ida = from, cc = 0; *ida; ida++, cc++)
969 ido = repo_reserve_ids(repo, 0, cc);
970 ida = repo->idarraydata + ido;
971 if (frompool && pool != frompool)
977 id = copyreldep(pool, frompool, id);
979 id = str2id(pool, id2str(frompool, id), 1);
985 memcpy(ida, from, (cc + 1) * sizeof(Id));
986 repo->idarraysize += cc + 1;
990 #define COPYDIR_DIRCACHE_SIZE 512
992 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
995 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
997 if (cache && cache[did & 255] == did)
998 return cache[(did & 255) + 256];
999 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1003 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1005 Id parent = dirpool_parent(&fromdata->dirpool, did);
1006 Id compid = dirpool_compid(&fromdata->dirpool, did);
1008 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1009 if (fromspool != &pool->ss)
1010 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1011 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1014 cache[did & 255] = did;
1015 cache[(did & 255) + 256] = compid;
1020 struct solvable_copy_cbdata {
1027 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1029 struct solvable_copy_cbdata *cbdata = vcbdata;
1031 Repodata *data = cbdata->data;
1032 Id handle = cbdata->handle;
1033 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1034 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1036 keyname = key->name;
1037 if (keyname >= ID_NUM_INTERNAL)
1038 keyname = str2id(pool, id2str(frompool, keyname), 1);
1041 case REPOKEY_TYPE_ID:
1042 case REPOKEY_TYPE_CONSTANTID:
1044 assert(!data->localpool); /* implement me! */
1045 if (pool != frompool || fromdata->localpool)
1048 id = copyreldep(pool, frompool, id);
1050 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1052 if (key->type == REPOKEY_TYPE_ID)
1053 repodata_set_id(data, handle, keyname, id);
1055 repodata_set_constantid(data, handle, keyname, id);
1057 case REPOKEY_TYPE_STR:
1058 repodata_set_str(data, handle, keyname, kv->str);
1060 case REPOKEY_TYPE_VOID:
1061 repodata_set_void(data, handle, keyname);
1063 case REPOKEY_TYPE_NUM:
1064 repodata_set_num(data, handle, keyname, kv->num);
1066 case REPOKEY_TYPE_CONSTANT:
1067 repodata_set_constant(data, handle, keyname, kv->num);
1069 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1071 assert(!data->localpool); /* implement me! */
1072 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1073 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1075 case REPOKEY_TYPE_DIRSTRARRAY:
1077 assert(!data->localpool); /* implement me! */
1078 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1079 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1088 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1090 Repo *repo = s->repo;
1091 Repo *fromrepo = r->repo;
1092 Pool *pool = repo->pool;
1093 struct solvable_copy_cbdata cbdata;
1095 /* copy solvable data */
1096 if (pool == fromrepo->pool)
1101 s->vendor = r->vendor;
1106 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1108 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1110 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1112 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1114 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1115 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1116 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1117 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1118 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1119 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1120 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1121 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1123 /* copy all attributes */
1127 cbdata.handle = s - pool->solvables;
1128 cbdata.dircache = dircache;
1129 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1132 /* used to sort entries returned in some database order */
1134 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1136 struct rpmid const *a = va, *b = vb;
1138 r = strcmp(a->name, b->name);
1141 return a->dbid - b->dbid;
1145 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1148 Pool *pool = repo->pool;
1149 Solvable *a = pool->solvables + *(Id *)va;
1150 Solvable *b = pool->solvables + *(Id *)vb;
1153 if (a->name != b->name)
1154 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1155 rpmdbid = repo->rpmdbid;
1156 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1160 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1162 Pool *pool = repo->pool;
1165 tmp = pool->solvables[pa];
1166 pool->solvables[pa] = pool->solvables[pb];
1167 pool->solvables[pb] = tmp;
1170 Id tmpid = repo->rpmdbid[pa - repo->start];
1171 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1172 repo->rpmdbid[pb - repo->start] = tmpid;
1174 /* only works if nothing is already internalized! */
1175 if (data && data->attrs)
1177 Id *tmpattrs = data->attrs[pa - data->start];
1178 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1179 data->attrs[pb - data->start] = tmpattrs;
1184 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1186 memset(cookie, 0, 32);
1187 cookie[3] = RPMDB_COOKIE_VERSION;
1188 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1189 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1192 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1194 opendbenv(const char *rootdir)
1196 char dbpath[PATH_MAX];
1200 if (db_env_create(&dbenv, 0))
1202 perror("db_env_create");
1205 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1206 if (access(dbpath, W_OK) == -1)
1208 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1213 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1215 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1220 perror("dbenv open");
1221 dbenv->close(dbenv, 0);
1229 count_headers(const char *rootdir, DB_ENV *dbenv)
1231 char dbpath[PATH_MAX];
1232 struct stat statbuf;
1240 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1241 if (stat(dbpath, &statbuf))
1243 memset(&dbkey, 0, sizeof(dbkey));
1244 memset(&dbdata, 0, sizeof(dbdata));
1245 if (db_create(&db, dbenv, 0))
1247 perror("db_create");
1250 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1252 perror("db->open Name index");
1255 if (db->get_byteswapped(db, &byteswapped))
1257 perror("db->get_byteswapped");
1260 if (db->cursor(db, NULL, &dbc, 0))
1262 perror("db->cursor");
1265 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1266 count += dbdata.size >> 3;
1273 * read rpm db as repo
1278 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1280 Pool *pool = repo->pool;
1281 unsigned char buf[16];
1286 unsigned char *dp, *dbidp;
1288 struct rpmid *rpmids, *rp;
1294 unsigned int refmask, h;
1295 char dbpath[PATH_MAX];
1299 struct stat packagesstat;
1300 unsigned char newcookie[32];
1301 const unsigned char *oldcookie = 0;
1302 Id oldcookietype = 0;
1304 int count = 0, done = 0;
1307 now = sat_timems(0);
1308 memset(&dbkey, 0, sizeof(dbkey));
1309 memset(&dbdata, 0, sizeof(dbdata));
1314 data = repo_add_repodata(repo, flags);
1316 if (ref && !(ref->nsolvables && ref->rpmdbid))
1319 if (!(dbenv = opendbenv(rootdir)))
1322 /* XXX: should get ro lock of Packages database! */
1323 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1324 if (stat(dbpath, &packagesstat))
1329 mkrpmdbcookie(&packagesstat, newcookie);
1330 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1333 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1334 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1337 int solvstart = 0, solvend = 0;
1339 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1340 count = count_headers(rootdir, dbenv);
1341 if (db_create(&db, dbenv, 0))
1343 perror("db_create");
1346 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1348 perror("db->open Packages index");
1351 if (db->get_byteswapped(db, &byteswapped))
1353 perror("db->get_byteswapped");
1356 if (db->cursor(db, NULL, &dbc, 0))
1358 perror("db->cursor");
1361 dbidp = (unsigned char *)&dbid;
1366 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1370 s = pool_id2solvable(pool, repo_add_solvable(repo));
1372 solvstart = s - pool->solvables;
1373 solvend = s - pool->solvables + 1;
1376 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1377 if (dbkey.size != 4)
1379 fprintf(stderr, "corrupt Packages database (key size)\n");
1391 memcpy(dbidp, dp, 4);
1392 if (dbid == 0) /* the join key */
1394 if (dbdata.size < 8)
1396 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1399 if (dbdata.size > rpmheadsize)
1401 rpmheadsize = dbdata.size + 128;
1402 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1404 memcpy(buf, dbdata.data, 8);
1405 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1406 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1407 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1409 fprintf(stderr, "corrupt rpm database (data size)\n");
1412 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1413 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1414 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1415 if (rpm2solv(pool, repo, data, s, rpmhead, flags))
1422 /* We can reuse this solvable, but make sure it's still
1423 associated with this repo. */
1424 memset(s, 0, sizeof(*s));
1427 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1431 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1432 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1437 /* oops, could not reuse. free it instead */
1438 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1445 /* now sort all solvables in the new solvstart..solvend block */
1446 if (solvend - solvstart > 1)
1448 pkgids = sat_malloc2(solvend - solvstart, sizeof(Id));
1449 for (i = solvstart; i < solvend; i++)
1450 pkgids[i - solvstart] = i;
1451 sat_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1453 for (i = solvstart; i < solvend; i++)
1455 int j = pkgids[i - solvstart];
1457 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1459 swap_solvables(repo, data, i, j);
1466 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1468 memset(dircache, 0, sizeof(dircache));
1469 if (db_create(&db, dbenv, 0))
1471 perror("db_create");
1474 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1476 perror("db->open Name index");
1479 if (db->get_byteswapped(db, &byteswapped))
1481 perror("db->get_byteswapped");
1484 if (db->cursor(db, NULL, &dbc, 0))
1486 perror("db->cursor");
1489 dbidp = (unsigned char *)&dbid;
1492 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1494 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1508 memcpy(dbidp, dp, 4);
1509 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1510 rpmids[nrpmids].dbid = dbid;
1511 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1512 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1513 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1524 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1526 dbidp = (unsigned char *)&dbid;
1530 /* create hash from dbid to ref */
1531 refmask = mkmask(ref->nsolvables);
1532 refhash = sat_calloc(refmask + 1, sizeof(Id));
1533 for (i = 0; i < ref->end - ref->start; i++)
1535 if (!ref->rpmdbid[i])
1537 h = ref->rpmdbid[i] & refmask;
1539 h = (h + 317) & refmask;
1540 refhash[h] = i + 1; /* make it non-zero */
1543 /* count the misses, they will cost us time */
1544 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1546 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1552 while ((id = refhash[h]))
1554 if (ref->rpmdbid[id - 1] == dbid)
1556 h = (h + 317) & refmask;
1565 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1567 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1569 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1572 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1576 while ((id = refhash[h]))
1578 if (ref->rpmdbid[id - 1] == dbid)
1580 h = (h + 317) & refmask;
1584 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1587 solvable_copy(s, r, data, dircache);
1594 if (db_create(&db, dbenv, 0))
1596 perror("db_create");
1599 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1601 perror("db->open var/lib/rpm/Packages");
1604 if (db->get_byteswapped(db, &byteswapped))
1606 perror("db->get_byteswapped");
1618 memcpy(buf, dbidp, 4);
1623 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1626 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1627 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1630 if (dbdata.size < 8)
1632 fprintf(stderr, "corrupt rpm database (size)\n");
1635 if (dbdata.size > rpmheadsize)
1637 rpmheadsize = dbdata.size + 128;
1638 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1640 memcpy(buf, dbdata.data, 8);
1641 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1642 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1643 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1645 fprintf(stderr, "corrupt rpm database (data size)\n");
1648 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1649 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1651 rpm2solv(pool, repo, data, s, rpmhead, flags);
1652 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1656 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1657 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1665 for (i = 0; i < nrpmids; i++)
1666 sat_free(rpmids[i].name);
1672 dbenv->close(dbenv, 0);
1675 if (!(flags & REPO_NO_INTERNALIZE))
1676 repodata_internalize(data);
1677 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1678 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1679 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1680 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1681 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1685 static inline unsigned int
1686 getu32(const unsigned char *dp)
1688 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1693 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1695 int i, sigdsize, sigcnt, l;
1696 Pool *pool = repo->pool;
1698 RpmHead *rpmhead = 0;
1699 int rpmheadsize = 0;
1700 char *payloadformat;
1702 unsigned char lead[4096];
1703 int headerstart, headerend;
1706 unsigned char pkgid[16];
1711 data = repo_add_repodata(repo, flags);
1713 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1714 chksumtype = REPOKEY_TYPE_SHA256;
1715 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1716 chksumtype = REPOKEY_TYPE_SHA1;
1717 for (i = 0; i < nrpms; i++)
1719 if ((fp = fopen(rpms[i], "r")) == 0)
1724 if (fstat(fileno(fp), &stb))
1730 chksumh = sat_chksum_free(chksumh, 0);
1732 chksumh = sat_chksum_create(chksumtype);
1733 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1735 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1740 sat_chksum_add(chksumh, lead, 96 + 16);
1741 if (lead[78] != 0 || lead[79] != 5)
1743 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1747 if (getu32(lead + 96) != 0x8eade801)
1749 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1753 sigcnt = getu32(lead + 96 + 8);
1754 sigdsize = getu32(lead + 96 + 12);
1755 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1757 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1761 sigdsize += sigcnt * 16;
1762 sigdsize = (sigdsize + 7) & ~7;
1763 headerstart = 96 + 16 + sigdsize;
1765 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1767 unsigned char *chksum;
1768 unsigned int chksumsize;
1769 /* extract pkgid from the signature header */
1770 if (sigdsize > rpmheadsize)
1772 rpmheadsize = sigdsize + 128;
1773 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1775 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1777 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1782 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1783 rpmhead->cnt = sigcnt;
1784 rpmhead->dcnt = sigdsize - sigcnt * 16;
1785 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1786 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1787 if (chksum && chksumsize == 16)
1790 memcpy(pkgid, chksum, 16);
1795 /* just skip the signature header */
1798 l = sigdsize > 4096 ? 4096 : sigdsize;
1799 if (fread(lead, l, 1, fp) != 1)
1801 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1806 sat_chksum_add(chksumh, lead, l);
1810 if (fread(lead, 16, 1, fp) != 1)
1812 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1817 sat_chksum_add(chksumh, lead, 16);
1818 if (getu32(lead) != 0x8eade801)
1820 fprintf(stderr, "%s: bad header\n", rpms[i]);
1824 sigcnt = getu32(lead + 8);
1825 sigdsize = getu32(lead + 12);
1826 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1828 fprintf(stderr, "%s: bad header\n", rpms[i]);
1832 l = sigdsize + sigcnt * 16;
1833 headerend = headerstart + 16 + l;
1834 if (l > rpmheadsize)
1836 rpmheadsize = l + 128;
1837 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1839 if (fread(rpmhead->data, l, 1, fp) != 1)
1841 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1846 sat_chksum_add(chksumh, rpmhead->data, l);
1847 rpmhead->cnt = sigcnt;
1848 rpmhead->dcnt = sigdsize;
1849 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1850 if (headexists(rpmhead, TAG_PATCHESNAME))
1852 /* this is a patch rpm, ignore */
1856 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1857 if (payloadformat && !strcmp(payloadformat, "drpm"))
1859 /* this is a delta rpm */
1864 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1865 sat_chksum_add(chksumh, lead, l);
1867 s = pool_id2solvable(pool, repo_add_solvable(repo));
1868 rpm2solv(pool, repo, data, s, rpmhead, flags);
1871 Id handle = s - pool->solvables;
1872 repodata_set_location(data, handle, 0, 0, rpms[i]);
1873 if (S_ISREG(stb.st_mode))
1874 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1875 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1877 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1879 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1883 chksumh = sat_chksum_free(chksumh, 0);
1886 if (!(flags & REPO_NO_INTERNALIZE))
1887 repodata_internalize(data);
1891 linkhash(const char *lt, char *hash)
1894 const unsigned char *str = (const unsigned char *)lt;
1898 while ((c = *str++) != 0)
1900 sprintf(hash, "%08x", r);
1901 sprintf(hash + 8, "%08x", l);
1902 sprintf(hash + 16, "%08x", 0);
1903 sprintf(hash + 24, "%08x", 0);
1907 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1909 RpmHead *rpmhead = rpmhandle;
1914 unsigned int *di, diidx;
1915 unsigned int *co = 0;
1916 unsigned int lastdir;
1919 int cnt, dcnt, cnt2;
1923 char md5[33], *md5p = 0;
1925 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1928 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1930 for (i = 0; i < dcnt; i++)
1931 (*cb)(cbdata, dn[i], 0, (char *)0);
1935 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1941 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1942 if (!di || cnt != cnt2)
1949 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1950 if (!fm || cnt != cnt2)
1958 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1960 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1961 if (!md || cnt != cnt2)
1971 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
1973 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
1974 if (!co || cnt != cnt2)
1987 for (i = 0; i < cnt; i++)
1992 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
1995 l = l1 + strlen(bn[i]) + 1;
1999 space = sat_realloc(space, spacen);
2001 if (lastdir != diidx)
2003 strcpy(space, dn[diidx]);
2007 strcpy(space + l1, bn[i]);
2016 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2022 linkhash(lt[i], md5);
2028 sprintf(md5, "%08x%08x", (fm[i] >> 12) & 65535, 0);
2032 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2045 rpm_query(void *rpmhandle, Id what)
2047 const char *name, *arch, *sourcerpm;
2051 RpmHead *rpmhead = rpmhandle;
2056 name = headstring(rpmhead, TAG_NAME);
2059 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2061 arch = headstring(rpmhead, TAG_ARCH);
2064 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2071 evr = headtoevr(rpmhead);
2072 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2074 sprintf(r, "%s-%s.%s", name, evr, arch);
2078 name = headstring(rpmhead, TAG_NAME);
2082 r = headtoevr(rpmhead);
2089 struct rpm_by_state {
2104 #define ENTRIES_BLOCK 255
2105 #define NAMEDATA_BLOCK 1023
2107 static struct rpmdbentry *
2108 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2122 struct rpmdbentry *entries = 0;
2128 dbenv = state->dbenv;
2129 if (db_create(&db, dbenv, 0))
2131 perror("db_create");
2134 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2136 perror("db->open index");
2140 if (db->get_byteswapped(db, &byteswapped))
2142 perror("db->get_byteswapped");
2146 if (db->cursor(db, NULL, &dbc, 0))
2148 perror("db->cursor");
2152 memset(&dbkey, 0, sizeof(dbkey));
2153 memset(&dbdata, 0, sizeof(dbdata));
2156 dbkey.data = (void *)match;
2157 dbkey.size = strlen(match);
2159 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2161 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2169 ((char *)&rpmdbid)[0] = dp[3];
2170 ((char *)&rpmdbid)[1] = dp[2];
2171 ((char *)&rpmdbid)[2] = dp[1];
2172 ((char *)&rpmdbid)[3] = dp[0];
2175 memcpy((char *)&rpmdbid, dp, 4);
2176 entries = sat_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2177 entries[nentries].rpmdbid = rpmdbid;
2178 entries[nentries].nameoff = namedatal;
2180 namedata = sat_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2181 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2182 namedata[namedatal + dbkey.size] = 0;
2183 namedatal += dbkey.size + 1;
2192 *nentriesp = nentries;
2193 *namedatap = namedata;
2198 freestate(struct rpm_by_state *state)
2204 state->db->close(state->db, 0);
2206 state->dbenv->close(state->dbenv, 0);
2207 sat_free(state->rpmhead);
2211 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2213 struct rpm_by_state state;
2214 struct rpmdbentry *entries;
2221 queue_empty(rpmdbidq);
2222 memset(&state, 0, sizeof(state));
2223 if (!(state.dbenv = opendbenv(rootdir)))
2225 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2227 for (i = 0; i < nentries; i++)
2228 queue_push(rpmdbidq, entries[i].rpmdbid);
2236 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2238 struct rpm_by_state *state = *statep;
2239 unsigned char buf[16];
2249 *statep = (void *)0;
2255 state = sat_calloc(1, sizeof(*state));
2258 if (!state->dbopened)
2260 state->dbopened = 1;
2261 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2263 if (db_create(&state->db, state->dbenv, 0))
2265 perror("db_create");
2267 state->dbenv->close(state->dbenv, 0);
2271 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2273 perror("db->open var/lib/rpm/Packages");
2274 state->db->close(state->db, 0);
2276 state->dbenv->close(state->dbenv, 0);
2280 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2282 perror("db->get_byteswapped");
2283 state->db->close(state->db, 0);
2285 state->dbenv->close(state->dbenv, 0);
2290 memcpy(buf, &rpmdbid, 4);
2291 if (state->byteswapped)
2294 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2295 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2297 memset(&dbkey, 0, sizeof(dbkey));
2298 memset(&dbdata, 0, sizeof(dbdata));
2303 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2308 if (dbdata.size < 8)
2310 fprintf(stderr, "corrupt rpm database (size)\n");
2313 if (dbdata.size > state->rpmheadsize)
2315 state->rpmheadsize = dbdata.size + 128;
2316 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2318 rpmhead = state->rpmhead;
2319 memcpy(buf, dbdata.data, 8);
2320 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2321 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2322 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2324 fprintf(stderr, "corrupt rpm database (data size)\n");
2327 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2328 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2333 rpm_byfp(FILE *fp, const char *name, void **statep)
2335 struct rpm_by_state *state = *statep;
2336 int headerstart, headerend;
2338 int sigdsize, sigcnt, l;
2339 unsigned char lead[4096];
2342 return rpm_byrpmdbid(0, 0, statep);
2345 state = sat_calloc(1, sizeof(*state));
2348 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2350 fprintf(stderr, "%s: not a rpm\n", name);
2353 if (lead[78] != 0 || lead[79] != 5)
2355 fprintf(stderr, "%s: not a V5 header\n", name);
2358 if (getu32(lead + 96) != 0x8eade801)
2360 fprintf(stderr, "%s: bad signature header\n", name);
2363 sigcnt = getu32(lead + 96 + 8);
2364 sigdsize = getu32(lead + 96 + 12);
2365 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2367 fprintf(stderr, "%s: bad signature header\n", name);
2370 sigdsize += sigcnt * 16;
2371 sigdsize = (sigdsize + 7) & ~7;
2372 headerstart = 96 + 16 + sigdsize;
2375 l = sigdsize > 4096 ? 4096 : sigdsize;
2376 if (fread(lead, l, 1, fp) != 1)
2378 fprintf(stderr, "%s: unexpected EOF\n", name);
2383 if (fread(lead, 16, 1, fp) != 1)
2385 fprintf(stderr, "%s: unexpected EOF\n", name);
2388 if (getu32(lead) != 0x8eade801)
2390 fprintf(stderr, "%s: bad header\n", name);
2394 sigcnt = getu32(lead + 8);
2395 sigdsize = getu32(lead + 12);
2396 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2398 fprintf(stderr, "%s: bad header\n", name);
2402 l = sigdsize + sigcnt * 16;
2403 headerend = headerstart + 16 + l;
2404 if (l > state->rpmheadsize)
2406 state->rpmheadsize = l + 128;
2407 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2409 rpmhead = state->rpmhead;
2410 if (fread(rpmhead->data, l, 1, fp) != 1)
2412 fprintf(stderr, "%s: unexpected EOF\n", name);
2416 rpmhead->cnt = sigcnt;
2417 rpmhead->dcnt = sigdsize;
2418 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2423 rpm_byrpmh(Header h, void **statep)
2425 struct rpm_by_state *state = *statep;
2426 const unsigned char *uh;
2427 int sigdsize, sigcnt, l;
2430 uh = headerUnload(h);
2433 sigcnt = getu32(uh);
2434 sigdsize = getu32(uh + 4);
2435 l = sigdsize + sigcnt * 16;
2438 state = sat_calloc(1, sizeof(*state));
2441 if (l > state->rpmheadsize)
2443 state->rpmheadsize = l + 128;
2444 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2446 rpmhead = state->rpmhead;
2447 memcpy(rpmhead->data, uh + 8, l - 8);
2449 rpmhead->cnt = sigcnt;
2450 rpmhead->dcnt = sigdsize;
2451 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2457 r64dec1(char *p, unsigned int *vp, int *eofp)
2462 for (i = 0; i < 4; )
2467 if (x >= 'A' && x <= 'Z')
2469 else if (x >= 'a' && x <= 'z')
2471 else if (x >= '0' && x <= '9')
2498 crc24(unsigned char *p, int len)
2500 unsigned int crc = 0xb704ceL;
2505 crc ^= (*p++) << 16;
2506 for (i = 0; i < 8; i++)
2507 if ((crc <<= 1) & 0x1000000)
2510 return crc & 0xffffffL;
2513 static unsigned char *
2514 unarmor(char *pubkey, int *pktlp)
2518 unsigned char *buf, *bp;
2522 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2524 pubkey = strchr(pubkey, '\n');
2529 pubkey = strchr(pubkey, '\n');
2532 /* skip header lines */
2535 while (*pubkey == ' ' || *pubkey == '\t')
2537 if (*pubkey == '\n')
2539 pubkey = strchr(pubkey, '\n');
2544 p = strchr(pubkey, '=');
2548 bp = buf = sat_malloc(l * 3 / 4 + 4);
2552 pubkey = r64dec1(pubkey, &v, &eof);
2562 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2565 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2570 if (v != crc24(buf, bp - buf))
2575 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2577 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2587 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2590 unsigned char keyid[8];
2591 unsigned int kcr = 0, maxex = 0;
2592 unsigned char *pubkey = 0;
2594 unsigned char *userid = 0;
2597 for (; pl; p += l, pl -= l)
2601 if (!(x & 128) || pl <= 0)
2606 tag = (x & 0x3c) >> 2;
2628 else if (x >= 192 && x < 224)
2632 l = ((x - 192) << 8) + *p++ + 192;
2639 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2650 pubkey = sat_realloc(pubkey, l);
2652 memcpy(pubkey, p, l);
2659 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2663 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2667 memset(keyid, 0, 8);
2668 if (p[7] == 1) /* RSA */
2671 unsigned char fp[16];
2675 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2676 memcpy(keyid, p + 10 + ql - 8, 8);
2677 h = sat_chksum_create(REPOKEY_TYPE_MD5);
2678 sat_chksum_add(h, p + 10, ql);
2680 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2681 sat_chksum_add(h, q + 2, ql);
2682 sat_chksum_free(h, fp);
2683 for (i = 0; i < 16; i++)
2684 sprintf(fpx + i * 2, "%02x", fp[i]);
2685 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2692 unsigned char hdr[3];
2693 unsigned char fp[20];
2696 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2700 h = sat_chksum_create(REPOKEY_TYPE_SHA1);
2701 sat_chksum_add(h, hdr, 3);
2702 sat_chksum_add(h, p, l);
2703 sat_chksum_free(h, fp);
2704 for (i = 0; i < 20; i++)
2705 sprintf(fpx + i * 2, "%02x", fp[i]);
2706 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2707 memcpy(keyid, fp + 12, 8);
2712 if (p[0] == 3 && p[1] == 5)
2717 // printf("V3 signature packet\n");
2718 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2720 if (!memcmp(keyid, p + 6, 8))
2722 // printf("SELF SIG\n");
2726 // printf("OTHER SIG\n");
2730 htype = REPOKEY_TYPE_MD5;
2731 else if (p[16] == 2)
2732 htype = REPOKEY_TYPE_SHA1;
2733 else if (p[16] == 8)
2734 htype = REPOKEY_TYPE_SHA256;
2737 void *h = sat_chksum_create(htype);
2738 unsigned char b[3], *cs;
2741 b[1] = pubkeyl >> 8;
2743 sat_chksum_add(h, b, 3);
2744 sat_chksum_add(h, pubkey, pubkeyl);
2745 if (p[2] >= 0x10 && p[2] <= 0x13)
2746 sat_chksum_add(h, userid, useridl);
2747 sat_chksum_add(h, p + 2, 5);
2748 cs = sat_chksum_get(h, 0);
2749 sat_chksum_free(h, 0);
2755 int j, ql, haveissuer;
2757 unsigned int ex = 0, scr = 0;
2758 unsigned char issuer[8];
2760 // printf("V4 signature packet\n");
2761 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2766 for (j = 0; q && j < 2; j++)
2768 ql = q[0] << 8 | q[1];
2784 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2795 sl = ((x - 192) << 8) + *q++ + 192;
2804 // printf("%d SIGSUB %d %d\n", j, x, sl);
2805 if (x == 16 && sl == 9 && !haveissuer)
2807 memcpy(issuer, q + 1, 8);
2810 if (x == 2 && j == 0)
2811 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2812 if (x == 9 && j == 0)
2813 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2825 htype = REPOKEY_TYPE_MD5;
2827 htype = REPOKEY_TYPE_SHA1;
2829 htype = REPOKEY_TYPE_SHA256;
2830 if (htype && pubkeyl)
2832 void *h = sat_chksum_create(htype);
2833 unsigned char b[6], *cs;
2837 b[1] = pubkeyl >> 8;
2839 sat_chksum_add(h, b, 3);
2840 sat_chksum_add(h, pubkey, pubkeyl);
2841 if (p[1] >= 0x10 && p[1] <= 0x13)
2844 b[1] = useridl >> 24;
2845 b[2] = useridl >> 16;
2846 b[3] = useridl >> 8;
2848 sat_chksum_add(h, b, 5);
2849 sat_chksum_add(h, userid, useridl);
2851 hl = 6 + (p[4] << 8 | p[5]);
2852 sat_chksum_add(h, p, hl);
2859 sat_chksum_add(h, b, 6);
2860 cs = sat_chksum_get(h, 0);
2861 sat_chksum_free(h, 0);
2864 if (!memcmp(keyid, issuer, 8))
2866 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2872 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2879 userid = sat_realloc(userid, l);
2881 memcpy(userid, p, l);
2886 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2891 /* this is private to rpm, but rpm lacks an interface to retrieve
2892 * the values. Sigh. */
2893 struct pgpDigParams_s {
2894 const char * userid;
2895 const unsigned char * hash;
2896 const char * params[4];
2898 unsigned char version; /*!< version number. */
2899 unsigned char time[4]; /*!< time that the key was created. */
2900 unsigned char pubkey_algo; /*!< public key algorithm. */
2901 unsigned char hash_algo;
2902 unsigned char sigtype;
2903 unsigned char hashlen;
2904 unsigned char signhash16[2];
2905 unsigned char signid[8];
2906 unsigned char saved;
2910 struct pgpDigParams_s signature;
2911 struct pgpDigParams_s pubkey;
2915 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
2917 Pool *pool = s->repo->pool;
2918 unsigned char *pkts;
2923 char evrbuf[8 + 1 + 8 + 1];
2925 pkts = unarmor(pubkey, &pktsl);
2928 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
2929 parsekeydata(s, data, pkts, pktsl);
2930 /* only rpm knows how to do the release calculation, we don't dare
2931 * to recreate all the bugs */
2933 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
2934 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
2935 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]);
2936 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
2938 s->name = str2id(pool, "gpg-pubkey", 1);
2939 s->evr = str2id(pool, evrbuf, 1);
2941 for (i = 0; i < 8; i++)
2942 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
2943 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
2944 if (dig->pubkey.userid)
2945 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
2947 sat_free((void *)pkts);
2952 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
2954 Pool *pool = repo->pool;
2955 struct rpm_by_state state;
2956 struct rpmdbentry *entries;
2958 char *namedata, *str;
2963 data = repo_add_repodata(repo, flags);
2965 memset(&state, 0, sizeof(state));
2966 if (!(state.dbenv = opendbenv(rootdir)))
2968 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
2969 for (i = 0 ; i < nentries; i++)
2971 void *statep = &state;
2972 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
2975 str = headstring(rpmhead, TAG_DESCRIPTION);
2978 s = pool_id2solvable(pool, repo_add_solvable(repo));
2979 pubkey2solvable(s, data, str);
2980 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2982 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
2984 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
2985 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
2988 if (!(flags & REPO_NO_INTERNALIZE))
2989 repodata_internalize(data);
2993 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
2995 Pool *pool = repo->pool;
3002 data = repo_add_repodata(repo, flags);
3005 for (i = 0; i < nkeys; i++)
3007 if ((fp = fopen(keys[i], "r")) == 0)
3014 if (bufl - l < 4096)
3016 buf = sat_realloc(buf, bufl + 4096);
3019 ll = fread(buf, 1, bufl - l, fp);
3025 s = pool_id2solvable(pool, repo_add_solvable(repo));
3026 pubkey2solvable(s, data, buf);
3029 if (!(flags & REPO_NO_INTERNALIZE))
3030 repodata_internalize(data);