- rename xmalloc/... functions to sat_malloc, as we're a
[platform/upstream/libsolv.git] / src / repo_solv.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_solv.c
10  * 
11  * Read the binary dump of a Repo and create a Repo * from it
12  * 
13  *  See
14  *   Repo *pool_addrepo_solv(Pool *pool, FILE *fp)
15  * below
16  * 
17  */
18
19
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include "repo_solv.h"
27 #include "util.h"
28 #include "attr_store_p.h"
29
30 #define INTERESTED_START        SOLVABLE_NAME
31 #define INTERESTED_END          SOLVABLE_FRESHENS
32
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
39
40 static Pool *mypool;            /* for pool_debug... */
41
42 /*-----------------------------------------------------------------*/
43 /* .solv read functions */
44
45 /*
46  * read u32
47  */
48
49 static unsigned int
50 read_u32(Repodata *data)
51 {
52   int c, i;
53   unsigned int x = 0;
54
55   if (data->error)
56     return 0;
57   for (i = 0; i < 4; i++)
58     {
59       c = getc(data->fp);
60       if (c == EOF)
61         {
62           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
63           data->error = SOLV_ERROR_EOF;
64           return 0;
65         }
66       x = (x << 8) | c;
67     }
68   return x;
69 }
70
71
72 /*
73  * read u8
74  */
75
76 static unsigned int
77 read_u8(Repodata *data)
78 {
79   int c;
80
81   if (data->error)
82     return 0;
83   c = getc(data->fp);
84   if (c == EOF)
85     {
86       pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
87       data->error = SOLV_ERROR_EOF;
88       return 0;
89     }
90   return c;
91 }
92
93
94 /*
95  * read Id
96  */
97
98 static Id
99 read_id(Repodata *data, Id max)
100 {
101   unsigned int x = 0;
102   int c, i;
103
104   if (data->error)
105     return 0;
106   for (i = 0; i < 5; i++)
107     {
108       c = getc(data->fp);
109       if (c == EOF)
110         {
111           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
112           data->error = SOLV_ERROR_EOF;
113           return 0;
114         }
115       if (!(c & 128))
116         {
117           x = (x << 7) | c;
118           if (max && x >= max)
119             {
120               pool_debug(mypool, SAT_ERROR, "read_id: id too large (%u/%u)\n", x, max);
121               data->error = SOLV_ERROR_ID_RANGE;
122               return 0;
123             }
124           return x;
125         }
126       x = (x << 7) ^ c ^ 128;
127     }
128   pool_debug(mypool, SAT_ERROR, "read_id: id too long\n");
129   data->error = SOLV_ERROR_CORRUPT;
130   return 0;
131 }
132
133
134 /*
135  * read array of Ids
136  */
137
138 static Id *
139 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, int relative)
140 {
141   unsigned int x = 0;
142   int c;
143   Id old = 0;
144
145   if (data->error)
146     return 0;
147   for (;;)
148     {
149       c = getc(data->fp);
150       if (c == EOF)
151         {
152           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
153           data->error = SOLV_ERROR_EOF;
154           return 0;
155         }
156       if ((c & 128) == 0)
157         {
158           x = (x << 6) | (c & 63);
159           if (relative)
160             {
161               if (x == 0 && c == 0x40)
162                 {
163                   /* prereq hack */
164                   if (store == end)
165                     {
166                       pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
167                       data->error = SOLV_ERROR_OVERFLOW;
168                       return 0;
169                     }
170                   *store++ = SOLVABLE_PREREQMARKER;
171                   old = 0;
172                   x = 0;
173                   continue;
174                 }
175               x = (x - 1) + old;
176               old = x;
177             }
178           if (max && x >= max)
179             {
180               pool_debug(mypool, SAT_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
181               data->error = SOLV_ERROR_ID_RANGE;
182               return 0;
183             }
184           if (map)
185             x = map[x];
186           if (store == end)
187             {
188               pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
189               return 0;
190             }
191           *store++ = x;
192           if ((c & 64) == 0)
193             {
194               if (x == 0)       /* already have trailing zero? */
195                 return store;
196               if (store == end)
197                 {
198                   pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
199                   data->error = SOLV_ERROR_OVERFLOW;
200                   return 0;
201                 }
202               *store++ = 0;
203               return store;
204             }
205           x = 0;
206           continue;
207         }
208       x = (x << 7) ^ c ^ 128;
209     }
210 }
211
212 static void
213 read_str(Repodata *data, char **inbuf, unsigned *len)
214 {
215   unsigned char *buf = (unsigned char*)*inbuf;
216   if (!buf)
217     {
218       buf = sat_malloc(1024);
219       *len = 1024;
220     }
221   int c;
222   unsigned ofs = 0;
223   while((c = getc(data->fp)) != 0)
224     {
225       if (c == EOF)
226         {
227           pool_debug (mypool, SAT_ERROR, "unexpected EOF\n");
228           data->error = SOLV_ERROR_EOF;
229           return;
230         }
231       /* Plus 1 as we also want to add the 0.  */
232       if (ofs + 1 >= *len)
233         {
234           *len += 256;
235           /* Don't realloc on the inbuf, it might be on the stack.  */
236           if (buf == (unsigned char*)*inbuf)
237             {
238               buf = sat_malloc(*len);
239               memcpy(buf, *inbuf, *len - 256);
240             }
241           else
242             buf = sat_realloc(buf, *len);
243         }
244       buf[ofs++] = c;
245     }
246   buf[ofs++] = 0;
247   *inbuf = (char*)buf;
248 }
249
250 static void
251 skip_item (Repodata *data, unsigned type, unsigned numid, unsigned numrel)
252 {
253   switch (type)
254     {
255       case TYPE_VOID:
256         break;
257       case TYPE_ID:
258         read_id(data, numid + numrel);   /* just check Id */
259         break;
260       case TYPE_U32:
261         read_u32(data);
262         break;
263       case TYPE_ATTR_STRING:
264       case TYPE_STR:
265         while(read_u8(data) != 0)
266           ;
267         break;
268       case TYPE_IDARRAY:
269       case TYPE_IDVALUEARRAY:
270       case TYPE_IDVALUEVALUEARRAY:
271       case TYPE_REL_IDARRAY:
272       case TYPE_ATTR_INTLIST:
273         while ((read_u8(data) & 0xc0) != 0)
274           ;
275         break;
276       case TYPE_COUNT_NAMED:
277         {
278           unsigned count = read_id(data, 0);
279           while (count--)
280             {
281               read_id(data, numid);    /* Name */
282               unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
283               skip_item(data, t, numid, numrel);
284             }
285         }
286         break;
287       case TYPE_COUNTED:
288         {
289           unsigned count = read_id(data, 0);
290           unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
291           while (count--)
292             skip_item(data, t, numid, numrel);
293         }
294         break;
295       case TYPE_ATTR_CHUNK:
296         read_id(data, 0);
297         /* Fallthrough.  */
298       case TYPE_ATTR_INT:
299         read_id(data, 0);
300         break;
301       case TYPE_ATTR_LOCALIDS:
302         while (read_id(data, 0) != 0)
303           ;
304         break;
305       default:
306         pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
307         data->error = SOLV_ERROR_CORRUPT;
308         break;
309     }
310 }
311
312 static int
313 key_cmp (const void *pa, const void *pb)
314 {
315   Repokey *a = (Repokey *)pa;
316   Repokey *b = (Repokey *)pb;
317   return a->name - b->name;
318 }
319
320 static void
321 parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
322 {
323   Id key, id;
324   Id *ida, *ide;
325   Repodata *data;
326   int i, n;
327
328   repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
329   data = repo->repodata + repo->nrepodata++;
330   data->repo = repo;
331   memset(data, 0, sizeof(*data));
332
333   while ((key = *keyp++) != 0)
334     {
335       id = keys[key].name;
336       switch (keys[key].type)
337         {
338         case TYPE_IDVALUEARRAY:
339           if (id != REPODATA_KEYS)
340             {
341               skip_item(maindata, TYPE_IDVALUEARRAY, numid, numrel);
342               break;
343             }
344           ida = sat_calloc(keys[key].size, sizeof(Id));
345           ide = read_idarray(maindata, 0, 0, ida, ida + keys[key].size, 0);
346           n = ide - ida - 1;
347           if (n & 1)
348             {
349               data->error = SOLV_ERROR_CORRUPT;
350               return;
351             }
352           data->nkeys = 1 + (n >> 1);
353           data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
354           memset(data->keys, 0, sizeof(Repokey));
355           for (i = 1, ide = ida; i < data->nkeys; i++)
356             {
357               if (*ide >= numid)
358                 {
359                   data->error = SOLV_ERROR_CORRUPT;
360                   pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
361                   return;
362                 }
363               data->keys[i].name = idmap ? idmap[*ide++] : *ide++;
364               data->keys[i].type = *ide++;
365               data->keys[i].size = 0;
366               data->keys[i].storage = 0;
367             }
368           sat_free(ida);
369           if (data->nkeys > 2)
370             qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
371           break;
372         case TYPE_STR:
373           if (id != REPODATA_LOCATION)
374             skip_item(maindata, TYPE_STR, numid, numrel);
375           else
376             {
377               char buf[1024];
378               unsigned len = sizeof (buf);
379               char *filename = buf;
380               read_str(maindata, &filename, &len);
381               data->location = strdup(filename);
382               if (filename != buf)
383                 free(filename);
384             }
385           break;
386         default:
387           skip_item(maindata, keys[key].type, numid, numrel);
388           break;
389         }
390     }
391 }
392
393 /*-----------------------------------------------------------------*/
394
395
396 static void
397 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
398 {
399   Id key;
400   while ((key = *keyp++) != 0)
401     skip_item(data, keys[key].type, numid, numrel);
402 }
403
404 /*-----------------------------------------------------------------*/
405
406 static void
407 incore_add_id(Repodata *data, Id x)
408 {
409   unsigned char *dp;
410   /* make sure we have at least 5 bytes free */
411   if (data->incoredatafree < 5)
412     {
413       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
414       data->incoredatafree = 1024;
415     }
416   dp = data->incoredata + data->incoredatalen;
417   if (x < 0)
418     abort();
419   if (x >= (1 << 14))
420     {
421       if (x >= (1 << 28))
422         *dp++ = (x >> 28) | 128;
423       if (x >= (1 << 21))
424         *dp++ = (x >> 21) | 128;
425       *dp++ = (x >> 14) | 128;
426     }
427   if (x >= (1 << 7))
428     *dp++ = (x >> 7) | 128;
429   *dp++ = x & 127;
430   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
431   data->incoredatalen = dp - data->incoredata;
432 }
433
434 static void
435 incore_add_u32(Repodata *data, unsigned int x)
436 {
437   unsigned char *dp;
438   /* make sure we have at least 4 bytes free */
439   if (data->incoredatafree < 4)
440     {
441       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
442       data->incoredatafree = 1024;
443     }
444   dp = data->incoredata + data->incoredatalen;
445   *dp++ = x >> 24;
446   *dp++ = x >> 16;
447   *dp++ = x >> 8;
448   *dp++ = x;
449   data->incoredatafree -= 4;
450   data->incoredatalen += 4;
451 }
452
453 static void
454 incore_add_u8(Repodata *data, unsigned int x)
455 {
456   unsigned char *dp;
457   /* make sure we have at least 1 byte free */
458   if (data->incoredatafree < 1)
459     {
460       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
461       data->incoredatafree = 1024;
462     }
463   dp = data->incoredata + data->incoredatalen;
464   *dp++ = x;
465   data->incoredatafree--;
466   data->incoredatalen++;
467 }
468
469 // ----------------------------------------------
470
471 /*
472  * read repo from .solv file
473  *  and add it to pool
474  */
475
476 int
477 repo_add_solv(Repo *repo, FILE *fp)
478 {
479   Pool *pool = repo->pool;
480   int i, l;
481   unsigned int numid, numrel, numsolv;
482   unsigned int numkeys, numschemata, numinfo;
483 #if 0
484   Attrstore *embedded_store = 0;
485 #endif
486
487   Offset sizeid;
488   Offset *str;                         /* map Id -> Offset into string space */
489   char *strsp;                         /* repo string space */
490   char *sp;                            /* pointer into string space */
491   Id *idmap;                           /* map of repo Ids to pool Ids */
492   Id id;
493   unsigned int hashmask, h;
494   int hh;
495   Id *hashtbl;
496   Id name, evr, did;
497   int flags;
498   Reldep *ran;
499   unsigned int size_idarray;
500   Id *idarraydatap, *idarraydataend;
501   Offset ido;
502   Solvable *s;
503   unsigned int solvflags;
504   unsigned int solvversion;
505   Repokey *keys;
506   Id *schemadata, *schemadatap, *schemadataend;
507   Id *schemata, key;
508
509   Repodata data;
510
511   memset(&data, 0, sizeof(data));
512   data.repo = repo;
513   data.fp = fp;
514
515   mypool = pool;
516
517   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
518     {
519       pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
520       return SOLV_ERROR_NOT_SOLV;
521     }
522   solvversion = read_u32(&data);
523   switch (solvversion)
524     {
525       case SOLV_VERSION_1:
526       case SOLV_VERSION_2:
527       case SOLV_VERSION_3:
528         break;
529       default:
530         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
531         return SOLV_ERROR_UNSUPPORTED;
532     }
533
534   pool_freeidhashes(pool);
535
536   numid = read_u32(&data);
537   numrel = read_u32(&data);
538   numsolv = read_u32(&data);
539   numkeys = read_u32(&data);
540   numschemata = read_u32(&data);
541   numinfo = read_u32(&data);
542   solvflags = read_u32(&data);
543
544   if (solvversion < SOLV_VERSION_3
545       && numinfo)
546     {
547       pool_debug(pool, SAT_ERROR, "unsupported SOLV format (has info)\n");
548       return SOLV_ERROR_UNSUPPORTED;
549     }
550
551   /*******  Part 1: string IDs  *****************************************/
552
553   sizeid = read_u32(&data);            /* size of string+Id space */
554
555   /*
556    * read strings and Ids
557    * 
558    */
559
560   
561   /*
562    * alloc buffers
563    */
564
565   /* alloc string buffer */
566   pool->ss.stringspace = sat_realloc(pool->ss.stringspace, pool->ss.sstrings + sizeid + 1);
567   /* alloc string offsets (Id -> Offset into string space) */
568   pool->ss.strings = sat_realloc2(pool->ss.strings, pool->ss.nstrings + numid, sizeof(Offset));
569
570   strsp = pool->ss.stringspace;
571   str = pool->ss.strings;                      /* array of offsets into strsp, indexed by Id */
572
573   /* point to _BEHIND_ already allocated string/Id space */
574   strsp += pool->ss.sstrings;
575
576
577   /*
578    * read new repo at end of pool
579    */
580   
581   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
582     {
583       if (fread(strsp, sizeid, 1, fp) != 1)
584         {
585           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
586           return SOLV_ERROR_EOF;
587         }
588     }
589   else
590     {
591       unsigned int pfsize = read_u32(&data);
592       char *prefix = sat_malloc(pfsize);
593       char *pp = prefix;
594       char *old_str = "";
595       char *dest = strsp;
596       if (fread(prefix, pfsize, 1, fp) != 1)
597         {
598           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
599           sat_free(prefix);
600           return SOLV_ERROR_EOF;
601         }
602       for (i = 1; i < numid; i++)
603         {
604           int same = (unsigned char)*pp++;
605           size_t len = strlen (pp) + 1;
606           if (same)
607             memcpy(dest, old_str, same);
608           memcpy(dest + same, pp, len);
609           pp += len;
610           old_str = dest;
611           dest += same + len;
612         }
613       sat_free(prefix);
614     }
615   strsp[sizeid] = 0;                   /* make string space \0 terminated */
616   sp = strsp;
617
618
619   /* alloc id map for name and rel Ids. this maps ids in the solv files
620    * to the ids in our pool */
621   idmap = sat_calloc(numid + numrel, sizeof(Id));
622
623   /*
624    * build hashes for all read strings
625    * 
626    */
627   
628   hashmask = mkmask(pool->ss.nstrings + numid);
629
630 #if 0
631   POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
632   POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
633 #endif
634
635   /*
636    * create hashtable with strings already in pool
637    */
638
639   hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
640   for (i = 1; i < pool->ss.nstrings; i++)  /* leave out our dummy zero id */
641     {
642       h = strhash(pool->ss.stringspace + pool->ss.strings[i]) & hashmask;
643       hh = HASHCHAIN_START;
644       while (hashtbl[h])
645         h = HASHCHAIN_NEXT(h, hh, hashmask);
646       hashtbl[h] = i;
647     }
648
649   /*
650    * run over string space, calculate offsets
651    * 
652    * build id map (maps solv Id -> pool Id)
653    */
654   
655   for (i = 1; i < numid; i++)
656     {
657       if (sp >= strsp + sizeid)
658         {
659           sat_free(hashtbl);
660           sat_free(idmap);
661           pool_debug(pool, SAT_ERROR, "not enough strings\n");
662           return SOLV_ERROR_OVERFLOW;
663         }
664       if (!*sp)                        /* empty string */
665         {
666           idmap[i] = ID_EMPTY;
667           sp++;
668           continue;
669         }
670
671       /* find hash slot */
672       h = strhash(sp) & hashmask;
673       hh = HASHCHAIN_START;
674       for (;;)
675         {
676           id = hashtbl[h];
677           if (id == 0)
678             break;
679           if (!strcmp(pool->ss.stringspace + pool->ss.strings[id], sp))
680             break;                     /* existing string */
681           h = HASHCHAIN_NEXT(h, hh, hashmask);
682         }
683
684       /* length == offset to next string */
685       l = strlen(sp) + 1;
686       if (id == ID_NULL)               /* end of hash chain -> new string */
687         {
688           id = pool->ss.nstrings++;
689           hashtbl[h] = id;
690           str[id] = pool->ss.sstrings;    /* save Offset */
691           if (sp != pool->ss.stringspace + pool->ss.sstrings)   /* not at end-of-buffer */
692             memmove(pool->ss.stringspace + pool->ss.sstrings, sp, l);   /* append to pool buffer */
693           pool->ss.sstrings += l;
694         }
695       idmap[i] = id;                   /* repo relative -> pool relative */
696       sp += l;                         /* next string */
697     }
698   sat_free(hashtbl);
699   pool_shrink_strings(pool);           /* vacuum */
700
701   
702   /*******  Part 2: Relation IDs  ***************************************/
703
704   /*
705    * read RelDeps
706    * 
707    */
708   
709   if (numrel)
710     {
711       /* extend rels */
712       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
713       ran = pool->rels;
714
715       hashmask = mkmask(pool->nrels + numrel);
716 #if 0
717       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
718       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
719 #endif
720       /*
721        * prep hash table with already existing RelDeps
722        */
723       
724       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
725       for (i = 1; i < pool->nrels; i++)
726         {
727           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
728           hh = HASHCHAIN_START;
729           while (hashtbl[h])
730             h = HASHCHAIN_NEXT(h, hh, hashmask);
731           hashtbl[h] = i;
732         }
733
734       /*
735        * read RelDeps from repo
736        */
737       
738       for (i = 0; i < numrel; i++)
739         {
740           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
741           evr = read_id(&data, i + numid);
742           flags = read_u8(&data);
743           name = idmap[name];           /* map to (pool relative) Ids */
744           evr = idmap[evr];
745           h = relhash(name, evr, flags) & hashmask;
746           hh = HASHCHAIN_START;
747           for (;;)
748             {
749               id = hashtbl[h];
750               if (id == ID_NULL)        /* end of hash chain */
751                 break;
752               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
753                 break;
754               h = HASHCHAIN_NEXT(h, hh, hashmask);
755             }
756           if (id == ID_NULL)            /* new RelDep */
757             {
758               id = pool->nrels++;
759               hashtbl[h] = id;
760               ran[id].name = name;
761               ran[id].evr = evr;
762               ran[id].flags = flags;
763             }
764           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
765         }
766       sat_free(hashtbl);
767       pool_shrink_rels(pool);           /* vacuum */
768     }
769
770
771   /*******  Part 3: Keys  ***********************************************/
772
773   keys = sat_calloc(numkeys, sizeof(*keys));
774   /* keys start at 1 */
775   for (i = 1; i < numkeys; i++)
776     {
777       id = read_id(&data, numid);
778       if (idmap)
779         id = idmap[id];
780       keys[i].name = id;
781       keys[i].type = read_id(&data, 0);
782       keys[i].size = read_id(&data, 0);
783       keys[i].storage = KEY_STORAGE_DROPPED;
784       switch (keys[i].type)
785         {
786         case TYPE_ID:
787           switch(id)
788             {
789             case SOLVABLE_NAME:
790             case SOLVABLE_ARCH:
791             case SOLVABLE_EVR:
792             case SOLVABLE_VENDOR:
793               keys[i].storage = KEY_STORAGE_SOLVABLE;
794               break;
795             default:
796               keys[i].storage = KEY_STORAGE_INCORE;
797               break;
798             }
799           break;
800         case TYPE_IDARRAY:
801         case TYPE_REL_IDARRAY:
802           if (id >= INTERESTED_START && id <= INTERESTED_END)
803             keys[i].storage = KEY_STORAGE_SOLVABLE;
804           else
805             keys[i].storage = KEY_STORAGE_INCORE;
806           break;
807         case TYPE_STR:
808           keys[i].storage = KEY_STORAGE_INCORE;
809           break;
810         case TYPE_U32:
811           if (id == RPM_RPMDBID)
812             keys[i].storage = KEY_STORAGE_SOLVABLE;
813           else
814             keys[i].storage = KEY_STORAGE_INCORE;
815           break;
816         default:
817           break;
818         }
819     }
820
821   data.keys = keys;
822   data.nkeys = numkeys;
823
824   /*******  Part 4: Schemata ********************************************/
825   
826   id = read_id(&data, 0);
827   schemadata = sat_calloc(id, sizeof(Id));
828   schemadatap = schemadata;
829   schemadataend = schemadata + id;
830   schemata = sat_calloc(numschemata, sizeof(Id));
831   for (i = 0; i < numschemata; i++)
832     {
833       schemata[i] = schemadatap - schemadata;
834       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend, 0);
835     }
836   data.schemata = schemata;
837   data.nschemata = numschemata;
838   data.schemadata = schemadata;
839
840   /*******  Part 5: Info  ***********************************************/
841   for (i = 0; i < numinfo; i++)
842     {
843       /* for now we're just interested in data that starts with
844        * the repodata_external id
845        */
846       Id *keyp = schemadata + schemata[read_id(&data, numschemata)];
847       key = *keyp;
848       if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == TYPE_VOID)
849         {
850           /* external data for some ids */
851           parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
852         }
853       else
854         skip_schema(&data, keyp, keys, numid, numrel);
855     }
856
857   /*******  Part 6: packed sizes (optional)  ****************************/
858   char *exists = 0;
859   if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
860     {
861       exists = sat_malloc (numsolv);
862       for (i = 0; i < numsolv; i++)
863         exists[i] = read_id(&data, 0) != 0;
864     }
865
866   /*******  Part 7: item data *******************************************/
867
868   /* calculate idarray size */
869   size_idarray = 0;
870   for (i = 1; i < numkeys; i++)
871     {
872       id = keys[i].name;
873       if ((keys[i].type == TYPE_IDARRAY || keys[i].type == TYPE_REL_IDARRAY)
874           && id >= INTERESTED_START && id <= INTERESTED_END)
875         size_idarray += keys[i].size;
876     }
877
878   /* allocate needed space in repo */
879   if (size_idarray)
880     {
881       repo_reserve_ids(repo, 0, size_idarray);
882       idarraydatap = repo->idarraydata + repo->idarraysize;
883       repo->idarraysize += size_idarray;
884       idarraydataend = idarraydatap + size_idarray;
885       repo->lastoff = 0;
886     }
887   else
888     {
889       idarraydatap = 0;
890       idarraydataend = 0;
891     }
892
893   /* read solvables */
894   s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
895
896   /* store start and end of our id block */
897   data.start = s - pool->solvables;
898   data.end = data.start + numsolv;
899
900   for (i = 0; i < numsolv; i++, s++)
901     {
902       if (data.error)
903         break;
904       if (exists && !exists[i])
905         continue;
906       Id *keyp = schemadata + schemata[read_id(&data, numschemata)];
907       while ((key = *keyp++) != 0)
908         {
909           id = keys[key].name;
910           switch (keys[key].type)
911             {
912             case TYPE_ID:
913               did = read_id(&data, numid + numrel);
914               if (idmap)
915                 did = idmap[did];
916               if (id == SOLVABLE_NAME)
917                 s->name = did;
918               else if (id == SOLVABLE_ARCH)
919                 s->arch = did;
920               else if (id == SOLVABLE_EVR)
921                 s->evr = did;
922               else if (id == SOLVABLE_VENDOR)
923                 s->vendor = did;
924               else if (keys[key].storage == KEY_STORAGE_INCORE)
925                 incore_add_id(&data, did);
926 #if 0
927               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
928 #endif
929               break;
930             case TYPE_U32:
931               h = read_u32(&data);
932 #if 0
933               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
934 #endif
935               if (id == RPM_RPMDBID)
936                 {
937                   if (!repo->rpmdbid)
938                     repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
939                   repo->rpmdbid[i] = h;
940                 }
941               else if (keys[key].storage == KEY_STORAGE_INCORE)
942                 incore_add_u32(&data, h);
943               break;
944             case TYPE_STR:
945               if (keys[key].storage == KEY_STORAGE_INCORE)
946                 {
947                   while ((h = read_u8(&data)) != 0)
948                     incore_add_u8(&data, h);
949                   incore_add_u8(&data, 0);
950                 }
951               else
952                 {
953                   while (read_u8(&data) != 0)
954                     ;
955                 }
956               break;
957             case TYPE_IDARRAY:
958             case TYPE_REL_IDARRAY:
959               if (id < INTERESTED_START || id > INTERESTED_END)
960                 {
961                   if (keys[key].storage == KEY_STORAGE_INCORE)
962                     {
963                       if (idmap)
964                         {
965                           Id old = 0, rel = keys[key].type == TYPE_REL_IDARRAY ? SOLVABLE_PREREQMARKER : 0;
966                           do
967                             {
968                               did = read_id(&data, 0);
969                               h = did & 0x40;
970                               did = (did & 0x3f) | ((did >> 1) & ~0x3f);
971                               if (rel)
972                                 {
973                                   if (did == 0)
974                                     {
975                                       did = rel;
976                                       old = 0;
977                                     }
978                                   else
979                                     {
980                                       did += old;
981                                       old = did;
982                                     }
983                                 }
984                               if (did >= numid + numrel)
985                                 abort();
986                               did = idmap[did];
987                               did = ((did & ~0x3f) << 1) | h;
988                               incore_add_id(&data, did);
989                             }
990                           while (h);
991                         }
992                       else
993                         {
994                           while (((h = read_u8(&data)) & 0xc0) != 0)
995                             incore_add_u8(&data, h);
996                           break;
997                         }
998                     }
999                   else
1000                     {
1001                       while ((read_u8(&data) & 0xc0) != 0)
1002                         ;
1003                       break;
1004                     }
1005                   break;
1006                 }
1007               ido = idarraydatap - repo->idarraydata;
1008               idarraydatap = read_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend, keys[key].type == TYPE_REL_IDARRAY);
1009               if (id == SOLVABLE_PROVIDES)
1010                 s->provides = ido;
1011               else if (id == SOLVABLE_OBSOLETES)
1012                 s->obsoletes = ido;
1013               else if (id == SOLVABLE_CONFLICTS)
1014                 s->conflicts = ido;
1015               else if (id == SOLVABLE_REQUIRES)
1016                 s->requires = ido;
1017               else if (id == SOLVABLE_RECOMMENDS)
1018                 s->recommends= ido;
1019               else if (id == SOLVABLE_SUPPLEMENTS)
1020                 s->supplements = ido;
1021               else if (id == SOLVABLE_SUGGESTS)
1022                 s->suggests = ido;
1023               else if (id == SOLVABLE_ENHANCES)
1024                 s->enhances = ido;
1025               else if (id == SOLVABLE_FRESHENS)
1026                 s->freshens = ido;
1027 #if 0
1028               POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1029               for (; repo->idarraydata[ido]; ido++)
1030                 POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1031 #endif
1032               break;
1033 #if 0
1034             case TYPE_VOID:
1035
1036             case TYPE_ATTR_INT:
1037             case TYPE_ATTR_CHUNK:
1038             case TYPE_ATTR_STRING:
1039             case TYPE_ATTR_INTLIST:
1040             case TYPE_ATTR_LOCALIDS:
1041               if (!embedded_store)
1042                 embedded_store = new_store (pool);
1043               add_attr_from_file (embedded_store, i, id, keys[key].type, idmap, numid, &data);
1044               break;
1045 #endif
1046             default:
1047               skip_item(&data, keys[key].type, numid, numrel);
1048             }
1049         }
1050     }
1051
1052   if (data.error)
1053     {
1054       /* free solvables */
1055       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1056       /* free id array */
1057       repo->idarraysize -= size_idarray;
1058       /* free incore data */
1059       data.incoredata = sat_free(data.incoredata);
1060       data.incoredatalen = data.incoredatafree = 0;
1061     }
1062
1063   if (data.incoredatafree)
1064     {
1065       /* shrink excess size */
1066       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1067       data.incoredatafree = 0;
1068     }
1069
1070   for (i = 1; i < numkeys; i++)
1071     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1072       break;
1073   if (i < numkeys && !data.error)
1074     {
1075       /* we have vertical data, make it available */
1076       data.verticaloffset = ftell(fp);
1077     }
1078   else
1079     {
1080       /* no longer needed */
1081       data.fp = 0;
1082     }
1083
1084   if (data.incoredatalen || data.fp)
1085     {
1086       /* we got some data, make it available */
1087       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1088       repo->repodata[repo->nrepodata++] = data;
1089     }
1090   else
1091     {
1092       sat_free(schemata);
1093       sat_free(schemadata);
1094       sat_free(keys);
1095     }
1096
1097   sat_free(exists);
1098 #if 0
1099   if (embedded_store)
1100     {
1101       attr_store_pack (embedded_store);
1102       /* If we have any attributes we also have pages.  */
1103       read_or_setup_pages (fp, embedded_store);
1104       /* The NULL name here means embedded attributes.  */
1105       repo_add_attrstore (repo, embedded_store, NULL);
1106     }
1107 #endif
1108   sat_free(idmap);
1109   mypool = 0;
1110   return data.error;
1111 }
1112
1113 // EOF