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