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  * repodata.h
10  *
11  */
12 
13 #ifndef LIBSOLV_REPODATA_H
14 #define LIBSOLV_REPODATA_H
15 
16 #include <stdio.h>
17 
18 #include "pooltypes.h"
19 #include "pool.h"
20 #include "dirpool.h"
21 
22 #ifdef LIBSOLV_INTERNAL
23 #include "repopage.h"
24 #endif
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define SIZEOF_MD5	16
31 #define SIZEOF_SHA1	20
32 #define SIZEOF_SHA224	28
33 #define SIZEOF_SHA256	32
34 #define SIZEOF_SHA384	48
35 #define SIZEOF_SHA512	64
36 
37 struct s_Repo;
38 struct s_KeyValue;
39 
40 typedef struct s_Repokey {
41   Id name;
42   Id type;			/* REPOKEY_TYPE_xxx */
43   unsigned int size;
44   unsigned int storage;		/* KEY_STORAGE_xxx */
45 } Repokey;
46 
47 #define KEY_STORAGE_DROPPED             0
48 #define KEY_STORAGE_SOLVABLE            1
49 #define KEY_STORAGE_INCORE              2
50 #define KEY_STORAGE_VERTICAL_OFFSET     3
51 
52 #ifdef LIBSOLV_INTERNAL
53 struct dircache;
54 #endif
55 
56 /* repodata states */
57 #define REPODATA_AVAILABLE	0
58 #define REPODATA_STUB		1
59 #define REPODATA_ERROR		2
60 #define REPODATA_STORE		3
61 #define REPODATA_LOADING	4
62 
63 /* repodata filelist types */
64 /* note that FILELIST_FILTERED means that the data contains a filtered
65  * filelist *AND* that it is authoritative for all included solvables. */
66 #define REPODATA_FILELIST_FILTERED	1
67 #define REPODATA_FILELIST_EXTENSION	2
68 
69 typedef struct s_Repodata {
70   Id repodataid;		/* our id */
71   struct s_Repo *repo;		/* back pointer to repo */
72 
73   int state;			/* available, stub or error */
74 
75   void (*loadcallback)(struct s_Repodata *);
76 
77   int start;			/* start of solvables this repodata is valid for */
78   int end;			/* last solvable + 1 of this repodata */
79 
80   Repokey *keys;		/* keys, first entry is always zero */
81   int nkeys;			/* length of keys array */
82   unsigned char keybits[32];	/* keyname hash */
83 
84   Id *schemata;			/* schema -> offset into schemadata */
85   int nschemata;		/* number of schemata */
86   Id *schemadata;		/* schema storage */
87 
88   Stringpool spool;		/* local string pool */
89   int localpool;		/* is local string pool used */
90 
91   Dirpool dirpool;		/* local dir pool */
92 
93 #ifdef LIBSOLV_INTERNAL
94   FILE *fp;			/* file pointer of solv file */
95   int error;			/* corrupt solv file */
96 
97   int filelisttype;		/* type of filelist */
98   Id *filelistfilter;		/* filelist filter used */
99   char *filelistfilterdata;	/* filelist filter string space */
100 
101   unsigned int schemadatalen;   /* schema storage size */
102   Id *schematahash;		/* unification helper */
103 
104   unsigned char *incoredata;	/* in-core data */
105   unsigned int incoredatalen;	/* in-core data used */
106   unsigned int incoredatafree;	/* free data len */
107 
108   Id mainschema;		/* SOLVID_META schema */
109   Id *mainschemaoffsets;	/* SOLVID_META offsets into incoredata */
110 
111   Id *incoreoffset;		/* offset for all entries */
112 
113   Id *verticaloffset;		/* offset for all verticals, nkeys elements */
114   Id lastverticaloffset;	/* end of verticals */
115 
116   Repopagestore store;		/* our page store */
117   Id storestate;		/* incremented every time the store might change */
118 
119   unsigned char *vincore;	/* internal vertical data */
120   unsigned int vincorelen;	/* data size */
121 
122   Id **attrs;			/* un-internalized attributes */
123   Id **xattrs;			/* anonymous handles */
124   int nxattrs;			/* number of handles */
125 
126   unsigned char *attrdata;	/* their string data space */
127   unsigned int attrdatalen;	/* its len */
128   Id *attriddata;		/* their id space */
129   unsigned int attriddatalen;	/* its len */
130   unsigned long long *attrnum64data;	/* their 64bit num data space */
131   unsigned int attrnum64datalen;	/* its len */
132 
133   /* array cache to speed up repodata_add functions*/
134   Id lasthandle;
135   Id lastkey;
136   Id lastdatalen;
137 
138   /* directory cache to speed up repodata_str2dir */
139   struct dircache *dircache;
140 #endif
141 
142 } Repodata;
143 
144 #define SOLVID_META		-1
145 #define SOLVID_POS		-2
146 
147 
148 /*-----
149  * management functions
150  */
151 void repodata_initdata(Repodata *data, struct s_Repo *repo, int localpool);
152 void repodata_freedata(Repodata *data);
153 
154 void repodata_free(Repodata *data);
155 void repodata_empty(Repodata *data, int localpool);
156 
157 void repodata_load(Repodata *data);
158 
159 /*
160  * key management functions
161  */
162 Id repodata_key2id(Repodata *data, Repokey *key, int create);
163 
164 static inline Repokey *
repodata_id2key(Repodata * data,Id keyid)165 repodata_id2key(Repodata *data, Id keyid)
166 {
167   return data->keys + keyid;
168 }
169 
170 /*
171  * schema management functions
172  */
173 Id repodata_schema2id(Repodata *data, Id *schema, int create);
174 void repodata_free_schemahash(Repodata *data);
175 
176 static inline Id *
repodata_id2schema(Repodata * data,Id schemaid)177 repodata_id2schema(Repodata *data, Id schemaid)
178 {
179   return data->schemadata + data->schemata[schemaid];
180 }
181 
182 /*
183  * data search and access
184  */
185 
186 /* check if there is a chance that the repodata contains data for
187  * the specified keyname */
188 static inline int
repodata_precheck_keyname(Repodata * data,Id keyname)189 repodata_precheck_keyname(Repodata *data, Id keyname)
190 {
191   unsigned char x = data->keybits[(keyname >> 3) & (sizeof(data->keybits) - 1)];
192   return x && (x & (1 << (keyname & 7))) ? 1 : 0;
193 }
194 
195 /* check if the repodata contains data for the specified keyname */
196 static inline int
repodata_has_keyname(Repodata * data,Id keyname)197 repodata_has_keyname(Repodata *data, Id keyname)
198 {
199   int i;
200   if (!repodata_precheck_keyname(data, keyname))
201     return 0;
202   for (i = 1; i < data->nkeys; i++)
203     if (data->keys[i].name == keyname)
204       return 1;
205   return 0;
206 }
207 
208 /* search key <keyname> (all keys, if keyname == 0) for Id <solvid>
209  * Call <callback> for each match */
210 void repodata_search(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, struct s_KeyValue *kv), void *cbdata);
211 void repodata_search_keyskip(Repodata *data, Id solvid, Id keyname, int flags, Id *keyskip, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, struct s_KeyValue *kv), void *cbdata);
212 void repodata_search_arrayelement(Repodata *data, Id solvid, Id keyname, int flags, struct s_KeyValue *kv, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, struct s_KeyValue *kv), void *cbdata);
213 
214 /* Make sure the found KeyValue has the "str" field set. Return "str"
215  * if valid, NULL if not possible */
216 const char *repodata_stringify(Pool *pool, Repodata *data, Repokey *key, struct s_KeyValue *kv, int flags);
217 
218 /* filelist filter support */
219 void repodata_set_filelisttype(Repodata *data, int filelisttype);
220 int repodata_filelistfilter_matches(Repodata *data, const char *str);
221 void repodata_free_filelistfilter(Repodata *data);
222 
223 /* lookup functions */
224 Id repodata_lookup_type(Repodata *data, Id solvid, Id keyname);
225 Id repodata_lookup_id(Repodata *data, Id solvid, Id keyname);
226 const char *repodata_lookup_str(Repodata *data, Id solvid, Id keyname);
227 unsigned long long repodata_lookup_num(Repodata *data, Id solvid, Id keyname, unsigned long long notfound);
228 int repodata_lookup_void(Repodata *data, Id solvid, Id keyname);
229 const unsigned char *repodata_lookup_bin_checksum(Repodata *data, Id solvid, Id keyname, Id *typep);
230 int repodata_lookup_idarray(Repodata *data, Id solvid, Id keyname, Queue *q);
231 const void *repodata_lookup_binary(Repodata *data, Id solvid, Id keyname, int *lenp);
232 unsigned int repodata_lookup_count(Repodata *data, Id solvid, Id keyname);	/* internal */
233 
234 /* internal, used in fileprovides code */
235 const unsigned char *repodata_lookup_packed_dirstrarray(Repodata *data, Id solvid, Id keyname);
236 
237 /* internal, fill keyskip array with data */
238 Id *repodata_fill_keyskip(Repodata *data, Id solvid, Id *keyskip);
239 
240 /*-----
241  * data assignment functions
242  */
243 
244 /*
245  * extend the data so that it contains the specified solvables
246  * (no longer needed, as the repodata_set functions autoextend)
247  */
248 void repodata_extend(Repodata *data, Id p);
249 void repodata_extend_block(Repodata *data, Id p, int num);
250 void repodata_shrink(Repodata *data, int end);
251 
252 /* internalize freshly set data, so that it is found by the search
253  * functions and written out */
254 void repodata_internalize(Repodata *data);
255 
256 /* create an anonymous handle. useful for substructures like
257  * fixarray/flexarray  */
258 Id repodata_new_handle(Repodata *data);
259 
260 /* basic types: void, num, string, Id */
261 void repodata_set_void(Repodata *data, Id solvid, Id keyname);
262 void repodata_set_num(Repodata *data, Id solvid, Id keyname, unsigned long long num);
263 void repodata_set_id(Repodata *data, Id solvid, Id keyname, Id id);
264 void repodata_set_str(Repodata *data, Id solvid, Id keyname, const char *str);
265 void repodata_set_binary(Repodata *data, Id solvid, Id keyname, void *buf, int len);
266 /* create id from string, then set_id */
267 void repodata_set_poolstr(Repodata *data, Id solvid, Id keyname, const char *str);
268 
269 /* set numeric constant */
270 void repodata_set_constant(Repodata *data, Id solvid, Id keyname, unsigned int constant);
271 
272 /* set Id constant */
273 void repodata_set_constantid(Repodata *data, Id solvid, Id keyname, Id id);
274 
275 /* checksum */
276 void repodata_set_bin_checksum(Repodata *data, Id solvid, Id keyname, Id type,
277 			       const unsigned char *buf);
278 void repodata_set_checksum(Repodata *data, Id solvid, Id keyname, Id type,
279 			   const char *str);
280 void repodata_set_idarray(Repodata *data, Id solvid, Id keyname, Queue *q);
281 
282 /* directory (for package file list) */
283 void repodata_add_dirnumnum(Repodata *data, Id solvid, Id keyname, Id dir, Id num, Id num2);
284 void repodata_add_dirstr(Repodata *data, Id solvid, Id keyname, Id dir, const char *str);
285 void repodata_free_dircache(Repodata *data);
286 
287 
288 /* arrays */
289 void repodata_add_idarray(Repodata *data, Id solvid, Id keyname, Id id);
290 void repodata_add_poolstr_array(Repodata *data, Id solvid, Id keyname, const char *str);
291 void repodata_add_fixarray(Repodata *data, Id solvid, Id keyname, Id ghandle);
292 void repodata_add_flexarray(Repodata *data, Id solvid, Id keyname, Id ghandle);
293 
294 /* generic */
295 void repodata_set_kv(Repodata *data, Id solvid, Id keyname, Id keytype, struct s_KeyValue *kv);
296 void repodata_unset(Repodata *data, Id solvid, Id keyname);
297 void repodata_unset_uninternalized(Repodata *data, Id solvid, Id keyname);
298 
299 /*
300  merge/swap attributes from one solvable to another
301  works only if the data is not yet internalized
302 */
303 void repodata_merge_attrs(Repodata *data, Id dest, Id src);
304 void repodata_merge_some_attrs(Repodata *data, Id dest, Id src, Map *keyidmap, int overwrite);
305 void repodata_swap_attrs(Repodata *data, Id dest, Id src);
306 
307 Repodata *repodata_create_stubs(Repodata *data);
308 
309 /*
310  * load all paged data, used to speed up copying in repo_rpmdb
311  */
312 void repodata_disable_paging(Repodata *data);
313 
314 /* helper functions */
315 Id repodata_globalize_id(Repodata *data, Id id, int create);
316 Id repodata_localize_id(Repodata *data, Id id, int create);
317 Id repodata_translate_id(Repodata *data, Repodata *fromdata, Id id, int create);
318 Id repodata_translate_dir_slow(Repodata *data, Repodata *fromdata, Id dir, int create, Id *cache);
319 
320 Id repodata_str2dir(Repodata *data, const char *dir, int create);
321 const char *repodata_dir2str(Repodata *data, Id did, const char *suf);
322 const char *repodata_chk2str(Repodata *data, Id type, const unsigned char *buf);
323 void repodata_set_location(Repodata *data, Id solvid, int medianr, const char *dir, const char *file);
324 void repodata_set_deltalocation(Repodata *data, Id handle, int medianr, const char *dir, const char *file);
325 void repodata_set_sourcepkg(Repodata *data, Id solvid, const char *sourcepkg);
326 
327 /* uninternalized data lookup / search */
328 Repokey *repodata_lookup_kv_uninternalized(Repodata *data, Id solvid, Id keyname, struct s_KeyValue *kv);
329 void repodata_search_uninternalized(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, struct s_KeyValue *kv), void *cbdata);
330 
331 /* stats */
332 unsigned int repodata_memused(Repodata *data);
333 
334 static inline Id
repodata_translate_dir(Repodata * data,Repodata * fromdata,Id dir,int create,Id * cache)335 repodata_translate_dir(Repodata *data, Repodata *fromdata, Id dir, int create, Id *cache)
336 {
337   if (cache && dir && cache[(dir & 255) * 2] == dir)
338     return cache[(dir & 255) * 2 + 1];
339   return repodata_translate_dir_slow(data, fromdata, dir, create, cache);
340 }
341 
342 static inline Id *
repodata_create_dirtranscache(Repodata * data)343 repodata_create_dirtranscache(Repodata *data)
344 {
345   return (Id *)solv_calloc(256, sizeof(Id) * 2);
346 }
347 
348 static inline Id *
repodata_free_dirtranscache(Id * cache)349 repodata_free_dirtranscache(Id *cache)
350 {
351   return (Id *)solv_free(cache);
352 }
353 
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 
359 #endif /* LIBSOLV_REPODATA_H */
360