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"
32 #define INTERESTED_START SOLVABLE_NAME
33 #define INTERESTED_END SOLVABLE_ENHANCES
35 #define SOLV_ERROR_NOT_SOLV 1
36 #define SOLV_ERROR_UNSUPPORTED 2
37 #define SOLV_ERROR_EOF 3
38 #define SOLV_ERROR_ID_RANGE 4
39 #define SOLV_ERROR_OVERFLOW 5
40 #define SOLV_ERROR_CORRUPT 6
42 static Pool *mypool; /* for pool_debug... */
45 static void repodata_load_stub(Repodata *data);
48 /*******************************************************************************
49 * functions to extract data from a file handle
57 read_u32(Repodata *data)
64 for (i = 0; i < 4; i++)
69 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
70 data->error = SOLV_ERROR_EOF;
84 read_u8(Repodata *data)
93 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
94 data->error = SOLV_ERROR_EOF;
106 read_id(Repodata *data, Id max)
113 for (i = 0; i < 5; i++)
118 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
119 data->error = SOLV_ERROR_EOF;
127 pool_debug(mypool, SAT_ERROR, "read_id: id too large (%u/%u)\n", x, max);
128 data->error = SOLV_ERROR_ID_RANGE;
133 x = (x << 7) ^ c ^ 128;
135 pool_debug(mypool, SAT_ERROR, "read_id: id too long\n");
136 data->error = SOLV_ERROR_CORRUPT;
142 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
154 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
155 data->error = SOLV_ERROR_EOF;
160 x = (x << 7) ^ c ^ 128;
163 x = (x << 6) | (c & 63);
166 pool_debug(mypool, SAT_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
167 data->error = SOLV_ERROR_ID_RANGE;
174 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
180 if (x == 0) /* already have trailing zero? */
184 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
185 data->error = SOLV_ERROR_OVERFLOW;
196 /*******************************************************************************
197 * functions to extract data from memory
204 static inline unsigned char *
205 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
208 dp = data_read_id(dp, &x);
211 pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
212 *error = SOLV_ERROR_ID_RANGE;
215 *ret = map ? map[x] : x;
220 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
231 x = (x << 7) ^ c ^ 128;
234 x = (x << 6) | (c & 63);
237 pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
238 *error = SOLV_ERROR_ID_RANGE;
252 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
264 x = (x << 7) ^ c ^ 128;
267 x = (x << 6) | (c & 63);
281 pool_debug(mypool, SAT_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
282 *error = SOLV_ERROR_ID_RANGE;
285 *store++ = map ? map[x] : x;
298 /*******************************************************************************
299 * functions to add data to our incore memory space
302 #define INCORE_ADD_CHUNK 8192
303 #define DATA_READ_CHUNK 8192
306 incore_add_id(Repodata *data, Id x)
309 /* make sure we have at least 5 bytes free */
310 if (data->incoredatafree < 5)
312 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
313 data->incoredatafree = INCORE_ADD_CHUNK;
315 dp = data->incoredata + data->incoredatalen;
321 *dp++ = (x >> 28) | 128;
323 *dp++ = (x >> 21) | 128;
324 *dp++ = (x >> 14) | 128;
327 *dp++ = (x >> 7) | 128;
329 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
330 data->incoredatalen = dp - data->incoredata;
334 incore_add_blob(Repodata *data, unsigned char *buf, int len)
336 if (data->incoredatafree < len)
338 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
339 data->incoredatafree = INCORE_ADD_CHUNK + len;
341 memcpy(data->incoredata + data->incoredatalen, buf, len);
342 data->incoredatafree -= len;
343 data->incoredatalen += len;
347 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
349 /* We have to map the IDs, which might also change
350 the necessary number of bytes, so we can't just copy
351 over the blob and adjust it. */
356 dp = data_read_ideof(dp, &id, &eof);
357 if (max && id >= max)
359 pool_debug(mypool, SAT_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
360 data->error = SOLV_ERROR_ID_RANGE;
365 id = (id & 63) | ((id & ~63) << 1);
366 incore_add_id(data, eof ? id : id | 64);
373 incore_add_u32(Repodata *data, unsigned int x)
376 /* make sure we have at least 4 bytes free */
377 if (data->incoredatafree < 4)
379 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
380 data->incoredatafree = INCORE_ADD_CHUNK;
382 dp = data->incoredata + data->incoredatalen;
387 data->incoredatafree -= 4;
388 data->incoredatalen += 4;
393 incore_add_u8(Repodata *data, unsigned int x)
396 /* make sure we have at least 1 byte free */
397 if (data->incoredatafree < 1)
399 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
400 data->incoredatafree = 1024;
402 dp = data->incoredata + data->incoredatalen;
404 data->incoredatafree--;
405 data->incoredatalen++;
410 /*******************************************************************************
411 * callback to create our stub sub-repodatas from the incore data
414 struct create_stub_data {
420 create_stub_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
422 struct create_stub_data *stubdata = cbdata;
423 if (key->name == REPOSITORY_EXTERNAL && key->type == REPOKEY_TYPE_FLEXARRAY)
427 repodata_internalize(stubdata->data);
428 if (data->start != data->end)
430 repodata_extend(stubdata->data, data->start);
431 repodata_extend(stubdata->data, data->end - 1);
436 return SEARCH_NEXT_SOLVABLE;
437 stubdata->data = repo_add_repodata(data->repo, 0);
438 stubdata->data->state = REPODATA_STUB;
439 stubdata->data->loadcallback = repodata_load_stub;
440 return SEARCH_ENTERSUB;
443 return SEARCH_NEXT_KEY;
446 case REPOKEY_TYPE_ID:
447 repodata_set_id(stubdata->data, SOLVID_META, key->name, kv->id);
449 case REPOKEY_TYPE_CONSTANTID:
450 repodata_set_constantid(stubdata->data, SOLVID_META, key->name, kv->id);
452 case REPOKEY_TYPE_STR:
453 repodata_set_str(stubdata->data, SOLVID_META, key->name, kv->str);
455 case REPOKEY_TYPE_VOID:
456 repodata_set_void(stubdata->data, SOLVID_META, key->name);
458 case REPOKEY_TYPE_NUM:
459 repodata_set_num(stubdata->data, SOLVID_META, key->name, kv->num);
461 case REPOKEY_TYPE_IDARRAY:
462 repodata_add_idarray(stubdata->data, SOLVID_META, key->name, kv->id);
463 if (key->name == REPOSITORY_KEYS)
465 if (!stubdata->xkeyname)
466 stubdata->xkeyname = kv->id;
471 xkey.name = stubdata->xkeyname;
473 xkey.storage = KEY_STORAGE_INCORE;
475 repodata_key2id(stubdata->data, &xkey, 1);
476 stubdata->xkeyname = 0;
479 stubdata->xkeyname = 0;
482 case REPOKEY_TYPE_MD5:
483 case REPOKEY_TYPE_SHA1:
484 case REPOKEY_TYPE_SHA256:
485 repodata_set_checksum(stubdata->data, SOLVID_META, key->name, key->type, kv->str);
488 return SEARCH_NEXT_KEY;
494 /*******************************************************************************
499 * read repo from .solv file and add it to pool
500 * if stubdata is set, substitute it with read data
501 * (this is used to replace a repodata stub with the real data)
505 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
507 Pool *pool = repo->pool;
509 unsigned int numid, numrel, numdir, numsolv;
510 unsigned int numkeys, numschemata;
513 Offset *str; /* map Id -> Offset into string space */
514 char *strsp; /* repo string space */
515 char *sp; /* pointer into string space */
516 Id *idmap; /* map of repo Ids to pool Ids */
518 unsigned int hashmask, h;
524 unsigned int size_idarray;
525 Id *idarraydatap, *idarraydataend;
528 unsigned int solvflags;
529 unsigned int solvversion;
531 Id *schemadata, *schemadatap, *schemadataend;
532 Id *schemata, key, *keyp;
535 int maxsize, allsize;
536 unsigned char *buf, *bufend, *dp, *dps;
539 int needchunk; /* need a new chunk of data */
542 struct _Stringpool *spool;
548 memset(&data, 0, sizeof(data));
551 repopagestore_init(&data.store);
555 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
557 pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
558 return SOLV_ERROR_NOT_SOLV;
560 solvversion = read_u32(&data);
566 pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
567 return SOLV_ERROR_UNSUPPORTED;
570 pool_freeidhashes(pool);
572 numid = read_u32(&data);
573 numrel = read_u32(&data);
574 numdir = read_u32(&data);
575 numsolv = read_u32(&data);
576 numkeys = read_u32(&data);
577 numschemata = read_u32(&data);
578 solvflags = read_u32(&data);
580 if (numdir && numdir < 2)
582 pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
583 return SOLV_ERROR_CORRUPT;
590 pool_debug(pool, SAT_ERROR, "relations are forbidden in a sub-repository\n");
591 return SOLV_ERROR_CORRUPT;
593 if (parent->end - parent->start != numsolv)
595 pool_debug(pool, SAT_ERROR, "sub-repository solvable number doesn't match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
596 return SOLV_ERROR_CORRUPT;
600 /******* Part 1: string IDs *****************************************/
602 sizeid = read_u32(&data); /* size of string+Id space */
605 * read strings and Ids
620 spool->stringspace = sat_malloc(7);
621 strcpy(spool->stringspace, "<NULL>");
626 /* alloc string buffer */
627 spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
628 /* alloc string offsets (Id -> Offset into string space) */
629 spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
631 strsp = spool->stringspace;
632 str = spool->strings; /* array of offsets into strsp, indexed by Id */
634 /* point to _BEHIND_ already allocated string/Id space */
635 strsp += spool->sstrings;
639 * read new repo at end of pool
642 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
644 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
646 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
647 return SOLV_ERROR_EOF;
652 unsigned int pfsize = read_u32(&data);
653 char *prefix = sat_malloc(pfsize);
659 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
661 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
663 return SOLV_ERROR_EOF;
665 for (i = 1; i < numid; i++)
667 int same = (unsigned char)*pp++;
668 size_t len = strlen(pp) + 1;
669 freesp -= same + len;
672 pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
674 return SOLV_ERROR_OVERFLOW;
677 memcpy(dest, old_str, same);
678 memcpy(dest + same, pp, len);
686 pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
687 return SOLV_ERROR_CORRUPT;
690 strsp[sizeid] = 0; /* make string space \0 terminated */
695 /* no shared pool, thus no idmap and no unification */
697 spool->nstrings = numid;
701 /* we need the '' for directories */
702 pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
703 return SOLV_ERROR_CORRUPT;
705 for (i = 1; i < spool->nstrings; i++)
707 if (sp >= strsp + sizeid)
709 pool_debug(pool, SAT_ERROR, "not enough strings\n");
710 return SOLV_ERROR_OVERFLOW;
712 str[i] = sp - spool->stringspace;
713 sp += strlen(sp) + 1;
715 spool->sstrings = sp - spool->stringspace;
720 /* alloc id map for name and rel Ids. this maps ids in the solv files
721 * to the ids in our pool */
722 idmap = sat_calloc(numid + numrel, sizeof(Id));
725 * build hashes for all read strings
729 hashmask = mkmask(spool->nstrings + numid);
732 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
733 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
737 * create hashtable with strings already in pool
740 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
741 for (i = 1; i < spool->nstrings; i++) /* leave out our dummy zero id */
743 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
744 hh = HASHCHAIN_START;
746 h = HASHCHAIN_NEXT(h, hh, hashmask);
751 * run over string space, calculate offsets
753 * build id map (maps solv Id -> pool Id)
756 for (i = 1; i < numid; i++)
758 if (sp >= strsp + sizeid)
762 pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
763 return SOLV_ERROR_OVERFLOW;
765 if (!*sp) /* empty string */
773 h = strhash(sp) & hashmask;
774 hh = HASHCHAIN_START;
780 if (!strcmp(spool->stringspace + spool->strings[id], sp))
781 break; /* existing string */
782 h = HASHCHAIN_NEXT(h, hh, hashmask);
785 /* length == offset to next string */
787 if (id == ID_NULL) /* end of hash chain -> new string */
789 id = spool->nstrings++;
791 str[id] = spool->sstrings; /* save Offset */
792 if (sp != spool->stringspace + spool->sstrings) /* not at end-of-buffer */
793 memmove(spool->stringspace + spool->sstrings, sp, l); /* append to pool buffer */
794 spool->sstrings += l;
796 idmap[i] = id; /* repo relative -> pool relative */
797 sp += l; /* next string */
801 pool_shrink_strings(pool); /* vacuum */
804 /******* Part 2: Relation IDs ***************************************/
814 pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
817 hashmask = mkmask(pool->nrels + numrel);
819 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
820 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
823 * prep hash table with already existing RelDeps
826 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
827 for (i = 1; i < pool->nrels; i++)
829 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
830 hh = HASHCHAIN_START;
832 h = HASHCHAIN_NEXT(h, hh, hashmask);
837 * read RelDeps from repo
840 for (i = 0; i < numrel; i++)
842 name = read_id(&data, i + numid); /* read (repo relative) Ids */
843 evr = read_id(&data, i + numid);
844 flags = read_u8(&data);
845 name = idmap[name]; /* map to (pool relative) Ids */
847 h = relhash(name, evr, flags) & hashmask;
848 hh = HASHCHAIN_START;
852 if (id == ID_NULL) /* end of hash chain */
854 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
856 h = HASHCHAIN_NEXT(h, hh, hashmask);
858 if (id == ID_NULL) /* new RelDep */
864 ran[id].flags = flags;
866 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
869 pool_shrink_rels(pool); /* vacuum */
873 /******* Part 3: Dirs ***********************************************/
876 data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
877 data.dirpool.ndirs = numdir;
878 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
879 data.dirpool.dirs[1] = 1; /* dir 1: / */
880 for (i = 2; i < numdir; i++)
882 id = read_id(&data, i + numid);
884 data.dirpool.dirs[i] = -(id - numid);
886 data.dirpool.dirs[i] = idmap[id];
888 data.dirpool.dirs[i] = id;
892 /******* Part 4: Keys ***********************************************/
894 keys = sat_calloc(numkeys, sizeof(*keys));
895 /* keys start at 1 */
896 for (i = 1; i < numkeys; i++)
898 id = read_id(&data, numid);
902 id = str2id(pool, stringpool_id2str(spool, id), 1);
903 type = read_id(&data, numid);
907 type = str2id(pool, stringpool_id2str(spool, type), 1);
908 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
910 pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
911 data.error = SOLV_ERROR_UNSUPPORTED;
912 type = REPOKEY_TYPE_VOID;
916 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
917 keys[i].storage = read_id(&data, 0);
918 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
919 keys[i].storage = KEY_STORAGE_SOLVABLE;
920 else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
921 keys[i].storage = KEY_STORAGE_INCORE;
922 if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
925 keys[i].size = idmap[keys[i].size];
927 keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
930 fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
931 keys[i].size, keys[i].storage);
935 have_xdata = parent ? 1 : 0;
936 for (i = 1; i < numkeys; i++)
937 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
941 data.nkeys = numkeys;
942 for (i = 1; i < numkeys; i++)
945 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
948 /******* Part 5: Schemata ********************************************/
950 id = read_id(&data, 0);
951 schemadata = sat_calloc(id + 1, sizeof(Id));
952 schemadatap = schemadata + 1;
953 schemadataend = schemadatap + id;
954 schemata = sat_calloc(numschemata, sizeof(Id));
955 for (i = 1; i < numschemata; i++)
957 schemata[i] = schemadatap - schemadata;
958 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
960 Id *sp = schemadata + schemata[i];
961 fprintf(stderr, "schema %d:", i);
963 fprintf(stderr, " %d", *sp);
964 fprintf(stderr, "\n");
967 data.schemata = schemata;
968 data.nschemata = numschemata;
969 data.schemadata = schemadata;
970 data.schemadatalen = schemadataend - data.schemadata;
972 /******* Part 6: Data ********************************************/
974 idarraydatap = idarraydataend = 0;
977 maxsize = read_id(&data, 0);
978 allsize = read_id(&data, 0);
979 maxsize += 5; /* so we can read the next schema of an array */
980 if (maxsize > allsize)
983 buf = sat_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
988 if (l < DATA_READ_CHUNK)
992 if (!l || fread(buf, l, 1, data.fp) != 1)
994 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
995 data.error = SOLV_ERROR_EOF;
1002 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1005 incore_add_id(&data, 0); /* XXX? */
1006 incore_add_id(&data, id);
1007 keyp = schemadata + schemata[id];
1008 data.mainschema = id;
1009 for (i = 0; keyp[i]; i++)
1012 data.mainschemaoffsets = sat_calloc(i, sizeof(Id));
1022 printf("key %d at %d\n", key, keyp - 1 - schemadata);
1030 if (s && keydepth == 3)
1032 s++; /* next solvable */
1034 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1036 id = stack[keydepth - 1];
1039 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1040 incore_add_id(&data, id);
1042 keyp = schemadata + schemata[id];
1049 keyp = schemadata + stack[--keydepth];
1050 nentries = stack[--keydepth];
1052 printf("pop flexarray %d %d\n", keydepth, nentries);
1055 s = 0; /* back from solvables */
1060 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1061 if (keydepth == 0 || needchunk)
1063 int left = bufend - dp;
1064 /* read data chunk to dp */
1069 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1070 data.error = SOLV_ERROR_EOF;
1076 memmove(buf, dp, left);
1078 if (l < DATA_READ_CHUNK)
1079 l = DATA_READ_CHUNK;
1082 if (l && fread(buf + left, l, 1, data.fp) != 1)
1084 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1085 data.error = SOLV_ERROR_EOF;
1090 bufend = buf + left;
1091 if (allsize + left < maxsize)
1092 maxsize = allsize + left;
1099 printf("=> %s %s %p\n", id2str(pool, keys[key].name), id2str(pool, keys[key].type), s);
1101 id = keys[key].name;
1102 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1105 dp = data_skip(dp, REPOKEY_TYPE_ID);
1106 dp = data_skip(dp, REPOKEY_TYPE_ID);
1107 incore_add_blob(&data, dps, dp - dps);
1110 switch (keys[key].type)
1112 case REPOKEY_TYPE_ID:
1113 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1114 if (s && id == SOLVABLE_NAME)
1116 else if (s && id == SOLVABLE_ARCH)
1118 else if (s && id == SOLVABLE_EVR)
1120 else if (s && id == SOLVABLE_VENDOR)
1122 else if (keys[key].storage == KEY_STORAGE_INCORE)
1123 incore_add_id(&data, did);
1125 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1128 case REPOKEY_TYPE_U32:
1129 dp = data_read_u32(dp, &h);
1131 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1133 if (s && id == RPM_RPMDBID)
1136 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1137 repo->rpmdbid[(s - pool->solvables) - repo->start] = h;
1139 else if (keys[key].storage == KEY_STORAGE_INCORE)
1140 incore_add_u32(&data, h);
1142 case REPOKEY_TYPE_IDARRAY:
1143 case REPOKEY_TYPE_REL_IDARRAY:
1144 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1147 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1148 if (keys[key].storage != KEY_STORAGE_INCORE)
1151 incore_map_idarray(&data, dps, idmap, numid);
1153 incore_add_blob(&data, dps, dp - dps);
1156 ido = idarraydatap - repo->idarraydata;
1157 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1158 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1159 else if (id == SOLVABLE_REQUIRES)
1160 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1161 else if (id == SOLVABLE_PROVIDES)
1162 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1164 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1165 if (idarraydatap > idarraydataend)
1167 pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1168 data.error = SOLV_ERROR_OVERFLOW;
1171 if (id == SOLVABLE_PROVIDES)
1173 else if (id == SOLVABLE_OBSOLETES)
1175 else if (id == SOLVABLE_CONFLICTS)
1177 else if (id == SOLVABLE_REQUIRES)
1179 else if (id == SOLVABLE_RECOMMENDS)
1181 else if (id == SOLVABLE_SUPPLEMENTS)
1182 s->supplements = ido;
1183 else if (id == SOLVABLE_SUGGESTS)
1185 else if (id == SOLVABLE_ENHANCES)
1188 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1189 for (; repo->idarraydata[ido]; ido++)
1190 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));
1193 case REPOKEY_TYPE_FIXARRAY:
1194 case REPOKEY_TYPE_FLEXARRAY:
1197 if (keydepth == sizeof(stack)/sizeof(*stack))
1199 pool_debug(pool, SAT_ERROR, "array stack overflow\n");
1200 data.error = SOLV_ERROR_CORRUPT;
1203 stack[keydepth++] = nentries;
1204 stack[keydepth++] = keyp - schemadata;
1205 stack[keydepth++] = 0;
1206 dp = data_read_id(dp, &nentries);
1207 incore_add_id(&data, nentries);
1210 /* zero size array? */
1212 nentries = stack[--keydepth];
1215 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1217 /* horray! here come the solvables */
1218 if (nentries != numsolv)
1220 pool_debug(pool, SAT_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1221 data.error = SOLV_ERROR_CORRUPT;
1226 pool_debug(pool, SAT_ERROR, "more than one solvable block\n");
1227 data.error = SOLV_ERROR_CORRUPT;
1231 s = pool_id2solvable(pool, parent->start);
1233 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1234 data.start = s - pool->solvables;
1235 data.end = data.start + numsolv;
1236 repodata_extend_block(&data, data.start, numsolv);
1237 for (i = 1; i < numkeys; i++)
1240 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1241 && id >= INTERESTED_START && id <= INTERESTED_END)
1242 size_idarray += keys[i].size;
1244 /* allocate needed space in repo */
1245 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1246 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1247 idarraydatap = repo->idarraydata + repo->idarraysize;
1248 repo->idarraysize += size_idarray;
1249 idarraydataend = idarraydatap + size_idarray;
1252 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1255 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1256 incore_add_id(&data, id);
1257 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1261 pool_debug(pool, SAT_ERROR, "illegal fixarray\n");
1262 data.error = SOLV_ERROR_CORRUPT;
1264 stack[keydepth - 1] = id;
1266 keyp = schemadata + schemata[id];
1270 dp = data_skip(dp, keys[key].type);
1271 if (keys[key].storage == KEY_STORAGE_INCORE)
1272 incore_add_blob(&data, dps, dp - dps);
1276 /* should shrink idarraydata again */
1280 pool_debug(pool, SAT_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1281 data.error = SOLV_ERROR_CORRUPT;
1287 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1288 data.error = SOLV_ERROR_EOF;
1295 /* free solvables */
1296 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1298 repo->idarraysize -= size_idarray;
1299 /* free incore data */
1300 data.incoredata = sat_free(data.incoredata);
1301 data.incoredatalen = data.incoredatafree = 0;
1304 if (data.incoredatafree)
1306 /* shrink excess size */
1307 data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1308 data.incoredatafree = 0;
1311 for (i = 1; i < numkeys; i++)
1312 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1314 if (i < numkeys && !data.error)
1317 unsigned int pagesize;
1319 /* we have vertical data, make it available */
1320 data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1321 for (i = 1; i < numkeys; i++)
1322 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1324 data.verticaloffset[i] = fileoffset;
1325 fileoffset += keys[i].size;
1327 data.lastverticaloffset = fileoffset;
1328 pagesize = read_u32(&data);
1329 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1333 /* no longer needed */
1341 /* XXX: free repodata? */
1347 /* overwrite stub repodata */
1348 repodata_free(parent);
1353 /* make it available as new repodata */
1354 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1355 repo->repodata[repo->nrepodata++] = data;
1358 /* create stub repodata entries for all external */
1359 for (key = 1 ; key < data.nkeys; key++)
1360 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1362 if (key < data.nkeys)
1364 struct create_stub_data stubdata;
1366 memset(&stubdata, 0, sizeof(stubdata));
1367 repodata_search(&data, SOLVID_META, REPOSITORY_EXTERNAL, SEARCH_ARRAYSENTINEL, create_stub_cb, &stubdata);
1370 POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_solv took %d ms\n", sat_timems(now));
1371 POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1372 POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (1024/sizeof(Id)));
1377 repo_add_solv(Repo *repo, FILE *fp)
1379 return repo_add_solv_parent(repo, fp, 0);
1383 repodata_load_stub(Repodata *data)
1386 Pool *pool = data->repo->pool;
1387 if (!pool->loadcallback)
1389 data->state = REPODATA_ERROR;
1392 /* so that we can retrieve meta data */
1393 data->state = REPODATA_AVAILABLE;
1394 fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1397 data->state = REPODATA_ERROR;
1400 if (repo_add_solv_parent(data->repo, fp, data))
1401 data->state = REPODATA_ERROR;
1403 data->state = REPODATA_AVAILABLE;