2 * Copyright (c) 2007-2012, 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>
26 #include <rpm/rpmio.h>
27 #include <rpm/rpmpgp.h>
29 #include <rpm/header.h>
31 #include <rpm/rpmdb.h>
47 #include "repo_rpmdb.h"
49 /* 3: added triggers */
50 /* 4: fixed triggers */
51 #define RPMDB_COOKIE_VERSION 4
54 #define TAG_VERSION 1001
55 #define TAG_RELEASE 1002
56 #define TAG_EPOCH 1003
57 #define TAG_SUMMARY 1004
58 #define TAG_DESCRIPTION 1005
59 #define TAG_BUILDTIME 1006
60 #define TAG_BUILDHOST 1007
61 #define TAG_INSTALLTIME 1008
63 #define TAG_DISTRIBUTION 1010
64 #define TAG_VENDOR 1011
65 #define TAG_LICENSE 1014
66 #define TAG_PACKAGER 1015
67 #define TAG_GROUP 1016
70 #define TAG_FILESIZES 1028
71 #define TAG_FILEMODES 1030
72 #define TAG_FILEMD5S 1035
73 #define TAG_FILELINKTOS 1036
74 #define TAG_FILEFLAGS 1037
75 #define TAG_SOURCERPM 1044
76 #define TAG_PROVIDENAME 1047
77 #define TAG_REQUIREFLAGS 1048
78 #define TAG_REQUIRENAME 1049
79 #define TAG_REQUIREVERSION 1050
80 #define TAG_NOSOURCE 1051
81 #define TAG_NOPATCH 1052
82 #define TAG_CONFLICTFLAGS 1053
83 #define TAG_CONFLICTNAME 1054
84 #define TAG_CONFLICTVERSION 1055
85 #define TAG_TRIGGERNAME 1066
86 #define TAG_TRIGGERVERSION 1067
87 #define TAG_TRIGGERFLAGS 1068
88 #define TAG_OBSOLETENAME 1090
89 #define TAG_FILEDEVICES 1095
90 #define TAG_FILEINODES 1096
91 #define TAG_PROVIDEFLAGS 1112
92 #define TAG_PROVIDEVERSION 1113
93 #define TAG_OBSOLETEFLAGS 1114
94 #define TAG_OBSOLETEVERSION 1115
95 #define TAG_DIRINDEXES 1116
96 #define TAG_BASENAMES 1117
97 #define TAG_DIRNAMES 1118
98 #define TAG_PAYLOADFORMAT 1124
99 #define TAG_PATCHESNAME 1133
100 #define TAG_FILECOLORS 1140
101 #define TAG_SUGGESTSNAME 1156
102 #define TAG_SUGGESTSVERSION 1157
103 #define TAG_SUGGESTSFLAGS 1158
104 #define TAG_ENHANCESNAME 1159
105 #define TAG_ENHANCESVERSION 1160
106 #define TAG_ENHANCESFLAGS 1161
108 #define SIGTAG_SIZE 1000
109 #define SIGTAG_PGP 1002 /* RSA signature */
110 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
111 #define SIGTAG_GPG 1005 /* DSA signature */
113 #define DEP_LESS (1 << 1)
114 #define DEP_GREATER (1 << 2)
115 #define DEP_EQUAL (1 << 3)
116 #define DEP_STRONG (1 << 27)
117 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
119 #define FILEFLAG_GHOST (1 << 6)
123 # define RPM_INDEX_SIZE 4
124 # define TAG_DISTEPOCH 1218
126 # define RPM_INDEX_SIZE 8
134 typedef struct rpmhead {
138 unsigned char data[1];
142 static inline unsigned char *
143 headfindtag(RpmHead *h, int tag)
146 unsigned char *d, taga[4];
152 for (i = 0; i < h->cnt; i++, d -= 16)
153 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
159 headexists(RpmHead *h, int tag)
161 return headfindtag(h, tag) ? 1 : 0;
164 static unsigned int *
165 headint32array(RpmHead *h, int tag, int *cnt)
167 unsigned int i, o, *r;
168 unsigned char *d = headfindtag(h, tag);
170 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
172 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
173 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
174 if (o + 4 * i > h->dcnt)
177 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
180 for (o = 0; o < i; o++, d += 4)
181 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
185 /* returns the first entry of an integer array */
187 headint32(RpmHead *h, int tag)
190 unsigned char *d = headfindtag(h, tag);
192 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
194 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
195 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
196 if (i == 0 || o + 4 * i > h->dcnt)
199 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
202 static unsigned int *
203 headint16array(RpmHead *h, int tag, int *cnt)
205 unsigned int i, o, *r;
206 unsigned char *d = headfindtag(h, tag);
208 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
210 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
211 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
212 if (o + 4 * i > h->dcnt)
215 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
218 for (o = 0; o < i; o++, d += 2)
219 r[o] = d[0] << 8 | d[1];
224 headstring(RpmHead *h, int tag)
227 unsigned char *d = headfindtag(h, tag);
228 /* 6: STRING, 9: I18NSTRING */
229 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
231 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
234 return (char *)h->dp + o;
238 headstringarray(RpmHead *h, int tag, int *cnt)
241 unsigned char *d = headfindtag(h, tag);
244 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
246 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
247 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
248 r = solv_calloc(i ? i : 1, sizeof(char *));
252 for (o = 0; o < i; o++)
256 d += strlen((char *)d) + 1;
257 if (d >= h->dp + h->dcnt)
266 static unsigned char *
267 headbinary(RpmHead *h, int tag, unsigned int *sizep)
270 unsigned char *d = headfindtag(h, tag);
271 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
273 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
274 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
275 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
282 static char *headtoevr(RpmHead *h)
292 version = headstring(h, TAG_VERSION);
293 release = headstring(h, TAG_RELEASE);
294 epoch = headint32(h, TAG_EPOCH);
295 if (!version || !release)
297 fprintf(stderr, "headtoevr: bad rpm header\n");
300 for (v = version; *v >= '0' && *v <= '9'; v++)
302 if (epoch || (v != version && *v == ':'))
304 char epochbuf[11]; /* 32bit decimal will fit in */
305 sprintf(epochbuf, "%u", epoch);
306 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
307 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
311 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
312 sprintf(evr, "%s-%s", version, release);
315 distepoch = headstring(h, TAG_DISTEPOCH);
316 if (distepoch && *distepoch)
319 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
321 strcpy(evr + l, distepoch);
329 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
331 const unsigned char *cp;
334 unsigned char *buf = 0, *bp;
336 /* check if it's already utf8, code taken from screen ;-) */
337 cp = (const unsigned char *)str;
338 while ((c = *cp++) != 0)
342 if ((c & 0xc0) != 0x80)
343 break; /* encoding error */
344 c = (c & 0x3f) | (state << 6);
345 if (!(state & 0x40000000))
347 /* check for overlong sequences */
348 if ((c & 0x820823e0) == 0x80000000)
350 else if ((c & 0x020821f0) == 0x02000000)
352 else if ((c & 0x000820f8) == 0x00080000)
354 else if ((c & 0x0000207c) == 0x00002000)
364 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
366 c = (c & 0x03) | 0xbfffff00; /* 4 */
368 c = (c & 0x07) | 0xbfffc000; /* 3 */
370 c = (c & 0x0f) | 0xbff00000; /* 2 */
372 c = (c & 0x1f) | 0xfc000000; /* 1 */
376 state = (c & 0x80000000) ? c : 0;
380 /* not utf8, assume latin1 */
381 buf = solv_malloc(2 * strlen(str) + 1);
382 cp = (const unsigned char *)str;
385 while ((c = *cp++) != 0)
398 repodata_set_str(repodata, handle, tag, str);
404 #define MAKEDEPS_FILTER_WEAK (1 << 0)
405 #define MAKEDEPS_FILTER_STRONG (1 << 1)
406 #define MAKEDEPS_NO_RPMLIB (1 << 2)
409 * strong: 0: ignore strongness
410 * 1: filter to strong
414 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
418 int i, cc, nc, vc, fc;
420 unsigned int olddeps;
424 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
425 n = headstringarray(rpmhead, tagn, &nc);
428 v = headstringarray(rpmhead, tagv, &vc);
434 f = headint32array(rpmhead, tagf, &fc);
441 if (nc != vc || nc != fc)
443 fprintf(stderr, "bad dependency entries\n");
448 haspre = 0; /* add no prereq marker */
451 /* we do filtering */
453 for (i = 0; i < nc; i++)
455 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
457 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
458 if (!strncmp(n[i], "rpmlib(", 7))
460 if ((f[i] & DEP_PRE) != 0)
465 else if (tagn == TAG_REQUIRENAME)
467 /* no filtering, just look for the first prereq */
468 for (i = 0; i < nc; i++)
469 if ((f[i] & DEP_PRE) != 0)
483 olddeps = repo_reserve_ids(repo, 0, cc);
484 ida = repo->idarraydata + olddeps;
491 haspre = 2; /* pass two: prereqs */
493 *ida++ = SOLVABLE_PREREQMARKER;
495 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
497 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
499 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
501 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
502 if (!strncmp(n[i], "rpmlib(", 7))
504 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
508 if ((f[i] & DEP_LESS) != 0)
510 if ((f[i] & DEP_EQUAL) != 0)
512 if ((f[i] & DEP_GREATER) != 0)
514 name = pool_str2id(pool, n[i], 1);
515 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
516 evr = pool_str2id(pool, v[i] + 2, 1);
518 evr = pool_str2id(pool, v[i], 1);
519 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
522 *ida++ = pool_str2id(pool, n[i], 1);
525 repo->idarraysize += cc + 1;
533 #ifdef USE_FILEFILTER
535 #define FILEFILTER_EXACT 0
536 #define FILEFILTER_STARTS 1
537 #define FILEFILTER_CONTAINS 2
545 static struct filefilter filefilters[] = {
546 { FILEFILTER_CONTAINS, "/bin/", 0},
547 { FILEFILTER_CONTAINS, "/sbin/", 0},
548 { FILEFILTER_CONTAINS, "/lib/", 0},
549 { FILEFILTER_CONTAINS, "/lib64/", 0},
550 { FILEFILTER_CONTAINS, "/etc/", 0},
551 { FILEFILTER_STARTS, "/usr/games/", 0},
552 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
553 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
554 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
560 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
564 unsigned int *fkb, *fn, *fsz, *fm, *fino;
565 unsigned int inotest[256], inotestok;
569 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
570 if (!fsz || fc != fszc)
575 /* stupid rpm records sizes of directories, so we have to check the mode */
576 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
577 if (!fm || fc != fszc)
583 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
584 if (!fino || fc != fszc)
592 if (fc < sizeof(inotest))
594 memset(inotest, 0, sizeof(inotest));
595 for (i = 0; i < fc; i++)
598 if (fsz[i] == 0 || !S_ISREG(fm[i]))
600 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
601 bit = 1 << (fino[i] & 31);
602 if ((inotest[off] & bit) != 0)
611 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
613 unsigned int mask, hash, hh;
614 if (!fdev || fc != fszc)
623 while ((mask & (mask - 1)) != 0)
624 mask = mask & (mask - 1);
626 if (mask > sizeof(inotest)/sizeof(*inotest))
627 fx = solv_calloc(mask, sizeof(unsigned int));
631 memset(fx, 0, mask * sizeof(unsigned int));
634 for (i = 0; i < fc; i++)
636 if (fsz[i] == 0 || !S_ISREG(fm[i]))
638 hash = (fino[i] + fdev[i] * 31) & mask;
640 while ((j = fx[hash]) != 0)
642 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
644 fsz[i] = 0; /* kill entry */
647 hash = (hash + hh++) & mask;
657 fn = solv_calloc(dc, sizeof(unsigned int));
658 fkb = solv_calloc(dc, sizeof(unsigned int));
659 for (i = 0; i < fc; i++)
664 if (fsz[i] == 0 || !S_ISREG(fm[i]))
666 fkb[di[i]] += fsz[i] / 1024 + 1;
671 handle = s - pool->solvables;
672 for (i = 0; i < dc; i++)
678 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
679 did = repodata_str2dir(data, "/usr/src", 1);
681 continue; /* work around rpm bug */
684 did = repodata_str2dir(data, dn[i], 1);
685 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
691 /* assumes last processed array is provides! */
693 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
700 #ifdef USE_FILEFILTER
702 struct filefilter *ff;
711 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
714 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
720 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
729 fprintf(stderr, "bad filelist\n");
734 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
736 for (i = 0; i < bnc; i++)
738 #ifdef USE_FILEFILTER
740 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
744 switch (ff->dirmatch)
746 case FILEFILTER_STARTS:
747 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
750 case FILEFILTER_CONTAINS:
751 if (!strstr(dn[di[i]], ff->dir))
754 case FILEFILTER_EXACT:
756 if (strcmp(dn[di[i]], ff->dir))
763 if (strcmp(bn[i], ff->base))
768 if (j == sizeof(filefilters)/sizeof(*filefilters))
772 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
776 fn = solv_realloc(fn, fna);
778 strcpy(fn, dn[di[i]]);
780 olddeps = repo_addid_dep(repo, olddeps, pool_str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
787 handle = s - pool->solvables;
788 did = repodata_str2dir(data, dn[di[i]], 1);
791 did = repodata_str2dir(data, "/", 1);
792 if (b && b[0] == '/')
793 b++; /* work around rpm bug */
795 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
809 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
811 const char *p, *sevr, *sarch;
813 p = strrchr(sourcerpm, '.');
814 if (!p || strcmp(p, ".rpm") != 0)
817 while (p > sourcerpm && *p != '.')
819 if (*p != '.' || p == sourcerpm)
822 while (p > sourcerpm && *p != '-')
824 if (*p != '-' || p == sourcerpm)
827 while (p > sourcerpm && *p != '-')
829 if (*p != '-' || p == sourcerpm)
832 if (!strcmp(sarch, "src.rpm"))
833 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
834 else if (!strcmp(sarch, "nosrc.rpm"))
835 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
837 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
838 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
839 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
841 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
842 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
843 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
845 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
849 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
855 name = headstring(rpmhead, TAG_NAME);
856 if (!strcmp(name, "gpg-pubkey"))
858 s->name = pool_str2id(pool, name, 1);
861 fprintf(stderr, "package has no name\n");
864 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
866 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
869 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
870 s->arch = ARCH_NOSRC;
875 s->arch = ARCH_NOARCH;
876 evr = headtoevr(rpmhead);
877 s->evr = pool_str2id(pool, evr, 1);
878 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
880 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
881 if ((flags & RPM_ADD_NO_FILELIST) == 0)
882 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
883 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
884 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
885 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
886 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
887 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
889 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
890 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
891 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
892 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
893 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
894 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
902 handle = s - pool->solvables;
903 str = headstring(rpmhead, TAG_SUMMARY);
905 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
906 str = headstring(rpmhead, TAG_DESCRIPTION);
910 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
911 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
915 /* oh my, found SUSE special author section */
917 str = solv_strdup(str);
920 while (l > 0 && str[l - 1] == '\n')
923 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
925 aut = str; /* copy over */
926 while (*p == ' ' || *p == '\n')
939 while (aut != str && aut[-1] == '\n')
943 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
947 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
949 str = headstring(rpmhead, TAG_GROUP);
951 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
952 str = headstring(rpmhead, TAG_LICENSE);
954 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
955 str = headstring(rpmhead, TAG_URL);
957 repodata_set_str(data, handle, SOLVABLE_URL, str);
958 str = headstring(rpmhead, TAG_DISTRIBUTION);
960 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
961 str = headstring(rpmhead, TAG_PACKAGER);
963 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
964 u32 = headint32(rpmhead, TAG_BUILDTIME);
966 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
967 u32 = headint32(rpmhead, TAG_INSTALLTIME);
969 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
970 u32 = headint32(rpmhead, TAG_SIZE);
972 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
974 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
975 if ((flags & RPM_ADD_TRIGGERS) != 0)
978 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
981 for (; (id = repo->idarraydata[ida]) != 0; ida++)
983 /* we currently do not support rel ids in incore data, so
984 * strip off versioning information */
987 Reldep *rd = GETRELDEP(pool, id);
992 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1002 copyreldep(Pool *pool, Pool *frompool, Id id)
1004 Reldep *rd = GETRELDEP(frompool, id);
1005 Id name = rd->name, evr = rd->evr;
1007 name = copyreldep(pool, frompool, name);
1009 name = pool_str2id(pool, pool_id2str(frompool, name), 1);
1011 evr = copyreldep(pool, frompool, evr);
1013 evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1014 return pool_rel2id(pool, name, evr, rd->flags, 1);
1018 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1023 Pool *frompool = fromrepo->pool;
1027 from = fromrepo->idarraydata + fromoff;
1028 for (ida = from, cc = 0; *ida; ida++, cc++)
1032 ido = repo_reserve_ids(repo, 0, cc);
1033 ida = repo->idarraydata + ido;
1034 if (frompool && pool != frompool)
1040 id = copyreldep(pool, frompool, id);
1042 id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1048 memcpy(ida, from, (cc + 1) * sizeof(Id));
1049 repo->idarraysize += cc + 1;
1053 #define COPYDIR_DIRCACHE_SIZE 512
1055 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1058 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1060 if (cache && cache[did & 255] == did)
1061 return cache[(did & 255) + 256];
1062 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1066 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1068 Id parent = dirpool_parent(&fromdata->dirpool, did);
1069 Id compid = dirpool_compid(&fromdata->dirpool, did);
1071 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1072 if (fromspool != &pool->ss)
1073 compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1074 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1077 cache[did & 255] = did;
1078 cache[(did & 255) + 256] = compid;
1083 struct solvable_copy_cbdata {
1090 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1092 struct solvable_copy_cbdata *cbdata = vcbdata;
1094 Repodata *data = cbdata->data;
1095 Id handle = cbdata->handle;
1096 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1097 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1099 keyname = key->name;
1100 if (keyname >= ID_NUM_INTERNAL && pool != frompool)
1101 keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1104 case REPOKEY_TYPE_ID:
1105 case REPOKEY_TYPE_CONSTANTID:
1106 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1108 assert(!data->localpool); /* implement me! */
1109 if (pool != frompool || fromdata->localpool)
1112 id = copyreldep(pool, frompool, id);
1114 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1116 if (key->type == REPOKEY_TYPE_ID)
1117 repodata_set_id(data, handle, keyname, id);
1118 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1119 repodata_set_constantid(data, handle, keyname, id);
1121 repodata_add_idarray(data, handle, keyname, id);
1123 case REPOKEY_TYPE_STR:
1124 repodata_set_str(data, handle, keyname, kv->str);
1126 case REPOKEY_TYPE_VOID:
1127 repodata_set_void(data, handle, keyname);
1129 case REPOKEY_TYPE_NUM:
1130 repodata_set_num(data, handle, keyname, kv->num);
1132 case REPOKEY_TYPE_CONSTANT:
1133 repodata_set_constant(data, handle, keyname, kv->num);
1135 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1137 assert(!data->localpool); /* implement me! */
1138 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1139 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1141 case REPOKEY_TYPE_DIRSTRARRAY:
1143 assert(!data->localpool); /* implement me! */
1144 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1145 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1154 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1156 Repo *repo = s->repo;
1157 Repo *fromrepo = r->repo;
1158 Pool *pool = repo->pool;
1159 struct solvable_copy_cbdata cbdata;
1161 /* copy solvable data */
1162 if (pool == fromrepo->pool)
1167 s->vendor = r->vendor;
1172 s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1174 s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1176 s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1178 s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1180 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1181 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1182 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1183 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1184 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1185 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1186 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1187 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1189 /* copy all attributes */
1193 cbdata.handle = s - pool->solvables;
1194 cbdata.dircache = dircache;
1195 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1198 /* used to sort entries returned in some database order */
1200 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1202 struct rpmid const *a = va, *b = vb;
1204 r = strcmp(a->name, b->name);
1207 return a->dbid - b->dbid;
1211 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1214 Pool *pool = repo->pool;
1215 Solvable *a = pool->solvables + *(Id *)va;
1216 Solvable *b = pool->solvables + *(Id *)vb;
1219 if (a->name != b->name)
1220 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1221 rpmdbid = repo->rpmdbid;
1222 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1226 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1228 Pool *pool = repo->pool;
1231 tmp = pool->solvables[pa];
1232 pool->solvables[pa] = pool->solvables[pb];
1233 pool->solvables[pb] = tmp;
1236 Id tmpid = repo->rpmdbid[pa - repo->start];
1237 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1238 repo->rpmdbid[pb - repo->start] = tmpid;
1240 /* only works if nothing is already internalized! */
1242 repodata_swap_attrs(data, pa, pb);
1246 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1249 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1251 # if defined(WORDS_BIGENDIAN)
1256 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1258 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1262 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1265 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1267 # if defined(WORDS_BIGENDIAN)
1272 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1274 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1279 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1281 memset(cookie, 0, 32);
1282 cookie[3] = RPMDB_COOKIE_VERSION;
1283 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1284 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1287 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1289 opendbenv(const char *rootdir)
1291 char dbpath[PATH_MAX];
1295 if (db_env_create(&dbenv, 0))
1297 perror("db_env_create");
1300 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1301 dbenv->set_thread_count(dbenv, 8);
1303 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1304 if (access(dbpath, W_OK) == -1)
1306 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1311 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1313 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1318 perror("dbenv open");
1319 dbenv->close(dbenv, 0);
1327 count_headers(const char *rootdir, DB_ENV *dbenv)
1329 char dbpath[PATH_MAX];
1330 struct stat statbuf;
1337 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1338 if (stat(dbpath, &statbuf))
1340 memset(&dbkey, 0, sizeof(dbkey));
1341 memset(&dbdata, 0, sizeof(dbdata));
1342 if (db_create(&db, dbenv, 0))
1344 perror("db_create");
1347 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1349 perror("db->open Name index");
1352 if (db->cursor(db, NULL, &dbc, 0))
1354 perror("db->cursor");
1357 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1358 count += dbdata.size / RPM_INDEX_SIZE;
1365 * read rpm db as repo
1370 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1372 Pool *pool = repo->pool;
1373 unsigned char buf[16];
1380 struct rpmid *rpmids, *rp;
1386 unsigned int refmask, h;
1387 char dbpath[PATH_MAX];
1391 struct stat packagesstat;
1392 unsigned char newcookie[32];
1393 const unsigned char *oldcookie = 0;
1394 Id oldcookietype = 0;
1396 int count = 0, done = 0;
1399 now = solv_timems(0);
1400 memset(&dbkey, 0, sizeof(dbkey));
1401 memset(&dbdata, 0, sizeof(dbdata));
1406 data = repo_add_repodata(repo, flags);
1408 if (ref && !(ref->nsolvables && ref->rpmdbid))
1411 if (!(dbenv = opendbenv(rootdir)))
1414 /* XXX: should get ro lock of Packages database! */
1415 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1416 if (stat(dbpath, &packagesstat))
1421 mkrpmdbcookie(&packagesstat, newcookie);
1422 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1425 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1426 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1429 int solvstart = 0, solvend = 0;
1431 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1432 count = count_headers(rootdir, dbenv);
1433 if (db_create(&db, dbenv, 0))
1435 perror("db_create");
1438 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1440 perror("db->open Packages index");
1443 if (db->get_byteswapped(db, &byteswapped))
1445 perror("db->get_byteswapped");
1448 if (db->cursor(db, NULL, &dbc, 0))
1450 perror("db->cursor");
1457 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1461 s = pool_id2solvable(pool, repo_add_solvable(repo));
1463 solvstart = s - pool->solvables;
1464 solvend = s - pool->solvables + 1;
1467 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1468 if (dbkey.size != 4)
1470 fprintf(stderr, "corrupt Packages database (key size)\n");
1473 dbid = db2rpmdbid(dbkey.data, byteswapped);
1474 if (dbid == 0) /* the join key */
1476 if (dbdata.size < 8)
1478 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1481 if (dbdata.size > rpmheadsize)
1483 rpmheadsize = dbdata.size + 128;
1484 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1486 memcpy(buf, dbdata.data, 8);
1487 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1488 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1489 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1491 fprintf(stderr, "corrupt rpm database (data size)\n");
1494 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1495 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1496 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1497 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1504 /* We can reuse this solvable, but make sure it's still
1505 associated with this repo. */
1506 memset(s, 0, sizeof(*s));
1509 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1513 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1514 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1519 /* oops, could not reuse. free it instead */
1520 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1527 /* now sort all solvables in the new solvstart..solvend block */
1528 if (solvend - solvstart > 1)
1530 pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1531 for (i = solvstart; i < solvend; i++)
1532 pkgids[i - solvstart] = i;
1533 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1535 for (i = solvstart; i < solvend; i++)
1537 int j = pkgids[i - solvstart];
1539 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1541 swap_solvables(repo, data, i, j);
1548 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1550 memset(dircache, 0, sizeof(dircache));
1551 if (db_create(&db, dbenv, 0))
1553 perror("db_create");
1556 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1558 perror("db->open Name index");
1561 if (db->get_byteswapped(db, &byteswapped))
1563 perror("db->get_byteswapped");
1566 if (db->cursor(db, NULL, &dbc, 0))
1568 perror("db->cursor");
1573 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1575 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1579 while(dl >= RPM_INDEX_SIZE)
1581 rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1582 rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1583 rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1584 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1585 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1587 dp += RPM_INDEX_SIZE;
1588 dl -= RPM_INDEX_SIZE;
1596 solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1601 /* create hash from dbid to ref */
1602 refmask = mkmask(ref->nsolvables);
1603 refhash = solv_calloc(refmask + 1, sizeof(Id));
1604 for (i = 0; i < ref->end - ref->start; i++)
1606 if (!ref->rpmdbid[i])
1608 h = ref->rpmdbid[i] & refmask;
1610 h = (h + 317) & refmask;
1611 refhash[h] = i + 1; /* make it non-zero */
1614 /* count the misses, they will cost us time */
1615 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1617 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1623 while ((id = refhash[h]))
1625 if (ref->rpmdbid[id - 1] == dbid)
1627 h = (h + 317) & refmask;
1636 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1638 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1640 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1643 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1647 while ((id = refhash[h]))
1649 if (ref->rpmdbid[id - 1] == dbid)
1651 h = (h + 317) & refmask;
1655 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1658 solvable_copy(s, r, data, dircache);
1665 if (db_create(&db, dbenv, 0))
1667 perror("db_create");
1670 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1672 perror("db->open var/lib/rpm/Packages");
1675 if (db->get_byteswapped(db, &byteswapped))
1677 perror("db->get_byteswapped");
1681 rpmdbid2db(buf, rp->dbid, byteswapped);
1686 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1689 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1690 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1693 if (dbdata.size < 8)
1695 fprintf(stderr, "corrupt rpm database (size)\n");
1698 if (dbdata.size > rpmheadsize)
1700 rpmheadsize = dbdata.size + 128;
1701 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1703 memcpy(buf, dbdata.data, 8);
1704 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1705 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1706 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1708 fprintf(stderr, "corrupt rpm database (data size)\n");
1711 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1712 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1714 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1715 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1719 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1720 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1728 for (i = 0; i < nrpmids; i++)
1729 solv_free(rpmids[i].name);
1735 dbenv->close(dbenv, 0);
1738 if (!(flags & REPO_NO_INTERNALIZE))
1739 repodata_internalize(data);
1740 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1741 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1742 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1743 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1744 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1749 static inline unsigned int
1750 getu32(const unsigned char *dp)
1752 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1757 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1759 int i, sigdsize, sigcnt, l;
1760 Pool *pool = repo->pool;
1762 RpmHead *rpmhead = 0;
1763 int rpmheadsize = 0;
1764 char *payloadformat;
1766 unsigned char lead[4096];
1767 int headerstart, headerend;
1770 unsigned char pkgid[16];
1775 data = repo_add_repodata(repo, flags);
1777 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1778 chksumtype = REPOKEY_TYPE_SHA256;
1779 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1780 chksumtype = REPOKEY_TYPE_SHA1;
1781 for (i = 0; i < nrpms; i++)
1783 if ((fp = fopen(rpms[i], "r")) == 0)
1788 if (fstat(fileno(fp), &stb))
1794 chksumh = solv_chksum_free(chksumh, 0);
1796 chksumh = solv_chksum_create(chksumtype);
1797 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1799 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1804 solv_chksum_add(chksumh, lead, 96 + 16);
1805 if (lead[78] != 0 || lead[79] != 5)
1807 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1811 if (getu32(lead + 96) != 0x8eade801)
1813 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1817 sigcnt = getu32(lead + 96 + 8);
1818 sigdsize = getu32(lead + 96 + 12);
1819 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1821 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1825 sigdsize += sigcnt * 16;
1826 sigdsize = (sigdsize + 7) & ~7;
1827 headerstart = 96 + 16 + sigdsize;
1829 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1831 unsigned char *chksum;
1832 unsigned int chksumsize;
1833 /* extract pkgid from the signature header */
1834 if (sigdsize > rpmheadsize)
1836 rpmheadsize = sigdsize + 128;
1837 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1839 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1841 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1846 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1847 rpmhead->cnt = sigcnt;
1848 rpmhead->dcnt = sigdsize - sigcnt * 16;
1849 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1850 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1851 if (chksum && chksumsize == 16)
1854 memcpy(pkgid, chksum, 16);
1859 /* just skip the signature header */
1862 l = sigdsize > 4096 ? 4096 : sigdsize;
1863 if (fread(lead, l, 1, fp) != 1)
1865 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1870 solv_chksum_add(chksumh, lead, l);
1874 if (fread(lead, 16, 1, fp) != 1)
1876 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1881 solv_chksum_add(chksumh, lead, 16);
1882 if (getu32(lead) != 0x8eade801)
1884 fprintf(stderr, "%s: bad header\n", rpms[i]);
1888 sigcnt = getu32(lead + 8);
1889 sigdsize = getu32(lead + 12);
1890 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1892 fprintf(stderr, "%s: bad header\n", rpms[i]);
1896 l = sigdsize + sigcnt * 16;
1897 headerend = headerstart + 16 + l;
1898 if (l > rpmheadsize)
1900 rpmheadsize = l + 128;
1901 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1903 if (fread(rpmhead->data, l, 1, fp) != 1)
1905 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1910 solv_chksum_add(chksumh, rpmhead->data, l);
1911 rpmhead->cnt = sigcnt;
1912 rpmhead->dcnt = sigdsize;
1913 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1914 if (headexists(rpmhead, TAG_PATCHESNAME))
1916 /* this is a patch rpm, ignore */
1920 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1921 if (payloadformat && !strcmp(payloadformat, "drpm"))
1923 /* this is a delta rpm */
1928 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1929 solv_chksum_add(chksumh, lead, l);
1931 s = pool_id2solvable(pool, repo_add_solvable(repo));
1932 rpm2solv(pool, repo, data, s, rpmhead, flags);
1935 Id handle = s - pool->solvables;
1936 repodata_set_location(data, handle, 0, 0, rpms[i]);
1937 if (S_ISREG(stb.st_mode))
1938 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1939 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1941 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1943 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
1947 chksumh = solv_chksum_free(chksumh, 0);
1950 if (!(flags & REPO_NO_INTERNALIZE))
1951 repodata_internalize(data);
1956 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1958 int end = repo->end;
1959 repo_add_rpms(repo, &rpm, 1, flags);
1960 if (end == repo->end)
1963 return repo->end - 1;
1967 linkhash(const char *lt, char *hash)
1970 const unsigned char *str = (const unsigned char *)lt;
1974 while ((c = *str++) != 0)
1976 sprintf(hash, "%08x", r);
1977 sprintf(hash + 8, "%08x", l);
1978 sprintf(hash + 16, "%08x", 0);
1979 sprintf(hash + 24, "%08x", 0);
1983 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1985 RpmHead *rpmhead = rpmhandle;
1990 unsigned int *di, diidx;
1991 unsigned int *co = 0;
1992 unsigned int *ff = 0;
1993 unsigned int lastdir;
1996 int cnt, dcnt, cnt2;
2000 char md5[33], *md5p = 0;
2002 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2005 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2007 for (i = 0; i < dcnt; i++)
2008 (*cb)(cbdata, dn[i], 0, (char *)0);
2012 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2018 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2019 if (!di || cnt != cnt2)
2026 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2027 if (!fm || cnt != cnt2)
2035 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2037 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2038 if (!md || cnt != cnt2)
2048 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2050 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2051 if (!co || cnt != cnt2)
2062 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2064 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2065 if (!ff || cnt != cnt2)
2079 for (i = 0; i < cnt; i++)
2081 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2086 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2089 l = l1 + strlen(bn[i]) + 1;
2093 space = solv_realloc(space, spacen);
2095 if (lastdir != diidx)
2097 strcpy(space, dn[diidx]);
2101 strcpy(space + l1, bn[i]);
2110 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2116 linkhash(lt[i], md5);
2122 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2126 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2140 rpm_query(void *rpmhandle, Id what)
2142 const char *name, *arch, *sourcerpm;
2146 RpmHead *rpmhead = rpmhandle;
2151 name = headstring(rpmhead, TAG_NAME);
2154 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2156 arch = headstring(rpmhead, TAG_ARCH);
2159 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2166 evr = headtoevr(rpmhead);
2167 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2169 sprintf(r, "%s-%s.%s", name, evr, arch);
2173 name = headstring(rpmhead, TAG_NAME);
2174 r = solv_strdup(name);
2177 r = headtoevr(rpmhead);
2184 struct rpm_by_state {
2199 #define ENTRIES_BLOCK 255
2200 #define NAMEDATA_BLOCK 1023
2202 static struct rpmdbentry *
2203 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2216 struct rpmdbentry *entries = 0;
2222 dbenv = state->dbenv;
2223 if (db_create(&db, dbenv, 0))
2225 perror("db_create");
2228 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2230 perror("db->open index");
2234 if (db->get_byteswapped(db, &byteswapped))
2236 perror("db->get_byteswapped");
2240 if (db->cursor(db, NULL, &dbc, 0))
2242 perror("db->cursor");
2246 memset(&dbkey, 0, sizeof(dbkey));
2247 memset(&dbdata, 0, sizeof(dbdata));
2250 dbkey.data = (void *)match;
2251 dbkey.size = strlen(match);
2253 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2255 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2259 while(dl >= RPM_INDEX_SIZE)
2261 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2262 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2263 entries[nentries].nameoff = namedatal;
2265 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2266 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2267 namedata[namedatal + dbkey.size] = 0;
2268 namedatal += dbkey.size + 1;
2269 dp += RPM_INDEX_SIZE;
2270 dl -= RPM_INDEX_SIZE;
2277 *nentriesp = nentries;
2278 *namedatap = namedata;
2283 freestate(struct rpm_by_state *state)
2289 state->db->close(state->db, 0);
2291 state->dbenv->close(state->dbenv, 0);
2292 solv_free(state->rpmhead);
2296 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2298 struct rpm_by_state state;
2299 struct rpmdbentry *entries;
2306 queue_empty(rpmdbidq);
2307 memset(&state, 0, sizeof(state));
2308 if (!(state.dbenv = opendbenv(rootdir)))
2310 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2312 for (i = 0; i < nentries; i++)
2313 queue_push(rpmdbidq, entries[i].rpmdbid);
2315 solv_free(namedata);
2321 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2323 struct rpm_by_state *state = *statep;
2324 unsigned char buf[16];
2334 *statep = (void *)0;
2340 state = solv_calloc(1, sizeof(*state));
2343 if (!state->dbopened)
2345 state->dbopened = 1;
2346 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2348 if (db_create(&state->db, state->dbenv, 0))
2350 perror("db_create");
2352 state->dbenv->close(state->dbenv, 0);
2356 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2358 perror("db->open var/lib/rpm/Packages");
2359 state->db->close(state->db, 0);
2361 state->dbenv->close(state->dbenv, 0);
2365 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2367 perror("db->get_byteswapped");
2368 state->db->close(state->db, 0);
2370 state->dbenv->close(state->dbenv, 0);
2375 rpmdbid2db(buf, rpmdbid, state->byteswapped);
2376 memset(&dbkey, 0, sizeof(dbkey));
2377 memset(&dbdata, 0, sizeof(dbdata));
2382 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2387 if (dbdata.size < 8)
2389 fprintf(stderr, "corrupt rpm database (size)\n");
2392 if (dbdata.size > state->rpmheadsize)
2394 state->rpmheadsize = dbdata.size + 128;
2395 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2397 rpmhead = state->rpmhead;
2398 memcpy(buf, dbdata.data, 8);
2399 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2400 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2401 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2403 fprintf(stderr, "corrupt rpm database (data size)\n");
2406 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2407 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2412 rpm_byfp(FILE *fp, const char *name, void **statep)
2414 struct rpm_by_state *state = *statep;
2415 /* int headerstart, headerend; */
2417 int sigdsize, sigcnt, l;
2418 unsigned char lead[4096];
2421 return rpm_byrpmdbid(0, 0, statep);
2424 state = solv_calloc(1, sizeof(*state));
2427 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2429 fprintf(stderr, "%s: not a rpm\n", name);
2432 if (lead[78] != 0 || lead[79] != 5)
2434 fprintf(stderr, "%s: not a V5 header\n", name);
2437 if (getu32(lead + 96) != 0x8eade801)
2439 fprintf(stderr, "%s: bad signature header\n", name);
2442 sigcnt = getu32(lead + 96 + 8);
2443 sigdsize = getu32(lead + 96 + 12);
2444 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2446 fprintf(stderr, "%s: bad signature header\n", name);
2449 sigdsize += sigcnt * 16;
2450 sigdsize = (sigdsize + 7) & ~7;
2451 /* headerstart = 96 + 16 + sigdsize; */
2454 l = sigdsize > 4096 ? 4096 : sigdsize;
2455 if (fread(lead, l, 1, fp) != 1)
2457 fprintf(stderr, "%s: unexpected EOF\n", name);
2462 if (fread(lead, 16, 1, fp) != 1)
2464 fprintf(stderr, "%s: unexpected EOF\n", name);
2467 if (getu32(lead) != 0x8eade801)
2469 fprintf(stderr, "%s: bad header\n", name);
2473 sigcnt = getu32(lead + 8);
2474 sigdsize = getu32(lead + 12);
2475 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2477 fprintf(stderr, "%s: bad header\n", name);
2481 l = sigdsize + sigcnt * 16;
2482 /* headerend = headerstart + 16 + l; */
2483 if (l > state->rpmheadsize)
2485 state->rpmheadsize = l + 128;
2486 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2488 rpmhead = state->rpmhead;
2489 if (fread(rpmhead->data, l, 1, fp) != 1)
2491 fprintf(stderr, "%s: unexpected EOF\n", name);
2495 rpmhead->cnt = sigcnt;
2496 rpmhead->dcnt = sigdsize;
2497 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2502 rpm_byrpmh(Header h, void **statep)
2504 struct rpm_by_state *state = *statep;
2505 const unsigned char *uh;
2506 int sigdsize, sigcnt, l;
2510 uh = headerUnload(h);
2512 uh = headerUnload(h, NULL);
2516 sigcnt = getu32(uh);
2517 sigdsize = getu32(uh + 4);
2518 l = sigdsize + sigcnt * 16;
2521 state = solv_calloc(1, sizeof(*state));
2524 if (l > state->rpmheadsize)
2526 state->rpmheadsize = l + 128;
2527 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2529 rpmhead = state->rpmhead;
2530 memcpy(rpmhead->data, uh + 8, l - 8);
2532 rpmhead->cnt = sigcnt;
2533 rpmhead->dcnt = sigdsize;
2534 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2539 #ifdef ENABLE_RPMDB_PUBKEY
2542 r64dec1(char *p, unsigned int *vp, int *eofp)
2547 for (i = 0; i < 4; )
2552 if (x >= 'A' && x <= 'Z')
2554 else if (x >= 'a' && x <= 'z')
2556 else if (x >= '0' && x <= '9')
2583 crc24(unsigned char *p, int len)
2585 unsigned int crc = 0xb704ceL;
2590 crc ^= (*p++) << 16;
2591 for (i = 0; i < 8; i++)
2592 if ((crc <<= 1) & 0x1000000)
2595 return crc & 0xffffffL;
2598 static unsigned char *
2599 unarmor(char *pubkey, int *pktlp)
2603 unsigned char *buf, *bp;
2607 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2609 pubkey = strchr(pubkey, '\n');
2614 pubkey = strchr(pubkey, '\n');
2617 /* skip header lines */
2620 while (*pubkey == ' ' || *pubkey == '\t')
2622 if (*pubkey == '\n')
2624 pubkey = strchr(pubkey, '\n');
2629 p = strchr(pubkey, '=');
2633 bp = buf = solv_malloc(l * 3 / 4 + 4);
2637 pubkey = r64dec1(pubkey, &v, &eof);
2647 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2650 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2655 if (v != crc24(buf, bp - buf))
2660 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2662 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2672 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2675 unsigned char keyid[8];
2676 unsigned int kcr = 0, maxex = 0;
2677 unsigned char *pubkey = 0;
2678 unsigned char *userid = 0;
2684 for (; pl; p += l, pl -= l)
2688 if (!(x & 128) || pl <= 0)
2693 tag = (x & 0x3c) >> 2;
2715 else if (x >= 192 && x < 224)
2719 l = ((x - 192) << 8) + *p++ + 192;
2726 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2737 pubkey = solv_realloc(pubkey, l);
2739 memcpy(pubkey, p, l);
2748 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2752 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2756 memset(keyid, 0, 8);
2757 if (p[7] == 1) /* RSA */
2760 unsigned char fp[16];
2764 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2765 memcpy(keyid, p + 10 + ql - 8, 8);
2766 h = solv_chksum_create(REPOKEY_TYPE_MD5);
2767 solv_chksum_add(h, p + 10, ql);
2769 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2770 solv_chksum_add(h, q + 2, ql);
2771 solv_chksum_free(h, fp);
2772 for (i = 0; i < 16; i++)
2773 sprintf(fpx + i * 2, "%02x", fp[i]);
2774 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2781 unsigned char hdr[3];
2782 unsigned char fp[20];
2785 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2789 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2790 solv_chksum_add(h, hdr, 3);
2791 solv_chksum_add(h, p, l);
2792 solv_chksum_free(h, fp);
2793 for (i = 0; i < 20; i++)
2794 sprintf(fpx + i * 2, "%02x", fp[i]);
2795 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2796 memcpy(keyid, fp + 12, 8);
2801 if (p[0] == 3 && p[1] == 5)
2806 /* printf("V3 signature packet\n"); */
2809 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2811 if (!memcmp(keyid, p + 6, 8))
2813 /* printf("SELF SIG\n"); */
2817 /* printf("OTHER SIG\n"); */
2821 htype = REPOKEY_TYPE_MD5;
2822 else if (p[16] == 2)
2823 htype = REPOKEY_TYPE_SHA1;
2824 else if (p[16] == 8)
2825 htype = REPOKEY_TYPE_SHA256;
2828 void *h = solv_chksum_create(htype);
2829 unsigned char b[3], *cs;
2832 b[1] = pubkeyl >> 8;
2834 solv_chksum_add(h, b, 3);
2835 solv_chksum_add(h, pubkey, pubkeyl);
2836 if (p[2] >= 0x10 && p[2] <= 0x13)
2837 solv_chksum_add(h, userid, useridl);
2838 solv_chksum_add(h, p + 2, 5);
2839 cs = solv_chksum_get(h, 0);
2840 solv_chksum_free(h, 0);
2846 int j, ql, haveissuer;
2848 unsigned int ex = 0;
2850 unsigned int scr = 0;
2852 unsigned char issuer[8];
2854 /* printf("V4 signature packet\n"); */
2857 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2862 for (j = 0; q && j < 2; j++)
2869 ql = q[0] << 8 | q[1];
2890 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2901 sl = ((x - 192) << 8) + *q++ + 192;
2910 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
2911 if (x == 16 && sl == 9 && !haveissuer)
2913 memcpy(issuer, q + 1, 8);
2917 if (x == 2 && j == 0)
2918 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2920 if (x == 9 && j == 0)
2921 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2933 htype = REPOKEY_TYPE_MD5;
2935 htype = REPOKEY_TYPE_SHA1;
2937 htype = REPOKEY_TYPE_SHA256;
2938 if (htype && pubkeyl)
2940 void *h = solv_chksum_create(htype);
2941 unsigned char b[6], *cs;
2945 b[1] = pubkeyl >> 8;
2947 solv_chksum_add(h, b, 3);
2948 solv_chksum_add(h, pubkey, pubkeyl);
2949 if (p[1] >= 0x10 && p[1] <= 0x13)
2952 b[1] = useridl >> 24;
2953 b[2] = useridl >> 16;
2954 b[3] = useridl >> 8;
2956 solv_chksum_add(h, b, 5);
2957 solv_chksum_add(h, userid, useridl);
2959 hl = 6 + (p[4] << 8 | p[5]);
2960 solv_chksum_add(h, p, hl);
2967 solv_chksum_add(h, b, 6);
2968 cs = solv_chksum_get(h, 0);
2969 solv_chksum_free(h, 0);
2972 if (!memcmp(keyid, issuer, 8))
2974 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
2980 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
2987 userid = solv_realloc(userid, l);
2989 memcpy(userid, p, l);
2996 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3001 /* this is private to rpm, but rpm lacks an interface to retrieve
3002 * the values. Sigh. */
3003 struct pgpDigParams_s {
3004 const char * userid;
3005 const unsigned char * hash;
3006 const char * params[4];
3008 unsigned char version; /*!< version number. */
3009 unsigned char time[4]; /*!< time that the key was created. */
3010 unsigned char pubkey_algo; /*!< public key algorithm. */
3011 unsigned char hash_algo;
3012 unsigned char sigtype;
3013 unsigned char hashlen;
3014 unsigned char signhash16[2];
3015 unsigned char signid[8];
3016 unsigned char saved;
3020 struct pgpDigParams_s signature;
3021 struct pgpDigParams_s pubkey;
3025 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3027 Pool *pool = s->repo->pool;
3028 unsigned char *pkts;
3033 char evrbuf[8 + 1 + 8 + 1];
3035 pkts = unarmor(pubkey, &pktsl);
3038 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3039 parsekeydata(s, data, pkts, pktsl);
3040 /* only rpm knows how to do the release calculation, we don't dare
3041 * to recreate all the bugs */
3045 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3047 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3048 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3049 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]);
3050 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3052 s->name = pool_str2id(pool, "gpg-pubkey", 1);
3053 s->evr = pool_str2id(pool, evrbuf, 1);
3055 for (i = 0; i < 8; i++)
3056 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3057 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3058 if (dig->pubkey.userid)
3059 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3061 (void)pgpFreeDig(dig);
3063 (void)pgpDigFree(dig);
3065 solv_free((void *)pkts);
3070 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
3072 Pool *pool = repo->pool;
3073 struct rpm_by_state state;
3074 struct rpmdbentry *entries;
3076 char *namedata, *str;
3081 data = repo_add_repodata(repo, flags);
3083 memset(&state, 0, sizeof(state));
3084 if (!(state.dbenv = opendbenv(rootdir)))
3086 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3087 for (i = 0 ; i < nentries; i++)
3089 void *statep = &state;
3090 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3093 str = headstring(rpmhead, TAG_DESCRIPTION);
3096 s = pool_id2solvable(pool, repo_add_solvable(repo));
3097 pubkey2solvable(s, data, str);
3098 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3100 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3102 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3103 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3106 solv_free(namedata);
3108 if (!(flags & REPO_NO_INTERNALIZE))
3109 repodata_internalize(data);
3114 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
3116 Pool *pool = repo->pool;
3123 data = repo_add_repodata(repo, flags);
3126 for (i = 0; i < nkeys; i++)
3128 if ((fp = fopen(keys[i], "r")) == 0)
3135 if (bufl - l < 4096)
3138 buf = solv_realloc(buf, bufl);
3140 ll = fread(buf, 1, bufl - l, fp);
3147 s = pool_id2solvable(pool, repo_add_solvable(repo));
3148 pubkey2solvable(s, data, buf);
3151 if (!(flags & REPO_NO_INTERNALIZE))
3152 repodata_internalize(data);
3156 #endif /* ENABLE_RPMDB_PUBKEY */