2 * Copyright (c) 2007, 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>
37 #include "repo_rpmdb.h"
39 #define RPMDB_COOKIE_VERSION 2
42 #define TAG_VERSION 1001
43 #define TAG_RELEASE 1002
44 #define TAG_EPOCH 1003
45 #define TAG_SUMMARY 1004
46 #define TAG_DESCRIPTION 1005
47 #define TAG_BUILDTIME 1006
48 #define TAG_BUILDHOST 1007
49 #define TAG_INSTALLTIME 1008
51 #define TAG_DISTRIBUTION 1010
52 #define TAG_VENDOR 1011
53 #define TAG_LICENSE 1014
54 #define TAG_PACKAGER 1015
55 #define TAG_GROUP 1016
58 #define TAG_FILESIZES 1028
59 #define TAG_FILEMODES 1030
60 #define TAG_FILEMD5S 1035
61 #define TAG_FILELINKTOS 1036
62 #define TAG_SOURCERPM 1044
63 #define TAG_PROVIDENAME 1047
64 #define TAG_REQUIREFLAGS 1048
65 #define TAG_REQUIRENAME 1049
66 #define TAG_REQUIREVERSION 1050
67 #define TAG_NOSOURCE 1051
68 #define TAG_NOPATCH 1052
69 #define TAG_CONFLICTFLAGS 1053
70 #define TAG_CONFLICTNAME 1054
71 #define TAG_CONFLICTVERSION 1055
72 #define TAG_OBSOLETENAME 1090
73 #define TAG_FILEDEVICES 1095
74 #define TAG_FILEINODES 1096
75 #define TAG_PROVIDEFLAGS 1112
76 #define TAG_PROVIDEVERSION 1113
77 #define TAG_OBSOLETEFLAGS 1114
78 #define TAG_OBSOLETEVERSION 1115
79 #define TAG_DIRINDEXES 1116
80 #define TAG_BASENAMES 1117
81 #define TAG_DIRNAMES 1118
82 #define TAG_PAYLOADFORMAT 1124
83 #define TAG_PATCHESNAME 1133
84 #define TAG_FILECOLORS 1140
85 #define TAG_SUGGESTSNAME 1156
86 #define TAG_SUGGESTSVERSION 1157
87 #define TAG_SUGGESTSFLAGS 1158
88 #define TAG_ENHANCESNAME 1159
89 #define TAG_ENHANCESVERSION 1160
90 #define TAG_ENHANCESFLAGS 1161
92 #define SIGTAG_SIZE 1000
93 #define SIGTAG_PGP 1002 /* RSA signature */
94 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
95 #define SIGTAG_GPG 1005 /* DSA signature */
97 #define DEP_LESS (1 << 1)
98 #define DEP_GREATER (1 << 2)
99 #define DEP_EQUAL (1 << 3)
100 #define DEP_STRONG (1 << 27)
101 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
109 typedef struct rpmhead {
113 unsigned char data[1];
117 static inline unsigned char *
118 headfindtag(RpmHead *h, int tag)
121 unsigned char *d, taga[4];
127 for (i = 0; i < h->cnt; i++, d -= 16)
128 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
134 headexists(RpmHead *h, int tag)
136 return headfindtag(h, tag) ? 1 : 0;
139 static unsigned int *
140 headint32array(RpmHead *h, int tag, int *cnt)
142 unsigned int i, o, *r;
143 unsigned char *d = headfindtag(h, tag);
145 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
147 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
148 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
149 if (o + 4 * i > h->dcnt)
152 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
155 for (o = 0; o < i; o++, d += 4)
156 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
160 /* returns the first entry of an integer array */
162 headint32(RpmHead *h, int tag)
165 unsigned char *d = headfindtag(h, tag);
167 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
169 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
170 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
171 if (i == 0 || o + 4 * i > h->dcnt)
174 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
177 static unsigned int *
178 headint16array(RpmHead *h, int tag, int *cnt)
180 unsigned int i, o, *r;
181 unsigned char *d = headfindtag(h, tag);
183 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
185 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
186 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
187 if (o + 4 * i > h->dcnt)
190 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
193 for (o = 0; o < i; o++, d += 2)
194 r[o] = d[0] << 8 | d[1];
199 headstring(RpmHead *h, int tag)
202 unsigned char *d = headfindtag(h, tag);
203 /* 6: STRING, 9: I18NSTRING */
204 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
206 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
209 return (char *)h->dp + o;
213 headstringarray(RpmHead *h, int tag, int *cnt)
216 unsigned char *d = headfindtag(h, tag);
219 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
221 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
222 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
223 r = sat_calloc(i ? i : 1, sizeof(char *));
227 for (o = 0; o < i; o++)
231 d += strlen((char *)d) + 1;
232 if (d >= h->dp + h->dcnt)
241 static unsigned char *
242 headbinary(RpmHead *h, int tag, unsigned int *sizep)
245 unsigned char *d = headfindtag(h, tag);
246 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
248 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
249 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
250 if (o > h->dcnt || o + i < o || o + i > h->dcnt)
257 static char *headtoevr(RpmHead *h)
264 version = headstring(h, TAG_VERSION);
265 release = headstring(h, TAG_RELEASE);
266 epoch = headint32(h, TAG_EPOCH);
267 if (!version || !release)
269 fprintf(stderr, "headtoevr: bad rpm header\n");
272 for (v = version; *v >= 0 && *v <= '9'; v++)
274 if (epoch || (v != version && *v == ':'))
276 char epochbuf[11]; /* 32bit decimal will fit in */
277 sprintf(epochbuf, "%u", epoch);
278 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
279 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
283 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
284 sprintf(evr, "%s-%s", version, release);
291 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
293 const unsigned char *cp;
296 unsigned char *buf = 0, *bp;
298 /* check if it's already utf8, code taken from screen ;-) */
299 cp = (const unsigned char *)str;
300 while ((c = *cp++) != 0)
304 if ((c & 0xc0) != 0x80)
305 break; /* encoding error */
306 c = (c & 0x3f) | (state << 6);
307 if (!(state & 0x40000000))
309 /* check for overlong sequences */
310 if ((c & 0x820823e0) == 0x80000000)
312 else if ((c & 0x020821f0) == 0x02000000)
314 else if ((c & 0x000820f8) == 0x00080000)
316 else if ((c & 0x0000207c) == 0x00002000)
326 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
328 c = (c & 0x03) | 0xbfffff00; /* 4 */
330 c = (c & 0x07) | 0xbfffc000; /* 3 */
332 c = (c & 0x0f) | 0xbff00000; /* 2 */
334 c = (c & 0x1f) | 0xfc000000; /* 1 */
338 state = (c & 0x80000000) ? c : 0;
342 /* not utf8, assume latin1 */
343 buf = sat_malloc(2 * strlen(str) + 1);
344 cp = (const unsigned char *)str;
347 while ((c = *cp++) != 0)
360 repodata_set_str(repodata, handle, tag, str);
366 #define MAKEDEPS_FILTER_WEAK (1 << 0)
367 #define MAKEDEPS_FILTER_STRONG (1 << 1)
368 #define MAKEDEPS_NO_RPMLIB (1 << 2)
371 * strong: 0: ignore strongness
372 * 1: filter to strong
376 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
380 int i, cc, nc, vc, fc;
382 unsigned int olddeps;
386 strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
387 n = headstringarray(rpmhead, tagn, &nc);
390 v = headstringarray(rpmhead, tagv, &vc);
396 f = headint32array(rpmhead, tagf, &fc);
403 if (nc != vc || nc != fc)
405 fprintf(stderr, "bad dependency entries\n");
410 haspre = 0; /* add no prereq marker */
413 /* we do filtering */
415 for (i = 0; i < nc; i++)
417 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
419 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
420 if (!strncmp(n[i], "rpmlib(", 7))
422 if ((f[i] & DEP_PRE) != 0)
427 else if (tagn == TAG_REQUIRENAME)
429 /* no filtering, just look for the first prereq */
430 for (i = 0; i < nc; i++)
431 if ((f[i] & DEP_PRE) != 0)
445 olddeps = repo_reserve_ids(repo, 0, cc);
446 ida = repo->idarraydata + olddeps;
453 haspre = 2; /* pass two: prereqs */
455 *ida++ = SOLVABLE_PREREQMARKER;
457 if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
459 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
461 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
463 if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
464 if (!strncmp(n[i], "rpmlib(", 7))
466 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
470 if ((f[i] & DEP_LESS) != 0)
472 if ((f[i] & DEP_EQUAL) != 0)
474 if ((f[i] & DEP_GREATER) != 0)
476 name = str2id(pool, n[i], 1);
477 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
478 evr = str2id(pool, v[i] + 2, 1);
480 evr = str2id(pool, v[i], 1);
481 *ida++ = rel2id(pool, name, evr, flags, 1);
484 *ida++ = str2id(pool, n[i], 1);
487 repo->idarraysize += cc + 1;
495 #ifdef USE_FILEFILTER
497 #define FILEFILTER_EXACT 0
498 #define FILEFILTER_STARTS 1
499 #define FILEFILTER_CONTAINS 2
507 static struct filefilter filefilters[] = {
508 { FILEFILTER_CONTAINS, "/bin/", 0},
509 { FILEFILTER_CONTAINS, "/sbin/", 0},
510 { FILEFILTER_CONTAINS, "/lib/", 0},
511 { FILEFILTER_CONTAINS, "/lib64/", 0},
512 { FILEFILTER_CONTAINS, "/etc/", 0},
513 { FILEFILTER_STARTS, "/usr/games/", 0},
514 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
515 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
516 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
522 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
526 unsigned int *fkb, *fn, *fsz, *fm, *fino;
527 unsigned int inotest[256], inotestok;
531 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
532 if (!fsz || fc != fszc)
537 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
538 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
539 if (!fm || fc != fszc)
545 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
546 if (!fino || fc != fszc)
554 if (fc < sizeof(inotest))
556 memset(inotest, 0, sizeof(inotest));
557 for (i = 0; i < fc; i++)
560 if (fsz[i] == 0 || !S_ISREG(fm[i]))
562 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
563 bit = 1 << (fino[i] & 31);
564 if ((inotest[off] & bit) != 0)
573 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
575 unsigned int mask, hash, hh;
576 if (!fdev || fc != fszc)
585 while ((mask & (mask - 1)) != 0)
586 mask = mask & (mask - 1);
588 if (mask > sizeof(inotest)/sizeof(*inotest))
589 fx = sat_calloc(mask, sizeof(unsigned int));
593 memset(fx, 0, mask * sizeof(unsigned int));
596 for (i = 0; i < fc; i++)
598 if (fsz[i] == 0 || !S_ISREG(fm[i]))
600 hash = (fino[i] + fdev[i] * 31) & mask;
602 while ((j = fx[hash]) != 0)
604 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
606 fsz[i] = 0; /* kill entry */
609 hash = (hash + hh++) & mask;
619 fn = sat_calloc(dic, sizeof(unsigned int));
620 fkb = sat_calloc(dic, sizeof(unsigned int));
621 for (i = 0; i < fc; i++)
623 if (fsz[i] == 0 || !S_ISREG(fm[i]))
628 fkb[di[i]] += fsz[i] / 1024 + 1;
633 handle = s - pool->solvables;
634 for (i = 0; i < fc; i++)
638 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
639 did = repodata_str2dir(data, "/usr/src", 1);
641 did = repodata_str2dir(data, dn[i], 1);
642 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
648 /* assumes last processed array is provides! */
650 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
657 #ifdef USE_FILEFILTER
659 struct filefilter *ff;
668 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
671 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
677 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
686 fprintf(stderr, "bad filelist\n");
691 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
693 for (i = 0; i < bnc; i++)
695 #ifdef USE_FILEFILTER
697 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
701 switch (ff->dirmatch)
703 case FILEFILTER_STARTS:
704 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
707 case FILEFILTER_CONTAINS:
708 if (!strstr(dn[di[i]], ff->dir))
711 case FILEFILTER_EXACT:
713 if (strcmp(dn[di[i]], ff->dir))
720 if (strcmp(bn[i], ff->base))
725 if (j == sizeof(filefilters)/sizeof(*filefilters))
729 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
733 fn = sat_realloc(fn, fna);
735 strcpy(fn, dn[di[i]]);
737 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
742 handle = s - pool->solvables;
743 did = repodata_str2dir(data, dn[di[i]], 1);
745 did = repodata_str2dir(data, "/", 1);
746 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
760 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
762 const char *p, *sevr, *sarch;
764 p = strrchr(sourcerpm, '.');
765 if (!p || strcmp(p, ".rpm") != 0)
768 while (p > sourcerpm && *p != '.')
770 if (*p != '.' || p == sourcerpm)
773 while (p > sourcerpm && *p != '-')
775 if (*p != '-' || p == sourcerpm)
778 while (p > sourcerpm && *p != '-')
780 if (*p != '-' || p == sourcerpm)
783 if (!strcmp(sarch, "src.rpm"))
784 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
785 else if (!strcmp(sarch, "nosrc.rpm"))
786 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
788 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
789 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
790 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
792 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
793 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
794 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
796 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
800 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
806 name = headstring(rpmhead, TAG_NAME);
807 if (!strcmp(name, "gpg-pubkey"))
809 s->name = str2id(pool, name, 1);
812 fprintf(stderr, "package has no name\n");
815 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
817 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
820 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
821 s->arch = ARCH_NOSRC;
826 s->arch = ARCH_NOARCH;
827 evr = headtoevr(rpmhead);
828 s->evr = str2id(pool, evr, 1);
829 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
831 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
832 if ((flags & RPM_ADD_NO_FILELIST) == 0)
833 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
834 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
835 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
836 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
837 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
838 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
840 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
841 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
842 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
843 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
844 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
845 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
853 handle = s - pool->solvables;
854 str = headstring(rpmhead, TAG_SUMMARY);
856 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
857 str = headstring(rpmhead, TAG_DESCRIPTION);
861 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
862 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
866 /* oh my, found SUSE special author section */
871 while (l > 0 && str[l - 1] == '\n')
874 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
876 aut = str; /* copy over */
877 while (*p == ' ' || *p == '\n')
890 while (aut != str && aut[-1] == '\n')
894 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
898 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
900 str = headstring(rpmhead, TAG_GROUP);
902 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
903 str = headstring(rpmhead, TAG_LICENSE);
905 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
906 str = headstring(rpmhead, TAG_URL);
908 repodata_set_str(data, handle, SOLVABLE_URL, str);
909 str = headstring(rpmhead, TAG_DISTRIBUTION);
911 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
912 str = headstring(rpmhead, TAG_PACKAGER);
914 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
915 u32 = headint32(rpmhead, TAG_BUILDTIME);
917 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
918 u32 = headint32(rpmhead, TAG_INSTALLTIME);
920 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
921 u32 = headint32(rpmhead, TAG_SIZE);
923 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
925 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
932 copyreldep(Pool *pool, Pool *frompool, Id id)
934 Reldep *rd = GETRELDEP(frompool, id);
935 Id name = rd->name, evr = rd->evr;
937 name = copyreldep(pool, frompool, name);
939 name = str2id(pool, id2str(frompool, name), 1);
941 evr = copyreldep(pool, frompool, evr);
943 evr = str2id(pool, id2str(frompool, evr), 1);
944 return rel2id(pool, name, evr, rd->flags, 1);
948 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
953 Pool *frompool = fromrepo->pool;
957 from = fromrepo->idarraydata + fromoff;
958 for (ida = from, cc = 0; *ida; ida++, cc++)
962 ido = repo_reserve_ids(repo, 0, cc);
963 ida = repo->idarraydata + ido;
964 if (frompool && pool != frompool)
970 id = copyreldep(pool, frompool, id);
972 id = str2id(pool, id2str(frompool, id), 1);
978 memcpy(ida, from, (cc + 1) * sizeof(Id));
979 repo->idarraysize += cc + 1;
983 #define COPYDIR_DIRCACHE_SIZE 512
985 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
988 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
990 if (cache && cache[did & 255] == did)
991 return cache[(did & 255) + 256];
992 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
996 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
998 Id parent = dirpool_parent(&fromdata->dirpool, did);
999 Id compid = dirpool_compid(&fromdata->dirpool, did);
1001 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1002 if (fromspool != &pool->ss)
1003 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1004 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1007 cache[did & 255] = did;
1008 cache[(did & 255) + 256] = compid;
1013 struct solvable_copy_cbdata {
1020 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1022 struct solvable_copy_cbdata *cbdata = vcbdata;
1024 Repodata *data = cbdata->data;
1025 Id handle = cbdata->handle;
1026 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1027 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1029 keyname = key->name;
1030 if (keyname >= ID_NUM_INTERNAL)
1031 keyname = str2id(pool, id2str(frompool, keyname), 1);
1034 case REPOKEY_TYPE_ID:
1035 case REPOKEY_TYPE_CONSTANTID:
1037 assert(!data->localpool); /* implement me! */
1038 if (pool != frompool || fromdata->localpool)
1041 id = copyreldep(pool, frompool, id);
1043 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1045 if (key->type == REPOKEY_TYPE_ID)
1046 repodata_set_id(data, handle, keyname, id);
1048 repodata_set_constantid(data, handle, keyname, id);
1050 case REPOKEY_TYPE_STR:
1051 repodata_set_str(data, handle, keyname, kv->str);
1053 case REPOKEY_TYPE_VOID:
1054 repodata_set_void(data, handle, keyname);
1056 case REPOKEY_TYPE_NUM:
1057 repodata_set_num(data, handle, keyname, kv->num);
1059 case REPOKEY_TYPE_CONSTANT:
1060 repodata_set_constant(data, handle, keyname, kv->num);
1062 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1064 assert(!data->localpool); /* implement me! */
1065 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1066 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1068 case REPOKEY_TYPE_DIRSTRARRAY:
1070 assert(!data->localpool); /* implement me! */
1071 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1072 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1081 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1083 Repo *repo = s->repo;
1084 Repo *fromrepo = r->repo;
1085 Pool *pool = repo->pool;
1086 struct solvable_copy_cbdata cbdata;
1088 /* copy solvable data */
1089 if (pool == fromrepo->pool)
1094 s->vendor = r->vendor;
1099 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1101 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1103 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1105 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1107 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1108 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1109 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1110 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1111 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1112 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1113 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1114 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1116 /* copy all attributes */
1120 cbdata.handle = s - pool->solvables;
1121 cbdata.dircache = dircache;
1122 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1125 /* used to sort entries returned in some database order */
1127 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1129 struct rpmid const *a = va, *b = vb;
1131 r = strcmp(a->name, b->name);
1134 return a->dbid - b->dbid;
1138 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1141 Pool *pool = repo->pool;
1142 Solvable *a = pool->solvables + *(Id *)va;
1143 Solvable *b = pool->solvables + *(Id *)vb;
1146 if (a->name != b->name)
1147 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1148 rpmdbid = repo->rpmdbid;
1149 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1153 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1155 Pool *pool = repo->pool;
1158 tmp = pool->solvables[pa];
1159 pool->solvables[pa] = pool->solvables[pb];
1160 pool->solvables[pb] = tmp;
1163 Id tmpid = repo->rpmdbid[pa - repo->start];
1164 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1165 repo->rpmdbid[pb - repo->start] = tmpid;
1167 /* only works if nothing is already internalized! */
1168 if (data && data->attrs)
1170 Id *tmpattrs = data->attrs[pa - data->start];
1171 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1172 data->attrs[pb - data->start] = tmpattrs;
1177 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1179 memset(cookie, 0, 32);
1180 cookie[3] = RPMDB_COOKIE_VERSION;
1181 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1182 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1186 count_headers(const char *rootdir, DB_ENV *dbenv)
1188 char dbpath[PATH_MAX];
1189 struct stat statbuf;
1197 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1198 if (stat(dbpath, &statbuf))
1200 memset(&dbkey, 0, sizeof(dbkey));
1201 memset(&dbdata, 0, sizeof(dbdata));
1202 if (db_create(&db, dbenv, 0))
1204 perror("db_create");
1207 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1209 perror("db->open Name index");
1212 if (db->get_byteswapped(db, &byteswapped))
1214 perror("db->get_byteswapped");
1217 if (db->cursor(db, NULL, &dbc, 0))
1219 perror("db->cursor");
1222 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1223 count += dbdata.size >> 3;
1230 * read rpm db as repo
1235 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1237 Pool *pool = repo->pool;
1238 unsigned char buf[16];
1243 unsigned char *dp, *dbidp;
1245 struct rpmid *rpmids, *rp;
1251 unsigned int refmask, h;
1252 char dbpath[PATH_MAX];
1256 struct stat packagesstat;
1257 unsigned char newcookie[32];
1258 const unsigned char *oldcookie = 0;
1259 Id oldcookietype = 0;
1261 int count = 0, done = 0;
1264 now = sat_timems(0);
1265 memset(&dbkey, 0, sizeof(dbkey));
1266 memset(&dbdata, 0, sizeof(dbdata));
1271 if (!(flags & REPO_REUSE_REPODATA))
1272 data = repo_add_repodata(repo, 0);
1274 data = repo_last_repodata(repo);
1276 if (ref && !(ref->nsolvables && ref->rpmdbid))
1279 if (db_env_create(&dbenv, 0))
1281 perror("db_env_create");
1284 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1285 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1287 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
1289 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1292 perror("dbenv open");
1296 /* XXX: should get ro lock of Packages database! */
1297 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1298 if (stat(dbpath, &packagesstat))
1303 mkrpmdbcookie(&packagesstat, newcookie);
1304 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1307 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1308 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1312 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1313 count = count_headers(rootdir, dbenv);
1314 if (db_create(&db, dbenv, 0))
1316 perror("db_create");
1319 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1321 perror("db->open Packages index");
1324 if (db->get_byteswapped(db, &byteswapped))
1326 perror("db->get_byteswapped");
1329 if (db->cursor(db, NULL, &dbc, 0))
1331 perror("db->cursor");
1334 dbidp = (unsigned char *)&dbid;
1339 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1342 s = pool_id2solvable(pool, repo_add_solvable(repo));
1344 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1345 if (dbkey.size != 4)
1347 fprintf(stderr, "corrupt Packages database (key size)\n");
1359 memcpy(dbidp, dp, 4);
1360 if (dbid == 0) /* the join key */
1362 if (dbdata.size < 8)
1364 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1367 if (dbdata.size > rpmheadsize)
1369 rpmheadsize = dbdata.size + 128;
1370 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1372 memcpy(buf, dbdata.data, 8);
1373 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1374 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1375 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1377 fprintf(stderr, "corrupt rpm database (data size)\n");
1380 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1381 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1382 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1383 if (rpm2solv(pool, repo, data, s, rpmhead, flags))
1390 /* We can reuse this solvable, but make sure it's still
1391 associated with this repo. */
1392 memset(s, 0, sizeof(*s));
1395 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1399 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1400 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1405 /* oops, could not reuse. free it instead */
1406 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1412 /* now sort all solvables */
1413 if (repo->end - repo->start > 1)
1415 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1416 for (i = repo->start; i < repo->end; i++)
1417 pkgids[i - repo->start] = i;
1418 sat_sort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp, repo);
1420 for (i = repo->start; i < repo->end; i++)
1422 int j = pkgids[i - repo->start];
1424 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1426 swap_solvables(repo, data, i, j);
1433 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1435 memset(dircache, 0, sizeof(dircache));
1436 if (db_create(&db, dbenv, 0))
1438 perror("db_create");
1441 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1443 perror("db->open Name index");
1446 if (db->get_byteswapped(db, &byteswapped))
1448 perror("db->get_byteswapped");
1451 if (db->cursor(db, NULL, &dbc, 0))
1453 perror("db->cursor");
1456 dbidp = (unsigned char *)&dbid;
1459 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1461 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1475 memcpy(dbidp, dp, 4);
1476 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1477 rpmids[nrpmids].dbid = dbid;
1478 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1479 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1480 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1491 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1493 dbidp = (unsigned char *)&dbid;
1497 /* create hash from dbid to ref */
1498 refmask = mkmask(ref->nsolvables);
1499 refhash = sat_calloc(refmask + 1, sizeof(Id));
1500 for (i = 0; i < ref->end - ref->start; i++)
1502 if (!ref->rpmdbid[i])
1504 h = ref->rpmdbid[i] & refmask;
1506 h = (h + 317) & refmask;
1507 refhash[h] = i + 1; /* make it non-zero */
1510 /* count the misses, they will cost us time */
1511 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1513 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1519 while ((id = refhash[h]))
1521 if (ref->rpmdbid[id - 1] == dbid)
1523 h = (h + 317) & refmask;
1532 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1534 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1536 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1539 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1543 while ((id = refhash[h]))
1545 if (ref->rpmdbid[id - 1] == dbid)
1547 h = (h + 317) & refmask;
1551 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1554 solvable_copy(s, r, data, dircache);
1561 if (db_create(&db, dbenv, 0))
1563 perror("db_create");
1566 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1568 perror("db->open var/lib/rpm/Packages");
1571 if (db->get_byteswapped(db, &byteswapped))
1573 perror("db->get_byteswapped");
1585 memcpy(buf, dbidp, 4);
1590 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1593 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1594 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1597 if (dbdata.size < 8)
1599 fprintf(stderr, "corrupt rpm database (size)\n");
1602 if (dbdata.size > rpmheadsize)
1604 rpmheadsize = dbdata.size + 128;
1605 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1607 memcpy(buf, dbdata.data, 8);
1608 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1609 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1610 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1612 fprintf(stderr, "corrupt rpm database (data size)\n");
1615 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1616 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1618 rpm2solv(pool, repo, data, s, rpmhead, flags);
1619 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1623 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1624 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1632 for (i = 0; i < nrpmids; i++)
1633 sat_free(rpmids[i].name);
1639 dbenv->close(dbenv, 0);
1642 if (!(flags & REPO_NO_INTERNALIZE))
1643 repodata_internalize(data);
1644 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1645 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1646 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1647 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1648 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1652 static inline unsigned int
1653 getu32(unsigned char *dp)
1655 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1660 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1662 int i, sigdsize, sigcnt, l;
1663 Pool *pool = repo->pool;
1665 RpmHead *rpmhead = 0;
1666 int rpmheadsize = 0;
1667 char *payloadformat;
1669 unsigned char lead[4096];
1670 int headerstart, headerend;
1673 unsigned char pkgid[16];
1678 if (!(flags & REPO_REUSE_REPODATA))
1679 data = repo_add_repodata(repo, 0);
1681 data = repo_last_repodata(repo);
1683 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1684 chksumtype = REPOKEY_TYPE_SHA256;
1685 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1686 chksumtype = REPOKEY_TYPE_SHA1;
1687 for (i = 0; i < nrpms; i++)
1689 if ((fp = fopen(rpms[i], "r")) == 0)
1694 if (fstat(fileno(fp), &stb))
1700 chksumh = sat_chksum_free(chksumh, 0);
1702 chksumh = sat_chksum_create(chksumtype);
1703 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1705 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1710 sat_chksum_add(chksumh, lead, 96 + 16);
1711 if (lead[78] != 0 || lead[79] != 5)
1713 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1717 if (getu32(lead + 96) != 0x8eade801)
1719 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1723 sigcnt = getu32(lead + 96 + 8);
1724 sigdsize = getu32(lead + 96 + 12);
1725 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1727 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1731 sigdsize += sigcnt * 16;
1732 sigdsize = (sigdsize + 7) & ~7;
1733 headerstart = 96 + 16 + sigdsize;
1735 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1737 unsigned char *chksum;
1738 unsigned int chksumsize;
1739 /* extract pkgid from the signature header */
1740 if (sigdsize > rpmheadsize)
1742 rpmheadsize = sigdsize + 128;
1743 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1745 if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1747 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1752 sat_chksum_add(chksumh, rpmhead->data, sigdsize);
1753 rpmhead->cnt = sigcnt;
1754 rpmhead->dcnt = sigdsize - sigcnt * 16;
1755 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1756 chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
1757 if (chksum && chksumsize == 16)
1760 memcpy(pkgid, chksum, 16);
1765 /* just skip the signature header */
1768 l = sigdsize > 4096 ? 4096 : sigdsize;
1769 if (fread(lead, l, 1, fp) != 1)
1771 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1776 sat_chksum_add(chksumh, lead, l);
1780 if (fread(lead, 16, 1, fp) != 1)
1782 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1787 sat_chksum_add(chksumh, lead, 16);
1788 if (getu32(lead) != 0x8eade801)
1790 fprintf(stderr, "%s: bad header\n", rpms[i]);
1794 sigcnt = getu32(lead + 8);
1795 sigdsize = getu32(lead + 12);
1796 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1798 fprintf(stderr, "%s: bad header\n", rpms[i]);
1802 l = sigdsize + sigcnt * 16;
1803 headerend = headerstart + 16 + l;
1804 if (l > rpmheadsize)
1806 rpmheadsize = l + 128;
1807 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1809 if (fread(rpmhead->data, l, 1, fp) != 1)
1811 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1816 sat_chksum_add(chksumh, rpmhead->data, l);
1817 rpmhead->cnt = sigcnt;
1818 rpmhead->dcnt = sigdsize;
1819 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1820 if (headexists(rpmhead, TAG_PATCHESNAME))
1822 /* this is a patch rpm, ignore */
1826 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1827 if (payloadformat && !strcmp(payloadformat, "drpm"))
1829 /* this is a delta rpm */
1834 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
1835 sat_chksum_add(chksumh, lead, l);
1837 s = pool_id2solvable(pool, repo_add_solvable(repo));
1838 rpm2solv(pool, repo, data, s, rpmhead, flags);
1841 Id handle = s - pool->solvables;
1842 repodata_set_location(data, handle, 0, 0, rpms[i]);
1843 if (S_ISREG(stb.st_mode))
1844 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1845 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1847 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
1849 repodata_set_bin_checksum(data, handle, SOLVABLE_CHECKSUM, chksumtype, sat_chksum_get(chksumh, 0));
1853 chksumh = sat_chksum_free(chksumh, 0);
1856 if (!(flags & REPO_NO_INTERNALIZE))
1857 repodata_internalize(data);
1861 linkhash(const char *lt, char *hash)
1864 const unsigned char *str = (const unsigned char *)lt;
1868 while ((c = *str++) != 0)
1870 sprintf(hash, "%08x", r);
1871 sprintf(hash + 8, "%08x", l);
1872 sprintf(hash + 16, "%08x", 0);
1873 sprintf(hash + 24, "%08x", 0);
1877 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1879 RpmHead *rpmhead = rpmhandle;
1884 unsigned int *di, diidx;
1885 unsigned int lastdir;
1888 int cnt, dcnt, cnt2;
1892 char md5[33], *md5p = 0;
1894 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1897 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1899 for (i = 0; i < dcnt; i++)
1900 (*cb)(cbdata, dn[i], 0, (char *)0);
1904 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1910 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1911 if (!di || cnt != cnt2)
1918 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1919 if (!fm || cnt != cnt2)
1927 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1929 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1930 if (!md || cnt != cnt2)
1942 for (i = 0; i < cnt; i++)
1947 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
1950 l = l1 + strlen(bn[i]) + 1;
1954 space = sat_realloc(space, spacen);
1956 if (lastdir != diidx)
1958 strcpy(space, dn[diidx]);
1962 strcpy(space + l1, bn[i]);
1971 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
1977 linkhash(lt[i], md5);
1984 (*cb)(cbdata, space, fm[i], md5p);
1996 struct rpm_by_state {
2007 rpm_installedrpmdbids(const char *rootdir, Queue *rpmdbidq)
2009 char dbpath[PATH_MAX];
2021 queue_empty(rpmdbidq);
2024 if (db_env_create(&dbenv, 0))
2026 perror("db_env_create");
2029 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
2031 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
2033 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
2036 perror("dbenv open");
2037 dbenv->close(dbenv, 0);
2040 if (db_create(&db, dbenv, 0))
2042 perror("db_create");
2043 dbenv->close(dbenv, 0);
2046 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2048 perror("db->open Name index");
2050 dbenv->close(dbenv, 0);
2053 if (db->get_byteswapped(db, &byteswapped))
2055 perror("db->get_byteswapped");
2057 dbenv->close(dbenv, 0);
2060 if (db->cursor(db, NULL, &dbc, 0))
2062 perror("db->cursor");
2064 dbenv->close(dbenv, 0);
2067 memset(&dbkey, 0, sizeof(dbkey));
2068 memset(&dbdata, 0, sizeof(dbdata));
2069 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
2071 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2079 ((char *)&rpmdbid)[0] = dp[3];
2080 ((char *)&rpmdbid)[1] = dp[2];
2081 ((char *)&rpmdbid)[2] = dp[1];
2082 ((char *)&rpmdbid)[3] = dp[0];
2085 memcpy((char *)&rpmdbid, dp, 4);
2087 queue_push(rpmdbidq, rpmdbid);
2095 dbenv->close(dbenv, 0);
2100 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2102 struct rpm_by_state *state = *statep;
2103 unsigned char buf[16];
2114 state->db->close(state->db, 0);
2116 state->dbenv->close(state->dbenv, 0);
2117 sat_free(state->rpmhead);
2119 *statep = (void *)0;
2125 state = sat_calloc(1, sizeof(*state));
2128 if (!state->dbopened)
2130 char dbpath[PATH_MAX];
2131 state->dbopened = 1;
2132 if (db_env_create(&state->dbenv, 0))
2134 perror("db_env_create");
2140 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
2142 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
2144 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
2147 perror("dbenv open");
2148 state->dbenv->close(state->dbenv, 0);
2152 if (db_create(&state->db, state->dbenv, 0))
2154 perror("db_create");
2156 state->dbenv->close(state->dbenv, 0);
2160 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2162 perror("db->open var/lib/rpm/Packages");
2163 state->db->close(state->db, 0);
2165 state->dbenv->close(state->dbenv, 0);
2169 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2171 perror("db->get_byteswapped");
2172 state->db->close(state->db, 0);
2174 state->dbenv->close(state->dbenv, 0);
2179 memcpy(buf, &rpmdbid, 4);
2180 if (state->byteswapped)
2183 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2184 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2186 memset(&dbkey, 0, sizeof(dbkey));
2187 memset(&dbdata, 0, sizeof(dbdata));
2192 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2197 if (dbdata.size < 8)
2199 fprintf(stderr, "corrupt rpm database (size)\n");
2202 if (dbdata.size > state->rpmheadsize)
2204 state->rpmheadsize = dbdata.size + 128;
2205 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2207 rpmhead = state->rpmhead;
2208 memcpy(buf, dbdata.data, 8);
2209 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2210 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2211 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2213 fprintf(stderr, "corrupt rpm database (data size)\n");
2216 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2217 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2222 rpm_byfp(FILE *fp, const char *name, void **statep)
2224 struct rpm_by_state *state = *statep;
2225 int headerstart, headerend;
2227 int sigdsize, sigcnt, l;
2228 unsigned char lead[4096];
2231 return rpm_byrpmdbid(0, 0, statep);
2234 state = sat_calloc(1, sizeof(*state));
2237 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2239 fprintf(stderr, "%s: not a rpm\n", name);
2242 if (lead[78] != 0 || lead[79] != 5)
2244 fprintf(stderr, "%s: not a V5 header\n", name);
2247 if (getu32(lead + 96) != 0x8eade801)
2249 fprintf(stderr, "%s: bad signature header\n", name);
2252 sigcnt = getu32(lead + 96 + 8);
2253 sigdsize = getu32(lead + 96 + 12);
2254 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2256 fprintf(stderr, "%s: bad signature header\n", name);
2259 sigdsize += sigcnt * 16;
2260 sigdsize = (sigdsize + 7) & ~7;
2261 headerstart = 96 + 16 + sigdsize;
2264 l = sigdsize > 4096 ? 4096 : sigdsize;
2265 if (fread(lead, l, 1, fp) != 1)
2267 fprintf(stderr, "%s: unexpected EOF\n", name);
2272 if (fread(lead, 16, 1, fp) != 1)
2274 fprintf(stderr, "%s: unexpected EOF\n", name);
2277 if (getu32(lead) != 0x8eade801)
2279 fprintf(stderr, "%s: bad header\n", name);
2283 sigcnt = getu32(lead + 8);
2284 sigdsize = getu32(lead + 12);
2285 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2287 fprintf(stderr, "%s: bad header\n", name);
2291 l = sigdsize + sigcnt * 16;
2292 headerend = headerstart + 16 + l;
2293 if (l > state->rpmheadsize)
2295 state->rpmheadsize = l + 128;
2296 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2298 rpmhead = state->rpmhead;
2299 if (fread(rpmhead->data, l, 1, fp) != 1)
2301 fprintf(stderr, "%s: unexpected EOF\n", name);
2305 rpmhead->cnt = sigcnt;
2306 rpmhead->dcnt = sigdsize;
2307 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;