2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Read the binary dump of a Repo and create a Repo * from it
14 * Repo *pool_addrepo_solv(Pool *pool, FILE *fp)
26 #include "repo_solv.h"
28 #include "attr_store_p.h"
30 #define INTERESTED_START SOLVABLE_NAME
31 #define INTERESTED_END SOLVABLE_FRESHENS
33 static Pool *mypool; /* for pool_debug... */
35 /*-----------------------------------------------------------------*/
36 /* .solv read functions */
48 for (i = 0; i < 4; i++)
53 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
73 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
85 read_id(FILE *fp, Id max)
90 for (i = 0; i < 5; i++)
95 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
103 pool_debug(mypool, SAT_FATAL, "read_id: id too large (%u/%u)\n", x, max);
108 x = (x << 7) ^ c ^ 128;
110 pool_debug(mypool, SAT_FATAL, "read_id: id too long\n");
120 read_idarray(FILE *fp, Id max, Id *map, Id *store, Id *end, int relative)
130 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
135 x = (x << 6) | (c & 63);
138 if (x == 0 && c == 0x40)
143 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
146 *store++ = SOLVABLE_PREREQMARKER;
156 pool_debug(mypool, SAT_FATAL, "read_idarray: id too large (%u/%u)\n", x, max);
163 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
169 if (x == 0) /* already have trailing zero? */
173 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
182 x = (x << 7) ^ c ^ 128;
187 read_str (FILE *fp, char **inbuf, unsigned *len)
189 unsigned char *buf = (unsigned char*)*inbuf;
192 buf = xmalloc (1024);
197 while((c = getc (fp)) != 0)
201 pool_debug (mypool, SAT_FATAL, "unexpected EOF\n");
204 /* Plus 1 as we also want to add the 0. */
208 /* Don't realloc on the inbuf, it might be on the stack. */
209 if (buf == (unsigned char*)*inbuf)
211 buf = xmalloc (*len);
212 memcpy (buf, *inbuf, *len - 256);
215 buf = xrealloc (buf, *len);
224 skip_item (FILE *fp, unsigned type, Id *idmap, unsigned numid, unsigned numrel)
229 read_id(fp, numid + numrel); /* just check Id */
234 case TYPE_ATTR_STRING:
236 while(read_u8(fp) != 0)
240 case TYPE_REL_IDARRAY:
241 while ((read_u8(fp) & 0xc0) != 0)
244 case TYPE_COUNT_NAMED:
246 unsigned count = read_id (fp, 0);
249 read_id (fp, numid); /* Name */
250 unsigned t = read_id (fp, TYPE_ATTR_TYPE_MAX + 1);
251 skip_item (fp, t, idmap, numid, numrel);
257 unsigned count = read_id (fp, 0);
258 unsigned t = read_id (fp, TYPE_ATTR_TYPE_MAX + 1);
260 skip_item (fp, t, idmap, numid, numrel);
263 case TYPE_ATTR_CHUNK:
269 case TYPE_ATTR_INTLIST:
270 case TYPE_ATTR_LOCALIDS:
271 while (read_id(fp, 0) != 0)
275 pool_debug(mypool, SAT_FATAL, "unknown type %d\n", type);
281 key_cmp (const void *pa, const void *pb)
283 struct key { Id name; unsigned type; };
284 struct key *a = (struct key*)pa;
285 struct key *b = (struct key*)pb;
286 return a->name - b->name;
290 parse_repodata (FILE *fp, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
292 unsigned count = read_id (fp, 0);
297 read_id (fp, numid); /* no name */
298 unsigned type = read_id (fp, TYPE_ATTR_TYPE_MAX + 1);
299 if (type != TYPE_COUNT_NAMED)
301 skip_item (fp, type, idmap, numid, numrel);
304 unsigned c = read_id (fp, 0);
309 pool_debug (mypool, SAT_FATAL, "invalid attribute data\n");
312 read_id (fp, numid); /* no name */
313 if (read_id (fp, TYPE_ATTR_TYPE_MAX + 1) != TYPE_STR)
315 pool_debug (mypool, SAT_FATAL, "invalid attribute data\n");
319 unsigned len = sizeof (buf);
320 char *filename = buf;
321 read_str (fp, &filename, &len);
323 read_id (fp, numid); /* no name */
324 if (read_id (fp, TYPE_ATTR_TYPE_MAX + 1) != TYPE_COUNTED)
326 pool_debug (mypool, SAT_FATAL, "invalid attribute data\n");
330 unsigned nkeys = read_id (fp, 0);
332 || read_id (fp, TYPE_ATTR_TYPE_MAX + 1) != TYPE_ID)
334 pool_debug (mypool, SAT_FATAL, "invalid attribute data\n");
340 data = xrealloc (repo->repodata, repo->nrepodata * sizeof (*data));
341 repo->repodata = data;
342 data += repo->nrepodata - 1;
343 memset (data, 0, sizeof (*data));
348 data->keys = xmalloc (data->nkeys * sizeof (data->keys[0]));
349 for (i = 0; i < data->nkeys; i++)
351 data->keys[i].name = idmap[read_id (fp, numid)];
352 data->keys[i].type = read_id (fp, 0);
354 qsort (data->keys, data->nkeys, sizeof (data->keys[0]), key_cmp);
356 data->name = strdup (filename);
363 /*-----------------------------------------------------------------*/
371 // ----------------------------------------------
374 * read repo from .solv file
379 repo_add_solv(Repo *repo, FILE *fp)
381 Pool *pool = repo->pool;
383 unsigned int numid, numrel, numsolv;
384 unsigned int numkeys, numschemata, numinfo;
385 Attrstore *embedded_store = 0;
388 Offset *str; /* map Id -> Offset into string space */
389 char *strsp; /* repo string space */
390 char *sp; /* pointer into string space */
391 Id *idmap; /* map of repo Ids to pool Ids */
393 unsigned int hashmask, h;
399 unsigned int size_idarray;
400 Id *idarraydatap, *idarraydataend;
403 unsigned int solvflags;
404 unsigned int solvversion;
406 Id *schemadata, *schemadatap, *schemadataend;
411 if (read_u32(fp) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
413 pool_debug(pool, SAT_FATAL, "not a SOLV file\n");
416 solvversion = read_u32(fp);
424 pool_debug(pool, SAT_FATAL, "unsupported SOLV version\n");
428 pool_freeidhashes(pool);
430 numid = read_u32(fp);
431 numrel = read_u32(fp);
432 numsolv = read_u32(fp);
433 numkeys = read_u32(fp);
434 numschemata = read_u32(fp);
435 numinfo = read_u32(fp);
436 solvflags = read_u32(fp);
438 if (solvversion < SOLV_VERSION_3
441 pool_debug(pool, SAT_FATAL, "unsupported SOLV format (has info)\n");
445 /******* Part 1: string IDs *****************************************/
447 sizeid = read_u32(fp); /* size of string+Id space */
450 * read strings and Ids
459 /* alloc string buffer */
460 strsp = (char *)xrealloc(pool->ss.stringspace, pool->ss.sstrings + sizeid + 1);
461 /* alloc string offsets (Id -> Offset into string space) */
462 str = (Offset *)xrealloc(pool->ss.strings, (pool->ss.nstrings + numid) * sizeof(Offset));
464 pool->ss.stringspace = strsp;
465 pool->ss.strings = str; /* array of offsets into strsp, indexed by Id */
467 /* point to _BEHIND_ already allocated string/Id space */
468 strsp += pool->ss.sstrings;
470 /* alloc id map for name and rel Ids. this maps ids in the solv files
471 * to the ids in our pool */
472 idmap = (Id *)xcalloc(numid + numrel, sizeof(Id));
475 * read new repo at end of pool
478 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
480 if (fread(strsp, sizeid, 1, fp) != 1)
482 pool_debug(pool, SAT_FATAL, "read error while reading strings\n");
488 unsigned int pfsize = read_u32 (fp);
489 char *prefix = xmalloc (pfsize);
493 if (fread (prefix, pfsize, 1, fp) != 1)
495 pool_debug(pool, SAT_FATAL, "read error while reading strings\n");
498 for (i = 1; i < numid; i++)
500 int same = (unsigned char)*pp++;
501 size_t len = strlen (pp) + 1;
503 memcpy (dest, old_str, same);
504 memcpy (dest + same, pp, len);
511 strsp[sizeid] = 0; /* make string space \0 terminated */
515 * build hashes for all read strings
519 hashmask = mkmask(pool->ss.nstrings + numid);
522 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
523 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
527 * ensure sufficient hash size
530 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
533 * fill hashtable with strings already in pool
536 for (i = 1; i < pool->ss.nstrings; i++) /* leave out our dummy zero id */
538 h = strhash(pool->ss.stringspace + pool->ss.strings[i]) & hashmask;
539 hh = HASHCHAIN_START;
541 h = HASHCHAIN_NEXT(h, hh, hashmask);
546 * run over string space, calculate offsets
548 * build id map (maps solv Id -> pool Id)
551 for (i = 1; i < numid; i++)
553 if (sp >= strsp + sizeid)
555 pool_debug(pool, SAT_FATAL, "not enough strings\n");
558 if (!*sp) /* empty string */
566 h = strhash(sp) & hashmask;
567 hh = HASHCHAIN_START;
573 if (!strcmp(pool->ss.stringspace + pool->ss.strings[id], sp))
574 break; /* existing string */
575 h = HASHCHAIN_NEXT(h, hh, hashmask);
578 /* length == offset to next string */
580 if (id == ID_NULL) /* end of hash chain -> new string */
582 id = pool->ss.nstrings++;
584 str[id] = pool->ss.sstrings; /* save Offset */
585 if (sp != pool->ss.stringspace + pool->ss.sstrings) /* not at end-of-buffer */
586 memmove(pool->ss.stringspace + pool->ss.sstrings, sp, l); /* append to pool buffer */
587 pool->ss.sstrings += l;
589 idmap[i] = id; /* repo relative -> pool relative */
590 sp += l; /* next string */
593 pool_shrink_strings(pool); /* vacuum */
596 /******* Part 2: Relation IDs ***************************************/
606 ran = (Reldep *)xrealloc(pool->rels, (pool->nrels + numrel) * sizeof(Reldep));
609 pool_debug(pool, SAT_FATAL, "no mem for rel space\n");
612 pool->rels = ran; /* extended rel space */
614 hashmask = mkmask(pool->nrels + numrel);
616 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
617 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
620 * prep hash table with already existing RelDeps
623 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
624 for (i = 1; i < pool->nrels; i++)
626 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
627 hh = HASHCHAIN_START;
629 h = HASHCHAIN_NEXT(h, hh, hashmask);
634 * read RelDeps from repo
637 for (i = 0; i < numrel; i++)
639 name = read_id(fp, i + numid); /* read (repo relative) Ids */
640 evr = read_id(fp, i + numid);
642 name = idmap[name]; /* map to (pool relative) Ids */
644 h = relhash(name, evr, flags) & hashmask;
645 hh = HASHCHAIN_START;
649 if (id == ID_NULL) /* end of hash chain */
651 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
653 h = HASHCHAIN_NEXT(h, hh, hashmask);
655 if (id == ID_NULL) /* new RelDep */
661 ran[id].flags = flags;
663 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
666 pool_shrink_rels(pool); /* vacuum */
670 /******* Part 3: Keys ***********************************************/
672 keys = xcalloc(numkeys, sizeof(*keys));
673 /* keys start at 1 */
674 for (i = 1; i < numkeys; i++)
676 keys[i].name = idmap[read_id(fp, numid)];
677 keys[i].type = read_id(fp, 0);
678 keys[i].size = read_id(fp, 0);
681 /******* Part 4: Schemata ********************************************/
684 schemadata = xcalloc(id, sizeof(Id));
685 schemadatap = schemadata;
686 schemadataend = schemadata + id;
687 schemata = xcalloc(numschemata, sizeof(Id));
688 for (i = 0; i < numschemata; i++)
690 schemata[i] = schemadatap - schemadata;
691 schemadatap = read_idarray(fp, numid, 0, schemadatap, schemadataend, 0);
694 /******* Part 5: Info ***********************************************/
695 /* we skip the info for now... */
696 for (i = 0; i < numinfo; i++)
698 unsigned name = idmap[read_id (fp, numid)];
699 unsigned type = read_id (fp, TYPE_ATTR_TYPE_MAX + 1);
700 if (type == TYPE_COUNT_NAMED
701 && !strcmp (id2str (pool, name), "repodata"))
702 parse_repodata (fp, idmap, numid, numrel, repo);
704 skip_item (fp, type, idmap, numid, numrel);
707 /******* Part 6: packed sizes (optional) ****************************/
709 if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
711 exists = xmalloc (numsolv);
712 for (i = 0; i < numsolv; i++)
713 exists[i] = read_id(fp, 0) != 0;
716 /******* Part 7: item data *******************************************/
718 /* calculate idarray size */
720 for (i = 1; i < numkeys; i++)
723 if ((keys[i].type == TYPE_IDARRAY || keys[i].type == TYPE_REL_IDARRAY)
724 && id >= INTERESTED_START && id <= INTERESTED_END)
725 size_idarray += keys[i].size;
728 /* allocate needed space in repo */
731 repo_reserve_ids(repo, 0, size_idarray);
732 idarraydatap = repo->idarraydata + repo->idarraysize;
733 repo->idarraysize += size_idarray;
734 idarraydataend = repo->idarraydata + repo->idarraysize;
744 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
746 if ((solvflags & SOLV_FLAG_VERTICAL) != 0)
748 Id *solvschema = xcalloc(numsolv, sizeof(Id));
749 unsigned char *used = xmalloc(numschemata);
750 Solvable *sstart = s;
753 for (i = 0; i < numsolv; i++)
754 solvschema[i] = read_id(fp, numschemata);
755 for (key = 1; key < numkeys; key++)
758 type = keys[key].type;
759 memset(used, 0, numschemata);
760 for (i = 0; i < numschemata; i++)
762 Id *keyp = schemadata + schemata[i];
770 for (i = 0, s = sstart; i < numsolv; i++, s++)
772 if (!used[solvschema[i]])
777 did = idmap[read_id(fp, numid + numrel)];
778 if (id == SOLVABLE_NAME)
780 else if (id == SOLVABLE_ARCH)
782 else if (id == SOLVABLE_EVR)
784 else if (id == SOLVABLE_VENDOR)
789 if (id == RPM_RPMDBID)
792 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
793 repo->rpmdbid[i] = h;
797 while(read_u8(fp) != 0)
801 case TYPE_REL_IDARRAY:
802 if (id < INTERESTED_START || id > INTERESTED_END)
804 /* not interested in array */
805 while ((read_u8(fp) & 0xc0) != 0)
809 ido = idarraydatap - repo->idarraydata;
810 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend, type == TYPE_REL_IDARRAY);
811 if (id == SOLVABLE_PROVIDES)
813 else if (id == SOLVABLE_OBSOLETES)
815 else if (id == SOLVABLE_CONFLICTS)
817 else if (id == SOLVABLE_REQUIRES)
819 else if (id == SOLVABLE_RECOMMENDS)
821 else if (id == SOLVABLE_SUPPLEMENTS)
822 s->supplements = ido;
823 else if (id == SOLVABLE_SUGGESTS)
825 else if (id == SOLVABLE_ENHANCES)
827 else if (id == SOLVABLE_FRESHENS)
831 case TYPE_ATTR_CHUNK:
832 case TYPE_ATTR_STRING:
833 case TYPE_ATTR_INTLIST:
834 case TYPE_ATTR_LOCALIDS:
836 embedded_store = new_store (pool);
837 add_attr_from_file (embedded_store, i, id, type, idmap, numid, fp);
851 for (i = 0; i < numsolv; i++, s++)
853 if (exists && !exists[i])
855 Id *keyp = schemadata + schemata[read_id(fp, numschemata)];
856 while ((key = *keyp++) != 0)
859 switch (keys[key].type)
862 did = idmap[read_id(fp, numid + numrel)];
863 if (id == SOLVABLE_NAME)
865 else if (id == SOLVABLE_ARCH)
867 else if (id == SOLVABLE_EVR)
869 else if (id == SOLVABLE_VENDOR)
872 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
878 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
880 if (id == RPM_RPMDBID)
883 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
884 repo->rpmdbid[i] = h;
888 while(read_u8(fp) != 0)
892 case TYPE_REL_IDARRAY:
893 if (id < INTERESTED_START || id > INTERESTED_END)
895 /* not interested in array */
896 while ((read_u8(fp) & 0xc0) != 0)
900 ido = idarraydatap - repo->idarraydata;
901 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend, keys[key].type == TYPE_REL_IDARRAY);
902 if (id == SOLVABLE_PROVIDES)
904 else if (id == SOLVABLE_OBSOLETES)
906 else if (id == SOLVABLE_CONFLICTS)
908 else if (id == SOLVABLE_REQUIRES)
910 else if (id == SOLVABLE_RECOMMENDS)
912 else if (id == SOLVABLE_SUPPLEMENTS)
913 s->supplements = ido;
914 else if (id == SOLVABLE_SUGGESTS)
916 else if (id == SOLVABLE_ENHANCES)
918 else if (id == SOLVABLE_FRESHENS)
921 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
922 for (; repo->idarraydata[ido]; ido++)
923 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));
927 case TYPE_ATTR_CHUNK:
928 case TYPE_ATTR_STRING:
929 case TYPE_ATTR_INTLIST:
930 case TYPE_ATTR_LOCALIDS:
932 embedded_store = new_store (pool);
933 add_attr_from_file (embedded_store, i, id, keys[key].type, idmap, numid, fp);
941 attr_store_pack (embedded_store);
942 /* If we have any attributes we also have pages. */
943 read_or_setup_pages (fp, embedded_store);
944 /* The NULL name here means embedded attributes. */
945 repo_add_attrstore (repo, embedded_store, NULL);