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 /* 3: added triggers */
47 #define RPMDB_COOKIE_VERSION 3
50 #define TAG_VERSION 1001
51 #define TAG_RELEASE 1002
52 #define TAG_EPOCH 1003
53 #define TAG_SUMMARY 1004
54 #define TAG_DESCRIPTION 1005
55 #define TAG_BUILDTIME 1006
56 #define TAG_BUILDHOST 1007
57 #define TAG_INSTALLTIME 1008
59 #define TAG_DISTRIBUTION 1010
60 #define TAG_VENDOR 1011
61 #define TAG_LICENSE 1014
62 #define TAG_PACKAGER 1015
63 #define TAG_GROUP 1016
66 #define TAG_FILESIZES 1028
67 #define TAG_FILEMODES 1030
68 #define TAG_FILEMD5S 1035
69 #define TAG_FILELINKTOS 1036
70 #define TAG_SOURCERPM 1044
71 #define TAG_PROVIDENAME 1047
72 #define TAG_REQUIREFLAGS 1048
73 #define TAG_REQUIRENAME 1049
74 #define TAG_REQUIREVERSION 1050
75 #define TAG_NOSOURCE 1051
76 #define TAG_NOPATCH 1052
77 #define TAG_CONFLICTFLAGS 1053
78 #define TAG_CONFLICTNAME 1054
79 #define TAG_CONFLICTVERSION 1055
80 #define TAG_TRIGGERNAME 1066
81 #define TAG_TRIGGERVERSION 1067
82 #define TAG_TRIGGERFLAGS 1068
83 #define TAG_OBSOLETENAME 1090
84 #define TAG_FILEDEVICES 1095
85 #define TAG_FILEINODES 1096
86 #define TAG_PROVIDEFLAGS 1112
87 #define TAG_PROVIDEVERSION 1113
88 #define TAG_OBSOLETEFLAGS 1114
89 #define TAG_OBSOLETEVERSION 1115
90 #define TAG_DIRINDEXES 1116
91 #define TAG_BASENAMES 1117
92 #define TAG_DIRNAMES 1118
93 #define TAG_PAYLOADFORMAT 1124
94 #define TAG_PATCHESNAME 1133
95 #define TAG_FILECOLORS 1140
96 #define TAG_SUGGESTSNAME 1156
97 #define TAG_SUGGESTSVERSION 1157
98 #define TAG_SUGGESTSFLAGS 1158
99 #define TAG_ENHANCESNAME 1159
100 #define TAG_ENHANCESVERSION 1160
101 #define TAG_ENHANCESFLAGS 1161
103 #define SIGTAG_SIZE 1000
104 #define SIGTAG_PGP 1002 /* RSA signature */
105 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
106 #define SIGTAG_GPG 1005 /* DSA signature */
108 #define DEP_LESS (1 << 1)
109 #define DEP_GREATER (1 << 2)
110 #define DEP_EQUAL (1 << 3)
111 #define DEP_STRONG (1 << 27)
112 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
120 typedef struct rpmhead {
124 unsigned char data[1];
128 static inline unsigned char *
129 headfindtag(RpmHead *h, int tag)
132 unsigned char *d, taga[4];
138 for (i = 0; i < h->cnt; i++, d -= 16)
139 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
145 headexists(RpmHead *h, int tag)
147 return headfindtag(h, tag) ? 1 : 0;
150 static unsigned int *
151 headint32array(RpmHead *h, int tag, int *cnt)
153 unsigned int i, o, *r;
154 unsigned char *d = headfindtag(h, tag);
156 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
158 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
159 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
160 if (o + 4 * i > h->dcnt)
163 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
166 for (o = 0; o < i; o++, d += 4)
167 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
171 /* returns the first entry of an integer array */
173 headint32(RpmHead *h, int tag)
176 unsigned char *d = headfindtag(h, tag);
178 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
180 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
181 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
182 if (i == 0 || o + 4 * i > h->dcnt)
185 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
188 static unsigned int *
189 headint16array(RpmHead *h, int tag, int *cnt)
191 unsigned int i, o, *r;
192 unsigned char *d = headfindtag(h, tag);
194 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
196 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
197 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
198 if (o + 4 * i > h->dcnt)
201 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
204 for (o = 0; o < i; o++, d += 2)
205 r[o] = d[0] << 8 | d[1];
210 headstring(RpmHead *h, int tag)
213 unsigned char *d = headfindtag(h, tag);
214 /* 6: STRING, 9: I18NSTRING */
215 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
217 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
220 return (char *)h->dp + o;
224 headstringarray(RpmHead *h, int tag, int *cnt)
227 unsigned char *d = headfindtag(h, tag);
230 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
232 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
233 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
234 r = sat_calloc(i ? i : 1, sizeof(char *));
238 for (o = 0; o < i; o++)
242 d += strlen((char *)d) + 1;
243 if (d >= h->dp + h->dcnt)
252 static unsigned char *
253 headbinary(RpmHead *h, int tag, unsigned int *sizep)
256 unsigned char *d = headfindtag(h, tag);
257 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
259 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
260 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
261 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
268 static char *headtoevr(RpmHead *h)
275 version = headstring(h, TAG_VERSION);
276 release = headstring(h, TAG_RELEASE);
277 epoch = headint32(h, TAG_EPOCH);
278 if (!version || !release)
280 fprintf(stderr, "headtoevr: bad rpm header\n");
283 for (v = version; *v >= 0 && *v <= '9'; v++)
285 if (epoch || (v != version && *v == ':'))
287 char epochbuf[11]; /* 32bit decimal will fit in */
288 sprintf(epochbuf, "%u", epoch);
289 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
290 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
294 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
295 sprintf(evr, "%s-%s", version, release);
302 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
304 const unsigned char *cp;
307 unsigned char *buf = 0, *bp;
309 /* check if it's already utf8, code taken from screen ;-) */
310 cp = (const unsigned char *)str;
311 while ((c = *cp++) != 0)
315 if ((c & 0xc0) != 0x80)
316 break; /* encoding error */
317 c = (c & 0x3f) | (state << 6);
318 if (!(state & 0x40000000))
320 /* check for overlong sequences */
321 if ((c & 0x820823e0) == 0x80000000)
323 else if ((c & 0x020821f0) == 0x02000000)
325 else if ((c & 0x000820f8) == 0x00080000)
327 else if ((c & 0x0000207c) == 0x00002000)
337 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
339 c = (c & 0x03) | 0xbfffff00; /* 4 */
341 c = (c & 0x07) | 0xbfffc000; /* 3 */
343 c = (c & 0x0f) | 0xbff00000; /* 2 */
345 c = (c & 0x1f) | 0xfc000000; /* 1 */
349 state = (c & 0x80000000) ? c : 0;
353 /* not utf8, assume latin1 */
354 buf = sat_malloc(2 * strlen(str) + 1);
355 cp = (const unsigned char *)str;
358 while ((c = *cp++) != 0)
371 repodata_set_str(repodata, handle, tag, str);
377 #define MAKEDEPS_FILTER_WEAK (1 << 0)
378 #define MAKEDEPS_FILTER_STRONG (1 << 1)
379 #define MAKEDEPS_NO_RPMLIB (1 << 2)
382 * strong: 0: ignore strongness
383 * 1: filter to strong
387 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
391 int i, cc, nc, vc, fc;
393 unsigned int olddeps;
397 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
398 n = headstringarray(rpmhead, tagn, &nc);
401 v = headstringarray(rpmhead, tagv, &vc);
407 f = headint32array(rpmhead, tagf, &fc);
414 if (nc != vc || nc != fc)
416 fprintf(stderr, "bad dependency entries\n");
421 haspre = 0; /* add no prereq marker */
424 /* we do filtering */
426 for (i = 0; i < nc; i++)
428 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
430 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
431 if (!strncmp(n[i], "rpmlib(", 7))
433 if ((f[i] & DEP_PRE) != 0)
438 else if (tagn == TAG_REQUIRENAME)
440 /* no filtering, just look for the first prereq */
441 for (i = 0; i < nc; i++)
442 if ((f[i] & DEP_PRE) != 0)
456 olddeps = repo_reserve_ids(repo, 0, cc);
457 ida = repo->idarraydata + olddeps;
464 haspre = 2; /* pass two: prereqs */
466 *ida++ = SOLVABLE_PREREQMARKER;
468 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
470 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
472 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
474 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
475 if (!strncmp(n[i], "rpmlib(", 7))
477 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
481 if ((f[i] & DEP_LESS) != 0)
483 if ((f[i] & DEP_EQUAL) != 0)
485 if ((f[i] & DEP_GREATER) != 0)
487 name = str2id(pool, n[i], 1);
488 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
489 evr = str2id(pool, v[i] + 2, 1);
491 evr = str2id(pool, v[i], 1);
492 *ida++ = rel2id(pool, name, evr, flags, 1);
495 *ida++ = str2id(pool, n[i], 1);
498 repo->idarraysize += cc + 1;
506 #ifdef USE_FILEFILTER
508 #define FILEFILTER_EXACT 0
509 #define FILEFILTER_STARTS 1
510 #define FILEFILTER_CONTAINS 2
518 static struct filefilter filefilters[] = {
519 { FILEFILTER_CONTAINS, "/bin/", 0},
520 { FILEFILTER_CONTAINS, "/sbin/", 0},
521 { FILEFILTER_CONTAINS, "/lib/", 0},
522 { FILEFILTER_CONTAINS, "/lib64/", 0},
523 { FILEFILTER_CONTAINS, "/etc/", 0},
524 { FILEFILTER_STARTS, "/usr/games/", 0},
525 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
526 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
527 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
533 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
537 unsigned int *fkb, *fn, *fsz, *fm, *fino;
538 unsigned int inotest[256], inotestok;
542 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
543 if (!fsz || fc != fszc)
548 /* stupid rpm records sizes of directories, so we have to check the mode */
549 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
550 if (!fm || fc != fszc)
556 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
557 if (!fino || fc != fszc)
565 if (fc < sizeof(inotest))
567 memset(inotest, 0, sizeof(inotest));
568 for (i = 0; i < fc; i++)
571 if (fsz[i] == 0 || !S_ISREG(fm[i]))
573 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
574 bit = 1 << (fino[i] & 31);
575 if ((inotest[off] & bit) != 0)
584 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
586 unsigned int mask, hash, hh;
587 if (!fdev || fc != fszc)
596 while ((mask & (mask - 1)) != 0)
597 mask = mask & (mask - 1);
599 if (mask > sizeof(inotest)/sizeof(*inotest))
600 fx = sat_calloc(mask, sizeof(unsigned int));
604 memset(fx, 0, mask * sizeof(unsigned int));
607 for (i = 0; i < fc; i++)
609 if (fsz[i] == 0 || !S_ISREG(fm[i]))
611 hash = (fino[i] + fdev[i] * 31) & mask;
613 while ((j = fx[hash]) != 0)
615 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
617 fsz[i] = 0; /* kill entry */
620 hash = (hash + hh++) & mask;
630 fn = sat_calloc(dc, sizeof(unsigned int));
631 fkb = sat_calloc(dc, sizeof(unsigned int));
632 for (i = 0; i < fc; i++)
637 if (fsz[i] == 0 || !S_ISREG(fm[i]))
639 fkb[di[i]] += fsz[i] / 1024 + 1;
644 handle = s - pool->solvables;
645 for (i = 0; i < dc; i++)
651 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
652 did = repodata_str2dir(data, "/usr/src", 1);
654 continue; /* work around rpm bug */
657 did = repodata_str2dir(data, dn[i], 1);
658 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
664 /* assumes last processed array is provides! */
666 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
673 #ifdef USE_FILEFILTER
675 struct filefilter *ff;
684 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
687 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
693 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
702 fprintf(stderr, "bad filelist\n");
707 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
709 for (i = 0; i < bnc; i++)
711 #ifdef USE_FILEFILTER
713 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
717 switch (ff->dirmatch)
719 case FILEFILTER_STARTS:
720 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
723 case FILEFILTER_CONTAINS:
724 if (!strstr(dn[di[i]], ff->dir))
727 case FILEFILTER_EXACT:
729 if (strcmp(dn[di[i]], ff->dir))
736 if (strcmp(bn[i], ff->base))
741 if (j == sizeof(filefilters)/sizeof(*filefilters))
745 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
749 fn = sat_realloc(fn, fna);
751 strcpy(fn, dn[di[i]]);
753 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
760 handle = s - pool->solvables;
761 did = repodata_str2dir(data, dn[di[i]], 1);
764 did = repodata_str2dir(data, "/", 1);
765 if (b && b[0] == '/')
766 b++; /* work around rpm bug */
768 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
782 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
784 const char *p, *sevr, *sarch;
786 p = strrchr(sourcerpm, '.');
787 if (!p || strcmp(p, ".rpm") != 0)
790 while (p > sourcerpm && *p != '.')
792 if (*p != '.' || p == sourcerpm)
795 while (p > sourcerpm && *p != '-')
797 if (*p != '-' || p == sourcerpm)
800 while (p > sourcerpm && *p != '-')
802 if (*p != '-' || p == sourcerpm)
805 if (!strcmp(sarch, "src.rpm"))
806 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
807 else if (!strcmp(sarch, "nosrc.rpm"))
808 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
810 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
811 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
812 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
814 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
815 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
816 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
818 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
822 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
828 name = headstring(rpmhead, TAG_NAME);
829 if (!strcmp(name, "gpg-pubkey"))
831 s->name = str2id(pool, name, 1);
834 fprintf(stderr, "package has no name\n");
837 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
839 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
842 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
843 s->arch = ARCH_NOSRC;
848 s->arch = ARCH_NOARCH;
849 evr = headtoevr(rpmhead);
850 s->evr = str2id(pool, evr, 1);
851 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
853 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
854 if ((flags & RPM_ADD_NO_FILELIST) == 0)
855 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
856 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
857 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
858 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
859 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
860 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
862 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
863 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
864 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
865 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
866 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
867 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
875 handle = s - pool->solvables;
876 str = headstring(rpmhead, TAG_SUMMARY);
878 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
879 str = headstring(rpmhead, TAG_DESCRIPTION);
883 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
884 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
888 /* oh my, found SUSE special author section */
893 while (l > 0 && str[l - 1] == '\n')
896 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
898 aut = str; /* copy over */
899 while (*p == ' ' || *p == '\n')
912 while (aut != str && aut[-1] == '\n')
916 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
920 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
922 str = headstring(rpmhead, TAG_GROUP);
924 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
925 str = headstring(rpmhead, TAG_LICENSE);
927 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
928 str = headstring(rpmhead, TAG_URL);
930 repodata_set_str(data, handle, SOLVABLE_URL, str);
931 str = headstring(rpmhead, TAG_DISTRIBUTION);
933 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
934 str = headstring(rpmhead, TAG_PACKAGER);
936 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
937 u32 = headint32(rpmhead, TAG_BUILDTIME);
939 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
940 u32 = headint32(rpmhead, TAG_INSTALLTIME);
942 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
943 u32 = headint32(rpmhead, TAG_SIZE);
945 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
947 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
948 if ((flags & RPM_ADD_TRIGGERS) != 0)
951 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
954 for (; (id = repo->idarraydata[ida]) != 0; ida++)
956 /* we currently do not support rel ids in incore data, so
957 * strip off versioning information */
960 Reldep *rd = GETRELDEP(pool, id);
965 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
975 copyreldep(Pool *pool, Pool *frompool, Id id)
977 Reldep *rd = GETRELDEP(frompool, id);
978 Id name = rd->name, evr = rd->evr;
980 name = copyreldep(pool, frompool, name);
982 name = str2id(pool, id2str(frompool, name), 1);
984 evr = copyreldep(pool, frompool, evr);
986 evr = str2id(pool, id2str(frompool, evr), 1);
987 return rel2id(pool, name, evr, rd->flags, 1);
991 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
996 Pool *frompool = fromrepo->pool;
1000 from = fromrepo->idarraydata + fromoff;
1001 for (ida = from, cc = 0; *ida; ida++, cc++)
1005 ido = repo_reserve_ids(repo, 0, cc);
1006 ida = repo->idarraydata + ido;
1007 if (frompool && pool != frompool)
1013 id = copyreldep(pool, frompool, id);
1015 id = str2id(pool, id2str(frompool, id), 1);
1021 memcpy(ida, from, (cc + 1) * sizeof(Id));
1022 repo->idarraysize += cc + 1;
1026 #define COPYDIR_DIRCACHE_SIZE 512
1028 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1031 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1033 if (cache && cache[did & 255] == did)
1034 return cache[(did & 255) + 256];
1035 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1039 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1041 Id parent = dirpool_parent(&fromdata->dirpool, did);
1042 Id compid = dirpool_compid(&fromdata->dirpool, did);
1044 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1045 if (fromspool != &pool->ss)
1046 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1047 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1050 cache[did & 255] = did;
1051 cache[(did & 255) + 256] = compid;
1056 struct solvable_copy_cbdata {
1063 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1065 struct solvable_copy_cbdata *cbdata = vcbdata;
1067 Repodata *data = cbdata->data;
1068 Id handle = cbdata->handle;
1069 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1070 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1072 keyname = key->name;
1073 if (keyname >= ID_NUM_INTERNAL)
1074 keyname = str2id(pool, id2str(frompool, keyname), 1);
1077 case REPOKEY_TYPE_ID:
1078 case REPOKEY_TYPE_CONSTANTID:
1080 assert(!data->localpool); /* implement me! */
1081 if (pool != frompool || fromdata->localpool)
1084 id = copyreldep(pool, frompool, id);
1086 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1088 if (key->type == REPOKEY_TYPE_ID)
1089 repodata_set_id(data, handle, keyname, id);
1091 repodata_set_constantid(data, handle, keyname, id);
1093 case REPOKEY_TYPE_STR:
1094 repodata_set_str(data, handle, keyname, kv->str);
1096 case REPOKEY_TYPE_VOID:
1097 repodata_set_void(data, handle, keyname);
1099 case REPOKEY_TYPE_NUM:
1100 repodata_set_num(data, handle, keyname, kv->num);
1102 case REPOKEY_TYPE_CONSTANT:
1103 repodata_set_constant(data, handle, keyname, kv->num);
1105 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1107 assert(!data->localpool); /* implement me! */
1108 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1109 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1111 case REPOKEY_TYPE_DIRSTRARRAY:
1113 assert(!data->localpool); /* implement me! */
1114 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1115 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1124 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1126 Repo *repo = s->repo;
1127 Repo *fromrepo = r->repo;
1128 Pool *pool = repo->pool;
1129 struct solvable_copy_cbdata cbdata;
1131 /* copy solvable data */
1132 if (pool == fromrepo->pool)
1137 s->vendor = r->vendor;
1142 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1144 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1146 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1148 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1150 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1151 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1152 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1153 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1154 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1155 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1156 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1157 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1159 /* copy all attributes */
1163 cbdata.handle = s - pool->solvables;
1164 cbdata.dircache = dircache;
1165 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1168 /* used to sort entries returned in some database order */
1170 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1172 struct rpmid const *a = va, *b = vb;
1174 r = strcmp(a->name, b->name);
1177 return a->dbid - b->dbid;
1181 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1184 Pool *pool = repo->pool;
1185 Solvable *a = pool->solvables + *(Id *)va;
1186 Solvable *b = pool->solvables + *(Id *)vb;
1189 if (a->name != b->name)
1190 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1191 rpmdbid = repo->rpmdbid;
1192 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1196 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1198 Pool *pool = repo->pool;
1201 tmp = pool->solvables[pa];
1202 pool->solvables[pa] = pool->solvables[pb];
1203 pool->solvables[pb] = tmp;
1206 Id tmpid = repo->rpmdbid[pa - repo->start];
1207 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1208 repo->rpmdbid[pb - repo->start] = tmpid;
1210 /* only works if nothing is already internalized! */
1211 if (data && data->attrs)
1213 Id *tmpattrs = data->attrs[pa - data->start];
1214 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1215 data->attrs[pb - data->start] = tmpattrs;
1220 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1222 memset(cookie, 0, 32);
1223 cookie[3] = RPMDB_COOKIE_VERSION;
1224 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1225 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1228 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1230 opendbenv(const char *rootdir)
1232 char dbpath[PATH_MAX];
1236 if (db_env_create(&dbenv, 0))
1238 perror("db_env_create");
1241 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1242 dbenv->set_thread_count(dbenv, 8);
1244 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1245 if (access(dbpath, W_OK) == -1)
1247 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1252 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1254 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1259 perror("dbenv open");
1260 dbenv->close(dbenv, 0);
1268 count_headers(const char *rootdir, DB_ENV *dbenv)
1270 char dbpath[PATH_MAX];
1271 struct stat statbuf;
1279 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1280 if (stat(dbpath, &statbuf))
1282 memset(&dbkey, 0, sizeof(dbkey));
1283 memset(&dbdata, 0, sizeof(dbdata));
1284 if (db_create(&db, dbenv, 0))
1286 perror("db_create");
1289 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1291 perror("db->open Name index");
1294 if (db->get_byteswapped(db, &byteswapped))
1296 perror("db->get_byteswapped");
1299 if (db->cursor(db, NULL, &dbc, 0))
1301 perror("db->cursor");
1304 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1305 count += dbdata.size >> 3;
1312 * read rpm db as repo
1317 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1319 Pool *pool = repo->pool;
1320 unsigned char buf[16];
1325 unsigned char *dp, *dbidp;
1327 struct rpmid *rpmids, *rp;
1333 unsigned int refmask, h;
1334 char dbpath[PATH_MAX];
1338 struct stat packagesstat;
1339 unsigned char newcookie[32];
1340 const unsigned char *oldcookie = 0;
1341 Id oldcookietype = 0;
1343 int count = 0, done = 0;
1346 now = sat_timems(0);
1347 memset(&dbkey, 0, sizeof(dbkey));
1348 memset(&dbdata, 0, sizeof(dbdata));
1353 data = repo_add_repodata(repo, flags);
1355 if (ref && !(ref->nsolvables && ref->rpmdbid))
1358 if (!(dbenv = opendbenv(rootdir)))
1361 /* XXX: should get ro lock of Packages database! */
1362 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1363 if (stat(dbpath, &packagesstat))
1368 mkrpmdbcookie(&packagesstat, newcookie);
1369 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1372 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1373 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1376 int solvstart = 0, solvend = 0;
1378 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1379 count = count_headers(rootdir, dbenv);
1380 if (db_create(&db, dbenv, 0))
1382 perror("db_create");
1385 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1387 perror("db->open Packages index");
1390 if (db->get_byteswapped(db, &byteswapped))
1392 perror("db->get_byteswapped");
1395 if (db->cursor(db, NULL, &dbc, 0))
1397 perror("db->cursor");
1400 dbidp = (unsigned char *)&dbid;
1405 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1409 s = pool_id2solvable(pool, repo_add_solvable(repo));
1411 solvstart = s - pool->solvables;
1412 solvend = s - pool->solvables + 1;
1415 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1416 if (dbkey.size != 4)
1418 fprintf(stderr, "corrupt Packages database (key size)\n");
1430 memcpy(dbidp, dp, 4);
1431 if (dbid == 0) /* the join key */
1433 if (dbdata.size < 8)
1435 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1438 if (dbdata.size > rpmheadsize)
1440 rpmheadsize = dbdata.size + 128;
1441 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1443 memcpy(buf, dbdata.data, 8);
1444 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1445 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1446 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1448 fprintf(stderr, "corrupt rpm database (data size)\n");
1451 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1452 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1453 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1454 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1461 /* We can reuse this solvable, but make sure it's still
1462 associated with this repo. */
1463 memset(s, 0, sizeof(*s));
1466 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1470 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1471 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1476 /* oops, could not reuse. free it instead */
1477 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1484 /* now sort all solvables in the new solvstart..solvend block */
1485 if (solvend - solvstart > 1)
1487 pkgids = sat_malloc2(solvend - solvstart, sizeof(Id));
1488 for (i = solvstart; i < solvend; i++)
1489 pkgids[i - solvstart] = i;
1490 sat_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1492 for (i = solvstart; i < solvend; i++)
1494 int j = pkgids[i - solvstart];
1496 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1498 swap_solvables(repo, data, i, j);
1505 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1507 memset(dircache, 0, sizeof(dircache));
1508 if (db_create(&db, dbenv, 0))
1510 perror("db_create");
1513 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1515 perror("db->open Name index");
1518 if (db->get_byteswapped(db, &byteswapped))
1520 perror("db->get_byteswapped");
1523 if (db->cursor(db, NULL, &dbc, 0))
1525 perror("db->cursor");
1528 dbidp = (unsigned char *)&dbid;
1531 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1533 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1547 memcpy(dbidp, dp, 4);
1548 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1549 rpmids[nrpmids].dbid = dbid;
1550 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1551 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1552 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1563 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1565 dbidp = (unsigned char *)&dbid;
1569 /* create hash from dbid to ref */
1570 refmask = mkmask(ref->nsolvables);
1571 refhash = sat_calloc(refmask + 1, sizeof(Id));
1572 for (i = 0; i < ref->end - ref->start; i++)
1574 if (!ref->rpmdbid[i])
1576 h = ref->rpmdbid[i] & refmask;
1578 h = (h + 317) & refmask;
1579 refhash[h] = i + 1; /* make it non-zero */
1582 /* count the misses, they will cost us time */
1583 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1585 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1591 while ((id = refhash[h]))
1593 if (ref->rpmdbid[id - 1] == dbid)
1595 h = (h + 317) & refmask;
1604 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1606 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1608 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1611 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1615 while ((id = refhash[h]))
1617 if (ref->rpmdbid[id - 1] == dbid)
1619 h = (h + 317) & refmask;
1623 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1626 solvable_copy(s, r, data, dircache);
1633 if (db_create(&db, dbenv, 0))
1635 perror("db_create");
1638 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1640 perror("db->open var/lib/rpm/Packages");
1643 if (db->get_byteswapped(db, &byteswapped))
1645 perror("db->get_byteswapped");
1657 memcpy(buf, dbidp, 4);
1662 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1665 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1666 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1669 if (dbdata.size < 8)
1671 fprintf(stderr, "corrupt rpm database (size)\n");
1674 if (dbdata.size > rpmheadsize)
1676 rpmheadsize = dbdata.size + 128;
1677 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1679 memcpy(buf, dbdata.data, 8);
1680 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1681 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1682 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1684 fprintf(stderr, "corrupt rpm database (data size)\n");
1687 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1688 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1690 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1691 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1695 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1696 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1704 for (i = 0; i < nrpmids; i++)
1705 sat_free(rpmids[i].name);
1711 dbenv->close(dbenv, 0);
1714 if (!(flags & REPO_NO_INTERNALIZE))
1715 repodata_internalize(data);
1716 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1717 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1718 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1719 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1720 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1724 static inline unsigned int
1725 getu32(const unsigned char *dp)
1727 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1732 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1734 int i, sigdsize, sigcnt, l;
1735 Pool *pool = repo->pool;
1737 RpmHead *rpmhead = 0;
1738 int rpmheadsize = 0;
1739 char *payloadformat;
1741 unsigned char lead[4096];
1742 int headerstart, headerend;
1745 unsigned char pkgid[16];
1750 data = repo_add_repodata(repo, flags);
1752 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1753 chksumtype = REPOKEY_TYPE_SHA256;
1754 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1755 chksumtype = REPOKEY_TYPE_SHA1;
1756 for (i = 0; i < nrpms; i++)
1758 if ((fp = fopen(rpms[i], "r")) == 0)
1763 if (fstat(fileno(fp), &stb))
1769 chksumh = sat_chksum_free(chksumh, 0);
1771 chksumh = sat_chksum_create(chksumtype);
1772 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1774 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1779 sat_chksum_add(chksumh, lead, 96 + 16);
1780 if (lead[78] != 0 || lead[79] != 5)
1782 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1786 if (getu32(lead + 96) != 0x8eade801)
1788 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1792 sigcnt = getu32(lead + 96 + 8);
1793 sigdsize = getu32(lead + 96 + 12);
1794 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1796 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1800 sigdsize += sigcnt * 16;
1801 sigdsize = (sigdsize + 7) & ~7;
1802 headerstart = 96 + 16 + sigdsize;
1804 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1806 unsigned char *chksum;
1807 unsigned int chksumsize;
1808 /* extract pkgid from the signature header */
1809 if (sigdsize > rpmheadsize)
1811 rpmheadsize = sigdsize + 128;
1812 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1814 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1816 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1821 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1822 rpmhead->cnt = sigcnt;
1823 rpmhead->dcnt = sigdsize - sigcnt * 16;
1824 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1825 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1826 if (chksum && chksumsize == 16)
1829 memcpy(pkgid, chksum, 16);
1834 /* just skip the signature header */
1837 l = sigdsize > 4096 ? 4096 : sigdsize;
1838 if (fread(lead, l, 1, fp) != 1)
1840 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1845 sat_chksum_add(chksumh, lead, l);
1849 if (fread(lead, 16, 1, fp) != 1)
1851 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1856 sat_chksum_add(chksumh, lead, 16);
1857 if (getu32(lead) != 0x8eade801)
1859 fprintf(stderr, "%s: bad header\n", rpms[i]);
1863 sigcnt = getu32(lead + 8);
1864 sigdsize = getu32(lead + 12);
1865 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1867 fprintf(stderr, "%s: bad header\n", rpms[i]);
1871 l = sigdsize + sigcnt * 16;
1872 headerend = headerstart + 16 + l;
1873 if (l > rpmheadsize)
1875 rpmheadsize = l + 128;
1876 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1878 if (fread(rpmhead->data, l, 1, fp) != 1)
1880 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1885 sat_chksum_add(chksumh, rpmhead->data, l);
1886 rpmhead->cnt = sigcnt;
1887 rpmhead->dcnt = sigdsize;
1888 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1889 if (headexists(rpmhead, TAG_PATCHESNAME))
1891 /* this is a patch rpm, ignore */
1895 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1896 if (payloadformat && !strcmp(payloadformat, "drpm"))
1898 /* this is a delta rpm */
1903 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1904 sat_chksum_add(chksumh, lead, l);
1906 s = pool_id2solvable(pool, repo_add_solvable(repo));
1907 rpm2solv(pool, repo, data, s, rpmhead, flags);
1910 Id handle = s - pool->solvables;
1911 repodata_set_location(data, handle, 0, 0, rpms[i]);
1912 if (S_ISREG(stb.st_mode))
1913 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1914 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1916 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1918 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1922 chksumh = sat_chksum_free(chksumh, 0);
1925 if (!(flags & REPO_NO_INTERNALIZE))
1926 repodata_internalize(data);
1930 linkhash(const char *lt, char *hash)
1933 const unsigned char *str = (const unsigned char *)lt;
1937 while ((c = *str++) != 0)
1939 sprintf(hash, "%08x", r);
1940 sprintf(hash + 8, "%08x", l);
1941 sprintf(hash + 16, "%08x", 0);
1942 sprintf(hash + 24, "%08x", 0);
1946 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1948 RpmHead *rpmhead = rpmhandle;
1953 unsigned int *di, diidx;
1954 unsigned int *co = 0;
1955 unsigned int lastdir;
1958 int cnt, dcnt, cnt2;
1962 char md5[33], *md5p = 0;
1964 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1967 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1969 for (i = 0; i < dcnt; i++)
1970 (*cb)(cbdata, dn[i], 0, (char *)0);
1974 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1980 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1981 if (!di || cnt != cnt2)
1988 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1989 if (!fm || cnt != cnt2)
1997 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1999 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2000 if (!md || cnt != cnt2)
2010 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2012 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2013 if (!co || cnt != cnt2)
2026 for (i = 0; i < cnt; i++)
2031 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2034 l = l1 + strlen(bn[i]) + 1;
2038 space = sat_realloc(space, spacen);
2040 if (lastdir != diidx)
2042 strcpy(space, dn[diidx]);
2046 strcpy(space + l1, bn[i]);
2055 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2061 linkhash(lt[i], md5);
2067 sprintf(md5, "%08x%08x", (fm[i] >> 12) & 65535, 0);
2071 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2084 rpm_query(void *rpmhandle, Id what)
2086 const char *name, *arch, *sourcerpm;
2090 RpmHead *rpmhead = rpmhandle;
2095 name = headstring(rpmhead, TAG_NAME);
2098 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2100 arch = headstring(rpmhead, TAG_ARCH);
2103 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2110 evr = headtoevr(rpmhead);
2111 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2113 sprintf(r, "%s-%s.%s", name, evr, arch);
2117 name = headstring(rpmhead, TAG_NAME);
2121 r = headtoevr(rpmhead);
2128 struct rpm_by_state {
2143 #define ENTRIES_BLOCK 255
2144 #define NAMEDATA_BLOCK 1023
2146 static struct rpmdbentry *
2147 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2161 struct rpmdbentry *entries = 0;
2167 dbenv = state->dbenv;
2168 if (db_create(&db, dbenv, 0))
2170 perror("db_create");
2173 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2175 perror("db->open index");
2179 if (db->get_byteswapped(db, &byteswapped))
2181 perror("db->get_byteswapped");
2185 if (db->cursor(db, NULL, &dbc, 0))
2187 perror("db->cursor");
2191 memset(&dbkey, 0, sizeof(dbkey));
2192 memset(&dbdata, 0, sizeof(dbdata));
2195 dbkey.data = (void *)match;
2196 dbkey.size = strlen(match);
2198 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2200 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2208 ((char *)&rpmdbid)[0] = dp[3];
2209 ((char *)&rpmdbid)[1] = dp[2];
2210 ((char *)&rpmdbid)[2] = dp[1];
2211 ((char *)&rpmdbid)[3] = dp[0];
2214 memcpy((char *)&rpmdbid, dp, 4);
2215 entries = sat_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2216 entries[nentries].rpmdbid = rpmdbid;
2217 entries[nentries].nameoff = namedatal;
2219 namedata = sat_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2220 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2221 namedata[namedatal + dbkey.size] = 0;
2222 namedatal += dbkey.size + 1;
2231 *nentriesp = nentries;
2232 *namedatap = namedata;
2237 freestate(struct rpm_by_state *state)
2243 state->db->close(state->db, 0);
2245 state->dbenv->close(state->dbenv, 0);
2246 sat_free(state->rpmhead);
2250 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2252 struct rpm_by_state state;
2253 struct rpmdbentry *entries;
2260 queue_empty(rpmdbidq);
2261 memset(&state, 0, sizeof(state));
2262 if (!(state.dbenv = opendbenv(rootdir)))
2264 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2266 for (i = 0; i < nentries; i++)
2267 queue_push(rpmdbidq, entries[i].rpmdbid);
2275 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2277 struct rpm_by_state *state = *statep;
2278 unsigned char buf[16];
2288 *statep = (void *)0;
2294 state = sat_calloc(1, sizeof(*state));
2297 if (!state->dbopened)
2299 state->dbopened = 1;
2300 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2302 if (db_create(&state->db, state->dbenv, 0))
2304 perror("db_create");
2306 state->dbenv->close(state->dbenv, 0);
2310 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2312 perror("db->open var/lib/rpm/Packages");
2313 state->db->close(state->db, 0);
2315 state->dbenv->close(state->dbenv, 0);
2319 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2321 perror("db->get_byteswapped");
2322 state->db->close(state->db, 0);
2324 state->dbenv->close(state->dbenv, 0);
2329 memcpy(buf, &rpmdbid, 4);
2330 if (state->byteswapped)
2333 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2334 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2336 memset(&dbkey, 0, sizeof(dbkey));
2337 memset(&dbdata, 0, sizeof(dbdata));
2342 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2347 if (dbdata.size < 8)
2349 fprintf(stderr, "corrupt rpm database (size)\n");
2352 if (dbdata.size > state->rpmheadsize)
2354 state->rpmheadsize = dbdata.size + 128;
2355 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2357 rpmhead = state->rpmhead;
2358 memcpy(buf, dbdata.data, 8);
2359 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2360 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2361 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2363 fprintf(stderr, "corrupt rpm database (data size)\n");
2366 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2367 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2372 rpm_byfp(FILE *fp, const char *name, void **statep)
2374 struct rpm_by_state *state = *statep;
2375 int headerstart, headerend;
2377 int sigdsize, sigcnt, l;
2378 unsigned char lead[4096];
2381 return rpm_byrpmdbid(0, 0, statep);
2384 state = sat_calloc(1, sizeof(*state));
2387 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2389 fprintf(stderr, "%s: not a rpm\n", name);
2392 if (lead[78] != 0 || lead[79] != 5)
2394 fprintf(stderr, "%s: not a V5 header\n", name);
2397 if (getu32(lead + 96) != 0x8eade801)
2399 fprintf(stderr, "%s: bad signature header\n", name);
2402 sigcnt = getu32(lead + 96 + 8);
2403 sigdsize = getu32(lead + 96 + 12);
2404 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2406 fprintf(stderr, "%s: bad signature header\n", name);
2409 sigdsize += sigcnt * 16;
2410 sigdsize = (sigdsize + 7) & ~7;
2411 headerstart = 96 + 16 + sigdsize;
2414 l = sigdsize > 4096 ? 4096 : sigdsize;
2415 if (fread(lead, l, 1, fp) != 1)
2417 fprintf(stderr, "%s: unexpected EOF\n", name);
2422 if (fread(lead, 16, 1, fp) != 1)
2424 fprintf(stderr, "%s: unexpected EOF\n", name);
2427 if (getu32(lead) != 0x8eade801)
2429 fprintf(stderr, "%s: bad header\n", name);
2433 sigcnt = getu32(lead + 8);
2434 sigdsize = getu32(lead + 12);
2435 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2437 fprintf(stderr, "%s: bad header\n", name);
2441 l = sigdsize + sigcnt * 16;
2442 headerend = headerstart + 16 + l;
2443 if (l > state->rpmheadsize)
2445 state->rpmheadsize = l + 128;
2446 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2448 rpmhead = state->rpmhead;
2449 if (fread(rpmhead->data, l, 1, fp) != 1)
2451 fprintf(stderr, "%s: unexpected EOF\n", name);
2455 rpmhead->cnt = sigcnt;
2456 rpmhead->dcnt = sigdsize;
2457 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2462 rpm_byrpmh(Header h, void **statep)
2464 struct rpm_by_state *state = *statep;
2465 const unsigned char *uh;
2466 int sigdsize, sigcnt, l;
2469 uh = headerUnload(h);
2472 sigcnt = getu32(uh);
2473 sigdsize = getu32(uh + 4);
2474 l = sigdsize + sigcnt * 16;
2477 state = sat_calloc(1, sizeof(*state));
2480 if (l > state->rpmheadsize)
2482 state->rpmheadsize = l + 128;
2483 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2485 rpmhead = state->rpmhead;
2486 memcpy(rpmhead->data, uh + 8, l - 8);
2488 rpmhead->cnt = sigcnt;
2489 rpmhead->dcnt = sigdsize;
2490 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2496 r64dec1(char *p, unsigned int *vp, int *eofp)
2501 for (i = 0; i < 4; )
2506 if (x >= 'A' && x <= 'Z')
2508 else if (x >= 'a' && x <= 'z')
2510 else if (x >= '0' && x <= '9')
2537 crc24(unsigned char *p, int len)
2539 unsigned int crc = 0xb704ceL;
2544 crc ^= (*p++) << 16;
2545 for (i = 0; i < 8; i++)
2546 if ((crc <<= 1) & 0x1000000)
2549 return crc & 0xffffffL;
2552 static unsigned char *
2553 unarmor(char *pubkey, int *pktlp)
2557 unsigned char *buf, *bp;
2561 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2563 pubkey = strchr(pubkey, '\n');
2568 pubkey = strchr(pubkey, '\n');
2571 /* skip header lines */
2574 while (*pubkey == ' ' || *pubkey == '\t')
2576 if (*pubkey == '\n')
2578 pubkey = strchr(pubkey, '\n');
2583 p = strchr(pubkey, '=');
2587 bp = buf = sat_malloc(l * 3 / 4 + 4);
2591 pubkey = r64dec1(pubkey, &v, &eof);
2601 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2604 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2609 if (v != crc24(buf, bp - buf))
2614 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2616 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2626 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2629 unsigned char keyid[8];
2630 unsigned int kcr = 0, maxex = 0;
2631 unsigned char *pubkey = 0;
2633 unsigned char *userid = 0;
2636 for (; pl; p += l, pl -= l)
2640 if (!(x & 128) || pl <= 0)
2645 tag = (x & 0x3c) >> 2;
2667 else if (x >= 192 && x < 224)
2671 l = ((x - 192) << 8) + *p++ + 192;
2678 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2689 pubkey = sat_realloc(pubkey, l);
2691 memcpy(pubkey, p, l);
2698 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2702 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2706 memset(keyid, 0, 8);
2707 if (p[7] == 1) /* RSA */
2710 unsigned char fp[16];
2714 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2715 memcpy(keyid, p + 10 + ql - 8, 8);
2716 h = sat_chksum_create(REPOKEY_TYPE_MD5);
2717 sat_chksum_add(h, p + 10, ql);
2719 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2720 sat_chksum_add(h, q + 2, ql);
2721 sat_chksum_free(h, fp);
2722 for (i = 0; i < 16; i++)
2723 sprintf(fpx + i * 2, "%02x", fp[i]);
2724 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2731 unsigned char hdr[3];
2732 unsigned char fp[20];
2735 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2739 h = sat_chksum_create(REPOKEY_TYPE_SHA1);
2740 sat_chksum_add(h, hdr, 3);
2741 sat_chksum_add(h, p, l);
2742 sat_chksum_free(h, fp);
2743 for (i = 0; i < 20; i++)
2744 sprintf(fpx + i * 2, "%02x", fp[i]);
2745 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2746 memcpy(keyid, fp + 12, 8);
2751 if (p[0] == 3 && p[1] == 5)
2756 // printf("V3 signature packet\n");
2759 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2761 if (!memcmp(keyid, p + 6, 8))
2763 // printf("SELF SIG\n");
2767 // printf("OTHER SIG\n");
2771 htype = REPOKEY_TYPE_MD5;
2772 else if (p[16] == 2)
2773 htype = REPOKEY_TYPE_SHA1;
2774 else if (p[16] == 8)
2775 htype = REPOKEY_TYPE_SHA256;
2778 void *h = sat_chksum_create(htype);
2779 unsigned char b[3], *cs;
2782 b[1] = pubkeyl >> 8;
2784 sat_chksum_add(h, b, 3);
2785 sat_chksum_add(h, pubkey, pubkeyl);
2786 if (p[2] >= 0x10 && p[2] <= 0x13)
2787 sat_chksum_add(h, userid, useridl);
2788 sat_chksum_add(h, p + 2, 5);
2789 cs = sat_chksum_get(h, 0);
2790 sat_chksum_free(h, 0);
2796 int j, ql, haveissuer;
2798 unsigned int ex = 0, scr = 0;
2799 unsigned char issuer[8];
2801 // printf("V4 signature packet\n");
2804 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2809 for (j = 0; q && j < 2; j++)
2816 ql = q[0] << 8 | q[1];
2837 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2848 sl = ((x - 192) << 8) + *q++ + 192;
2857 // printf("%d SIGSUB %d %d\n", j, x, sl);
2858 if (x == 16 && sl == 9 && !haveissuer)
2860 memcpy(issuer, q + 1, 8);
2863 if (x == 2 && j == 0)
2864 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2865 if (x == 9 && j == 0)
2866 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2878 htype = REPOKEY_TYPE_MD5;
2880 htype = REPOKEY_TYPE_SHA1;
2882 htype = REPOKEY_TYPE_SHA256;
2883 if (htype && pubkeyl)
2885 void *h = sat_chksum_create(htype);
2886 unsigned char b[6], *cs;
2890 b[1] = pubkeyl >> 8;
2892 sat_chksum_add(h, b, 3);
2893 sat_chksum_add(h, pubkey, pubkeyl);
2894 if (p[1] >= 0x10 && p[1] <= 0x13)
2897 b[1] = useridl >> 24;
2898 b[2] = useridl >> 16;
2899 b[3] = useridl >> 8;
2901 sat_chksum_add(h, b, 5);
2902 sat_chksum_add(h, userid, useridl);
2904 hl = 6 + (p[4] << 8 | p[5]);
2905 sat_chksum_add(h, p, hl);
2912 sat_chksum_add(h, b, 6);
2913 cs = sat_chksum_get(h, 0);
2914 sat_chksum_free(h, 0);
2917 if (!memcmp(keyid, issuer, 8))
2919 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2925 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2932 userid = sat_realloc(userid, l);
2934 memcpy(userid, p, l);
2939 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2944 /* this is private to rpm, but rpm lacks an interface to retrieve
2945 * the values. Sigh. */
2946 struct pgpDigParams_s {
2947 const char * userid;
2948 const unsigned char * hash;
2949 const char * params[4];
2951 unsigned char version; /*!< version number. */
2952 unsigned char time[4]; /*!< time that the key was created. */
2953 unsigned char pubkey_algo; /*!< public key algorithm. */
2954 unsigned char hash_algo;
2955 unsigned char sigtype;
2956 unsigned char hashlen;
2957 unsigned char signhash16[2];
2958 unsigned char signid[8];
2959 unsigned char saved;
2963 struct pgpDigParams_s signature;
2964 struct pgpDigParams_s pubkey;
2968 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
2970 Pool *pool = s->repo->pool;
2971 unsigned char *pkts;
2976 char evrbuf[8 + 1 + 8 + 1];
2978 pkts = unarmor(pubkey, &pktsl);
2981 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
2982 parsekeydata(s, data, pkts, pktsl);
2983 /* only rpm knows how to do the release calculation, we don't dare
2984 * to recreate all the bugs */
2986 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
2987 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
2988 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]);
2989 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
2991 s->name = str2id(pool, "gpg-pubkey", 1);
2992 s->evr = str2id(pool, evrbuf, 1);
2994 for (i = 0; i < 8; i++)
2995 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
2996 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
2997 if (dig->pubkey.userid)
2998 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3000 sat_free((void *)pkts);
3005 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
3007 Pool *pool = repo->pool;
3008 struct rpm_by_state state;
3009 struct rpmdbentry *entries;
3011 char *namedata, *str;
3016 data = repo_add_repodata(repo, flags);
3018 memset(&state, 0, sizeof(state));
3019 if (!(state.dbenv = opendbenv(rootdir)))
3021 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3022 for (i = 0 ; i < nentries; i++)
3024 void *statep = &state;
3025 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3028 str = headstring(rpmhead, TAG_DESCRIPTION);
3031 s = pool_id2solvable(pool, repo_add_solvable(repo));
3032 pubkey2solvable(s, data, str);
3033 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3035 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3037 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3038 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3041 if (!(flags & REPO_NO_INTERNALIZE))
3042 repodata_internalize(data);
3046 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
3048 Pool *pool = repo->pool;
3055 data = repo_add_repodata(repo, flags);
3058 for (i = 0; i < nkeys; i++)
3060 if ((fp = fopen(keys[i], "r")) == 0)
3067 if (bufl - l < 4096)
3070 buf = sat_realloc(buf, bufl);
3072 ll = fread(buf, 1, bufl - l, fp);
3079 s = pool_id2solvable(pool, repo_add_solvable(repo));
3080 pubkey2solvable(s, data, buf);
3083 if (!(flags & REPO_NO_INTERNALIZE))
3084 repodata_internalize(data);