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