2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Add a repo in solv format
22 #include "repo_solv.h"
28 #include "poolid_private.h" /* WHATPROVIDES_BLOCK */
30 #define INTERESTED_START SOLVABLE_NAME
31 #define INTERESTED_END SOLVABLE_ENHANCES
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
42 /*******************************************************************************
43 * functions to extract data from a file handle
51 read_u32(Repodata *data)
58 for (i = 0; i < 4; i++)
63 pool_debug(data->repo->pool, SOLV_ERROR, "unexpected EOF\n");
64 data->error = SOLV_ERROR_EOF;
78 read_u8(Repodata *data)
87 pool_debug(data->repo->pool, SOLV_ERROR, "unexpected EOF\n");
88 data->error = SOLV_ERROR_EOF;
100 read_id(Repodata *data, Id max)
107 for (i = 0; i < 5; i++)
112 pool_debug(data->repo->pool, SOLV_ERROR, "unexpected EOF\n");
113 data->error = SOLV_ERROR_EOF;
121 pool_debug(data->repo->pool, SOLV_ERROR, "read_id: id too large (%u/%u)\n", x, max);
122 data->error = SOLV_ERROR_ID_RANGE;
127 x = (x << 7) ^ c ^ 128;
129 pool_debug(data->repo->pool, SOLV_ERROR, "read_id: id too long\n");
130 data->error = SOLV_ERROR_CORRUPT;
136 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
148 pool_debug(data->repo->pool, SOLV_ERROR, "unexpected EOF\n");
149 data->error = SOLV_ERROR_EOF;
154 x = (x << 7) ^ c ^ 128;
157 x = (x << 6) | (c & 63);
160 pool_debug(data->repo->pool, SOLV_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
161 data->error = SOLV_ERROR_ID_RANGE;
168 pool_debug(data->repo->pool, SOLV_ERROR, "read_idarray: array overflow\n");
174 if (x == 0) /* already have trailing zero? */
178 pool_debug(data->repo->pool, SOLV_ERROR, "read_idarray: array overflow\n");
179 data->error = SOLV_ERROR_OVERFLOW;
190 /*******************************************************************************
191 * functions to extract data from memory
198 static inline unsigned char *
199 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, Repodata *data)
202 dp = data_read_id(dp, &x);
203 if (x < 0 || (max && x >= max))
205 pool_debug(data->repo->pool, SOLV_ERROR, "data_read_id_max: id too large (%u/%u)\n", x, max);
206 data->error = SOLV_ERROR_ID_RANGE;
209 *ret = map ? map[x] : x;
213 static unsigned char *
214 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, Repodata *data)
225 x = (x << 7) ^ c ^ 128;
228 x = (x << 6) | (c & 63);
231 pool_debug(data->repo->pool, SOLV_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
232 data->error = SOLV_ERROR_ID_RANGE;
245 static unsigned char *
246 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, Repodata *data, Id marker)
258 x = (x << 7) ^ c ^ 128;
261 x = (x << 6) | (c & 63);
275 pool_debug(data->repo->pool, SOLV_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
276 data->error = SOLV_ERROR_ID_RANGE;
279 *store++ = map ? map[x] : x;
292 /*******************************************************************************
293 * functions to add data to our incore memory space
296 #define INCORE_ADD_CHUNK 8192
297 #define DATA_READ_CHUNK 8192
300 incore_add_id(Repodata *data, Id sx)
302 unsigned int x = (unsigned int)sx;
304 /* make sure we have at least 5 bytes free */
305 if (data->incoredatafree < 5)
307 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
308 data->incoredatafree = INCORE_ADD_CHUNK;
310 dp = data->incoredata + data->incoredatalen;
314 *dp++ = (x >> 28) | 128;
316 *dp++ = (x >> 21) | 128;
317 *dp++ = (x >> 14) | 128;
320 *dp++ = (x >> 7) | 128;
322 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
323 data->incoredatalen = dp - data->incoredata;
327 incore_add_ideof(Repodata *data, Id sx, int eof)
329 unsigned int x = (unsigned int)sx;
331 /* make sure we have at least 5 bytes free */
332 if (data->incoredatafree < 5)
334 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
335 data->incoredatafree = INCORE_ADD_CHUNK;
337 dp = data->incoredata + data->incoredatalen;
341 *dp++ = (x >> 27) | 128;
343 *dp++ = (x >> 20) | 128;
344 *dp++ = (x >> 13) | 128;
347 *dp++ = (x >> 6) | 128;
348 *dp++ = eof ? (x & 63) : (x & 63) | 64;
349 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
350 data->incoredatalen = dp - data->incoredata;
354 incore_add_blob(Repodata *data, unsigned char *buf, int len)
356 if (data->incoredatafree < len)
358 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
359 data->incoredatafree = INCORE_ADD_CHUNK + len;
361 memcpy(data->incoredata + data->incoredatalen, buf, len);
362 data->incoredatafree -= len;
363 data->incoredatalen += len;
367 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
369 /* We have to map the IDs, which might also change
370 the necessary number of bytes, so we can't just copy
371 over the blob and adjust it. */
376 dp = data_read_ideof(dp, &id, &eof);
377 if (id < 0 || (max && id >= max))
379 pool_debug(data->repo->pool, SOLV_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
380 data->error = SOLV_ERROR_ID_RANGE;
384 incore_add_ideof(data, id, eof);
392 incore_add_u32(Repodata *data, unsigned int x)
395 /* make sure we have at least 4 bytes free */
396 if (data->incoredatafree < 4)
398 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
399 data->incoredatafree = INCORE_ADD_CHUNK;
401 dp = data->incoredata + data->incoredatalen;
406 data->incoredatafree -= 4;
407 data->incoredatalen += 4;
411 incore_add_u8(Repodata *data, unsigned int x)
414 /* make sure we have at least 1 byte free */
415 if (data->incoredatafree < 1)
417 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + 1024);
418 data->incoredatafree = 1024;
420 dp = data->incoredata + data->incoredatalen;
422 data->incoredatafree--;
423 data->incoredatalen++;
428 /*******************************************************************************
433 * read repo from .solv file and add it to pool
437 repo_add_solv(Repo *repo, FILE *fp, int flags)
439 Pool *pool = repo->pool;
441 unsigned int numid, numrel, numdir, numsolv;
442 unsigned int numkeys, numschemata;
445 Offset *str; /* map Id -> Offset into string space */
446 char *strsp; /* repo string space */
447 char *sp; /* pointer into string space */
448 Id *idmap; /* map of repo Ids to pool Ids */
450 unsigned int hashmask, h;
456 unsigned int size_idarray;
457 Id *idarraydatap, *idarraydataend;
460 unsigned int solvflags;
461 unsigned int solvversion;
463 Id *schemadata, *schemadatap, *schemadataend;
464 Id *schemata, key, *keyp;
467 int maxsize, allsize;
468 unsigned char *buf, *bufend, *dp, *dps;
471 int needchunk; /* need a new chunk of data */
473 int oldnstrings = pool->ss.nstrings;
474 int oldnrels = pool->nrels;
476 struct _Stringpool *spool;
478 Repodata *parent = 0;
481 now = solv_timems(0);
483 if ((flags & REPO_USE_LOADING) != 0)
485 /* this is a stub replace operation */
486 flags |= REPO_EXTEND_SOLVABLES;
487 /* use REPO_REUSE_REPODATA hack so that the old repodata is kept */
488 parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
491 memset(&data, 0, sizeof(data));
494 repopagestore_init(&data.store);
496 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
498 pool_debug(pool, SOLV_ERROR, "not a SOLV file\n");
499 return SOLV_ERROR_NOT_SOLV;
501 solvversion = read_u32(&data);
507 pool_debug(pool, SOLV_ERROR, "unsupported SOLV version\n");
508 return SOLV_ERROR_UNSUPPORTED;
511 numid = read_u32(&data);
512 numrel = read_u32(&data);
513 numdir = read_u32(&data);
514 numsolv = read_u32(&data);
515 numkeys = read_u32(&data);
516 numschemata = read_u32(&data);
517 solvflags = read_u32(&data);
519 if (numdir && numdir < 2)
521 pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
522 return SOLV_ERROR_CORRUPT;
525 if (numrel && (flags & REPO_LOCALPOOL) != 0)
527 pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
528 return SOLV_ERROR_CORRUPT;
530 if (parent && numsolv)
532 /* make sure that we exactly replace the stub repodata */
533 if (parent->end - parent->start != numsolv)
535 pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
536 return SOLV_ERROR_CORRUPT;
538 for (i = 0; i < numsolv; i++)
539 if (pool->solvables[parent->start + i].repo != repo)
541 pool_debug(pool, SOLV_ERROR, "main repository contains holes\n");
542 return SOLV_ERROR_CORRUPT;
546 /******* Part 1: string IDs *****************************************/
548 sizeid = read_u32(&data); /* size of string+Id space */
551 * read strings and Ids
560 if (!(flags & REPO_LOCALPOOL))
566 spool->stringspace = solv_malloc(7);
567 strcpy(spool->stringspace, "<NULL>");
572 /* alloc string buffer */
573 spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
574 /* alloc string offsets (Id -> Offset into string space) */
575 spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
577 strsp = spool->stringspace;
578 str = spool->strings; /* array of offsets into strsp, indexed by Id */
580 /* point to _BEHIND_ already allocated string/Id space */
581 strsp += spool->sstrings;
585 * read new repo at end of pool
588 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
590 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
592 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
593 return SOLV_ERROR_EOF;
598 unsigned int pfsize = read_u32(&data);
599 char *prefix = solv_malloc(pfsize);
605 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
607 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
609 return SOLV_ERROR_EOF;
611 for (i = 1; i < numid; i++)
613 int same = (unsigned char)*pp++;
614 size_t len = strlen(pp) + 1;
615 freesp -= same + len;
618 pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
620 return SOLV_ERROR_OVERFLOW;
623 memcpy(dest, old_str, same);
624 memcpy(dest + same, pp, len);
632 pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
633 return SOLV_ERROR_CORRUPT;
636 strsp[sizeid] = 0; /* make string space \0 terminated */
639 if ((flags & REPO_LOCALPOOL) != 0)
641 /* no shared pool, thus no idmap and no unification */
643 spool->nstrings = numid;
647 /* we need the '' for directories */
648 pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
649 return SOLV_ERROR_CORRUPT;
651 for (i = 1; i < spool->nstrings; i++)
653 if (sp >= strsp + sizeid)
655 pool_debug(pool, SOLV_ERROR, "not enough strings\n");
656 return SOLV_ERROR_OVERFLOW;
658 str[i] = sp - spool->stringspace;
659 sp += strlen(sp) + 1;
661 spool->sstrings = sp - spool->stringspace;
665 /* alloc id map for name and rel Ids. this maps ids in the solv files
666 * to the ids in our pool */
667 idmap = solv_calloc(numid + numrel, sizeof(Id));
669 /* grow hash if needed, otherwise reuse */
670 hashmask = mkmask(spool->nstrings + numid);
672 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
673 POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d, old %d\n", hashmask + 1, spool->stringhashmask + 1);
675 if (hashmask > spool->stringhashmask)
677 spool->stringhashtbl = solv_free(spool->stringhashtbl);
678 spool->stringhashmask = hashmask;
679 spool->stringhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
680 for (i = 1; i < spool->nstrings; i++)
682 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
683 hh = HASHCHAIN_START;
685 h = HASHCHAIN_NEXT(h, hh, hashmask);
691 hashtbl = spool->stringhashtbl;
692 hashmask = spool->stringhashmask;
696 * run over strings and merge with pool.
697 * also populate id map (maps solv Id -> pool Id)
699 for (i = 1; i < numid; i++)
701 if (sp >= strsp + sizeid)
705 pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
706 return SOLV_ERROR_OVERFLOW;
708 if (!*sp) /* empty string */
716 h = strhash(sp) & hashmask;
717 hh = HASHCHAIN_START;
723 if (!strcmp(spool->stringspace + spool->strings[id], sp))
724 break; /* already in pool */
725 h = HASHCHAIN_NEXT(h, hh, hashmask);
728 /* length == offset to next string */
730 if (!id) /* end of hash chain -> new string */
732 id = spool->nstrings++;
734 str[id] = spool->sstrings; /* save offset */
735 if (sp != spool->stringspace + spool->sstrings)
736 memmove(spool->stringspace + spool->sstrings, sp, l);
737 spool->sstrings += l;
739 idmap[i] = id; /* repo relative -> pool relative */
740 sp += l; /* next string */
742 if (hashmask > mkmask(spool->nstrings + 8192))
744 spool->stringhashtbl = solv_free(spool->stringhashtbl);
745 spool->stringhashmask = 0;
748 pool_shrink_strings(pool); /* vacuum */
751 /******* Part 2: Relation IDs ***************************************/
761 pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
764 /* grow hash if needed, otherwise reuse */
765 hashmask = mkmask(pool->nrels + numrel);
767 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
768 POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d, old %d\n", hashmask + 1, pool->relhashmask + 1);
770 if (hashmask > pool->relhashmask)
772 pool->relhashtbl = solv_free(pool->relhashtbl);
773 pool->relhashmask = hashmask;
774 pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
775 for (i = 1; i < pool->nrels; i++)
777 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
778 hh = HASHCHAIN_START;
780 h = HASHCHAIN_NEXT(h, hh, hashmask);
786 hashtbl = pool->relhashtbl;
787 hashmask = pool->relhashmask;
791 * read RelDeps from repo
793 for (i = 0; i < numrel; i++)
795 name = read_id(&data, i + numid); /* read (repo relative) Ids */
796 evr = read_id(&data, i + numid);
797 relflags = read_u8(&data);
798 name = idmap[name]; /* map to (pool relative) Ids */
800 h = relhash(name, evr, relflags) & hashmask;
801 hh = HASHCHAIN_START;
805 if (!id) /* end of hash chain reached */
807 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
809 h = HASHCHAIN_NEXT(h, hh, hashmask);
811 if (!id) /* new RelDep */
817 ran[id].flags = relflags;
819 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
821 if (hashmask > mkmask(pool->nrels + 4096))
823 pool->relhashtbl = solv_free(pool->relhashtbl);
824 pool->relhashmask = 0;
826 pool_shrink_rels(pool); /* vacuum */
829 /* if we added ids/rels, make room in our whatprovide arrays */
830 if (!(flags & REPO_LOCALPOOL))
832 if (pool->whatprovides && oldnstrings != pool->ss.nstrings)
834 int newlen = (pool->ss.nstrings + WHATPROVIDES_BLOCK) & ~WHATPROVIDES_BLOCK;
835 pool->whatprovides = solv_realloc2(pool->whatprovides, newlen, sizeof(Offset));
836 memset(pool->whatprovides + oldnstrings, 0, (newlen - oldnstrings) * sizeof(Offset));
838 if (pool->whatprovides_rel && oldnrels != pool->nrels)
840 int newlen = (pool->nrels + WHATPROVIDES_BLOCK) & ~WHATPROVIDES_BLOCK;
841 pool->whatprovides_rel = solv_realloc2(pool->whatprovides_rel, newlen, sizeof(Offset));
842 memset(pool->whatprovides_rel + oldnrels, 0, (newlen - oldnrels) * sizeof(Offset));
846 /******* Part 3: Dirs ***********************************************/
849 data.dirpool.dirs = solv_malloc2(numdir, sizeof(Id));
850 data.dirpool.ndirs = numdir;
851 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
852 data.dirpool.dirs[1] = 1; /* dir 1: / */
853 for (i = 2; i < numdir; i++)
855 id = read_id(&data, i + numid);
857 data.dirpool.dirs[i] = -(id - numid);
859 data.dirpool.dirs[i] = idmap[id];
861 data.dirpool.dirs[i] = id;
865 /******* Part 4: Keys ***********************************************/
867 keys = solv_calloc(numkeys, sizeof(*keys));
868 /* keys start at 1 */
869 for (i = 1; i < numkeys; i++)
871 id = read_id(&data, numid);
874 else if ((flags & REPO_LOCALPOOL) != 0)
875 id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
876 type = read_id(&data, numid);
879 else if ((flags & REPO_LOCALPOOL) != 0)
880 type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
881 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
883 pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_id2str(pool, type));
884 data.error = SOLV_ERROR_UNSUPPORTED;
885 type = REPOKEY_TYPE_VOID;
889 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
890 keys[i].storage = read_id(&data, 0);
891 /* old versions used SOLVABLE for main solvable data */
892 if (keys[i].storage == KEY_STORAGE_SOLVABLE)
893 keys[i].storage = KEY_STORAGE_INCORE;
894 if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
896 pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
897 data.error = SOLV_ERROR_UNSUPPORTED;
899 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
901 if (keys[i].storage != KEY_STORAGE_INCORE)
903 pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
904 data.error = SOLV_ERROR_UNSUPPORTED;
906 keys[i].storage = KEY_STORAGE_SOLVABLE;
908 /* cannot handle rel idarrays in incore/vertical */
909 if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
911 pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
912 data.error = SOLV_ERROR_UNSUPPORTED;
914 /* cannot handle mapped ids in vertical */
915 if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
917 pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
918 data.error = SOLV_ERROR_UNSUPPORTED;
921 if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
922 keys[i].size = idmap[keys[i].size];
924 fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
925 keys[i].size, keys[i].storage);
929 have_xdata = parent ? 1 : 0;
930 for (i = 1; i < numkeys; i++)
931 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
935 data.nkeys = numkeys;
936 for (i = 1; i < numkeys; i++)
939 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
942 /******* Part 5: Schemata ********************************************/
944 id = read_id(&data, 0);
945 schemadata = solv_calloc(id + 1, sizeof(Id));
946 schemadatap = schemadata + 1;
947 schemadataend = schemadatap + id;
948 schemata = solv_calloc(numschemata, sizeof(Id));
949 for (i = 1; i < numschemata; i++)
951 schemata[i] = schemadatap - schemadata;
952 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
954 Id *sp = schemadata + schemata[i];
955 fprintf(stderr, "schema %d:", i);
957 fprintf(stderr, " %d", *sp);
958 fprintf(stderr, "\n");
961 data.schemata = schemata;
962 data.nschemata = numschemata;
963 data.schemadata = schemadata;
964 data.schemadatalen = schemadataend - data.schemadata;
966 /******* Part 6: Data ********************************************/
968 idarraydatap = idarraydataend = 0;
971 maxsize = read_id(&data, 0);
972 allsize = read_id(&data, 0);
973 maxsize += 5; /* so we can read the next schema of an array */
974 if (maxsize > allsize)
977 buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
982 if (l < DATA_READ_CHUNK)
986 if (!l || fread(buf, l, 1, data.fp) != 1)
988 pool_debug(pool, SOLV_ERROR, "unexpected EOF\n");
989 data.error = SOLV_ERROR_EOF;
996 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
999 incore_add_id(&data, 0); /* XXX? */
1000 incore_add_id(&data, id);
1001 keyp = schemadata + schemata[id];
1002 data.mainschema = id;
1003 for (i = 0; keyp[i]; i++)
1006 data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
1014 /* make sure we have enough room */
1015 if (keydepth == 0 || needchunk)
1017 int left = bufend - dp;
1018 /* read data chunk to dp */
1023 pool_debug(pool, SOLV_ERROR, "buffer overrun\n");
1024 data.error = SOLV_ERROR_EOF;
1030 memmove(buf, dp, left);
1032 if (l < DATA_READ_CHUNK)
1033 l = DATA_READ_CHUNK;
1036 if (l && fread(buf + left, l, 1, data.fp) != 1)
1038 pool_debug(pool, SOLV_ERROR, "unexpected EOF\n");
1039 data.error = SOLV_ERROR_EOF;
1044 bufend = buf + left;
1045 if (allsize + left < maxsize)
1046 maxsize = allsize + left;
1054 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1062 if (s && keydepth == 3)
1064 s++; /* next solvable */
1066 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1068 id = stack[keydepth - 1];
1071 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
1072 incore_add_id(&data, id);
1074 keyp = schemadata + schemata[id];
1081 keyp = schemadata + stack[--keydepth];
1082 nentries = stack[--keydepth];
1084 printf("pop flexarray %d %d\n", keydepth, nentries);
1087 s = 0; /* back from solvables */
1092 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1095 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1097 id = keys[key].name;
1098 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1101 dp = data_skip(dp, REPOKEY_TYPE_ID);
1102 dp = data_skip(dp, REPOKEY_TYPE_ID);
1103 incore_add_blob(&data, dps, dp - dps); /* just record offset/size */
1106 switch (keys[key].type)
1108 case REPOKEY_TYPE_ID:
1109 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data);
1110 if (s && id == SOLVABLE_NAME)
1112 else if (s && id == SOLVABLE_ARCH)
1114 else if (s && id == SOLVABLE_EVR)
1116 else if (s && id == SOLVABLE_VENDOR)
1118 else if (keys[key].storage == KEY_STORAGE_INCORE)
1119 incore_add_id(&data, did);
1121 POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1124 case REPOKEY_TYPE_IDARRAY:
1125 case REPOKEY_TYPE_REL_IDARRAY:
1126 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1129 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1130 if (keys[key].storage != KEY_STORAGE_INCORE)
1133 incore_map_idarray(&data, dps, idmap, numid + numrel);
1135 incore_add_blob(&data, dps, dp - dps);
1138 ido = idarraydatap - repo->idarraydata;
1139 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1140 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data);
1141 else if (id == SOLVABLE_REQUIRES)
1142 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, SOLVABLE_PREREQMARKER);
1143 else if (id == SOLVABLE_PROVIDES)
1144 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, SOLVABLE_FILEMARKER);
1146 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, 0);
1147 if (idarraydatap > idarraydataend)
1149 pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1150 data.error = SOLV_ERROR_OVERFLOW;
1153 if (id == SOLVABLE_PROVIDES)
1155 else if (id == SOLVABLE_OBSOLETES)
1157 else if (id == SOLVABLE_CONFLICTS)
1159 else if (id == SOLVABLE_REQUIRES)
1161 else if (id == SOLVABLE_RECOMMENDS)
1163 else if (id == SOLVABLE_SUPPLEMENTS)
1164 s->supplements = ido;
1165 else if (id == SOLVABLE_SUGGESTS)
1167 else if (id == SOLVABLE_ENHANCES)
1170 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1171 for (; repo->idarraydata[ido]; ido++)
1172 POOL_DEBUG(SOLV_DEBUG_STATS," %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1175 case REPOKEY_TYPE_FIXARRAY:
1176 case REPOKEY_TYPE_FLEXARRAY:
1179 if (keydepth == sizeof(stack)/sizeof(*stack))
1181 pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1182 data.error = SOLV_ERROR_CORRUPT;
1185 stack[keydepth++] = nentries;
1186 stack[keydepth++] = keyp - schemadata;
1187 stack[keydepth++] = 0;
1188 dp = data_read_id_max(dp, &nentries, 0, 0, &data);
1189 incore_add_id(&data, nentries);
1192 /* zero size array? */
1194 nentries = stack[--keydepth];
1197 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1199 /* horray! here come the solvables */
1200 if (nentries != numsolv)
1202 pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1203 data.error = SOLV_ERROR_CORRUPT;
1208 pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1209 data.error = SOLV_ERROR_CORRUPT;
1213 s = pool_id2solvable(pool, parent->start);
1215 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1216 data.start = s - pool->solvables;
1217 data.end = data.start + numsolv;
1218 repodata_extend_block(&data, data.start, numsolv);
1219 for (i = 1; i < numkeys; i++)
1222 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1223 && id >= INTERESTED_START && id <= INTERESTED_END)
1224 size_idarray += keys[i].size;
1226 /* allocate needed space in repo */
1227 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1228 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1229 idarraydatap = repo->idarraydata + repo->idarraysize;
1230 repo->idarraysize += size_idarray;
1231 idarraydataend = idarraydatap + size_idarray;
1234 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1237 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
1238 incore_add_id(&data, id);
1239 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1243 pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1244 data.error = SOLV_ERROR_CORRUPT;
1246 stack[keydepth - 1] = id;
1248 keyp = schemadata + schemata[id];
1251 if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1253 if (keys[key].type == REPOKEY_TYPE_U32)
1254 dp = data_read_u32(dp, (unsigned int *)&id);
1256 dp = data_read_id_max(dp, &id, 0, 0, &data);
1258 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1259 repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1263 dp = data_skip(dp, keys[key].type);
1264 if (keys[key].storage == KEY_STORAGE_INCORE)
1265 incore_add_blob(&data, dps, dp - dps);
1269 /* should shrink idarraydata again */
1273 pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1274 data.error = SOLV_ERROR_CORRUPT;
1280 pool_debug(pool, SOLV_ERROR, "buffer overrun\n");
1281 data.error = SOLV_ERROR_EOF;
1288 /* free solvables */
1289 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1291 repo->idarraysize -= size_idarray;
1292 /* free incore data */
1293 data.incoredata = solv_free(data.incoredata);
1294 data.incoredatalen = data.incoredatafree = 0;
1297 if (data.incoredatafree)
1299 /* shrink excess size */
1300 data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1301 data.incoredatafree = 0;
1304 for (i = 1; i < numkeys; i++)
1305 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1307 if (i < numkeys && !data.error)
1310 unsigned int pagesize;
1312 /* we have vertical data, make it available */
1313 data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1314 for (i = 1; i < numkeys; i++)
1315 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1317 data.verticaloffset[i] = fileoffset;
1318 fileoffset += keys[i].size;
1320 data.lastverticaloffset = fileoffset;
1321 pagesize = read_u32(&data);
1322 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1326 /* no longer needed */
1333 /* XXX: free repodata? */
1339 /* overwrite stub repodata */
1340 repodata_freedata(parent);
1341 data.repodataid = parent->repodataid;
1346 /* make it available as new repodata */
1347 if (!repo->nrepodata)
1349 repo->nrepodata = 1;
1350 repo->repodata = solv_calloc(2, sizeof(data));
1353 repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1354 data.repodataid = repo->nrepodata;
1355 repo->repodata[repo->nrepodata++] = data;
1358 /* create stub repodata entries for all external */
1359 if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1361 for (key = 1 ; key < data.nkeys; key++)
1362 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1364 if (key < data.nkeys)
1365 repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1368 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1369 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1370 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));