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_MD5:
429 for (i = 0; i < SIZEOF_MD5; i++)
433 case REPOKEY_TYPE_SHA1:
436 for (i = 0; i < SIZEOF_SHA1; i++)
440 case REPOKEY_TYPE_SHA256:
443 for (i = 0; i < SIZEOF_SHA256; i++)
447 case REPOKEY_TYPE_IDARRAY:
448 case REPOKEY_TYPE_REL_IDARRAY:
449 while ((read_u8(data) & 0xc0) != 0)
452 case REPOKEY_TYPE_DIRNUMNUMARRAY:
455 read_id(data, numid + data->dirpool.ndirs); /* just check Id */
457 if (!(read_id(data, 0) & 0x40))
461 case REPOKEY_TYPE_DIRSTRARRAY:
464 Id id = read_id(data, 0);
465 while (read_u8(data) != 0)
472 pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
473 data->error = SOLV_ERROR_CORRUPT;
479 key_cmp (const void *pa, const void *pb)
481 Repokey *a = (Repokey *)pa;
482 Repokey *b = (Repokey *)pb;
483 return a->name - b->name;
486 static void repodata_load_solv(Repodata *data);
489 parse_external_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
491 Repo *repo = maindata->repo;
497 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
498 data = repo->repodata + repo->nrepodata++;
499 memset(data, 0, sizeof(*data));
502 data->state = REPODATA_STUB;
503 data->loadcallback = repodata_load_solv;
505 while ((key = *keyp++) != 0)
508 switch (keys[key].type)
510 case REPOKEY_TYPE_IDARRAY:
511 if (id != REPODATA_KEYS)
513 skip_item(maindata, REPOKEY_TYPE_IDARRAY, numid, numrel);
516 /* read_idarray writes a terminating 0, that's why the + 1 */
517 ida = sat_calloc(keys[key].size + 1, sizeof(Id));
518 ide = read_idarray(maindata, numid, idmap, ida, ida + keys[key].size + 1);
522 pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
523 maindata->error = SOLV_ERROR_CORRUPT;
526 data->nkeys = 1 + (n >> 1);
527 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
528 memset(data->keys, 0, sizeof(Repokey));
529 for (i = 1, ide = ida; i < data->nkeys; i++)
531 data->keys[i].name = *ide++;
532 data->keys[i].type = *ide++;
533 data->keys[i].size = 0;
534 data->keys[i].storage = 0;
538 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
540 case REPOKEY_TYPE_STR:
541 if (id != REPODATA_LOCATION)
542 skip_item(maindata, REPOKEY_TYPE_STR, numid, numrel);
546 unsigned len = sizeof(buf);
547 char *filename = buf;
548 read_str(maindata, &filename, &len);
549 data->location = strdup(filename);
555 skip_item(maindata, keys[key].type, numid, numrel);
562 parse_info_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
566 while ((key = *keyp++) != 0)
569 if (id == REPODATA_ADDEDFILEPROVIDES && keys[key].type == REPOKEY_TYPE_REL_IDARRAY)
572 /* + 1 just in case */
573 ida = sat_calloc(keys[key].size + 1, sizeof(Id));
574 read_idarray(maindata, 0, 0, ida, ida + keys[key].size + 1);
575 maindata->addedfileprovides = ida;
584 *ida = idmap ? idmap[old] : old;
588 skip_item(maindata, keys[key].type, numid, numrel);
592 /*-----------------------------------------------------------------*/
596 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
599 while ((key = *keyp++) != 0)
600 skip_item(data, keys[key].type, numid, numrel);
603 /*-----------------------------------------------------------------*/
606 incore_add_id(Repodata *data, Id x)
609 /* make sure we have at least 5 bytes free */
610 if (data->incoredatafree < 5)
612 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
613 data->incoredatafree = 1024;
615 dp = data->incoredata + data->incoredatalen;
621 *dp++ = (x >> 28) | 128;
623 *dp++ = (x >> 21) | 128;
624 *dp++ = (x >> 14) | 128;
627 *dp++ = (x >> 7) | 128;
629 data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
630 data->incoredatalen = dp - data->incoredata;
634 incore_add_blob(Repodata *data, unsigned char *buf, int len)
636 if (data->incoredatafree < len)
638 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024 + len);
639 data->incoredatafree = 1024 + len;
641 memcpy(data->incoredata + data->incoredatalen, buf, len);
642 data->incoredatafree -= len;
643 data->incoredatalen += len;
647 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
649 /* We have to map the IDs, which might also change
650 the necessary number of bytes, so we can't just copy
651 over the blob and adjust it. */
656 dp = data_read_ideof(dp, &id, &eof);
657 if (max && id >= max)
659 pool_debug(mypool, SAT_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
660 data->error = SOLV_ERROR_ID_RANGE;
665 id = (id & 63) | ((id & ~63) << 1);
666 incore_add_id(data, eof ? id : id | 64);
673 incore_add_u32(Repodata *data, unsigned int x)
676 /* make sure we have at least 4 bytes free */
677 if (data->incoredatafree < 4)
679 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
680 data->incoredatafree = 1024;
682 dp = data->incoredata + data->incoredatalen;
687 data->incoredatafree -= 4;
688 data->incoredatalen += 4;
693 incore_add_u8(Repodata *data, unsigned int x)
696 /* make sure we have at least 1 byte free */
697 if (data->incoredatafree < 1)
699 data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
700 data->incoredatafree = 1024;
702 dp = data->incoredata + data->incoredatalen;
704 data->incoredatafree--;
705 data->incoredatalen++;
711 // ----------------------------------------------
714 * read repo from .solv file
719 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
721 Pool *pool = repo->pool;
723 unsigned int numid, numrel, numdir, numsolv;
724 unsigned int numkeys, numschemata, numinfo, numextra, contentver;
727 Offset *str; /* map Id -> Offset into string space */
728 char *strsp; /* repo string space */
729 char *sp; /* pointer into string space */
730 Id *idmap; /* map of repo Ids to pool Ids */
732 unsigned int hashmask, h;
738 unsigned int size_idarray;
739 Id *idarraydatap, *idarraydataend;
742 unsigned int solvflags;
743 unsigned int solvversion;
745 Id *schemadata, *schemadatap, *schemadataend;
748 unsigned oldnrepodata;
749 int maxsize, allsize;
750 unsigned char *buf, *dp, *dps;
753 struct _Stringpool *spool;
757 memset(&data, 0, sizeof(data));
764 if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
766 pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
767 return SOLV_ERROR_NOT_SOLV;
769 solvversion = read_u32(&data);
777 pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
778 return SOLV_ERROR_UNSUPPORTED;
781 pool_freeidhashes(pool);
783 numid = read_u32(&data);
784 numrel = read_u32(&data);
785 numdir = read_u32(&data);
786 numsolv = read_u32(&data);
787 numkeys = read_u32(&data);
788 numschemata = read_u32(&data);
789 numinfo = read_u32(&data);
790 if (solvversion > SOLV_VERSION_6)
792 numextra = read_u32(&data);
793 contentver = read_u32(&data);
796 numextra = 0, contentver = 1;
797 solvflags = read_u32(&data);
799 if (numdir && numdir < 2)
801 pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
802 return SOLV_ERROR_CORRUPT;
809 pool_debug(pool, SAT_ERROR, "relations are forbidden in a store\n");
810 return SOLV_ERROR_CORRUPT;
812 if (parent->end - parent->start != numsolv)
814 pool_debug(pool, SAT_ERROR, "unequal number of solvables in a store\n");
815 return SOLV_ERROR_CORRUPT;
817 if (parent->nextra != numextra)
819 pool_debug(pool, SAT_ERROR, "unequal number of non-solvables in a store\n");
820 return SOLV_ERROR_CORRUPT;
824 pool_debug(pool, SAT_ERROR, "info blocks are forbidden in a store\n");
825 return SOLV_ERROR_CORRUPT;
829 /******* Part 1: string IDs *****************************************/
831 sizeid = read_u32(&data); /* size of string+Id space */
834 * read strings and Ids
849 spool->stringspace = sat_malloc(7);
850 strcpy(spool->stringspace, "<NULL>");
855 /* alloc string buffer */
856 spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
857 /* alloc string offsets (Id -> Offset into string space) */
858 spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
860 strsp = spool->stringspace;
861 str = spool->strings; /* array of offsets into strsp, indexed by Id */
863 /* point to _BEHIND_ already allocated string/Id space */
864 strsp += spool->sstrings;
868 * read new repo at end of pool
871 if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
873 if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
875 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
876 return SOLV_ERROR_EOF;
881 unsigned int pfsize = read_u32(&data);
882 char *prefix = sat_malloc(pfsize);
888 if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
890 pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
892 return SOLV_ERROR_EOF;
894 for (i = 1; i < numid; i++)
896 int same = (unsigned char)*pp++;
897 size_t len = strlen(pp) + 1;
898 freesp -= same + len;
901 pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
903 return SOLV_ERROR_OVERFLOW;
906 memcpy(dest, old_str, same);
907 memcpy(dest + same, pp, len);
915 pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
916 return SOLV_ERROR_CORRUPT;
919 strsp[sizeid] = 0; /* make string space \0 terminated */
924 /* no shared pool, thus no idmap and no unification */
926 spool->nstrings = numid;
930 /* we need the '' for directories */
931 pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
932 return SOLV_ERROR_CORRUPT;
934 for (i = 1; i < spool->nstrings; i++)
936 if (sp >= strsp + sizeid)
938 pool_debug(pool, SAT_ERROR, "not enough strings\n");
939 return SOLV_ERROR_OVERFLOW;
941 str[i] = sp - spool->stringspace;
942 sp += strlen(sp) + 1;
944 spool->sstrings = sp - spool->stringspace;
949 /* alloc id map for name and rel Ids. this maps ids in the solv files
950 * to the ids in our pool */
951 idmap = sat_calloc(numid + numrel, sizeof(Id));
954 * build hashes for all read strings
958 hashmask = mkmask(spool->nstrings + numid);
961 POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
962 POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
966 * create hashtable with strings already in pool
969 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
970 for (i = 1; i < spool->nstrings; i++) /* leave out our dummy zero id */
972 h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
973 hh = HASHCHAIN_START;
975 h = HASHCHAIN_NEXT(h, hh, hashmask);
980 * run over string space, calculate offsets
982 * build id map (maps solv Id -> pool Id)
985 for (i = 1; i < numid; i++)
987 if (sp >= strsp + sizeid)
991 pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
992 return SOLV_ERROR_OVERFLOW;
994 if (!*sp) /* empty string */
1001 /* find hash slot */
1002 h = strhash(sp) & hashmask;
1003 hh = HASHCHAIN_START;
1009 if (!strcmp(spool->stringspace + spool->strings[id], sp))
1010 break; /* existing string */
1011 h = HASHCHAIN_NEXT(h, hh, hashmask);
1014 /* length == offset to next string */
1016 if (id == ID_NULL) /* end of hash chain -> new string */
1018 id = spool->nstrings++;
1020 str[id] = spool->sstrings; /* save Offset */
1021 if (sp != spool->stringspace + spool->sstrings) /* not at end-of-buffer */
1022 memmove(spool->stringspace + spool->sstrings, sp, l); /* append to pool buffer */
1023 spool->sstrings += l;
1025 idmap[i] = id; /* repo relative -> pool relative */
1026 sp += l; /* next string */
1030 pool_shrink_strings(pool); /* vacuum */
1033 /******* Part 2: Relation IDs ***************************************/
1043 pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
1046 hashmask = mkmask(pool->nrels + numrel);
1048 POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
1049 POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
1052 * prep hash table with already existing RelDeps
1055 hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
1056 for (i = 1; i < pool->nrels; i++)
1058 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
1059 hh = HASHCHAIN_START;
1061 h = HASHCHAIN_NEXT(h, hh, hashmask);
1066 * read RelDeps from repo
1069 for (i = 0; i < numrel; i++)
1071 name = read_id(&data, i + numid); /* read (repo relative) Ids */
1072 evr = read_id(&data, i + numid);
1073 flags = read_u8(&data);
1074 name = idmap[name]; /* map to (pool relative) Ids */
1076 h = relhash(name, evr, flags) & hashmask;
1077 hh = HASHCHAIN_START;
1081 if (id == ID_NULL) /* end of hash chain */
1083 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
1085 h = HASHCHAIN_NEXT(h, hh, hashmask);
1087 if (id == ID_NULL) /* new RelDep */
1091 ran[id].name = name;
1093 ran[id].flags = flags;
1095 idmap[i + numid] = MAKERELDEP(id); /* fill Id map */
1098 pool_shrink_rels(pool); /* vacuum */
1102 /******* Part 3: Dirs ***********************************************/
1105 data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1106 data.dirpool.ndirs = numdir;
1107 data.dirpool.dirs[0] = 0; /* dir 0: virtual root */
1108 data.dirpool.dirs[1] = 1; /* dir 1: / */
1109 for (i = 2; i < numdir; i++)
1111 id = read_id(&data, i + numid);
1113 data.dirpool.dirs[i] = -(id - numid);
1115 data.dirpool.dirs[i] = idmap[id];
1117 data.dirpool.dirs[i] = id;
1121 /******* Part 4: Keys ***********************************************/
1123 keys = sat_calloc(numkeys, sizeof(*keys));
1124 /* keys start at 1 */
1125 for (i = 1; i < numkeys; i++)
1127 id = read_id(&data, numid);
1131 id = str2id(pool, stringpool_id2str(spool, id), 1);
1132 type = read_id(&data, numid);
1136 type = str2id(pool, stringpool_id2str(spool, type), 1);
1137 if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_COUNTED)
1139 pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1140 data.error = SOLV_ERROR_UNSUPPORTED;
1141 type = REPOKEY_TYPE_VOID;
1144 keys[i].type = type;
1145 keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1146 keys[i].storage = read_id(&data, 0);
1147 if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1148 keys[i].storage = KEY_STORAGE_SOLVABLE;
1149 else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1150 keys[i].storage = KEY_STORAGE_INCORE;
1151 if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1154 keys[i].size = idmap[keys[i].size];
1156 keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1159 fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1160 keys[i].size, keys[i].storage);
1164 have_xdata = parent ? 1 : 0;
1165 for (i = 1; i < numkeys; i++)
1166 if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1170 data.nkeys = numkeys;
1172 /******* Part 5: Schemata ********************************************/
1174 id = read_id(&data, 0);
1175 schemadata = sat_calloc(id + 1, sizeof(Id));
1176 schemadatap = schemadata + 1;
1177 schemadataend = schemadatap + id;
1178 schemata = sat_calloc(numschemata, sizeof(Id));
1179 for (i = 1; i < numschemata; i++)
1181 schemata[i] = schemadatap - schemadata;
1182 schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1184 Id *sp = schemadata + schemata[i];
1185 fprintf (stderr, "schema %d:", i);
1187 fprintf (stderr, " %d", *sp);
1188 fprintf (stderr, "\n");
1191 data.schemata = schemata;
1192 data.nschemata = numschemata;
1193 data.schemadata = schemadata;
1194 data.schemadatalen = schemadataend - data.schemadata;
1197 /******* Part 6: Info ***********************************************/
1198 oldnrepodata = repo->nrepodata;
1201 id = read_id(&data, 0);
1202 id = read_id(&data, 0);
1204 for (i = 0; i < numinfo; i++)
1206 /* for now we're just interested in data that starts with
1207 * the repodata_external id
1210 id = read_id(&data, numschemata);
1211 keyp = schemadata + schemata[id];
1213 if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1215 /* external data for some ids */
1216 parse_external_repodata(&data, keyp, keys, idmap, numid, numrel);
1218 else if (keys[key].name == REPODATA_INFO)
1220 parse_info_repodata(&data, keyp, keys, idmap, numid, numrel);
1224 skip_schema(&data, keyp, keys, numid, numrel);
1229 /******* Part 7: item data *******************************************/
1231 /* calculate idarray size */
1233 for (i = 1; i < numkeys; i++)
1236 if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1237 && id >= INTERESTED_START && id <= INTERESTED_END)
1238 size_idarray += keys[i].size;
1241 if (numsolv || numextra)
1243 maxsize = read_id(&data, 0);
1244 allsize = read_id(&data, 0);
1245 if (maxsize > allsize)
1247 pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1248 data.error = SOLV_ERROR_CORRUPT;
1252 maxsize = allsize = 0;
1254 /* allocate needed space in repo */
1255 /* we add maxsize because it is an upper limit for all idarrays */
1256 repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1257 idarraydatap = repo->idarraydata + repo->idarraysize;
1258 repo->idarraysize += size_idarray;
1259 idarraydataend = idarraydatap + size_idarray;
1262 /* read solvables */
1266 s = pool_id2solvable(pool, parent->start);
1268 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1269 /* store start and end of our id block */
1270 data.start = s - pool->solvables;
1271 data.end = data.start + numsolv;
1272 /* In case we have info blocks, make them refer to our part of the
1274 for (i = oldnrepodata; i < repo->nrepodata; i++)
1276 repo->repodata[i].start = data.start;
1277 repo->repodata[i].end = data.end;
1285 data.extrastart = repo->nextra;
1286 repodata_extend_extra(&data, numextra);
1287 repo->nextra += numextra;
1288 for (i = oldnrepodata; i < repo->nrepodata; i++)
1290 repo->repodata[i].extrastart = data.extrastart;
1291 repo->repodata[i].nextra = data.nextra;
1297 /* reserve one byte so that all offsets are not zero */
1298 incore_add_id(&data, 0);
1299 repodata_extend_block(&data, data.start, numsolv);
1303 buf = sat_calloc(maxsize + 4, 1);
1305 for (i = 0; i < numsolv + numextra; i++, s++)
1314 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1315 data.error = SOLV_ERROR_EOF;
1319 memmove(buf, dp, left);
1323 if (l && fread(buf + left, l, 1, data.fp) != 1)
1325 pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1326 data.error = SOLV_ERROR_EOF;
1333 dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1337 data.incoreoffset[i] = data.incoredatalen;
1339 data.extraoffset[i - numsolv] = data.incoredatalen;
1340 incore_add_id(&data, id);
1346 fprintf(stderr, "solv %d: schema %d\n", i, id);
1348 fprintf(stderr, "extra %d: schema %d\n", i - numsolv, id);
1350 keyp = schemadata + schemata[id];
1351 while ((key = *keyp++) != 0)
1356 id = keys[key].name;
1359 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1361 fprintf(stderr, "extra %d name %d type %d class %d\n", i - numsolv, id, keys[key].type, keys[key].storage);
1363 if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1365 /* copy offset/length into incore */
1367 dp = data_skip(dp, REPOKEY_TYPE_ID);
1368 dp = data_skip(dp, REPOKEY_TYPE_ID);
1369 incore_add_blob(&data, dps, dp - dps);
1372 switch (keys[key].type)
1374 case REPOKEY_TYPE_ID:
1375 dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1376 if (id == SOLVABLE_NAME)
1378 else if (id == SOLVABLE_ARCH)
1380 else if (id == SOLVABLE_EVR)
1382 else if (id == SOLVABLE_VENDOR)
1384 else if (keys[key].storage == KEY_STORAGE_INCORE)
1385 incore_add_id(&data, did);
1387 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1390 case REPOKEY_TYPE_U32:
1391 dp = data_read_u32(dp, &h);
1393 POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1395 if (id == RPM_RPMDBID)
1398 repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1399 repo->rpmdbid[i] = h;
1401 else if (keys[key].storage == KEY_STORAGE_INCORE)
1402 incore_add_u32(&data, h);
1404 case REPOKEY_TYPE_IDARRAY:
1405 case REPOKEY_TYPE_REL_IDARRAY:
1406 if (id < INTERESTED_START || id > INTERESTED_END)
1409 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1410 if (keys[key].storage != KEY_STORAGE_INCORE)
1413 incore_map_idarray(&data, dps, idmap, numid);
1415 incore_add_blob(&data, dps, dp - dps);
1418 ido = idarraydatap - repo->idarraydata;
1419 if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1420 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1421 else if (id == SOLVABLE_REQUIRES)
1422 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1423 else if (id == SOLVABLE_PROVIDES)
1424 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1426 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1427 if (idarraydatap > idarraydataend)
1429 pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1430 data.error = SOLV_ERROR_OVERFLOW;
1433 if (id == SOLVABLE_PROVIDES)
1435 else if (id == SOLVABLE_OBSOLETES)
1437 else if (id == SOLVABLE_CONFLICTS)
1439 else if (id == SOLVABLE_REQUIRES)
1441 else if (id == SOLVABLE_RECOMMENDS)
1443 else if (id == SOLVABLE_SUPPLEMENTS)
1444 s->supplements = ido;
1445 else if (id == SOLVABLE_SUGGESTS)
1447 else if (id == SOLVABLE_ENHANCES)
1449 else if (id == SOLVABLE_FRESHENS)
1452 POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1453 for (; repo->idarraydata[ido]; ido++)
1454 POOL_DEBUG(SAT_DEBUG_STATS," %s\n", dep2str(pool, repo->idarraydata[ido]));
1457 case REPOKEY_TYPE_COUNTED:
1460 dp = data_read_id(dp, &num);
1461 incore_add_id(&data, num);
1462 dp = data_read_id_max(dp, &did, 0, numschemata, &data.error);
1463 incore_add_id(&data, did);
1466 Id *kp = schemadata + schemata[did];
1470 switch (keys[*kp].type)
1472 case REPOKEY_TYPE_ID:
1473 dp = data_read_id_max(dp, &tid, idmap, numid + numrel, &data.error);
1474 incore_add_id(&data, tid);
1478 //dp = data_skip(dp, keys[*kp].type);
1479 dp = data_skip_recursive(&data, dp, keys + *kp);
1480 incore_add_blob(&data, dps, dp - dps);
1489 //dp = data_skip(dp, keys[key].type);
1490 dp = data_skip_recursive(&data, dp, keys + key);
1491 if (keys[key].storage == KEY_STORAGE_INCORE)
1492 incore_add_blob(&data, dps, dp - dps);
1498 /* should shrink idarraydata again */
1505 pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1506 data.error = SOLV_ERROR_EOF;
1513 /* free solvables */
1514 repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1516 repo->idarraysize -= size_idarray;
1517 /* free incore data */
1518 data.incoredata = sat_free(data.incoredata);
1519 data.incoredatalen = data.incoredatafree = 0;
1522 if (data.incoredatafree)
1524 /* shrink excess size */
1525 data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1526 data.incoredatafree = 0;
1529 for (i = 1; i < numkeys; i++)
1530 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1532 if (i < numkeys && !data.error)
1535 unsigned int pagesize;
1537 /* we have vertical data, make it available */
1538 data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1539 for (i = 1; i < numkeys; i++)
1540 if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1542 data.verticaloffset[i] = fileoffset;
1543 fileoffset += keys[i].size;
1545 data.lastverticaloffset = fileoffset;
1546 pagesize = read_u32(&data);
1547 repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1551 /* no longer needed */
1555 if (parent && !data.error)
1558 sat_free(parent->schemata);
1559 sat_free(parent->schemadata);
1560 sat_free(parent->keys);
1561 sat_free(parent->location);
1564 else if ((data.incoredatalen || data.fp) && !data.error)
1566 /* we got some data, make it available */
1567 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1568 repo->repodata[repo->nrepodata++] = data;
1573 sat_free(data.dirpool.dirs);
1574 sat_free(data.incoreoffset);
1576 sat_free(schemadata);
1586 repo_add_solv(Repo *repo, FILE *fp)
1588 return repo_add_solv_parent(repo, fp, 0);
1592 repodata_load_solv(Repodata *data)
1595 Pool *pool = data->repo->pool;
1596 if (!pool->loadcallback)
1598 data->state = REPODATA_ERROR;
1601 fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1604 data->state = REPODATA_ERROR;
1607 if (repo_add_solv_parent(data->repo, fp, data))
1608 data->state = REPODATA_ERROR;
1610 data->state = REPODATA_AVAILABLE;