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_ideof(Repodata *data, Id sx, int eof)
333 unsigned int x = (unsigned int)sx;
335 /* make sure we have at least 5 bytes free */
336 if (data->incoredatafree < 5)
338 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
339 data->incoredatafree = INCORE_ADD_CHUNK;
341 dp = data->incoredata + data->incoredatalen;
345 *dp++ = (x >> 27) | 128;
347 *dp++ = (x >> 20) | 128;
348 *dp++ = (x >> 13) | 128;
351 *dp++ = (x >> 6) | 128;
352 *dp++ = eof ? (x & 63) : (x & 63) | 64;
353 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
354 data->incoredatalen = dp - data->incoredata;
358 incore_add_blob(Repodata *data, unsigned char *buf, int len)
360 if (data->incoredatafree < len)
362 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
363 data->incoredatafree = INCORE_ADD_CHUNK + len;
365 memcpy(data->incoredata + data->incoredatalen, buf, len);
366 data->incoredatafree -= len;
367 data->incoredatalen += len;
371 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
373 /* We have to map the IDs, which might also change
374 the necessary number of bytes, so we can't just copy
375 over the blob and adjust it. */
380 dp = data_read_ideof(dp, &id, &eof);
381 if (id < 0 || (max && id >= max))
383 pool_debug(mypool, SOLV_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
384 data->error = SOLV_ERROR_ID_RANGE;
388 incore_add_ideof(data, id, eof);
396 incore_add_u32(Repodata *data, unsigned int x)
399 /* make sure we have at least 4 bytes free */
400 if (data->incoredatafree < 4)
402 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
403 data->incoredatafree = INCORE_ADD_CHUNK;
405 dp = data->incoredata + data->incoredatalen;
410 data->incoredatafree -= 4;
411 data->incoredatalen += 4;
415 incore_add_u8(Repodata *data, unsigned int x)
418 /* make sure we have at least 1 byte free */
419 if (data->incoredatafree < 1)
421 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + 1024);
422 data->incoredatafree = 1024;
424 dp = data->incoredata + data->incoredatalen;
426 data->incoredatafree--;
427 data->incoredatalen++;
432 /*******************************************************************************
437 * read repo from .solv file and add it to pool
441 repo_add_solv(Repo *repo, FILE *fp, int flags)
443 Pool *pool = repo->pool;
445 unsigned int numid, numrel, numdir, numsolv;
446 unsigned int numkeys, numschemata;
449 Offset *str; /* map Id -> Offset into string space */
450 char *strsp; /* repo string space */
451 char *sp; /* pointer into string space */
452 Id *idmap; /* map of repo Ids to pool Ids */
454 unsigned int hashmask, h;
460 unsigned int size_idarray;
461 Id *idarraydatap, *idarraydataend;
464 unsigned int solvflags;
465 unsigned int solvversion;
467 Id *schemadata, *schemadatap, *schemadataend;
468 Id *schemata, key, *keyp;
471 int maxsize, allsize;
472 unsigned char *buf, *bufend, *dp, *dps;
475 int needchunk; /* need a new chunk of data */
478 struct _Stringpool *spool;
480 Repodata *parent = 0;
483 now = solv_timems(0);
485 if ((flags & REPO_USE_LOADING) != 0)
487 /* this is a stub replace operation */
488 flags |= REPO_EXTEND_SOLVABLES;
489 /* use REPO_REUSE_REPODATA hack so that the old repodata is kept */
490 parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
493 memset(&data, 0, sizeof(data));
496 repopagestore_init(&data.store);
500 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
502 pool_debug(pool, SOLV_ERROR, "not a SOLV file\n");
503 return SOLV_ERROR_NOT_SOLV;
505 solvversion = read_u32(&data);
511 pool_debug(pool, SOLV_ERROR, "unsupported SOLV version\n");
512 return SOLV_ERROR_UNSUPPORTED;
515 numid = read_u32(&data);
516 numrel = read_u32(&data);
517 numdir = read_u32(&data);
518 numsolv = read_u32(&data);
519 numkeys = read_u32(&data);
520 numschemata = read_u32(&data);
521 solvflags = read_u32(&data);
523 if (numdir && numdir < 2)
525 pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
526 return SOLV_ERROR_CORRUPT;
529 if (numrel && (flags & REPO_LOCALPOOL) != 0)
531 pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
532 return SOLV_ERROR_CORRUPT;
534 if (parent && numsolv)
536 /* make sure that we exactly replace the stub repodata */
537 if (parent->end - parent->start != numsolv)
539 pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
540 return SOLV_ERROR_CORRUPT;
542 for (i = 0; i < numsolv; i++)
543 if (pool->solvables[parent->start + i].repo != repo)
545 pool_debug(pool, SOLV_ERROR, "main repository contains holes\n");
546 return SOLV_ERROR_CORRUPT;
550 /******* Part 1: string IDs *****************************************/
552 sizeid = read_u32(&data); /* size of string+Id space */
555 * read strings and Ids
564 if (!(flags & REPO_LOCALPOOL))
567 if (pool->whatprovides)
568 pool_freewhatprovides(pool);
574 spool->stringspace = solv_malloc(7);
575 strcpy(spool->stringspace, "<NULL>");
580 /* alloc string buffer */
581 spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
582 /* alloc string offsets (Id -> Offset into string space) */
583 spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
585 strsp = spool->stringspace;
586 str = spool->strings; /* array of offsets into strsp, indexed by Id */
588 /* point to _BEHIND_ already allocated string/Id space */
589 strsp += spool->sstrings;
593 * read new repo at end of pool
596 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
598 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
600 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
601 return SOLV_ERROR_EOF;
606 unsigned int pfsize = read_u32(&data);
607 char *prefix = solv_malloc(pfsize);
613 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
615 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
617 return SOLV_ERROR_EOF;
619 for (i = 1; i < numid; i++)
621 int same = (unsigned char)*pp++;
622 size_t len = strlen(pp) + 1;
623 freesp -= same + len;
626 pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
628 return SOLV_ERROR_OVERFLOW;
631 memcpy(dest, old_str, same);
632 memcpy(dest + same, pp, len);
640 pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
641 return SOLV_ERROR_CORRUPT;
644 strsp[sizeid] = 0; /* make string space \0 terminated */
647 if ((flags & REPO_LOCALPOOL) != 0)
649 /* no shared pool, thus no idmap and no unification */
651 spool->nstrings = numid;
655 /* we need the '' for directories */
656 pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
657 return SOLV_ERROR_CORRUPT;
659 for (i = 1; i < spool->nstrings; i++)
661 if (sp >= strsp + sizeid)
663 pool_debug(pool, SOLV_ERROR, "not enough strings\n");
664 return SOLV_ERROR_OVERFLOW;
666 str[i] = sp - spool->stringspace;
667 sp += strlen(sp) + 1;
669 spool->sstrings = sp - spool->stringspace;
673 /* alloc id map for name and rel Ids. this maps ids in the solv files
674 * to the ids in our pool */
675 idmap = solv_calloc(numid + numrel, sizeof(Id));
677 /* grow hash if needed, otherwise reuse */
678 hashmask = mkmask(spool->nstrings + numid);
680 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
681 POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d, old %d\n", hashmask + 1, spool->stringhashmask + 1);
683 if (hashmask > spool->stringhashmask)
685 spool->stringhashtbl = solv_free(spool->stringhashtbl);
686 spool->stringhashmask = hashmask;
687 spool->stringhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
688 for (i = 1; i < spool->nstrings; i++)
690 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
691 hh = HASHCHAIN_START;
693 h = HASHCHAIN_NEXT(h, hh, hashmask);
699 hashtbl = spool->stringhashtbl;
700 hashmask = spool->stringhashmask;
704 * run over strings and merge with pool.
705 * also populate id map (maps solv Id -> pool Id)
707 for (i = 1; i < numid; i++)
709 if (sp >= strsp + sizeid)
713 pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
714 return SOLV_ERROR_OVERFLOW;
716 if (!*sp) /* empty string */
724 h = strhash(sp) & hashmask;
725 hh = HASHCHAIN_START;
731 if (!strcmp(spool->stringspace + spool->strings[id], sp))
732 break; /* already in pool */
733 h = HASHCHAIN_NEXT(h, hh, hashmask);
736 /* length == offset to next string */
738 if (!id) /* end of hash chain -> new string */
740 id = spool->nstrings++;
742 str[id] = spool->sstrings; /* save offset */
743 if (sp != spool->stringspace + spool->sstrings)
744 memmove(spool->stringspace + spool->sstrings, sp, l);
745 spool->sstrings += l;
747 idmap[i] = id; /* repo relative -> pool relative */
748 sp += l; /* next string */
750 if (hashmask > mkmask(spool->nstrings + 8192))
752 spool->stringhashtbl = solv_free(spool->stringhashtbl);
753 spool->stringhashmask = 0;
756 pool_shrink_strings(pool); /* vacuum */
759 /******* Part 2: Relation IDs ***************************************/
769 pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
772 /* grow hash if needed, otherwise reuse */
773 hashmask = mkmask(pool->nrels + numrel);
775 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
776 POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d, old %d\n", hashmask + 1, pool->relhashmask + 1);
778 if (hashmask > pool->relhashmask)
780 pool->relhashtbl = solv_free(pool->relhashtbl);
781 pool->relhashmask = hashmask;
782 pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
783 for (i = 1; i < pool->nrels; i++)
785 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
786 hh = HASHCHAIN_START;
788 h = HASHCHAIN_NEXT(h, hh, hashmask);
794 hashtbl = pool->relhashtbl;
795 hashmask = pool->relhashmask;
799 * read RelDeps from repo
801 for (i = 0; i < numrel; i++)
803 name = read_id(&data, i + numid); /* read (repo relative) Ids */
804 evr = read_id(&data, i + numid);
805 relflags = read_u8(&data);
806 name = idmap[name]; /* map to (pool relative) Ids */
808 h = relhash(name, evr, relflags) & hashmask;
809 hh = HASHCHAIN_START;
813 if (!id) /* end of hash chain reached */
815 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
817 h = HASHCHAIN_NEXT(h, hh, hashmask);
819 if (!id) /* new RelDep */
825 ran[id].flags = relflags;
827 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
829 if (hashmask > mkmask(pool->nrels + 4096))
831 pool->relhashtbl = solv_free(pool->relhashtbl);
832 pool->relhashmask = 0;
834 pool_shrink_rels(pool); /* vacuum */
838 /******* Part 3: Dirs ***********************************************/
841 data.dirpool.dirs = solv_malloc2(numdir, sizeof(Id));
842 data.dirpool.ndirs = numdir;
843 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
844 data.dirpool.dirs[1] = 1; /* dir 1: / */
845 for (i = 2; i < numdir; i++)
847 id = read_id(&data, i + numid);
849 data.dirpool.dirs[i] = -(id - numid);
851 data.dirpool.dirs[i] = idmap[id];
853 data.dirpool.dirs[i] = id;
857 /******* Part 4: Keys ***********************************************/
859 keys = solv_calloc(numkeys, sizeof(*keys));
860 /* keys start at 1 */
861 for (i = 1; i < numkeys; i++)
863 id = read_id(&data, numid);
866 else if ((flags & REPO_LOCALPOOL) != 0)
867 id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
868 type = read_id(&data, numid);
871 else if ((flags & REPO_LOCALPOOL) != 0)
872 type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
873 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
875 pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_id2str(pool, type));
876 data.error = SOLV_ERROR_UNSUPPORTED;
877 type = REPOKEY_TYPE_VOID;
881 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
882 keys[i].storage = read_id(&data, 0);
883 /* old versions used SOLVABLE for main solvable data */
884 if (keys[i].storage == KEY_STORAGE_SOLVABLE)
885 keys[i].storage = KEY_STORAGE_INCORE;
886 if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
888 pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
889 data.error = SOLV_ERROR_UNSUPPORTED;
891 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
893 if (keys[i].storage != KEY_STORAGE_INCORE)
895 pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
896 data.error = SOLV_ERROR_UNSUPPORTED;
898 keys[i].storage = KEY_STORAGE_SOLVABLE;
900 /* cannot handle rel idarrays in incore/vertical */
901 if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
903 pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
904 data.error = SOLV_ERROR_UNSUPPORTED;
906 /* cannot handle mapped ids in vertical */
907 if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
909 pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
910 data.error = SOLV_ERROR_UNSUPPORTED;
913 if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
914 keys[i].size = idmap[keys[i].size];
916 fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
917 keys[i].size, keys[i].storage);
921 have_xdata = parent ? 1 : 0;
922 for (i = 1; i < numkeys; i++)
923 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
927 data.nkeys = numkeys;
928 for (i = 1; i < numkeys; i++)
931 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
934 /******* Part 5: Schemata ********************************************/
936 id = read_id(&data, 0);
937 schemadata = solv_calloc(id + 1, sizeof(Id));
938 schemadatap = schemadata + 1;
939 schemadataend = schemadatap + id;
940 schemata = solv_calloc(numschemata, sizeof(Id));
941 for (i = 1; i < numschemata; i++)
943 schemata[i] = schemadatap - schemadata;
944 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
946 Id *sp = schemadata + schemata[i];
947 fprintf(stderr, "schema %d:", i);
949 fprintf(stderr, " %d", *sp);
950 fprintf(stderr, "\n");
953 data.schemata = schemata;
954 data.nschemata = numschemata;
955 data.schemadata = schemadata;
956 data.schemadatalen = schemadataend - data.schemadata;
958 /******* Part 6: Data ********************************************/
960 idarraydatap = idarraydataend = 0;
963 maxsize = read_id(&data, 0);
964 allsize = read_id(&data, 0);
965 maxsize += 5; /* so we can read the next schema of an array */
966 if (maxsize > allsize)
969 buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
974 if (l < DATA_READ_CHUNK)
978 if (!l || fread(buf, l, 1, data.fp) != 1)
980 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
981 data.error = SOLV_ERROR_EOF;
988 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
991 incore_add_id(&data, 0); /* XXX? */
992 incore_add_id(&data, id);
993 keyp = schemadata + schemata[id];
994 data.mainschema = id;
995 for (i = 0; keyp[i]; i++)
998 data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
1006 /* make sure we have enough room */
1007 if (keydepth == 0 || needchunk)
1009 int left = bufend - dp;
1010 /* read data chunk to dp */
1015 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1016 data.error = SOLV_ERROR_EOF;
1022 memmove(buf, dp, left);
1024 if (l < DATA_READ_CHUNK)
1025 l = DATA_READ_CHUNK;
1028 if (l && fread(buf + left, l, 1, data.fp) != 1)
1030 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
1031 data.error = SOLV_ERROR_EOF;
1036 bufend = buf + left;
1037 if (allsize + left < maxsize)
1038 maxsize = allsize + left;
1046 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1054 if (s && keydepth == 3)
1056 s++; /* next solvable */
1058 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1060 id = stack[keydepth - 1];
1063 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1064 incore_add_id(&data, id);
1066 keyp = schemadata + schemata[id];
1073 keyp = schemadata + stack[--keydepth];
1074 nentries = stack[--keydepth];
1076 printf("pop flexarray %d %d\n", keydepth, nentries);
1079 s = 0; /* back from solvables */
1084 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1087 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1089 id = keys[key].name;
1090 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1093 dp = data_skip(dp, REPOKEY_TYPE_ID);
1094 dp = data_skip(dp, REPOKEY_TYPE_ID);
1095 incore_add_blob(&data, dps, dp - dps); /* just record offset/size */
1098 switch (keys[key].type)
1100 case REPOKEY_TYPE_ID:
1101 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1102 if (s && id == SOLVABLE_NAME)
1104 else if (s && id == SOLVABLE_ARCH)
1106 else if (s && id == SOLVABLE_EVR)
1108 else if (s && id == SOLVABLE_VENDOR)
1110 else if (keys[key].storage == KEY_STORAGE_INCORE)
1111 incore_add_id(&data, did);
1113 POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1116 case REPOKEY_TYPE_IDARRAY:
1117 case REPOKEY_TYPE_REL_IDARRAY:
1118 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1121 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1122 if (keys[key].storage != KEY_STORAGE_INCORE)
1125 incore_map_idarray(&data, dps, idmap, numid + numrel);
1127 incore_add_blob(&data, dps, dp - dps);
1130 ido = idarraydatap - repo->idarraydata;
1131 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1132 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1133 else if (id == SOLVABLE_REQUIRES)
1134 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1135 else if (id == SOLVABLE_PROVIDES)
1136 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1138 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1139 if (idarraydatap > idarraydataend)
1141 pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1142 data.error = SOLV_ERROR_OVERFLOW;
1145 if (id == SOLVABLE_PROVIDES)
1147 else if (id == SOLVABLE_OBSOLETES)
1149 else if (id == SOLVABLE_CONFLICTS)
1151 else if (id == SOLVABLE_REQUIRES)
1153 else if (id == SOLVABLE_RECOMMENDS)
1155 else if (id == SOLVABLE_SUPPLEMENTS)
1156 s->supplements = ido;
1157 else if (id == SOLVABLE_SUGGESTS)
1159 else if (id == SOLVABLE_ENHANCES)
1162 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1163 for (; repo->idarraydata[ido]; ido++)
1164 POOL_DEBUG(SOLV_DEBUG_STATS," %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1167 case REPOKEY_TYPE_FIXARRAY:
1168 case REPOKEY_TYPE_FLEXARRAY:
1171 if (keydepth == sizeof(stack)/sizeof(*stack))
1173 pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1174 data.error = SOLV_ERROR_CORRUPT;
1177 stack[keydepth++] = nentries;
1178 stack[keydepth++] = keyp - schemadata;
1179 stack[keydepth++] = 0;
1180 dp = data_read_id_max(dp, &nentries, 0, 0, &data.error);
1181 incore_add_id(&data, nentries);
1184 /* zero size array? */
1186 nentries = stack[--keydepth];
1189 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1191 /* horray! here come the solvables */
1192 if (nentries != numsolv)
1194 pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1195 data.error = SOLV_ERROR_CORRUPT;
1200 pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1201 data.error = SOLV_ERROR_CORRUPT;
1205 s = pool_id2solvable(pool, parent->start);
1207 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1208 data.start = s - pool->solvables;
1209 data.end = data.start + numsolv;
1210 repodata_extend_block(&data, data.start, numsolv);
1211 for (i = 1; i < numkeys; i++)
1214 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1215 && id >= INTERESTED_START && id <= INTERESTED_END)
1216 size_idarray += keys[i].size;
1218 /* allocate needed space in repo */
1219 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1220 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1221 idarraydatap = repo->idarraydata + repo->idarraysize;
1222 repo->idarraysize += size_idarray;
1223 idarraydataend = idarraydatap + size_idarray;
1226 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1229 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1230 incore_add_id(&data, id);
1231 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1235 pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1236 data.error = SOLV_ERROR_CORRUPT;
1238 stack[keydepth - 1] = id;
1240 keyp = schemadata + schemata[id];
1243 if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1245 if (keys[key].type == REPOKEY_TYPE_U32)
1246 dp = data_read_u32(dp, (unsigned int *)&id);
1248 dp = data_read_id_max(dp, &id, 0, 0, &data.error);
1250 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1251 repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1255 dp = data_skip(dp, keys[key].type);
1256 if (keys[key].storage == KEY_STORAGE_INCORE)
1257 incore_add_blob(&data, dps, dp - dps);
1261 /* should shrink idarraydata again */
1265 pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1266 data.error = SOLV_ERROR_CORRUPT;
1272 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1273 data.error = SOLV_ERROR_EOF;
1280 /* free solvables */
1281 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1283 repo->idarraysize -= size_idarray;
1284 /* free incore data */
1285 data.incoredata = solv_free(data.incoredata);
1286 data.incoredatalen = data.incoredatafree = 0;
1289 if (data.incoredatafree)
1291 /* shrink excess size */
1292 data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1293 data.incoredatafree = 0;
1296 for (i = 1; i < numkeys; i++)
1297 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1299 if (i < numkeys && !data.error)
1302 unsigned int pagesize;
1304 /* we have vertical data, make it available */
1305 data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1306 for (i = 1; i < numkeys; i++)
1307 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1309 data.verticaloffset[i] = fileoffset;
1310 fileoffset += keys[i].size;
1312 data.lastverticaloffset = fileoffset;
1313 pagesize = read_u32(&data);
1314 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1318 /* no longer needed */
1326 /* XXX: free repodata? */
1332 /* overwrite stub repodata */
1333 repodata_freedata(parent);
1334 data.repodataid = parent->repodataid;
1339 /* make it available as new repodata */
1340 if (!repo->nrepodata)
1342 repo->nrepodata = 1;
1343 repo->repodata = solv_calloc(2, sizeof(data));
1346 repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1347 data.repodataid = repo->nrepodata;
1348 repo->repodata[repo->nrepodata++] = data;
1351 /* create stub repodata entries for all external */
1352 if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1354 for (key = 1 ; key < data.nkeys; key++)
1355 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1357 if (key < data.nkeys)
1358 repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1361 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1362 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1363 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));