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 /*-----------------------------------------------------------------*/
33 /* .solv read functions */
45 for (i = 0; i < 4; i++)
50 fprintf(stderr, "unexpected EOF\n");
70 fprintf(stderr, "unexpected EOF\n");
82 read_id(FILE *fp, Id max)
87 for (i = 0; i < 5; i++)
92 fprintf(stderr, "unexpected EOF\n");
100 fprintf(stderr, "read_id: id too large (%u/%u)\n", x, max);
105 x = (x << 7) ^ c ^ 128;
107 fprintf(stderr, "read_id: id too long\n");
117 read_idarray(FILE *fp, Id max, Id *map, Id *store, Id *end)
126 fprintf(stderr, "unexpected EOF\n");
131 x = (x << 6) | (c & 63);
134 fprintf(stderr, "read_idarray: id too large (%u/%u)\n", x, max);
139 fprintf(stderr, "read_idarray: array overflow\n");
147 fprintf(stderr, "read_idarray: array overflow\n");
156 x = (x << 7) ^ c ^ 128;
161 /*-----------------------------------------------------------------*/
163 typedef struct solvdata {
170 // ----------------------------------------------
173 * read repo from .solv file
178 pool_addrepo_solv(Pool *pool, FILE *fp, const char *reponame)
181 unsigned int numid, numrel, numsolv, numsrcdata, numsolvdata;
182 int numsolvdatabits, type;
184 Offset *str; /* map Id -> Offset into string space */
185 char *strsp; /* repo string space */
186 char *sp; /* pointer into string space */
187 Id *idmap; /* map of repo Ids to pool Ids */
189 unsigned int hashmask, h;
196 unsigned int size, size_str, size_idarray;
198 Id *idarraydatap, *idarraydataend;
200 unsigned int databits;
203 if (read_u32(fp) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
205 fprintf(stderr, "not a SOLV file\n");
208 if (read_u32(fp) != SOLV_VERSION)
210 fprintf(stderr, "unsupported SOLV version\n");
214 /* create empty Repo */
215 repo = pool_addrepo_empty(pool);
216 pool_freeidhashes(pool);
218 repo->name = reponame;
220 numid = read_u32(fp);
221 numrel = read_u32(fp);
222 numsolv= read_u32(fp);
224 sizeid = read_u32(fp); /* size of string+Id space */
227 * read strings and Ids
235 /* alloc string buffer */
236 strsp = (char *)xrealloc(pool->stringspace, pool->sstrings + sizeid + 1);
237 /* alloc string offsets (Id -> Offset into string space) */
238 str = (Offset *)xrealloc(pool->strings, (pool->nstrings + numid) * sizeof(Offset));
240 pool->stringspace = strsp;
241 pool->strings = str; /* array of offsets into strsp, indexed by Id */
243 /* point to _BEHIND_ already allocated string/Id space */
244 strsp += pool->sstrings;
246 /* alloc id map for name and rel Ids */
247 idmap = (Id *)xcalloc(numid + numrel, sizeof(Id));
250 * read new repo at end of pool
253 if (fread(strsp, sizeid, 1, fp) != 1)
255 fprintf(stderr, "read error while reading strings\n");
258 strsp[sizeid] = 0; /* make string space \0 terminated */
262 * build hashes for all read strings
266 hashmask = mkmask(pool->nstrings + numid);
269 printf("read %d strings\n", numid);
270 printf("string hash buckets: %d\n", hashmask + 1);
274 * ensure sufficient hash size
277 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
280 * fill hashtable with strings already in pool
283 for (i = 1; i < pool->nstrings; i++) /* leave out our dummy zero id */
285 h = strhash(pool->stringspace + pool->strings[i]) & hashmask;
286 hh = HASHCHAIN_START;
288 h = HASHCHAIN_NEXT(h, hh, hashmask);
293 * run over string space, calculate offsets
295 * build id map (maps solv Id -> pool Id)
298 for (i = 1; i < numid; i++)
300 if (sp >= strsp + sizeid)
302 fprintf(stderr, "not enough strings\n");
305 if (!*sp) /* empty string */
313 h = strhash(sp) & hashmask;
314 hh = HASHCHAIN_START;
320 if (!strcmp(pool->stringspace + pool->strings[id], sp))
321 break; /* existing string */
322 h = HASHCHAIN_NEXT(h, hh, hashmask);
325 /* length == offset to next string */
327 if (id == ID_NULL) /* end of hash chain -> new string */
329 id = pool->nstrings++;
331 str[id] = pool->sstrings; /* save Offset */
332 if (sp != pool->stringspace + pool->sstrings) /* not at end-of-buffer */
333 memmove(pool->stringspace + pool->sstrings, sp, l); /* append to pool buffer */
336 idmap[i] = id; /* repo relative -> pool relative */
337 sp += l; /* next string */
340 pool_shrink_strings(pool); /* vacuum */
351 ran = (Reldep *)xrealloc(pool->rels, (pool->nrels + numrel) * sizeof(Reldep));
354 fprintf(stderr, "no mem for rel space\n");
357 pool->rels = ran; /* extended rel space */
359 hashmask = mkmask(pool->nrels + numrel);
361 printf("read %d rels\n", numrel);
362 printf("rel hash buckets: %d\n", hashmask + 1);
365 * prep hash table with already existing RelDeps
368 hashtbl = (Id *)xcalloc(hashmask + 1, sizeof(Id));
369 for (i = 1; i < pool->nrels; i++)
371 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
372 hh = HASHCHAIN_START;
374 h = HASHCHAIN_NEXT(h, hh, hashmask);
379 * read RelDeps from repo
382 for (i = 0; i < numrel; i++)
384 name = read_id(fp, i + numid); /* read (repo relative) Ids */
385 evr = read_id(fp, i + numid);
387 name = idmap[name]; /* map to (pool relative) Ids */
389 h = relhash(name, evr, flags) & hashmask;
390 hh = HASHCHAIN_START;
394 if (id == ID_NULL) /* end of hash chain */
396 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
398 h = HASHCHAIN_NEXT(h, hh, hashmask);
400 if (id == ID_NULL) /* new RelDep */
406 ran[id].flags = flags;
408 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
411 pool_shrink_rels(pool); /* vacuum */
415 * read (but dont store) repo data
419 printf("read repo data\n");
421 numsrcdata = read_u32(fp);
422 for (i = 0; i < numsrcdata; i++)
425 id = idmap[read_id(fp, numid)];
429 read_id(fp, numid + numrel); /* just check Id */
435 while(read_u8(fp) != 0)
439 fprintf(stderr, "unknown type\n");
450 printf("read solvable data info\n");
452 numsolvdata = read_u32(fp);
454 solvdata = (SolvData *)xmalloc(numsolvdata * sizeof(SolvData));
458 for (i = 0; i < numsolvdata; i++)
461 solvdata[i].type = type;
462 if ((type & TYPE_BITMAP) != 0)
467 id = idmap[read_id(fp, numid)];
469 printf("#%d: %s\n", i, id2str(pool, id));
473 solvdata[i].size = size;
474 if (id >= INTERESTED_START && id <= INTERESTED_END)
476 if (type == TYPE_STR)
478 if (type == TYPE_IDARRAY)
479 size_idarray += size;
483 if (numsolvdatabits >= 32)
485 fprintf(stderr, "too many data map bits\n");
490 size_idarray++; /* first entry is always zero */
491 repo->idarraydata = (Id *)xmalloc(sizeof(Id) * size_idarray);
492 repo->idarraysize = size_idarray;
493 idarraydatap = repo->idarraydata;
495 idarraydataend = repo->idarraydata + size_idarray;
499 repo->idarraydata = 0;
500 repo->idarraysize = 0;
505 /* alloc solvables */
506 pool->solvables = (Solvable *)xrealloc(pool->solvables, (pool->nsolvables + numsolv) * sizeof(Solvable));
508 if (numsolv) /* clear newly allocated area */
509 memset(pool->solvables + pool->nsolvables, 0, numsolv * sizeof(Solvable));
510 repo->start = pool->nsolvables;
511 repo->nsolvables = numsolv;
518 printf("read solvables\n");
520 for (i = 0, s = pool->solvables + repo->start; i < numsolv; i++, s++)
526 for (j = 0; j < (numsolvdatabits + 7) >> 3; j++)
527 databits = (databits << 8) | read_u8(fp);
529 for (j = 0; j < numsolvdata; j++)
531 type = solvdata[j].type;
532 if ((type & TYPE_BITMAP) != 0)
546 did = idmap[read_id(fp, numid + numrel)];
547 if (id == SOLVABLE_NAME)
549 else if (id == SOLVABLE_ARCH)
551 else if (id == SOLVABLE_EVR)
553 else if (id == SOLVABLE_VENDOR)
556 printf("%s -> %s\n", id2str(pool, id), id2str(pool, did));
562 printf("%s -> %u\n", id2str(pool, id), h);
564 if (id == RPM_RPMDBID)
567 repo->rpmdbid = (Id *)xcalloc(numsolv, sizeof(Id));
568 repo->rpmdbid[i] = h;
572 while(read_u8(fp) != 0)
576 if (id < INTERESTED_START || id > INTERESTED_END)
578 /* not interested in array */
579 while ((read_u8(fp) & 0xc0) != 0)
583 ido = idarraydatap - repo->idarraydata;
584 idarraydatap = read_idarray(fp, numid + numrel, idmap, idarraydatap, idarraydataend);
585 if (id == SOLVABLE_PROVIDES)
587 else if (id == SOLVABLE_OBSOLETES)
589 else if (id == SOLVABLE_CONFLICTS)
591 else if (id == SOLVABLE_REQUIRES)
593 else if (id == SOLVABLE_RECOMMENDS)
595 else if (id == SOLVABLE_SUPPLEMENTS)
596 s->supplements = ido;
597 else if (id == SOLVABLE_SUGGESTS)
599 else if (id == SOLVABLE_ENHANCES)
601 else if (id == SOLVABLE_FRESHENS)
604 printf("%s ->\n", id2str(pool, id));
605 for (; repo->idarraydata[ido]; ido++)
606 printf(" %s\n", dep2str(pool, repo->idarraydata[ido]));
616 pool->nsolvables += numsolv;