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