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_OBSOLETENAME 1090
90 #define TAG_FILEDEVICES 1095
91 #define TAG_FILEINODES 1096
92 #define TAG_SOURCEPACKAGE 1106
93 #define TAG_PROVIDEFLAGS 1112
94 #define TAG_PROVIDEVERSION 1113
95 #define TAG_OBSOLETEFLAGS 1114
96 #define TAG_OBSOLETEVERSION 1115
97 #define TAG_DIRINDEXES 1116
98 #define TAG_BASENAMES 1117
99 #define TAG_DIRNAMES 1118
100 #define TAG_PAYLOADFORMAT 1124
101 #define TAG_PATCHESNAME 1133
102 #define TAG_FILECOLORS 1140
103 #define TAG_SUGGESTSNAME 1156
104 #define TAG_SUGGESTSVERSION 1157
105 #define TAG_SUGGESTSFLAGS 1158
106 #define TAG_ENHANCESNAME 1159
107 #define TAG_ENHANCESVERSION 1160
108 #define TAG_ENHANCESFLAGS 1161
111 #define TAG_DISTEPOCH 1218
114 #define TAG_LONGFILESIZES 5008
115 #define TAG_LONGSIZE 5009
118 #define TAG_SIGBASE 256
119 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
120 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
122 #define SIGTAG_SIZE 1000
123 #define SIGTAG_PGP 1002 /* RSA signature */
124 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
125 #define SIGTAG_GPG 1005 /* DSA signature */
127 #define DEP_LESS (1 << 1)
128 #define DEP_GREATER (1 << 2)
129 #define DEP_EQUAL (1 << 3)
130 #define DEP_STRONG (1 << 27)
131 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
133 #define FILEFLAG_GHOST (1 << 6)
137 # define RPM_INDEX_SIZE 4
139 # define RPM_INDEX_SIZE 8
147 typedef struct rpmhead {
151 int forcebinary; /* sigh */
152 unsigned char data[1];
156 static inline unsigned char *
157 headfindtag(RpmHead *h, int tag)
160 unsigned char *d, taga[4];
166 for (i = 0; i < h->cnt; i++, d -= 16)
167 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
173 headexists(RpmHead *h, int tag)
175 return headfindtag(h, tag) ? 1 : 0;
178 static unsigned int *
179 headint32array(RpmHead *h, int tag, int *cnt)
181 unsigned int i, o, *r;
182 unsigned char *d = headfindtag(h, tag);
184 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
186 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
187 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
188 if (o + 4 * i > h->dcnt)
191 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
194 for (o = 0; o < i; o++, d += 4)
195 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
199 /* returns the first entry of an integer array */
201 headint32(RpmHead *h, int tag)
204 unsigned char *d = headfindtag(h, tag);
206 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
208 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
209 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
210 if (i == 0 || o + 4 * i > h->dcnt)
213 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
216 /* returns the first entry of an 64bit integer array */
217 static unsigned long long
218 headint64(RpmHead *h, int tag)
221 unsigned char *d = headfindtag(h, tag);
222 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
224 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
225 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
226 if (i == 0 || o + 8 * i > h->dcnt)
229 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
230 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
233 static unsigned int *
234 headint16array(RpmHead *h, int tag, int *cnt)
236 unsigned int i, o, *r;
237 unsigned char *d = headfindtag(h, tag);
239 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
241 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
242 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
243 if (o + 4 * i > h->dcnt)
246 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
249 for (o = 0; o < i; o++, d += 2)
250 r[o] = d[0] << 8 | d[1];
255 headstring(RpmHead *h, int tag)
258 unsigned char *d = headfindtag(h, tag);
259 /* 6: STRING, 9: I18NSTRING */
260 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
262 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
265 return (char *)h->dp + o;
269 headstringarray(RpmHead *h, int tag, int *cnt)
272 unsigned char *d = headfindtag(h, tag);
275 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
277 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
278 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
279 r = solv_calloc(i ? i : 1, sizeof(char *));
283 for (o = 0; o < i; o++)
287 d += strlen((char *)d) + 1;
288 if (d >= h->dp + h->dcnt)
297 static unsigned char *
298 headbinary(RpmHead *h, int tag, unsigned int *sizep)
301 unsigned char *d = headfindtag(h, tag);
302 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
304 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
305 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
306 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
313 static char *headtoevr(RpmHead *h)
321 version = headstring(h, TAG_VERSION);
322 release = headstring(h, TAG_RELEASE);
323 epoch = headint32(h, TAG_EPOCH);
324 if (!version || !release)
326 fprintf(stderr, "headtoevr: bad rpm header\n");
329 for (v = version; *v >= '0' && *v <= '9'; v++)
331 if (epoch || (v != version && *v == ':'))
333 char epochbuf[11]; /* 32bit decimal will fit in */
334 sprintf(epochbuf, "%u", epoch);
335 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
336 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
340 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
341 sprintf(evr, "%s-%s", version, release);
343 distepoch = headstring(h, TAG_DISTEPOCH);
344 if (distepoch && *distepoch)
347 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
349 strcpy(evr + l, distepoch);
356 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
358 const unsigned char *cp;
361 unsigned char *buf = 0, *bp;
363 /* check if it's already utf8, code taken from screen ;-) */
364 cp = (const unsigned char *)str;
365 while ((c = *cp++) != 0)
369 if ((c & 0xc0) != 0x80)
370 break; /* encoding error */
371 c = (c & 0x3f) | (state << 6);
372 if (!(state & 0x40000000))
374 /* check for overlong sequences */
375 if ((c & 0x820823e0) == 0x80000000)
377 else if ((c & 0x020821f0) == 0x02000000)
379 else if ((c & 0x000820f8) == 0x00080000)
381 else if ((c & 0x0000207c) == 0x00002000)
391 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
393 c = (c & 0x03) | 0xbfffff00; /* 4 */
395 c = (c & 0x07) | 0xbfffc000; /* 3 */
397 c = (c & 0x0f) | 0xbff00000; /* 2 */
399 c = (c & 0x1f) | 0xfc000000; /* 1 */
403 state = (c & 0x80000000) ? c : 0;
407 /* not utf8, assume latin1 */
408 buf = solv_malloc(2 * strlen(str) + 1);
409 cp = (const unsigned char *)str;
412 while ((c = *cp++) != 0)
425 repodata_set_str(repodata, handle, tag, str);
431 #define MAKEDEPS_FILTER_WEAK (1 << 0)
432 #define MAKEDEPS_FILTER_STRONG (1 << 1)
433 #define MAKEDEPS_NO_RPMLIB (1 << 2)
436 * strong: 0: ignore strongness
437 * 1: filter to strong
441 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
445 int i, cc, nc, vc, fc;
447 unsigned int olddeps;
451 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
452 n = headstringarray(rpmhead, tagn, &nc);
455 v = headstringarray(rpmhead, tagv, &vc);
461 f = headint32array(rpmhead, tagf, &fc);
468 if (nc != vc || nc != fc)
470 fprintf(stderr, "bad dependency entries\n");
475 haspre = 0; /* add no prereq marker */
478 /* we do filtering */
480 for (i = 0; i < nc; i++)
482 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
484 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
485 if (!strncmp(n[i], "rpmlib(", 7))
487 if ((f[i] & DEP_PRE) != 0)
492 else if (tagn == TAG_REQUIRENAME)
494 /* no filtering, just look for the first prereq */
495 for (i = 0; i < nc; i++)
496 if ((f[i] & DEP_PRE) != 0)
510 olddeps = repo_reserve_ids(repo, 0, cc);
511 ida = repo->idarraydata + olddeps;
518 haspre = 2; /* pass two: prereqs */
520 *ida++ = SOLVABLE_PREREQMARKER;
522 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
524 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
526 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
528 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
529 if (!strncmp(n[i], "rpmlib(", 7))
531 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
535 if ((f[i] & DEP_LESS) != 0)
537 if ((f[i] & DEP_EQUAL) != 0)
539 if ((f[i] & DEP_GREATER) != 0)
541 name = pool_str2id(pool, n[i], 1);
542 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
543 evr = pool_str2id(pool, v[i] + 2, 1);
545 evr = pool_str2id(pool, v[i], 1);
546 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
549 *ida++ = pool_str2id(pool, n[i], 1);
552 repo->idarraysize += cc + 1;
560 #ifdef USE_FILEFILTER
562 #define FILEFILTER_EXACT 0
563 #define FILEFILTER_STARTS 1
564 #define FILEFILTER_CONTAINS 2
572 static struct filefilter filefilters[] = {
573 { FILEFILTER_CONTAINS, "/bin/", 0},
574 { FILEFILTER_CONTAINS, "/sbin/", 0},
575 { FILEFILTER_CONTAINS, "/lib/", 0},
576 { FILEFILTER_CONTAINS, "/lib64/", 0},
577 { FILEFILTER_CONTAINS, "/etc/", 0},
578 { FILEFILTER_STARTS, "/usr/games/", 0},
579 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
580 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
581 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
587 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
591 unsigned int *fkb, *fn, *fsz, *fm, *fino;
592 unsigned int inotest[256], inotestok;
596 /* XXX: use TAG_LONGFILESIZES if available */
597 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
598 if (!fsz || fc != fszc)
603 /* stupid rpm records sizes of directories, so we have to check the mode */
604 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
605 if (!fm || fc != fszc)
611 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
612 if (!fino || fc != fszc)
620 if (fc < sizeof(inotest))
622 memset(inotest, 0, sizeof(inotest));
623 for (i = 0; i < fc; i++)
626 if (fsz[i] == 0 || !S_ISREG(fm[i]))
628 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
629 bit = 1 << (fino[i] & 31);
630 if ((inotest[off] & bit) != 0)
639 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
641 unsigned int mask, hash, hh;
642 if (!fdev || fc != fszc)
651 while ((mask & (mask - 1)) != 0)
652 mask = mask & (mask - 1);
654 if (mask > sizeof(inotest)/sizeof(*inotest))
655 fx = solv_calloc(mask, sizeof(unsigned int));
659 memset(fx, 0, mask * sizeof(unsigned int));
662 for (i = 0; i < fc; i++)
664 if (fsz[i] == 0 || !S_ISREG(fm[i]))
666 hash = (fino[i] + fdev[i] * 31) & mask;
668 while ((j = fx[hash]) != 0)
670 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
672 fsz[i] = 0; /* kill entry */
675 hash = (hash + hh++) & mask;
685 fn = solv_calloc(dc, sizeof(unsigned int));
686 fkb = solv_calloc(dc, sizeof(unsigned int));
687 for (i = 0; i < fc; i++)
692 if (fsz[i] == 0 || !S_ISREG(fm[i]))
694 fkb[di[i]] += fsz[i] / 1024 + 1;
699 handle = s - pool->solvables;
700 for (i = 0; i < dc; i++)
706 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
707 did = repodata_str2dir(data, "/usr/src", 1);
709 continue; /* work around rpm bug */
712 did = repodata_str2dir(data, dn[i], 1);
713 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
719 /* assumes last processed array is provides! */
721 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 handle = s - pool->solvables;
816 did = repodata_str2dir(data, dn[di[i]], 1);
819 did = repodata_str2dir(data, "/", 1);
820 if (b && b[0] == '/')
821 b++; /* work around rpm bug */
823 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
837 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
843 name = headstring(rpmhead, TAG_NAME);
846 fprintf(stderr, "package has no name\n");
849 if (!strcmp(name, "gpg-pubkey"))
851 s->name = pool_str2id(pool, name, 1);
852 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
853 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
854 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
857 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
858 s->arch = ARCH_NOSRC;
863 s->arch = ARCH_NOARCH;
864 evr = headtoevr(rpmhead);
865 s->evr = pool_str2id(pool, evr, 1);
866 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
868 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
869 if ((flags & RPM_ADD_NO_FILELIST) == 0)
870 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
871 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
872 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
873 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
874 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
875 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
877 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
878 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
879 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
880 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
881 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
882 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
889 unsigned long long u64;
891 handle = s - pool->solvables;
892 str = headstring(rpmhead, TAG_SUMMARY);
894 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
895 str = headstring(rpmhead, TAG_DESCRIPTION);
899 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
900 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
904 /* oh my, found SUSE special author section */
906 str = solv_strdup(str);
909 while (l > 0 && str[l - 1] == '\n')
912 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
914 aut = str; /* copy over */
915 while (*p == ' ' || *p == '\n')
928 while (aut != str && aut[-1] == '\n')
932 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
936 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
938 str = headstring(rpmhead, TAG_GROUP);
940 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
941 str = headstring(rpmhead, TAG_LICENSE);
943 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
944 str = headstring(rpmhead, TAG_URL);
946 repodata_set_str(data, handle, SOLVABLE_URL, str);
947 str = headstring(rpmhead, TAG_DISTRIBUTION);
949 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
950 str = headstring(rpmhead, TAG_PACKAGER);
952 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
953 if ((flags & RPM_ADD_WITH_PKGID) != 0)
955 unsigned char *chksum;
956 unsigned int chksumsize;
957 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
958 if (chksum && chksumsize == 16)
959 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
961 if ((flags & RPM_ADD_WITH_HDRID) != 0)
963 str = headstring(rpmhead, TAG_SHA1HEADER);
964 if (str && strlen(str) == 40)
965 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
966 else if (str && strlen(str) == 64)
967 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
969 u32 = headint32(rpmhead, TAG_BUILDTIME);
971 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
972 u32 = headint32(rpmhead, TAG_INSTALLTIME);
974 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
975 u64 = headint64(rpmhead, TAG_LONGSIZE);
977 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
980 u32 = headint32(rpmhead, TAG_SIZE);
982 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
985 repodata_set_sourcepkg(data, handle, sourcerpm);
986 if ((flags & RPM_ADD_TRIGGERS) != 0)
989 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
992 for (; (id = repo->idarraydata[ida]) != 0; ida++)
994 /* we currently do not support rel ids in incore data, so
995 * strip off versioning information */
998 Reldep *rd = GETRELDEP(pool, id);
1003 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1013 copyreldep(Pool *pool, Pool *frompool, Id id)
1015 Reldep *rd = GETRELDEP(frompool, id);
1016 Id name = rd->name, evr = rd->evr;
1018 name = copyreldep(pool, frompool, name);
1020 name = pool_str2id(pool, pool_id2str(frompool, name), 1);
1022 evr = copyreldep(pool, frompool, evr);
1024 evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1025 return pool_rel2id(pool, name, evr, rd->flags, 1);
1029 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1034 Pool *frompool = fromrepo->pool;
1038 from = fromrepo->idarraydata + fromoff;
1039 for (ida = from, cc = 0; *ida; ida++, cc++)
1043 ido = repo_reserve_ids(repo, 0, cc);
1044 ida = repo->idarraydata + ido;
1045 if (frompool && pool != frompool)
1051 id = copyreldep(pool, frompool, id);
1053 id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1059 memcpy(ida, from, (cc + 1) * sizeof(Id));
1060 repo->idarraysize += cc + 1;
1064 #define COPYDIR_DIRCACHE_SIZE 512
1066 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1069 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1071 if (cache && cache[did & 255] == did)
1072 return cache[(did & 255) + 256];
1073 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1077 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1079 Id parent = dirpool_parent(&fromdata->dirpool, did);
1080 Id compid = dirpool_compid(&fromdata->dirpool, did);
1082 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1083 if (fromspool != &pool->ss)
1084 compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1085 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1088 cache[did & 255] = did;
1089 cache[(did & 255) + 256] = compid;
1094 struct solvable_copy_cbdata {
1101 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1103 struct solvable_copy_cbdata *cbdata = vcbdata;
1105 Repodata *data = cbdata->data;
1106 Id handle = cbdata->handle;
1107 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1108 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1110 keyname = key->name;
1111 if (keyname >= ID_NUM_INTERNAL && pool != frompool)
1112 keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1115 case REPOKEY_TYPE_ID:
1116 case REPOKEY_TYPE_CONSTANTID:
1117 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1119 assert(!data->localpool); /* implement me! */
1120 if (pool != frompool || fromdata->localpool)
1123 id = copyreldep(pool, frompool, id);
1125 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1127 if (key->type == REPOKEY_TYPE_ID)
1128 repodata_set_id(data, handle, keyname, id);
1129 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1130 repodata_set_constantid(data, handle, keyname, id);
1132 repodata_add_idarray(data, handle, keyname, id);
1134 case REPOKEY_TYPE_STR:
1135 repodata_set_str(data, handle, keyname, kv->str);
1137 case REPOKEY_TYPE_VOID:
1138 repodata_set_void(data, handle, keyname);
1140 case REPOKEY_TYPE_NUM:
1141 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1143 case REPOKEY_TYPE_CONSTANT:
1144 repodata_set_constant(data, handle, keyname, kv->num);
1146 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1148 assert(!data->localpool); /* implement me! */
1149 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1150 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1152 case REPOKEY_TYPE_DIRSTRARRAY:
1154 assert(!data->localpool); /* implement me! */
1155 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1156 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1165 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1167 Repo *repo = s->repo;
1168 Repo *fromrepo = r->repo;
1169 Pool *pool = repo->pool;
1170 struct solvable_copy_cbdata cbdata;
1172 /* copy solvable data */
1173 if (pool == fromrepo->pool)
1178 s->vendor = r->vendor;
1183 s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1185 s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1187 s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1189 s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1191 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1192 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1193 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1194 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1195 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1196 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1197 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1198 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1200 /* copy all attributes */
1204 cbdata.handle = s - pool->solvables;
1205 cbdata.dircache = dircache;
1206 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1209 /* used to sort entries returned in some database order */
1211 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1213 struct rpmid const *a = va, *b = vb;
1215 r = strcmp(a->name, b->name);
1218 return a->dbid - b->dbid;
1222 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1225 Pool *pool = repo->pool;
1226 Solvable *a = pool->solvables + *(Id *)va;
1227 Solvable *b = pool->solvables + *(Id *)vb;
1230 if (a->name != b->name)
1231 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1232 rpmdbid = repo->rpmdbid;
1233 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1237 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1239 Pool *pool = repo->pool;
1242 tmp = pool->solvables[pa];
1243 pool->solvables[pa] = pool->solvables[pb];
1244 pool->solvables[pb] = tmp;
1247 Id tmpid = repo->rpmdbid[pa - repo->start];
1248 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1249 repo->rpmdbid[pb - repo->start] = tmpid;
1251 /* only works if nothing is already internalized! */
1253 repodata_swap_attrs(data, pa, pb);
1257 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1260 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1262 # if defined(WORDS_BIGENDIAN)
1267 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1269 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1273 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1276 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1278 # if defined(WORDS_BIGENDIAN)
1283 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1285 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1290 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1292 memset(cookie, 0, 32);
1293 cookie[3] = RPMDB_COOKIE_VERSION;
1294 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1295 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1298 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1300 opendbenv(const char *rootdir)
1302 char dbpath[PATH_MAX];
1306 if (db_env_create(&dbenv, 0))
1308 perror("db_env_create");
1311 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1312 dbenv->set_thread_count(dbenv, 8);
1314 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1315 if (access(dbpath, W_OK) == -1)
1317 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1322 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1324 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1329 perror("dbenv open");
1330 dbenv->close(dbenv, 0);
1338 count_headers(const char *rootdir, DB_ENV *dbenv)
1340 char dbpath[PATH_MAX];
1341 struct stat statbuf;
1348 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1349 if (stat(dbpath, &statbuf))
1351 memset(&dbkey, 0, sizeof(dbkey));
1352 memset(&dbdata, 0, sizeof(dbdata));
1353 if (db_create(&db, dbenv, 0))
1355 perror("db_create");
1358 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1360 perror("db->open Name index");
1363 if (db->cursor(db, NULL, &dbc, 0))
1365 perror("db->cursor");
1368 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1369 count += dbdata.size / RPM_INDEX_SIZE;
1376 * read rpm db as repo
1381 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1383 Pool *pool = repo->pool;
1384 unsigned char buf[16];
1391 struct rpmid *rpmids, *rp;
1397 unsigned int refmask, h;
1398 char dbpath[PATH_MAX];
1402 struct stat packagesstat;
1403 unsigned char newcookie[32];
1404 const unsigned char *oldcookie = 0;
1405 Id oldcookietype = 0;
1407 int count = 0, done = 0;
1409 const char *rootdir = 0;
1411 now = solv_timems(0);
1412 memset(&dbkey, 0, sizeof(dbkey));
1413 memset(&dbdata, 0, sizeof(dbdata));
1415 data = repo_add_repodata(repo, flags);
1417 if (ref && !(ref->nsolvables && ref->rpmdbid))
1420 if (flags & REPO_USE_ROOTDIR)
1421 rootdir = pool_get_rootdir(pool);
1422 if (!(dbenv = opendbenv(rootdir)))
1424 return pool_error(pool, -1, "repo_add_rpmdb: opendbenv failed");
1427 /* XXX: should get ro lock of Packages database! */
1428 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1429 if (stat(dbpath, &packagesstat))
1431 return pool_error(pool, -1, "repo_add_rpmdb: %s: %s", dbpath, strerror(errno));
1433 mkrpmdbcookie(&packagesstat, newcookie);
1434 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1437 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1438 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1441 int solvstart = 0, solvend = 0;
1443 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1444 count = count_headers(rootdir, dbenv);
1445 if (db_create(&db, dbenv, 0))
1447 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1448 dbenv->close(dbenv, 0);
1451 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1453 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1455 dbenv->close(dbenv, 0);
1458 if (db->get_byteswapped(db, &byteswapped))
1461 dbenv->close(dbenv, 0);
1462 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1464 if (db->cursor(db, NULL, &dbc, 0))
1467 dbenv->close(dbenv, 0);
1468 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1474 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1478 s = pool_id2solvable(pool, repo_add_solvable(repo));
1480 solvstart = s - pool->solvables;
1481 solvend = s - pool->solvables + 1;
1484 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1485 if (dbkey.size != 4)
1489 dbenv->close(dbenv, 0);
1490 return pool_error(pool, -1, "corrupt Packages database (key size)");
1492 dbid = db2rpmdbid(dbkey.data, byteswapped);
1493 if (dbid == 0) /* the join key */
1495 if (dbdata.size < 8)
1499 dbenv->close(dbenv, 0);
1500 return pool_error(pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1502 if (dbdata.size > rpmheadsize)
1504 rpmheadsize = dbdata.size + 128;
1505 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1507 memcpy(buf, dbdata.data, 8);
1508 rpmhead->forcebinary = 1;
1509 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1510 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1511 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1515 dbenv->close(dbenv, 0);
1516 return pool_error(pool, -1, "corrupt rpm database (data size)\n");
1518 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1519 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1520 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1521 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1528 /* We can reuse this solvable, but make sure it's still
1529 associated with this repo. */
1530 memset(s, 0, sizeof(*s));
1533 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1537 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1538 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1543 /* oops, could not reuse. free it instead */
1544 repo_free_solvable(repo, s - pool->solvables, 1);
1551 /* now sort all solvables in the new solvstart..solvend block */
1552 if (solvend - solvstart > 1)
1554 pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1555 for (i = solvstart; i < solvend; i++)
1556 pkgids[i - solvstart] = i;
1557 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1559 for (i = solvstart; i < solvend; i++)
1561 int j = pkgids[i - solvstart];
1563 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1565 swap_solvables(repo, data, i, j);
1572 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1574 memset(dircache, 0, sizeof(dircache));
1575 if (db_create(&db, dbenv, 0))
1577 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1578 dbenv->close(dbenv, 0);
1581 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1583 pool_error(pool, -1, "repo_add_rpmdb: db->open Name index failed: %s", strerror(errno));
1585 dbenv->close(dbenv, 0);
1588 if (db->get_byteswapped(db, &byteswapped))
1591 dbenv->close(dbenv, 0);
1592 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1594 if (db->cursor(db, NULL, &dbc, 0))
1597 dbenv->close(dbenv, 0);
1598 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1602 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1604 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1608 while(dl >= RPM_INDEX_SIZE)
1610 rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1611 rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1612 rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1613 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1614 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1616 dp += RPM_INDEX_SIZE;
1617 dl -= RPM_INDEX_SIZE;
1625 solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1630 /* create hash from dbid to ref */
1631 refmask = mkmask(ref->nsolvables);
1632 refhash = solv_calloc(refmask + 1, sizeof(Id));
1633 for (i = 0; i < ref->end - ref->start; i++)
1635 if (!ref->rpmdbid[i])
1637 h = ref->rpmdbid[i] & refmask;
1639 h = (h + 317) & refmask;
1640 refhash[h] = i + 1; /* make it non-zero */
1643 /* count the misses, they will cost us time */
1644 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1646 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1652 while ((id = refhash[h]))
1654 if (ref->rpmdbid[id - 1] == dbid)
1656 h = (h + 317) & refmask;
1665 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1667 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1669 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1672 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1676 while ((id = refhash[h]))
1678 if (ref->rpmdbid[id - 1] == dbid)
1680 h = (h + 317) & refmask;
1684 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1687 solvable_copy(s, r, data, dircache);
1694 if (db_create(&db, dbenv, 0))
1696 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1697 dbenv->close(dbenv, 0);
1700 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1702 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1704 dbenv->close(dbenv, 0);
1707 if (db->get_byteswapped(db, &byteswapped))
1710 dbenv->close(dbenv, 0);
1711 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1714 rpmdbid2db(buf, rp->dbid, byteswapped);
1719 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1722 dbenv->close(dbenv, 0);
1723 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1725 if (dbdata.size < 8)
1728 dbenv->close(dbenv, 0);
1729 return pool_error(pool, -1, "corrupt Packages database (size)");
1731 if (dbdata.size > rpmheadsize)
1733 rpmheadsize = dbdata.size + 128;
1734 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1736 memcpy(buf, dbdata.data, 8);
1737 rpmhead->forcebinary = 1;
1738 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1739 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1740 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1743 dbenv->close(dbenv, 0);
1744 return pool_error(pool, -1, "corrupt Packages database (data size)");
1746 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1747 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1749 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1750 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1754 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1755 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1763 for (i = 0; i < nrpmids; i++)
1764 solv_free(rpmids[i].name);
1770 dbenv->close(dbenv, 0);
1773 if (!(flags & REPO_NO_INTERNALIZE))
1774 repodata_internalize(data);
1775 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1776 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1777 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1778 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1779 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)));
1784 static inline unsigned int
1785 getu32(const unsigned char *dp)
1787 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1792 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1794 int sigdsize, sigcnt, l;
1795 Pool *pool = repo->pool;
1797 RpmHead *rpmhead = 0;
1798 int rpmheadsize = 0;
1799 char *payloadformat;
1801 unsigned char lead[4096];
1802 int headerstart, headerend;
1805 unsigned char pkgid[16];
1806 unsigned char leadsigid[16];
1807 unsigned char hdrid[32];
1808 int pkgidtype, leadsigidtype, hdridtype;
1811 void *leadsigchksumh = 0;
1812 int forcebinary = 0;
1814 data = repo_add_repodata(repo, flags);
1816 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1817 chksumtype = REPOKEY_TYPE_SHA256;
1818 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1819 chksumtype = REPOKEY_TYPE_SHA1;
1821 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1823 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1826 if (fstat(fileno(fp), &stb))
1828 pool_error(pool, -1, "fstat: %s", strerror(errno));
1833 chksumh = solv_chksum_create(chksumtype);
1834 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1835 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1836 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1838 pool_error(pool, -1, "%s: not a rpm", rpm);
1842 forcebinary = lead[6] != 0 || lead[7] != 1;
1844 solv_chksum_add(chksumh, lead, 96 + 16);
1846 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1847 if (lead[78] != 0 || lead[79] != 5)
1849 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1853 if (getu32(lead + 96) != 0x8eade801)
1855 pool_error(pool, -1, "%s: bad signature header", rpm);
1859 sigcnt = getu32(lead + 96 + 8);
1860 sigdsize = getu32(lead + 96 + 12);
1861 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1863 pool_error(pool, -1, "%s: bad signature header", rpm);
1867 sigdsize += sigcnt * 16;
1868 sigdsize = (sigdsize + 7) & ~7;
1869 headerstart = 96 + 16 + sigdsize;
1870 pkgidtype = leadsigidtype = hdridtype = 0;
1871 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1873 /* extract pkgid or hdrid from the signature header */
1874 if (sigdsize > rpmheadsize)
1876 rpmheadsize = sigdsize + 128;
1877 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1879 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1881 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1886 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1888 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1889 rpmhead->forcebinary = 0;
1890 rpmhead->cnt = sigcnt;
1891 rpmhead->dcnt = sigdsize - sigcnt * 16;
1892 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1893 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1895 unsigned char *chksum;
1896 unsigned int chksumsize;
1897 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1898 if (chksum && chksumsize == 16)
1900 pkgidtype = REPOKEY_TYPE_MD5;
1901 memcpy(pkgid, chksum, 16);
1904 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1906 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1907 if (str && strlen(str) == 40)
1909 if (solv_hex2bin(&str, hdrid, 20) == 20)
1910 hdridtype = REPOKEY_TYPE_SHA1;
1912 else if (str && strlen(str) == 64)
1914 if (solv_hex2bin(&str, hdrid, 32) == 32)
1915 hdridtype = REPOKEY_TYPE_SHA256;
1921 /* just skip the signature header */
1924 l = sigdsize > 4096 ? 4096 : sigdsize;
1925 if (fread(lead, l, 1, fp) != 1)
1927 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1932 solv_chksum_add(chksumh, lead, l);
1934 solv_chksum_add(leadsigchksumh, lead, l);
1940 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
1941 leadsigidtype = REPOKEY_TYPE_MD5;
1943 if (fread(lead, 16, 1, fp) != 1)
1945 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1950 solv_chksum_add(chksumh, lead, 16);
1951 if (getu32(lead) != 0x8eade801)
1953 pool_error(pool, -1, "%s: bad header", rpm);
1957 sigcnt = getu32(lead + 8);
1958 sigdsize = getu32(lead + 12);
1959 if (sigcnt >= 0x100000 || sigdsize >= 0x800000)
1961 pool_error(pool, -1, "%s: bad header", rpm);
1965 l = sigdsize + sigcnt * 16;
1966 headerend = headerstart + 16 + l;
1967 if (l > rpmheadsize)
1969 rpmheadsize = l + 128;
1970 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1972 if (fread(rpmhead->data, l, 1, fp) != 1)
1974 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1979 solv_chksum_add(chksumh, rpmhead->data, l);
1980 rpmhead->forcebinary = forcebinary;
1981 rpmhead->cnt = sigcnt;
1982 rpmhead->dcnt = sigdsize;
1983 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1984 if (headexists(rpmhead, TAG_PATCHESNAME))
1986 /* this is a patch rpm, ignore */
1987 pool_error(pool, -1, "%s: is patch rpm", rpm);
1989 solv_chksum_free(chksumh, 0);
1993 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1994 if (payloadformat && !strcmp(payloadformat, "drpm"))
1996 /* this is a delta rpm */
1997 pool_error(pool, -1, "%s: is delta rpm", rpm);
1999 solv_chksum_free(chksumh, 0);
2004 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2005 solv_chksum_add(chksumh, lead, l);
2007 s = pool_id2solvable(pool, repo_add_solvable(repo));
2008 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2010 repo_free_solvable(repo, s - pool->solvables, 1);
2011 solv_chksum_free(chksumh, 0);
2015 if (!(flags & REPO_NO_LOCATION))
2016 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2017 if (S_ISREG(stb.st_mode))
2018 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2019 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2021 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2023 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2025 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2028 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2029 chksumh = solv_chksum_free(chksumh, 0);
2033 if (!(flags & REPO_NO_INTERNALIZE))
2034 repodata_internalize(data);
2035 return s - pool->solvables;
2039 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2041 Pool *pool = repo->pool;
2043 RpmHead *rpmhead = rpmhandle;
2045 char *payloadformat;
2047 data = repo_add_repodata(repo, flags);
2048 if (headexists(rpmhead, TAG_PATCHESNAME))
2050 pool_error(pool, -1, "is a patch rpm");
2053 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2054 if (payloadformat && !strcmp(payloadformat, "drpm"))
2056 /* this is a delta rpm */
2057 pool_error(pool, -1, "is a delta rpm");
2060 s = pool_id2solvable(pool, repo_add_solvable(repo));
2061 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2063 repo_free_solvable(repo, s - pool->solvables, 1);
2066 if (!(flags & REPO_NO_INTERNALIZE))
2067 repodata_internalize(data);
2068 return s - pool->solvables;
2072 linkhash(const char *lt, char *hash)
2075 const unsigned char *str = (const unsigned char *)lt;
2079 while ((c = *str++) != 0)
2081 sprintf(hash, "%08x", r);
2082 sprintf(hash + 8, "%08x", l);
2083 sprintf(hash + 16, "%08x", 0);
2084 sprintf(hash + 24, "%08x", 0);
2088 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
2090 RpmHead *rpmhead = rpmhandle;
2095 unsigned int *di, diidx;
2096 unsigned int *co = 0;
2097 unsigned int *ff = 0;
2098 unsigned int lastdir;
2101 int cnt, dcnt, cnt2;
2105 char md5[33], *md5p = 0;
2107 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2110 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2112 for (i = 0; i < dcnt; i++)
2113 (*cb)(cbdata, dn[i], 0, (char *)0);
2117 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2123 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2124 if (!di || cnt != cnt2)
2131 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2132 if (!fm || cnt != cnt2)
2140 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2142 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2143 if (!md || cnt != cnt2)
2153 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2155 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2156 if (!co || cnt != cnt2)
2167 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2169 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2170 if (!ff || cnt != cnt2)
2184 for (i = 0; i < cnt; i++)
2186 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2191 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2194 l = l1 + strlen(bn[i]) + 1;
2198 space = solv_realloc(space, spacen);
2200 if (lastdir != diidx)
2202 strcpy(space, dn[diidx]);
2206 strcpy(space + l1, bn[i]);
2215 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2221 linkhash(lt[i], md5);
2227 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2231 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2245 rpm_query(void *rpmhandle, Id what)
2247 const char *name, *arch, *sourcerpm;
2251 RpmHead *rpmhead = rpmhandle;
2256 name = headstring(rpmhead, TAG_NAME);
2259 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2260 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2261 arch = headstring(rpmhead, TAG_ARCH);
2264 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2271 evr = headtoevr(rpmhead);
2274 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2276 sprintf(r, "%s-%s.%s", name, evr, arch);
2280 name = headstring(rpmhead, TAG_NAME);
2281 r = solv_strdup(name);
2284 r = headtoevr(rpmhead);
2291 struct rpm_by_state {
2306 #define ENTRIES_BLOCK 255
2307 #define NAMEDATA_BLOCK 1023
2309 static struct rpmdbentry *
2310 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2323 struct rpmdbentry *entries = 0;
2329 dbenv = state->dbenv;
2330 if (db_create(&db, dbenv, 0))
2332 perror("db_create");
2335 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2337 perror("db->open index");
2341 if (db->get_byteswapped(db, &byteswapped))
2343 perror("db->get_byteswapped");
2347 if (db->cursor(db, NULL, &dbc, 0))
2349 perror("db->cursor");
2353 memset(&dbkey, 0, sizeof(dbkey));
2354 memset(&dbdata, 0, sizeof(dbdata));
2357 dbkey.data = (void *)match;
2358 dbkey.size = strlen(match);
2360 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2362 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2366 while(dl >= RPM_INDEX_SIZE)
2368 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2369 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2370 entries[nentries].nameoff = namedatal;
2372 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2373 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2374 namedata[namedatal + dbkey.size] = 0;
2375 namedatal += dbkey.size + 1;
2376 dp += RPM_INDEX_SIZE;
2377 dl -= RPM_INDEX_SIZE;
2384 *nentriesp = nentries;
2385 *namedatap = namedata;
2390 freestate(struct rpm_by_state *state)
2396 state->db->close(state->db, 0);
2398 state->dbenv->close(state->dbenv, 0);
2399 solv_free(state->rpmhead);
2403 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2405 struct rpm_by_state state;
2406 struct rpmdbentry *entries;
2413 queue_empty(rpmdbidq);
2414 memset(&state, 0, sizeof(state));
2415 if (!(state.dbenv = opendbenv(rootdir)))
2417 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2419 for (i = 0; i < nentries; i++)
2420 queue_push(rpmdbidq, entries[i].rpmdbid);
2422 solv_free(namedata);
2428 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2430 struct rpm_by_state *state = *statep;
2431 unsigned char buf[16];
2441 *statep = (void *)0;
2447 state = solv_calloc(1, sizeof(*state));
2450 if (!state->dbopened)
2452 state->dbopened = 1;
2453 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2455 if (db_create(&state->db, state->dbenv, 0))
2457 perror("db_create");
2459 state->dbenv->close(state->dbenv, 0);
2463 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2465 perror("db->open var/lib/rpm/Packages");
2466 state->db->close(state->db, 0);
2468 state->dbenv->close(state->dbenv, 0);
2472 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2474 perror("db->get_byteswapped");
2475 state->db->close(state->db, 0);
2477 state->dbenv->close(state->dbenv, 0);
2482 rpmdbid2db(buf, rpmdbid, state->byteswapped);
2483 memset(&dbkey, 0, sizeof(dbkey));
2484 memset(&dbdata, 0, sizeof(dbdata));
2489 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2494 if (dbdata.size < 8)
2496 fprintf(stderr, "corrupt rpm database (size)\n");
2499 if (dbdata.size > state->rpmheadsize)
2501 state->rpmheadsize = dbdata.size + 128;
2502 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2504 rpmhead = state->rpmhead;
2505 memcpy(buf, dbdata.data, 8);
2506 rpmhead->forcebinary = 1;
2507 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2508 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2509 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2511 fprintf(stderr, "corrupt rpm database (data size)\n");
2514 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2515 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2520 rpm_byfp(FILE *fp, const char *name, void **statep)
2522 struct rpm_by_state *state = *statep;
2523 /* int headerstart, headerend; */
2525 int sigdsize, sigcnt, l;
2526 unsigned char lead[4096];
2527 int forcebinary = 0;
2530 return rpm_byrpmdbid(0, 0, statep);
2533 state = solv_calloc(1, sizeof(*state));
2536 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2538 fprintf(stderr, "%s: not a rpm\n", name);
2541 forcebinary = lead[6] != 0 || lead[7] != 1;
2542 if (lead[78] != 0 || lead[79] != 5)
2544 fprintf(stderr, "%s: not a V5 header\n", name);
2547 if (getu32(lead + 96) != 0x8eade801)
2549 fprintf(stderr, "%s: bad signature header\n", name);
2552 sigcnt = getu32(lead + 96 + 8);
2553 sigdsize = getu32(lead + 96 + 12);
2554 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2556 fprintf(stderr, "%s: bad signature header\n", name);
2559 sigdsize += sigcnt * 16;
2560 sigdsize = (sigdsize + 7) & ~7;
2561 /* headerstart = 96 + 16 + sigdsize; */
2564 l = sigdsize > 4096 ? 4096 : sigdsize;
2565 if (fread(lead, l, 1, fp) != 1)
2567 fprintf(stderr, "%s: unexpected EOF\n", name);
2572 if (fread(lead, 16, 1, fp) != 1)
2574 fprintf(stderr, "%s: unexpected EOF\n", name);
2577 if (getu32(lead) != 0x8eade801)
2579 fprintf(stderr, "%s: bad header\n", name);
2583 sigcnt = getu32(lead + 8);
2584 sigdsize = getu32(lead + 12);
2585 if (sigcnt >= 0x100000 || sigdsize >= 0x800000)
2587 fprintf(stderr, "%s: bad header\n", name);
2591 l = sigdsize + sigcnt * 16;
2592 /* headerend = headerstart + 16 + l; */
2593 if (l > state->rpmheadsize)
2595 state->rpmheadsize = l + 128;
2596 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2598 rpmhead = state->rpmhead;
2599 if (fread(rpmhead->data, l, 1, fp) != 1)
2601 fprintf(stderr, "%s: unexpected EOF\n", name);
2605 rpmhead->forcebinary = forcebinary;
2606 rpmhead->cnt = sigcnt;
2607 rpmhead->dcnt = sigdsize;
2608 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2612 #ifdef ENABLE_RPMDB_BYRPMHEADER
2615 rpm_byrpmh(Header h, void **statep)
2617 struct rpm_by_state *state = *statep;
2618 const unsigned char *uh;
2619 int sigdsize, sigcnt, l;
2623 uh = headerUnload(h);
2625 uh = headerUnload(h, NULL);
2629 sigcnt = getu32(uh);
2630 sigdsize = getu32(uh + 4);
2631 l = sigdsize + sigcnt * 16;
2634 state = solv_calloc(1, sizeof(*state));
2637 if (l > state->rpmheadsize)
2639 state->rpmheadsize = l + 128;
2640 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2642 rpmhead = state->rpmhead;
2643 memcpy(rpmhead->data, uh + 8, l - 8);
2645 rpmhead->forcebinary = 0;
2646 rpmhead->cnt = sigcnt;
2647 rpmhead->dcnt = sigdsize;
2648 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2655 #ifdef ENABLE_RPMDB_PUBKEY
2658 r64dec1(char *p, unsigned int *vp, int *eofp)
2663 for (i = 0; i < 4; )
2668 if (x >= 'A' && x <= 'Z')
2670 else if (x >= 'a' && x <= 'z')
2672 else if (x >= '0' && x <= '9')
2699 crc24(unsigned char *p, int len)
2701 unsigned int crc = 0xb704ceL;
2706 crc ^= (*p++) << 16;
2707 for (i = 0; i < 8; i++)
2708 if ((crc <<= 1) & 0x1000000)
2711 return crc & 0xffffffL;
2714 static unsigned char *
2715 unarmor(char *pubkey, int *pktlp)
2719 unsigned char *buf, *bp;
2723 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2725 pubkey = strchr(pubkey, '\n');
2730 pubkey = strchr(pubkey, '\n');
2733 /* skip header lines */
2736 while (*pubkey == ' ' || *pubkey == '\t')
2738 if (*pubkey == '\n')
2740 pubkey = strchr(pubkey, '\n');
2745 p = strchr(pubkey, '=');
2749 bp = buf = solv_malloc(l * 3 / 4 + 4);
2753 pubkey = r64dec1(pubkey, &v, &eof);
2763 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2766 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2771 if (v != crc24(buf, bp - buf))
2776 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2778 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2788 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2791 unsigned char keyid[8];
2792 unsigned int kcr = 0, maxex = 0;
2793 unsigned char *pubkey = 0;
2794 unsigned char *userid = 0;
2800 for (; pl; p += l, pl -= l)
2804 if (!(x & 128) || pl <= 0)
2809 tag = (x & 0x3c) >> 2;
2831 else if (x >= 192 && x < 224)
2835 l = ((x - 192) << 8) + *p++ + 192;
2842 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2853 pubkey = solv_realloc(pubkey, l);
2855 memcpy(pubkey, p, l);
2864 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2868 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2872 memset(keyid, 0, 8);
2873 if (p[7] == 1) /* RSA */
2876 unsigned char fp[16];
2880 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2881 memcpy(keyid, p + 10 + ql - 8, 8);
2882 h = solv_chksum_create(REPOKEY_TYPE_MD5);
2883 solv_chksum_add(h, p + 10, ql);
2885 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2886 solv_chksum_add(h, q + 2, ql);
2887 solv_chksum_free(h, fp);
2888 for (i = 0; i < 16; i++)
2889 sprintf(fpx + i * 2, "%02x", fp[i]);
2890 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2897 unsigned char hdr[3];
2898 unsigned char fp[20];
2901 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2905 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2906 solv_chksum_add(h, hdr, 3);
2907 solv_chksum_add(h, p, l);
2908 solv_chksum_free(h, fp);
2909 for (i = 0; i < 20; i++)
2910 sprintf(fpx + i * 2, "%02x", fp[i]);
2911 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2912 memcpy(keyid, fp + 12, 8);
2917 if (p[0] == 3 && p[1] == 5)
2922 /* printf("V3 signature packet\n"); */
2925 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2927 if (!memcmp(keyid, p + 6, 8))
2929 /* printf("SELF SIG\n"); */
2933 /* printf("OTHER SIG\n"); */
2937 htype = REPOKEY_TYPE_MD5;
2938 else if (p[16] == 2)
2939 htype = REPOKEY_TYPE_SHA1;
2940 else if (p[16] == 8)
2941 htype = REPOKEY_TYPE_SHA256;
2944 void *h = solv_chksum_create(htype);
2945 unsigned char b[3], *cs;
2948 b[1] = pubkeyl >> 8;
2950 solv_chksum_add(h, b, 3);
2951 solv_chksum_add(h, pubkey, pubkeyl);
2952 if (p[2] >= 0x10 && p[2] <= 0x13)
2953 solv_chksum_add(h, userid, useridl);
2954 solv_chksum_add(h, p + 2, 5);
2955 cs = solv_chksum_get(h, 0);
2956 solv_chksum_free(h, 0);
2962 int j, ql, haveissuer;
2964 unsigned int ex = 0;
2966 unsigned int scr = 0;
2968 unsigned char issuer[8];
2970 /* printf("V4 signature packet\n"); */
2973 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2978 for (j = 0; q && j < 2; j++)
2985 ql = q[0] << 8 | q[1];
3006 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
3017 sl = ((x - 192) << 8) + *q++ + 192;
3026 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
3027 if (x == 16 && sl == 9 && !haveissuer)
3029 memcpy(issuer, q + 1, 8);
3033 if (x == 2 && j == 0)
3034 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3036 if (x == 9 && j == 0)
3037 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3049 htype = REPOKEY_TYPE_MD5;
3051 htype = REPOKEY_TYPE_SHA1;
3053 htype = REPOKEY_TYPE_SHA256;
3054 if (htype && pubkeyl)
3056 void *h = solv_chksum_create(htype);
3057 unsigned char b[6], *cs;
3061 b[1] = pubkeyl >> 8;
3063 solv_chksum_add(h, b, 3);
3064 solv_chksum_add(h, pubkey, pubkeyl);
3065 if (p[1] >= 0x10 && p[1] <= 0x13)
3068 b[1] = useridl >> 24;
3069 b[2] = useridl >> 16;
3070 b[3] = useridl >> 8;
3072 solv_chksum_add(h, b, 5);
3073 solv_chksum_add(h, userid, useridl);
3075 hl = 6 + (p[4] << 8 | p[5]);
3076 solv_chksum_add(h, p, hl);
3083 solv_chksum_add(h, b, 6);
3084 cs = solv_chksum_get(h, 0);
3085 solv_chksum_free(h, 0);
3088 if (!memcmp(keyid, issuer, 8))
3090 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
3096 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
3103 userid = solv_realloc(userid, l);
3105 memcpy(userid, p, l);
3112 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3117 /* this is private to rpm, but rpm lacks an interface to retrieve
3118 * the values. Sigh. */
3119 struct pgpDigParams_s {
3120 const char * userid;
3121 const unsigned char * hash;
3122 const char * params[4];
3124 unsigned char version; /*!< version number. */
3125 unsigned char time[4]; /*!< time that the key was created. */
3126 unsigned char pubkey_algo; /*!< public key algorithm. */
3127 unsigned char hash_algo;
3128 unsigned char sigtype;
3129 unsigned char hashlen;
3130 unsigned char signhash16[2];
3131 unsigned char signid[8];
3132 unsigned char saved;
3136 struct pgpDigParams_s signature;
3137 struct pgpDigParams_s pubkey;
3141 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3143 Pool *pool = s->repo->pool;
3144 unsigned char *pkts;
3149 char evrbuf[8 + 1 + 8 + 1];
3151 pkts = unarmor(pubkey, &pktsl);
3154 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3155 parsekeydata(s, data, pkts, pktsl);
3156 /* only rpm knows how to do the release calculation, we don't dare
3157 * to recreate all the bugs */
3161 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3163 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3164 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3165 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]);
3166 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3168 s->name = pool_str2id(pool, "gpg-pubkey", 1);
3169 s->evr = pool_str2id(pool, evrbuf, 1);
3171 for (i = 0; i < 8; i++)
3172 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3173 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3174 if (dig->pubkey.userid)
3175 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3177 (void)pgpFreeDig(dig);
3179 (void)pgpDigFree(dig);
3181 solv_free((void *)pkts);
3186 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
3188 Pool *pool = repo->pool;
3189 struct rpm_by_state state;
3190 struct rpmdbentry *entries;
3192 char *namedata, *str;
3196 const char *rootdir = 0;
3198 data = repo_add_repodata(repo, flags);
3199 if (flags & REPO_USE_ROOTDIR)
3200 rootdir = pool_get_rootdir(pool);
3202 memset(&state, 0, sizeof(state));
3203 if (!(state.dbenv = opendbenv(rootdir)))
3205 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3206 for (i = 0 ; i < nentries; i++)
3208 void *statep = &state;
3209 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3212 str = headstring(rpmhead, TAG_DESCRIPTION);
3215 s = pool_id2solvable(pool, repo_add_solvable(repo));
3216 pubkey2solvable(s, data, str);
3217 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3219 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3221 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3222 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3225 solv_free(namedata);
3227 if (!(flags & REPO_NO_INTERNALIZE))
3228 repodata_internalize(data);
3233 repo_add_pubkey(Repo *repo, const char *key, int flags)
3235 Pool *pool = repo->pool;
3242 data = repo_add_repodata(repo, flags);
3245 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
3247 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3252 if (bufl - l < 4096)
3255 buf = solv_realloc(buf, bufl);
3257 ll = fread(buf, 1, bufl - l, fp);
3261 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3270 s = pool_id2solvable(pool, repo_add_solvable(repo));
3271 if (!pubkey2solvable(s, data, buf))
3273 repo_free_solvable(repo, s - pool->solvables, 1);
3278 if (!(flags & REPO_NO_INTERNALIZE))
3279 repodata_internalize(data);
3280 return s - pool->solvables;
3283 #endif /* ENABLE_RPMDB_PUBKEY */