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_sizek(Repodata *data, unsigned int sx)
330 incore_add_id(data, (Id)(sx << 10));
335 incore_add_id(data, (Id)(sx >> 25));
336 data->incoredata[data->incoredatalen - 1] |= 128;
338 incore_add_id(data, (Id)((sx << 10) | 0x80000000));
339 data->incoredata[data->incoredatalen - 5] = (sx >> 18) | 128;
344 incore_add_ideof(Repodata *data, Id sx, int eof)
346 unsigned int x = (unsigned int)sx;
348 /* make sure we have at least 5 bytes free */
349 if (data->incoredatafree < 5)
351 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
352 data->incoredatafree = INCORE_ADD_CHUNK;
354 dp = data->incoredata + data->incoredatalen;
358 *dp++ = (x >> 27) | 128;
360 *dp++ = (x >> 20) | 128;
361 *dp++ = (x >> 13) | 128;
364 *dp++ = (x >> 6) | 128;
365 *dp++ = eof ? (x & 63) : (x & 63) | 64;
366 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
367 data->incoredatalen = dp - data->incoredata;
371 incore_add_blob(Repodata *data, unsigned char *buf, int len)
373 if (data->incoredatafree < len)
375 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
376 data->incoredatafree = INCORE_ADD_CHUNK + len;
378 memcpy(data->incoredata + data->incoredatalen, buf, len);
379 data->incoredatafree -= len;
380 data->incoredatalen += len;
384 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
386 /* We have to map the IDs, which might also change
387 the necessary number of bytes, so we can't just copy
388 over the blob and adjust it. */
393 dp = data_read_ideof(dp, &id, &eof);
394 if (id < 0 || (max && id >= max))
396 pool_debug(data->repo->pool, SOLV_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
397 data->error = SOLV_ERROR_ID_RANGE;
401 incore_add_ideof(data, id, eof);
409 incore_add_u32(Repodata *data, unsigned int x)
412 /* make sure we have at least 4 bytes free */
413 if (data->incoredatafree < 4)
415 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
416 data->incoredatafree = INCORE_ADD_CHUNK;
418 dp = data->incoredata + data->incoredatalen;
423 data->incoredatafree -= 4;
424 data->incoredatalen += 4;
428 incore_add_u8(Repodata *data, unsigned int x)
431 /* make sure we have at least 1 byte free */
432 if (data->incoredatafree < 1)
434 data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + 1024);
435 data->incoredatafree = 1024;
437 dp = data->incoredata + data->incoredatalen;
439 data->incoredatafree--;
440 data->incoredatalen++;
445 /*******************************************************************************
450 * read repo from .solv file and add it to pool
454 repo_add_solv(Repo *repo, FILE *fp, int flags)
456 Pool *pool = repo->pool;
458 unsigned int numid, numrel, numdir, numsolv;
459 unsigned int numkeys, numschemata;
462 Offset *str; /* map Id -> Offset into string space */
463 char *strsp; /* repo string space */
464 char *sp; /* pointer into string space */
465 Id *idmap; /* map of repo Ids to pool Ids */
467 unsigned int hashmask, h;
473 unsigned int size_idarray;
474 Id *idarraydatap, *idarraydataend;
477 unsigned int solvflags;
478 unsigned int solvversion;
480 Id *schemadata, *schemadatap, *schemadataend;
481 Id *schemata, key, *keyp;
484 int maxsize, allsize;
485 unsigned char *buf, *bufend, *dp, *dps;
488 int needchunk; /* need a new chunk of data */
490 int oldnstrings = pool->ss.nstrings;
491 int oldnrels = pool->nrels;
493 struct _Stringpool *spool;
495 Repodata *parent = 0;
498 int extendstart = 0, extendend = 0; /* set in case we're extending */
500 now = solv_timems(0);
502 if ((flags & REPO_USE_LOADING) != 0)
504 /* this is a stub replace operation */
505 flags |= REPO_EXTEND_SOLVABLES;
506 /* use REPO_REUSE_REPODATA hack so that the old repodata is kept */
507 parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
508 extendstart = parent->start;
509 extendend = parent->end;
511 else if (flags & REPO_EXTEND_SOLVABLES)
513 /* extend all solvables of this repo */
514 extendstart = repo->start;
515 extendend = repo->end;
518 memset(&data, 0, sizeof(data));
521 repopagestore_init(&data.store);
523 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
525 pool_debug(pool, SOLV_ERROR, "not a SOLV file\n");
526 return SOLV_ERROR_NOT_SOLV;
528 solvversion = read_u32(&data);
534 pool_debug(pool, SOLV_ERROR, "unsupported SOLV version\n");
535 return SOLV_ERROR_UNSUPPORTED;
538 numid = read_u32(&data);
539 numrel = read_u32(&data);
540 numdir = read_u32(&data);
541 numsolv = read_u32(&data);
542 numkeys = read_u32(&data);
543 numschemata = read_u32(&data);
544 solvflags = read_u32(&data);
546 if (numdir && numdir < 2)
548 pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
549 return SOLV_ERROR_CORRUPT;
552 if (numrel && (flags & REPO_LOCALPOOL) != 0)
554 pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
555 return SOLV_ERROR_CORRUPT;
557 if ((flags & REPO_EXTEND_SOLVABLES) && numsolv)
559 /* make sure that we exactly replace the stub repodata */
560 if (extendend - extendstart != numsolv)
562 pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", extendend - extendstart, numsolv);
563 return SOLV_ERROR_CORRUPT;
565 for (i = 0; i < numsolv; i++)
566 if (pool->solvables[extendstart + i].repo != repo)
568 pool_debug(pool, SOLV_ERROR, "main repository contains holes, cannot extend\n");
569 return SOLV_ERROR_CORRUPT;
573 /******* Part 1: string IDs *****************************************/
575 sizeid = read_u32(&data); /* size of string+Id space */
578 * read strings and Ids
587 if (!(flags & REPO_LOCALPOOL))
593 spool->stringspace = solv_malloc(7);
594 strcpy(spool->stringspace, "<NULL>");
599 /* alloc string buffer */
600 spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
601 /* alloc string offsets (Id -> Offset into string space) */
602 spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
604 strsp = spool->stringspace;
605 str = spool->strings; /* array of offsets into strsp, indexed by Id */
607 /* point to _BEHIND_ already allocated string/Id space */
608 strsp += spool->sstrings;
612 * read new repo at end of pool
615 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
617 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
619 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
620 return SOLV_ERROR_EOF;
625 unsigned int pfsize = read_u32(&data);
626 char *prefix = solv_malloc(pfsize);
632 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
634 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
636 return SOLV_ERROR_EOF;
638 for (i = 1; i < numid; i++)
640 int same = (unsigned char)*pp++;
641 size_t len = strlen(pp) + 1;
642 freesp -= same + len;
645 pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
647 return SOLV_ERROR_OVERFLOW;
650 memcpy(dest, old_str, same);
651 memcpy(dest + same, pp, len);
659 pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
660 return SOLV_ERROR_CORRUPT;
663 strsp[sizeid] = 0; /* make string space \0 terminated */
666 if ((flags & REPO_LOCALPOOL) != 0)
668 /* no shared pool, thus no idmap and no unification */
670 spool->nstrings = numid;
674 /* we need the '' for directories */
675 pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
676 return SOLV_ERROR_CORRUPT;
678 for (i = 1; i < spool->nstrings; i++)
680 if (sp >= strsp + sizeid)
682 pool_debug(pool, SOLV_ERROR, "not enough strings\n");
683 return SOLV_ERROR_OVERFLOW;
685 str[i] = sp - spool->stringspace;
686 sp += strlen(sp) + 1;
688 spool->sstrings = sp - spool->stringspace;
692 /* alloc id map for name and rel Ids. this maps ids in the solv files
693 * to the ids in our pool */
694 idmap = solv_calloc(numid + numrel, sizeof(Id));
696 /* grow hash if needed, otherwise reuse */
697 hashmask = mkmask(spool->nstrings + numid);
699 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
700 POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d, old %d\n", hashmask + 1, spool->stringhashmask + 1);
702 if (hashmask > spool->stringhashmask)
704 spool->stringhashtbl = solv_free(spool->stringhashtbl);
705 spool->stringhashmask = hashmask;
706 spool->stringhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
707 for (i = 1; i < spool->nstrings; i++)
709 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
710 hh = HASHCHAIN_START;
712 h = HASHCHAIN_NEXT(h, hh, hashmask);
718 hashtbl = spool->stringhashtbl;
719 hashmask = spool->stringhashmask;
723 * run over strings and merge with pool.
724 * also populate id map (maps solv Id -> pool Id)
726 for (i = 1; i < numid; i++)
728 if (sp >= strsp + sizeid)
732 pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
733 return SOLV_ERROR_OVERFLOW;
735 if (!*sp) /* empty string */
743 h = strhash(sp) & hashmask;
744 hh = HASHCHAIN_START;
750 if (!strcmp(spool->stringspace + spool->strings[id], sp))
751 break; /* already in pool */
752 h = HASHCHAIN_NEXT(h, hh, hashmask);
755 /* length == offset to next string */
757 if (!id) /* end of hash chain -> new string */
759 id = spool->nstrings++;
761 str[id] = spool->sstrings; /* save offset */
762 if (sp != spool->stringspace + spool->sstrings)
763 memmove(spool->stringspace + spool->sstrings, sp, l);
764 spool->sstrings += l;
766 idmap[i] = id; /* repo relative -> pool relative */
767 sp += l; /* next string */
769 if (hashmask > mkmask(spool->nstrings + 8192))
771 spool->stringhashtbl = solv_free(spool->stringhashtbl);
772 spool->stringhashmask = 0;
775 pool_shrink_strings(pool); /* vacuum */
778 /******* Part 2: Relation IDs ***************************************/
788 pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
791 /* grow hash if needed, otherwise reuse */
792 hashmask = mkmask(pool->nrels + numrel);
794 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
795 POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d, old %d\n", hashmask + 1, pool->relhashmask + 1);
797 if (hashmask > pool->relhashmask)
799 pool->relhashtbl = solv_free(pool->relhashtbl);
800 pool->relhashmask = hashmask;
801 pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
802 for (i = 1; i < pool->nrels; i++)
804 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
805 hh = HASHCHAIN_START;
807 h = HASHCHAIN_NEXT(h, hh, hashmask);
813 hashtbl = pool->relhashtbl;
814 hashmask = pool->relhashmask;
818 * read RelDeps from repo
820 for (i = 0; i < numrel; i++)
822 name = read_id(&data, i + numid); /* read (repo relative) Ids */
823 evr = read_id(&data, i + numid);
824 relflags = read_u8(&data);
825 name = idmap[name]; /* map to (pool relative) Ids */
827 h = relhash(name, evr, relflags) & hashmask;
828 hh = HASHCHAIN_START;
832 if (!id) /* end of hash chain reached */
834 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
836 h = HASHCHAIN_NEXT(h, hh, hashmask);
838 if (!id) /* new RelDep */
844 ran[id].flags = relflags;
846 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
848 if (hashmask > mkmask(pool->nrels + 4096))
850 pool->relhashtbl = solv_free(pool->relhashtbl);
851 pool->relhashmask = 0;
853 pool_shrink_rels(pool); /* vacuum */
856 /* if we added ids/rels, make room in our whatprovide arrays */
857 if (!(flags & REPO_LOCALPOOL))
859 if (pool->whatprovides && oldnstrings != pool->ss.nstrings)
861 int newlen = (pool->ss.nstrings + WHATPROVIDES_BLOCK) & ~WHATPROVIDES_BLOCK;
862 pool->whatprovides = solv_realloc2(pool->whatprovides, newlen, sizeof(Offset));
863 memset(pool->whatprovides + oldnstrings, 0, (newlen - oldnstrings) * sizeof(Offset));
865 if (pool->whatprovides_rel && oldnrels != pool->nrels)
867 int newlen = (pool->nrels + WHATPROVIDES_BLOCK) & ~WHATPROVIDES_BLOCK;
868 pool->whatprovides_rel = solv_realloc2(pool->whatprovides_rel, newlen, sizeof(Offset));
869 memset(pool->whatprovides_rel + oldnrels, 0, (newlen - oldnrels) * sizeof(Offset));
873 /******* Part 3: Dirs ***********************************************/
876 data.dirpool.dirs = solv_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 = solv_calloc(numkeys, sizeof(*keys));
895 /* keys start at 1 */
896 for (i = 1; i < numkeys; i++)
898 id = read_id(&data, numid);
901 else if ((flags & REPO_LOCALPOOL) != 0)
902 id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
903 type = read_id(&data, numid);
906 else if ((flags & REPO_LOCALPOOL) != 0)
907 type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
908 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
910 pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_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 /* old versions used SOLVABLE for main solvable data */
919 if (keys[i].storage == KEY_STORAGE_SOLVABLE)
920 keys[i].storage = KEY_STORAGE_INCORE;
921 if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
923 pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
924 data.error = SOLV_ERROR_UNSUPPORTED;
926 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
928 if (keys[i].storage != KEY_STORAGE_INCORE)
930 pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
931 data.error = SOLV_ERROR_UNSUPPORTED;
933 keys[i].storage = KEY_STORAGE_SOLVABLE;
935 /* cannot handle rel idarrays in incore/vertical */
936 if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
938 pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
939 data.error = SOLV_ERROR_UNSUPPORTED;
941 /* cannot handle mapped ids in vertical */
942 if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
944 pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
945 data.error = SOLV_ERROR_UNSUPPORTED;
948 if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
949 keys[i].size = idmap[keys[i].size];
951 fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
952 keys[i].size, keys[i].storage);
957 for (i = 1; i < numkeys; i++)
958 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
962 data.nkeys = numkeys;
963 for (i = 1; i < numkeys; i++)
966 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
969 /******* Part 5: Schemata ********************************************/
971 id = read_id(&data, 0);
972 schemadata = solv_calloc(id + 1, sizeof(Id));
973 schemadatap = schemadata + 1;
974 schemadataend = schemadatap + id;
975 schemata = solv_calloc(numschemata, sizeof(Id));
976 for (i = 1; i < numschemata; i++)
978 schemata[i] = schemadatap - schemadata;
979 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
981 Id *sp = schemadata + schemata[i];
982 fprintf(stderr, "schema %d:", i);
984 fprintf(stderr, " %d", *sp);
985 fprintf(stderr, "\n");
988 data.schemata = schemata;
989 data.nschemata = numschemata;
990 data.schemadata = schemadata;
991 data.schemadatalen = schemadataend - data.schemadata;
993 /******* Part 6: Data ********************************************/
995 idarraydatap = idarraydataend = 0;
998 maxsize = read_id(&data, 0);
999 allsize = read_id(&data, 0);
1000 maxsize += 5; /* so we can read the next schema of an array */
1001 if (maxsize > allsize)
1004 buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
1009 if (l < DATA_READ_CHUNK)
1010 l = DATA_READ_CHUNK;
1013 if (!l || fread(buf, l, 1, data.fp) != 1)
1015 pool_debug(pool, SOLV_ERROR, "unexpected EOF\n");
1016 data.error = SOLV_ERROR_EOF;
1023 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
1026 incore_add_id(&data, 0); /* so that incoreoffset 0 means schema 0 */
1027 incore_add_id(&data, id); /* main schema id */
1028 keyp = schemadata + schemata[id];
1029 data.mainschema = id;
1030 for (i = 0; keyp[i]; i++)
1033 data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
1041 /* make sure we have enough room */
1042 if (keydepth == 0 || needchunk)
1044 int left = bufend - dp;
1045 /* read data chunk to dp */
1050 pool_debug(pool, SOLV_ERROR, "buffer overrun\n");
1051 data.error = SOLV_ERROR_EOF;
1057 memmove(buf, dp, left);
1059 if (l < DATA_READ_CHUNK)
1060 l = DATA_READ_CHUNK;
1063 if (l && fread(buf + left, l, 1, data.fp) != 1)
1065 pool_debug(pool, SOLV_ERROR, "unexpected EOF\n");
1066 data.error = SOLV_ERROR_EOF;
1071 bufend = buf + left;
1072 if (allsize + left < maxsize)
1073 maxsize = allsize + left;
1081 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1089 if (s && keydepth == 3)
1091 s++; /* next solvable */
1092 if (have_incoredata)
1093 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1095 id = stack[keydepth - 1];
1098 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
1099 incore_add_id(&data, id);
1101 keyp = schemadata + schemata[id];
1108 keyp = schemadata + stack[--keydepth];
1109 nentries = stack[--keydepth];
1111 printf("pop flexarray %d %d\n", keydepth, nentries);
1114 s = 0; /* back from solvables */
1119 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1122 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1124 id = keys[key].name;
1125 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1128 dp = data_skip(dp, REPOKEY_TYPE_ID);
1129 dp = data_skip(dp, REPOKEY_TYPE_ID);
1130 incore_add_blob(&data, dps, dp - dps); /* just record offset/size */
1133 switch (keys[key].type)
1135 case REPOKEY_TYPE_ID:
1136 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data);
1137 if (s && id == SOLVABLE_NAME)
1139 else if (s && id == SOLVABLE_ARCH)
1141 else if (s && id == SOLVABLE_EVR)
1143 else if (s && id == SOLVABLE_VENDOR)
1145 else if (keys[key].storage == KEY_STORAGE_INCORE)
1146 incore_add_id(&data, did);
1148 POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1151 case REPOKEY_TYPE_IDARRAY:
1152 case REPOKEY_TYPE_REL_IDARRAY:
1153 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1156 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1157 if (keys[key].storage != KEY_STORAGE_INCORE)
1160 incore_map_idarray(&data, dps, idmap, numid + numrel);
1162 incore_add_blob(&data, dps, dp - dps);
1165 ido = idarraydatap - repo->idarraydata;
1166 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1167 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data);
1168 else if (id == SOLVABLE_REQUIRES)
1169 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, SOLVABLE_PREREQMARKER);
1170 else if (id == SOLVABLE_PROVIDES)
1171 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, SOLVABLE_FILEMARKER);
1173 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data, 0);
1174 if (idarraydatap > idarraydataend)
1176 pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1177 data.error = SOLV_ERROR_OVERFLOW;
1180 if (id == SOLVABLE_PROVIDES)
1182 else if (id == SOLVABLE_OBSOLETES)
1184 else if (id == SOLVABLE_CONFLICTS)
1186 else if (id == SOLVABLE_REQUIRES)
1188 else if (id == SOLVABLE_RECOMMENDS)
1190 else if (id == SOLVABLE_SUPPLEMENTS)
1191 s->supplements = ido;
1192 else if (id == SOLVABLE_SUGGESTS)
1194 else if (id == SOLVABLE_ENHANCES)
1197 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1198 for (; repo->idarraydata[ido]; ido++)
1199 POOL_DEBUG(SOLV_DEBUG_STATS," %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1202 case REPOKEY_TYPE_FIXARRAY:
1203 case REPOKEY_TYPE_FLEXARRAY:
1206 if (keydepth == sizeof(stack)/sizeof(*stack))
1208 pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1209 data.error = SOLV_ERROR_CORRUPT;
1212 stack[keydepth++] = nentries;
1213 stack[keydepth++] = keyp - schemadata;
1214 stack[keydepth++] = 0;
1215 dp = data_read_id_max(dp, &nentries, 0, 0, &data);
1216 incore_add_id(&data, nentries);
1219 /* zero size array? */
1221 nentries = stack[--keydepth];
1224 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1226 /* horray! here come the solvables */
1227 if (nentries != numsolv)
1229 pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1230 data.error = SOLV_ERROR_CORRUPT;
1235 pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1236 data.error = SOLV_ERROR_CORRUPT;
1239 if ((flags & REPO_EXTEND_SOLVABLES) != 0)
1240 s = pool_id2solvable(pool, extendstart);
1242 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1243 data.start = s - pool->solvables;
1244 data.end = data.start + numsolv;
1245 repodata_extend_block(&data, data.start, numsolv);
1246 for (i = 1; i < numkeys; i++)
1249 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1250 && id >= INTERESTED_START && id <= INTERESTED_END)
1251 size_idarray += keys[i].size;
1253 /* allocate needed space in repo */
1254 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1255 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1256 idarraydatap = repo->idarraydata + repo->idarraysize;
1257 repo->idarraysize += size_idarray;
1258 idarraydataend = idarraydatap + size_idarray;
1260 if (have_incoredata)
1261 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1264 dp = data_read_id_max(dp, &id, 0, numschemata, &data);
1265 incore_add_id(&data, id);
1266 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1270 pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1271 data.error = SOLV_ERROR_CORRUPT;
1273 stack[keydepth - 1] = id;
1275 keyp = schemadata + schemata[id];
1277 case REPOKEY_TYPE_NUM:
1278 if (!(solvflags & SOLV_FLAG_SIZE_BYTES) && keys[key].storage == KEY_STORAGE_INCORE &&
1279 (id == SOLVABLE_INSTALLSIZE || id == SOLVABLE_DOWNLOADSIZE || id == DELTA_DOWNLOADSIZE))
1281 /* old solv file with sizes in kilos. transcode. */
1282 dp = data_read_id(dp, &id);
1283 incore_add_sizek(&data, (unsigned int)id);
1288 if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1290 if (keys[key].type == REPOKEY_TYPE_U32)
1291 dp = data_read_u32(dp, (unsigned int *)&id);
1293 dp = data_read_id_max(dp, &id, 0, 0, &data);
1295 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1296 repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1300 dp = data_skip(dp, keys[key].type);
1301 if (keys[key].storage == KEY_STORAGE_INCORE)
1302 incore_add_blob(&data, dps, dp - dps);
1306 /* should shrink idarraydata again */
1310 pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1311 data.error = SOLV_ERROR_CORRUPT;
1317 pool_debug(pool, SOLV_ERROR, "buffer overrun\n");
1318 data.error = SOLV_ERROR_EOF;
1325 /* free solvables */
1326 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1328 repo->idarraysize -= size_idarray;
1329 /* free incore data */
1330 data.incoredata = solv_free(data.incoredata);
1331 data.incoredatalen = data.incoredatafree = 0;
1334 if (data.incoredatafree)
1336 /* shrink excess size */
1337 data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1338 data.incoredatafree = 0;
1341 for (i = 1; i < numkeys; i++)
1342 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1344 if (i < numkeys && !data.error)
1347 unsigned int pagesize;
1349 /* we have vertical data, make it available */
1350 data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1351 for (i = 1; i < numkeys; i++)
1352 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1354 data.verticaloffset[i] = fileoffset;
1355 fileoffset += keys[i].size;
1357 data.lastverticaloffset = fileoffset;
1358 pagesize = read_u32(&data);
1359 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1363 /* no longer needed */
1370 /* XXX: free repodata? */
1376 /* overwrite stub repodata */
1377 repodata_freedata(parent);
1378 data.repodataid = parent->repodataid;
1383 /* make it available as new repodata */
1384 if (!repo->nrepodata)
1386 repo->nrepodata = 1;
1387 repo->repodata = solv_calloc(2, sizeof(data));
1390 repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1391 data.repodataid = repo->nrepodata;
1392 repo->repodata[repo->nrepodata++] = data;
1395 /* create stub repodata entries for all external */
1396 if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1398 for (key = 1 ; key < data.nkeys; key++)
1399 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1401 if (key < data.nkeys)
1402 repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1405 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1406 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1407 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));