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