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]);
775 set_description_author(Repodata *data, Id handle, char *str)
778 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
779 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
783 /* oh my, found SUSE special author section */
785 str = solv_strdup(str);
788 while (l > 0 && str[l - 1] == '\n')
791 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
793 aut = str; /* copy over */
794 while (*p == ' ' || *p == '\n')
807 while (aut != str && aut[-1] == '\n')
811 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
815 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
819 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
825 name = headstring(rpmhead, TAG_NAME);
828 pool_error(pool, 0, "package has no name");
831 if (!strcmp(name, "gpg-pubkey"))
833 s->name = pool_str2id(pool, name, 1);
834 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
835 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
836 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
839 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
840 s->arch = ARCH_NOSRC;
845 s->arch = ARCH_NOARCH;
846 evr = headtoevr(rpmhead);
847 s->evr = pool_str2id(pool, evr, 1);
848 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
850 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
851 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
852 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
853 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
854 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
855 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
857 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
858 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
859 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
860 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
861 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
862 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
869 unsigned long long u64;
871 handle = s - pool->solvables;
872 str = headstring(rpmhead, TAG_SUMMARY);
874 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
875 str = headstring(rpmhead, TAG_DESCRIPTION);
877 set_description_author(data, handle, str);
878 str = headstring(rpmhead, TAG_GROUP);
880 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
881 str = headstring(rpmhead, TAG_LICENSE);
883 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
884 str = headstring(rpmhead, TAG_URL);
886 repodata_set_str(data, handle, SOLVABLE_URL, str);
887 str = headstring(rpmhead, TAG_DISTRIBUTION);
889 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
890 str = headstring(rpmhead, TAG_PACKAGER);
892 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
893 if ((flags & RPM_ADD_WITH_PKGID) != 0)
895 unsigned char *chksum;
896 unsigned int chksumsize;
897 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
898 if (chksum && chksumsize == 16)
899 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
901 if ((flags & RPM_ADD_WITH_HDRID) != 0)
903 str = headstring(rpmhead, TAG_SHA1HEADER);
904 if (str && strlen(str) == 40)
905 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
906 else if (str && strlen(str) == 64)
907 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
909 u32 = headint32(rpmhead, TAG_BUILDTIME);
911 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
912 u32 = headint32(rpmhead, TAG_INSTALLTIME);
914 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
915 u64 = headint64(rpmhead, TAG_LONGSIZE);
917 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
920 u32 = headint32(rpmhead, TAG_SIZE);
922 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
925 repodata_set_sourcepkg(data, handle, sourcerpm);
926 if ((flags & RPM_ADD_TRIGGERS) != 0)
929 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
932 for (; (id = repo->idarraydata[ida]) != 0; ida++)
934 /* we currently do not support rel ids in incore data, so
935 * strip off versioning information */
938 Reldep *rd = GETRELDEP(pool, id);
943 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
947 if ((flags & RPM_ADD_NO_FILELIST) == 0)
948 addfilelist(data, handle, rpmhead);
949 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
950 addchangelog(data, handle, rpmhead);
957 /******************************************************************/
958 /* Rpm Database stuff
965 RpmHead *rpmhead; /* header storage space */
969 DB_ENV *dbenv; /* database environment */
970 DB *db; /* packages database */
971 int byteswapped; /* endianess of packages database */
979 #define ENTRIES_BLOCK 255
980 #define NAMEDATA_BLOCK 1023
983 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
986 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
988 # if defined(WORDS_BIGENDIAN)
993 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
995 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
999 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1002 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1004 # if defined(WORDS_BIGENDIAN)
1009 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1011 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1015 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1017 opendbenv(struct rpmdbstate *state, const char *rootdir)
1019 char dbpath[PATH_MAX];
1023 if (db_env_create(&dbenv, 0))
1024 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1025 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1026 dbenv->set_thread_count(dbenv, 8);
1028 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1029 if (access(dbpath, W_OK) == -1)
1031 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1036 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1038 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1043 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1044 dbenv->close(dbenv, 0);
1047 state->dbenv = dbenv;
1052 openpkgdb(struct rpmdbstate *state)
1054 if (state->dbopened)
1055 return state->dbopened > 0 ? 1 : 0;
1056 state->dbopened = -1;
1057 if (!state->dbenv && !opendbenv(state, state->rootdir))
1059 if (db_create(&state->db, state->dbenv, 0))
1061 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1063 state->dbenv->close(state->dbenv, 0);
1067 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1069 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1070 state->db->close(state->db, 0);
1072 state->dbenv->close(state->dbenv, 0);
1076 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1078 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1079 state->db->close(state->db, 0);
1081 state->dbenv->close(state->dbenv, 0);
1085 state->dbopened = 1;
1089 /* get the rpmdbids of all installed packages from the Name index database.
1090 * This is much faster then querying the big Packages database */
1091 static struct rpmdbentry *
1092 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1106 struct rpmdbentry *entries = 0;
1113 if (!state->dbenv && !opendbenv(state, state->rootdir))
1115 dbenv = state->dbenv;
1116 if (db_create(&db, dbenv, 0))
1118 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1121 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1123 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1127 if (db->get_byteswapped(db, &byteswapped))
1129 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1133 if (db->cursor(db, NULL, &dbc, 0))
1135 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1139 memset(&dbkey, 0, sizeof(dbkey));
1140 memset(&dbdata, 0, sizeof(dbdata));
1143 dbkey.data = (void *)match;
1144 dbkey.size = strlen(match);
1146 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1148 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1152 nameoff = namedatal;
1155 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1156 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1157 namedata[namedatal + dbkey.size] = 0;
1158 namedatal += dbkey.size + 1;
1160 while(dl >= RPM_INDEX_SIZE)
1162 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1163 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1164 entries[nentries].nameoff = nameoff;
1166 dp += RPM_INDEX_SIZE;
1167 dl -= RPM_INDEX_SIZE;
1174 /* make sure that enteries is != 0 if there was no error */
1176 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1177 *nentriesp = nentries;
1179 *namedatap = namedata;
1183 /* retrive header by rpmdbid */
1185 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1187 unsigned char buf[16];
1194 pool_error(state->pool, 0, "illegal rpmdbid");
1197 if (state->dbopened != 1 && !openpkgdb(state))
1199 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1200 memset(&dbkey, 0, sizeof(dbkey));
1201 memset(&dbdata, 0, sizeof(dbdata));
1206 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1208 if (dbdata.size < 8)
1210 pool_error(state->pool, 0, "corrupt rpm database (size)");
1213 if (dbdata.size > state->rpmheadsize)
1215 state->rpmheadsize = dbdata.size + 128;
1216 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1218 rpmhead = state->rpmhead;
1219 memcpy(buf, dbdata.data, 8);
1220 rpmhead->forcebinary = 1;
1221 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1222 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1223 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1225 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1228 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1229 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1233 /* retrive header by berkeleydb cursor */
1235 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1237 unsigned char buf[16];
1243 memset(&dbkey, 0, sizeof(dbkey));
1244 memset(&dbdata, 0, sizeof(dbdata));
1245 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1247 if (dbkey.size != 4)
1248 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1249 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1250 if (dbid == 0) /* the join key */
1252 if (dbdata.size < 8)
1253 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1254 if (dbdata.size > state->rpmheadsize)
1256 state->rpmheadsize = dbdata.size + 128;
1257 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1259 rpmhead = state->rpmhead;
1260 memcpy(buf, dbdata.data, 8);
1261 rpmhead->forcebinary = 1;
1262 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1263 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1264 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1265 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1266 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1267 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1274 freestate(struct rpmdbstate *state)
1280 solv_free(state->rootdir);
1282 state->db->close(state->db, 0);
1284 state->dbenv->close(state->dbenv, 0);
1285 solv_free(state->rpmhead);
1289 rpm_state_create(Pool *pool, const char *rootdir)
1291 struct rpmdbstate *state;
1292 state = solv_calloc(1, sizeof(*state));
1295 state->rootdir = solv_strdup(rootdir);
1300 rpm_state_free(void *state)
1303 return solv_free(state);
1307 count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
1309 char dbpath[PATH_MAX];
1310 struct stat statbuf;
1317 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1318 if (stat(dbpath, &statbuf))
1320 memset(&dbkey, 0, sizeof(dbkey));
1321 memset(&dbdata, 0, sizeof(dbdata));
1322 if (db_create(&db, dbenv, 0))
1324 pool_error(pool, 0, "db_create: %s", strerror(errno));
1327 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1329 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1333 if (db->cursor(db, NULL, &dbc, 0))
1336 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1339 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1340 count += dbdata.size / RPM_INDEX_SIZE;
1346 /******************************************************************/
1349 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1357 from = fromrepo->idarraydata + fromoff;
1358 for (ida = from, cc = 0; *ida; ida++, cc++)
1362 ido = repo_reserve_ids(repo, 0, cc);
1363 ida = repo->idarraydata + ido;
1364 memcpy(ida, from, (cc + 1) * sizeof(Id));
1365 repo->idarraysize += cc + 1;
1369 #define COPYDIR_DIRCACHE_SIZE 512
1371 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1374 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1376 if (cache && cache[did & 255] == did)
1377 return cache[(did & 255) + 256];
1378 return copydir_complex(pool, data, fromdata, did, cache);
1382 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1384 Id parent = dirpool_parent(&fromdata->dirpool, did);
1385 Id compid = dirpool_compid(&fromdata->dirpool, did);
1387 parent = copydir(pool, data, fromdata, parent, cache);
1388 if (data->localpool || fromdata->localpool)
1389 compid = repodata_translate_id(data, fromdata, compid, 1);
1390 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1393 cache[did & 255] = did;
1394 cache[(did & 255) + 256] = compid;
1399 struct solvable_copy_cbdata {
1407 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1409 struct solvable_copy_cbdata *cbdata = vcbdata;
1411 Repodata *data = cbdata->data;
1412 Id handle = cbdata->handle;
1413 Pool *pool = data->repo->pool;
1415 keyname = key->name;
1418 case REPOKEY_TYPE_ID:
1419 case REPOKEY_TYPE_CONSTANTID:
1420 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1422 if (data->localpool || fromdata->localpool)
1423 id = repodata_translate_id(data, fromdata, id, 1);
1424 if (key->type == REPOKEY_TYPE_ID)
1425 repodata_set_id(data, handle, keyname, id);
1426 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1427 repodata_set_constantid(data, handle, keyname, id);
1429 repodata_add_idarray(data, handle, keyname, id);
1431 case REPOKEY_TYPE_STR:
1432 repodata_set_str(data, handle, keyname, kv->str);
1434 case REPOKEY_TYPE_VOID:
1435 repodata_set_void(data, handle, keyname);
1437 case REPOKEY_TYPE_NUM:
1438 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1440 case REPOKEY_TYPE_CONSTANT:
1441 repodata_set_constant(data, handle, keyname, kv->num);
1443 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1445 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1446 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1448 case REPOKEY_TYPE_DIRSTRARRAY:
1450 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1451 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1453 case REPOKEY_TYPE_FLEXARRAY:
1456 assert(cbdata->subhandle);
1457 cbdata->handle = cbdata->subhandle;
1458 cbdata->subhandle = 0;
1463 assert(!cbdata->subhandle);
1464 cbdata->subhandle = cbdata->handle;
1466 cbdata->handle = repodata_new_handle(data);
1467 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1476 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1479 Repo *repo = s->repo;
1480 Pool *pool = repo->pool;
1481 Repo *fromrepo = r->repo;
1482 struct solvable_copy_cbdata cbdata;
1484 /* copy solvable data */
1488 s->vendor = r->vendor;
1489 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1490 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1491 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1492 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1493 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1494 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1495 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1496 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1498 /* copy all attributes */
1502 cbdata.handle = s - pool->solvables;
1503 cbdata.subhandle = 0;
1504 cbdata.dircache = dircache;
1505 p = r - fromrepo->pool->solvables;
1507 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1509 FOR_REPODATAS(fromrepo, i, data)
1511 if (p < data->start || p >= data->end)
1513 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1518 /* used to sort entries by package name that got returned in some database order */
1520 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1522 struct rpmdbentry const *a = va, *b = vb;
1523 char *namedata = dp;
1525 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1528 return a->rpmdbid - b->rpmdbid;
1532 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1535 Pool *pool = repo->pool;
1536 Solvable *a = pool->solvables + *(Id *)va;
1537 Solvable *b = pool->solvables + *(Id *)vb;
1540 if (a->name != b->name)
1541 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1542 rpmdbid = repo->rpmdbid;
1543 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1547 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1549 Pool *pool = repo->pool;
1552 tmp = pool->solvables[pa];
1553 pool->solvables[pa] = pool->solvables[pb];
1554 pool->solvables[pb] = tmp;
1557 Id tmpid = repo->rpmdbid[pa - repo->start];
1558 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1559 repo->rpmdbid[pb - repo->start] = tmpid;
1561 /* only works if nothing is already internalized! */
1563 repodata_swap_attrs(data, pa, pb);
1567 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1569 memset(cookie, 0, 32);
1570 cookie[3] = RPMDB_COOKIE_VERSION;
1571 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1572 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1576 * read rpm db as repo
1581 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1583 Pool *pool = repo->pool;
1584 char dbpath[PATH_MAX];
1585 struct stat packagesstat;
1586 unsigned char newcookie[32];
1587 const unsigned char *oldcookie = 0;
1588 Id oldcookietype = 0;
1590 int count = 0, done = 0;
1591 const char *rootdir = 0;
1592 struct rpmdbstate state;
1597 now = solv_timems(0);
1598 memset(&state, 0, sizeof(state));
1601 data = repo_add_repodata(repo, flags);
1603 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1605 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1610 if (flags & REPO_USE_ROOTDIR)
1611 rootdir = pool_get_rootdir(pool);
1612 if (!opendbenv(&state, rootdir))
1615 /* XXX: should get ro lock of Packages database! */
1616 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1617 if (stat(dbpath, &packagesstat))
1619 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1623 mkrpmdbcookie(&packagesstat, newcookie);
1624 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1627 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1628 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1630 int solvstart = 0, solvend = 0;
1634 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1635 repo_empty(ref, 1); /* get it out of the way */
1636 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1637 count = count_headers(pool, rootdir, state.dbenv);
1638 if (!openpkgdb(&state))
1643 if (state.db->cursor(state.db, NULL, &dbc, 0))
1646 return pool_error(pool, -1, "db->cursor failed");
1650 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1660 s = pool_id2solvable(pool, repo_add_solvable(repo));
1662 solvstart = s - pool->solvables;
1663 solvend = s - pool->solvables + 1;
1666 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1667 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1668 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1675 /* We can reuse this solvable, but make sure it's still
1676 associated with this repo. */
1677 memset(s, 0, sizeof(*s));
1680 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1684 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1685 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1691 /* oops, could not reuse. free it instead */
1692 repo_free_solvable(repo, s - pool->solvables, 1);
1696 /* now sort all solvables in the new solvstart..solvend block */
1697 if (solvend - solvstart > 1)
1699 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1700 for (i = solvstart; i < solvend; i++)
1701 pkgids[i - solvstart] = i;
1702 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1704 for (i = solvstart; i < solvend; i++)
1706 int j = pkgids[i - solvstart];
1708 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1710 swap_solvables(repo, data, i, j);
1717 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1718 struct rpmdbentry *entries = 0, *rp;
1721 unsigned int refmask, h;
1725 memset(dircache, 0, sizeof(dircache));
1727 /* get ids of installed rpms */
1728 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1737 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1739 /* create hash from dbid to ref */
1740 refmask = mkmask(ref->nsolvables);
1741 refhash = solv_calloc(refmask + 1, sizeof(Id));
1742 for (i = 0; i < ref->end - ref->start; i++)
1744 if (!ref->rpmdbid[i])
1746 h = ref->rpmdbid[i] & refmask;
1748 h = (h + 317) & refmask;
1749 refhash[h] = i + 1; /* make it non-zero */
1752 /* count the misses, they will cost us time */
1753 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1755 for (i = 0, rp = entries; i < nentries; i++, rp++)
1759 Id dbid = rp->rpmdbid;
1761 while ((id = refhash[h]))
1763 if (ref->rpmdbid[id - 1] == dbid)
1765 h = (h + 317) & refmask;
1774 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1775 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1777 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1779 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1781 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1783 Id dbid = rp->rpmdbid;
1784 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1788 while ((id = refhash[h]))
1790 if (ref->rpmdbid[id - 1] == dbid)
1792 h = (h + 317) & refmask;
1796 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1799 solvable_copy(s, r, data, dircache);
1804 res = getrpmdbid(&state, dbid);
1808 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1811 solv_free(namedata);
1815 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1816 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1820 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1821 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1826 solv_free(namedata);
1828 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1833 if (!(flags & REPO_NO_INTERNALIZE))
1834 repodata_internalize(data);
1835 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1836 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1837 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1838 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1839 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)));
1844 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1850 return repo_add_rpmdb(repo, 0, flags);
1851 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1852 if (repo_add_solv(ref, fp, 0) != 0)
1857 if (ref && ref->start == ref->end)
1863 repo_disable_paging(ref);
1864 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1870 static inline unsigned int
1871 getu32(const unsigned char *dp)
1873 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1878 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1880 unsigned int sigdsize, sigcnt, l;
1881 Pool *pool = repo->pool;
1883 RpmHead *rpmhead = 0;
1884 int rpmheadsize = 0;
1885 char *payloadformat;
1887 unsigned char lead[4096];
1888 int headerstart, headerend;
1891 unsigned char pkgid[16];
1892 unsigned char leadsigid[16];
1893 unsigned char hdrid[32];
1894 int pkgidtype, leadsigidtype, hdridtype;
1897 void *leadsigchksumh = 0;
1898 int forcebinary = 0;
1900 data = repo_add_repodata(repo, flags);
1902 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1903 chksumtype = REPOKEY_TYPE_SHA256;
1904 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1905 chksumtype = REPOKEY_TYPE_SHA1;
1907 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1909 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1912 if (fstat(fileno(fp), &stb))
1914 pool_error(pool, -1, "fstat: %s", strerror(errno));
1919 chksumh = solv_chksum_create(chksumtype);
1920 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1921 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1922 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1924 pool_error(pool, -1, "%s: not a rpm", rpm);
1928 forcebinary = lead[6] != 0 || lead[7] != 1;
1930 solv_chksum_add(chksumh, lead, 96 + 16);
1932 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1933 if (lead[78] != 0 || lead[79] != 5)
1935 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1939 if (getu32(lead + 96) != 0x8eade801)
1941 pool_error(pool, -1, "%s: bad signature header", rpm);
1945 sigcnt = getu32(lead + 96 + 8);
1946 sigdsize = getu32(lead + 96 + 12);
1947 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1949 pool_error(pool, -1, "%s: bad signature header", rpm);
1953 sigdsize += sigcnt * 16;
1954 sigdsize = (sigdsize + 7) & ~7;
1955 headerstart = 96 + 16 + sigdsize;
1956 pkgidtype = leadsigidtype = hdridtype = 0;
1957 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1959 /* extract pkgid or hdrid from the signature header */
1960 if (sigdsize > rpmheadsize)
1962 rpmheadsize = sigdsize + 128;
1963 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1965 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1967 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1972 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1974 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1975 rpmhead->forcebinary = 0;
1976 rpmhead->cnt = sigcnt;
1977 rpmhead->dcnt = sigdsize - sigcnt * 16;
1978 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1979 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1981 unsigned char *chksum;
1982 unsigned int chksumsize;
1983 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1984 if (chksum && chksumsize == 16)
1986 pkgidtype = REPOKEY_TYPE_MD5;
1987 memcpy(pkgid, chksum, 16);
1990 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1992 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1993 if (str && strlen(str) == 40)
1995 if (solv_hex2bin(&str, hdrid, 20) == 20)
1996 hdridtype = REPOKEY_TYPE_SHA1;
1998 else if (str && strlen(str) == 64)
2000 if (solv_hex2bin(&str, hdrid, 32) == 32)
2001 hdridtype = REPOKEY_TYPE_SHA256;
2007 /* just skip the signature header */
2010 l = sigdsize > 4096 ? 4096 : sigdsize;
2011 if (fread(lead, l, 1, fp) != 1)
2013 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2018 solv_chksum_add(chksumh, lead, l);
2020 solv_chksum_add(leadsigchksumh, lead, l);
2026 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2027 leadsigidtype = REPOKEY_TYPE_MD5;
2029 if (fread(lead, 16, 1, fp) != 1)
2031 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2036 solv_chksum_add(chksumh, lead, 16);
2037 if (getu32(lead) != 0x8eade801)
2039 pool_error(pool, -1, "%s: bad header", rpm);
2043 sigcnt = getu32(lead + 8);
2044 sigdsize = getu32(lead + 12);
2045 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2047 pool_error(pool, -1, "%s: bad header", rpm);
2051 l = sigdsize + sigcnt * 16;
2052 headerend = headerstart + 16 + l;
2053 if (l > rpmheadsize)
2055 rpmheadsize = l + 128;
2056 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2058 if (fread(rpmhead->data, l, 1, fp) != 1)
2060 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2065 solv_chksum_add(chksumh, rpmhead->data, l);
2066 rpmhead->forcebinary = forcebinary;
2067 rpmhead->cnt = sigcnt;
2068 rpmhead->dcnt = sigdsize;
2069 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2070 if (headexists(rpmhead, TAG_PATCHESNAME))
2072 /* this is a patch rpm, ignore */
2073 pool_error(pool, -1, "%s: is patch rpm", rpm);
2075 solv_chksum_free(chksumh, 0);
2079 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2080 if (payloadformat && !strcmp(payloadformat, "drpm"))
2082 /* this is a delta rpm */
2083 pool_error(pool, -1, "%s: is delta rpm", rpm);
2085 solv_chksum_free(chksumh, 0);
2090 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2091 solv_chksum_add(chksumh, lead, l);
2093 s = pool_id2solvable(pool, repo_add_solvable(repo));
2094 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2096 repo_free_solvable(repo, s - pool->solvables, 1);
2097 solv_chksum_free(chksumh, 0);
2101 if (!(flags & REPO_NO_LOCATION))
2102 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2103 if (S_ISREG(stb.st_mode))
2104 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2105 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2107 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2109 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2111 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2114 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2115 chksumh = solv_chksum_free(chksumh, 0);
2119 if (!(flags & REPO_NO_INTERNALIZE))
2120 repodata_internalize(data);
2121 return s - pool->solvables;
2125 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2127 Pool *pool = repo->pool;
2129 RpmHead *rpmhead = rpmhandle;
2131 char *payloadformat;
2133 data = repo_add_repodata(repo, flags);
2134 if (headexists(rpmhead, TAG_PATCHESNAME))
2136 pool_error(pool, -1, "is a patch rpm");
2139 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2140 if (payloadformat && !strcmp(payloadformat, "drpm"))
2142 /* this is a delta rpm */
2143 pool_error(pool, -1, "is a delta rpm");
2146 s = pool_id2solvable(pool, repo_add_solvable(repo));
2147 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2149 repo_free_solvable(repo, s - pool->solvables, 1);
2152 if (!(flags & REPO_NO_INTERNALIZE))
2153 repodata_internalize(data);
2154 return s - pool->solvables;
2158 linkhash(const char *lt, char *hash)
2161 const unsigned char *str = (const unsigned char *)lt;
2165 while ((c = *str++) != 0)
2167 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2171 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2173 RpmHead *rpmhead = rpmhandle;
2178 unsigned int *di, diidx;
2179 unsigned int *co = 0;
2180 unsigned int *ff = 0;
2181 unsigned int lastdir;
2184 int cnt, dcnt, cnt2;
2189 struct filelistinfo info;
2191 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2194 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2196 for (i = 0; i < dcnt; i++)
2197 (*cb)(cbdata, dn[i], 0);
2201 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2207 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2208 if (!di || cnt != cnt2)
2215 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2216 if (!fm || cnt != cnt2)
2224 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2226 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2227 if (!md || cnt != cnt2)
2237 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2239 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2240 if (!co || cnt != cnt2)
2251 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2253 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2254 if (!ff || cnt != cnt2)
2268 memset(&info, 0, sizeof(info));
2269 for (i = 0; i < cnt; i++)
2271 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2276 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2277 l = l1 + strlen(bn[i]) + 1;
2281 space = solv_realloc(space, spacen);
2283 if (lastdir != diidx)
2285 strcpy(space, dn[diidx]);
2289 strcpy(space + l1, bn[i]);
2290 info.diridx = diidx;
2296 info.digest = md[i];
2297 if (fm && S_ISLNK(fm[i]))
2302 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2308 linkhash(lt[i], md5);
2314 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2320 (*cb)(cbdata, space, &info);
2334 rpm_query(void *rpmhandle, Id what)
2336 const char *name, *arch, *sourcerpm;
2340 RpmHead *rpmhead = rpmhandle;
2345 name = headstring(rpmhead, TAG_NAME);
2348 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2349 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2350 arch = headstring(rpmhead, TAG_ARCH);
2353 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2360 evr = headtoevr(rpmhead);
2361 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2363 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2367 name = headstring(rpmhead, TAG_NAME);
2368 r = solv_strdup(name);
2370 case SOLVABLE_SUMMARY:
2371 name = headstring(rpmhead, TAG_SUMMARY);
2372 r = solv_strdup(name);
2374 case SOLVABLE_DESCRIPTION:
2375 name = headstring(rpmhead, TAG_DESCRIPTION);
2376 r = solv_strdup(name);
2379 r = headtoevr(rpmhead);
2386 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2388 RpmHead *rpmhead = rpmhandle;
2393 case SOLVABLE_INSTALLTIME:
2394 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2395 return u32 ? u32 : notfound;
2401 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2403 struct rpmdbentry *entries;
2406 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2409 queue_empty(rpmdbidq);
2410 for (i = 0; i < nentries; i++)
2411 queue_push(rpmdbidq, entries[i].rpmdbid);
2418 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2420 struct rpmdbstate *state = rpmstate;
2423 r = getrpmdbid(state, rpmdbid);
2425 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2426 return r <= 0 ? 0 : state->rpmhead;
2430 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2432 struct rpmdbstate *state = rpmstate;
2433 /* int headerstart, headerend; */
2435 unsigned int sigdsize, sigcnt, l;
2436 unsigned char lead[4096];
2437 int forcebinary = 0;
2439 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2441 pool_error(state->pool, 0, "%s: not a rpm", name);
2444 forcebinary = lead[6] != 0 || lead[7] != 1;
2445 if (lead[78] != 0 || lead[79] != 5)
2447 pool_error(state->pool, 0, "%s: not a V5 header", name);
2450 if (getu32(lead + 96) != 0x8eade801)
2452 pool_error(state->pool, 0, "%s: bad signature header", name);
2455 sigcnt = getu32(lead + 96 + 8);
2456 sigdsize = getu32(lead + 96 + 12);
2457 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2459 pool_error(state->pool, 0, "%s: bad signature header", name);
2462 sigdsize += sigcnt * 16;
2463 sigdsize = (sigdsize + 7) & ~7;
2464 /* headerstart = 96 + 16 + sigdsize; */
2467 l = sigdsize > 4096 ? 4096 : sigdsize;
2468 if (fread(lead, l, 1, fp) != 1)
2470 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2475 if (fread(lead, 16, 1, fp) != 1)
2477 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2480 if (getu32(lead) != 0x8eade801)
2482 pool_error(state->pool, 0, "%s: bad header", name);
2485 sigcnt = getu32(lead + 8);
2486 sigdsize = getu32(lead + 12);
2487 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2489 pool_error(state->pool, 0, "%s: bad header", name);
2492 l = sigdsize + sigcnt * 16;
2493 /* headerend = headerstart + 16 + l; */
2494 if (l > state->rpmheadsize)
2496 state->rpmheadsize = l + 128;
2497 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2499 rpmhead = state->rpmhead;
2500 if (fread(rpmhead->data, l, 1, fp) != 1)
2502 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2505 rpmhead->forcebinary = forcebinary;
2506 rpmhead->cnt = sigcnt;
2507 rpmhead->dcnt = sigdsize;
2508 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2512 #ifdef ENABLE_RPMDB_BYRPMHEADER
2515 rpm_byrpmh(void *rpmstate, Header h)
2517 struct rpmdbstate *state = rpmstate;
2518 const unsigned char *uh;
2519 unsigned int sigdsize, sigcnt, l;
2523 uh = headerUnload(h);
2525 uh = headerUnload(h, NULL);
2529 sigcnt = getu32(uh);
2530 sigdsize = getu32(uh + 4);
2531 l = sigdsize + sigcnt * 16;
2532 if (l > state->rpmheadsize)
2534 state->rpmheadsize = l + 128;
2535 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2537 rpmhead = state->rpmhead;
2538 memcpy(rpmhead->data, uh + 8, l - 8);
2540 rpmhead->forcebinary = 0;
2541 rpmhead->cnt = sigcnt;
2542 rpmhead->dcnt = sigdsize;
2543 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;