Merge branch 'master' of git@git.opensuse.org:projects/zypp/sat-solver
[platform/upstream/libsolv.git] / src / pool.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  * pool.h
10  * 
11  */
12
13 #ifndef SATSOLVER_POOL_H
14 #define SATSOLVER_POOL_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdio.h>
21
22 #include "pooltypes.h"
23 #include "poolid.h"
24 #include "solvable.h"
25 #include "bitmap.h"
26 #include "queue.h"
27 #include "strpool.h"
28
29 /* well known ids */
30 #include "knownid.h"
31
32 /* well known solvable */
33 #define SYSTEMSOLVABLE          1
34
35
36 /* how many strings to maintain (round robin) */
37 #define POOL_TMPSPACEBUF 16
38
39 //-----------------------------------------------
40
41 struct _Repo;
42 struct _Repodata;
43 struct _Repokey;
44 struct _KeyValue;
45
46 typedef struct _Repopos {
47   struct _Repo *repo;
48   Id solvid;
49   Id repodataid;
50   Id schema;
51   Id dp; 
52 } Repopos;
53
54 struct _Pool {
55   struct _Stringpool ss;
56
57   Reldep *rels;                 /* table of rels: Id -> Reldep */
58   int nrels;                    /* number of unique rels */
59   Hashtable relhashtbl;         /* hashtable: (name,evr,op)Hash -> Id */
60   Hashmask relhashmask;
61
62   struct _Repo **repos;
63   int nrepos;
64
65   struct _Repo *installed;      /* packages considered installed */
66
67   Solvable *solvables;
68   int nsolvables;
69
70   const char **languages;
71   int nlanguages;
72   Id *languagecache;
73   int languagecacheother;
74
75   /* flags to tell the library how the installed rpm works */
76   int promoteepoch;             /* true: missing epoch is replaced by epoch of dependency   */
77   int obsoleteusesprovides;     /* true: obsoletes are matched against provides, not names */
78   int implicitobsoleteusesprovides;     /* true: implicit obsoletes due to same name are matched against provides, not names */
79   int novirtualconflicts;       /* true: conflicts on names, not on provides */
80   int allowselfconflicts;       /* true: packages which conflict with itself are installable */
81
82   Id *id2arch;                  /* map arch ids to scores */
83   Id lastarch;                  /* last valid entry in id2arch */
84   Queue vendormap;              /* map vendor to vendorclasses mask */
85
86   /* providers data, as two-step indirect list
87    * whatprovides[Id] -> Offset into whatprovidesdata for name
88    * whatprovidesdata[Offset] -> ID_NULL-terminated list of solvables providing Id
89    */
90   Offset *whatprovides;         /* Offset to providers of a specific name, Id -> Offset  */
91   Offset *whatprovides_rel;     /* Offset to providers of a specific relation, Id -> Offset  */
92
93   Id *whatprovidesdata;         /* Ids of solvable providing Id */
94   Offset whatprovidesdataoff;   /* next free slot within whatprovidesdata */
95   int whatprovidesdataleft;     /* number of 'free slots' within whatprovidesdata */
96
97   /* If nonzero, then consider only the solvables with Ids set in this
98      bitmap for solving.  If zero, consider all solvables.  */
99   Map *considered;
100
101   Id (*nscallback)(struct _Pool *, void *data, Id name, Id evr);
102   void *nscallbackdata;
103
104   /* our tmp space string space */
105   char *tmpspacebuf[POOL_TMPSPACEBUF];
106   int   tmpspacelen[POOL_TMPSPACEBUF];
107   int   tmpspacen;
108
109   /* debug mask and callback */
110   int  debugmask;
111   void (*debugcallback)(const struct _Pool *, void *data, int type, const char *str);
112   void *debugcallbackdata;
113
114   /* load callback */
115   FILE * (*loadcallback)(struct _Pool *, struct _Repodata *, void *);
116   void *loadcallbackdata;
117
118   /* search position */
119   Repopos pos;
120 };
121
122 #define SAT_FATAL                       (1<<0)
123 #define SAT_ERROR                       (1<<1)
124 #define SAT_WARN                        (1<<2)
125 #define SAT_DEBUG_STATS                 (1<<3)
126 #define SAT_DEBUG_RULE_CREATION         (1<<4)
127 #define SAT_DEBUG_PROPAGATE             (1<<5)
128 #define SAT_DEBUG_ANALYZE               (1<<6)
129 #define SAT_DEBUG_UNSOLVABLE            (1<<7)
130 #define SAT_DEBUG_SOLUTIONS             (1<<8)
131 #define SAT_DEBUG_POLICY                (1<<9)
132 #define SAT_DEBUG_RESULT                (1<<10)
133 #define SAT_DEBUG_JOB                   (1<<11)
134 #define SAT_DEBUG_SCHUBI                (1<<12)
135 #define SAT_DEBUG_SOLVER                (1<<13)
136 #define SAT_DEBUG_TRANSACTION           (1<<14)
137
138 #define SAT_DEBUG_TO_STDERR             (1<<30)
139
140 //-----------------------------------------------
141
142
143 /* mark dependencies with relation by setting bit31 */
144
145 #define MAKERELDEP(id) ((id) | 0x80000000)
146 #define ISRELDEP(id) (((id) & 0x80000000) != 0)
147 #define GETRELID(id) ((id) ^ 0x80000000)                                /* returns Id */
148 #define GETRELDEP(pool, id) ((pool)->rels + ((id) ^ 0x80000000))        /* returns Reldep* */
149
150 #define REL_GT          1
151 #define REL_EQ          2
152 #define REL_LT          4
153
154 #define REL_AND         16
155 #define REL_OR          17
156 #define REL_WITH        18
157 #define REL_NAMESPACE   19
158 #define REL_ARCH        20
159 #define REL_FILECONFLICT        21
160
161 #if !defined(__GNUC__) && !defined(__attribute__)
162 # define __attribute__(x)
163 #endif
164
165 /**
166  * Creates a new pool
167  */
168 extern Pool *pool_create(void);
169 /**
170  * Delete a pool
171  */
172 extern void pool_free(Pool *pool);
173
174 extern void pool_debug(const Pool *pool, int type, const char *format, ...) __attribute__((format(printf, 3, 4)));
175
176 extern char *pool_alloctmpspace(Pool *pool, int len);
177
178 extern void pool_set_installed(Pool *pool, struct _Repo *repo);
179
180 /**
181  * Solvable management
182  */
183 extern Id pool_add_solvable(Pool *pool);
184 extern Id pool_add_solvable_block(Pool *pool, int count);
185
186 extern void pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids);
187 static inline Solvable *pool_id2solvable(const Pool *pool, Id p)
188 {
189   return pool->solvables + p;
190 }
191
192 extern const char *solvable2str(Pool *pool, Solvable *s);
193 static inline const char *solvid2str(Pool *pool, Id p)
194 {
195   return solvable2str(pool, pool->solvables + p);
196 }
197
198 void pool_set_languages(Pool *pool, const char **languages, int nlanguages);
199 Id pool_id2langid(Pool *pool, Id id, const char *lang, int create);
200
201 Id solvable_lookup_id(Solvable *s, Id keyname);
202 unsigned int solvable_lookup_num(Solvable *s, Id keyname, unsigned int notfound);
203 const char *solvable_lookup_str(Solvable *s, Id keyname);
204 const char *solvable_lookup_str_poollang(Solvable *s, Id keyname);
205 const char *solvable_lookup_str_lang(Solvable *s, Id keyname, const char *lang);
206 int solvable_lookup_bool(Solvable *s, Id keyname);
207 int solvable_lookup_void(Solvable *s, Id keyname);
208 char * solvable_get_location(Solvable *s, unsigned int *medianrp);
209 const unsigned char *solvable_lookup_bin_checksum(Solvable *s, Id keyname, Id *typep);
210 const char *solvable_lookup_checksum(Solvable *s, Id keyname, Id *typep);
211 int solvable_identical(Solvable *s1, Solvable *s2);
212 Id solvable_selfprovidedep(Solvable *s);
213
214 int solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap);
215 int solvable_trivial_installable_repo(Solvable *s, struct _Repo *installed);
216 int solvable_trivial_installable_queue(Solvable *s, Queue *installed);
217
218 void pool_create_state_maps(Pool *pool, Queue *installed, Map *installedmap, Map *conflictsmap);
219
220 int pool_match_nevr_rel(Pool *pool, Solvable *s, Id d);
221 int pool_match_dep(Pool *pool, Id d1, Id d2);
222
223 static inline int pool_match_nevr(Pool *pool, Solvable *s, Id d)
224 {
225   if (!ISRELDEP(d))
226     return d == s->name;
227   else
228     return pool_match_nevr_rel(pool, s, d);
229 }
230
231
232 /**
233  * Prepares a pool for solving
234  */
235 extern void pool_createwhatprovides(Pool *pool);
236 extern void pool_addfileprovides(Pool *pool);
237 extern void pool_addfileprovides_ids(Pool *pool, struct _Repo *installed, Id **idp);
238 extern void pool_freewhatprovides(Pool *pool);
239 extern Id pool_queuetowhatprovides(Pool *pool, Queue *q);
240
241 static inline int pool_installable(const Pool *pool, Solvable *s)
242 {
243   if (!s->arch || s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
244     return 0;
245   if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
246     return 0;
247   if (pool->considered)
248     { 
249       Id id = s - pool->solvables;
250       if (!MAPTST(pool->considered, id))
251         return 0;
252     }
253   return 1;
254 }
255
256 extern Id pool_addrelproviders(Pool *pool, Id d);
257
258 static inline Id pool_whatprovides(Pool *pool, Id d)
259 {
260   Id v;
261   if (!ISRELDEP(d))
262     return pool->whatprovides[d];
263   v = GETRELID(d);
264   if (pool->whatprovides_rel[v])
265     return pool->whatprovides_rel[v];
266   return pool_addrelproviders(pool, d);
267 }
268
269 static inline Id *pool_whatprovides_ptr(Pool *pool, Id d)
270 {
271   Id off = pool_whatprovides(pool, d);
272   return pool->whatprovidesdata + off;
273 }
274
275 extern void pool_setdebuglevel(Pool *pool, int level);
276
277 static inline void pool_setdebugcallback(Pool *pool, void (*debugcallback)(const struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
278 {
279   pool->debugcallback = debugcallback;
280   pool->debugcallbackdata = debugcallbackdata;
281 }
282
283 static inline void pool_setdebugmask(Pool *pool, int mask)
284 {
285   pool->debugmask = mask;
286 }
287
288 static inline void pool_setloadcallback(Pool *pool, FILE *(*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
289 {
290   pool->loadcallback = cb;
291   pool->loadcallbackdata = loadcbdata;
292 }
293
294 /* search the pool. the following filters are available:
295  *   p     - search just this solvable
296  *   key   - search only this key
297  *   match - key must match this string
298  */
299 void pool_search(Pool *pool, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, struct _Repodata *data, struct _Repokey *key, struct _KeyValue *kv), void *cbdata);
300
301 void pool_clear_pos(Pool *pool);
302
303
304 typedef struct _duchanges {
305   const char *path;
306   int kbytes;
307   int files;
308 } DUChanges;
309
310 void pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps);
311 int pool_calc_installsizechange(Pool *pool, Map *installedmap);
312 void pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res);
313 void pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap);
314
315 const char *pool_lookup_str(Pool *pool, Id entry, Id keyname);
316 Id pool_lookup_id(Pool *pool, Id entry, Id keyname);
317 unsigned int pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound);
318 int pool_lookup_void(Pool *pool, Id entry, Id keyname);
319 const unsigned char *pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep);
320 const char *pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep);
321
322 void pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts);
323
324
325
326 /* loop over all providers of d */
327 #define FOR_PROVIDES(v, vp, d)                                          \
328   for (vp = pool_whatprovides(pool, d) ; (v = pool->whatprovidesdata[vp++]) != 0; )
329
330 /* loop over all repositories */
331 /* note that idx is not the repoid */
332 #define FOR_REPOS(idx, r)                                               \
333   for (idx = 0; idx < pool->nrepos; idx++)                              \
334     if ((r = pool->repos[idx]) != 0)
335     
336
337 #define POOL_DEBUG(type, ...) do {if ((pool->debugmask & (type)) != 0) pool_debug(pool, (type), __VA_ARGS__);} while (0)
338 #define IF_POOLDEBUG(type) if ((pool->debugmask & (type)) != 0)
339
340 #ifdef __cplusplus
341 }
342 #endif
343
344
345 #endif /* SATSOLVER_POOL_H */