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 #define RPMDB_COOKIE_VERSION 4
56 #define TAG_VERSION 1001
57 #define TAG_RELEASE 1002
58 #define TAG_EPOCH 1003
59 #define TAG_SUMMARY 1004
60 #define TAG_DESCRIPTION 1005
61 #define TAG_BUILDTIME 1006
62 #define TAG_BUILDHOST 1007
63 #define TAG_INSTALLTIME 1008
65 #define TAG_DISTRIBUTION 1010
66 #define TAG_VENDOR 1011
67 #define TAG_LICENSE 1014
68 #define TAG_PACKAGER 1015
69 #define TAG_GROUP 1016
72 #define TAG_FILESIZES 1028
73 #define TAG_FILEMODES 1030
74 #define TAG_FILEMD5S 1035
75 #define TAG_FILELINKTOS 1036
76 #define TAG_FILEFLAGS 1037
77 #define TAG_SOURCERPM 1044
78 #define TAG_PROVIDENAME 1047
79 #define TAG_REQUIREFLAGS 1048
80 #define TAG_REQUIRENAME 1049
81 #define TAG_REQUIREVERSION 1050
82 #define TAG_NOSOURCE 1051
83 #define TAG_NOPATCH 1052
84 #define TAG_CONFLICTFLAGS 1053
85 #define TAG_CONFLICTNAME 1054
86 #define TAG_CONFLICTVERSION 1055
87 #define TAG_TRIGGERNAME 1066
88 #define TAG_TRIGGERVERSION 1067
89 #define TAG_TRIGGERFLAGS 1068
90 #define TAG_CHANGELOGTIME 1080
91 #define TAG_CHANGELOGNAME 1081
92 #define TAG_CHANGELOGTEXT 1082
93 #define TAG_OBSOLETENAME 1090
94 #define TAG_FILEDEVICES 1095
95 #define TAG_FILEINODES 1096
96 #define TAG_SOURCEPACKAGE 1106
97 #define TAG_PROVIDEFLAGS 1112
98 #define TAG_PROVIDEVERSION 1113
99 #define TAG_OBSOLETEFLAGS 1114
100 #define TAG_OBSOLETEVERSION 1115
101 #define TAG_DIRINDEXES 1116
102 #define TAG_BASENAMES 1117
103 #define TAG_DIRNAMES 1118
104 #define TAG_PAYLOADFORMAT 1124
105 #define TAG_PATCHESNAME 1133
106 #define TAG_FILECOLORS 1140
107 #define TAG_SUGGESTSNAME 1156
108 #define TAG_SUGGESTSVERSION 1157
109 #define TAG_SUGGESTSFLAGS 1158
110 #define TAG_ENHANCESNAME 1159
111 #define TAG_ENHANCESVERSION 1160
112 #define TAG_ENHANCESFLAGS 1161
115 #define TAG_DISTEPOCH 1218
118 #define TAG_LONGFILESIZES 5008
119 #define TAG_LONGSIZE 5009
122 #define TAG_SIGBASE 256
123 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
124 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
126 #define SIGTAG_SIZE 1000
127 #define SIGTAG_PGP 1002 /* RSA signature */
128 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
129 #define SIGTAG_GPG 1005 /* DSA signature */
131 #define DEP_LESS (1 << 1)
132 #define DEP_GREATER (1 << 2)
133 #define DEP_EQUAL (1 << 3)
134 #define DEP_STRONG (1 << 27)
135 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
137 #define FILEFLAG_GHOST (1 << 6)
141 # define RPM_INDEX_SIZE 4
143 # define RPM_INDEX_SIZE 8
147 typedef struct rpmhead {
151 int forcebinary; /* sigh, see rh#478907 */
152 unsigned char data[1];
156 static inline unsigned char *
157 headfindtag(RpmHead *h, int tag)
160 unsigned char *d, taga[4];
166 for (i = 0; i < h->cnt; i++, d -= 16)
167 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
173 headexists(RpmHead *h, int tag)
175 return headfindtag(h, tag) ? 1 : 0;
178 static unsigned int *
179 headint32array(RpmHead *h, int tag, int *cnt)
181 unsigned int i, o, *r;
182 unsigned char *d = headfindtag(h, tag);
184 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
186 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
187 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
188 if (o + 4 * i > h->dcnt)
191 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
194 for (o = 0; o < i; o++, d += 4)
195 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
199 /* returns the first entry of an integer array */
201 headint32(RpmHead *h, int tag)
204 unsigned char *d = headfindtag(h, tag);
206 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
208 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
209 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
210 if (i == 0 || o + 4 * i > h->dcnt)
213 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
216 static unsigned long long *
217 headint64array(RpmHead *h, int tag, int *cnt)
220 unsigned long long *r;
221 unsigned char *d = headfindtag(h, tag);
223 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
225 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
226 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
227 if (o + 8 * i > h->dcnt)
230 r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
233 for (o = 0; o < i; o++, d += 8)
235 unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
236 r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
241 /* returns the first entry of an 64bit integer array */
242 static unsigned long long
243 headint64(RpmHead *h, int tag)
246 unsigned char *d = headfindtag(h, tag);
247 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
249 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
250 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
251 if (i == 0 || o + 8 * i > h->dcnt)
254 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
255 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
258 static unsigned int *
259 headint16array(RpmHead *h, int tag, int *cnt)
261 unsigned int i, o, *r;
262 unsigned char *d = headfindtag(h, tag);
264 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
266 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
267 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
268 if (o + 4 * i > h->dcnt)
271 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
274 for (o = 0; o < i; o++, d += 2)
275 r[o] = d[0] << 8 | d[1];
280 headstring(RpmHead *h, int tag)
283 unsigned char *d = headfindtag(h, tag);
284 /* 6: STRING, 9: I18NSTRING */
285 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
287 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
290 return (char *)h->dp + o;
294 headstringarray(RpmHead *h, int tag, int *cnt)
297 unsigned char *d = headfindtag(h, tag);
300 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
302 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
303 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
304 r = solv_calloc(i ? i : 1, sizeof(char *));
308 for (o = 0; o < i; o++)
312 d += strlen((char *)d) + 1;
313 if (d >= h->dp + h->dcnt)
322 static unsigned char *
323 headbinary(RpmHead *h, int tag, unsigned int *sizep)
326 unsigned char *d = headfindtag(h, tag);
327 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
329 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
330 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
331 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
338 static char *headtoevr(RpmHead *h)
346 version = headstring(h, TAG_VERSION);
347 release = headstring(h, TAG_RELEASE);
348 epoch = headint32(h, TAG_EPOCH);
349 if (!version || !release)
351 fprintf(stderr, "headtoevr: bad rpm header\n");
354 for (v = version; *v >= '0' && *v <= '9'; v++)
356 if (epoch || (v != version && *v == ':'))
358 char epochbuf[11]; /* 32bit decimal will fit in */
359 sprintf(epochbuf, "%u", epoch);
360 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
361 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
365 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
366 sprintf(evr, "%s-%s", version, release);
368 distepoch = headstring(h, TAG_DISTEPOCH);
369 if (distepoch && *distepoch)
372 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
374 strcpy(evr + l, distepoch);
381 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
383 if (str[solv_validutf8(str)])
385 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
386 repodata_set_str(repodata, handle, tag, ustr);
390 repodata_set_str(repodata, handle, tag, str);
394 #define MAKEDEPS_FILTER_WEAK (1 << 0)
395 #define MAKEDEPS_FILTER_STRONG (1 << 1)
396 #define MAKEDEPS_NO_RPMLIB (1 << 2)
399 * strong: 0: ignore strongness
400 * 1: filter to strong
404 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
408 int i, cc, nc, vc, fc;
410 unsigned int olddeps;
414 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
415 n = headstringarray(rpmhead, tagn, &nc);
419 v = headstringarray(rpmhead, tagv, &vc);
420 f = headint32array(rpmhead, tagf, &fc);
421 if (!v || !f || nc != vc || nc != fc)
423 char *pkgname = rpm_query(rpmhead, 0);
424 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
433 haspre = 0; /* add no prereq marker */
436 /* we do filtering */
438 for (i = 0; i < nc; i++)
440 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
442 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
443 if (!strncmp(n[i], "rpmlib(", 7))
445 if ((f[i] & DEP_PRE) != 0)
450 else if (tagn == TAG_REQUIRENAME)
452 /* no filtering, just look for the first prereq */
453 for (i = 0; i < nc; i++)
454 if ((f[i] & DEP_PRE) != 0)
468 olddeps = repo_reserve_ids(repo, 0, cc);
469 ida = repo->idarraydata + olddeps;
476 haspre = 2; /* pass two: prereqs */
478 *ida++ = SOLVABLE_PREREQMARKER;
480 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
482 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
484 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
486 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
487 if (!strncmp(n[i], "rpmlib(", 7))
489 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
493 if ((f[i] & DEP_LESS) != 0)
495 if ((f[i] & DEP_EQUAL) != 0)
497 if ((f[i] & DEP_GREATER) != 0)
499 name = pool_str2id(pool, n[i], 1);
500 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
501 evr = pool_str2id(pool, v[i] + 2, 1);
503 evr = pool_str2id(pool, v[i], 1);
504 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
507 *ida++ = pool_str2id(pool, n[i], 1);
510 repo->idarraysize += cc + 1;
519 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
523 unsigned int *fkb, *fn, *fsz, *fm, *fino;
524 unsigned long long *fsz64;
525 unsigned int inotest[256], inotestok;
529 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
531 /* convert to kbyte */
532 fsz = solv_malloc2(fszc, sizeof(*fsz));
533 for (i = 0; i < fszc; i++)
534 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
537 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
539 /* convert to kbyte */
540 for (i = 0; i < fszc; i++)
542 fsz[i] = fsz[i] / 1024 + 1;
552 /* stupid rpm records sizes of directories, so we have to check the mode */
553 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
554 if (!fm || fc != fszc)
560 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
561 if (!fino || fc != fszc)
569 /* kill hardlinked entries */
571 if (fc < sizeof(inotest))
573 /* quick test just hashing the inode numbers */
574 memset(inotest, 0, sizeof(inotest));
575 for (i = 0; i < fc; i++)
578 if (fsz[i] == 0 || !S_ISREG(fm[i]))
579 continue; /* does not matter */
580 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
581 bit = 1 << (fino[i] & 31);
582 if ((inotest[off] & bit) != 0)
587 inotestok = 1; /* no conflict found */
591 /* hardlinked files are possible, check ino/dev pairs */
592 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
594 unsigned int mask, hash, hh;
595 if (!fdev || fc != fszc)
604 while ((mask & (mask - 1)) != 0)
605 mask = mask & (mask - 1);
607 if (mask > sizeof(inotest)/sizeof(*inotest))
608 fx = solv_calloc(mask, sizeof(unsigned int));
612 memset(fx, 0, mask * sizeof(unsigned int));
615 for (i = 0; i < fc; i++)
617 if (fsz[i] == 0 || !S_ISREG(fm[i]))
619 hash = (fino[i] + fdev[i] * 31) & mask;
621 while ((j = fx[hash]) != 0)
623 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
625 fsz[i] = 0; /* kill entry */
628 hash = (hash + hh++) & mask;
639 /* sum up inode count and kbytes for each directory */
640 fn = solv_calloc(dc, sizeof(unsigned int));
641 fkb = solv_calloc(dc, sizeof(unsigned int));
642 for (i = 0; i < fc; i++)
647 if (fsz[i] == 0 || !S_ISREG(fm[i]))
649 fkb[di[i]] += fsz[i];
654 for (i = 0; i < dc; i++)
660 Solvable *s = data->repo->pool->solvables + handle;
661 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
662 did = repodata_str2dir(data, "/usr/src", 1);
664 continue; /* work around rpm bug */
667 did = repodata_str2dir(data, dn[i], 1);
668 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
675 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
687 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
690 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
696 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
705 pool_error(data->repo->pool, 0, "bad filelist");
709 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
711 for (i = 0; i < bnc; i++)
716 if (di[i] == lastdii)
720 did = repodata_str2dir(data, dn[di[i]], 1);
722 did = repodata_str2dir(data, "/", 1);
726 if (b && *b == '/') /* work around rpm bug */
728 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
736 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
741 int i, cnc, cxc, ctc;
744 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
745 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
746 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
747 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
755 for (i = 0; i < ctc; i++)
757 Id h = repodata_new_handle(data);
759 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
761 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
763 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
766 for (i = 0; i < hq.count; i++)
767 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
776 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
782 name = headstring(rpmhead, TAG_NAME);
785 pool_error(pool, 0, "package has no name");
788 if (!strcmp(name, "gpg-pubkey"))
790 s->name = pool_str2id(pool, name, 1);
791 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
792 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
793 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
796 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
797 s->arch = ARCH_NOSRC;
802 s->arch = ARCH_NOARCH;
803 evr = headtoevr(rpmhead);
804 s->evr = pool_str2id(pool, evr, 1);
805 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
807 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
808 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
809 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
810 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
811 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
812 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
814 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
815 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
816 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
817 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
818 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
819 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
826 unsigned long long u64;
828 handle = s - pool->solvables;
829 str = headstring(rpmhead, TAG_SUMMARY);
831 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
832 str = headstring(rpmhead, TAG_DESCRIPTION);
836 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
837 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
841 /* oh my, found SUSE special author section */
843 str = solv_strdup(str);
846 while (l > 0 && str[l - 1] == '\n')
849 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
851 aut = str; /* copy over */
852 while (*p == ' ' || *p == '\n')
865 while (aut != str && aut[-1] == '\n')
869 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
873 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
875 str = headstring(rpmhead, TAG_GROUP);
877 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
878 str = headstring(rpmhead, TAG_LICENSE);
880 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
881 str = headstring(rpmhead, TAG_URL);
883 repodata_set_str(data, handle, SOLVABLE_URL, str);
884 str = headstring(rpmhead, TAG_DISTRIBUTION);
886 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
887 str = headstring(rpmhead, TAG_PACKAGER);
889 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
890 if ((flags & RPM_ADD_WITH_PKGID) != 0)
892 unsigned char *chksum;
893 unsigned int chksumsize;
894 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
895 if (chksum && chksumsize == 16)
896 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
898 if ((flags & RPM_ADD_WITH_HDRID) != 0)
900 str = headstring(rpmhead, TAG_SHA1HEADER);
901 if (str && strlen(str) == 40)
902 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
903 else if (str && strlen(str) == 64)
904 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
906 u32 = headint32(rpmhead, TAG_BUILDTIME);
908 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
909 u32 = headint32(rpmhead, TAG_INSTALLTIME);
911 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
912 u64 = headint64(rpmhead, TAG_LONGSIZE);
914 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
917 u32 = headint32(rpmhead, TAG_SIZE);
919 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
922 repodata_set_sourcepkg(data, handle, sourcerpm);
923 if ((flags & RPM_ADD_TRIGGERS) != 0)
926 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
929 for (; (id = repo->idarraydata[ida]) != 0; ida++)
931 /* we currently do not support rel ids in incore data, so
932 * strip off versioning information */
935 Reldep *rd = GETRELDEP(pool, id);
940 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
944 if ((flags & RPM_ADD_NO_FILELIST) == 0)
945 addfilelist(data, handle, rpmhead);
946 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
947 addchangelog(data, handle, rpmhead);
954 /******************************************************************/
955 /* Rpm Database stuff
962 RpmHead *rpmhead; /* header storage space */
966 DB_ENV *dbenv; /* database environment */
967 DB *db; /* packages database */
968 int byteswapped; /* endianess of packages database */
976 #define ENTRIES_BLOCK 255
977 #define NAMEDATA_BLOCK 1023
980 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
983 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
985 # if defined(WORDS_BIGENDIAN)
990 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
992 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
996 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
999 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1001 # if defined(WORDS_BIGENDIAN)
1006 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1008 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1012 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1014 opendbenv(struct rpmdbstate *state, const char *rootdir)
1016 char dbpath[PATH_MAX];
1020 if (db_env_create(&dbenv, 0))
1021 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1022 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1023 dbenv->set_thread_count(dbenv, 8);
1025 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1026 if (access(dbpath, W_OK) == -1)
1028 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1033 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1035 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1040 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1041 dbenv->close(dbenv, 0);
1044 state->dbenv = dbenv;
1049 openpkgdb(struct rpmdbstate *state)
1051 if (state->dbopened)
1052 return state->dbopened > 0 ? 1 : 0;
1053 state->dbopened = -1;
1054 if (!state->dbenv && !opendbenv(state, state->rootdir))
1056 if (db_create(&state->db, state->dbenv, 0))
1058 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1060 state->dbenv->close(state->dbenv, 0);
1064 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1066 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1067 state->db->close(state->db, 0);
1069 state->dbenv->close(state->dbenv, 0);
1073 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1075 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1076 state->db->close(state->db, 0);
1078 state->dbenv->close(state->dbenv, 0);
1082 state->dbopened = 1;
1086 /* get the rpmdbids of all installed packages from the Name index database.
1087 * This is much faster then querying the big Packages database */
1088 static struct rpmdbentry *
1089 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1103 struct rpmdbentry *entries = 0;
1110 if (!state->dbenv && !opendbenv(state, state->rootdir))
1112 dbenv = state->dbenv;
1113 if (db_create(&db, dbenv, 0))
1115 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1118 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1120 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1124 if (db->get_byteswapped(db, &byteswapped))
1126 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1130 if (db->cursor(db, NULL, &dbc, 0))
1132 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1136 memset(&dbkey, 0, sizeof(dbkey));
1137 memset(&dbdata, 0, sizeof(dbdata));
1140 dbkey.data = (void *)match;
1141 dbkey.size = strlen(match);
1143 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1145 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1149 nameoff = namedatal;
1152 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1153 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1154 namedata[namedatal + dbkey.size] = 0;
1155 namedatal += dbkey.size + 1;
1157 while(dl >= RPM_INDEX_SIZE)
1159 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1160 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1161 entries[nentries].nameoff = nameoff;
1163 dp += RPM_INDEX_SIZE;
1164 dl -= RPM_INDEX_SIZE;
1171 /* make sure that enteries is != 0 if there was no error */
1173 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1174 *nentriesp = nentries;
1176 *namedatap = namedata;
1180 /* retrive header by rpmdbid */
1182 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1184 unsigned char buf[16];
1191 pool_error(state->pool, 0, "illegal rpmdbid");
1194 if (state->dbopened != 1 && !openpkgdb(state))
1196 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1197 memset(&dbkey, 0, sizeof(dbkey));
1198 memset(&dbdata, 0, sizeof(dbdata));
1203 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1205 if (dbdata.size < 8)
1207 pool_error(state->pool, 0, "corrupt rpm database (size)");
1210 if (dbdata.size > state->rpmheadsize)
1212 state->rpmheadsize = dbdata.size + 128;
1213 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1215 rpmhead = state->rpmhead;
1216 memcpy(buf, dbdata.data, 8);
1217 rpmhead->forcebinary = 1;
1218 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1219 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1220 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1222 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1225 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1226 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1230 /* retrive header by berkeleydb cursor */
1232 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1234 unsigned char buf[16];
1240 memset(&dbkey, 0, sizeof(dbkey));
1241 memset(&dbdata, 0, sizeof(dbdata));
1242 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1244 if (dbkey.size != 4)
1245 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1246 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1247 if (dbid == 0) /* the join key */
1249 if (dbdata.size < 8)
1250 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1251 if (dbdata.size > state->rpmheadsize)
1253 state->rpmheadsize = dbdata.size + 128;
1254 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1256 rpmhead = state->rpmhead;
1257 memcpy(buf, dbdata.data, 8);
1258 rpmhead->forcebinary = 1;
1259 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1260 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1261 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1262 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1263 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1264 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1271 freestate(struct rpmdbstate *state)
1277 solv_free(state->rootdir);
1279 state->db->close(state->db, 0);
1281 state->dbenv->close(state->dbenv, 0);
1282 solv_free(state->rpmhead);
1286 rpm_state_create(Pool *pool, const char *rootdir)
1288 struct rpmdbstate *state;
1289 state = solv_calloc(1, sizeof(*state));
1292 state->rootdir = solv_strdup(rootdir);
1297 rpm_state_free(void *state)
1300 return solv_free(state);
1304 count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
1306 char dbpath[PATH_MAX];
1307 struct stat statbuf;
1314 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1315 if (stat(dbpath, &statbuf))
1317 memset(&dbkey, 0, sizeof(dbkey));
1318 memset(&dbdata, 0, sizeof(dbdata));
1319 if (db_create(&db, dbenv, 0))
1321 pool_error(pool, 0, "db_create: %s", strerror(errno));
1324 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1326 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1330 if (db->cursor(db, NULL, &dbc, 0))
1333 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1336 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1337 count += dbdata.size / RPM_INDEX_SIZE;
1343 /******************************************************************/
1346 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1354 from = fromrepo->idarraydata + fromoff;
1355 for (ida = from, cc = 0; *ida; ida++, cc++)
1359 ido = repo_reserve_ids(repo, 0, cc);
1360 ida = repo->idarraydata + ido;
1361 memcpy(ida, from, (cc + 1) * sizeof(Id));
1362 repo->idarraysize += cc + 1;
1366 #define COPYDIR_DIRCACHE_SIZE 512
1368 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1371 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1373 if (cache && cache[did & 255] == did)
1374 return cache[(did & 255) + 256];
1375 return copydir_complex(pool, data, fromdata, did, cache);
1379 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1381 Id parent = dirpool_parent(&fromdata->dirpool, did);
1382 Id compid = dirpool_compid(&fromdata->dirpool, did);
1384 parent = copydir(pool, data, fromdata, parent, cache);
1385 if (data->localpool || fromdata->localpool)
1386 compid = repodata_translate_id(data, fromdata, compid, 1);
1387 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1390 cache[did & 255] = did;
1391 cache[(did & 255) + 256] = compid;
1396 struct solvable_copy_cbdata {
1404 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1406 struct solvable_copy_cbdata *cbdata = vcbdata;
1408 Repodata *data = cbdata->data;
1409 Id handle = cbdata->handle;
1410 Pool *pool = data->repo->pool;
1412 keyname = key->name;
1415 case REPOKEY_TYPE_ID:
1416 case REPOKEY_TYPE_CONSTANTID:
1417 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1419 if (data->localpool || fromdata->localpool)
1420 id = repodata_translate_id(data, fromdata, id, 1);
1421 if (key->type == REPOKEY_TYPE_ID)
1422 repodata_set_id(data, handle, keyname, id);
1423 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1424 repodata_set_constantid(data, handle, keyname, id);
1426 repodata_add_idarray(data, handle, keyname, id);
1428 case REPOKEY_TYPE_STR:
1429 repodata_set_str(data, handle, keyname, kv->str);
1431 case REPOKEY_TYPE_VOID:
1432 repodata_set_void(data, handle, keyname);
1434 case REPOKEY_TYPE_NUM:
1435 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1437 case REPOKEY_TYPE_CONSTANT:
1438 repodata_set_constant(data, handle, keyname, kv->num);
1440 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1442 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1443 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1445 case REPOKEY_TYPE_DIRSTRARRAY:
1447 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1448 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1450 case REPOKEY_TYPE_FLEXARRAY:
1453 assert(cbdata->subhandle);
1454 cbdata->handle = cbdata->subhandle;
1455 cbdata->subhandle = 0;
1460 assert(!cbdata->subhandle);
1461 cbdata->subhandle = cbdata->handle;
1463 cbdata->handle = repodata_new_handle(data);
1464 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1473 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1476 Repo *repo = s->repo;
1477 Pool *pool = repo->pool;
1478 Repo *fromrepo = r->repo;
1479 struct solvable_copy_cbdata cbdata;
1481 /* copy solvable data */
1485 s->vendor = r->vendor;
1486 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1487 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1488 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1489 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1490 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1491 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1492 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1493 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1495 /* copy all attributes */
1499 cbdata.handle = s - pool->solvables;
1500 cbdata.subhandle = 0;
1501 cbdata.dircache = dircache;
1502 p = r - fromrepo->pool->solvables;
1504 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1506 FOR_REPODATAS(fromrepo, i, data)
1508 if (p < data->start || p >= data->end)
1510 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1515 /* used to sort entries by package name that got returned in some database order */
1517 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1519 struct rpmdbentry const *a = va, *b = vb;
1520 char *namedata = dp;
1522 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1525 return a->rpmdbid - b->rpmdbid;
1529 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1532 Pool *pool = repo->pool;
1533 Solvable *a = pool->solvables + *(Id *)va;
1534 Solvable *b = pool->solvables + *(Id *)vb;
1537 if (a->name != b->name)
1538 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1539 rpmdbid = repo->rpmdbid;
1540 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1544 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1546 Pool *pool = repo->pool;
1549 tmp = pool->solvables[pa];
1550 pool->solvables[pa] = pool->solvables[pb];
1551 pool->solvables[pb] = tmp;
1554 Id tmpid = repo->rpmdbid[pa - repo->start];
1555 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1556 repo->rpmdbid[pb - repo->start] = tmpid;
1558 /* only works if nothing is already internalized! */
1560 repodata_swap_attrs(data, pa, pb);
1564 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1566 memset(cookie, 0, 32);
1567 cookie[3] = RPMDB_COOKIE_VERSION;
1568 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1569 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1573 * read rpm db as repo
1578 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1580 Pool *pool = repo->pool;
1581 char dbpath[PATH_MAX];
1582 struct stat packagesstat;
1583 unsigned char newcookie[32];
1584 const unsigned char *oldcookie = 0;
1585 Id oldcookietype = 0;
1587 int count = 0, done = 0;
1588 const char *rootdir = 0;
1589 struct rpmdbstate state;
1594 now = solv_timems(0);
1595 memset(&state, 0, sizeof(state));
1598 data = repo_add_repodata(repo, flags);
1600 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1602 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1607 if (flags & REPO_USE_ROOTDIR)
1608 rootdir = pool_get_rootdir(pool);
1609 if (!opendbenv(&state, rootdir))
1612 /* XXX: should get ro lock of Packages database! */
1613 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1614 if (stat(dbpath, &packagesstat))
1616 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1620 mkrpmdbcookie(&packagesstat, newcookie);
1621 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1624 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1625 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1627 int solvstart = 0, solvend = 0;
1631 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1632 repo_empty(ref, 1); /* get it out of the way */
1633 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1634 count = count_headers(pool, rootdir, state.dbenv);
1635 if (!openpkgdb(&state))
1640 if (state.db->cursor(state.db, NULL, &dbc, 0))
1643 return pool_error(pool, -1, "db->cursor failed");
1647 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1657 s = pool_id2solvable(pool, repo_add_solvable(repo));
1659 solvstart = s - pool->solvables;
1660 solvend = s - pool->solvables + 1;
1663 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1664 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1665 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1672 /* We can reuse this solvable, but make sure it's still
1673 associated with this repo. */
1674 memset(s, 0, sizeof(*s));
1677 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1681 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1682 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1688 /* oops, could not reuse. free it instead */
1689 repo_free_solvable(repo, s - pool->solvables, 1);
1693 /* now sort all solvables in the new solvstart..solvend block */
1694 if (solvend - solvstart > 1)
1696 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1697 for (i = solvstart; i < solvend; i++)
1698 pkgids[i - solvstart] = i;
1699 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1701 for (i = solvstart; i < solvend; i++)
1703 int j = pkgids[i - solvstart];
1705 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1707 swap_solvables(repo, data, i, j);
1714 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1715 struct rpmdbentry *entries = 0, *rp;
1718 unsigned int refmask, h;
1722 memset(dircache, 0, sizeof(dircache));
1724 /* get ids of installed rpms */
1725 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1734 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1736 /* create hash from dbid to ref */
1737 refmask = mkmask(ref->nsolvables);
1738 refhash = solv_calloc(refmask + 1, sizeof(Id));
1739 for (i = 0; i < ref->end - ref->start; i++)
1741 if (!ref->rpmdbid[i])
1743 h = ref->rpmdbid[i] & refmask;
1745 h = (h + 317) & refmask;
1746 refhash[h] = i + 1; /* make it non-zero */
1749 /* count the misses, they will cost us time */
1750 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1752 for (i = 0, rp = entries; i < nentries; i++, rp++)
1756 Id dbid = rp->rpmdbid;
1758 while ((id = refhash[h]))
1760 if (ref->rpmdbid[id - 1] == dbid)
1762 h = (h + 317) & refmask;
1771 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1772 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1774 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1776 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1778 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1780 Id dbid = rp->rpmdbid;
1781 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1785 while ((id = refhash[h]))
1787 if (ref->rpmdbid[id - 1] == dbid)
1789 h = (h + 317) & refmask;
1793 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1796 solvable_copy(s, r, data, dircache);
1801 res = getrpmdbid(&state, dbid);
1805 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1808 solv_free(namedata);
1812 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1813 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1817 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1818 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1823 solv_free(namedata);
1825 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1830 if (!(flags & REPO_NO_INTERNALIZE))
1831 repodata_internalize(data);
1832 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1833 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1834 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1835 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1836 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)));
1841 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1847 return repo_add_rpmdb(repo, 0, flags);
1848 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1849 if (repo_add_solv(ref, fp, 0) != 0)
1854 if (ref && ref->start == ref->end)
1860 repo_disable_paging(ref);
1861 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1867 static inline unsigned int
1868 getu32(const unsigned char *dp)
1870 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1875 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1877 unsigned int sigdsize, sigcnt, l;
1878 Pool *pool = repo->pool;
1880 RpmHead *rpmhead = 0;
1881 int rpmheadsize = 0;
1882 char *payloadformat;
1884 unsigned char lead[4096];
1885 int headerstart, headerend;
1888 unsigned char pkgid[16];
1889 unsigned char leadsigid[16];
1890 unsigned char hdrid[32];
1891 int pkgidtype, leadsigidtype, hdridtype;
1894 void *leadsigchksumh = 0;
1895 int forcebinary = 0;
1897 data = repo_add_repodata(repo, flags);
1899 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1900 chksumtype = REPOKEY_TYPE_SHA256;
1901 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1902 chksumtype = REPOKEY_TYPE_SHA1;
1904 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1906 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1909 if (fstat(fileno(fp), &stb))
1911 pool_error(pool, -1, "fstat: %s", strerror(errno));
1916 chksumh = solv_chksum_create(chksumtype);
1917 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1918 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1919 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1921 pool_error(pool, -1, "%s: not a rpm", rpm);
1925 forcebinary = lead[6] != 0 || lead[7] != 1;
1927 solv_chksum_add(chksumh, lead, 96 + 16);
1929 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1930 if (lead[78] != 0 || lead[79] != 5)
1932 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1936 if (getu32(lead + 96) != 0x8eade801)
1938 pool_error(pool, -1, "%s: bad signature header", rpm);
1942 sigcnt = getu32(lead + 96 + 8);
1943 sigdsize = getu32(lead + 96 + 12);
1944 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1946 pool_error(pool, -1, "%s: bad signature header", rpm);
1950 sigdsize += sigcnt * 16;
1951 sigdsize = (sigdsize + 7) & ~7;
1952 headerstart = 96 + 16 + sigdsize;
1953 pkgidtype = leadsigidtype = hdridtype = 0;
1954 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1956 /* extract pkgid or hdrid from the signature header */
1957 if (sigdsize > rpmheadsize)
1959 rpmheadsize = sigdsize + 128;
1960 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1962 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1964 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1969 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1971 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1972 rpmhead->forcebinary = 0;
1973 rpmhead->cnt = sigcnt;
1974 rpmhead->dcnt = sigdsize - sigcnt * 16;
1975 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1976 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1978 unsigned char *chksum;
1979 unsigned int chksumsize;
1980 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1981 if (chksum && chksumsize == 16)
1983 pkgidtype = REPOKEY_TYPE_MD5;
1984 memcpy(pkgid, chksum, 16);
1987 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1989 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1990 if (str && strlen(str) == 40)
1992 if (solv_hex2bin(&str, hdrid, 20) == 20)
1993 hdridtype = REPOKEY_TYPE_SHA1;
1995 else if (str && strlen(str) == 64)
1997 if (solv_hex2bin(&str, hdrid, 32) == 32)
1998 hdridtype = REPOKEY_TYPE_SHA256;
2004 /* just skip the signature header */
2007 l = sigdsize > 4096 ? 4096 : sigdsize;
2008 if (fread(lead, l, 1, fp) != 1)
2010 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2015 solv_chksum_add(chksumh, lead, l);
2017 solv_chksum_add(leadsigchksumh, lead, l);
2023 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2024 leadsigidtype = REPOKEY_TYPE_MD5;
2026 if (fread(lead, 16, 1, fp) != 1)
2028 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2033 solv_chksum_add(chksumh, lead, 16);
2034 if (getu32(lead) != 0x8eade801)
2036 pool_error(pool, -1, "%s: bad header", rpm);
2040 sigcnt = getu32(lead + 8);
2041 sigdsize = getu32(lead + 12);
2042 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2044 pool_error(pool, -1, "%s: bad header", rpm);
2048 l = sigdsize + sigcnt * 16;
2049 headerend = headerstart + 16 + l;
2050 if (l > rpmheadsize)
2052 rpmheadsize = l + 128;
2053 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2055 if (fread(rpmhead->data, l, 1, fp) != 1)
2057 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2062 solv_chksum_add(chksumh, rpmhead->data, l);
2063 rpmhead->forcebinary = forcebinary;
2064 rpmhead->cnt = sigcnt;
2065 rpmhead->dcnt = sigdsize;
2066 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2067 if (headexists(rpmhead, TAG_PATCHESNAME))
2069 /* this is a patch rpm, ignore */
2070 pool_error(pool, -1, "%s: is patch rpm", rpm);
2072 solv_chksum_free(chksumh, 0);
2076 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2077 if (payloadformat && !strcmp(payloadformat, "drpm"))
2079 /* this is a delta rpm */
2080 pool_error(pool, -1, "%s: is delta rpm", rpm);
2082 solv_chksum_free(chksumh, 0);
2087 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2088 solv_chksum_add(chksumh, lead, l);
2090 s = pool_id2solvable(pool, repo_add_solvable(repo));
2091 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2093 repo_free_solvable(repo, s - pool->solvables, 1);
2094 solv_chksum_free(chksumh, 0);
2098 if (!(flags & REPO_NO_LOCATION))
2099 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2100 if (S_ISREG(stb.st_mode))
2101 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2102 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2104 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2106 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2108 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2111 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2112 chksumh = solv_chksum_free(chksumh, 0);
2116 if (!(flags & REPO_NO_INTERNALIZE))
2117 repodata_internalize(data);
2118 return s - pool->solvables;
2122 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2124 Pool *pool = repo->pool;
2126 RpmHead *rpmhead = rpmhandle;
2128 char *payloadformat;
2130 data = repo_add_repodata(repo, flags);
2131 if (headexists(rpmhead, TAG_PATCHESNAME))
2133 pool_error(pool, -1, "is a patch rpm");
2136 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2137 if (payloadformat && !strcmp(payloadformat, "drpm"))
2139 /* this is a delta rpm */
2140 pool_error(pool, -1, "is a delta rpm");
2143 s = pool_id2solvable(pool, repo_add_solvable(repo));
2144 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2146 repo_free_solvable(repo, s - pool->solvables, 1);
2149 if (!(flags & REPO_NO_INTERNALIZE))
2150 repodata_internalize(data);
2151 return s - pool->solvables;
2155 linkhash(const char *lt, char *hash)
2158 const unsigned char *str = (const unsigned char *)lt;
2162 while ((c = *str++) != 0)
2164 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2168 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2170 RpmHead *rpmhead = rpmhandle;
2175 unsigned int *di, diidx;
2176 unsigned int *co = 0;
2177 unsigned int *ff = 0;
2178 unsigned int lastdir;
2181 int cnt, dcnt, cnt2;
2186 struct filelistinfo info;
2188 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2191 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2193 for (i = 0; i < dcnt; i++)
2194 (*cb)(cbdata, dn[i], 0);
2198 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2204 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2205 if (!di || cnt != cnt2)
2212 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2213 if (!fm || cnt != cnt2)
2221 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2223 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2224 if (!md || cnt != cnt2)
2234 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2236 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2237 if (!co || cnt != cnt2)
2248 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2250 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2251 if (!ff || cnt != cnt2)
2265 memset(&info, 0, sizeof(info));
2266 for (i = 0; i < cnt; i++)
2268 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2273 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2274 l = l1 + strlen(bn[i]) + 1;
2278 space = solv_realloc(space, spacen);
2280 if (lastdir != diidx)
2282 strcpy(space, dn[diidx]);
2286 strcpy(space + l1, bn[i]);
2287 info.diridx = diidx;
2293 info.digest = md[i];
2294 if (fm && S_ISLNK(fm[i]))
2299 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2305 linkhash(lt[i], md5);
2311 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2317 (*cb)(cbdata, space, &info);
2331 rpm_query(void *rpmhandle, Id what)
2333 const char *name, *arch, *sourcerpm;
2337 RpmHead *rpmhead = rpmhandle;
2342 name = headstring(rpmhead, TAG_NAME);
2345 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2346 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2347 arch = headstring(rpmhead, TAG_ARCH);
2350 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2357 evr = headtoevr(rpmhead);
2358 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2360 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2364 name = headstring(rpmhead, TAG_NAME);
2365 r = solv_strdup(name);
2367 case SOLVABLE_SUMMARY:
2368 name = headstring(rpmhead, TAG_SUMMARY);
2369 r = solv_strdup(name);
2371 case SOLVABLE_DESCRIPTION:
2372 name = headstring(rpmhead, TAG_DESCRIPTION);
2373 r = solv_strdup(name);
2376 r = headtoevr(rpmhead);
2383 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2385 RpmHead *rpmhead = rpmhandle;
2390 case SOLVABLE_INSTALLTIME:
2391 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2392 return u32 ? u32 : notfound;
2398 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2400 struct rpmdbentry *entries;
2403 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2406 queue_empty(rpmdbidq);
2407 for (i = 0; i < nentries; i++)
2408 queue_push(rpmdbidq, entries[i].rpmdbid);
2415 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2417 struct rpmdbstate *state = rpmstate;
2420 r = getrpmdbid(state, rpmdbid);
2422 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2423 return r <= 0 ? 0 : state->rpmhead;
2427 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2429 struct rpmdbstate *state = rpmstate;
2430 /* int headerstart, headerend; */
2432 unsigned int sigdsize, sigcnt, l;
2433 unsigned char lead[4096];
2434 int forcebinary = 0;
2436 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2438 pool_error(state->pool, 0, "%s: not a rpm", name);
2441 forcebinary = lead[6] != 0 || lead[7] != 1;
2442 if (lead[78] != 0 || lead[79] != 5)
2444 pool_error(state->pool, 0, "%s: not a V5 header", name);
2447 if (getu32(lead + 96) != 0x8eade801)
2449 pool_error(state->pool, 0, "%s: bad signature header", name);
2452 sigcnt = getu32(lead + 96 + 8);
2453 sigdsize = getu32(lead + 96 + 12);
2454 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2456 pool_error(state->pool, 0, "%s: bad signature header", name);
2459 sigdsize += sigcnt * 16;
2460 sigdsize = (sigdsize + 7) & ~7;
2461 /* headerstart = 96 + 16 + sigdsize; */
2464 l = sigdsize > 4096 ? 4096 : sigdsize;
2465 if (fread(lead, l, 1, fp) != 1)
2467 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2472 if (fread(lead, 16, 1, fp) != 1)
2474 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2477 if (getu32(lead) != 0x8eade801)
2479 pool_error(state->pool, 0, "%s: bad header", name);
2482 sigcnt = getu32(lead + 8);
2483 sigdsize = getu32(lead + 12);
2484 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2486 pool_error(state->pool, 0, "%s: bad header", name);
2489 l = sigdsize + sigcnt * 16;
2490 /* headerend = headerstart + 16 + l; */
2491 if (l > state->rpmheadsize)
2493 state->rpmheadsize = l + 128;
2494 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2496 rpmhead = state->rpmhead;
2497 if (fread(rpmhead->data, l, 1, fp) != 1)
2499 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2502 rpmhead->forcebinary = forcebinary;
2503 rpmhead->cnt = sigcnt;
2504 rpmhead->dcnt = sigdsize;
2505 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2509 #ifdef ENABLE_RPMDB_BYRPMHEADER
2512 rpm_byrpmh(void *rpmstate, Header h)
2514 struct rpmdbstate *state = rpmstate;
2515 const unsigned char *uh;
2516 unsigned int sigdsize, sigcnt, l;
2520 uh = headerUnload(h);
2522 uh = headerUnload(h, NULL);
2526 sigcnt = getu32(uh);
2527 sigdsize = getu32(uh + 4);
2528 l = sigdsize + sigcnt * 16;
2529 if (l > state->rpmheadsize)
2531 state->rpmheadsize = l + 128;
2532 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2534 rpmhead = state->rpmhead;
2535 memcpy(rpmhead->data, uh + 8, l - 8);
2537 rpmhead->forcebinary = 0;
2538 rpmhead->cnt = sigcnt;
2539 rpmhead->dcnt = sigdsize;
2540 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;