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>
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_PROVIDEFLAGS 1112
93 #define TAG_PROVIDEVERSION 1113
94 #define TAG_OBSOLETEFLAGS 1114
95 #define TAG_OBSOLETEVERSION 1115
96 #define TAG_DIRINDEXES 1116
97 #define TAG_BASENAMES 1117
98 #define TAG_DIRNAMES 1118
99 #define TAG_PAYLOADFORMAT 1124
100 #define TAG_PATCHESNAME 1133
101 #define TAG_FILECOLORS 1140
102 #define TAG_SUGGESTSNAME 1156
103 #define TAG_SUGGESTSVERSION 1157
104 #define TAG_SUGGESTSFLAGS 1158
105 #define TAG_ENHANCESNAME 1159
106 #define TAG_ENHANCESVERSION 1160
107 #define TAG_ENHANCESFLAGS 1161
110 #define TAG_DISTEPOCH 1218
113 #define TAG_LONGFILESIZES 5008
114 #define TAG_LONGSIZE 5009
117 #define TAG_SIGBASE 256
118 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
119 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
121 #define SIGTAG_SIZE 1000
122 #define SIGTAG_PGP 1002 /* RSA signature */
123 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
124 #define SIGTAG_GPG 1005 /* DSA signature */
126 #define DEP_LESS (1 << 1)
127 #define DEP_GREATER (1 << 2)
128 #define DEP_EQUAL (1 << 3)
129 #define DEP_STRONG (1 << 27)
130 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
132 #define FILEFLAG_GHOST (1 << 6)
136 # define RPM_INDEX_SIZE 4
138 # define RPM_INDEX_SIZE 8
146 typedef struct rpmhead {
150 int forcebinary; /* sigh */
151 unsigned char data[1];
155 static inline unsigned char *
156 headfindtag(RpmHead *h, int tag)
159 unsigned char *d, taga[4];
165 for (i = 0; i < h->cnt; i++, d -= 16)
166 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
172 headexists(RpmHead *h, int tag)
174 return headfindtag(h, tag) ? 1 : 0;
177 static unsigned int *
178 headint32array(RpmHead *h, int tag, int *cnt)
180 unsigned int i, o, *r;
181 unsigned char *d = headfindtag(h, tag);
183 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
185 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
186 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
187 if (o + 4 * i > h->dcnt)
190 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
193 for (o = 0; o < i; o++, d += 4)
194 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
198 /* returns the first entry of an integer array */
200 headint32(RpmHead *h, int tag)
203 unsigned char *d = headfindtag(h, tag);
205 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
207 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
208 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
209 if (i == 0 || o + 4 * i > h->dcnt)
212 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
215 /* returns the first entry of an 64bit integer array */
216 static unsigned long long
217 headint64(RpmHead *h, int tag)
220 unsigned char *d = headfindtag(h, tag);
221 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
223 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
224 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
225 if (i == 0 || o + 8 * i > h->dcnt)
228 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
229 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
232 static unsigned int *
233 headint16array(RpmHead *h, int tag, int *cnt)
235 unsigned int i, o, *r;
236 unsigned char *d = headfindtag(h, tag);
238 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
240 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
241 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
242 if (o + 4 * i > h->dcnt)
245 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
248 for (o = 0; o < i; o++, d += 2)
249 r[o] = d[0] << 8 | d[1];
254 headstring(RpmHead *h, int tag)
257 unsigned char *d = headfindtag(h, tag);
258 /* 6: STRING, 9: I18NSTRING */
259 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
261 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
264 return (char *)h->dp + o;
268 headstringarray(RpmHead *h, int tag, int *cnt)
271 unsigned char *d = headfindtag(h, tag);
274 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
276 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
277 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
278 r = solv_calloc(i ? i : 1, sizeof(char *));
282 for (o = 0; o < i; o++)
286 d += strlen((char *)d) + 1;
287 if (d >= h->dp + h->dcnt)
296 static unsigned char *
297 headbinary(RpmHead *h, int tag, unsigned int *sizep)
300 unsigned char *d = headfindtag(h, tag);
301 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
303 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
304 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
305 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
312 static char *headtoevr(RpmHead *h)
320 version = headstring(h, TAG_VERSION);
321 release = headstring(h, TAG_RELEASE);
322 epoch = headint32(h, TAG_EPOCH);
323 if (!version || !release)
325 fprintf(stderr, "headtoevr: bad rpm header\n");
328 for (v = version; *v >= '0' && *v <= '9'; v++)
330 if (epoch || (v != version && *v == ':'))
332 char epochbuf[11]; /* 32bit decimal will fit in */
333 sprintf(epochbuf, "%u", epoch);
334 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
335 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
339 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
340 sprintf(evr, "%s-%s", version, release);
342 distepoch = headstring(h, TAG_DISTEPOCH);
343 if (distepoch && *distepoch)
346 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
348 strcpy(evr + l, distepoch);
355 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
357 const unsigned char *cp;
360 unsigned char *buf = 0, *bp;
362 /* check if it's already utf8, code taken from screen ;-) */
363 cp = (const unsigned char *)str;
364 while ((c = *cp++) != 0)
368 if ((c & 0xc0) != 0x80)
369 break; /* encoding error */
370 c = (c & 0x3f) | (state << 6);
371 if (!(state & 0x40000000))
373 /* check for overlong sequences */
374 if ((c & 0x820823e0) == 0x80000000)
376 else if ((c & 0x020821f0) == 0x02000000)
378 else if ((c & 0x000820f8) == 0x00080000)
380 else if ((c & 0x0000207c) == 0x00002000)
390 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
392 c = (c & 0x03) | 0xbfffff00; /* 4 */
394 c = (c & 0x07) | 0xbfffc000; /* 3 */
396 c = (c & 0x0f) | 0xbff00000; /* 2 */
398 c = (c & 0x1f) | 0xfc000000; /* 1 */
402 state = (c & 0x80000000) ? c : 0;
406 /* not utf8, assume latin1 */
407 buf = solv_malloc(2 * strlen(str) + 1);
408 cp = (const unsigned char *)str;
411 while ((c = *cp++) != 0)
424 repodata_set_str(repodata, handle, tag, str);
430 #define MAKEDEPS_FILTER_WEAK (1 << 0)
431 #define MAKEDEPS_FILTER_STRONG (1 << 1)
432 #define MAKEDEPS_NO_RPMLIB (1 << 2)
435 * strong: 0: ignore strongness
436 * 1: filter to strong
440 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
444 int i, cc, nc, vc, fc;
446 unsigned int olddeps;
450 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
451 n = headstringarray(rpmhead, tagn, &nc);
454 v = headstringarray(rpmhead, tagv, &vc);
460 f = headint32array(rpmhead, tagf, &fc);
467 if (nc != vc || nc != fc)
469 fprintf(stderr, "bad dependency entries\n");
474 haspre = 0; /* add no prereq marker */
477 /* we do filtering */
479 for (i = 0; i < nc; i++)
481 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
483 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
484 if (!strncmp(n[i], "rpmlib(", 7))
486 if ((f[i] & DEP_PRE) != 0)
491 else if (tagn == TAG_REQUIRENAME)
493 /* no filtering, just look for the first prereq */
494 for (i = 0; i < nc; i++)
495 if ((f[i] & DEP_PRE) != 0)
509 olddeps = repo_reserve_ids(repo, 0, cc);
510 ida = repo->idarraydata + olddeps;
517 haspre = 2; /* pass two: prereqs */
519 *ida++ = SOLVABLE_PREREQMARKER;
521 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
523 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
525 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
527 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
528 if (!strncmp(n[i], "rpmlib(", 7))
530 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
534 if ((f[i] & DEP_LESS) != 0)
536 if ((f[i] & DEP_EQUAL) != 0)
538 if ((f[i] & DEP_GREATER) != 0)
540 name = pool_str2id(pool, n[i], 1);
541 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
542 evr = pool_str2id(pool, v[i] + 2, 1);
544 evr = pool_str2id(pool, v[i], 1);
545 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
548 *ida++ = pool_str2id(pool, n[i], 1);
551 repo->idarraysize += cc + 1;
559 #ifdef USE_FILEFILTER
561 #define FILEFILTER_EXACT 0
562 #define FILEFILTER_STARTS 1
563 #define FILEFILTER_CONTAINS 2
571 static struct filefilter filefilters[] = {
572 { FILEFILTER_CONTAINS, "/bin/", 0},
573 { FILEFILTER_CONTAINS, "/sbin/", 0},
574 { FILEFILTER_CONTAINS, "/lib/", 0},
575 { FILEFILTER_CONTAINS, "/lib64/", 0},
576 { FILEFILTER_CONTAINS, "/etc/", 0},
577 { FILEFILTER_STARTS, "/usr/games/", 0},
578 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
579 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
580 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
586 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
590 unsigned int *fkb, *fn, *fsz, *fm, *fino;
591 unsigned int inotest[256], inotestok;
595 /* XXX: use TAG_LONGFILESIZES if available */
596 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
597 if (!fsz || fc != fszc)
602 /* stupid rpm records sizes of directories, so we have to check the mode */
603 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
604 if (!fm || fc != fszc)
610 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
611 if (!fino || fc != fszc)
619 if (fc < sizeof(inotest))
621 memset(inotest, 0, sizeof(inotest));
622 for (i = 0; i < fc; i++)
625 if (fsz[i] == 0 || !S_ISREG(fm[i]))
627 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
628 bit = 1 << (fino[i] & 31);
629 if ((inotest[off] & bit) != 0)
638 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
640 unsigned int mask, hash, hh;
641 if (!fdev || fc != fszc)
650 while ((mask & (mask - 1)) != 0)
651 mask = mask & (mask - 1);
653 if (mask > sizeof(inotest)/sizeof(*inotest))
654 fx = solv_calloc(mask, sizeof(unsigned int));
658 memset(fx, 0, mask * sizeof(unsigned int));
661 for (i = 0; i < fc; i++)
663 if (fsz[i] == 0 || !S_ISREG(fm[i]))
665 hash = (fino[i] + fdev[i] * 31) & mask;
667 while ((j = fx[hash]) != 0)
669 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
671 fsz[i] = 0; /* kill entry */
674 hash = (hash + hh++) & mask;
684 fn = solv_calloc(dc, sizeof(unsigned int));
685 fkb = solv_calloc(dc, sizeof(unsigned int));
686 for (i = 0; i < fc; i++)
691 if (fsz[i] == 0 || !S_ISREG(fm[i]))
693 fkb[di[i]] += fsz[i] / 1024 + 1;
698 handle = s - pool->solvables;
699 for (i = 0; i < dc; i++)
705 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
706 did = repodata_str2dir(data, "/usr/src", 1);
708 continue; /* work around rpm bug */
711 did = repodata_str2dir(data, dn[i], 1);
712 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
718 /* assumes last processed array is provides! */
720 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
727 #ifdef USE_FILEFILTER
729 struct filefilter *ff;
738 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
741 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
747 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
756 fprintf(stderr, "bad filelist\n");
761 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
763 for (i = 0; i < bnc; i++)
765 #ifdef USE_FILEFILTER
767 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
771 switch (ff->dirmatch)
773 case FILEFILTER_STARTS:
774 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
777 case FILEFILTER_CONTAINS:
778 if (!strstr(dn[di[i]], ff->dir))
781 case FILEFILTER_EXACT:
783 if (strcmp(dn[di[i]], ff->dir))
790 if (strcmp(bn[i], ff->base))
795 if (j == sizeof(filefilters)/sizeof(*filefilters))
799 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
803 fn = solv_realloc(fn, fna);
805 strcpy(fn, dn[di[i]]);
807 olddeps = repo_addid_dep(repo, olddeps, pool_str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
814 handle = s - pool->solvables;
815 did = repodata_str2dir(data, dn[di[i]], 1);
818 did = repodata_str2dir(data, "/", 1);
819 if (b && b[0] == '/')
820 b++; /* work around rpm bug */
822 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
836 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
838 const char *p, *sevr, *sarch;
840 p = strrchr(sourcerpm, '.');
841 if (!p || strcmp(p, ".rpm") != 0)
844 while (p > sourcerpm && *p != '.')
846 if (*p != '.' || p == sourcerpm)
849 while (p > sourcerpm && *p != '-')
851 if (*p != '-' || p == sourcerpm)
854 while (p > sourcerpm && *p != '-')
856 if (*p != '-' || p == sourcerpm)
859 if (!strcmp(sarch, "src.rpm"))
860 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
861 else if (!strcmp(sarch, "nosrc.rpm"))
862 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
864 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
865 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
866 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
868 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
869 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
870 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
872 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
876 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
882 name = headstring(rpmhead, TAG_NAME);
883 if (!strcmp(name, "gpg-pubkey"))
885 s->name = pool_str2id(pool, name, 1);
888 fprintf(stderr, "package has no name\n");
891 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
892 if (sourcerpm || rpmhead->forcebinary)
893 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
896 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
897 s->arch = ARCH_NOSRC;
902 s->arch = ARCH_NOARCH;
903 evr = headtoevr(rpmhead);
904 s->evr = pool_str2id(pool, evr, 1);
905 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
907 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
908 if ((flags & RPM_ADD_NO_FILELIST) == 0)
909 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
910 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
911 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
912 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
913 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
914 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
916 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
917 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
918 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
919 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
920 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
921 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
928 unsigned long long u64;
930 handle = s - pool->solvables;
931 str = headstring(rpmhead, TAG_SUMMARY);
933 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
934 str = headstring(rpmhead, TAG_DESCRIPTION);
938 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
939 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
943 /* oh my, found SUSE special author section */
945 str = solv_strdup(str);
948 while (l > 0 && str[l - 1] == '\n')
951 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
953 aut = str; /* copy over */
954 while (*p == ' ' || *p == '\n')
967 while (aut != str && aut[-1] == '\n')
971 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
975 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
977 str = headstring(rpmhead, TAG_GROUP);
979 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
980 str = headstring(rpmhead, TAG_LICENSE);
982 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
983 str = headstring(rpmhead, TAG_URL);
985 repodata_set_str(data, handle, SOLVABLE_URL, str);
986 str = headstring(rpmhead, TAG_DISTRIBUTION);
988 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
989 str = headstring(rpmhead, TAG_PACKAGER);
991 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
992 if ((flags & RPM_ADD_WITH_PKGID) != 0)
994 unsigned char *chksum;
995 unsigned int chksumsize;
996 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
997 if (chksum && chksumsize == 16)
998 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
1000 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1002 str = headstring(rpmhead, TAG_SHA1HEADER);
1003 if (str && strlen(str) == 40)
1004 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
1005 else if (str && strlen(str) == 64)
1006 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
1008 u32 = headint32(rpmhead, TAG_BUILDTIME);
1010 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1011 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1013 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1014 u64 = headint64(rpmhead, TAG_LONGSIZE);
1016 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1019 u32 = headint32(rpmhead, TAG_SIZE);
1021 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1024 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
1025 if ((flags & RPM_ADD_TRIGGERS) != 0)
1028 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1031 for (; (id = repo->idarraydata[ida]) != 0; ida++)
1033 /* we currently do not support rel ids in incore data, so
1034 * strip off versioning information */
1035 while (ISRELDEP(id))
1037 Reldep *rd = GETRELDEP(pool, id);
1042 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1052 copyreldep(Pool *pool, Pool *frompool, Id id)
1054 Reldep *rd = GETRELDEP(frompool, id);
1055 Id name = rd->name, evr = rd->evr;
1057 name = copyreldep(pool, frompool, name);
1059 name = pool_str2id(pool, pool_id2str(frompool, name), 1);
1061 evr = copyreldep(pool, frompool, evr);
1063 evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1064 return pool_rel2id(pool, name, evr, rd->flags, 1);
1068 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1073 Pool *frompool = fromrepo->pool;
1077 from = fromrepo->idarraydata + fromoff;
1078 for (ida = from, cc = 0; *ida; ida++, cc++)
1082 ido = repo_reserve_ids(repo, 0, cc);
1083 ida = repo->idarraydata + ido;
1084 if (frompool && pool != frompool)
1090 id = copyreldep(pool, frompool, id);
1092 id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1098 memcpy(ida, from, (cc + 1) * sizeof(Id));
1099 repo->idarraysize += cc + 1;
1103 #define COPYDIR_DIRCACHE_SIZE 512
1105 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1108 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1110 if (cache && cache[did & 255] == did)
1111 return cache[(did & 255) + 256];
1112 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1116 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1118 Id parent = dirpool_parent(&fromdata->dirpool, did);
1119 Id compid = dirpool_compid(&fromdata->dirpool, did);
1121 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1122 if (fromspool != &pool->ss)
1123 compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1124 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1127 cache[did & 255] = did;
1128 cache[(did & 255) + 256] = compid;
1133 struct solvable_copy_cbdata {
1140 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1142 struct solvable_copy_cbdata *cbdata = vcbdata;
1144 Repodata *data = cbdata->data;
1145 Id handle = cbdata->handle;
1146 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1147 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1149 keyname = key->name;
1150 if (keyname >= ID_NUM_INTERNAL && pool != frompool)
1151 keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1154 case REPOKEY_TYPE_ID:
1155 case REPOKEY_TYPE_CONSTANTID:
1156 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1158 assert(!data->localpool); /* implement me! */
1159 if (pool != frompool || fromdata->localpool)
1162 id = copyreldep(pool, frompool, id);
1164 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1166 if (key->type == REPOKEY_TYPE_ID)
1167 repodata_set_id(data, handle, keyname, id);
1168 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1169 repodata_set_constantid(data, handle, keyname, id);
1171 repodata_add_idarray(data, handle, keyname, id);
1173 case REPOKEY_TYPE_STR:
1174 repodata_set_str(data, handle, keyname, kv->str);
1176 case REPOKEY_TYPE_VOID:
1177 repodata_set_void(data, handle, keyname);
1179 case REPOKEY_TYPE_NUM:
1180 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1182 case REPOKEY_TYPE_CONSTANT:
1183 repodata_set_constant(data, handle, keyname, kv->num);
1185 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1187 assert(!data->localpool); /* implement me! */
1188 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1189 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1191 case REPOKEY_TYPE_DIRSTRARRAY:
1193 assert(!data->localpool); /* implement me! */
1194 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1195 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1204 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1206 Repo *repo = s->repo;
1207 Repo *fromrepo = r->repo;
1208 Pool *pool = repo->pool;
1209 struct solvable_copy_cbdata cbdata;
1211 /* copy solvable data */
1212 if (pool == fromrepo->pool)
1217 s->vendor = r->vendor;
1222 s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1224 s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1226 s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1228 s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1230 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1231 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1232 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1233 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1234 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1235 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1236 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1237 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1239 /* copy all attributes */
1243 cbdata.handle = s - pool->solvables;
1244 cbdata.dircache = dircache;
1245 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1248 /* used to sort entries returned in some database order */
1250 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1252 struct rpmid const *a = va, *b = vb;
1254 r = strcmp(a->name, b->name);
1257 return a->dbid - b->dbid;
1261 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1264 Pool *pool = repo->pool;
1265 Solvable *a = pool->solvables + *(Id *)va;
1266 Solvable *b = pool->solvables + *(Id *)vb;
1269 if (a->name != b->name)
1270 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1271 rpmdbid = repo->rpmdbid;
1272 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1276 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1278 Pool *pool = repo->pool;
1281 tmp = pool->solvables[pa];
1282 pool->solvables[pa] = pool->solvables[pb];
1283 pool->solvables[pb] = tmp;
1286 Id tmpid = repo->rpmdbid[pa - repo->start];
1287 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1288 repo->rpmdbid[pb - repo->start] = tmpid;
1290 /* only works if nothing is already internalized! */
1292 repodata_swap_attrs(data, pa, pb);
1296 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1299 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1301 # if defined(WORDS_BIGENDIAN)
1306 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1308 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1312 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1315 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1317 # if defined(WORDS_BIGENDIAN)
1322 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1324 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1329 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1331 memset(cookie, 0, 32);
1332 cookie[3] = RPMDB_COOKIE_VERSION;
1333 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1334 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1337 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1339 opendbenv(const char *rootdir)
1341 char dbpath[PATH_MAX];
1345 if (db_env_create(&dbenv, 0))
1347 perror("db_env_create");
1350 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1351 dbenv->set_thread_count(dbenv, 8);
1353 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1354 if (access(dbpath, W_OK) == -1)
1356 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1361 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1363 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1368 perror("dbenv open");
1369 dbenv->close(dbenv, 0);
1377 count_headers(const char *rootdir, DB_ENV *dbenv)
1379 char dbpath[PATH_MAX];
1380 struct stat statbuf;
1387 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1388 if (stat(dbpath, &statbuf))
1390 memset(&dbkey, 0, sizeof(dbkey));
1391 memset(&dbdata, 0, sizeof(dbdata));
1392 if (db_create(&db, dbenv, 0))
1394 perror("db_create");
1397 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1399 perror("db->open Name index");
1402 if (db->cursor(db, NULL, &dbc, 0))
1404 perror("db->cursor");
1407 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1408 count += dbdata.size / RPM_INDEX_SIZE;
1415 * read rpm db as repo
1420 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1422 Pool *pool = repo->pool;
1423 unsigned char buf[16];
1430 struct rpmid *rpmids, *rp;
1436 unsigned int refmask, h;
1437 char dbpath[PATH_MAX];
1441 struct stat packagesstat;
1442 unsigned char newcookie[32];
1443 const unsigned char *oldcookie = 0;
1444 Id oldcookietype = 0;
1446 int count = 0, done = 0;
1449 now = solv_timems(0);
1450 memset(&dbkey, 0, sizeof(dbkey));
1451 memset(&dbdata, 0, sizeof(dbdata));
1456 data = repo_add_repodata(repo, flags);
1458 if (ref && !(ref->nsolvables && ref->rpmdbid))
1461 if (!(dbenv = opendbenv(rootdir)))
1463 return pool_error(pool, -1, "repo_add_rpmdb: opendbenv failed");
1466 /* XXX: should get ro lock of Packages database! */
1467 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1468 if (stat(dbpath, &packagesstat))
1470 return pool_error(pool, -1, "repo_add_rpmdb: %s: %s", dbpath, strerror(errno));
1472 mkrpmdbcookie(&packagesstat, newcookie);
1473 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1476 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1477 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1480 int solvstart = 0, solvend = 0;
1482 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1483 count = count_headers(rootdir, dbenv);
1484 if (db_create(&db, dbenv, 0))
1486 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1487 dbenv->close(dbenv, 0);
1490 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1492 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1494 dbenv->close(dbenv, 0);
1497 if (db->get_byteswapped(db, &byteswapped))
1500 dbenv->close(dbenv, 0);
1501 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1503 if (db->cursor(db, NULL, &dbc, 0))
1506 dbenv->close(dbenv, 0);
1507 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1513 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1517 s = pool_id2solvable(pool, repo_add_solvable(repo));
1519 solvstart = s - pool->solvables;
1520 solvend = s - pool->solvables + 1;
1523 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1524 if (dbkey.size != 4)
1528 dbenv->close(dbenv, 0);
1529 return pool_error(pool, -1, "corrupt Packages database (key size)");
1531 dbid = db2rpmdbid(dbkey.data, byteswapped);
1532 if (dbid == 0) /* the join key */
1534 if (dbdata.size < 8)
1538 dbenv->close(dbenv, 0);
1539 return pool_error(pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1541 if (dbdata.size > rpmheadsize)
1543 rpmheadsize = dbdata.size + 128;
1544 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1546 memcpy(buf, dbdata.data, 8);
1547 rpmhead->forcebinary = 1;
1548 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1549 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1550 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1554 dbenv->close(dbenv, 0);
1555 return pool_error(pool, -1, "corrupt rpm database (data size)\n");
1557 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1558 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1559 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1560 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1567 /* We can reuse this solvable, but make sure it's still
1568 associated with this repo. */
1569 memset(s, 0, sizeof(*s));
1572 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1576 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1577 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1582 /* oops, could not reuse. free it instead */
1583 repo_free_solvable(repo, s - pool->solvables, 1);
1590 /* now sort all solvables in the new solvstart..solvend block */
1591 if (solvend - solvstart > 1)
1593 pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1594 for (i = solvstart; i < solvend; i++)
1595 pkgids[i - solvstart] = i;
1596 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1598 for (i = solvstart; i < solvend; i++)
1600 int j = pkgids[i - solvstart];
1602 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1604 swap_solvables(repo, data, i, j);
1611 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1613 memset(dircache, 0, sizeof(dircache));
1614 if (db_create(&db, dbenv, 0))
1616 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1617 dbenv->close(dbenv, 0);
1620 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1622 pool_error(pool, -1, "repo_add_rpmdb: db->open Name index failed: %s", strerror(errno));
1624 dbenv->close(dbenv, 0);
1627 if (db->get_byteswapped(db, &byteswapped))
1630 dbenv->close(dbenv, 0);
1631 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1633 if (db->cursor(db, NULL, &dbc, 0))
1636 dbenv->close(dbenv, 0);
1637 return pool_error(pool, -1, "repo_add_rpmdb: db->cursor failed");
1641 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1643 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1647 while(dl >= RPM_INDEX_SIZE)
1649 rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1650 rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1651 rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1652 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1653 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1655 dp += RPM_INDEX_SIZE;
1656 dl -= RPM_INDEX_SIZE;
1664 solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1669 /* create hash from dbid to ref */
1670 refmask = mkmask(ref->nsolvables);
1671 refhash = solv_calloc(refmask + 1, sizeof(Id));
1672 for (i = 0; i < ref->end - ref->start; i++)
1674 if (!ref->rpmdbid[i])
1676 h = ref->rpmdbid[i] & refmask;
1678 h = (h + 317) & refmask;
1679 refhash[h] = i + 1; /* make it non-zero */
1682 /* count the misses, they will cost us time */
1683 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1685 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1691 while ((id = refhash[h]))
1693 if (ref->rpmdbid[id - 1] == dbid)
1695 h = (h + 317) & refmask;
1704 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1706 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1708 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1711 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1715 while ((id = refhash[h]))
1717 if (ref->rpmdbid[id - 1] == dbid)
1719 h = (h + 317) & refmask;
1723 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1726 solvable_copy(s, r, data, dircache);
1733 if (db_create(&db, dbenv, 0))
1735 pool_error(pool, -1, "repo_add_rpmdb: db_create: %s", strerror(errno));
1736 dbenv->close(dbenv, 0);
1739 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1741 pool_error(pool, -1, "repo_add_rpmdb: db->open Packages index failed: %s", strerror(errno));
1743 dbenv->close(dbenv, 0);
1746 if (db->get_byteswapped(db, &byteswapped))
1749 dbenv->close(dbenv, 0);
1750 return pool_error(pool, -1, "repo_add_rpmdb: db->get_byteswapped failed");
1753 rpmdbid2db(buf, rp->dbid, byteswapped);
1758 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1761 dbenv->close(dbenv, 0);
1762 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1764 if (dbdata.size < 8)
1767 dbenv->close(dbenv, 0);
1768 return pool_error(pool, -1, "corrupt Packages database (size)");
1770 if (dbdata.size > rpmheadsize)
1772 rpmheadsize = dbdata.size + 128;
1773 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1775 memcpy(buf, dbdata.data, 8);
1776 rpmhead->forcebinary = 1;
1777 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1778 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1779 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1782 dbenv->close(dbenv, 0);
1783 return pool_error(pool, -1, "corrupt Packages database (data size)");
1785 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1786 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1788 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1789 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1793 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1794 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1802 for (i = 0; i < nrpmids; i++)
1803 solv_free(rpmids[i].name);
1809 dbenv->close(dbenv, 0);
1812 if (!(flags & REPO_NO_INTERNALIZE))
1813 repodata_internalize(data);
1814 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1815 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1816 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1817 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1818 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)));
1823 static inline unsigned int
1824 getu32(const unsigned char *dp)
1826 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1831 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1833 int sigdsize, sigcnt, l;
1834 Pool *pool = repo->pool;
1836 RpmHead *rpmhead = 0;
1837 int rpmheadsize = 0;
1838 char *payloadformat;
1840 unsigned char lead[4096];
1841 int headerstart, headerend;
1844 unsigned char pkgid[16];
1845 unsigned char leadsigid[16];
1846 unsigned char hdrid[32];
1847 int pkgidtype, leadsigidtype, hdridtype;
1850 void *leadsigchksumh = 0;
1851 int forcebinary = 0;
1853 data = repo_add_repodata(repo, flags);
1855 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1856 chksumtype = REPOKEY_TYPE_SHA256;
1857 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1858 chksumtype = REPOKEY_TYPE_SHA1;
1860 if ((fp = fopen(rpm, "r")) == 0)
1862 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1865 if (fstat(fileno(fp), &stb))
1867 pool_error(pool, -1, "fstat: %s", strerror(errno));
1872 chksumh = solv_chksum_create(chksumtype);
1873 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1874 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1875 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1877 pool_error(pool, -1, "%s: not a rpm", rpm);
1881 forcebinary = lead[6] != 0 || lead[7] != 1;
1883 solv_chksum_add(chksumh, lead, 96 + 16);
1885 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1886 if (lead[78] != 0 || lead[79] != 5)
1888 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1892 if (getu32(lead + 96) != 0x8eade801)
1894 pool_error(pool, -1, "%s: bad signature header", rpm);
1898 sigcnt = getu32(lead + 96 + 8);
1899 sigdsize = getu32(lead + 96 + 12);
1900 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1902 pool_error(pool, -1, "%s: bad signature header", rpm);
1906 sigdsize += sigcnt * 16;
1907 sigdsize = (sigdsize + 7) & ~7;
1908 headerstart = 96 + 16 + sigdsize;
1909 pkgidtype = leadsigidtype = hdridtype = 0;
1910 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1912 /* extract pkgid or hdrid from the signature header */
1913 if (sigdsize > rpmheadsize)
1915 rpmheadsize = sigdsize + 128;
1916 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1918 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1920 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1925 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1927 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1928 rpmhead->forcebinary = 0;
1929 rpmhead->cnt = sigcnt;
1930 rpmhead->dcnt = sigdsize - sigcnt * 16;
1931 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1932 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1934 unsigned char *chksum;
1935 unsigned int chksumsize;
1936 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1937 if (chksum && chksumsize == 16)
1939 pkgidtype = REPOKEY_TYPE_MD5;
1940 memcpy(pkgid, chksum, 16);
1943 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1945 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1946 if (str && strlen(str) == 40)
1948 if (solv_hex2bin(&str, hdrid, 20) == 20)
1949 hdridtype = REPOKEY_TYPE_SHA1;
1951 else if (str && strlen(str) == 64)
1953 if (solv_hex2bin(&str, hdrid, 32) == 32)
1954 hdridtype = REPOKEY_TYPE_SHA256;
1960 /* just skip the signature header */
1963 l = sigdsize > 4096 ? 4096 : sigdsize;
1964 if (fread(lead, l, 1, fp) != 1)
1966 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1971 solv_chksum_add(chksumh, lead, l);
1973 solv_chksum_add(leadsigchksumh, lead, l);
1979 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
1980 leadsigidtype = REPOKEY_TYPE_MD5;
1982 if (fread(lead, 16, 1, fp) != 1)
1984 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1989 solv_chksum_add(chksumh, lead, 16);
1990 if (getu32(lead) != 0x8eade801)
1992 pool_error(pool, -1, "%s: bad header", rpm);
1996 sigcnt = getu32(lead + 8);
1997 sigdsize = getu32(lead + 12);
1998 if (sigcnt >= 0x100000 || sigdsize >= 0x800000)
2000 pool_error(pool, -1, "%s: bad header", rpm);
2004 l = sigdsize + sigcnt * 16;
2005 headerend = headerstart + 16 + l;
2006 if (l > rpmheadsize)
2008 rpmheadsize = l + 128;
2009 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2011 if (fread(rpmhead->data, l, 1, fp) != 1)
2013 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2018 solv_chksum_add(chksumh, rpmhead->data, l);
2019 rpmhead->forcebinary = forcebinary;
2020 rpmhead->cnt = sigcnt;
2021 rpmhead->dcnt = sigdsize;
2022 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2023 if (headexists(rpmhead, TAG_PATCHESNAME))
2025 /* this is a patch rpm, ignore */
2026 pool_error(pool, -1, "%s: is patch rpm", rpm);
2028 solv_chksum_free(chksumh, 0);
2032 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2033 if (payloadformat && !strcmp(payloadformat, "drpm"))
2035 /* this is a delta rpm */
2036 pool_error(pool, -1, "%s: is delta rpm", rpm);
2038 solv_chksum_free(chksumh, 0);
2043 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2044 solv_chksum_add(chksumh, lead, l);
2046 s = pool_id2solvable(pool, repo_add_solvable(repo));
2047 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2049 repo_free_solvable(repo, s - pool->solvables, 1);
2050 solv_chksum_free(chksumh, 0);
2054 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2055 if (S_ISREG(stb.st_mode))
2056 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2057 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2059 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2061 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2063 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2066 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2067 chksumh = solv_chksum_free(chksumh, 0);
2071 if (!(flags & REPO_NO_INTERNALIZE))
2072 repodata_internalize(data);
2073 return s - pool->solvables;
2077 linkhash(const char *lt, char *hash)
2080 const unsigned char *str = (const unsigned char *)lt;
2084 while ((c = *str++) != 0)
2086 sprintf(hash, "%08x", r);
2087 sprintf(hash + 8, "%08x", l);
2088 sprintf(hash + 16, "%08x", 0);
2089 sprintf(hash + 24, "%08x", 0);
2093 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
2095 RpmHead *rpmhead = rpmhandle;
2100 unsigned int *di, diidx;
2101 unsigned int *co = 0;
2102 unsigned int *ff = 0;
2103 unsigned int lastdir;
2106 int cnt, dcnt, cnt2;
2110 char md5[33], *md5p = 0;
2112 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2115 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2117 for (i = 0; i < dcnt; i++)
2118 (*cb)(cbdata, dn[i], 0, (char *)0);
2122 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2128 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2129 if (!di || cnt != cnt2)
2136 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2137 if (!fm || cnt != cnt2)
2145 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2147 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2148 if (!md || cnt != cnt2)
2158 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2160 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2161 if (!co || cnt != cnt2)
2172 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2174 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2175 if (!ff || cnt != cnt2)
2189 for (i = 0; i < cnt; i++)
2191 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2196 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2199 l = l1 + strlen(bn[i]) + 1;
2203 space = solv_realloc(space, spacen);
2205 if (lastdir != diidx)
2207 strcpy(space, dn[diidx]);
2211 strcpy(space + l1, bn[i]);
2220 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2226 linkhash(lt[i], md5);
2232 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2236 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2250 rpm_query(void *rpmhandle, Id what)
2252 const char *name, *arch, *sourcerpm;
2256 RpmHead *rpmhead = rpmhandle;
2261 name = headstring(rpmhead, TAG_NAME);
2264 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2265 if (sourcerpm || rpmhead->forcebinary)
2266 arch = headstring(rpmhead, TAG_ARCH);
2269 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2276 evr = headtoevr(rpmhead);
2279 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2281 sprintf(r, "%s-%s.%s", name, evr, arch);
2285 name = headstring(rpmhead, TAG_NAME);
2286 r = solv_strdup(name);
2289 r = headtoevr(rpmhead);
2296 struct rpm_by_state {
2311 #define ENTRIES_BLOCK 255
2312 #define NAMEDATA_BLOCK 1023
2314 static struct rpmdbentry *
2315 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2328 struct rpmdbentry *entries = 0;
2334 dbenv = state->dbenv;
2335 if (db_create(&db, dbenv, 0))
2337 perror("db_create");
2340 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2342 perror("db->open index");
2346 if (db->get_byteswapped(db, &byteswapped))
2348 perror("db->get_byteswapped");
2352 if (db->cursor(db, NULL, &dbc, 0))
2354 perror("db->cursor");
2358 memset(&dbkey, 0, sizeof(dbkey));
2359 memset(&dbdata, 0, sizeof(dbdata));
2362 dbkey.data = (void *)match;
2363 dbkey.size = strlen(match);
2365 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2367 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2371 while(dl >= RPM_INDEX_SIZE)
2373 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2374 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2375 entries[nentries].nameoff = namedatal;
2377 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2378 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2379 namedata[namedatal + dbkey.size] = 0;
2380 namedatal += dbkey.size + 1;
2381 dp += RPM_INDEX_SIZE;
2382 dl -= RPM_INDEX_SIZE;
2389 *nentriesp = nentries;
2390 *namedatap = namedata;
2395 freestate(struct rpm_by_state *state)
2401 state->db->close(state->db, 0);
2403 state->dbenv->close(state->dbenv, 0);
2404 solv_free(state->rpmhead);
2408 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2410 struct rpm_by_state state;
2411 struct rpmdbentry *entries;
2418 queue_empty(rpmdbidq);
2419 memset(&state, 0, sizeof(state));
2420 if (!(state.dbenv = opendbenv(rootdir)))
2422 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2424 for (i = 0; i < nentries; i++)
2425 queue_push(rpmdbidq, entries[i].rpmdbid);
2427 solv_free(namedata);
2433 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2435 struct rpm_by_state *state = *statep;
2436 unsigned char buf[16];
2446 *statep = (void *)0;
2452 state = solv_calloc(1, sizeof(*state));
2455 if (!state->dbopened)
2457 state->dbopened = 1;
2458 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2460 if (db_create(&state->db, state->dbenv, 0))
2462 perror("db_create");
2464 state->dbenv->close(state->dbenv, 0);
2468 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2470 perror("db->open var/lib/rpm/Packages");
2471 state->db->close(state->db, 0);
2473 state->dbenv->close(state->dbenv, 0);
2477 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2479 perror("db->get_byteswapped");
2480 state->db->close(state->db, 0);
2482 state->dbenv->close(state->dbenv, 0);
2487 rpmdbid2db(buf, rpmdbid, state->byteswapped);
2488 memset(&dbkey, 0, sizeof(dbkey));
2489 memset(&dbdata, 0, sizeof(dbdata));
2494 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2499 if (dbdata.size < 8)
2501 fprintf(stderr, "corrupt rpm database (size)\n");
2504 if (dbdata.size > state->rpmheadsize)
2506 state->rpmheadsize = dbdata.size + 128;
2507 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2509 rpmhead = state->rpmhead;
2510 memcpy(buf, dbdata.data, 8);
2511 rpmhead->forcebinary = 1;
2512 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2513 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2514 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2516 fprintf(stderr, "corrupt rpm database (data size)\n");
2519 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2520 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2525 rpm_byfp(FILE *fp, const char *name, void **statep)
2527 struct rpm_by_state *state = *statep;
2528 /* int headerstart, headerend; */
2530 int sigdsize, sigcnt, l;
2531 unsigned char lead[4096];
2532 int forcebinary = 0;
2535 return rpm_byrpmdbid(0, 0, statep);
2538 state = solv_calloc(1, sizeof(*state));
2541 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2543 fprintf(stderr, "%s: not a rpm\n", name);
2546 forcebinary = lead[6] != 0 || lead[7] != 1;
2547 if (lead[78] != 0 || lead[79] != 5)
2549 fprintf(stderr, "%s: not a V5 header\n", name);
2552 if (getu32(lead + 96) != 0x8eade801)
2554 fprintf(stderr, "%s: bad signature header\n", name);
2557 sigcnt = getu32(lead + 96 + 8);
2558 sigdsize = getu32(lead + 96 + 12);
2559 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2561 fprintf(stderr, "%s: bad signature header\n", name);
2564 sigdsize += sigcnt * 16;
2565 sigdsize = (sigdsize + 7) & ~7;
2566 /* headerstart = 96 + 16 + sigdsize; */
2569 l = sigdsize > 4096 ? 4096 : sigdsize;
2570 if (fread(lead, l, 1, fp) != 1)
2572 fprintf(stderr, "%s: unexpected EOF\n", name);
2577 if (fread(lead, 16, 1, fp) != 1)
2579 fprintf(stderr, "%s: unexpected EOF\n", name);
2582 if (getu32(lead) != 0x8eade801)
2584 fprintf(stderr, "%s: bad header\n", name);
2588 sigcnt = getu32(lead + 8);
2589 sigdsize = getu32(lead + 12);
2590 if (sigcnt >= 0x100000 || sigdsize >= 0x800000)
2592 fprintf(stderr, "%s: bad header\n", name);
2596 l = sigdsize + sigcnt * 16;
2597 /* headerend = headerstart + 16 + l; */
2598 if (l > state->rpmheadsize)
2600 state->rpmheadsize = l + 128;
2601 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2603 rpmhead = state->rpmhead;
2604 if (fread(rpmhead->data, l, 1, fp) != 1)
2606 fprintf(stderr, "%s: unexpected EOF\n", name);
2610 rpmhead->forcebinary = forcebinary;
2611 rpmhead->cnt = sigcnt;
2612 rpmhead->dcnt = sigdsize;
2613 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2618 rpm_byrpmh(Header h, void **statep)
2620 struct rpm_by_state *state = *statep;
2621 const unsigned char *uh;
2622 int sigdsize, sigcnt, l;
2626 uh = headerUnload(h);
2628 uh = headerUnload(h, NULL);
2632 sigcnt = getu32(uh);
2633 sigdsize = getu32(uh + 4);
2634 l = sigdsize + sigcnt * 16;
2637 state = solv_calloc(1, sizeof(*state));
2640 if (l > state->rpmheadsize)
2642 state->rpmheadsize = l + 128;
2643 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2645 rpmhead = state->rpmhead;
2646 memcpy(rpmhead->data, uh + 8, l - 8);
2648 rpmhead->forcebinary = 0;
2649 rpmhead->cnt = sigcnt;
2650 rpmhead->dcnt = sigdsize;
2651 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2656 #ifdef ENABLE_RPMDB_PUBKEY
2659 r64dec1(char *p, unsigned int *vp, int *eofp)
2664 for (i = 0; i < 4; )
2669 if (x >= 'A' && x <= 'Z')
2671 else if (x >= 'a' && x <= 'z')
2673 else if (x >= '0' && x <= '9')
2700 crc24(unsigned char *p, int len)
2702 unsigned int crc = 0xb704ceL;
2707 crc ^= (*p++) << 16;
2708 for (i = 0; i < 8; i++)
2709 if ((crc <<= 1) & 0x1000000)
2712 return crc & 0xffffffL;
2715 static unsigned char *
2716 unarmor(char *pubkey, int *pktlp)
2720 unsigned char *buf, *bp;
2724 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2726 pubkey = strchr(pubkey, '\n');
2731 pubkey = strchr(pubkey, '\n');
2734 /* skip header lines */
2737 while (*pubkey == ' ' || *pubkey == '\t')
2739 if (*pubkey == '\n')
2741 pubkey = strchr(pubkey, '\n');
2746 p = strchr(pubkey, '=');
2750 bp = buf = solv_malloc(l * 3 / 4 + 4);
2754 pubkey = r64dec1(pubkey, &v, &eof);
2764 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2767 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2772 if (v != crc24(buf, bp - buf))
2777 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2779 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2789 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2792 unsigned char keyid[8];
2793 unsigned int kcr = 0, maxex = 0;
2794 unsigned char *pubkey = 0;
2795 unsigned char *userid = 0;
2801 for (; pl; p += l, pl -= l)
2805 if (!(x & 128) || pl <= 0)
2810 tag = (x & 0x3c) >> 2;
2832 else if (x >= 192 && x < 224)
2836 l = ((x - 192) << 8) + *p++ + 192;
2843 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2854 pubkey = solv_realloc(pubkey, l);
2856 memcpy(pubkey, p, l);
2865 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2869 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2873 memset(keyid, 0, 8);
2874 if (p[7] == 1) /* RSA */
2877 unsigned char fp[16];
2881 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2882 memcpy(keyid, p + 10 + ql - 8, 8);
2883 h = solv_chksum_create(REPOKEY_TYPE_MD5);
2884 solv_chksum_add(h, p + 10, ql);
2886 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2887 solv_chksum_add(h, q + 2, ql);
2888 solv_chksum_free(h, fp);
2889 for (i = 0; i < 16; i++)
2890 sprintf(fpx + i * 2, "%02x", fp[i]);
2891 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2898 unsigned char hdr[3];
2899 unsigned char fp[20];
2902 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2906 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2907 solv_chksum_add(h, hdr, 3);
2908 solv_chksum_add(h, p, l);
2909 solv_chksum_free(h, fp);
2910 for (i = 0; i < 20; i++)
2911 sprintf(fpx + i * 2, "%02x", fp[i]);
2912 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2913 memcpy(keyid, fp + 12, 8);
2918 if (p[0] == 3 && p[1] == 5)
2923 /* printf("V3 signature packet\n"); */
2926 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2928 if (!memcmp(keyid, p + 6, 8))
2930 /* printf("SELF SIG\n"); */
2934 /* printf("OTHER SIG\n"); */
2938 htype = REPOKEY_TYPE_MD5;
2939 else if (p[16] == 2)
2940 htype = REPOKEY_TYPE_SHA1;
2941 else if (p[16] == 8)
2942 htype = REPOKEY_TYPE_SHA256;
2945 void *h = solv_chksum_create(htype);
2946 unsigned char b[3], *cs;
2949 b[1] = pubkeyl >> 8;
2951 solv_chksum_add(h, b, 3);
2952 solv_chksum_add(h, pubkey, pubkeyl);
2953 if (p[2] >= 0x10 && p[2] <= 0x13)
2954 solv_chksum_add(h, userid, useridl);
2955 solv_chksum_add(h, p + 2, 5);
2956 cs = solv_chksum_get(h, 0);
2957 solv_chksum_free(h, 0);
2963 int j, ql, haveissuer;
2965 unsigned int ex = 0;
2967 unsigned int scr = 0;
2969 unsigned char issuer[8];
2971 /* printf("V4 signature packet\n"); */
2974 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2979 for (j = 0; q && j < 2; j++)
2986 ql = q[0] << 8 | q[1];
3007 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
3018 sl = ((x - 192) << 8) + *q++ + 192;
3027 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
3028 if (x == 16 && sl == 9 && !haveissuer)
3030 memcpy(issuer, q + 1, 8);
3034 if (x == 2 && j == 0)
3035 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3037 if (x == 9 && j == 0)
3038 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
3050 htype = REPOKEY_TYPE_MD5;
3052 htype = REPOKEY_TYPE_SHA1;
3054 htype = REPOKEY_TYPE_SHA256;
3055 if (htype && pubkeyl)
3057 void *h = solv_chksum_create(htype);
3058 unsigned char b[6], *cs;
3062 b[1] = pubkeyl >> 8;
3064 solv_chksum_add(h, b, 3);
3065 solv_chksum_add(h, pubkey, pubkeyl);
3066 if (p[1] >= 0x10 && p[1] <= 0x13)
3069 b[1] = useridl >> 24;
3070 b[2] = useridl >> 16;
3071 b[3] = useridl >> 8;
3073 solv_chksum_add(h, b, 5);
3074 solv_chksum_add(h, userid, useridl);
3076 hl = 6 + (p[4] << 8 | p[5]);
3077 solv_chksum_add(h, p, hl);
3084 solv_chksum_add(h, b, 6);
3085 cs = solv_chksum_get(h, 0);
3086 solv_chksum_free(h, 0);
3089 if (!memcmp(keyid, issuer, 8))
3091 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
3097 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
3104 userid = solv_realloc(userid, l);
3106 memcpy(userid, p, l);
3113 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
3118 /* this is private to rpm, but rpm lacks an interface to retrieve
3119 * the values. Sigh. */
3120 struct pgpDigParams_s {
3121 const char * userid;
3122 const unsigned char * hash;
3123 const char * params[4];
3125 unsigned char version; /*!< version number. */
3126 unsigned char time[4]; /*!< time that the key was created. */
3127 unsigned char pubkey_algo; /*!< public key algorithm. */
3128 unsigned char hash_algo;
3129 unsigned char sigtype;
3130 unsigned char hashlen;
3131 unsigned char signhash16[2];
3132 unsigned char signid[8];
3133 unsigned char saved;
3137 struct pgpDigParams_s signature;
3138 struct pgpDigParams_s pubkey;
3142 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3144 Pool *pool = s->repo->pool;
3145 unsigned char *pkts;
3150 char evrbuf[8 + 1 + 8 + 1];
3152 pkts = unarmor(pubkey, &pktsl);
3155 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3156 parsekeydata(s, data, pkts, pktsl);
3157 /* only rpm knows how to do the release calculation, we don't dare
3158 * to recreate all the bugs */
3162 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3164 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3165 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3166 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]);
3167 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3169 s->name = pool_str2id(pool, "gpg-pubkey", 1);
3170 s->evr = pool_str2id(pool, evrbuf, 1);
3172 for (i = 0; i < 8; i++)
3173 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3174 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3175 if (dig->pubkey.userid)
3176 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3178 (void)pgpFreeDig(dig);
3180 (void)pgpDigFree(dig);
3182 solv_free((void *)pkts);
3187 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
3189 Pool *pool = repo->pool;
3190 struct rpm_by_state state;
3191 struct rpmdbentry *entries;
3193 char *namedata, *str;
3198 data = repo_add_repodata(repo, flags);
3200 memset(&state, 0, sizeof(state));
3201 if (!(state.dbenv = opendbenv(rootdir)))
3203 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3204 for (i = 0 ; i < nentries; i++)
3206 void *statep = &state;
3207 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3210 str = headstring(rpmhead, TAG_DESCRIPTION);
3213 s = pool_id2solvable(pool, repo_add_solvable(repo));
3214 pubkey2solvable(s, data, str);
3215 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3217 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3219 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3220 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3223 solv_free(namedata);
3225 if (!(flags & REPO_NO_INTERNALIZE))
3226 repodata_internalize(data);
3231 repo_add_pubkey(Repo *repo, const char *key, int flags)
3233 Pool *pool = repo->pool;
3240 data = repo_add_repodata(repo, flags);
3243 if ((fp = fopen(key, "r")) == 0)
3245 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3250 if (bufl - l < 4096)
3253 buf = solv_realloc(buf, bufl);
3255 ll = fread(buf, 1, bufl - l, fp);
3259 pool_error(pool, -1, "%s: %s", key, strerror(errno));
3268 s = pool_id2solvable(pool, repo_add_solvable(repo));
3269 if (!pubkey2solvable(s, data, buf))
3271 repo_free_solvable(repo, s - pool->solvables, 1);
3276 if (!(flags & REPO_NO_INTERNALIZE))
3277 repodata_internalize(data);
3278 return s - pool->solvables;
3281 #endif /* ENABLE_RPMDB_PUBKEY */