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 #define SOLV_ERROR_NOT_SOLV 1
34 #define SOLV_ERROR_UNSUPPORTED 2
35 #define SOLV_ERROR_EOF 3
36 #define SOLV_ERROR_ID_RANGE 4
37 #define SOLV_ERROR_OVERFLOW 5
38 #define SOLV_ERROR_CORRUPT 6
40 static Pool *mypool; /* for pool_debug... */
42 /*-----------------------------------------------------------------*/
43 /* .solv read functions */
50 read_u32(Repodata *data)
57 for (i = 0; i < 4; i++)
62 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
63 data->error = SOLV_ERROR_EOF;
77 read_u8(Repodata *data)
86 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
87 data->error = SOLV_ERROR_EOF;
99 read_id(Repodata *data, Id max)
106 for (i = 0; i < 5; i++)
111 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
112 data->error = SOLV_ERROR_EOF;
120 pool_debug(mypool, SAT_ERROR, "read_id: id too large (%u/%u)\n", x, max);
121 data->error = SOLV_ERROR_ID_RANGE;
126 x = (x << 7) ^ c ^ 128;
128 pool_debug(mypool, SAT_ERROR, "read_id: id too long\n");
129 data->error = SOLV_ERROR_CORRUPT;
139 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, int relative)
152 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
153 data->error = SOLV_ERROR_EOF;
158 x = (x << 6) | (c & 63);
161 if (x == 0 && c == 0x40)
166 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
167 data->error = SOLV_ERROR_OVERFLOW;
170 *store++ = SOLVABLE_PREREQMARKER;
180 pool_debug(mypool, SAT_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
181 data->error = SOLV_ERROR_ID_RANGE;
188 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
194 if (x == 0) /* already have trailing zero? */
198 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
199 data->error = SOLV_ERROR_OVERFLOW;
208 x = (x << 7) ^ c ^ 128;
213 read_str(Repodata *data, char **inbuf, unsigned *len)
215 unsigned char *buf = (unsigned char*)*inbuf;
218 buf = sat_malloc(1024);
223 while((c = getc(data->fp)) != 0)
227 pool_debug (mypool, SAT_ERROR, "unexpected EOF\n");
228 data->error = SOLV_ERROR_EOF;
231 /* Plus 1 as we also want to add the 0. */
235 /* Don't realloc on the inbuf, it might be on the stack. */
236 if (buf == (unsigned char*)*inbuf)
238 buf = sat_malloc(*len);
239 memcpy(buf, *inbuf, *len - 256);
242 buf = sat_realloc(buf, *len);
251 skip_item (Repodata *data, unsigned type, unsigned numid, unsigned numrel)
258 read_id(data, numid + numrel); /* just check Id */
261 read_id(data, numid + data->ndirs); /* just check Id */
266 case TYPE_ATTR_STRING:
268 while(read_u8(data) != 0)
272 case TYPE_IDVALUEARRAY:
273 case TYPE_DIRVALUEVALUEARRAY:
274 case TYPE_REL_IDARRAY:
275 case TYPE_ATTR_INTLIST:
276 while ((read_u8(data) & 0xc0) != 0)
279 case TYPE_COUNT_NAMED:
281 unsigned count = read_id(data, 0);
284 read_id(data, numid); /* Name */
285 unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
286 skip_item(data, t, numid, numrel);
292 unsigned count = read_id(data, 0);
293 unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
295 skip_item(data, t, numid, numrel);
298 case TYPE_ATTR_CHUNK:
304 case TYPE_ATTR_LOCALIDS:
305 while (read_id(data, 0) != 0)
309 pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
310 data->error = SOLV_ERROR_CORRUPT;
316 key_cmp (const void *pa, const void *pb)
318 Repokey *a = (Repokey *)pa;
319 Repokey *b = (Repokey *)pb;
320 return a->name - b->name;
324 parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
331 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
332 data = repo->repodata + repo->nrepodata++;
334 memset(data, 0, sizeof(*data));
336 while ((key = *keyp++) != 0)
339 switch (keys[key].type)
341 case TYPE_IDVALUEARRAY:
342 if (id != REPODATA_KEYS)
344 skip_item(maindata, TYPE_IDVALUEARRAY, numid, numrel);
347 ida = sat_calloc(keys[key].size, sizeof(Id));
348 ide = read_idarray(maindata, 0, 0, ida, ida + keys[key].size, 0);
352 data->error = SOLV_ERROR_CORRUPT;
355 data->nkeys = 1 + (n >> 1);
356 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
357 memset(data->keys, 0, sizeof(Repokey));
358 for (i = 1, ide = ida; i < data->nkeys; i++)
362 data->error = SOLV_ERROR_CORRUPT;
363 pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
366 data->keys[i].name = idmap ? idmap[*ide++] : *ide++;
367 data->keys[i].type = *ide++;
368 data->keys[i].size = 0;
369 data->keys[i].storage = 0;
373 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
376 if (id != REPODATA_LOCATION)
377 skip_item(maindata, TYPE_STR, numid, numrel);
381 unsigned len = sizeof (buf);
382 char *filename = buf;
383 read_str(maindata, &filename, &len);
384 data->location = strdup(filename);
390 skip_item(maindata, keys[key].type, numid, numrel);
396 /*-----------------------------------------------------------------*/
400 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
403 while ((key = *keyp++) != 0)
404 skip_item(data, keys[key].type, numid, numrel);
407 /*-----------------------------------------------------------------*/
410 incore_add_id(Repodata *data, Id x)
413 /* make sure we have at least 5 bytes free */
414 if (data->incoredatafree < 5)
416 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
417 data->incoredatafree = 1024;
419 dp = data->incoredata + data->incoredatalen;
425 *dp++ = (x >> 28) | 128;
427 *dp++ = (x >> 21) | 128;
428 *dp++ = (x >> 14) | 128;
431 *dp++ = (x >> 7) | 128;
433 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
434 data->incoredatalen = dp - data->incoredata;
438 incore_add_u32(Repodata *data, unsigned int x)
441 /* make sure we have at least 4 bytes free */
442 if (data->incoredatafree < 4)
444 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
445 data->incoredatafree = 1024;
447 dp = data->incoredata + data->incoredatalen;
452 data->incoredatafree -= 4;
453 data->incoredatalen += 4;
457 incore_add_u8(Repodata *data, unsigned int x)
460 /* make sure we have at least 1 byte free */
461 if (data->incoredatafree < 1)
463 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
464 data->incoredatafree = 1024;
466 dp = data->incoredata + data->incoredatalen;
468 data->incoredatafree--;
469 data->incoredatalen++;
472 // ----------------------------------------------
475 * read repo from .solv file
480 repo_add_solv(Repo *repo, FILE *fp)
482 Pool *pool = repo->pool;
484 unsigned int numid, numrel, numdir, numsolv;
485 unsigned int numkeys, numschemata, numinfo;
487 Attrstore *embedded_store = 0;
491 Offset *str; /* map Id -> Offset into string space */
492 char *strsp; /* repo string space */
493 char *sp; /* pointer into string space */
494 Id *idmap; /* map of repo Ids to pool Ids */
496 unsigned int hashmask, h;
502 unsigned int size_idarray;
503 Id *idarraydatap, *idarraydataend;
506 unsigned int solvflags;
507 unsigned int solvversion;
509 Id *schemadata, *schemadatap, *schemadataend;
514 memset(&data, 0, sizeof(data));
520 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
522 pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
523 return SOLV_ERROR_NOT_SOLV;
525 solvversion = read_u32(&data);
534 pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
535 return SOLV_ERROR_UNSUPPORTED;
538 pool_freeidhashes(pool);
540 numid = read_u32(&data);
541 numrel = read_u32(&data);
542 if (solvversion >= SOLV_VERSION_4)
543 numdir = read_u32(&data);
546 numsolv = read_u32(&data);
547 numkeys = read_u32(&data);
548 numschemata = read_u32(&data);
549 numinfo = read_u32(&data);
550 solvflags = read_u32(&data);
552 if (numdir && numdir < 2)
554 pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
555 return SOLV_ERROR_CORRUPT;
557 if (numinfo && solvversion < SOLV_VERSION_3)
559 pool_debug(pool, SAT_ERROR, "unsupported SOLV format (has info)\n");
560 return SOLV_ERROR_UNSUPPORTED;
563 /******* Part 1: string IDs *****************************************/
565 sizeid = read_u32(&data); /* size of string+Id space */
568 * read strings and Ids
577 /* alloc string buffer */
578 pool->ss.stringspace = sat_realloc(pool->ss.stringspace, pool->ss.sstrings + sizeid + 1);
579 /* alloc string offsets (Id -> Offset into string space) */
580 pool->ss.strings = sat_realloc2(pool->ss.strings, pool->ss.nstrings + numid, sizeof(Offset));
582 strsp = pool->ss.stringspace;
583 str = pool->ss.strings; /* array of offsets into strsp, indexed by Id */
585 /* point to _BEHIND_ already allocated string/Id space */
586 strsp += pool->ss.sstrings;
590 * read new repo at end of pool
593 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
595 if (fread(strsp, sizeid, 1, fp) != 1)
597 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
598 return SOLV_ERROR_EOF;
603 unsigned int pfsize = read_u32(&data);
604 char *prefix = sat_malloc(pfsize);
608 if (fread(prefix, pfsize, 1, fp) != 1)
610 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
612 return SOLV_ERROR_EOF;
614 for (i = 1; i < numid; i++)
616 int same = (unsigned char)*pp++;
617 size_t len = strlen (pp) + 1;
619 memcpy(dest, old_str, same);
620 memcpy(dest + same, pp, len);
627 strsp[sizeid] = 0; /* make string space \0 terminated */
631 /* alloc id map for name and rel Ids. this maps ids in the solv files
632 * to the ids in our pool */
633 idmap = sat_calloc(numid + numrel, sizeof(Id));
636 * build hashes for all read strings
640 hashmask = mkmask(pool->ss.nstrings + numid);
643 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
644 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
648 * create hashtable with strings already in pool
651 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
652 for (i = 1; i < pool->ss.nstrings; i++) /* leave out our dummy zero id */
654 h = strhash(pool->ss.stringspace + pool->ss.strings[i]) & hashmask;
655 hh = HASHCHAIN_START;
657 h = HASHCHAIN_NEXT(h, hh, hashmask);
662 * run over string space, calculate offsets
664 * build id map (maps solv Id -> pool Id)
667 for (i = 1; i < numid; i++)
669 if (sp >= strsp + sizeid)
673 pool_debug(pool, SAT_ERROR, "not enough strings\n");
674 return SOLV_ERROR_OVERFLOW;
676 if (!*sp) /* empty string */
684 h = strhash(sp) & hashmask;
685 hh = HASHCHAIN_START;
691 if (!strcmp(pool->ss.stringspace + pool->ss.strings[id], sp))
692 break; /* existing string */
693 h = HASHCHAIN_NEXT(h, hh, hashmask);
696 /* length == offset to next string */
698 if (id == ID_NULL) /* end of hash chain -> new string */
700 id = pool->ss.nstrings++;
702 str[id] = pool->ss.sstrings; /* save Offset */
703 if (sp != pool->ss.stringspace + pool->ss.sstrings) /* not at end-of-buffer */
704 memmove(pool->ss.stringspace + pool->ss.sstrings, sp, l); /* append to pool buffer */
705 pool->ss.sstrings += l;
707 idmap[i] = id; /* repo relative -> pool relative */
708 sp += l; /* next string */
711 pool_shrink_strings(pool); /* vacuum */
714 /******* Part 2: Relation IDs ***************************************/
724 pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
727 hashmask = mkmask(pool->nrels + numrel);
729 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
730 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
733 * prep hash table with already existing RelDeps
736 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
737 for (i = 1; i < pool->nrels; i++)
739 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
740 hh = HASHCHAIN_START;
742 h = HASHCHAIN_NEXT(h, hh, hashmask);
747 * read RelDeps from repo
750 for (i = 0; i < numrel; i++)
752 name = read_id(&data, i + numid); /* read (repo relative) Ids */
753 evr = read_id(&data, i + numid);
754 flags = read_u8(&data);
755 name = idmap[name]; /* map to (pool relative) Ids */
757 h = relhash(name, evr, flags) & hashmask;
758 hh = HASHCHAIN_START;
762 if (id == ID_NULL) /* end of hash chain */
764 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
766 h = HASHCHAIN_NEXT(h, hh, hashmask);
768 if (id == ID_NULL) /* new RelDep */
774 ran[id].flags = flags;
776 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
779 pool_shrink_rels(pool); /* vacuum */
783 /******* Part 3: Dirs ***********************************************/
786 data.dirs = sat_calloc(numdir, sizeof(Id));
788 /* dir 0: no directory
791 for (i = 2; i < numdir; i++)
793 id = read_id(&data, i + numid);
795 data.dirs[i] = -(id - numid);
797 data.dirs[i] = idmap[id];
801 /******* Part 4: Keys ***********************************************/
803 keys = sat_calloc(numkeys, sizeof(*keys));
804 /* keys start at 1 */
805 for (i = 1; i < numkeys; i++)
807 id = read_id(&data, numid);
811 keys[i].type = read_id(&data, 0);
812 keys[i].size = read_id(&data, 0);
813 keys[i].storage = KEY_STORAGE_DROPPED;
814 switch (keys[i].type)
822 case SOLVABLE_VENDOR:
823 keys[i].storage = KEY_STORAGE_SOLVABLE;
826 keys[i].storage = KEY_STORAGE_INCORE;
831 case TYPE_REL_IDARRAY:
832 if (id >= INTERESTED_START && id <= INTERESTED_END)
833 keys[i].storage = KEY_STORAGE_SOLVABLE;
835 keys[i].storage = KEY_STORAGE_INCORE;
838 keys[i].storage = KEY_STORAGE_INCORE;
841 if (id == RPM_RPMDBID)
842 keys[i].storage = KEY_STORAGE_SOLVABLE;
844 keys[i].storage = KEY_STORAGE_INCORE;
852 data.nkeys = numkeys;
854 /******* Part 5: Schemata ********************************************/
856 id = read_id(&data, 0);
857 schemadata = sat_calloc(id, sizeof(Id));
858 schemadatap = schemadata;
859 schemadataend = schemadata + id;
860 schemata = sat_calloc(numschemata, sizeof(Id));
861 for (i = 0; i < numschemata; i++)
863 schemata[i] = schemadatap - schemadata;
864 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend, 0);
866 data.schemata = schemata;
867 data.nschemata = numschemata;
868 data.schemadata = schemadata;
870 /******* Part 6: Info ***********************************************/
871 for (i = 0; i < numinfo; i++)
873 /* for now we're just interested in data that starts with
874 * the repodata_external id
876 Id *keyp = schemadata + schemata[read_id(&data, numschemata)];
878 if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == TYPE_VOID)
880 /* external data for some ids */
881 parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
884 skip_schema(&data, keyp, keys, numid, numrel);
887 /******* Part 7: packed sizes (optional) ****************************/
889 if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
891 exists = sat_malloc (numsolv);
892 for (i = 0; i < numsolv; i++)
893 exists[i] = read_id(&data, 0) != 0;
896 /******* Part 8: item data *******************************************/
898 /* calculate idarray size */
900 for (i = 1; i < numkeys; i++)
903 if ((keys[i].type == TYPE_IDARRAY || keys[i].type == TYPE_REL_IDARRAY)
904 && id >= INTERESTED_START && id <= INTERESTED_END)
905 size_idarray += keys[i].size;
908 /* allocate needed space in repo */
911 repo_reserve_ids(repo, 0, size_idarray);
912 idarraydatap = repo->idarraydata + repo->idarraysize;
913 repo->idarraysize += size_idarray;
914 idarraydataend = idarraydatap + size_idarray;
924 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
926 /* store start and end of our id block */
927 data.start = s - pool->solvables;
928 data.end = data.start + numsolv;
930 for (i = 0; i < numsolv; i++, s++)
934 if (exists && !exists[i])
936 Id *keyp = schemadata + schemata[read_id(&data, numschemata)];
937 while ((key = *keyp++) != 0)
940 switch (keys[key].type)
943 did = read_id(&data, numid + numrel);
946 if (id == SOLVABLE_NAME)
948 else if (id == SOLVABLE_ARCH)
950 else if (id == SOLVABLE_EVR)
952 else if (id == SOLVABLE_VENDOR)
954 else if (keys[key].storage == KEY_STORAGE_INCORE)
955 incore_add_id(&data, did);
957 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
963 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
965 if (id == RPM_RPMDBID)
968 repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
969 repo->rpmdbid[i] = h;
971 else if (keys[key].storage == KEY_STORAGE_INCORE)
972 incore_add_u32(&data, h);
975 if (keys[key].storage == KEY_STORAGE_INCORE)
977 while ((h = read_u8(&data)) != 0)
978 incore_add_u8(&data, h);
979 incore_add_u8(&data, 0);
983 while (read_u8(&data) != 0)
988 case TYPE_REL_IDARRAY:
989 if (id < INTERESTED_START || id > INTERESTED_END)
991 if (keys[key].storage == KEY_STORAGE_INCORE)
995 Id old = 0, rel = keys[key].type == TYPE_REL_IDARRAY ? SOLVABLE_PREREQMARKER : 0;
998 did = read_id(&data, 0);
1000 did = (did & 0x3f) | ((did >> 1) & ~0x3f);
1014 if (did >= numid + numrel)
1017 did = ((did & ~0x3f) << 1) | h;
1018 incore_add_id(&data, did);
1024 while (((h = read_u8(&data)) & 0xc0) != 0)
1025 incore_add_u8(&data, h);
1031 while ((read_u8(&data) & 0xc0) != 0)
1037 ido = idarraydatap - repo->idarraydata;
1038 idarraydatap = read_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend, keys[key].type == TYPE_REL_IDARRAY);
1039 if (id == SOLVABLE_PROVIDES)
1041 else if (id == SOLVABLE_OBSOLETES)
1043 else if (id == SOLVABLE_CONFLICTS)
1045 else if (id == SOLVABLE_REQUIRES)
1047 else if (id == SOLVABLE_RECOMMENDS)
1049 else if (id == SOLVABLE_SUPPLEMENTS)
1050 s->supplements = ido;
1051 else if (id == SOLVABLE_SUGGESTS)
1053 else if (id == SOLVABLE_ENHANCES)
1055 else if (id == SOLVABLE_FRESHENS)
1058 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1059 for (; repo->idarraydata[ido]; ido++)
1060 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));
1067 case TYPE_ATTR_CHUNK:
1068 case TYPE_ATTR_STRING:
1069 case TYPE_ATTR_INTLIST:
1070 case TYPE_ATTR_LOCALIDS:
1071 if (!embedded_store)
1072 embedded_store = new_store (pool);
1073 add_attr_from_file (embedded_store, i, id, keys[key].type, idmap, numid, &data, keys[key].size);
1077 skip_item(&data, keys[key].type, numid, numrel);
1084 /* free solvables */
1085 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1087 repo->idarraysize -= size_idarray;
1088 /* free incore data */
1089 data.incoredata = sat_free(data.incoredata);
1090 data.incoredatalen = data.incoredatafree = 0;
1093 if (data.incoredatafree)
1095 /* shrink excess size */
1096 data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1097 data.incoredatafree = 0;
1100 for (i = 1; i < numkeys; i++)
1101 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1103 if (i < numkeys && !data.error)
1105 /* we have vertical data, make it available */
1106 data.verticaloffset = ftell(fp);
1110 /* no longer needed */
1114 if (data.incoredatalen || data.fp)
1116 /* we got some data, make it available */
1117 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1118 repo->repodata[repo->nrepodata++] = data;
1123 sat_free(data.dirs);
1125 sat_free(schemadata);
1133 attr_store_pack (embedded_store);
1134 /* If we have any attributes we also have pages. */
1135 read_or_setup_pages (fp, embedded_store);
1136 /* The NULL name here means embedded attributes. */
1137 repo_add_attrstore (repo, embedded_store, NULL);