- actually "unsigned long" is wrong, too. It should be "size_t" and
[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   unsigned int now;
541
542   struct _Stringpool *spool;
543
544   Repodata data;
545
546   now = sat_timems(0);
547
548   memset(&data, 0, sizeof(data));
549   data.repo = repo;
550   data.fp = fp;
551   repopagestore_init(&data.store);
552
553   mypool = pool;
554
555   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
556     {
557       pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
558       return SOLV_ERROR_NOT_SOLV;
559     }
560   solvversion = read_u32(&data);
561   switch (solvversion)
562     {
563       case SOLV_VERSION_8:
564         break;
565       default:
566         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
567         return SOLV_ERROR_UNSUPPORTED;
568     }
569
570   pool_freeidhashes(pool);
571
572   numid = read_u32(&data);
573   numrel = read_u32(&data);
574   numdir = read_u32(&data);
575   numsolv = read_u32(&data);
576   numkeys = read_u32(&data);
577   numschemata = read_u32(&data);
578   solvflags = read_u32(&data);
579
580   if (numdir && numdir < 2)
581     {
582       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
583       return SOLV_ERROR_CORRUPT;
584     }
585
586   if (parent)
587     {
588       if (numrel)
589         {
590           pool_debug(pool, SAT_ERROR, "relations are forbidden in a sub-repository\n");
591           return SOLV_ERROR_CORRUPT;
592         }
593       if (parent->end - parent->start != numsolv)
594         {
595           pool_debug(pool, SAT_ERROR, "sub-repository solvable number doesn't match main repository (%d - %d)\n", parent->end - parent->start, numsolv);
596           return SOLV_ERROR_CORRUPT;
597         }
598     }
599
600   /*******  Part 1: string IDs  *****************************************/
601
602   sizeid = read_u32(&data);            /* size of string+Id space */
603
604   /*
605    * read strings and Ids
606    * 
607    */
608
609   
610   /*
611    * alloc buffers
612    */
613
614   if (!parent)
615     spool = &pool->ss;
616   else
617     {
618       data.localpool = 1;
619       spool = &data.spool;
620       spool->stringspace = sat_malloc(7);
621       strcpy(spool->stringspace, "<NULL>");
622       spool->sstrings = 7;
623       spool->nstrings = 0;
624     }
625
626   /* alloc string buffer */
627   spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
628   /* alloc string offsets (Id -> Offset into string space) */
629   spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
630
631   strsp = spool->stringspace;
632   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
633
634   /* point to _BEHIND_ already allocated string/Id space */
635   strsp += spool->sstrings;
636
637
638   /*
639    * read new repo at end of pool
640    */
641   
642   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
643     {
644       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
645         {
646           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
647           return SOLV_ERROR_EOF;
648         }
649     }
650   else
651     {
652       unsigned int pfsize = read_u32(&data);
653       char *prefix = sat_malloc(pfsize);
654       char *pp = prefix;
655       char *old_str = 0;
656       char *dest = strsp;
657       int freesp = sizeid;
658
659       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
660         {
661           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
662           sat_free(prefix);
663           return SOLV_ERROR_EOF;
664         }
665       for (i = 1; i < numid; i++)
666         {
667           int same = (unsigned char)*pp++;
668           size_t len = strlen(pp) + 1;
669           freesp -= same + len;
670           if (freesp < 0)
671             {
672               pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
673               sat_free(prefix);
674               return SOLV_ERROR_OVERFLOW;
675             }
676           if (same)
677             memcpy(dest, old_str, same);
678           memcpy(dest + same, pp, len);
679           pp += len;
680           old_str = dest;
681           dest += same + len;
682         }
683       sat_free(prefix);
684       if (freesp != 0)
685         {
686           pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
687           return SOLV_ERROR_CORRUPT;
688         }
689     }
690   strsp[sizeid] = 0;                   /* make string space \0 terminated */
691   sp = strsp;
692
693   if (parent)
694     {
695       /* no shared pool, thus no idmap and no unification */
696       idmap = 0;
697       spool->nstrings = numid;
698       str[0] = 0;
699       if (*sp)
700         {
701           /* we need the '' for directories */
702           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
703           return SOLV_ERROR_CORRUPT;
704         }
705       for (i = 1; i < spool->nstrings; i++)
706         {
707           if (sp >= strsp + sizeid)
708             {
709               pool_debug(pool, SAT_ERROR, "not enough strings\n");
710               return SOLV_ERROR_OVERFLOW;
711             }
712           str[i] = sp - spool->stringspace;
713           sp += strlen(sp) + 1;
714         }
715       spool->sstrings = sp - spool->stringspace;
716     }
717   else
718     {
719
720       /* alloc id map for name and rel Ids. this maps ids in the solv files
721        * to the ids in our pool */
722       idmap = sat_calloc(numid + numrel, sizeof(Id));
723
724       /*
725        * build hashes for all read strings
726        * 
727        */
728       
729       hashmask = mkmask(spool->nstrings + numid);
730
731 #if 0
732       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
733       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
734 #endif
735
736       /*
737        * create hashtable with strings already in pool
738        */
739
740       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
741       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
742         {
743           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
744           hh = HASHCHAIN_START;
745           while (hashtbl[h])
746             h = HASHCHAIN_NEXT(h, hh, hashmask);
747           hashtbl[h] = i;
748         }
749
750       /*
751        * run over string space, calculate offsets
752        * 
753        * build id map (maps solv Id -> pool Id)
754        */
755       
756       for (i = 1; i < numid; i++)
757         {
758           if (sp >= strsp + sizeid)
759             {
760               sat_free(hashtbl);
761               sat_free(idmap);
762               pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
763               return SOLV_ERROR_OVERFLOW;
764             }
765           if (!*sp)                            /* empty string */
766             {
767               idmap[i] = ID_EMPTY;
768               sp++;
769               continue;
770             }
771
772           /* find hash slot */
773           h = strhash(sp) & hashmask;
774           hh = HASHCHAIN_START;
775           for (;;)
776             {
777               id = hashtbl[h];
778               if (id == 0)
779                 break;
780               if (!strcmp(spool->stringspace + spool->strings[id], sp))
781                 break;                 /* existing string */
782               h = HASHCHAIN_NEXT(h, hh, hashmask);
783             }
784
785           /* length == offset to next string */
786           l = strlen(sp) + 1;
787           if (id == ID_NULL)           /* end of hash chain -> new string */
788             {
789               id = spool->nstrings++;
790               hashtbl[h] = id;
791               str[id] = spool->sstrings;    /* save Offset */
792               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
793                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
794               spool->sstrings += l;
795             }
796           idmap[i] = id;                       /* repo relative -> pool relative */
797           sp += l;                             /* next string */
798         }
799       sat_free(hashtbl);
800     }
801   pool_shrink_strings(pool);           /* vacuum */
802
803   
804   /*******  Part 2: Relation IDs  ***************************************/
805
806   /*
807    * read RelDeps
808    * 
809    */
810   
811   if (numrel)
812     {
813       /* extend rels */
814       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
815       ran = pool->rels;
816
817       hashmask = mkmask(pool->nrels + numrel);
818 #if 0
819       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
820       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
821 #endif
822       /*
823        * prep hash table with already existing RelDeps
824        */
825       
826       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
827       for (i = 1; i < pool->nrels; i++)
828         {
829           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
830           hh = HASHCHAIN_START;
831           while (hashtbl[h])
832             h = HASHCHAIN_NEXT(h, hh, hashmask);
833           hashtbl[h] = i;
834         }
835
836       /*
837        * read RelDeps from repo
838        */
839       
840       for (i = 0; i < numrel; i++)
841         {
842           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
843           evr = read_id(&data, i + numid);
844           flags = read_u8(&data);
845           name = idmap[name];           /* map to (pool relative) Ids */
846           evr = idmap[evr];
847           h = relhash(name, evr, flags) & hashmask;
848           hh = HASHCHAIN_START;
849           for (;;)
850             {
851               id = hashtbl[h];
852               if (id == ID_NULL)        /* end of hash chain */
853                 break;
854               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
855                 break;
856               h = HASHCHAIN_NEXT(h, hh, hashmask);
857             }
858           if (id == ID_NULL)            /* new RelDep */
859             {
860               id = pool->nrels++;
861               hashtbl[h] = id;
862               ran[id].name = name;
863               ran[id].evr = evr;
864               ran[id].flags = flags;
865             }
866           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
867         }
868       sat_free(hashtbl);
869       pool_shrink_rels(pool);           /* vacuum */
870     }
871
872
873   /*******  Part 3: Dirs  ***********************************************/
874   if (numdir)
875     {
876       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
877       data.dirpool.ndirs = numdir;
878       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
879       data.dirpool.dirs[1] = 1;         /* dir 1: / */
880       for (i = 2; i < numdir; i++)
881         {
882           id = read_id(&data, i + numid);
883           if (id >= numid)
884             data.dirpool.dirs[i] = -(id - numid);
885           else if (idmap)
886             data.dirpool.dirs[i] = idmap[id];
887           else
888             data.dirpool.dirs[i] = id;
889         }
890     }
891
892   /*******  Part 4: Keys  ***********************************************/
893
894   keys = sat_calloc(numkeys, sizeof(*keys));
895   /* keys start at 1 */
896   for (i = 1; i < numkeys; i++)
897     {
898       id = read_id(&data, numid);
899       if (idmap)
900         id = idmap[id];
901       else if (parent)
902         id = str2id(pool, stringpool_id2str(spool, id), 1);
903       type = read_id(&data, numid);
904       if (idmap)
905         type = idmap[type];
906       else if (parent)
907         type = str2id(pool, stringpool_id2str(spool, type), 1);
908       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
909         {
910           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
911           data.error = SOLV_ERROR_UNSUPPORTED;
912           type = REPOKEY_TYPE_VOID;
913         }
914       keys[i].name = id;
915       keys[i].type = type;
916       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
917       keys[i].storage = read_id(&data, 0);
918       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
919         keys[i].storage = KEY_STORAGE_SOLVABLE;
920       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
921         keys[i].storage = KEY_STORAGE_INCORE;
922       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
923         {
924           if (idmap)
925             keys[i].size = idmap[keys[i].size];
926           else if (parent)
927             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
928         }
929 #if 0
930       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
931                keys[i].size, keys[i].storage);
932 #endif
933     }
934
935   have_xdata = parent ? 1 : 0;
936   for (i = 1; i < numkeys; i++)
937     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
938       have_xdata = 1;
939
940   data.keys = keys;
941   data.nkeys = numkeys;
942   for (i = 1; i < numkeys; i++)
943     {
944       id = keys[i].name;
945       data.keybits[(id >> 3) & (sizeof(data.keybits) - 1)] |= 1 << (id & 7);
946     }
947
948   /*******  Part 5: Schemata ********************************************/
949   
950   id = read_id(&data, 0);
951   schemadata = sat_calloc(id + 1, sizeof(Id));
952   schemadatap = schemadata + 1;
953   schemadataend = schemadatap + id;
954   schemata = sat_calloc(numschemata, sizeof(Id));
955   for (i = 1; i < numschemata; i++)
956     {
957       schemata[i] = schemadatap - schemadata;
958       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
959 #if 0
960       Id *sp = schemadata + schemata[i];
961       fprintf(stderr, "schema %d:", i);
962       for (; *sp; sp++)
963         fprintf(stderr, " %d", *sp);
964       fprintf(stderr, "\n");
965 #endif
966     }
967   data.schemata = schemata;
968   data.nschemata = numschemata;
969   data.schemadata = schemadata;
970   data.schemadatalen = schemadataend - data.schemadata;
971
972   /*******  Part 6: Data ********************************************/
973
974   idarraydatap = idarraydataend = 0;
975   size_idarray = 0;
976
977   maxsize = read_id(&data, 0);
978   allsize = read_id(&data, 0);
979   maxsize += 5; /* so we can read the next schema of an array */
980   if (maxsize > allsize)
981     maxsize = allsize;
982
983   buf = sat_calloc(maxsize + DATA_READ_CHUNK + 4, 1);   /* 4 extra bytes to detect overflows */
984   bufend = buf;
985   dp = buf;
986
987   l = maxsize;
988   if (l < DATA_READ_CHUNK)
989     l = DATA_READ_CHUNK;
990   if (l > allsize)
991     l = allsize;
992   if (!l || fread(buf, l, 1, data.fp) != 1)
993     {
994       pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
995       data.error = SOLV_ERROR_EOF;
996       id = 0;
997     }
998   else
999     {
1000       bufend = buf + l;
1001       allsize -= l;
1002       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1003     }
1004
1005   incore_add_id(&data, 0);      /* XXX? */
1006   incore_add_id(&data, id);
1007   keyp = schemadata + schemata[id];
1008   data.mainschema = id;
1009   for (i = 0; keyp[i]; i++)
1010     ;
1011   if (i)
1012     data.mainschemaoffsets = sat_calloc(i, sizeof(Id));
1013
1014   nentries = 0;
1015   keydepth = 0;
1016   s = 0;
1017   needchunk = 1;
1018   for(;;)
1019     {
1020       key = *keyp++;
1021 #if 0
1022 printf("key %d at %d\n", key, keyp - 1 - schemadata);
1023 #endif
1024       if (!key)
1025         {
1026           if (keydepth <= 3)
1027             needchunk = 1;
1028           if (nentries)
1029             {
1030               if (s && keydepth == 3)
1031                 {
1032                   s++;  /* next solvable */
1033                   if (have_xdata)
1034                     data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1035                 }
1036               id = stack[keydepth - 1];
1037               if (!id)
1038                 {
1039                   dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1040                   incore_add_id(&data, id);
1041                 }
1042               keyp = schemadata + schemata[id];
1043               nentries--;
1044               continue;
1045             }
1046           if (!keydepth)
1047             break;
1048           --keydepth;
1049           keyp = schemadata + stack[--keydepth];
1050           nentries = stack[--keydepth];
1051 #if 0
1052 printf("pop flexarray %d %d\n", keydepth, nentries);
1053 #endif
1054           if (!keydepth && s)
1055             s = 0;      /* back from solvables */
1056           continue;
1057         }
1058
1059       if (keydepth == 0)
1060         data.mainschemaoffsets[keyp - 1 - (schemadata + schemata[data.mainschema])] = data.incoredatalen;
1061       if (keydepth == 0 || needchunk)
1062         {
1063           int left = bufend - dp;
1064           /* read data chunk to dp */
1065           if (data.error)
1066             break;
1067           if (left < 0)
1068             {
1069               pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1070               data.error = SOLV_ERROR_EOF;
1071               break;
1072             }
1073           if (left < maxsize)
1074             {
1075               if (left)
1076                 memmove(buf, dp, left);
1077               l = maxsize - left;
1078               if (l < DATA_READ_CHUNK)
1079                 l = DATA_READ_CHUNK;
1080               if (l > allsize)
1081                 l = allsize;
1082               if (l && fread(buf + left, l, 1, data.fp) != 1)
1083                 {
1084                   pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1085                   data.error = SOLV_ERROR_EOF;
1086                   break;
1087                 }
1088               allsize -= l;
1089               left += l;
1090               bufend = buf + left;
1091               if (allsize + left < maxsize)
1092                 maxsize = allsize + left;
1093               dp = buf;
1094             }
1095           needchunk = 0;
1096         }
1097
1098 #if 0
1099 printf("=> %s %s %p\n", id2str(pool, keys[key].name), id2str(pool, keys[key].type), s);
1100 #endif
1101       id = keys[key].name;
1102       if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1103         {
1104           dps = dp;
1105           dp = data_skip(dp, REPOKEY_TYPE_ID);
1106           dp = data_skip(dp, REPOKEY_TYPE_ID);
1107           incore_add_blob(&data, dps, dp - dps);
1108           continue;
1109         }
1110       switch (keys[key].type)
1111         {
1112         case REPOKEY_TYPE_ID:
1113           dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1114           if (s && id == SOLVABLE_NAME)
1115             s->name = did; 
1116           else if (s && id == SOLVABLE_ARCH)
1117             s->arch = did; 
1118           else if (s && id == SOLVABLE_EVR)
1119             s->evr = did; 
1120           else if (s && id == SOLVABLE_VENDOR)
1121             s->vendor = did; 
1122           else if (keys[key].storage == KEY_STORAGE_INCORE)
1123             incore_add_id(&data, did);
1124 #if 0
1125           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1126 #endif
1127           break;
1128         case REPOKEY_TYPE_U32:
1129           dp = data_read_u32(dp, &h);
1130 #if 0
1131           POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1132 #endif
1133           if (s && id == RPM_RPMDBID)
1134             {
1135               if (!repo->rpmdbid)
1136                 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1137               repo->rpmdbid[(s - pool->solvables) - repo->start] = h;
1138             }
1139           else if (keys[key].storage == KEY_STORAGE_INCORE)
1140             incore_add_u32(&data, h);
1141           break;
1142         case REPOKEY_TYPE_IDARRAY:
1143         case REPOKEY_TYPE_REL_IDARRAY:
1144           if (!s || id < INTERESTED_START || id > INTERESTED_END)
1145             {
1146               dps = dp;
1147               dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1148               if (keys[key].storage != KEY_STORAGE_INCORE)
1149                 break;
1150               if (idmap)
1151                 incore_map_idarray(&data, dps, idmap, numid);
1152               else
1153                 incore_add_blob(&data, dps, dp - dps);
1154               break;
1155             }
1156           ido = idarraydatap - repo->idarraydata;
1157           if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1158             dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1159           else if (id == SOLVABLE_REQUIRES)
1160             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1161           else if (id == SOLVABLE_PROVIDES)
1162             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1163           else
1164             dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1165           if (idarraydatap > idarraydataend)
1166             {
1167               pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1168               data.error = SOLV_ERROR_OVERFLOW;
1169               break;
1170             }
1171           if (id == SOLVABLE_PROVIDES)
1172             s->provides = ido;
1173           else if (id == SOLVABLE_OBSOLETES)
1174             s->obsoletes = ido;
1175           else if (id == SOLVABLE_CONFLICTS)
1176             s->conflicts = ido;
1177           else if (id == SOLVABLE_REQUIRES)
1178             s->requires = ido;
1179           else if (id == SOLVABLE_RECOMMENDS)
1180             s->recommends= ido;
1181           else if (id == SOLVABLE_SUPPLEMENTS)
1182             s->supplements = ido;
1183           else if (id == SOLVABLE_SUGGESTS)
1184             s->suggests = ido;
1185           else if (id == SOLVABLE_ENHANCES)
1186             s->enhances = ido;
1187 #if 0
1188           POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1189           for (; repo->idarraydata[ido]; ido++)
1190             POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1191 #endif
1192           break;
1193         case REPOKEY_TYPE_FIXARRAY:
1194         case REPOKEY_TYPE_FLEXARRAY:
1195           if (!keydepth)
1196             needchunk = 1;
1197           if (keydepth == sizeof(stack)/sizeof(*stack))
1198             {
1199               pool_debug(pool, SAT_ERROR, "array stack overflow\n");
1200               data.error = SOLV_ERROR_CORRUPT;
1201               break;
1202             }
1203           stack[keydepth++] = nentries;
1204           stack[keydepth++] = keyp - schemadata;
1205           stack[keydepth++] = 0;
1206           dp = data_read_id(dp, &nentries);
1207           incore_add_id(&data, nentries);
1208           if (!nentries)
1209             {
1210               /* zero size array? */
1211               keydepth -= 2;
1212               nentries = stack[--keydepth];
1213               break;
1214             }
1215           if (keydepth == 3 && id == REPOSITORY_SOLVABLES)
1216             {
1217               /* horray! here come the solvables */
1218               if (nentries != numsolv)
1219                 {
1220                   pool_debug(pool, SAT_ERROR, "inconsistent number of solvables: %d %d\n", nentries, numsolv);
1221                   data.error = SOLV_ERROR_CORRUPT;
1222                   break;
1223                 }
1224               if (idarraydatap)
1225                 {
1226                   pool_debug(pool, SAT_ERROR, "more than one solvable block\n");
1227                   data.error = SOLV_ERROR_CORRUPT;
1228                   break;
1229                 }
1230               if (parent)
1231                 s = pool_id2solvable(pool, parent->start);
1232               else
1233                 s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1234               data.start = s - pool->solvables;
1235               data.end = data.start + numsolv;
1236               repodata_extend_block(&data, data.start, numsolv);
1237               for (i = 1; i < numkeys; i++)
1238                 {
1239                   id = keys[i].name;
1240                   if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1241                       && id >= INTERESTED_START && id <= INTERESTED_END)
1242                     size_idarray += keys[i].size;
1243                 }
1244               /* allocate needed space in repo */
1245               /* we add maxsize because it is an upper limit for all idarrays, thus we can't overflow */
1246               repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1247               idarraydatap = repo->idarraydata + repo->idarraysize;
1248               repo->idarraysize += size_idarray;
1249               idarraydataend = idarraydatap + size_idarray;
1250               repo->lastoff = 0;
1251               if (have_xdata)
1252                 data.incoreoffset[(s - pool->solvables) - data.start] = data.incoredatalen;
1253             }
1254           nentries--;
1255           dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1256           incore_add_id(&data, id);
1257           if (keys[key].type == REPOKEY_TYPE_FIXARRAY)
1258             {
1259               if (!id)
1260                 {
1261                   pool_debug(pool, SAT_ERROR, "illegal fixarray\n");
1262                   data.error = SOLV_ERROR_CORRUPT;
1263                 }
1264               stack[keydepth - 1] = id;
1265             }
1266           keyp = schemadata + schemata[id];
1267           break;
1268         default:
1269           dps = dp;
1270           dp = data_skip(dp, keys[key].type);
1271           if (keys[key].storage == KEY_STORAGE_INCORE)
1272             incore_add_blob(&data, dps, dp - dps);
1273           break;
1274         }
1275     }
1276   /* should shrink idarraydata again */
1277
1278   if (keydepth)
1279     {
1280       pool_debug(pool, SAT_ERROR, "unexpected EOF, depth = %d\n", keydepth);
1281       data.error = SOLV_ERROR_CORRUPT;
1282     }
1283   if (!data.error)
1284     {
1285       if (dp > bufend)
1286         {
1287           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1288           data.error = SOLV_ERROR_EOF;
1289         }
1290     }
1291   sat_free(buf);
1292
1293   if (data.error)
1294     {
1295       /* free solvables */
1296       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1297       /* free id array */
1298       repo->idarraysize -= size_idarray;
1299       /* free incore data */
1300       data.incoredata = sat_free(data.incoredata);
1301       data.incoredatalen = data.incoredatafree = 0;
1302     }
1303
1304   if (data.incoredatafree)
1305     {
1306       /* shrink excess size */
1307       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1308       data.incoredatafree = 0;
1309     }
1310
1311   for (i = 1; i < numkeys; i++)
1312     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1313       break;
1314   if (i < numkeys && !data.error)
1315     {
1316       Id fileoffset = 0;
1317       unsigned int pagesize;
1318       
1319       /* we have vertical data, make it available */
1320       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1321       for (i = 1; i < numkeys; i++)
1322         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1323           {
1324             data.verticaloffset[i] = fileoffset;
1325             fileoffset += keys[i].size;
1326           }
1327       data.lastverticaloffset = fileoffset;
1328       pagesize = read_u32(&data);
1329       data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
1330     }
1331   else
1332     {
1333       /* no longer needed */
1334       data.fp = 0;
1335     }
1336   sat_free(idmap);
1337   mypool = 0;
1338
1339   if (data.error)
1340     {
1341       /* XXX: free repodata? */
1342       return data.error;
1343     }
1344
1345   if (parent)
1346     {
1347       /* overwrite stub repodata */
1348       repodata_free(parent);
1349       *parent = data;
1350     }
1351   else
1352     {
1353       /* make it available as new repodata */
1354       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1355       repo->repodata[repo->nrepodata++] = data;
1356     }
1357
1358   /* create stub repodata entries for all external */
1359   for (key = 1 ; key < data.nkeys; key++)
1360     if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
1361       break;
1362   if (key < data.nkeys)
1363     {
1364       struct create_stub_data stubdata;
1365       /* got some */
1366       memset(&stubdata, 0, sizeof(stubdata));
1367       repodata_search(&data, SOLVID_META, REPOSITORY_EXTERNAL, SEARCH_ARRAYSENTINEL, create_stub_cb, &stubdata);
1368     }
1369
1370   POOL_DEBUG(SAT_DEBUG_STATS, "repo_add_solv took %d ms\n", sat_timems(now));
1371   POOL_DEBUG(SAT_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1372   POOL_DEBUG(SAT_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", data.incoredatalen/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1373   return 0;
1374 }
1375
1376 int
1377 repo_add_solv(Repo *repo, FILE *fp)
1378 {
1379   return repo_add_solv_parent(repo, fp, 0);
1380 }
1381
1382 static void
1383 repodata_load_stub(Repodata *data)
1384 {
1385   FILE *fp;
1386   Pool *pool = data->repo->pool;
1387   if (!pool->loadcallback)
1388     {   
1389       data->state = REPODATA_ERROR;
1390       return;
1391     }   
1392   /* so that we can retrieve meta data */
1393   data->state = REPODATA_AVAILABLE;
1394   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1395   if (!fp)
1396     {   
1397       data->state = REPODATA_ERROR;
1398       return;
1399     }   
1400   if (repo_add_solv_parent(data->repo, fp, data))
1401     data->state = REPODATA_ERROR;
1402   else
1403     data->state = REPODATA_AVAILABLE;
1404   fclose(fp);
1405 }