- make repo_add_solv fast again
[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
304 static void
305 incore_add_id(Repodata *data, Id x)
306 {
307   unsigned char *dp;
308   /* make sure we have at least 5 bytes free */
309   if (data->incoredatafree < 5)
310     {
311       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
312       data->incoredatafree = INCORE_ADD_CHUNK;
313     }
314   dp = data->incoredata + data->incoredatalen;
315   if (x < 0)
316     abort();
317   if (x >= (1 << 14))
318     {
319       if (x >= (1 << 28))
320         *dp++ = (x >> 28) | 128;
321       if (x >= (1 << 21))
322         *dp++ = (x >> 21) | 128;
323       *dp++ = (x >> 14) | 128;
324     }
325   if (x >= (1 << 7))
326     *dp++ = (x >> 7) | 128;
327   *dp++ = x & 127;
328   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
329   data->incoredatalen = dp - data->incoredata;
330 }
331
332 static void
333 incore_add_blob(Repodata *data, unsigned char *buf, int len)
334 {
335   if (data->incoredatafree < len)
336     {
337       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
338       data->incoredatafree = INCORE_ADD_CHUNK + len;
339     }
340   memcpy(data->incoredata + data->incoredatalen, buf, len);
341   data->incoredatafree -= len;
342   data->incoredatalen += len;
343 }
344
345 static void
346 incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
347 {
348   /* We have to map the IDs, which might also change
349      the necessary number of bytes, so we can't just copy
350      over the blob and adjust it.  */
351   for (;;)
352     {
353       Id id;
354       int eof;
355       dp = data_read_ideof(dp, &id, &eof);
356       if (max && id >= max)
357         {
358           pool_debug(mypool, SAT_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
359           data->error = SOLV_ERROR_ID_RANGE;
360           break;
361         }
362       id = map[id];
363       if (id >= 64)
364         id = (id & 63) | ((id & ~63) << 1);
365       incore_add_id(data, eof ? id : id | 64);
366       if (eof)
367         break;
368     }
369 }
370
371 static void
372 incore_add_u32(Repodata *data, unsigned int x)
373 {
374   unsigned char *dp;
375   /* make sure we have at least 4 bytes free */
376   if (data->incoredatafree < 4)
377     {
378       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK);
379       data->incoredatafree = INCORE_ADD_CHUNK;
380     }
381   dp = data->incoredata + data->incoredatalen;
382   *dp++ = x >> 24;
383   *dp++ = x >> 16;
384   *dp++ = x >> 8;
385   *dp++ = x;
386   data->incoredatafree -= 4;
387   data->incoredatalen += 4;
388 }
389
390 #if 0
391 static void
392 incore_add_u8(Repodata *data, unsigned int x)
393 {
394   unsigned char *dp;
395   /* make sure we have at least 1 byte free */
396   if (data->incoredatafree < 1)
397     {
398       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
399       data->incoredatafree = 1024;
400     }
401   dp = data->incoredata + data->incoredatalen;
402   *dp++ = x;
403   data->incoredatafree--;
404   data->incoredatalen++;
405 }
406 #endif
407
408
409 /*******************************************************************************
410  * callback to create our stub sub-repodatas from the incore data
411  */
412
413 struct create_stub_data {
414   Repodata *data;
415   Id xkeyname;
416 };
417
418 int
419 create_stub_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
420 {
421   struct create_stub_data *stubdata = cbdata;
422   if (key->name == REPOSITORY_EXTERNAL && key->type == REPOKEY_TYPE_FLEXARRAY)
423     {
424       if (stubdata->data)
425         {
426           repodata_internalize(stubdata->data);
427           if (data->start != data->end)
428             {
429               repodata_extend(stubdata->data, data->start);
430               repodata_extend(stubdata->data, data->end - 1);
431             }
432           stubdata->data = 0;
433         }
434       if (kv->eof == 2)
435         return SEARCH_NEXT_SOLVABLE;
436       stubdata->data = repo_add_repodata(data->repo, 0);
437       stubdata->data->state = REPODATA_STUB;
438       stubdata->data->loadcallback = repodata_load_stub;
439       return SEARCH_ENTERSUB;
440     }
441   if (!stubdata->data)
442     return SEARCH_NEXT_KEY;
443   switch(key->type)
444     {
445     case REPOKEY_TYPE_ID:
446       repodata_set_id(stubdata->data, SOLVID_META, key->name, kv->id);
447       break;
448     case REPOKEY_TYPE_CONSTANTID:
449       repodata_set_constantid(stubdata->data, SOLVID_META, key->name, kv->id);
450       break;
451     case REPOKEY_TYPE_STR:
452       repodata_set_str(stubdata->data, SOLVID_META, key->name, kv->str);
453       break;
454     case REPOKEY_TYPE_VOID:
455       repodata_set_void(stubdata->data, SOLVID_META, key->name);
456       break;
457     case REPOKEY_TYPE_NUM:
458       repodata_set_num(stubdata->data, SOLVID_META, key->name, kv->num);
459       break;
460     case REPOKEY_TYPE_IDARRAY:
461       repodata_add_idarray(stubdata->data, SOLVID_META, key->name, kv->id);
462       if (key->name == REPOSITORY_KEYS)
463         {
464           if (!stubdata->xkeyname)
465             stubdata->xkeyname = kv->id;
466           else
467             {
468               Repokey xkey;
469
470               xkey.name = stubdata->xkeyname;
471               xkey.type = kv->id;
472               xkey.storage = KEY_STORAGE_INCORE;
473               xkey.size = 0;
474               repodata_key2id(stubdata->data, &xkey, 1);
475               stubdata->xkeyname = 0;
476             }
477           if (kv->eof)
478             stubdata->xkeyname = 0;
479         }
480       break;
481     case REPOKEY_TYPE_MD5:
482     case REPOKEY_TYPE_SHA1:
483     case REPOKEY_TYPE_SHA256:
484       repodata_set_checksum(stubdata->data, SOLVID_META, key->name, key->type, kv->str);
485       break;
486     default:
487       return SEARCH_NEXT_KEY;
488     }
489   return 0;
490 }
491
492
493 /*******************************************************************************
494  * our main function
495  */
496
497 /*
498  * read repo from .solv file and add it to pool
499  * if stubdata is set, substitute it with read data
500  * (this is used to replace a repodata stub with the real data)
501  */
502
503 static int
504 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
505 {
506   Pool *pool = repo->pool;
507   int i, l;
508   unsigned int numid, numrel, numdir, numsolv;
509   unsigned int numkeys, numschemata;
510
511   Offset sizeid;
512   Offset *str;                         /* map Id -> Offset into string space */
513   char *strsp;                         /* repo string space */
514   char *sp;                            /* pointer into string space */
515   Id *idmap;                           /* map of repo Ids to pool Ids */
516   Id id, type;
517   unsigned int hashmask, h;
518   int hh;
519   Id *hashtbl;
520   Id name, evr, did;
521   int flags;
522   Reldep *ran;
523   unsigned int size_idarray;
524   Id *idarraydatap, *idarraydataend;
525   Offset ido;
526   Solvable *s;
527   unsigned int solvflags;
528   unsigned int solvversion;
529   Repokey *keys;
530   Id *schemadata, *schemadatap, *schemadataend;
531   Id *schemata, key, *keyp;
532   int nentries;
533   int have_xdata;
534   int maxsize, allsize;
535   unsigned char *buf, *dp, *dps;
536   int left;
537   Id stack[10];
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 */
977   if (maxsize > allsize)
978     maxsize = allsize;
979
980   left = 0;
981   buf = sat_calloc(maxsize + 4, 1);
982   dp = buf;
983
984   l = maxsize;
985   if (l > allsize)
986     l = allsize;
987   if (!l || fread(buf, l, 1, data.fp) != 1)
988     {
989       pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
990       data.error = SOLV_ERROR_EOF;
991       id = 0;
992     }
993   else
994     {
995       left = l;
996       allsize -= l;
997       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
998     }
999
1000   incore_add_id(&data, 0);      /* XXX? */
1001   incore_add_id(&data, id);
1002   keyp = schemadata + schemata[id];
1003   data.mainschema = id;
1004   for (i = 0; keyp[i]; i++)
1005     ;
1006   if (i)
1007     data.mainschemaoffsets = sat_calloc(i, sizeof(Id));
1008
1009   nentries = 0;
1010   keydepth = 0;
1011   s = 0;
1012   needchunk = 1;
1013   for(;;)
1014     {
1015       key = *keyp++;
1016 #if 0
1017 printf("key %d at %d\n", key, keyp - 1 - schemadata);
1018 #endif
1019       if (!key)
1020         {
1021           if (keydepth <= 2)
1022             needchunk = 1;
1023           if (nentries)
1024             {
1025               if (s && keydepth == 2)
1026                 {
1027                   s++;  /* next solvable */
1028                   if (have_xdata)
1029                     data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1030                 }
1031               dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1032               incore_add_id(&data, id);
1033               keyp = schemadata + schemata[id];
1034               nentries--;
1035               continue;
1036             }
1037           if (!keydepth)
1038             break;
1039           keyp = schemadata + stack[--keydepth];
1040           nentries = stack[--keydepth];
1041 #if 0
1042 printf("pop flexarray %d %d\n", keydepth, nentries);
1043 #endif
1044           if (!keydepth && s)
1045             s = 0;      /* back from solvables */
1046           continue;
1047         }
1048
1049       if (keydepth == 0)
1050         data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1051       if (needchunk)
1052         {
1053           /* read data chunk to dp */
1054           if (data.error)
1055             break;
1056           left -= (dp - buf);
1057           if (left < 0)
1058             {
1059               pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1060               data.error = SOLV_ERROR_EOF;
1061               break;
1062             }
1063           if (left)
1064             memmove(buf, dp, left);
1065           l = maxsize - left;
1066           if (l > allsize)
1067             l = allsize;
1068           if (l && fread(buf + left, l, 1, data.fp) != 1)
1069             {
1070               pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1071               data.error = SOLV_ERROR_EOF;
1072               break;
1073             }
1074           allsize -= l;
1075           left += l;
1076           dp = buf;
1077           needchunk = 0;
1078         }
1079
1080 #if 0
1081 printf("=> %s %s %p\n", id2str(pool, keys[key].name), id2str(pool, keys[key].type), s);
1082 #endif
1083       id = keys[key].name;
1084       if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1085         {
1086           dps = dp;
1087           dp = data_skip(dp, REPOKEY_TYPE_ID);
1088           dp = data_skip(dp, REPOKEY_TYPE_ID);
1089           incore_add_blob(&data, dps, dp - dps);
1090           continue;
1091         }
1092       switch (keys[key].type)
1093         {
1094         case REPOKEY_TYPE_ID:
1095           dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1096           if (s && id == SOLVABLE_NAME)
1097             s->name = did; 
1098           else if (s && id == SOLVABLE_ARCH)
1099             s->arch = did; 
1100           else if (s && id == SOLVABLE_EVR)
1101             s->evr = did; 
1102           else if (s && id == SOLVABLE_VENDOR)
1103             s->vendor = did; 
1104           else if (keys[key].storage == KEY_STORAGE_INCORE)
1105             incore_add_id(&data, did);
1106 #if 0
1107           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1108 #endif
1109           break;
1110         case REPOKEY_TYPE_U32:
1111           dp = data_read_u32(dp, &h);
1112 #if 0
1113           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1114 #endif
1115           if (s && id == RPM_RPMDBID)
1116             {
1117               if (!repo->rpmdbid)
1118                 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1119               repo->rpmdbid[(s - pool->solvables) - repo->start] = h;
1120             }
1121           else if (keys[key].storage == KEY_STORAGE_INCORE)
1122             incore_add_u32(&data, h);
1123           break;
1124         case REPOKEY_TYPE_IDARRAY:
1125         case REPOKEY_TYPE_REL_IDARRAY:
1126           if (!s || id < INTERESTED_START || id > INTERESTED_END)
1127             {
1128               dps = dp;
1129               dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1130               if (keys[key].storage != KEY_STORAGE_INCORE)
1131                 break;
1132               if (idmap)
1133                 incore_map_idarray(&data, dps, idmap, numid);
1134               else
1135                 incore_add_blob(&data, dps, dp - dps);
1136               break;
1137             }
1138           ido = idarraydatap - repo->idarraydata;
1139           if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1140             dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1141           else if (id == SOLVABLE_REQUIRES)
1142             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1143           else if (id == SOLVABLE_PROVIDES)
1144             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1145           else
1146             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1147           if (idarraydatap > idarraydataend)
1148             {
1149               pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1150               data.error = SOLV_ERROR_OVERFLOW;
1151               break;
1152             }
1153           if (id == SOLVABLE_PROVIDES)
1154             s->provides = ido;
1155           else if (id == SOLVABLE_OBSOLETES)
1156             s->obsoletes = ido;
1157           else if (id == SOLVABLE_CONFLICTS)
1158             s->conflicts = ido;
1159           else if (id == SOLVABLE_REQUIRES)
1160             s->requires = ido;
1161           else if (id == SOLVABLE_RECOMMENDS)
1162             s->recommends= ido;
1163           else if (id == SOLVABLE_SUPPLEMENTS)
1164             s->supplements = ido;
1165           else if (id == SOLVABLE_SUGGESTS)
1166             s->suggests = ido;
1167           else if (id == SOLVABLE_ENHANCES)
1168             s->enhances = ido;
1169 #if 0
1170           POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1171           for (; repo->idarraydata[ido]; ido++)
1172             POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1173 #endif
1174           break;
1175         case REPOKEY_TYPE_FLEXARRAY:
1176           if (keydepth == sizeof(stack)/sizeof(*stack))
1177             {
1178               pool_debug(pool, SAT_ERROR, "flexarray stack overflow\n");
1179               data.error = SOLV_ERROR_CORRUPT;
1180               break;
1181             }
1182           stack[keydepth++] = nentries;
1183           stack[keydepth++] = keyp - schemadata;
1184           dp = data_read_id(dp, &nentries);
1185           incore_add_id(&data, nentries);
1186           if (!nentries)
1187             {
1188               /* zero size array? */
1189               keydepth--;
1190               nentries = stack[--keydepth];
1191               break;
1192             }
1193           if (keydepth == 2 && id == REPOSITORY_SOLVABLES)
1194             {
1195               /* horray! here come the solvables */
1196               if (nentries != numsolv)
1197                 {
1198                   pool_debug(pool, SAT_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1199                   data.error = SOLV_ERROR_CORRUPT;
1200                   break;
1201                 }
1202               if (idarraydatap)
1203                 {
1204                   pool_debug(pool, SAT_ERROR, "more than one solvable block\n");
1205                   data.error = SOLV_ERROR_CORRUPT;
1206                   break;
1207                 }
1208               if (parent)
1209                 s = pool_id2solvable(pool, parent->start);
1210               else
1211                 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1212               data.start = s - pool->solvables;
1213               data.end = data.start + numsolv;
1214               repodata_extend_block(&data, data.start, numsolv);
1215               for (i = 1; i < numkeys; i++)
1216                 {
1217                   id = keys[i].name;
1218                   if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1219                       && id >= INTERESTED_START && id <= INTERESTED_END)
1220                     size_idarray += keys[i].size;
1221                 }
1222               /* allocate needed space in repo */
1223               /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1224               repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1225               idarraydatap = repo->idarraydata + repo->idarraysize;
1226               repo->idarraysize += size_idarray;
1227               idarraydataend = idarraydatap + size_idarray;
1228               repo->lastoff = 0;
1229               if (have_xdata)
1230                 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1231             }
1232           nentries--;
1233           dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1234           incore_add_id(&data, id);
1235           keyp = schemadata + schemata[id];
1236           break;
1237         default:
1238           dps = dp;
1239           dp = data_skip(dp, keys[key].type);
1240           if (keys[key].storage == KEY_STORAGE_INCORE)
1241             incore_add_blob(&data, dps, dp - dps);
1242           break;
1243         }
1244     }
1245   /* should shrink idarraydata again */
1246
1247   if (keydepth)
1248     {
1249       pool_debug(pool, SAT_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1250       data.error = SOLV_ERROR_CORRUPT;
1251     }
1252   if (!data.error)
1253     {
1254       left -= (dp - buf);
1255       if (left < 0)
1256         {
1257           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1258           data.error = SOLV_ERROR_EOF;
1259         }
1260     }
1261   sat_free(buf);
1262
1263   if (data.error)
1264     {
1265       /* free solvables */
1266       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1267       /* free id array */
1268       repo->idarraysize -= size_idarray;
1269       /* free incore data */
1270       data.incoredata = sat_free(data.incoredata);
1271       data.incoredatalen = data.incoredatafree = 0;
1272     }
1273
1274   if (data.incoredatafree)
1275     {
1276       /* shrink excess size */
1277       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1278       data.incoredatafree = 0;
1279     }
1280
1281   for (i = 1; i < numkeys; i++)
1282     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1283       break;
1284   if (i < numkeys && !data.error)
1285     {
1286       Id fileoffset = 0;
1287       unsigned int pagesize;
1288       
1289       /* we have vertical data, make it available */
1290       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1291       for (i = 1; i < numkeys; i++)
1292         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1293           {
1294             data.verticaloffset[i] = fileoffset;
1295             fileoffset += keys[i].size;
1296           }
1297       data.lastverticaloffset = fileoffset;
1298       pagesize = read_u32(&data);
1299       data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1300     }
1301   else
1302     {
1303       /* no longer needed */
1304       data.fp = 0;
1305     }
1306   sat_free(idmap);
1307   mypool = 0;
1308
1309   if (data.error)
1310     {
1311       /* XXX: free repodata? */
1312       return data.error;
1313     }
1314
1315   if (parent)
1316     {
1317       /* overwrite stub repodata */
1318       repodata_free(parent);
1319       *parent = data;
1320     }
1321   else
1322     {
1323       /* make it available as new repodata */
1324       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1325       repo->repodata[repo->nrepodata++] = data;
1326     }
1327
1328   /* create stub repodata entries for all external */
1329   for (key = 1 ; key < data.nkeys; key++)
1330     if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1331       break;
1332   if (key < data.nkeys)
1333     {
1334       struct create_stub_data stubdata;
1335       /* got some */
1336       memset(&stubdata, 0, sizeof(stubdata));
1337       repodata_search(&data, SOLVID_META, REPOSITORY_EXTERNAL, SEARCH_ARRAYSENTINEL, create_stub_cb, &stubdata);
1338     }
1339   return 0;
1340 }
1341
1342 int
1343 repo_add_solv(Repo *repo, FILE *fp)
1344 {
1345   return repo_add_solv_parent(repo, fp, 0);
1346 }
1347
1348 static void
1349 repodata_load_stub(Repodata *data)
1350 {
1351   FILE *fp;
1352   Pool *pool = data->repo->pool;
1353   if (!pool->loadcallback)
1354     {   
1355       data->state = REPODATA_ERROR;
1356       return;
1357     }   
1358   /* so that we can retrieve meta data */
1359   data->state = REPODATA_AVAILABLE;
1360   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1361   if (!fp)
1362     {   
1363       data->state = REPODATA_ERROR;
1364       return;
1365     }   
1366   if (repo_add_solv_parent(data->repo, fp, data))
1367     data->state = REPODATA_ERROR;
1368   else
1369     data->state = REPODATA_AVAILABLE;
1370   fclose(fp);
1371 }