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))
153 #define DEP_RICH (1 << 29)
155 #define FILEFLAG_GHOST (1 << 6)
159 # define RPM_INDEX_SIZE 4 /* just the rpmdbid */
161 # define RPM_INDEX_SIZE 8 /* rpmdbid + array index */
165 typedef struct rpmhead {
169 int forcebinary; /* sigh, see rh#478907 */
170 unsigned char data[1];
174 static inline unsigned char *
175 headfindtag(RpmHead *h, int tag)
178 unsigned char *d, taga[4];
184 for (i = 0; i < h->cnt; i++, d -= 16)
185 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
191 headexists(RpmHead *h, int tag)
193 return headfindtag(h, tag) ? 1 : 0;
196 static unsigned int *
197 headint32array(RpmHead *h, int tag, int *cnt)
199 unsigned int i, o, *r;
200 unsigned char *d = headfindtag(h, tag);
202 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
204 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
205 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
206 if (o + 4 * i > h->dcnt)
209 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
212 for (o = 0; o < i; o++, d += 4)
213 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
217 /* returns the first entry of an integer array */
219 headint32(RpmHead *h, int tag)
222 unsigned char *d = headfindtag(h, tag);
224 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
226 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
227 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
228 if (i == 0 || o + 4 * i > h->dcnt)
231 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
234 static unsigned long long *
235 headint64array(RpmHead *h, int tag, int *cnt)
238 unsigned long long *r;
239 unsigned char *d = headfindtag(h, tag);
241 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
243 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
244 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
245 if (o + 8 * i > h->dcnt)
248 r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
251 for (o = 0; o < i; o++, d += 8)
253 unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
254 r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
259 /* returns the first entry of an 64bit integer array */
260 static unsigned long long
261 headint64(RpmHead *h, int tag)
264 unsigned char *d = headfindtag(h, tag);
265 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
267 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
268 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
269 if (i == 0 || o + 8 * i > h->dcnt)
272 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
273 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
276 static unsigned int *
277 headint16array(RpmHead *h, int tag, int *cnt)
279 unsigned int i, o, *r;
280 unsigned char *d = headfindtag(h, tag);
282 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
284 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
285 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
286 if (o + 4 * i > h->dcnt)
289 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
292 for (o = 0; o < i; o++, d += 2)
293 r[o] = d[0] << 8 | d[1];
298 headstring(RpmHead *h, int tag)
301 unsigned char *d = headfindtag(h, tag);
302 /* 6: STRING, 9: I18NSTRING */
303 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
305 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
308 return (char *)h->dp + o;
312 headstringarray(RpmHead *h, int tag, int *cnt)
315 unsigned char *d = headfindtag(h, tag);
318 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
320 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
321 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
322 r = solv_calloc(i ? i : 1, sizeof(char *));
326 for (o = 0; o < i; o++)
330 d += strlen((char *)d) + 1;
331 if (d >= h->dp + h->dcnt)
340 static unsigned char *
341 headbinary(RpmHead *h, int tag, unsigned int *sizep)
344 unsigned char *d = headfindtag(h, tag);
345 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
347 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
348 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
349 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
356 static char *headtoevr(RpmHead *h)
364 version = headstring(h, TAG_VERSION);
365 release = headstring(h, TAG_RELEASE);
366 epoch = headint32(h, TAG_EPOCH);
367 if (!version || !release)
369 fprintf(stderr, "headtoevr: bad rpm header\n");
372 for (v = version; *v >= '0' && *v <= '9'; v++)
374 if (epoch || (v != version && *v == ':'))
376 char epochbuf[11]; /* 32bit decimal will fit in */
377 sprintf(epochbuf, "%u", epoch);
378 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
379 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
383 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
384 sprintf(evr, "%s-%s", version, release);
386 distepoch = headstring(h, TAG_DISTEPOCH);
387 if (distepoch && *distepoch)
390 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
392 strcpy(evr + l, distepoch);
399 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
401 if (str[solv_validutf8(str)])
403 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
404 repodata_set_str(repodata, handle, tag, ustr);
408 repodata_set_str(repodata, handle, tag, str);
412 * strong: 0: ignore strongness
413 * 1: filter to strong
417 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
421 int i, cc, nc, vc, fc;
423 unsigned int olddeps;
427 n = headstringarray(rpmhead, tagn, &nc);
432 case TAG_SUGGESTNAME:
433 tagn = TAG_OLDSUGGESTSNAME;
434 tagv = TAG_OLDSUGGESTSVERSION;
435 tagf = TAG_OLDSUGGESTSFLAGS;
438 case TAG_ENHANCENAME:
439 tagn = TAG_OLDENHANCESNAME;
440 tagv = TAG_OLDENHANCESVERSION;
441 tagf = TAG_OLDENHANCESFLAGS;
444 case TAG_RECOMMENDNAME:
445 tagn = TAG_OLDSUGGESTSNAME;
446 tagv = TAG_OLDSUGGESTSVERSION;
447 tagf = TAG_OLDSUGGESTSFLAGS;
450 case TAG_SUPPLEMENTNAME:
451 tagn = TAG_OLDENHANCESNAME;
452 tagv = TAG_OLDENHANCESVERSION;
453 tagf = TAG_OLDENHANCESFLAGS;
459 n = headstringarray(rpmhead, tagn, &nc);
464 v = headstringarray(rpmhead, tagv, &vc);
465 f = headint32array(rpmhead, tagf, &fc);
466 if (!v || !f || nc != vc || nc != fc)
468 char *pkgname = rpm_query(rpmhead, 0);
469 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
478 haspre = 0; /* add no prereq marker */
479 premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
480 if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
482 /* we do filtering */
484 for (i = 0; i < nc; i++)
486 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
488 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
489 if (!strncmp(n[i], "rpmlib(", 7))
491 if ((f[i] & premask) != 0)
498 /* no filtering, just look for the first prereq */
499 for (i = 0; i < nc; i++)
500 if ((f[i] & premask) != 0)
513 cc += haspre; /* add slot for the prereq marker */
514 olddeps = repo_reserve_ids(repo, 0, cc);
515 ida = repo->idarraydata + olddeps;
523 haspre = 2; /* pass two: prereqs */
525 *ida++ = SOLVABLE_PREREQMARKER;
527 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
531 if (haspre == 1 && (f[i] & premask) != 0)
533 if (haspre == 2 && (f[i] & premask) == 0)
536 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
537 if (!strncmp(n[i], "rpmlib(", 7))
539 #ifdef ENABLE_COMPLEX_DEPS
540 if ((f[i] & (DEP_RICH|DEP_LESS| DEP_EQUAL|DEP_GREATER)) == DEP_RICH && n[i][0] == '(')
542 id = pool_parserpmrichdep(pool, n[i]);
550 id = pool_str2id(pool, n[i], 1);
551 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
555 if ((f[i] & DEP_LESS) != 0)
557 if ((f[i] & DEP_EQUAL) != 0)
559 if ((f[i] & DEP_GREATER) != 0)
561 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
562 evr = pool_str2id(pool, v[i] + 2, 1);
564 evr = pool_str2id(pool, v[i], 1);
565 id = pool_rel2id(pool, id, evr, fl, 1);
570 repo->idarraysize += cc + 1;
579 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
583 unsigned int *fkb, *fn, *fsz, *fm, *fino;
584 unsigned long long *fsz64;
585 unsigned int inotest[256], inotestok;
589 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
591 /* convert to kbyte */
592 fsz = solv_malloc2(fszc, sizeof(*fsz));
593 for (i = 0; i < fszc; i++)
594 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
597 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
599 /* convert to kbyte */
600 for (i = 0; i < fszc; i++)
602 fsz[i] = fsz[i] / 1024 + 1;
612 /* stupid rpm records sizes of directories, so we have to check the mode */
613 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
614 if (!fm || fc != fszc)
620 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
621 if (!fino || fc != fszc)
629 /* kill hardlinked entries */
631 if (fc < sizeof(inotest))
633 /* quick test just hashing the inode numbers */
634 memset(inotest, 0, sizeof(inotest));
635 for (i = 0; i < fc; i++)
638 if (fsz[i] == 0 || !S_ISREG(fm[i]))
639 continue; /* does not matter */
640 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
641 bit = 1 << (fino[i] & 31);
642 if ((inotest[off] & bit) != 0)
647 inotestok = 1; /* no conflict found */
651 /* hardlinked files are possible, check ino/dev pairs */
652 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
654 unsigned int mask, hash, hh;
655 if (!fdev || fc != fszc)
664 while ((mask & (mask - 1)) != 0)
665 mask = mask & (mask - 1);
667 if (mask > sizeof(inotest)/sizeof(*inotest))
668 fx = solv_calloc(mask, sizeof(unsigned int));
672 memset(fx, 0, mask * sizeof(unsigned int));
675 for (i = 0; i < fc; i++)
677 if (fsz[i] == 0 || !S_ISREG(fm[i]))
679 hash = (fino[i] + fdev[i] * 31) & mask;
681 while ((j = fx[hash]) != 0)
683 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
685 fsz[i] = 0; /* kill entry */
688 hash = (hash + hh++) & mask;
699 /* sum up inode count and kbytes for each directory */
700 fn = solv_calloc(dc, sizeof(unsigned int));
701 fkb = solv_calloc(dc, sizeof(unsigned int));
702 for (i = 0; i < fc; i++)
705 continue; /* corrupt entry */
707 if (fsz[i] == 0 || !S_ISREG(fm[i]))
709 fkb[di[i]] += fsz[i];
714 for (i = 0; i < dc; i++)
720 Solvable *s = data->repo->pool->solvables + handle;
721 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
722 did = repodata_str2dir(data, "/usr/src", 1);
724 continue; /* work around rpm bug */
727 did = repodata_str2dir(data, dn[i], 1);
728 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
735 is_filtered(const char *dir)
739 /* the dirs always have a trailing / in rpm */
740 if (strstr(dir, "bin/"))
742 if (!strncmp(dir, "/etc/", 5))
744 if (!strcmp(dir, "/usr/lib/"))
750 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
758 unsigned int lastdii = -1;
759 int lastfiltered = 0;
763 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
766 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
772 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
781 pool_error(data->repo->pool, 0, "bad filelist");
785 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
787 for (i = 0; i < bnc; i++)
792 if (di[i] == lastdii)
797 continue; /* corrupt entry */
799 if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
801 lastfiltered = is_filtered(dn[di[i]]);
802 if (lastfiltered == 1)
805 did = repodata_str2dir(data, dn[lastdii], 1);
807 did = repodata_str2dir(data, "/", 1);
810 if (b && *b == '/') /* work around rpm bug */
814 if (lastfiltered != 2 || strcmp(b, "sendmail"))
817 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
825 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
830 int i, cnc, cxc, ctc;
833 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
834 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
835 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
836 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
844 for (i = 0; i < ctc; i++)
846 Id h = repodata_new_handle(data);
848 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
850 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
852 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
855 for (i = 0; i < hq.count; i++)
856 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
864 set_description_author(Repodata *data, Id handle, char *str)
867 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
868 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
872 /* oh my, found SUSE special author section */
874 str = solv_strdup(str);
877 while (l > 0 && str[l - 1] == '\n')
880 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
882 aut = str; /* copy over */
883 while (*p == ' ' || *p == '\n')
896 while (aut != str && aut[-1] == '\n')
900 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
904 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
908 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
914 name = headstring(rpmhead, TAG_NAME);
917 pool_error(pool, 0, "package has no name");
920 if (!strcmp(name, "gpg-pubkey"))
922 s->name = pool_str2id(pool, name, 1);
923 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
924 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
925 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
928 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
929 s->arch = ARCH_NOSRC;
934 s->arch = ARCH_NOARCH;
935 evr = headtoevr(rpmhead);
936 s->evr = pool_str2id(pool, evr, 1);
937 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
939 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
940 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
941 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
942 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags);
943 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
944 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
946 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0);
947 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0);
948 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0);
949 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0);
951 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
952 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
959 unsigned long long u64;
961 handle = s - pool->solvables;
962 str = headstring(rpmhead, TAG_SUMMARY);
964 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
965 str = headstring(rpmhead, TAG_DESCRIPTION);
967 set_description_author(data, handle, str);
968 str = headstring(rpmhead, TAG_GROUP);
970 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
971 str = headstring(rpmhead, TAG_LICENSE);
973 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
974 str = headstring(rpmhead, TAG_URL);
976 repodata_set_str(data, handle, SOLVABLE_URL, str);
977 str = headstring(rpmhead, TAG_DISTRIBUTION);
979 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
980 str = headstring(rpmhead, TAG_PACKAGER);
982 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
983 if ((flags & RPM_ADD_WITH_PKGID) != 0)
985 unsigned char *chksum;
986 unsigned int chksumsize;
987 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
988 if (chksum && chksumsize == 16)
989 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
991 if ((flags & RPM_ADD_WITH_HDRID) != 0)
993 str = headstring(rpmhead, TAG_SHA1HEADER);
994 if (str && strlen(str) == 40)
995 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
996 else if (str && strlen(str) == 64)
997 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
999 u32 = headint32(rpmhead, TAG_BUILDTIME);
1001 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1002 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1004 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1005 u64 = headint64(rpmhead, TAG_LONGSIZE);
1007 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1010 u32 = headint32(rpmhead, TAG_SIZE);
1012 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1015 repodata_set_sourcepkg(data, handle, sourcerpm);
1016 if ((flags & RPM_ADD_TRIGGERS) != 0)
1019 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
1022 for (; (id = repo->idarraydata[ida]) != 0; ida++)
1024 /* we currently do not support rel ids in incore data, so
1025 * strip off versioning information */
1026 while (ISRELDEP(id))
1028 Reldep *rd = GETRELDEP(pool, id);
1033 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1037 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1038 addfilelist(data, handle, rpmhead, flags);
1039 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1040 addchangelog(data, handle, rpmhead);
1047 /******************************************************************/
1048 /* Rpm Database stuff
1055 RpmHead *rpmhead; /* header storage space */
1059 DB_ENV *dbenv; /* database environment */
1060 DB *db; /* packages database */
1061 int byteswapped; /* endianess of packages database */
1062 int is_ostree; /* read-only db that lives in /usr/share/rpm */
1070 #define ENTRIES_BLOCK 255
1071 #define NAMEDATA_BLOCK 1023
1074 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1077 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1079 # if defined(WORDS_BIGENDIAN)
1084 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1086 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1090 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1093 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1095 # if defined(WORDS_BIGENDIAN)
1100 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1102 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1108 serialize_dbenv_ops(struct rpmdbstate *state)
1110 char lpath[PATH_MAX];
1115 snprintf(lpath, PATH_MAX, "%s/var/lib/rpm/.dbenv.lock", state->rootdir ? state->rootdir : "");
1116 oldmask = umask(022);
1117 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
1121 memset(&fl, 0, sizeof(fl));
1122 fl.l_type = F_WRLCK;
1123 fl.l_whence = SEEK_SET;
1126 if (fcntl(fd, F_SETLKW, &fl) != -1)
1136 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1138 opendbenv(struct rpmdbstate *state)
1140 const char *rootdir = state->rootdir;
1141 char dbpath[PATH_MAX];
1145 if (db_env_create(&dbenv, 0))
1146 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1147 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1148 dbenv->set_thread_count(dbenv, 8);
1150 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1151 if (access(dbpath, W_OK) == -1)
1153 snprintf(dbpath, PATH_MAX, "%s/usr/share/rpm/Packages", rootdir ? rootdir : "");
1154 if (access(dbpath, R_OK) == 0)
1155 state->is_ostree = 1;
1156 snprintf(dbpath, PATH_MAX, "%s%s", rootdir ? rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1157 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1162 int serialize_fd = serialize_dbenv_ops(state);
1163 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1164 if (serialize_fd >= 0)
1165 close(serialize_fd);
1167 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1172 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1173 dbenv->close(dbenv, 0);
1176 state->dbenv = dbenv;
1181 closedbenv(struct rpmdbstate *state)
1184 uint32_t eflags = 0;
1190 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
1191 if (!(eflags & DB_PRIVATE))
1193 int serialize_fd = serialize_dbenv_ops(state);
1194 state->dbenv->close(state->dbenv, 0);
1195 if (serialize_fd >= 0)
1196 close(serialize_fd);
1199 state->dbenv->close(state->dbenv, 0);
1201 state->dbenv->close(state->dbenv, 0);
1207 openpkgdb(struct rpmdbstate *state)
1209 if (state->dbopened)
1210 return state->dbopened > 0 ? 1 : 0;
1211 state->dbopened = -1;
1212 if (!state->dbenv && !opendbenv(state))
1214 if (db_create(&state->db, state->dbenv, 0))
1216 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1221 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1223 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1224 state->db->close(state->db, 0);
1229 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1231 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1232 state->db->close(state->db, 0);
1237 state->dbopened = 1;
1241 /* get the rpmdbids of all installed packages from the Name index database.
1242 * This is much faster then querying the big Packages database */
1243 static struct rpmdbentry *
1244 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1258 struct rpmdbentry *entries = 0;
1265 if (!state->dbenv && !opendbenv(state))
1267 dbenv = state->dbenv;
1268 if (db_create(&db, dbenv, 0))
1270 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1273 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1275 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1279 if (db->get_byteswapped(db, &byteswapped))
1281 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1285 if (db->cursor(db, NULL, &dbc, 0))
1287 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1291 memset(&dbkey, 0, sizeof(dbkey));
1292 memset(&dbdata, 0, sizeof(dbdata));
1295 dbkey.data = (void *)match;
1296 dbkey.size = strlen(match);
1298 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1300 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1304 nameoff = namedatal;
1307 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1308 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1309 namedata[namedatal + dbkey.size] = 0;
1310 namedatal += dbkey.size + 1;
1312 while(dl >= RPM_INDEX_SIZE)
1314 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1315 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1316 entries[nentries].nameoff = nameoff;
1318 dp += RPM_INDEX_SIZE;
1319 dl -= RPM_INDEX_SIZE;
1326 /* make sure that enteries is != 0 if there was no error */
1328 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1329 *nentriesp = nentries;
1331 *namedatap = namedata;
1335 /* retrive header by rpmdbid */
1337 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1339 unsigned char buf[16];
1346 pool_error(state->pool, 0, "illegal rpmdbid");
1349 if (state->dbopened != 1 && !openpkgdb(state))
1351 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1352 memset(&dbkey, 0, sizeof(dbkey));
1353 memset(&dbdata, 0, sizeof(dbdata));
1358 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1360 if (dbdata.size < 8)
1362 pool_error(state->pool, 0, "corrupt rpm database (size)");
1365 if (dbdata.size > state->rpmheadsize)
1367 state->rpmheadsize = dbdata.size + 128;
1368 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1370 rpmhead = state->rpmhead;
1371 memcpy(buf, dbdata.data, 8);
1372 rpmhead->forcebinary = 1;
1373 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1374 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1375 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1377 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1380 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1381 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1385 /* retrive header by berkeleydb cursor */
1387 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1389 unsigned char buf[16];
1395 memset(&dbkey, 0, sizeof(dbkey));
1396 memset(&dbdata, 0, sizeof(dbdata));
1397 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1399 if (dbkey.size != 4)
1400 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1401 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1402 if (dbid == 0) /* the join key */
1404 if (dbdata.size < 8)
1405 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1406 if (dbdata.size > state->rpmheadsize)
1408 state->rpmheadsize = dbdata.size + 128;
1409 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1411 rpmhead = state->rpmhead;
1412 memcpy(buf, dbdata.data, 8);
1413 rpmhead->forcebinary = 1;
1414 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1415 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1416 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1417 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1418 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1419 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1426 freestate(struct rpmdbstate *state)
1432 state->db->close(state->db, 0);
1436 solv_free(state->rootdir);
1437 solv_free(state->rpmhead);
1441 rpm_state_create(Pool *pool, const char *rootdir)
1443 struct rpmdbstate *state;
1444 state = solv_calloc(1, sizeof(*state));
1447 state->rootdir = solv_strdup(rootdir);
1452 rpm_state_free(void *state)
1455 return solv_free(state);
1459 count_headers(struct rpmdbstate *state)
1461 Pool *pool = state->pool;
1462 char dbpath[PATH_MAX];
1463 struct stat statbuf;
1470 snprintf(dbpath, PATH_MAX, "%s%s/Name", state->rootdir ? state->rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1471 if (stat(dbpath, &statbuf))
1473 memset(&dbkey, 0, sizeof(dbkey));
1474 memset(&dbdata, 0, sizeof(dbdata));
1475 if (db_create(&db, state->dbenv, 0))
1477 pool_error(pool, 0, "db_create: %s", strerror(errno));
1480 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1482 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1486 if (db->cursor(db, NULL, &dbc, 0))
1489 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1492 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1493 count += dbdata.size / RPM_INDEX_SIZE;
1499 /******************************************************************/
1502 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1510 from = fromrepo->idarraydata + fromoff;
1511 for (ida = from, cc = 0; *ida; ida++, cc++)
1515 ido = repo_reserve_ids(repo, 0, cc);
1516 ida = repo->idarraydata + ido;
1517 memcpy(ida, from, (cc + 1) * sizeof(Id));
1518 repo->idarraysize += cc + 1;
1522 #define COPYDIR_DIRCACHE_SIZE 512
1524 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1527 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1529 if (cache && cache[did & 255] == did)
1530 return cache[(did & 255) + 256];
1531 return copydir_complex(pool, data, fromdata, did, cache);
1535 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1537 Id parent = dirpool_parent(&fromdata->dirpool, did);
1538 Id compid = dirpool_compid(&fromdata->dirpool, did);
1540 parent = copydir(pool, data, fromdata, parent, cache);
1541 if (data->localpool || fromdata->localpool)
1542 compid = repodata_translate_id(data, fromdata, compid, 1);
1543 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1546 cache[did & 255] = did;
1547 cache[(did & 255) + 256] = compid;
1552 struct solvable_copy_cbdata {
1560 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1562 struct solvable_copy_cbdata *cbdata = vcbdata;
1564 Repodata *data = cbdata->data;
1565 Id handle = cbdata->handle;
1566 Pool *pool = data->repo->pool;
1568 keyname = key->name;
1571 case REPOKEY_TYPE_ID:
1572 case REPOKEY_TYPE_CONSTANTID:
1573 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1575 if (data->localpool || fromdata->localpool)
1576 id = repodata_translate_id(data, fromdata, id, 1);
1577 if (key->type == REPOKEY_TYPE_ID)
1578 repodata_set_id(data, handle, keyname, id);
1579 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1580 repodata_set_constantid(data, handle, keyname, id);
1582 repodata_add_idarray(data, handle, keyname, id);
1584 case REPOKEY_TYPE_STR:
1585 repodata_set_str(data, handle, keyname, kv->str);
1587 case REPOKEY_TYPE_VOID:
1588 repodata_set_void(data, handle, keyname);
1590 case REPOKEY_TYPE_NUM:
1591 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1593 case REPOKEY_TYPE_CONSTANT:
1594 repodata_set_constant(data, handle, keyname, kv->num);
1596 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1598 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1599 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1601 case REPOKEY_TYPE_DIRSTRARRAY:
1603 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1604 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1606 case REPOKEY_TYPE_FLEXARRAY:
1609 assert(cbdata->subhandle);
1610 cbdata->handle = cbdata->subhandle;
1611 cbdata->subhandle = 0;
1616 assert(!cbdata->subhandle);
1617 cbdata->subhandle = cbdata->handle;
1619 cbdata->handle = repodata_new_handle(data);
1620 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1623 if (solv_chksum_len(key->type))
1625 repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1634 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1637 Repo *repo = s->repo;
1638 Pool *pool = repo->pool;
1639 Repo *fromrepo = r->repo;
1640 struct solvable_copy_cbdata cbdata;
1642 /* copy solvable data */
1646 s->vendor = r->vendor;
1647 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1648 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1649 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1650 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1651 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1652 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1653 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1654 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1656 /* copy all attributes */
1660 cbdata.handle = s - pool->solvables;
1661 cbdata.subhandle = 0;
1662 cbdata.dircache = dircache;
1663 p = r - fromrepo->pool->solvables;
1665 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1667 FOR_REPODATAS(fromrepo, i, data)
1669 if (p >= data->start && p < data->end)
1670 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1671 cbdata.dircache = 0; /* only for first repodata */
1676 /* used to sort entries by package name that got returned in some database order */
1678 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1680 struct rpmdbentry const *a = va, *b = vb;
1681 char *namedata = dp;
1683 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1686 return a->rpmdbid - b->rpmdbid;
1690 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1693 Pool *pool = repo->pool;
1694 Solvable *a = pool->solvables + *(Id *)va;
1695 Solvable *b = pool->solvables + *(Id *)vb;
1698 if (a->name != b->name)
1699 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1700 rpmdbid = repo->rpmdbid;
1701 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1705 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1707 Pool *pool = repo->pool;
1710 tmp = pool->solvables[pa];
1711 pool->solvables[pa] = pool->solvables[pb];
1712 pool->solvables[pb] = tmp;
1715 Id tmpid = repo->rpmdbid[pa - repo->start];
1716 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1717 repo->rpmdbid[pb - repo->start] = tmpid;
1719 /* only works if nothing is already internalized! */
1721 repodata_swap_attrs(data, pa, pb);
1725 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1728 memset(cookie, 0, 32);
1729 cookie[3] = RPMDB_COOKIE_VERSION;
1730 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1731 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1732 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1734 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1736 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1738 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1740 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1746 * read rpm db as repo
1751 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1753 Pool *pool = repo->pool;
1754 char dbpath[PATH_MAX];
1755 struct stat packagesstat;
1756 unsigned char newcookie[32];
1757 const unsigned char *oldcookie = 0;
1758 Id oldcookietype = 0;
1760 int count = 0, done = 0;
1761 struct rpmdbstate state;
1766 now = solv_timems(0);
1767 memset(&state, 0, sizeof(state));
1769 if (flags & REPO_USE_ROOTDIR)
1770 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1772 data = repo_add_repodata(repo, flags);
1774 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1776 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1781 if (!opendbenv(&state))
1783 solv_free(state.rootdir);
1787 /* XXX: should get ro lock of Packages database! */
1788 snprintf(dbpath, PATH_MAX, "%s%s/Packages", state.rootdir ? state.rootdir : "", state.is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
1789 if (stat(dbpath, &packagesstat))
1791 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1795 mkrpmdbcookie(&packagesstat, newcookie, flags);
1796 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1799 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1800 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1802 int solvstart = 0, solvend = 0;
1806 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1807 repo_empty(ref, 1); /* get it out of the way */
1808 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1809 count = count_headers(&state);
1810 if (!openpkgdb(&state))
1815 if (state.db->cursor(state.db, NULL, &dbc, 0))
1818 return pool_error(pool, -1, "db->cursor failed");
1822 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1832 s = pool_id2solvable(pool, repo_add_solvable(repo));
1834 solvstart = s - pool->solvables;
1835 solvend = s - pool->solvables + 1;
1838 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1839 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1840 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1847 /* We can reuse this solvable, but make sure it's still
1848 associated with this repo. */
1849 memset(s, 0, sizeof(*s));
1852 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1856 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1857 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1863 /* oops, could not reuse. free it instead */
1864 repo_free_solvable(repo, s - pool->solvables, 1);
1868 /* now sort all solvables in the new solvstart..solvend block */
1869 if (solvend - solvstart > 1)
1871 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1872 for (i = solvstart; i < solvend; i++)
1873 pkgids[i - solvstart] = i;
1874 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1876 for (i = solvstart; i < solvend; i++)
1878 int j = pkgids[i - solvstart];
1880 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1882 swap_solvables(repo, data, i, j);
1889 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1890 struct rpmdbentry *entries = 0, *rp;
1893 unsigned int refmask, h;
1897 memset(dircache, 0, sizeof(dircache));
1899 /* get ids of installed rpms */
1900 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1909 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1911 /* create hash from dbid to ref */
1912 refmask = mkmask(ref->nsolvables);
1913 refhash = solv_calloc(refmask + 1, sizeof(Id));
1914 for (i = 0; i < ref->end - ref->start; i++)
1916 if (!ref->rpmdbid[i])
1918 h = ref->rpmdbid[i] & refmask;
1920 h = (h + 317) & refmask;
1921 refhash[h] = i + 1; /* make it non-zero */
1924 /* count the misses, they will cost us time */
1925 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1927 for (i = 0, rp = entries; i < nentries; i++, rp++)
1931 Id dbid = rp->rpmdbid;
1933 while ((id = refhash[h]))
1935 if (ref->rpmdbid[id - 1] == dbid)
1937 h = (h + 317) & refmask;
1946 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1947 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1949 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1951 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1953 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1955 Id dbid = rp->rpmdbid;
1956 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1960 while ((id = refhash[h]))
1962 if (ref->rpmdbid[id - 1] == dbid)
1964 h = (h + 317) & refmask;
1968 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1971 solvable_copy(s, r, data, dircache);
1976 res = getrpmdbid(&state, dbid);
1980 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1983 solv_free(namedata);
1987 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1988 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1992 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1993 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1998 solv_free(namedata);
2000 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
2005 if (!(flags & REPO_NO_INTERNALIZE))
2006 repodata_internalize(data);
2007 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
2008 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
2009 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
2010 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
2011 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)));
2016 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
2022 return repo_add_rpmdb(repo, 0, flags);
2023 ref = repo_create(repo->pool, "add_rpmdb_reffp");
2024 if (repo_add_solv(ref, fp, 0) != 0)
2029 if (ref && ref->start == ref->end)
2035 repo_disable_paging(ref);
2036 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
2042 static inline unsigned int
2043 getu32(const unsigned char *dp)
2045 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
2050 repo_add_rpm(Repo *repo, const char *rpm, int flags)
2052 unsigned int sigdsize, sigcnt, l;
2053 Pool *pool = repo->pool;
2055 RpmHead *rpmhead = 0;
2056 int rpmheadsize = 0;
2057 char *payloadformat;
2059 unsigned char lead[4096];
2060 int headerstart, headerend;
2063 unsigned char pkgid[16];
2064 unsigned char leadsigid[16];
2065 unsigned char hdrid[32];
2066 int pkgidtype, leadsigidtype, hdridtype;
2068 Chksum *chksumh = 0;
2069 Chksum *leadsigchksumh = 0;
2070 int forcebinary = 0;
2072 data = repo_add_repodata(repo, flags);
2074 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
2075 chksumtype = REPOKEY_TYPE_SHA256;
2076 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
2077 chksumtype = REPOKEY_TYPE_SHA1;
2079 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
2081 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
2084 if (fstat(fileno(fp), &stb))
2086 pool_error(pool, -1, "fstat: %s", strerror(errno));
2091 chksumh = solv_chksum_create(chksumtype);
2092 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
2093 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
2094 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2096 pool_error(pool, -1, "%s: not a rpm", rpm);
2100 forcebinary = lead[6] != 0 || lead[7] != 1;
2102 solv_chksum_add(chksumh, lead, 96 + 16);
2104 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
2105 if (lead[78] != 0 || lead[79] != 5)
2107 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
2111 if (getu32(lead + 96) != 0x8eade801)
2113 pool_error(pool, -1, "%s: bad signature header", rpm);
2117 sigcnt = getu32(lead + 96 + 8);
2118 sigdsize = getu32(lead + 96 + 12);
2119 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2121 pool_error(pool, -1, "%s: bad signature header", rpm);
2125 sigdsize += sigcnt * 16;
2126 sigdsize = (sigdsize + 7) & ~7;
2127 headerstart = 96 + 16 + sigdsize;
2128 pkgidtype = leadsigidtype = hdridtype = 0;
2129 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
2131 /* extract pkgid or hdrid from the signature header */
2132 if (sigdsize > rpmheadsize)
2134 rpmheadsize = sigdsize + 128;
2135 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2137 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
2139 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2144 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
2146 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
2147 rpmhead->forcebinary = 0;
2148 rpmhead->cnt = sigcnt;
2149 rpmhead->dcnt = sigdsize - sigcnt * 16;
2150 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2151 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2153 unsigned char *chksum;
2154 unsigned int chksumsize;
2155 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2156 if (chksum && chksumsize == 16)
2158 pkgidtype = REPOKEY_TYPE_MD5;
2159 memcpy(pkgid, chksum, 16);
2162 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2164 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2165 if (str && strlen(str) == 40)
2167 if (solv_hex2bin(&str, hdrid, 20) == 20)
2168 hdridtype = REPOKEY_TYPE_SHA1;
2170 else if (str && strlen(str) == 64)
2172 if (solv_hex2bin(&str, hdrid, 32) == 32)
2173 hdridtype = REPOKEY_TYPE_SHA256;
2179 /* just skip the signature header */
2182 l = sigdsize > 4096 ? 4096 : sigdsize;
2183 if (fread(lead, l, 1, fp) != 1)
2185 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2190 solv_chksum_add(chksumh, lead, l);
2192 solv_chksum_add(leadsigchksumh, lead, l);
2198 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2199 leadsigidtype = REPOKEY_TYPE_MD5;
2201 if (fread(lead, 16, 1, fp) != 1)
2203 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2208 solv_chksum_add(chksumh, lead, 16);
2209 if (getu32(lead) != 0x8eade801)
2211 pool_error(pool, -1, "%s: bad header", rpm);
2215 sigcnt = getu32(lead + 8);
2216 sigdsize = getu32(lead + 12);
2217 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2219 pool_error(pool, -1, "%s: bad header", rpm);
2223 l = sigdsize + sigcnt * 16;
2224 headerend = headerstart + 16 + l;
2225 if (l > rpmheadsize)
2227 rpmheadsize = l + 128;
2228 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2230 if (fread(rpmhead->data, l, 1, fp) != 1)
2232 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2237 solv_chksum_add(chksumh, rpmhead->data, l);
2238 rpmhead->forcebinary = forcebinary;
2239 rpmhead->cnt = sigcnt;
2240 rpmhead->dcnt = sigdsize;
2241 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2242 if (headexists(rpmhead, TAG_PATCHESNAME))
2244 /* this is a patch rpm, ignore */
2245 pool_error(pool, -1, "%s: is patch rpm", rpm);
2247 solv_chksum_free(chksumh, 0);
2251 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2252 if (payloadformat && !strcmp(payloadformat, "drpm"))
2254 /* this is a delta rpm */
2255 pool_error(pool, -1, "%s: is delta rpm", rpm);
2257 solv_chksum_free(chksumh, 0);
2262 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2263 solv_chksum_add(chksumh, lead, l);
2265 s = pool_id2solvable(pool, repo_add_solvable(repo));
2266 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2268 repo_free_solvable(repo, s - pool->solvables, 1);
2269 solv_chksum_free(chksumh, 0);
2273 if (!(flags & REPO_NO_LOCATION))
2274 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2275 if (S_ISREG(stb.st_mode))
2276 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2277 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2279 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2281 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2283 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2286 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2287 chksumh = solv_chksum_free(chksumh, 0);
2290 if (!(flags & REPO_NO_INTERNALIZE))
2291 repodata_internalize(data);
2292 return s - pool->solvables;
2296 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2298 Pool *pool = repo->pool;
2300 RpmHead *rpmhead = rpmhandle;
2302 char *payloadformat;
2304 data = repo_add_repodata(repo, flags);
2305 if (headexists(rpmhead, TAG_PATCHESNAME))
2307 pool_error(pool, -1, "is a patch rpm");
2310 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2311 if (payloadformat && !strcmp(payloadformat, "drpm"))
2313 /* this is a delta rpm */
2314 pool_error(pool, -1, "is a delta rpm");
2317 s = pool_id2solvable(pool, repo_add_solvable(repo));
2318 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2320 repo_free_solvable(repo, s - pool->solvables, 1);
2323 if (!(flags & REPO_NO_INTERNALIZE))
2324 repodata_internalize(data);
2325 return s - pool->solvables;
2329 linkhash(const char *lt, char *hash)
2332 const unsigned char *str = (const unsigned char *)lt;
2336 while ((c = *str++) != 0)
2338 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2342 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2344 RpmHead *rpmhead = rpmhandle;
2349 unsigned int *di, diidx;
2350 unsigned int *co = 0;
2351 unsigned int *ff = 0;
2352 unsigned int lastdir;
2355 int cnt, dcnt, cnt2;
2360 struct filelistinfo info;
2362 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2365 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2367 for (i = 0; i < dcnt; i++)
2368 (*cb)(cbdata, dn[i], 0);
2372 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2378 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2379 if (!di || cnt != cnt2)
2386 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2387 if (!fm || cnt != cnt2)
2395 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2397 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2398 if (!md || cnt != cnt2)
2408 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2410 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2411 if (!co || cnt != cnt2)
2422 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2424 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2425 if (!ff || cnt != cnt2)
2439 memset(&info, 0, sizeof(info));
2440 for (i = 0; i < cnt; i++)
2442 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2447 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2448 l = l1 + strlen(bn[i]) + 1;
2452 space = solv_realloc(space, spacen);
2454 if (lastdir != diidx)
2456 strcpy(space, dn[diidx]);
2460 strcpy(space + l1, bn[i]);
2461 info.diridx = diidx;
2467 info.digest = md[i];
2468 if (fm && S_ISLNK(fm[i]))
2473 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2479 linkhash(lt[i], md5);
2485 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2491 (*cb)(cbdata, space, &info);
2505 rpm_query(void *rpmhandle, Id what)
2507 const char *name, *arch, *sourcerpm;
2511 RpmHead *rpmhead = rpmhandle;
2516 name = headstring(rpmhead, TAG_NAME);
2519 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2520 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2521 arch = headstring(rpmhead, TAG_ARCH);
2524 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2531 evr = headtoevr(rpmhead);
2532 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2534 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2538 name = headstring(rpmhead, TAG_NAME);
2539 r = solv_strdup(name);
2541 case SOLVABLE_SUMMARY:
2542 name = headstring(rpmhead, TAG_SUMMARY);
2543 r = solv_strdup(name);
2545 case SOLVABLE_DESCRIPTION:
2546 name = headstring(rpmhead, TAG_DESCRIPTION);
2547 r = solv_strdup(name);
2550 r = headtoevr(rpmhead);
2557 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2559 RpmHead *rpmhead = rpmhandle;
2564 case SOLVABLE_INSTALLTIME:
2565 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2566 return u32 ? u32 : notfound;
2572 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2574 struct rpmdbentry *entries;
2577 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2580 queue_empty(rpmdbidq);
2581 for (i = 0; i < nentries; i++)
2582 queue_push(rpmdbidq, entries[i].rpmdbid);
2589 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2591 struct rpmdbstate *state = rpmstate;
2594 r = getrpmdbid(state, rpmdbid);
2596 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2597 return r <= 0 ? 0 : state->rpmhead;
2601 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2603 struct rpmdbstate *state = rpmstate;
2604 /* int headerstart, headerend; */
2606 unsigned int sigdsize, sigcnt, l;
2607 unsigned char lead[4096];
2608 int forcebinary = 0;
2610 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2612 pool_error(state->pool, 0, "%s: not a rpm", name);
2615 forcebinary = lead[6] != 0 || lead[7] != 1;
2616 if (lead[78] != 0 || lead[79] != 5)
2618 pool_error(state->pool, 0, "%s: not a V5 header", name);
2621 if (getu32(lead + 96) != 0x8eade801)
2623 pool_error(state->pool, 0, "%s: bad signature header", name);
2626 sigcnt = getu32(lead + 96 + 8);
2627 sigdsize = getu32(lead + 96 + 12);
2628 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2630 pool_error(state->pool, 0, "%s: bad signature header", name);
2633 sigdsize += sigcnt * 16;
2634 sigdsize = (sigdsize + 7) & ~7;
2635 /* headerstart = 96 + 16 + sigdsize; */
2638 l = sigdsize > 4096 ? 4096 : sigdsize;
2639 if (fread(lead, l, 1, fp) != 1)
2641 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2646 if (fread(lead, 16, 1, fp) != 1)
2648 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2651 if (getu32(lead) != 0x8eade801)
2653 pool_error(state->pool, 0, "%s: bad header", name);
2656 sigcnt = getu32(lead + 8);
2657 sigdsize = getu32(lead + 12);
2658 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2660 pool_error(state->pool, 0, "%s: bad header", name);
2663 l = sigdsize + sigcnt * 16;
2664 /* headerend = headerstart + 16 + l; */
2665 if (l > state->rpmheadsize)
2667 state->rpmheadsize = l + 128;
2668 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2670 rpmhead = state->rpmhead;
2671 if (fread(rpmhead->data, l, 1, fp) != 1)
2673 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2676 rpmhead->forcebinary = forcebinary;
2677 rpmhead->cnt = sigcnt;
2678 rpmhead->dcnt = sigdsize;
2679 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2683 #ifdef ENABLE_RPMDB_BYRPMHEADER
2686 rpm_byrpmh(void *rpmstate, Header h)
2688 struct rpmdbstate *state = rpmstate;
2689 const unsigned char *uh;
2690 unsigned int sigdsize, sigcnt, l;
2694 uh = headerUnload(h);
2696 uh = headerUnload(h, NULL);
2700 sigcnt = getu32(uh);
2701 sigdsize = getu32(uh + 4);
2702 l = sigdsize + sigcnt * 16;
2703 if (l > state->rpmheadsize)
2705 state->rpmheadsize = l + 128;
2706 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2708 rpmhead = state->rpmhead;
2709 memcpy(rpmhead->data, uh + 8, l - 8);
2711 rpmhead->forcebinary = 0;
2712 rpmhead->cnt = sigcnt;
2713 rpmhead->dcnt = sigdsize;
2714 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;