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 const unsigned char *cp;
386 unsigned char *buf = 0, *bp;
388 /* check if it's already utf8, code taken from screen ;-) */
389 cp = (const unsigned char *)str;
390 while ((c = *cp++) != 0)
394 if ((c & 0xc0) != 0x80)
395 break; /* encoding error */
396 c = (c & 0x3f) | (state << 6);
397 if (!(state & 0x40000000))
399 /* check for overlong sequences */
400 if ((c & 0x820823e0) == 0x80000000)
402 else if ((c & 0x020821f0) == 0x02000000)
404 else if ((c & 0x000820f8) == 0x00080000)
406 else if ((c & 0x0000207c) == 0x00002000)
416 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
418 c = (c & 0x03) | 0xbfffff00; /* 4 */
420 c = (c & 0x07) | 0xbfffc000; /* 3 */
422 c = (c & 0x0f) | 0xbff00000; /* 2 */
424 c = (c & 0x1f) | 0xfc000000; /* 1 */
428 state = (c & 0x80000000) ? c : 0;
432 /* not utf8, assume latin1 */
433 buf = solv_malloc(2 * strlen(str) + 1);
434 cp = (const unsigned char *)str;
437 while ((c = *cp++) != 0)
450 repodata_set_str(repodata, handle, tag, str);
456 #define MAKEDEPS_FILTER_WEAK (1 << 0)
457 #define MAKEDEPS_FILTER_STRONG (1 << 1)
458 #define MAKEDEPS_NO_RPMLIB (1 << 2)
461 * strong: 0: ignore strongness
462 * 1: filter to strong
466 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
470 int i, cc, nc, vc, fc;
472 unsigned int olddeps;
476 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
477 n = headstringarray(rpmhead, tagn, &nc);
481 v = headstringarray(rpmhead, tagv, &vc);
482 f = headint32array(rpmhead, tagf, &fc);
483 if (!v || !f || nc != vc || nc != fc)
485 char *pkgname = rpm_query(rpmhead, 0);
486 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
495 haspre = 0; /* add no prereq marker */
498 /* we do filtering */
500 for (i = 0; i < nc; i++)
502 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
504 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
505 if (!strncmp(n[i], "rpmlib(", 7))
507 if ((f[i] & DEP_PRE) != 0)
512 else if (tagn == TAG_REQUIRENAME)
514 /* no filtering, just look for the first prereq */
515 for (i = 0; i < nc; i++)
516 if ((f[i] & DEP_PRE) != 0)
530 olddeps = repo_reserve_ids(repo, 0, cc);
531 ida = repo->idarraydata + olddeps;
538 haspre = 2; /* pass two: prereqs */
540 *ida++ = SOLVABLE_PREREQMARKER;
542 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
544 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
546 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
548 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
549 if (!strncmp(n[i], "rpmlib(", 7))
551 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
555 if ((f[i] & DEP_LESS) != 0)
557 if ((f[i] & DEP_EQUAL) != 0)
559 if ((f[i] & DEP_GREATER) != 0)
561 name = pool_str2id(pool, n[i], 1);
562 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
563 evr = pool_str2id(pool, v[i] + 2, 1);
565 evr = pool_str2id(pool, v[i], 1);
566 *ida++ = pool_rel2id(pool, name, evr, flags, 1);
569 *ida++ = pool_str2id(pool, n[i], 1);
572 repo->idarraysize += cc + 1;
581 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
585 unsigned int *fkb, *fn, *fsz, *fm, *fino;
586 unsigned long long *fsz64;
587 unsigned int inotest[256], inotestok;
591 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
593 /* convert to kbyte */
594 fsz = solv_malloc2(fszc, sizeof(*fsz));
595 for (i = 0; i < fszc; i++)
596 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
599 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
601 /* convert to kbyte */
602 for (i = 0; i < fszc; i++)
604 fsz[i] = fsz[i] / 1024 + 1;
614 /* stupid rpm records sizes of directories, so we have to check the mode */
615 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
616 if (!fm || fc != fszc)
622 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
623 if (!fino || fc != fszc)
631 /* kill hardlinked entries */
633 if (fc < sizeof(inotest))
635 /* quick test just hashing the inode numbers */
636 memset(inotest, 0, sizeof(inotest));
637 for (i = 0; i < fc; i++)
640 if (fsz[i] == 0 || !S_ISREG(fm[i]))
641 continue; /* does not matter */
642 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
643 bit = 1 << (fino[i] & 31);
644 if ((inotest[off] & bit) != 0)
649 inotestok = 1; /* no conflict found */
653 /* hardlinked files are possible, check ino/dev pairs */
654 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
656 unsigned int mask, hash, hh;
657 if (!fdev || fc != fszc)
666 while ((mask & (mask - 1)) != 0)
667 mask = mask & (mask - 1);
669 if (mask > sizeof(inotest)/sizeof(*inotest))
670 fx = solv_calloc(mask, sizeof(unsigned int));
674 memset(fx, 0, mask * sizeof(unsigned int));
677 for (i = 0; i < fc; i++)
679 if (fsz[i] == 0 || !S_ISREG(fm[i]))
681 hash = (fino[i] + fdev[i] * 31) & mask;
683 while ((j = fx[hash]) != 0)
685 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
687 fsz[i] = 0; /* kill entry */
690 hash = (hash + hh++) & mask;
701 /* sum up inode count and kbytes for each directory */
702 fn = solv_calloc(dc, sizeof(unsigned int));
703 fkb = solv_calloc(dc, sizeof(unsigned int));
704 for (i = 0; i < fc; i++)
709 if (fsz[i] == 0 || !S_ISREG(fm[i]))
711 fkb[di[i]] += fsz[i];
716 for (i = 0; i < dc; i++)
722 Solvable *s = data->repo->pool->solvables + handle;
723 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
724 did = repodata_str2dir(data, "/usr/src", 1);
726 continue; /* work around rpm bug */
729 did = repodata_str2dir(data, dn[i], 1);
730 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
737 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
749 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
752 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
758 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
767 pool_error(data->repo->pool, 0, "bad filelist");
771 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
773 for (i = 0; i < bnc; i++)
778 if (di[i] == lastdii)
782 did = repodata_str2dir(data, dn[di[i]], 1);
784 did = repodata_str2dir(data, "/", 1);
788 if (b && *b == '/') /* work around rpm bug */
790 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
798 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
803 int i, cnc, cxc, ctc;
806 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
807 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
808 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
809 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
817 for (i = 0; i < ctc; i++)
819 Id h = repodata_new_handle(data);
821 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
823 repodata_set_str(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
825 repodata_set_str(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
828 for (i = 0; i < hq.count; i++)
829 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
838 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
844 name = headstring(rpmhead, TAG_NAME);
847 pool_error(pool, 0, "package has no name");
850 if (!strcmp(name, "gpg-pubkey"))
852 s->name = pool_str2id(pool, name, 1);
853 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
854 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
855 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
858 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
859 s->arch = ARCH_NOSRC;
864 s->arch = ARCH_NOARCH;
865 evr = headtoevr(rpmhead);
866 s->evr = pool_str2id(pool, evr, 1);
867 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
869 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
870 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
871 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
872 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
873 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
874 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
876 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
877 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
878 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
879 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
880 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
881 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
888 unsigned long long u64;
890 handle = s - pool->solvables;
891 str = headstring(rpmhead, TAG_SUMMARY);
893 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
894 str = headstring(rpmhead, TAG_DESCRIPTION);
898 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
899 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
903 /* oh my, found SUSE special author section */
905 str = solv_strdup(str);
908 while (l > 0 && str[l - 1] == '\n')
911 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
913 aut = str; /* copy over */
914 while (*p == ' ' || *p == '\n')
927 while (aut != str && aut[-1] == '\n')
931 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
935 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
937 str = headstring(rpmhead, TAG_GROUP);
939 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
940 str = headstring(rpmhead, TAG_LICENSE);
942 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
943 str = headstring(rpmhead, TAG_URL);
945 repodata_set_str(data, handle, SOLVABLE_URL, str);
946 str = headstring(rpmhead, TAG_DISTRIBUTION);
948 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
949 str = headstring(rpmhead, TAG_PACKAGER);
951 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
952 if ((flags & RPM_ADD_WITH_PKGID) != 0)
954 unsigned char *chksum;
955 unsigned int chksumsize;
956 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
957 if (chksum && chksumsize == 16)
958 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
960 if ((flags & RPM_ADD_WITH_HDRID) != 0)
962 str = headstring(rpmhead, TAG_SHA1HEADER);
963 if (str && strlen(str) == 40)
964 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
965 else if (str && strlen(str) == 64)
966 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
968 u32 = headint32(rpmhead, TAG_BUILDTIME);
970 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
971 u32 = headint32(rpmhead, TAG_INSTALLTIME);
973 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
974 u64 = headint64(rpmhead, TAG_LONGSIZE);
976 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
979 u32 = headint32(rpmhead, TAG_SIZE);
981 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
984 repodata_set_sourcepkg(data, handle, sourcerpm);
985 if ((flags & RPM_ADD_TRIGGERS) != 0)
988 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
991 for (; (id = repo->idarraydata[ida]) != 0; ida++)
993 /* we currently do not support rel ids in incore data, so
994 * strip off versioning information */
997 Reldep *rd = GETRELDEP(pool, id);
1002 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1006 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1007 addfilelist(data, handle, rpmhead);
1008 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1009 addchangelog(data, handle, rpmhead);
1016 /******************************************************************/
1017 /* Rpm Database stuff
1024 RpmHead *rpmhead; /* header storage space */
1028 DB_ENV *dbenv; /* database environment */
1029 DB *db; /* packages database */
1030 int byteswapped; /* endianess of packages database */
1038 #define ENTRIES_BLOCK 255
1039 #define NAMEDATA_BLOCK 1023
1042 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
1045 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1047 # if defined(WORDS_BIGENDIAN)
1052 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
1054 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
1058 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1061 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1063 # if defined(WORDS_BIGENDIAN)
1068 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1070 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1074 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1076 opendbenv(struct rpmdbstate *state, const char *rootdir)
1078 char dbpath[PATH_MAX];
1082 if (db_env_create(&dbenv, 0))
1083 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1084 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1085 dbenv->set_thread_count(dbenv, 8);
1087 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1088 if (access(dbpath, W_OK) == -1)
1090 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1095 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1097 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1102 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1103 dbenv->close(dbenv, 0);
1106 state->dbenv = dbenv;
1111 openpkgdb(struct rpmdbstate *state)
1113 if (state->dbopened)
1114 return state->dbopened > 0 ? 1 : 0;
1115 state->dbopened = -1;
1116 if (!state->dbenv && !opendbenv(state, state->rootdir))
1118 if (db_create(&state->db, state->dbenv, 0))
1120 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1122 state->dbenv->close(state->dbenv, 0);
1126 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1128 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1129 state->db->close(state->db, 0);
1131 state->dbenv->close(state->dbenv, 0);
1135 if (state->db->get_byteswapped(state->db, &state->byteswapped))
1137 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1138 state->db->close(state->db, 0);
1140 state->dbenv->close(state->dbenv, 0);
1144 state->dbopened = 1;
1148 /* get the rpmdbids of all installed packages from the Name index database.
1149 * This is much faster then querying the big Packages database */
1150 static struct rpmdbentry *
1151 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1165 struct rpmdbentry *entries = 0;
1172 if (!state->dbenv && !opendbenv(state, state->rootdir))
1174 dbenv = state->dbenv;
1175 if (db_create(&db, dbenv, 0))
1177 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1180 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1182 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1186 if (db->get_byteswapped(db, &byteswapped))
1188 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1192 if (db->cursor(db, NULL, &dbc, 0))
1194 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1198 memset(&dbkey, 0, sizeof(dbkey));
1199 memset(&dbdata, 0, sizeof(dbdata));
1202 dbkey.data = (void *)match;
1203 dbkey.size = strlen(match);
1205 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1207 if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1211 nameoff = namedatal;
1214 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1215 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1216 namedata[namedatal + dbkey.size] = 0;
1217 namedatal += dbkey.size + 1;
1219 while(dl >= RPM_INDEX_SIZE)
1221 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1222 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1223 entries[nentries].nameoff = nameoff;
1225 dp += RPM_INDEX_SIZE;
1226 dl -= RPM_INDEX_SIZE;
1233 /* make sure that enteries is != 0 if there was no error */
1235 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1236 *nentriesp = nentries;
1238 *namedatap = namedata;
1242 /* retrive header by rpmdbid */
1244 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1246 unsigned char buf[16];
1253 pool_error(state->pool, 0, "illegal rpmdbid");
1256 if (state->dbopened != 1 && !openpkgdb(state))
1258 rpmdbid2db(buf, rpmdbid, state->byteswapped);
1259 memset(&dbkey, 0, sizeof(dbkey));
1260 memset(&dbdata, 0, sizeof(dbdata));
1265 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1267 if (dbdata.size < 8)
1269 pool_error(state->pool, 0, "corrupt rpm database (size)");
1272 if (dbdata.size > state->rpmheadsize)
1274 state->rpmheadsize = dbdata.size + 128;
1275 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1277 rpmhead = state->rpmhead;
1278 memcpy(buf, dbdata.data, 8);
1279 rpmhead->forcebinary = 1;
1280 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1281 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1282 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1284 pool_error(state->pool, 0, "corrupt rpm database (data size)");
1287 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1288 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1292 /* retrive header by berkeleydb cursor */
1294 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1296 unsigned char buf[16];
1302 memset(&dbkey, 0, sizeof(dbkey));
1303 memset(&dbdata, 0, sizeof(dbdata));
1304 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1306 if (dbkey.size != 4)
1307 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1308 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1309 if (dbid == 0) /* the join key */
1311 if (dbdata.size < 8)
1312 return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1313 if (dbdata.size > state->rpmheadsize)
1315 state->rpmheadsize = dbdata.size + 128;
1316 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1318 rpmhead = state->rpmhead;
1319 memcpy(buf, dbdata.data, 8);
1320 rpmhead->forcebinary = 1;
1321 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1322 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1323 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1324 return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1325 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1326 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1333 freestate(struct rpmdbstate *state)
1339 solv_free(state->rootdir);
1341 state->db->close(state->db, 0);
1343 state->dbenv->close(state->dbenv, 0);
1344 solv_free(state->rpmhead);
1348 rpm_state_create(Pool *pool, const char *rootdir)
1350 struct rpmdbstate *state;
1351 state = solv_calloc(1, sizeof(*state));
1354 state->rootdir = solv_strdup(rootdir);
1359 rpm_state_free(void *state)
1362 return solv_free(state);
1366 count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
1368 char dbpath[PATH_MAX];
1369 struct stat statbuf;
1376 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1377 if (stat(dbpath, &statbuf))
1379 memset(&dbkey, 0, sizeof(dbkey));
1380 memset(&dbdata, 0, sizeof(dbdata));
1381 if (db_create(&db, dbenv, 0))
1383 pool_error(pool, 0, "db_create: %s", strerror(errno));
1386 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1388 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1392 if (db->cursor(db, NULL, &dbc, 0))
1395 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1398 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1399 count += dbdata.size / RPM_INDEX_SIZE;
1405 /******************************************************************/
1408 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1416 from = fromrepo->idarraydata + fromoff;
1417 for (ida = from, cc = 0; *ida; ida++, cc++)
1421 ido = repo_reserve_ids(repo, 0, cc);
1422 ida = repo->idarraydata + ido;
1423 memcpy(ida, from, (cc + 1) * sizeof(Id));
1424 repo->idarraysize += cc + 1;
1428 #define COPYDIR_DIRCACHE_SIZE 512
1430 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1433 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1435 if (cache && cache[did & 255] == did)
1436 return cache[(did & 255) + 256];
1437 return copydir_complex(pool, data, fromdata, did, cache);
1441 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1443 Id parent = dirpool_parent(&fromdata->dirpool, did);
1444 Id compid = dirpool_compid(&fromdata->dirpool, did);
1446 parent = copydir(pool, data, fromdata, parent, cache);
1447 if (data->localpool || fromdata->localpool)
1448 compid = repodata_translate_id(data, fromdata, compid, 1);
1449 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1452 cache[did & 255] = did;
1453 cache[(did & 255) + 256] = compid;
1458 struct solvable_copy_cbdata {
1466 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1468 struct solvable_copy_cbdata *cbdata = vcbdata;
1470 Repodata *data = cbdata->data;
1471 Id handle = cbdata->handle;
1472 Pool *pool = data->repo->pool;
1474 keyname = key->name;
1477 case REPOKEY_TYPE_ID:
1478 case REPOKEY_TYPE_CONSTANTID:
1479 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1481 if (data->localpool || fromdata->localpool)
1482 id = repodata_translate_id(data, fromdata, id, 1);
1483 if (key->type == REPOKEY_TYPE_ID)
1484 repodata_set_id(data, handle, keyname, id);
1485 else if (key->type == REPOKEY_TYPE_CONSTANTID)
1486 repodata_set_constantid(data, handle, keyname, id);
1488 repodata_add_idarray(data, handle, keyname, id);
1490 case REPOKEY_TYPE_STR:
1491 repodata_set_str(data, handle, keyname, kv->str);
1493 case REPOKEY_TYPE_VOID:
1494 repodata_set_void(data, handle, keyname);
1496 case REPOKEY_TYPE_NUM:
1497 repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1499 case REPOKEY_TYPE_CONSTANT:
1500 repodata_set_constant(data, handle, keyname, kv->num);
1502 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1504 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1505 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1507 case REPOKEY_TYPE_DIRSTRARRAY:
1509 id = copydir(pool, data, fromdata, id, cbdata->dircache);
1510 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1512 case REPOKEY_TYPE_FLEXARRAY:
1515 assert(cbdata->subhandle);
1516 cbdata->handle = cbdata->subhandle;
1517 cbdata->subhandle = 0;
1522 assert(!cbdata->subhandle);
1523 cbdata->subhandle = cbdata->handle;
1525 cbdata->handle = repodata_new_handle(data);
1526 repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1535 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1538 Repo *repo = s->repo;
1539 Pool *pool = repo->pool;
1540 Repo *fromrepo = r->repo;
1541 struct solvable_copy_cbdata cbdata;
1543 /* copy solvable data */
1547 s->vendor = r->vendor;
1548 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1549 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1550 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1551 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1552 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1553 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1554 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1555 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1557 /* copy all attributes */
1561 cbdata.handle = s - pool->solvables;
1562 cbdata.subhandle = 0;
1563 cbdata.dircache = dircache;
1564 p = r - fromrepo->pool->solvables;
1566 repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1568 FOR_REPODATAS(fromrepo, i, data)
1570 if (p < data->start || p >= data->end)
1572 repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1577 /* used to sort entries by package name that got returned in some database order */
1579 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1581 struct rpmdbentry const *a = va, *b = vb;
1582 char *namedata = dp;
1584 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1587 return a->rpmdbid - b->rpmdbid;
1591 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1594 Pool *pool = repo->pool;
1595 Solvable *a = pool->solvables + *(Id *)va;
1596 Solvable *b = pool->solvables + *(Id *)vb;
1599 if (a->name != b->name)
1600 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1601 rpmdbid = repo->rpmdbid;
1602 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1606 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1608 Pool *pool = repo->pool;
1611 tmp = pool->solvables[pa];
1612 pool->solvables[pa] = pool->solvables[pb];
1613 pool->solvables[pb] = tmp;
1616 Id tmpid = repo->rpmdbid[pa - repo->start];
1617 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1618 repo->rpmdbid[pb - repo->start] = tmpid;
1620 /* only works if nothing is already internalized! */
1622 repodata_swap_attrs(data, pa, pb);
1626 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1628 memset(cookie, 0, 32);
1629 cookie[3] = RPMDB_COOKIE_VERSION;
1630 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1631 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1635 * read rpm db as repo
1640 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1642 Pool *pool = repo->pool;
1643 char dbpath[PATH_MAX];
1644 struct stat packagesstat;
1645 unsigned char newcookie[32];
1646 const unsigned char *oldcookie = 0;
1647 Id oldcookietype = 0;
1649 int count = 0, done = 0;
1650 const char *rootdir = 0;
1651 struct rpmdbstate state;
1656 now = solv_timems(0);
1657 memset(&state, 0, sizeof(state));
1660 data = repo_add_repodata(repo, flags);
1662 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1664 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1669 if (flags & REPO_USE_ROOTDIR)
1670 rootdir = pool_get_rootdir(pool);
1671 if (!opendbenv(&state, rootdir))
1674 /* XXX: should get ro lock of Packages database! */
1675 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1676 if (stat(dbpath, &packagesstat))
1678 pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1682 mkrpmdbcookie(&packagesstat, newcookie);
1683 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1686 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1687 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1689 int solvstart = 0, solvend = 0;
1693 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1694 repo_empty(ref, 1); /* get it out of the way */
1695 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1696 count = count_headers(pool, rootdir, state.dbenv);
1697 if (!openpkgdb(&state))
1702 if (state.db->cursor(state.db, NULL, &dbc, 0))
1705 return pool_error(pool, -1, "db->cursor failed");
1709 while ((dbid = getrpmcursor(&state, dbc)) != 0)
1719 s = pool_id2solvable(pool, repo_add_solvable(repo));
1721 solvstart = s - pool->solvables;
1722 solvend = s - pool->solvables + 1;
1725 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1726 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1727 if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1734 /* We can reuse this solvable, but make sure it's still
1735 associated with this repo. */
1736 memset(s, 0, sizeof(*s));
1739 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1743 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1744 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1750 /* oops, could not reuse. free it instead */
1751 repo_free_solvable(repo, s - pool->solvables, 1);
1755 /* now sort all solvables in the new solvstart..solvend block */
1756 if (solvend - solvstart > 1)
1758 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1759 for (i = solvstart; i < solvend; i++)
1760 pkgids[i - solvstart] = i;
1761 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1763 for (i = solvstart; i < solvend; i++)
1765 int j = pkgids[i - solvstart];
1767 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1769 swap_solvables(repo, data, i, j);
1776 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1777 struct rpmdbentry *entries = 0, *rp;
1780 unsigned int refmask, h;
1784 memset(dircache, 0, sizeof(dircache));
1786 /* get ids of installed rpms */
1787 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1796 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1798 /* create hash from dbid to ref */
1799 refmask = mkmask(ref->nsolvables);
1800 refhash = solv_calloc(refmask + 1, sizeof(Id));
1801 for (i = 0; i < ref->end - ref->start; i++)
1803 if (!ref->rpmdbid[i])
1805 h = ref->rpmdbid[i] & refmask;
1807 h = (h + 317) & refmask;
1808 refhash[h] = i + 1; /* make it non-zero */
1811 /* count the misses, they will cost us time */
1812 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1814 for (i = 0, rp = entries; i < nentries; i++, rp++)
1818 Id dbid = rp->rpmdbid;
1820 while ((id = refhash[h]))
1822 if (ref->rpmdbid[id - 1] == dbid)
1824 h = (h + 317) & refmask;
1833 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1834 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1836 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1838 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1840 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1842 Id dbid = rp->rpmdbid;
1843 repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1847 while ((id = refhash[h]))
1849 if (ref->rpmdbid[id - 1] == dbid)
1851 h = (h + 317) & refmask;
1855 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1858 solvable_copy(s, r, data, dircache);
1863 res = getrpmdbid(&state, dbid);
1867 return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1870 solv_free(namedata);
1874 rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1875 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1879 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1880 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1885 solv_free(namedata);
1887 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1892 if (!(flags & REPO_NO_INTERNALIZE))
1893 repodata_internalize(data);
1894 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1895 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1896 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1897 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1898 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)));
1903 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1909 return repo_add_rpmdb(repo, 0, flags);
1910 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1911 if (repo_add_solv(ref, fp, 0) != 0)
1916 if (ref && ref->start == ref->end)
1922 repo_disable_paging(ref);
1923 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1929 static inline unsigned int
1930 getu32(const unsigned char *dp)
1932 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1937 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1939 unsigned int sigdsize, sigcnt, l;
1940 Pool *pool = repo->pool;
1942 RpmHead *rpmhead = 0;
1943 int rpmheadsize = 0;
1944 char *payloadformat;
1946 unsigned char lead[4096];
1947 int headerstart, headerend;
1950 unsigned char pkgid[16];
1951 unsigned char leadsigid[16];
1952 unsigned char hdrid[32];
1953 int pkgidtype, leadsigidtype, hdridtype;
1956 void *leadsigchksumh = 0;
1957 int forcebinary = 0;
1959 data = repo_add_repodata(repo, flags);
1961 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1962 chksumtype = REPOKEY_TYPE_SHA256;
1963 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1964 chksumtype = REPOKEY_TYPE_SHA1;
1966 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1968 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1971 if (fstat(fileno(fp), &stb))
1973 pool_error(pool, -1, "fstat: %s", strerror(errno));
1978 chksumh = solv_chksum_create(chksumtype);
1979 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1980 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1981 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1983 pool_error(pool, -1, "%s: not a rpm", rpm);
1987 forcebinary = lead[6] != 0 || lead[7] != 1;
1989 solv_chksum_add(chksumh, lead, 96 + 16);
1991 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1992 if (lead[78] != 0 || lead[79] != 5)
1994 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1998 if (getu32(lead + 96) != 0x8eade801)
2000 pool_error(pool, -1, "%s: bad signature header", rpm);
2004 sigcnt = getu32(lead + 96 + 8);
2005 sigdsize = getu32(lead + 96 + 12);
2006 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2008 pool_error(pool, -1, "%s: bad signature header", rpm);
2012 sigdsize += sigcnt * 16;
2013 sigdsize = (sigdsize + 7) & ~7;
2014 headerstart = 96 + 16 + sigdsize;
2015 pkgidtype = leadsigidtype = hdridtype = 0;
2016 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
2018 /* extract pkgid or hdrid from the signature header */
2019 if (sigdsize > rpmheadsize)
2021 rpmheadsize = sigdsize + 128;
2022 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2024 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
2026 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2031 solv_chksum_add(chksumh, rpmhead->data, sigdsize);
2033 solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
2034 rpmhead->forcebinary = 0;
2035 rpmhead->cnt = sigcnt;
2036 rpmhead->dcnt = sigdsize - sigcnt * 16;
2037 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2038 if ((flags & RPM_ADD_WITH_PKGID) != 0)
2040 unsigned char *chksum;
2041 unsigned int chksumsize;
2042 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2043 if (chksum && chksumsize == 16)
2045 pkgidtype = REPOKEY_TYPE_MD5;
2046 memcpy(pkgid, chksum, 16);
2049 if ((flags & RPM_ADD_WITH_HDRID) != 0)
2051 const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2052 if (str && strlen(str) == 40)
2054 if (solv_hex2bin(&str, hdrid, 20) == 20)
2055 hdridtype = REPOKEY_TYPE_SHA1;
2057 else if (str && strlen(str) == 64)
2059 if (solv_hex2bin(&str, hdrid, 32) == 32)
2060 hdridtype = REPOKEY_TYPE_SHA256;
2066 /* just skip the signature header */
2069 l = sigdsize > 4096 ? 4096 : sigdsize;
2070 if (fread(lead, l, 1, fp) != 1)
2072 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2077 solv_chksum_add(chksumh, lead, l);
2079 solv_chksum_add(leadsigchksumh, lead, l);
2085 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2086 leadsigidtype = REPOKEY_TYPE_MD5;
2088 if (fread(lead, 16, 1, fp) != 1)
2090 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2095 solv_chksum_add(chksumh, lead, 16);
2096 if (getu32(lead) != 0x8eade801)
2098 pool_error(pool, -1, "%s: bad header", rpm);
2102 sigcnt = getu32(lead + 8);
2103 sigdsize = getu32(lead + 12);
2104 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2106 pool_error(pool, -1, "%s: bad header", rpm);
2110 l = sigdsize + sigcnt * 16;
2111 headerend = headerstart + 16 + l;
2112 if (l > rpmheadsize)
2114 rpmheadsize = l + 128;
2115 rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2117 if (fread(rpmhead->data, l, 1, fp) != 1)
2119 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2124 solv_chksum_add(chksumh, rpmhead->data, l);
2125 rpmhead->forcebinary = forcebinary;
2126 rpmhead->cnt = sigcnt;
2127 rpmhead->dcnt = sigdsize;
2128 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2129 if (headexists(rpmhead, TAG_PATCHESNAME))
2131 /* this is a patch rpm, ignore */
2132 pool_error(pool, -1, "%s: is patch rpm", rpm);
2134 solv_chksum_free(chksumh, 0);
2138 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2139 if (payloadformat && !strcmp(payloadformat, "drpm"))
2141 /* this is a delta rpm */
2142 pool_error(pool, -1, "%s: is delta rpm", rpm);
2144 solv_chksum_free(chksumh, 0);
2149 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2150 solv_chksum_add(chksumh, lead, l);
2152 s = pool_id2solvable(pool, repo_add_solvable(repo));
2153 if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2155 repo_free_solvable(repo, s - pool->solvables, 1);
2156 solv_chksum_free(chksumh, 0);
2160 if (!(flags & REPO_NO_LOCATION))
2161 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2162 if (S_ISREG(stb.st_mode))
2163 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2164 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2166 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2168 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2170 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2173 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2174 chksumh = solv_chksum_free(chksumh, 0);
2178 if (!(flags & REPO_NO_INTERNALIZE))
2179 repodata_internalize(data);
2180 return s - pool->solvables;
2184 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2186 Pool *pool = repo->pool;
2188 RpmHead *rpmhead = rpmhandle;
2190 char *payloadformat;
2192 data = repo_add_repodata(repo, flags);
2193 if (headexists(rpmhead, TAG_PATCHESNAME))
2195 pool_error(pool, -1, "is a patch rpm");
2198 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2199 if (payloadformat && !strcmp(payloadformat, "drpm"))
2201 /* this is a delta rpm */
2202 pool_error(pool, -1, "is a delta rpm");
2205 s = pool_id2solvable(pool, repo_add_solvable(repo));
2206 if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2208 repo_free_solvable(repo, s - pool->solvables, 1);
2211 if (!(flags & REPO_NO_INTERNALIZE))
2212 repodata_internalize(data);
2213 return s - pool->solvables;
2217 linkhash(const char *lt, char *hash)
2220 const unsigned char *str = (const unsigned char *)lt;
2224 while ((c = *str++) != 0)
2226 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2230 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2232 RpmHead *rpmhead = rpmhandle;
2237 unsigned int *di, diidx;
2238 unsigned int *co = 0;
2239 unsigned int *ff = 0;
2240 unsigned int lastdir;
2243 int cnt, dcnt, cnt2;
2248 struct filelistinfo info;
2250 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2253 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2255 for (i = 0; i < dcnt; i++)
2256 (*cb)(cbdata, dn[i], 0);
2260 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2266 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2267 if (!di || cnt != cnt2)
2274 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2275 if (!fm || cnt != cnt2)
2283 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2285 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2286 if (!md || cnt != cnt2)
2296 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2298 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2299 if (!co || cnt != cnt2)
2310 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2312 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2313 if (!ff || cnt != cnt2)
2327 memset(&info, 0, sizeof(info));
2328 for (i = 0; i < cnt; i++)
2330 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2335 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2336 l = l1 + strlen(bn[i]) + 1;
2340 space = solv_realloc(space, spacen);
2342 if (lastdir != diidx)
2344 strcpy(space, dn[diidx]);
2348 strcpy(space + l1, bn[i]);
2349 info.diridx = diidx;
2355 info.digest = md[i];
2356 if (fm && S_ISLNK(fm[i]))
2361 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2367 linkhash(lt[i], md5);
2373 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2379 (*cb)(cbdata, space, &info);
2393 rpm_query(void *rpmhandle, Id what)
2395 const char *name, *arch, *sourcerpm;
2399 RpmHead *rpmhead = rpmhandle;
2404 name = headstring(rpmhead, TAG_NAME);
2407 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2408 if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2409 arch = headstring(rpmhead, TAG_ARCH);
2412 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2419 evr = headtoevr(rpmhead);
2420 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2422 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2426 name = headstring(rpmhead, TAG_NAME);
2427 r = solv_strdup(name);
2429 case SOLVABLE_SUMMARY:
2430 name = headstring(rpmhead, TAG_SUMMARY);
2431 r = solv_strdup(name);
2433 case SOLVABLE_DESCRIPTION:
2434 name = headstring(rpmhead, TAG_DESCRIPTION);
2435 r = solv_strdup(name);
2438 r = headtoevr(rpmhead);
2445 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2447 RpmHead *rpmhead = rpmhandle;
2452 case SOLVABLE_INSTALLTIME:
2453 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2454 return u32 ? u32 : notfound;
2460 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2462 struct rpmdbentry *entries;
2465 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2468 queue_empty(rpmdbidq);
2469 for (i = 0; i < nentries; i++)
2470 queue_push(rpmdbidq, entries[i].rpmdbid);
2477 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2479 struct rpmdbstate *state = rpmstate;
2482 r = getrpmdbid(state, rpmdbid);
2484 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2485 return r <= 0 ? 0 : state->rpmhead;
2489 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2491 struct rpmdbstate *state = rpmstate;
2492 /* int headerstart, headerend; */
2494 unsigned int sigdsize, sigcnt, l;
2495 unsigned char lead[4096];
2496 int forcebinary = 0;
2498 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2500 pool_error(state->pool, 0, "%s: not a rpm", name);
2503 forcebinary = lead[6] != 0 || lead[7] != 1;
2504 if (lead[78] != 0 || lead[79] != 5)
2506 pool_error(state->pool, 0, "%s: not a V5 header", name);
2509 if (getu32(lead + 96) != 0x8eade801)
2511 pool_error(state->pool, 0, "%s: bad signature header", name);
2514 sigcnt = getu32(lead + 96 + 8);
2515 sigdsize = getu32(lead + 96 + 12);
2516 if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2518 pool_error(state->pool, 0, "%s: bad signature header", name);
2521 sigdsize += sigcnt * 16;
2522 sigdsize = (sigdsize + 7) & ~7;
2523 /* headerstart = 96 + 16 + sigdsize; */
2526 l = sigdsize > 4096 ? 4096 : sigdsize;
2527 if (fread(lead, l, 1, fp) != 1)
2529 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2534 if (fread(lead, 16, 1, fp) != 1)
2536 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2539 if (getu32(lead) != 0x8eade801)
2541 pool_error(state->pool, 0, "%s: bad header", name);
2544 sigcnt = getu32(lead + 8);
2545 sigdsize = getu32(lead + 12);
2546 if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2548 pool_error(state->pool, 0, "%s: bad header", name);
2551 l = sigdsize + sigcnt * 16;
2552 /* headerend = headerstart + 16 + l; */
2553 if (l > state->rpmheadsize)
2555 state->rpmheadsize = l + 128;
2556 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2558 rpmhead = state->rpmhead;
2559 if (fread(rpmhead->data, l, 1, fp) != 1)
2561 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2564 rpmhead->forcebinary = forcebinary;
2565 rpmhead->cnt = sigcnt;
2566 rpmhead->dcnt = sigdsize;
2567 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2571 #ifdef ENABLE_RPMDB_BYRPMHEADER
2574 rpm_byrpmh(void *rpmstate, Header h)
2576 struct rpmdbstate *state = rpmstate;
2577 const unsigned char *uh;
2578 unsigned int sigdsize, sigcnt, l;
2582 uh = headerUnload(h);
2584 uh = headerUnload(h, NULL);
2588 sigcnt = getu32(uh);
2589 sigdsize = getu32(uh + 4);
2590 l = sigdsize + sigcnt * 16;
2591 if (l > state->rpmheadsize)
2593 state->rpmheadsize = l + 128;
2594 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2596 rpmhead = state->rpmhead;
2597 memcpy(rpmhead->data, uh + 8, l - 8);
2599 rpmhead->forcebinary = 0;
2600 rpmhead->cnt = sigcnt;
2601 rpmhead->dcnt = sigdsize;
2602 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;