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"
29 #define INTERESTED_START SOLVABLE_NAME
30 #define INTERESTED_END SOLVABLE_FRESHENS
32 static Pool *mypool; /* for pool_debug... */
34 /*-----------------------------------------------------------------*/
35 /* .solv read functions */
47 for (i = 0; i < 4; i++)
52 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
72 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
84 read_id(FILE *fp, Id max)
89 for (i = 0; i < 5; i++)
94 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
102 pool_debug(mypool, SAT_FATAL, "read_id: id too large (%u/%u)\n", x, max);
107 x = (x << 7) ^ c ^ 128;
109 pool_debug(mypool, SAT_FATAL, "read_id: id too long\n");
119 read_idarray(FILE *fp, Id max, Id *map, Id *store, Id *end, int relative)
129 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
134 x = (x << 6) | (c & 63);
137 if (x == 0 && c == 0x40)
142 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
145 *store++ = SOLVABLE_PREREQMARKER;
155 pool_debug(mypool, SAT_FATAL, "read_idarray: id too large (%u/%u)\n", x, max);
162 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
168 if (x == 0) /* already have trailing zero? */
172 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
181 x = (x << 7) ^ c ^ 128;
186 /*-----------------------------------------------------------------*/
195 // ----------------------------------------------
198 * read repo from .solv file
203 repo_add_solv(Repo *repo, FILE *fp)
205 Pool *pool = repo->pool;
207 unsigned int numid, numrel, numsolv;
208 unsigned int numkeys, numschemata, numinfo;
212 Offset *str; /* map Id -> Offset into string space */
213 char *strsp; /* repo string space */
214 char *sp; /* pointer into string space */
215 Id *idmap; /* map of repo Ids to pool Ids */
217 unsigned int hashmask, h;
223 unsigned int size_idarray;
224 Id *idarraydatap, *idarraydataend;
227 unsigned int solvflags;
228 unsigned int solvversion;
230 Id *schemadata, *schemadatap, *schemadataend;
235 if (read_u32(fp) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
237 pool_debug(pool, SAT_FATAL, "not a SOLV file\n");
240 solvversion = read_u32(fp);
247 pool_debug(pool, SAT_FATAL, "unsupported SOLV version\n");
251 pool_freeidhashes(pool);
253 numid = read_u32(fp);
254 numrel = read_u32(fp);
255 numsolv = read_u32(fp);
256 numkeys = read_u32(fp);
257 numschemata = read_u32(fp);
258 numinfo = read_u32(fp);
259 solvflags = read_u32(fp);
261 /******* Part 1: string IDs *****************************************/
263 sizeid = read_u32(fp); /* size of string+Id space */
266 * read strings and Ids
275 /* alloc string buffer */
276 strsp = (char *)xrealloc(pool->ss.stringspace, pool->ss.sstrings + sizeid + 1);
277 /* alloc string offsets (Id -> Offset into string space) */
278 str = (Offset *)xrealloc(pool->ss.strings, (pool->ss.nstrings + numid) * sizeof(Offset));
280 pool->ss.stringspace = strsp;
281 pool->ss.strings = str; /* array of offsets into strsp, indexed by Id */
283 /* point to _BEHIND_ already allocated string/Id space */
284 strsp += pool->ss.sstrings;
286 /* alloc id map for name and rel Ids. this maps ids in the solv files
287 * to the ids in our pool */
288 idmap = (Id *)xcalloc(numid + numrel, sizeof(Id));
291 * read new repo at end of pool
294 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
296 if (fread(strsp, sizeid, 1, fp) != 1)
298 pool_debug(pool, SAT_FATAL, "read error while reading strings\n");
304 unsigned int pfsize = read_u32 (fp);
305 char *prefix = xmalloc (pfsize);
309 if (fread (prefix, pfsize, 1, fp) != 1)
311 pool_debug(pool, SAT_FATAL, "read error while reading strings\n");
314 for (i = 1; i < numid; i++)
316 int same = (unsigned char)*pp++;
317 size_t len = strlen (pp) + 1;
319 memcpy (dest, old_str, same);
320 memcpy (dest + same, pp, len);
327 strsp[sizeid] = 0; /* make string space \0 terminated */
331 * build hashes for all read strings
335 hashmask = mkmask(pool->ss.nstrings + numid);
338 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
339 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
343 * ensure sufficient hash size
346 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
349 * fill hashtable with strings already in pool
352 for (i = 1; i < pool->ss.nstrings; i++) /* leave out our dummy zero id */
354 h = strhash(pool->ss.stringspace + pool->ss.strings[i]) & hashmask;
355 hh = HASHCHAIN_START;
357 h = HASHCHAIN_NEXT(h, hh, hashmask);
362 * run over string space, calculate offsets
364 * build id map (maps solv Id -> pool Id)
367 for (i = 1; i < numid; i++)
369 if (sp >= strsp + sizeid)
371 pool_debug(pool, SAT_FATAL, "not enough strings\n");
374 if (!*sp) /* empty string */
382 h = strhash(sp) & hashmask;
383 hh = HASHCHAIN_START;
389 if (!strcmp(pool->ss.stringspace + pool->ss.strings[id], sp))
390 break; /* existing string */
391 h = HASHCHAIN_NEXT(h, hh, hashmask);
394 /* length == offset to next string */
396 if (id == ID_NULL) /* end of hash chain -> new string */
398 id = pool->ss.nstrings++;
400 str[id] = pool->ss.sstrings; /* save Offset */
401 if (sp != pool->ss.stringspace + pool->ss.sstrings) /* not at end-of-buffer */
402 memmove(pool->ss.stringspace + pool->ss.sstrings, sp, l); /* append to pool buffer */
403 pool->ss.sstrings += l;
405 idmap[i] = id; /* repo relative -> pool relative */
406 sp += l; /* next string */
409 pool_shrink_strings(pool); /* vacuum */
412 /******* Part 2: Relation IDs ***************************************/
422 ran = (Reldep *)xrealloc(pool->rels, (pool->nrels + numrel) * sizeof(Reldep));
425 pool_debug(pool, SAT_FATAL, "no mem for rel space\n");
428 pool->rels = ran; /* extended rel space */
430 hashmask = mkmask(pool->nrels + numrel);
432 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
433 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
436 * prep hash table with already existing RelDeps
439 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
440 for (i = 1; i < pool->nrels; i++)
442 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
443 hh = HASHCHAIN_START;
445 h = HASHCHAIN_NEXT(h, hh, hashmask);
450 * read RelDeps from repo
453 for (i = 0; i < numrel; i++)
455 name = read_id(fp, i + numid); /* read (repo relative) Ids */
456 evr = read_id(fp, i + numid);
458 name = idmap[name]; /* map to (pool relative) Ids */
460 h = relhash(name, evr, flags) & hashmask;
461 hh = HASHCHAIN_START;
465 if (id == ID_NULL) /* end of hash chain */
467 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
469 h = HASHCHAIN_NEXT(h, hh, hashmask);
471 if (id == ID_NULL) /* new RelDep */
477 ran[id].flags = flags;
479 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
482 pool_shrink_rels(pool); /* vacuum */
486 /******* Part 3: Keys ***********************************************/
488 keys = xcalloc(numkeys, sizeof(*keys));
489 /* keys start at 1 */
490 for (i = 1; i < numkeys; i++)
492 keys[i].name = idmap[read_id(fp, numid)];
493 keys[i].type = read_id(fp, 0);
494 keys[i].size = read_id(fp, 0);
497 /******* Part 4: Schemata ********************************************/
500 schemadata = xcalloc(id, sizeof(Id));
501 schemadatap = schemadata;
502 schemadataend = schemadata + id;
503 schemata = xcalloc(numschemata, sizeof(Id));
504 for (i = 0; i < numschemata; i++)
506 schemata[i] = schemadatap - schemadata;
507 schemadatap = read_idarray(fp, numid, 0, schemadatap, schemadataend, 0);
510 /******* Part 5: Info ***********************************************/
511 /* we skip the info for now... */
512 for (i = 0; i < numinfo; i++)
514 Id *schema = schemadata + schemata[read_id(fp, numschemata)];
515 while ((key = *schema++) != 0)
517 type = keys[key].type;
521 read_id(fp, numid + numrel); /* just check Id */
527 while(read_u8(fp) != 0)
531 case TYPE_REL_IDARRAY:
532 while ((read_u8(fp) & 0xc0) != 0)
536 pool_debug(pool, SAT_FATAL, "unknown type %d\n", type);
542 /******* Part 6: packed sizes (optional) ****************************/
543 if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
545 for (i = 0; i < numsolv; i++)
549 /******* Part 7: item data *******************************************/
551 /* calculate idarray size */
553 for (i = 1; i < numkeys; i++)
556 if ((keys[i].type == TYPE_IDARRAY || keys[i].type == TYPE_REL_IDARRAY)
557 && id >= INTERESTED_START && id <= INTERESTED_END)
558 size_idarray += keys[i].size;
561 /* allocate needed space in repo */
564 repo_reserve_ids(repo, 0, size_idarray);
565 idarraydatap = repo->idarraydata + repo->idarraysize;
566 repo->idarraysize += size_idarray;
567 idarraydataend = repo->idarraydata + repo->idarraysize;
577 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
579 if ((solvflags & SOLV_FLAG_VERTICAL) != 0)
581 Id *solvschema = xcalloc(numsolv, sizeof(Id));
582 unsigned char *used = xmalloc(numschemata);
583 Solvable *sstart = s;
586 for (i = 0; i < numsolv; i++)
587 solvschema[i] = read_id(fp, numschemata);
588 for (key = 1; key < numkeys; key++)
591 type = keys[key].type;
592 memset(used, 0, numschemata);
593 for (i = 0; i < numschemata; i++)
595 Id *keyp = schemadata + schemata[i];
603 for (i = 0, s = sstart; i < numsolv; i++, s++)
605 if (!used[solvschema[i]])
610 did = idmap[read_id(fp, numid + numrel)];
611 if (id == SOLVABLE_NAME)
613 else if (id == SOLVABLE_ARCH)
615 else if (id == SOLVABLE_EVR)
617 else if (id == SOLVABLE_VENDOR)
622 if (id == RPM_RPMDBID)
625 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
626 repo->rpmdbid[i] = h;
630 while(read_u8(fp) != 0)
634 case TYPE_REL_IDARRAY:
635 if (id < INTERESTED_START || id > INTERESTED_END)
637 /* not interested in array */
638 while ((read_u8(fp) & 0xc0) != 0)
642 ido = idarraydatap - repo->idarraydata;
643 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend, type == TYPE_REL_IDARRAY);
644 if (id == SOLVABLE_PROVIDES)
646 else if (id == SOLVABLE_OBSOLETES)
648 else if (id == SOLVABLE_CONFLICTS)
650 else if (id == SOLVABLE_REQUIRES)
652 else if (id == SOLVABLE_RECOMMENDS)
654 else if (id == SOLVABLE_SUPPLEMENTS)
655 s->supplements = ido;
656 else if (id == SOLVABLE_SUGGESTS)
658 else if (id == SOLVABLE_ENHANCES)
660 else if (id == SOLVABLE_FRESHENS)
675 for (i = 0; i < numsolv; i++, s++)
677 Id *keyp = schemadata + schemata[read_id(fp, numschemata)];
678 while ((key = *keyp++) != 0)
681 switch (keys[key].type)
684 did = idmap[read_id(fp, numid + numrel)];
685 if (id == SOLVABLE_NAME)
687 else if (id == SOLVABLE_ARCH)
689 else if (id == SOLVABLE_EVR)
691 else if (id == SOLVABLE_VENDOR)
694 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
700 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
702 if (id == RPM_RPMDBID)
705 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
706 repo->rpmdbid[i] = h;
710 while(read_u8(fp) != 0)
714 case TYPE_REL_IDARRAY:
715 if (id < INTERESTED_START || id > INTERESTED_END)
717 /* not interested in array */
718 while ((read_u8(fp) & 0xc0) != 0)
722 ido = idarraydatap - repo->idarraydata;
723 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend, keys[key].type == TYPE_REL_IDARRAY);
724 if (id == SOLVABLE_PROVIDES)
726 else if (id == SOLVABLE_OBSOLETES)
728 else if (id == SOLVABLE_CONFLICTS)
730 else if (id == SOLVABLE_REQUIRES)
732 else if (id == SOLVABLE_RECOMMENDS)
734 else if (id == SOLVABLE_SUPPLEMENTS)
735 s->supplements = ido;
736 else if (id == SOLVABLE_SUGGESTS)
738 else if (id == SOLVABLE_ENHANCES)
740 else if (id == SOLVABLE_FRESHENS)
743 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
744 for (; repo->idarraydata[ido]; ido++)
745 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));