- fix problem_to_solutions segfault
[platform/upstream/libsolv.git] / src / solver.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  * solver.h
10  *
11  */
12
13 #ifndef SATSOLVER_SOLVER_H
14 #define SATSOLVER_SOLVER_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include "pooltypes.h"
21 #include "pool.h"
22 #include "repo.h"
23 #include "queue.h"
24 #include "bitmap.h"
25 /*
26  * Callback definitions in order to "overwrite" the policies by an external application.
27  */
28  
29 typedef void  (*BestSolvableCb) (Pool *pool, Queue *canditates);
30 typedef int  (*ArchCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
31 typedef int  (*VendorCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
32 typedef void (*UpdateCandidateCb) (Pool *pool, Solvable *solvable, Queue *canditates);
33
34
35 /* ----------------------------------------------
36  * Rule
37  *
38  *   providerN(B) == Package Id of package providing tag B
39  *   N = 1, 2, 3, in case of multiple providers
40  *
41  * A requires B : !A | provider1(B) | provider2(B)
42  *
43  * A conflicts B : (!A | !provider1(B)) & (!A | !provider2(B)) ...
44  *
45  * 'not' is encoded as a negative Id
46  * 
47  * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
48  */
49
50 typedef struct rule {
51   Id p;                 /* first literal in rule */
52   Id d;                 /* Id offset into 'list of providers terminated by 0' as used by whatprovides; pool->whatprovides + d */
53                         /* in case of binary rules, d == 0, w1 == p, w2 == other literal */
54                         /* in case of disabled rules: ~d, aka -d - 1 */
55   Id w1, w2;            /* watches, literals not-yet-decided */
56                                        /* if !w2, assertion, not rule */
57   Id n1, n2;            /* next rules in linked list, corresponding to w1,w2 */
58 } Rule;
59
60 struct solver;
61
62 typedef struct solver {
63   Pool *pool;
64   Repo *installed;
65   
66   /* list of rules, ordered
67    * rpm rules first, then features, updates, jobs, learnt
68    * see start/end offsets below
69    */
70   Rule *rules;                          /* all rules */
71   Id nrules;                            /* [Offset] index of the last rule */
72
73   Queue ruleassertions;                 /* Queue of all assertion rules */
74
75   /* start/end offset for rule 'areas' */
76     
77   Id rpmrules_end;                      /* [Offset] rpm rules end */
78     
79   Id featurerules;                      /* feature rules start/end */
80   Id featurerules_end;
81     
82   Id updaterules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
83   Id updaterules_end;
84     
85   Id jobrules;                          /* user rules */
86   Id jobrules_end;
87     
88   Id learntrules;                       /* learnt rules, (end == nrules) */
89
90   Map noupdate;                         /* don't try to update these
91                                            installed solvables */
92   Map noobsoletes;                      /* ignore obsoletes for these
93                                            (multiinstall) */
94
95   Queue weakruleq;                      /* index into 'rules' for weak ones */
96   Map weakrulemap;                      /* map rule# to '1' for weak rules, 1..learntrules */
97
98   Id *watches;                          /* Array of rule offsets
99                                          * watches has nsolvables*2 entries and is addressed from the middle
100                                          * middle-solvable : decision to conflict, offset point to linked-list of rules
101                                          * middle+solvable : decision to install: offset point to linked-list of rules
102                                          */
103
104   Queue ruletojob;                      /* index into job queue: jobs for which a rule exits */
105
106   /* our decisions: */
107   Queue decisionq;                      /* >0:install, <0:remove/conflict */
108   Queue decisionq_why;                  /* index of rule, Offset into rules */
109   int directdecisions;                  /* number of decisions with no rule */
110
111   Id *decisionmap;                      /* map for all available solvables,
112                                          * = 0: undecided
113                                          * > 0: level of decision when installed,
114                                          * < 0: level of decision when conflict */
115
116   /* learnt rule history */
117   Queue learnt_why;
118   Queue learnt_pool;
119
120   Queue branches;
121   int (*solution_callback)(struct solver *solv, void *data);
122   void *solution_callback_data;
123
124   int propagate_index;                  /* index into decisionq for non-propagated decisions */
125
126   Queue problems;                       /* index of conflicting rules, < 0 for job rules */
127   Queue recommendations;                /* recommended packages */
128   Queue suggestions;                    /* suggested packages */
129   Queue orphaned;                       /* orphaned packages */
130
131   int stats_learned;                    /* statistic */
132   int stats_unsolvable;                 /* statistic */
133
134   Map recommendsmap;                    /* recommended packages from decisionmap */
135   Map suggestsmap;                      /* suggested packages from decisionmap */
136   int recommends_index;                 /* recommendsmap/suggestsmap is created up to this level */
137
138   Id *obsoletes;                        /* obsoletes for each installed solvable */
139   Id *obsoletes_data;                   /* data area for obsoletes */
140   Id *multiversionupdaters;             /* updaters for multiversion packages in updatesystem mode */
141
142   /*-------------------------------------------------------------------------------------------------------------
143    * Solver configuration
144    *-------------------------------------------------------------------------------------------------------------*/
145
146   int fixsystem;                        /* repair errors in rpm dependency graph */
147   int allowdowngrade;                   /* allow to downgrade installed solvable */
148   int allowarchchange;                  /* allow to change architecture of installed solvables */
149   int allowvendorchange;                /* allow to change vendor of installed solvables */
150   int allowuninstall;                   /* allow removal of installed solvables */
151   int updatesystem;                     /* distupgrade */
152   int allowvirtualconflicts;            /* false: conflicts on package name, true: conflicts on package provides */
153   int allowselfconflicts;               /* true: packages wich conflict with itself are installable */
154   int obsoleteusesprovides;             /* true: obsoletes are matched against provides, not names */
155   int implicitobsoleteusesprovides;     /* true: implicit obsoletes due to same name are matched against provides, not names */
156   int noupdateprovide;                  /* true: update packages needs not to provide old package */
157   int dosplitprovides;                  /* true: consider legacy split provides */
158   int dontinstallrecommended;           /* true: do not install recommended packages */
159   int ignorealreadyrecommended;         /* true: ignore recommended packages that were already recommended by the installed packages */
160   int dontshowinstalledrecommended;     /* true: do not show recommended packages that are already installed */
161   
162   /* distupgrade also needs updatesystem and dosplitprovides */
163   int distupgrade;
164   int distupgrade_removeunsupported;
165
166   /* Callbacks for defining the bahaviour of the SAT solver */
167
168   /* Finding best candidate
169    *
170    * Callback definition:
171    * void  bestSolvable (Pool *pool, Queue *canditates)
172    *     candidates       : List of canditates which has to be sorted by the function call
173    *     return candidates: Sorted list of the candidates(first is the best).
174    */
175    BestSolvableCb bestSolvableCb;
176
177   /* Checking if two solvables has compatible architectures
178    *
179    * Callback definition:
180    *     int  archCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
181    *     
182    *     return 0 it the two solvables has compatible architectures
183    */
184    ArchCheckCb archCheckCb;
185
186   /* Checking if two solvables has compatible vendors
187    *
188    * Callback definition:
189    *     int  vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
190    *     
191    *     return 0 it the two solvables has compatible architectures
192    */
193    VendorCheckCb vendorCheckCb;
194     
195   /* Evaluate update candidate
196    *
197    * Callback definition:
198    * void pdateCandidateCb (Pool *pool, Solvable *solvable, Queue *canditates)
199    *     solvable   : for which updates should be search
200    *     candidates : List of candidates (This list depends on other
201    *                  restrictions like architecture and vendor policies too)
202    */
203    UpdateCandidateCb   updateCandidateCb;
204     
205
206   /* some strange queue that doesn't belong here */
207   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
208
209   
210   Queue *job;                           /* tmp store for job we're working on */
211   Id infarchrules;                      /* inferior arch rules */
212   Id infarchrules_end;
213   Id duprules;                          /* dist upgrade rules */
214   Id duprules_end;
215   Map updatemap;                        /* bring those packages to the newest version */
216   Map dupmap;                           /* packages from dup repos */
217   Map dupinvolvedmap;                   /* packages involved in dup process */
218 } Solver;
219
220 /*
221  * queue commands
222  */
223
224 #define SOLVER_SOLVABLE                 0x01
225 #define SOLVER_SOLVABLE_NAME            0x02
226 #define SOLVER_SOLVABLE_PROVIDES        0x03
227 #define SOLVER_SOLVABLE_ONE_OF          0x04
228
229 #define SOLVER_SELECTMASK               0xff
230
231 #define SOLVER_NOOP                     0x0000
232 #define SOLVER_INSTALL                  0x0100
233 #define SOLVER_ERASE                    0x0200
234 #define SOLVER_UPDATE                   0x0300
235 #define SOLVER_WEAKENDEPS               0x0400
236 #define SOLVER_NOOBSOLETES              0x0500
237 #define SOLVER_LOCK                     0x0600
238 #define SOLVER_DISTUPGRADE              0x0700
239
240 #define SOLVER_JOBMASK                  0xff00
241
242 #define SOLVER_WEAK                     0x010000
243 #define SOLVER_ESSENTIAL                0x020000
244
245 /* old API compatibility, do not use in new code */
246 #if 1
247 #define SOLVER_INSTALL_SOLVABLE (SOLVER_INSTALL|SOLVER_SOLVABLE)
248 #define SOLVER_ERASE_SOLVABLE (SOLVER_ERASE|SOLVER_SOLVABLE)
249 #define SOLVER_INSTALL_SOLVABLE_NAME (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME)
250 #define SOLVER_ERASE_SOLVABLE_NAME (SOLVER_ERASE|SOLVER_SOLVABLE_NAME)
251 #define SOLVER_INSTALL_SOLVABLE_PROVIDES (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES)
252 #define SOLVER_ERASE_SOLVABLE_PROVIDES (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES)
253 #define SOLVER_INSTALL_SOLVABLE_UPDATE (SOLVER_UPDATE|SOLVER_SOLVABLE)
254 #define SOLVER_INSTALL_SOLVABLE_ONE_OF (SOLVER_INSTALL|SOLVER_SOLVABLE_ONE_OF)
255 #define SOLVER_WEAKEN_SOLVABLE_DEPS (SOLVER_WEAKENDEPS|SOLVER_SOLVABLE)
256 #define SOLVER_NOOBSOLETES_SOLVABLE (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE)
257 #define SOLVER_NOOBSOLETES_SOLVABLE_NAME (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME)
258 #define SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_PROVIDES)
259 #endif
260
261 typedef enum {
262   SOLVER_PROBLEM_UPDATE_RULE,
263   SOLVER_PROBLEM_JOB_RULE,
264   SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP,
265   SOLVER_PROBLEM_NOT_INSTALLABLE,
266   SOLVER_PROBLEM_NOTHING_PROVIDES_DEP,
267   SOLVER_PROBLEM_SAME_NAME,
268   SOLVER_PROBLEM_PACKAGE_CONFLICT,
269   SOLVER_PROBLEM_PACKAGE_OBSOLETES,
270   SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE,
271   SOLVER_PROBLEM_SELF_CONFLICT,
272   SOLVER_PROBLEM_RPM_RULE,
273   SOLVER_PROBLEM_DISTUPGRADE_RULE,
274   SOLVER_PROBLEM_INFARCH_RULE
275 } SolverProbleminfo;
276
277 #define SOLVER_SOLUTION_JOB             (0)
278 #define SOLVER_SOLUTION_DISTUPGRADE     (-1)
279 #define SOLVER_SOLUTION_INFARCH         (-2)
280
281 extern Solver *solver_create(Pool *pool);
282 extern void solver_free(Solver *solv);
283 extern void solver_solve(Solver *solv, Queue *job);
284 extern int solver_dep_installed(Solver *solv, Id dep);
285 extern int solver_splitprovides(Solver *solv, Id dep);
286
287 extern Id solver_next_problem(Solver *solv, Id problem);
288 extern Id solver_next_solution(Solver *solv, Id problem, Id solution);
289 extern Id solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp);
290 extern Id solver_findproblemrule(Solver *solv, Id problem);
291 extern void solver_findallproblemrules(Solver *solv, Id problem, Queue *rules);
292
293 extern SolverProbleminfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
294
295 /* XXX: why is this not static? */
296 Id *solver_create_decisions_obsoletesmap(Solver *solv);
297
298 static inline int
299 solver_dep_fulfilled(Solver *solv, Id dep)
300 {
301   Pool *pool = solv->pool;
302   Id p, pp;
303
304   if (ISRELDEP(dep))
305     {
306       Reldep *rd = GETRELDEP(pool, dep);
307       if (rd->flags == REL_AND)
308         {
309           if (!solver_dep_fulfilled(solv, rd->name))
310             return 0;
311           return solver_dep_fulfilled(solv, rd->evr);
312         }
313       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
314         return solver_splitprovides(solv, rd->evr);
315       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
316         return solver_dep_installed(solv, rd->evr);
317     }
318   FOR_PROVIDES(p, pp, dep)
319     {
320       if (solv->decisionmap[p] > 0)
321         return 1;
322     }
323   return 0;
324 }
325
326 static inline int
327 solver_is_supplementing(Solver *solv, Solvable *s)
328 {
329   Id sup, *supp;
330   if (!s->supplements)
331     return 0;
332   supp = s->repo->idarraydata + s->supplements;
333   while ((sup = *supp++) != 0)
334     if (solver_dep_fulfilled(solv, sup))
335       return 1;
336   return 0;
337 }
338
339 static inline int
340 solver_is_enhancing(Solver *solv, Solvable *s)
341 {
342   Id enh, *enhp;
343   if (!s->enhances)
344     return 0;
345   enhp = s->repo->idarraydata + s->enhances;
346   while ((enh = *enhp++) != 0)
347     if (solver_dep_fulfilled(solv, enh))
348       return 1;
349   return 0;
350 }
351
352 void solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps);
353 int solver_calc_installsizechange(Solver *solv);
354 void solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res);
355
356 void solver_find_involved(Solver *solv, Queue *installedq, Solvable *s, Queue *q);
357
358 static inline void
359 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
360 {
361   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
362 }
363
364 /* iterate over all literals of a rule */
365 /* WARNING: loop body must not relocate whatprovidesdata, e.g. by
366  * looking up the providers of a dependency */
367 #define FOR_RULELITERALS(l, dp, r)                              \
368     for (l = r->d < 0 ? -r->d - 1 : r->d,                       \
369          dp = !l ? &r->w2 : pool->whatprovidesdata + l,         \
370          l = r->p; l; l = (dp != &r->w2 + 1 ? *dp++ : 0))
371
372 /* iterate over all packages selected by a job */
373 #define FOR_JOB_SELECT(p, pp, select, what) \
374     for (pp = (select == SOLVER_SOLVABLE ? 0 :  \
375                select == SOLVER_SOLVABLE_ONE_OF ? what : \
376                pool_whatprovides(pool, what)),                          \
377          p = (select == SOLVER_SOLVABLE ? what : pool->whatprovidesdata[pp++]) ; p ; p = pool->whatprovidesdata[pp++]) \
378       if (select != SOLVER_SOLVABLE_NAME || pool_match_nevr(pool, pool->solvables + p, what))
379
380 #ifdef __cplusplus
381 }
382 #endif
383
384 #endif /* SATSOLVER_SOLVER_H */