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_IN ((1 << 6) | (1 << 9) | (1 << 10))
136 #define DEP_PRE_UN ((1 << 6) | (1 << 11) | (1 << 12))
138 #define FILEFLAG_GHOST (1 << 6)
142 # define RPM_INDEX_SIZE 4
144 # define RPM_INDEX_SIZE 8
148 typedef struct rpmhead {
152 int forcebinary; /* sigh, see rh#478907 */
153 unsigned char data[1];
157 static inline unsigned char *
158 headfindtag(RpmHead *h, int tag)
161 unsigned char *d, taga[4];
167 for (i = 0; i < h->cnt; i++, d -= 16)
168 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
174 headexists(RpmHead *h, int tag)
176 return headfindtag(h, tag) ? 1 : 0;
179 static unsigned int *
180 headint32array(RpmHead *h, int tag, int *cnt)
182 unsigned int i, o, *r;
183 unsigned char *d = headfindtag(h, tag);
185 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
187 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
188 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
189 if (o + 4 * i > h->dcnt)
192 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
195 for (o = 0; o < i; o++, d += 4)
196 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
200 /* returns the first entry of an integer array */
202 headint32(RpmHead *h, int tag)
205 unsigned char *d = headfindtag(h, tag);
207 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
209 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
210 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
211 if (i == 0 || o + 4 * i > h->dcnt)
214 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
217 static unsigned long long *
218 headint64array(RpmHead *h, int tag, int *cnt)
221 unsigned long long *r;
222 unsigned char *d = headfindtag(h, tag);
224 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
226 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
227 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
228 if (o + 8 * i > h->dcnt)
231 r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
234 for (o = 0; o < i; o++, d += 8)
236 unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
237 r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
242 /* returns the first entry of an 64bit integer array */
243 static unsigned long long
244 headint64(RpmHead *h, int tag)
247 unsigned char *d = headfindtag(h, tag);
248 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
250 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
251 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
252 if (i == 0 || o + 8 * i > h->dcnt)
255 i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
256 return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
259 static unsigned int *
260 headint16array(RpmHead *h, int tag, int *cnt)
262 unsigned int i, o, *r;
263 unsigned char *d = headfindtag(h, tag);
265 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
267 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
268 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
269 if (o + 4 * i > h->dcnt)
272 r = solv_calloc(i ? i : 1, sizeof(unsigned int));
275 for (o = 0; o < i; o++, d += 2)
276 r[o] = d[0] << 8 | d[1];
281 headstring(RpmHead *h, int tag)
284 unsigned char *d = headfindtag(h, tag);
285 /* 6: STRING, 9: I18NSTRING */
286 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
288 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
291 return (char *)h->dp + o;
295 headstringarray(RpmHead *h, int tag, int *cnt)
298 unsigned char *d = headfindtag(h, tag);
301 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
303 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
304 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
305 r = solv_calloc(i ? i : 1, sizeof(char *));
309 for (o = 0; o < i; o++)
313 d += strlen((char *)d) + 1;
314 if (d >= h->dp + h->dcnt)
323 static unsigned char *
324 headbinary(RpmHead *h, int tag, unsigned int *sizep)
327 unsigned char *d = headfindtag(h, tag);
328 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
330 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
331 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
332 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
339 static char *headtoevr(RpmHead *h)
347 version = headstring(h, TAG_VERSION);
348 release = headstring(h, TAG_RELEASE);
349 epoch = headint32(h, TAG_EPOCH);
350 if (!version || !release)
352 fprintf(stderr, "headtoevr: bad rpm header\n");
355 for (v = version; *v >= '0' && *v <= '9'; v++)
357 if (epoch || (v != version && *v == ':'))
359 char epochbuf[11]; /* 32bit decimal will fit in */
360 sprintf(epochbuf, "%u", epoch);
361 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
362 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
366 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
367 sprintf(evr, "%s-%s", version, release);
369 distepoch = headstring(h, TAG_DISTEPOCH);
370 if (distepoch && *distepoch)
373 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
375 strcpy(evr + l, distepoch);
382 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
384 if (str[solv_validutf8(str)])
386 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
387 repodata_set_str(repodata, handle, tag, ustr);
391 repodata_set_str(repodata, handle, tag, str);
395 #define MAKEDEPS_FILTER_WEAK (1 << 0)
396 #define MAKEDEPS_FILTER_STRONG (1 << 1)
397 #define MAKEDEPS_NO_RPMLIB (1 << 2)
400 * strong: 0: ignore strongness
401 * 1: filter to strong
405 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
409 int i, cc, nc, vc, fc;
411 unsigned int olddeps;
415 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
416 n = headstringarray(rpmhead, tagn, &nc);
420 v = headstringarray(rpmhead, tagv, &vc);
421 f = headint32array(rpmhead, tagf, &fc);
422 if (!v || !f || nc != vc || nc != fc)
424 char *pkgname = rpm_query(rpmhead, 0);
425 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
434 haspre = 0; /* add no prereq marker */
435 premask = DEP_PRE_IN | DEP_PRE_UN;
438 /* we do filtering */
440 for (i = 0; i < nc; i++)
442 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
444 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
445 if (!strncmp(n[i], "rpmlib(", 7))
447 if ((f[i] & premask) != 0)
452 else if (tagn == TAG_REQUIRENAME)
454 /* no filtering, just look for the first prereq */
455 for (i = 0; i < nc; i++)
456 if ((f[i] & premask) != 0)
469 cc += haspre; /* add slot for the prereq marker */
470 olddeps = repo_reserve_ids(repo, 0, cc);
471 ida = repo->idarraydata + olddeps;
478 haspre = 2; /* pass two: prereqs */
480 *ida++ = SOLVABLE_PREREQMARKER;
482 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
484 if (haspre == 1 && (f[i] & premask) != 0)
486 if (haspre == 2 && (f[i] & premask) == 0)
488 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
489 if (!strncmp(n[i], "rpmlib(", 7))
491 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
495 if ((f[i] & DEP_LESS) != 0)
497 if ((f[i] & DEP_EQUAL) != 0)
499 if ((f[i] & DEP_GREATER) != 0)
501 name = pool_str2id(pool, n[i], 1);
502 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
503 evr = pool_str2id(pool, v[i] + 2, 1);
505 evr = pool_str2id(pool, v[i], 1);
506 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
509 *ida++ = pool_str2id(pool, n[i], 1);
512 repo->idarraysize += cc + 1;
521 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
525 unsigned int *fkb, *fn, *fsz, *fm, *fino;
526 unsigned long long *fsz64;
527 unsigned int inotest[256], inotestok;
531 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
533 /* convert to kbyte */
534 fsz = solv_malloc2(fszc, sizeof(*fsz));
535 for (i = 0; i < fszc; i++)
536 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
539 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
541 /* convert to kbyte */
542 for (i = 0; i < fszc; i++)
544 fsz[i] = fsz[i] / 1024 + 1;
554 /* stupid rpm records sizes of directories, so we have to check the mode */
555 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
556 if (!fm || fc != fszc)
562 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
563 if (!fino || fc != fszc)
571 /* kill hardlinked entries */
573 if (fc < sizeof(inotest))
575 /* quick test just hashing the inode numbers */
576 memset(inotest, 0, sizeof(inotest));
577 for (i = 0; i < fc; i++)
580 if (fsz[i] == 0 || !S_ISREG(fm[i]))
581 continue; /* does not matter */
582 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
583 bit = 1 << (fino[i] & 31);
584 if ((inotest[off] & bit) != 0)
589 inotestok = 1; /* no conflict found */
593 /* hardlinked files are possible, check ino/dev pairs */
594 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
596 unsigned int mask, hash, hh;
597 if (!fdev || fc != fszc)
606 while ((mask & (mask - 1)) != 0)
607 mask = mask & (mask - 1);
609 if (mask > sizeof(inotest)/sizeof(*inotest))
610 fx = solv_calloc(mask, sizeof(unsigned int));
614 memset(fx, 0, mask * sizeof(unsigned int));
617 for (i = 0; i < fc; i++)
619 if (fsz[i] == 0 || !S_ISREG(fm[i]))
621 hash = (fino[i] + fdev[i] * 31) & mask;
623 while ((j = fx[hash]) != 0)
625 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
627 fsz[i] = 0; /* kill entry */
630 hash = (hash + hh++) & mask;
641 /* sum up inode count and kbytes for each directory */
642 fn = solv_calloc(dc, sizeof(unsigned int));
643 fkb = solv_calloc(dc, sizeof(unsigned int));
644 for (i = 0; i < fc; i++)
649 if (fsz[i] == 0 || !S_ISREG(fm[i]))
651 fkb[di[i]] += fsz[i];
656 for (i = 0; i < dc; i++)
662 Solvable *s = data->repo->pool->solvables + handle;
663 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
664 did = repodata_str2dir(data, "/usr/src", 1);
666 continue; /* work around rpm bug */
669 did = repodata_str2dir(data, dn[i], 1);
670 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
677 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
689 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
692 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
698 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
707 pool_error(data->repo->pool, 0, "bad filelist");
711 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
713 for (i = 0; i < bnc; i++)
718 if (di[i] == lastdii)
722 did = repodata_str2dir(data, dn[di[i]], 1);
724 did = repodata_str2dir(data, "/", 1);
728 if (b && *b == '/') /* work around rpm bug */
730 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
738 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
743 int i, cnc, cxc, ctc;
746 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
747 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
748 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
749 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
757 for (i = 0; i < ctc; i++)
759 Id h = repodata_new_handle(data);
761 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
763 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
765 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
768 for (i = 0; i < hq.count; i++)
769 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
777 set_description_author(Repodata *data, Id handle, char *str)
780 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
781 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
785 /* oh my, found SUSE special author section */
787 str = solv_strdup(str);
790 while (l > 0 && str[l - 1] == '\n')
793 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
795 aut = str; /* copy over */
796 while (*p == ' ' || *p == '\n')
809 while (aut != str && aut[-1] == '\n')
813 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
817 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
821 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
827 name = headstring(rpmhead, TAG_NAME);
830 pool_error(pool, 0, "package has no name");
833 if (!strcmp(name, "gpg-pubkey"))
835 s->name = pool_str2id(pool, name, 1);
836 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
837 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
838 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
841 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
842 s->arch = ARCH_NOSRC;
847 s->arch = ARCH_NOARCH;
848 evr = headtoevr(rpmhead);
849 s->evr = pool_str2id(pool, evr, 1);
850 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
852 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
853 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
854 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
855 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
856 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
857 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
859 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
860 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
861 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
862 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
863 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
864 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
871 unsigned long long u64;
873 handle = s - pool->solvables;
874 str = headstring(rpmhead, TAG_SUMMARY);
876 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
877 str = headstring(rpmhead, TAG_DESCRIPTION);
879 set_description_author(data, handle, str);
880 str = headstring(rpmhead, TAG_GROUP);
882 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
883 str = headstring(rpmhead, TAG_LICENSE);
885 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
886 str = headstring(rpmhead, TAG_URL);
888 repodata_set_str(data, handle, SOLVABLE_URL, str);
889 str = headstring(rpmhead, TAG_DISTRIBUTION);
891 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
892 str = headstring(rpmhead, TAG_PACKAGER);
894 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
895 if ((flags & RPM_ADD_WITH_PKGID) != 0)
897 unsigned char *chksum;
898 unsigned int chksumsize;
899 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
900 if (chksum && chksumsize == 16)
901 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
903 if ((flags & RPM_ADD_WITH_HDRID) != 0)
905 str = headstring(rpmhead, TAG_SHA1HEADER);
906 if (str && strlen(str) == 40)
907 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
908 else if (str && strlen(str) == 64)
909 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
911 u32 = headint32(rpmhead, TAG_BUILDTIME);
913 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
914 u32 = headint32(rpmhead, TAG_INSTALLTIME);
916 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
917 u64 = headint64(rpmhead, TAG_LONGSIZE);
919 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
922 u32 = headint32(rpmhead, TAG_SIZE);
924 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
927 repodata_set_sourcepkg(data, handle, sourcerpm);
928 if ((flags & RPM_ADD_TRIGGERS) != 0)
931 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
934 for (; (id = repo->idarraydata[ida]) != 0; ida++)
936 /* we currently do not support rel ids in incore data, so
937 * strip off versioning information */
940 Reldep *rd = GETRELDEP(pool, id);
945 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
949 if ((flags & RPM_ADD_NO_FILELIST) == 0)
950 addfilelist(data, handle, rpmhead);
951 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
952 addchangelog(data, handle, rpmhead);
959 /******************************************************************/
960 /* Rpm Database stuff
967 RpmHead *rpmhead; /* header storage space */
971 DB_ENV *dbenv; /* database environment */
972 DB *db; /* packages database */
973 int byteswapped; /* endianess of packages database */
981 #define ENTRIES_BLOCK 255
982 #define NAMEDATA_BLOCK 1023
985 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
988 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
990 # if defined(WORDS_BIGENDIAN)
995 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
997 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1001 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1004 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1006 # if defined(WORDS_BIGENDIAN)
1011 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1013 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1017 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1019 opendbenv(struct rpmdbstate *state, const char *rootdir)
1021 char dbpath[PATH_MAX];
1025 if (db_env_create(&dbenv, 0))
1026 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1027 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1028 dbenv->set_thread_count(dbenv, 8);
1030 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1031 if (access(dbpath, W_OK) == -1)
1033 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1038 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1040 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1045 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1046 dbenv->close(dbenv, 0);
1049 state->dbenv = dbenv;
1054 openpkgdb(struct rpmdbstate *state)
1056 if (state->dbopened)
1057 return state->dbopened > 0 ? 1 : 0;
1058 state->dbopened = -1;
1059 if (!state->dbenv && !opendbenv(state, state->rootdir))
1061 if (db_create(&state->db, state->dbenv, 0))
1063 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1065 state->dbenv->close(state->dbenv, 0);
1069 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1071 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1072 state->db->close(state->db, 0);
1074 state->dbenv->close(state->dbenv, 0);
1078 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1080 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1081 state->db->close(state->db, 0);
1083 state->dbenv->close(state->dbenv, 0);
1087 state->dbopened = 1;
1091 /* get the rpmdbids of all installed packages from the Name index database.
1092 * This is much faster then querying the big Packages database */
1093 static struct rpmdbentry *
1094 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1108 struct rpmdbentry *entries = 0;
1115 if (!state->dbenv && !opendbenv(state, state->rootdir))
1117 dbenv = state->dbenv;
1118 if (db_create(&db, dbenv, 0))
1120 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1123 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1125 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1129 if (db->get_byteswapped(db, &byteswapped))
1131 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1135 if (db->cursor(db, NULL, &dbc, 0))
1137 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1141 memset(&dbkey, 0, sizeof(dbkey));
1142 memset(&dbdata, 0, sizeof(dbdata));
1145 dbkey.data = (void *)match;
1146 dbkey.size = strlen(match);
1148 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1150 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1154 nameoff = namedatal;
1157 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1158 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1159 namedata[namedatal + dbkey.size] = 0;
1160 namedatal += dbkey.size + 1;
1162 while(dl >= RPM_INDEX_SIZE)
1164 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1165 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1166 entries[nentries].nameoff = nameoff;
1168 dp += RPM_INDEX_SIZE;
1169 dl -= RPM_INDEX_SIZE;
1176 /* make sure that enteries is != 0 if there was no error */
1178 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1179 *nentriesp = nentries;
1181 *namedatap = namedata;
1185 /* retrive header by rpmdbid */
1187 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1189 unsigned char buf[16];
1196 pool_error(state->pool, 0, "illegal rpmdbid");
1199 if (state->dbopened != 1 && !openpkgdb(state))
1201 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1202 memset(&dbkey, 0, sizeof(dbkey));
1203 memset(&dbdata, 0, sizeof(dbdata));
1208 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1210 if (dbdata.size < 8)
1212 pool_error(state->pool, 0, "corrupt rpm database (size)");
1215 if (dbdata.size > state->rpmheadsize)
1217 state->rpmheadsize = dbdata.size + 128;
1218 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1220 rpmhead = state->rpmhead;
1221 memcpy(buf, dbdata.data, 8);
1222 rpmhead->forcebinary = 1;
1223 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1224 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1225 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1227 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1230 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1231 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1235 /* retrive header by berkeleydb cursor */
1237 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1239 unsigned char buf[16];
1245 memset(&dbkey, 0, sizeof(dbkey));
1246 memset(&dbdata, 0, sizeof(dbdata));
1247 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1249 if (dbkey.size != 4)
1250 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1251 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1252 if (dbid == 0) /* the join key */
1254 if (dbdata.size < 8)
1255 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1256 if (dbdata.size > state->rpmheadsize)
1258 state->rpmheadsize = dbdata.size + 128;
1259 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1261 rpmhead = state->rpmhead;
1262 memcpy(buf, dbdata.data, 8);
1263 rpmhead->forcebinary = 1;
1264 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1265 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1266 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1267 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1268 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1269 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1276 freestate(struct rpmdbstate *state)
1282 solv_free(state->rootdir);
1284 state->db->close(state->db, 0);
1286 state->dbenv->close(state->dbenv, 0);
1287 solv_free(state->rpmhead);
1291 rpm_state_create(Pool *pool, const char *rootdir)
1293 struct rpmdbstate *state;
1294 state = solv_calloc(1, sizeof(*state));
1297 state->rootdir = solv_strdup(rootdir);
1302 rpm_state_free(void *state)
1305 return solv_free(state);
1309 count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
1311 char dbpath[PATH_MAX];
1312 struct stat statbuf;
1319 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1320 if (stat(dbpath, &statbuf))
1322 memset(&dbkey, 0, sizeof(dbkey));
1323 memset(&dbdata, 0, sizeof(dbdata));
1324 if (db_create(&db, dbenv, 0))
1326 pool_error(pool, 0, "db_create: %s", strerror(errno));
1329 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1331 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1335 if (db->cursor(db, NULL, &dbc, 0))
1338 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1341 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1342 count += dbdata.size / RPM_INDEX_SIZE;
1348 /******************************************************************/
1351 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1359 from = fromrepo->idarraydata + fromoff;
1360 for (ida = from, cc = 0; *ida; ida++, cc++)
1364 ido = repo_reserve_ids(repo, 0, cc);
1365 ida = repo->idarraydata + ido;
1366 memcpy(ida, from, (cc + 1) * sizeof(Id));
1367 repo->idarraysize += cc + 1;
1371 #define COPYDIR_DIRCACHE_SIZE 512
1373 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1376 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1378 if (cache && cache[did & 255] == did)
1379 return cache[(did & 255) + 256];
1380 return copydir_complex(pool, data, fromdata, did, cache);
1384 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1386 Id parent = dirpool_parent(&fromdata->dirpool, did);
1387 Id compid = dirpool_compid(&fromdata->dirpool, did);
1389 parent = copydir(pool, data, fromdata, parent, cache);
1390 if (data->localpool || fromdata->localpool)
1391 compid = repodata_translate_id(data, fromdata, compid, 1);
1392 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1395 cache[did & 255] = did;
1396 cache[(did & 255) + 256] = compid;
1401 struct solvable_copy_cbdata {
1409 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1411 struct solvable_copy_cbdata *cbdata = vcbdata;
1413 Repodata *data = cbdata->data;
1414 Id handle = cbdata->handle;
1415 Pool *pool = data->repo->pool;
1417 keyname = key->name;
1420 case REPOKEY_TYPE_ID:
1421 case REPOKEY_TYPE_CONSTANTID:
1422 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1424 if (data->localpool || fromdata->localpool)
1425 id = repodata_translate_id(data, fromdata, id, 1);
1426 if (key->type == REPOKEY_TYPE_ID)
1427 repodata_set_id(data, handle, keyname, id);
1428 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1429 repodata_set_constantid(data, handle, keyname, id);
1431 repodata_add_idarray(data, handle, keyname, id);
1433 case REPOKEY_TYPE_STR:
1434 repodata_set_str(data, handle, keyname, kv->str);
1436 case REPOKEY_TYPE_VOID:
1437 repodata_set_void(data, handle, keyname);
1439 case REPOKEY_TYPE_NUM:
1440 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1442 case REPOKEY_TYPE_CONSTANT:
1443 repodata_set_constant(data, handle, keyname, kv->num);
1445 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1447 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1448 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1450 case REPOKEY_TYPE_DIRSTRARRAY:
1452 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1453 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1455 case REPOKEY_TYPE_FLEXARRAY:
1458 assert(cbdata->subhandle);
1459 cbdata->handle = cbdata->subhandle;
1460 cbdata->subhandle = 0;
1465 assert(!cbdata->subhandle);
1466 cbdata->subhandle = cbdata->handle;
1468 cbdata->handle = repodata_new_handle(data);
1469 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1478 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1481 Repo *repo = s->repo;
1482 Pool *pool = repo->pool;
1483 Repo *fromrepo = r->repo;
1484 struct solvable_copy_cbdata cbdata;
1486 /* copy solvable data */
1490 s->vendor = r->vendor;
1491 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1492 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1493 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1494 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1495 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1496 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1497 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1498 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1500 /* copy all attributes */
1504 cbdata.handle = s - pool->solvables;
1505 cbdata.subhandle = 0;
1506 cbdata.dircache = dircache;
1507 p = r - fromrepo->pool->solvables;
1509 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1511 FOR_REPODATAS(fromrepo, i, data)
1513 if (p < data->start || p >= data->end)
1515 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1520 /* used to sort entries by package name that got returned in some database order */
1522 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1524 struct rpmdbentry const *a = va, *b = vb;
1525 char *namedata = dp;
1527 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1530 return a->rpmdbid - b->rpmdbid;
1534 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1537 Pool *pool = repo->pool;
1538 Solvable *a = pool->solvables + *(Id *)va;
1539 Solvable *b = pool->solvables + *(Id *)vb;
1542 if (a->name != b->name)
1543 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1544 rpmdbid = repo->rpmdbid;
1545 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1549 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1551 Pool *pool = repo->pool;
1554 tmp = pool->solvables[pa];
1555 pool->solvables[pa] = pool->solvables[pb];
1556 pool->solvables[pb] = tmp;
1559 Id tmpid = repo->rpmdbid[pa - repo->start];
1560 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1561 repo->rpmdbid[pb - repo->start] = tmpid;
1563 /* only works if nothing is already internalized! */
1565 repodata_swap_attrs(data, pa, pb);
1569 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1571 memset(cookie, 0, 32);
1572 cookie[3] = RPMDB_COOKIE_VERSION;
1573 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1574 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1578 * read rpm db as repo
1583 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1585 Pool *pool = repo->pool;
1586 char dbpath[PATH_MAX];
1587 struct stat packagesstat;
1588 unsigned char newcookie[32];
1589 const unsigned char *oldcookie = 0;
1590 Id oldcookietype = 0;
1592 int count = 0, done = 0;
1593 const char *rootdir = 0;
1594 struct rpmdbstate state;
1599 now = solv_timems(0);
1600 memset(&state, 0, sizeof(state));
1603 data = repo_add_repodata(repo, flags);
1605 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1607 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1612 if (flags & REPO_USE_ROOTDIR)
1613 rootdir = pool_get_rootdir(pool);
1614 if (!opendbenv(&state, rootdir))
1617 /* XXX: should get ro lock of Packages database! */
1618 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1619 if (stat(dbpath, &packagesstat))
1621 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1625 mkrpmdbcookie(&packagesstat, newcookie);
1626 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1629 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1630 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1632 int solvstart = 0, solvend = 0;
1636 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1637 repo_empty(ref, 1); /* get it out of the way */
1638 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1639 count = count_headers(pool, rootdir, state.dbenv);
1640 if (!openpkgdb(&state))
1645 if (state.db->cursor(state.db, NULL, &dbc, 0))
1648 return pool_error(pool, -1, "db->cursor failed");
1652 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1662 s = pool_id2solvable(pool, repo_add_solvable(repo));
1664 solvstart = s - pool->solvables;
1665 solvend = s - pool->solvables + 1;
1668 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1669 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1670 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1677 /* We can reuse this solvable, but make sure it's still
1678 associated with this repo. */
1679 memset(s, 0, sizeof(*s));
1682 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1686 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1687 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1693 /* oops, could not reuse. free it instead */
1694 repo_free_solvable(repo, s - pool->solvables, 1);
1698 /* now sort all solvables in the new solvstart..solvend block */
1699 if (solvend - solvstart > 1)
1701 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1702 for (i = solvstart; i < solvend; i++)
1703 pkgids[i - solvstart] = i;
1704 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1706 for (i = solvstart; i < solvend; i++)
1708 int j = pkgids[i - solvstart];
1710 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1712 swap_solvables(repo, data, i, j);
1719 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1720 struct rpmdbentry *entries = 0, *rp;
1723 unsigned int refmask, h;
1727 memset(dircache, 0, sizeof(dircache));
1729 /* get ids of installed rpms */
1730 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1739 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1741 /* create hash from dbid to ref */
1742 refmask = mkmask(ref->nsolvables);
1743 refhash = solv_calloc(refmask + 1, sizeof(Id));
1744 for (i = 0; i < ref->end - ref->start; i++)
1746 if (!ref->rpmdbid[i])
1748 h = ref->rpmdbid[i] & refmask;
1750 h = (h + 317) & refmask;
1751 refhash[h] = i + 1; /* make it non-zero */
1754 /* count the misses, they will cost us time */
1755 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1757 for (i = 0, rp = entries; i < nentries; i++, rp++)
1761 Id dbid = rp->rpmdbid;
1763 while ((id = refhash[h]))
1765 if (ref->rpmdbid[id - 1] == dbid)
1767 h = (h + 317) & refmask;
1776 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1777 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1779 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1781 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1783 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1785 Id dbid = rp->rpmdbid;
1786 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1790 while ((id = refhash[h]))
1792 if (ref->rpmdbid[id - 1] == dbid)
1794 h = (h + 317) & refmask;
1798 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1801 solvable_copy(s, r, data, dircache);
1806 res = getrpmdbid(&state, dbid);
1810 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1813 solv_free(namedata);
1817 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1818 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1822 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1823 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1828 solv_free(namedata);
1830 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1835 if (!(flags & REPO_NO_INTERNALIZE))
1836 repodata_internalize(data);
1837 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1838 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1839 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1840 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1841 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)));
1846 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1852 return repo_add_rpmdb(repo, 0, flags);
1853 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1854 if (repo_add_solv(ref, fp, 0) != 0)
1859 if (ref && ref->start == ref->end)
1865 repo_disable_paging(ref);
1866 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1872 static inline unsigned int
1873 getu32(const unsigned char *dp)
1875 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1880 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1882 unsigned int sigdsize, sigcnt, l;
1883 Pool *pool = repo->pool;
1885 RpmHead *rpmhead = 0;
1886 int rpmheadsize = 0;
1887 char *payloadformat;
1889 unsigned char lead[4096];
1890 int headerstart, headerend;
1893 unsigned char pkgid[16];
1894 unsigned char leadsigid[16];
1895 unsigned char hdrid[32];
1896 int pkgidtype, leadsigidtype, hdridtype;
1899 void *leadsigchksumh = 0;
1900 int forcebinary = 0;
1902 data = repo_add_repodata(repo, flags);
1904 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1905 chksumtype = REPOKEY_TYPE_SHA256;
1906 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1907 chksumtype = REPOKEY_TYPE_SHA1;
1909 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1911 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1914 if (fstat(fileno(fp), &stb))
1916 pool_error(pool, -1, "fstat: %s", strerror(errno));
1921 chksumh = solv_chksum_create(chksumtype);
1922 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1923 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1924 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1926 pool_error(pool, -1, "%s: not a rpm", rpm);
1930 forcebinary = lead[6] != 0 || lead[7] != 1;
1932 solv_chksum_add(chksumh, lead, 96 + 16);
1934 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1935 if (lead[78] != 0 || lead[79] != 5)
1937 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1941 if (getu32(lead + 96) != 0x8eade801)
1943 pool_error(pool, -1, "%s: bad signature header", rpm);
1947 sigcnt = getu32(lead + 96 + 8);
1948 sigdsize = getu32(lead + 96 + 12);
1949 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1951 pool_error(pool, -1, "%s: bad signature header", rpm);
1955 sigdsize += sigcnt * 16;
1956 sigdsize = (sigdsize + 7) & ~7;
1957 headerstart = 96 + 16 + sigdsize;
1958 pkgidtype = leadsigidtype = hdridtype = 0;
1959 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1961 /* extract pkgid or hdrid from the signature header */
1962 if (sigdsize > rpmheadsize)
1964 rpmheadsize = sigdsize + 128;
1965 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1967 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1969 pool_error(pool, -1, "%s: unexpected EOF", rpm);
1974 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1976 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1977 rpmhead->forcebinary = 0;
1978 rpmhead->cnt = sigcnt;
1979 rpmhead->dcnt = sigdsize - sigcnt * 16;
1980 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1981 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1983 unsigned char *chksum;
1984 unsigned int chksumsize;
1985 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1986 if (chksum && chksumsize == 16)
1988 pkgidtype = REPOKEY_TYPE_MD5;
1989 memcpy(pkgid, chksum, 16);
1992 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1994 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
1995 if (str && strlen(str) == 40)
1997 if (solv_hex2bin(&str, hdrid, 20) == 20)
1998 hdridtype = REPOKEY_TYPE_SHA1;
2000 else if (str && strlen(str) == 64)
2002 if (solv_hex2bin(&str, hdrid, 32) == 32)
2003 hdridtype = REPOKEY_TYPE_SHA256;
2009 /* just skip the signature header */
2012 l = sigdsize > 4096 ? 4096 : sigdsize;
2013 if (fread(lead, l, 1, fp) != 1)
2015 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2020 solv_chksum_add(chksumh, lead, l);
2022 solv_chksum_add(leadsigchksumh, lead, l);
2028 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2029 leadsigidtype = REPOKEY_TYPE_MD5;
2031 if (fread(lead, 16, 1, fp) != 1)
2033 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2038 solv_chksum_add(chksumh, lead, 16);
2039 if (getu32(lead) != 0x8eade801)
2041 pool_error(pool, -1, "%s: bad header", rpm);
2045 sigcnt = getu32(lead + 8);
2046 sigdsize = getu32(lead + 12);
2047 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2049 pool_error(pool, -1, "%s: bad header", rpm);
2053 l = sigdsize + sigcnt * 16;
2054 headerend = headerstart + 16 + l;
2055 if (l > rpmheadsize)
2057 rpmheadsize = l + 128;
2058 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2060 if (fread(rpmhead->data, l, 1, fp) != 1)
2062 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2067 solv_chksum_add(chksumh, rpmhead->data, l);
2068 rpmhead->forcebinary = forcebinary;
2069 rpmhead->cnt = sigcnt;
2070 rpmhead->dcnt = sigdsize;
2071 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2072 if (headexists(rpmhead, TAG_PATCHESNAME))
2074 /* this is a patch rpm, ignore */
2075 pool_error(pool, -1, "%s: is patch rpm", rpm);
2077 solv_chksum_free(chksumh, 0);
2081 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2082 if (payloadformat && !strcmp(payloadformat, "drpm"))
2084 /* this is a delta rpm */
2085 pool_error(pool, -1, "%s: is delta rpm", rpm);
2087 solv_chksum_free(chksumh, 0);
2092 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2093 solv_chksum_add(chksumh, lead, l);
2095 s = pool_id2solvable(pool, repo_add_solvable(repo));
2096 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2098 repo_free_solvable(repo, s - pool->solvables, 1);
2099 solv_chksum_free(chksumh, 0);
2103 if (!(flags & REPO_NO_LOCATION))
2104 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2105 if (S_ISREG(stb.st_mode))
2106 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2107 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2109 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2111 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2113 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2116 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2117 chksumh = solv_chksum_free(chksumh, 0);
2121 if (!(flags & REPO_NO_INTERNALIZE))
2122 repodata_internalize(data);
2123 return s - pool->solvables;
2127 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2129 Pool *pool = repo->pool;
2131 RpmHead *rpmhead = rpmhandle;
2133 char *payloadformat;
2135 data = repo_add_repodata(repo, flags);
2136 if (headexists(rpmhead, TAG_PATCHESNAME))
2138 pool_error(pool, -1, "is a patch rpm");
2141 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2142 if (payloadformat && !strcmp(payloadformat, "drpm"))
2144 /* this is a delta rpm */
2145 pool_error(pool, -1, "is a delta rpm");
2148 s = pool_id2solvable(pool, repo_add_solvable(repo));
2149 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2151 repo_free_solvable(repo, s - pool->solvables, 1);
2154 if (!(flags & REPO_NO_INTERNALIZE))
2155 repodata_internalize(data);
2156 return s - pool->solvables;
2160 linkhash(const char *lt, char *hash)
2163 const unsigned char *str = (const unsigned char *)lt;
2167 while ((c = *str++) != 0)
2169 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2173 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2175 RpmHead *rpmhead = rpmhandle;
2180 unsigned int *di, diidx;
2181 unsigned int *co = 0;
2182 unsigned int *ff = 0;
2183 unsigned int lastdir;
2186 int cnt, dcnt, cnt2;
2191 struct filelistinfo info;
2193 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2196 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2198 for (i = 0; i < dcnt; i++)
2199 (*cb)(cbdata, dn[i], 0);
2203 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2209 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2210 if (!di || cnt != cnt2)
2217 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2218 if (!fm || cnt != cnt2)
2226 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2228 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2229 if (!md || cnt != cnt2)
2239 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2241 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2242 if (!co || cnt != cnt2)
2253 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2255 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2256 if (!ff || cnt != cnt2)
2270 memset(&info, 0, sizeof(info));
2271 for (i = 0; i < cnt; i++)
2273 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2278 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2279 l = l1 + strlen(bn[i]) + 1;
2283 space = solv_realloc(space, spacen);
2285 if (lastdir != diidx)
2287 strcpy(space, dn[diidx]);
2291 strcpy(space + l1, bn[i]);
2292 info.diridx = diidx;
2298 info.digest = md[i];
2299 if (fm && S_ISLNK(fm[i]))
2304 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2310 linkhash(lt[i], md5);
2316 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2322 (*cb)(cbdata, space, &info);
2336 rpm_query(void *rpmhandle, Id what)
2338 const char *name, *arch, *sourcerpm;
2342 RpmHead *rpmhead = rpmhandle;
2347 name = headstring(rpmhead, TAG_NAME);
2350 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2351 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2352 arch = headstring(rpmhead, TAG_ARCH);
2355 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2362 evr = headtoevr(rpmhead);
2363 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2365 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2369 name = headstring(rpmhead, TAG_NAME);
2370 r = solv_strdup(name);
2372 case SOLVABLE_SUMMARY:
2373 name = headstring(rpmhead, TAG_SUMMARY);
2374 r = solv_strdup(name);
2376 case SOLVABLE_DESCRIPTION:
2377 name = headstring(rpmhead, TAG_DESCRIPTION);
2378 r = solv_strdup(name);
2381 r = headtoevr(rpmhead);
2388 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2390 RpmHead *rpmhead = rpmhandle;
2395 case SOLVABLE_INSTALLTIME:
2396 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2397 return u32 ? u32 : notfound;
2403 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2405 struct rpmdbentry *entries;
2408 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2411 queue_empty(rpmdbidq);
2412 for (i = 0; i < nentries; i++)
2413 queue_push(rpmdbidq, entries[i].rpmdbid);
2420 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2422 struct rpmdbstate *state = rpmstate;
2425 r = getrpmdbid(state, rpmdbid);
2427 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2428 return r <= 0 ? 0 : state->rpmhead;
2432 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2434 struct rpmdbstate *state = rpmstate;
2435 /* int headerstart, headerend; */
2437 unsigned int sigdsize, sigcnt, l;
2438 unsigned char lead[4096];
2439 int forcebinary = 0;
2441 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2443 pool_error(state->pool, 0, "%s: not a rpm", name);
2446 forcebinary = lead[6] != 0 || lead[7] != 1;
2447 if (lead[78] != 0 || lead[79] != 5)
2449 pool_error(state->pool, 0, "%s: not a V5 header", name);
2452 if (getu32(lead + 96) != 0x8eade801)
2454 pool_error(state->pool, 0, "%s: bad signature header", name);
2457 sigcnt = getu32(lead + 96 + 8);
2458 sigdsize = getu32(lead + 96 + 12);
2459 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2461 pool_error(state->pool, 0, "%s: bad signature header", name);
2464 sigdsize += sigcnt * 16;
2465 sigdsize = (sigdsize + 7) & ~7;
2466 /* headerstart = 96 + 16 + sigdsize; */
2469 l = sigdsize > 4096 ? 4096 : sigdsize;
2470 if (fread(lead, l, 1, fp) != 1)
2472 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2477 if (fread(lead, 16, 1, fp) != 1)
2479 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2482 if (getu32(lead) != 0x8eade801)
2484 pool_error(state->pool, 0, "%s: bad header", name);
2487 sigcnt = getu32(lead + 8);
2488 sigdsize = getu32(lead + 12);
2489 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2491 pool_error(state->pool, 0, "%s: bad header", name);
2494 l = sigdsize + sigcnt * 16;
2495 /* headerend = headerstart + 16 + l; */
2496 if (l > state->rpmheadsize)
2498 state->rpmheadsize = l + 128;
2499 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2501 rpmhead = state->rpmhead;
2502 if (fread(rpmhead->data, l, 1, fp) != 1)
2504 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2507 rpmhead->forcebinary = forcebinary;
2508 rpmhead->cnt = sigcnt;
2509 rpmhead->dcnt = sigdsize;
2510 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2514 #ifdef ENABLE_RPMDB_BYRPMHEADER
2517 rpm_byrpmh(void *rpmstate, Header h)
2519 struct rpmdbstate *state = rpmstate;
2520 const unsigned char *uh;
2521 unsigned int sigdsize, sigcnt, l;
2525 uh = headerUnload(h);
2527 uh = headerUnload(h, NULL);
2531 sigcnt = getu32(uh);
2532 sigdsize = getu32(uh + 4);
2533 l = sigdsize + sigcnt * 16;
2534 if (l > state->rpmheadsize)
2536 state->rpmheadsize = l + 128;
2537 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2539 rpmhead = state->rpmhead;
2540 memcpy(rpmhead->data, uh + 8, l - 8);
2542 rpmhead->forcebinary = 0;
2543 rpmhead->cnt = sigcnt;
2544 rpmhead->dcnt = sigdsize;
2545 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;