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"
31 #define INTERESTED_START SOLVABLE_NAME
32 #define INTERESTED_END SOLVABLE_FRESHENS
34 #define SOLV_ERROR_NOT_SOLV 1
35 #define SOLV_ERROR_UNSUPPORTED 2
36 #define SOLV_ERROR_EOF 3
37 #define SOLV_ERROR_ID_RANGE 4
38 #define SOLV_ERROR_OVERFLOW 5
39 #define SOLV_ERROR_CORRUPT 6
41 static Pool *mypool; /* for pool_debug... */
43 /*-----------------------------------------------------------------*/
44 /* .solv read functions */
51 read_u32(Repodata *data)
58 for (i = 0; i < 4; i++)
63 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
64 data->error = SOLV_ERROR_EOF;
78 read_u8(Repodata *data)
87 pool_debug(mypool, SAT_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(mypool, SAT_ERROR, "unexpected EOF\n");
113 data->error = SOLV_ERROR_EOF;
121 pool_debug(mypool, SAT_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(mypool, SAT_ERROR, "read_id: id too long\n");
130 data->error = SOLV_ERROR_CORRUPT;
141 read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
154 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
155 data->error = SOLV_ERROR_EOF;
160 x = (x << 7) ^ c ^ 128;
163 x = (x << 6) | (c & 63);
169 pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
170 data->error = SOLV_ERROR_OVERFLOW;
178 *store++ = marker; /* do not map! */
187 pool_debug(mypool, SAT_ERROR, "read_rel_idarray: id too large (%u/%u)\n", x, max);
188 data->error = SOLV_ERROR_ID_RANGE;
195 pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
201 if (x == 0) /* already have trailing zero? */
205 pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
206 data->error = SOLV_ERROR_OVERFLOW;
217 static inline unsigned char *
218 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
221 dp = data_read_id(dp, &x);
224 pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
225 *error = SOLV_ERROR_ID_RANGE;
228 *ret = map ? map[x] : x;
233 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
244 x = (x << 7) ^ c ^ 128;
247 x = (x << 6) | (c & 63);
250 pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
251 *error = SOLV_ERROR_ID_RANGE;
265 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
277 x = (x << 7) ^ c ^ 128;
280 x = (x << 6) | (c & 63);
294 pool_debug(mypool, SAT_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
295 *error = SOLV_ERROR_ID_RANGE;
298 *store++ = map ? map[x] : x;
310 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
322 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
323 data->error = SOLV_ERROR_EOF;
328 x = (x << 7) ^ c ^ 128;
331 x = (x << 6) | (c & 63);
334 pool_debug(mypool, SAT_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
335 data->error = SOLV_ERROR_ID_RANGE;
342 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
348 if (x == 0) /* already have trailing zero? */
352 pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
353 data->error = SOLV_ERROR_OVERFLOW;
364 read_str(Repodata *data, char **inbuf, unsigned *len)
366 unsigned char *buf = (unsigned char*)*inbuf;
369 buf = sat_malloc(1024);
374 while((c = getc(data->fp)) != 0)
378 pool_debug (mypool, SAT_ERROR, "unexpected EOF\n");
379 data->error = SOLV_ERROR_EOF;
382 /* Plus 1 as we also want to add the 0. */
386 /* Don't realloc on the inbuf, it might be on the stack. */
387 if (buf == (unsigned char*)*inbuf)
389 buf = sat_malloc(*len);
390 memcpy(buf, *inbuf, *len - 256);
393 buf = sat_realloc(buf, *len);
402 skip_item(Repodata *data, unsigned type, unsigned numid, unsigned numrel)
406 case REPOKEY_TYPE_VOID:
407 case REPOKEY_TYPE_CONSTANT:
408 case REPOKEY_TYPE_CONSTANTID:
410 case REPOKEY_TYPE_ID:
411 read_id(data, numid + numrel); /* just check Id */
413 case REPOKEY_TYPE_DIR:
414 read_id(data, numid + data->dirpool.ndirs); /* just check Id */
416 case REPOKEY_TYPE_NUM:
419 case REPOKEY_TYPE_U32:
422 case REPOKEY_TYPE_STR:
423 while (read_u8(data) != 0)
426 case REPOKEY_TYPE_IDARRAY:
427 case REPOKEY_TYPE_REL_IDARRAY:
428 while ((read_u8(data) & 0xc0) != 0)
431 case REPOKEY_TYPE_DIRNUMNUMARRAY:
434 read_id(data, numid + data->dirpool.ndirs); /* just check Id */
436 if (!(read_id(data, 0) & 0x40))
440 case REPOKEY_TYPE_DIRSTRARRAY:
443 Id id = read_id(data, 0);
444 while (read_u8(data) != 0)
451 pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
452 data->error = SOLV_ERROR_CORRUPT;
458 key_cmp (const void *pa, const void *pb)
460 Repokey *a = (Repokey *)pa;
461 Repokey *b = (Repokey *)pb;
462 return a->name - b->name;
465 static void repodata_load_solv(Repodata *data);
468 parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
475 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
476 data = repo->repodata + repo->nrepodata++;
477 memset(data, 0, sizeof(*data));
480 data->state = REPODATA_STUB;
481 data->loadcallback = repodata_load_solv;
483 while ((key = *keyp++) != 0)
486 switch (keys[key].type)
488 case REPOKEY_TYPE_IDARRAY:
489 if (id != REPODATA_KEYS)
491 skip_item(maindata, REPOKEY_TYPE_IDARRAY, numid, numrel);
494 /* read_idarray writes a terminating 0, that's why the + 1 */
495 ida = sat_calloc(keys[key].size + 1, sizeof(Id));
496 ide = read_idarray(maindata, 0, 0, ida, ida + keys[key].size + 1);
500 pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
501 data->error = SOLV_ERROR_CORRUPT;
504 data->nkeys = 1 + (n >> 1);
505 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
506 memset(data->keys, 0, sizeof(Repokey));
507 for (i = 1, ide = ida; i < data->nkeys; i++)
511 pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
512 data->error = SOLV_ERROR_CORRUPT;
515 data->keys[i].name = idmap ? idmap[*ide++] : *ide++;
516 data->keys[i].type = idmap ? idmap[*ide++] : *ide++;
517 data->keys[i].size = 0;
518 data->keys[i].storage = 0;
522 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
524 case REPOKEY_TYPE_STR:
525 if (id != REPODATA_LOCATION)
526 skip_item(maindata, REPOKEY_TYPE_STR, numid, numrel);
530 unsigned len = sizeof(buf);
531 char *filename = buf;
532 read_str(maindata, &filename, &len);
533 data->location = strdup(filename);
539 skip_item(maindata, keys[key].type, numid, numrel);
545 /*-----------------------------------------------------------------*/
549 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
552 while ((key = *keyp++) != 0)
553 skip_item(data, keys[key].type, numid, numrel);
556 /*-----------------------------------------------------------------*/
559 incore_add_id(Repodata *data, Id x)
562 /* make sure we have at least 5 bytes free */
563 if (data->incoredatafree < 5)
565 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
566 data->incoredatafree = 1024;
568 dp = data->incoredata + data->incoredatalen;
574 *dp++ = (x >> 28) | 128;
576 *dp++ = (x >> 21) | 128;
577 *dp++ = (x >> 14) | 128;
580 *dp++ = (x >> 7) | 128;
582 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
583 data->incoredatalen = dp - data->incoredata;
587 incore_add_blob(Repodata *data, unsigned char *buf, int len)
589 if (data->incoredatafree < len)
591 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024 + len);
592 data->incoredatafree = 1024 + len;
594 memcpy(data->incoredata + data->incoredatalen, buf, len);
595 data->incoredatafree -= len;
596 data->incoredatalen += len;
600 incore_add_u32(Repodata *data, unsigned int x)
603 /* make sure we have at least 4 bytes free */
604 if (data->incoredatafree < 4)
606 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
607 data->incoredatafree = 1024;
609 dp = data->incoredata + data->incoredatalen;
614 data->incoredatafree -= 4;
615 data->incoredatalen += 4;
620 incore_add_u8(Repodata *data, unsigned int x)
623 /* make sure we have at least 1 byte free */
624 if (data->incoredatafree < 1)
626 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
627 data->incoredatafree = 1024;
629 dp = data->incoredata + data->incoredatalen;
631 data->incoredatafree--;
632 data->incoredatalen++;
638 // ----------------------------------------------
642 * read repo from .solv file
647 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
649 Pool *pool = repo->pool;
651 unsigned int numid, numrel, numdir, numsolv;
652 unsigned int numkeys, numschemata, numinfo;
655 Offset *str; /* map Id -> Offset into string space */
656 char *strsp; /* repo string space */
657 char *sp; /* pointer into string space */
658 Id *idmap; /* map of repo Ids to pool Ids */
660 unsigned int hashmask, h;
666 unsigned int size_idarray;
667 Id *idarraydatap, *idarraydataend;
670 unsigned int solvflags;
671 unsigned int solvversion;
673 Id *schemadata, *schemadatap, *schemadataend;
676 unsigned oldnrepodata;
677 int maxsize, allsize;
678 unsigned char *buf, *dp, *dps;
681 struct _Stringpool *spool;
685 memset(&data, 0, sizeof(data));
692 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
694 pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
695 return SOLV_ERROR_NOT_SOLV;
697 solvversion = read_u32(&data);
703 pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
704 return SOLV_ERROR_UNSUPPORTED;
707 pool_freeidhashes(pool);
709 numid = read_u32(&data);
710 numrel = read_u32(&data);
711 numdir = read_u32(&data);
712 numsolv = read_u32(&data);
713 numkeys = read_u32(&data);
714 numschemata = read_u32(&data);
715 numinfo = read_u32(&data);
716 solvflags = read_u32(&data);
718 if (numdir && numdir < 2)
720 pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
721 return SOLV_ERROR_CORRUPT;
728 pool_debug(pool, SAT_ERROR, "relations are forbidden in a store\n");
729 return SOLV_ERROR_CORRUPT;
731 if (parent->end - parent->start != numsolv)
733 pool_debug(pool, SAT_ERROR, "unequal number of solvables in a store\n");
734 return SOLV_ERROR_CORRUPT;
738 pool_debug(pool, SAT_ERROR, "info blocks are forbidden in a store\n");
739 return SOLV_ERROR_CORRUPT;
743 /******* Part 1: string IDs *****************************************/
745 sizeid = read_u32(&data); /* size of string+Id space */
748 * read strings and Ids
763 spool->stringspace = sat_malloc(7);
764 strcpy(spool->stringspace, "<NULL>");
769 /* alloc string buffer */
770 spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
771 /* alloc string offsets (Id -> Offset into string space) */
772 spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
774 strsp = spool->stringspace;
775 str = spool->strings; /* array of offsets into strsp, indexed by Id */
777 /* point to _BEHIND_ already allocated string/Id space */
778 strsp += spool->sstrings;
782 * read new repo at end of pool
785 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
787 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
789 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
790 return SOLV_ERROR_EOF;
795 unsigned int pfsize = read_u32(&data);
796 char *prefix = sat_malloc(pfsize);
802 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
804 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
806 return SOLV_ERROR_EOF;
808 for (i = 1; i < numid; i++)
810 int same = (unsigned char)*pp++;
811 size_t len = strlen(pp) + 1;
812 freesp -= same + len;
815 pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
817 return SOLV_ERROR_OVERFLOW;
820 memcpy(dest, old_str, same);
821 memcpy(dest + same, pp, len);
829 pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
830 return SOLV_ERROR_CORRUPT;
833 strsp[sizeid] = 0; /* make string space \0 terminated */
838 /* no shared pool, thus no idmap and no unification */
840 spool->nstrings = numid;
844 /* we need the '' for directories */
845 pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
846 return SOLV_ERROR_CORRUPT;
848 for (i = 1; i < spool->nstrings; i++)
850 if (sp >= strsp + sizeid)
852 pool_debug(pool, SAT_ERROR, "not enough strings\n");
853 return SOLV_ERROR_OVERFLOW;
855 str[i] = sp - spool->stringspace;
856 sp += strlen(sp) + 1;
858 spool->sstrings = sp - spool->stringspace;
863 /* alloc id map for name and rel Ids. this maps ids in the solv files
864 * to the ids in our pool */
865 idmap = sat_calloc(numid + numrel, sizeof(Id));
868 * build hashes for all read strings
872 hashmask = mkmask(spool->nstrings + numid);
875 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
876 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
880 * create hashtable with strings already in pool
883 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
884 for (i = 1; i < spool->nstrings; i++) /* leave out our dummy zero id */
886 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
887 hh = HASHCHAIN_START;
889 h = HASHCHAIN_NEXT(h, hh, hashmask);
894 * run over string space, calculate offsets
896 * build id map (maps solv Id -> pool Id)
899 for (i = 1; i < numid; i++)
901 if (sp >= strsp + sizeid)
905 pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
906 return SOLV_ERROR_OVERFLOW;
908 if (!*sp) /* empty string */
916 h = strhash(sp) & hashmask;
917 hh = HASHCHAIN_START;
923 if (!strcmp(spool->stringspace + spool->strings[id], sp))
924 break; /* existing string */
925 h = HASHCHAIN_NEXT(h, hh, hashmask);
928 /* length == offset to next string */
930 if (id == ID_NULL) /* end of hash chain -> new string */
932 id = spool->nstrings++;
934 str[id] = spool->sstrings; /* save Offset */
935 if (sp != spool->stringspace + spool->sstrings) /* not at end-of-buffer */
936 memmove(spool->stringspace + spool->sstrings, sp, l); /* append to pool buffer */
937 spool->sstrings += l;
939 idmap[i] = id; /* repo relative -> pool relative */
940 sp += l; /* next string */
944 pool_shrink_strings(pool); /* vacuum */
947 /******* Part 2: Relation IDs ***************************************/
957 pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
960 hashmask = mkmask(pool->nrels + numrel);
962 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
963 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
966 * prep hash table with already existing RelDeps
969 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
970 for (i = 1; i < pool->nrels; i++)
972 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
973 hh = HASHCHAIN_START;
975 h = HASHCHAIN_NEXT(h, hh, hashmask);
980 * read RelDeps from repo
983 for (i = 0; i < numrel; i++)
985 name = read_id(&data, i + numid); /* read (repo relative) Ids */
986 evr = read_id(&data, i + numid);
987 flags = read_u8(&data);
988 name = idmap[name]; /* map to (pool relative) Ids */
990 h = relhash(name, evr, flags) & hashmask;
991 hh = HASHCHAIN_START;
995 if (id == ID_NULL) /* end of hash chain */
997 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
999 h = HASHCHAIN_NEXT(h, hh, hashmask);
1001 if (id == ID_NULL) /* new RelDep */
1005 ran[id].name = name;
1007 ran[id].flags = flags;
1009 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
1012 pool_shrink_rels(pool); /* vacuum */
1016 /******* Part 3: Dirs ***********************************************/
1019 data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1020 data.dirpool.ndirs = numdir;
1021 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
1022 data.dirpool.dirs[1] = 1; /* dir 1: / */
1023 for (i = 2; i < numdir; i++)
1025 id = read_id(&data, i + numid);
1027 data.dirpool.dirs[i] = -(id - numid);
1029 data.dirpool.dirs[i] = idmap[id];
1031 data.dirpool.dirs[i] = id;
1035 /******* Part 4: Keys ***********************************************/
1037 keys = sat_calloc(numkeys, sizeof(*keys));
1038 /* keys start at 1 */
1039 for (i = 1; i < numkeys; i++)
1041 id = read_id(&data, numid);
1045 id = str2id(pool, stringpool_id2str(spool, id), 1);
1046 type = read_id(&data, numid);
1050 type = str2id(pool, stringpool_id2str(spool, type), 1);
1051 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DIRNUMNUMARRAY)
1053 pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1054 data.error = SOLV_ERROR_UNSUPPORTED;
1055 type = REPOKEY_TYPE_VOID;
1058 keys[i].type = type;
1059 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1060 keys[i].storage = read_id(&data, 0);
1061 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1062 keys[i].storage = KEY_STORAGE_SOLVABLE;
1063 else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1064 keys[i].storage = KEY_STORAGE_INCORE;
1065 if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1068 keys[i].size = idmap[keys[i].size];
1070 keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1073 fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1074 keys[i].size, keys[i].storage);
1078 have_xdata = parent ? 1 : 0;
1079 for (i = 1; i < numkeys; i++)
1080 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1084 data.nkeys = numkeys;
1086 /******* Part 5: Schemata ********************************************/
1088 id = read_id(&data, 0);
1089 schemadata = sat_calloc(id + 1, sizeof(Id));
1090 schemadatap = schemadata + 1;
1091 schemadataend = schemadatap + id;
1092 schemata = sat_calloc(numschemata, sizeof(Id));
1093 for (i = 1; i < numschemata; i++)
1095 schemata[i] = schemadatap - schemadata;
1096 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1098 Id *sp = schemadata + schemata[i];
1099 fprintf (stderr, "schema %d:", i);
1101 fprintf (stderr, " %d", *sp);
1102 fprintf (stderr, "\n");
1105 data.schemata = schemata;
1106 data.nschemata = numschemata;
1107 data.schemadata = schemadata;
1108 data.schemadatalen = schemadataend - data.schemadata;
1111 /******* Part 6: Info ***********************************************/
1112 oldnrepodata = repo->nrepodata;
1115 id = read_id(&data, 0);
1116 id = read_id(&data, 0);
1118 for (i = 0; i < numinfo; i++)
1120 /* for now we're just interested in data that starts with
1121 * the repodata_external id
1124 id = read_id(&data, numschemata);
1125 keyp = schemadata + schemata[id];
1127 if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1129 /* external data for some ids */
1130 parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
1133 skip_schema(&data, keyp, keys, numid, numrel);
1137 /******* Part 7: item data *******************************************/
1139 /* calculate idarray size */
1141 for (i = 1; i < numkeys; i++)
1144 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1145 && id >= INTERESTED_START && id <= INTERESTED_END)
1146 size_idarray += keys[i].size;
1151 maxsize = read_id(&data, 0);
1152 allsize = read_id(&data, 0);
1153 if (maxsize > allsize)
1155 pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1156 data.error = SOLV_ERROR_CORRUPT;
1160 maxsize = allsize = 0;
1162 /* allocate needed space in repo */
1163 /* we add maxsize because it is an upper limit for all idarrays */
1164 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1165 idarraydatap = repo->idarraydata + repo->idarraysize;
1166 repo->idarraysize += size_idarray;
1167 idarraydataend = idarraydatap + size_idarray;
1170 /* read solvables */
1174 s = pool_id2solvable(pool, parent->start);
1176 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1177 /* store start and end of our id block */
1178 data.start = s - pool->solvables;
1179 data.end = data.start + numsolv;
1180 /* In case we have info blocks, make them refer to our part of the
1182 for (i = oldnrepodata; i < repo->nrepodata; i++)
1184 repo->repodata[i].start = data.start;
1185 repo->repodata[i].end = data.end;
1192 repodata_extend_block(&data, data.start, numsolv);
1195 buf = sat_calloc(maxsize + 4, 1);
1197 for (i = 0; i < numsolv; i++, s++)
1206 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1207 data.error = SOLV_ERROR_EOF;
1211 memmove(buf, dp, left);
1215 if (l && fread(buf + left, l, 1, data.fp) != 1)
1217 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1218 data.error = SOLV_ERROR_EOF;
1225 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1228 data.incoreoffset[i] = data.incoredatalen;
1229 incore_add_id(&data, id);
1231 keyp = schemadata + schemata[id];
1232 while ((key = *keyp++) != 0)
1237 id = keys[key].name;
1239 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1241 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1243 /* copy offset/length into incore */
1245 dp = data_skip(dp, REPOKEY_TYPE_ID);
1246 dp = data_skip(dp, REPOKEY_TYPE_ID);
1247 incore_add_blob(&data, dps, dp - dps);
1250 switch (keys[key].type)
1252 case REPOKEY_TYPE_ID:
1253 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1254 if (id == SOLVABLE_NAME)
1256 else if (id == SOLVABLE_ARCH)
1258 else if (id == SOLVABLE_EVR)
1260 else if (id == SOLVABLE_VENDOR)
1262 else if (keys[key].storage == KEY_STORAGE_INCORE)
1263 incore_add_id(&data, did);
1265 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1268 case REPOKEY_TYPE_U32:
1269 dp = data_read_u32(dp, &h);
1271 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1273 if (id == RPM_RPMDBID)
1276 repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1277 repo->rpmdbid[i] = h;
1279 else if (keys[key].storage == KEY_STORAGE_INCORE)
1280 incore_add_u32(&data, h);
1282 case REPOKEY_TYPE_IDARRAY:
1283 case REPOKEY_TYPE_REL_IDARRAY:
1284 if (id < INTERESTED_START || id > INTERESTED_END)
1287 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1288 if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
1290 if (keys[key].storage == KEY_STORAGE_INCORE)
1291 incore_add_blob(&data, dps, dp - dps);
1294 ido = idarraydatap - repo->idarraydata;
1295 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1296 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1297 else if (id == SOLVABLE_REQUIRES)
1298 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1299 else if (id == SOLVABLE_PROVIDES)
1300 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1302 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1303 if (idarraydatap > idarraydataend)
1305 pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1306 data.error = SOLV_ERROR_OVERFLOW;
1309 if (id == SOLVABLE_PROVIDES)
1311 else if (id == SOLVABLE_OBSOLETES)
1313 else if (id == SOLVABLE_CONFLICTS)
1315 else if (id == SOLVABLE_REQUIRES)
1317 else if (id == SOLVABLE_RECOMMENDS)
1319 else if (id == SOLVABLE_SUPPLEMENTS)
1320 s->supplements = ido;
1321 else if (id == SOLVABLE_SUGGESTS)
1323 else if (id == SOLVABLE_ENHANCES)
1325 else if (id == SOLVABLE_FRESHENS)
1328 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1329 for (; repo->idarraydata[ido]; ido++)
1330 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));
1335 dp = data_skip(dp, keys[key].type);
1336 if (keys[key].storage == KEY_STORAGE_INCORE)
1337 incore_add_blob(&data, dps, dp - dps);
1343 /* should shrink idarraydata again */
1350 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1351 data.error = SOLV_ERROR_EOF;
1358 /* free solvables */
1359 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1361 repo->idarraysize -= size_idarray;
1362 /* free incore data */
1363 data.incoredata = sat_free(data.incoredata);
1364 data.incoredatalen = data.incoredatafree = 0;
1367 if (data.incoredatafree)
1369 /* shrink excess size */
1370 data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1371 data.incoredatafree = 0;
1374 for (i = 1; i < numkeys; i++)
1375 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1377 if (i < numkeys && !data.error)
1380 unsigned int pagesize;
1382 /* we have vertical data, make it available */
1383 data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1384 for (i = 1; i < numkeys; i++)
1385 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1387 data.verticaloffset[i] = fileoffset;
1388 fileoffset += keys[i].size;
1390 data.lastverticaloffset = fileoffset;
1391 pagesize = read_u32(&data);
1392 repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1396 /* no longer needed */
1400 if (parent && !data.error)
1403 sat_free(parent->schemata);
1404 sat_free(parent->schemadata);
1405 sat_free(parent->keys);
1406 sat_free(parent->location);
1409 else if ((data.incoredatalen || data.fp) && !data.error)
1411 /* we got some data, make it available */
1412 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1413 repo->repodata[repo->nrepodata++] = data;
1418 sat_free(data.dirpool.dirs);
1419 sat_free(data.incoreoffset);
1421 sat_free(schemadata);
1431 repo_add_solv(Repo *repo, FILE *fp)
1433 return repo_add_solv_parent(repo, fp, 0);
1437 repodata_load_solv(Repodata *data)
1440 Pool *pool = data->repo->pool;
1441 if (!pool->loadcallback)
1443 data->state = REPODATA_ERROR;
1446 fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1449 data->state = REPODATA_ERROR;
1452 if (repo_add_solv_parent(data->repo, fp, data))
1453 data->state = REPODATA_ERROR;
1455 data->state = REPODATA_AVAILABLE;