- simplify code a bit, also allow to store RPM_RPMDBID as num (formerly only u32...
[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
29 #include "repopack.h"
30 #include "repopage.h"
31
32 #define INTERESTED_START        SOLVABLE_NAME
33 #define INTERESTED_END          SOLVABLE_ENHANCES
34
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
41
42 static Pool *mypool;            /* for pool_debug... */
43
44
45
46 /*******************************************************************************
47  * functions to extract data from a file handle
48  */
49
50 /*
51  * read u32
52  */
53
54 static unsigned int
55 read_u32(Repodata *data)
56 {
57   int c, i;
58   unsigned int x = 0;
59
60   if (data->error)
61     return 0;
62   for (i = 0; i < 4; i++)
63     {
64       c = getc(data->fp);
65       if (c == EOF)
66         {
67           pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
68           data->error = SOLV_ERROR_EOF;
69           return 0;
70         }
71       x = (x << 8) | c;
72     }
73   return x;
74 }
75
76
77 /*
78  * read u8
79  */
80
81 static unsigned int
82 read_u8(Repodata *data)
83 {
84   int c;
85
86   if (data->error)
87     return 0;
88   c = getc(data->fp);
89   if (c == EOF)
90     {
91       pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
92       data->error = SOLV_ERROR_EOF;
93       return 0;
94     }
95   return c;
96 }
97
98
99 /*
100  * read Id
101  */
102
103 static Id
104 read_id(Repodata *data, Id max)
105 {
106   unsigned int x = 0;
107   int c, i;
108
109   if (data->error)
110     return 0;
111   for (i = 0; i < 5; i++)
112     {
113       c = getc(data->fp);
114       if (c == EOF)
115         {
116           pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
117           data->error = SOLV_ERROR_EOF;
118           return 0;
119         }
120       if (!(c & 128))
121         {
122           x = (x << 7) | c;
123           if (max && x >= max)
124             {
125               pool_debug(mypool, SOLV_ERROR, "read_id: id too large (%u/%u)\n", x, max);
126               data->error = SOLV_ERROR_ID_RANGE;
127               return 0;
128             }
129           return x;
130         }
131       x = (x << 7) ^ c ^ 128;
132     }
133   pool_debug(mypool, SOLV_ERROR, "read_id: id too long\n");
134   data->error = SOLV_ERROR_CORRUPT;
135   return 0;
136 }
137
138
139 static Id *
140 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
141 {
142   unsigned int x = 0;
143   int c;
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, SOLV_ERROR, "unexpected EOF\n");
153           data->error = SOLV_ERROR_EOF;
154           return 0;
155         }
156       if ((c & 128) != 0)
157         {
158           x = (x << 7) ^ c ^ 128;
159           continue;
160         }
161       x = (x << 6) | (c & 63);
162       if (max && x >= max)
163         {
164           pool_debug(mypool, SOLV_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
165           data->error = SOLV_ERROR_ID_RANGE;
166           return 0;
167         }
168       if (map)
169         x = map[x];
170       if (store == end)
171         {
172           pool_debug(mypool, SOLV_ERROR, "read_idarray: array overflow\n");
173           return 0;
174         }
175       *store++ = x;
176       if ((c & 64) == 0)
177         {
178           if (x == 0)   /* already have trailing zero? */
179             return store;
180           if (store == end)
181             {
182               pool_debug(mypool, SOLV_ERROR, "read_idarray: array overflow\n");
183               data->error = SOLV_ERROR_OVERFLOW;
184               return 0;
185             }
186           *store++ = 0;
187           return store;
188         }
189       x = 0;
190     }
191 }
192
193
194 /*******************************************************************************
195  * functions to extract data from memory
196  */
197
198 /*
199  * read array of Ids
200  */
201
202 static inline unsigned char *
203 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
204 {
205   Id x;
206   dp = data_read_id(dp, &x);
207   if (x < 0 || (max && x >= max))
208     {
209       pool_debug(mypool, SOLV_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
210       *error = SOLV_ERROR_ID_RANGE;
211       x = 0;
212     }
213   *ret = map ? map[x] : x;
214   return dp;
215 }
216
217 static unsigned char *
218 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
219 {
220   Id *store = *storep;
221   unsigned int x = 0;
222   int c;
223
224   for (;;)
225     {
226       c = *dp++;
227       if ((c & 128) != 0)
228         {
229           x = (x << 7) ^ c ^ 128;
230           continue;
231         }
232       x = (x << 6) | (c & 63);
233       if (max && x >= max)
234         {
235           pool_debug(mypool, SOLV_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
236           *error = SOLV_ERROR_ID_RANGE;
237           break;
238         }
239       *store++ = x;
240       if ((c & 64) == 0)
241         break;
242       x = 0;
243     }
244   *store++ = 0;
245   *storep = store;
246   return dp;
247 }
248
249 static unsigned char *
250 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
251 {
252   Id *store = *storep;
253   Id old = 0;
254   unsigned int x = 0;
255   int c;
256
257   for (;;)
258     {
259       c = *dp++;
260       if ((c & 128) != 0)
261         {
262           x = (x << 7) ^ c ^ 128;
263           continue;
264         }
265       x = (x << 6) | (c & 63);
266       if (x == 0)
267         {
268           if (!(c & 64))
269             break;
270           if (marker)
271             *store++ = marker;
272           old = 0;
273           continue;
274         }
275       x = old + (x - 1);
276       old = x;
277       if (max && x >= max)
278         {
279           pool_debug(mypool, SOLV_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
280           *error = SOLV_ERROR_ID_RANGE;
281           break;
282         }
283       *store++ = map ? map[x] : x;
284       if (!(c & 64))
285         break;
286       x = 0;
287     }
288   *store++ = 0;
289   *storep = store;
290   return dp;
291 }
292
293
294
295
296 /*******************************************************************************
297  * functions to add data to our incore memory space
298  */
299
300 #define INCORE_ADD_CHUNK 8192
301 #define DATA_READ_CHUNK 8192
302
303 static void
304 incore_add_id(Repodata *data, Id sx)
305 {
306   unsigned int x = (unsigned int)sx;
307   unsigned char *dp;
308   /* make sure we have at least 5 bytes free */
309   if (data->incoredatafree < 5)
310     {
311       data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
312       data->incoredatafree = INCORE_ADD_CHUNK;
313     }
314   dp = data->incoredata + data->incoredatalen;
315   if (x >= (1 << 14))
316     {
317       if (x >= (1 << 28))
318         *dp++ = (x >> 28) | 128;
319       if (x >= (1 << 21))
320         *dp++ = (x >> 21) | 128;
321       *dp++ = (x >> 14) | 128;
322     }
323   if (x >= (1 << 7))
324     *dp++ = (x >> 7) | 128;
325   *dp++ = x & 127;
326   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
327   data->incoredatalen = dp - data->incoredata;
328 }
329
330 static void
331 incore_add_blob(Repodata *data, unsigned char *buf, int len)
332 {
333   if (data->incoredatafree < len)
334     {
335       data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
336       data->incoredatafree = INCORE_ADD_CHUNK + len;
337     }
338   memcpy(data->incoredata + data->incoredatalen, buf, len);
339   data->incoredatafree -= len;
340   data->incoredatalen += len;
341 }
342
343 static void
344 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
345 {
346   /* We have to map the IDs, which might also change
347      the necessary number of bytes, so we can't just copy
348      over the blob and adjust it.  */
349   for (;;)
350     {
351       Id id;
352       int eof;
353       dp = data_read_ideof(dp, &id, &eof);
354       if (id < 0 || (max && id >= max))
355         {
356           pool_debug(mypool, SOLV_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
357           data->error = SOLV_ERROR_ID_RANGE;
358           break;
359         }
360       id = map[id];
361       if (id < 0)
362         abort();        /* for now */
363       if (id >= 64)
364         id = (id & 63) | ((id & ~63) << 1);
365       incore_add_id(data, eof ? id : id | 64);
366       if (eof)
367         break;
368     }
369 }
370
371 #if 0
372 static void
373 incore_add_u32(Repodata *data, unsigned int x)
374 {
375   unsigned char *dp;
376   /* make sure we have at least 4 bytes free */
377   if (data->incoredatafree < 4)
378     {
379       data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
380       data->incoredatafree = INCORE_ADD_CHUNK;
381     }
382   dp = data->incoredata + data->incoredatalen;
383   *dp++ = x >> 24;
384   *dp++ = x >> 16;
385   *dp++ = x >> 8;
386   *dp++ = x;
387   data->incoredatafree -= 4;
388   data->incoredatalen += 4;
389 }
390
391 static void
392 incore_add_u8(Repodata *data, unsigned int x)
393 {
394   unsigned char *dp;
395   /* make sure we have at least 1 byte free */
396   if (data->incoredatafree < 1)
397     {
398       data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + 1024);
399       data->incoredatafree = 1024;
400     }
401   dp = data->incoredata + data->incoredatalen;
402   *dp++ = x;
403   data->incoredatafree--;
404   data->incoredatalen++;
405 }
406 #endif
407
408
409 /*******************************************************************************
410  * our main function
411  */
412
413 /*
414  * read repo from .solv file and add it to pool
415  */
416
417 int
418 repo_add_solv(Repo *repo, FILE *fp, int flags)
419 {
420   Pool *pool = repo->pool;
421   int i, l;
422   unsigned int numid, numrel, numdir, numsolv;
423   unsigned int numkeys, numschemata;
424
425   Offset sizeid;
426   Offset *str;                         /* map Id -> Offset into string space */
427   char *strsp;                         /* repo string space */
428   char *sp;                            /* pointer into string space */
429   Id *idmap;                           /* map of repo Ids to pool Ids */
430   Id id, type;
431   unsigned int hashmask, h;
432   int hh;
433   Id *hashtbl;
434   Id name, evr, did;
435   int relflags;
436   Reldep *ran;
437   unsigned int size_idarray;
438   Id *idarraydatap, *idarraydataend;
439   Offset ido;
440   Solvable *s;
441   unsigned int solvflags;
442   unsigned int solvversion;
443   Repokey *keys;
444   Id *schemadata, *schemadatap, *schemadataend;
445   Id *schemata, key, *keyp;
446   int nentries;
447   int have_xdata;
448   int maxsize, allsize;
449   unsigned char *buf, *bufend, *dp, *dps;
450   Id stack[3 * 5];
451   int keydepth;
452   int needchunk;        /* need a new chunk of data */
453   unsigned int now;
454
455   struct _Stringpool *spool;
456
457   Repodata *parent = 0;
458   Repodata data;
459
460   now = solv_timems(0);
461
462   if ((flags & REPO_USE_LOADING) != 0)
463     {
464       /* this is a stub replace operation */
465       flags |= REPO_EXTEND_SOLVABLES;
466       /* use REPO_REUSE_REPODATA hack so that the old repodata is kept */
467       parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
468     }
469     
470   memset(&data, 0, sizeof(data));
471   data.repo = repo;
472   data.fp = fp;
473   repopagestore_init(&data.store);
474
475   mypool = pool;
476
477   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
478     {
479       pool_debug(pool, SOLV_ERROR, "not a SOLV file\n");
480       return SOLV_ERROR_NOT_SOLV;
481     }
482   solvversion = read_u32(&data);
483   switch (solvversion)
484     {
485       case SOLV_VERSION_8:
486         break;
487       default:
488         pool_debug(pool, SOLV_ERROR, "unsupported SOLV version\n");
489         return SOLV_ERROR_UNSUPPORTED;
490     }
491
492   pool_freeidhashes(pool);
493
494   numid = read_u32(&data);
495   numrel = read_u32(&data);
496   numdir = read_u32(&data);
497   numsolv = read_u32(&data);
498   numkeys = read_u32(&data);
499   numschemata = read_u32(&data);
500   solvflags = read_u32(&data);
501
502   if (numdir && numdir < 2)
503     {
504       pool_debug(pool, SOLV_ERROR, "bad number of dirs\n");
505       return SOLV_ERROR_CORRUPT;
506     }
507
508   if (numrel && (flags & REPO_LOCALPOOL) != 0)
509     {
510       pool_debug(pool, SOLV_ERROR, "relations are forbidden in a local pool\n");
511       return SOLV_ERROR_CORRUPT;
512     }
513   if (parent && numsolv)
514     {
515       /* make sure that we exactly replace the stub repodata */
516       if (parent->end - parent->start != numsolv)
517         {
518           pool_debug(pool, SOLV_ERROR, "sub-repository solvable number does not match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
519           return SOLV_ERROR_CORRUPT;
520         }
521       for (i = 0; i < numsolv; i++)
522         if (pool->solvables[parent->start + i].repo != repo)
523           {
524             pool_debug(pool, SOLV_ERROR, "main repository contains holes\n");
525             return SOLV_ERROR_CORRUPT;
526           }
527     }
528
529   /*******  Part 1: string IDs  *****************************************/
530
531   sizeid = read_u32(&data);            /* size of string+Id space */
532
533   /*
534    * read strings and Ids
535    * 
536    */
537
538   
539   /*
540    * alloc buffers
541    */
542
543   if (!(flags & REPO_LOCALPOOL))
544     spool = &pool->ss;
545   else
546     {
547       data.localpool = 1;
548       spool = &data.spool;
549       spool->stringspace = solv_malloc(7);
550       strcpy(spool->stringspace, "<NULL>");
551       spool->sstrings = 7;
552       spool->nstrings = 0;
553     }
554
555   /* alloc string buffer */
556   spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
557   /* alloc string offsets (Id -> Offset into string space) */
558   spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
559
560   strsp = spool->stringspace;
561   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
562
563   /* point to _BEHIND_ already allocated string/Id space */
564   strsp += spool->sstrings;
565
566
567   /*
568    * read new repo at end of pool
569    */
570   
571   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
572     {
573       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
574         {
575           pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
576           return SOLV_ERROR_EOF;
577         }
578     }
579   else
580     {
581       unsigned int pfsize = read_u32(&data);
582       char *prefix = solv_malloc(pfsize);
583       char *pp = prefix;
584       char *old_str = 0;
585       char *dest = strsp;
586       int freesp = sizeid;
587
588       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
589         {
590           pool_debug(pool, SOLV_ERROR, "read error while reading strings\n");
591           solv_free(prefix);
592           return SOLV_ERROR_EOF;
593         }
594       for (i = 1; i < numid; i++)
595         {
596           int same = (unsigned char)*pp++;
597           size_t len = strlen(pp) + 1;
598           freesp -= same + len;
599           if (freesp < 0)
600             {
601               pool_debug(pool, SOLV_ERROR, "overflow while expanding strings\n");
602               solv_free(prefix);
603               return SOLV_ERROR_OVERFLOW;
604             }
605           if (same)
606             memcpy(dest, old_str, same);
607           memcpy(dest + same, pp, len);
608           pp += len;
609           old_str = dest;
610           dest += same + len;
611         }
612       solv_free(prefix);
613       if (freesp != 0)
614         {
615           pool_debug(pool, SOLV_ERROR, "expanding strings size mismatch\n");
616           return SOLV_ERROR_CORRUPT;
617         }
618     }
619   strsp[sizeid] = 0;                   /* make string space \0 terminated */
620   sp = strsp;
621
622   if ((flags & REPO_LOCALPOOL) != 0)
623     {
624       /* no shared pool, thus no idmap and no unification */
625       idmap = 0;
626       spool->nstrings = numid;
627       str[0] = 0;
628       if (*sp)
629         {
630           /* we need the '' for directories */
631           pool_debug(pool, SOLV_ERROR, "store strings don't start with ''\n");
632           return SOLV_ERROR_CORRUPT;
633         }
634       for (i = 1; i < spool->nstrings; i++)
635         {
636           if (sp >= strsp + sizeid)
637             {
638               pool_debug(pool, SOLV_ERROR, "not enough strings\n");
639               return SOLV_ERROR_OVERFLOW;
640             }
641           str[i] = sp - spool->stringspace;
642           sp += strlen(sp) + 1;
643         }
644       spool->sstrings = sp - spool->stringspace;
645     }
646   else
647     {
648
649       /* alloc id map for name and rel Ids. this maps ids in the solv files
650        * to the ids in our pool */
651       idmap = solv_calloc(numid + numrel, sizeof(Id));
652
653       /*
654        * build hashes for all read strings
655        * 
656        */
657       
658       hashmask = mkmask(spool->nstrings + numid);
659
660 #if 0
661       POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
662       POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
663 #endif
664
665       /*
666        * create hashtable with strings already in pool
667        */
668
669       hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
670       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
671         {
672           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
673           hh = HASHCHAIN_START;
674           while (hashtbl[h])
675             h = HASHCHAIN_NEXT(h, hh, hashmask);
676           hashtbl[h] = i;
677         }
678
679       /*
680        * run over string space, calculate offsets
681        * 
682        * build id map (maps solv Id -> pool Id)
683        */
684       
685       for (i = 1; i < numid; i++)
686         {
687           if (sp >= strsp + sizeid)
688             {
689               solv_free(hashtbl);
690               solv_free(idmap);
691               pool_debug(pool, SOLV_ERROR, "not enough strings %d %d\n", i, numid);
692               return SOLV_ERROR_OVERFLOW;
693             }
694           if (!*sp)                            /* empty string */
695             {
696               idmap[i] = ID_EMPTY;
697               sp++;
698               continue;
699             }
700
701           /* find hash slot */
702           h = strhash(sp) & hashmask;
703           hh = HASHCHAIN_START;
704           for (;;)
705             {
706               id = hashtbl[h];
707               if (id == 0)
708                 break;
709               if (!strcmp(spool->stringspace + spool->strings[id], sp))
710                 break;                 /* existing string */
711               h = HASHCHAIN_NEXT(h, hh, hashmask);
712             }
713
714           /* length == offset to next string */
715           l = strlen(sp) + 1;
716           if (id == ID_NULL)           /* end of hash chain -> new string */
717             {
718               id = spool->nstrings++;
719               hashtbl[h] = id;
720               str[id] = spool->sstrings;    /* save Offset */
721               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
722                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
723               spool->sstrings += l;
724             }
725           idmap[i] = id;                       /* repo relative -> pool relative */
726           sp += l;                             /* next string */
727         }
728       solv_free(hashtbl);
729     }
730   pool_shrink_strings(pool);           /* vacuum */
731
732   
733   /*******  Part 2: Relation IDs  ***************************************/
734
735   /*
736    * read RelDeps
737    * 
738    */
739   
740   if (numrel)
741     {
742       /* extend rels */
743       pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
744       ran = pool->rels;
745
746       hashmask = mkmask(pool->nrels + numrel);
747 #if 0
748       POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
749       POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
750 #endif
751       /*
752        * prep hash table with already existing RelDeps
753        */
754       
755       hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
756       for (i = 1; i < pool->nrels; i++)
757         {
758           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
759           hh = HASHCHAIN_START;
760           while (hashtbl[h])
761             h = HASHCHAIN_NEXT(h, hh, hashmask);
762           hashtbl[h] = i;
763         }
764
765       /*
766        * read RelDeps from repo
767        */
768       
769       for (i = 0; i < numrel; i++)
770         {
771           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
772           evr = read_id(&data, i + numid);
773           relflags = read_u8(&data);
774           name = idmap[name];           /* map to (pool relative) Ids */
775           evr = idmap[evr];
776           h = relhash(name, evr, relflags) & hashmask;
777           hh = HASHCHAIN_START;
778           for (;;)
779             {
780               id = hashtbl[h];
781               if (id == ID_NULL)        /* end of hash chain */
782                 break;
783               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
784                 break;
785               h = HASHCHAIN_NEXT(h, hh, hashmask);
786             }
787           if (id == ID_NULL)            /* new RelDep */
788             {
789               id = pool->nrels++;
790               hashtbl[h] = id;
791               ran[id].name = name;
792               ran[id].evr = evr;
793               ran[id].flags = relflags;
794             }
795           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
796         }
797       solv_free(hashtbl);
798       pool_shrink_rels(pool);           /* vacuum */
799     }
800
801
802   /*******  Part 3: Dirs  ***********************************************/
803   if (numdir)
804     {
805       data.dirpool.dirs = solv_malloc2(numdir, sizeof(Id));
806       data.dirpool.ndirs = numdir;
807       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
808       data.dirpool.dirs[1] = 1;         /* dir 1: / */
809       for (i = 2; i < numdir; i++)
810         {
811           id = read_id(&data, i + numid);
812           if (id >= numid)
813             data.dirpool.dirs[i] = -(id - numid);
814           else if (idmap)
815             data.dirpool.dirs[i] = idmap[id];
816           else
817             data.dirpool.dirs[i] = id;
818         }
819     }
820
821   /*******  Part 4: Keys  ***********************************************/
822
823   keys = solv_calloc(numkeys, sizeof(*keys));
824   /* keys start at 1 */
825   for (i = 1; i < numkeys; i++)
826     {
827       id = read_id(&data, numid);
828       if (idmap)
829         id = idmap[id];
830       else if ((flags & REPO_LOCALPOOL) != 0)
831         id = pool_str2id(pool, stringpool_id2str(spool, id), 1);
832       type = read_id(&data, numid);
833       if (idmap)
834         type = idmap[type];
835       else if ((flags & REPO_LOCALPOOL) != 0)
836         type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
837       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
838         {
839           pool_debug(pool, SOLV_ERROR, "unsupported data type '%s'\n", pool_id2str(pool, type));
840           data.error = SOLV_ERROR_UNSUPPORTED;
841           type = REPOKEY_TYPE_VOID;
842         }
843       keys[i].name = id;
844       keys[i].type = type;
845       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
846       keys[i].storage = read_id(&data, 0);
847       /* old versions used SOLVABLE for main solvable data */
848       if (keys[i].storage == KEY_STORAGE_SOLVABLE)
849         keys[i].storage = KEY_STORAGE_INCORE;
850       if (keys[i].storage != KEY_STORAGE_INCORE && keys[i].storage != KEY_STORAGE_VERTICAL_OFFSET)
851         {
852           pool_debug(pool, SOLV_ERROR, "unsupported storage type %d\n", keys[i].storage);
853           data.error = SOLV_ERROR_UNSUPPORTED;
854         }
855       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
856         {
857           if (keys[i].storage != KEY_STORAGE_INCORE)
858             {
859               pool_debug(pool, SOLV_ERROR, "main solvable data must use incore storage%d\n", keys[i].storage);
860               data.error = SOLV_ERROR_UNSUPPORTED;
861             }
862           keys[i].storage = KEY_STORAGE_SOLVABLE;
863         }
864       /* cannot handle rel idarrays in incore/vertical */
865       if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
866         {
867           pool_debug(pool, SOLV_ERROR, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE\n");
868           data.error = SOLV_ERROR_UNSUPPORTED;
869         }
870       /* cannot handle mapped ids in vertical */
871       if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
872         {
873           pool_debug(pool, SOLV_ERROR, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET\n");
874           data.error = SOLV_ERROR_UNSUPPORTED;
875         }
876  
877       if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
878         keys[i].size = idmap[keys[i].size];
879 #if 0
880       fprintf(stderr, "key %d %s %s %d %d\n", i, pool_id2str(pool,id), pool_id2str(pool, keys[i].type),
881                keys[i].size, keys[i].storage);
882 #endif
883     }
884
885   have_xdata = parent ? 1 : 0;
886   for (i = 1; i < numkeys; i++)
887     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
888       have_xdata = 1;
889
890   data.keys = keys;
891   data.nkeys = numkeys;
892   for (i = 1; i < numkeys; i++)
893     {
894       id = keys[i].name;
895       data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
896     }
897
898   /*******  Part 5: Schemata ********************************************/
899   
900   id = read_id(&data, 0);
901   schemadata = solv_calloc(id + 1, sizeof(Id));
902   schemadatap = schemadata + 1;
903   schemadataend = schemadatap + id;
904   schemata = solv_calloc(numschemata, sizeof(Id));
905   for (i = 1; i < numschemata; i++)
906     {
907       schemata[i] = schemadatap - schemadata;
908       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
909 #if 0
910       Id *sp = schemadata + schemata[i];
911       fprintf(stderr, "schema %d:", i);
912       for (; *sp; sp++)
913         fprintf(stderr, " %d", *sp);
914       fprintf(stderr, "\n");
915 #endif
916     }
917   data.schemata = schemata;
918   data.nschemata = numschemata;
919   data.schemadata = schemadata;
920   data.schemadatalen = schemadataend - data.schemadata;
921
922   /*******  Part 6: Data ********************************************/
923
924   idarraydatap = idarraydataend = 0;
925   size_idarray = 0;
926
927   maxsize = read_id(&data, 0);
928   allsize = read_id(&data, 0);
929   maxsize += 5; /* so we can read the next schema of an array */
930   if (maxsize > allsize)
931     maxsize = allsize;
932
933   buf = solv_calloc(maxsize + DATA_READ_CHUNK + 4, 1);  /* 4 extra bytes to detect overflows */
934   bufend = buf;
935   dp = buf;
936
937   l = maxsize;
938   if (l < DATA_READ_CHUNK)
939     l = DATA_READ_CHUNK;
940   if (l > allsize)
941     l = allsize;
942   if (!l || fread(buf, l, 1, data.fp) != 1)
943     {
944       pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
945       data.error = SOLV_ERROR_EOF;
946       id = 0;
947     }
948   else
949     {
950       bufend = buf + l;
951       allsize -= l;
952       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
953     }
954
955   incore_add_id(&data, 0);      /* XXX? */
956   incore_add_id(&data, id);
957   keyp = schemadata + schemata[id];
958   data.mainschema = id;
959   for (i = 0; keyp[i]; i++)
960     ;
961   if (i)
962     data.mainschemaoffsets = solv_calloc(i, sizeof(Id));
963
964   nentries = 0;
965   keydepth = 0;
966   s = 0;
967   needchunk = 1;
968   for(;;)
969     {
970       /* make sure we have enough room */
971       if (keydepth == 0 || needchunk)
972         {
973           int left = bufend - dp;
974           /* read data chunk to dp */
975           if (data.error)
976             break;
977           if (left < 0)
978             {
979               pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
980               data.error = SOLV_ERROR_EOF;
981               break;
982             }
983           if (left < maxsize)
984             {
985               if (left)
986                 memmove(buf, dp, left);
987               l = maxsize - left;
988               if (l < DATA_READ_CHUNK)
989                 l = DATA_READ_CHUNK;
990               if (l > allsize)
991                 l = allsize;
992               if (l && fread(buf + left, l, 1, data.fp) != 1)
993                 {
994                   pool_debug(mypool, SOLV_ERROR, "unexpected EOF\n");
995                   data.error = SOLV_ERROR_EOF;
996                   break;
997                 }
998               allsize -= l;
999               left += l;
1000               bufend = buf + left;
1001               if (allsize + left < maxsize)
1002                 maxsize = allsize + left;
1003               dp = buf;
1004             }
1005           needchunk = 0;
1006         }
1007
1008       key = *keyp++;
1009 #if 0
1010 printf("key %d at %d\n", key, (int)(keyp - 1 - schemadata));
1011 #endif
1012       if (!key)
1013         {
1014           if (keydepth <= 3)
1015             needchunk = 1;
1016           if (nentries)
1017             {
1018               if (s && keydepth == 3)
1019                 {
1020                   s++;  /* next solvable */
1021                   if (have_xdata)
1022                     data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1023                 }
1024               id = stack[keydepth - 1];
1025               if (!id)
1026                 {
1027                   dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1028                   incore_add_id(&data, id);
1029                 }
1030               keyp = schemadata + schemata[id];
1031               nentries--;
1032               continue;
1033             }
1034           if (!keydepth)
1035             break;
1036           --keydepth;
1037           keyp = schemadata + stack[--keydepth];
1038           nentries = stack[--keydepth];
1039 #if 0
1040 printf("pop flexarray %d %d\n", keydepth, nentries);
1041 #endif
1042           if (!keydepth && s)
1043             s = 0;      /* back from solvables */
1044           continue;
1045         }
1046
1047       if (keydepth == 0)
1048         data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1049
1050 #if 0
1051 printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, keys[key].type), s);
1052 #endif
1053       id = keys[key].name;
1054       if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1055         {
1056           dps = dp;
1057           dp = data_skip(dp, REPOKEY_TYPE_ID);
1058           dp = data_skip(dp, REPOKEY_TYPE_ID);
1059           incore_add_blob(&data, dps, dp - dps);        /* just record offset/size */
1060           continue;
1061         }
1062       switch (keys[key].type)
1063         {
1064         case REPOKEY_TYPE_ID:
1065           dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1066           if (s && id == SOLVABLE_NAME)
1067             s->name = did; 
1068           else if (s && id == SOLVABLE_ARCH)
1069             s->arch = did; 
1070           else if (s && id == SOLVABLE_EVR)
1071             s->evr = did; 
1072           else if (s && id == SOLVABLE_VENDOR)
1073             s->vendor = did; 
1074           else if (keys[key].storage == KEY_STORAGE_INCORE)
1075             incore_add_id(&data, did);
1076 #if 0
1077           POOL_DEBUG(SOLV_DEBUG_STATS, "%s -> %s\n", pool_id2str(pool, id), pool_id2str(pool, did));
1078 #endif
1079           break;
1080         case REPOKEY_TYPE_IDARRAY:
1081         case REPOKEY_TYPE_REL_IDARRAY:
1082           if (!s || id < INTERESTED_START || id > INTERESTED_END)
1083             {
1084               dps = dp;
1085               dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1086               if (keys[key].storage != KEY_STORAGE_INCORE)
1087                 break;
1088               if (idmap)
1089                 incore_map_idarray(&data, dps, idmap, numid);
1090               else
1091                 incore_add_blob(&data, dps, dp - dps);
1092               break;
1093             }
1094           ido = idarraydatap - repo->idarraydata;
1095           if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1096             dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1097           else if (id == SOLVABLE_REQUIRES)
1098             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1099           else if (id == SOLVABLE_PROVIDES)
1100             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1101           else
1102             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1103           if (idarraydatap > idarraydataend)
1104             {
1105               pool_debug(pool, SOLV_ERROR, "idarray overflow\n");
1106               data.error = SOLV_ERROR_OVERFLOW;
1107               break;
1108             }
1109           if (id == SOLVABLE_PROVIDES)
1110             s->provides = ido;
1111           else if (id == SOLVABLE_OBSOLETES)
1112             s->obsoletes = ido;
1113           else if (id == SOLVABLE_CONFLICTS)
1114             s->conflicts = ido;
1115           else if (id == SOLVABLE_REQUIRES)
1116             s->requires = ido;
1117           else if (id == SOLVABLE_RECOMMENDS)
1118             s->recommends= ido;
1119           else if (id == SOLVABLE_SUPPLEMENTS)
1120             s->supplements = ido;
1121           else if (id == SOLVABLE_SUGGESTS)
1122             s->suggests = ido;
1123           else if (id == SOLVABLE_ENHANCES)
1124             s->enhances = ido;
1125 #if 0
1126           POOL_DEBUG(SOLV_DEBUG_STATS, "%s ->\n", pool_id2str(pool, id));
1127           for (; repo->idarraydata[ido]; ido++)
1128             POOL_DEBUG(SOLV_DEBUG_STATS,"  %s\n", pool_dep2str(pool, repo->idarraydata[ido]));
1129 #endif
1130           break;
1131         case REPOKEY_TYPE_FIXARRAY:
1132         case REPOKEY_TYPE_FLEXARRAY:
1133           if (!keydepth)
1134             needchunk = 1;
1135           if (keydepth == sizeof(stack)/sizeof(*stack))
1136             {
1137               pool_debug(pool, SOLV_ERROR, "array stack overflow\n");
1138               data.error = SOLV_ERROR_CORRUPT;
1139               break;
1140             }
1141           stack[keydepth++] = nentries;
1142           stack[keydepth++] = keyp - schemadata;
1143           stack[keydepth++] = 0;
1144           dp = data_read_id_max(dp, &nentries, 0, 0, &data.error);
1145           incore_add_id(&data, nentries);
1146           if (!nentries)
1147             {
1148               /* zero size array? */
1149               keydepth -= 2;
1150               nentries = stack[--keydepth];
1151               break;
1152             }
1153           if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1154             {
1155               /* horray! here come the solvables */
1156               if (nentries != numsolv)
1157                 {
1158                   pool_debug(pool, SOLV_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1159                   data.error = SOLV_ERROR_CORRUPT;
1160                   break;
1161                 }
1162               if (idarraydatap)
1163                 {
1164                   pool_debug(pool, SOLV_ERROR, "more than one solvable block\n");
1165                   data.error = SOLV_ERROR_CORRUPT;
1166                   break;
1167                 }
1168               if (parent)
1169                 s = pool_id2solvable(pool, parent->start);
1170               else
1171                 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1172               data.start = s - pool->solvables;
1173               data.end = data.start + numsolv;
1174               repodata_extend_block(&data, data.start, numsolv);
1175               for (i = 1; i < numkeys; i++)
1176                 {
1177                   id = keys[i].name;
1178                   if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1179                       && id >= INTERESTED_START && id <= INTERESTED_END)
1180                     size_idarray += keys[i].size;
1181                 }
1182               /* allocate needed space in repo */
1183               /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1184               repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1185               idarraydatap = repo->idarraydata + repo->idarraysize;
1186               repo->idarraysize += size_idarray;
1187               idarraydataend = idarraydatap + size_idarray;
1188               repo->lastoff = 0;
1189               if (have_xdata)
1190                 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1191             }
1192           nentries--;
1193           dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1194           incore_add_id(&data, id);
1195           if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1196             {
1197               if (!id)
1198                 {
1199                   pool_debug(pool, SOLV_ERROR, "illegal fixarray\n");
1200                   data.error = SOLV_ERROR_CORRUPT;
1201                 }
1202               stack[keydepth - 1] = id;
1203             }
1204           keyp = schemadata + schemata[id];
1205           break;
1206         default:
1207           if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
1208             {
1209               if (keys[key].type == REPOKEY_TYPE_U32)
1210                 dp = data_read_u32(dp, (unsigned int *)&id);
1211               else
1212                 dp = data_read_id_max(dp, &id, 0, 0, &data.error);
1213               if (!repo->rpmdbid)
1214                 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1215               repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
1216               break;
1217             }
1218           dps = dp;
1219           dp = data_skip(dp, keys[key].type);
1220           if (keys[key].storage == KEY_STORAGE_INCORE)
1221             incore_add_blob(&data, dps, dp - dps);
1222           break;
1223         }
1224     }
1225   /* should shrink idarraydata again */
1226
1227   if (keydepth)
1228     {
1229       pool_debug(pool, SOLV_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1230       data.error = SOLV_ERROR_CORRUPT;
1231     }
1232   if (!data.error)
1233     {
1234       if (dp > bufend)
1235         {
1236           pool_debug(mypool, SOLV_ERROR, "buffer overrun\n");
1237           data.error = SOLV_ERROR_EOF;
1238         }
1239     }
1240   solv_free(buf);
1241
1242   if (data.error)
1243     {
1244       /* free solvables */
1245       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1246       /* free id array */
1247       repo->idarraysize -= size_idarray;
1248       /* free incore data */
1249       data.incoredata = solv_free(data.incoredata);
1250       data.incoredatalen = data.incoredatafree = 0;
1251     }
1252
1253   if (data.incoredatafree)
1254     {
1255       /* shrink excess size */
1256       data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
1257       data.incoredatafree = 0;
1258     }
1259
1260   for (i = 1; i < numkeys; i++)
1261     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1262       break;
1263   if (i < numkeys && !data.error)
1264     {
1265       Id fileoffset = 0;
1266       unsigned int pagesize;
1267       
1268       /* we have vertical data, make it available */
1269       data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
1270       for (i = 1; i < numkeys; i++)
1271         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1272           {
1273             data.verticaloffset[i] = fileoffset;
1274             fileoffset += keys[i].size;
1275           }
1276       data.lastverticaloffset = fileoffset;
1277       pagesize = read_u32(&data);
1278       data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1279     }
1280   else
1281     {
1282       /* no longer needed */
1283       data.fp = 0;
1284     }
1285   solv_free(idmap);
1286   mypool = 0;
1287
1288   if (data.error)
1289     {
1290       /* XXX: free repodata? */
1291       return data.error;
1292     }
1293
1294   if (parent)
1295     {
1296       /* overwrite stub repodata */
1297       repodata_freedata(parent);
1298       data.repodataid = parent->repodataid;
1299       *parent = data;
1300     }
1301   else
1302     {
1303       /* make it available as new repodata */
1304       if (!repo->nrepodata)
1305         {
1306           repo->nrepodata = 1;
1307           repo->repodata = solv_calloc(2, sizeof(data));
1308         }
1309       else
1310         repo->repodata = solv_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1311       data.repodataid = repo->nrepodata;
1312       repo->repodata[repo->nrepodata++] = data;
1313     }
1314
1315   /* create stub repodata entries for all external */
1316   if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
1317     {
1318       for (key = 1 ; key < data.nkeys; key++)
1319         if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1320           break;
1321       if (key < data.nkeys)
1322         repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
1323     }
1324
1325   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));
1326   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1327   POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1328   return 0;
1329 }
1330