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>
25 #include <rpm/rpmio.h>
26 #include <rpm/rpmpgp.h>
27 #include <rpm/header.h>
28 #include <rpm/rpmdb.h>
44 #include "repo_rpmdb.h"
46 /* 3: added triggers */
47 #define RPMDB_COOKIE_VERSION 3
50 #define TAG_VERSION 1001
51 #define TAG_RELEASE 1002
52 #define TAG_EPOCH 1003
53 #define TAG_SUMMARY 1004
54 #define TAG_DESCRIPTION 1005
55 #define TAG_BUILDTIME 1006
56 #define TAG_BUILDHOST 1007
57 #define TAG_INSTALLTIME 1008
59 #define TAG_DISTRIBUTION 1010
60 #define TAG_VENDOR 1011
61 #define TAG_LICENSE 1014
62 #define TAG_PACKAGER 1015
63 #define TAG_GROUP 1016
66 #define TAG_FILESIZES 1028
67 #define TAG_FILEMODES 1030
68 #define TAG_FILEMD5S 1035
69 #define TAG_FILELINKTOS 1036
70 #define TAG_SOURCERPM 1044
71 #define TAG_PROVIDENAME 1047
72 #define TAG_REQUIREFLAGS 1048
73 #define TAG_REQUIRENAME 1049
74 #define TAG_REQUIREVERSION 1050
75 #define TAG_NOSOURCE 1051
76 #define TAG_NOPATCH 1052
77 #define TAG_CONFLICTFLAGS 1053
78 #define TAG_CONFLICTNAME 1054
79 #define TAG_CONFLICTVERSION 1055
80 #define TAG_TRIGGERNAME 1066
81 #define TAG_TRIGGERVERSION 1067
82 #define TAG_TRIGGERFLAGS 1068
83 #define TAG_OBSOLETENAME 1090
84 #define TAG_FILEDEVICES 1095
85 #define TAG_FILEINODES 1096
86 #define TAG_PROVIDEFLAGS 1112
87 #define TAG_PROVIDEVERSION 1113
88 #define TAG_OBSOLETEFLAGS 1114
89 #define TAG_OBSOLETEVERSION 1115
90 #define TAG_DIRINDEXES 1116
91 #define TAG_BASENAMES 1117
92 #define TAG_DIRNAMES 1118
93 #define TAG_PAYLOADFORMAT 1124
94 #define TAG_PATCHESNAME 1133
95 #define TAG_FILECOLORS 1140
96 #define TAG_SUGGESTSNAME 1156
97 #define TAG_SUGGESTSVERSION 1157
98 #define TAG_SUGGESTSFLAGS 1158
99 #define TAG_ENHANCESNAME 1159
100 #define TAG_ENHANCESVERSION 1160
101 #define TAG_ENHANCESFLAGS 1161
103 #define SIGTAG_SIZE 1000
104 #define SIGTAG_PGP 1002 /* RSA signature */
105 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
106 #define SIGTAG_GPG 1005 /* DSA signature */
108 #define DEP_LESS (1 << 1)
109 #define DEP_GREATER (1 << 2)
110 #define DEP_EQUAL (1 << 3)
111 #define DEP_STRONG (1 << 27)
112 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
120 typedef struct rpmhead {
124 unsigned char data[1];
128 static inline unsigned char *
129 headfindtag(RpmHead *h, int tag)
132 unsigned char *d, taga[4];
138 for (i = 0; i < h->cnt; i++, d -= 16)
139 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
145 headexists(RpmHead *h, int tag)
147 return headfindtag(h, tag) ? 1 : 0;
150 static unsigned int *
151 headint32array(RpmHead *h, int tag, int *cnt)
153 unsigned int i, o, *r;
154 unsigned char *d = headfindtag(h, tag);
156 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
158 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
159 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
160 if (o + 4 * i > h->dcnt)
163 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
166 for (o = 0; o < i; o++, d += 4)
167 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
171 /* returns the first entry of an integer array */
173 headint32(RpmHead *h, int tag)
176 unsigned char *d = headfindtag(h, tag);
178 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
180 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
181 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
182 if (i == 0 || o + 4 * i > h->dcnt)
185 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
188 static unsigned int *
189 headint16array(RpmHead *h, int tag, int *cnt)
191 unsigned int i, o, *r;
192 unsigned char *d = headfindtag(h, tag);
194 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
196 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
197 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
198 if (o + 4 * i > h->dcnt)
201 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
204 for (o = 0; o < i; o++, d += 2)
205 r[o] = d[0] << 8 | d[1];
210 headstring(RpmHead *h, int tag)
213 unsigned char *d = headfindtag(h, tag);
214 /* 6: STRING, 9: I18NSTRING */
215 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
217 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
220 return (char *)h->dp + o;
224 headstringarray(RpmHead *h, int tag, int *cnt)
227 unsigned char *d = headfindtag(h, tag);
230 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
232 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
233 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
234 r = sat_calloc(i ? i : 1, sizeof(char *));
238 for (o = 0; o < i; o++)
242 d += strlen((char *)d) + 1;
243 if (d >= h->dp + h->dcnt)
252 static unsigned char *
253 headbinary(RpmHead *h, int tag, unsigned int *sizep)
256 unsigned char *d = headfindtag(h, tag);
257 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
259 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
260 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
261 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
268 static char *headtoevr(RpmHead *h)
275 version = headstring(h, TAG_VERSION);
276 release = headstring(h, TAG_RELEASE);
277 epoch = headint32(h, TAG_EPOCH);
278 if (!version || !release)
280 fprintf(stderr, "headtoevr: bad rpm header\n");
283 for (v = version; *v >= 0 && *v <= '9'; v++)
285 if (epoch || (v != version && *v == ':'))
287 char epochbuf[11]; /* 32bit decimal will fit in */
288 sprintf(epochbuf, "%u", epoch);
289 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
290 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
294 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
295 sprintf(evr, "%s-%s", version, release);
302 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
304 const unsigned char *cp;
307 unsigned char *buf = 0, *bp;
309 /* check if it's already utf8, code taken from screen ;-) */
310 cp = (const unsigned char *)str;
311 while ((c = *cp++) != 0)
315 if ((c & 0xc0) != 0x80)
316 break; /* encoding error */
317 c = (c & 0x3f) | (state << 6);
318 if (!(state & 0x40000000))
320 /* check for overlong sequences */
321 if ((c & 0x820823e0) == 0x80000000)
323 else if ((c & 0x020821f0) == 0x02000000)
325 else if ((c & 0x000820f8) == 0x00080000)
327 else if ((c & 0x0000207c) == 0x00002000)
337 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
339 c = (c & 0x03) | 0xbfffff00; /* 4 */
341 c = (c & 0x07) | 0xbfffc000; /* 3 */
343 c = (c & 0x0f) | 0xbff00000; /* 2 */
345 c = (c & 0x1f) | 0xfc000000; /* 1 */
349 state = (c & 0x80000000) ? c : 0;
353 /* not utf8, assume latin1 */
354 buf = sat_malloc(2 * strlen(str) + 1);
355 cp = (const unsigned char *)str;
358 while ((c = *cp++) != 0)
371 repodata_set_str(repodata, handle, tag, str);
377 #define MAKEDEPS_FILTER_WEAK (1 << 0)
378 #define MAKEDEPS_FILTER_STRONG (1 << 1)
379 #define MAKEDEPS_NO_RPMLIB (1 << 2)
382 * strong: 0: ignore strongness
383 * 1: filter to strong
387 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
391 int i, cc, nc, vc, fc;
393 unsigned int olddeps;
397 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
398 n = headstringarray(rpmhead, tagn, &nc);
401 v = headstringarray(rpmhead, tagv, &vc);
407 f = headint32array(rpmhead, tagf, &fc);
414 if (nc != vc || nc != fc)
416 fprintf(stderr, "bad dependency entries\n");
421 haspre = 0; /* add no prereq marker */
424 /* we do filtering */
426 for (i = 0; i < nc; i++)
428 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
430 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
431 if (!strncmp(n[i], "rpmlib(", 7))
433 if ((f[i] & DEP_PRE) != 0)
438 else if (tagn == TAG_REQUIRENAME)
440 /* no filtering, just look for the first prereq */
441 for (i = 0; i < nc; i++)
442 if ((f[i] & DEP_PRE) != 0)
456 olddeps = repo_reserve_ids(repo, 0, cc);
457 ida = repo->idarraydata + olddeps;
464 haspre = 2; /* pass two: prereqs */
466 *ida++ = SOLVABLE_PREREQMARKER;
468 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
470 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
472 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
474 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
475 if (!strncmp(n[i], "rpmlib(", 7))
477 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
481 if ((f[i] & DEP_LESS) != 0)
483 if ((f[i] & DEP_EQUAL) != 0)
485 if ((f[i] & DEP_GREATER) != 0)
487 name = str2id(pool, n[i], 1);
488 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
489 evr = str2id(pool, v[i] + 2, 1);
491 evr = str2id(pool, v[i], 1);
492 *ida++ = rel2id(pool, name, evr, flags, 1);
495 *ida++ = str2id(pool, n[i], 1);
498 repo->idarraysize += cc + 1;
506 #ifdef USE_FILEFILTER
508 #define FILEFILTER_EXACT 0
509 #define FILEFILTER_STARTS 1
510 #define FILEFILTER_CONTAINS 2
518 static struct filefilter filefilters[] = {
519 { FILEFILTER_CONTAINS, "/bin/", 0},
520 { FILEFILTER_CONTAINS, "/sbin/", 0},
521 { FILEFILTER_CONTAINS, "/lib/", 0},
522 { FILEFILTER_CONTAINS, "/lib64/", 0},
523 { FILEFILTER_CONTAINS, "/etc/", 0},
524 { FILEFILTER_STARTS, "/usr/games/", 0},
525 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
526 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
527 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
533 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
537 unsigned int *fkb, *fn, *fsz, *fm, *fino;
538 unsigned int inotest[256], inotestok;
542 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
543 if (!fsz || fc != fszc)
548 /* stupid rpm records sizes of directories, so we have to check the mode */
549 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
550 if (!fm || fc != fszc)
556 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
557 if (!fino || fc != fszc)
565 if (fc < sizeof(inotest))
567 memset(inotest, 0, sizeof(inotest));
568 for (i = 0; i < fc; i++)
571 if (fsz[i] == 0 || !S_ISREG(fm[i]))
573 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
574 bit = 1 << (fino[i] & 31);
575 if ((inotest[off] & bit) != 0)
584 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
586 unsigned int mask, hash, hh;
587 if (!fdev || fc != fszc)
596 while ((mask & (mask - 1)) != 0)
597 mask = mask & (mask - 1);
599 if (mask > sizeof(inotest)/sizeof(*inotest))
600 fx = sat_calloc(mask, sizeof(unsigned int));
604 memset(fx, 0, mask * sizeof(unsigned int));
607 for (i = 0; i < fc; i++)
609 if (fsz[i] == 0 || !S_ISREG(fm[i]))
611 hash = (fino[i] + fdev[i] * 31) & mask;
613 while ((j = fx[hash]) != 0)
615 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
617 fsz[i] = 0; /* kill entry */
620 hash = (hash + hh++) & mask;
630 fn = sat_calloc(dic, sizeof(unsigned int));
631 fkb = sat_calloc(dic, sizeof(unsigned int));
632 for (i = 0; i < fc; i++)
637 if (fsz[i] == 0 || !S_ISREG(fm[i]))
639 fkb[di[i]] += fsz[i] / 1024 + 1;
644 handle = s - pool->solvables;
645 for (i = 0; i < fc; i++)
649 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
650 did = repodata_str2dir(data, "/usr/src", 1);
652 did = repodata_str2dir(data, dn[i], 1);
653 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
659 /* assumes last processed array is provides! */
661 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
668 #ifdef USE_FILEFILTER
670 struct filefilter *ff;
679 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
682 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
688 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
697 fprintf(stderr, "bad filelist\n");
702 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
704 for (i = 0; i < bnc; i++)
706 #ifdef USE_FILEFILTER
708 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
712 switch (ff->dirmatch)
714 case FILEFILTER_STARTS:
715 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
718 case FILEFILTER_CONTAINS:
719 if (!strstr(dn[di[i]], ff->dir))
722 case FILEFILTER_EXACT:
724 if (strcmp(dn[di[i]], ff->dir))
731 if (strcmp(bn[i], ff->base))
736 if (j == sizeof(filefilters)/sizeof(*filefilters))
740 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
744 fn = sat_realloc(fn, fna);
746 strcpy(fn, dn[di[i]]);
748 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
753 handle = s - pool->solvables;
754 did = repodata_str2dir(data, dn[di[i]], 1);
756 did = repodata_str2dir(data, "/", 1);
757 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
771 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
773 const char *p, *sevr, *sarch;
775 p = strrchr(sourcerpm, '.');
776 if (!p || strcmp(p, ".rpm") != 0)
779 while (p > sourcerpm && *p != '.')
781 if (*p != '.' || p == sourcerpm)
784 while (p > sourcerpm && *p != '-')
786 if (*p != '-' || p == sourcerpm)
789 while (p > sourcerpm && *p != '-')
791 if (*p != '-' || p == sourcerpm)
794 if (!strcmp(sarch, "src.rpm"))
795 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
796 else if (!strcmp(sarch, "nosrc.rpm"))
797 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
799 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
800 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
801 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
803 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
804 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
805 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
807 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
811 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
817 name = headstring(rpmhead, TAG_NAME);
818 if (!strcmp(name, "gpg-pubkey"))
820 s->name = str2id(pool, name, 1);
823 fprintf(stderr, "package has no name\n");
826 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
828 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
831 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
832 s->arch = ARCH_NOSRC;
837 s->arch = ARCH_NOARCH;
838 evr = headtoevr(rpmhead);
839 s->evr = str2id(pool, evr, 1);
840 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
842 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
843 if ((flags & RPM_ADD_NO_FILELIST) == 0)
844 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
845 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
846 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
847 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
848 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
849 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
851 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
852 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
853 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
854 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
855 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
856 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
864 handle = s - pool->solvables;
865 str = headstring(rpmhead, TAG_SUMMARY);
867 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
868 str = headstring(rpmhead, TAG_DESCRIPTION);
872 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
873 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
877 /* oh my, found SUSE special author section */
882 while (l > 0 && str[l - 1] == '\n')
885 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
887 aut = str; /* copy over */
888 while (*p == ' ' || *p == '\n')
901 while (aut != str && aut[-1] == '\n')
905 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
909 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
911 str = headstring(rpmhead, TAG_GROUP);
913 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
914 str = headstring(rpmhead, TAG_LICENSE);
916 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
917 str = headstring(rpmhead, TAG_URL);
919 repodata_set_str(data, handle, SOLVABLE_URL, str);
920 str = headstring(rpmhead, TAG_DISTRIBUTION);
922 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
923 str = headstring(rpmhead, TAG_PACKAGER);
925 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
926 u32 = headint32(rpmhead, TAG_BUILDTIME);
928 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
929 u32 = headint32(rpmhead, TAG_INSTALLTIME);
931 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
932 u32 = headint32(rpmhead, TAG_SIZE);
934 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
936 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
937 if ((flags & RPM_ADD_TRIGGERS) != 0)
940 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
943 for (; (id = repo->idarraydata[ida]) != 0; ida++)
945 /* we currently do not support rel ids in incore data, so
946 * strip off versioning information */
949 Reldep *rd = GETRELDEP(pool, id);
954 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
964 copyreldep(Pool *pool, Pool *frompool, Id id)
966 Reldep *rd = GETRELDEP(frompool, id);
967 Id name = rd->name, evr = rd->evr;
969 name = copyreldep(pool, frompool, name);
971 name = str2id(pool, id2str(frompool, name), 1);
973 evr = copyreldep(pool, frompool, evr);
975 evr = str2id(pool, id2str(frompool, evr), 1);
976 return rel2id(pool, name, evr, rd->flags, 1);
980 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
985 Pool *frompool = fromrepo->pool;
989 from = fromrepo->idarraydata + fromoff;
990 for (ida = from, cc = 0; *ida; ida++, cc++)
994 ido = repo_reserve_ids(repo, 0, cc);
995 ida = repo->idarraydata + ido;
996 if (frompool && pool != frompool)
1002 id = copyreldep(pool, frompool, id);
1004 id = str2id(pool, id2str(frompool, id), 1);
1010 memcpy(ida, from, (cc + 1) * sizeof(Id));
1011 repo->idarraysize += cc + 1;
1015 #define COPYDIR_DIRCACHE_SIZE 512
1017 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
1020 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1022 if (cache && cache[did & 255] == did)
1023 return cache[(did & 255) + 256];
1024 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
1028 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
1030 Id parent = dirpool_parent(&fromdata->dirpool, did);
1031 Id compid = dirpool_compid(&fromdata->dirpool, did);
1033 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1034 if (fromspool != &pool->ss)
1035 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1036 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1039 cache[did & 255] = did;
1040 cache[(did & 255) + 256] = compid;
1045 struct solvable_copy_cbdata {
1052 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1054 struct solvable_copy_cbdata *cbdata = vcbdata;
1056 Repodata *data = cbdata->data;
1057 Id handle = cbdata->handle;
1058 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1059 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1061 keyname = key->name;
1062 if (keyname >= ID_NUM_INTERNAL)
1063 keyname = str2id(pool, id2str(frompool, keyname), 1);
1066 case REPOKEY_TYPE_ID:
1067 case REPOKEY_TYPE_CONSTANTID:
1069 assert(!data->localpool); /* implement me! */
1070 if (pool != frompool || fromdata->localpool)
1073 id = copyreldep(pool, frompool, id);
1075 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1077 if (key->type == REPOKEY_TYPE_ID)
1078 repodata_set_id(data, handle, keyname, id);
1080 repodata_set_constantid(data, handle, keyname, id);
1082 case REPOKEY_TYPE_STR:
1083 repodata_set_str(data, handle, keyname, kv->str);
1085 case REPOKEY_TYPE_VOID:
1086 repodata_set_void(data, handle, keyname);
1088 case REPOKEY_TYPE_NUM:
1089 repodata_set_num(data, handle, keyname, kv->num);
1091 case REPOKEY_TYPE_CONSTANT:
1092 repodata_set_constant(data, handle, keyname, kv->num);
1094 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1096 assert(!data->localpool); /* implement me! */
1097 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1098 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1100 case REPOKEY_TYPE_DIRSTRARRAY:
1102 assert(!data->localpool); /* implement me! */
1103 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1104 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1113 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1115 Repo *repo = s->repo;
1116 Repo *fromrepo = r->repo;
1117 Pool *pool = repo->pool;
1118 struct solvable_copy_cbdata cbdata;
1120 /* copy solvable data */
1121 if (pool == fromrepo->pool)
1126 s->vendor = r->vendor;
1131 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1133 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1135 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1137 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1139 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1140 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1141 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1142 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1143 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1144 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1145 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1146 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1148 /* copy all attributes */
1152 cbdata.handle = s - pool->solvables;
1153 cbdata.dircache = dircache;
1154 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1157 /* used to sort entries returned in some database order */
1159 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1161 struct rpmid const *a = va, *b = vb;
1163 r = strcmp(a->name, b->name);
1166 return a->dbid - b->dbid;
1170 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1173 Pool *pool = repo->pool;
1174 Solvable *a = pool->solvables + *(Id *)va;
1175 Solvable *b = pool->solvables + *(Id *)vb;
1178 if (a->name != b->name)
1179 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1180 rpmdbid = repo->rpmdbid;
1181 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1185 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1187 Pool *pool = repo->pool;
1190 tmp = pool->solvables[pa];
1191 pool->solvables[pa] = pool->solvables[pb];
1192 pool->solvables[pb] = tmp;
1195 Id tmpid = repo->rpmdbid[pa - repo->start];
1196 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1197 repo->rpmdbid[pb - repo->start] = tmpid;
1199 /* only works if nothing is already internalized! */
1200 if (data && data->attrs)
1202 Id *tmpattrs = data->attrs[pa - data->start];
1203 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1204 data->attrs[pb - data->start] = tmpattrs;
1209 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1211 memset(cookie, 0, 32);
1212 cookie[3] = RPMDB_COOKIE_VERSION;
1213 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1214 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1217 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1219 opendbenv(const char *rootdir)
1221 char dbpath[PATH_MAX];
1225 if (db_env_create(&dbenv, 0))
1227 perror("db_env_create");
1230 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1231 if (access(dbpath, W_OK) == -1)
1233 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1238 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1240 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1245 perror("dbenv open");
1246 dbenv->close(dbenv, 0);
1254 count_headers(const char *rootdir, DB_ENV *dbenv)
1256 char dbpath[PATH_MAX];
1257 struct stat statbuf;
1265 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1266 if (stat(dbpath, &statbuf))
1268 memset(&dbkey, 0, sizeof(dbkey));
1269 memset(&dbdata, 0, sizeof(dbdata));
1270 if (db_create(&db, dbenv, 0))
1272 perror("db_create");
1275 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1277 perror("db->open Name index");
1280 if (db->get_byteswapped(db, &byteswapped))
1282 perror("db->get_byteswapped");
1285 if (db->cursor(db, NULL, &dbc, 0))
1287 perror("db->cursor");
1290 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1291 count += dbdata.size >> 3;
1298 * read rpm db as repo
1303 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1305 Pool *pool = repo->pool;
1306 unsigned char buf[16];
1311 unsigned char *dp, *dbidp;
1313 struct rpmid *rpmids, *rp;
1319 unsigned int refmask, h;
1320 char dbpath[PATH_MAX];
1324 struct stat packagesstat;
1325 unsigned char newcookie[32];
1326 const unsigned char *oldcookie = 0;
1327 Id oldcookietype = 0;
1329 int count = 0, done = 0;
1332 now = sat_timems(0);
1333 memset(&dbkey, 0, sizeof(dbkey));
1334 memset(&dbdata, 0, sizeof(dbdata));
1339 data = repo_add_repodata(repo, flags);
1341 if (ref && !(ref->nsolvables && ref->rpmdbid))
1344 if (!(dbenv = opendbenv(rootdir)))
1347 /* XXX: should get ro lock of Packages database! */
1348 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1349 if (stat(dbpath, &packagesstat))
1354 mkrpmdbcookie(&packagesstat, newcookie);
1355 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1358 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1359 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1362 int solvstart = 0, solvend = 0;
1364 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1365 count = count_headers(rootdir, dbenv);
1366 if (db_create(&db, dbenv, 0))
1368 perror("db_create");
1371 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1373 perror("db->open Packages index");
1376 if (db->get_byteswapped(db, &byteswapped))
1378 perror("db->get_byteswapped");
1381 if (db->cursor(db, NULL, &dbc, 0))
1383 perror("db->cursor");
1386 dbidp = (unsigned char *)&dbid;
1391 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1395 s = pool_id2solvable(pool, repo_add_solvable(repo));
1397 solvstart = s - pool->solvables;
1398 solvend = s - pool->solvables + 1;
1401 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1402 if (dbkey.size != 4)
1404 fprintf(stderr, "corrupt Packages database (key size)\n");
1416 memcpy(dbidp, dp, 4);
1417 if (dbid == 0) /* the join key */
1419 if (dbdata.size < 8)
1421 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1424 if (dbdata.size > rpmheadsize)
1426 rpmheadsize = dbdata.size + 128;
1427 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1429 memcpy(buf, dbdata.data, 8);
1430 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1431 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1432 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1434 fprintf(stderr, "corrupt rpm database (data size)\n");
1437 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1438 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1439 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1440 if (rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS))
1447 /* We can reuse this solvable, but make sure it's still
1448 associated with this repo. */
1449 memset(s, 0, sizeof(*s));
1452 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1456 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1457 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1462 /* oops, could not reuse. free it instead */
1463 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1470 /* now sort all solvables in the new solvstart..solvend block */
1471 if (solvend - solvstart > 1)
1473 pkgids = sat_malloc2(solvend - solvstart, sizeof(Id));
1474 for (i = solvstart; i < solvend; i++)
1475 pkgids[i - solvstart] = i;
1476 sat_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1478 for (i = solvstart; i < solvend; i++)
1480 int j = pkgids[i - solvstart];
1482 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1484 swap_solvables(repo, data, i, j);
1491 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1493 memset(dircache, 0, sizeof(dircache));
1494 if (db_create(&db, dbenv, 0))
1496 perror("db_create");
1499 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1501 perror("db->open Name index");
1504 if (db->get_byteswapped(db, &byteswapped))
1506 perror("db->get_byteswapped");
1509 if (db->cursor(db, NULL, &dbc, 0))
1511 perror("db->cursor");
1514 dbidp = (unsigned char *)&dbid;
1517 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1519 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1533 memcpy(dbidp, dp, 4);
1534 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1535 rpmids[nrpmids].dbid = dbid;
1536 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1537 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1538 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1549 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1551 dbidp = (unsigned char *)&dbid;
1555 /* create hash from dbid to ref */
1556 refmask = mkmask(ref->nsolvables);
1557 refhash = sat_calloc(refmask + 1, sizeof(Id));
1558 for (i = 0; i < ref->end - ref->start; i++)
1560 if (!ref->rpmdbid[i])
1562 h = ref->rpmdbid[i] & refmask;
1564 h = (h + 317) & refmask;
1565 refhash[h] = i + 1; /* make it non-zero */
1568 /* count the misses, they will cost us time */
1569 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1571 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1577 while ((id = refhash[h]))
1579 if (ref->rpmdbid[id - 1] == dbid)
1581 h = (h + 317) & refmask;
1590 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1592 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1594 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1597 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1601 while ((id = refhash[h]))
1603 if (ref->rpmdbid[id - 1] == dbid)
1605 h = (h + 317) & refmask;
1609 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1612 solvable_copy(s, r, data, dircache);
1619 if (db_create(&db, dbenv, 0))
1621 perror("db_create");
1624 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1626 perror("db->open var/lib/rpm/Packages");
1629 if (db->get_byteswapped(db, &byteswapped))
1631 perror("db->get_byteswapped");
1643 memcpy(buf, dbidp, 4);
1648 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1651 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1652 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1655 if (dbdata.size < 8)
1657 fprintf(stderr, "corrupt rpm database (size)\n");
1660 if (dbdata.size > rpmheadsize)
1662 rpmheadsize = dbdata.size + 128;
1663 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1665 memcpy(buf, dbdata.data, 8);
1666 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1667 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1668 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1670 fprintf(stderr, "corrupt rpm database (data size)\n");
1673 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1674 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1676 rpm2solv(pool, repo, data, s, rpmhead, flags | RPM_ADD_TRIGGERS);
1677 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1681 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1682 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1690 for (i = 0; i < nrpmids; i++)
1691 sat_free(rpmids[i].name);
1697 dbenv->close(dbenv, 0);
1700 if (!(flags & REPO_NO_INTERNALIZE))
1701 repodata_internalize(data);
1702 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1703 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1704 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1705 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1706 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1710 static inline unsigned int
1711 getu32(const unsigned char *dp)
1713 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1718 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1720 int i, sigdsize, sigcnt, l;
1721 Pool *pool = repo->pool;
1723 RpmHead *rpmhead = 0;
1724 int rpmheadsize = 0;
1725 char *payloadformat;
1727 unsigned char lead[4096];
1728 int headerstart, headerend;
1731 unsigned char pkgid[16];
1736 data = repo_add_repodata(repo, flags);
1738 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1739 chksumtype = REPOKEY_TYPE_SHA256;
1740 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1741 chksumtype = REPOKEY_TYPE_SHA1;
1742 for (i = 0; i < nrpms; i++)
1744 if ((fp = fopen(rpms[i], "r")) == 0)
1749 if (fstat(fileno(fp), &stb))
1755 chksumh = sat_chksum_free(chksumh, 0);
1757 chksumh = sat_chksum_create(chksumtype);
1758 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1760 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1765 sat_chksum_add(chksumh, lead, 96 + 16);
1766 if (lead[78] != 0 || lead[79] != 5)
1768 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1772 if (getu32(lead + 96) != 0x8eade801)
1774 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1778 sigcnt = getu32(lead + 96 + 8);
1779 sigdsize = getu32(lead + 96 + 12);
1780 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1782 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1786 sigdsize += sigcnt * 16;
1787 sigdsize = (sigdsize + 7) & ~7;
1788 headerstart = 96 + 16 + sigdsize;
1790 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1792 unsigned char *chksum;
1793 unsigned int chksumsize;
1794 /* extract pkgid from the signature header */
1795 if (sigdsize > rpmheadsize)
1797 rpmheadsize = sigdsize + 128;
1798 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1800 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1802 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1807 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1808 rpmhead->cnt = sigcnt;
1809 rpmhead->dcnt = sigdsize - sigcnt * 16;
1810 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1811 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1812 if (chksum && chksumsize == 16)
1815 memcpy(pkgid, chksum, 16);
1820 /* just skip the signature header */
1823 l = sigdsize > 4096 ? 4096 : sigdsize;
1824 if (fread(lead, l, 1, fp) != 1)
1826 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1831 sat_chksum_add(chksumh, lead, l);
1835 if (fread(lead, 16, 1, fp) != 1)
1837 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1842 sat_chksum_add(chksumh, lead, 16);
1843 if (getu32(lead) != 0x8eade801)
1845 fprintf(stderr, "%s: bad header\n", rpms[i]);
1849 sigcnt = getu32(lead + 8);
1850 sigdsize = getu32(lead + 12);
1851 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1853 fprintf(stderr, "%s: bad header\n", rpms[i]);
1857 l = sigdsize + sigcnt * 16;
1858 headerend = headerstart + 16 + l;
1859 if (l > rpmheadsize)
1861 rpmheadsize = l + 128;
1862 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1864 if (fread(rpmhead->data, l, 1, fp) != 1)
1866 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1871 sat_chksum_add(chksumh, rpmhead->data, l);
1872 rpmhead->cnt = sigcnt;
1873 rpmhead->dcnt = sigdsize;
1874 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1875 if (headexists(rpmhead, TAG_PATCHESNAME))
1877 /* this is a patch rpm, ignore */
1881 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1882 if (payloadformat && !strcmp(payloadformat, "drpm"))
1884 /* this is a delta rpm */
1889 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1890 sat_chksum_add(chksumh, lead, l);
1892 s = pool_id2solvable(pool, repo_add_solvable(repo));
1893 rpm2solv(pool, repo, data, s, rpmhead, flags);
1896 Id handle = s - pool->solvables;
1897 repodata_set_location(data, handle, 0, 0, rpms[i]);
1898 if (S_ISREG(stb.st_mode))
1899 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1900 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1902 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1904 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1908 chksumh = sat_chksum_free(chksumh, 0);
1911 if (!(flags & REPO_NO_INTERNALIZE))
1912 repodata_internalize(data);
1916 linkhash(const char *lt, char *hash)
1919 const unsigned char *str = (const unsigned char *)lt;
1923 while ((c = *str++) != 0)
1925 sprintf(hash, "%08x", r);
1926 sprintf(hash + 8, "%08x", l);
1927 sprintf(hash + 16, "%08x", 0);
1928 sprintf(hash + 24, "%08x", 0);
1932 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1934 RpmHead *rpmhead = rpmhandle;
1939 unsigned int *di, diidx;
1940 unsigned int *co = 0;
1941 unsigned int lastdir;
1944 int cnt, dcnt, cnt2;
1948 char md5[33], *md5p = 0;
1950 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1953 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1955 for (i = 0; i < dcnt; i++)
1956 (*cb)(cbdata, dn[i], 0, (char *)0);
1960 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1966 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1967 if (!di || cnt != cnt2)
1974 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1975 if (!fm || cnt != cnt2)
1983 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1985 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1986 if (!md || cnt != cnt2)
1996 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
1998 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
1999 if (!co || cnt != cnt2)
2012 for (i = 0; i < cnt; i++)
2017 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2020 l = l1 + strlen(bn[i]) + 1;
2024 space = sat_realloc(space, spacen);
2026 if (lastdir != diidx)
2028 strcpy(space, dn[diidx]);
2032 strcpy(space + l1, bn[i]);
2041 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2047 linkhash(lt[i], md5);
2053 sprintf(md5, "%08x%08x", (fm[i] >> 12) & 65535, 0);
2057 (*cb)(cbdata, space, co ? (fm[i] | co[i] << 24) : fm[i], md5p);
2070 rpm_query(void *rpmhandle, Id what)
2072 const char *name, *arch, *sourcerpm;
2076 RpmHead *rpmhead = rpmhandle;
2081 name = headstring(rpmhead, TAG_NAME);
2084 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2086 arch = headstring(rpmhead, TAG_ARCH);
2089 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2096 evr = headtoevr(rpmhead);
2097 l = strlen(name) + 1 + strlen(evr) + 1 + strlen(arch) + 1;
2099 sprintf(r, "%s-%s.%s", name, evr, arch);
2103 name = headstring(rpmhead, TAG_NAME);
2107 r = headtoevr(rpmhead);
2114 struct rpm_by_state {
2129 #define ENTRIES_BLOCK 255
2130 #define NAMEDATA_BLOCK 1023
2132 static struct rpmdbentry *
2133 getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
2147 struct rpmdbentry *entries = 0;
2153 dbenv = state->dbenv;
2154 if (db_create(&db, dbenv, 0))
2156 perror("db_create");
2159 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
2161 perror("db->open index");
2165 if (db->get_byteswapped(db, &byteswapped))
2167 perror("db->get_byteswapped");
2171 if (db->cursor(db, NULL, &dbc, 0))
2173 perror("db->cursor");
2177 memset(&dbkey, 0, sizeof(dbkey));
2178 memset(&dbdata, 0, sizeof(dbdata));
2181 dbkey.data = (void *)match;
2182 dbkey.size = strlen(match);
2184 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
2186 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2194 ((char *)&rpmdbid)[0] = dp[3];
2195 ((char *)&rpmdbid)[1] = dp[2];
2196 ((char *)&rpmdbid)[2] = dp[1];
2197 ((char *)&rpmdbid)[3] = dp[0];
2200 memcpy((char *)&rpmdbid, dp, 4);
2201 entries = sat_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
2202 entries[nentries].rpmdbid = rpmdbid;
2203 entries[nentries].nameoff = namedatal;
2205 namedata = sat_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
2206 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
2207 namedata[namedatal + dbkey.size] = 0;
2208 namedatal += dbkey.size + 1;
2217 *nentriesp = nentries;
2218 *namedatap = namedata;
2223 freestate(struct rpm_by_state *state)
2229 state->db->close(state->db, 0);
2231 state->dbenv->close(state->dbenv, 0);
2232 sat_free(state->rpmhead);
2236 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
2238 struct rpm_by_state state;
2239 struct rpmdbentry *entries;
2246 queue_empty(rpmdbidq);
2247 memset(&state, 0, sizeof(state));
2248 if (!(state.dbenv = opendbenv(rootdir)))
2250 entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
2252 for (i = 0; i < nentries; i++)
2253 queue_push(rpmdbidq, entries[i].rpmdbid);
2261 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2263 struct rpm_by_state *state = *statep;
2264 unsigned char buf[16];
2274 *statep = (void *)0;
2280 state = sat_calloc(1, sizeof(*state));
2283 if (!state->dbopened)
2285 state->dbopened = 1;
2286 if (!state->dbenv && !(state->dbenv = opendbenv(rootdir)))
2288 if (db_create(&state->db, state->dbenv, 0))
2290 perror("db_create");
2292 state->dbenv->close(state->dbenv, 0);
2296 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2298 perror("db->open var/lib/rpm/Packages");
2299 state->db->close(state->db, 0);
2301 state->dbenv->close(state->dbenv, 0);
2305 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2307 perror("db->get_byteswapped");
2308 state->db->close(state->db, 0);
2310 state->dbenv->close(state->dbenv, 0);
2315 memcpy(buf, &rpmdbid, 4);
2316 if (state->byteswapped)
2319 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2320 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2322 memset(&dbkey, 0, sizeof(dbkey));
2323 memset(&dbdata, 0, sizeof(dbdata));
2328 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2333 if (dbdata.size < 8)
2335 fprintf(stderr, "corrupt rpm database (size)\n");
2338 if (dbdata.size > state->rpmheadsize)
2340 state->rpmheadsize = dbdata.size + 128;
2341 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2343 rpmhead = state->rpmhead;
2344 memcpy(buf, dbdata.data, 8);
2345 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2346 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2347 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2349 fprintf(stderr, "corrupt rpm database (data size)\n");
2352 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2353 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2358 rpm_byfp(FILE *fp, const char *name, void **statep)
2360 struct rpm_by_state *state = *statep;
2361 int headerstart, headerend;
2363 int sigdsize, sigcnt, l;
2364 unsigned char lead[4096];
2367 return rpm_byrpmdbid(0, 0, statep);
2370 state = sat_calloc(1, sizeof(*state));
2373 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2375 fprintf(stderr, "%s: not a rpm\n", name);
2378 if (lead[78] != 0 || lead[79] != 5)
2380 fprintf(stderr, "%s: not a V5 header\n", name);
2383 if (getu32(lead + 96) != 0x8eade801)
2385 fprintf(stderr, "%s: bad signature header\n", name);
2388 sigcnt = getu32(lead + 96 + 8);
2389 sigdsize = getu32(lead + 96 + 12);
2390 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2392 fprintf(stderr, "%s: bad signature header\n", name);
2395 sigdsize += sigcnt * 16;
2396 sigdsize = (sigdsize + 7) & ~7;
2397 headerstart = 96 + 16 + sigdsize;
2400 l = sigdsize > 4096 ? 4096 : sigdsize;
2401 if (fread(lead, l, 1, fp) != 1)
2403 fprintf(stderr, "%s: unexpected EOF\n", name);
2408 if (fread(lead, 16, 1, fp) != 1)
2410 fprintf(stderr, "%s: unexpected EOF\n", name);
2413 if (getu32(lead) != 0x8eade801)
2415 fprintf(stderr, "%s: bad header\n", name);
2419 sigcnt = getu32(lead + 8);
2420 sigdsize = getu32(lead + 12);
2421 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2423 fprintf(stderr, "%s: bad header\n", name);
2427 l = sigdsize + sigcnt * 16;
2428 headerend = headerstart + 16 + l;
2429 if (l > state->rpmheadsize)
2431 state->rpmheadsize = l + 128;
2432 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2434 rpmhead = state->rpmhead;
2435 if (fread(rpmhead->data, l, 1, fp) != 1)
2437 fprintf(stderr, "%s: unexpected EOF\n", name);
2441 rpmhead->cnt = sigcnt;
2442 rpmhead->dcnt = sigdsize;
2443 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2448 rpm_byrpmh(Header h, void **statep)
2450 struct rpm_by_state *state = *statep;
2451 const unsigned char *uh;
2452 int sigdsize, sigcnt, l;
2455 uh = headerUnload(h);
2458 sigcnt = getu32(uh);
2459 sigdsize = getu32(uh + 4);
2460 l = sigdsize + sigcnt * 16;
2463 state = sat_calloc(1, sizeof(*state));
2466 if (l > state->rpmheadsize)
2468 state->rpmheadsize = l + 128;
2469 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2471 rpmhead = state->rpmhead;
2472 memcpy(rpmhead->data, uh + 8, l - 8);
2474 rpmhead->cnt = sigcnt;
2475 rpmhead->dcnt = sigdsize;
2476 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2482 r64dec1(char *p, unsigned int *vp, int *eofp)
2487 for (i = 0; i < 4; )
2492 if (x >= 'A' && x <= 'Z')
2494 else if (x >= 'a' && x <= 'z')
2496 else if (x >= '0' && x <= '9')
2523 crc24(unsigned char *p, int len)
2525 unsigned int crc = 0xb704ceL;
2530 crc ^= (*p++) << 16;
2531 for (i = 0; i < 8; i++)
2532 if ((crc <<= 1) & 0x1000000)
2535 return crc & 0xffffffL;
2538 static unsigned char *
2539 unarmor(char *pubkey, int *pktlp)
2543 unsigned char *buf, *bp;
2547 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
2549 pubkey = strchr(pubkey, '\n');
2554 pubkey = strchr(pubkey, '\n');
2557 /* skip header lines */
2560 while (*pubkey == ' ' || *pubkey == '\t')
2562 if (*pubkey == '\n')
2564 pubkey = strchr(pubkey, '\n');
2569 p = strchr(pubkey, '=');
2573 bp = buf = sat_malloc(l * 3 / 4 + 4);
2577 pubkey = r64dec1(pubkey, &v, &eof);
2587 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2590 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
2595 if (v != crc24(buf, bp - buf))
2600 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
2602 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
2612 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
2615 unsigned char keyid[8];
2616 unsigned int kcr = 0, maxex = 0;
2617 unsigned char *pubkey = 0;
2619 unsigned char *userid = 0;
2622 for (; pl; p += l, pl -= l)
2626 if (!(x & 128) || pl <= 0)
2631 tag = (x & 0x3c) >> 2;
2653 else if (x >= 192 && x < 224)
2657 l = ((x - 192) << 8) + *p++ + 192;
2664 l = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
2675 pubkey = sat_realloc(pubkey, l);
2677 memcpy(pubkey, p, l);
2684 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2688 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
2692 memset(keyid, 0, 8);
2693 if (p[7] == 1) /* RSA */
2696 unsigned char fp[16];
2700 ql = ((p[8] << 8 | p[9]) + 7) / 8;
2701 memcpy(keyid, p + 10 + ql - 8, 8);
2702 h = sat_chksum_create(REPOKEY_TYPE_MD5);
2703 sat_chksum_add(h, p + 10, ql);
2705 ql = ((q[0] << 8 | q[1]) + 7) / 8;
2706 sat_chksum_add(h, q + 2, ql);
2707 sat_chksum_free(h, fp);
2708 for (i = 0; i < 16; i++)
2709 sprintf(fpx + i * 2, "%02x", fp[i]);
2710 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2717 unsigned char hdr[3];
2718 unsigned char fp[20];
2721 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
2725 h = sat_chksum_create(REPOKEY_TYPE_SHA1);
2726 sat_chksum_add(h, hdr, 3);
2727 sat_chksum_add(h, p, l);
2728 sat_chksum_free(h, fp);
2729 for (i = 0; i < 20; i++)
2730 sprintf(fpx + i * 2, "%02x", fp[i]);
2731 setutf8string(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
2732 memcpy(keyid, fp + 12, 8);
2737 if (p[0] == 3 && p[1] == 5)
2742 // printf("V3 signature packet\n");
2743 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
2745 if (!memcmp(keyid, p + 6, 8))
2747 // printf("SELF SIG\n");
2751 // printf("OTHER SIG\n");
2755 htype = REPOKEY_TYPE_MD5;
2756 else if (p[16] == 2)
2757 htype = REPOKEY_TYPE_SHA1;
2758 else if (p[16] == 8)
2759 htype = REPOKEY_TYPE_SHA256;
2762 void *h = sat_chksum_create(htype);
2763 unsigned char b[3], *cs;
2766 b[1] = pubkeyl >> 8;
2768 sat_chksum_add(h, b, 3);
2769 sat_chksum_add(h, pubkey, pubkeyl);
2770 if (p[2] >= 0x10 && p[2] <= 0x13)
2771 sat_chksum_add(h, userid, useridl);
2772 sat_chksum_add(h, p + 2, 5);
2773 cs = sat_chksum_get(h, 0);
2774 sat_chksum_free(h, 0);
2780 int j, ql, haveissuer;
2782 unsigned int ex = 0, scr = 0;
2783 unsigned char issuer[8];
2785 // printf("V4 signature packet\n");
2786 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
2791 for (j = 0; q && j < 2; j++)
2793 ql = q[0] << 8 | q[1];
2809 sl = q[0] << 24 | q[1] << 16 | q[2] << 8 | q[3];
2820 sl = ((x - 192) << 8) + *q++ + 192;
2829 // printf("%d SIGSUB %d %d\n", j, x, sl);
2830 if (x == 16 && sl == 9 && !haveissuer)
2832 memcpy(issuer, q + 1, 8);
2835 if (x == 2 && j == 0)
2836 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2837 if (x == 9 && j == 0)
2838 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
2850 htype = REPOKEY_TYPE_MD5;
2852 htype = REPOKEY_TYPE_SHA1;
2854 htype = REPOKEY_TYPE_SHA256;
2855 if (htype && pubkeyl)
2857 void *h = sat_chksum_create(htype);
2858 unsigned char b[6], *cs;
2862 b[1] = pubkeyl >> 8;
2864 sat_chksum_add(h, b, 3);
2865 sat_chksum_add(h, pubkey, pubkeyl);
2866 if (p[1] >= 0x10 && p[1] <= 0x13)
2869 b[1] = useridl >> 24;
2870 b[2] = useridl >> 16;
2871 b[3] = useridl >> 8;
2873 sat_chksum_add(h, b, 5);
2874 sat_chksum_add(h, userid, useridl);
2876 hl = 6 + (p[4] << 8 | p[5]);
2877 sat_chksum_add(h, p, hl);
2884 sat_chksum_add(h, b, 6);
2885 cs = sat_chksum_get(h, 0);
2886 sat_chksum_free(h, 0);
2889 if (!memcmp(keyid, issuer, 8))
2891 // printf("SELF SIG cr %d ex %d\n", cr, ex);
2897 // printf("OTHER SIG cr %d ex %d\n", cr, ex);
2904 userid = sat_realloc(userid, l);
2906 memcpy(userid, p, l);
2911 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
2916 /* this is private to rpm, but rpm lacks an interface to retrieve
2917 * the values. Sigh. */
2918 struct pgpDigParams_s {
2919 const char * userid;
2920 const unsigned char * hash;
2921 const char * params[4];
2923 unsigned char version; /*!< version number. */
2924 unsigned char time[4]; /*!< time that the key was created. */
2925 unsigned char pubkey_algo; /*!< public key algorithm. */
2926 unsigned char hash_algo;
2927 unsigned char sigtype;
2928 unsigned char hashlen;
2929 unsigned char signhash16[2];
2930 unsigned char signid[8];
2931 unsigned char saved;
2935 struct pgpDigParams_s signature;
2936 struct pgpDigParams_s pubkey;
2940 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
2942 Pool *pool = s->repo->pool;
2943 unsigned char *pkts;
2948 char evrbuf[8 + 1 + 8 + 1];
2950 pkts = unarmor(pubkey, &pktsl);
2953 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
2954 parsekeydata(s, data, pkts, pktsl);
2955 /* only rpm knows how to do the release calculation, we don't dare
2956 * to recreate all the bugs */
2958 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
2959 btime = dig->pubkey.time[0] << 24 | dig->pubkey.time[1] << 16 | dig->pubkey.time[2] << 8 | dig->pubkey.signid[3];
2960 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]);
2961 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
2963 s->name = str2id(pool, "gpg-pubkey", 1);
2964 s->evr = str2id(pool, evrbuf, 1);
2966 for (i = 0; i < 8; i++)
2967 sprintf(keyid + 2 * i, "%02x", dig->pubkey.signid[i]);
2968 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
2969 if (dig->pubkey.userid)
2970 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, dig->pubkey.userid);
2972 sat_free((void *)pkts);
2977 repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
2979 Pool *pool = repo->pool;
2980 struct rpm_by_state state;
2981 struct rpmdbentry *entries;
2983 char *namedata, *str;
2988 data = repo_add_repodata(repo, flags);
2990 memset(&state, 0, sizeof(state));
2991 if (!(state.dbenv = opendbenv(rootdir)))
2993 entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
2994 for (i = 0 ; i < nentries; i++)
2996 void *statep = &state;
2997 RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
3000 str = headstring(rpmhead, TAG_DESCRIPTION);
3003 s = pool_id2solvable(pool, repo_add_solvable(repo));
3004 pubkey2solvable(s, data, str);
3005 u32 = headint32(rpmhead, TAG_INSTALLTIME);
3007 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, u32);
3009 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
3010 repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
3013 if (!(flags & REPO_NO_INTERNALIZE))
3014 repodata_internalize(data);
3018 repo_add_pubkeys(Repo *repo, const char **keys, int nkeys, int flags)
3020 Pool *pool = repo->pool;
3027 data = repo_add_repodata(repo, flags);
3030 for (i = 0; i < nkeys; i++)
3032 if ((fp = fopen(keys[i], "r")) == 0)
3039 if (bufl - l < 4096)
3042 buf = sat_realloc(buf, bufl);
3044 ll = fread(buf, 1, bufl - l, fp);
3051 s = pool_id2solvable(pool, repo_add_solvable(repo));
3052 pubkey2solvable(s, data, buf);
3055 if (!(flags & REPO_NO_INTERNALIZE))
3056 repodata_internalize(data);