2 * Copyright (c) 2007-2012, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * convert rpm db to repo
15 #include <sys/types.h>
26 #include <rpm/rpmio.h>
27 #include <rpm/rpmpgp.h>
29 #include <rpm/header.h>
31 #include <rpm/rpmdb.h>
47 #include "repo_rpmdb.h"
49 /* 3: added triggers */
50 /* 4: fixed triggers */
51 #define RPMDB_COOKIE_VERSION 4
54 #define TAG_VERSION 1001
55 #define TAG_RELEASE 1002
56 #define TAG_EPOCH 1003
57 #define TAG_SUMMARY 1004
58 #define TAG_DESCRIPTION 1005
59 #define TAG_BUILDTIME 1006
60 #define TAG_BUILDHOST 1007
61 #define TAG_INSTALLTIME 1008
63 #define TAG_DISTRIBUTION 1010
64 #define TAG_VENDOR 1011
65 #define TAG_LICENSE 1014
66 #define TAG_PACKAGER 1015
67 #define TAG_GROUP 1016
70 #define TAG_FILESIZES 1028
71 #define TAG_FILEMODES 1030
72 #define TAG_FILEMD5S 1035
73 #define TAG_FILELINKTOS 1036
74 #define TAG_FILEFLAGS 1037
75 #define TAG_SOURCERPM 1044
76 #define TAG_PROVIDENAME 1047
77 #define TAG_REQUIREFLAGS 1048
78 #define TAG_REQUIRENAME 1049
79 #define TAG_REQUIREVERSION 1050
80 #define TAG_NOSOURCE 1051
81 #define TAG_NOPATCH 1052
82 #define TAG_CONFLICTFLAGS 1053
83 #define TAG_CONFLICTNAME 1054
84 #define TAG_CONFLICTVERSION 1055
85 #define TAG_TRIGGERNAME 1066
86 #define TAG_TRIGGERVERSION 1067
87 #define TAG_TRIGGERFLAGS 1068
88 #define TAG_OBSOLETENAME 1090
89 #define TAG_FILEDEVICES 1095
90 #define TAG_FILEINODES 1096
91 #define TAG_PROVIDEFLAGS 1112
92 #define TAG_PROVIDEVERSION 1113
93 #define TAG_OBSOLETEFLAGS 1114
94 #define TAG_OBSOLETEVERSION 1115
95 #define TAG_DIRINDEXES 1116
96 #define TAG_BASENAMES 1117
97 #define TAG_DIRNAMES 1118
98 #define TAG_PAYLOADFORMAT 1124
99 #define TAG_PATCHESNAME 1133
100 #define TAG_FILECOLORS 1140
101 #define TAG_SUGGESTSNAME 1156
102 #define TAG_SUGGESTSVERSION 1157
103 #define TAG_SUGGESTSFLAGS 1158
104 #define TAG_ENHANCESNAME 1159
105 #define TAG_ENHANCESVERSION 1160
106 #define TAG_ENHANCESFLAGS 1161
108 #define SIGTAG_SIZE 1000
109 #define SIGTAG_PGP 1002 /* RSA signature */
110 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
111 #define SIGTAG_GPG 1005 /* DSA signature */
113 #define DEP_LESS (1 << 1)
114 #define DEP_GREATER (1 << 2)
115 #define DEP_EQUAL (1 << 3)
116 #define DEP_STRONG (1 << 27)
117 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
119 #define FILEFLAG_GHOST (1 << 6)
123 # define RPM_INDEX_SIZE 4
125 # define RPM_INDEX_SIZE 8
133 typedef struct rpmhead {
137 unsigned char data[1];
141 static inline unsigned char *
142 headfindtag(RpmHead *h, int tag)
145 unsigned char *d, taga[4];
151 for (i = 0; i < h->cnt; i++, d -= 16)
152 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
158 headexists(RpmHead *h, int tag)
160 return headfindtag(h, tag) ? 1 : 0;
163 static unsigned int *
164 headint32array(RpmHead *h, int tag, int *cnt)
166 unsigned int i, o, *r;
167 unsigned char *d = headfindtag(h, tag);
169 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
171 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
172 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
173 if (o + 4 * i > h->dcnt)
176 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
179 for (o = 0; o < i; o++, d += 4)
180 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
184 /* returns the first entry of an integer array */
186 headint32(RpmHead *h, int tag)
189 unsigned char *d = headfindtag(h, tag);
191 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
193 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
194 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
195 if (i == 0 || o + 4 * i > h->dcnt)
198 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
201 static unsigned int *
202 headint16array(RpmHead *h, int tag, int *cnt)
204 unsigned int i, o, *r;
205 unsigned char *d = headfindtag(h, tag);
207 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
209 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
210 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
211 if (o + 4 * i > h->dcnt)
214 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
217 for (o = 0; o < i; o++, d += 2)
218 r[o] = d[0] << 8 | d[1];
223 headstring(RpmHead *h, int tag)
226 unsigned char *d = headfindtag(h, tag);
227 /* 6: STRING, 9: I18NSTRING */
228 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
230 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
233 return (char *)h->dp + o;
237 headstringarray(RpmHead *h, int tag, int *cnt)
240 unsigned char *d = headfindtag(h, tag);
243 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
245 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
246 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
247 r = solv_calloc(i ? i : 1, sizeof(char *));
251 for (o = 0; o < i; o++)
255 d += strlen((char *)d) + 1;
256 if (d >= h->dp + h->dcnt)
265 static unsigned char *
266 headbinary(RpmHead *h, int tag, unsigned int *sizep)
269 unsigned char *d = headfindtag(h, tag);
270 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
272 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
273 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
274 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
281 static char *headtoevr(RpmHead *h)
288 version = headstring(h, TAG_VERSION);
289 release = headstring(h, TAG_RELEASE);
290 epoch = headint32(h, TAG_EPOCH);
291 if (!version || !release)
293 fprintf(stderr, "headtoevr: bad rpm header\n");
296 for (v = version; *v >= '0' && *v <= '9'; v++)
298 if (epoch || (v != version && *v == ':'))
300 char epochbuf[11]; /* 32bit decimal will fit in */
301 sprintf(epochbuf, "%u", epoch);
302 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
303 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
307 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
308 sprintf(evr, "%s-%s", version, release);
315 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
317 const unsigned char *cp;
320 unsigned char *buf = 0, *bp;
322 /* check if it's already utf8, code taken from screen ;-) */
323 cp = (const unsigned char *)str;
324 while ((c = *cp++) != 0)
328 if ((c & 0xc0) != 0x80)
329 break; /* encoding error */
330 c = (c & 0x3f) | (state << 6);
331 if (!(state & 0x40000000))
333 /* check for overlong sequences */
334 if ((c & 0x820823e0) == 0x80000000)
336 else if ((c & 0x020821f0) == 0x02000000)
338 else if ((c & 0x000820f8) == 0x00080000)
340 else if ((c & 0x0000207c) == 0x00002000)
350 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
352 c = (c & 0x03) | 0xbfffff00; /* 4 */
354 c = (c & 0x07) | 0xbfffc000; /* 3 */
356 c = (c & 0x0f) | 0xbff00000; /* 2 */
358 c = (c & 0x1f) | 0xfc000000; /* 1 */
362 state = (c & 0x80000000) ? c : 0;
366 /* not utf8, assume latin1 */
367 buf = solv_malloc(2 * strlen(str) + 1);
368 cp = (const unsigned char *)str;
371 while ((c = *cp++) != 0)
384 repodata_set_str(repodata, handle, tag, str);
390 #define MAKEDEPS_FILTER_WEAK (1 << 0)
391 #define MAKEDEPS_FILTER_STRONG (1 << 1)
392 #define MAKEDEPS_NO_RPMLIB (1 << 2)
395 * strong: 0: ignore strongness
396 * 1: filter to strong
400 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
404 int i, cc, nc, vc, fc;
406 unsigned int olddeps;
410 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
411 n = headstringarray(rpmhead, tagn, &nc);
414 v = headstringarray(rpmhead, tagv, &vc);
420 f = headint32array(rpmhead, tagf, &fc);
427 if (nc != vc || nc != fc)
429 fprintf(stderr, "bad dependency entries\n");
434 haspre = 0; /* add no prereq marker */
437 /* we do filtering */
439 for (i = 0; i < nc; i++)
441 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
443 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
444 if (!strncmp(n[i], "rpmlib(", 7))
446 if ((f[i] & DEP_PRE) != 0)
451 else if (tagn == TAG_REQUIRENAME)
453 /* no filtering, just look for the first prereq */
454 for (i = 0; i < nc; i++)
455 if ((f[i] & DEP_PRE) != 0)
469 olddeps = repo_reserve_ids(repo, 0, cc);
470 ida = repo->idarraydata + olddeps;
477 haspre = 2; /* pass two: prereqs */
479 *ida++ = SOLVABLE_PREREQMARKER;
481 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
483 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
485 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
487 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
488 if (!strncmp(n[i], "rpmlib(", 7))
490 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
494 if ((f[i] & DEP_LESS) != 0)
496 if ((f[i] & DEP_EQUAL) != 0)
498 if ((f[i] & DEP_GREATER) != 0)
500 name = pool_str2id(pool, n[i], 1);
501 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
502 evr = pool_str2id(pool, v[i] + 2, 1);
504 evr = pool_str2id(pool, v[i], 1);
505 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
508 *ida++ = pool_str2id(pool, n[i], 1);
511 repo->idarraysize += cc + 1;
519 #ifdef USE_FILEFILTER
521 #define FILEFILTER_EXACT 0
522 #define FILEFILTER_STARTS 1
523 #define FILEFILTER_CONTAINS 2
531 static struct filefilter filefilters[] = {
532 { FILEFILTER_CONTAINS, "/bin/", 0},
533 { FILEFILTER_CONTAINS, "/sbin/", 0},
534 { FILEFILTER_CONTAINS, "/lib/", 0},
535 { FILEFILTER_CONTAINS, "/lib64/", 0},
536 { FILEFILTER_CONTAINS, "/etc/", 0},
537 { FILEFILTER_STARTS, "/usr/games/", 0},
538 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
539 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
540 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
546 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
550 unsigned int *fkb, *fn, *fsz, *fm, *fino;
551 unsigned int inotest[256], inotestok;
555 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
556 if (!fsz || fc != fszc)
561 /* stupid rpm records sizes of directories, so we have to check the mode */
562 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
563 if (!fm || fc != fszc)
569 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
570 if (!fino || fc != fszc)
578 if (fc < sizeof(inotest))
580 memset(inotest, 0, sizeof(inotest));
581 for (i = 0; i < fc; i++)
584 if (fsz[i] == 0 || !S_ISREG(fm[i]))
586 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
587 bit = 1 << (fino[i] & 31);
588 if ((inotest[off] & bit) != 0)
597 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
599 unsigned int mask, hash, hh;
600 if (!fdev || fc != fszc)
609 while ((mask & (mask - 1)) != 0)
610 mask = mask & (mask - 1);
612 if (mask > sizeof(inotest)/sizeof(*inotest))
613 fx = solv_calloc(mask, sizeof(unsigned int));
617 memset(fx, 0, mask * sizeof(unsigned int));
620 for (i = 0; i < fc; i++)
622 if (fsz[i] == 0 || !S_ISREG(fm[i]))
624 hash = (fino[i] + fdev[i] * 31) & mask;
626 while ((j = fx[hash]) != 0)
628 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
630 fsz[i] = 0; /* kill entry */
633 hash = (hash + hh++) & mask;
643 fn = solv_calloc(dc, sizeof(unsigned int));
644 fkb = solv_calloc(dc, sizeof(unsigned int));
645 for (i = 0; i < fc; i++)
650 if (fsz[i] == 0 || !S_ISREG(fm[i]))
652 fkb[di[i]] += fsz[i] / 1024 + 1;
657 handle = s - pool->solvables;
658 for (i = 0; i < dc; i++)
664 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
665 did = repodata_str2dir(data, "/usr/src", 1);
667 continue; /* work around rpm bug */
670 did = repodata_str2dir(data, dn[i], 1);
671 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
677 /* assumes last processed array is provides! */
679 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
686 #ifdef USE_FILEFILTER
688 struct filefilter *ff;
697 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
700 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
706 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
715 fprintf(stderr, "bad filelist\n");
720 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
722 for (i = 0; i < bnc; i++)
724 #ifdef USE_FILEFILTER
726 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
730 switch (ff->dirmatch)
732 case FILEFILTER_STARTS:
733 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
736 case FILEFILTER_CONTAINS:
737 if (!strstr(dn[di[i]], ff->dir))
740 case FILEFILTER_EXACT:
742 if (strcmp(dn[di[i]], ff->dir))
749 if (strcmp(bn[i], ff->base))
754 if (j == sizeof(filefilters)/sizeof(*filefilters))
758 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
762 fn = solv_realloc(fn, fna);
764 strcpy(fn, dn[di[i]]);
766 olddeps = repo_addid_dep(repo, olddeps, pool_str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
773 handle = s - pool->solvables;
774 did = repodata_str2dir(data, dn[di[i]], 1);
777 did = repodata_str2dir(data, "/", 1);
778 if (b && b[0] == '/')
779 b++; /* work around rpm bug */
781 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
795 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
797 const char *p, *sevr, *sarch;
799 p = strrchr(sourcerpm, '.');
800 if (!p || strcmp(p, ".rpm") != 0)
803 while (p > sourcerpm && *p != '.')
805 if (*p != '.' || p == sourcerpm)
808 while (p > sourcerpm && *p != '-')
810 if (*p != '-' || p == sourcerpm)
813 while (p > sourcerpm && *p != '-')
815 if (*p != '-' || p == sourcerpm)
818 if (!strcmp(sarch, "src.rpm"))
819 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
820 else if (!strcmp(sarch, "nosrc.rpm"))
821 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
823 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
824 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
825 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
827 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
828 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
829 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
831 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
835 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
841 name = headstring(rpmhead, TAG_NAME);
842 if (!strcmp(name, "gpg-pubkey"))
844 s->name = pool_str2id(pool, name, 1);
847 fprintf(stderr, "package has no name\n");
850 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
852 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
855 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
856 s->arch = ARCH_NOSRC;
861 s->arch = ARCH_NOARCH;
862 evr = headtoevr(rpmhead);
863 s->evr = pool_str2id(pool, evr, 1);
864 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
866 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
867 if ((flags & RPM_ADD_NO_FILELIST) == 0)
868 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
869 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
870 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
871 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
872 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
873 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
875 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
876 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
877 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
878 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
879 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
880 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
888 handle = s - pool->solvables;
889 str = headstring(rpmhead, TAG_SUMMARY);
891 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
892 str = headstring(rpmhead, TAG_DESCRIPTION);
896 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
897 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
901 /* oh my, found SUSE special author section */
903 str = solv_strdup(str);
906 while (l > 0 && str[l - 1] == '\n')
909 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
911 aut = str; /* copy over */
912 while (*p == ' ' || *p == '\n')
925 while (aut != str && aut[-1] == '\n')
929 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
933 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
935 str = headstring(rpmhead, TAG_GROUP);
937 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
938 str = headstring(rpmhead, TAG_LICENSE);
940 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
941 str = headstring(rpmhead, TAG_URL);
943 repodata_set_str(data, handle, SOLVABLE_URL, str);
944 str = headstring(rpmhead, TAG_DISTRIBUTION);
946 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
947 str = headstring(rpmhead, TAG_PACKAGER);
949 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
950 u32 = headint32(rpmhead, TAG_BUILDTIME);
952 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
953 u32 = headint32(rpmhead, TAG_INSTALLTIME);
955 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
956 u32 = headint32(rpmhead, TAG_SIZE);
958 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
960 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
961 if ((flags & RPM_ADD_TRIGGERS) != 0)
964 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
967 for (; (id = repo->idarraydata[ida]) != 0; ida++)
969 /* we currently do not support rel ids in incore data, so
970 * strip off versioning information */
973 Reldep *rd = GETRELDEP(pool, id);
978 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
988 copyreldep(Pool *pool, Pool *frompool, Id id)
990 Reldep *rd = GETRELDEP(frompool, id);
991 Id name = rd->name, evr = rd->evr;
993 name = copyreldep(pool, frompool, name);
995 name = pool_str2id(pool, pool_id2str(frompool, name), 1);
997 evr = copyreldep(pool, frompool, evr);
999 evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
1000 return pool_rel2id(pool, name, evr, rd->flags, 1);
1004 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1009 Pool *frompool = fromrepo->pool;
1013 from = fromrepo->idarraydata + fromoff;
1014 for (ida = from, cc = 0; *ida; ida++, cc++)
1018 ido = repo_reserve_ids(repo, 0, cc);
1019 ida = repo->idarraydata + ido;
1020 if (frompool && pool != frompool)
1026 id = copyreldep(pool, frompool, id);
1028 id = pool_str2id(pool, pool_id2str(frompool, id), 1);
1034 memcpy(ida, from, (cc + 1) * sizeof(Id));
1035 repo->idarraysize += cc + 1;
1039 #define COPYDIR_DIRCACHE_SIZE 512
1041 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1044 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1046 if (cache && cache[did & 255] == did)
1047 return cache[(did & 255) + 256];
1048 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1052 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1054 Id parent = dirpool_parent(&fromdata->dirpool, did);
1055 Id compid = dirpool_compid(&fromdata->dirpool, did);
1057 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1058 if (fromspool != &pool->ss)
1059 compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
1060 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1063 cache[did & 255] = did;
1064 cache[(did & 255) + 256] = compid;
1069 struct solvable_copy_cbdata {
1076 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1078 struct solvable_copy_cbdata *cbdata = vcbdata;
1080 Repodata *data = cbdata->data;
1081 Id handle = cbdata->handle;
1082 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1083 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1085 keyname = key->name;
1086 if (keyname >= ID_NUM_INTERNAL)
1087 keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
1090 case REPOKEY_TYPE_ID:
1091 case REPOKEY_TYPE_CONSTANTID:
1093 assert(!data->localpool); /* implement me! */
1094 if (pool != frompool || fromdata->localpool)
1097 id = copyreldep(pool, frompool, id);
1099 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1101 if (key->type == REPOKEY_TYPE_ID)
1102 repodata_set_id(data, handle, keyname, id);
1104 repodata_set_constantid(data, handle, keyname, id);
1106 case REPOKEY_TYPE_STR:
1107 repodata_set_str(data, handle, keyname, kv->str);
1109 case REPOKEY_TYPE_VOID:
1110 repodata_set_void(data, handle, keyname);
1112 case REPOKEY_TYPE_NUM:
1113 repodata_set_num(data, handle, keyname, kv->num);
1115 case REPOKEY_TYPE_CONSTANT:
1116 repodata_set_constant(data, handle, keyname, kv->num);
1118 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1120 assert(!data->localpool); /* implement me! */
1121 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1122 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1124 case REPOKEY_TYPE_DIRSTRARRAY:
1126 assert(!data->localpool); /* implement me! */
1127 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1128 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1130 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1132 assert(!data->localpool); /* implement me! */
1134 break; /* can't do those at the moment */
1135 if (pool != frompool || fromdata->localpool)
1136 id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
1137 repodata_add_idarray(data, handle, keyname, id);
1146 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1148 Repo *repo = s->repo;
1149 Repo *fromrepo = r->repo;
1150 Pool *pool = repo->pool;
1151 struct solvable_copy_cbdata cbdata;
1153 /* copy solvable data */
1154 if (pool == fromrepo->pool)
1159 s->vendor = r->vendor;
1164 s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
1166 s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
1168 s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
1170 s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
1172 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1173 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1174 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1175 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1176 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1177 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1178 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1179 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1181 /* copy all attributes */
1185 cbdata.handle = s - pool->solvables;
1186 cbdata.dircache = dircache;
1187 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1190 /* used to sort entries returned in some database order */
1192 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1194 struct rpmid const *a = va, *b = vb;
1196 r = strcmp(a->name, b->name);
1199 return a->dbid - b->dbid;
1203 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1206 Pool *pool = repo->pool;
1207 Solvable *a = pool->solvables + *(Id *)va;
1208 Solvable *b = pool->solvables + *(Id *)vb;
1211 if (a->name != b->name)
1212 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1213 rpmdbid = repo->rpmdbid;
1214 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1218 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1220 Pool *pool = repo->pool;
1223 tmp = pool->solvables[pa];
1224 pool->solvables[pa] = pool->solvables[pb];
1225 pool->solvables[pb] = tmp;
1228 Id tmpid = repo->rpmdbid[pa - repo->start];
1229 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1230 repo->rpmdbid[pb - repo->start] = tmpid;
1232 /* only works if nothing is already internalized! */
1234 repodata_swap_attrs(data, pa, pb);
1238 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1241 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1243 # if defined(WORDS_BIGENDIAN)
1248 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1250 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1254 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1257 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1259 # if defined(WORDS_BIGENDIAN)
1264 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1266 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1271 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1273 memset(cookie, 0, 32);
1274 cookie[3] = RPMDB_COOKIE_VERSION;
1275 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1276 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1279 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1281 opendbenv(const char *rootdir)
1283 char dbpath[PATH_MAX];
1287 if (db_env_create(&dbenv, 0))
1289 perror("db_env_create");
1292 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1293 dbenv->set_thread_count(dbenv, 8);
1295 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1296 if (access(dbpath, W_OK) == -1)
1298 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1303 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1305 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1310 perror("dbenv open");
1311 dbenv->close(dbenv, 0);
1319 count_headers(const char *rootdir, DB_ENV *dbenv)
1321 char dbpath[PATH_MAX];
1322 struct stat statbuf;
1329 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1330 if (stat(dbpath, &statbuf))
1332 memset(&dbkey, 0, sizeof(dbkey));
1333 memset(&dbdata, 0, sizeof(dbdata));
1334 if (db_create(&db, dbenv, 0))
1336 perror("db_create");
1339 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1341 perror("db->open Name index");
1344 if (db->cursor(db, NULL, &dbc, 0))
1346 perror("db->cursor");
1349 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1350 count += dbdata.size / RPM_INDEX_SIZE;
1357 * read rpm db as repo
1362 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1364 Pool *pool = repo->pool;
1365 unsigned char buf[16];
1372 struct rpmid *rpmids, *rp;
1378 unsigned int refmask, h;
1379 char dbpath[PATH_MAX];
1383 struct stat packagesstat;
1384 unsigned char newcookie[32];
1385 const unsigned char *oldcookie = 0;
1386 Id oldcookietype = 0;
1388 int count = 0, done = 0;
1391 now = solv_timems(0);
1392 memset(&dbkey, 0, sizeof(dbkey));
1393 memset(&dbdata, 0, sizeof(dbdata));
1398 data = repo_add_repodata(repo, flags);
1400 if (ref && !(ref->nsolvables && ref->rpmdbid))
1403 if (!(dbenv = opendbenv(rootdir)))
1406 /* XXX: should get ro lock of Packages database! */
1407 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1408 if (stat(dbpath, &packagesstat))
1413 mkrpmdbcookie(&packagesstat, newcookie);
1414 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1417 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1418 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1421 int solvstart = 0, solvend = 0;
1423 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1424 count = count_headers(rootdir, dbenv);
1425 if (db_create(&db, dbenv, 0))
1427 perror("db_create");
1430 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1432 perror("db->open Packages index");
1435 if (db->get_byteswapped(db, &byteswapped))
1437 perror("db->get_byteswapped");
1440 if (db->cursor(db, NULL, &dbc, 0))
1442 perror("db->cursor");
1449 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1453 s = pool_id2solvable(pool, repo_add_solvable(repo));
1455 solvstart = s - pool->solvables;
1456 solvend = s - pool->solvables + 1;
1459 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1460 if (dbkey.size != 4)
1462 fprintf(stderr, "corrupt Packages database (key size)\n");
1465 dbid = db2rpmdbid(dbkey.data, byteswapped);
1466 if (dbid == 0) /* the join key */
1468 if (dbdata.size < 8)
1470 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1473 if (dbdata.size > rpmheadsize)
1475 rpmheadsize = dbdata.size + 128;
1476 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1478 memcpy(buf, dbdata.data, 8);
1479 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1480 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1481 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1483 fprintf(stderr, "corrupt rpm database (data size)\n");
1486 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1487 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1488 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1489 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1496 /* We can reuse this solvable, but make sure it's still
1497 associated with this repo. */
1498 memset(s, 0, sizeof(*s));
1501 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1505 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1506 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1511 /* oops, could not reuse. free it instead */
1512 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1519 /* now sort all solvables in the new solvstart..solvend block */
1520 if (solvend - solvstart > 1)
1522 pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1523 for (i = solvstart; i < solvend; i++)
1524 pkgids[i - solvstart] = i;
1525 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1527 for (i = solvstart; i < solvend; i++)
1529 int j = pkgids[i - solvstart];
1531 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1533 swap_solvables(repo, data, i, j);
1540 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1542 memset(dircache, 0, sizeof(dircache));
1543 if (db_create(&db, dbenv, 0))
1545 perror("db_create");
1548 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1550 perror("db->open Name index");
1553 if (db->get_byteswapped(db, &byteswapped))
1555 perror("db->get_byteswapped");
1558 if (db->cursor(db, NULL, &dbc, 0))
1560 perror("db->cursor");
1565 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1567 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1571 while(dl >= RPM_INDEX_SIZE)
1573 rpmids = solv_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1574 rpmids[nrpmids].dbid = db2rpmdbid(dp, byteswapped);
1575 rpmids[nrpmids].name = solv_malloc((int)dbkey.size + 1);
1576 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1577 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1579 dp += RPM_INDEX_SIZE;
1580 dl -= RPM_INDEX_SIZE;
1588 solv_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1593 /* create hash from dbid to ref */
1594 refmask = mkmask(ref->nsolvables);
1595 refhash = solv_calloc(refmask + 1, sizeof(Id));
1596 for (i = 0; i < ref->end - ref->start; i++)
1598 if (!ref->rpmdbid[i])
1600 h = ref->rpmdbid[i] & refmask;
1602 h = (h + 317) & refmask;
1603 refhash[h] = i + 1; /* make it non-zero */
1606 /* count the misses, they will cost us time */
1607 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1609 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1615 while ((id = refhash[h]))
1617 if (ref->rpmdbid[id - 1] == dbid)
1619 h = (h + 317) & refmask;
1628 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1630 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1632 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1635 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->dbid;
1639 while ((id = refhash[h]))
1641 if (ref->rpmdbid[id - 1] == dbid)
1643 h = (h + 317) & refmask;
1647 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1650 solvable_copy(s, r, data, dircache);
1657 if (db_create(&db, dbenv, 0))
1659 perror("db_create");
1662 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1664 perror("db->open var/lib/rpm/Packages");
1667 if (db->get_byteswapped(db, &byteswapped))
1669 perror("db->get_byteswapped");
1673 rpmdbid2db(buf, rp->dbid, byteswapped);
1678 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1681 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1682 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1685 if (dbdata.size < 8)
1687 fprintf(stderr, "corrupt rpm database (size)\n");
1690 if (dbdata.size > rpmheadsize)
1692 rpmheadsize = dbdata.size + 128;
1693 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1695 memcpy(buf, dbdata.data, 8);
1696 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1697 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1698 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1700 fprintf(stderr, "corrupt rpm database (data size)\n");
1703 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1704 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1706 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1707 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1711 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1712 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1720 for (i = 0; i < nrpmids; i++)
1721 solv_free(rpmids[i].name);
1727 dbenv->close(dbenv, 0);
1730 if (!(flags & REPO_NO_INTERNALIZE))
1731 repodata_internalize(data);
1732 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1733 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1734 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1735 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1736 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)));
1741 static inline unsigned int
1742 getu32(const unsigned char *dp)
1744 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1749 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1751 int i, sigdsize, sigcnt, l;
1752 Pool *pool = repo->pool;
1754 RpmHead *rpmhead = 0;
1755 int rpmheadsize = 0;
1756 char *payloadformat;
1758 unsigned char lead[4096];
1759 int headerstart, headerend;
1762 unsigned char pkgid[16];
1767 data = repo_add_repodata(repo, flags);
1769 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1770 chksumtype = REPOKEY_TYPE_SHA256;
1771 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1772 chksumtype = REPOKEY_TYPE_SHA1;
1773 for (i = 0; i < nrpms; i++)
1775 if ((fp = fopen(rpms[i], "r")) == 0)
1780 if (fstat(fileno(fp), &stb))
1786 chksumh = solv_chksum_free(chksumh, 0);
1788 chksumh = solv_chksum_create(chksumtype);
1789 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1791 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1796 solv_chksum_add(chksumh, lead, 96 + 16);
1797 if (lead[78] != 0 || lead[79] != 5)
1799 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1803 if (getu32(lead + 96) != 0x8eade801)
1805 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1809 sigcnt = getu32(lead + 96 + 8);
1810 sigdsize = getu32(lead + 96 + 12);
1811 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1813 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1817 sigdsize += sigcnt * 16;
1818 sigdsize = (sigdsize + 7) & ~7;
1819 headerstart = 96 + 16 + sigdsize;
1821 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1823 unsigned char *chksum;
1824 unsigned int chksumsize;
1825 /* extract pkgid from the signature header */
1826 if (sigdsize > rpmheadsize)
1828 rpmheadsize = sigdsize + 128;
1829 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1831 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1833 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1838 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1839 rpmhead->cnt = sigcnt;
1840 rpmhead->dcnt = sigdsize - sigcnt * 16;
1841 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1842 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1843 if (chksum && chksumsize == 16)
1846 memcpy(pkgid, chksum, 16);
1851 /* just skip the signature header */
1854 l = sigdsize > 4096 ? 4096 : sigdsize;
1855 if (fread(lead, l, 1, fp) != 1)
1857 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1862 solv_chksum_add(chksumh, lead, l);
1866 if (fread(lead, 16, 1, fp) != 1)
1868 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1873 solv_chksum_add(chksumh, lead, 16);
1874 if (getu32(lead) != 0x8eade801)
1876 fprintf(stderr, "%s: bad header\n", rpms[i]);
1880 sigcnt = getu32(lead + 8);
1881 sigdsize = getu32(lead + 12);
1882 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1884 fprintf(stderr, "%s: bad header\n", rpms[i]);
1888 l = sigdsize + sigcnt * 16;
1889 headerend = headerstart + 16 + l;
1890 if (l > rpmheadsize)
1892 rpmheadsize = l + 128;
1893 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1895 if (fread(rpmhead->data, l, 1, fp) != 1)
1897 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1902 solv_chksum_add(chksumh, rpmhead->data, l);
1903 rpmhead->cnt = sigcnt;
1904 rpmhead->dcnt = sigdsize;
1905 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1906 if (headexists(rpmhead, TAG_PATCHESNAME))
1908 /* this is a patch rpm, ignore */
1912 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1913 if (payloadformat && !strcmp(payloadformat, "drpm"))
1915 /* this is a delta rpm */
1920 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1921 solv_chksum_add(chksumh, lead, l);
1923 s = pool_id2solvable(pool, repo_add_solvable(repo));
1924 rpm2solv(pool, repo, data, s, rpmhead, flags);
1927 Id handle = s - pool->solvables;
1928 repodata_set_location(data, handle, 0, 0, rpms[i]);
1929 if (S_ISREG(stb.st_mode))
1930 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1931 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1933 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1935 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
1939 chksumh = solv_chksum_free(chksumh, 0);
1942 if (!(flags & REPO_NO_INTERNALIZE))
1943 repodata_internalize(data);
1948 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1950 int end = repo->end;
1951 repo_add_rpms(repo, &rpm, 1, flags);
1952 if (end == repo->end)
1955 return repo->end - 1;
1959 linkhash(const char *lt, char *hash)
1962 const unsigned char *str = (const unsigned char *)lt;
1966 while ((c = *str++) != 0)
1968 sprintf(hash, "%08x", r);
1969 sprintf(hash + 8, "%08x", l);
1970 sprintf(hash + 16, "%08x", 0);
1971 sprintf(hash + 24, "%08x", 0);
1975 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1977 RpmHead *rpmhead = rpmhandle;
1982 unsigned int *di, diidx;
1983 unsigned int *co = 0;
1984 unsigned int *ff = 0;
1985 unsigned int lastdir;
1988 int cnt, dcnt, cnt2;
1992 char md5[33], *md5p = 0;
1994 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1997 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1999 for (i = 0; i < dcnt; i++)
2000 (*cb)(cbdata, dn[i], 0, (char *)0);
2004 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2010 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2011 if (!di || cnt != cnt2)
2018 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2019 if (!fm || cnt != cnt2)
2027 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2029 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2030 if (!md || cnt != cnt2)
2040 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2042 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2043 if (!co || cnt != cnt2)
2054 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2056 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2057 if (!ff || cnt != cnt2)
2071 for (i = 0; i < cnt; i++)
2073 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2078 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2081 l = l1 + strlen(bn[i]) + 1;
2085 space = solv_realloc(space, spacen);
2087 if (lastdir != diidx)
2089 strcpy(space, dn[diidx]);
2093 strcpy(space + l1, bn[i]);
2102 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2108 linkhash(lt[i], md5);
2114 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2118 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2132 rpm_query(void *rpmhandle, Id what)
2134 const char *name, *arch, *sourcerpm;
2138 RpmHead *rpmhead = rpmhandle;
2143 name = headstring(rpmhead, TAG_NAME);
2146 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2148 arch = headstring(rpmhead, TAG_ARCH);
2151 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2158 evr = headtoevr(rpmhead);
2159 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2161 sprintf(r, "%s-%s.%s", name, evr, arch);
2165 name = headstring(rpmhead, TAG_NAME);
2166 r = solv_strdup(name);
2169 r = headtoevr(rpmhead);
2176 struct rpm_by_state {
2191 #define ENTRIES_BLOCK 255
2192 #define NAMEDATA_BLOCK 1023
2194 static struct rpmdbentry *
2195 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2208 struct rpmdbentry *entries = 0;
2214 dbenv = state->dbenv;
2215 if (db_create(&db, dbenv, 0))
2217 perror("db_create");
2220 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2222 perror("db->open index");
2226 if (db->get_byteswapped(db, &byteswapped))
2228 perror("db->get_byteswapped");
2232 if (db->cursor(db, NULL, &dbc, 0))
2234 perror("db->cursor");
2238 memset(&dbkey, 0, sizeof(dbkey));
2239 memset(&dbdata, 0, sizeof(dbdata));
2242 dbkey.data = (void *)match;
2243 dbkey.size = strlen(match);
2245 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2247 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2251 while(dl >= RPM_INDEX_SIZE)
2253 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2254 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
2255 entries[nentries].nameoff = namedatal;
2257 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2258 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2259 namedata[namedatal + dbkey.size] = 0;
2260 namedatal += dbkey.size + 1;
2261 dp += RPM_INDEX_SIZE;
2262 dl -= RPM_INDEX_SIZE;
2269 *nentriesp = nentries;
2270 *namedatap = namedata;
2275 freestate(struct rpm_by_state *state)
2281 state->db->close(state->db, 0);
2283 state->dbenv->close(state->dbenv, 0);
2284 solv_free(state->rpmhead);
2288 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2290 struct rpm_by_state state;
2291 struct rpmdbentry *entries;
2298 queue_empty(rpmdbidq);
2299 memset(&state, 0, sizeof(state));
2300 if (!(state.dbenv = opendbenv(rootdir)))
2302 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2304 for (i = 0; i < nentries; i++)
2305 queue_push(rpmdbidq, entries[i].rpmdbid);
2307 solv_free(namedata);
2313 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2315 struct rpm_by_state *state = *statep;
2316 unsigned char buf[16];
2326 *statep = (void *)0;
2332 state = solv_calloc(1, sizeof(*state));
2335 if (!state->dbopened)
2337 state->dbopened = 1;
2338 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2340 if (db_create(&state->db, state->dbenv, 0))
2342 perror("db_create");
2344 state->dbenv->close(state->dbenv, 0);
2348 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2350 perror("db->open var/lib/rpm/Packages");
2351 state->db->close(state->db, 0);
2353 state->dbenv->close(state->dbenv, 0);
2357 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2359 perror("db->get_byteswapped");
2360 state->db->close(state->db, 0);
2362 state->dbenv->close(state->dbenv, 0);
2367 rpmdbid2db(buf, rpmdbid, state->byteswapped);
2368 memset(&dbkey, 0, sizeof(dbkey));
2369 memset(&dbdata, 0, sizeof(dbdata));
2374 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2379 if (dbdata.size < 8)
2381 fprintf(stderr, "corrupt rpm database (size)\n");
2384 if (dbdata.size > state->rpmheadsize)
2386 state->rpmheadsize = dbdata.size + 128;
2387 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2389 rpmhead = state->rpmhead;
2390 memcpy(buf, dbdata.data, 8);
2391 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2392 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2393 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2395 fprintf(stderr, "corrupt rpm database (data size)\n");
2398 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2399 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2404 rpm_byfp(FILE *fp, const char *name, void **statep)
2406 struct rpm_by_state *state = *statep;
2407 /* int headerstart, headerend; */
2409 int sigdsize, sigcnt, l;
2410 unsigned char lead[4096];
2413 return rpm_byrpmdbid(0, 0, statep);
2416 state = solv_calloc(1, sizeof(*state));
2419 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2421 fprintf(stderr, "%s: not a rpm\n", name);
2424 if (lead[78] != 0 || lead[79] != 5)
2426 fprintf(stderr, "%s: not a V5 header\n", name);
2429 if (getu32(lead + 96) != 0x8eade801)
2431 fprintf(stderr, "%s: bad signature header\n", name);
2434 sigcnt = getu32(lead + 96 + 8);
2435 sigdsize = getu32(lead + 96 + 12);
2436 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2438 fprintf(stderr, "%s: bad signature header\n", name);
2441 sigdsize += sigcnt * 16;
2442 sigdsize = (sigdsize + 7) & ~7;
2443 /* headerstart = 96 + 16 + sigdsize; */
2446 l = sigdsize > 4096 ? 4096 : sigdsize;
2447 if (fread(lead, l, 1, fp) != 1)
2449 fprintf(stderr, "%s: unexpected EOF\n", name);
2454 if (fread(lead, 16, 1, fp) != 1)
2456 fprintf(stderr, "%s: unexpected EOF\n", name);
2459 if (getu32(lead) != 0x8eade801)
2461 fprintf(stderr, "%s: bad header\n", name);
2465 sigcnt = getu32(lead + 8);
2466 sigdsize = getu32(lead + 12);
2467 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2469 fprintf(stderr, "%s: bad header\n", name);
2473 l = sigdsize + sigcnt * 16;
2474 /* headerend = headerstart + 16 + l; */
2475 if (l > state->rpmheadsize)
2477 state->rpmheadsize = l + 128;
2478 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2480 rpmhead = state->rpmhead;
2481 if (fread(rpmhead->data, l, 1, fp) != 1)
2483 fprintf(stderr, "%s: unexpected EOF\n", name);
2487 rpmhead->cnt = sigcnt;
2488 rpmhead->dcnt = sigdsize;
2489 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2494 rpm_byrpmh(Header h, void **statep)
2496 struct rpm_by_state *state = *statep;
2497 const unsigned char *uh;
2498 int sigdsize, sigcnt, l;
2502 uh = headerUnload(h);
2504 uh = headerUnload(h, NULL);
2508 sigcnt = getu32(uh);
2509 sigdsize = getu32(uh + 4);
2510 l = sigdsize + sigcnt * 16;
2513 state = solv_calloc(1, sizeof(*state));
2516 if (l > state->rpmheadsize)
2518 state->rpmheadsize = l + 128;
2519 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2521 rpmhead = state->rpmhead;
2522 memcpy(rpmhead->data, uh + 8, l - 8);
2524 rpmhead->cnt = sigcnt;
2525 rpmhead->dcnt = sigdsize;
2526 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2531 #ifdef ENABLE_RPMDB_PUBKEY
2534 r64dec1(char *p, unsigned int *vp, int *eofp)
2539 for (i = 0; i < 4; )
2544 if (x >= 'A' && x <= 'Z')
2546 else if (x >= 'a' && x <= 'z')
2548 else if (x >= '0' && x <= '9')
2575 crc24(unsigned char *p, int len)
2577 unsigned int crc = 0xb704ceL;
2582 crc ^= (*p++) << 16;
2583 for (i = 0; i < 8; i++)
2584 if ((crc <<= 1) & 0x1000000)
2587 return crc & 0xffffffL;
2590 static unsigned char *
2591 unarmor(char *pubkey, int *pktlp)
2595 unsigned char *buf, *bp;
2599 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2601 pubkey = strchr(pubkey, '\n');
2606 pubkey = strchr(pubkey, '\n');
2609 /* skip header lines */
2612 while (*pubkey == ' ' || *pubkey == '\t')
2614 if (*pubkey == '\n')
2616 pubkey = strchr(pubkey, '\n');
2621 p = strchr(pubkey, '=');
2625 bp = buf = solv_malloc(l * 3 / 4 + 4);
2629 pubkey = r64dec1(pubkey, &v, &eof);
2639 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2642 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2647 if (v != crc24(buf, bp - buf))
2652 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2654 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2664 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2667 unsigned char keyid[8];
2668 unsigned int kcr = 0, maxex = 0;
2669 unsigned char *pubkey = 0;
2670 unsigned char *userid = 0;
2676 for (; pl; p += l, pl -= l)
2680 if (!(x & 128) || pl <= 0)
2685 tag = (x & 0x3c) >> 2;
2707 else if (x >= 192 && x < 224)
2711 l = ((x - 192) << 8) + *p++ + 192;
2718 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2729 pubkey = solv_realloc(pubkey, l);
2731 memcpy(pubkey, p, l);
2740 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2744 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2748 memset(keyid, 0, 8);
2749 if (p[7] == 1) /* RSA */
2752 unsigned char fp[16];
2756 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2757 memcpy(keyid, p + 10 + ql - 8, 8);
2758 h = solv_chksum_create(REPOKEY_TYPE_MD5);
2759 solv_chksum_add(h, p + 10, ql);
2761 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2762 solv_chksum_add(h, q + 2, ql);
2763 solv_chksum_free(h, fp);
2764 for (i = 0; i < 16; i++)
2765 sprintf(fpx + i * 2, "%02x", fp[i]);
2766 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2773 unsigned char hdr[3];
2774 unsigned char fp[20];
2777 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2781 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
2782 solv_chksum_add(h, hdr, 3);
2783 solv_chksum_add(h, p, l);
2784 solv_chksum_free(h, fp);
2785 for (i = 0; i < 20; i++)
2786 sprintf(fpx + i * 2, "%02x", fp[i]);
2787 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2788 memcpy(keyid, fp + 12, 8);
2793 if (p[0] == 3 && p[1] == 5)
2798 // printf("V3 signature packet\n");
2801 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2803 if (!memcmp(keyid, p + 6, 8))
2805 // printf("SELF SIG\n");
2809 // printf("OTHER SIG\n");
2813 htype = REPOKEY_TYPE_MD5;
2814 else if (p[16] == 2)
2815 htype = REPOKEY_TYPE_SHA1;
2816 else if (p[16] == 8)
2817 htype = REPOKEY_TYPE_SHA256;
2820 void *h = solv_chksum_create(htype);
2821 unsigned char b[3], *cs;
2824 b[1] = pubkeyl >> 8;
2826 solv_chksum_add(h, b, 3);
2827 solv_chksum_add(h, pubkey, pubkeyl);
2828 if (p[2] >= 0x10 && p[2] <= 0x13)
2829 solv_chksum_add(h, userid, useridl);
2830 solv_chksum_add(h, p + 2, 5);
2831 cs = solv_chksum_get(h, 0);
2832 solv_chksum_free(h, 0);
2838 int j, ql, haveissuer;
2840 unsigned int ex = 0;
2842 unsigned int scr = 0;
2844 unsigned char issuer[8];
2846 // printf("V4 signature packet\n");
2849 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2854 for (j = 0; q && j < 2; j++)
2861 ql = q[0] << 8 | q[1];
2882 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2893 sl = ((x - 192) << 8) + *q++ + 192;
2902 // printf("%d SIGSUB %d %d\n", j, x, sl);
2903 if (x == 16 && sl == 9 && !haveissuer)
2905 memcpy(issuer, q + 1, 8);
2909 if (x == 2 && j == 0)
2910 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2912 if (x == 9 && j == 0)
2913 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2925 htype = REPOKEY_TYPE_MD5;
2927 htype = REPOKEY_TYPE_SHA1;
2929 htype = REPOKEY_TYPE_SHA256;
2930 if (htype && pubkeyl)
2932 void *h = solv_chksum_create(htype);
2933 unsigned char b[6], *cs;
2937 b[1] = pubkeyl >> 8;
2939 solv_chksum_add(h, b, 3);
2940 solv_chksum_add(h, pubkey, pubkeyl);
2941 if (p[1] >= 0x10 && p[1] <= 0x13)
2944 b[1] = useridl >> 24;
2945 b[2] = useridl >> 16;
2946 b[3] = useridl >> 8;
2948 solv_chksum_add(h, b, 5);
2949 solv_chksum_add(h, userid, useridl);
2951 hl = 6 + (p[4] << 8 | p[5]);
2952 solv_chksum_add(h, p, hl);
2959 solv_chksum_add(h, b, 6);
2960 cs = solv_chksum_get(h, 0);
2961 solv_chksum_free(h, 0);
2964 if (!memcmp(keyid, issuer, 8))
2966 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2972 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2979 userid = solv_realloc(userid, l);
2981 memcpy(userid, p, l);
2988 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2993 /* this is private to rpm, but rpm lacks an interface to retrieve
2994 * the values. Sigh. */
2995 struct pgpDigParams_s {
2996 const char * userid;
2997 const unsigned char * hash;
2998 const char * params[4];
3000 unsigned char version; /*!< version number. */
3001 unsigned char time[4]; /*!< time that the key was created. */
3002 unsigned char pubkey_algo; /*!< public key algorithm. */
3003 unsigned char hash_algo;
3004 unsigned char sigtype;
3005 unsigned char hashlen;
3006 unsigned char signhash16[2];
3007 unsigned char signid[8];
3008 unsigned char saved;
3012 struct pgpDigParams_s signature;
3013 struct pgpDigParams_s pubkey;
3017 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3019 Pool *pool = s->repo->pool;
3020 unsigned char *pkts;
3025 char evrbuf[8 + 1 + 8 + 1];
3027 pkts = unarmor(pubkey, &pktsl);
3030 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3031 parsekeydata(s, data, pkts, pktsl);
3032 /* only rpm knows how to do the release calculation, we don't dare
3033 * to recreate all the bugs */
3037 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3039 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3040 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3041 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]);
3042 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3044 s->name = pool_str2id(pool, "gpg-pubkey", 1);
3045 s->evr = pool_str2id(pool, evrbuf, 1);
3047 for (i = 0; i < 8; i++)
3048 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3049 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3050 if (dig->pubkey.userid)
3051 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3053 (void)pgpFreeDig(dig);
3055 (void)pgpDigFree(dig);
3057 solv_free((void *)pkts);
3062 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
3064 Pool *pool = repo->pool;
3065 struct rpm_by_state state;
3066 struct rpmdbentry *entries;
3068 char *namedata, *str;
3073 data = repo_add_repodata(repo, flags);
3075 memset(&state, 0, sizeof(state));
3076 if (!(state.dbenv = opendbenv(rootdir)))
3078 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3079 for (i = 0 ; i < nentries; i++)
3081 void *statep = &state;
3082 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3085 str = headstring(rpmhead, TAG_DESCRIPTION);
3088 s = pool_id2solvable(pool, repo_add_solvable(repo));
3089 pubkey2solvable(s, data, str);
3090 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3092 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3094 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3095 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3098 solv_free(namedata);
3100 if (!(flags & REPO_NO_INTERNALIZE))
3101 repodata_internalize(data);
3106 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
3108 Pool *pool = repo->pool;
3115 data = repo_add_repodata(repo, flags);
3118 for (i = 0; i < nkeys; i++)
3120 if ((fp = fopen(keys[i], "r")) == 0)
3127 if (bufl - l < 4096)
3130 buf = solv_realloc(buf, bufl);
3132 ll = fread(buf, 1, bufl - l, fp);
3139 s = pool_id2solvable(pool, repo_add_solvable(repo));
3140 pubkey2solvable(s, data, buf);
3143 if (!(flags & REPO_NO_INTERNALIZE))
3144 repodata_internalize(data);
3148 #endif /* ENABLE_RPMDB_PUBKEY */