Add REPOKEY_TYPE_COUNTED type (for arrays of structures).
[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, contentver;
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     {
793       numextra = read_u32(&data);
794       contentver = read_u32(&data);
795     }
796   else
797     numextra = 0, contentver = 1;
798   solvflags = read_u32(&data);
799
800   if (numdir && numdir < 2)
801     {
802       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
803       return SOLV_ERROR_CORRUPT;
804     }
805
806   if (parent)
807     {
808       if (numrel)
809         {
810           pool_debug(pool, SAT_ERROR, "relations are forbidden in a store\n");
811           return SOLV_ERROR_CORRUPT;
812         }
813       if (parent->end - parent->start != numsolv)
814         {
815           pool_debug(pool, SAT_ERROR, "unequal number of solvables in a store\n");
816           return SOLV_ERROR_CORRUPT;
817         }
818       if (parent->nextra != numextra)
819         {
820           pool_debug(pool, SAT_ERROR, "unequal number of non-solvables in a store\n");
821           return SOLV_ERROR_CORRUPT;
822         }
823       if (numinfo)
824         {
825           pool_debug(pool, SAT_ERROR, "info blocks are forbidden in a store\n");
826           return SOLV_ERROR_CORRUPT;
827         }
828     }
829
830   /*******  Part 1: string IDs  *****************************************/
831
832   sizeid = read_u32(&data);            /* size of string+Id space */
833
834   /*
835    * read strings and Ids
836    * 
837    */
838
839   
840   /*
841    * alloc buffers
842    */
843
844   if (!parent)
845     spool = &pool->ss;
846   else
847     {
848       data.localpool = 1;
849       spool = &data.spool;
850       spool->stringspace = sat_malloc(7);
851       strcpy(spool->stringspace, "<NULL>");
852       spool->sstrings = 7;
853       spool->nstrings = 0;
854     }
855
856   /* alloc string buffer */
857   spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
858   /* alloc string offsets (Id -> Offset into string space) */
859   spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
860
861   strsp = spool->stringspace;
862   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
863
864   /* point to _BEHIND_ already allocated string/Id space */
865   strsp += spool->sstrings;
866
867
868   /*
869    * read new repo at end of pool
870    */
871   
872   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
873     {
874       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
875         {
876           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
877           return SOLV_ERROR_EOF;
878         }
879     }
880   else
881     {
882       unsigned int pfsize = read_u32(&data);
883       char *prefix = sat_malloc(pfsize);
884       char *pp = prefix;
885       char *old_str = 0;
886       char *dest = strsp;
887       int freesp = sizeid;
888
889       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
890         {
891           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
892           sat_free(prefix);
893           return SOLV_ERROR_EOF;
894         }
895       for (i = 1; i < numid; i++)
896         {
897           int same = (unsigned char)*pp++;
898           size_t len = strlen(pp) + 1;
899           freesp -= same + len;
900           if (freesp < 0)
901             {
902               pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
903               sat_free(prefix);
904               return SOLV_ERROR_OVERFLOW;
905             }
906           if (same)
907             memcpy(dest, old_str, same);
908           memcpy(dest + same, pp, len);
909           pp += len;
910           old_str = dest;
911           dest += same + len;
912         }
913       sat_free(prefix);
914       if (freesp != 0)
915         {
916           pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
917           return SOLV_ERROR_CORRUPT;
918         }
919     }
920   strsp[sizeid] = 0;                   /* make string space \0 terminated */
921   sp = strsp;
922
923   if (parent)
924     {
925       /* no shared pool, thus no idmap and no unification */
926       idmap = 0;
927       spool->nstrings = numid;
928       str[0] = 0;
929       if (*sp)
930         {
931           /* we need the '' for directories */
932           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
933           return SOLV_ERROR_CORRUPT;
934         }
935       for (i = 1; i < spool->nstrings; i++)
936         {
937           if (sp >= strsp + sizeid)
938             {
939               pool_debug(pool, SAT_ERROR, "not enough strings\n");
940               return SOLV_ERROR_OVERFLOW;
941             }
942           str[i] = sp - spool->stringspace;
943           sp += strlen(sp) + 1;
944         }
945       spool->sstrings = sp - spool->stringspace;
946     }
947   else
948     {
949
950       /* alloc id map for name and rel Ids. this maps ids in the solv files
951        * to the ids in our pool */
952       idmap = sat_calloc(numid + numrel, sizeof(Id));
953
954       /*
955        * build hashes for all read strings
956        * 
957        */
958       
959       hashmask = mkmask(spool->nstrings + numid);
960
961 #if 0
962       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
963       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
964 #endif
965
966       /*
967        * create hashtable with strings already in pool
968        */
969
970       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
971       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
972         {
973           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
974           hh = HASHCHAIN_START;
975           while (hashtbl[h])
976             h = HASHCHAIN_NEXT(h, hh, hashmask);
977           hashtbl[h] = i;
978         }
979
980       /*
981        * run over string space, calculate offsets
982        * 
983        * build id map (maps solv Id -> pool Id)
984        */
985       
986       for (i = 1; i < numid; i++)
987         {
988           if (sp >= strsp + sizeid)
989             {
990               sat_free(hashtbl);
991               sat_free(idmap);
992               pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
993               return SOLV_ERROR_OVERFLOW;
994             }
995           if (!*sp)                            /* empty string */
996             {
997               idmap[i] = ID_EMPTY;
998               sp++;
999               continue;
1000             }
1001
1002           /* find hash slot */
1003           h = strhash(sp) & hashmask;
1004           hh = HASHCHAIN_START;
1005           for (;;)
1006             {
1007               id = hashtbl[h];
1008               if (id == 0)
1009                 break;
1010               if (!strcmp(spool->stringspace + spool->strings[id], sp))
1011                 break;                 /* existing string */
1012               h = HASHCHAIN_NEXT(h, hh, hashmask);
1013             }
1014
1015           /* length == offset to next string */
1016           l = strlen(sp) + 1;
1017           if (id == ID_NULL)           /* end of hash chain -> new string */
1018             {
1019               id = spool->nstrings++;
1020               hashtbl[h] = id;
1021               str[id] = spool->sstrings;    /* save Offset */
1022               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
1023                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
1024               spool->sstrings += l;
1025             }
1026           idmap[i] = id;                       /* repo relative -> pool relative */
1027           sp += l;                             /* next string */
1028         }
1029       sat_free(hashtbl);
1030     }
1031   pool_shrink_strings(pool);           /* vacuum */
1032
1033   
1034   /*******  Part 2: Relation IDs  ***************************************/
1035
1036   /*
1037    * read RelDeps
1038    * 
1039    */
1040   
1041   if (numrel)
1042     {
1043       /* extend rels */
1044       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
1045       ran = pool->rels;
1046
1047       hashmask = mkmask(pool->nrels + numrel);
1048 #if 0
1049       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
1050       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
1051 #endif
1052       /*
1053        * prep hash table with already existing RelDeps
1054        */
1055       
1056       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
1057       for (i = 1; i < pool->nrels; i++)
1058         {
1059           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
1060           hh = HASHCHAIN_START;
1061           while (hashtbl[h])
1062             h = HASHCHAIN_NEXT(h, hh, hashmask);
1063           hashtbl[h] = i;
1064         }
1065
1066       /*
1067        * read RelDeps from repo
1068        */
1069       
1070       for (i = 0; i < numrel; i++)
1071         {
1072           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
1073           evr = read_id(&data, i + numid);
1074           flags = read_u8(&data);
1075           name = idmap[name];           /* map to (pool relative) Ids */
1076           evr = idmap[evr];
1077           h = relhash(name, evr, flags) & hashmask;
1078           hh = HASHCHAIN_START;
1079           for (;;)
1080             {
1081               id = hashtbl[h];
1082               if (id == ID_NULL)        /* end of hash chain */
1083                 break;
1084               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
1085                 break;
1086               h = HASHCHAIN_NEXT(h, hh, hashmask);
1087             }
1088           if (id == ID_NULL)            /* new RelDep */
1089             {
1090               id = pool->nrels++;
1091               hashtbl[h] = id;
1092               ran[id].name = name;
1093               ran[id].evr = evr;
1094               ran[id].flags = flags;
1095             }
1096           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
1097         }
1098       sat_free(hashtbl);
1099       pool_shrink_rels(pool);           /* vacuum */
1100     }
1101
1102
1103   /*******  Part 3: Dirs  ***********************************************/
1104   if (numdir)
1105     {
1106       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1107       data.dirpool.ndirs = numdir;
1108       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
1109       data.dirpool.dirs[1] = 1;         /* dir 1: / */
1110       for (i = 2; i < numdir; i++)
1111         {
1112           id = read_id(&data, i + numid);
1113           if (id >= numid)
1114             data.dirpool.dirs[i] = -(id - numid);
1115           else if (idmap)
1116             data.dirpool.dirs[i] = idmap[id];
1117           else
1118             data.dirpool.dirs[i] = id;
1119         }
1120     }
1121
1122   /*******  Part 4: Keys  ***********************************************/
1123
1124   keys = sat_calloc(numkeys, sizeof(*keys));
1125   /* keys start at 1 */
1126   for (i = 1; i < numkeys; i++)
1127     {
1128       id = read_id(&data, numid);
1129       if (idmap)
1130         id = idmap[id];
1131       else if (parent)
1132         id = str2id(pool, stringpool_id2str(spool, id), 1);
1133       type = read_id(&data, numid);
1134       if (idmap)
1135         type = idmap[type];
1136       else if (parent)
1137         type = str2id(pool, stringpool_id2str(spool, type), 1);
1138       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_SHA256)
1139         {
1140           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1141           data.error = SOLV_ERROR_UNSUPPORTED;
1142           type = REPOKEY_TYPE_VOID;
1143         }
1144       keys[i].name = id;
1145       keys[i].type = type;
1146       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1147       keys[i].storage = read_id(&data, 0);
1148       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1149         keys[i].storage = KEY_STORAGE_SOLVABLE;
1150       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1151         keys[i].storage = KEY_STORAGE_INCORE;
1152       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1153         {
1154           if (idmap)
1155             keys[i].size = idmap[keys[i].size];
1156           else if (parent)
1157             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1158         }
1159 #if 0
1160       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1161                keys[i].size, keys[i].storage);
1162 #endif
1163     }
1164
1165   have_xdata = parent ? 1 : 0;
1166   for (i = 1; i < numkeys; i++)
1167     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1168       have_xdata = 1;
1169
1170   data.keys = keys;
1171   data.nkeys = numkeys;
1172
1173   /*******  Part 5: Schemata ********************************************/
1174   
1175   id = read_id(&data, 0);
1176   schemadata = sat_calloc(id + 1, sizeof(Id));
1177   schemadatap = schemadata + 1;
1178   schemadataend = schemadatap + id;
1179   schemata = sat_calloc(numschemata, sizeof(Id));
1180   for (i = 1; i < numschemata; i++)
1181     {
1182       schemata[i] = schemadatap - schemadata;
1183       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1184 #if 0
1185       Id *sp = schemadata + schemata[i];
1186       fprintf (stderr, "schema %d:", i);
1187       for (; *sp; sp++)
1188         fprintf (stderr, " %d", *sp);
1189       fprintf (stderr, "\n");
1190 #endif
1191     }
1192   data.schemata = schemata;
1193   data.nschemata = numschemata;
1194   data.schemadata = schemadata;
1195   data.schemadatalen = schemadataend - data.schemadata;
1196
1197
1198   /*******  Part 6: Info  ***********************************************/
1199   oldnrepodata = repo->nrepodata;
1200   if (numinfo)
1201     {
1202       id = read_id(&data, 0);
1203       id = read_id(&data, 0);
1204     }
1205   for (i = 0; i < numinfo; i++)
1206     {
1207       /* for now we're just interested in data that starts with
1208        * the repodata_external id
1209        */
1210       Id *keyp;
1211       id = read_id(&data, numschemata);
1212       keyp = schemadata + schemata[id];
1213       key = *keyp;
1214       if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1215         {
1216           /* external data for some ids */
1217           parse_external_repodata(&data, keyp, keys, idmap, numid, numrel);
1218         }
1219       else if (keys[key].name == REPODATA_INFO)
1220         {
1221           parse_info_repodata(&data, keyp, keys, idmap, numid, numrel);
1222         }
1223       else
1224         {
1225           skip_schema(&data, keyp, keys, numid, numrel);
1226         }
1227     }
1228
1229
1230   /*******  Part 7: item data *******************************************/
1231
1232   /* calculate idarray size */
1233   size_idarray = 0;
1234   for (i = 1; i < numkeys; i++)
1235     {
1236       id = keys[i].name;
1237       if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1238           && id >= INTERESTED_START && id <= INTERESTED_END)
1239         size_idarray += keys[i].size;
1240     }
1241
1242   if (numsolv || numextra)
1243     {
1244       maxsize = read_id(&data, 0);
1245       allsize = read_id(&data, 0);
1246       if (maxsize > allsize)
1247         {
1248           pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1249           data.error = SOLV_ERROR_CORRUPT;
1250         }
1251     }
1252   else
1253     maxsize = allsize = 0;
1254
1255   /* allocate needed space in repo */
1256   /* we add maxsize because it is an upper limit for all idarrays */
1257   repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1258   idarraydatap = repo->idarraydata + repo->idarraysize;
1259   repo->idarraysize += size_idarray;
1260   idarraydataend = idarraydatap + size_idarray;
1261   repo->lastoff = 0;
1262
1263   /* read solvables */
1264   if (numsolv)
1265     {
1266       if (parent)
1267         s = pool_id2solvable(pool, parent->start);
1268       else
1269         s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1270       /* store start and end of our id block */
1271       data.start = s - pool->solvables;
1272       data.end = data.start + numsolv;
1273       /* In case we have info blocks, make them refer to our part of the 
1274          repository now.  */
1275       for (i = oldnrepodata; i < repo->nrepodata; i++)
1276         {
1277           repo->repodata[i].start = data.start;
1278           repo->repodata[i].end = data.end;
1279         }
1280     }
1281   else
1282     s = 0;
1283
1284   if (numextra)
1285     {
1286       data.extrastart = repo->nextra;
1287       repodata_extend_extra(&data, numextra);
1288       repo->nextra += numextra;
1289       for (i = oldnrepodata; i < repo->nrepodata; i++)
1290         {
1291           repo->repodata[i].extrastart = data.extrastart;
1292           repo->repodata[i].nextra = data.nextra;
1293         }
1294     }
1295
1296   if (have_xdata)
1297     {
1298       /* reserve one byte so that all offsets are not zero */
1299       incore_add_id(&data, 0);
1300       repodata_extend_block(&data, data.start, numsolv);
1301     }
1302
1303   left = 0;
1304   buf = sat_calloc(maxsize + 4, 1);
1305   dp = buf;
1306   for (i = 0; i < numsolv + numextra; i++, s++)
1307     {
1308       Id *keyp;
1309       if (data.error)
1310         break;
1311
1312       left -= (dp - buf);
1313       if (left < 0)
1314         {
1315           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1316           data.error = SOLV_ERROR_EOF;
1317           break;
1318         }
1319       if (left)
1320         memmove(buf, dp, left);
1321       l = maxsize - left;
1322       if (l > allsize)
1323         l = allsize;
1324       if (l && fread(buf + left, l, 1, data.fp) != 1)
1325         {
1326           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1327           data.error = SOLV_ERROR_EOF;
1328           break;
1329         }
1330       allsize -= l;
1331       left += l;
1332       dp = buf;
1333
1334       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1335       if (have_xdata)
1336         {
1337           if (i < numsolv)
1338             data.incoreoffset[i] = data.incoredatalen;
1339           else
1340             data.extraoffset[i - numsolv] = data.incoredatalen;
1341           incore_add_id(&data, id);
1342         }
1343       if (i >= numsolv)
1344         s = 0;
1345 #if 0
1346       if (i < numsolv)
1347         fprintf(stderr, "solv %d: schema %d\n", i, id);
1348       else
1349         fprintf(stderr, "extra %d: schema %d\n", i - numsolv, id);
1350 #endif
1351       keyp = schemadata + schemata[id];
1352       while ((key = *keyp++) != 0)
1353         {
1354           if (data.error)
1355             break;
1356
1357           id = keys[key].name;
1358 #if 0
1359           if (i < numsolv)
1360             fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1361           else
1362             fprintf(stderr, "extra %d name %d type %d class %d\n", i - numsolv, id, keys[key].type, keys[key].storage);
1363 #endif
1364           if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1365             {
1366               /* copy offset/length into incore */
1367               dps = dp;
1368               dp = data_skip(dp, REPOKEY_TYPE_ID);
1369               dp = data_skip(dp, REPOKEY_TYPE_ID);
1370               incore_add_blob(&data, dps, dp - dps);
1371               continue;
1372             }
1373           switch (keys[key].type)
1374             {
1375             case REPOKEY_TYPE_ID:
1376               dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1377               if (id == SOLVABLE_NAME)
1378                 s->name = did;
1379               else if (id == SOLVABLE_ARCH)
1380                 s->arch = did;
1381               else if (id == SOLVABLE_EVR)
1382                 s->evr = did;
1383               else if (id == SOLVABLE_VENDOR)
1384                 s->vendor = did;
1385               else if (keys[key].storage == KEY_STORAGE_INCORE)
1386                 incore_add_id(&data, did);
1387 #if 0
1388               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1389 #endif
1390               break;
1391             case REPOKEY_TYPE_U32:
1392               dp = data_read_u32(dp, &h);
1393 #if 0
1394               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1395 #endif
1396               if (id == RPM_RPMDBID)
1397                 {
1398                   if (!repo->rpmdbid)
1399                     repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1400                   repo->rpmdbid[i] = h;
1401                 }
1402               else if (keys[key].storage == KEY_STORAGE_INCORE)
1403                 incore_add_u32(&data, h);
1404               break;
1405             case REPOKEY_TYPE_IDARRAY:
1406             case REPOKEY_TYPE_REL_IDARRAY:
1407               if (id < INTERESTED_START || id > INTERESTED_END)
1408                 {
1409                   dps = dp;
1410                   dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1411                   if (keys[key].storage != KEY_STORAGE_INCORE)
1412                     break;
1413                   if (idmap)
1414                     incore_map_idarray(&data, dps, idmap, numid);
1415                   else
1416                     incore_add_blob(&data, dps, dp - dps);
1417                   break;
1418                 }
1419               ido = idarraydatap - repo->idarraydata;
1420               if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1421                 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1422               else if (id == SOLVABLE_REQUIRES)
1423                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1424               else if (id == SOLVABLE_PROVIDES)
1425                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1426               else
1427                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1428               if (idarraydatap > idarraydataend)
1429                 {
1430                   pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1431                   data.error = SOLV_ERROR_OVERFLOW;
1432                   break;
1433                 }
1434               if (id == SOLVABLE_PROVIDES)
1435                 s->provides = ido;
1436               else if (id == SOLVABLE_OBSOLETES)
1437                 s->obsoletes = ido;
1438               else if (id == SOLVABLE_CONFLICTS)
1439                 s->conflicts = ido;
1440               else if (id == SOLVABLE_REQUIRES)
1441                 s->requires = ido;
1442               else if (id == SOLVABLE_RECOMMENDS)
1443                 s->recommends= ido;
1444               else if (id == SOLVABLE_SUPPLEMENTS)
1445                 s->supplements = ido;
1446               else if (id == SOLVABLE_SUGGESTS)
1447                 s->suggests = ido;
1448               else if (id == SOLVABLE_ENHANCES)
1449                 s->enhances = ido;
1450               else if (id == SOLVABLE_FRESHENS)
1451                 s->freshens = ido;
1452 #if 0
1453               POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1454               for (; repo->idarraydata[ido]; ido++)
1455                 POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1456 #endif
1457               break;
1458             default:
1459               dps = dp;
1460               dp = data_skip(dp, keys[key].type);
1461               if (keys[key].storage == KEY_STORAGE_INCORE)
1462                 incore_add_blob(&data, dps, dp - dps);
1463               break;
1464             }
1465         }
1466     }
1467
1468   /* should shrink idarraydata again */
1469
1470   if (!data.error)
1471     {
1472       left -= (dp - buf);
1473       if (left < 0)
1474         {
1475           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1476           data.error = SOLV_ERROR_EOF;
1477         }
1478     }
1479   sat_free(buf);
1480
1481   if (data.error)
1482     {
1483       /* free solvables */
1484       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1485       /* free id array */
1486       repo->idarraysize -= size_idarray;
1487       /* free incore data */
1488       data.incoredata = sat_free(data.incoredata);
1489       data.incoredatalen = data.incoredatafree = 0;
1490     }
1491
1492   if (data.incoredatafree)
1493     {
1494       /* shrink excess size */
1495       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1496       data.incoredatafree = 0;
1497     }
1498
1499   for (i = 1; i < numkeys; i++)
1500     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1501       break;
1502   if (i < numkeys && !data.error)
1503     {
1504       Id fileoffset = 0;
1505       unsigned int pagesize;
1506       
1507       /* we have vertical data, make it available */
1508       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1509       for (i = 1; i < numkeys; i++)
1510         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1511           {
1512             data.verticaloffset[i] = fileoffset;
1513             fileoffset += keys[i].size;
1514           }
1515       data.lastverticaloffset = fileoffset;
1516       pagesize = read_u32(&data);
1517       repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1518     }
1519   else
1520     {
1521       /* no longer needed */
1522       data.fp = 0;
1523     }
1524
1525   if (parent && !data.error)
1526     {
1527       /* we're a store */
1528       sat_free(parent->schemata);
1529       sat_free(parent->schemadata);
1530       sat_free(parent->keys);
1531       sat_free(parent->location);
1532       *parent = data;
1533     }
1534   else if ((data.incoredatalen || data.fp) && !data.error)
1535     {
1536       /* we got some data, make it available */
1537       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1538       repo->repodata[repo->nrepodata++] = data;
1539     }
1540   else
1541     {
1542       /* discard data */
1543       sat_free(data.dirpool.dirs);
1544       sat_free(data.incoreoffset);
1545       sat_free(schemata);
1546       sat_free(schemadata);
1547       sat_free(keys);
1548     }
1549
1550   sat_free(idmap);
1551   mypool = 0;
1552   return data.error;
1553 }
1554
1555 int
1556 repo_add_solv(Repo *repo, FILE *fp)
1557 {
1558   return repo_add_solv_parent(repo, fp, 0);
1559 }
1560
1561 static void
1562 repodata_load_solv(Repodata *data)
1563 {
1564   FILE *fp;
1565   Pool *pool = data->repo->pool;
1566   if (!pool->loadcallback)
1567     {   
1568       data->state = REPODATA_ERROR;
1569       return;
1570     }   
1571   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1572   if (!fp)
1573     {   
1574       data->state = REPODATA_ERROR;
1575       return;
1576     }   
1577   if (repo_add_solv_parent(data->repo, fp, data))
1578     data->state = REPODATA_ERROR;
1579   else
1580     data->state = REPODATA_AVAILABLE;
1581   fclose(fp);
1582 }