2 * Copyright (c) 2007-2012, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * convert rpm db to repo
15 #include <sys/types.h>
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
30 #include <rpm/header.h>
32 #include <rpm/rpmdb.h>
35 # if defined(SUSE) || defined(HAVE_RPM_DB_H)
48 #include "repo_rpmdb.h"
49 #include "repo_solv.h"
50 #ifdef ENABLE_COMPLEX_DEPS
51 #include "pool_parserpmrichdep.h"
54 /* 3: added triggers */
55 /* 4: fixed triggers */
56 /* 5: fixed checksum copying */
57 #define RPMDB_COOKIE_VERSION 5
60 #define TAG_VERSION 1001
61 #define TAG_RELEASE 1002
62 #define TAG_EPOCH 1003
63 #define TAG_SUMMARY 1004
64 #define TAG_DESCRIPTION 1005
65 #define TAG_BUILDTIME 1006
66 #define TAG_BUILDHOST 1007
67 #define TAG_INSTALLTIME 1008
69 #define TAG_DISTRIBUTION 1010
70 #define TAG_VENDOR 1011
71 #define TAG_LICENSE 1014
72 #define TAG_PACKAGER 1015
73 #define TAG_GROUP 1016
76 #define TAG_FILESIZES 1028
77 #define TAG_FILEMODES 1030
78 #define TAG_FILEMD5S 1035
79 #define TAG_FILELINKTOS 1036
80 #define TAG_FILEFLAGS 1037
81 #define TAG_SOURCERPM 1044
82 #define TAG_PROVIDENAME 1047
83 #define TAG_REQUIREFLAGS 1048
84 #define TAG_REQUIRENAME 1049
85 #define TAG_REQUIREVERSION 1050
86 #define TAG_NOSOURCE 1051
87 #define TAG_NOPATCH 1052
88 #define TAG_CONFLICTFLAGS 1053
89 #define TAG_CONFLICTNAME 1054
90 #define TAG_CONFLICTVERSION 1055
91 #define TAG_TRIGGERNAME 1066
92 #define TAG_TRIGGERVERSION 1067
93 #define TAG_TRIGGERFLAGS 1068
94 #define TAG_CHANGELOGTIME 1080
95 #define TAG_CHANGELOGNAME 1081
96 #define TAG_CHANGELOGTEXT 1082
97 #define TAG_OBSOLETENAME 1090
98 #define TAG_FILEDEVICES 1095
99 #define TAG_FILEINODES 1096
100 #define TAG_SOURCEPACKAGE 1106
101 #define TAG_PROVIDEFLAGS 1112
102 #define TAG_PROVIDEVERSION 1113
103 #define TAG_OBSOLETEFLAGS 1114
104 #define TAG_OBSOLETEVERSION 1115
105 #define TAG_DIRINDEXES 1116
106 #define TAG_BASENAMES 1117
107 #define TAG_DIRNAMES 1118
108 #define TAG_PAYLOADFORMAT 1124
109 #define TAG_PATCHESNAME 1133
110 #define TAG_FILECOLORS 1140
111 #define TAG_OLDSUGGESTSNAME 1156
112 #define TAG_OLDSUGGESTSVERSION 1157
113 #define TAG_OLDSUGGESTSFLAGS 1158
114 #define TAG_OLDENHANCESNAME 1159
115 #define TAG_OLDENHANCESVERSION 1160
116 #define TAG_OLDENHANCESFLAGS 1161
119 #define TAG_DISTEPOCH 1218
122 #define TAG_LONGFILESIZES 5008
123 #define TAG_LONGSIZE 5009
124 #define TAG_RECOMMENDNAME 5046
125 #define TAG_RECOMMENDVERSION 5047
126 #define TAG_RECOMMENDFLAGS 5048
127 #define TAG_SUGGESTNAME 5049
128 #define TAG_SUGGESTVERSION 5050
129 #define TAG_SUGGESTFLAGS 5051
130 #define TAG_SUPPLEMENTNAME 5052
131 #define TAG_SUPPLEMENTVERSION 5053
132 #define TAG_SUPPLEMENTFLAGS 5054
133 #define TAG_ENHANCENAME 5055
134 #define TAG_ENHANCEVERSION 5056
135 #define TAG_ENHANCEFLAGS 5057
138 #define TAG_SIGBASE 256
139 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
140 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
142 #define SIGTAG_SIZE 1000
143 #define SIGTAG_PGP 1002 /* RSA signature */
144 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
145 #define SIGTAG_GPG 1005 /* DSA signature */
147 #define DEP_LESS (1 << 1)
148 #define DEP_GREATER (1 << 2)
149 #define DEP_EQUAL (1 << 3)
150 #define DEP_STRONG (1 << 27)
151 #define DEP_PRE_IN ((1 << 6) | (1 << 9) | (1 << 10))
152 #define DEP_PRE_UN ((1 << 6) | (1 << 11) | (1 << 12))
154 #define FILEFLAG_GHOST (1 << 6)
158 # define RPM_INDEX_SIZE 4 /* just the rpmdbid */
160 # define RPM_INDEX_SIZE 8 /* rpmdbid + array index */
164 typedef struct rpmhead {
168 int forcebinary; /* sigh, see rh#478907 */
169 unsigned char data[1];
173 static inline unsigned char *
174 headfindtag(RpmHead *h, int tag)
177 unsigned char *d, taga[4];
183 for (i = 0; i < h->cnt; i++, d -= 16)
184 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
190 headexists(RpmHead *h, int tag)
192 return headfindtag(h, tag) ? 1 : 0;
195 static unsigned int *
196 headint32array(RpmHead *h, int tag, int *cnt)
198 unsigned int i, o, *r;
199 unsigned char *d = headfindtag(h, tag);
201 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
203 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
204 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
205 if (o + 4 * i > h->dcnt)
208 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
211 for (o = 0; o < i; o++, d += 4)
212 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
216 /* returns the first entry of an integer array */
218 headint32(RpmHead *h, int tag)
221 unsigned char *d = headfindtag(h, tag);
223 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
225 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
226 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
227 if (i == 0 || o + 4 * i > h->dcnt)
230 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
233 static unsigned long long *
234 headint64array(RpmHead *h, int tag, int *cnt)
237 unsigned long long *r;
238 unsigned char *d = headfindtag(h, tag);
240 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
242 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
243 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
244 if (o + 8 * i > h->dcnt)
247 r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
250 for (o = 0; o < i; o++, d += 8)
252 unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
253 r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
258 /* returns the first entry of an 64bit integer array */
259 static unsigned long long
260 headint64(RpmHead *h, int tag)
263 unsigned char *d = headfindtag(h, tag);
264 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
266 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
267 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
268 if (i == 0 || o + 8 * i > h->dcnt)
271 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
272 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
275 static unsigned int *
276 headint16array(RpmHead *h, int tag, int *cnt)
278 unsigned int i, o, *r;
279 unsigned char *d = headfindtag(h, tag);
281 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
283 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
284 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
285 if (o + 4 * i > h->dcnt)
288 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
291 for (o = 0; o < i; o++, d += 2)
292 r[o] = d[0] << 8 | d[1];
297 headstring(RpmHead *h, int tag)
300 unsigned char *d = headfindtag(h, tag);
301 /* 6: STRING, 9: I18NSTRING */
302 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
304 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
307 return (char *)h->dp + o;
311 headstringarray(RpmHead *h, int tag, int *cnt)
314 unsigned char *d = headfindtag(h, tag);
317 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
319 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
320 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
321 r = solv_calloc(i ? i : 1, sizeof(char *));
325 for (o = 0; o < i; o++)
329 d += strlen((char *)d) + 1;
330 if (d >= h->dp + h->dcnt)
339 static unsigned char *
340 headbinary(RpmHead *h, int tag, unsigned int *sizep)
343 unsigned char *d = headfindtag(h, tag);
344 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
346 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
347 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
348 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
355 static char *headtoevr(RpmHead *h)
363 version = headstring(h, TAG_VERSION);
364 release = headstring(h, TAG_RELEASE);
365 epoch = headint32(h, TAG_EPOCH);
366 if (!version || !release)
368 fprintf(stderr, "headtoevr: bad rpm header\n");
371 for (v = version; *v >= '0' && *v <= '9'; v++)
373 if (epoch || (v != version && *v == ':'))
375 char epochbuf[11]; /* 32bit decimal will fit in */
376 sprintf(epochbuf, "%u", epoch);
377 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
378 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
382 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
383 sprintf(evr, "%s-%s", version, release);
385 distepoch = headstring(h, TAG_DISTEPOCH);
386 if (distepoch && *distepoch)
389 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
391 strcpy(evr + l, distepoch);
398 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
400 if (str[solv_validutf8(str)])
402 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
403 repodata_set_str(repodata, handle, tag, ustr);
407 repodata_set_str(repodata, handle, tag, str);
411 * strong: 0: ignore strongness
412 * 1: filter to strong
416 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
420 int i, cc, nc, vc, fc;
422 unsigned int olddeps;
426 n = headstringarray(rpmhead, tagn, &nc);
431 case TAG_SUGGESTNAME:
432 tagn = TAG_OLDSUGGESTSNAME;
433 tagv = TAG_OLDSUGGESTSVERSION;
434 tagf = TAG_OLDSUGGESTSFLAGS;
437 case TAG_ENHANCENAME:
438 tagn = TAG_OLDENHANCESNAME;
439 tagv = TAG_OLDENHANCESVERSION;
440 tagf = TAG_OLDENHANCESFLAGS;
443 case TAG_RECOMMENDNAME:
444 tagn = TAG_OLDSUGGESTSNAME;
445 tagv = TAG_OLDSUGGESTSVERSION;
446 tagf = TAG_OLDSUGGESTSFLAGS;
449 case TAG_SUPPLEMENTNAME:
450 tagn = TAG_OLDENHANCESNAME;
451 tagv = TAG_OLDENHANCESVERSION;
452 tagf = TAG_OLDENHANCESFLAGS;
458 n = headstringarray(rpmhead, tagn, &nc);
463 v = headstringarray(rpmhead, tagv, &vc);
464 f = headint32array(rpmhead, tagf, &fc);
465 if (!v || !f || nc != vc || nc != fc)
467 char *pkgname = rpm_query(rpmhead, 0);
468 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
477 haspre = 0; /* add no prereq marker */
478 premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
479 if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
481 /* we do filtering */
483 for (i = 0; i < nc; i++)
485 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
487 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
488 if (!strncmp(n[i], "rpmlib(", 7))
490 if ((f[i] & premask) != 0)
497 /* no filtering, just look for the first prereq */
498 for (i = 0; i < nc; i++)
499 if ((f[i] & premask) != 0)
512 cc += haspre; /* add slot for the prereq marker */
513 olddeps = repo_reserve_ids(repo, 0, cc);
514 ida = repo->idarraydata + olddeps;
522 haspre = 2; /* pass two: prereqs */
524 *ida++ = SOLVABLE_PREREQMARKER;
526 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
530 if (haspre == 1 && (f[i] & premask) != 0)
532 if (haspre == 2 && (f[i] & premask) == 0)
535 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
536 if (!strncmp(n[i], "rpmlib(", 7))
538 #ifdef ENABLE_COMPLEX_DEPS
539 if ((f[i] & (DEP_LESS|DEP_EQUAL|DEP_GREATER)) == 0 && n[i][0] == '(')
541 id = pool_parserpmrichdep(pool, n[i]);
549 id = pool_str2id(pool, n[i], 1);
550 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
554 if ((f[i] & DEP_LESS) != 0)
556 if ((f[i] & DEP_EQUAL) != 0)
558 if ((f[i] & DEP_GREATER) != 0)
560 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
561 evr = pool_str2id(pool, v[i] + 2, 1);
563 evr = pool_str2id(pool, v[i], 1);
564 id = pool_rel2id(pool, id, evr, fl, 1);
569 repo->idarraysize += cc + 1;
578 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
582 unsigned int *fkb, *fn, *fsz, *fm, *fino;
583 unsigned long long *fsz64;
584 unsigned int inotest[256], inotestok;
588 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
590 /* convert to kbyte */
591 fsz = solv_malloc2(fszc, sizeof(*fsz));
592 for (i = 0; i < fszc; i++)
593 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
596 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
598 /* convert to kbyte */
599 for (i = 0; i < fszc; i++)
601 fsz[i] = fsz[i] / 1024 + 1;
611 /* stupid rpm records sizes of directories, so we have to check the mode */
612 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
613 if (!fm || fc != fszc)
619 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
620 if (!fino || fc != fszc)
628 /* kill hardlinked entries */
630 if (fc < sizeof(inotest))
632 /* quick test just hashing the inode numbers */
633 memset(inotest, 0, sizeof(inotest));
634 for (i = 0; i < fc; i++)
637 if (fsz[i] == 0 || !S_ISREG(fm[i]))
638 continue; /* does not matter */
639 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
640 bit = 1 << (fino[i] & 31);
641 if ((inotest[off] & bit) != 0)
646 inotestok = 1; /* no conflict found */
650 /* hardlinked files are possible, check ino/dev pairs */
651 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
653 unsigned int mask, hash, hh;
654 if (!fdev || fc != fszc)
663 while ((mask & (mask - 1)) != 0)
664 mask = mask & (mask - 1);
666 if (mask > sizeof(inotest)/sizeof(*inotest))
667 fx = solv_calloc(mask, sizeof(unsigned int));
671 memset(fx, 0, mask * sizeof(unsigned int));
674 for (i = 0; i < fc; i++)
676 if (fsz[i] == 0 || !S_ISREG(fm[i]))
678 hash = (fino[i] + fdev[i] * 31) & mask;
680 while ((j = fx[hash]) != 0)
682 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
684 fsz[i] = 0; /* kill entry */
687 hash = (hash + hh++) & mask;
698 /* sum up inode count and kbytes for each directory */
699 fn = solv_calloc(dc, sizeof(unsigned int));
700 fkb = solv_calloc(dc, sizeof(unsigned int));
701 for (i = 0; i < fc; i++)
704 continue; /* corrupt entry */
706 if (fsz[i] == 0 || !S_ISREG(fm[i]))
708 fkb[di[i]] += fsz[i];
713 for (i = 0; i < dc; i++)
719 Solvable *s = data->repo->pool->solvables + handle;
720 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
721 did = repodata_str2dir(data, "/usr/src", 1);
723 continue; /* work around rpm bug */
726 did = repodata_str2dir(data, dn[i], 1);
727 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
734 is_filtered(const char *dir)
738 /* the dirs always have a trailing / in rpm */
739 if (strstr(dir, "bin/"))
741 if (!strncmp(dir, "/etc/", 5))
743 if (!strcmp(dir, "/usr/lib/"))
749 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
757 unsigned int lastdii = -1;
758 int lastfiltered = 0;
762 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
765 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
771 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
780 pool_error(data->repo->pool, 0, "bad filelist");
784 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
786 for (i = 0; i < bnc; i++)
791 if (di[i] == lastdii)
796 continue; /* corrupt entry */
798 if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
800 lastfiltered = is_filtered(dn[di[i]]);
801 if (lastfiltered == 1)
804 did = repodata_str2dir(data, dn[lastdii], 1);
806 did = repodata_str2dir(data, "/", 1);
809 if (b && *b == '/') /* work around rpm bug */
813 if (lastfiltered != 2 || strcmp(b, "sendmail"))
816 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
824 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
829 int i, cnc, cxc, ctc;
832 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
833 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
834 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
835 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
843 for (i = 0; i < ctc; i++)
845 Id h = repodata_new_handle(data);
847 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
849 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
851 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
854 for (i = 0; i < hq.count; i++)
855 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
863 set_description_author(Repodata *data, Id handle, char *str)
866 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
867 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
871 /* oh my, found SUSE special author section */
873 str = solv_strdup(str);
876 while (l > 0 && str[l - 1] == '\n')
879 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
881 aut = str; /* copy over */
882 while (*p == ' ' || *p == '\n')
895 while (aut != str && aut[-1] == '\n')
899 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
903 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
907 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
913 name = headstring(rpmhead, TAG_NAME);
916 pool_error(pool, 0, "package has no name");
919 if (!strcmp(name, "gpg-pubkey"))
921 s->name = pool_str2id(pool, name, 1);
922 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
923 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
924 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
927 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
928 s->arch = ARCH_NOSRC;
933 s->arch = ARCH_NOARCH;
934 evr = headtoevr(rpmhead);
935 s->evr = pool_str2id(pool, evr, 1);
936 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
938 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
939 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
940 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
941 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags);
942 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
943 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
945 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0);
946 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0);
947 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0);
948 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0);
950 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
951 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
958 unsigned long long u64;
960 handle = s - pool->solvables;
961 str = headstring(rpmhead, TAG_SUMMARY);
963 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
964 str = headstring(rpmhead, TAG_DESCRIPTION);
966 set_description_author(data, handle, str);
967 str = headstring(rpmhead, TAG_GROUP);
969 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
970 str = headstring(rpmhead, TAG_LICENSE);
972 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
973 str = headstring(rpmhead, TAG_URL);
975 repodata_set_str(data, handle, SOLVABLE_URL, str);
976 str = headstring(rpmhead, TAG_DISTRIBUTION);
978 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
979 str = headstring(rpmhead, TAG_PACKAGER);
981 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
982 if ((flags & RPM_ADD_WITH_PKGID) != 0)
984 unsigned char *chksum;
985 unsigned int chksumsize;
986 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
987 if (chksum && chksumsize == 16)
988 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
990 if ((flags & RPM_ADD_WITH_HDRID) != 0)
992 str = headstring(rpmhead, TAG_SHA1HEADER);
993 if (str && strlen(str) == 40)
994 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
995 else if (str && strlen(str) == 64)
996 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
998 u32 = headint32(rpmhead, TAG_BUILDTIME);
1000 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1001 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1003 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1004 u64 = headint64(rpmhead, TAG_LONGSIZE);
1006 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1009 u32 = headint32(rpmhead, TAG_SIZE);
1011 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1014 repodata_set_sourcepkg(data, handle, sourcerpm);
1015 if ((flags & RPM_ADD_TRIGGERS) != 0)
1018 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1021 for (; (id = repo->idarraydata[ida]) != 0; ida++)
1023 /* we currently do not support rel ids in incore data, so
1024 * strip off versioning information */
1025 while (ISRELDEP(id))
1027 Reldep *rd = GETRELDEP(pool, id);
1032 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1036 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1037 addfilelist(data, handle, rpmhead, flags);
1038 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1039 addchangelog(data, handle, rpmhead);
1046 /******************************************************************/
1047 /* Rpm Database stuff
1054 RpmHead *rpmhead; /* header storage space */
1058 DB_ENV *dbenv; /* database environment */
1059 DB *db; /* packages database */
1060 int byteswapped; /* endianess of packages database */
1061 int is_ostree; /* read-only db that lives in /usr/share/rpm */
1069 #define ENTRIES_BLOCK 255
1070 #define NAMEDATA_BLOCK 1023
1073 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1076 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1078 # if defined(WORDS_BIGENDIAN)
1083 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1085 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1089 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1092 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1094 # if defined(WORDS_BIGENDIAN)
1099 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1101 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1107 serialize_dbenv_ops(struct rpmdbstate *state)
1109 char lpath[PATH_MAX];
1114 snprintf(lpath, PATH_MAX, "%s/var/lib/rpm/.dbenv.lock", state->rootdir ? state->rootdir : "");
1115 oldmask = umask(022);
1116 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
1120 memset(&fl, 0, sizeof(fl));
1121 fl.l_type = F_WRLCK;
1122 fl.l_whence = SEEK_SET;
1125 if (fcntl(fd, F_SETLKW, &fl) != -1)
1135 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1137 opendbenv(struct rpmdbstate *state)
1139 const char *rootdir = state->rootdir;
1140 char dbpath[PATH_MAX];
1144 if (db_env_create(&dbenv, 0))
1145 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1146 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1147 dbenv->set_thread_count(dbenv, 8);
1149 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1150 if (access(dbpath, W_OK) == -1)
1152 snprintf(dbpath, PATH_MAX, "%s/usr/share/rpm/Packages", rootdir ? rootdir : "");
1153 if (access(dbpath, R_OK) == 0)
1154 state->is_ostree = 1;
1155 snprintf(dbpath, PATH_MAX, "%s%s", rootdir ? rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1156 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1161 int serialize_fd = serialize_dbenv_ops(state);
1162 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1163 if (serialize_fd >= 0)
1164 close(serialize_fd);
1166 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1171 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1172 dbenv->close(dbenv, 0);
1175 state->dbenv = dbenv;
1180 closedbenv(struct rpmdbstate *state)
1183 uint32_t eflags = 0;
1189 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
1190 if (!(eflags & DB_PRIVATE))
1192 int serialize_fd = serialize_dbenv_ops(state);
1193 state->dbenv->close(state->dbenv, 0);
1194 if (serialize_fd >= 0)
1195 close(serialize_fd);
1198 state->dbenv->close(state->dbenv, 0);
1200 state->dbenv->close(state->dbenv, 0);
1206 openpkgdb(struct rpmdbstate *state)
1208 if (state->dbopened)
1209 return state->dbopened > 0 ? 1 : 0;
1210 state->dbopened = -1;
1211 if (!state->dbenv && !opendbenv(state))
1213 if (db_create(&state->db, state->dbenv, 0))
1215 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1220 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1222 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1223 state->db->close(state->db, 0);
1228 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1230 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1231 state->db->close(state->db, 0);
1236 state->dbopened = 1;
1240 /* get the rpmdbids of all installed packages from the Name index database.
1241 * This is much faster then querying the big Packages database */
1242 static struct rpmdbentry *
1243 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1257 struct rpmdbentry *entries = 0;
1264 if (!state->dbenv && !opendbenv(state))
1266 dbenv = state->dbenv;
1267 if (db_create(&db, dbenv, 0))
1269 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1272 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1274 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1278 if (db->get_byteswapped(db, &byteswapped))
1280 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1284 if (db->cursor(db, NULL, &dbc, 0))
1286 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1290 memset(&dbkey, 0, sizeof(dbkey));
1291 memset(&dbdata, 0, sizeof(dbdata));
1294 dbkey.data = (void *)match;
1295 dbkey.size = strlen(match);
1297 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1299 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1303 nameoff = namedatal;
1306 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1307 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1308 namedata[namedatal + dbkey.size] = 0;
1309 namedatal += dbkey.size + 1;
1311 while(dl >= RPM_INDEX_SIZE)
1313 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1314 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1315 entries[nentries].nameoff = nameoff;
1317 dp += RPM_INDEX_SIZE;
1318 dl -= RPM_INDEX_SIZE;
1325 /* make sure that enteries is != 0 if there was no error */
1327 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1328 *nentriesp = nentries;
1330 *namedatap = namedata;
1334 /* retrive header by rpmdbid */
1336 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1338 unsigned char buf[16];
1345 pool_error(state->pool, 0, "illegal rpmdbid");
1348 if (state->dbopened != 1 && !openpkgdb(state))
1350 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1351 memset(&dbkey, 0, sizeof(dbkey));
1352 memset(&dbdata, 0, sizeof(dbdata));
1357 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1359 if (dbdata.size < 8)
1361 pool_error(state->pool, 0, "corrupt rpm database (size)");
1364 if (dbdata.size > state->rpmheadsize)
1366 state->rpmheadsize = dbdata.size + 128;
1367 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1369 rpmhead = state->rpmhead;
1370 memcpy(buf, dbdata.data, 8);
1371 rpmhead->forcebinary = 1;
1372 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1373 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1374 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1376 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1379 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1380 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1384 /* retrive header by berkeleydb cursor */
1386 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1388 unsigned char buf[16];
1394 memset(&dbkey, 0, sizeof(dbkey));
1395 memset(&dbdata, 0, sizeof(dbdata));
1396 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1398 if (dbkey.size != 4)
1399 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1400 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1401 if (dbid == 0) /* the join key */
1403 if (dbdata.size < 8)
1404 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1405 if (dbdata.size > state->rpmheadsize)
1407 state->rpmheadsize = dbdata.size + 128;
1408 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1410 rpmhead = state->rpmhead;
1411 memcpy(buf, dbdata.data, 8);
1412 rpmhead->forcebinary = 1;
1413 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1414 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1415 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1416 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1417 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1418 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1425 freestate(struct rpmdbstate *state)
1431 state->db->close(state->db, 0);
1435 solv_free(state->rootdir);
1436 solv_free(state->rpmhead);
1440 rpm_state_create(Pool *pool, const char *rootdir)
1442 struct rpmdbstate *state;
1443 state = solv_calloc(1, sizeof(*state));
1446 state->rootdir = solv_strdup(rootdir);
1451 rpm_state_free(void *state)
1454 return solv_free(state);
1458 count_headers(struct rpmdbstate *state)
1460 Pool *pool = state->pool;
1461 char dbpath[PATH_MAX];
1462 struct stat statbuf;
1469 snprintf(dbpath, PATH_MAX, "%s%s/Name", state->rootdir ? state->rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1470 if (stat(dbpath, &statbuf))
1472 memset(&dbkey, 0, sizeof(dbkey));
1473 memset(&dbdata, 0, sizeof(dbdata));
1474 if (db_create(&db, state->dbenv, 0))
1476 pool_error(pool, 0, "db_create: %s", strerror(errno));
1479 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1481 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1485 if (db->cursor(db, NULL, &dbc, 0))
1488 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1491 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1492 count += dbdata.size / RPM_INDEX_SIZE;
1498 /******************************************************************/
1501 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1509 from = fromrepo->idarraydata + fromoff;
1510 for (ida = from, cc = 0; *ida; ida++, cc++)
1514 ido = repo_reserve_ids(repo, 0, cc);
1515 ida = repo->idarraydata + ido;
1516 memcpy(ida, from, (cc + 1) * sizeof(Id));
1517 repo->idarraysize += cc + 1;
1521 #define COPYDIR_DIRCACHE_SIZE 512
1523 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1526 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1528 if (cache && cache[did & 255] == did)
1529 return cache[(did & 255) + 256];
1530 return copydir_complex(pool, data, fromdata, did, cache);
1534 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1536 Id parent = dirpool_parent(&fromdata->dirpool, did);
1537 Id compid = dirpool_compid(&fromdata->dirpool, did);
1539 parent = copydir(pool, data, fromdata, parent, cache);
1540 if (data->localpool || fromdata->localpool)
1541 compid = repodata_translate_id(data, fromdata, compid, 1);
1542 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1545 cache[did & 255] = did;
1546 cache[(did & 255) + 256] = compid;
1551 struct solvable_copy_cbdata {
1559 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1561 struct solvable_copy_cbdata *cbdata = vcbdata;
1563 Repodata *data = cbdata->data;
1564 Id handle = cbdata->handle;
1565 Pool *pool = data->repo->pool;
1567 keyname = key->name;
1570 case REPOKEY_TYPE_ID:
1571 case REPOKEY_TYPE_CONSTANTID:
1572 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1574 if (data->localpool || fromdata->localpool)
1575 id = repodata_translate_id(data, fromdata, id, 1);
1576 if (key->type == REPOKEY_TYPE_ID)
1577 repodata_set_id(data, handle, keyname, id);
1578 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1579 repodata_set_constantid(data, handle, keyname, id);
1581 repodata_add_idarray(data, handle, keyname, id);
1583 case REPOKEY_TYPE_STR:
1584 repodata_set_str(data, handle, keyname, kv->str);
1586 case REPOKEY_TYPE_VOID:
1587 repodata_set_void(data, handle, keyname);
1589 case REPOKEY_TYPE_NUM:
1590 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1592 case REPOKEY_TYPE_CONSTANT:
1593 repodata_set_constant(data, handle, keyname, kv->num);
1595 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1597 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1598 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1600 case REPOKEY_TYPE_DIRSTRARRAY:
1602 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1603 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1605 case REPOKEY_TYPE_FLEXARRAY:
1608 assert(cbdata->subhandle);
1609 cbdata->handle = cbdata->subhandle;
1610 cbdata->subhandle = 0;
1615 assert(!cbdata->subhandle);
1616 cbdata->subhandle = cbdata->handle;
1618 cbdata->handle = repodata_new_handle(data);
1619 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1622 if (solv_chksum_len(key->type))
1624 repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1633 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1636 Repo *repo = s->repo;
1637 Pool *pool = repo->pool;
1638 Repo *fromrepo = r->repo;
1639 struct solvable_copy_cbdata cbdata;
1641 /* copy solvable data */
1645 s->vendor = r->vendor;
1646 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1647 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1648 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1649 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1650 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1651 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1652 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1653 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1655 /* copy all attributes */
1659 cbdata.handle = s - pool->solvables;
1660 cbdata.subhandle = 0;
1661 cbdata.dircache = dircache;
1662 p = r - fromrepo->pool->solvables;
1664 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1666 FOR_REPODATAS(fromrepo, i, data)
1668 if (p >= data->start && p < data->end)
1669 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1670 cbdata.dircache = 0; /* only for first repodata */
1675 /* used to sort entries by package name that got returned in some database order */
1677 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1679 struct rpmdbentry const *a = va, *b = vb;
1680 char *namedata = dp;
1682 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1685 return a->rpmdbid - b->rpmdbid;
1689 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1692 Pool *pool = repo->pool;
1693 Solvable *a = pool->solvables + *(Id *)va;
1694 Solvable *b = pool->solvables + *(Id *)vb;
1697 if (a->name != b->name)
1698 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1699 rpmdbid = repo->rpmdbid;
1700 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1704 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1706 Pool *pool = repo->pool;
1709 tmp = pool->solvables[pa];
1710 pool->solvables[pa] = pool->solvables[pb];
1711 pool->solvables[pb] = tmp;
1714 Id tmpid = repo->rpmdbid[pa - repo->start];
1715 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1716 repo->rpmdbid[pb - repo->start] = tmpid;
1718 /* only works if nothing is already internalized! */
1720 repodata_swap_attrs(data, pa, pb);
1724 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1727 memset(cookie, 0, 32);
1728 cookie[3] = RPMDB_COOKIE_VERSION;
1729 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1730 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1731 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1733 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1735 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1737 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1739 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1745 * read rpm db as repo
1750 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1752 Pool *pool = repo->pool;
1753 char dbpath[PATH_MAX];
1754 struct stat packagesstat;
1755 unsigned char newcookie[32];
1756 const unsigned char *oldcookie = 0;
1757 Id oldcookietype = 0;
1759 int count = 0, done = 0;
1760 struct rpmdbstate state;
1765 now = solv_timems(0);
1766 memset(&state, 0, sizeof(state));
1768 if (flags & REPO_USE_ROOTDIR)
1769 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1771 data = repo_add_repodata(repo, flags);
1773 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1775 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1780 if (!opendbenv(&state))
1782 solv_free(state.rootdir);
1786 /* XXX: should get ro lock of Packages database! */
1787 snprintf(dbpath, PATH_MAX, "%s%s/Packages", state.rootdir ? state.rootdir : "", state.is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1788 if (stat(dbpath, &packagesstat))
1790 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1794 mkrpmdbcookie(&packagesstat, newcookie, flags);
1795 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1798 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1799 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1801 int solvstart = 0, solvend = 0;
1805 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1806 repo_empty(ref, 1); /* get it out of the way */
1807 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1808 count = count_headers(&state);
1809 if (!openpkgdb(&state))
1814 if (state.db->cursor(state.db, NULL, &dbc, 0))
1817 return pool_error(pool, -1, "db->cursor failed");
1821 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1831 s = pool_id2solvable(pool, repo_add_solvable(repo));
1833 solvstart = s - pool->solvables;
1834 solvend = s - pool->solvables + 1;
1837 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1838 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1839 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1846 /* We can reuse this solvable, but make sure it's still
1847 associated with this repo. */
1848 memset(s, 0, sizeof(*s));
1851 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1855 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1856 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1862 /* oops, could not reuse. free it instead */
1863 repo_free_solvable(repo, s - pool->solvables, 1);
1867 /* now sort all solvables in the new solvstart..solvend block */
1868 if (solvend - solvstart > 1)
1870 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1871 for (i = solvstart; i < solvend; i++)
1872 pkgids[i - solvstart] = i;
1873 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1875 for (i = solvstart; i < solvend; i++)
1877 int j = pkgids[i - solvstart];
1879 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1881 swap_solvables(repo, data, i, j);
1888 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1889 struct rpmdbentry *entries = 0, *rp;
1892 unsigned int refmask, h;
1896 memset(dircache, 0, sizeof(dircache));
1898 /* get ids of installed rpms */
1899 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1908 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1910 /* create hash from dbid to ref */
1911 refmask = mkmask(ref->nsolvables);
1912 refhash = solv_calloc(refmask + 1, sizeof(Id));
1913 for (i = 0; i < ref->end - ref->start; i++)
1915 if (!ref->rpmdbid[i])
1917 h = ref->rpmdbid[i] & refmask;
1919 h = (h + 317) & refmask;
1920 refhash[h] = i + 1; /* make it non-zero */
1923 /* count the misses, they will cost us time */
1924 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1926 for (i = 0, rp = entries; i < nentries; i++, rp++)
1930 Id dbid = rp->rpmdbid;
1932 while ((id = refhash[h]))
1934 if (ref->rpmdbid[id - 1] == dbid)
1936 h = (h + 317) & refmask;
1945 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1946 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1948 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1950 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1952 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1954 Id dbid = rp->rpmdbid;
1955 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1959 while ((id = refhash[h]))
1961 if (ref->rpmdbid[id - 1] == dbid)
1963 h = (h + 317) & refmask;
1967 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1970 solvable_copy(s, r, data, dircache);
1975 res = getrpmdbid(&state, dbid);
1979 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1982 solv_free(namedata);
1986 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1987 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1991 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1992 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1997 solv_free(namedata);
1999 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
2004 if (!(flags & REPO_NO_INTERNALIZE))
2005 repodata_internalize(data);
2006 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
2007 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
2008 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
2009 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
2010 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
2015 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
2021 return repo_add_rpmdb(repo, 0, flags);
2022 ref = repo_create(repo->pool, "add_rpmdb_reffp");
2023 if (repo_add_solv(ref, fp, 0) != 0)
2028 if (ref && ref->start == ref->end)
2034 repo_disable_paging(ref);
2035 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
2041 static inline unsigned int
2042 getu32(const unsigned char *dp)
2044 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
2049 repo_add_rpm(Repo *repo, const char *rpm, int flags)
2051 unsigned int sigdsize, sigcnt, l;
2052 Pool *pool = repo->pool;
2054 RpmHead *rpmhead = 0;
2055 int rpmheadsize = 0;
2056 char *payloadformat;
2058 unsigned char lead[4096];
2059 int headerstart, headerend;
2062 unsigned char pkgid[16];
2063 unsigned char leadsigid[16];
2064 unsigned char hdrid[32];
2065 int pkgidtype, leadsigidtype, hdridtype;
2067 Chksum *chksumh = 0;
2068 Chksum *leadsigchksumh = 0;
2069 int forcebinary = 0;
2071 data = repo_add_repodata(repo, flags);
2073 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
2074 chksumtype = REPOKEY_TYPE_SHA256;
2075 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
2076 chksumtype = REPOKEY_TYPE_SHA1;
2078 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
2080 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
2083 if (fstat(fileno(fp), &stb))
2085 pool_error(pool, -1, "fstat: %s", strerror(errno));
2090 chksumh = solv_chksum_create(chksumtype);
2091 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
2092 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
2093 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2095 pool_error(pool, -1, "%s: not a rpm", rpm);
2099 forcebinary = lead[6] != 0 || lead[7] != 1;
2101 solv_chksum_add(chksumh, lead, 96 + 16);
2103 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
2104 if (lead[78] != 0 || lead[79] != 5)
2106 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
2110 if (getu32(lead + 96) != 0x8eade801)
2112 pool_error(pool, -1, "%s: bad signature header", rpm);
2116 sigcnt = getu32(lead + 96 + 8);
2117 sigdsize = getu32(lead + 96 + 12);
2118 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2120 pool_error(pool, -1, "%s: bad signature header", rpm);
2124 sigdsize += sigcnt * 16;
2125 sigdsize = (sigdsize + 7) & ~7;
2126 headerstart = 96 + 16 + sigdsize;
2127 pkgidtype = leadsigidtype = hdridtype = 0;
2128 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
2130 /* extract pkgid or hdrid from the signature header */
2131 if (sigdsize > rpmheadsize)
2133 rpmheadsize = sigdsize + 128;
2134 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2136 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
2138 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2143 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
2145 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
2146 rpmhead->forcebinary = 0;
2147 rpmhead->cnt = sigcnt;
2148 rpmhead->dcnt = sigdsize - sigcnt * 16;
2149 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2150 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2152 unsigned char *chksum;
2153 unsigned int chksumsize;
2154 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2155 if (chksum && chksumsize == 16)
2157 pkgidtype = REPOKEY_TYPE_MD5;
2158 memcpy(pkgid, chksum, 16);
2161 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2163 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2164 if (str && strlen(str) == 40)
2166 if (solv_hex2bin(&str, hdrid, 20) == 20)
2167 hdridtype = REPOKEY_TYPE_SHA1;
2169 else if (str && strlen(str) == 64)
2171 if (solv_hex2bin(&str, hdrid, 32) == 32)
2172 hdridtype = REPOKEY_TYPE_SHA256;
2178 /* just skip the signature header */
2181 l = sigdsize > 4096 ? 4096 : sigdsize;
2182 if (fread(lead, l, 1, fp) != 1)
2184 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2189 solv_chksum_add(chksumh, lead, l);
2191 solv_chksum_add(leadsigchksumh, lead, l);
2197 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2198 leadsigidtype = REPOKEY_TYPE_MD5;
2200 if (fread(lead, 16, 1, fp) != 1)
2202 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2207 solv_chksum_add(chksumh, lead, 16);
2208 if (getu32(lead) != 0x8eade801)
2210 pool_error(pool, -1, "%s: bad header", rpm);
2214 sigcnt = getu32(lead + 8);
2215 sigdsize = getu32(lead + 12);
2216 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2218 pool_error(pool, -1, "%s: bad header", rpm);
2222 l = sigdsize + sigcnt * 16;
2223 headerend = headerstart + 16 + l;
2224 if (l > rpmheadsize)
2226 rpmheadsize = l + 128;
2227 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2229 if (fread(rpmhead->data, l, 1, fp) != 1)
2231 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2236 solv_chksum_add(chksumh, rpmhead->data, l);
2237 rpmhead->forcebinary = forcebinary;
2238 rpmhead->cnt = sigcnt;
2239 rpmhead->dcnt = sigdsize;
2240 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2241 if (headexists(rpmhead, TAG_PATCHESNAME))
2243 /* this is a patch rpm, ignore */
2244 pool_error(pool, -1, "%s: is patch rpm", rpm);
2246 solv_chksum_free(chksumh, 0);
2250 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2251 if (payloadformat && !strcmp(payloadformat, "drpm"))
2253 /* this is a delta rpm */
2254 pool_error(pool, -1, "%s: is delta rpm", rpm);
2256 solv_chksum_free(chksumh, 0);
2261 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2262 solv_chksum_add(chksumh, lead, l);
2264 s = pool_id2solvable(pool, repo_add_solvable(repo));
2265 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2267 repo_free_solvable(repo, s - pool->solvables, 1);
2268 solv_chksum_free(chksumh, 0);
2272 if (!(flags & REPO_NO_LOCATION))
2273 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2274 if (S_ISREG(stb.st_mode))
2275 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2276 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2278 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2280 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2282 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2285 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2286 chksumh = solv_chksum_free(chksumh, 0);
2289 if (!(flags & REPO_NO_INTERNALIZE))
2290 repodata_internalize(data);
2291 return s - pool->solvables;
2295 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2297 Pool *pool = repo->pool;
2299 RpmHead *rpmhead = rpmhandle;
2301 char *payloadformat;
2303 data = repo_add_repodata(repo, flags);
2304 if (headexists(rpmhead, TAG_PATCHESNAME))
2306 pool_error(pool, -1, "is a patch rpm");
2309 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2310 if (payloadformat && !strcmp(payloadformat, "drpm"))
2312 /* this is a delta rpm */
2313 pool_error(pool, -1, "is a delta rpm");
2316 s = pool_id2solvable(pool, repo_add_solvable(repo));
2317 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2319 repo_free_solvable(repo, s - pool->solvables, 1);
2322 if (!(flags & REPO_NO_INTERNALIZE))
2323 repodata_internalize(data);
2324 return s - pool->solvables;
2328 linkhash(const char *lt, char *hash)
2331 const unsigned char *str = (const unsigned char *)lt;
2335 while ((c = *str++) != 0)
2337 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2341 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2343 RpmHead *rpmhead = rpmhandle;
2348 unsigned int *di, diidx;
2349 unsigned int *co = 0;
2350 unsigned int *ff = 0;
2351 unsigned int lastdir;
2354 int cnt, dcnt, cnt2;
2359 struct filelistinfo info;
2361 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2364 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2366 for (i = 0; i < dcnt; i++)
2367 (*cb)(cbdata, dn[i], 0);
2371 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2377 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2378 if (!di || cnt != cnt2)
2385 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2386 if (!fm || cnt != cnt2)
2394 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2396 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2397 if (!md || cnt != cnt2)
2407 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2409 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2410 if (!co || cnt != cnt2)
2421 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2423 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2424 if (!ff || cnt != cnt2)
2438 memset(&info, 0, sizeof(info));
2439 for (i = 0; i < cnt; i++)
2441 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2446 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2447 l = l1 + strlen(bn[i]) + 1;
2451 space = solv_realloc(space, spacen);
2453 if (lastdir != diidx)
2455 strcpy(space, dn[diidx]);
2459 strcpy(space + l1, bn[i]);
2460 info.diridx = diidx;
2466 info.digest = md[i];
2467 if (fm && S_ISLNK(fm[i]))
2472 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2478 linkhash(lt[i], md5);
2484 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2490 (*cb)(cbdata, space, &info);
2504 rpm_query(void *rpmhandle, Id what)
2506 const char *name, *arch, *sourcerpm;
2510 RpmHead *rpmhead = rpmhandle;
2515 name = headstring(rpmhead, TAG_NAME);
2518 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2519 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2520 arch = headstring(rpmhead, TAG_ARCH);
2523 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2530 evr = headtoevr(rpmhead);
2531 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2533 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2537 name = headstring(rpmhead, TAG_NAME);
2538 r = solv_strdup(name);
2540 case SOLVABLE_SUMMARY:
2541 name = headstring(rpmhead, TAG_SUMMARY);
2542 r = solv_strdup(name);
2544 case SOLVABLE_DESCRIPTION:
2545 name = headstring(rpmhead, TAG_DESCRIPTION);
2546 r = solv_strdup(name);
2549 r = headtoevr(rpmhead);
2556 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2558 RpmHead *rpmhead = rpmhandle;
2563 case SOLVABLE_INSTALLTIME:
2564 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2565 return u32 ? u32 : notfound;
2571 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2573 struct rpmdbentry *entries;
2576 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2579 queue_empty(rpmdbidq);
2580 for (i = 0; i < nentries; i++)
2581 queue_push(rpmdbidq, entries[i].rpmdbid);
2588 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2590 struct rpmdbstate *state = rpmstate;
2593 r = getrpmdbid(state, rpmdbid);
2595 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2596 return r <= 0 ? 0 : state->rpmhead;
2600 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2602 struct rpmdbstate *state = rpmstate;
2603 /* int headerstart, headerend; */
2605 unsigned int sigdsize, sigcnt, l;
2606 unsigned char lead[4096];
2607 int forcebinary = 0;
2609 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2611 pool_error(state->pool, 0, "%s: not a rpm", name);
2614 forcebinary = lead[6] != 0 || lead[7] != 1;
2615 if (lead[78] != 0 || lead[79] != 5)
2617 pool_error(state->pool, 0, "%s: not a V5 header", name);
2620 if (getu32(lead + 96) != 0x8eade801)
2622 pool_error(state->pool, 0, "%s: bad signature header", name);
2625 sigcnt = getu32(lead + 96 + 8);
2626 sigdsize = getu32(lead + 96 + 12);
2627 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2629 pool_error(state->pool, 0, "%s: bad signature header", name);
2632 sigdsize += sigcnt * 16;
2633 sigdsize = (sigdsize + 7) & ~7;
2634 /* headerstart = 96 + 16 + sigdsize; */
2637 l = sigdsize > 4096 ? 4096 : sigdsize;
2638 if (fread(lead, l, 1, fp) != 1)
2640 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2645 if (fread(lead, 16, 1, fp) != 1)
2647 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2650 if (getu32(lead) != 0x8eade801)
2652 pool_error(state->pool, 0, "%s: bad header", name);
2655 sigcnt = getu32(lead + 8);
2656 sigdsize = getu32(lead + 12);
2657 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2659 pool_error(state->pool, 0, "%s: bad header", name);
2662 l = sigdsize + sigcnt * 16;
2663 /* headerend = headerstart + 16 + l; */
2664 if (l > state->rpmheadsize)
2666 state->rpmheadsize = l + 128;
2667 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2669 rpmhead = state->rpmhead;
2670 if (fread(rpmhead->data, l, 1, fp) != 1)
2672 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2675 rpmhead->forcebinary = forcebinary;
2676 rpmhead->cnt = sigcnt;
2677 rpmhead->dcnt = sigdsize;
2678 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2682 #ifdef ENABLE_RPMDB_BYRPMHEADER
2685 rpm_byrpmh(void *rpmstate, Header h)
2687 struct rpmdbstate *state = rpmstate;
2688 const unsigned char *uh;
2689 unsigned int sigdsize, sigcnt, l;
2693 uh = headerUnload(h);
2695 uh = headerUnload(h, NULL);
2699 sigcnt = getu32(uh);
2700 sigdsize = getu32(uh + 4);
2701 l = sigdsize + sigcnt * 16;
2702 if (l > state->rpmheadsize)
2704 state->rpmheadsize = l + 128;
2705 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2707 rpmhead = state->rpmhead;
2708 memcpy(rpmhead->data, uh + 8, l - 8);
2710 rpmhead->forcebinary = 0;
2711 rpmhead->cnt = sigcnt;
2712 rpmhead->dcnt = sigdsize;
2713 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;