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"
51 /* 3: added triggers */
52 /* 4: fixed triggers */
53 /* 5: fixed checksum copying */
54 #define RPMDB_COOKIE_VERSION 5
57 #define TAG_VERSION 1001
58 #define TAG_RELEASE 1002
59 #define TAG_EPOCH 1003
60 #define TAG_SUMMARY 1004
61 #define TAG_DESCRIPTION 1005
62 #define TAG_BUILDTIME 1006
63 #define TAG_BUILDHOST 1007
64 #define TAG_INSTALLTIME 1008
66 #define TAG_DISTRIBUTION 1010
67 #define TAG_VENDOR 1011
68 #define TAG_LICENSE 1014
69 #define TAG_PACKAGER 1015
70 #define TAG_GROUP 1016
73 #define TAG_FILESIZES 1028
74 #define TAG_FILEMODES 1030
75 #define TAG_FILEMD5S 1035
76 #define TAG_FILELINKTOS 1036
77 #define TAG_FILEFLAGS 1037
78 #define TAG_SOURCERPM 1044
79 #define TAG_PROVIDENAME 1047
80 #define TAG_REQUIREFLAGS 1048
81 #define TAG_REQUIRENAME 1049
82 #define TAG_REQUIREVERSION 1050
83 #define TAG_NOSOURCE 1051
84 #define TAG_NOPATCH 1052
85 #define TAG_CONFLICTFLAGS 1053
86 #define TAG_CONFLICTNAME 1054
87 #define TAG_CONFLICTVERSION 1055
88 #define TAG_TRIGGERNAME 1066
89 #define TAG_TRIGGERVERSION 1067
90 #define TAG_TRIGGERFLAGS 1068
91 #define TAG_CHANGELOGTIME 1080
92 #define TAG_CHANGELOGNAME 1081
93 #define TAG_CHANGELOGTEXT 1082
94 #define TAG_OBSOLETENAME 1090
95 #define TAG_FILEDEVICES 1095
96 #define TAG_FILEINODES 1096
97 #define TAG_SOURCEPACKAGE 1106
98 #define TAG_PROVIDEFLAGS 1112
99 #define TAG_PROVIDEVERSION 1113
100 #define TAG_OBSOLETEFLAGS 1114
101 #define TAG_OBSOLETEVERSION 1115
102 #define TAG_DIRINDEXES 1116
103 #define TAG_BASENAMES 1117
104 #define TAG_DIRNAMES 1118
105 #define TAG_PAYLOADFORMAT 1124
106 #define TAG_PATCHESNAME 1133
107 #define TAG_FILECOLORS 1140
108 #define TAG_OLDSUGGESTSNAME 1156
109 #define TAG_OLDSUGGESTSVERSION 1157
110 #define TAG_OLDSUGGESTSFLAGS 1158
111 #define TAG_OLDENHANCESNAME 1159
112 #define TAG_OLDENHANCESVERSION 1160
113 #define TAG_OLDENHANCESFLAGS 1161
116 #define TAG_DISTEPOCH 1218
119 #define TAG_LONGFILESIZES 5008
120 #define TAG_LONGSIZE 5009
121 #define TAG_RECOMMENDNAME 5046
122 #define TAG_RECOMMENDVERSION 5047
123 #define TAG_RECOMMENDFLAGS 5048
124 #define TAG_SUGGESTNAME 5049
125 #define TAG_SUGGESTVERSION 5050
126 #define TAG_SUGGESTFLAGS 5051
127 #define TAG_SUPPLEMENTNAME 5052
128 #define TAG_SUPPLEMENTVERSION 5053
129 #define TAG_SUPPLEMENTFLAGS 5054
130 #define TAG_ENHANCENAME 5055
131 #define TAG_ENHANCEVERSION 5056
132 #define TAG_ENHANCEFLAGS 5057
135 #define TAG_SIGBASE 256
136 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
137 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
139 #define SIGTAG_SIZE 1000
140 #define SIGTAG_PGP 1002 /* RSA signature */
141 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
142 #define SIGTAG_GPG 1005 /* DSA signature */
144 #define DEP_LESS (1 << 1)
145 #define DEP_GREATER (1 << 2)
146 #define DEP_EQUAL (1 << 3)
147 #define DEP_STRONG (1 << 27)
148 #define DEP_PRE_IN ((1 << 6) | (1 << 9) | (1 << 10))
149 #define DEP_PRE_UN ((1 << 6) | (1 << 11) | (1 << 12))
151 #define FILEFLAG_GHOST (1 << 6)
155 # define RPM_INDEX_SIZE 4 /* just the rpmdbid */
157 # define RPM_INDEX_SIZE 8 /* rpmdbid + array index */
161 typedef struct rpmhead {
165 int forcebinary; /* sigh, see rh#478907 */
166 unsigned char data[1];
170 static inline unsigned char *
171 headfindtag(RpmHead *h, int tag)
174 unsigned char *d, taga[4];
180 for (i = 0; i < h->cnt; i++, d -= 16)
181 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
187 headexists(RpmHead *h, int tag)
189 return headfindtag(h, tag) ? 1 : 0;
192 static unsigned int *
193 headint32array(RpmHead *h, int tag, int *cnt)
195 unsigned int i, o, *r;
196 unsigned char *d = headfindtag(h, tag);
198 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
200 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
201 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
202 if (o + 4 * i > h->dcnt)
205 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
208 for (o = 0; o < i; o++, d += 4)
209 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
213 /* returns the first entry of an integer array */
215 headint32(RpmHead *h, int tag)
218 unsigned char *d = headfindtag(h, tag);
220 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
222 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
223 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
224 if (i == 0 || o + 4 * i > h->dcnt)
227 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
230 static unsigned long long *
231 headint64array(RpmHead *h, int tag, int *cnt)
234 unsigned long long *r;
235 unsigned char *d = headfindtag(h, tag);
237 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
239 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
240 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
241 if (o + 8 * i > h->dcnt)
244 r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
247 for (o = 0; o < i; o++, d += 8)
249 unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
250 r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
255 /* returns the first entry of an 64bit integer array */
256 static unsigned long long
257 headint64(RpmHead *h, int tag)
260 unsigned char *d = headfindtag(h, tag);
261 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
263 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
264 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
265 if (i == 0 || o + 8 * i > h->dcnt)
268 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
269 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
272 static unsigned int *
273 headint16array(RpmHead *h, int tag, int *cnt)
275 unsigned int i, o, *r;
276 unsigned char *d = headfindtag(h, tag);
278 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
280 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
281 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
282 if (o + 4 * i > h->dcnt)
285 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
288 for (o = 0; o < i; o++, d += 2)
289 r[o] = d[0] << 8 | d[1];
294 headstring(RpmHead *h, int tag)
297 unsigned char *d = headfindtag(h, tag);
298 /* 6: STRING, 9: I18NSTRING */
299 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
301 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
304 return (char *)h->dp + o;
308 headstringarray(RpmHead *h, int tag, int *cnt)
311 unsigned char *d = headfindtag(h, tag);
314 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
316 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
317 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
318 r = solv_calloc(i ? i : 1, sizeof(char *));
322 for (o = 0; o < i; o++)
326 d += strlen((char *)d) + 1;
327 if (d >= h->dp + h->dcnt)
336 static unsigned char *
337 headbinary(RpmHead *h, int tag, unsigned int *sizep)
340 unsigned char *d = headfindtag(h, tag);
341 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
343 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
344 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
345 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
352 static char *headtoevr(RpmHead *h)
360 version = headstring(h, TAG_VERSION);
361 release = headstring(h, TAG_RELEASE);
362 epoch = headint32(h, TAG_EPOCH);
363 if (!version || !release)
365 fprintf(stderr, "headtoevr: bad rpm header\n");
368 for (v = version; *v >= '0' && *v <= '9'; v++)
370 if (epoch || (v != version && *v == ':'))
372 char epochbuf[11]; /* 32bit decimal will fit in */
373 sprintf(epochbuf, "%u", epoch);
374 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
375 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
379 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
380 sprintf(evr, "%s-%s", version, release);
382 distepoch = headstring(h, TAG_DISTEPOCH);
383 if (distepoch && *distepoch)
386 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
388 strcpy(evr + l, distepoch);
395 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
397 if (str[solv_validutf8(str)])
399 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
400 repodata_set_str(repodata, handle, tag, ustr);
404 repodata_set_str(repodata, handle, tag, str);
409 * strong: 0: ignore strongness
410 * 1: filter to strong
414 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
418 int i, cc, nc, vc, fc;
420 unsigned int olddeps;
424 n = headstringarray(rpmhead, tagn, &nc);
429 case TAG_SUGGESTNAME:
430 tagn = TAG_OLDSUGGESTSNAME;
431 tagv = TAG_OLDSUGGESTSVERSION;
432 tagf = TAG_OLDSUGGESTSFLAGS;
435 case TAG_ENHANCENAME:
436 tagn = TAG_OLDENHANCESNAME;
437 tagv = TAG_OLDENHANCESVERSION;
438 tagf = TAG_OLDENHANCESFLAGS;
441 case TAG_RECOMMENDNAME:
442 tagn = TAG_OLDSUGGESTSNAME;
443 tagv = TAG_OLDSUGGESTSVERSION;
444 tagf = TAG_OLDSUGGESTSFLAGS;
447 case TAG_SUPPLEMENTNAME:
448 tagn = TAG_OLDENHANCESNAME;
449 tagv = TAG_OLDENHANCESVERSION;
450 tagf = TAG_OLDENHANCESFLAGS;
456 n = headstringarray(rpmhead, tagn, &nc);
461 v = headstringarray(rpmhead, tagv, &vc);
462 f = headint32array(rpmhead, tagf, &fc);
463 if (!v || !f || nc != vc || nc != fc)
465 char *pkgname = rpm_query(rpmhead, 0);
466 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
475 haspre = 0; /* add no prereq marker */
476 premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
477 if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
479 /* we do filtering */
481 for (i = 0; i < nc; i++)
483 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
485 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
486 if (!strncmp(n[i], "rpmlib(", 7))
488 if ((f[i] & premask) != 0)
495 /* no filtering, just look for the first prereq */
496 for (i = 0; i < nc; i++)
497 if ((f[i] & premask) != 0)
510 cc += haspre; /* add slot for the prereq marker */
511 olddeps = repo_reserve_ids(repo, 0, cc);
512 ida = repo->idarraydata + olddeps;
519 haspre = 2; /* pass two: prereqs */
521 *ida++ = SOLVABLE_PREREQMARKER;
523 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
527 if (haspre == 1 && (f[i] & premask) != 0)
529 if (haspre == 2 && (f[i] & premask) == 0)
532 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
533 if (!strncmp(n[i], "rpmlib(", 7))
535 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
539 if ((f[i] & DEP_LESS) != 0)
541 if ((f[i] & DEP_EQUAL) != 0)
543 if ((f[i] & DEP_GREATER) != 0)
545 name = pool_str2id(pool, n[i], 1);
546 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
547 evr = pool_str2id(pool, v[i] + 2, 1);
549 evr = pool_str2id(pool, v[i], 1);
550 *ida++ = pool_rel2id(pool, name, evr, fl, 1);
553 *ida++ = pool_str2id(pool, n[i], 1);
556 repo->idarraysize += cc + 1;
565 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
569 unsigned int *fkb, *fn, *fsz, *fm, *fino;
570 unsigned long long *fsz64;
571 unsigned int inotest[256], inotestok;
575 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
577 /* convert to kbyte */
578 fsz = solv_malloc2(fszc, sizeof(*fsz));
579 for (i = 0; i < fszc; i++)
580 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
583 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
585 /* convert to kbyte */
586 for (i = 0; i < fszc; i++)
588 fsz[i] = fsz[i] / 1024 + 1;
598 /* stupid rpm records sizes of directories, so we have to check the mode */
599 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
600 if (!fm || fc != fszc)
606 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
607 if (!fino || fc != fszc)
615 /* kill hardlinked entries */
617 if (fc < sizeof(inotest))
619 /* quick test just hashing the inode numbers */
620 memset(inotest, 0, sizeof(inotest));
621 for (i = 0; i < fc; i++)
624 if (fsz[i] == 0 || !S_ISREG(fm[i]))
625 continue; /* does not matter */
626 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
627 bit = 1 << (fino[i] & 31);
628 if ((inotest[off] & bit) != 0)
633 inotestok = 1; /* no conflict found */
637 /* hardlinked files are possible, check ino/dev pairs */
638 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
640 unsigned int mask, hash, hh;
641 if (!fdev || fc != fszc)
650 while ((mask & (mask - 1)) != 0)
651 mask = mask & (mask - 1);
653 if (mask > sizeof(inotest)/sizeof(*inotest))
654 fx = solv_calloc(mask, sizeof(unsigned int));
658 memset(fx, 0, mask * sizeof(unsigned int));
661 for (i = 0; i < fc; i++)
663 if (fsz[i] == 0 || !S_ISREG(fm[i]))
665 hash = (fino[i] + fdev[i] * 31) & mask;
667 while ((j = fx[hash]) != 0)
669 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
671 fsz[i] = 0; /* kill entry */
674 hash = (hash + hh++) & mask;
685 /* sum up inode count and kbytes for each directory */
686 fn = solv_calloc(dc, sizeof(unsigned int));
687 fkb = solv_calloc(dc, sizeof(unsigned int));
688 for (i = 0; i < fc; i++)
691 continue; /* corrupt entry */
693 if (fsz[i] == 0 || !S_ISREG(fm[i]))
695 fkb[di[i]] += fsz[i];
700 for (i = 0; i < dc; i++)
706 Solvable *s = data->repo->pool->solvables + handle;
707 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
708 did = repodata_str2dir(data, "/usr/src", 1);
710 continue; /* work around rpm bug */
713 did = repodata_str2dir(data, dn[i], 1);
714 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
721 is_filtered(const char *dir)
725 /* the dirs always have a trailing / in rpm */
726 if (strstr(dir, "bin/"))
728 if (!strncmp(dir, "/etc/", 5))
730 if (!strcmp(dir, "/usr/lib/"))
736 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
744 unsigned int lastdii = -1;
745 int lastfiltered = 0;
749 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
752 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
758 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
767 pool_error(data->repo->pool, 0, "bad filelist");
771 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
773 for (i = 0; i < bnc; i++)
778 if (di[i] == lastdii)
783 continue; /* corrupt entry */
785 if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
787 lastfiltered = is_filtered(dn[di[i]]);
788 if (lastfiltered == 1)
791 did = repodata_str2dir(data, dn[lastdii], 1);
793 did = repodata_str2dir(data, "/", 1);
796 if (b && *b == '/') /* work around rpm bug */
800 if (lastfiltered != 2 || strcmp(b, "sendmail"))
803 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
811 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
816 int i, cnc, cxc, ctc;
819 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
820 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
821 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
822 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
830 for (i = 0; i < ctc; i++)
832 Id h = repodata_new_handle(data);
834 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
836 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
838 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
841 for (i = 0; i < hq.count; i++)
842 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
850 set_description_author(Repodata *data, Id handle, char *str)
853 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
854 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
858 /* oh my, found SUSE special author section */
860 str = solv_strdup(str);
863 while (l > 0 && str[l - 1] == '\n')
866 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
868 aut = str; /* copy over */
869 while (*p == ' ' || *p == '\n')
882 while (aut != str && aut[-1] == '\n')
886 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
890 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
894 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
900 name = headstring(rpmhead, TAG_NAME);
903 pool_error(pool, 0, "package has no name");
906 if (!strcmp(name, "gpg-pubkey"))
908 s->name = pool_str2id(pool, name, 1);
909 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
910 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
911 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
914 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
915 s->arch = ARCH_NOSRC;
920 s->arch = ARCH_NOARCH;
921 evr = headtoevr(rpmhead);
922 s->evr = pool_str2id(pool, evr, 1);
923 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
925 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
926 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
927 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
928 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags);
929 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
930 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
932 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0);
933 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0);
934 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0);
935 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0);
937 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
938 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
945 unsigned long long u64;
947 handle = s - pool->solvables;
948 str = headstring(rpmhead, TAG_SUMMARY);
950 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
951 str = headstring(rpmhead, TAG_DESCRIPTION);
953 set_description_author(data, handle, str);
954 str = headstring(rpmhead, TAG_GROUP);
956 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
957 str = headstring(rpmhead, TAG_LICENSE);
959 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
960 str = headstring(rpmhead, TAG_URL);
962 repodata_set_str(data, handle, SOLVABLE_URL, str);
963 str = headstring(rpmhead, TAG_DISTRIBUTION);
965 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
966 str = headstring(rpmhead, TAG_PACKAGER);
968 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
969 if ((flags & RPM_ADD_WITH_PKGID) != 0)
971 unsigned char *chksum;
972 unsigned int chksumsize;
973 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
974 if (chksum && chksumsize == 16)
975 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
977 if ((flags & RPM_ADD_WITH_HDRID) != 0)
979 str = headstring(rpmhead, TAG_SHA1HEADER);
980 if (str && strlen(str) == 40)
981 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
982 else if (str && strlen(str) == 64)
983 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
985 u32 = headint32(rpmhead, TAG_BUILDTIME);
987 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
988 u32 = headint32(rpmhead, TAG_INSTALLTIME);
990 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
991 u64 = headint64(rpmhead, TAG_LONGSIZE);
993 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
996 u32 = headint32(rpmhead, TAG_SIZE);
998 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1001 repodata_set_sourcepkg(data, handle, sourcerpm);
1002 if ((flags & RPM_ADD_TRIGGERS) != 0)
1005 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1008 for (; (id = repo->idarraydata[ida]) != 0; ida++)
1010 /* we currently do not support rel ids in incore data, so
1011 * strip off versioning information */
1012 while (ISRELDEP(id))
1014 Reldep *rd = GETRELDEP(pool, id);
1019 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1023 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1024 addfilelist(data, handle, rpmhead, flags);
1025 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1026 addchangelog(data, handle, rpmhead);
1033 /******************************************************************/
1034 /* Rpm Database stuff
1041 RpmHead *rpmhead; /* header storage space */
1045 DB_ENV *dbenv; /* database environment */
1046 DB *db; /* packages database */
1047 int byteswapped; /* endianess of packages database */
1055 #define ENTRIES_BLOCK 255
1056 #define NAMEDATA_BLOCK 1023
1059 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1062 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1064 # if defined(WORDS_BIGENDIAN)
1069 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1071 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1075 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1078 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1080 # if defined(WORDS_BIGENDIAN)
1085 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1087 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1093 serialize_dbenv_ops(struct rpmdbstate *state)
1095 char lpath[PATH_MAX];
1100 snprintf(lpath, PATH_MAX, "%s/var/lib/rpm/.dbenv.lock", state->rootdir ? state->rootdir : "");
1101 oldmask = umask(022);
1102 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
1106 memset(&fl, 0, sizeof(fl));
1107 fl.l_type = F_WRLCK;
1108 fl.l_whence = SEEK_SET;
1111 if (fcntl(fd, F_SETLKW, &fl) != -1)
1121 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1123 opendbenv(struct rpmdbstate *state)
1125 const char *rootdir = state->rootdir;
1126 char dbpath[PATH_MAX];
1130 if (db_env_create(&dbenv, 0))
1131 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1132 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1133 dbenv->set_thread_count(dbenv, 8);
1135 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1136 if (access(dbpath, W_OK) == -1)
1138 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1143 int serialize_fd = serialize_dbenv_ops(state);
1144 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1145 if (serialize_fd >= 0)
1146 close(serialize_fd);
1148 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1153 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1154 dbenv->close(dbenv, 0);
1157 state->dbenv = dbenv;
1162 closedbenv(struct rpmdbstate *state)
1165 uint32_t eflags = 0;
1171 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
1172 if (!(eflags & DB_PRIVATE))
1174 int serialize_fd = serialize_dbenv_ops(state);
1175 state->dbenv->close(state->dbenv, 0);
1176 if (serialize_fd >= 0)
1177 close(serialize_fd);
1180 state->dbenv->close(state->dbenv, 0);
1182 state->dbenv->close(state->dbenv, 0);
1188 openpkgdb(struct rpmdbstate *state)
1190 if (state->dbopened)
1191 return state->dbopened > 0 ? 1 : 0;
1192 state->dbopened = -1;
1193 if (!state->dbenv && !opendbenv(state))
1195 if (db_create(&state->db, state->dbenv, 0))
1197 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1202 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1204 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1205 state->db->close(state->db, 0);
1210 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1212 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1213 state->db->close(state->db, 0);
1218 state->dbopened = 1;
1222 /* get the rpmdbids of all installed packages from the Name index database.
1223 * This is much faster then querying the big Packages database */
1224 static struct rpmdbentry *
1225 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1239 struct rpmdbentry *entries = 0;
1246 if (!state->dbenv && !opendbenv(state))
1248 dbenv = state->dbenv;
1249 if (db_create(&db, dbenv, 0))
1251 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1254 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1256 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1260 if (db->get_byteswapped(db, &byteswapped))
1262 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1266 if (db->cursor(db, NULL, &dbc, 0))
1268 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1272 memset(&dbkey, 0, sizeof(dbkey));
1273 memset(&dbdata, 0, sizeof(dbdata));
1276 dbkey.data = (void *)match;
1277 dbkey.size = strlen(match);
1279 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1281 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1285 nameoff = namedatal;
1288 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1289 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1290 namedata[namedatal + dbkey.size] = 0;
1291 namedatal += dbkey.size + 1;
1293 while(dl >= RPM_INDEX_SIZE)
1295 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1296 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1297 entries[nentries].nameoff = nameoff;
1299 dp += RPM_INDEX_SIZE;
1300 dl -= RPM_INDEX_SIZE;
1307 /* make sure that enteries is != 0 if there was no error */
1309 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1310 *nentriesp = nentries;
1312 *namedatap = namedata;
1316 /* retrive header by rpmdbid */
1318 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1320 unsigned char buf[16];
1327 pool_error(state->pool, 0, "illegal rpmdbid");
1330 if (state->dbopened != 1 && !openpkgdb(state))
1332 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1333 memset(&dbkey, 0, sizeof(dbkey));
1334 memset(&dbdata, 0, sizeof(dbdata));
1339 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1341 if (dbdata.size < 8)
1343 pool_error(state->pool, 0, "corrupt rpm database (size)");
1346 if (dbdata.size > state->rpmheadsize)
1348 state->rpmheadsize = dbdata.size + 128;
1349 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1351 rpmhead = state->rpmhead;
1352 memcpy(buf, dbdata.data, 8);
1353 rpmhead->forcebinary = 1;
1354 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1355 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1356 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1358 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1361 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1362 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1366 /* retrive header by berkeleydb cursor */
1368 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1370 unsigned char buf[16];
1376 memset(&dbkey, 0, sizeof(dbkey));
1377 memset(&dbdata, 0, sizeof(dbdata));
1378 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1380 if (dbkey.size != 4)
1381 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1382 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1383 if (dbid == 0) /* the join key */
1385 if (dbdata.size < 8)
1386 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1387 if (dbdata.size > state->rpmheadsize)
1389 state->rpmheadsize = dbdata.size + 128;
1390 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1392 rpmhead = state->rpmhead;
1393 memcpy(buf, dbdata.data, 8);
1394 rpmhead->forcebinary = 1;
1395 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1396 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1397 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1398 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1399 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1400 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1407 freestate(struct rpmdbstate *state)
1413 solv_free(state->rootdir);
1415 state->db->close(state->db, 0);
1418 solv_free(state->rpmhead);
1422 rpm_state_create(Pool *pool, const char *rootdir)
1424 struct rpmdbstate *state;
1425 state = solv_calloc(1, sizeof(*state));
1428 state->rootdir = solv_strdup(rootdir);
1433 rpm_state_free(void *state)
1436 return solv_free(state);
1440 count_headers(struct rpmdbstate *state)
1442 Pool *pool = state->pool;
1443 char dbpath[PATH_MAX];
1444 struct stat statbuf;
1451 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", state->rootdir ? state->rootdir : "");
1452 if (stat(dbpath, &statbuf))
1454 memset(&dbkey, 0, sizeof(dbkey));
1455 memset(&dbdata, 0, sizeof(dbdata));
1456 if (db_create(&db, state->dbenv, 0))
1458 pool_error(pool, 0, "db_create: %s", strerror(errno));
1461 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1463 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1467 if (db->cursor(db, NULL, &dbc, 0))
1470 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1473 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1474 count += dbdata.size / RPM_INDEX_SIZE;
1480 /******************************************************************/
1483 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1491 from = fromrepo->idarraydata + fromoff;
1492 for (ida = from, cc = 0; *ida; ida++, cc++)
1496 ido = repo_reserve_ids(repo, 0, cc);
1497 ida = repo->idarraydata + ido;
1498 memcpy(ida, from, (cc + 1) * sizeof(Id));
1499 repo->idarraysize += cc + 1;
1503 #define COPYDIR_DIRCACHE_SIZE 512
1505 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1508 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1510 if (cache && cache[did & 255] == did)
1511 return cache[(did & 255) + 256];
1512 return copydir_complex(pool, data, fromdata, did, cache);
1516 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1518 Id parent = dirpool_parent(&fromdata->dirpool, did);
1519 Id compid = dirpool_compid(&fromdata->dirpool, did);
1521 parent = copydir(pool, data, fromdata, parent, cache);
1522 if (data->localpool || fromdata->localpool)
1523 compid = repodata_translate_id(data, fromdata, compid, 1);
1524 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1527 cache[did & 255] = did;
1528 cache[(did & 255) + 256] = compid;
1533 struct solvable_copy_cbdata {
1541 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1543 struct solvable_copy_cbdata *cbdata = vcbdata;
1545 Repodata *data = cbdata->data;
1546 Id handle = cbdata->handle;
1547 Pool *pool = data->repo->pool;
1549 keyname = key->name;
1552 case REPOKEY_TYPE_ID:
1553 case REPOKEY_TYPE_CONSTANTID:
1554 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1556 if (data->localpool || fromdata->localpool)
1557 id = repodata_translate_id(data, fromdata, id, 1);
1558 if (key->type == REPOKEY_TYPE_ID)
1559 repodata_set_id(data, handle, keyname, id);
1560 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1561 repodata_set_constantid(data, handle, keyname, id);
1563 repodata_add_idarray(data, handle, keyname, id);
1565 case REPOKEY_TYPE_STR:
1566 repodata_set_str(data, handle, keyname, kv->str);
1568 case REPOKEY_TYPE_VOID:
1569 repodata_set_void(data, handle, keyname);
1571 case REPOKEY_TYPE_NUM:
1572 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1574 case REPOKEY_TYPE_CONSTANT:
1575 repodata_set_constant(data, handle, keyname, kv->num);
1577 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1579 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1580 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1582 case REPOKEY_TYPE_DIRSTRARRAY:
1584 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1585 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1587 case REPOKEY_TYPE_FLEXARRAY:
1590 assert(cbdata->subhandle);
1591 cbdata->handle = cbdata->subhandle;
1592 cbdata->subhandle = 0;
1597 assert(!cbdata->subhandle);
1598 cbdata->subhandle = cbdata->handle;
1600 cbdata->handle = repodata_new_handle(data);
1601 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1603 case REPOKEY_TYPE_MD5:
1604 case REPOKEY_TYPE_SHA1:
1605 case REPOKEY_TYPE_SHA224:
1606 case REPOKEY_TYPE_SHA256:
1607 case REPOKEY_TYPE_SHA384:
1608 case REPOKEY_TYPE_SHA512:
1609 repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1618 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1621 Repo *repo = s->repo;
1622 Pool *pool = repo->pool;
1623 Repo *fromrepo = r->repo;
1624 struct solvable_copy_cbdata cbdata;
1626 /* copy solvable data */
1630 s->vendor = r->vendor;
1631 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1632 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1633 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1634 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1635 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1636 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1637 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1638 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1640 /* copy all attributes */
1644 cbdata.handle = s - pool->solvables;
1645 cbdata.subhandle = 0;
1646 cbdata.dircache = dircache;
1647 p = r - fromrepo->pool->solvables;
1649 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1651 FOR_REPODATAS(fromrepo, i, data)
1653 if (p >= data->start && p < data->end)
1654 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1655 cbdata.dircache = 0; /* only for first repodata */
1660 /* used to sort entries by package name that got returned in some database order */
1662 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1664 struct rpmdbentry const *a = va, *b = vb;
1665 char *namedata = dp;
1667 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1670 return a->rpmdbid - b->rpmdbid;
1674 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1677 Pool *pool = repo->pool;
1678 Solvable *a = pool->solvables + *(Id *)va;
1679 Solvable *b = pool->solvables + *(Id *)vb;
1682 if (a->name != b->name)
1683 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1684 rpmdbid = repo->rpmdbid;
1685 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1689 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1691 Pool *pool = repo->pool;
1694 tmp = pool->solvables[pa];
1695 pool->solvables[pa] = pool->solvables[pb];
1696 pool->solvables[pb] = tmp;
1699 Id tmpid = repo->rpmdbid[pa - repo->start];
1700 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1701 repo->rpmdbid[pb - repo->start] = tmpid;
1703 /* only works if nothing is already internalized! */
1705 repodata_swap_attrs(data, pa, pb);
1709 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1712 memset(cookie, 0, 32);
1713 cookie[3] = RPMDB_COOKIE_VERSION;
1714 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1715 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1716 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1718 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1720 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1722 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1724 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1730 * read rpm db as repo
1735 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1737 Pool *pool = repo->pool;
1738 char dbpath[PATH_MAX];
1739 struct stat packagesstat;
1740 unsigned char newcookie[32];
1741 const unsigned char *oldcookie = 0;
1742 Id oldcookietype = 0;
1744 int count = 0, done = 0;
1745 struct rpmdbstate state;
1750 now = solv_timems(0);
1751 memset(&state, 0, sizeof(state));
1753 if (flags & REPO_USE_ROOTDIR)
1754 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1756 data = repo_add_repodata(repo, flags);
1758 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1760 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1765 if (!opendbenv(&state))
1767 solv_free(state.rootdir);
1771 /* XXX: should get ro lock of Packages database! */
1772 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", state.rootdir ? state.rootdir : "");
1773 if (stat(dbpath, &packagesstat))
1775 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1779 mkrpmdbcookie(&packagesstat, newcookie, flags);
1780 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1783 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1784 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1786 int solvstart = 0, solvend = 0;
1790 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1791 repo_empty(ref, 1); /* get it out of the way */
1792 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1793 count = count_headers(&state);
1794 if (!openpkgdb(&state))
1799 if (state.db->cursor(state.db, NULL, &dbc, 0))
1802 return pool_error(pool, -1, "db->cursor failed");
1806 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1816 s = pool_id2solvable(pool, repo_add_solvable(repo));
1818 solvstart = s - pool->solvables;
1819 solvend = s - pool->solvables + 1;
1822 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1823 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1824 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1831 /* We can reuse this solvable, but make sure it's still
1832 associated with this repo. */
1833 memset(s, 0, sizeof(*s));
1836 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1840 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1841 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1847 /* oops, could not reuse. free it instead */
1848 repo_free_solvable(repo, s - pool->solvables, 1);
1852 /* now sort all solvables in the new solvstart..solvend block */
1853 if (solvend - solvstart > 1)
1855 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1856 for (i = solvstart; i < solvend; i++)
1857 pkgids[i - solvstart] = i;
1858 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1860 for (i = solvstart; i < solvend; i++)
1862 int j = pkgids[i - solvstart];
1864 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1866 swap_solvables(repo, data, i, j);
1873 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1874 struct rpmdbentry *entries = 0, *rp;
1877 unsigned int refmask, h;
1881 memset(dircache, 0, sizeof(dircache));
1883 /* get ids of installed rpms */
1884 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1893 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1895 /* create hash from dbid to ref */
1896 refmask = mkmask(ref->nsolvables);
1897 refhash = solv_calloc(refmask + 1, sizeof(Id));
1898 for (i = 0; i < ref->end - ref->start; i++)
1900 if (!ref->rpmdbid[i])
1902 h = ref->rpmdbid[i] & refmask;
1904 h = (h + 317) & refmask;
1905 refhash[h] = i + 1; /* make it non-zero */
1908 /* count the misses, they will cost us time */
1909 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1911 for (i = 0, rp = entries; i < nentries; i++, rp++)
1915 Id dbid = rp->rpmdbid;
1917 while ((id = refhash[h]))
1919 if (ref->rpmdbid[id - 1] == dbid)
1921 h = (h + 317) & refmask;
1930 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1931 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1933 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1935 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1937 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1939 Id dbid = rp->rpmdbid;
1940 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1944 while ((id = refhash[h]))
1946 if (ref->rpmdbid[id - 1] == dbid)
1948 h = (h + 317) & refmask;
1952 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1955 solvable_copy(s, r, data, dircache);
1960 res = getrpmdbid(&state, dbid);
1964 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1967 solv_free(namedata);
1971 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1972 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1976 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1977 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1982 solv_free(namedata);
1984 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1989 if (!(flags & REPO_NO_INTERNALIZE))
1990 repodata_internalize(data);
1991 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1992 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1993 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1994 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1995 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)));
2000 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
2006 return repo_add_rpmdb(repo, 0, flags);
2007 ref = repo_create(repo->pool, "add_rpmdb_reffp");
2008 if (repo_add_solv(ref, fp, 0) != 0)
2013 if (ref && ref->start == ref->end)
2019 repo_disable_paging(ref);
2020 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
2026 static inline unsigned int
2027 getu32(const unsigned char *dp)
2029 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
2034 repo_add_rpm(Repo *repo, const char *rpm, int flags)
2036 unsigned int sigdsize, sigcnt, l;
2037 Pool *pool = repo->pool;
2039 RpmHead *rpmhead = 0;
2040 int rpmheadsize = 0;
2041 char *payloadformat;
2043 unsigned char lead[4096];
2044 int headerstart, headerend;
2047 unsigned char pkgid[16];
2048 unsigned char leadsigid[16];
2049 unsigned char hdrid[32];
2050 int pkgidtype, leadsigidtype, hdridtype;
2052 Chksum *chksumh = 0;
2053 Chksum *leadsigchksumh = 0;
2054 int forcebinary = 0;
2056 data = repo_add_repodata(repo, flags);
2058 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
2059 chksumtype = REPOKEY_TYPE_SHA256;
2060 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
2061 chksumtype = REPOKEY_TYPE_SHA1;
2063 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
2065 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
2068 if (fstat(fileno(fp), &stb))
2070 pool_error(pool, -1, "fstat: %s", strerror(errno));
2075 chksumh = solv_chksum_create(chksumtype);
2076 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
2077 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
2078 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2080 pool_error(pool, -1, "%s: not a rpm", rpm);
2084 forcebinary = lead[6] != 0 || lead[7] != 1;
2086 solv_chksum_add(chksumh, lead, 96 + 16);
2088 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
2089 if (lead[78] != 0 || lead[79] != 5)
2091 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
2095 if (getu32(lead + 96) != 0x8eade801)
2097 pool_error(pool, -1, "%s: bad signature header", rpm);
2101 sigcnt = getu32(lead + 96 + 8);
2102 sigdsize = getu32(lead + 96 + 12);
2103 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2105 pool_error(pool, -1, "%s: bad signature header", rpm);
2109 sigdsize += sigcnt * 16;
2110 sigdsize = (sigdsize + 7) & ~7;
2111 headerstart = 96 + 16 + sigdsize;
2112 pkgidtype = leadsigidtype = hdridtype = 0;
2113 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
2115 /* extract pkgid or hdrid from the signature header */
2116 if (sigdsize > rpmheadsize)
2118 rpmheadsize = sigdsize + 128;
2119 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2121 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
2123 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2128 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
2130 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
2131 rpmhead->forcebinary = 0;
2132 rpmhead->cnt = sigcnt;
2133 rpmhead->dcnt = sigdsize - sigcnt * 16;
2134 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2135 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2137 unsigned char *chksum;
2138 unsigned int chksumsize;
2139 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2140 if (chksum && chksumsize == 16)
2142 pkgidtype = REPOKEY_TYPE_MD5;
2143 memcpy(pkgid, chksum, 16);
2146 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2148 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2149 if (str && strlen(str) == 40)
2151 if (solv_hex2bin(&str, hdrid, 20) == 20)
2152 hdridtype = REPOKEY_TYPE_SHA1;
2154 else if (str && strlen(str) == 64)
2156 if (solv_hex2bin(&str, hdrid, 32) == 32)
2157 hdridtype = REPOKEY_TYPE_SHA256;
2163 /* just skip the signature header */
2166 l = sigdsize > 4096 ? 4096 : sigdsize;
2167 if (fread(lead, l, 1, fp) != 1)
2169 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2174 solv_chksum_add(chksumh, lead, l);
2176 solv_chksum_add(leadsigchksumh, lead, l);
2182 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2183 leadsigidtype = REPOKEY_TYPE_MD5;
2185 if (fread(lead, 16, 1, fp) != 1)
2187 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2192 solv_chksum_add(chksumh, lead, 16);
2193 if (getu32(lead) != 0x8eade801)
2195 pool_error(pool, -1, "%s: bad header", rpm);
2199 sigcnt = getu32(lead + 8);
2200 sigdsize = getu32(lead + 12);
2201 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2203 pool_error(pool, -1, "%s: bad header", rpm);
2207 l = sigdsize + sigcnt * 16;
2208 headerend = headerstart + 16 + l;
2209 if (l > rpmheadsize)
2211 rpmheadsize = l + 128;
2212 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2214 if (fread(rpmhead->data, l, 1, fp) != 1)
2216 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2221 solv_chksum_add(chksumh, rpmhead->data, l);
2222 rpmhead->forcebinary = forcebinary;
2223 rpmhead->cnt = sigcnt;
2224 rpmhead->dcnt = sigdsize;
2225 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2226 if (headexists(rpmhead, TAG_PATCHESNAME))
2228 /* this is a patch rpm, ignore */
2229 pool_error(pool, -1, "%s: is patch rpm", rpm);
2231 solv_chksum_free(chksumh, 0);
2235 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2236 if (payloadformat && !strcmp(payloadformat, "drpm"))
2238 /* this is a delta rpm */
2239 pool_error(pool, -1, "%s: is delta rpm", rpm);
2241 solv_chksum_free(chksumh, 0);
2246 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2247 solv_chksum_add(chksumh, lead, l);
2249 s = pool_id2solvable(pool, repo_add_solvable(repo));
2250 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2252 repo_free_solvable(repo, s - pool->solvables, 1);
2253 solv_chksum_free(chksumh, 0);
2257 if (!(flags & REPO_NO_LOCATION))
2258 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2259 if (S_ISREG(stb.st_mode))
2260 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2261 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2263 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2265 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2267 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2270 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2271 chksumh = solv_chksum_free(chksumh, 0);
2274 if (!(flags & REPO_NO_INTERNALIZE))
2275 repodata_internalize(data);
2276 return s - pool->solvables;
2280 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2282 Pool *pool = repo->pool;
2284 RpmHead *rpmhead = rpmhandle;
2286 char *payloadformat;
2288 data = repo_add_repodata(repo, flags);
2289 if (headexists(rpmhead, TAG_PATCHESNAME))
2291 pool_error(pool, -1, "is a patch rpm");
2294 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2295 if (payloadformat && !strcmp(payloadformat, "drpm"))
2297 /* this is a delta rpm */
2298 pool_error(pool, -1, "is a delta rpm");
2301 s = pool_id2solvable(pool, repo_add_solvable(repo));
2302 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2304 repo_free_solvable(repo, s - pool->solvables, 1);
2307 if (!(flags & REPO_NO_INTERNALIZE))
2308 repodata_internalize(data);
2309 return s - pool->solvables;
2313 linkhash(const char *lt, char *hash)
2316 const unsigned char *str = (const unsigned char *)lt;
2320 while ((c = *str++) != 0)
2322 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2326 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2328 RpmHead *rpmhead = rpmhandle;
2333 unsigned int *di, diidx;
2334 unsigned int *co = 0;
2335 unsigned int *ff = 0;
2336 unsigned int lastdir;
2339 int cnt, dcnt, cnt2;
2344 struct filelistinfo info;
2346 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2349 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2351 for (i = 0; i < dcnt; i++)
2352 (*cb)(cbdata, dn[i], 0);
2356 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2362 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2363 if (!di || cnt != cnt2)
2370 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2371 if (!fm || cnt != cnt2)
2379 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2381 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2382 if (!md || cnt != cnt2)
2392 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2394 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2395 if (!co || cnt != cnt2)
2406 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2408 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2409 if (!ff || cnt != cnt2)
2423 memset(&info, 0, sizeof(info));
2424 for (i = 0; i < cnt; i++)
2426 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2431 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2432 l = l1 + strlen(bn[i]) + 1;
2436 space = solv_realloc(space, spacen);
2438 if (lastdir != diidx)
2440 strcpy(space, dn[diidx]);
2444 strcpy(space + l1, bn[i]);
2445 info.diridx = diidx;
2451 info.digest = md[i];
2452 if (fm && S_ISLNK(fm[i]))
2457 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2463 linkhash(lt[i], md5);
2469 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2475 (*cb)(cbdata, space, &info);
2489 rpm_query(void *rpmhandle, Id what)
2491 const char *name, *arch, *sourcerpm;
2495 RpmHead *rpmhead = rpmhandle;
2500 name = headstring(rpmhead, TAG_NAME);
2503 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2504 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2505 arch = headstring(rpmhead, TAG_ARCH);
2508 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2515 evr = headtoevr(rpmhead);
2516 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2518 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2522 name = headstring(rpmhead, TAG_NAME);
2523 r = solv_strdup(name);
2525 case SOLVABLE_SUMMARY:
2526 name = headstring(rpmhead, TAG_SUMMARY);
2527 r = solv_strdup(name);
2529 case SOLVABLE_DESCRIPTION:
2530 name = headstring(rpmhead, TAG_DESCRIPTION);
2531 r = solv_strdup(name);
2534 r = headtoevr(rpmhead);
2541 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2543 RpmHead *rpmhead = rpmhandle;
2548 case SOLVABLE_INSTALLTIME:
2549 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2550 return u32 ? u32 : notfound;
2556 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2558 struct rpmdbentry *entries;
2561 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2564 queue_empty(rpmdbidq);
2565 for (i = 0; i < nentries; i++)
2566 queue_push(rpmdbidq, entries[i].rpmdbid);
2573 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2575 struct rpmdbstate *state = rpmstate;
2578 r = getrpmdbid(state, rpmdbid);
2580 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2581 return r <= 0 ? 0 : state->rpmhead;
2585 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2587 struct rpmdbstate *state = rpmstate;
2588 /* int headerstart, headerend; */
2590 unsigned int sigdsize, sigcnt, l;
2591 unsigned char lead[4096];
2592 int forcebinary = 0;
2594 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2596 pool_error(state->pool, 0, "%s: not a rpm", name);
2599 forcebinary = lead[6] != 0 || lead[7] != 1;
2600 if (lead[78] != 0 || lead[79] != 5)
2602 pool_error(state->pool, 0, "%s: not a V5 header", name);
2605 if (getu32(lead + 96) != 0x8eade801)
2607 pool_error(state->pool, 0, "%s: bad signature header", name);
2610 sigcnt = getu32(lead + 96 + 8);
2611 sigdsize = getu32(lead + 96 + 12);
2612 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2614 pool_error(state->pool, 0, "%s: bad signature header", name);
2617 sigdsize += sigcnt * 16;
2618 sigdsize = (sigdsize + 7) & ~7;
2619 /* headerstart = 96 + 16 + sigdsize; */
2622 l = sigdsize > 4096 ? 4096 : sigdsize;
2623 if (fread(lead, l, 1, fp) != 1)
2625 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2630 if (fread(lead, 16, 1, fp) != 1)
2632 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2635 if (getu32(lead) != 0x8eade801)
2637 pool_error(state->pool, 0, "%s: bad header", name);
2640 sigcnt = getu32(lead + 8);
2641 sigdsize = getu32(lead + 12);
2642 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2644 pool_error(state->pool, 0, "%s: bad header", name);
2647 l = sigdsize + sigcnt * 16;
2648 /* headerend = headerstart + 16 + l; */
2649 if (l > state->rpmheadsize)
2651 state->rpmheadsize = l + 128;
2652 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2654 rpmhead = state->rpmhead;
2655 if (fread(rpmhead->data, l, 1, fp) != 1)
2657 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2660 rpmhead->forcebinary = forcebinary;
2661 rpmhead->cnt = sigcnt;
2662 rpmhead->dcnt = sigdsize;
2663 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2667 #ifdef ENABLE_RPMDB_BYRPMHEADER
2670 rpm_byrpmh(void *rpmstate, Header h)
2672 struct rpmdbstate *state = rpmstate;
2673 const unsigned char *uh;
2674 unsigned int sigdsize, sigcnt, l;
2678 uh = headerUnload(h);
2680 uh = headerUnload(h, NULL);
2684 sigcnt = getu32(uh);
2685 sigdsize = getu32(uh + 4);
2686 l = sigdsize + sigcnt * 16;
2687 if (l > state->rpmheadsize)
2689 state->rpmheadsize = l + 128;
2690 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2692 rpmhead = state->rpmhead;
2693 memcpy(rpmhead->data, uh + 8, l - 8);
2695 rpmhead->forcebinary = 0;
2696 rpmhead->cnt = sigcnt;
2697 rpmhead->dcnt = sigdsize;
2698 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;