- add new flags for the load functions:
[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, SAT_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, SAT_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, SAT_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, SAT_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, SAT_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, SAT_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, SAT_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, SAT_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, SAT_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 (max && x >= max)
208     {
209       pool_debug(mypool, SAT_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 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, SAT_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 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, SAT_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 x)
305 {
306   unsigned char *dp;
307   /* make sure we have at least 5 bytes free */
308   if (data->incoredatafree < 5)
309     {
310       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
311       data->incoredatafree = INCORE_ADD_CHUNK;
312     }
313   dp = data->incoredata + data->incoredatalen;
314   if (x < 0)
315     abort();
316   if (x >= (1 << 14))
317     {
318       if (x >= (1 << 28))
319         *dp++ = (x >> 28) | 128;
320       if (x >= (1 << 21))
321         *dp++ = (x >> 21) | 128;
322       *dp++ = (x >> 14) | 128;
323     }
324   if (x >= (1 << 7))
325     *dp++ = (x >> 7) | 128;
326   *dp++ = x & 127;
327   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
328   data->incoredatalen = dp - data->incoredata;
329 }
330
331 static void
332 incore_add_blob(Repodata *data, unsigned char *buf, int len)
333 {
334   if (data->incoredatafree < len)
335     {
336       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
337       data->incoredatafree = INCORE_ADD_CHUNK + len;
338     }
339   memcpy(data->incoredata + data->incoredatalen, buf, len);
340   data->incoredatafree -= len;
341   data->incoredatalen += len;
342 }
343
344 static void
345 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
346 {
347   /* We have to map the IDs, which might also change
348      the necessary number of bytes, so we can't just copy
349      over the blob and adjust it.  */
350   for (;;)
351     {
352       Id id;
353       int eof;
354       dp = data_read_ideof(dp, &id, &eof);
355       if (max && id >= max)
356         {
357           pool_debug(mypool, SAT_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
358           data->error = SOLV_ERROR_ID_RANGE;
359           break;
360         }
361       id = map[id];
362       if (id >= 64)
363         id = (id & 63) | ((id & ~63) << 1);
364       incore_add_id(data, eof ? id : id | 64);
365       if (eof)
366         break;
367     }
368 }
369
370 static void
371 incore_add_u32(Repodata *data, unsigned int x)
372 {
373   unsigned char *dp;
374   /* make sure we have at least 4 bytes free */
375   if (data->incoredatafree < 4)
376     {
377       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
378       data->incoredatafree = INCORE_ADD_CHUNK;
379     }
380   dp = data->incoredata + data->incoredatalen;
381   *dp++ = x >> 24;
382   *dp++ = x >> 16;
383   *dp++ = x >> 8;
384   *dp++ = x;
385   data->incoredatafree -= 4;
386   data->incoredatalen += 4;
387 }
388
389 #if 0
390 static void
391 incore_add_u8(Repodata *data, unsigned int x)
392 {
393   unsigned char *dp;
394   /* make sure we have at least 1 byte free */
395   if (data->incoredatafree < 1)
396     {
397       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
398       data->incoredatafree = 1024;
399     }
400   dp = data->incoredata + data->incoredatalen;
401   *dp++ = x;
402   data->incoredatafree--;
403   data->incoredatalen++;
404 }
405 #endif
406
407
408 /*******************************************************************************
409  * our main function
410  */
411
412 /*
413  * read repo from .solv file and add it to pool
414  */
415
416 int
417 repo_add_solv_flags(Repo *repo, FILE *fp, int flags)
418 {
419   Pool *pool = repo->pool;
420   int i, l;
421   unsigned int numid, numrel, numdir, numsolv;
422   unsigned int numkeys, numschemata;
423
424   Offset sizeid;
425   Offset *str;                         /* map Id -> Offset into string space */
426   char *strsp;                         /* repo string space */
427   char *sp;                            /* pointer into string space */
428   Id *idmap;                           /* map of repo Ids to pool Ids */
429   Id id, type;
430   unsigned int hashmask, h;
431   int hh;
432   Id *hashtbl;
433   Id name, evr, did;
434   int relflags;
435   Reldep *ran;
436   unsigned int size_idarray;
437   Id *idarraydatap, *idarraydataend;
438   Offset ido;
439   Solvable *s;
440   unsigned int solvflags;
441   unsigned int solvversion;
442   Repokey *keys;
443   Id *schemadata, *schemadatap, *schemadataend;
444   Id *schemata, key, *keyp;
445   int nentries;
446   int have_xdata;
447   int maxsize, allsize;
448   unsigned char *buf, *bufend, *dp, *dps;
449   Id stack[3 * 5];
450   int keydepth;
451   int needchunk;        /* need a new chunk of data */
452   unsigned int now;
453
454   struct _Stringpool *spool;
455
456   Repodata *parent = 0;
457   Repodata data;
458
459   now = sat_timems(0);
460
461   if ((flags & REPO_USE_LOADING) != 0)
462     {
463       /* this is a stub replace operation */
464       flags |= REPO_EXTEND_SOLVABLES;
465       parent = repo_add_repodata(repo, flags | REPO_REUSE_REPODATA);
466     }
467     
468   memset(&data, 0, sizeof(data));
469   data.repo = repo;
470   data.fp = fp;
471   repopagestore_init(&data.store);
472
473   mypool = pool;
474
475   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
476     {
477       pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
478       return SOLV_ERROR_NOT_SOLV;
479     }
480   solvversion = read_u32(&data);
481   switch (solvversion)
482     {
483       case SOLV_VERSION_8:
484         break;
485       default:
486         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
487         return SOLV_ERROR_UNSUPPORTED;
488     }
489
490   pool_freeidhashes(pool);
491
492   numid = read_u32(&data);
493   numrel = read_u32(&data);
494   numdir = read_u32(&data);
495   numsolv = read_u32(&data);
496   numkeys = read_u32(&data);
497   numschemata = read_u32(&data);
498   solvflags = read_u32(&data);
499
500   if (numdir && numdir < 2)
501     {
502       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
503       return SOLV_ERROR_CORRUPT;
504     }
505
506   if (numrel && (flags & REPO_LOCALPOOL) != 0)
507     {
508       pool_debug(pool, SAT_ERROR, "relations are forbidden in a local pool\n");
509       return SOLV_ERROR_CORRUPT;
510     }
511   if (parent && numsolv && parent->end - parent->start != numsolv)
512     {
513       pool_debug(pool, SAT_ERROR, "sub-repository solvable number doesn't match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
514       return SOLV_ERROR_CORRUPT;
515     }
516
517   /*******  Part 1: string IDs  *****************************************/
518
519   sizeid = read_u32(&data);            /* size of string+Id space */
520
521   /*
522    * read strings and Ids
523    * 
524    */
525
526   
527   /*
528    * alloc buffers
529    */
530
531   if (!(flags & REPO_LOCALPOOL))
532     spool = &pool->ss;
533   else
534     {
535       data.localpool = 1;
536       spool = &data.spool;
537       spool->stringspace = sat_malloc(7);
538       strcpy(spool->stringspace, "<NULL>");
539       spool->sstrings = 7;
540       spool->nstrings = 0;
541     }
542
543   /* alloc string buffer */
544   spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
545   /* alloc string offsets (Id -> Offset into string space) */
546   spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
547
548   strsp = spool->stringspace;
549   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
550
551   /* point to _BEHIND_ already allocated string/Id space */
552   strsp += spool->sstrings;
553
554
555   /*
556    * read new repo at end of pool
557    */
558   
559   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
560     {
561       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
562         {
563           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
564           return SOLV_ERROR_EOF;
565         }
566     }
567   else
568     {
569       unsigned int pfsize = read_u32(&data);
570       char *prefix = sat_malloc(pfsize);
571       char *pp = prefix;
572       char *old_str = 0;
573       char *dest = strsp;
574       int freesp = sizeid;
575
576       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
577         {
578           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
579           sat_free(prefix);
580           return SOLV_ERROR_EOF;
581         }
582       for (i = 1; i < numid; i++)
583         {
584           int same = (unsigned char)*pp++;
585           size_t len = strlen(pp) + 1;
586           freesp -= same + len;
587           if (freesp < 0)
588             {
589               pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
590               sat_free(prefix);
591               return SOLV_ERROR_OVERFLOW;
592             }
593           if (same)
594             memcpy(dest, old_str, same);
595           memcpy(dest + same, pp, len);
596           pp += len;
597           old_str = dest;
598           dest += same + len;
599         }
600       sat_free(prefix);
601       if (freesp != 0)
602         {
603           pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
604           return SOLV_ERROR_CORRUPT;
605         }
606     }
607   strsp[sizeid] = 0;                   /* make string space \0 terminated */
608   sp = strsp;
609
610   if ((flags & REPO_LOCALPOOL) != 0)
611     {
612       /* no shared pool, thus no idmap and no unification */
613       idmap = 0;
614       spool->nstrings = numid;
615       str[0] = 0;
616       if (*sp)
617         {
618           /* we need the '' for directories */
619           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
620           return SOLV_ERROR_CORRUPT;
621         }
622       for (i = 1; i < spool->nstrings; i++)
623         {
624           if (sp >= strsp + sizeid)
625             {
626               pool_debug(pool, SAT_ERROR, "not enough strings\n");
627               return SOLV_ERROR_OVERFLOW;
628             }
629           str[i] = sp - spool->stringspace;
630           sp += strlen(sp) + 1;
631         }
632       spool->sstrings = sp - spool->stringspace;
633     }
634   else
635     {
636
637       /* alloc id map for name and rel Ids. this maps ids in the solv files
638        * to the ids in our pool */
639       idmap = sat_calloc(numid + numrel, sizeof(Id));
640
641       /*
642        * build hashes for all read strings
643        * 
644        */
645       
646       hashmask = mkmask(spool->nstrings + numid);
647
648 #if 0
649       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
650       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
651 #endif
652
653       /*
654        * create hashtable with strings already in pool
655        */
656
657       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
658       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
659         {
660           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
661           hh = HASHCHAIN_START;
662           while (hashtbl[h])
663             h = HASHCHAIN_NEXT(h, hh, hashmask);
664           hashtbl[h] = i;
665         }
666
667       /*
668        * run over string space, calculate offsets
669        * 
670        * build id map (maps solv Id -> pool Id)
671        */
672       
673       for (i = 1; i < numid; i++)
674         {
675           if (sp >= strsp + sizeid)
676             {
677               sat_free(hashtbl);
678               sat_free(idmap);
679               pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
680               return SOLV_ERROR_OVERFLOW;
681             }
682           if (!*sp)                            /* empty string */
683             {
684               idmap[i] = ID_EMPTY;
685               sp++;
686               continue;
687             }
688
689           /* find hash slot */
690           h = strhash(sp) & hashmask;
691           hh = HASHCHAIN_START;
692           for (;;)
693             {
694               id = hashtbl[h];
695               if (id == 0)
696                 break;
697               if (!strcmp(spool->stringspace + spool->strings[id], sp))
698                 break;                 /* existing string */
699               h = HASHCHAIN_NEXT(h, hh, hashmask);
700             }
701
702           /* length == offset to next string */
703           l = strlen(sp) + 1;
704           if (id == ID_NULL)           /* end of hash chain -> new string */
705             {
706               id = spool->nstrings++;
707               hashtbl[h] = id;
708               str[id] = spool->sstrings;    /* save Offset */
709               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
710                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
711               spool->sstrings += l;
712             }
713           idmap[i] = id;                       /* repo relative -> pool relative */
714           sp += l;                             /* next string */
715         }
716       sat_free(hashtbl);
717     }
718   pool_shrink_strings(pool);           /* vacuum */
719
720   
721   /*******  Part 2: Relation IDs  ***************************************/
722
723   /*
724    * read RelDeps
725    * 
726    */
727   
728   if (numrel)
729     {
730       /* extend rels */
731       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
732       ran = pool->rels;
733
734       hashmask = mkmask(pool->nrels + numrel);
735 #if 0
736       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
737       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
738 #endif
739       /*
740        * prep hash table with already existing RelDeps
741        */
742       
743       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
744       for (i = 1; i < pool->nrels; i++)
745         {
746           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
747           hh = HASHCHAIN_START;
748           while (hashtbl[h])
749             h = HASHCHAIN_NEXT(h, hh, hashmask);
750           hashtbl[h] = i;
751         }
752
753       /*
754        * read RelDeps from repo
755        */
756       
757       for (i = 0; i < numrel; i++)
758         {
759           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
760           evr = read_id(&data, i + numid);
761           relflags = read_u8(&data);
762           name = idmap[name];           /* map to (pool relative) Ids */
763           evr = idmap[evr];
764           h = relhash(name, evr, relflags) & hashmask;
765           hh = HASHCHAIN_START;
766           for (;;)
767             {
768               id = hashtbl[h];
769               if (id == ID_NULL)        /* end of hash chain */
770                 break;
771               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == relflags)
772                 break;
773               h = HASHCHAIN_NEXT(h, hh, hashmask);
774             }
775           if (id == ID_NULL)            /* new RelDep */
776             {
777               id = pool->nrels++;
778               hashtbl[h] = id;
779               ran[id].name = name;
780               ran[id].evr = evr;
781               ran[id].flags = relflags;
782             }
783           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
784         }
785       sat_free(hashtbl);
786       pool_shrink_rels(pool);           /* vacuum */
787     }
788
789
790   /*******  Part 3: Dirs  ***********************************************/
791   if (numdir)
792     {
793       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
794       data.dirpool.ndirs = numdir;
795       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
796       data.dirpool.dirs[1] = 1;         /* dir 1: / */
797       for (i = 2; i < numdir; i++)
798         {
799           id = read_id(&data, i + numid);
800           if (id >= numid)
801             data.dirpool.dirs[i] = -(id - numid);
802           else if (idmap)
803             data.dirpool.dirs[i] = idmap[id];
804           else
805             data.dirpool.dirs[i] = id;
806         }
807     }
808
809   /*******  Part 4: Keys  ***********************************************/
810
811   keys = sat_calloc(numkeys, sizeof(*keys));
812   /* keys start at 1 */
813   for (i = 1; i < numkeys; i++)
814     {
815       id = read_id(&data, numid);
816       if (idmap)
817         id = idmap[id];
818       else if ((flags & REPO_LOCALPOOL) != 0)
819         id = str2id(pool, stringpool_id2str(spool, id), 1);
820       type = read_id(&data, numid);
821       if (idmap)
822         type = idmap[type];
823       else if ((flags & REPO_LOCALPOOL) != 0)
824         type = str2id(pool, stringpool_id2str(spool, type), 1);
825       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
826         {
827           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
828           data.error = SOLV_ERROR_UNSUPPORTED;
829           type = REPOKEY_TYPE_VOID;
830         }
831       keys[i].name = id;
832       keys[i].type = type;
833       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
834       keys[i].storage = read_id(&data, 0);
835       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
836         keys[i].storage = KEY_STORAGE_SOLVABLE;
837       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
838         keys[i].storage = KEY_STORAGE_INCORE;
839       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
840         {
841           if (idmap)
842             keys[i].size = idmap[keys[i].size];
843           else if ((flags & REPO_LOCALPOOL) != 0)
844             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
845         }
846 #if 0
847       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
848                keys[i].size, keys[i].storage);
849 #endif
850     }
851
852   have_xdata = parent ? 1 : 0;
853   for (i = 1; i < numkeys; i++)
854     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
855       have_xdata = 1;
856
857   data.keys = keys;
858   data.nkeys = numkeys;
859   for (i = 1; i < numkeys; i++)
860     {
861       id = keys[i].name;
862       data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
863     }
864
865   /*******  Part 5: Schemata ********************************************/
866   
867   id = read_id(&data, 0);
868   schemadata = sat_calloc(id + 1, sizeof(Id));
869   schemadatap = schemadata + 1;
870   schemadataend = schemadatap + id;
871   schemata = sat_calloc(numschemata, sizeof(Id));
872   for (i = 1; i < numschemata; i++)
873     {
874       schemata[i] = schemadatap - schemadata;
875       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
876 #if 0
877       Id *sp = schemadata + schemata[i];
878       fprintf(stderr, "schema %d:", i);
879       for (; *sp; sp++)
880         fprintf(stderr, " %d", *sp);
881       fprintf(stderr, "\n");
882 #endif
883     }
884   data.schemata = schemata;
885   data.nschemata = numschemata;
886   data.schemadata = schemadata;
887   data.schemadatalen = schemadataend - data.schemadata;
888
889   /*******  Part 6: Data ********************************************/
890
891   idarraydatap = idarraydataend = 0;
892   size_idarray = 0;
893
894   maxsize = read_id(&data, 0);
895   allsize = read_id(&data, 0);
896   maxsize += 5; /* so we can read the next schema of an array */
897   if (maxsize > allsize)
898     maxsize = allsize;
899
900   buf = sat_calloc(maxsize + DATA_READ_CHUNK + 4, 1);   /* 4 extra bytes to detect overflows */
901   bufend = buf;
902   dp = buf;
903
904   l = maxsize;
905   if (l < DATA_READ_CHUNK)
906     l = DATA_READ_CHUNK;
907   if (l > allsize)
908     l = allsize;
909   if (!l || fread(buf, l, 1, data.fp) != 1)
910     {
911       pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
912       data.error = SOLV_ERROR_EOF;
913       id = 0;
914     }
915   else
916     {
917       bufend = buf + l;
918       allsize -= l;
919       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
920     }
921
922   incore_add_id(&data, 0);      /* XXX? */
923   incore_add_id(&data, id);
924   keyp = schemadata + schemata[id];
925   data.mainschema = id;
926   for (i = 0; keyp[i]; i++)
927     ;
928   if (i)
929     data.mainschemaoffsets = sat_calloc(i, sizeof(Id));
930
931   nentries = 0;
932   keydepth = 0;
933   s = 0;
934   needchunk = 1;
935   for(;;)
936     {
937       key = *keyp++;
938 #if 0
939 printf("key %d at %d\n", key, keyp - 1 - schemadata);
940 #endif
941       if (!key)
942         {
943           if (keydepth <= 3)
944             needchunk = 1;
945           if (nentries)
946             {
947               if (s && keydepth == 3)
948                 {
949                   s++;  /* next solvable */
950                   if (have_xdata)
951                     data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
952                 }
953               id = stack[keydepth - 1];
954               if (!id)
955                 {
956                   dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
957                   incore_add_id(&data, id);
958                 }
959               keyp = schemadata + schemata[id];
960               nentries--;
961               continue;
962             }
963           if (!keydepth)
964             break;
965           --keydepth;
966           keyp = schemadata + stack[--keydepth];
967           nentries = stack[--keydepth];
968 #if 0
969 printf("pop flexarray %d %d\n", keydepth, nentries);
970 #endif
971           if (!keydepth && s)
972             s = 0;      /* back from solvables */
973           continue;
974         }
975
976       if (keydepth == 0)
977         data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
978       if (keydepth == 0 || needchunk)
979         {
980           int left = bufend - dp;
981           /* read data chunk to dp */
982           if (data.error)
983             break;
984           if (left < 0)
985             {
986               pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
987               data.error = SOLV_ERROR_EOF;
988               break;
989             }
990           if (left < maxsize)
991             {
992               if (left)
993                 memmove(buf, dp, left);
994               l = maxsize - left;
995               if (l < DATA_READ_CHUNK)
996                 l = DATA_READ_CHUNK;
997               if (l > allsize)
998                 l = allsize;
999               if (l && fread(buf + left, l, 1, data.fp) != 1)
1000                 {
1001                   pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1002                   data.error = SOLV_ERROR_EOF;
1003                   break;
1004                 }
1005               allsize -= l;
1006               left += l;
1007               bufend = buf + left;
1008               if (allsize + left < maxsize)
1009                 maxsize = allsize + left;
1010               dp = buf;
1011             }
1012           needchunk = 0;
1013         }
1014
1015 #if 0
1016 printf("=> %s %s %p\n", id2str(pool, keys[key].name), id2str(pool, keys[key].type), s);
1017 #endif
1018       id = keys[key].name;
1019       if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1020         {
1021           dps = dp;
1022           dp = data_skip(dp, REPOKEY_TYPE_ID);
1023           dp = data_skip(dp, REPOKEY_TYPE_ID);
1024           incore_add_blob(&data, dps, dp - dps);
1025           continue;
1026         }
1027       switch (keys[key].type)
1028         {
1029         case REPOKEY_TYPE_ID:
1030           dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1031           if (s && id == SOLVABLE_NAME)
1032             s->name = did; 
1033           else if (s && id == SOLVABLE_ARCH)
1034             s->arch = did; 
1035           else if (s && id == SOLVABLE_EVR)
1036             s->evr = did; 
1037           else if (s && id == SOLVABLE_VENDOR)
1038             s->vendor = did; 
1039           else if (keys[key].storage == KEY_STORAGE_INCORE)
1040             incore_add_id(&data, did);
1041 #if 0
1042           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1043 #endif
1044           break;
1045         case REPOKEY_TYPE_U32:
1046           dp = data_read_u32(dp, &h);
1047 #if 0
1048           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1049 #endif
1050           if (s && id == RPM_RPMDBID)
1051             {
1052               if (!repo->rpmdbid)
1053                 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1054               repo->rpmdbid[(s - pool->solvables) - repo->start] = h;
1055             }
1056           else if (keys[key].storage == KEY_STORAGE_INCORE)
1057             incore_add_u32(&data, h);
1058           break;
1059         case REPOKEY_TYPE_IDARRAY:
1060         case REPOKEY_TYPE_REL_IDARRAY:
1061           if (!s || id < INTERESTED_START || id > INTERESTED_END)
1062             {
1063               dps = dp;
1064               dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1065               if (keys[key].storage != KEY_STORAGE_INCORE)
1066                 break;
1067               if (idmap)
1068                 incore_map_idarray(&data, dps, idmap, numid);
1069               else
1070                 incore_add_blob(&data, dps, dp - dps);
1071               break;
1072             }
1073           ido = idarraydatap - repo->idarraydata;
1074           if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1075             dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1076           else if (id == SOLVABLE_REQUIRES)
1077             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1078           else if (id == SOLVABLE_PROVIDES)
1079             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1080           else
1081             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1082           if (idarraydatap > idarraydataend)
1083             {
1084               pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1085               data.error = SOLV_ERROR_OVERFLOW;
1086               break;
1087             }
1088           if (id == SOLVABLE_PROVIDES)
1089             s->provides = ido;
1090           else if (id == SOLVABLE_OBSOLETES)
1091             s->obsoletes = ido;
1092           else if (id == SOLVABLE_CONFLICTS)
1093             s->conflicts = ido;
1094           else if (id == SOLVABLE_REQUIRES)
1095             s->requires = ido;
1096           else if (id == SOLVABLE_RECOMMENDS)
1097             s->recommends= ido;
1098           else if (id == SOLVABLE_SUPPLEMENTS)
1099             s->supplements = ido;
1100           else if (id == SOLVABLE_SUGGESTS)
1101             s->suggests = ido;
1102           else if (id == SOLVABLE_ENHANCES)
1103             s->enhances = ido;
1104 #if 0
1105           POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1106           for (; repo->idarraydata[ido]; ido++)
1107             POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1108 #endif
1109           break;
1110         case REPOKEY_TYPE_FIXARRAY:
1111         case REPOKEY_TYPE_FLEXARRAY:
1112           if (!keydepth)
1113             needchunk = 1;
1114           if (keydepth == sizeof(stack)/sizeof(*stack))
1115             {
1116               pool_debug(pool, SAT_ERROR, "array stack overflow\n");
1117               data.error = SOLV_ERROR_CORRUPT;
1118               break;
1119             }
1120           stack[keydepth++] = nentries;
1121           stack[keydepth++] = keyp - schemadata;
1122           stack[keydepth++] = 0;
1123           dp = data_read_id(dp, &nentries);
1124           incore_add_id(&data, nentries);
1125           if (!nentries)
1126             {
1127               /* zero size array? */
1128               keydepth -= 2;
1129               nentries = stack[--keydepth];
1130               break;
1131             }
1132           if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1133             {
1134               /* horray! here come the solvables */
1135               if (nentries != numsolv)
1136                 {
1137                   pool_debug(pool, SAT_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1138                   data.error = SOLV_ERROR_CORRUPT;
1139                   break;
1140                 }
1141               if (idarraydatap)
1142                 {
1143                   pool_debug(pool, SAT_ERROR, "more than one solvable block\n");
1144                   data.error = SOLV_ERROR_CORRUPT;
1145                   break;
1146                 }
1147               if (parent)
1148                 s = pool_id2solvable(pool, parent->start);
1149               else
1150                 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1151               data.start = s - pool->solvables;
1152               data.end = data.start + numsolv;
1153               repodata_extend_block(&data, data.start, numsolv);
1154               for (i = 1; i < numkeys; i++)
1155                 {
1156                   id = keys[i].name;
1157                   if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1158                       && id >= INTERESTED_START && id <= INTERESTED_END)
1159                     size_idarray += keys[i].size;
1160                 }
1161               /* allocate needed space in repo */
1162               /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1163               repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1164               idarraydatap = repo->idarraydata + repo->idarraysize;
1165               repo->idarraysize += size_idarray;
1166               idarraydataend = idarraydatap + size_idarray;
1167               repo->lastoff = 0;
1168               if (have_xdata)
1169                 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1170             }
1171           nentries--;
1172           dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1173           incore_add_id(&data, id);
1174           if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1175             {
1176               if (!id)
1177                 {
1178                   pool_debug(pool, SAT_ERROR, "illegal fixarray\n");
1179                   data.error = SOLV_ERROR_CORRUPT;
1180                 }
1181               stack[keydepth - 1] = id;
1182             }
1183           keyp = schemadata + schemata[id];
1184           break;
1185         default:
1186           dps = dp;
1187           dp = data_skip(dp, keys[key].type);
1188           if (keys[key].storage == KEY_STORAGE_INCORE)
1189             incore_add_blob(&data, dps, dp - dps);
1190           break;
1191         }
1192     }
1193   /* should shrink idarraydata again */
1194
1195   if (keydepth)
1196     {
1197       pool_debug(pool, SAT_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1198       data.error = SOLV_ERROR_CORRUPT;
1199     }
1200   if (!data.error)
1201     {
1202       if (dp > bufend)
1203         {
1204           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1205           data.error = SOLV_ERROR_EOF;
1206         }
1207     }
1208   sat_free(buf);
1209
1210   if (data.error)
1211     {
1212       /* free solvables */
1213       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1214       /* free id array */
1215       repo->idarraysize -= size_idarray;
1216       /* free incore data */
1217       data.incoredata = sat_free(data.incoredata);
1218       data.incoredatalen = data.incoredatafree = 0;
1219     }
1220
1221   if (data.incoredatafree)
1222     {
1223       /* shrink excess size */
1224       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1225       data.incoredatafree = 0;
1226     }
1227
1228   for (i = 1; i < numkeys; i++)
1229     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1230       break;
1231   if (i < numkeys && !data.error)
1232     {
1233       Id fileoffset = 0;
1234       unsigned int pagesize;
1235       
1236       /* we have vertical data, make it available */
1237       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1238       for (i = 1; i < numkeys; i++)
1239         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1240           {
1241             data.verticaloffset[i] = fileoffset;
1242             fileoffset += keys[i].size;
1243           }
1244       data.lastverticaloffset = fileoffset;
1245       pagesize = read_u32(&data);
1246       data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1247     }
1248   else
1249     {
1250       /* no longer needed */
1251       data.fp = 0;
1252     }
1253   sat_free(idmap);
1254   mypool = 0;
1255
1256   if (data.error)
1257     {
1258       /* XXX: free repodata? */
1259       return data.error;
1260     }
1261
1262   if (parent)
1263     {
1264       /* overwrite stub repodata */
1265       repodata_freedata(parent);
1266       *parent = data;
1267     }
1268   else
1269     {
1270       /* make it available as new repodata */
1271       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1272       repo->repodata[repo->nrepodata++] = data;
1273     }
1274
1275   /* create stub repodata entries for all external */
1276   for (key = 1 ; key < data.nkeys; key++)
1277     if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1278       break;
1279   if (key < data.nkeys && !parent)
1280     repodata_create_stubs(&data);
1281
1282   POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_solv took %d ms\n", sat_timems(now));
1283   POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1284   POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1285   return 0;
1286 }
1287
1288 int
1289 repo_add_solv(Repo *repo, FILE *fp)
1290 {
1291   return repo_add_solv_flags(repo, fp, 0);
1292 }