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>
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
30 #include <rpm/header.h>
32 #include <rpm/rpmdb.h>
35 # if defined(SUSE) || defined(HAVE_RPM_DB_H)
48 #include "repo_rpmdb.h"
50 /* 3: added triggers */
51 /* 4: fixed triggers */
52 #define RPMDB_COOKIE_VERSION 4
55 #define TAG_VERSION 1001
56 #define TAG_RELEASE 1002
57 #define TAG_EPOCH 1003
58 #define TAG_SUMMARY 1004
59 #define TAG_DESCRIPTION 1005
60 #define TAG_BUILDTIME 1006
61 #define TAG_BUILDHOST 1007
62 #define TAG_INSTALLTIME 1008
64 #define TAG_DISTRIBUTION 1010
65 #define TAG_VENDOR 1011
66 #define TAG_LICENSE 1014
67 #define TAG_PACKAGER 1015
68 #define TAG_GROUP 1016
71 #define TAG_FILESIZES 1028
72 #define TAG_FILEMODES 1030
73 #define TAG_FILEMD5S 1035
74 #define TAG_FILELINKTOS 1036
75 #define TAG_FILEFLAGS 1037
76 #define TAG_SOURCERPM 1044
77 #define TAG_PROVIDENAME 1047
78 #define TAG_REQUIREFLAGS 1048
79 #define TAG_REQUIRENAME 1049
80 #define TAG_REQUIREVERSION 1050
81 #define TAG_NOSOURCE 1051
82 #define TAG_NOPATCH 1052
83 #define TAG_CONFLICTFLAGS 1053
84 #define TAG_CONFLICTNAME 1054
85 #define TAG_CONFLICTVERSION 1055
86 #define TAG_TRIGGERNAME 1066
87 #define TAG_TRIGGERVERSION 1067
88 #define TAG_TRIGGERFLAGS 1068
89 #define TAG_CHANGELOGTIME 1080
90 #define TAG_CHANGELOGNAME 1081
91 #define TAG_CHANGELOGTEXT 1082
92 #define TAG_OBSOLETENAME 1090
93 #define TAG_FILEDEVICES 1095
94 #define TAG_FILEINODES 1096
95 #define TAG_SOURCEPACKAGE 1106
96 #define TAG_PROVIDEFLAGS 1112
97 #define TAG_PROVIDEVERSION 1113
98 #define TAG_OBSOLETEFLAGS 1114
99 #define TAG_OBSOLETEVERSION 1115
100 #define TAG_DIRINDEXES 1116
101 #define TAG_BASENAMES 1117
102 #define TAG_DIRNAMES 1118
103 #define TAG_PAYLOADFORMAT 1124
104 #define TAG_PATCHESNAME 1133
105 #define TAG_FILECOLORS 1140
106 #define TAG_SUGGESTSNAME 1156
107 #define TAG_SUGGESTSVERSION 1157
108 #define TAG_SUGGESTSFLAGS 1158
109 #define TAG_ENHANCESNAME 1159
110 #define TAG_ENHANCESVERSION 1160
111 #define TAG_ENHANCESFLAGS 1161
114 #define TAG_DISTEPOCH 1218
117 #define TAG_LONGFILESIZES 5008
118 #define TAG_LONGSIZE 5009
121 #define TAG_SIGBASE 256
122 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
123 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
125 #define SIGTAG_SIZE 1000
126 #define SIGTAG_PGP 1002 /* RSA signature */
127 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
128 #define SIGTAG_GPG 1005 /* DSA signature */
130 #define DEP_LESS (1 << 1)
131 #define DEP_GREATER (1 << 2)
132 #define DEP_EQUAL (1 << 3)
133 #define DEP_STRONG (1 << 27)
134 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
136 #define FILEFLAG_GHOST (1 << 6)
140 # define RPM_INDEX_SIZE 4
142 # define RPM_INDEX_SIZE 8
150 typedef struct rpmhead {
154 int forcebinary; /* sigh */
155 unsigned char data[1];
159 static inline unsigned char *
160 headfindtag(RpmHead *h, int tag)
163 unsigned char *d, taga[4];
169 for (i = 0; i < h->cnt; i++, d -= 16)
170 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
176 headexists(RpmHead *h, int tag)
178 return headfindtag(h, tag) ? 1 : 0;
181 static unsigned int *
182 headint32array(RpmHead *h, int tag, int *cnt)
184 unsigned int i, o, *r;
185 unsigned char *d = headfindtag(h, tag);
187 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
189 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
190 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
191 if (o + 4 * i > h->dcnt)
194 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
197 for (o = 0; o < i; o++, d += 4)
198 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
202 /* returns the first entry of an integer array */
204 headint32(RpmHead *h, int tag)
207 unsigned char *d = headfindtag(h, tag);
209 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
211 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
212 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
213 if (i == 0 || o + 4 * i > h->dcnt)
216 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
219 /* returns the first entry of an 64bit integer array */
220 static unsigned long long
221 headint64(RpmHead *h, int tag)
224 unsigned char *d = headfindtag(h, tag);
225 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
227 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
228 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
229 if (i == 0 || o + 8 * i > h->dcnt)
232 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
233 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
236 static unsigned int *
237 headint16array(RpmHead *h, int tag, int *cnt)
239 unsigned int i, o, *r;
240 unsigned char *d = headfindtag(h, tag);
242 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
244 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
245 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
246 if (o + 4 * i > h->dcnt)
249 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
252 for (o = 0; o < i; o++, d += 2)
253 r[o] = d[0] << 8 | d[1];
258 headstring(RpmHead *h, int tag)
261 unsigned char *d = headfindtag(h, tag);
262 /* 6: STRING, 9: I18NSTRING */
263 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
265 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
268 return (char *)h->dp + o;
272 headstringarray(RpmHead *h, int tag, int *cnt)
275 unsigned char *d = headfindtag(h, tag);
278 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
280 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
281 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
282 r = solv_calloc(i ? i : 1, sizeof(char *));
286 for (o = 0; o < i; o++)
290 d += strlen((char *)d) + 1;
291 if (d >= h->dp + h->dcnt)
300 static unsigned char *
301 headbinary(RpmHead *h, int tag, unsigned int *sizep)
304 unsigned char *d = headfindtag(h, tag);
305 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
307 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
308 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
309 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
316 static char *headtoevr(RpmHead *h)
324 version = headstring(h, TAG_VERSION);
325 release = headstring(h, TAG_RELEASE);
326 epoch = headint32(h, TAG_EPOCH);
327 if (!version || !release)
329 fprintf(stderr, "headtoevr: bad rpm header\n");
332 for (v = version; *v >= '0' && *v <= '9'; v++)
334 if (epoch || (v != version && *v == ':'))
336 char epochbuf[11]; /* 32bit decimal will fit in */
337 sprintf(epochbuf, "%u", epoch);
338 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
339 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
343 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
344 sprintf(evr, "%s-%s", version, release);
346 distepoch = headstring(h, TAG_DISTEPOCH);
347 if (distepoch && *distepoch)
350 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
352 strcpy(evr + l, distepoch);
359 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
361 const unsigned char *cp;
364 unsigned char *buf = 0, *bp;
366 /* check if it's already utf8, code taken from screen ;-) */
367 cp = (const unsigned char *)str;
368 while ((c = *cp++) != 0)
372 if ((c & 0xc0) != 0x80)
373 break; /* encoding error */
374 c = (c & 0x3f) | (state << 6);
375 if (!(state & 0x40000000))
377 /* check for overlong sequences */
378 if ((c & 0x820823e0) == 0x80000000)
380 else if ((c & 0x020821f0) == 0x02000000)
382 else if ((c & 0x000820f8) == 0x00080000)
384 else if ((c & 0x0000207c) == 0x00002000)
394 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
396 c = (c & 0x03) | 0xbfffff00; /* 4 */
398 c = (c & 0x07) | 0xbfffc000; /* 3 */
400 c = (c & 0x0f) | 0xbff00000; /* 2 */
402 c = (c & 0x1f) | 0xfc000000; /* 1 */
406 state = (c & 0x80000000) ? c : 0;
410 /* not utf8, assume latin1 */
411 buf = solv_malloc(2 * strlen(str) + 1);
412 cp = (const unsigned char *)str;
415 while ((c = *cp++) != 0)
428 repodata_set_str(repodata, handle, tag, str);
434 #define MAKEDEPS_FILTER_WEAK (1 << 0)
435 #define MAKEDEPS_FILTER_STRONG (1 << 1)
436 #define MAKEDEPS_NO_RPMLIB (1 << 2)
439 * strong: 0: ignore strongness
440 * 1: filter to strong
444 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
448 int i, cc, nc, vc, fc;
450 unsigned int olddeps;
454 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
455 n = headstringarray(rpmhead, tagn, &nc);
459 v = headstringarray(rpmhead, tagv, &vc);
460 f = headint32array(rpmhead, tagf, &fc);
461 if (!v || !f || nc != vc || nc != fc)
463 char *pkgname = rpm_query(rpmhead, 0);
464 fprintf(stderr, "bad dependency entries for %s: %d %d %d\n", pkgname ? pkgname : "<NULL>", nc, vc, fc);
473 haspre = 0; /* add no prereq marker */
476 /* we do filtering */
478 for (i = 0; i < nc; i++)
480 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
482 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
483 if (!strncmp(n[i], "rpmlib(", 7))
485 if ((f[i] & DEP_PRE) != 0)
490 else if (tagn == TAG_REQUIRENAME)
492 /* no filtering, just look for the first prereq */
493 for (i = 0; i < nc; i++)
494 if ((f[i] & DEP_PRE) != 0)
508 olddeps = repo_reserve_ids(repo, 0, cc);
509 ida = repo->idarraydata + olddeps;
516 haspre = 2; /* pass two: prereqs */
518 *ida++ = SOLVABLE_PREREQMARKER;
520 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
522 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
524 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
526 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
527 if (!strncmp(n[i], "rpmlib(", 7))
529 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
533 if ((f[i] & DEP_LESS) != 0)
535 if ((f[i] & DEP_EQUAL) != 0)
537 if ((f[i] & DEP_GREATER) != 0)
539 name = pool_str2id(pool, n[i], 1);
540 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
541 evr = pool_str2id(pool, v[i] + 2, 1);
543 evr = pool_str2id(pool, v[i], 1);
544 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
547 *ida++ = pool_str2id(pool, n[i], 1);
550 repo->idarraysize += cc + 1;
558 #ifdef USE_FILEFILTER
560 #define FILEFILTER_EXACT 0
561 #define FILEFILTER_STARTS 1
562 #define FILEFILTER_CONTAINS 2
570 static struct filefilter filefilters[] = {
571 { FILEFILTER_CONTAINS, "/bin/", 0},
572 { FILEFILTER_CONTAINS, "/sbin/", 0},
573 { FILEFILTER_CONTAINS, "/lib/", 0},
574 { FILEFILTER_CONTAINS, "/lib64/", 0},
575 { FILEFILTER_CONTAINS, "/etc/", 0},
576 { FILEFILTER_STARTS, "/usr/games/", 0},
577 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
578 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
579 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
585 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
589 unsigned int *fkb, *fn, *fsz, *fm, *fino;
590 unsigned int inotest[256], inotestok;
594 /* XXX: use TAG_LONGFILESIZES if available */
595 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
596 if (!fsz || fc != fszc)
601 /* stupid rpm records sizes of directories, so we have to check the mode */
602 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
603 if (!fm || fc != fszc)
609 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
610 if (!fino || fc != fszc)
618 if (fc < sizeof(inotest))
620 memset(inotest, 0, sizeof(inotest));
621 for (i = 0; i < fc; i++)
624 if (fsz[i] == 0 || !S_ISREG(fm[i]))
626 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
627 bit = 1 << (fino[i] & 31);
628 if ((inotest[off] & bit) != 0)
637 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
639 unsigned int mask, hash, hh;
640 if (!fdev || fc != fszc)
649 while ((mask & (mask - 1)) != 0)
650 mask = mask & (mask - 1);
652 if (mask > sizeof(inotest)/sizeof(*inotest))
653 fx = solv_calloc(mask, sizeof(unsigned int));
657 memset(fx, 0, mask * sizeof(unsigned int));
660 for (i = 0; i < fc; i++)
662 if (fsz[i] == 0 || !S_ISREG(fm[i]))
664 hash = (fino[i] + fdev[i] * 31) & mask;
666 while ((j = fx[hash]) != 0)
668 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
670 fsz[i] = 0; /* kill entry */
673 hash = (hash + hh++) & mask;
683 fn = solv_calloc(dc, sizeof(unsigned int));
684 fkb = solv_calloc(dc, sizeof(unsigned int));
685 for (i = 0; i < fc; i++)
690 if (fsz[i] == 0 || !S_ISREG(fm[i]))
692 fkb[di[i]] += fsz[i] / 1024 + 1;
697 handle = s - pool->solvables;
698 for (i = 0; i < dc; i++)
704 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
705 did = repodata_str2dir(data, "/usr/src", 1);
707 continue; /* work around rpm bug */
710 did = repodata_str2dir(data, dn[i], 1);
711 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
717 /* assumes last processed array is provides! */
719 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
728 #ifdef USE_FILEFILTER
730 struct filefilter *ff;
739 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
742 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
748 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
757 fprintf(stderr, "bad filelist\n");
762 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
764 for (i = 0; i < bnc; i++)
766 #ifdef USE_FILEFILTER
768 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
772 switch (ff->dirmatch)
774 case FILEFILTER_STARTS:
775 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
778 case FILEFILTER_CONTAINS:
779 if (!strstr(dn[di[i]], ff->dir))
782 case FILEFILTER_EXACT:
784 if (strcmp(dn[di[i]], ff->dir))
791 if (strcmp(bn[i], ff->base))
796 if (j == sizeof(filefilters)/sizeof(*filefilters))
800 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
804 fn = solv_realloc(fn, fna);
806 strcpy(fn, dn[di[i]]);
808 olddeps = repo_addid_dep(repo, olddeps, pool_str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
815 if (di[i] == lastdii)
819 did = repodata_str2dir(data, dn[di[i]], 1);
821 did = repodata_str2dir(data, "/", 1);
825 if (b && *b == '/') /* work around rpm bug */
827 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, b);
841 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
846 int i, cnc, cxc, ctc;
849 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
850 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
851 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
852 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
860 for (i = 0; i < ctc; i++)
862 Id h = repodata_new_handle(data);
864 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
866 repodata_set_str(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
868 repodata_set_str(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
871 for (i = 0; i < hq.count; i++)
872 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
881 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
887 name = headstring(rpmhead, TAG_NAME);
890 fprintf(stderr, "package has no name\n");
893 if (!strcmp(name, "gpg-pubkey"))
895 s->name = pool_str2id(pool, name, 1);
896 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
897 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
898 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
901 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
902 s->arch = ARCH_NOSRC;
907 s->arch = ARCH_NOARCH;
908 evr = headtoevr(rpmhead);
909 s->evr = pool_str2id(pool, evr, 1);
910 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
912 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
913 if ((flags & RPM_ADD_NO_FILELIST) == 0)
914 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
915 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
916 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
917 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
918 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
919 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
921 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
922 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
923 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
924 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
925 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
926 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
933 unsigned long long u64;
935 handle = s - pool->solvables;
936 str = headstring(rpmhead, TAG_SUMMARY);
938 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
939 str = headstring(rpmhead, TAG_DESCRIPTION);
943 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
944 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
948 /* oh my, found SUSE special author section */
950 str = solv_strdup(str);
953 while (l > 0 && str[l - 1] == '\n')
956 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
958 aut = str; /* copy over */
959 while (*p == ' ' || *p == '\n')
972 while (aut != str && aut[-1] == '\n')
976 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
980 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
982 str = headstring(rpmhead, TAG_GROUP);
984 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
985 str = headstring(rpmhead, TAG_LICENSE);
987 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
988 str = headstring(rpmhead, TAG_URL);
990 repodata_set_str(data, handle, SOLVABLE_URL, str);
991 str = headstring(rpmhead, TAG_DISTRIBUTION);
993 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
994 str = headstring(rpmhead, TAG_PACKAGER);
996 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
997 if ((flags & RPM_ADD_WITH_PKGID) != 0)
999 unsigned char *chksum;
1000 unsigned int chksumsize;
1001 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
1002 if (chksum && chksumsize == 16)
1003 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
1005 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1007 str = headstring(rpmhead, TAG_SHA1HEADER);
1008 if (str && strlen(str) == 40)
1009 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
1010 else if (str && strlen(str) == 64)
1011 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
1013 u32 = headint32(rpmhead, TAG_BUILDTIME);
1015 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1016 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1018 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1019 u64 = headint64(rpmhead, TAG_LONGSIZE);
1021 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1024 u32 = headint32(rpmhead, TAG_SIZE);
1026 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1029 repodata_set_sourcepkg(data, handle, sourcerpm);
1030 if ((flags & RPM_ADD_TRIGGERS) != 0)
1033 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1036 for (; (id = repo->idarraydata[ida]) != 0; ida++)
1038 /* we currently do not support rel ids in incore data, so
1039 * strip off versioning information */
1040 while (ISRELDEP(id))
1042 Reldep *rd = GETRELDEP(pool, id);
1047 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1051 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1052 addchangelog(data, handle, rpmhead);
1059 copyreldep(Pool *pool, Pool *frompool, Id id)
1061 Reldep *rd = GETRELDEP(frompool, id);
1062 Id name = rd->name, evr = rd->evr;
1064 name = copyreldep(pool, frompool, name);
1066 name = pool_str2id(pool, pool_id2str(frompool, name), 1);
1068 evr = copyreldep(pool, frompool, evr);
1070 evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1071 return pool_rel2id(pool, name, evr, rd->flags, 1);
1075 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1080 Pool *frompool = fromrepo->pool;
1084 from = fromrepo->idarraydata + fromoff;
1085 for (ida = from, cc = 0; *ida; ida++, cc++)
1089 ido = repo_reserve_ids(repo, 0, cc);
1090 ida = repo->idarraydata + ido;
1091 if (frompool && pool != frompool)
1097 id = copyreldep(pool, frompool, id);
1099 id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1105 memcpy(ida, from, (cc + 1) * sizeof(Id));
1106 repo->idarraysize += cc + 1;
1110 #define COPYDIR_DIRCACHE_SIZE 512
1112 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1115 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1117 if (cache && cache[did & 255] == did)
1118 return cache[(did & 255) + 256];
1119 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1123 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1125 Id parent = dirpool_parent(&fromdata->dirpool, did);
1126 Id compid = dirpool_compid(&fromdata->dirpool, did);
1128 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1129 if (fromspool != &pool->ss)
1130 compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1131 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1134 cache[did & 255] = did;
1135 cache[(did & 255) + 256] = compid;
1140 struct solvable_copy_cbdata {
1148 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1150 struct solvable_copy_cbdata *cbdata = vcbdata;
1152 Repodata *data = cbdata->data;
1153 Id handle = cbdata->handle;
1154 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1155 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1157 keyname = key->name;
1158 if (keyname >= ID_NUM_INTERNAL && pool != frompool)
1159 keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1162 case REPOKEY_TYPE_ID:
1163 case REPOKEY_TYPE_CONSTANTID:
1164 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1166 assert(!data->localpool); /* implement me! */
1167 if (pool != frompool || fromdata->localpool)
1170 id = copyreldep(pool, frompool, id);
1172 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1174 if (key->type == REPOKEY_TYPE_ID)
1175 repodata_set_id(data, handle, keyname, id);
1176 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1177 repodata_set_constantid(data, handle, keyname, id);
1179 repodata_add_idarray(data, handle, keyname, id);
1181 case REPOKEY_TYPE_STR:
1182 repodata_set_str(data, handle, keyname, kv->str);
1184 case REPOKEY_TYPE_VOID:
1185 repodata_set_void(data, handle, keyname);
1187 case REPOKEY_TYPE_NUM:
1188 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1190 case REPOKEY_TYPE_CONSTANT:
1191 repodata_set_constant(data, handle, keyname, kv->num);
1193 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1195 assert(!data->localpool); /* implement me! */
1196 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1197 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1199 case REPOKEY_TYPE_DIRSTRARRAY:
1201 assert(!data->localpool); /* implement me! */
1202 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1203 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1205 case REPOKEY_TYPE_FLEXARRAY:
1208 assert(cbdata->subhandle);
1209 cbdata->handle = cbdata->subhandle;
1210 cbdata->subhandle = 0;
1215 assert(!cbdata->subhandle);
1216 cbdata->subhandle = cbdata->handle;
1218 cbdata->handle = repodata_new_handle(data);
1219 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1228 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1230 Repo *repo = s->repo;
1231 Repo *fromrepo = r->repo;
1232 Pool *pool = repo->pool;
1233 struct solvable_copy_cbdata cbdata;
1235 /* copy solvable data */
1236 if (pool == fromrepo->pool)
1241 s->vendor = r->vendor;
1246 s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1248 s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1250 s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1252 s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1254 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1255 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1256 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1257 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1258 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1259 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1260 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1261 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1263 /* copy all attributes */
1267 cbdata.handle = s - pool->solvables;
1268 cbdata.subhandle = 0;
1269 cbdata.dircache = dircache;
1270 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1273 /* used to sort entries returned in some database order */
1275 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1277 struct rpmid const *a = va, *b = vb;
1279 r = strcmp(a->name, b->name);
1282 return a->dbid - b->dbid;
1286 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1289 Pool *pool = repo->pool;
1290 Solvable *a = pool->solvables + *(Id *)va;
1291 Solvable *b = pool->solvables + *(Id *)vb;
1294 if (a->name != b->name)
1295 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1296 rpmdbid = repo->rpmdbid;
1297 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1301 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1303 Pool *pool = repo->pool;
1306 tmp = pool->solvables[pa];
1307 pool->solvables[pa] = pool->solvables[pb];
1308 pool->solvables[pb] = tmp;
1311 Id tmpid = repo->rpmdbid[pa - repo->start];
1312 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1313 repo->rpmdbid[pb - repo->start] = tmpid;
1315 /* only works if nothing is already internalized! */
1317 repodata_swap_attrs(data, pa, pb);
1321 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1324 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1326 # if defined(WORDS_BIGENDIAN)
1331 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1333 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1337 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1340 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1342 # if defined(WORDS_BIGENDIAN)
1347 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1349 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1354 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1356 memset(cookie, 0, 32);
1357 cookie[3] = RPMDB_COOKIE_VERSION;
1358 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1359 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1362 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1364 opendbenv(const char *rootdir)
1366 char dbpath[PATH_MAX];
1370 if (db_env_create(&dbenv, 0))
1372 perror("db_env_create");
1375 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1376 dbenv->set_thread_count(dbenv, 8);
1378 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1379 if (access(dbpath, W_OK) == -1)
1381 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1386 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1388 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1393 perror("dbenv open");
1394 dbenv->close(dbenv, 0);
1402 count_headers(const char *rootdir, DB_ENV *dbenv)
1404 char dbpath[PATH_MAX];
1405 struct stat statbuf;
1412 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1413 if (stat(dbpath, &statbuf))
1415 memset(&dbkey, 0, sizeof(dbkey));
1416 memset(&dbdata, 0, sizeof(dbdata));
1417 if (db_create(&db, dbenv, 0))
1419 perror("db_create");
1422 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1424 perror("db->open Name index");
1427 if (db->cursor(db, NULL, &dbc, 0))
1429 perror("db->cursor");
1432 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1433 count += dbdata.size / RPM_INDEX_SIZE;
1440 * read rpm db as repo
1445 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1447 Pool *pool = repo->pool;
1448 unsigned char buf[16];
1455 struct rpmid *rpmids, *rp;
1461 unsigned int refmask, h;
1462 char dbpath[PATH_MAX];
1466 struct stat packagesstat;
1467 unsigned char newcookie[32];
1468 const unsigned char *oldcookie = 0;
1469 Id oldcookietype = 0;
1471 int count = 0, done = 0;
1473 const char *rootdir = 0;
1475 now = solv_timems(0);
1476 memset(&dbkey, 0, sizeof(dbkey));
1477 memset(&dbdata, 0, sizeof(dbdata));
1479 data = repo_add_repodata(repo, flags);
1481 if (ref && !(ref->nsolvables && ref->rpmdbid))
1484 if (flags & REPO_USE_ROOTDIR)
1485 rootdir = pool_get_rootdir(pool);
1486 if (!(dbenv = opendbenv(rootdir)))
1488 return pool_error(pool, -1, "repo_add_rpmdb: opendbenv failed");
1491 /* XXX: should get ro lock of Packages database! */
1492 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1493 if (stat(dbpath, &packagesstat))
1495 pool_error(pool, -1, "repo_add_rpmdb: %s: %s", dbpath, strerror(errno));
1496 dbenv->close(dbenv, 0);
1499 mkrpmdbcookie(&packagesstat, newcookie);
1500 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1503 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1504 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1507 int solvstart = 0, solvend = 0;
1509 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1510 count = count_headers(rootdir, dbenv);
1511 if (db_create(&db, dbenv, 0))
1513 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1514 dbenv->close(dbenv, 0);
1517 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1519 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1521 dbenv->close(dbenv, 0);
1524 if (db->get_byteswapped(db, &byteswapped))
1527 dbenv->close(dbenv, 0);
1528 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1530 if (db->cursor(db, NULL, &dbc, 0))
1533 dbenv->close(dbenv, 0);
1534 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1540 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1544 s = pool_id2solvable(pool, repo_add_solvable(repo));
1546 solvstart = s - pool->solvables;
1547 solvend = s - pool->solvables + 1;
1550 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1551 if (dbkey.size != 4)
1555 dbenv->close(dbenv, 0);
1556 return pool_error(pool, -1, "corrupt Packages database (key size)");
1558 dbid = db2rpmdbid(dbkey.data, byteswapped);
1559 if (dbid == 0) /* the join key */
1561 if (dbdata.size < 8)
1565 dbenv->close(dbenv, 0);
1566 return pool_error(pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1568 if (dbdata.size > rpmheadsize)
1570 rpmheadsize = dbdata.size + 128;
1571 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1573 memcpy(buf, dbdata.data, 8);
1574 rpmhead->forcebinary = 1;
1575 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1576 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1577 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1581 dbenv->close(dbenv, 0);
1582 return pool_error(pool, -1, "corrupt rpm database (data size)\n");
1584 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1585 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1586 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1587 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1594 /* We can reuse this solvable, but make sure it's still
1595 associated with this repo. */
1596 memset(s, 0, sizeof(*s));
1599 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1603 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1604 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1609 /* oops, could not reuse. free it instead */
1610 repo_free_solvable(repo, s - pool->solvables, 1);
1617 /* now sort all solvables in the new solvstart..solvend block */
1618 if (solvend - solvstart > 1)
1620 pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1621 for (i = solvstart; i < solvend; i++)
1622 pkgids[i - solvstart] = i;
1623 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1625 for (i = solvstart; i < solvend; i++)
1627 int j = pkgids[i - solvstart];
1629 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1631 swap_solvables(repo, data, i, j);
1638 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1640 memset(dircache, 0, sizeof(dircache));
1641 if (db_create(&db, dbenv, 0))
1643 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1644 dbenv->close(dbenv, 0);
1647 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1649 pool_error(pool, -1, "repo_add_rpmdb: db->open Name index failed: %s", strerror(errno));
1651 dbenv->close(dbenv, 0);
1654 if (db->get_byteswapped(db, &byteswapped))
1657 dbenv->close(dbenv, 0);
1658 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1660 if (db->cursor(db, NULL, &dbc, 0))
1663 dbenv->close(dbenv, 0);
1664 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1668 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1670 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1674 while(dl >= RPM_INDEX_SIZE)
1676 rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1677 rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1678 rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1679 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1680 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1682 dp += RPM_INDEX_SIZE;
1683 dl -= RPM_INDEX_SIZE;
1691 solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1696 /* create hash from dbid to ref */
1697 refmask = mkmask(ref->nsolvables);
1698 refhash = solv_calloc(refmask + 1, sizeof(Id));
1699 for (i = 0; i < ref->end - ref->start; i++)
1701 if (!ref->rpmdbid[i])
1703 h = ref->rpmdbid[i] & refmask;
1705 h = (h + 317) & refmask;
1706 refhash[h] = i + 1; /* make it non-zero */
1709 /* count the misses, they will cost us time */
1710 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1712 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1718 while ((id = refhash[h]))
1720 if (ref->rpmdbid[id - 1] == dbid)
1722 h = (h + 317) & refmask;
1731 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1733 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1735 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1738 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1742 while ((id = refhash[h]))
1744 if (ref->rpmdbid[id - 1] == dbid)
1746 h = (h + 317) & refmask;
1750 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1753 solvable_copy(s, r, data, dircache);
1760 if (db_create(&db, dbenv, 0))
1762 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1763 dbenv->close(dbenv, 0);
1766 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1768 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1770 dbenv->close(dbenv, 0);
1773 if (db->get_byteswapped(db, &byteswapped))
1776 dbenv->close(dbenv, 0);
1777 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1780 rpmdbid2db(buf, rp->dbid, byteswapped);
1785 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1788 dbenv->close(dbenv, 0);
1789 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1791 if (dbdata.size < 8)
1794 dbenv->close(dbenv, 0);
1795 return pool_error(pool, -1, "corrupt Packages database (size)");
1797 if (dbdata.size > rpmheadsize)
1799 rpmheadsize = dbdata.size + 128;
1800 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1802 memcpy(buf, dbdata.data, 8);
1803 rpmhead->forcebinary = 1;
1804 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1805 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1806 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1809 dbenv->close(dbenv, 0);
1810 return pool_error(pool, -1, "corrupt Packages database (data size)");
1812 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1813 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1815 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1816 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1820 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1821 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1829 for (i = 0; i < nrpmids; i++)
1830 solv_free(rpmids[i].name);
1836 dbenv->close(dbenv, 0);
1839 if (!(flags & REPO_NO_INTERNALIZE))
1840 repodata_internalize(data);
1841 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1842 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1843 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1844 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1845 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)));
1850 static inline unsigned int
1851 getu32(const unsigned char *dp)
1853 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1858 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1860 unsigned int sigdsize, sigcnt, l;
1861 Pool *pool = repo->pool;
1863 RpmHead *rpmhead = 0;
1864 int rpmheadsize = 0;
1865 char *payloadformat;
1867 unsigned char lead[4096];
1868 int headerstart, headerend;
1871 unsigned char pkgid[16];
1872 unsigned char leadsigid[16];
1873 unsigned char hdrid[32];
1874 int pkgidtype, leadsigidtype, hdridtype;
1877 void *leadsigchksumh = 0;
1878 int forcebinary = 0;
1880 data = repo_add_repodata(repo, flags);
1882 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1883 chksumtype = REPOKEY_TYPE_SHA256;
1884 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1885 chksumtype = REPOKEY_TYPE_SHA1;
1887 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1889 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1892 if (fstat(fileno(fp), &stb))
1894 pool_error(pool, -1, "fstat: %s", strerror(errno));
1899 chksumh = solv_chksum_create(chksumtype);
1900 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1901 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1902 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1904 pool_error(pool, -1, "%s: not a rpm", rpm);
1908 forcebinary = lead[6] != 0 || lead[7] != 1;
1910 solv_chksum_add(chksumh, lead, 96 + 16);
1912 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1913 if (lead[78] != 0 || lead[79] != 5)
1915 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1919 if (getu32(lead + 96) != 0x8eade801)
1921 pool_error(pool, -1, "%s: bad signature header", rpm);
1925 sigcnt = getu32(lead + 96 + 8);
1926 sigdsize = getu32(lead + 96 + 12);
1927 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1929 pool_error(pool, -1, "%s: bad signature header", rpm);
1933 sigdsize += sigcnt * 16;
1934 sigdsize = (sigdsize + 7) & ~7;
1935 headerstart = 96 + 16 + sigdsize;
1936 pkgidtype = leadsigidtype = hdridtype = 0;
1937 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1939 /* extract pkgid or hdrid from the signature header */
1940 if (sigdsize > rpmheadsize)
1942 rpmheadsize = sigdsize + 128;
1943 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1945 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1947 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1952 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1954 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1955 rpmhead->forcebinary = 0;
1956 rpmhead->cnt = sigcnt;
1957 rpmhead->dcnt = sigdsize - sigcnt * 16;
1958 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1959 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1961 unsigned char *chksum;
1962 unsigned int chksumsize;
1963 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1964 if (chksum && chksumsize == 16)
1966 pkgidtype = REPOKEY_TYPE_MD5;
1967 memcpy(pkgid, chksum, 16);
1970 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1972 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1973 if (str && strlen(str) == 40)
1975 if (solv_hex2bin(&str, hdrid, 20) == 20)
1976 hdridtype = REPOKEY_TYPE_SHA1;
1978 else if (str && strlen(str) == 64)
1980 if (solv_hex2bin(&str, hdrid, 32) == 32)
1981 hdridtype = REPOKEY_TYPE_SHA256;
1987 /* just skip the signature header */
1990 l = sigdsize > 4096 ? 4096 : sigdsize;
1991 if (fread(lead, l, 1, fp) != 1)
1993 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1998 solv_chksum_add(chksumh, lead, l);
2000 solv_chksum_add(leadsigchksumh, lead, l);
2006 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2007 leadsigidtype = REPOKEY_TYPE_MD5;
2009 if (fread(lead, 16, 1, fp) != 1)
2011 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2016 solv_chksum_add(chksumh, lead, 16);
2017 if (getu32(lead) != 0x8eade801)
2019 pool_error(pool, -1, "%s: bad header", rpm);
2023 sigcnt = getu32(lead + 8);
2024 sigdsize = getu32(lead + 12);
2025 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2027 pool_error(pool, -1, "%s: bad header", rpm);
2031 l = sigdsize + sigcnt * 16;
2032 headerend = headerstart + 16 + l;
2033 if (l > rpmheadsize)
2035 rpmheadsize = l + 128;
2036 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2038 if (fread(rpmhead->data, l, 1, fp) != 1)
2040 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2045 solv_chksum_add(chksumh, rpmhead->data, l);
2046 rpmhead->forcebinary = forcebinary;
2047 rpmhead->cnt = sigcnt;
2048 rpmhead->dcnt = sigdsize;
2049 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2050 if (headexists(rpmhead, TAG_PATCHESNAME))
2052 /* this is a patch rpm, ignore */
2053 pool_error(pool, -1, "%s: is patch rpm", rpm);
2055 solv_chksum_free(chksumh, 0);
2059 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2060 if (payloadformat && !strcmp(payloadformat, "drpm"))
2062 /* this is a delta rpm */
2063 pool_error(pool, -1, "%s: is delta rpm", rpm);
2065 solv_chksum_free(chksumh, 0);
2070 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2071 solv_chksum_add(chksumh, lead, l);
2073 s = pool_id2solvable(pool, repo_add_solvable(repo));
2074 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2076 repo_free_solvable(repo, s - pool->solvables, 1);
2077 solv_chksum_free(chksumh, 0);
2081 if (!(flags & REPO_NO_LOCATION))
2082 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2083 if (S_ISREG(stb.st_mode))
2084 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2085 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2087 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2089 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2091 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2094 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2095 chksumh = solv_chksum_free(chksumh, 0);
2099 if (!(flags & REPO_NO_INTERNALIZE))
2100 repodata_internalize(data);
2101 return s - pool->solvables;
2105 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2107 Pool *pool = repo->pool;
2109 RpmHead *rpmhead = rpmhandle;
2111 char *payloadformat;
2113 data = repo_add_repodata(repo, flags);
2114 if (headexists(rpmhead, TAG_PATCHESNAME))
2116 pool_error(pool, -1, "is a patch rpm");
2119 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2120 if (payloadformat && !strcmp(payloadformat, "drpm"))
2122 /* this is a delta rpm */
2123 pool_error(pool, -1, "is a delta rpm");
2126 s = pool_id2solvable(pool, repo_add_solvable(repo));
2127 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2129 repo_free_solvable(repo, s - pool->solvables, 1);
2132 if (!(flags & REPO_NO_INTERNALIZE))
2133 repodata_internalize(data);
2134 return s - pool->solvables;
2138 linkhash(const char *lt, char *hash)
2141 const unsigned char *str = (const unsigned char *)lt;
2145 while ((c = *str++) != 0)
2147 sprintf(hash, "%08x", r);
2148 sprintf(hash + 8, "%08x", l);
2149 sprintf(hash + 16, "%08x", 0);
2150 sprintf(hash + 24, "%08x", 0);
2154 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
2156 RpmHead *rpmhead = rpmhandle;
2161 unsigned int *di, diidx;
2162 unsigned int *co = 0;
2163 unsigned int *ff = 0;
2164 unsigned int lastdir;
2167 int cnt, dcnt, cnt2;
2171 char md5[33], *md5p = 0;
2173 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2176 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2178 for (i = 0; i < dcnt; i++)
2179 (*cb)(cbdata, dn[i], 0, (char *)0);
2183 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2189 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2190 if (!di || cnt != cnt2)
2197 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2198 if (!fm || cnt != cnt2)
2206 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2208 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2209 if (!md || cnt != cnt2)
2219 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2221 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2222 if (!co || cnt != cnt2)
2233 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2235 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2236 if (!ff || cnt != cnt2)
2250 for (i = 0; i < cnt; i++)
2252 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2257 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2260 l = l1 + strlen(bn[i]) + 1;
2264 space = solv_realloc(space, spacen);
2266 if (lastdir != diidx)
2268 strcpy(space, dn[diidx]);
2272 strcpy(space + l1, bn[i]);
2281 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2287 linkhash(lt[i], md5);
2293 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2297 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2311 rpm_query(void *rpmhandle, Id what)
2313 const char *name, *arch, *sourcerpm;
2317 RpmHead *rpmhead = rpmhandle;
2322 name = headstring(rpmhead, TAG_NAME);
2325 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2326 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2327 arch = headstring(rpmhead, TAG_ARCH);
2330 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2337 evr = headtoevr(rpmhead);
2338 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2340 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2344 name = headstring(rpmhead, TAG_NAME);
2345 r = solv_strdup(name);
2348 r = headtoevr(rpmhead);
2355 struct rpm_by_state {
2370 #define ENTRIES_BLOCK 255
2371 #define NAMEDATA_BLOCK 1023
2373 static struct rpmdbentry *
2374 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2388 struct rpmdbentry *entries = 0;
2394 dbenv = state->dbenv;
2395 if (db_create(&db, dbenv, 0))
2397 perror("db_create");
2400 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2402 perror("db->open index");
2406 if (db->get_byteswapped(db, &byteswapped))
2408 perror("db->get_byteswapped");
2412 if (db->cursor(db, NULL, &dbc, 0))
2414 perror("db->cursor");
2418 memset(&dbkey, 0, sizeof(dbkey));
2419 memset(&dbdata, 0, sizeof(dbdata));
2422 dbkey.data = (void *)match;
2423 dbkey.size = strlen(match);
2425 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2427 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2431 nameoff = namedatal;
2434 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2435 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2436 namedata[namedatal + dbkey.size] = 0;
2437 namedatal += dbkey.size + 1;
2439 while(dl >= RPM_INDEX_SIZE)
2441 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2442 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2443 entries[nentries].nameoff = nameoff;
2445 dp += RPM_INDEX_SIZE;
2446 dl -= RPM_INDEX_SIZE;
2453 *nentriesp = nentries;
2455 *namedatap = namedata;
2460 openpkgdb(struct rpm_by_state *state, const char *rootdir)
2462 if (state->dbopened)
2463 return state->dbopened > 0 ? 1 : 0;
2464 state->dbopened = -1;
2465 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2467 if (db_create(&state->db, state->dbenv, 0))
2469 perror("db_create");
2471 state->dbenv->close(state->dbenv, 0);
2475 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2477 perror("db->open var/lib/rpm/Packages");
2478 state->db->close(state->db, 0);
2480 state->dbenv->close(state->dbenv, 0);
2484 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2486 perror("db->get_byteswapped");
2487 state->db->close(state->db, 0);
2489 state->dbenv->close(state->dbenv, 0);
2493 state->dbopened = 1;
2498 freestate(struct rpm_by_state *state)
2504 state->db->close(state->db, 0);
2506 state->dbenv->close(state->dbenv, 0);
2507 solv_free(state->rpmhead);
2511 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2513 struct rpm_by_state state;
2514 struct rpmdbentry *entries;
2521 queue_empty(rpmdbidq);
2522 memset(&state, 0, sizeof(state));
2523 if (!(state.dbenv = opendbenv(rootdir)))
2525 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2527 for (i = 0; i < nentries; i++)
2528 queue_push(rpmdbidq, entries[i].rpmdbid);
2530 solv_free(namedata);
2536 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2538 struct rpm_by_state *state = *statep;
2539 unsigned char buf[16];
2549 *statep = (void *)0;
2555 state = solv_calloc(1, sizeof(*state));
2558 if (!state->dbopened && !openpkgdb(state, rootdir))
2560 rpmdbid2db(buf, rpmdbid, state->byteswapped);
2561 memset(&dbkey, 0, sizeof(dbkey));
2562 memset(&dbdata, 0, sizeof(dbdata));
2567 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2572 if (dbdata.size < 8)
2574 fprintf(stderr, "corrupt rpm database (size)\n");
2577 if (dbdata.size > state->rpmheadsize)
2579 state->rpmheadsize = dbdata.size + 128;
2580 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2582 rpmhead = state->rpmhead;
2583 memcpy(buf, dbdata.data, 8);
2584 rpmhead->forcebinary = 1;
2585 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2586 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2587 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2589 fprintf(stderr, "corrupt rpm database (data size)\n");
2592 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2593 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2598 rpm_byfp(FILE *fp, const char *name, void **statep)
2600 struct rpm_by_state *state = *statep;
2601 /* int headerstart, headerend; */
2603 unsigned int sigdsize, sigcnt, l;
2604 unsigned char lead[4096];
2605 int forcebinary = 0;
2608 return rpm_byrpmdbid(0, 0, statep);
2611 state = solv_calloc(1, sizeof(*state));
2614 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2616 fprintf(stderr, "%s: not a rpm\n", name);
2619 forcebinary = lead[6] != 0 || lead[7] != 1;
2620 if (lead[78] != 0 || lead[79] != 5)
2622 fprintf(stderr, "%s: not a V5 header\n", name);
2625 if (getu32(lead + 96) != 0x8eade801)
2627 fprintf(stderr, "%s: bad signature header\n", name);
2630 sigcnt = getu32(lead + 96 + 8);
2631 sigdsize = getu32(lead + 96 + 12);
2632 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2634 fprintf(stderr, "%s: bad signature header\n", name);
2637 sigdsize += sigcnt * 16;
2638 sigdsize = (sigdsize + 7) & ~7;
2639 /* headerstart = 96 + 16 + sigdsize; */
2642 l = sigdsize > 4096 ? 4096 : sigdsize;
2643 if (fread(lead, l, 1, fp) != 1)
2645 fprintf(stderr, "%s: unexpected EOF\n", name);
2650 if (fread(lead, 16, 1, fp) != 1)
2652 fprintf(stderr, "%s: unexpected EOF\n", name);
2655 if (getu32(lead) != 0x8eade801)
2657 fprintf(stderr, "%s: bad header\n", name);
2661 sigcnt = getu32(lead + 8);
2662 sigdsize = getu32(lead + 12);
2663 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2665 fprintf(stderr, "%s: bad header\n", name);
2669 l = sigdsize + sigcnt * 16;
2670 /* headerend = headerstart + 16 + l; */
2671 if (l > state->rpmheadsize)
2673 state->rpmheadsize = l + 128;
2674 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2676 rpmhead = state->rpmhead;
2677 if (fread(rpmhead->data, l, 1, fp) != 1)
2679 fprintf(stderr, "%s: unexpected EOF\n", name);
2683 rpmhead->forcebinary = forcebinary;
2684 rpmhead->cnt = sigcnt;
2685 rpmhead->dcnt = sigdsize;
2686 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2690 #ifdef ENABLE_RPMDB_BYRPMHEADER
2693 rpm_byrpmh(Header h, void **statep)
2695 struct rpm_by_state *state = *statep;
2696 const unsigned char *uh;
2697 unsigned int sigdsize, sigcnt, l;
2701 uh = headerUnload(h);
2703 uh = headerUnload(h, NULL);
2707 sigcnt = getu32(uh);
2708 sigdsize = getu32(uh + 4);
2709 l = sigdsize + sigcnt * 16;
2712 state = solv_calloc(1, sizeof(*state));
2715 if (l > state->rpmheadsize)
2717 state->rpmheadsize = l + 128;
2718 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2720 rpmhead = state->rpmhead;
2721 memcpy(rpmhead->data, uh + 8, l - 8);
2723 rpmhead->forcebinary = 0;
2724 rpmhead->cnt = sigcnt;
2725 rpmhead->dcnt = sigdsize;
2726 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2733 #ifdef ENABLE_RPMDB_PUBKEY
2736 r64dec1(char *p, unsigned int *vp, int *eofp)
2741 for (i = 0; i < 4; )
2746 if (x >= 'A' && x <= 'Z')
2748 else if (x >= 'a' && x <= 'z')
2750 else if (x >= '0' && x <= '9')
2777 crc24(unsigned char *p, int len)
2779 unsigned int crc = 0xb704ceL;
2784 crc ^= (*p++) << 16;
2785 for (i = 0; i < 8; i++)
2786 if ((crc <<= 1) & 0x1000000)
2789 return crc & 0xffffffL;
2792 static unsigned char *
2793 unarmor(char *pubkey, int *pktlp)
2797 unsigned char *buf, *bp;
2801 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2803 pubkey = strchr(pubkey, '\n');
2808 pubkey = strchr(pubkey, '\n');
2811 /* skip header lines */
2814 while (*pubkey == ' ' || *pubkey == '\t')
2816 if (*pubkey == '\n')
2818 pubkey = strchr(pubkey, '\n');
2823 p = strchr(pubkey, '=');
2827 bp = buf = solv_malloc(l * 3 / 4 + 4);
2831 pubkey = r64dec1(pubkey, &v, &eof);
2841 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2844 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2849 if (v != crc24(buf, bp - buf))
2854 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2856 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2866 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2869 unsigned char keyid[8];
2870 unsigned int kcr = 0, maxex = 0;
2871 unsigned char *pubkey = 0;
2872 unsigned char *userid = 0;
2878 for (; pl; p += l, pl -= l)
2882 if (!(x & 128) || pl <= 0)
2887 tag = (x & 0x3c) >> 2;
2909 else if (x >= 192 && x < 224)
2913 l = ((x - 192) << 8) + *p++ + 192;
2918 /* sanity: p[0] must be zero */
2919 if (pl <= 4 || p[0] != 0)
2921 l = p[1] << 16 | p[2] << 8 | p[3];
2932 pubkey = solv_realloc(pubkey, l);
2934 memcpy(pubkey, p, l);
2943 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2947 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2951 memset(keyid, 0, 8);
2952 if (p[7] == 1) /* RSA */
2955 unsigned char fp[16];
2959 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2960 memcpy(keyid, p + 10 + ql - 8, 8);
2961 h = solv_chksum_create(REPOKEY_TYPE_MD5);
2962 solv_chksum_add(h, p + 10, ql);
2964 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2965 solv_chksum_add(h, q + 2, ql);
2966 solv_chksum_free(h, fp);
2967 for (i = 0; i < 16; i++)
2968 sprintf(fpx + i * 2, "%02x", fp[i]);
2969 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2976 unsigned char hdr[3];
2977 unsigned char fp[20];
2980 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2984 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2985 solv_chksum_add(h, hdr, 3);
2986 solv_chksum_add(h, p, l);
2987 solv_chksum_free(h, fp);
2988 for (i = 0; i < 20; i++)
2989 sprintf(fpx + i * 2, "%02x", fp[i]);
2990 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2991 memcpy(keyid, fp + 12, 8);
2996 if (p[0] == 3 && p[1] == 5)
3001 /* printf("V3 signature packet\n"); */
3004 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
3006 if (!memcmp(keyid, p + 6, 8))
3008 /* printf("SELF SIG\n"); */
3012 /* printf("OTHER SIG\n"); */
3016 htype = REPOKEY_TYPE_MD5;
3017 else if (p[16] == 2)
3018 htype = REPOKEY_TYPE_SHA1;
3019 else if (p[16] == 8)
3020 htype = REPOKEY_TYPE_SHA256;
3023 void *h = solv_chksum_create(htype);
3024 unsigned char b[3], *cs;
3027 b[1] = pubkeyl >> 8;
3029 solv_chksum_add(h, b, 3);
3030 solv_chksum_add(h, pubkey, pubkeyl);
3031 if (p[2] >= 0x10 && p[2] <= 0x13)
3032 solv_chksum_add(h, userid, useridl);
3033 solv_chksum_add(h, p + 2, 5);
3034 cs = solv_chksum_get(h, 0);
3035 solv_chksum_free(h, 0);
3041 int j, ql, haveissuer;
3043 unsigned int ex = 0;
3045 unsigned int scr = 0;
3047 unsigned char issuer[8];
3049 /* printf("V4 signature packet\n"); */
3052 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
3057 for (j = 0; q && j < 2; j++)
3064 ql = q[0] << 8 | q[1];
3080 if (ql < 4 || q[0] != 0)
3085 sl = q[1] << 16 | q[2] << 8 | q[3];
3096 sl = ((x - 192) << 8) + *q++ + 192;
3105 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
3106 if (x == 16 && sl == 9 && !haveissuer)
3108 memcpy(issuer, q + 1, 8);
3112 if (x == 2 && j == 0)
3113 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3115 if (x == 9 && j == 0)
3116 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3128 htype = REPOKEY_TYPE_MD5;
3130 htype = REPOKEY_TYPE_SHA1;
3132 htype = REPOKEY_TYPE_SHA256;
3133 if (htype && pubkeyl)
3135 void *h = solv_chksum_create(htype);
3136 unsigned char b[6], *cs;
3140 b[1] = pubkeyl >> 8;
3142 solv_chksum_add(h, b, 3);
3143 solv_chksum_add(h, pubkey, pubkeyl);
3144 if (p[1] >= 0x10 && p[1] <= 0x13)
3147 b[1] = useridl >> 24;
3148 b[2] = useridl >> 16;
3149 b[3] = useridl >> 8;
3151 solv_chksum_add(h, b, 5);
3152 solv_chksum_add(h, userid, useridl);
3154 hl = 6 + (p[4] << 8 | p[5]);
3155 solv_chksum_add(h, p, hl);
3162 solv_chksum_add(h, b, 6);
3163 cs = solv_chksum_get(h, 0);
3164 solv_chksum_free(h, 0);
3167 if (!memcmp(keyid, issuer, 8))
3169 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
3175 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
3182 userid = solv_realloc(userid, l);
3184 memcpy(userid, p, l);
3191 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3196 /* this is private to rpm, but rpm lacks an interface to retrieve
3197 * the values. Sigh. */
3198 struct pgpDigParams_s {
3199 const char * userid;
3200 const unsigned char * hash;
3201 const char * params[4];
3203 unsigned char version; /*!< version number. */
3204 unsigned char time[4]; /*!< time that the key was created. */
3205 unsigned char pubkey_algo; /*!< public key algorithm. */
3206 unsigned char hash_algo;
3207 unsigned char sigtype;
3208 unsigned char hashlen;
3209 unsigned char signhash16[2];
3210 unsigned char signid[8];
3211 unsigned char saved;
3215 struct pgpDigParams_s signature;
3216 struct pgpDigParams_s pubkey;
3220 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3222 Pool *pool = s->repo->pool;
3223 unsigned char *pkts;
3228 char evrbuf[8 + 1 + 8 + 1];
3230 pkts = unarmor(pubkey, &pktsl);
3233 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3234 parsekeydata(s, data, pkts, pktsl);
3235 /* only rpm knows how to do the release calculation, we don't dare
3236 * to recreate all the bugs */
3240 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3242 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3243 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3244 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]);
3245 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3247 s->name = pool_str2id(pool, "gpg-pubkey", 1);
3248 s->evr = pool_str2id(pool, evrbuf, 1);
3250 for (i = 0; i < 8; i++)
3251 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3252 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3253 if (dig->pubkey.userid)
3254 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3256 (void)pgpFreeDig(dig);
3258 (void)pgpDigFree(dig);
3260 solv_free((void *)pkts);
3265 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
3267 Pool *pool = repo->pool;
3268 struct rpm_by_state state;
3269 struct rpmdbentry *entries;
3271 char *namedata, *str;
3275 const char *rootdir = 0;
3277 data = repo_add_repodata(repo, flags);
3278 if (flags & REPO_USE_ROOTDIR)
3279 rootdir = pool_get_rootdir(pool);
3281 memset(&state, 0, sizeof(state));
3282 if (!(state.dbenv = opendbenv(rootdir)))
3284 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3285 for (i = 0 ; i < nentries; i++)
3287 void *statep = &state;
3288 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3291 str = headstring(rpmhead, TAG_DESCRIPTION);
3294 s = pool_id2solvable(pool, repo_add_solvable(repo));
3295 pubkey2solvable(s, data, str);
3296 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3298 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3300 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3301 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3304 solv_free(namedata);
3306 if (!(flags & REPO_NO_INTERNALIZE))
3307 repodata_internalize(data);
3312 repo_add_pubkey(Repo *repo, const char *key, int flags)
3314 Pool *pool = repo->pool;
3321 data = repo_add_repodata(repo, flags);
3324 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
3326 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3331 if (bufl - l < 4096)
3334 buf = solv_realloc(buf, bufl);
3336 ll = fread(buf, 1, bufl - l, fp);
3340 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3349 s = pool_id2solvable(pool, repo_add_solvable(repo));
3350 if (!pubkey2solvable(s, data, buf))
3352 repo_free_solvable(repo, s - pool->solvables, 1);
3357 if (!(flags & REPO_NO_INTERNALIZE))
3358 repodata_internalize(data);
3359 return s - pool->solvables;
3362 #endif /* ENABLE_RPMDB_PUBKEY */