2 * Copyright (c) 2007-2018, SUSE 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>
29 #include <rpm/rpmio.h>
30 #include <rpm/rpmpgp.h>
32 #include <rpm/header.h>
34 #include <rpm/rpmdb.h>
44 #include "repo_rpmdb.h"
45 #include "repo_solv.h"
46 #ifdef ENABLE_COMPLEX_DEPS
47 #include "pool_parserpmrichdep.h"
50 /* 3: added triggers */
51 /* 4: fixed triggers */
52 /* 5: fixed checksum copying */
53 /* 6: add SOLVABLE_PREREQ_IGNOREINST support */
54 /* 7: fix bug in ignoreinst logic */
55 #define RPMDB_COOKIE_VERSION 7
58 #define TAG_VERSION 1001
59 #define TAG_RELEASE 1002
60 #define TAG_EPOCH 1003
61 #define TAG_SUMMARY 1004
62 #define TAG_DESCRIPTION 1005
63 #define TAG_BUILDTIME 1006
64 #define TAG_BUILDHOST 1007
65 #define TAG_INSTALLTIME 1008
67 #define TAG_DISTRIBUTION 1010
68 #define TAG_VENDOR 1011
69 #define TAG_LICENSE 1014
70 #define TAG_PACKAGER 1015
71 #define TAG_GROUP 1016
74 #define TAG_FILESIZES 1028
75 #define TAG_FILEMODES 1030
76 #define TAG_FILEMD5S 1035
77 #define TAG_FILELINKTOS 1036
78 #define TAG_FILEFLAGS 1037
79 #define TAG_SOURCERPM 1044
80 #define TAG_PROVIDENAME 1047
81 #define TAG_REQUIREFLAGS 1048
82 #define TAG_REQUIRENAME 1049
83 #define TAG_REQUIREVERSION 1050
84 #define TAG_NOSOURCE 1051
85 #define TAG_NOPATCH 1052
86 #define TAG_CONFLICTFLAGS 1053
87 #define TAG_CONFLICTNAME 1054
88 #define TAG_CONFLICTVERSION 1055
89 #define TAG_TRIGGERNAME 1066
90 #define TAG_TRIGGERVERSION 1067
91 #define TAG_TRIGGERFLAGS 1068
92 #define TAG_CHANGELOGTIME 1080
93 #define TAG_CHANGELOGNAME 1081
94 #define TAG_CHANGELOGTEXT 1082
95 #define TAG_OBSOLETENAME 1090
96 #define TAG_FILEDEVICES 1095
97 #define TAG_FILEINODES 1096
98 #define TAG_SOURCEPACKAGE 1106
99 #define TAG_PROVIDEFLAGS 1112
100 #define TAG_PROVIDEVERSION 1113
101 #define TAG_OBSOLETEFLAGS 1114
102 #define TAG_OBSOLETEVERSION 1115
103 #define TAG_DIRINDEXES 1116
104 #define TAG_BASENAMES 1117
105 #define TAG_DIRNAMES 1118
106 #define TAG_PAYLOADFORMAT 1124
107 #define TAG_PATCHESNAME 1133
108 #define TAG_FILECOLORS 1140
109 #define TAG_OLDSUGGESTSNAME 1156
110 #define TAG_OLDSUGGESTSVERSION 1157
111 #define TAG_OLDSUGGESTSFLAGS 1158
112 #define TAG_OLDENHANCESNAME 1159
113 #define TAG_OLDENHANCESVERSION 1160
114 #define TAG_OLDENHANCESFLAGS 1161
117 #define TAG_DISTEPOCH 1218
120 #define TAG_LONGFILESIZES 5008
121 #define TAG_LONGSIZE 5009
122 #define TAG_RECOMMENDNAME 5046
123 #define TAG_RECOMMENDVERSION 5047
124 #define TAG_RECOMMENDFLAGS 5048
125 #define TAG_SUGGESTNAME 5049
126 #define TAG_SUGGESTVERSION 5050
127 #define TAG_SUGGESTFLAGS 5051
128 #define TAG_SUPPLEMENTNAME 5052
129 #define TAG_SUPPLEMENTVERSION 5053
130 #define TAG_SUPPLEMENTFLAGS 5054
131 #define TAG_ENHANCENAME 5055
132 #define TAG_ENHANCEVERSION 5056
133 #define TAG_ENHANCEFLAGS 5057
136 #define TAG_SIGBASE 256
137 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
138 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
139 #define TAG_SHA256HEADER (TAG_SIGBASE + 17)
141 #define SIGTAG_SIZE 1000
142 #define SIGTAG_PGP 1002 /* RSA signature */
143 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
144 #define SIGTAG_GPG 1005 /* DSA signature */
146 #define DEP_LESS (1 << 1)
147 #define DEP_GREATER (1 << 2)
148 #define DEP_EQUAL (1 << 3)
149 #define DEP_STRONG (1 << 27)
150 #define DEP_PRE_IN ((1 << 6) | (1 << 9) | (1 << 10))
151 #define DEP_PRE_UN ((1 << 6) | (1 << 11) | (1 << 12))
153 #define FILEFLAG_GHOST (1 << 6)
156 /* some limits to guard against corrupt rpms */
157 /* dsize limits taken from rpm's lib/header.c */
158 #define MAX_SIG_CNT 0x10000
159 #define MAX_SIG_DSIZE 0x4000000
161 #define MAX_HDR_CNT 0x10000
162 #define MAX_HDR_DSIZE 0x10000000
165 #ifndef ENABLE_RPMPKG_LIBRPM
167 typedef struct rpmhead {
171 unsigned char data[1];
175 static inline unsigned char *
176 headfindtag(RpmHead *h, int tag)
179 unsigned char *d, taga[4];
185 for (i = 0; i < h->cnt; i++, d -= 16)
186 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
192 headexists(RpmHead *h, int tag)
194 return headfindtag(h, tag) ? 1 : 0;
198 headint32array(RpmHead *h, int tag, int *cnt)
202 unsigned char *d = headfindtag(h, tag);
204 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
206 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
207 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
208 if (o > h->dcnt || i > h->dcnt || o + 4 * i > h->dcnt)
211 r = solv_calloc(i ? i : 1, sizeof(uint32_t));
214 for (o = 0; o < i; o++, d += 4)
215 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
219 /* returns the first entry of an integer array */
221 headint32(RpmHead *h, int tag)
224 unsigned char *d = headfindtag(h, tag);
226 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
228 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
229 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
230 if (i == 0 || o > h->dcnt || i > h->dcnt || o + 4 * i > h->dcnt)
233 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
237 headint64array(RpmHead *h, int tag, int *cnt)
241 unsigned char *d = headfindtag(h, tag);
243 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
245 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
246 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
247 if (o > h->dcnt || i > h->dcnt || o + 8 * i > h->dcnt)
250 r = solv_calloc(i ? i : 1, sizeof(uint64_t));
253 for (o = 0; o < i; o++, d += 8)
255 uint32_t x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
256 r[o] = (uint64_t)x << 32 | (uint32_t)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
261 /* returns the first entry of an 64bit integer array */
263 headint64(RpmHead *h, int tag)
267 unsigned char *d = headfindtag(h, tag);
269 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
271 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
272 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
273 if (i == 0 || o > h->dcnt || i > h->dcnt || o + 8 * i > h->dcnt)
276 x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
277 return (uint64_t)x << 32 | (uint32_t)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
281 headint16array(RpmHead *h, int tag, int *cnt)
285 unsigned char *d = headfindtag(h, tag);
287 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
289 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
290 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
291 if (o > h->dcnt || i > h->dcnt || o + 2 * i > h->dcnt)
294 r = solv_calloc(i ? i : 1, sizeof(uint16_t));
297 for (o = 0; o < i; o++, d += 2)
298 r[o] = d[0] << 8 | d[1];
303 headstring(RpmHead *h, int tag)
306 unsigned char *d = headfindtag(h, tag);
307 /* 6: STRING, 9: I18NSTRING */
308 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
310 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
313 return (char *)h->dp + o;
317 headstringarray(RpmHead *h, int tag, int *cnt)
320 unsigned char *d = headfindtag(h, tag);
323 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
325 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
326 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
327 if (o > h->dcnt || i > h->dcnt)
329 r = solv_calloc(i ? i : 1, sizeof(char *));
333 for (o = 0; o < i; o++)
337 d += strlen((char *)d) + 1;
338 if (d >= h->dp + h->dcnt)
347 static unsigned char *
348 headbinary(RpmHead *h, int tag, unsigned int *sizep)
351 unsigned char *d = headfindtag(h, tag);
352 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
354 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
355 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
356 if (o > h->dcnt || i > h->dcnt || o + i > h->dcnt)
364 headissourceheuristic(RpmHead *h)
367 unsigned char *d = headfindtag(h, TAG_DIRNAMES);
368 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
370 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
371 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
372 return i == 1 && o < h->dcnt && !h->dp[o] ? 1 : 0;
383 typedef struct headerToken_s RpmHead;
386 headexists(RpmHead *h, int tag)
388 return headerIsEntry(h, tag);
391 static void *headget(RpmHead *h, int tag, int *cnt, int alloc)
394 if (!headerGet(h, tag, &td, alloc ? HEADERGET_ALLOC : HEADERGET_MINMEM))
402 headint32array(RpmHead *h, int tag, int *cnt)
404 return headget(h, tag, cnt, 1);
408 headint32(RpmHead *h, int tag)
410 uint32_t *arr = headget(h, tag, 0, 0);
411 return arr ? arr[0] : 0;
415 headint64array(RpmHead *h, int tag, int *cnt)
417 return headget(h, tag, cnt, 1);
420 /* returns the first entry of an 64bit integer array */
422 headint64(RpmHead *h, int tag)
424 uint64_t *arr = headget(h, tag, 0, 0);
425 return arr ? arr[0] : 0;
429 headint16array(RpmHead *h, int tag, int *cnt)
431 return headget(h, tag, cnt, 1);
435 headstring(RpmHead *h, int tag)
437 return headget(h, tag, 0, 0);
441 headstringarray(RpmHead *h, int tag, int *cnt)
443 return headget(h, tag, cnt, 1);
446 static unsigned char *
447 headbinary(RpmHead *h, int tag, unsigned int *sizep)
449 return headget(h, tag, (int *)sizep, 0);
453 headissourceheuristic(RpmHead *h)
455 return headerIsSource(h);
466 static char *headtoevr(RpmHead *h)
474 version = headstring(h, TAG_VERSION);
475 release = headstring(h, TAG_RELEASE);
476 epoch = headint32(h, TAG_EPOCH);
477 if (!version || !release)
479 fprintf(stderr, "headtoevr: bad rpm header\n");
482 for (v = version; *v >= '0' && *v <= '9'; v++)
484 if (epoch || (v != version && *v == ':'))
486 char epochbuf[11]; /* 32bit decimal will fit in */
487 sprintf(epochbuf, "%u", epoch);
488 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
489 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
493 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
494 sprintf(evr, "%s-%s", version, release);
496 distepoch = headstring(h, TAG_DISTEPOCH);
497 if (distepoch && *distepoch)
500 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
502 strcpy(evr + l, distepoch);
509 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
511 if (str[solv_validutf8(str)])
513 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
514 repodata_set_str(repodata, handle, tag, ustr);
518 repodata_set_str(repodata, handle, tag, str);
522 ignq_sortcmp(const void *va, const void *vb, void *dp)
524 int r = *(Id *)va - *(Id *)vb;
526 r = ((Id *)va)[1] - ((Id *)vb)[1];
531 * strong: 0: ignore strongness
532 * 1: filter to strong
536 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags, Queue *ignq)
540 int i, cc, nc, vc, fc;
541 int haspre, premask, has_ign;
542 unsigned int olddeps;
546 n = headstringarray(rpmhead, tagn, &nc);
551 case TAG_SUGGESTNAME:
552 tagn = TAG_OLDSUGGESTSNAME;
553 tagv = TAG_OLDSUGGESTSVERSION;
554 tagf = TAG_OLDSUGGESTSFLAGS;
557 case TAG_ENHANCENAME:
558 tagn = TAG_OLDENHANCESNAME;
559 tagv = TAG_OLDENHANCESVERSION;
560 tagf = TAG_OLDENHANCESFLAGS;
563 case TAG_RECOMMENDNAME:
564 tagn = TAG_OLDSUGGESTSNAME;
565 tagv = TAG_OLDSUGGESTSVERSION;
566 tagf = TAG_OLDSUGGESTSFLAGS;
569 case TAG_SUPPLEMENTNAME:
570 tagn = TAG_OLDENHANCESNAME;
571 tagv = TAG_OLDENHANCESVERSION;
572 tagf = TAG_OLDENHANCESFLAGS;
578 n = headstringarray(rpmhead, tagn, &nc);
583 v = headstringarray(rpmhead, tagv, &vc);
584 f = headint32array(rpmhead, tagf, &fc);
585 if (!v || !f || nc != vc || nc != fc)
587 char *pkgname = rpm_query(rpmhead, 0);
588 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
597 haspre = 0; /* add no prereq marker */
598 premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
599 if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
601 /* we do filtering */
603 for (i = 0; i < nc; i++)
605 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
607 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
608 if (!strncmp(n[i], "rpmlib(", 7))
610 if ((f[i] & premask) != 0)
617 /* no filtering, just look for the first prereq */
618 for (i = 0; i < nc; i++)
619 if ((f[i] & premask) != 0)
632 cc += haspre; /* add slot for the prereq marker */
633 olddeps = repo_reserve_ids(repo, 0, cc);
634 ida = repo->idarraydata + olddeps;
644 haspre = 2; /* pass two: prereqs */
646 *ida++ = SOLVABLE_PREREQMARKER;
648 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
652 if (haspre == 1 && (f[i] & premask) != 0)
654 if (haspre == 2 && (f[i] & premask) == 0)
657 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
658 if (!strncmp(n[i], "rpmlib(", 7))
660 #ifdef ENABLE_COMPLEX_DEPS
661 if ((f[i] & (DEP_LESS|DEP_EQUAL|DEP_GREATER)) == 0 && n[i][0] == '(')
663 id = pool_parserpmrichdep(pool, n[i]);
671 id = pool_str2id(pool, n[i], 1);
672 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
676 if ((f[i] & DEP_LESS) != 0)
678 if ((f[i] & DEP_EQUAL) != 0)
680 if ((f[i] & DEP_GREATER) != 0)
682 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
683 evr = pool_str2id(pool, v[i] + 2, 1);
685 evr = pool_str2id(pool, v[i], 1);
686 id = pool_rel2id(pool, id, evr, fl, 1);
689 if (haspre == 2 && ignq)
691 int is_ign = (f[i] & DEP_PRE_IN) != 0 && (f[i] & DEP_PRE_UN) == 0 ? 1 : 0;
693 queue_push2(ignq, id, is_ign);
697 repo->idarraysize += cc + 1;
701 if (ignq && ignq->count)
704 if (has_ign && ignq->count == 2)
710 solv_sort(ignq->elements, ignq->count / 2, sizeof(Id) * 2, ignq_sortcmp, 0);
711 for (i = j = 0; i < ignq->count; i += 2)
713 id = ignq->elements[i];
714 if (id != lastid && ignq->elements[i + 1] > 0)
715 ignq->elements[j++] = id;
719 queue_truncate(ignq, j);
725 repodata_str2dir_rooted(Repodata *data, char *str, int create)
731 if (l + 2 <= sizeof(buf))
734 bp = solv_malloc(l + 2);
737 id = repodata_str2dir(data, bp, create);
744 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, uint32_t *di, int fc, int dc)
748 unsigned int *fkb, *fn;
750 uint32_t *fsz, *fino;
752 unsigned int inotest[256], inotestok;
756 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
758 /* convert to kbyte */
759 fsz = solv_malloc2(fszc, sizeof(*fsz));
760 for (i = 0; i < fszc; i++)
761 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
764 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
766 /* convert to kbyte */
767 for (i = 0; i < fszc; i++)
769 fsz[i] = fsz[i] / 1024 + 1;
779 /* stupid rpm records sizes of directories, so we have to check the mode */
780 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
781 if (!fm || fc != fszc)
787 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
788 if (!fino || fc != fszc)
796 /* kill hardlinked entries */
798 if (fc < sizeof(inotest))
800 /* quick test just hashing the inode numbers */
801 memset(inotest, 0, sizeof(inotest));
802 for (i = 0; i < fc; i++)
805 if (fsz[i] == 0 || !S_ISREG(fm[i]))
806 continue; /* does not matter */
807 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
808 bit = 1 << (fino[i] & 31);
809 if ((inotest[off] & bit) != 0)
814 inotestok = 1; /* no conflict found */
818 /* hardlinked files are possible, check ino/dev pairs */
819 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
821 unsigned int mask, hash, hh;
822 if (!fdev || fc != fszc)
831 while ((mask & (mask - 1)) != 0)
832 mask = mask & (mask - 1);
834 if (mask > sizeof(inotest)/sizeof(*inotest))
835 fx = solv_calloc(mask, sizeof(unsigned int));
839 memset(fx, 0, mask * sizeof(unsigned int));
842 for (i = 0; i < fc; i++)
844 if (fsz[i] == 0 || !S_ISREG(fm[i]))
846 hash = (fino[i] + fdev[i] * 31) & mask;
848 while ((j = fx[hash]) != 0)
850 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
852 fsz[i] = 0; /* kill entry */
855 hash = (hash + hh++) & mask;
866 /* sum up inode count and kbytes for each directory */
867 fn = solv_calloc(dc, sizeof(unsigned int));
868 fkb = solv_calloc(dc, sizeof(unsigned int));
869 for (i = 0; i < fc; i++)
872 continue; /* corrupt entry */
874 if (fsz[i] == 0 || !S_ISREG(fm[i]))
876 fkb[di[i]] += fsz[i];
881 for (i = 0; i < dc; i++)
887 Solvable *s = data->repo->pool->solvables + handle;
888 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
889 did = repodata_str2dir(data, "/usr/src", 1);
891 did = repodata_str2dir_rooted(data, dn[i], 1);
894 did = repodata_str2dir(data, dn[i], 1);
895 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
902 is_filtered(const char *dir)
906 /* the dirs always have a trailing / in rpm */
907 if (strstr(dir, "bin/"))
909 if (!strncmp(dir, "/etc/", 5))
911 if (!strcmp(dir, "/usr/lib/"))
917 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
925 uint32_t lastdii = -1;
926 int lastfiltered = 0;
930 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
933 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
939 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
948 pool_error(data->repo->pool, 0, "bad filelist");
952 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
955 for (i = 0; i < bnc; i++)
959 if (did < 0 || di[i] != lastdii)
962 continue; /* corrupt entry */
965 if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
967 lastfiltered = is_filtered(dn[di[i]]);
968 if (lastfiltered == 1)
971 if (dn[lastdii][0] != '/')
972 did = repodata_str2dir_rooted(data, dn[lastdii], 1);
974 did = repodata_str2dir(data, dn[lastdii], 1);
978 if (*b == '/') /* work around rpm bug */
980 if (lastfiltered && (lastfiltered != 2 || strcmp(b, "sendmail")))
982 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
990 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
995 int i, cnc, cxc, ctc;
998 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
999 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
1000 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
1001 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
1009 for (i = 0; i < ctc; i++)
1011 Id h = repodata_new_handle(data);
1013 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
1015 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
1017 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
1020 for (i = 0; i < hq.count; i++)
1021 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
1029 set_description_author(Repodata *data, Id handle, char *str)
1032 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
1033 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
1037 /* oh my, found SUSE special author section */
1039 str = solv_strdup(str);
1042 while (l > 0 && str[l - 1] == '\n')
1045 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
1047 aut = str; /* copy over */
1048 while (*p == ' ' || *p == '\n')
1061 while (aut != str && aut[-1] == '\n')
1065 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
1069 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
1073 rpmhead2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
1081 name = headstring(rpmhead, TAG_NAME);
1084 pool_error(pool, 0, "package has no name");
1087 if (!strcmp(name, "gpg-pubkey"))
1089 s->name = pool_str2id(pool, name, 1);
1090 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
1091 if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
1092 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
1095 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
1096 s->arch = ARCH_NOSRC;
1101 s->arch = ARCH_NOARCH;
1102 evr = headtoevr(rpmhead);
1103 s->evr = pool_str2id(pool, evr, 1);
1104 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
1106 queue_init_buffer(&ignq, ignqbuf, sizeof(ignqbuf)/sizeof(*ignqbuf));
1108 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0, 0);
1109 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
1110 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
1111 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags, &ignq);
1112 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0, 0);
1113 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0, 0);
1115 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0, 0);
1116 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0, 0);
1117 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0, 0);
1118 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0, 0);
1120 repo_rewrite_suse_deps(s, 0);
1122 if (data && ignq.count)
1123 repodata_set_idarray(data, s - pool->solvables, SOLVABLE_PREREQ_IGNOREINST, &ignq);
1131 unsigned long long u64;
1133 handle = s - pool->solvables;
1134 str = headstring(rpmhead, TAG_SUMMARY);
1136 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
1137 str = headstring(rpmhead, TAG_DESCRIPTION);
1139 set_description_author(data, handle, str);
1140 str = headstring(rpmhead, TAG_GROUP);
1142 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
1143 str = headstring(rpmhead, TAG_LICENSE);
1145 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
1146 str = headstring(rpmhead, TAG_URL);
1148 repodata_set_str(data, handle, SOLVABLE_URL, str);
1149 str = headstring(rpmhead, TAG_DISTRIBUTION);
1151 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
1152 str = headstring(rpmhead, TAG_PACKAGER);
1154 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
1155 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1157 unsigned char *chksum;
1158 unsigned int chksumsize;
1159 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
1160 if (chksum && chksumsize == 16)
1161 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
1163 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1165 str = headstring(rpmhead, TAG_SHA1HEADER);
1166 if (str && strlen(str) == 40)
1167 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
1168 else if (str && strlen(str) == 64)
1169 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
1171 u32 = headint32(rpmhead, TAG_BUILDTIME);
1173 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1174 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1176 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1177 u64 = headint64(rpmhead, TAG_LONGSIZE);
1179 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1182 u32 = headint32(rpmhead, TAG_SIZE);
1184 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1187 repodata_set_sourcepkg(data, handle, sourcerpm);
1188 if ((flags & RPM_ADD_TRIGGERS) != 0)
1190 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0, 0);
1192 for (lastid = 0; (id = repo->idarraydata[ida]) != 0; ida++, lastid = id)
1194 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1196 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1197 addfilelist(data, handle, rpmhead, flags);
1198 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1199 addchangelog(data, handle, rpmhead);
1205 static inline unsigned int
1206 getu32(const unsigned char *dp)
1208 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1218 #define ENTRIES_BLOCK 255
1219 #define NAMEDATA_BLOCK 1023
1221 # ifdef ENABLE_RPMDB_LIBRPM
1222 # include "repo_rpmdb_librpm.h"
1224 # include "repo_rpmdb_bdb.h"
1229 /* dummy state just to store pool/rootdir and header data */
1234 RpmHead *rpmhead; /* header storage space */
1241 #ifndef ENABLE_RPMPKG_LIBRPM
1244 headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
1247 unsigned int len = 16 * cnt + dsize + pad;
1248 if (len + 1 > state->rpmheadsize)
1250 state->rpmheadsize = len + 128;
1251 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1253 rpmhead = state->rpmhead;
1254 if (fread(rpmhead->data, len, 1, fp) != 1)
1257 return pool_error(state->pool, 0, "%s: unexpected EOF", name);
1260 solv_chksum_add(chk1, rpmhead->data, len);
1262 solv_chksum_add(chk2, rpmhead->data, len);
1263 rpmhead->data[len] = 0;
1265 rpmhead->dcnt = dsize;
1266 rpmhead->dp = rpmhead->data + cnt * 16;
1270 #if defined(ENABLE_RPMDB_BYRPMHEADER)
1272 headfromblob(struct rpmdbstate *state, const unsigned char *blob, unsigned int cnt, unsigned int dsize)
1275 unsigned int len = 16 * cnt + dsize;
1276 if (len + 1 > state->rpmheadsize)
1278 state->rpmheadsize = len + 128;
1279 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1281 rpmhead = state->rpmhead;
1282 memcpy(rpmhead->data, blob, len);
1283 rpmhead->data[len] = 0;
1285 rpmhead->dcnt = dsize;
1286 rpmhead->dp = rpmhead->data + cnt * 16;
1293 headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
1295 unsigned int len = 16 * cnt + dsize + pad;
1296 char *buf = solv_malloc(8 + len);
1298 memcpy(buf, lead + 8, 8);
1299 if (fread(buf + 8, len, 1, fp) != 1)
1302 return pool_error(state->pool, 0, "%s: unexpected EOF", name);
1305 solv_chksum_add(chk1, buf + 8, len);
1307 solv_chksum_add(chk2, buf + 8, len);
1308 h = headerImport(buf, 8 + len - pad, HEADERIMPORT_FAST);
1312 return pool_error(state->pool, 0, "%s: headerImport error", name);
1315 headfree(state->rpmhead);
1323 freestate(struct rpmdbstate *state)
1327 if (state->pkgdbopened)
1329 if (state->dbenvopened)
1333 solv_free(state->rootdir);
1334 headfree(state->rpmhead);
1338 rpm_state_create(Pool *pool, const char *rootdir)
1340 struct rpmdbstate *state;
1341 state = solv_calloc(1, sizeof(*state));
1344 state->rootdir = solv_strdup(rootdir);
1349 rpm_state_free(void *state)
1353 return solv_free(state);
1360 /******************************************************************/
1363 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1371 from = fromrepo->idarraydata + fromoff;
1372 for (ida = from, cc = 0; *ida; ida++, cc++)
1376 ido = repo_reserve_ids(repo, 0, cc);
1377 ida = repo->idarraydata + ido;
1378 memcpy(ida, from, (cc + 1) * sizeof(Id));
1379 repo->idarraysize += cc + 1;
1383 #define COPYDIR_DIRCACHE_SIZE 512
1385 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1388 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1390 if (cache && did && cache[did & 255] == did)
1391 return cache[(did & 255) + 256];
1392 return copydir_complex(pool, data, fromdata, did, cache);
1396 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1401 /* make sure that the dirpool has an entry */
1402 if (!data->dirpool.ndirs)
1403 dirpool_add_dir(&data->dirpool, 0, 0, 1);
1406 parent = dirpool_parent(&fromdata->dirpool, did);
1407 compid = dirpool_compid(&fromdata->dirpool, did);
1409 parent = copydir(pool, data, fromdata, parent, cache);
1410 if (data->localpool || fromdata->localpool)
1411 compid = repodata_translate_id(data, fromdata, compid, 1);
1412 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1415 cache[did & 255] = did;
1416 cache[(did & 255) + 256] = compid;
1421 struct solvable_copy_cbdata {
1429 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1431 struct solvable_copy_cbdata *cbdata = vcbdata;
1433 Repodata *data = cbdata->data;
1434 Id handle = cbdata->handle;
1435 Pool *pool = data->repo->pool;
1437 keyname = key->name;
1440 case REPOKEY_TYPE_ID:
1441 case REPOKEY_TYPE_CONSTANTID:
1442 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1444 if (data->localpool || fromdata->localpool)
1445 id = repodata_translate_id(data, fromdata, id, 1);
1446 if (key->type == REPOKEY_TYPE_ID)
1447 repodata_set_id(data, handle, keyname, id);
1448 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1449 repodata_set_constantid(data, handle, keyname, id);
1451 repodata_add_idarray(data, handle, keyname, id);
1453 case REPOKEY_TYPE_STR:
1454 repodata_set_str(data, handle, keyname, kv->str);
1456 case REPOKEY_TYPE_VOID:
1457 repodata_set_void(data, handle, keyname);
1459 case REPOKEY_TYPE_NUM:
1460 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1462 case REPOKEY_TYPE_CONSTANT:
1463 repodata_set_constant(data, handle, keyname, kv->num);
1465 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1467 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1469 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1471 case REPOKEY_TYPE_DIRSTRARRAY:
1473 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1475 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1477 case REPOKEY_TYPE_FLEXARRAY:
1480 assert(cbdata->subhandle);
1481 cbdata->handle = cbdata->subhandle;
1482 cbdata->subhandle = 0;
1487 assert(!cbdata->subhandle);
1488 cbdata->subhandle = cbdata->handle;
1490 cbdata->handle = repodata_new_handle(data);
1491 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1494 if (solv_chksum_len(key->type))
1496 repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1505 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1508 Repo *repo = s->repo;
1509 Pool *pool = repo->pool;
1510 Repo *fromrepo = r->repo;
1511 struct solvable_copy_cbdata cbdata;
1513 /* copy solvable data */
1517 s->vendor = r->vendor;
1518 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1519 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1520 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1521 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1522 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1523 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1524 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1525 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1527 /* copy all attributes */
1531 cbdata.handle = s - pool->solvables;
1532 cbdata.subhandle = 0;
1533 cbdata.dircache = dircache;
1534 p = r - fromrepo->pool->solvables;
1536 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1538 FOR_REPODATAS(fromrepo, i, data)
1540 if (p >= data->start && p < data->end)
1541 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1542 cbdata.dircache = 0; /* only for first repodata */
1547 /* used to sort entries by package name that got returned in some database order */
1549 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1551 struct rpmdbentry const *a = va, *b = vb;
1552 char *namedata = dp;
1554 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1557 return a->rpmdbid - b->rpmdbid;
1561 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1564 Pool *pool = repo->pool;
1565 Solvable *a = pool->solvables + *(Id *)va;
1566 Solvable *b = pool->solvables + *(Id *)vb;
1569 if (a->name != b->name)
1570 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1571 rpmdbid = repo->rpmdbid;
1572 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1576 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1578 Pool *pool = repo->pool;
1581 tmp = pool->solvables[pa];
1582 pool->solvables[pa] = pool->solvables[pb];
1583 pool->solvables[pb] = tmp;
1586 Id tmpid = repo->rpmdbid[pa - repo->start];
1587 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1588 repo->rpmdbid[pb - repo->start] = tmpid;
1590 /* only works if nothing is already internalized! */
1592 repodata_swap_attrs(data, pa, pb);
1596 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1599 memset(cookie, 0, 32);
1600 cookie[3] = RPMDB_COOKIE_VERSION;
1601 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1602 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1603 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1605 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1607 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1609 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1611 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1617 * read rpm db as repo
1622 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1624 Pool *pool = repo->pool;
1625 struct stat packagesstat;
1626 unsigned char newcookie[32];
1627 const unsigned char *oldcookie = 0;
1628 Id oldcookietype = 0;
1630 int count = 0, done = 0;
1631 struct rpmdbstate state;
1636 now = solv_timems(0);
1637 memset(&state, 0, sizeof(state));
1639 if (flags & REPO_USE_ROOTDIR)
1640 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1642 data = repo_add_repodata(repo, flags);
1644 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1646 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1651 if (!opendbenv(&state))
1653 solv_free(state.rootdir);
1657 /* XXX: should get ro lock of Packages database! */
1658 if (stat_database(&state, "Packages", &packagesstat, 1))
1663 mkrpmdbcookie(&packagesstat, newcookie, flags);
1664 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1667 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1668 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1670 int solvstart = 0, solvend = 0;
1673 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1674 repo_empty(ref, 1); /* get it out of the way */
1675 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1676 count = count_headers(&state);
1677 if (!openpkgdb(&state))
1682 if (pkgdb_cursor_open(&state))
1689 while ((dbid = pkgdb_cursor_getrpm(&state)) != 0)
1693 pkgdb_cursor_close(&state);
1699 s = pool_id2solvable(pool, repo_add_solvable(repo));
1701 solvstart = s - pool->solvables;
1702 solvend = s - pool->solvables + 1;
1705 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1706 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1707 if (rpmhead2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1714 /* We can reuse this solvable, but make sure it's still
1715 associated with this repo. */
1716 memset(s, 0, sizeof(*s));
1719 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1723 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1724 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1727 pkgdb_cursor_close(&state);
1730 /* oops, could not reuse. free it instead */
1731 repo_free_solvable(repo, s - pool->solvables, 1);
1735 /* now sort all solvables in the new solvstart..solvend block */
1736 if (solvend - solvstart > 1)
1738 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1739 for (i = solvstart; i < solvend; i++)
1740 pkgids[i - solvstart] = i;
1741 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1743 for (i = solvstart; i < solvend; i++)
1745 int j = pkgids[i - solvstart];
1747 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1749 swap_solvables(repo, data, i, j);
1756 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1757 struct rpmdbentry *entries = 0, *rp;
1760 unsigned int refmask, h;
1764 memset(dircache, 0, sizeof(dircache));
1766 /* get ids of installed rpms */
1767 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1776 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1778 /* create hash from dbid to ref */
1779 refmask = mkmask(ref->nsolvables);
1780 refhash = solv_calloc(refmask + 1, sizeof(Id));
1781 for (i = 0; i < ref->end - ref->start; i++)
1783 if (!ref->rpmdbid[i])
1785 h = ref->rpmdbid[i] & refmask;
1787 h = (h + 317) & refmask;
1788 refhash[h] = i + 1; /* make it non-zero */
1791 /* count the misses, they will cost us time */
1792 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1794 for (i = 0, rp = entries; i < nentries; i++, rp++)
1798 Id dbid = rp->rpmdbid;
1800 while ((id = refhash[h]))
1802 if (ref->rpmdbid[id - 1] == dbid)
1804 h = (h + 317) & refmask;
1813 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1814 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1816 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1818 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1820 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1822 Id dbid = rp->rpmdbid;
1823 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1827 while ((id = refhash[h]))
1829 if (ref->rpmdbid[id - 1] == dbid)
1831 h = (h + 317) & refmask;
1835 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1838 solvable_copy(s, r, data, dircache);
1843 res = getrpm_dbid(&state, dbid);
1847 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1850 solv_free(namedata);
1854 rpmhead2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1855 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1859 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1860 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1865 solv_free(namedata);
1867 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1872 if (!(flags & REPO_NO_INTERNALIZE))
1873 repodata_internalize(data);
1874 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1875 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1876 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1877 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1878 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)));
1883 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1889 return repo_add_rpmdb(repo, 0, flags);
1890 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1891 if (repo_add_solv(ref, fp, 0) != 0)
1896 if (ref && ref->start == ref->end)
1902 repo_disable_paging(ref);
1903 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1909 #endif /* ENABLE_RPMDB */
1912 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1914 unsigned int sigdsize, sigcnt, sigpad, l;
1915 Pool *pool = repo->pool;
1917 struct rpmdbstate state;
1918 char *payloadformat;
1920 unsigned char lead[4096];
1921 int headerstart, headerend;
1924 unsigned char pkgid[16];
1925 unsigned char leadsigid[16];
1926 unsigned char hdrid[32];
1927 int pkgidtype, leadsigidtype, hdridtype;
1929 Chksum *chksumh = 0;
1930 Chksum *leadsigchksumh = 0;
1932 data = repo_add_repodata(repo, flags);
1934 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1935 chksumtype = REPOKEY_TYPE_SHA256;
1936 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1937 chksumtype = REPOKEY_TYPE_SHA1;
1940 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1942 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1945 if (fstat(fileno(fp), &stb))
1947 pool_error(pool, -1, "fstat: %s", strerror(errno));
1953 memset(&state, 0, sizeof(state));
1958 chksumh = solv_chksum_create(chksumtype);
1959 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1960 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1961 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1963 pool_error(pool, -1, "%s: not a rpm", rpm);
1968 solv_chksum_add(chksumh, lead, 96 + 16);
1970 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1972 /* process signature header */
1973 if (lead[78] != 0 || lead[79] != 5)
1975 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1979 if (getu32(lead + 96) != 0x8eade801)
1981 pool_error(pool, -1, "%s: bad signature header", rpm);
1985 sigcnt = getu32(lead + 96 + 8);
1986 sigdsize = getu32(lead + 96 + 12);
1987 if (sigcnt >= MAX_SIG_CNT || sigdsize >= MAX_SIG_DSIZE)
1989 pool_error(pool, -1, "%s: bad signature header", rpm);
1993 sigpad = sigdsize & 7 ? 8 - (sigdsize & 7) : 0;
1994 headerstart = 96 + 16 + sigcnt * 16 + sigdsize + sigpad;
1995 pkgidtype = leadsigidtype = hdridtype = 0;
1996 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1998 if (!headfromfp(&state, rpm, fp, lead + 96, sigcnt, sigdsize, sigpad, chksumh, leadsigchksumh))
2003 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2005 unsigned char *chksum;
2006 unsigned int chksumsize;
2007 chksum = headbinary(state.rpmhead, SIGTAG_MD5, &chksumsize);
2008 if (chksum && chksumsize == 16)
2010 pkgidtype = REPOKEY_TYPE_MD5;
2011 memcpy(pkgid, chksum, 16);
2014 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2016 const char *str = headstring(state.rpmhead, TAG_SHA1HEADER);
2017 if (str && strlen(str) == 40)
2019 if (solv_hex2bin(&str, hdrid, 20) == 20)
2020 hdridtype = REPOKEY_TYPE_SHA1;
2022 else if (str && strlen(str) == 64)
2024 if (solv_hex2bin(&str, hdrid, 32) == 32)
2025 hdridtype = REPOKEY_TYPE_SHA256;
2031 /* just skip the signature header */
2032 unsigned int len = sigcnt * 16 + sigdsize + sigpad;
2035 l = len > 4096 ? 4096 : len;
2036 if (fread(lead, l, 1, fp) != 1)
2038 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2043 solv_chksum_add(chksumh, lead, l);
2045 solv_chksum_add(leadsigchksumh, lead, l);
2051 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2052 leadsigidtype = REPOKEY_TYPE_MD5;
2055 /* process main header */
2056 if (fread(lead, 16, 1, fp) != 1)
2058 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2063 solv_chksum_add(chksumh, lead, 16);
2064 if (getu32(lead) != 0x8eade801)
2066 pool_error(pool, -1, "%s: bad header", rpm);
2070 sigcnt = getu32(lead + 8);
2071 sigdsize = getu32(lead + 12);
2072 if (sigcnt >= MAX_HDR_CNT || sigdsize >= MAX_HDR_DSIZE)
2074 pool_error(pool, -1, "%s: bad header", rpm);
2078 headerend = headerstart + 16 + sigdsize + sigcnt * 16;
2080 if (!headfromfp(&state, rpm, fp, lead, sigcnt, sigdsize, 0, chksumh, 0))
2085 if (headexists(state.rpmhead, TAG_PATCHESNAME))
2087 /* this is a patch rpm, ignore */
2088 pool_error(pool, -1, "%s: is patch rpm", rpm);
2090 solv_chksum_free(chksumh, 0);
2091 headfree(state.rpmhead);
2094 payloadformat = headstring(state.rpmhead, TAG_PAYLOADFORMAT);
2095 if (payloadformat && !strcmp(payloadformat, "drpm"))
2097 /* this is a delta rpm */
2098 pool_error(pool, -1, "%s: is delta rpm", rpm);
2100 solv_chksum_free(chksumh, 0);
2101 headfree(state.rpmhead);
2105 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2106 solv_chksum_add(chksumh, lead, l);
2108 s = pool_id2solvable(pool, repo_add_solvable(repo));
2109 if (!rpmhead2solv(pool, repo, data, s, state.rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2111 repo_free_solvable(repo, s - pool->solvables, 1);
2112 solv_chksum_free(chksumh, 0);
2113 headfree(state.rpmhead);
2116 if (!(flags & REPO_NO_LOCATION))
2117 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2118 if (S_ISREG(stb.st_mode))
2119 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2120 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2122 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2124 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2126 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2129 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2130 chksumh = solv_chksum_free(chksumh, 0);
2132 headfree(state.rpmhead);
2133 if (!(flags & REPO_NO_INTERNALIZE))
2134 repodata_internalize(data);
2135 return s - pool->solvables;
2139 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2141 Pool *pool = repo->pool;
2143 RpmHead *rpmhead = rpmhandle;
2145 char *payloadformat;
2147 data = repo_add_repodata(repo, flags);
2148 if (headexists(rpmhead, TAG_PATCHESNAME))
2150 pool_error(pool, -1, "is a patch rpm");
2153 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2154 if (payloadformat && !strcmp(payloadformat, "drpm"))
2156 /* this is a delta rpm */
2157 pool_error(pool, -1, "is a delta rpm");
2160 s = pool_id2solvable(pool, repo_add_solvable(repo));
2161 if (!rpmhead2solv(pool, repo, data, s, rpmhead, flags))
2163 repo_free_solvable(repo, s - pool->solvables, 1);
2166 if (!(flags & REPO_NO_INTERNALIZE))
2167 repodata_internalize(data);
2168 return s - pool->solvables;
2172 linkhash(const char *lt, char *hash)
2175 const unsigned char *str = (const unsigned char *)lt;
2179 while ((c = *str++) != 0)
2181 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2185 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2187 RpmHead *rpmhead = rpmhandle;
2192 uint32_t *di, diidx;
2196 unsigned int lastdir;
2198 int cnt, dcnt, cnt2;
2203 struct filelistinfo info;
2205 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2208 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2210 for (i = 0; i < dcnt; i++)
2211 (*cb)(cbdata, dn[i], 0);
2215 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2221 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2222 if (!di || cnt != cnt2)
2229 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2230 if (!fm || cnt != cnt2)
2238 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2240 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2241 if (!md || cnt != cnt2)
2251 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2253 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2254 if (co && cnt != cnt2)
2265 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2267 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2268 if (!ff || cnt != cnt2)
2282 memset(&info, 0, sizeof(info));
2283 for (i = 0; i < cnt; i++)
2285 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2290 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2291 l = l1 + strlen(bn[i]) + 1;
2295 space = solv_realloc(space, spacen);
2297 if (lastdir != diidx)
2299 strcpy(space, dn[diidx]);
2303 strcpy(space + l1, bn[i]);
2304 info.diridx = diidx;
2310 info.digest = md[i];
2311 if (fm && S_ISLNK(fm[i]))
2316 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2322 linkhash(lt[i], md5);
2328 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2332 info.color = co ? co[i] : 0;
2333 (*cb)(cbdata, space, &info);
2347 rpm_query(void *rpmhandle, Id what)
2349 const char *name, *arch, *sourcerpm;
2353 RpmHead *rpmhead = rpmhandle;
2358 name = headstring(rpmhead, TAG_NAME);
2361 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2362 if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
2363 arch = headstring(rpmhead, TAG_ARCH);
2366 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2373 evr = headtoevr(rpmhead);
2374 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2376 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2380 name = headstring(rpmhead, TAG_NAME);
2381 r = solv_strdup(name);
2383 case SOLVABLE_SUMMARY:
2384 name = headstring(rpmhead, TAG_SUMMARY);
2385 r = solv_strdup(name);
2387 case SOLVABLE_DESCRIPTION:
2388 name = headstring(rpmhead, TAG_DESCRIPTION);
2389 r = solv_strdup(name);
2392 r = headtoevr(rpmhead);
2399 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2401 RpmHead *rpmhead = rpmhandle;
2406 case SOLVABLE_INSTALLTIME:
2407 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2408 return u32 ? u32 : notfound;
2416 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2418 struct rpmdbentry *entries;
2421 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2424 queue_empty(rpmdbidq);
2425 for (i = 0; i < nentries; i++)
2426 queue_push(rpmdbidq, entries[i].rpmdbid);
2433 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2435 struct rpmdbstate *state = rpmstate;
2438 r = getrpm_dbid(state, rpmdbid);
2440 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2441 return r <= 0 ? 0 : state->rpmhead;
2444 #endif /* ENABLE_RPMDB */
2447 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2449 struct rpmdbstate *state = rpmstate;
2450 unsigned int sigdsize, sigcnt, l;
2451 unsigned char lead[4096];
2453 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2455 pool_error(state->pool, 0, "%s: not a rpm", name);
2458 if (lead[78] != 0 || lead[79] != 5)
2460 pool_error(state->pool, 0, "%s: not a V5 header", name);
2464 /* skip signature header */
2465 if (getu32(lead + 96) != 0x8eade801)
2467 pool_error(state->pool, 0, "%s: bad signature header", name);
2470 sigcnt = getu32(lead + 96 + 8);
2471 sigdsize = getu32(lead + 96 + 12);
2472 if (sigcnt >= MAX_SIG_CNT || sigdsize >= MAX_SIG_DSIZE)
2474 pool_error(state->pool, 0, "%s: bad signature header", name);
2477 sigdsize += sigcnt * 16;
2478 sigdsize = (sigdsize + 7) & ~7;
2481 l = sigdsize > 4096 ? 4096 : sigdsize;
2482 if (fread(lead, l, 1, fp) != 1)
2484 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2490 if (fread(lead, 16, 1, fp) != 1)
2492 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2495 if (getu32(lead) != 0x8eade801)
2497 pool_error(state->pool, 0, "%s: bad header", name);
2500 sigcnt = getu32(lead + 8);
2501 sigdsize = getu32(lead + 12);
2502 if (sigcnt >= MAX_HDR_CNT || sigdsize >= MAX_HDR_DSIZE)
2504 pool_error(state->pool, 0, "%s: bad header", name);
2507 if (!headfromfp(state, name, fp, lead, sigcnt, sigdsize, 0, 0, 0))
2509 return state->rpmhead;
2512 #if defined(ENABLE_RPMDB_BYRPMHEADER) || defined(ENABLE_RPMDB_LIBRPM)
2515 rpm_byrpmh(void *rpmstate, Header h)
2517 struct rpmdbstate *state = rpmstate;
2518 #ifndef ENABLE_RPMPKG_LIBRPM
2519 const unsigned char *uh;
2520 unsigned int dsize, cnt;
2525 uh = headerUnload(h);
2527 uh = headerUnload(h, NULL);
2532 dsize = getu32(uh + 4);
2533 if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
2538 headfromblob(state, uh + 8, cnt, dsize);
2544 headfree(state->rpmhead);
2545 state->rpmhead = headerLink(h);
2547 return state->rpmhead;
2550 #endif /* defined(ENABLE_RPMDB_BYRPMHEADER) || defined(ENABLE_RPMDB_LIBRPM) */