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>
31 #include "repo_rpmdb.h"
33 #define RPMDB_COOKIE_VERSION 1
36 #define TAG_VERSION 1001
37 #define TAG_RELEASE 1002
38 #define TAG_EPOCH 1003
39 #define TAG_SUMMARY 1004
40 #define TAG_DESCRIPTION 1005
41 #define TAG_BUILDTIME 1006
42 #define TAG_INSTALLTIME 1008
44 #define TAG_VENDOR 1011
45 #define TAG_LICENSE 1014
46 #define TAG_GROUP 1016
48 #define TAG_FILESIZES 1028
49 #define TAG_FILEMODES 1030
50 #define TAG_SOURCERPM 1044
51 #define TAG_PROVIDENAME 1047
52 #define TAG_REQUIREFLAGS 1048
53 #define TAG_REQUIRENAME 1049
54 #define TAG_REQUIREVERSION 1050
55 #define TAG_NOSOURCE 1051
56 #define TAG_NOPATCH 1052
57 #define TAG_CONFLICTFLAGS 1053
58 #define TAG_CONFLICTNAME 1054
59 #define TAG_CONFLICTVERSION 1055
60 #define TAG_OBSOLETENAME 1090
61 #define TAG_FILEDEVICES 1095
62 #define TAG_FILEINODES 1096
63 #define TAG_PROVIDEFLAGS 1112
64 #define TAG_PROVIDEVERSION 1113
65 #define TAG_OBSOLETEFLAGS 1114
66 #define TAG_OBSOLETEVERSION 1115
67 #define TAG_DIRINDEXES 1116
68 #define TAG_BASENAMES 1117
69 #define TAG_DIRNAMES 1118
70 #define TAG_PAYLOADFORMAT 1124
71 #define TAG_PATCHESNAME 1133
72 #define TAG_SUGGESTSNAME 1156
73 #define TAG_SUGGESTSVERSION 1157
74 #define TAG_SUGGESTSFLAGS 1158
75 #define TAG_ENHANCESNAME 1159
76 #define TAG_ENHANCESVERSION 1160
77 #define TAG_ENHANCESFLAGS 1161
79 #define DEP_LESS (1 << 1)
80 #define DEP_GREATER (1 << 2)
81 #define DEP_EQUAL (1 << 3)
82 #define DEP_STRONG (1 << 27)
83 #define DEP_PRE ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
91 typedef struct rpmhead {
95 unsigned char data[1];
99 headexists(RpmHead *h, int tag)
102 unsigned char *d, taga[4];
109 for (i = 0; i < h->cnt; i++, d -= 16)
110 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
115 static unsigned int *
116 headint32array(RpmHead *h, int tag, int *cnt)
118 unsigned int i, o, *r;
119 unsigned char *d, taga[4];
126 for (i = 0; i < h->cnt; i++, d -= 16)
127 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
131 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
133 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
134 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
135 if (o + 4 * i > h->dcnt)
138 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
141 for (o = 0; o < i; o++, d += 4)
142 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
147 headint32(RpmHead *h, int tag)
150 unsigned char *d, taga[4];
157 for (i = 0; i < h->cnt; i++, d -= 16)
158 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
162 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
164 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
165 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
166 if (i == 0 || o + 4 * i > h->dcnt)
169 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
172 static unsigned int *
173 headint16array(RpmHead *h, int tag, int *cnt)
175 unsigned int i, o, *r;
176 unsigned char *d, taga[4];
183 for (i = 0; i < h->cnt; i++, d -= 16)
184 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
188 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
190 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
191 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
192 if (o + 4 * i > h->dcnt)
195 r = sat_calloc(i ? i : 1, sizeof(unsigned int));
198 for (o = 0; o < i; o++, d += 2)
199 r[o] = d[0] << 8 | d[1];
204 headstring(RpmHead *h, int tag)
207 unsigned char *d, taga[4];
213 for (i = 0; i < h->cnt; i++, d -= 16)
214 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
218 /* 6: STRING, 9: I18NSTRING */
219 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
221 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
222 return (char *)h->dp + o;
226 headstringarray(RpmHead *h, int tag, int *cnt)
229 unsigned char *d, taga[4];
237 for (i = 0; i < h->cnt; i++, d -= 16)
238 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
242 if (d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
244 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
245 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
246 r = sat_calloc(i ? i : 1, sizeof(char *));
250 for (o = 0; o < i; o++)
254 d += strlen((char *)d) + 1;
255 if (d >= h->dp + h->dcnt)
264 static char *headtoevr(RpmHead *h)
271 version = headstring(h, TAG_VERSION);
272 release = headstring(h, TAG_RELEASE);
273 epoch = headint32(h, TAG_EPOCH);
274 if (!version || !release)
276 fprintf(stderr, "headtoevr: bad rpm header\n");
279 for (v = version; *v >= 0 && *v <= '9'; v++)
281 if (epoch || (v != version && *v == ':'))
283 char epochbuf[11]; /* 32bit decimal will fit in */
284 sprintf(epochbuf, "%u", epoch);
285 evr = sat_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
286 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
290 evr = sat_malloc(strlen(version) + 1 + strlen(release) + 1);
291 sprintf(evr, "%s-%s", version, release);
298 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
300 const unsigned char *cp;
305 /* check if it's already utf8, code taken from screen ;-) */
306 cp = (const unsigned char *)str;
307 while ((c = *cp++) != 0)
311 if ((c & 0xc0) != 0x80)
312 break; /* encoding error */
313 c = (c & 0x3f) | (state << 6);
314 if (!(state & 0x40000000))
316 /* check for overlong sequences */
317 if ((c & 0x820823e0) == 0x80000000)
319 else if ((c & 0x020821f0) == 0x02000000)
321 else if ((c & 0x000820f8) == 0x00080000)
323 else if ((c & 0x0000207c) == 0x00002000)
333 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
335 c = (c & 0x03) | 0xbfffff00; /* 4 */
337 c = (c & 0x07) | 0xbfffc000; /* 3 */
339 c = (c & 0x0f) | 0xbff00000; /* 2 */
341 c = (c & 0x1f) | 0xfc000000; /* 1 */
343 c = 0xfdffffff; /* overlong */
347 state = (c & 0x80000000) ? c : 0;
351 /* not utf8, assume latin1 */
352 buf = sat_malloc(2 * strlen(str) + 1);
353 cp = (const unsigned char *)str;
356 while ((c = *cp++) != 0)
369 repodata_set_str(repodata, handle, tag, str);
375 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int strong)
379 int i, cc, nc, vc, fc;
381 unsigned int olddeps;
384 n = headstringarray(rpmhead, tagn, &nc);
387 v = headstringarray(rpmhead, tagv, &vc);
393 f = headint32array(rpmhead, tagf, &fc);
400 if (nc != vc || nc != fc)
402 fprintf(stderr, "bad dependency entries\n");
410 for (i = 0; i < nc; i++)
411 if ((f[i] & DEP_STRONG) == (strong == 1 ? 0 : DEP_STRONG))
414 if ((f[i] & DEP_PRE) != 0)
420 for (i = 0; i < nc; i++)
421 if ((f[i] & DEP_PRE) != 0)
427 if (tagn != TAG_REQUIRENAME)
437 olddeps = repo_reserve_ids(repo, 0, cc);
438 ida = repo->idarraydata + olddeps;
447 *ida++ = SOLVABLE_PREREQMARKER;
449 if (strong && (f[i] & DEP_STRONG) != (strong == 1 ? 0 : DEP_STRONG))
451 if (haspre == 1 && (f[i] & DEP_PRE) != 0)
453 if (haspre == 2 && (f[i] & DEP_PRE) == 0)
455 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
459 if ((f[i] & DEP_LESS) != 0)
461 if ((f[i] & DEP_EQUAL) != 0)
463 if ((f[i] & DEP_GREATER) != 0)
465 name = str2id(pool, n[i], 1);
466 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
467 evr = str2id(pool, v[i] + 2, 1);
469 evr = str2id(pool, v[i], 1);
470 *ida++ = rel2id(pool, name, evr, flags, 1);
473 *ida++ = str2id(pool, n[i], 1);
476 repo->idarraysize += cc + 1;
484 #ifdef USE_FILEFILTER
486 #define FILEFILTER_EXACT 0
487 #define FILEFILTER_STARTS 1
488 #define FILEFILTER_CONTAINS 2
496 static struct filefilter filefilters[] = {
497 { FILEFILTER_CONTAINS, "/bin/", 0},
498 { FILEFILTER_CONTAINS, "/sbin/", 0},
499 { FILEFILTER_CONTAINS, "/lib/", 0},
500 { FILEFILTER_CONTAINS, "/lib64/", 0},
501 { FILEFILTER_CONTAINS, "/etc/", 0},
502 { FILEFILTER_STARTS, "/usr/games/", 0},
503 { FILEFILTER_EXACT, "/usr/share/dict/", "words"},
504 { FILEFILTER_STARTS, "/usr/share/", "magic.mime"},
505 { FILEFILTER_STARTS, "/opt/gnome/games/", 0},
511 adddudata(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dic)
515 unsigned int *fkb, *fn, *fsz, *fm, *fino;
516 unsigned int inotest[256], inotestok;
520 fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
521 if (!fsz || fc != fszc)
526 /* stupid rpm recodrs sizes of directories, so we have to check the mode */
527 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
528 if (!fm || fc != fszc)
534 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
535 if (!fino || fc != fszc)
543 if (fc < sizeof(inotest))
545 memset(inotest, 0, sizeof(inotest));
546 for (i = 0; i < fc; i++)
549 if (fsz[i] == 0 || !S_ISREG(fm[i]))
551 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
552 bit = 1 << (fino[i] & 31);
553 if ((inotest[off] & bit) != 0)
562 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
564 unsigned int mask, hash, hh;
565 if (!fdev || fc != fszc)
574 while ((mask & (mask - 1)) != 0)
575 mask = mask & (mask - 1);
577 if (mask > sizeof(inotest)/sizeof(*inotest))
578 fx = sat_calloc(mask, sizeof(unsigned int));
582 memset(fx, 0, mask * sizeof(unsigned int));
585 for (i = 0; i < fc; i++)
587 if (fsz[i] == 0 || !S_ISREG(fm[i]))
589 hash = (fino[i] + fdev[i] * 31) & mask;
591 while ((j = fx[hash]) != 0)
593 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
595 fsz[i] = 0; /* kill entry */
598 hash = (hash + hh++) & mask;
608 fn = sat_calloc(dic, sizeof(unsigned int));
609 fkb = sat_calloc(dic, sizeof(unsigned int));
610 for (i = 0; i < fc; i++)
612 if (fsz[i] == 0 || !S_ISREG(fm[i]))
617 fkb[di[i]] += fsz[i] / 1024 + 1;
622 repodata_extend(repodata, s - pool->solvables);
623 handle = (s - pool->solvables) - repodata->start;
624 handle = repodata_get_handle(repodata, handle);
625 for (i = 0; i < fc; i++)
629 if (!*dn[i] && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
630 did = repodata_str2dir(repodata, "/usr/src", 1);
632 did = repodata_str2dir(repodata, dn[i], 1);
633 repodata_add_dirnumnum(repodata, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
639 /* assumes last processed array is provides! */
641 addfileprovides(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead, unsigned int olddeps)
648 #ifdef USE_FILEFILTER
650 struct filefilter *ff;
659 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
662 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
668 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
677 fprintf(stderr, "bad filelist\n");
682 adddudata(pool, repo, repodata, s, rpmhead, dn, di, bnc, dic);
684 for (i = 0; i < bnc; i++)
686 #ifdef USE_FILEFILTER
688 for (j = 0; j < sizeof(filefilters)/sizeof(*filefilters); j++, ff++)
692 switch (ff->dirmatch)
694 case FILEFILTER_STARTS:
695 if (strncmp(dn[di[i]], ff->dir, strlen(ff->dir)))
698 case FILEFILTER_CONTAINS:
699 if (!strstr(dn[di[i]], ff->dir))
702 case FILEFILTER_EXACT:
704 if (strcmp(dn[di[i]], ff->dir))
711 if (strcmp(bn[i], ff->base))
716 if (j == sizeof(filefilters)/sizeof(*filefilters))
720 j = strlen(bn[i]) + strlen(dn[di[i]]) + 1;
724 fn = sat_realloc(fn, fna);
726 strcpy(fn, dn[di[i]]);
728 olddeps = repo_addid_dep(repo, olddeps, str2id(pool, fn, 1), SOLVABLE_FILEMARKER);
733 repodata_extend(repodata, s - pool->solvables);
734 handle = (s - pool->solvables) - repodata->start;
735 handle = repodata_get_handle(repodata, handle);
736 did = repodata_str2dir(repodata, dn[di[i]], 1);
738 did = repodata_str2dir(repodata, "/", 1);
739 repodata_add_dirstr(repodata, handle, SOLVABLE_FILELIST, did, bn[i]);
753 addsourcerpm(Pool *pool, Repodata *repodata, Id handle, char *sourcerpm, char *name, char *evr)
755 const char *p, *sevr, *sarch;
757 p = strrchr(sourcerpm, '.');
758 if (!p || strcmp(p, ".rpm") != 0)
761 while (p > sourcerpm && *p != '.')
763 if (*p != '.' || p == sourcerpm)
766 while (p > sourcerpm && *p != '-')
768 if (*p != '-' || p == sourcerpm)
771 while (p > sourcerpm && *p != '-')
773 if (*p != '-' || p == sourcerpm)
776 if (!strcmp(sarch, "src.rpm"))
777 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
778 else if (!strcmp(sarch, "nosrc.rpm"))
779 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
781 repodata_set_constantid(repodata, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
782 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
783 repodata_set_void(repodata, handle, SOLVABLE_SOURCEEVR);
785 repodata_set_id(repodata, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
786 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
787 repodata_set_void(repodata, handle, SOLVABLE_SOURCENAME);
789 repodata_set_id(repodata, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
793 rpm2solv(Pool *pool, Repo *repo, Repodata *repodata, Solvable *s, RpmHead *rpmhead)
799 name = headstring(rpmhead, TAG_NAME);
800 if (!strcmp(name, "gpg-pubkey"))
802 s->name = str2id(pool, name, 1);
805 fprintf(stderr, "package has no name\n");
808 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
810 s->arch = str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
813 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
814 s->arch = ARCH_NOSRC;
819 s->arch = ARCH_NOARCH;
820 evr = headtoevr(rpmhead);
821 s->evr = str2id(pool, evr, 1);
822 s->vendor = str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
824 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
825 s->provides = addfileprovides(pool, repo, repodata, s, rpmhead, s->provides);
826 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
827 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, 0);
828 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
829 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
831 s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 2);
832 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, 1);
833 s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 2);
834 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, 1);
836 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
844 repodata_extend(repodata, s - pool->solvables);
845 handle = repodata_get_handle(repodata, (s - pool->solvables) - repodata->start);
846 str = headstring(rpmhead, TAG_SUMMARY);
848 setutf8string(repodata, handle, SOLVABLE_SUMMARY, str);
849 str = headstring(rpmhead, TAG_DESCRIPTION);
853 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
854 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
858 /* oh my, found SUSE special author section */
863 while (l > 0 && str[l - 1] == '\n')
866 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
868 aut = str; /* copy over */
869 while (*p == ' ' || *p == '\n')
882 while (aut != str && aut[-1] == '\n')
886 setutf8string(repodata, handle, SOLVABLE_AUTHORS, str);
890 setutf8string(repodata, handle, SOLVABLE_DESCRIPTION, str);
892 str = headstring(rpmhead, TAG_GROUP);
894 repodata_set_poolstr(repodata, handle, SOLVABLE_GROUP, str);
895 str = headstring(rpmhead, TAG_LICENSE);
897 repodata_set_poolstr(repodata, handle, SOLVABLE_LICENSE, str);
898 u32 = headint32(rpmhead, TAG_BUILDTIME);
900 repodata_set_num(repodata, handle, SOLVABLE_BUILDTIME, u32);
901 u32 = headint32(rpmhead, TAG_INSTALLTIME);
903 repodata_set_num(repodata, handle, SOLVABLE_INSTALLTIME, u32);
904 u32 = headint32(rpmhead, TAG_SIZE);
906 repodata_set_num(repodata, handle, SOLVABLE_INSTALLSIZE, (u32 + 1023) / 1024);
908 addsourcerpm(pool, repodata, handle, sourcerpm, name, evr);
915 copyreldep(Pool *pool, Pool *frompool, Id id)
917 Reldep *rd = GETRELDEP(frompool, id);
918 Id name = rd->name, evr = rd->evr;
920 name = copyreldep(pool, frompool, name);
922 name = str2id(pool, id2str(frompool, name), 1);
924 evr = copyreldep(pool, frompool, evr);
926 evr = str2id(pool, id2str(frompool, evr), 1);
927 return rel2id(pool, name, evr, rd->flags, 1);
931 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
936 Pool *frompool = fromrepo->pool;
940 from = fromrepo->idarraydata + fromoff;
941 for (ida = from, cc = 0; *ida; ida++, cc++)
945 ido = repo_reserve_ids(repo, 0, cc);
946 ida = repo->idarraydata + ido;
947 if (frompool && pool != frompool)
953 id = copyreldep(pool, frompool, id);
955 id = str2id(pool, id2str(frompool, id), 1);
961 memcpy(ida, from, (cc + 1) * sizeof(Id));
962 repo->idarraysize += cc + 1;
966 static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
969 copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
971 if (cache && cache[did & 255] == did)
972 return cache[(did & 255) + 256];
973 return copydir_complex(pool, data, fromspool, fromdata, did, cache);
977 copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
979 Id parent = dirpool_parent(&fromdata->dirpool, did);
980 Id compid = dirpool_compid(&fromdata->dirpool, did);
982 parent = copydir(pool, data, fromspool, fromdata, parent, cache);
983 if (fromspool != &pool->ss)
984 compid = str2id(pool, stringpool_id2str(fromspool, compid), 1);
985 compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
988 cache[did & 255] = did;
989 cache[(did & 255) + 256] = compid;
994 struct solvable_copy_cbdata {
1001 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1003 struct solvable_copy_cbdata *cbdata = vcbdata;
1005 Repodata *data = cbdata->data;
1006 Id handle = cbdata->handle;
1007 Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
1008 Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
1010 keyname = key->name;
1011 if (keyname >= ID_NUM_INTERNAL)
1012 keyname = str2id(pool, id2str(frompool, keyname), 1);
1015 case REPOKEY_TYPE_ID:
1016 case REPOKEY_TYPE_CONSTANTID:
1018 assert(!data->localpool); /* implement me! */
1019 if (pool != frompool || fromdata->localpool)
1022 id = copyreldep(pool, frompool, id);
1024 id = str2id(pool, stringpool_id2str(fromspool, id), 1);
1026 if (key->type == REPOKEY_TYPE_ID)
1027 repodata_set_id(data, handle, keyname, id);
1029 repodata_set_constantid(data, handle, keyname, id);
1031 case REPOKEY_TYPE_STR:
1032 repodata_set_str(data, handle, keyname, kv->str);
1034 case REPOKEY_TYPE_VOID:
1035 repodata_set_void(data, handle, keyname);
1037 case REPOKEY_TYPE_NUM:
1038 repodata_set_num(data, handle, keyname, kv->num);
1040 case REPOKEY_TYPE_CONSTANT:
1041 repodata_set_constant(data, handle, keyname, kv->num);
1043 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1045 assert(!data->localpool); /* implement me! */
1046 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1047 repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1049 case REPOKEY_TYPE_DIRSTRARRAY:
1051 assert(!data->localpool); /* implement me! */
1052 id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
1053 repodata_add_dirstr(data, handle, keyname, id, kv->str);
1062 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1064 Repo *repo = s->repo;
1065 Repo *fromrepo = r->repo;
1066 Pool *pool = repo->pool;
1067 struct solvable_copy_cbdata cbdata;
1069 /* copy solvable data */
1070 if (pool == fromrepo->pool)
1075 s->vendor = r->vendor;
1080 s->name = str2id(pool, id2str(fromrepo->pool, r->name), 1);
1082 s->evr = str2id(pool, id2str(fromrepo->pool, r->evr), 1);
1084 s->arch = str2id(pool, id2str(fromrepo->pool, r->arch), 1);
1086 s->vendor = str2id(pool, id2str(fromrepo->pool, r->vendor), 1);
1088 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1089 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1090 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1091 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1092 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1093 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1094 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1095 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1096 s->freshens = copydeps(pool, repo, r->freshens, fromrepo);
1098 /* copy all attributes */
1101 repodata_extend(data, s - pool->solvables);
1103 cbdata.handle = repodata_get_handle(data, (s - pool->solvables) - data->start);
1104 cbdata.dircache = dircache;
1105 repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE, solvable_copy_cb, &cbdata);
1108 /* used to sort entries returned in some database order */
1110 rpmids_sort_cmp(const void *va, const void *vb)
1112 struct rpmid const *a = va, *b = vb;
1114 r = strcmp(a->name, b->name);
1117 return a->dbid - b->dbid;
1120 static Repo *pkgids_sort_cmp_data;
1123 pkgids_sort_cmp(const void *va, const void *vb)
1125 Pool *pool = pkgids_sort_cmp_data->pool;
1126 Solvable *a = pool->solvables + *(Id *)va;
1127 Solvable *b = pool->solvables + *(Id *)vb;
1130 if (a->name != b->name)
1131 return strcmp(id2str(pool, a->name), id2str(pool, b->name));
1132 rpmdbid = pkgids_sort_cmp_data->rpmdbid;
1133 return rpmdbid[(a - pool->solvables) - pkgids_sort_cmp_data->start] - rpmdbid[(b - pool->solvables) - pkgids_sort_cmp_data->start];
1137 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1139 Pool *pool = repo->pool;
1142 tmp = pool->solvables[pa];
1143 pool->solvables[pa] = pool->solvables[pb];
1144 pool->solvables[pb] = tmp;
1147 Id tmpid = repo->rpmdbid[pa - repo->start];
1148 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1149 repo->rpmdbid[pb - repo->start] = tmpid;
1151 /* only works if nothing is already internalized! */
1152 if (data && data->attrs)
1154 Id tmpattrs = data->attrs[pa - data->start];
1155 data->attrs[pa - data->start] = data->attrs[pb - data->start];
1156 data->attrs[pb - data->start] = tmpattrs;
1161 mkrpmdbcookie(struct stat *st, unsigned char *cookie)
1163 memset(cookie, 0, 32);
1164 cookie[3] = RPMDB_COOKIE_VERSION;
1165 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1166 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1170 * read rpm db as repo
1175 repo_add_rpmdb(Repo *repo, Repo *ref, const char *rootdir)
1177 Pool *pool = repo->pool;
1178 unsigned char buf[16];
1183 unsigned char *dp, *dbidp;
1185 struct rpmid *rpmids, *rp;
1191 unsigned int refmask, h;
1194 char dbpath[PATH_MAX];
1198 struct stat packagesstat;
1200 memset(&dbkey, 0, sizeof(dbkey));
1201 memset(&dbdata, 0, sizeof(dbdata));
1203 if (repo->start != repo->end)
1204 abort(); /* FIXME: rpmdbid */
1209 repodata = repo_add_repodata(repo, 0);
1211 if (ref && !(ref->nsolvables && ref->rpmdbid))
1214 if (db_env_create(&dbenv, 0))
1216 perror("db_env_create");
1219 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir);
1220 if (dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0))
1222 perror("dbenv open");
1225 if (db_create(&db, dbenv, 0))
1227 perror("db_create");
1231 /* XXX: should get ro lock of Packages database! */
1232 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1233 if (stat(dbpath, &packagesstat))
1238 mkrpmdbcookie(&packagesstat, repo->rpmdbcookie);
1240 if (!ref || memcmp(repo->rpmdbcookie, ref->rpmdbcookie, 32) != 0)
1243 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1245 perror("db->open var/lib/rpm/Packages");
1248 if (db->get_byteswapped(db, &byteswapped))
1250 perror("db->get_byteswapped");
1253 if (db->cursor(db, NULL, &dbc, 0))
1255 perror("db->cursor");
1258 dbidp = (unsigned char *)&dbid;
1259 repo->rpmdbid = sat_calloc(256, sizeof(Id));
1265 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1268 s = pool_id2solvable(pool, repo_add_solvable(repo));
1271 repo->rpmdbid = sat_realloc(repo->rpmdbid, (asolv + 256) * sizeof(Id));
1272 memset(repo->rpmdbid + asolv, 0, 256 * sizeof(unsigned int));
1275 if (dbkey.size != 4)
1277 fprintf(stderr, "corrupt Packages database (key size)\n");
1289 memcpy(dbidp, dp, 4);
1290 if (dbid == 0) /* the join key */
1292 if (dbdata.size < 8)
1294 fprintf(stderr, "corrupt rpm database (size %u)\n", dbdata.size);
1297 if (dbdata.size > rpmheadsize)
1298 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1299 memcpy(buf, dbdata.data, 8);
1300 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1301 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1302 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1304 fprintf(stderr, "corrupt rpm database (data size)\n");
1307 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1308 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1309 repo->rpmdbid[i] = dbid;
1310 if (rpm2solv(pool, repo, repodata, s, rpmhead))
1317 /* We can reuse this solvable, but make sure it's still
1318 associated with this repo. */
1319 memset(s, 0, sizeof(*s));
1325 /* oops, could not reuse. free it instead */
1326 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
1332 /* now sort all solvables */
1333 if (repo->end - repo->start > 1)
1335 pkgids = sat_malloc2(repo->end - repo->start, sizeof(Id));
1336 for (i = repo->start; i < repo->end; i++)
1337 pkgids[i - repo->start] = i;
1338 pkgids_sort_cmp_data = repo;
1339 qsort(pkgids, repo->end - repo->start, sizeof(Id), pkgids_sort_cmp);
1341 for (i = repo->start; i < repo->end; i++)
1343 int j = pkgids[i - repo->start];
1345 j = pkgids[i - repo->start] = pkgids[j - repo->start];
1347 swap_solvables(repo, repodata, i, j);
1356 memset(dircache, 0, sizeof(dircache));
1357 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir);
1358 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1360 perror("db->open var/lib/rpm/Name");
1363 if (db->get_byteswapped(db, &byteswapped))
1365 perror("db->get_byteswapped");
1368 if (db->cursor(db, NULL, &dbc, 0))
1370 perror("db->cursor");
1373 dbidp = (unsigned char *)&dbid;
1376 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1378 if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1392 memcpy(dbidp, dp, 4);
1393 rpmids = sat_extend(rpmids, nrpmids, 1, sizeof(*rpmids), 255);
1394 rpmids[nrpmids].dbid = dbid;
1395 rpmids[nrpmids].name = sat_malloc((int)dbkey.size + 1);
1396 memcpy(rpmids[nrpmids].name, dbkey.data, (int)dbkey.size);
1397 rpmids[nrpmids].name[(int)dbkey.size] = 0;
1408 qsort(rpmids, nrpmids, sizeof(*rpmids), rpmids_sort_cmp);
1411 dbidp = (unsigned char *)&dbid;
1415 /* create hash from dbid to ref */
1416 refmask = mkmask(ref->nsolvables);
1417 refhash = sat_calloc(refmask + 1, sizeof(Id));
1418 for (i = 0; i < ref->nsolvables; i++)
1420 h = ref->rpmdbid[i] & refmask;
1422 h = (h + 317) & refmask;
1423 refhash[h] = i + 1; /* make it non-zero */
1426 repo->rpmdbid = sat_calloc(nrpmids, sizeof(unsigned int));
1427 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nrpmids));
1429 for (i = 0; i < nrpmids; i++, rp++, s++)
1432 repo->rpmdbid[i] = dbid;
1436 while ((id = refhash[h]))
1438 if (ref->rpmdbid[id - 1] == dbid)
1440 h = (h + 317) & refmask;
1444 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1445 solvable_copy(s, r, repodata, dircache);
1451 if (db_create(&db, 0, 0))
1453 perror("db_create");
1456 snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir);
1457 if (db->open(db, 0, dbpath, 0, DB_HASH, DB_RDONLY, 0664))
1459 perror("db->open var/lib/rpm/Packages");
1462 if (db->get_byteswapped(db, &byteswapped))
1464 perror("db->get_byteswapped");
1476 memcpy(buf, dbidp, 4);
1481 if (db->get(db, NULL, &dbkey, &dbdata, 0))
1484 fprintf(stderr, "corrupt rpm database\n");
1487 if (dbdata.size < 8)
1489 fprintf(stderr, "corrupt rpm database (size)\n");
1492 if (dbdata.size > rpmheadsize)
1493 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + dbdata.size);
1494 memcpy(buf, dbdata.data, 8);
1495 rpmhead->cnt = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1496 rpmhead->dcnt = buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7];
1497 if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1499 fprintf(stderr, "corrupt rpm database (data size)\n");
1502 memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1503 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1505 rpm2solv(pool, repo, repodata, s, rpmhead);
1512 for (i = 0; i < nrpmids; i++)
1513 sat_free(rpmids[i].name);
1521 dbenv->close(dbenv, 0);
1523 repodata_internalize(repodata);
1526 static inline unsigned int
1527 getu32(unsigned char *dp)
1529 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1533 add_location(Repodata *data, Solvable *s, Id handle, const char *location)
1535 Pool *pool = s->repo->pool;
1536 const char *name, *n1, *n2;
1539 repodata_extend(data, s - pool->solvables);
1541 /* skip ./ prefix */
1542 if (location[0] == '.' && location[1] == '/' && location[2] != '/')
1545 name = strrchr(location, '/');
1551 n2 = id2str(pool, s->arch);
1553 if (strncmp(location, n2, l) != 0 || location + l + 1 != name)
1555 /* too bad, need to store directory */
1556 char *dir = strdup(location);
1557 dir[name - location - 1] = 0;
1558 repodata_set_str(data, handle, SOLVABLE_MEDIADIR, dir);
1562 repodata_set_void(data, handle, SOLVABLE_MEDIADIR);
1565 for (n2 = id2str(pool, s->name); *n2; n1++, n2++)
1568 if (*n2 || *n1 != '-')
1571 for (n2 = id2str (pool, s->evr); *n2; n1++, n2++)
1574 if (*n2 || *n1 != '.')
1577 for (n2 = id2str (pool, s->arch); *n2; n1++, n2++)
1580 if (*n2 || strcmp (n1, ".rpm"))
1582 repodata_set_void(data, handle, SOLVABLE_MEDIAFILE);
1586 repodata_set_str(data, handle, SOLVABLE_MEDIAFILE, name);
1591 repo_add_rpms(Repo *repo, const char **rpms, int nrpms)
1593 int i, sigdsize, sigcnt, l;
1594 Pool *pool = repo->pool;
1597 RpmHead *rpmhead = 0;
1598 int rpmheadsize = 0;
1599 char *payloadformat;
1601 unsigned char lead[4096];
1602 int headerstart, headerend;
1607 repodata = repo_add_repodata(repo, 0);
1608 for (i = 0; i < nrpms; i++)
1610 if ((fp = fopen(rpms[i], "r")) == 0)
1615 if (fstat(fileno(fp), &stb))
1620 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1622 fprintf(stderr, "%s: not a rpm\n", rpms[i]);
1626 if (lead[78] != 0 || lead[79] != 5)
1628 fprintf(stderr, "%s: not a V5 header\n", rpms[i]);
1632 if (getu32(lead + 96) != 0x8eade801)
1634 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1638 sigcnt = getu32(lead + 96 + 8);
1639 sigdsize = getu32(lead + 96 + 12);
1640 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1642 fprintf(stderr, "%s: bad signature header\n", rpms[i]);
1646 sigdsize += sigcnt * 16;
1647 sigdsize = (sigdsize + 7) & ~7;
1648 headerstart = 96 + 16 + sigdsize;
1651 l = sigdsize > 4096 ? 4096 : sigdsize;
1652 if (fread(lead, l, 1, fp) != 1)
1654 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1660 if (fread(lead, 16, 1, fp) != 1)
1662 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1666 if (getu32(lead) != 0x8eade801)
1668 fprintf(stderr, "%s: bad header\n", rpms[i]);
1672 sigcnt = getu32(lead + 8);
1673 sigdsize = getu32(lead + 12);
1674 if (sigcnt >= 0x4000000 || sigdsize >= 0x40000000)
1676 fprintf(stderr, "%s: bad header\n", rpms[i]);
1680 l = sigdsize + sigcnt * 16;
1681 headerend = headerstart + 16 + l;
1682 if (l > rpmheadsize)
1683 rpmhead = sat_realloc(rpmhead, sizeof(*rpmhead) + l);
1684 if (fread(rpmhead->data, l, 1, fp) != 1)
1686 fprintf(stderr, "%s: unexpected EOF\n", rpms[i]);
1690 rpmhead->cnt = sigcnt;
1691 rpmhead->dcnt = sigdsize;
1692 rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1693 if (headexists(rpmhead, TAG_PATCHESNAME))
1695 /* this is a patch rpm, ignore */
1699 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
1700 if (payloadformat && !strcmp(payloadformat, "drpm"))
1702 /* this is a delta rpm */
1707 s = pool_id2solvable(pool, repo_add_solvable(repo));
1708 rpm2solv(pool, repo, repodata, s, rpmhead);
1711 Id handle = (s - pool->solvables) - repodata->start;
1712 handle = repodata_get_handle(repodata, handle);
1713 add_location(repodata, s, handle, rpms[i]);
1714 repodata_set_num(repodata, handle, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
1715 repodata_set_num(repodata, handle, SOLVABLE_HEADEREND, headerend);
1721 repodata_internalize(repodata);