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 pool_freeidhashes(pool);
517 numid = read_u32(&data);
518 numrel = read_u32(&data);
519 numdir = read_u32(&data);
520 numsolv = read_u32(&data);
521 numkeys = read_u32(&data);
522 numschemata = read_u32(&data);
523 solvflags = read_u32(&data);
525 if (numdir && numdir < 2)
527 pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
528 return SOLV_ERROR_CORRUPT;
531 if (numrel && (flags & REPO_LOCALPOOL) != 0)
533 pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
534 return SOLV_ERROR_CORRUPT;
536 if (parent && numsolv)
538 /* make sure that we exactly replace the stub repodata */
539 if (parent->end - parent->start != numsolv)
541 pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
542 return SOLV_ERROR_CORRUPT;
544 for (i = 0; i < numsolv; i++)
545 if (pool->solvables[parent->start + i].repo != repo)
547 pool_debug(pool, SOLV_ERROR, "main repository contains holes\n");
548 return SOLV_ERROR_CORRUPT;
552 /******* Part 1: string IDs *****************************************/
554 sizeid = read_u32(&data); /* size of string+Id space */
557 * read strings and Ids
566 if (!(flags & REPO_LOCALPOOL))
572 spool->stringspace = solv_malloc(7);
573 strcpy(spool->stringspace, "<NULL>");
578 /* alloc string buffer */
579 spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
580 /* alloc string offsets (Id -> Offset into string space) */
581 spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
583 strsp = spool->stringspace;
584 str = spool->strings; /* array of offsets into strsp, indexed by Id */
586 /* point to _BEHIND_ already allocated string/Id space */
587 strsp += spool->sstrings;
591 * read new repo at end of pool
594 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
596 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
598 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
599 return SOLV_ERROR_EOF;
604 unsigned int pfsize = read_u32(&data);
605 char *prefix = solv_malloc(pfsize);
611 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
613 pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
615 return SOLV_ERROR_EOF;
617 for (i = 1; i < numid; i++)
619 int same = (unsigned char)*pp++;
620 size_t len = strlen(pp) + 1;
621 freesp -= same + len;
624 pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
626 return SOLV_ERROR_OVERFLOW;
629 memcpy(dest, old_str, same);
630 memcpy(dest + same, pp, len);
638 pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
639 return SOLV_ERROR_CORRUPT;
642 strsp[sizeid] = 0; /* make string space \0 terminated */
645 if ((flags & REPO_LOCALPOOL) != 0)
647 /* no shared pool, thus no idmap and no unification */
649 spool->nstrings = numid;
653 /* we need the '' for directories */
654 pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
655 return SOLV_ERROR_CORRUPT;
657 for (i = 1; i < spool->nstrings; i++)
659 if (sp >= strsp + sizeid)
661 pool_debug(pool, SOLV_ERROR, "not enough strings\n");
662 return SOLV_ERROR_OVERFLOW;
664 str[i] = sp - spool->stringspace;
665 sp += strlen(sp) + 1;
667 spool->sstrings = sp - spool->stringspace;
672 /* alloc id map for name and rel Ids. this maps ids in the solv files
673 * to the ids in our pool */
674 idmap = solv_calloc(numid + numrel, sizeof(Id));
677 * build hashes for all read strings
681 hashmask = mkmask(spool->nstrings + numid);
684 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
685 POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
689 * create hashtable with strings already in pool
692 hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
693 for (i = 1; i < spool->nstrings; i++) /* leave out our dummy zero id */
695 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
696 hh = HASHCHAIN_START;
698 h = HASHCHAIN_NEXT(h, hh, hashmask);
703 * run over string space, calculate offsets
705 * build id map (maps solv Id -> pool Id)
708 for (i = 1; i < numid; i++)
710 if (sp >= strsp + sizeid)
714 pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
715 return SOLV_ERROR_OVERFLOW;
717 if (!*sp) /* empty string */
725 h = strhash(sp) & hashmask;
726 hh = HASHCHAIN_START;
732 if (!strcmp(spool->stringspace + spool->strings[id], sp))
733 break; /* existing string */
734 h = HASHCHAIN_NEXT(h, hh, hashmask);
737 /* length == offset to next string */
739 if (id == ID_NULL) /* end of hash chain -> new string */
741 id = spool->nstrings++;
743 str[id] = spool->sstrings; /* save Offset */
744 if (sp != spool->stringspace + spool->sstrings) /* not at end-of-buffer */
745 memmove(spool->stringspace + spool->sstrings, sp, l); /* append to pool buffer */
746 spool->sstrings += l;
748 idmap[i] = id; /* repo relative -> pool relative */
749 sp += l; /* next string */
753 pool_shrink_strings(pool); /* vacuum */
756 /******* Part 2: Relation IDs ***************************************/
766 pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
769 hashmask = mkmask(pool->nrels + numrel);
771 POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
772 POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
775 * prep hash table with already existing RelDeps
778 hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
779 for (i = 1; i < pool->nrels; i++)
781 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
782 hh = HASHCHAIN_START;
784 h = HASHCHAIN_NEXT(h, hh, hashmask);
789 * read RelDeps from repo
792 for (i = 0; i < numrel; i++)
794 name = read_id(&data, i + numid); /* read (repo relative) Ids */
795 evr = read_id(&data, i + numid);
796 relflags = read_u8(&data);
797 name = idmap[name]; /* map to (pool relative) Ids */
799 h = relhash(name, evr, relflags) & hashmask;
800 hh = HASHCHAIN_START;
804 if (id == ID_NULL) /* end of hash chain */
806 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
808 h = HASHCHAIN_NEXT(h, hh, hashmask);
810 if (id == ID_NULL) /* new RelDep */
816 ran[id].flags = relflags;
818 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
821 pool_shrink_rels(pool); /* vacuum */
825 /******* Part 3: Dirs ***********************************************/
828 data.dirpool.dirs = solv_malloc2(numdir, sizeof(Id));
829 data.dirpool.ndirs = numdir;
830 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
831 data.dirpool.dirs[1] = 1; /* dir 1: / */
832 for (i = 2; i < numdir; i++)
834 id = read_id(&data, i + numid);
836 data.dirpool.dirs[i] = -(id - numid);
838 data.dirpool.dirs[i] = idmap[id];
840 data.dirpool.dirs[i] = id;
844 /******* Part 4: Keys ***********************************************/
846 keys = solv_calloc(numkeys, sizeof(*keys));
847 /* keys start at 1 */
848 for (i = 1; i < numkeys; i++)
850 id = read_id(&data, numid);
853 else if ((flags & REPO_LOCALPOOL) != 0)
854 id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
855 type = read_id(&data, numid);
858 else if ((flags & REPO_LOCALPOOL) != 0)
859 type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
860 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
862 pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_id2str(pool, type));
863 data.error = SOLV_ERROR_UNSUPPORTED;
864 type = REPOKEY_TYPE_VOID;
868 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
869 keys[i].storage = read_id(&data, 0);
870 /* old versions used SOLVABLE for main solvable data */
871 if (keys[i].storage == KEY_STORAGE_SOLVABLE)
872 keys[i].storage = KEY_STORAGE_INCORE;
873 if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
875 pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
876 data.error = SOLV_ERROR_UNSUPPORTED;
878 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
880 if (keys[i].storage != KEY_STORAGE_INCORE)
882 pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
883 data.error = SOLV_ERROR_UNSUPPORTED;
885 keys[i].storage = KEY_STORAGE_SOLVABLE;
887 /* cannot handle rel idarrays in incore/vertical */
888 if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
890 pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
891 data.error = SOLV_ERROR_UNSUPPORTED;
893 /* cannot handle mapped ids in vertical */
894 if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
896 pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
897 data.error = SOLV_ERROR_UNSUPPORTED;
900 if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
901 keys[i].size = idmap[keys[i].size];
903 fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
904 keys[i].size, keys[i].storage);
908 have_xdata = parent ? 1 : 0;
909 for (i = 1; i < numkeys; i++)
910 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
914 data.nkeys = numkeys;
915 for (i = 1; i < numkeys; i++)
918 data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
921 /******* Part 5: Schemata ********************************************/
923 id = read_id(&data, 0);
924 schemadata = solv_calloc(id + 1, sizeof(Id));
925 schemadatap = schemadata + 1;
926 schemadataend = schemadatap + id;
927 schemata = solv_calloc(numschemata, sizeof(Id));
928 for (i = 1; i < numschemata; i++)
930 schemata[i] = schemadatap - schemadata;
931 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
933 Id *sp = schemadata + schemata[i];
934 fprintf(stderr, "schema %d:", i);
936 fprintf(stderr, " %d", *sp);
937 fprintf(stderr, "\n");
940 data.schemata = schemata;
941 data.nschemata = numschemata;
942 data.schemadata = schemadata;
943 data.schemadatalen = schemadataend - data.schemadata;
945 /******* Part 6: Data ********************************************/
947 idarraydatap = idarraydataend = 0;
950 maxsize = read_id(&data, 0);
951 allsize = read_id(&data, 0);
952 maxsize += 5; /* so we can read the next schema of an array */
953 if (maxsize > allsize)
956 buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1); /* 4 extra bytes to detect overflows */
961 if (l < DATA_READ_CHUNK)
965 if (!l || fread(buf, l, 1, data.fp) != 1)
967 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
968 data.error = SOLV_ERROR_EOF;
975 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
978 incore_add_id(&data, 0); /* XXX? */
979 incore_add_id(&data, id);
980 keyp = schemadata + schemata[id];
981 data.mainschema = id;
982 for (i = 0; keyp[i]; i++)
985 data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
993 /* make sure we have enough room */
994 if (keydepth == 0 || needchunk)
996 int left = bufend - dp;
997 /* read data chunk to dp */
1002 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1003 data.error = SOLV_ERROR_EOF;
1009 memmove(buf, dp, left);
1011 if (l < DATA_READ_CHUNK)
1012 l = DATA_READ_CHUNK;
1015 if (l && fread(buf + left, l, 1, data.fp) != 1)
1017 pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
1018 data.error = SOLV_ERROR_EOF;
1023 bufend = buf + left;
1024 if (allsize + left < maxsize)
1025 maxsize = allsize + left;
1033 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1041 if (s && keydepth == 3)
1043 s++; /* next solvable */
1045 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1047 id = stack[keydepth - 1];
1050 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1051 incore_add_id(&data, id);
1053 keyp = schemadata + schemata[id];
1060 keyp = schemadata + stack[--keydepth];
1061 nentries = stack[--keydepth];
1063 printf("pop flexarray %d %d\n", keydepth, nentries);
1066 s = 0; /* back from solvables */
1071 data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1074 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1076 id = keys[key].name;
1077 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1080 dp = data_skip(dp, REPOKEY_TYPE_ID);
1081 dp = data_skip(dp, REPOKEY_TYPE_ID);
1082 incore_add_blob(&data, dps, dp - dps); /* just record offset/size */
1085 switch (keys[key].type)
1087 case REPOKEY_TYPE_ID:
1088 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1089 if (s && id == SOLVABLE_NAME)
1091 else if (s && id == SOLVABLE_ARCH)
1093 else if (s && id == SOLVABLE_EVR)
1095 else if (s && id == SOLVABLE_VENDOR)
1097 else if (keys[key].storage == KEY_STORAGE_INCORE)
1098 incore_add_id(&data, did);
1100 POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1103 case REPOKEY_TYPE_IDARRAY:
1104 case REPOKEY_TYPE_REL_IDARRAY:
1105 if (!s || id < INTERESTED_START || id > INTERESTED_END)
1108 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1109 if (keys[key].storage != KEY_STORAGE_INCORE)
1112 incore_map_idarray(&data, dps, idmap, numid + numrel);
1114 incore_add_blob(&data, dps, dp - dps);
1117 ido = idarraydatap - repo->idarraydata;
1118 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1119 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1120 else if (id == SOLVABLE_REQUIRES)
1121 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1122 else if (id == SOLVABLE_PROVIDES)
1123 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1125 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1126 if (idarraydatap > idarraydataend)
1128 pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1129 data.error = SOLV_ERROR_OVERFLOW;
1132 if (id == SOLVABLE_PROVIDES)
1134 else if (id == SOLVABLE_OBSOLETES)
1136 else if (id == SOLVABLE_CONFLICTS)
1138 else if (id == SOLVABLE_REQUIRES)
1140 else if (id == SOLVABLE_RECOMMENDS)
1142 else if (id == SOLVABLE_SUPPLEMENTS)
1143 s->supplements = ido;
1144 else if (id == SOLVABLE_SUGGESTS)
1146 else if (id == SOLVABLE_ENHANCES)
1149 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1150 for (; repo->idarraydata[ido]; ido++)
1151 POOL_DEBUG(SOLV_DEBUG_STATS," %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1154 case REPOKEY_TYPE_FIXARRAY:
1155 case REPOKEY_TYPE_FLEXARRAY:
1158 if (keydepth == sizeof(stack)/sizeof(*stack))
1160 pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1161 data.error = SOLV_ERROR_CORRUPT;
1164 stack[keydepth++] = nentries;
1165 stack[keydepth++] = keyp - schemadata;
1166 stack[keydepth++] = 0;
1167 dp = data_read_id_max(dp, &nentries, 0, 0, &data.error);
1168 incore_add_id(&data, nentries);
1171 /* zero size array? */
1173 nentries = stack[--keydepth];
1176 if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1178 /* horray! here come the solvables */
1179 if (nentries != numsolv)
1181 pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1182 data.error = SOLV_ERROR_CORRUPT;
1187 pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1188 data.error = SOLV_ERROR_CORRUPT;
1192 s = pool_id2solvable(pool, parent->start);
1194 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1195 data.start = s - pool->solvables;
1196 data.end = data.start + numsolv;
1197 repodata_extend_block(&data, data.start, numsolv);
1198 for (i = 1; i < numkeys; i++)
1201 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1202 && id >= INTERESTED_START && id <= INTERESTED_END)
1203 size_idarray += keys[i].size;
1205 /* allocate needed space in repo */
1206 /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1207 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1208 idarraydatap = repo->idarraydata + repo->idarraysize;
1209 repo->idarraysize += size_idarray;
1210 idarraydataend = idarraydatap + size_idarray;
1213 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1216 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1217 incore_add_id(&data, id);
1218 if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1222 pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1223 data.error = SOLV_ERROR_CORRUPT;
1225 stack[keydepth - 1] = id;
1227 keyp = schemadata + schemata[id];
1230 if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1232 if (keys[key].type == REPOKEY_TYPE_U32)
1233 dp = data_read_u32(dp, (unsigned int *)&id);
1235 dp = data_read_id_max(dp, &id, 0, 0, &data.error);
1237 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1238 repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1242 dp = data_skip(dp, keys[key].type);
1243 if (keys[key].storage == KEY_STORAGE_INCORE)
1244 incore_add_blob(&data, dps, dp - dps);
1248 /* should shrink idarraydata again */
1252 pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1253 data.error = SOLV_ERROR_CORRUPT;
1259 pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1260 data.error = SOLV_ERROR_EOF;
1267 /* free solvables */
1268 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1270 repo->idarraysize -= size_idarray;
1271 /* free incore data */
1272 data.incoredata = solv_free(data.incoredata);
1273 data.incoredatalen = data.incoredatafree = 0;
1276 if (data.incoredatafree)
1278 /* shrink excess size */
1279 data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1280 data.incoredatafree = 0;
1283 for (i = 1; i < numkeys; i++)
1284 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1286 if (i < numkeys && !data.error)
1289 unsigned int pagesize;
1291 /* we have vertical data, make it available */
1292 data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1293 for (i = 1; i < numkeys; i++)
1294 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1296 data.verticaloffset[i] = fileoffset;
1297 fileoffset += keys[i].size;
1299 data.lastverticaloffset = fileoffset;
1300 pagesize = read_u32(&data);
1301 data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1305 /* no longer needed */
1313 /* XXX: free repodata? */
1319 /* overwrite stub repodata */
1320 repodata_freedata(parent);
1321 data.repodataid = parent->repodataid;
1326 /* make it available as new repodata */
1327 if (!repo->nrepodata)
1329 repo->nrepodata = 1;
1330 repo->repodata = solv_calloc(2, sizeof(data));
1333 repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1334 data.repodataid = repo->nrepodata;
1335 repo->repodata[repo->nrepodata++] = data;
1338 /* create stub repodata entries for all external */
1339 if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1341 for (key = 1 ; key < data.nkeys; key++)
1342 if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1344 if (key < data.nkeys)
1345 repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1348 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1349 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1350 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));