2 * Copyright (c) 2007, 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 #define RPMDB_COOKIE_VERSION 3
53 #define TAG_VERSION 1001
54 #define TAG_RELEASE 1002
55 #define TAG_EPOCH 1003
56 #define TAG_SUMMARY 1004
57 #define TAG_DESCRIPTION 1005
58 #define TAG_BUILDTIME 1006
59 #define TAG_BUILDHOST 1007
60 #define TAG_INSTALLTIME 1008
62 #define TAG_DISTRIBUTION 1010
63 #define TAG_VENDOR 1011
64 #define TAG_LICENSE 1014
65 #define TAG_PACKAGER 1015
66 #define TAG_GROUP 1016
69 #define TAG_FILESIZES 1028
70 #define TAG_FILEMODES 1030
71 #define TAG_FILEMD5S 1035
72 #define TAG_FILELINKTOS 1036
73 #define TAG_SOURCERPM 1044
74 #define TAG_PROVIDENAME 1047
75 #define TAG_REQUIREFLAGS 1048
76 #define TAG_REQUIRENAME 1049
77 #define TAG_REQUIREVERSION 1050
78 #define TAG_NOSOURCE 1051
79 #define TAG_NOPATCH 1052
80 #define TAG_CONFLICTFLAGS 1053
81 #define TAG_CONFLICTNAME 1054
82 #define TAG_CONFLICTVERSION 1055
83 #define TAG_TRIGGERNAME 1066
84 #define TAG_TRIGGERVERSION 1067
85 #define TAG_TRIGGERFLAGS 1068
86 #define TAG_OBSOLETENAME 1090
87 #define TAG_FILEDEVICES 1095
88 #define TAG_FILEINODES 1096
89 #define TAG_PROVIDEFLAGS 1112
90 #define TAG_PROVIDEVERSION 1113
91 #define TAG_OBSOLETEFLAGS 1114
92 #define TAG_OBSOLETEVERSION 1115
93 #define TAG_DIRINDEXES 1116
94 #define TAG_BASENAMES 1117
95 #define TAG_DIRNAMES 1118
96 #define TAG_PAYLOADFORMAT 1124
97 #define TAG_PATCHESNAME 1133
98 #define TAG_FILECOLORS 1140
99 #define TAG_SUGGESTSNAME 1156
100 #define TAG_SUGGESTSVERSION 1157
101 #define TAG_SUGGESTSFLAGS 1158
102 #define TAG_ENHANCESNAME 1159
103 #define TAG_ENHANCESVERSION 1160
104 #define TAG_ENHANCESFLAGS 1161
106 #define SIGTAG_SIZE 1000
107 #define SIGTAG_PGP 1002 /* RSA signature */
108 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
109 #define SIGTAG_GPG 1005 /* DSA signature */
111 #define DEP_LESS (1 << 1)
112 #define DEP_GREATER (1 << 2)
113 #define DEP_EQUAL (1 << 3)
114 #define DEP_STRONG (1 << 27)
115 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
119 # define RPM_INDEX_SIZE 4
121 # define RPM_INDEX_SIZE 8
129 typedef struct rpmhead {
133 unsigned char data[1];
137 static inline unsigned char *
138 headfindtag(RpmHead *h, int tag)
141 unsigned char *d, taga[4];
147 for (i = 0; i < h->cnt; i++, d -= 16)
148 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
154 headexists(RpmHead *h, int tag)
156 return headfindtag(h, tag) ? 1 : 0;
159 static unsigned int *
160 headint32array(RpmHead *h, int tag, int *cnt)
162 unsigned int i, o, *r;
163 unsigned char *d = headfindtag(h, tag);
165 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
167 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
168 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
169 if (o + 4 * i > h->dcnt)
172 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
175 for (o = 0; o < i; o++, d += 4)
176 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
180 /* returns the first entry of an integer array */
182 headint32(RpmHead *h, int tag)
185 unsigned char *d = headfindtag(h, tag);
187 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
189 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
190 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
191 if (i == 0 || o + 4 * i > h->dcnt)
194 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
197 static unsigned int *
198 headint16array(RpmHead *h, int tag, int *cnt)
200 unsigned int i, o, *r;
201 unsigned char *d = headfindtag(h, tag);
203 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
205 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
206 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
207 if (o + 4 * i > h->dcnt)
210 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
213 for (o = 0; o < i; o++, d += 2)
214 r[o] = d[0] << 8 | d[1];
219 headstring(RpmHead *h, int tag)
222 unsigned char *d = headfindtag(h, tag);
223 /* 6: STRING, 9: I18NSTRING */
224 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
226 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
229 return (char *)h->dp + o;
233 headstringarray(RpmHead *h, int tag, int *cnt)
236 unsigned char *d = headfindtag(h, tag);
239 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
241 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
242 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
243 r = sat_calloc(i ? i : 1, sizeof(char *));
247 for (o = 0; o < i; o++)
251 d += strlen((char *)d) + 1;
252 if (d >= h->dp + h->dcnt)
261 static unsigned char *
262 headbinary(RpmHead *h, int tag, unsigned int *sizep)
265 unsigned char *d = headfindtag(h, tag);
266 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
268 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
269 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
270 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
277 static char *headtoevr(RpmHead *h)
284 version = headstring(h, TAG_VERSION);
285 release = headstring(h, TAG_RELEASE);
286 epoch = headint32(h, TAG_EPOCH);
287 if (!version || !release)
289 fprintf(stderr, "headtoevr: bad rpm header\n");
292 for (v = version; *v >= 0 && *v <= '9'; v++)
294 if (epoch || (v != version && *v == ':'))
296 char epochbuf[11]; /* 32bit decimal will fit in */
297 sprintf(epochbuf, "%u", epoch);
298 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
299 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
303 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
304 sprintf(evr, "%s-%s", version, release);
311 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
313 const unsigned char *cp;
316 unsigned char *buf = 0, *bp;
318 /* check if it's already utf8, code taken from screen ;-) */
319 cp = (const unsigned char *)str;
320 while ((c = *cp++) != 0)
324 if ((c & 0xc0) != 0x80)
325 break; /* encoding error */
326 c = (c & 0x3f) | (state << 6);
327 if (!(state & 0x40000000))
329 /* check for overlong sequences */
330 if ((c & 0x820823e0) == 0x80000000)
332 else if ((c & 0x020821f0) == 0x02000000)
334 else if ((c & 0x000820f8) == 0x00080000)
336 else if ((c & 0x0000207c) == 0x00002000)
346 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
348 c = (c & 0x03) | 0xbfffff00; /* 4 */
350 c = (c & 0x07) | 0xbfffc000; /* 3 */
352 c = (c & 0x0f) | 0xbff00000; /* 2 */
354 c = (c & 0x1f) | 0xfc000000; /* 1 */
358 state = (c & 0x80000000) ? c : 0;
362 /* not utf8, assume latin1 */
363 buf = sat_malloc(2 * strlen(str) + 1);
364 cp = (const unsigned char *)str;
367 while ((c = *cp++) != 0)
380 repodata_set_str(repodata, handle, tag, str);
386 #define MAKEDEPS_FILTER_WEAK (1 << 0)
387 #define MAKEDEPS_FILTER_STRONG (1 << 1)
388 #define MAKEDEPS_NO_RPMLIB (1 << 2)
391 * strong: 0: ignore strongness
392 * 1: filter to strong
396 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
400 int i, cc, nc, vc, fc;
402 unsigned int olddeps;
406 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
407 n = headstringarray(rpmhead, tagn, &nc);
410 v = headstringarray(rpmhead, tagv, &vc);
416 f = headint32array(rpmhead, tagf, &fc);
423 if (nc != vc || nc != fc)
425 fprintf(stderr, "bad dependency entries\n");
430 haspre = 0; /* add no prereq marker */
433 /* we do filtering */
435 for (i = 0; i < nc; i++)
437 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
439 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
440 if (!strncmp(n[i], "rpmlib(", 7))
442 if ((f[i] & DEP_PRE) != 0)
447 else if (tagn == TAG_REQUIRENAME)
449 /* no filtering, just look for the first prereq */
450 for (i = 0; i < nc; i++)
451 if ((f[i] & DEP_PRE) != 0)
465 olddeps = repo_reserve_ids(repo, 0, cc);
466 ida = repo->idarraydata + olddeps;
473 haspre = 2; /* pass two: prereqs */
475 *ida++ = SOLVABLE_PREREQMARKER;
477 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
479 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
481 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
483 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
484 if (!strncmp(n[i], "rpmlib(", 7))
486 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
490 if ((f[i] & DEP_LESS) != 0)
492 if ((f[i] & DEP_EQUAL) != 0)
494 if ((f[i] & DEP_GREATER) != 0)
496 name = str2id(pool, n[i], 1);
497 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
498 evr = str2id(pool, v[i] + 2, 1);
500 evr = str2id(pool, v[i], 1);
501 *ida++ = rel2id(pool, name, evr, flags, 1);
504 *ida++ = str2id(pool, n[i], 1);
507 repo->idarraysize += cc + 1;
515 #ifdef USE_FILEFILTER
517 #define FILEFILTER_EXACT 0
518 #define FILEFILTER_STARTS 1
519 #define FILEFILTER_CONTAINS 2
527 static struct filefilter filefilters[] = {
528 { FILEFILTER_CONTAINS, "/bin/", 0},
529 { FILEFILTER_CONTAINS, "/sbin/", 0},
530 { FILEFILTER_CONTAINS, "/lib/", 0},
531 { FILEFILTER_CONTAINS, "/lib64/", 0},
532 { FILEFILTER_CONTAINS, "/etc/", 0},
533 { FILEFILTER_STARTS, "/usr/games/", 0},
534 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
535 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
536 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
542 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
546 unsigned int *fkb, *fn, *fsz, *fm, *fino;
547 unsigned int inotest[256], inotestok;
551 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
552 if (!fsz || fc != fszc)
557 /* stupid rpm records sizes of directories, so we have to check the mode */
558 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
559 if (!fm || fc != fszc)
565 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
566 if (!fino || fc != fszc)
574 if (fc < sizeof(inotest))
576 memset(inotest, 0, sizeof(inotest));
577 for (i = 0; i < fc; i++)
580 if (fsz[i] == 0 || !S_ISREG(fm[i]))
582 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
583 bit = 1 << (fino[i] & 31);
584 if ((inotest[off] & bit) != 0)
593 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
595 unsigned int mask, hash, hh;
596 if (!fdev || fc != fszc)
605 while ((mask & (mask - 1)) != 0)
606 mask = mask & (mask - 1);
608 if (mask > sizeof(inotest)/sizeof(*inotest))
609 fx = sat_calloc(mask, sizeof(unsigned int));
613 memset(fx, 0, mask * sizeof(unsigned int));
616 for (i = 0; i < fc; i++)
618 if (fsz[i] == 0 || !S_ISREG(fm[i]))
620 hash = (fino[i] + fdev[i] * 31) & mask;
622 while ((j = fx[hash]) != 0)
624 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
626 fsz[i] = 0; /* kill entry */
629 hash = (hash + hh++) & mask;
639 fn = sat_calloc(dc, sizeof(unsigned int));
640 fkb = sat_calloc(dc, sizeof(unsigned int));
641 for (i = 0; i < fc; i++)
646 if (fsz[i] == 0 || !S_ISREG(fm[i]))
648 fkb[di[i]] += fsz[i] / 1024 + 1;
653 handle = s - pool->solvables;
654 for (i = 0; i < dc; i++)
660 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
661 did = repodata_str2dir(data, "/usr/src", 1);
663 continue; /* work around rpm bug */
666 did = repodata_str2dir(data, dn[i], 1);
667 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
673 /* assumes last processed array is provides! */
675 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
682 #ifdef USE_FILEFILTER
684 struct filefilter *ff;
693 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
696 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
702 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
711 fprintf(stderr, "bad filelist\n");
716 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dnc);
718 for (i = 0; i < bnc; i++)
720 #ifdef USE_FILEFILTER
722 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
726 switch (ff->dirmatch)
728 case FILEFILTER_STARTS:
729 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
732 case FILEFILTER_CONTAINS:
733 if (!strstr(dn[di[i]], ff->dir))
736 case FILEFILTER_EXACT:
738 if (strcmp(dn[di[i]], ff->dir))
745 if (strcmp(bn[i], ff->base))
750 if (j == sizeof(filefilters)/sizeof(*filefilters))
754 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
758 fn = sat_realloc(fn, fna);
760 strcpy(fn, dn[di[i]]);
762 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
769 handle = s - pool->solvables;
770 did = repodata_str2dir(data, dn[di[i]], 1);
773 did = repodata_str2dir(data, "/", 1);
774 if (b && b[0] == '/')
775 b++; /* work around rpm bug */
777 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
791 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
793 const char *p, *sevr, *sarch;
795 p = strrchr(sourcerpm, '.');
796 if (!p || strcmp(p, ".rpm") != 0)
799 while (p > sourcerpm && *p != '.')
801 if (*p != '.' || p == sourcerpm)
804 while (p > sourcerpm && *p != '-')
806 if (*p != '-' || p == sourcerpm)
809 while (p > sourcerpm && *p != '-')
811 if (*p != '-' || p == sourcerpm)
814 if (!strcmp(sarch, "src.rpm"))
815 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
816 else if (!strcmp(sarch, "nosrc.rpm"))
817 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
819 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
820 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
821 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
823 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
824 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
825 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
827 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
831 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
837 name = headstring(rpmhead, TAG_NAME);
838 if (!strcmp(name, "gpg-pubkey"))
840 s->name = str2id(pool, name, 1);
843 fprintf(stderr, "package has no name\n");
846 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
848 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
851 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
852 s->arch = ARCH_NOSRC;
857 s->arch = ARCH_NOARCH;
858 evr = headtoevr(rpmhead);
859 s->evr = str2id(pool, evr, 1);
860 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
862 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
863 if ((flags & RPM_ADD_NO_FILELIST) == 0)
864 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
865 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
866 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
867 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
868 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
869 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
871 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
872 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
873 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
874 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
875 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
876 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
884 handle = s - pool->solvables;
885 str = headstring(rpmhead, TAG_SUMMARY);
887 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
888 str = headstring(rpmhead, TAG_DESCRIPTION);
892 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
893 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
897 /* oh my, found SUSE special author section */
902 while (l > 0 && str[l - 1] == '\n')
905 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
907 aut = str; /* copy over */
908 while (*p == ' ' || *p == '\n')
921 while (aut != str && aut[-1] == '\n')
925 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
929 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
931 str = headstring(rpmhead, TAG_GROUP);
933 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
934 str = headstring(rpmhead, TAG_LICENSE);
936 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
937 str = headstring(rpmhead, TAG_URL);
939 repodata_set_str(data, handle, SOLVABLE_URL, str);
940 str = headstring(rpmhead, TAG_DISTRIBUTION);
942 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
943 str = headstring(rpmhead, TAG_PACKAGER);
945 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
946 u32 = headint32(rpmhead, TAG_BUILDTIME);
948 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
949 u32 = headint32(rpmhead, TAG_INSTALLTIME);
951 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
952 u32 = headint32(rpmhead, TAG_SIZE);
954 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
956 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
957 if ((flags & RPM_ADD_TRIGGERS) != 0)
960 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
963 for (; (id = repo->idarraydata[ida]) != 0; ida++)
965 /* we currently do not support rel ids in incore data, so
966 * strip off versioning information */
969 Reldep *rd = GETRELDEP(pool, id);
974 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
984 copyreldep(Pool *pool, Pool *frompool, Id id)
986 Reldep *rd = GETRELDEP(frompool, id);
987 Id name = rd->name, evr = rd->evr;
989 name = copyreldep(pool, frompool, name);
991 name = str2id(pool, id2str(frompool, name), 1);
993 evr = copyreldep(pool, frompool, evr);
995 evr = str2id(pool, id2str(frompool, evr), 1);
996 return rel2id(pool, name, evr, rd->flags, 1);
1000 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1005 Pool *frompool = fromrepo->pool;
1009 from = fromrepo->idarraydata + fromoff;
1010 for (ida = from, cc = 0; *ida; ida++, cc++)
1014 ido = repo_reserve_ids(repo, 0, cc);
1015 ida = repo->idarraydata + ido;
1016 if (frompool && pool != frompool)
1022 id = copyreldep(pool, frompool, id);
1024 id = str2id(pool, id2str(frompool, id), 1);
1030 memcpy(ida, from, (cc + 1) * sizeof(Id));
1031 repo->idarraysize += cc + 1;
1035 #define COPYDIR_DIRCACHE_SIZE 512
1037 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1040 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1042 if (cache && cache[did & 255] == did)
1043 return cache[(did & 255) + 256];
1044 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1048 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1050 Id parent = dirpool_parent(&fromdata->dirpool, did);
1051 Id compid = dirpool_compid(&fromdata->dirpool, did);
1053 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1054 if (fromspool != &pool->ss)
1055 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1056 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1059 cache[did & 255] = did;
1060 cache[(did & 255) + 256] = compid;
1065 struct solvable_copy_cbdata {
1072 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1074 struct solvable_copy_cbdata *cbdata = vcbdata;
1076 Repodata *data = cbdata->data;
1077 Id handle = cbdata->handle;
1078 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1079 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1081 keyname = key->name;
1082 if (keyname >= ID_NUM_INTERNAL)
1083 keyname = str2id(pool, id2str(frompool, keyname), 1);
1086 case REPOKEY_TYPE_ID:
1087 case REPOKEY_TYPE_CONSTANTID:
1089 assert(!data->localpool); /* implement me! */
1090 if (pool != frompool || fromdata->localpool)
1093 id = copyreldep(pool, frompool, id);
1095 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1097 if (key->type == REPOKEY_TYPE_ID)
1098 repodata_set_id(data, handle, keyname, id);
1100 repodata_set_constantid(data, handle, keyname, id);
1102 case REPOKEY_TYPE_STR:
1103 repodata_set_str(data, handle, keyname, kv->str);
1105 case REPOKEY_TYPE_VOID:
1106 repodata_set_void(data, handle, keyname);
1108 case REPOKEY_TYPE_NUM:
1109 repodata_set_num(data, handle, keyname, kv->num);
1111 case REPOKEY_TYPE_CONSTANT:
1112 repodata_set_constant(data, handle, keyname, kv->num);
1114 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1116 assert(!data->localpool); /* implement me! */
1117 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1118 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1120 case REPOKEY_TYPE_DIRSTRARRAY:
1122 assert(!data->localpool); /* implement me! */
1123 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1124 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1133 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1135 Repo *repo = s->repo;
1136 Repo *fromrepo = r->repo;
1137 Pool *pool = repo->pool;
1138 struct solvable_copy_cbdata cbdata;
1140 /* copy solvable data */
1141 if (pool == fromrepo->pool)
1146 s->vendor = r->vendor;
1151 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1153 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1155 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1157 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1159 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1160 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1161 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1162 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1163 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1164 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1165 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1166 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1168 /* copy all attributes */
1172 cbdata.handle = s - pool->solvables;
1173 cbdata.dircache = dircache;
1174 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1177 /* used to sort entries returned in some database order */
1179 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1181 struct rpmid const *a = va, *b = vb;
1183 r = strcmp(a->name, b->name);
1186 return a->dbid - b->dbid;
1190 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1193 Pool *pool = repo->pool;
1194 Solvable *a = pool->solvables + *(Id *)va;
1195 Solvable *b = pool->solvables + *(Id *)vb;
1198 if (a->name != b->name)
1199 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1200 rpmdbid = repo->rpmdbid;
1201 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1205 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1207 Pool *pool = repo->pool;
1210 tmp = pool->solvables[pa];
1211 pool->solvables[pa] = pool->solvables[pb];
1212 pool->solvables[pb] = tmp;
1215 Id tmpid = repo->rpmdbid[pa - repo->start];
1216 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1217 repo->rpmdbid[pb - repo->start] = tmpid;
1219 /* only works if nothing is already internalized! */
1220 if (data && data->attrs)
1222 Id *tmpattrs = data->attrs[pa - data->start];
1223 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1224 data->attrs[pb - data->start] = tmpattrs;
1229 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1231 memset(cookie, 0, 32);
1232 cookie[3] = RPMDB_COOKIE_VERSION;
1233 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1234 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1237 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1239 opendbenv(const char *rootdir)
1241 char dbpath[PATH_MAX];
1245 if (db_env_create(&dbenv, 0))
1247 perror("db_env_create");
1250 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1251 dbenv->set_thread_count(dbenv, 8);
1253 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1254 if (access(dbpath, W_OK) == -1)
1256 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1261 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1263 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1268 perror("dbenv open");
1269 dbenv->close(dbenv, 0);
1277 count_headers(const char *rootdir, DB_ENV *dbenv)
1279 char dbpath[PATH_MAX];
1280 struct stat statbuf;
1288 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1289 if (stat(dbpath, &statbuf))
1291 memset(&dbkey, 0, sizeof(dbkey));
1292 memset(&dbdata, 0, sizeof(dbdata));
1293 if (db_create(&db, dbenv, 0))
1295 perror("db_create");
1298 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1300 perror("db->open Name index");
1303 if (db->get_byteswapped(db, &byteswapped))
1305 perror("db->get_byteswapped");
1308 if (db->cursor(db, NULL, &dbc, 0))
1310 perror("db->cursor");
1313 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1314 count += dbdata.size / RPM_INDEX_SIZE;
1321 * read rpm db as repo
1326 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1328 Pool *pool = repo->pool;
1329 unsigned char buf[16];
1334 unsigned char *dp, *dbidp;
1336 struct rpmid *rpmids, *rp;
1342 unsigned int refmask, h;
1343 char dbpath[PATH_MAX];
1347 struct stat packagesstat;
1348 unsigned char newcookie[32];
1349 const unsigned char *oldcookie = 0;
1350 Id oldcookietype = 0;
1352 int count = 0, done = 0;
1355 now = sat_timems(0);
1356 memset(&dbkey, 0, sizeof(dbkey));
1357 memset(&dbdata, 0, sizeof(dbdata));
1362 data = repo_add_repodata(repo, flags);
1364 if (ref && !(ref->nsolvables && ref->rpmdbid))
1367 if (!(dbenv = opendbenv(rootdir)))
1370 /* XXX: should get ro lock of Packages database! */
1371 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1372 if (stat(dbpath, &packagesstat))
1377 mkrpmdbcookie(&packagesstat, newcookie);
1378 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1381 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1382 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1385 int solvstart = 0, solvend = 0;
1387 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1388 count = count_headers(rootdir, dbenv);
1389 if (db_create(&db, dbenv, 0))
1391 perror("db_create");
1394 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1396 perror("db->open Packages index");
1399 if (db->get_byteswapped(db, &byteswapped))
1401 perror("db->get_byteswapped");
1404 #if defined(RPM5) && !defined(WORDS_BIGENDIAN)
1405 byteswapped = !byteswapped;
1407 if (db->cursor(db, NULL, &dbc, 0))
1409 perror("db->cursor");
1412 dbidp = (unsigned char *)&dbid;
1417 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1421 s = pool_id2solvable(pool, repo_add_solvable(repo));
1423 solvstart = s - pool->solvables;
1424 solvend = s - pool->solvables + 1;
1427 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1428 if (dbkey.size != 4)
1430 fprintf(stderr, "corrupt Packages database (key size)\n");
1442 memcpy(dbidp, dp, 4);
1443 if (dbid == 0) /* the join key */
1445 if (dbdata.size < 8)
1447 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1450 if (dbdata.size > rpmheadsize)
1452 rpmheadsize = dbdata.size + 128;
1453 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1455 memcpy(buf, dbdata.data, 8);
1456 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1457 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1458 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1460 fprintf(stderr, "corrupt rpm database (data size)\n");
1463 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1464 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1465 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1466 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1473 /* We can reuse this solvable, but make sure it's still
1474 associated with this repo. */
1475 memset(s, 0, sizeof(*s));
1478 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1482 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1483 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1488 /* oops, could not reuse. free it instead */
1489 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1496 /* now sort all solvables in the new solvstart..solvend block */
1497 if (solvend - solvstart > 1)
1499 pkgids = sat_malloc2(solvend - solvstart, sizeof(Id));
1500 for (i = solvstart; i < solvend; i++)
1501 pkgids[i - solvstart] = i;
1502 sat_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1504 for (i = solvstart; i < solvend; i++)
1506 int j = pkgids[i - solvstart];
1508 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1510 swap_solvables(repo, data, i, j);
1517 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1519 memset(dircache, 0, sizeof(dircache));
1520 if (db_create(&db, dbenv, 0))
1522 perror("db_create");
1525 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1527 perror("db->open Name index");
1530 if (db->get_byteswapped(db, &byteswapped))
1532 perror("db->get_byteswapped");
1535 #if defined(RPM5) && !defined(WORDS_BIGENDIAN)
1536 byteswapped = !byteswapped;
1538 if (db->cursor(db, NULL, &dbc, 0))
1540 perror("db->cursor");
1543 dbidp = (unsigned char *)&dbid;
1546 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1548 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1552 while(dl >= RPM_INDEX_SIZE)
1562 memcpy(dbidp, dp, 4);
1563 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1564 rpmids[nrpmids].dbid = dbid;
1565 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1566 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1567 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1569 dp += RPM_INDEX_SIZE;
1570 dl -= RPM_INDEX_SIZE;
1578 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1580 dbidp = (unsigned char *)&dbid;
1584 /* create hash from dbid to ref */
1585 refmask = mkmask(ref->nsolvables);
1586 refhash = sat_calloc(refmask + 1, sizeof(Id));
1587 for (i = 0; i < ref->end - ref->start; i++)
1589 if (!ref->rpmdbid[i])
1591 h = ref->rpmdbid[i] & refmask;
1593 h = (h + 317) & refmask;
1594 refhash[h] = i + 1; /* make it non-zero */
1597 /* count the misses, they will cost us time */
1598 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1600 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1606 while ((id = refhash[h]))
1608 if (ref->rpmdbid[id - 1] == dbid)
1610 h = (h + 317) & refmask;
1619 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1621 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1623 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1626 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1630 while ((id = refhash[h]))
1632 if (ref->rpmdbid[id - 1] == dbid)
1634 h = (h + 317) & refmask;
1638 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1641 solvable_copy(s, r, data, dircache);
1648 if (db_create(&db, dbenv, 0))
1650 perror("db_create");
1653 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1655 perror("db->open var/lib/rpm/Packages");
1658 if (db->get_byteswapped(db, &byteswapped))
1660 perror("db->get_byteswapped");
1663 #if defined(RPM5) && !defined(WORDS_BIGENDIAN)
1664 byteswapped = !byteswapped;
1675 memcpy(buf, dbidp, 4);
1680 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1683 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1684 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1687 if (dbdata.size < 8)
1689 fprintf(stderr, "corrupt rpm database (size)\n");
1692 if (dbdata.size > rpmheadsize)
1694 rpmheadsize = dbdata.size + 128;
1695 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1697 memcpy(buf, dbdata.data, 8);
1698 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1699 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1700 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1702 fprintf(stderr, "corrupt rpm database (data size)\n");
1705 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1706 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1708 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1709 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1713 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1714 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1722 for (i = 0; i < nrpmids; i++)
1723 sat_free(rpmids[i].name);
1729 dbenv->close(dbenv, 0);
1732 if (!(flags & REPO_NO_INTERNALIZE))
1733 repodata_internalize(data);
1734 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1735 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1736 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1737 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1738 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1742 static inline unsigned int
1743 getu32(const unsigned char *dp)
1745 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1750 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1752 int i, sigdsize, sigcnt, l;
1753 Pool *pool = repo->pool;
1755 RpmHead *rpmhead = 0;
1756 int rpmheadsize = 0;
1757 char *payloadformat;
1759 unsigned char lead[4096];
1760 int headerstart, headerend;
1763 unsigned char pkgid[16];
1768 data = repo_add_repodata(repo, flags);
1770 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1771 chksumtype = REPOKEY_TYPE_SHA256;
1772 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1773 chksumtype = REPOKEY_TYPE_SHA1;
1774 for (i = 0; i < nrpms; i++)
1776 if ((fp = fopen(rpms[i], "r")) == 0)
1781 if (fstat(fileno(fp), &stb))
1787 chksumh = sat_chksum_free(chksumh, 0);
1789 chksumh = sat_chksum_create(chksumtype);
1790 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1792 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1797 sat_chksum_add(chksumh, lead, 96 + 16);
1798 if (lead[78] != 0 || lead[79] != 5)
1800 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1804 if (getu32(lead + 96) != 0x8eade801)
1806 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1810 sigcnt = getu32(lead + 96 + 8);
1811 sigdsize = getu32(lead + 96 + 12);
1812 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1814 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1818 sigdsize += sigcnt * 16;
1819 sigdsize = (sigdsize + 7) & ~7;
1820 headerstart = 96 + 16 + sigdsize;
1822 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1824 unsigned char *chksum;
1825 unsigned int chksumsize;
1826 /* extract pkgid from the signature header */
1827 if (sigdsize > rpmheadsize)
1829 rpmheadsize = sigdsize + 128;
1830 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1832 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1834 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1839 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1840 rpmhead->cnt = sigcnt;
1841 rpmhead->dcnt = sigdsize - sigcnt * 16;
1842 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1843 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1844 if (chksum && chksumsize == 16)
1847 memcpy(pkgid, chksum, 16);
1852 /* just skip the signature header */
1855 l = sigdsize > 4096 ? 4096 : sigdsize;
1856 if (fread(lead, l, 1, fp) != 1)
1858 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1863 sat_chksum_add(chksumh, lead, l);
1867 if (fread(lead, 16, 1, fp) != 1)
1869 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1874 sat_chksum_add(chksumh, lead, 16);
1875 if (getu32(lead) != 0x8eade801)
1877 fprintf(stderr, "%s: bad header\n", rpms[i]);
1881 sigcnt = getu32(lead + 8);
1882 sigdsize = getu32(lead + 12);
1883 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1885 fprintf(stderr, "%s: bad header\n", rpms[i]);
1889 l = sigdsize + sigcnt * 16;
1890 headerend = headerstart + 16 + l;
1891 if (l > rpmheadsize)
1893 rpmheadsize = l + 128;
1894 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1896 if (fread(rpmhead->data, l, 1, fp) != 1)
1898 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1903 sat_chksum_add(chksumh, rpmhead->data, l);
1904 rpmhead->cnt = sigcnt;
1905 rpmhead->dcnt = sigdsize;
1906 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1907 if (headexists(rpmhead, TAG_PATCHESNAME))
1909 /* this is a patch rpm, ignore */
1913 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1914 if (payloadformat && !strcmp(payloadformat, "drpm"))
1916 /* this is a delta rpm */
1921 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1922 sat_chksum_add(chksumh, lead, l);
1924 s = pool_id2solvable(pool, repo_add_solvable(repo));
1925 rpm2solv(pool, repo, data, s, rpmhead, flags);
1928 Id handle = s - pool->solvables;
1929 repodata_set_location(data, handle, 0, 0, rpms[i]);
1930 if (S_ISREG(stb.st_mode))
1931 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1932 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1934 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1936 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1940 chksumh = sat_chksum_free(chksumh, 0);
1943 if (!(flags & REPO_NO_INTERNALIZE))
1944 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 lastdir;
1987 int cnt, dcnt, cnt2;
1991 char md5[33], *md5p = 0;
1993 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1996 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1998 for (i = 0; i < dcnt; i++)
1999 (*cb)(cbdata, dn[i], 0, (char *)0);
2003 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2009 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2010 if (!di || cnt != cnt2)
2017 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2018 if (!fm || cnt != cnt2)
2026 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2028 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2029 if (!md || cnt != cnt2)
2039 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2041 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2042 if (!co || cnt != cnt2)
2055 for (i = 0; i < cnt; i++)
2060 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2063 l = l1 + strlen(bn[i]) + 1;
2067 space = sat_realloc(space, spacen);
2069 if (lastdir != diidx)
2071 strcpy(space, dn[diidx]);
2075 strcpy(space + l1, bn[i]);
2084 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2090 linkhash(lt[i], md5);
2096 sprintf(md5, "%08x%08x", (fm[i] >> 12) & 65535, 0);
2100 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2113 rpm_query(void *rpmhandle, Id what)
2115 const char *name, *arch, *sourcerpm;
2119 RpmHead *rpmhead = rpmhandle;
2124 name = headstring(rpmhead, TAG_NAME);
2127 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2129 arch = headstring(rpmhead, TAG_ARCH);
2132 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2139 evr = headtoevr(rpmhead);
2140 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2142 sprintf(r, "%s-%s.%s", name, evr, arch);
2146 name = headstring(rpmhead, TAG_NAME);
2150 r = headtoevr(rpmhead);
2157 struct rpm_by_state {
2172 #define ENTRIES_BLOCK 255
2173 #define NAMEDATA_BLOCK 1023
2175 static struct rpmdbentry *
2176 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2190 struct rpmdbentry *entries = 0;
2196 dbenv = state->dbenv;
2197 if (db_create(&db, dbenv, 0))
2199 perror("db_create");
2202 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2204 perror("db->open index");
2208 if (db->get_byteswapped(db, &byteswapped))
2210 perror("db->get_byteswapped");
2214 #if defined(RPM5) && !defined(WORDS_BIGENDIAN)
2215 byteswapped = !byteswapped;
2217 if (db->cursor(db, NULL, &dbc, 0))
2219 perror("db->cursor");
2223 memset(&dbkey, 0, sizeof(dbkey));
2224 memset(&dbdata, 0, sizeof(dbdata));
2227 dbkey.data = (void *)match;
2228 dbkey.size = strlen(match);
2230 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2232 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2236 while(dl >= RPM_INDEX_SIZE)
2240 ((char *)&rpmdbid)[0] = dp[3];
2241 ((char *)&rpmdbid)[1] = dp[2];
2242 ((char *)&rpmdbid)[2] = dp[1];
2243 ((char *)&rpmdbid)[3] = dp[0];
2246 memcpy((char *)&rpmdbid, dp, 4);
2247 entries = sat_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2248 entries[nentries].rpmdbid = rpmdbid;
2249 entries[nentries].nameoff = namedatal;
2251 namedata = sat_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2252 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2253 namedata[namedatal + dbkey.size] = 0;
2254 namedatal += dbkey.size + 1;
2255 dp += RPM_INDEX_SIZE;
2256 dl -= RPM_INDEX_SIZE;
2263 *nentriesp = nentries;
2264 *namedatap = namedata;
2269 freestate(struct rpm_by_state *state)
2275 state->db->close(state->db, 0);
2277 state->dbenv->close(state->dbenv, 0);
2278 sat_free(state->rpmhead);
2282 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2284 struct rpm_by_state state;
2285 struct rpmdbentry *entries;
2292 queue_empty(rpmdbidq);
2293 memset(&state, 0, sizeof(state));
2294 if (!(state.dbenv = opendbenv(rootdir)))
2296 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2298 for (i = 0; i < nentries; i++)
2299 queue_push(rpmdbidq, entries[i].rpmdbid);
2307 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2309 struct rpm_by_state *state = *statep;
2310 unsigned char buf[16];
2320 *statep = (void *)0;
2326 state = sat_calloc(1, sizeof(*state));
2329 if (!state->dbopened)
2331 state->dbopened = 1;
2332 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2334 if (db_create(&state->db, state->dbenv, 0))
2336 perror("db_create");
2338 state->dbenv->close(state->dbenv, 0);
2342 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2344 perror("db->open var/lib/rpm/Packages");
2345 state->db->close(state->db, 0);
2347 state->dbenv->close(state->dbenv, 0);
2351 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2353 perror("db->get_byteswapped");
2354 state->db->close(state->db, 0);
2356 state->dbenv->close(state->dbenv, 0);
2360 #if defined(RPM5) && !defined(WORDS_BIGENDIAN)
2361 state->byteswapped = !state->byteswapped;
2364 memcpy(buf, &rpmdbid, 4);
2365 if (state->byteswapped)
2368 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2369 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2371 memset(&dbkey, 0, sizeof(dbkey));
2372 memset(&dbdata, 0, sizeof(dbdata));
2377 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2382 if (dbdata.size < 8)
2384 fprintf(stderr, "corrupt rpm database (size)\n");
2387 if (dbdata.size > state->rpmheadsize)
2389 state->rpmheadsize = dbdata.size + 128;
2390 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2392 rpmhead = state->rpmhead;
2393 memcpy(buf, dbdata.data, 8);
2394 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2395 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2396 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2398 fprintf(stderr, "corrupt rpm database (data size)\n");
2401 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2402 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2407 rpm_byfp(FILE *fp, const char *name, void **statep)
2409 struct rpm_by_state *state = *statep;
2410 int headerstart, headerend;
2412 int sigdsize, sigcnt, l;
2413 unsigned char lead[4096];
2416 return rpm_byrpmdbid(0, 0, statep);
2419 state = sat_calloc(1, sizeof(*state));
2422 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2424 fprintf(stderr, "%s: not a rpm\n", name);
2427 if (lead[78] != 0 || lead[79] != 5)
2429 fprintf(stderr, "%s: not a V5 header\n", name);
2432 if (getu32(lead + 96) != 0x8eade801)
2434 fprintf(stderr, "%s: bad signature header\n", name);
2437 sigcnt = getu32(lead + 96 + 8);
2438 sigdsize = getu32(lead + 96 + 12);
2439 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2441 fprintf(stderr, "%s: bad signature header\n", name);
2444 sigdsize += sigcnt * 16;
2445 sigdsize = (sigdsize + 7) & ~7;
2446 headerstart = 96 + 16 + sigdsize;
2449 l = sigdsize > 4096 ? 4096 : sigdsize;
2450 if (fread(lead, l, 1, fp) != 1)
2452 fprintf(stderr, "%s: unexpected EOF\n", name);
2457 if (fread(lead, 16, 1, fp) != 1)
2459 fprintf(stderr, "%s: unexpected EOF\n", name);
2462 if (getu32(lead) != 0x8eade801)
2464 fprintf(stderr, "%s: bad header\n", name);
2468 sigcnt = getu32(lead + 8);
2469 sigdsize = getu32(lead + 12);
2470 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2472 fprintf(stderr, "%s: bad header\n", name);
2476 l = sigdsize + sigcnt * 16;
2477 headerend = headerstart + 16 + l;
2478 if (l > state->rpmheadsize)
2480 state->rpmheadsize = l + 128;
2481 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2483 rpmhead = state->rpmhead;
2484 if (fread(rpmhead->data, l, 1, fp) != 1)
2486 fprintf(stderr, "%s: unexpected EOF\n", name);
2490 rpmhead->cnt = sigcnt;
2491 rpmhead->dcnt = sigdsize;
2492 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2497 rpm_byrpmh(Header h, void **statep)
2499 struct rpm_by_state *state = *statep;
2500 const unsigned char *uh;
2501 int sigdsize, sigcnt, l;
2505 uh = headerUnload(h);
2507 uh = headerUnload(h, NULL);
2511 sigcnt = getu32(uh);
2512 sigdsize = getu32(uh + 4);
2513 l = sigdsize + sigcnt * 16;
2516 state = sat_calloc(1, sizeof(*state));
2519 if (l > state->rpmheadsize)
2521 state->rpmheadsize = l + 128;
2522 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2524 rpmhead = state->rpmhead;
2525 memcpy(rpmhead->data, uh + 8, l - 8);
2527 rpmhead->cnt = sigcnt;
2528 rpmhead->dcnt = sigdsize;
2529 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2535 r64dec1(char *p, unsigned int *vp, int *eofp)
2540 for (i = 0; i < 4; )
2545 if (x >= 'A' && x <= 'Z')
2547 else if (x >= 'a' && x <= 'z')
2549 else if (x >= '0' && x <= '9')
2576 crc24(unsigned char *p, int len)
2578 unsigned int crc = 0xb704ceL;
2583 crc ^= (*p++) << 16;
2584 for (i = 0; i < 8; i++)
2585 if ((crc <<= 1) & 0x1000000)
2588 return crc & 0xffffffL;
2591 static unsigned char *
2592 unarmor(char *pubkey, int *pktlp)
2596 unsigned char *buf, *bp;
2600 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2602 pubkey = strchr(pubkey, '\n');
2607 pubkey = strchr(pubkey, '\n');
2610 /* skip header lines */
2613 while (*pubkey == ' ' || *pubkey == '\t')
2615 if (*pubkey == '\n')
2617 pubkey = strchr(pubkey, '\n');
2622 p = strchr(pubkey, '=');
2626 bp = buf = sat_malloc(l * 3 / 4 + 4);
2630 pubkey = r64dec1(pubkey, &v, &eof);
2640 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2643 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2648 if (v != crc24(buf, bp - buf))
2653 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2655 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2665 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2668 unsigned char keyid[8];
2669 unsigned int kcr = 0, maxex = 0;
2670 unsigned char *pubkey = 0;
2672 unsigned char *userid = 0;
2675 for (; pl; p += l, pl -= l)
2679 if (!(x & 128) || pl <= 0)
2684 tag = (x & 0x3c) >> 2;
2706 else if (x >= 192 && x < 224)
2710 l = ((x - 192) << 8) + *p++ + 192;
2717 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2728 pubkey = sat_realloc(pubkey, l);
2730 memcpy(pubkey, p, l);
2737 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2741 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2745 memset(keyid, 0, 8);
2746 if (p[7] == 1) /* RSA */
2749 unsigned char fp[16];
2753 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2754 memcpy(keyid, p + 10 + ql - 8, 8);
2755 h = sat_chksum_create(REPOKEY_TYPE_MD5);
2756 sat_chksum_add(h, p + 10, ql);
2758 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2759 sat_chksum_add(h, q + 2, ql);
2760 sat_chksum_free(h, fp);
2761 for (i = 0; i < 16; i++)
2762 sprintf(fpx + i * 2, "%02x", fp[i]);
2763 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2770 unsigned char hdr[3];
2771 unsigned char fp[20];
2774 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2778 h = sat_chksum_create(REPOKEY_TYPE_SHA1);
2779 sat_chksum_add(h, hdr, 3);
2780 sat_chksum_add(h, p, l);
2781 sat_chksum_free(h, fp);
2782 for (i = 0; i < 20; i++)
2783 sprintf(fpx + i * 2, "%02x", fp[i]);
2784 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2785 memcpy(keyid, fp + 12, 8);
2790 if (p[0] == 3 && p[1] == 5)
2795 // printf("V3 signature packet\n");
2798 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2800 if (!memcmp(keyid, p + 6, 8))
2802 // printf("SELF SIG\n");
2806 // printf("OTHER SIG\n");
2810 htype = REPOKEY_TYPE_MD5;
2811 else if (p[16] == 2)
2812 htype = REPOKEY_TYPE_SHA1;
2813 else if (p[16] == 8)
2814 htype = REPOKEY_TYPE_SHA256;
2817 void *h = sat_chksum_create(htype);
2818 unsigned char b[3], *cs;
2821 b[1] = pubkeyl >> 8;
2823 sat_chksum_add(h, b, 3);
2824 sat_chksum_add(h, pubkey, pubkeyl);
2825 if (p[2] >= 0x10 && p[2] <= 0x13)
2826 sat_chksum_add(h, userid, useridl);
2827 sat_chksum_add(h, p + 2, 5);
2828 cs = sat_chksum_get(h, 0);
2829 sat_chksum_free(h, 0);
2835 int j, ql, haveissuer;
2837 unsigned int ex = 0, scr = 0;
2838 unsigned char issuer[8];
2840 // printf("V4 signature packet\n");
2843 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2848 for (j = 0; q && j < 2; j++)
2855 ql = q[0] << 8 | q[1];
2876 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2887 sl = ((x - 192) << 8) + *q++ + 192;
2896 // printf("%d SIGSUB %d %d\n", j, x, sl);
2897 if (x == 16 && sl == 9 && !haveissuer)
2899 memcpy(issuer, q + 1, 8);
2902 if (x == 2 && j == 0)
2903 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2904 if (x == 9 && j == 0)
2905 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2917 htype = REPOKEY_TYPE_MD5;
2919 htype = REPOKEY_TYPE_SHA1;
2921 htype = REPOKEY_TYPE_SHA256;
2922 if (htype && pubkeyl)
2924 void *h = sat_chksum_create(htype);
2925 unsigned char b[6], *cs;
2929 b[1] = pubkeyl >> 8;
2931 sat_chksum_add(h, b, 3);
2932 sat_chksum_add(h, pubkey, pubkeyl);
2933 if (p[1] >= 0x10 && p[1] <= 0x13)
2936 b[1] = useridl >> 24;
2937 b[2] = useridl >> 16;
2938 b[3] = useridl >> 8;
2940 sat_chksum_add(h, b, 5);
2941 sat_chksum_add(h, userid, useridl);
2943 hl = 6 + (p[4] << 8 | p[5]);
2944 sat_chksum_add(h, p, hl);
2951 sat_chksum_add(h, b, 6);
2952 cs = sat_chksum_get(h, 0);
2953 sat_chksum_free(h, 0);
2956 if (!memcmp(keyid, issuer, 8))
2958 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2964 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2971 userid = sat_realloc(userid, l);
2973 memcpy(userid, p, l);
2978 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2983 /* this is private to rpm, but rpm lacks an interface to retrieve
2984 * the values. Sigh. */
2985 struct pgpDigParams_s {
2986 const char * userid;
2987 const unsigned char * hash;
2988 const char * params[4];
2990 unsigned char version; /*!< version number. */
2991 unsigned char time[4]; /*!< time that the key was created. */
2992 unsigned char pubkey_algo; /*!< public key algorithm. */
2993 unsigned char hash_algo;
2994 unsigned char sigtype;
2995 unsigned char hashlen;
2996 unsigned char signhash16[2];
2997 unsigned char signid[8];
2998 unsigned char saved;
3002 struct pgpDigParams_s signature;
3003 struct pgpDigParams_s pubkey;
3007 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
3009 Pool *pool = s->repo->pool;
3010 unsigned char *pkts;
3015 char evrbuf[8 + 1 + 8 + 1];
3017 pkts = unarmor(pubkey, &pktsl);
3020 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
3021 parsekeydata(s, data, pkts, pktsl);
3022 /* only rpm knows how to do the release calculation, we don't dare
3023 * to recreate all the bugs */
3027 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
3029 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
3030 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
3031 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]);
3032 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
3034 s->name = str2id(pool, "gpg-pubkey", 1);
3035 s->evr = str2id(pool, evrbuf, 1);
3037 for (i = 0; i < 8; i++)
3038 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
3039 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
3040 if (dig->pubkey.userid)
3041 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
3042 (void)pgpFreeDig(dig);
3043 sat_free((void *)pkts);
3048 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
3050 Pool *pool = repo->pool;
3051 struct rpm_by_state state;
3052 struct rpmdbentry *entries;
3054 char *namedata, *str;
3059 data = repo_add_repodata(repo, flags);
3061 memset(&state, 0, sizeof(state));
3062 if (!(state.dbenv = opendbenv(rootdir)))
3064 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
3065 for (i = 0 ; i < nentries; i++)
3067 void *statep = &state;
3068 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3071 str = headstring(rpmhead, TAG_DESCRIPTION);
3074 s = pool_id2solvable(pool, repo_add_solvable(repo));
3075 pubkey2solvable(s, data, str);
3076 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3078 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3080 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3081 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3086 if (!(flags & REPO_NO_INTERNALIZE))
3087 repodata_internalize(data);
3091 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
3093 Pool *pool = repo->pool;
3100 data = repo_add_repodata(repo, flags);
3103 for (i = 0; i < nkeys; i++)
3105 if ((fp = fopen(keys[i], "r")) == 0)
3112 if (bufl - l < 4096)
3115 buf = sat_realloc(buf, bufl);
3117 ll = fread(buf, 1, bufl - l, fp);
3124 s = pool_id2solvable(pool, repo_add_solvable(repo));
3125 pubkey2solvable(s, data, buf);
3128 if (!(flags & REPO_NO_INTERNALIZE))
3129 repodata_internalize(data);