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