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>
36 #include "repo_rpmdb.h"
38 #define RPMDB_COOKIE_VERSION 2
41 #define TAG_VERSION 1001
42 #define TAG_RELEASE 1002
43 #define TAG_EPOCH 1003
44 #define TAG_SUMMARY 1004
45 #define TAG_DESCRIPTION 1005
46 #define TAG_BUILDTIME 1006
47 #define TAG_BUILDHOST 1007
48 #define TAG_INSTALLTIME 1008
50 #define TAG_DISTRIBUTION 1010
51 #define TAG_VENDOR 1011
52 #define TAG_LICENSE 1014
53 #define TAG_PACKAGER 1015
54 #define TAG_GROUP 1016
57 #define TAG_FILESIZES 1028
58 #define TAG_FILEMODES 1030
59 #define TAG_FILEMD5S 1035
60 #define TAG_FILELINKTOS 1036
61 #define TAG_SOURCERPM 1044
62 #define TAG_PROVIDENAME 1047
63 #define TAG_REQUIREFLAGS 1048
64 #define TAG_REQUIRENAME 1049
65 #define TAG_REQUIREVERSION 1050
66 #define TAG_NOSOURCE 1051
67 #define TAG_NOPATCH 1052
68 #define TAG_CONFLICTFLAGS 1053
69 #define TAG_CONFLICTNAME 1054
70 #define TAG_CONFLICTVERSION 1055
71 #define TAG_OBSOLETENAME 1090
72 #define TAG_FILEDEVICES 1095
73 #define TAG_FILEINODES 1096
74 #define TAG_PROVIDEFLAGS 1112
75 #define TAG_PROVIDEVERSION 1113
76 #define TAG_OBSOLETEFLAGS 1114
77 #define TAG_OBSOLETEVERSION 1115
78 #define TAG_DIRINDEXES 1116
79 #define TAG_BASENAMES 1117
80 #define TAG_DIRNAMES 1118
81 #define TAG_PAYLOADFORMAT 1124
82 #define TAG_PATCHESNAME 1133
83 #define TAG_FILECOLORS 1140
84 #define TAG_SUGGESTSNAME 1156
85 #define TAG_SUGGESTSVERSION 1157
86 #define TAG_SUGGESTSFLAGS 1158
87 #define TAG_ENHANCESNAME 1159
88 #define TAG_ENHANCESVERSION 1160
89 #define TAG_ENHANCESFLAGS 1161
91 #define DEP_LESS (1 << 1)
92 #define DEP_GREATER (1 << 2)
93 #define DEP_EQUAL (1 << 3)
94 #define DEP_STRONG (1 << 27)
95 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
103 typedef struct rpmhead {
107 unsigned char data[1];
111 headexists(RpmHead *h, int tag)
114 unsigned char *d, taga[4];
121 for (i = 0; i < h->cnt; i++, d -= 16)
122 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
127 static unsigned int *
128 headint32array(RpmHead *h, int tag, int *cnt)
130 unsigned int i, o, *r;
131 unsigned char *d, taga[4];
138 for (i = 0; i < h->cnt; i++, d -= 16)
139 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
143 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
145 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
146 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
147 if (o + 4 * i > h->dcnt)
150 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
153 for (o = 0; o < i; o++, d += 4)
154 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
159 headint32(RpmHead *h, int tag)
162 unsigned char *d, taga[4];
169 for (i = 0; i < h->cnt; i++, d -= 16)
170 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
174 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
176 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
177 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
178 if (i == 0 || o + 4 * i > h->dcnt)
181 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
184 static unsigned int *
185 headint16array(RpmHead *h, int tag, int *cnt)
187 unsigned int i, o, *r;
188 unsigned char *d, taga[4];
195 for (i = 0; i < h->cnt; i++, d -= 16)
196 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
200 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
202 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
203 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
204 if (o + 4 * i > h->dcnt)
207 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
210 for (o = 0; o < i; o++, d += 2)
211 r[o] = d[0] << 8 | d[1];
216 headstring(RpmHead *h, int tag)
219 unsigned char *d, taga[4];
225 for (i = 0; i < h->cnt; i++, d -= 16)
226 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
230 /* 6: STRING, 9: I18NSTRING */
231 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
233 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
234 return (char *)h->dp + o;
238 headstringarray(RpmHead *h, int tag, int *cnt)
241 unsigned char *d, taga[4];
249 for (i = 0; i < h->cnt; i++, d -= 16)
250 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
254 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
256 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
257 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
258 r = sat_calloc(i ? i : 1, sizeof(char *));
262 for (o = 0; o < i; o++)
266 d += strlen((char *)d) + 1;
267 if (d >= h->dp + h->dcnt)
276 static char *headtoevr(RpmHead *h)
283 version = headstring(h, TAG_VERSION);
284 release = headstring(h, TAG_RELEASE);
285 epoch = headint32(h, TAG_EPOCH);
286 if (!version || !release)
288 fprintf(stderr, "headtoevr: bad rpm header\n");
291 for (v = version; *v >= 0 && *v <= '9'; v++)
293 if (epoch || (v != version && *v == ':'))
295 char epochbuf[11]; /* 32bit decimal will fit in */
296 sprintf(epochbuf, "%u", epoch);
297 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
298 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
302 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
303 sprintf(evr, "%s-%s", version, release);
310 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
312 const unsigned char *cp;
315 unsigned char *buf = 0, *bp;
317 /* check if it's already utf8, code taken from screen ;-) */
318 cp = (const unsigned char *)str;
319 while ((c = *cp++) != 0)
323 if ((c & 0xc0) != 0x80)
324 break; /* encoding error */
325 c = (c & 0x3f) | (state << 6);
326 if (!(state & 0x40000000))
328 /* check for overlong sequences */
329 if ((c & 0x820823e0) == 0x80000000)
331 else if ((c & 0x020821f0) == 0x02000000)
333 else if ((c & 0x000820f8) == 0x00080000)
335 else if ((c & 0x0000207c) == 0x00002000)
345 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
347 c = (c & 0x03) | 0xbfffff00; /* 4 */
349 c = (c & 0x07) | 0xbfffc000; /* 3 */
351 c = (c & 0x0f) | 0xbff00000; /* 2 */
353 c = (c & 0x1f) | 0xfc000000; /* 1 */
357 state = (c & 0x80000000) ? c : 0;
361 /* not utf8, assume latin1 */
362 buf = sat_malloc(2 * strlen(str) + 1);
363 cp = (const unsigned char *)str;
366 while ((c = *cp++) != 0)
379 repodata_set_str(repodata, handle, tag, str);
385 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
389 int i, cc, nc, vc, fc;
391 unsigned int olddeps;
394 n = headstringarray(rpmhead, tagn, &nc);
397 v = headstringarray(rpmhead, tagv, &vc);
403 f = headint32array(rpmhead, tagf, &fc);
410 if (nc != vc || nc != fc)
412 fprintf(stderr, "bad dependency entries\n");
420 for (i = 0; i < nc; i++)
421 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
424 if ((f[i] & DEP_PRE) != 0)
430 for (i = 0; i < nc; i++)
431 if ((f[i] & DEP_PRE) != 0)
437 if (tagn != TAG_REQUIRENAME)
447 olddeps = repo_reserve_ids(repo, 0, cc);
448 ida = repo->idarraydata + olddeps;
457 *ida++ = SOLVABLE_PREREQMARKER;
459 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
461 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
463 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
465 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
469 if ((f[i] & DEP_LESS) != 0)
471 if ((f[i] & DEP_EQUAL) != 0)
473 if ((f[i] & DEP_GREATER) != 0)
475 name = str2id(pool, n[i], 1);
476 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
477 evr = str2id(pool, v[i] + 2, 1);
479 evr = str2id(pool, v[i], 1);
480 *ida++ = rel2id(pool, name, evr, flags, 1);
483 *ida++ = str2id(pool, n[i], 1);
486 repo->idarraysize += cc + 1;
494 #ifdef USE_FILEFILTER
496 #define FILEFILTER_EXACT 0
497 #define FILEFILTER_STARTS 1
498 #define FILEFILTER_CONTAINS 2
506 static struct filefilter filefilters[] = {
507 { FILEFILTER_CONTAINS, "/bin/", 0},
508 { FILEFILTER_CONTAINS, "/sbin/", 0},
509 { FILEFILTER_CONTAINS, "/lib/", 0},
510 { FILEFILTER_CONTAINS, "/lib64/", 0},
511 { FILEFILTER_CONTAINS, "/etc/", 0},
512 { FILEFILTER_STARTS, "/usr/games/", 0},
513 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
514 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
515 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
521 adddudata(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
525 unsigned int *fkb, *fn, *fsz, *fm, *fino;
526 unsigned int inotest[256], inotestok;
530 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
531 if (!fsz || fc != fszc)
536 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
537 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
538 if (!fm || fc != fszc)
544 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
545 if (!fino || fc != fszc)
553 if (fc < sizeof(inotest))
555 memset(inotest, 0, sizeof(inotest));
556 for (i = 0; i < fc; i++)
559 if (fsz[i] == 0 || !S_ISREG(fm[i]))
561 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
562 bit = 1 << (fino[i] & 31);
563 if ((inotest[off] & bit) != 0)
572 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
574 unsigned int mask, hash, hh;
575 if (!fdev || fc != fszc)
584 while ((mask & (mask - 1)) != 0)
585 mask = mask & (mask - 1);
587 if (mask > sizeof(inotest)/sizeof(*inotest))
588 fx = sat_calloc(mask, sizeof(unsigned int));
592 memset(fx, 0, mask * sizeof(unsigned int));
595 for (i = 0; i < fc; i++)
597 if (fsz[i] == 0 || !S_ISREG(fm[i]))
599 hash = (fino[i] + fdev[i] * 31) & mask;
601 while ((j = fx[hash]) != 0)
603 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
605 fsz[i] = 0; /* kill entry */
608 hash = (hash + hh++) & mask;
618 fn = sat_calloc(dic, sizeof(unsigned int));
619 fkb = sat_calloc(dic, sizeof(unsigned int));
620 for (i = 0; i < fc; i++)
622 if (fsz[i] == 0 || !S_ISREG(fm[i]))
627 fkb[di[i]] += fsz[i] / 1024 + 1;
632 handle = s - pool->solvables;
633 for (i = 0; i < fc; i++)
637 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
638 did = repodata_str2dir(data, "/usr/src", 1);
640 did = repodata_str2dir(data, dn[i], 1);
641 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
647 /* assumes last processed array is provides! */
649 addfileprovides(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
656 #ifdef USE_FILEFILTER
658 struct filefilter *ff;
667 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
670 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
676 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
685 fprintf(stderr, "bad filelist\n");
690 adddudata(pool, repo, data, s, rpmhead, dn, di, bnc, dic);
692 for (i = 0; i < bnc; i++)
694 #ifdef USE_FILEFILTER
696 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
700 switch (ff->dirmatch)
702 case FILEFILTER_STARTS:
703 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
706 case FILEFILTER_CONTAINS:
707 if (!strstr(dn[di[i]], ff->dir))
710 case FILEFILTER_EXACT:
712 if (strcmp(dn[di[i]], ff->dir))
719 if (strcmp(bn[i], ff->base))
724 if (j == sizeof(filefilters)/sizeof(*filefilters))
728 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
732 fn = sat_realloc(fn, fna);
734 strcpy(fn, dn[di[i]]);
736 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
741 handle = s - pool->solvables;
742 did = repodata_str2dir(data, dn[di[i]], 1);
744 did = repodata_str2dir(data, "/", 1);
745 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, bn[i]);
759 addsourcerpm(Pool *pool, Repodata *data, Id handle, char *sourcerpm, char *name, char *evr)
761 const char *p, *sevr, *sarch;
763 p = strrchr(sourcerpm, '.');
764 if (!p || strcmp(p, ".rpm") != 0)
767 while (p > sourcerpm && *p != '.')
769 if (*p != '.' || p == sourcerpm)
772 while (p > sourcerpm && *p != '-')
774 if (*p != '-' || p == sourcerpm)
777 while (p > sourcerpm && *p != '-')
779 if (*p != '-' || p == sourcerpm)
782 if (!strcmp(sarch, "src.rpm"))
783 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
784 else if (!strcmp(sarch, "nosrc.rpm"))
785 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
787 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
788 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
789 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
791 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
792 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
793 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
795 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
799 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead)
805 name = headstring(rpmhead, TAG_NAME);
806 if (!strcmp(name, "gpg-pubkey"))
808 s->name = str2id(pool, name, 1);
811 fprintf(stderr, "package has no name\n");
814 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
816 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
819 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
820 s->arch = ARCH_NOSRC;
825 s->arch = ARCH_NOARCH;
826 evr = headtoevr(rpmhead);
827 s->evr = str2id(pool, evr, 1);
828 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
830 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
831 s->provides = addfileprovides(pool, repo, data, s, rpmhead, s->provides);
832 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
833 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
834 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
835 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
836 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
838 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
839 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
840 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
841 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
842 s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
843 s->conflicts = repo_fix_conflicts(repo, s->conflicts);
851 handle = s - pool->solvables;
852 str = headstring(rpmhead, TAG_SUMMARY);
854 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
855 str = headstring(rpmhead, TAG_DESCRIPTION);
859 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
860 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
864 /* oh my, found SUSE special author section */
869 while (l > 0 && str[l - 1] == '\n')
872 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
874 aut = str; /* copy over */
875 while (*p == ' ' || *p == '\n')
888 while (aut != str && aut[-1] == '\n')
892 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
896 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
898 str = headstring(rpmhead, TAG_GROUP);
900 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
901 str = headstring(rpmhead, TAG_LICENSE);
903 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
904 str = headstring(rpmhead, TAG_URL);
906 repodata_set_str(data, handle, SOLVABLE_URL, str);
907 str = headstring(rpmhead, TAG_DISTRIBUTION);
909 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
910 str = headstring(rpmhead, TAG_PACKAGER);
912 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
913 u32 = headint32(rpmhead, TAG_BUILDTIME);
915 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
916 u32 = headint32(rpmhead, TAG_INSTALLTIME);
918 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
919 u32 = headint32(rpmhead, TAG_SIZE);
921 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
923 addsourcerpm(pool, data, handle, sourcerpm, name, evr);
930 copyreldep(Pool *pool, Pool *frompool, Id id)
932 Reldep *rd = GETRELDEP(frompool, id);
933 Id name = rd->name, evr = rd->evr;
935 name = copyreldep(pool, frompool, name);
937 name = str2id(pool, id2str(frompool, name), 1);
939 evr = copyreldep(pool, frompool, evr);
941 evr = str2id(pool, id2str(frompool, evr), 1);
942 return rel2id(pool, name, evr, rd->flags, 1);
946 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
951 Pool *frompool = fromrepo->pool;
955 from = fromrepo->idarraydata + fromoff;
956 for (ida = from, cc = 0; *ida; ida++, cc++)
960 ido = repo_reserve_ids(repo, 0, cc);
961 ida = repo->idarraydata + ido;
962 if (frompool && pool != frompool)
968 id = copyreldep(pool, frompool, id);
970 id = str2id(pool, id2str(frompool, id), 1);
976 memcpy(ida, from, (cc + 1) * sizeof(Id));
977 repo->idarraysize += cc + 1;
981 #define COPYDIR_DIRCACHE_SIZE 512
983 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
986 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
988 if (cache && cache[did & 255] == did)
989 return cache[(did & 255) + 256];
990 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
994 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
996 Id parent = dirpool_parent(&fromdata->dirpool, did);
997 Id compid = dirpool_compid(&fromdata->dirpool, did);
999 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
1000 if (fromspool != &pool->ss)
1001 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
1002 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1005 cache[did & 255] = did;
1006 cache[(did & 255) + 256] = compid;
1011 struct solvable_copy_cbdata {
1018 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1020 struct solvable_copy_cbdata *cbdata = vcbdata;
1022 Repodata *data = cbdata->data;
1023 Id handle = cbdata->handle;
1024 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1025 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1027 keyname = key->name;
1028 if (keyname >= ID_NUM_INTERNAL)
1029 keyname = str2id(pool, id2str(frompool, keyname), 1);
1032 case REPOKEY_TYPE_ID:
1033 case REPOKEY_TYPE_CONSTANTID:
1035 assert(!data->localpool); /* implement me! */
1036 if (pool != frompool || fromdata->localpool)
1039 id = copyreldep(pool, frompool, id);
1041 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1043 if (key->type == REPOKEY_TYPE_ID)
1044 repodata_set_id(data, handle, keyname, id);
1046 repodata_set_constantid(data, handle, keyname, id);
1048 case REPOKEY_TYPE_STR:
1049 repodata_set_str(data, handle, keyname, kv->str);
1051 case REPOKEY_TYPE_VOID:
1052 repodata_set_void(data, handle, keyname);
1054 case REPOKEY_TYPE_NUM:
1055 repodata_set_num(data, handle, keyname, kv->num);
1057 case REPOKEY_TYPE_CONSTANT:
1058 repodata_set_constant(data, handle, keyname, kv->num);
1060 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1062 assert(!data->localpool); /* implement me! */
1063 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1064 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1066 case REPOKEY_TYPE_DIRSTRARRAY:
1068 assert(!data->localpool); /* implement me! */
1069 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1070 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1079 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1081 Repo *repo = s->repo;
1082 Repo *fromrepo = r->repo;
1083 Pool *pool = repo->pool;
1084 struct solvable_copy_cbdata cbdata;
1086 /* copy solvable data */
1087 if (pool == fromrepo->pool)
1092 s->vendor = r->vendor;
1097 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1099 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1101 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1103 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1105 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1106 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1107 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1108 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1109 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1110 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1111 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1112 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1114 /* copy all attributes */
1118 cbdata.handle = s - pool->solvables;
1119 cbdata.dircache = dircache;
1120 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1123 /* used to sort entries returned in some database order */
1125 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1127 struct rpmid const *a = va, *b = vb;
1129 r = strcmp(a->name, b->name);
1132 return a->dbid - b->dbid;
1136 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1139 Pool *pool = repo->pool;
1140 Solvable *a = pool->solvables + *(Id *)va;
1141 Solvable *b = pool->solvables + *(Id *)vb;
1144 if (a->name != b->name)
1145 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1146 rpmdbid = repo->rpmdbid;
1147 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1151 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1153 Pool *pool = repo->pool;
1156 tmp = pool->solvables[pa];
1157 pool->solvables[pa] = pool->solvables[pb];
1158 pool->solvables[pb] = tmp;
1161 Id tmpid = repo->rpmdbid[pa - repo->start];
1162 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1163 repo->rpmdbid[pb - repo->start] = tmpid;
1165 /* only works if nothing is already internalized! */
1166 if (data && data->attrs)
1168 Id *tmpattrs = data->attrs[pa - data->start];
1169 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1170 data->attrs[pb - data->start] = tmpattrs;
1175 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1177 memset(cookie, 0, 32);
1178 cookie[3] = RPMDB_COOKIE_VERSION;
1179 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1180 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1184 count_headers(const char *rootdir, DB_ENV *dbenv)
1186 char dbpath[PATH_MAX];
1187 struct stat statbuf;
1195 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1196 if (stat(dbpath, &statbuf))
1198 memset(&dbkey, 0, sizeof(dbkey));
1199 memset(&dbdata, 0, sizeof(dbdata));
1200 if (db_create(&db, dbenv, 0))
1202 perror("db_create");
1205 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1207 perror("db->open Name index");
1210 if (db->get_byteswapped(db, &byteswapped))
1212 perror("db->get_byteswapped");
1215 if (db->cursor(db, NULL, &dbc, 0))
1217 perror("db->cursor");
1220 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1221 count += dbdata.size >> 3;
1228 * read rpm db as repo
1233 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir, int flags)
1235 Pool *pool = repo->pool;
1236 unsigned char buf[16];
1241 unsigned char *dp, *dbidp;
1243 struct rpmid *rpmids, *rp;
1249 unsigned int refmask, h;
1250 char dbpath[PATH_MAX];
1254 struct stat packagesstat;
1255 unsigned char newcookie[32];
1256 const unsigned char *oldcookie = 0;
1257 Id oldcookietype = 0;
1259 int count = 0, done = 0;
1262 now = sat_timems(0);
1263 memset(&dbkey, 0, sizeof(dbkey));
1264 memset(&dbdata, 0, sizeof(dbdata));
1269 if (!(flags & REPO_REUSE_REPODATA))
1270 data = repo_add_repodata(repo, 0);
1272 data = repo_last_repodata(repo);
1274 if (ref && !(ref->nsolvables && ref->rpmdbid))
1277 if (db_env_create(&dbenv, 0))
1279 perror("db_env_create");
1282 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1283 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1285 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
1287 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1290 perror("dbenv open");
1294 /* XXX: should get ro lock of Packages database! */
1295 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1296 if (stat(dbpath, &packagesstat))
1301 mkrpmdbcookie(&packagesstat, newcookie);
1302 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1305 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1306 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1310 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1311 count = count_headers(rootdir, dbenv);
1312 if (db_create(&db, dbenv, 0))
1314 perror("db_create");
1317 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1319 perror("db->open Packages index");
1322 if (db->get_byteswapped(db, &byteswapped))
1324 perror("db->get_byteswapped");
1327 if (db->cursor(db, NULL, &dbc, 0))
1329 perror("db->cursor");
1332 dbidp = (unsigned char *)&dbid;
1337 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1340 s = pool_id2solvable(pool, repo_add_solvable(repo));
1342 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1343 if (dbkey.size != 4)
1345 fprintf(stderr, "corrupt Packages database (key size)\n");
1357 memcpy(dbidp, dp, 4);
1358 if (dbid == 0) /* the join key */
1360 if (dbdata.size < 8)
1362 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1365 if (dbdata.size > rpmheadsize)
1367 rpmheadsize = dbdata.size + 128;
1368 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1370 memcpy(buf, dbdata.data, 8);
1371 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1372 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1373 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1375 fprintf(stderr, "corrupt rpm database (data size)\n");
1378 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1379 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1380 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1381 if (rpm2solv(pool, repo, data, s, rpmhead))
1388 /* We can reuse this solvable, but make sure it's still
1389 associated with this repo. */
1390 memset(s, 0, sizeof(*s));
1393 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1397 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1398 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1403 /* oops, could not reuse. free it instead */
1404 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1410 /* now sort all solvables */
1411 if (repo->end - repo->start > 1)
1413 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1414 for (i = repo->start; i < repo->end; i++)
1415 pkgids[i - repo->start] = i;
1416 sat_sort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp, repo);
1418 for (i = repo->start; i < repo->end; i++)
1420 int j = pkgids[i - repo->start];
1422 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1424 swap_solvables(repo, data, i, j);
1431 Id dircache[COPYDIR_DIRCACHE_SIZE]; /* see copydir */
1433 memset(dircache, 0, sizeof(dircache));
1434 if (db_create(&db, dbenv, 0))
1436 perror("db_create");
1439 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1441 perror("db->open Name index");
1444 if (db->get_byteswapped(db, &byteswapped))
1446 perror("db->get_byteswapped");
1449 if (db->cursor(db, NULL, &dbc, 0))
1451 perror("db->cursor");
1454 dbidp = (unsigned char *)&dbid;
1457 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1459 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1473 memcpy(dbidp, dp, 4);
1474 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1475 rpmids[nrpmids].dbid = dbid;
1476 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1477 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1478 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1489 sat_sort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp, 0);
1491 dbidp = (unsigned char *)&dbid;
1495 /* create hash from dbid to ref */
1496 refmask = mkmask(ref->nsolvables);
1497 refhash = sat_calloc(refmask + 1, sizeof(Id));
1498 for (i = 0; i < ref->end - ref->start; i++)
1500 if (!ref->rpmdbid[i])
1502 h = ref->rpmdbid[i] & refmask;
1504 h = (h + 317) & refmask;
1505 refhash[h] = i + 1; /* make it non-zero */
1508 /* count the misses, they will cost us time */
1509 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1511 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++)
1517 while ((id = refhash[h]))
1519 if (ref->rpmdbid[id - 1] == dbid)
1521 h = (h + 317) & refmask;
1530 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1532 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1534 for (i = 0, rp = rpmids; i < nrpmids; i++, rp++, s++)
1537 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1541 while ((id = refhash[h]))
1543 if (ref->rpmdbid[id - 1] == dbid)
1545 h = (h + 317) & refmask;
1549 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1552 solvable_copy(s, r, data, dircache);
1559 if (db_create(&db, dbenv, 0))
1561 perror("db_create");
1564 if (db->open(db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1566 perror("db->open var/lib/rpm/Packages");
1569 if (db->get_byteswapped(db, &byteswapped))
1571 perror("db->get_byteswapped");
1583 memcpy(buf, dbidp, 4);
1588 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1591 fprintf(stderr, "corrupt rpm database, key %d not found\n", dbid);
1592 fprintf(stderr, "please run 'rpm --rebuilddb' to recreate the database index files\n");
1595 if (dbdata.size < 8)
1597 fprintf(stderr, "corrupt rpm database (size)\n");
1600 if (dbdata.size > rpmheadsize)
1602 rpmheadsize = dbdata.size + 128;
1603 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1605 memcpy(buf, dbdata.data, 8);
1606 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1607 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1608 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1610 fprintf(stderr, "corrupt rpm database (data size)\n");
1613 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1614 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1616 rpm2solv(pool, repo, data, s, rpmhead);
1617 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1621 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1622 pool_debug(pool, SAT_ERROR, "%%%% %d\n", done * 100 / count);
1630 for (i = 0; i < nrpmids; i++)
1631 sat_free(rpmids[i].name);
1637 dbenv->close(dbenv, 0);
1640 if (!(flags & REPO_NO_INTERNALIZE))
1641 repodata_internalize(data);
1642 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1643 pool_debug(pool, SAT_ERROR, "%%%% 100\n");
1644 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", sat_timems(now));
1645 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1646 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data->incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1650 static inline unsigned int
1651 getu32(unsigned char *dp)
1653 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1658 repo_add_rpms(Repo *repo, const char **rpms, int nrpms, int flags)
1660 int i, sigdsize, sigcnt, l;
1661 Pool *pool = repo->pool;
1663 RpmHead *rpmhead = 0;
1664 int rpmheadsize = 0;
1665 char *payloadformat;
1667 unsigned char lead[4096];
1668 int headerstart, headerend;
1672 if (!(flags & REPO_REUSE_REPODATA))
1673 data = repo_add_repodata(repo, 0);
1675 data = repo_last_repodata(repo);
1680 for (i = 0; i < nrpms; i++)
1682 if ((fp = fopen(rpms[i], "r")) == 0)
1687 if (fstat(fileno(fp), &stb))
1692 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1694 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1698 if (lead[78] != 0 || lead[79] != 5)
1700 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1704 if (getu32(lead + 96) != 0x8eade801)
1706 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1710 sigcnt = getu32(lead + 96 + 8);
1711 sigdsize = getu32(lead + 96 + 12);
1712 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1714 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1718 sigdsize += sigcnt * 16;
1719 sigdsize = (sigdsize + 7) & ~7;
1720 headerstart = 96 + 16 + sigdsize;
1723 l = sigdsize > 4096 ? 4096 : sigdsize;
1724 if (fread(lead, l, 1, fp) != 1)
1726 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1732 if (fread(lead, 16, 1, fp) != 1)
1734 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1738 if (getu32(lead) != 0x8eade801)
1740 fprintf(stderr, "%s: bad header\n", rpms[i]);
1744 sigcnt = getu32(lead + 8);
1745 sigdsize = getu32(lead + 12);
1746 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1748 fprintf(stderr, "%s: bad header\n", rpms[i]);
1752 l = sigdsize + sigcnt * 16;
1753 headerend = headerstart + 16 + l;
1754 if (l > rpmheadsize)
1756 rpmheadsize = l + 128;
1757 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1759 if (fread(rpmhead->data, l, 1, fp) != 1)
1761 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1765 rpmhead->cnt = sigcnt;
1766 rpmhead->dcnt = sigdsize;
1767 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1768 if (headexists(rpmhead, TAG_PATCHESNAME))
1770 /* this is a patch rpm, ignore */
1774 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1775 if (payloadformat && !strcmp(payloadformat, "drpm"))
1777 /* this is a delta rpm */
1782 s = pool_id2solvable(pool, repo_add_solvable(repo));
1783 rpm2solv(pool, repo, data, s, rpmhead);
1786 Id handle = s - pool->solvables;
1787 repodata_set_location(data, handle, 0, 0, rpms[i]);
1788 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1789 repodata_set_num(data, handle, SOLVABLE_HEADEREND, headerend);
1794 if (!(flags & REPO_NO_INTERNALIZE))
1795 repodata_internalize(data);
1799 linkhash(const char *lt, char *hash)
1802 const unsigned char *str = (const unsigned char *)lt;
1806 while ((c = *str++) != 0)
1808 sprintf(hash, "%08x", r);
1809 sprintf(hash + 8, "%08x", l);
1810 sprintf(hash + 16, "%08x", 0);
1811 sprintf(hash + 24, "%08x", 0);
1815 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, int, const char *), void *cbdata)
1817 RpmHead *rpmhead = rpmhandle;
1822 unsigned int *di, diidx;
1823 unsigned int lastdir;
1826 int cnt, dcnt, cnt2;
1830 char md5[33], *md5p = 0;
1832 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
1835 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
1837 for (i = 0; i < dcnt; i++)
1838 (*cb)(cbdata, dn[i], 0, (char *)0);
1842 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
1848 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
1849 if (!di || cnt != cnt2)
1856 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
1857 if (!fm || cnt != cnt2)
1865 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
1867 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
1868 if (!md || cnt != cnt2)
1880 for (i = 0; i < cnt; i++)
1885 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
1888 l = l1 + strlen(bn[i]) + 1;
1892 space = sat_realloc(space, spacen);
1894 if (lastdir != diidx)
1896 strcpy(space, dn[diidx]);
1900 strcpy(space + l1, bn[i]);
1909 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
1915 linkhash(lt[i], md5);
1922 (*cb)(cbdata, space, fm[i], md5p);
1934 struct rpm_by_state {
1945 rpm_installedrpmdbids(const char *rootdir, Queue *rpmdbidq)
1947 char dbpath[PATH_MAX];
1959 queue_empty(rpmdbidq);
1962 if (db_env_create(&dbenv, 0))
1964 perror("db_env_create");
1967 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1969 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
1971 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1974 perror("dbenv open");
1975 dbenv->close(dbenv, 0);
1978 if (db_create(&db, dbenv, 0))
1980 perror("db_create");
1981 dbenv->close(dbenv, 0);
1984 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1986 perror("db->open Name index");
1988 dbenv->close(dbenv, 0);
1991 if (db->get_byteswapped(db, &byteswapped))
1993 perror("db->get_byteswapped");
1995 dbenv->close(dbenv, 0);
1998 if (db->cursor(db, NULL, &dbc, 0))
2000 perror("db->cursor");
2002 dbenv->close(dbenv, 0);
2005 memset(&dbkey, 0, sizeof(dbkey));
2006 memset(&dbdata, 0, sizeof(dbdata));
2007 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
2009 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
2017 ((char *)&rpmdbid)[0] = dp[3];
2018 ((char *)&rpmdbid)[1] = dp[2];
2019 ((char *)&rpmdbid)[2] = dp[1];
2020 ((char *)&rpmdbid)[3] = dp[0];
2023 memcpy((char *)&rpmdbid, dp, 4);
2025 queue_push(rpmdbidq, rpmdbid);
2033 dbenv->close(dbenv, 0);
2038 rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
2040 struct rpm_by_state *state = *statep;
2041 unsigned char buf[16];
2052 state->db->close(state->db, 0);
2054 state->dbenv->close(state->dbenv, 0);
2055 sat_free(state->rpmhead);
2057 *statep = (void *)0;
2063 state = sat_calloc(1, sizeof(*state));
2066 if (!state->dbopened)
2068 char dbpath[PATH_MAX];
2069 state->dbopened = 1;
2070 if (db_env_create(&state->dbenv, 0))
2072 perror("db_env_create");
2078 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
2080 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0))
2082 if (state->dbenv->open(state->dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
2085 perror("dbenv open");
2086 state->dbenv->close(state->dbenv, 0);
2090 if (db_create(&state->db, state->dbenv, 0))
2092 perror("db_create");
2094 state->dbenv->close(state->dbenv, 0);
2098 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
2100 perror("db->open var/lib/rpm/Packages");
2101 state->db->close(state->db, 0);
2103 state->dbenv->close(state->dbenv, 0);
2107 if (state->db->get_byteswapped(state->db, &state->byteswapped))
2109 perror("db->get_byteswapped");
2110 state->db->close(state->db, 0);
2112 state->dbenv->close(state->dbenv, 0);
2117 memcpy(buf, &rpmdbid, 4);
2118 if (state->byteswapped)
2121 bx = buf[0]; buf[0] = buf[3]; buf[3] = bx;
2122 bx = buf[1]; buf[1] = buf[2]; buf[2] = bx;
2124 memset(&dbkey, 0, sizeof(dbkey));
2125 memset(&dbdata, 0, sizeof(dbdata));
2130 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
2135 if (dbdata.size < 8)
2137 fprintf(stderr, "corrupt rpm database (size)\n");
2140 if (dbdata.size > state->rpmheadsize)
2142 state->rpmheadsize = dbdata.size + 128;
2143 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
2145 rpmhead = state->rpmhead;
2146 memcpy(buf, dbdata.data, 8);
2147 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
2148 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
2149 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
2151 fprintf(stderr, "corrupt rpm database (data size)\n");
2154 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
2155 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2160 rpm_byfp(FILE *fp, const char *name, void **statep)
2162 struct rpm_by_state *state = *statep;
2163 int headerstart, headerend;
2165 int sigdsize, sigcnt, l;
2166 unsigned char lead[4096];
2169 return rpm_byrpmdbid(0, 0, statep);
2172 state = sat_calloc(1, sizeof(*state));
2175 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2177 fprintf(stderr, "%s: not a rpm\n", name);
2180 if (lead[78] != 0 || lead[79] != 5)
2182 fprintf(stderr, "%s: not a V5 header\n", name);
2185 if (getu32(lead + 96) != 0x8eade801)
2187 fprintf(stderr, "%s: bad signature header\n", name);
2190 sigcnt = getu32(lead + 96 + 8);
2191 sigdsize = getu32(lead + 96 + 12);
2192 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2194 fprintf(stderr, "%s: bad signature header\n", name);
2197 sigdsize += sigcnt * 16;
2198 sigdsize = (sigdsize + 7) & ~7;
2199 headerstart = 96 + 16 + sigdsize;
2202 l = sigdsize > 4096 ? 4096 : sigdsize;
2203 if (fread(lead, l, 1, fp) != 1)
2205 fprintf(stderr, "%s: unexpected EOF\n", name);
2210 if (fread(lead, 16, 1, fp) != 1)
2212 fprintf(stderr, "%s: unexpected EOF\n", name);
2215 if (getu32(lead) != 0x8eade801)
2217 fprintf(stderr, "%s: bad header\n", name);
2221 sigcnt = getu32(lead + 8);
2222 sigdsize = getu32(lead + 12);
2223 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
2225 fprintf(stderr, "%s: bad header\n", name);
2229 l = sigdsize + sigcnt * 16;
2230 headerend = headerstart + 16 + l;
2231 if (l > state->rpmheadsize)
2233 state->rpmheadsize = l + 128;
2234 state->rpmhead = sat_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2236 rpmhead = state->rpmhead;
2237 if (fread(rpmhead->data, l, 1, fp) != 1)
2239 fprintf(stderr, "%s: unexpected EOF\n", name);
2243 rpmhead->cnt = sigcnt;
2244 rpmhead->dcnt = sigdsize;
2245 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;