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 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)
128 pool_debug(mypool, SAT_FATAL, "unexpected EOF\n");
133 x = (x << 6) | (c & 63);
136 pool_debug(mypool, SAT_FATAL, "read_idarray: id too large (%u/%u)\n", x, max);
141 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
151 pool_debug(mypool, SAT_FATAL, "read_idarray: array overflow\n");
160 x = (x << 7) ^ c ^ 128;
165 /*-----------------------------------------------------------------*/
174 // ----------------------------------------------
177 * read repo from .solv file
182 repo_add_solv(Repo *repo, FILE *fp)
184 Pool *pool = repo->pool;
186 unsigned int numid, numrel, numsolv;
187 unsigned int numkeys, numschemata, numinfo;
191 Offset *str; /* map Id -> Offset into string space */
192 char *strsp; /* repo string space */
193 char *sp; /* pointer into string space */
194 Id *idmap; /* map of repo Ids to pool Ids */
196 unsigned int hashmask, h;
202 unsigned int size_idarray;
203 Id *idarraydatap, *idarraydataend;
206 unsigned int solvflags;
208 Id *schemadata, *schemadatap, *schemadataend;
213 if (read_u32(fp) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
215 pool_debug(pool, SAT_FATAL, "not a SOLV file\n");
218 if (read_u32(fp) != SOLV_VERSION_1)
220 pool_debug(pool, SAT_FATAL, "unsupported SOLV version\n");
224 pool_freeidhashes(pool);
226 numid = read_u32(fp);
227 numrel = read_u32(fp);
228 numsolv = read_u32(fp);
229 numkeys = read_u32(fp);
230 numschemata = read_u32(fp);
231 numinfo = read_u32(fp);
232 solvflags = read_u32(fp);
234 /******* Part 1: string IDs *****************************************/
236 sizeid = read_u32(fp); /* size of string+Id space */
239 * read strings and Ids
248 /* alloc string buffer */
249 strsp = (char *)xrealloc(pool->ss.stringspace, pool->ss.sstrings + sizeid + 1);
250 /* alloc string offsets (Id -> Offset into string space) */
251 str = (Offset *)xrealloc(pool->ss.strings, (pool->ss.nstrings + numid) * sizeof(Offset));
253 pool->ss.stringspace = strsp;
254 pool->ss.strings = str; /* array of offsets into strsp, indexed by Id */
256 /* point to _BEHIND_ already allocated string/Id space */
257 strsp += pool->ss.sstrings;
259 /* alloc id map for name and rel Ids. this maps ids in the solv files
260 * to the ids in our pool */
261 idmap = (Id *)xcalloc(numid + numrel, sizeof(Id));
264 * read new repo at end of pool
267 if (fread(strsp, sizeid, 1, fp) != 1)
269 pool_debug(pool, SAT_FATAL, "read error while reading strings\n");
272 strsp[sizeid] = 0; /* make string space \0 terminated */
276 * build hashes for all read strings
280 hashmask = mkmask(pool->ss.nstrings + numid);
283 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
284 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
288 * ensure sufficient hash size
291 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
294 * fill hashtable with strings already in pool
297 for (i = 1; i < pool->ss.nstrings; i++) /* leave out our dummy zero id */
299 h = strhash(pool->ss.stringspace + pool->ss.strings[i]) & hashmask;
300 hh = HASHCHAIN_START;
302 h = HASHCHAIN_NEXT(h, hh, hashmask);
307 * run over string space, calculate offsets
309 * build id map (maps solv Id -> pool Id)
312 for (i = 1; i < numid; i++)
314 if (sp >= strsp + sizeid)
316 pool_debug(pool, SAT_FATAL, "not enough strings\n");
319 if (!*sp) /* empty string */
327 h = strhash(sp) & hashmask;
328 hh = HASHCHAIN_START;
334 if (!strcmp(pool->ss.stringspace + pool->ss.strings[id], sp))
335 break; /* existing string */
336 h = HASHCHAIN_NEXT(h, hh, hashmask);
339 /* length == offset to next string */
341 if (id == ID_NULL) /* end of hash chain -> new string */
343 id = pool->ss.nstrings++;
345 str[id] = pool->ss.sstrings; /* save Offset */
346 if (sp != pool->ss.stringspace + pool->ss.sstrings) /* not at end-of-buffer */
347 memmove(pool->ss.stringspace + pool->ss.sstrings, sp, l); /* append to pool buffer */
348 pool->ss.sstrings += l;
350 idmap[i] = id; /* repo relative -> pool relative */
351 sp += l; /* next string */
354 pool_shrink_strings(pool); /* vacuum */
357 /******* Part 2: Relation IDs ***************************************/
367 ran = (Reldep *)xrealloc(pool->rels, (pool->nrels + numrel) * sizeof(Reldep));
370 pool_debug(pool, SAT_FATAL, "no mem for rel space\n");
373 pool->rels = ran; /* extended rel space */
375 hashmask = mkmask(pool->nrels + numrel);
377 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
378 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
381 * prep hash table with already existing RelDeps
384 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
385 for (i = 1; i < pool->nrels; i++)
387 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
388 hh = HASHCHAIN_START;
390 h = HASHCHAIN_NEXT(h, hh, hashmask);
395 * read RelDeps from repo
398 for (i = 0; i < numrel; i++)
400 name = read_id(fp, i + numid); /* read (repo relative) Ids */
401 evr = read_id(fp, i + numid);
403 name = idmap[name]; /* map to (pool relative) Ids */
405 h = relhash(name, evr, flags) & hashmask;
406 hh = HASHCHAIN_START;
410 if (id == ID_NULL) /* end of hash chain */
412 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
414 h = HASHCHAIN_NEXT(h, hh, hashmask);
416 if (id == ID_NULL) /* new RelDep */
422 ran[id].flags = flags;
424 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
427 pool_shrink_rels(pool); /* vacuum */
431 /******* Part 3: Keys ***********************************************/
433 keys = xcalloc(numkeys, sizeof(*keys));
434 /* keys start at 1 */
435 for (i = 1; i < numkeys; i++)
437 keys[i].name = idmap[read_id(fp, numid)];
438 keys[i].type = read_id(fp, 0);
439 keys[i].size = read_id(fp, 0);
442 /******* Part 4: Schemata ********************************************/
445 schemadata = xcalloc(id, sizeof(Id));
446 schemadatap = schemadata;
447 schemadataend = schemadata + id;
448 schemata = xcalloc(numschemata, sizeof(Id));
449 for (i = 0; i < numschemata; i++)
451 schemata[i] = schemadatap - schemadata;
452 schemadatap = read_idarray(fp, numid, 0, schemadatap, schemadataend);
455 /******* Part 5: Info ***********************************************/
456 /* we skip the info for now... */
457 for (i = 0; i < numinfo; i++)
459 Id *schema = schemadata + schemata[read_id(fp, numschemata)];
460 while ((key = *schema++) != 0)
462 type = keys[key].type;
466 read_id(fp, numid + numrel); /* just check Id */
472 while(read_u8(fp) != 0)
476 while ((read_u8(fp) & 0xc0) != 0)
480 pool_debug(pool, SAT_FATAL, "unknown type %d\n", type);
486 /******* Part 6: packed sizes (optional) ****************************/
487 if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
489 for (i = 0; i < numsolv; i++)
493 /******* Part 7: item data *******************************************/
495 /* calculate idarray size */
497 for (i = 1; i < numkeys; i++)
500 if (keys[i].type == TYPE_IDARRAY && id >= INTERESTED_START && id <= INTERESTED_END)
501 size_idarray += keys[i].size;
504 /* allocate needed space in repo */
507 repo_reserve_ids(repo, 0, size_idarray);
508 idarraydatap = repo->idarraydata + repo->idarraysize;
509 repo->idarraysize += size_idarray;
510 idarraydataend = repo->idarraydata + repo->idarraysize;
520 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
522 if ((solvflags & SOLV_FLAG_VERTICAL) != 0)
524 Id *solvschema = xcalloc(numsolv, sizeof(Id));
525 unsigned char *used = xmalloc(numschemata);
526 Solvable *sstart = s;
529 for (i = 0; i < numsolv; i++)
530 solvschema[i] = read_id(fp, numschemata);
531 for (key = 1; key < numkeys; key++)
534 type = keys[key].type;
535 memset(used, 0, numschemata);
536 for (i = 0; i < numschemata; i++)
538 Id *keyp = schemadata + schemata[i];
546 for (i = 0, s = sstart; i < numsolv; i++, s++)
548 if (!used[solvschema[i]])
553 did = idmap[read_id(fp, numid + numrel)];
554 if (id == SOLVABLE_NAME)
556 else if (id == SOLVABLE_ARCH)
558 else if (id == SOLVABLE_EVR)
560 else if (id == SOLVABLE_VENDOR)
565 if (id == RPM_RPMDBID)
568 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
569 repo->rpmdbid[i] = h;
573 while(read_u8(fp) != 0)
577 if (id < INTERESTED_START || id > INTERESTED_END)
579 /* not interested in array */
580 while ((read_u8(fp) & 0xc0) != 0)
584 ido = idarraydatap - repo->idarraydata;
585 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend);
586 if (id == SOLVABLE_PROVIDES)
588 else if (id == SOLVABLE_OBSOLETES)
590 else if (id == SOLVABLE_CONFLICTS)
592 else if (id == SOLVABLE_REQUIRES)
594 else if (id == SOLVABLE_RECOMMENDS)
596 else if (id == SOLVABLE_SUPPLEMENTS)
597 s->supplements = ido;
598 else if (id == SOLVABLE_SUGGESTS)
600 else if (id == SOLVABLE_ENHANCES)
602 else if (id == SOLVABLE_FRESHENS)
617 for (i = 0; i < numsolv; i++, s++)
619 Id *keyp = schemadata + schemata[read_id(fp, numschemata)];
620 while ((key = *keyp++) != 0)
623 switch (keys[key].type)
626 did = idmap[read_id(fp, numid + numrel)];
627 if (id == SOLVABLE_NAME)
629 else if (id == SOLVABLE_ARCH)
631 else if (id == SOLVABLE_EVR)
633 else if (id == SOLVABLE_VENDOR)
636 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
642 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
644 if (id == RPM_RPMDBID)
647 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
648 repo->rpmdbid[i] = h;
652 while(read_u8(fp) != 0)
656 if (id < INTERESTED_START || id > INTERESTED_END)
658 /* not interested in array */
659 while ((read_u8(fp) & 0xc0) != 0)
663 ido = idarraydatap - repo->idarraydata;
664 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend);
665 if (id == SOLVABLE_PROVIDES)
667 else if (id == SOLVABLE_OBSOLETES)
669 else if (id == SOLVABLE_CONFLICTS)
671 else if (id == SOLVABLE_REQUIRES)
673 else if (id == SOLVABLE_RECOMMENDS)
675 else if (id == SOLVABLE_SUPPLEMENTS)
676 s->supplements = ido;
677 else if (id == SOLVABLE_SUGGESTS)
679 else if (id == SOLVABLE_ENHANCES)
681 else if (id == SOLVABLE_FRESHENS)
684 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
685 for (; repo->idarraydata[ido]; ido++)
686 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));