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