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... */
46 /*******************************************************************************
47 * functions to extract data from a file handle
55 read_u32(Repodata *data)
62 for (i = 0; i < 4; i++)
67 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
68 data->error = SOLV_ERROR_EOF;
82 read_u8(Repodata *data)
91 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
92 data->error = SOLV_ERROR_EOF;
104 read_id(Repodata *data, Id max)
111 for (i = 0; i < 5; i++)
116 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
117 data->error = SOLV_ERROR_EOF;
125 pool_debug(mypool, SOLV_ERROR, "read_id: id too large (%u/%u)\n", x, max);
126 data->error = SOLV_ERROR_ID_RANGE;
131 x = (x << 7) ^ c ^ 128;
133 pool_debug(mypool, SOLV_ERROR, "read_id: id too long\n");
134 data->error = SOLV_ERROR_CORRUPT;
140 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
152 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
153 data->error = SOLV_ERROR_EOF;
158 x = (x << 7) ^ c ^ 128;
161 x = (x << 6) | (c & 63);
164 pool_debug(mypool, SOLV_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
165 data->error = SOLV_ERROR_ID_RANGE;
172 pool_debug(mypool, SOLV_ERROR, "read_idarray: array overflow\n");
178 if (x == 0) /* already have trailing zero? */
182 pool_debug(mypool, SOLV_ERROR, "read_idarray: array overflow\n");
183 data->error = SOLV_ERROR_OVERFLOW;
194 /*******************************************************************************
195 * functions to extract data from memory
202 static inline unsigned char *
203 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
206 dp = data_read_id(dp, &x);
207 if (x < 0 || (max && x >= max))
209 pool_debug(mypool, SOLV_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
210 *error = SOLV_ERROR_ID_RANGE;
213 *ret = map ? map[x] : x;
217 static unsigned char *
218 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
229 x = (x << 7) ^ c ^ 128;
232 x = (x << 6) | (c & 63);
235 pool_debug(mypool, SOLV_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
236 *error = SOLV_ERROR_ID_RANGE;
249 static unsigned char *
250 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
262 x = (x << 7) ^ c ^ 128;
265 x = (x << 6) | (c & 63);
279 pool_debug(mypool, SOLV_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
280 *error = SOLV_ERROR_ID_RANGE;
283 *store++ = map ? map[x] : x;
296 /*******************************************************************************
297 * functions to add data to our incore memory space
300 #define INCORE_ADD_CHUNK 8192
301 #define DATA_READ_CHUNK 8192
304 incore_add_id(Repodata *data, Id sx)
306 unsigned int x = (unsigned int)sx;
308 /* make sure we have at least 5 bytes free */
309 if (data->incoredatafree < 5)
311 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
312 data->incoredatafree = INCORE_ADD_CHUNK;
314 dp = data->incoredata + data->incoredatalen;
318 *dp++ = (x >> 28) | 128;
320 *dp++ = (x >> 21) | 128;
321 *dp++ = (x >> 14) | 128;
324 *dp++ = (x >> 7) | 128;
326 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
327 data->incoredatalen = dp - data->incoredata;
331 incore_add_blob(Repodata *data, unsigned char *buf, int len)
333 if (data->incoredatafree < len)
335 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
336 data->incoredatafree = INCORE_ADD_CHUNK + len;
338 memcpy(data->incoredata + data->incoredatalen, buf, len);
339 data->incoredatafree -= len;
340 data->incoredatalen += len;
344 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
346 /* We have to map the IDs, which might also change
347 the necessary number of bytes, so we can't just copy
348 over the blob and adjust it. */
353 dp = data_read_ideof(dp, &id, &eof);
354 if (id < 0 || (max && id >= max))
356 pool_debug(mypool, SOLV_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
357 data->error = SOLV_ERROR_ID_RANGE;
362 abort(); /* for now */
364 id = (id & 63) | ((id & ~63) << 1);
365 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 = solv_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;
392 incore_add_u8(Repodata *data, unsigned int x)
395 /* make sure we have at least 1 byte free */
396 if (data->incoredatafree < 1)
398 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + 1024);
399 data->incoredatafree = 1024;
401 dp = data->incoredata + data->incoredatalen;
403 data->incoredatafree--;
404 data->incoredatalen++;
409 /*******************************************************************************
414 * read repo from .solv file and add it to pool
418 repo_add_solv(Repo *repo, FILE *fp, int flags)
420 Pool *pool = repo->pool;
422 unsigned int numid, numrel, numdir, numsolv;
423 unsigned int numkeys, numschemata;
426 Offset *str; /* map Id -> Offset into string space */
427 char *strsp; /* repo string space */
428 char *sp; /* pointer into string space */
429 Id *idmap; /* map of repo Ids to pool Ids */
431 unsigned int hashmask, h;
437 unsigned int size_idarray;
438 Id *idarraydatap, *idarraydataend;
441 unsigned int solvflags;
442 unsigned int solvversion;
444 Id *schemadata, *schemadatap, *schemadataend;
445 Id *schemata, key, *keyp;
448 int maxsize, allsize;
449 unsigned char *buf, *bufend, *dp, *dps;
452 int needchunk; /* need a new chunk of data */
455 struct _Stringpool *spool;
457 Repodata *parent = 0;
460 now = solv_timems(0);
462 if ((flags & REPO_USE_LOADING) != 0)
464 /* this is a stub replace operation */
465 flags |= REPO_EXTEND_SOLVABLES;
466 /* use REPO_REUSE_REPODATA hack so that the old repodata is kept */
467 parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
470 memset(&data, 0, sizeof(data));
473 repopagestore_init(&data.store);
477 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
479 pool_debug(pool, SOLV_ERROR, "not a SOLV file\n");
480 return SOLV_ERROR_NOT_SOLV;
482 solvversion = read_u32(&data);
488 pool_debug(pool, SOLV_ERROR, "unsupported SOLV version\n");
489 return SOLV_ERROR_UNSUPPORTED;
492 pool_freeidhashes(pool);
494 numid = read_u32(&data);
495 numrel = read_u32(&data);
496 numdir = read_u32(&data);
497 numsolv = read_u32(&data);
498 numkeys = read_u32(&data);
499 numschemata = read_u32(&data);
500 solvflags = read_u32(&data);
502 if (numdir && numdir < 2)
504 pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
505 return SOLV_ERROR_CORRUPT;
508 if (numrel && (flags & REPO_LOCALPOOL) != 0)
510 pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
511 return SOLV_ERROR_CORRUPT;
513 if (parent && numsolv)
515 /* make sure that we exactly replace the stub repodata */
516 if (parent->end - parent->start != numsolv)
518 pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
519 return SOLV_ERROR_CORRUPT;
521 for (i = 0; i < numsolv; i++)
522 if (pool->solvables[parent->start + i].repo != repo)
524 pool_debug(pool, SOLV_ERROR, "main repository contains holes\n");
525 return SOLV_ERROR_CORRUPT;
529 /******* Part 1: string IDs *****************************************/
531 sizeid = read_u32(&data); /* size of string+Id space */
534 * read strings and Ids
543 if (!(flags & REPO_LOCALPOOL))
549 spool->stringspace = solv_malloc(7);
550 strcpy(spool->stringspace, "<NULL>");
555 /* alloc string buffer */
556 spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
557 /* alloc string offsets (Id -> Offset into string space) */
558 spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
560 strsp = spool->stringspace;
561 str = spool->strings; /* array of offsets into strsp, indexed by Id */
563 /* point to _BEHIND_ already allocated string/Id space */
564 strsp += spool->sstrings;
568 * read new repo at end of pool
571 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
573 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
575 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
576 return SOLV_ERROR_EOF;
581 unsigned int pfsize = read_u32(&data);
582 char *prefix = solv_malloc(pfsize);
588 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
590 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
592 return SOLV_ERROR_EOF;
594 for (i = 1; i < numid; i++)
596 int same = (unsigned char)*pp++;
597 size_t len = strlen(pp) + 1;
598 freesp -= same + len;
601 pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
603 return SOLV_ERROR_OVERFLOW;
606 memcpy(dest, old_str, same);
607 memcpy(dest + same, pp, len);
615 pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
616 return SOLV_ERROR_CORRUPT;
619 strsp[sizeid] = 0; /* make string space \0 terminated */
622 if ((flags & REPO_LOCALPOOL) != 0)
624 /* no shared pool, thus no idmap and no unification */
626 spool->nstrings = numid;
630 /* we need the '' for directories */
631 pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
632 return SOLV_ERROR_CORRUPT;
634 for (i = 1; i < spool->nstrings; i++)
636 if (sp >= strsp + sizeid)
638 pool_debug(pool, SOLV_ERROR, "not enough strings\n");
639 return SOLV_ERROR_OVERFLOW;
641 str[i] = sp - spool->stringspace;
642 sp += strlen(sp) + 1;
644 spool->sstrings = sp - spool->stringspace;
649 /* alloc id map for name and rel Ids. this maps ids in the solv files
650 * to the ids in our pool */
651 idmap = solv_calloc(numid + numrel, sizeof(Id));
654 * build hashes for all read strings
658 hashmask = mkmask(spool->nstrings + numid);
661 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
662 POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
666 * create hashtable with strings already in pool
669 hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
670 for (i = 1; i < spool->nstrings; i++) /* leave out our dummy zero id */
672 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
673 hh = HASHCHAIN_START;
675 h = HASHCHAIN_NEXT(h, hh, hashmask);
680 * run over string space, calculate offsets
682 * build id map (maps solv Id -> pool Id)
685 for (i = 1; i < numid; i++)
687 if (sp >= strsp + sizeid)
691 pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
692 return SOLV_ERROR_OVERFLOW;
694 if (!*sp) /* empty string */
702 h = strhash(sp) & hashmask;
703 hh = HASHCHAIN_START;
709 if (!strcmp(spool->stringspace + spool->strings[id], sp))
710 break; /* existing string */
711 h = HASHCHAIN_NEXT(h, hh, hashmask);
714 /* length == offset to next string */
716 if (id == ID_NULL) /* end of hash chain -> new string */
718 id = spool->nstrings++;
720 str[id] = spool->sstrings; /* save Offset */
721 if (sp != spool->stringspace + spool->sstrings) /* not at end-of-buffer */
722 memmove(spool->stringspace + spool->sstrings, sp, l); /* append to pool buffer */
723 spool->sstrings += l;
725 idmap[i] = id; /* repo relative -> pool relative */
726 sp += l; /* next string */
730 pool_shrink_strings(pool); /* vacuum */
733 /******* Part 2: Relation IDs ***************************************/
743 pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
746 hashmask = mkmask(pool->nrels + numrel);
748 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
749 POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
752 * prep hash table with already existing RelDeps
755 hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
756 for (i = 1; i < pool->nrels; i++)
758 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
759 hh = HASHCHAIN_START;
761 h = HASHCHAIN_NEXT(h, hh, hashmask);
766 * read RelDeps from repo
769 for (i = 0; i < numrel; i++)
771 name = read_id(&data, i + numid); /* read (repo relative) Ids */
772 evr = read_id(&data, i + numid);
773 relflags = read_u8(&data);
774 name = idmap[name]; /* map to (pool relative) Ids */
776 h = relhash(name, evr, relflags) & hashmask;
777 hh = HASHCHAIN_START;
781 if (id == ID_NULL) /* end of hash chain */
783 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
785 h = HASHCHAIN_NEXT(h, hh, hashmask);
787 if (id == ID_NULL) /* new RelDep */
793 ran[id].flags = relflags;
795 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
798 pool_shrink_rels(pool); /* vacuum */
802 /******* Part 3: Dirs ***********************************************/
805 data.dirpool.dirs = solv_malloc2(numdir, sizeof(Id));
806 data.dirpool.ndirs = numdir;
807 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
808 data.dirpool.dirs[1] = 1; /* dir 1: / */
809 for (i = 2; i < numdir; i++)
811 id = read_id(&data, i + numid);
813 data.dirpool.dirs[i] = -(id - numid);
815 data.dirpool.dirs[i] = idmap[id];
817 data.dirpool.dirs[i] = id;
821 /******* Part 4: Keys ***********************************************/
823 keys = solv_calloc(numkeys, sizeof(*keys));
824 /* keys start at 1 */
825 for (i = 1; i < numkeys; i++)
827 id = read_id(&data, numid);
830 else if ((flags & REPO_LOCALPOOL) != 0)
831 id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
832 type = read_id(&data, numid);
835 else if ((flags & REPO_LOCALPOOL) != 0)
836 type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
837 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
839 pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_id2str(pool, type));
840 data.error = SOLV_ERROR_UNSUPPORTED;
841 type = REPOKEY_TYPE_VOID;
845 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
846 keys[i].storage = read_id(&data, 0);
847 /* old versions used SOLVABLE for main solvable data */
848 if (keys[i].storage == KEY_STORAGE_SOLVABLE)
849 keys[i].storage = KEY_STORAGE_INCORE;
850 if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
852 pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
853 data.error = SOLV_ERROR_UNSUPPORTED;
855 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
857 if (keys[i].storage != KEY_STORAGE_INCORE)
859 pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
860 data.error = SOLV_ERROR_UNSUPPORTED;
862 keys[i].storage = KEY_STORAGE_SOLVABLE;
864 /* cannot handle rel idarrays in incore/vertical */
865 if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
867 pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
868 data.error = SOLV_ERROR_UNSUPPORTED;
870 /* cannot handle mapped ids in vertical */
871 if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
873 pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
874 data.error = SOLV_ERROR_UNSUPPORTED;
877 if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
878 keys[i].size = idmap[keys[i].size];
880 fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
881 keys[i].size, keys[i].storage);
885 have_xdata = parent ? 1 : 0;
886 for (i = 1; i < numkeys; i++)
887 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
891 data.nkeys = numkeys;
892 for (i = 1; i < numkeys; i++)
895 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
898 /******* Part 5: Schemata ********************************************/
900 id = read_id(&data, 0);
901 schemadata = solv_calloc(id + 1, sizeof(Id));
902 schemadatap = schemadata + 1;
903 schemadataend = schemadatap + id;
904 schemata = solv_calloc(numschemata, sizeof(Id));
905 for (i = 1; i < numschemata; i++)
907 schemata[i] = schemadatap - schemadata;
908 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
910 Id *sp = schemadata + schemata[i];
911 fprintf(stderr, "schema %d:", i);
913 fprintf(stderr, " %d", *sp);
914 fprintf(stderr, "\n");
917 data.schemata = schemata;
918 data.nschemata = numschemata;
919 data.schemadata = schemadata;
920 data.schemadatalen = schemadataend - data.schemadata;
922 /******* Part 6: Data ********************************************/
924 idarraydatap = idarraydataend = 0;
927 maxsize = read_id(&data, 0);
928 allsize = read_id(&data, 0);
929 maxsize += 5; /* so we can read the next schema of an array */
930 if (maxsize > allsize)
933 buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
938 if (l < DATA_READ_CHUNK)
942 if (!l || fread(buf, l, 1, data.fp) != 1)
944 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
945 data.error = SOLV_ERROR_EOF;
952 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
955 incore_add_id(&data, 0); /* XXX? */
956 incore_add_id(&data, id);
957 keyp = schemadata + schemata[id];
958 data.mainschema = id;
959 for (i = 0; keyp[i]; i++)
962 data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
970 /* make sure we have enough room */
971 if (keydepth == 0 || needchunk)
973 int left = bufend - dp;
974 /* read data chunk to dp */
979 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
980 data.error = SOLV_ERROR_EOF;
986 memmove(buf, dp, left);
988 if (l < DATA_READ_CHUNK)
992 if (l && fread(buf + left, l, 1, data.fp) != 1)
994 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
995 data.error = SOLV_ERROR_EOF;
1000 bufend = buf + left;
1001 if (allsize + left < maxsize)
1002 maxsize = allsize + left;
1010 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1018 if (s && keydepth == 3)
1020 s++; /* next solvable */
1022 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1024 id = stack[keydepth - 1];
1027 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1028 incore_add_id(&data, id);
1030 keyp = schemadata + schemata[id];
1037 keyp = schemadata + stack[--keydepth];
1038 nentries = stack[--keydepth];
1040 printf("pop flexarray %d %d\n", keydepth, nentries);
1043 s = 0; /* back from solvables */
1048 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1051 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1053 id = keys[key].name;
1054 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1057 dp = data_skip(dp, REPOKEY_TYPE_ID);
1058 dp = data_skip(dp, REPOKEY_TYPE_ID);
1059 incore_add_blob(&data, dps, dp - dps); /* just record offset/size */
1062 switch (keys[key].type)
1064 case REPOKEY_TYPE_ID:
1065 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1066 if (s && id == SOLVABLE_NAME)
1068 else if (s && id == SOLVABLE_ARCH)
1070 else if (s && id == SOLVABLE_EVR)
1072 else if (s && id == SOLVABLE_VENDOR)
1074 else if (keys[key].storage == KEY_STORAGE_INCORE)
1075 incore_add_id(&data, did);
1077 POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1080 case REPOKEY_TYPE_IDARRAY:
1081 case REPOKEY_TYPE_REL_IDARRAY:
1082 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1085 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1086 if (keys[key].storage != KEY_STORAGE_INCORE)
1089 incore_map_idarray(&data, dps, idmap, numid);
1091 incore_add_blob(&data, dps, dp - dps);
1094 ido = idarraydatap - repo->idarraydata;
1095 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1096 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1097 else if (id == SOLVABLE_REQUIRES)
1098 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1099 else if (id == SOLVABLE_PROVIDES)
1100 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1102 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1103 if (idarraydatap > idarraydataend)
1105 pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1106 data.error = SOLV_ERROR_OVERFLOW;
1109 if (id == SOLVABLE_PROVIDES)
1111 else if (id == SOLVABLE_OBSOLETES)
1113 else if (id == SOLVABLE_CONFLICTS)
1115 else if (id == SOLVABLE_REQUIRES)
1117 else if (id == SOLVABLE_RECOMMENDS)
1119 else if (id == SOLVABLE_SUPPLEMENTS)
1120 s->supplements = ido;
1121 else if (id == SOLVABLE_SUGGESTS)
1123 else if (id == SOLVABLE_ENHANCES)
1126 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1127 for (; repo->idarraydata[ido]; ido++)
1128 POOL_DEBUG(SOLV_DEBUG_STATS," %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1131 case REPOKEY_TYPE_FIXARRAY:
1132 case REPOKEY_TYPE_FLEXARRAY:
1135 if (keydepth == sizeof(stack)/sizeof(*stack))
1137 pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1138 data.error = SOLV_ERROR_CORRUPT;
1141 stack[keydepth++] = nentries;
1142 stack[keydepth++] = keyp - schemadata;
1143 stack[keydepth++] = 0;
1144 dp = data_read_id_max(dp, &nentries, 0, 0, &data.error);
1145 incore_add_id(&data, nentries);
1148 /* zero size array? */
1150 nentries = stack[--keydepth];
1153 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1155 /* horray! here come the solvables */
1156 if (nentries != numsolv)
1158 pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1159 data.error = SOLV_ERROR_CORRUPT;
1164 pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1165 data.error = SOLV_ERROR_CORRUPT;
1169 s = pool_id2solvable(pool, parent->start);
1171 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1172 data.start = s - pool->solvables;
1173 data.end = data.start + numsolv;
1174 repodata_extend_block(&data, data.start, numsolv);
1175 for (i = 1; i < numkeys; i++)
1178 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1179 && id >= INTERESTED_START && id <= INTERESTED_END)
1180 size_idarray += keys[i].size;
1182 /* allocate needed space in repo */
1183 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1184 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1185 idarraydatap = repo->idarraydata + repo->idarraysize;
1186 repo->idarraysize += size_idarray;
1187 idarraydataend = idarraydatap + size_idarray;
1190 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1193 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1194 incore_add_id(&data, id);
1195 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1199 pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1200 data.error = SOLV_ERROR_CORRUPT;
1202 stack[keydepth - 1] = id;
1204 keyp = schemadata + schemata[id];
1207 if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1209 if (keys[key].type == REPOKEY_TYPE_U32)
1210 dp = data_read_u32(dp, (unsigned int *)&id);
1212 dp = data_read_id_max(dp, &id, 0, 0, &data.error);
1214 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1215 repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1219 dp = data_skip(dp, keys[key].type);
1220 if (keys[key].storage == KEY_STORAGE_INCORE)
1221 incore_add_blob(&data, dps, dp - dps);
1225 /* should shrink idarraydata again */
1229 pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1230 data.error = SOLV_ERROR_CORRUPT;
1236 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1237 data.error = SOLV_ERROR_EOF;
1244 /* free solvables */
1245 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1247 repo->idarraysize -= size_idarray;
1248 /* free incore data */
1249 data.incoredata = solv_free(data.incoredata);
1250 data.incoredatalen = data.incoredatafree = 0;
1253 if (data.incoredatafree)
1255 /* shrink excess size */
1256 data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1257 data.incoredatafree = 0;
1260 for (i = 1; i < numkeys; i++)
1261 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1263 if (i < numkeys && !data.error)
1266 unsigned int pagesize;
1268 /* we have vertical data, make it available */
1269 data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1270 for (i = 1; i < numkeys; i++)
1271 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1273 data.verticaloffset[i] = fileoffset;
1274 fileoffset += keys[i].size;
1276 data.lastverticaloffset = fileoffset;
1277 pagesize = read_u32(&data);
1278 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1282 /* no longer needed */
1290 /* XXX: free repodata? */
1296 /* overwrite stub repodata */
1297 repodata_freedata(parent);
1298 data.repodataid = parent->repodataid;
1303 /* make it available as new repodata */
1304 if (!repo->nrepodata)
1306 repo->nrepodata = 1;
1307 repo->repodata = solv_calloc(2, sizeof(data));
1310 repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1311 data.repodataid = repo->nrepodata;
1312 repo->repodata[repo->nrepodata++] = data;
1315 /* create stub repodata entries for all external */
1316 if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1318 for (key = 1 ; key < data.nkeys; key++)
1319 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1321 if (key < data.nkeys)
1322 repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1325 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1326 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1327 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));