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 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
729 unsigned int lastdii = -1;
733 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
736 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
742 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
751 pool_error(data->repo->pool, 0, "bad filelist");
755 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
757 for (i = 0; i < bnc; i++)
762 if (di[i] == lastdii)
767 continue; /* corrupt entry */
768 did = repodata_str2dir(data, dn[di[i]], 1);
770 did = repodata_str2dir(data, "/", 1);
774 if (b && *b == '/') /* work around rpm bug */
776 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
784 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
789 int i, cnc, cxc, ctc;
792 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
793 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
794 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
795 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
803 for (i = 0; i < ctc; i++)
805 Id h = repodata_new_handle(data);
807 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
809 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
811 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
814 for (i = 0; i < hq.count; i++)
815 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
823 set_description_author(Repodata *data, Id handle, char *str)
826 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
827 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
831 /* oh my, found SUSE special author section */
833 str = solv_strdup(str);
836 while (l > 0 && str[l - 1] == '\n')
839 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
841 aut = str; /* copy over */
842 while (*p == ' ' || *p == '\n')
855 while (aut != str && aut[-1] == '\n')
859 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
863 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
867 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
873 name = headstring(rpmhead, TAG_NAME);
876 pool_error(pool, 0, "package has no name");
879 if (!strcmp(name, "gpg-pubkey"))
881 s->name = pool_str2id(pool, name, 1);
882 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
883 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
884 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
887 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
888 s->arch = ARCH_NOSRC;
893 s->arch = ARCH_NOARCH;
894 evr = headtoevr(rpmhead);
895 s->evr = pool_str2id(pool, evr, 1);
896 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
898 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
899 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
900 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
901 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags);
902 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
903 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
905 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0);
906 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0);
907 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0);
908 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0);
910 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
911 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
918 unsigned long long u64;
920 handle = s - pool->solvables;
921 str = headstring(rpmhead, TAG_SUMMARY);
923 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
924 str = headstring(rpmhead, TAG_DESCRIPTION);
926 set_description_author(data, handle, str);
927 str = headstring(rpmhead, TAG_GROUP);
929 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
930 str = headstring(rpmhead, TAG_LICENSE);
932 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
933 str = headstring(rpmhead, TAG_URL);
935 repodata_set_str(data, handle, SOLVABLE_URL, str);
936 str = headstring(rpmhead, TAG_DISTRIBUTION);
938 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
939 str = headstring(rpmhead, TAG_PACKAGER);
941 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
942 if ((flags & RPM_ADD_WITH_PKGID) != 0)
944 unsigned char *chksum;
945 unsigned int chksumsize;
946 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
947 if (chksum && chksumsize == 16)
948 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
950 if ((flags & RPM_ADD_WITH_HDRID) != 0)
952 str = headstring(rpmhead, TAG_SHA1HEADER);
953 if (str && strlen(str) == 40)
954 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
955 else if (str && strlen(str) == 64)
956 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
958 u32 = headint32(rpmhead, TAG_BUILDTIME);
960 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
961 u32 = headint32(rpmhead, TAG_INSTALLTIME);
963 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
964 u64 = headint64(rpmhead, TAG_LONGSIZE);
966 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
969 u32 = headint32(rpmhead, TAG_SIZE);
971 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
974 repodata_set_sourcepkg(data, handle, sourcerpm);
975 if ((flags & RPM_ADD_TRIGGERS) != 0)
978 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
981 for (; (id = repo->idarraydata[ida]) != 0; ida++)
983 /* we currently do not support rel ids in incore data, so
984 * strip off versioning information */
987 Reldep *rd = GETRELDEP(pool, id);
992 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
996 if ((flags & RPM_ADD_NO_FILELIST) == 0)
997 addfilelist(data, handle, rpmhead);
998 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
999 addchangelog(data, handle, rpmhead);
1006 /******************************************************************/
1007 /* Rpm Database stuff
1014 RpmHead *rpmhead; /* header storage space */
1018 DB_ENV *dbenv; /* database environment */
1019 DB *db; /* packages database */
1020 int byteswapped; /* endianess of packages database */
1028 #define ENTRIES_BLOCK 255
1029 #define NAMEDATA_BLOCK 1023
1032 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1035 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1037 # if defined(WORDS_BIGENDIAN)
1042 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1044 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1048 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1051 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1053 # if defined(WORDS_BIGENDIAN)
1058 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1060 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1066 serialize_dbenv_ops(struct rpmdbstate *state)
1068 char lpath[PATH_MAX];
1073 snprintf(lpath, PATH_MAX, "%s/var/lib/rpm/.dbenv.lock", state->rootdir ? state->rootdir : "");
1074 oldmask = umask(022);
1075 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
1079 memset(&fl, 0, sizeof(fl));
1080 fl.l_type = F_WRLCK;
1081 fl.l_whence = SEEK_SET;
1084 if (fcntl(fd, F_SETLKW, &fl) != -1)
1094 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1096 opendbenv(struct rpmdbstate *state)
1098 const char *rootdir = state->rootdir;
1099 char dbpath[PATH_MAX];
1103 if (db_env_create(&dbenv, 0))
1104 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1105 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1106 dbenv->set_thread_count(dbenv, 8);
1108 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1109 if (access(dbpath, W_OK) == -1)
1111 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1116 int serialize_fd = serialize_dbenv_ops(state);
1117 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1118 if (serialize_fd >= 0)
1119 close(serialize_fd);
1121 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1126 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1127 dbenv->close(dbenv, 0);
1130 state->dbenv = dbenv;
1135 closedbenv(struct rpmdbstate *state)
1138 uint32_t eflags = 0;
1144 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
1145 if (!(eflags & DB_PRIVATE))
1147 int serialize_fd = serialize_dbenv_ops(state);
1148 state->dbenv->close(state->dbenv, 0);
1149 if (serialize_fd >= 0)
1150 close(serialize_fd);
1153 state->dbenv->close(state->dbenv, 0);
1155 state->dbenv->close(state->dbenv, 0);
1161 openpkgdb(struct rpmdbstate *state)
1163 if (state->dbopened)
1164 return state->dbopened > 0 ? 1 : 0;
1165 state->dbopened = -1;
1166 if (!state->dbenv && !opendbenv(state))
1168 if (db_create(&state->db, state->dbenv, 0))
1170 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1175 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1177 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1178 state->db->close(state->db, 0);
1183 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1185 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1186 state->db->close(state->db, 0);
1191 state->dbopened = 1;
1195 /* get the rpmdbids of all installed packages from the Name index database.
1196 * This is much faster then querying the big Packages database */
1197 static struct rpmdbentry *
1198 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1212 struct rpmdbentry *entries = 0;
1219 if (!state->dbenv && !opendbenv(state))
1221 dbenv = state->dbenv;
1222 if (db_create(&db, dbenv, 0))
1224 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1227 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1229 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1233 if (db->get_byteswapped(db, &byteswapped))
1235 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1239 if (db->cursor(db, NULL, &dbc, 0))
1241 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1245 memset(&dbkey, 0, sizeof(dbkey));
1246 memset(&dbdata, 0, sizeof(dbdata));
1249 dbkey.data = (void *)match;
1250 dbkey.size = strlen(match);
1252 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1254 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1258 nameoff = namedatal;
1261 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1262 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1263 namedata[namedatal + dbkey.size] = 0;
1264 namedatal += dbkey.size + 1;
1266 while(dl >= RPM_INDEX_SIZE)
1268 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1269 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1270 entries[nentries].nameoff = nameoff;
1272 dp += RPM_INDEX_SIZE;
1273 dl -= RPM_INDEX_SIZE;
1280 /* make sure that enteries is != 0 if there was no error */
1282 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1283 *nentriesp = nentries;
1285 *namedatap = namedata;
1289 /* retrive header by rpmdbid */
1291 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1293 unsigned char buf[16];
1300 pool_error(state->pool, 0, "illegal rpmdbid");
1303 if (state->dbopened != 1 && !openpkgdb(state))
1305 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1306 memset(&dbkey, 0, sizeof(dbkey));
1307 memset(&dbdata, 0, sizeof(dbdata));
1312 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1314 if (dbdata.size < 8)
1316 pool_error(state->pool, 0, "corrupt rpm database (size)");
1319 if (dbdata.size > state->rpmheadsize)
1321 state->rpmheadsize = dbdata.size + 128;
1322 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1324 rpmhead = state->rpmhead;
1325 memcpy(buf, dbdata.data, 8);
1326 rpmhead->forcebinary = 1;
1327 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1328 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1329 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1331 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1334 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1335 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1339 /* retrive header by berkeleydb cursor */
1341 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1343 unsigned char buf[16];
1349 memset(&dbkey, 0, sizeof(dbkey));
1350 memset(&dbdata, 0, sizeof(dbdata));
1351 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1353 if (dbkey.size != 4)
1354 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1355 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1356 if (dbid == 0) /* the join key */
1358 if (dbdata.size < 8)
1359 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1360 if (dbdata.size > state->rpmheadsize)
1362 state->rpmheadsize = dbdata.size + 128;
1363 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1365 rpmhead = state->rpmhead;
1366 memcpy(buf, dbdata.data, 8);
1367 rpmhead->forcebinary = 1;
1368 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1369 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1370 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1371 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1372 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1373 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1380 freestate(struct rpmdbstate *state)
1386 solv_free(state->rootdir);
1388 state->db->close(state->db, 0);
1391 solv_free(state->rpmhead);
1395 rpm_state_create(Pool *pool, const char *rootdir)
1397 struct rpmdbstate *state;
1398 state = solv_calloc(1, sizeof(*state));
1401 state->rootdir = solv_strdup(rootdir);
1406 rpm_state_free(void *state)
1409 return solv_free(state);
1413 count_headers(struct rpmdbstate *state)
1415 Pool *pool = state->pool;
1416 char dbpath[PATH_MAX];
1417 struct stat statbuf;
1424 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", state->rootdir ? state->rootdir : "");
1425 if (stat(dbpath, &statbuf))
1427 memset(&dbkey, 0, sizeof(dbkey));
1428 memset(&dbdata, 0, sizeof(dbdata));
1429 if (db_create(&db, state->dbenv, 0))
1431 pool_error(pool, 0, "db_create: %s", strerror(errno));
1434 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1436 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1440 if (db->cursor(db, NULL, &dbc, 0))
1443 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1446 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1447 count += dbdata.size / RPM_INDEX_SIZE;
1453 /******************************************************************/
1456 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1464 from = fromrepo->idarraydata + fromoff;
1465 for (ida = from, cc = 0; *ida; ida++, cc++)
1469 ido = repo_reserve_ids(repo, 0, cc);
1470 ida = repo->idarraydata + ido;
1471 memcpy(ida, from, (cc + 1) * sizeof(Id));
1472 repo->idarraysize += cc + 1;
1476 #define COPYDIR_DIRCACHE_SIZE 512
1478 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1481 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1483 if (cache && cache[did & 255] == did)
1484 return cache[(did & 255) + 256];
1485 return copydir_complex(pool, data, fromdata, did, cache);
1489 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1491 Id parent = dirpool_parent(&fromdata->dirpool, did);
1492 Id compid = dirpool_compid(&fromdata->dirpool, did);
1494 parent = copydir(pool, data, fromdata, parent, cache);
1495 if (data->localpool || fromdata->localpool)
1496 compid = repodata_translate_id(data, fromdata, compid, 1);
1497 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1500 cache[did & 255] = did;
1501 cache[(did & 255) + 256] = compid;
1506 struct solvable_copy_cbdata {
1514 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1516 struct solvable_copy_cbdata *cbdata = vcbdata;
1518 Repodata *data = cbdata->data;
1519 Id handle = cbdata->handle;
1520 Pool *pool = data->repo->pool;
1522 keyname = key->name;
1525 case REPOKEY_TYPE_ID:
1526 case REPOKEY_TYPE_CONSTANTID:
1527 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1529 if (data->localpool || fromdata->localpool)
1530 id = repodata_translate_id(data, fromdata, id, 1);
1531 if (key->type == REPOKEY_TYPE_ID)
1532 repodata_set_id(data, handle, keyname, id);
1533 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1534 repodata_set_constantid(data, handle, keyname, id);
1536 repodata_add_idarray(data, handle, keyname, id);
1538 case REPOKEY_TYPE_STR:
1539 repodata_set_str(data, handle, keyname, kv->str);
1541 case REPOKEY_TYPE_VOID:
1542 repodata_set_void(data, handle, keyname);
1544 case REPOKEY_TYPE_NUM:
1545 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1547 case REPOKEY_TYPE_CONSTANT:
1548 repodata_set_constant(data, handle, keyname, kv->num);
1550 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1552 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1553 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1555 case REPOKEY_TYPE_DIRSTRARRAY:
1557 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1558 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1560 case REPOKEY_TYPE_FLEXARRAY:
1563 assert(cbdata->subhandle);
1564 cbdata->handle = cbdata->subhandle;
1565 cbdata->subhandle = 0;
1570 assert(!cbdata->subhandle);
1571 cbdata->subhandle = cbdata->handle;
1573 cbdata->handle = repodata_new_handle(data);
1574 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1576 case REPOKEY_TYPE_MD5:
1577 case REPOKEY_TYPE_SHA1:
1578 case REPOKEY_TYPE_SHA256:
1579 repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1588 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1591 Repo *repo = s->repo;
1592 Pool *pool = repo->pool;
1593 Repo *fromrepo = r->repo;
1594 struct solvable_copy_cbdata cbdata;
1596 /* copy solvable data */
1600 s->vendor = r->vendor;
1601 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1602 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1603 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1604 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1605 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1606 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1607 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1608 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1610 /* copy all attributes */
1614 cbdata.handle = s - pool->solvables;
1615 cbdata.subhandle = 0;
1616 cbdata.dircache = dircache;
1617 p = r - fromrepo->pool->solvables;
1619 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1621 FOR_REPODATAS(fromrepo, i, data)
1623 if (p >= data->start && p < data->end)
1624 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1625 cbdata.dircache = 0; /* only for first repodata */
1630 /* used to sort entries by package name that got returned in some database order */
1632 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1634 struct rpmdbentry const *a = va, *b = vb;
1635 char *namedata = dp;
1637 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1640 return a->rpmdbid - b->rpmdbid;
1644 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1647 Pool *pool = repo->pool;
1648 Solvable *a = pool->solvables + *(Id *)va;
1649 Solvable *b = pool->solvables + *(Id *)vb;
1652 if (a->name != b->name)
1653 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1654 rpmdbid = repo->rpmdbid;
1655 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1659 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1661 Pool *pool = repo->pool;
1664 tmp = pool->solvables[pa];
1665 pool->solvables[pa] = pool->solvables[pb];
1666 pool->solvables[pb] = tmp;
1669 Id tmpid = repo->rpmdbid[pa - repo->start];
1670 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1671 repo->rpmdbid[pb - repo->start] = tmpid;
1673 /* only works if nothing is already internalized! */
1675 repodata_swap_attrs(data, pa, pb);
1679 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1682 memset(cookie, 0, 32);
1683 cookie[3] = RPMDB_COOKIE_VERSION;
1684 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1685 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1686 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1688 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1690 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1692 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1694 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1700 * read rpm db as repo
1705 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1707 Pool *pool = repo->pool;
1708 char dbpath[PATH_MAX];
1709 struct stat packagesstat;
1710 unsigned char newcookie[32];
1711 const unsigned char *oldcookie = 0;
1712 Id oldcookietype = 0;
1714 int count = 0, done = 0;
1715 struct rpmdbstate state;
1720 now = solv_timems(0);
1721 memset(&state, 0, sizeof(state));
1723 if (flags & REPO_USE_ROOTDIR)
1724 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1726 data = repo_add_repodata(repo, flags);
1728 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1730 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1735 if (!opendbenv(&state))
1737 solv_free(state.rootdir);
1741 /* XXX: should get ro lock of Packages database! */
1742 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", state.rootdir ? state.rootdir : "");
1743 if (stat(dbpath, &packagesstat))
1745 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1749 mkrpmdbcookie(&packagesstat, newcookie, flags);
1750 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1753 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1754 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1756 int solvstart = 0, solvend = 0;
1760 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1761 repo_empty(ref, 1); /* get it out of the way */
1762 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1763 count = count_headers(&state);
1764 if (!openpkgdb(&state))
1769 if (state.db->cursor(state.db, NULL, &dbc, 0))
1772 return pool_error(pool, -1, "db->cursor failed");
1776 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1786 s = pool_id2solvable(pool, repo_add_solvable(repo));
1788 solvstart = s - pool->solvables;
1789 solvend = s - pool->solvables + 1;
1792 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1793 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1794 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1801 /* We can reuse this solvable, but make sure it's still
1802 associated with this repo. */
1803 memset(s, 0, sizeof(*s));
1806 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1810 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1811 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1817 /* oops, could not reuse. free it instead */
1818 repo_free_solvable(repo, s - pool->solvables, 1);
1822 /* now sort all solvables in the new solvstart..solvend block */
1823 if (solvend - solvstart > 1)
1825 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1826 for (i = solvstart; i < solvend; i++)
1827 pkgids[i - solvstart] = i;
1828 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1830 for (i = solvstart; i < solvend; i++)
1832 int j = pkgids[i - solvstart];
1834 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1836 swap_solvables(repo, data, i, j);
1843 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1844 struct rpmdbentry *entries = 0, *rp;
1847 unsigned int refmask, h;
1851 memset(dircache, 0, sizeof(dircache));
1853 /* get ids of installed rpms */
1854 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1863 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1865 /* create hash from dbid to ref */
1866 refmask = mkmask(ref->nsolvables);
1867 refhash = solv_calloc(refmask + 1, sizeof(Id));
1868 for (i = 0; i < ref->end - ref->start; i++)
1870 if (!ref->rpmdbid[i])
1872 h = ref->rpmdbid[i] & refmask;
1874 h = (h + 317) & refmask;
1875 refhash[h] = i + 1; /* make it non-zero */
1878 /* count the misses, they will cost us time */
1879 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1881 for (i = 0, rp = entries; i < nentries; i++, rp++)
1885 Id dbid = rp->rpmdbid;
1887 while ((id = refhash[h]))
1889 if (ref->rpmdbid[id - 1] == dbid)
1891 h = (h + 317) & refmask;
1900 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1901 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1903 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1905 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1907 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1909 Id dbid = rp->rpmdbid;
1910 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1914 while ((id = refhash[h]))
1916 if (ref->rpmdbid[id - 1] == dbid)
1918 h = (h + 317) & refmask;
1922 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1925 solvable_copy(s, r, data, dircache);
1930 res = getrpmdbid(&state, dbid);
1934 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1937 solv_free(namedata);
1941 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1942 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1946 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1947 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1952 solv_free(namedata);
1954 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1959 if (!(flags & REPO_NO_INTERNALIZE))
1960 repodata_internalize(data);
1961 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1962 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1963 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1964 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1965 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)));
1970 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1976 return repo_add_rpmdb(repo, 0, flags);
1977 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1978 if (repo_add_solv(ref, fp, 0) != 0)
1983 if (ref && ref->start == ref->end)
1989 repo_disable_paging(ref);
1990 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1996 static inline unsigned int
1997 getu32(const unsigned char *dp)
1999 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
2004 repo_add_rpm(Repo *repo, const char *rpm, int flags)
2006 unsigned int sigdsize, sigcnt, l;
2007 Pool *pool = repo->pool;
2009 RpmHead *rpmhead = 0;
2010 int rpmheadsize = 0;
2011 char *payloadformat;
2013 unsigned char lead[4096];
2014 int headerstart, headerend;
2017 unsigned char pkgid[16];
2018 unsigned char leadsigid[16];
2019 unsigned char hdrid[32];
2020 int pkgidtype, leadsigidtype, hdridtype;
2022 Chksum *chksumh = 0;
2023 Chksum *leadsigchksumh = 0;
2024 int forcebinary = 0;
2026 data = repo_add_repodata(repo, flags);
2028 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
2029 chksumtype = REPOKEY_TYPE_SHA256;
2030 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
2031 chksumtype = REPOKEY_TYPE_SHA1;
2033 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
2035 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
2038 if (fstat(fileno(fp), &stb))
2040 pool_error(pool, -1, "fstat: %s", strerror(errno));
2045 chksumh = solv_chksum_create(chksumtype);
2046 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
2047 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
2048 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2050 pool_error(pool, -1, "%s: not a rpm", rpm);
2054 forcebinary = lead[6] != 0 || lead[7] != 1;
2056 solv_chksum_add(chksumh, lead, 96 + 16);
2058 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
2059 if (lead[78] != 0 || lead[79] != 5)
2061 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
2065 if (getu32(lead + 96) != 0x8eade801)
2067 pool_error(pool, -1, "%s: bad signature header", rpm);
2071 sigcnt = getu32(lead + 96 + 8);
2072 sigdsize = getu32(lead + 96 + 12);
2073 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2075 pool_error(pool, -1, "%s: bad signature header", rpm);
2079 sigdsize += sigcnt * 16;
2080 sigdsize = (sigdsize + 7) & ~7;
2081 headerstart = 96 + 16 + sigdsize;
2082 pkgidtype = leadsigidtype = hdridtype = 0;
2083 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
2085 /* extract pkgid or hdrid from the signature header */
2086 if (sigdsize > rpmheadsize)
2088 rpmheadsize = sigdsize + 128;
2089 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2091 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
2093 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2098 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
2100 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
2101 rpmhead->forcebinary = 0;
2102 rpmhead->cnt = sigcnt;
2103 rpmhead->dcnt = sigdsize - sigcnt * 16;
2104 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2105 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2107 unsigned char *chksum;
2108 unsigned int chksumsize;
2109 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2110 if (chksum && chksumsize == 16)
2112 pkgidtype = REPOKEY_TYPE_MD5;
2113 memcpy(pkgid, chksum, 16);
2116 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2118 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2119 if (str && strlen(str) == 40)
2121 if (solv_hex2bin(&str, hdrid, 20) == 20)
2122 hdridtype = REPOKEY_TYPE_SHA1;
2124 else if (str && strlen(str) == 64)
2126 if (solv_hex2bin(&str, hdrid, 32) == 32)
2127 hdridtype = REPOKEY_TYPE_SHA256;
2133 /* just skip the signature header */
2136 l = sigdsize > 4096 ? 4096 : sigdsize;
2137 if (fread(lead, l, 1, fp) != 1)
2139 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2144 solv_chksum_add(chksumh, lead, l);
2146 solv_chksum_add(leadsigchksumh, lead, l);
2152 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2153 leadsigidtype = REPOKEY_TYPE_MD5;
2155 if (fread(lead, 16, 1, fp) != 1)
2157 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2162 solv_chksum_add(chksumh, lead, 16);
2163 if (getu32(lead) != 0x8eade801)
2165 pool_error(pool, -1, "%s: bad header", rpm);
2169 sigcnt = getu32(lead + 8);
2170 sigdsize = getu32(lead + 12);
2171 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2173 pool_error(pool, -1, "%s: bad header", rpm);
2177 l = sigdsize + sigcnt * 16;
2178 headerend = headerstart + 16 + l;
2179 if (l > rpmheadsize)
2181 rpmheadsize = l + 128;
2182 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2184 if (fread(rpmhead->data, l, 1, fp) != 1)
2186 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2191 solv_chksum_add(chksumh, rpmhead->data, l);
2192 rpmhead->forcebinary = forcebinary;
2193 rpmhead->cnt = sigcnt;
2194 rpmhead->dcnt = sigdsize;
2195 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2196 if (headexists(rpmhead, TAG_PATCHESNAME))
2198 /* this is a patch rpm, ignore */
2199 pool_error(pool, -1, "%s: is patch rpm", rpm);
2201 solv_chksum_free(chksumh, 0);
2205 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2206 if (payloadformat && !strcmp(payloadformat, "drpm"))
2208 /* this is a delta rpm */
2209 pool_error(pool, -1, "%s: is delta rpm", rpm);
2211 solv_chksum_free(chksumh, 0);
2216 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2217 solv_chksum_add(chksumh, lead, l);
2219 s = pool_id2solvable(pool, repo_add_solvable(repo));
2220 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2222 repo_free_solvable(repo, s - pool->solvables, 1);
2223 solv_chksum_free(chksumh, 0);
2227 if (!(flags & REPO_NO_LOCATION))
2228 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2229 if (S_ISREG(stb.st_mode))
2230 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2231 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2233 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2235 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2237 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2240 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2241 chksumh = solv_chksum_free(chksumh, 0);
2244 if (!(flags & REPO_NO_INTERNALIZE))
2245 repodata_internalize(data);
2246 return s - pool->solvables;
2250 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2252 Pool *pool = repo->pool;
2254 RpmHead *rpmhead = rpmhandle;
2256 char *payloadformat;
2258 data = repo_add_repodata(repo, flags);
2259 if (headexists(rpmhead, TAG_PATCHESNAME))
2261 pool_error(pool, -1, "is a patch rpm");
2264 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2265 if (payloadformat && !strcmp(payloadformat, "drpm"))
2267 /* this is a delta rpm */
2268 pool_error(pool, -1, "is a delta rpm");
2271 s = pool_id2solvable(pool, repo_add_solvable(repo));
2272 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2274 repo_free_solvable(repo, s - pool->solvables, 1);
2277 if (!(flags & REPO_NO_INTERNALIZE))
2278 repodata_internalize(data);
2279 return s - pool->solvables;
2283 linkhash(const char *lt, char *hash)
2286 const unsigned char *str = (const unsigned char *)lt;
2290 while ((c = *str++) != 0)
2292 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2296 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2298 RpmHead *rpmhead = rpmhandle;
2303 unsigned int *di, diidx;
2304 unsigned int *co = 0;
2305 unsigned int *ff = 0;
2306 unsigned int lastdir;
2309 int cnt, dcnt, cnt2;
2314 struct filelistinfo info;
2316 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2319 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2321 for (i = 0; i < dcnt; i++)
2322 (*cb)(cbdata, dn[i], 0);
2326 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2332 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2333 if (!di || cnt != cnt2)
2340 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2341 if (!fm || cnt != cnt2)
2349 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2351 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2352 if (!md || cnt != cnt2)
2362 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2364 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2365 if (!co || cnt != cnt2)
2376 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2378 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2379 if (!ff || cnt != cnt2)
2393 memset(&info, 0, sizeof(info));
2394 for (i = 0; i < cnt; i++)
2396 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2401 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2402 l = l1 + strlen(bn[i]) + 1;
2406 space = solv_realloc(space, spacen);
2408 if (lastdir != diidx)
2410 strcpy(space, dn[diidx]);
2414 strcpy(space + l1, bn[i]);
2415 info.diridx = diidx;
2421 info.digest = md[i];
2422 if (fm && S_ISLNK(fm[i]))
2427 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2433 linkhash(lt[i], md5);
2439 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2445 (*cb)(cbdata, space, &info);
2459 rpm_query(void *rpmhandle, Id what)
2461 const char *name, *arch, *sourcerpm;
2465 RpmHead *rpmhead = rpmhandle;
2470 name = headstring(rpmhead, TAG_NAME);
2473 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2474 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2475 arch = headstring(rpmhead, TAG_ARCH);
2478 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2485 evr = headtoevr(rpmhead);
2486 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2488 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2492 name = headstring(rpmhead, TAG_NAME);
2493 r = solv_strdup(name);
2495 case SOLVABLE_SUMMARY:
2496 name = headstring(rpmhead, TAG_SUMMARY);
2497 r = solv_strdup(name);
2499 case SOLVABLE_DESCRIPTION:
2500 name = headstring(rpmhead, TAG_DESCRIPTION);
2501 r = solv_strdup(name);
2504 r = headtoevr(rpmhead);
2511 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2513 RpmHead *rpmhead = rpmhandle;
2518 case SOLVABLE_INSTALLTIME:
2519 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2520 return u32 ? u32 : notfound;
2526 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2528 struct rpmdbentry *entries;
2531 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2534 queue_empty(rpmdbidq);
2535 for (i = 0; i < nentries; i++)
2536 queue_push(rpmdbidq, entries[i].rpmdbid);
2543 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2545 struct rpmdbstate *state = rpmstate;
2548 r = getrpmdbid(state, rpmdbid);
2550 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2551 return r <= 0 ? 0 : state->rpmhead;
2555 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2557 struct rpmdbstate *state = rpmstate;
2558 /* int headerstart, headerend; */
2560 unsigned int sigdsize, sigcnt, l;
2561 unsigned char lead[4096];
2562 int forcebinary = 0;
2564 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2566 pool_error(state->pool, 0, "%s: not a rpm", name);
2569 forcebinary = lead[6] != 0 || lead[7] != 1;
2570 if (lead[78] != 0 || lead[79] != 5)
2572 pool_error(state->pool, 0, "%s: not a V5 header", name);
2575 if (getu32(lead + 96) != 0x8eade801)
2577 pool_error(state->pool, 0, "%s: bad signature header", name);
2580 sigcnt = getu32(lead + 96 + 8);
2581 sigdsize = getu32(lead + 96 + 12);
2582 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2584 pool_error(state->pool, 0, "%s: bad signature header", name);
2587 sigdsize += sigcnt * 16;
2588 sigdsize = (sigdsize + 7) & ~7;
2589 /* headerstart = 96 + 16 + sigdsize; */
2592 l = sigdsize > 4096 ? 4096 : sigdsize;
2593 if (fread(lead, l, 1, fp) != 1)
2595 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2600 if (fread(lead, 16, 1, fp) != 1)
2602 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2605 if (getu32(lead) != 0x8eade801)
2607 pool_error(state->pool, 0, "%s: bad header", name);
2610 sigcnt = getu32(lead + 8);
2611 sigdsize = getu32(lead + 12);
2612 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2614 pool_error(state->pool, 0, "%s: bad header", name);
2617 l = sigdsize + sigcnt * 16;
2618 /* headerend = headerstart + 16 + l; */
2619 if (l > state->rpmheadsize)
2621 state->rpmheadsize = l + 128;
2622 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2624 rpmhead = state->rpmhead;
2625 if (fread(rpmhead->data, l, 1, fp) != 1)
2627 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2630 rpmhead->forcebinary = forcebinary;
2631 rpmhead->cnt = sigcnt;
2632 rpmhead->dcnt = sigdsize;
2633 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2637 #ifdef ENABLE_RPMDB_BYRPMHEADER
2640 rpm_byrpmh(void *rpmstate, Header h)
2642 struct rpmdbstate *state = rpmstate;
2643 const unsigned char *uh;
2644 unsigned int sigdsize, sigcnt, l;
2648 uh = headerUnload(h);
2650 uh = headerUnload(h, NULL);
2654 sigcnt = getu32(uh);
2655 sigdsize = getu32(uh + 4);
2656 l = sigdsize + sigcnt * 16;
2657 if (l > state->rpmheadsize)
2659 state->rpmheadsize = l + 128;
2660 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2662 rpmhead = state->rpmhead;
2663 memcpy(rpmhead->data, uh + 8, l - 8);
2665 rpmhead->forcebinary = 0;
2666 rpmhead->cnt = sigcnt;
2667 rpmhead->dcnt = sigdsize;
2668 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;