fix dataiterator returning random data in some cases
[platform/upstream/libsolv.git] / src / repodata.h
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 #define SIZEOF_MD5      16
27 #define SIZEOF_SHA1     20
28 #define SIZEOF_SHA256   32
29
30 struct _Repo;
31 struct _KeyValue;
32
33 typedef struct _Repokey {
34   Id name;
35   Id type;                      /* REPOKEY_TYPE_xxx */
36   unsigned int size;
37   unsigned int storage;         /* KEY_STORAGE_xxx */
38 } Repokey;
39
40 #define KEY_STORAGE_DROPPED             0
41 #define KEY_STORAGE_SOLVABLE            1
42 #define KEY_STORAGE_INCORE              2
43 #define KEY_STORAGE_VERTICAL_OFFSET     3
44
45 #ifdef LIBSOLV_INTERNAL
46 struct dircache;
47 #endif
48
49 typedef struct _Repodata {
50   Id repodataid;                /* our id */
51   struct _Repo *repo;           /* back pointer to repo */
52
53 #define REPODATA_AVAILABLE      0
54 #define REPODATA_STUB           1
55 #define REPODATA_ERROR          2
56 #define REPODATA_STORE          3
57 #define REPODATA_LOADING        4
58
59   int state;                    /* available, stub or error */
60
61   void (*loadcallback)(struct _Repodata *);
62
63   int start;                    /* start of solvables this repodata is valid for */
64   int end;                      /* last solvable + 1 of this repodata */
65
66   Repokey *keys;                /* keys, first entry is always zero */
67   int nkeys;                    /* length of keys array */
68   unsigned char keybits[32];    /* keyname hash */
69
70   Id *schemata;                 /* schema -> offset into schemadata */
71   int nschemata;                /* number of schemata */
72   Id *schemadata;               /* schema storage */
73
74   Stringpool spool;             /* local string pool */
75   int localpool;                /* is local string pool used */
76
77   Dirpool dirpool;              /* local dir pool */
78
79 #ifdef LIBSOLV_INTERNAL
80   FILE *fp;                     /* file pointer of solv file */
81   int error;                    /* corrupt solv file */
82
83   unsigned int schemadatalen;   /* schema storage size */
84   Id *schematahash;             /* unification helper */
85
86   unsigned char *incoredata;    /* in-core data */
87   unsigned int incoredatalen;   /* in-core data used */
88   unsigned int incoredatafree;  /* free data len */
89
90   Id mainschema;                /* SOLVID_META schema */
91   Id *mainschemaoffsets;        /* SOLVID_META offsets into incoredata */
92
93   Id *incoreoffset;             /* offset for all entries */
94
95   Id *verticaloffset;           /* offset for all verticals, nkeys elements */
96   Id lastverticaloffset;        /* end of verticals */
97
98   Repopagestore store;          /* our page store */
99   Id storestate;                /* incremented every time the store might change */
100
101   unsigned char *vincore;       /* internal vertical data */
102   unsigned int vincorelen;      /* data size */
103
104   Id **attrs;                   /* un-internalized attributes */
105   Id **xattrs;                  /* anonymous handles */
106   int nxattrs;                  /* number of handles */
107
108   unsigned char *attrdata;      /* their string data space */
109   unsigned int attrdatalen;     /* its len */
110   Id *attriddata;               /* their id space */
111   unsigned int attriddatalen;   /* its len */
112   unsigned long long *attrnum64data;    /* their 64bit num data space */
113   unsigned int attrnum64datalen;        /* its len */
114
115   /* array cache to speed up repodata_add functions*/
116   Id lasthandle;
117   Id lastkey;
118   Id lastdatalen;
119
120   /* directory cache to speed up repodata_str2dir */
121   struct dircache *dircache;
122 #endif
123
124 } Repodata;
125
126 #define SOLVID_META             -1
127 #define SOLVID_POS              -2
128 #define SOLVID_SUBSCHEMA        -3              /* internal! */
129
130
131 /*-----
132  * management functions
133  */
134 void repodata_initdata(Repodata *data, struct _Repo *repo, int localpool);
135 void repodata_freedata(Repodata *data);
136
137 void repodata_free(Repodata *data);
138 void repodata_empty(Repodata *data, int localpool);
139
140
141 /*
142  * key management functions
143  */
144 Id repodata_key2id(Repodata *data, Repokey *key, int create);
145
146 static inline Repokey *
147 repodata_id2key(Repodata *data, Id keyid)
148 {
149   return data->keys + keyid;
150 }
151
152 /*
153  * schema management functions
154  */
155 Id repodata_schema2id(Repodata *data, Id *schema, int create);
156 void repodata_free_schemahash(Repodata *data);
157
158 static inline Id *
159 repodata_id2schema(Repodata *data, Id schemaid)
160 {
161   return data->schemadata + data->schemata[schemaid];
162 }
163
164 /*
165  * data search and access
166  */
167
168 /* check if there is a chance that the repodata contains data for
169  * the specified keyname */
170 static inline int
171 repodata_precheck_keyname(Repodata *data, Id keyname)
172 {
173   unsigned char x = data->keybits[(keyname >> 3) & (sizeof(data->keybits) - 1)];
174   return x && (x & (1 << (keyname & 7))) ? 1 : 0;
175 }
176
177 /* check if the repodata contains data for the specified keyname */
178 static inline int
179 repodata_has_keyname(Repodata *data, Id keyname)
180 {
181   int i;
182   if (!repodata_precheck_keyname(data, keyname))
183     return 0;
184   for (i = 1; i < data->nkeys; i++)
185     if (data->keys[i].name == keyname)
186       return 1;
187   return 0;
188 }
189
190 /* search key <keyname> (all keys, if keyname == 0) for Id <solvid>
191  * Call <callback> for each match */
192 void repodata_search(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, struct _KeyValue *kv), void *cbdata);
193
194 /* Make sure the found KeyValue has the "str" field set. Return false
195  * if not possible */
196 int repodata_stringify(Pool *pool, Repodata *data, Repokey *key, struct _KeyValue *kv, int flags);
197
198 int repodata_filelistfilter_matches(Repodata *data, const char *str);
199
200
201 /* lookup functions */
202 Id repodata_lookup_type(Repodata *data, Id solvid, Id keyname);
203 Id repodata_lookup_id(Repodata *data, Id solvid, Id keyname);
204 const char *repodata_lookup_str(Repodata *data, Id solvid, Id keyname);
205 int repodata_lookup_num(Repodata *data, Id solvid, Id keyname, unsigned long long *value);
206 int repodata_lookup_void(Repodata *data, Id solvid, Id keyname);
207 const unsigned char *repodata_lookup_bin_checksum(Repodata *data, Id solvid, Id keyname, Id *typep);
208 int repodata_lookup_idarray(Repodata *data, Id solvid, Id keyname, Queue *q);
209
210
211 /*-----
212  * data assignment functions
213  */
214
215 /*
216  * extend the data so that it contains the specified solvables
217  * (no longer needed, as the repodata_set functions autoextend)
218  */
219 void repodata_extend(Repodata *data, Id p);
220 void repodata_extend_block(Repodata *data, Id p, int num);
221 void repodata_shrink(Repodata *data, int end);
222
223 /* internalize freshly set data, so that it is found by the search
224  * functions and written out */
225 void repodata_internalize(Repodata *data);
226
227 /* create an anonymous handle. useful for substructures like
228  * fixarray/flexarray  */
229 Id repodata_new_handle(Repodata *data);
230
231 /* basic types: void, num, string, Id */
232 void repodata_set_void(Repodata *data, Id solvid, Id keyname);
233 void repodata_set_num(Repodata *data, Id solvid, Id keyname, unsigned long long num);
234 void repodata_set_id(Repodata *data, Id solvid, Id keyname, Id id);
235 void repodata_set_str(Repodata *data, Id solvid, Id keyname, const char *str);
236 void repodata_set_binary(Repodata *data, Id solvid, Id keyname, void *buf, int len);
237 /* create id from string, then set_id */
238 void repodata_set_poolstr(Repodata *data, Id solvid, Id keyname, const char *str);
239
240 /* set numeric constant */
241 void repodata_set_constant(Repodata *data, Id solvid, Id keyname, unsigned int constant);
242
243 /* set Id constant */
244 void repodata_set_constantid(Repodata *data, Id solvid, Id keyname, Id id);
245
246 /* checksum */
247 void repodata_set_bin_checksum(Repodata *data, Id solvid, Id keyname, Id type,
248                                const unsigned char *buf);
249 void repodata_set_checksum(Repodata *data, Id solvid, Id keyname, Id type,
250                            const char *str);
251 void repodata_set_idarray(Repodata *data, Id solvid, Id keyname, Queue *q);
252
253
254 /* directory (for package file list) */
255 void repodata_add_dirnumnum(Repodata *data, Id solvid, Id keyname, Id dir, Id num, Id num2);
256 void repodata_add_dirstr(Repodata *data, Id solvid, Id keyname, Id dir, const char *str);
257 void repodata_free_dircache(Repodata *data);
258
259
260 /* Arrays */
261 void repodata_add_idarray(Repodata *data, Id solvid, Id keyname, Id id);
262 void repodata_add_poolstr_array(Repodata *data, Id solvid, Id keyname, const char *str);
263 void repodata_add_fixarray(Repodata *data, Id solvid, Id keyname, Id ghandle);
264 void repodata_add_flexarray(Repodata *data, Id solvid, Id keyname, Id ghandle);
265
266 void repodata_unset(Repodata *data, Id solvid, Id keyname);
267 void repodata_unset_uninternalized(Repodata *data, Id solvid, Id keyname);
268
269 /* 
270  merge/swap attributes from one solvable to another
271  works only if the data is not yet internalized
272 */
273 void repodata_merge_attrs(Repodata *data, Id dest, Id src);
274 void repodata_merge_some_attrs(Repodata *data, Id dest, Id src, Map *keyidmap, int overwrite);
275 void repodata_swap_attrs(Repodata *data, Id dest, Id src);
276
277 void repodata_create_stubs(Repodata *data);
278 void repodata_join(Repodata *data, Id joinkey);
279
280 /*
281  * load all paged data, used to speed up copying in repo_rpmdb
282  */
283 void repodata_disable_paging(Repodata *data);
284
285 /* helper functions */
286 Id repodata_globalize_id(Repodata *data, Id id, int create);
287 Id repodata_localize_id(Repodata *data, Id id, int create);
288 Id repodata_str2dir(Repodata *data, const char *dir, int create);
289 const char *repodata_dir2str(Repodata *data, Id did, const char *suf);
290 const char *repodata_chk2str(Repodata *data, Id type, const unsigned char *buf);
291 void repodata_set_location(Repodata *data, Id solvid, int medianr, const char *dir, const char *file);
292 void repodata_set_deltalocation(Repodata *data, Id handle, int medianr, const char *dir, const char *file);
293 void repodata_set_sourcepkg(Repodata *data, Id solvid, const char *sourcepkg);
294 Id repodata_lookup_id_uninternalized(Repodata *data, Id solvid, Id keyname, Id voidid);
295
296 /* stats */
297 unsigned int repodata_memused(Repodata *data);
298
299 #endif /* LIBSOLV_REPODATA_H */