fix a bug in the application link code
[platform/upstream/libsolv.git] / src / transaction.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * transaction.c
10  *
11  * Transaction handling
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "transaction.h"
21 #include "solver.h"
22 #include "bitmap.h"
23 #include "pool.h"
24 #include "poolarch.h"
25 #include "evr.h"
26 #include "util.h"
27
28 static int
29 obsq_sortcmp(const void *ap, const void *bp, void *dp)
30 {
31   Id a, b, oa, ob;
32   Pool *pool = dp;
33   Solvable *s, *oas, *obs;
34   int r;
35
36   a = ((Id *)ap)[0];
37   oa = ((Id *)ap)[1];
38   b = ((Id *)bp)[0];
39   ob = ((Id *)bp)[1];
40   if (a != b)
41     return a - b;
42   if (oa == ob)
43     return 0;
44   s = pool->solvables + a;
45   oas = pool->solvables + oa;
46   obs = pool->solvables + ob;
47   if (oas->name != obs->name)
48     {
49       /* bring "same name" obsoleters (i.e. upgraders) to front */
50       if (oas->name == s->name)
51         return -1;
52       if (obs->name == s->name)
53         return 1;
54       return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
55     }
56   r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
57   if (r)
58     return -r;  /* highest version first */
59   return oa - ob;
60 }
61
62 void
63 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
64 {
65   Pool *pool = trans->pool;
66   Solvable *s = pool->solvables + p;
67   Queue *ti = &trans->transaction_info;
68   Id q;
69   int i;
70
71   queue_empty(pkgs);
72   if (p <= 0 || !s->repo)
73     return;
74   if (s->repo == pool->installed)
75     {
76       q = trans->transaction_installed[p - pool->installed->start];
77       if (!q)
78         return;
79       if (q > 0)
80         {
81           /* only a single obsoleting package */
82           queue_push(pkgs, q);
83           return;
84         }
85       /* find which packages obsolete us */
86       for (i = 0; i < ti->count; i += 2)
87         if (ti->elements[i + 1] == p)
88           queue_push2(pkgs, p, ti->elements[i]);
89       /* sort obsoleters */
90       if (pkgs->count > 2)
91         solv_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
92       for (i = 0; i < pkgs->count; i += 2)
93         pkgs->elements[i / 2] = pkgs->elements[i + 1];
94       queue_truncate(pkgs, pkgs->count / 2);
95     }
96   else
97     {
98       /* find the packages we obsolete */
99       for (i = 0; i < ti->count; i += 2)
100         {
101           if (ti->elements[i] == p)
102             queue_push(pkgs, ti->elements[i + 1]);
103           else if (pkgs->count)
104             break;
105         }
106     }
107 }
108
109 Id
110 transaction_obs_pkg(Transaction *trans, Id p)
111 {
112   Pool *pool = trans->pool;
113   Solvable *s = pool->solvables + p;
114   Queue *ti;
115   int i;
116
117   if (p <= 0 || !s->repo)
118     return 0;
119   if (s->repo == pool->installed)
120     {
121       p = trans->transaction_installed[p - pool->installed->start];
122       return p < 0 ? -p : p;
123     }
124   ti = &trans->transaction_info;
125   for (i = 0; i < ti->count; i += 2)
126     if (ti->elements[i] == p)
127       return ti->elements[i + 1];
128   return 0;
129 }
130
131
132 /*
133  * calculate base type of transaction element
134  */
135
136 static Id
137 transaction_base_type(Transaction *trans, Id p)
138 {
139   Pool *pool = trans->pool;
140   Solvable *s, *s2;
141   int r;
142   Id p2;
143
144   if (!MAPTST(&trans->transactsmap, p))
145     return SOLVER_TRANSACTION_IGNORE;
146   p2 = transaction_obs_pkg(trans, p);
147   if (pool->installed && pool->solvables[p].repo == pool->installed)
148     {
149       /* erase */
150       if (!p2)
151         return SOLVER_TRANSACTION_ERASE;
152       s = pool->solvables + p;
153       s2 = pool->solvables + p2;
154       if (s->name == s2->name)
155         {
156           if (s->evr == s2->evr && solvable_identical(s, s2))
157             return SOLVER_TRANSACTION_REINSTALLED;
158           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
159           if (r < 0)
160             return SOLVER_TRANSACTION_UPGRADED;
161           else if (r > 0)
162             return SOLVER_TRANSACTION_DOWNGRADED;
163           return SOLVER_TRANSACTION_CHANGED;
164         }
165       return SOLVER_TRANSACTION_OBSOLETED;
166     }
167   else
168     {
169       /* install or multiinstall */
170       int multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
171       if (multi)
172         {
173           if (p2)
174             {
175               s = pool->solvables + p;
176               s2 = pool->solvables + p2;
177               if (s->name == s2->name && s->arch == s2->arch && s->evr == s2->evr)
178                 return SOLVER_TRANSACTION_MULTIREINSTALL;
179             }
180           return SOLVER_TRANSACTION_MULTIINSTALL;
181         }
182       if (!p2)
183         return SOLVER_TRANSACTION_INSTALL;
184       s = pool->solvables + p;
185       s2 = pool->solvables + p2;
186       if (s->name == s2->name)
187         {
188           if (s->evr == s2->evr && solvable_identical(s, s2))
189             return SOLVER_TRANSACTION_REINSTALL;
190           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
191           if (r > 0)
192             return SOLVER_TRANSACTION_UPGRADE;
193           else if (r < 0)
194             return SOLVER_TRANSACTION_DOWNGRADE;
195           else
196             return SOLVER_TRANSACTION_CHANGE;
197         }
198       return SOLVER_TRANSACTION_OBSOLETES;
199     }
200 }
201
202 /* these packages do not get installed by the package manager */
203 static inline int
204 is_pseudo_package(Pool *pool, Solvable *s)
205 {
206   const char *n = pool_id2str(pool, s->name);
207   if (*n == 'p' && !strncmp(n, "patch:", 6))
208     return 1;
209   if (*n == 'p' && !strncmp(n, "pattern:", 8))
210     return 1;
211   if (*n == 'p' && !strncmp(n, "product:", 8))
212     return 1;
213   if (*n == 'a' && !strncmp(n, "application:", 12))
214     return 1;
215   return 0;
216 }
217
218 /* these packages will never show up installed */
219 static inline int
220 is_noinst_pseudo_package(Pool *pool, Solvable *s)
221 {
222   const char *n = pool_id2str(pool, s->name);
223   if (!strncmp(n, "patch:", 6))
224     return 1;
225   if (!strncmp(n, "pattern:", 8))
226     {
227 #if defined(SUSE) && defined(ENABLE_LINKED_PKGS)
228       /* unlike normal patterns, autopatterns *can* be installed (via the package link),
229          so do not filter them */
230       if (s->provides)
231         {
232           Id prv, *prvp = s->repo->idarraydata + s->provides;
233           while ((prv = *prvp++) != 0)
234             if (ISRELDEP(prv) && !strcmp(pool_id2str(pool, prv), "autopattern()"))
235               return 0;
236         }
237 #endif
238       return 1;
239     }
240   return 0;
241 }
242
243 static int
244 obsoleted_by_pseudos_only(Transaction *trans, Id p)
245 {
246   Pool *pool = trans->pool;
247   Queue q;
248   Id op;
249   int i;
250
251   op = transaction_obs_pkg(trans, p);
252   if (op && !is_pseudo_package(pool, pool->solvables + op))
253     return 0;
254   queue_init(&q);
255   transaction_all_obs_pkgs(trans, p, &q);
256   for (i = 0; i < q.count; i++)
257     if (!is_pseudo_package(pool, pool->solvables + q.elements[i]))
258       break;
259   i = !q.count || i < q.count ? 0 : 1;
260   queue_free(&q);
261   return i;
262 }
263
264 /*
265  * return type of transaction element
266  *
267  * filtering is needed if either not all packages are shown
268  * or replaces are not shown, as otherwise parts of the
269  * transaction might not be shown to the user */
270
271 Id
272 transaction_type(Transaction *trans, Id p, int mode)
273 {
274   Pool *pool = trans->pool;
275   Solvable *s = pool->solvables + p;
276   Queue oq, rq;
277   Id type, q;
278   int i, j, ref = 0;
279
280   if (!s->repo)
281     return SOLVER_TRANSACTION_IGNORE;
282
283   /* XXX: SUSE only? */
284   if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_noinst_pseudo_package(pool, s))
285     return SOLVER_TRANSACTION_IGNORE;
286
287   type = transaction_base_type(trans, p);
288
289   if (type == SOLVER_TRANSACTION_IGNORE)
290     return SOLVER_TRANSACTION_IGNORE;   /* not part of the transaction */
291
292   if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
293     {
294       /* application wants to know what to feed to rpm */
295       if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
296         return type;
297       if (s->repo == pool->installed)
298         {
299           /* check if we're obsoleted by pseudos only */
300           if (obsoleted_by_pseudos_only(trans, s - pool->solvables))
301             return SOLVER_TRANSACTION_ERASE;
302           return SOLVER_TRANSACTION_IGNORE;     /* ignore as we're being obsoleted */
303         }
304       if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
305         return SOLVER_TRANSACTION_MULTIINSTALL;
306       return SOLVER_TRANSACTION_INSTALL;
307     }
308
309   if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
310     {
311       /* application wants to make no difference between install
312        * and multiinstall */
313       if (type == SOLVER_TRANSACTION_MULTIINSTALL)
314         type = SOLVER_TRANSACTION_INSTALL;
315       if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
316         type = SOLVER_TRANSACTION_REINSTALL;
317     }
318
319   if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
320     {
321       /* application wants to make no difference between change
322        * and reinstall */
323       if (type == SOLVER_TRANSACTION_CHANGED)
324         type = SOLVER_TRANSACTION_REINSTALLED;
325       else if (type == SOLVER_TRANSACTION_CHANGE)
326         type = SOLVER_TRANSACTION_REINSTALL;
327     }
328
329   if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
330     return type;
331
332   if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
333     {
334       /* erase element and we're showing the passive side */
335       if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
336         type = SOLVER_TRANSACTION_ERASE;
337       if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
338         type = SOLVER_TRANSACTION_UPGRADED;
339       return type;
340     }
341   if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
342     {
343       /* install element and we're showing the active side */
344       if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
345         type = SOLVER_TRANSACTION_INSTALL;
346       if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
347         type = SOLVER_TRANSACTION_UPGRADE;
348       return type;
349     }
350
351   /* the element doesn't match the show mode */
352
353   /* if we're showing all references, we can ignore this package */
354   if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
355     return SOLVER_TRANSACTION_IGNORE;
356
357   /* we're not showing all refs. check if some other package
358    * references us. If yes, it's safe to ignore this package,
359    * otherwise we need to map the type */
360
361   /* most of the time there's only one reference, so check it first */
362   q = transaction_obs_pkg(trans, p);
363
364   if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
365     {
366       Solvable *sq = pool->solvables + q;
367       if (sq->name != s->name)
368         {
369           /* it's a replace but we're not showing replaces. map type. */
370           if (s->repo == pool->installed)
371             return SOLVER_TRANSACTION_ERASE;
372           else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
373             return SOLVER_TRANSACTION_MULTIINSTALL;
374           else
375             return SOLVER_TRANSACTION_INSTALL;
376         }
377     }
378   
379   /* if there's a match, p will be shown when q
380    * is processed */
381   if (transaction_obs_pkg(trans, q) == p)
382     return SOLVER_TRANSACTION_IGNORE;
383
384   /* too bad, a miss. check em all */
385   queue_init(&oq);
386   queue_init(&rq);
387   transaction_all_obs_pkgs(trans, p, &oq);
388   for (i = 0; i < oq.count; i++)
389     {
390       q = oq.elements[i];
391       if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
392         {
393           Solvable *sq = pool->solvables + q;
394           if (sq->name != s->name)
395             continue;
396         }
397       /* check if we are referenced? */
398       if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
399         {
400           transaction_all_obs_pkgs(trans, q, &rq);
401           for (j = 0; j < rq.count; j++)
402             if (rq.elements[j] == p)
403               {
404                 ref = 1;
405                 break;
406               }
407           if (ref)
408             break;
409         }
410       else if (transaction_obs_pkg(trans, q) == p)
411         {
412           ref = 1;
413           break;
414         }
415     }
416   queue_free(&oq);
417   queue_free(&rq);
418
419   if (!ref)
420     {
421       /* we're not referenced. map type */
422       if (s->repo == pool->installed)
423         return SOLVER_TRANSACTION_ERASE;
424       else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
425         return SOLVER_TRANSACTION_MULTIINSTALL;
426       else
427         return SOLVER_TRANSACTION_INSTALL;
428     }
429   /* there was a ref, so p is shown with some other package */
430   return SOLVER_TRANSACTION_IGNORE;
431 }
432
433
434
435 static int
436 classify_cmp(const void *ap, const void *bp, void *dp)
437 {
438   Transaction *trans = dp;
439   Pool *pool = trans->pool;
440   const Id *a = ap;
441   const Id *b = bp;
442   int r;
443
444   r = a[0] - b[0];
445   if (r)
446     return r;
447   r = a[2] - b[2];
448   if (r)
449     return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
450   r = a[3] - b[3];
451   if (r)
452     return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
453   return 0;
454 }
455
456 static int
457 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
458 {
459   Transaction *trans = dp;
460   Pool *pool = trans->pool;
461   Id a = *(Id *)ap;
462   Id b = *(Id *)bp;
463   Solvable *sa, *sb;
464
465   sa = pool->solvables + a;
466   sb = pool->solvables + b;
467   if (sa->name != sb->name)
468     return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
469   if (sa->evr != sb->evr)
470     {
471       int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
472       if (r)
473         return r;
474     }
475   return a - b;
476 }
477
478 void
479 transaction_classify(Transaction *trans, int mode, Queue *classes)
480 {
481   Pool *pool = trans->pool;
482   int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
483   Solvable *s, *sq;
484   Id v, vq, type, p, q;
485   int i, j;
486
487   queue_empty(classes);
488   memset(ntypes, 0, sizeof(ntypes));
489   /* go through transaction and classify each step */
490   for (i = 0; i < trans->steps.count; i++)
491     {
492       p = trans->steps.elements[i];
493       s = pool->solvables + p;
494       type = transaction_type(trans, p, mode);
495       ntypes[type]++;
496       if (!pool->installed || s->repo != pool->installed)
497         continue;
498       /* don't report vendor/arch changes if we were mapped to erase. */
499       if (type == SOLVER_TRANSACTION_ERASE)
500         continue;
501       /* look at arch/vendor changes */
502       q = transaction_obs_pkg(trans, p);
503       if (!q)
504         continue;
505       sq = pool->solvables + q;
506
507       v = s->arch;
508       vq = sq->arch;
509       if (v != vq)
510         {
511           if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
512             v = vq = 0;
513           for (j = 0; j < classes->count; j += 4)
514             if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
515               break;
516           if (j == classes->count)
517             {
518               queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
519               queue_push(classes, 1);
520               queue_push(classes, v);
521               queue_push(classes, vq);
522             }
523           else
524             classes->elements[j + 1]++;
525         }
526
527       v = s->vendor ? s->vendor : 1;
528       vq = sq->vendor ? sq->vendor : 1;
529       if (v != vq)
530         {
531           if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
532             v = vq = 0;
533           for (j = 0; j < classes->count; j += 4)
534             if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
535               break;
536           if (j == classes->count)
537             {
538               queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
539               queue_push(classes, 1);
540               queue_push(classes, v);
541               queue_push(classes, vq);
542             }
543           else
544             classes->elements[j + 1]++;
545         }
546     }
547   /* now sort all vendor/arch changes */
548   if (classes->count > 4)
549     solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
550   /* finally add all classes. put erases last */
551   i = SOLVER_TRANSACTION_ERASE;
552   if (ntypes[i])
553     {
554       queue_unshift(classes, 0);
555       queue_unshift(classes, 0);
556       queue_unshift(classes, ntypes[i]);
557       queue_unshift(classes, i);
558     }
559   for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
560     {
561       if (!ntypes[i])
562         continue;
563       if (i == SOLVER_TRANSACTION_ERASE)
564         continue;
565       queue_unshift(classes, 0);
566       queue_unshift(classes, 0);
567       queue_unshift(classes, ntypes[i]);
568       queue_unshift(classes, i);
569     }
570 }
571
572 void
573 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
574 {
575   Pool *pool = trans->pool;
576   int i;
577   Id type, p, q;
578   Solvable *s, *sq;
579
580   queue_empty(pkgs);
581   for (i = 0; i < trans->steps.count; i++)
582     {
583       p = trans->steps.elements[i];
584       s = pool->solvables + p;
585       if (class <= SOLVER_TRANSACTION_MAXTYPE)
586         {
587           type = transaction_type(trans, p, mode);
588           if (type == class)
589             queue_push(pkgs, p);
590           continue;
591         }
592       if (!pool->installed || s->repo != pool->installed)
593         continue;
594       q = transaction_obs_pkg(trans, p);
595       if (!q)
596         continue;
597       sq = pool->solvables + q;
598       if (class == SOLVER_TRANSACTION_ARCHCHANGE)
599         {
600           if ((!from && !to) || (s->arch == from && sq->arch == to))
601             queue_push(pkgs, p);
602           continue;
603         }
604       if (class == SOLVER_TRANSACTION_VENDORCHANGE)
605         {
606           Id v = s->vendor ? s->vendor : 1;
607           Id vq = sq->vendor ? sq->vendor : 1;
608           if ((!from && !to) || (v == from && vq == to))
609             queue_push(pkgs, p);
610           continue;
611         }
612     }
613   if (pkgs->count > 1)
614     solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
615 }
616
617 static void
618 create_transaction_info(Transaction *trans, Queue *decisionq)
619 {
620   Pool *pool = trans->pool;
621   Queue *ti = &trans->transaction_info;
622   Repo *installed = pool->installed;
623   int i, j, multi;
624   Id p, p2, pp2;
625   Solvable *s, *s2;
626
627   queue_empty(ti);
628   trans->transaction_installed = solv_free(trans->transaction_installed);
629   if (!installed)
630     return;     /* no info needed */
631   for (i = 0; i < decisionq->count; i++)
632     {
633       p = decisionq->elements[i];
634       if (p <= 0 || p == SYSTEMSOLVABLE)
635         continue;
636       s = pool->solvables + p;
637       if (!s->repo || s->repo == installed)
638         continue;
639       multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
640       FOR_PROVIDES(p2, pp2, s->name)
641         {
642           if (!MAPTST(&trans->transactsmap, p2))
643             continue;
644           s2 = pool->solvables + p2;
645           if (s2->repo != installed)
646             continue;
647           if (multi && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
648             continue;
649           if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
650             continue;
651           if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
652             continue;
653           queue_push2(ti, p, p2);
654         }
655       if (s->obsoletes && !multi)
656         {
657           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
658           while ((obs = *obsp++) != 0)
659             {
660               FOR_PROVIDES(p2, pp2, obs)
661                 {
662                   if (!MAPTST(&trans->transactsmap, p2))
663                     continue;
664                   s2 = pool->solvables + p2;
665                   if (s2->repo != installed)
666                     continue;
667                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
668                     continue;
669                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
670                     continue;
671                   queue_push2(ti, p, p2);
672                 }
673             }
674         }
675     }
676   if (ti->count > 2)
677     {
678       /* sort and unify */
679       solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
680       for (i = j = 2; i < ti->count; i += 2)
681         {
682           if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
683             continue;
684           ti->elements[j++] = ti->elements[i];
685           ti->elements[j++] = ti->elements[i + 1];
686         }
687       queue_truncate(ti, j);
688     }
689
690   /* create transaction_installed helper */
691   /*   entry > 0: exactly one obsoleter, entry < 0: multiple obsoleters, -entry is "best" */
692   trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
693   for (i = 0; i < ti->count; i += 2)
694     {
695       j = ti->elements[i + 1] - installed->start;
696       if (!trans->transaction_installed[j])
697         trans->transaction_installed[j] = ti->elements[i];
698       else
699         {
700           /* more than one package obsoletes us. compare to find "best" */
701           Id q[4];
702           if (trans->transaction_installed[j] > 0)
703             trans->transaction_installed[j] = -trans->transaction_installed[j];
704           q[0] = q[2] = ti->elements[i + 1];
705           q[1] = ti->elements[i];
706           q[3] = -trans->transaction_installed[j];
707           if (obsq_sortcmp(q, q + 2, pool) < 0)
708             trans->transaction_installed[j] = -ti->elements[i];
709         }
710     }
711 }
712
713
714 Transaction *
715 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *multiversionmap)
716 {
717   Repo *installed = pool->installed;
718   int i, needmulti;
719   Id p;
720   Solvable *s;
721   Transaction *trans;
722
723   trans = transaction_create(pool);
724   if (multiversionmap && !multiversionmap->size)
725     multiversionmap = 0;        /* ignore empty map */
726   queue_empty(&trans->steps);
727   map_init(&trans->transactsmap, pool->nsolvables);
728   needmulti = 0;
729   for (i = 0; i < decisionq->count; i++)
730     {
731       p = decisionq->elements[i];
732       s = pool->solvables + (p > 0 ? p : -p);
733       if (!s->repo)
734         continue;
735       if (installed && s->repo == installed && p < 0)
736         MAPSET(&trans->transactsmap, -p);
737       if ((!installed || s->repo != installed) && p > 0)
738         {
739 #if 0
740           const char *n = pool_id2str(pool, s->name);
741           if (!strncmp(n, "patch:", 6))
742             continue;
743           if (!strncmp(n, "pattern:", 8))
744             continue;
745 #endif
746           MAPSET(&trans->transactsmap, p);
747           if (multiversionmap && MAPTST(multiversionmap, p))
748             needmulti = 1;
749         }
750     }
751   MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
752   if (needmulti)
753     map_init_clone(&trans->multiversionmap, multiversionmap);
754
755   create_transaction_info(trans, decisionq);
756
757   if (installed)
758     {
759       FOR_REPO_SOLVABLES(installed, p, s)
760         {
761           if (MAPTST(&trans->transactsmap, p))
762             queue_push(&trans->steps, p);
763         }
764     }
765   for (i = 0; i < decisionq->count; i++)
766     {
767       p = decisionq->elements[i];
768       if (p > 0 && MAPTST(&trans->transactsmap, p))
769         queue_push(&trans->steps, p);
770     }
771   return trans;
772 }
773
774 int
775 transaction_installedresult(Transaction *trans, Queue *installedq)
776 {
777   Pool *pool = trans->pool;
778   Repo *installed = pool->installed;
779   Solvable *s;
780   int i, cutoff;
781   Id p;
782
783   queue_empty(installedq);
784   /* first the new installs, than the kept packages */
785   for (i = 0; i < trans->steps.count; i++)
786     {
787       p = trans->steps.elements[i];
788       s = pool->solvables + p;
789       if (installed && s->repo == installed)
790         continue;
791       queue_push(installedq, p);
792     }
793   cutoff = installedq->count;
794   if (installed)
795     {
796       FOR_REPO_SOLVABLES(installed, p, s)
797         if (!MAPTST(&trans->transactsmap, p))
798           queue_push(installedq, p);
799     }
800   return cutoff;
801 }
802
803 static void
804 transaction_create_installedmap(Transaction *trans, Map *installedmap)
805 {
806   Pool *pool = trans->pool;
807   Repo *installed = pool->installed;
808   Solvable *s;
809   Id p;
810   int i;
811
812   map_init(installedmap, pool->nsolvables);
813   for (i = 0; i < trans->steps.count; i++)
814     {
815       p = trans->steps.elements[i];
816       s = pool->solvables + p;
817       if (!installed || s->repo != installed)
818         MAPSET(installedmap, p);
819     }
820   if (installed)
821     {
822       FOR_REPO_SOLVABLES(installed, p, s)
823         if (!MAPTST(&trans->transactsmap, p))
824           MAPSET(installedmap, p);
825     }
826 }
827
828 int
829 transaction_calc_installsizechange(Transaction *trans)
830 {
831   Map installedmap;
832   int change;
833
834   transaction_create_installedmap(trans, &installedmap);
835   change = pool_calc_installsizechange(trans->pool, &installedmap);
836   map_free(&installedmap);
837   return change;
838 }
839
840 void
841 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
842 {
843   Map installedmap;
844
845   transaction_create_installedmap(trans, &installedmap);
846   pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
847   map_free(&installedmap);
848 }
849
850 struct _TransactionElement {
851   Id p;         /* solvable id */
852   Id edges;     /* pointer into edges data */
853   Id mark;
854 };
855
856 struct _TransactionOrderdata {
857   struct _TransactionElement *tes;
858   int ntes;
859   Id *invedgedata;
860   int ninvedgedata;
861 };
862
863 #define TYPE_BROKEN     (1<<0)
864 #define TYPE_CON        (1<<1)
865
866 #define TYPE_REQ_P      (1<<2)
867 #define TYPE_PREREQ_P   (1<<3)
868
869 #define TYPE_REQ        (1<<4)
870 #define TYPE_PREREQ     (1<<5)
871
872 #define TYPE_CYCLETAIL  (1<<16)
873 #define TYPE_CYCLEHEAD  (1<<17)
874
875 #define EDGEDATA_BLOCK  127
876
877 Transaction *
878 transaction_create(Pool *pool)
879 {
880   Transaction *trans = solv_calloc(1, sizeof(*trans));
881   trans->pool = pool;
882   return trans;
883 }
884
885 Transaction *
886 transaction_create_clone(Transaction *srctrans)
887 {
888   Transaction *trans = transaction_create(srctrans->pool);
889   queue_init_clone(&trans->steps, &srctrans->steps);
890   queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
891   if (srctrans->transaction_installed)
892     {
893       Repo *installed = srctrans->pool->installed;
894       trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
895     }
896   map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
897   map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
898   if (srctrans->orderdata)
899     {
900       struct _TransactionOrderdata *od = srctrans->orderdata;
901       trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
902       trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
903       trans->orderdata->ntes = od->ntes;
904       trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
905       trans->orderdata->ninvedgedata = od->ninvedgedata;
906     }
907   return trans;
908 }
909
910 void
911 transaction_free(Transaction *trans)
912 {
913   queue_free(&trans->steps);
914   queue_free(&trans->transaction_info);
915   trans->transaction_installed = solv_free(trans->transaction_installed);
916   map_free(&trans->transactsmap);
917   map_free(&trans->multiversionmap);
918   transaction_free_orderdata(trans);
919   free(trans);
920 }
921
922 void
923 transaction_free_orderdata(Transaction *trans)
924 {
925   if (trans->orderdata)
926     {
927       struct _TransactionOrderdata *od = trans->orderdata;
928       od->tes = solv_free(od->tes);
929       od->invedgedata = solv_free(od->invedgedata);
930       trans->orderdata = solv_free(trans->orderdata);
931     }
932 }
933
934 struct orderdata {
935   Transaction *trans;
936   struct _TransactionElement *tes;
937   int ntes;
938   Id *edgedata;
939   int nedgedata;
940   Id *invedgedata;
941
942   Queue cycles;
943   Queue cyclesdata;
944   int ncycles;
945 };
946
947 static int
948 addteedge(struct orderdata *od, int from, int to, int type)
949 {
950   int i;
951   struct _TransactionElement *te;
952
953   if (from == to)
954     return 0;
955
956   /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
957
958   te = od->tes + from;
959   for (i = te->edges; od->edgedata[i]; i += 2)
960     if (od->edgedata[i] == to)
961       break;
962   /* test of brokenness */
963   if (type == TYPE_BROKEN)
964     return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
965   if (od->edgedata[i])
966     {
967       od->edgedata[i + 1] |= type;
968       return 0;
969     }
970   if (i + 1 == od->nedgedata)
971     {
972       /* printf("tail add %d\n", i - te->edges); */
973       if (!i)
974         te->edges = ++i;
975       od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
976     }
977   else
978     {
979       /* printf("extend %d\n", i - te->edges); */
980       od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
981       if (i > te->edges)
982         memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
983       i = od->nedgedata + (i - te->edges);
984       te->edges = od->nedgedata;
985     }
986   od->edgedata[i] = to;
987   od->edgedata[i + 1] = type;
988   od->edgedata[i + 2] = 0;      /* end marker */
989   od->nedgedata = i + 3;
990   return 0;
991 }
992
993 static int
994 addedge(struct orderdata *od, Id from, Id to, int type)
995 {
996   Transaction *trans = od->trans;
997   Pool *pool = trans->pool;
998   Solvable *s;
999   struct _TransactionElement *te;
1000   int i;
1001
1002   /* printf("addedge %d %d type %d\n", from, to, type); */
1003   s = pool->solvables + from;
1004   if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
1005     {
1006       /* obsolete, map to install */
1007       if (trans->transaction_installed[from - pool->installed->start] > 0)
1008         from = trans->transaction_installed[from - pool->installed->start];
1009       else
1010         {
1011           int ret = 0;
1012           Queue ti;
1013           Id tibuf[5];
1014
1015           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1016           transaction_all_obs_pkgs(trans, from, &ti);
1017           for (i = 0; i < ti.count; i++)
1018             ret |= addedge(od, ti.elements[i], to, type);
1019           queue_free(&ti);
1020           return ret;
1021         }
1022     }
1023   s = pool->solvables + to;
1024   if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
1025     {
1026       /* obsolete, map to install */
1027       if (trans->transaction_installed[to - pool->installed->start] > 0)
1028         to = trans->transaction_installed[to - pool->installed->start];
1029       else
1030         {
1031           int ret = 0;
1032           Queue ti;
1033           Id tibuf[5];
1034
1035           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1036           transaction_all_obs_pkgs(trans, to, &ti);
1037           for (i = 0; i < ti.count; i++)
1038             ret |= addedge(od, from, ti.elements[i], type);
1039           queue_free(&ti);
1040           return ret;
1041         }
1042     }
1043
1044   /* map from/to to te numbers */
1045   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1046     if (te->p == to)
1047       break;
1048   if (i == od->ntes)
1049     return 0;
1050   to = i;
1051
1052   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1053     if (te->p == from)
1054       break;
1055   if (i == od->ntes)
1056     return 0;
1057
1058   return addteedge(od, i, to, type);
1059 }
1060
1061 #if 1
1062 static int
1063 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
1064 {
1065   Transaction *trans = od->trans;
1066   Pool *pool = trans->pool;
1067   Id ti1buf[5], ti2buf[5];
1068   Queue ti1, ti2;
1069   int i, j;
1070
1071   /* both q1 and q2 are uninstalls. check if their TEs intersect */
1072   /* common case: just one TE for both packages */
1073 #if 0
1074   printf("havechoice %d %d %d\n", p, q1, q2);
1075 #endif
1076   if (trans->transaction_installed[q1 - pool->installed->start] == 0)
1077     return 1;
1078   if (trans->transaction_installed[q2 - pool->installed->start] == 0)
1079     return 1;
1080   if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
1081     return 0;
1082   if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
1083     return 1;
1084   queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
1085   transaction_all_obs_pkgs(trans, q1, &ti1);
1086   queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
1087   transaction_all_obs_pkgs(trans, q2, &ti2);
1088   for (i = 0; i < ti1.count; i++)
1089     for (j = 0; j < ti2.count; j++)
1090       if (ti1.elements[i] == ti2.elements[j])
1091         {
1092           /* found a common edge */
1093           queue_free(&ti1);
1094           queue_free(&ti2);
1095           return 0;
1096         }
1097   queue_free(&ti1);
1098   queue_free(&ti2);
1099   return 1;
1100 }
1101 #endif
1102
1103 static inline int
1104 havescripts(Pool *pool, Id solvid)
1105 {
1106   Solvable *s = pool->solvables + solvid;
1107   const char *dep;
1108   if (s->requires)
1109     {
1110       Id req, *reqp;
1111       int inpre = 0;
1112       reqp = s->repo->idarraydata + s->requires;
1113       while ((req = *reqp++) != 0)
1114         {
1115           if (req == SOLVABLE_PREREQMARKER)
1116             {
1117               inpre = 1;
1118               continue;
1119             }
1120           if (!inpre)
1121             continue;
1122           dep = pool_id2str(pool, req);
1123           if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1124             return 1;
1125         }
1126     }
1127   return 0;
1128 }
1129
1130 static void
1131 addsolvableedges(struct orderdata *od, Solvable *s)
1132 {
1133   Transaction *trans = od->trans;
1134   Pool *pool = trans->pool;
1135   Id req, *reqp, con, *conp;
1136   Id p, p2, pp2;
1137   int i, j, pre, numins;
1138   Repo *installed = pool->installed;
1139   Solvable *s2;
1140   Queue reqq;
1141   int provbyinst;
1142
1143 #if 0
1144   printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1145 #endif
1146   p = s - pool->solvables;
1147   queue_init(&reqq);
1148   if (s->requires)
1149     {
1150       reqp = s->repo->idarraydata + s->requires;
1151       pre = TYPE_REQ;
1152       while ((req = *reqp++) != 0)
1153         {
1154           if (req == SOLVABLE_PREREQMARKER)
1155             {
1156               pre = TYPE_PREREQ;
1157               continue;
1158             }
1159 #if 0
1160           if (pre != TYPE_PREREQ && installed && s->repo == installed)
1161             {
1162               /* ignore normal requires if we're getting obsoleted */
1163               if (trans->transaction_installed[p - pool->installed->start])
1164                 continue;
1165             }
1166 #endif
1167           queue_empty(&reqq);
1168           numins = 0;   /* number of packages to be installed providing it */
1169           provbyinst = 0;       /* provided by kept package */
1170           FOR_PROVIDES(p2, pp2, req)
1171             {
1172               s2 = pool->solvables + p2;
1173               if (p2 == p)
1174                 {
1175                   reqq.count = 0;       /* self provides */
1176                   break;
1177                 }
1178               if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1179                 {
1180                   provbyinst = 1;
1181 #if 0
1182                   printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1183                   reqq.count = 0;       /* provided by package that stays installed */
1184                   break;
1185 #else
1186                   continue;
1187 #endif
1188                 }
1189               if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1190                 continue;               /* package stays uninstalled */
1191               
1192               if (s->repo == installed)
1193                 {
1194                   /* s gets uninstalled */
1195                   queue_pushunique(&reqq, p2);
1196                   if (s2->repo != installed)
1197                     numins++;
1198                 }
1199               else
1200                 {
1201                   if (s2->repo == installed)
1202                     continue;   /* s2 gets uninstalled */
1203                   queue_pushunique(&reqq, p2);
1204                 }
1205             }
1206           if (provbyinst)
1207             {
1208               /* prune to harmless ->inst edges */
1209               for (i = j = 0; i < reqq.count; i++)
1210                 if (pool->solvables[reqq.elements[i]].repo != installed)
1211                   reqq.elements[j++] = reqq.elements[i];
1212               reqq.count = j;
1213             }
1214
1215           if (numins && reqq.count)
1216             {
1217               if (s->repo == installed)
1218                 {
1219                   for (i = 0; i < reqq.count; i++)
1220                     {
1221                       if (pool->solvables[reqq.elements[i]].repo == installed)
1222                         {
1223                           for (j = 0; j < reqq.count; j++)
1224                             {
1225                               if (pool->solvables[reqq.elements[j]].repo != installed)
1226                                 {
1227                                   if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1228                                     continue;   /* no self edge */
1229 #if 0
1230                                   printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, reqq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, reqq.elements[j]));
1231 #endif
1232                                   addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1233                                 }
1234                             }
1235                         }
1236                     }
1237                 }
1238               /* no mixed types, remove all deps on uninstalls */
1239               for (i = j = 0; i < reqq.count; i++)
1240                 if (pool->solvables[reqq.elements[i]].repo != installed)
1241                   reqq.elements[j++] = reqq.elements[i];
1242               reqq.count = j;
1243             }
1244           if (!reqq.count)
1245             continue;
1246           for (i = 0; i < reqq.count; i++)
1247             {
1248               int choice = 0;
1249               p2 = reqq.elements[i];
1250               if (pool->solvables[p2].repo != installed)
1251                 {
1252                   /* all elements of reqq are installs, thus have different TEs */
1253                   choice = reqq.count - 1;
1254                   if (pool->solvables[p].repo != installed)
1255                     {
1256 #if 0
1257                       printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1258 #endif
1259                       addedge(od, p, p2, pre);
1260                     }
1261                   else
1262                     {
1263 #if 0
1264                       printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1265 #endif
1266                       addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1267                     }
1268                 }
1269               else
1270                 {
1271                   if (s->repo != installed)
1272                     continue;   /* no inst->uninst edges, please! */
1273
1274                   /* uninst -> uninst edge. Those make trouble. Only add if we must */
1275                   if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1276                     {
1277                       /* p is obsoleted by another package and has no scripts */
1278                       /* we assume that the obsoletor is good enough to replace p */
1279                       continue;
1280                     }
1281 #if 1
1282                   choice = 0;
1283                   for (j = 0; j < reqq.count; j++)
1284                     {
1285                       if (i == j)
1286                         continue;
1287                       if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1288                         choice++;
1289                     }
1290 #endif
1291 #if 0
1292                   printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1293 #endif
1294                   addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1295                 }
1296             }
1297         }
1298     }
1299   if (s->conflicts)
1300     {
1301       conp = s->repo->idarraydata + s->conflicts;
1302       while ((con = *conp++) != 0)
1303         {
1304           FOR_PROVIDES(p2, pp2, con)
1305             {
1306               if (p2 == p)
1307                 continue;
1308               s2 = pool->solvables + p2;
1309               if (!s2->repo)
1310                 continue;
1311               if (s->repo == installed)
1312                 {
1313                   if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1314                     {
1315                       /* deinstall p before installing p2 */
1316 #if 0
1317                       printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1318 #endif
1319                       addedge(od, p2, p, TYPE_CON);
1320                     }
1321                 }
1322               else
1323                 {
1324                   if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1325                     {
1326                       /* deinstall p2 before installing p */
1327 #if 0
1328                       printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1329 #endif
1330                       addedge(od, p, p2, TYPE_CON);
1331                     }
1332                 }
1333
1334             }
1335         }
1336     }
1337   if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1338     {
1339       /* we're getting deinstalled/updated. Try to do this before our
1340        * triggers are hit */
1341       for (i = 0; i < reqq.count; i++)
1342         {
1343           Id tri = reqq.elements[i];
1344           FOR_PROVIDES(p2, pp2, tri)
1345             {
1346               if (p2 == p)
1347                 continue;
1348               s2 = pool->solvables + p2;
1349               if (!s2->repo)
1350                 continue;
1351               if (s2->name == s->name)
1352                 continue;       /* obsoleted anyway */
1353               if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1354                 {
1355                   /* deinstall/update p before installing p2 */
1356 #if 0
1357                   printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1358 #endif
1359                   addedge(od, p2, p, TYPE_CON);
1360                 }
1361             }
1362         }
1363     }
1364   queue_free(&reqq);
1365 }
1366
1367
1368 /* break an edge in a cycle */
1369 static void
1370 breakcycle(struct orderdata *od, Id *cycle)
1371 {
1372   Pool *pool = od->trans->pool;
1373   Id ddegmin, ddegmax, ddeg;
1374   int k, l;
1375   struct _TransactionElement *te;
1376
1377   l = 0;
1378   ddegmin = ddegmax = 0;
1379   for (k = 0; cycle[k + 1]; k += 2)
1380     {
1381       ddeg = od->edgedata[cycle[k + 1] + 1];
1382       if (ddeg > ddegmax)
1383         ddegmax = ddeg;
1384       if (!k || ddeg < ddegmin)
1385         {
1386           l = k;
1387           ddegmin = ddeg;
1388           continue;
1389         }
1390       if (ddeg == ddegmin)
1391         {
1392           if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1393             {
1394               /* prefer k, as l comes from a package with contains scriptlets */
1395               l = k;
1396               ddegmin = ddeg;
1397               continue;
1398             }
1399           /* same edge value, check for prereq */
1400         }
1401     }
1402
1403   /* record brkoen cycle starting with the tail */
1404   queue_push(&od->cycles, od->cyclesdata.count);                /* offset into data */
1405   queue_push(&od->cycles, k / 2);                               /* cycle elements */
1406   queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);      /* broken edge */
1407   od->ncycles++;
1408   for (k = l;;)
1409     {
1410       k += 2;
1411       if (!cycle[k + 1])
1412         k = 0;
1413       queue_push(&od->cyclesdata, cycle[k]);
1414       if (k == l)
1415         break;
1416     }
1417   queue_push(&od->cyclesdata, 0);       /* mark end */
1418
1419   /* break that edge */
1420   od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1421
1422 #if 1
1423   if (ddegmin < TYPE_REQ)
1424     return;
1425 #endif
1426
1427   /* cycle recorded, print it */
1428   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1429     POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1430   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1431   for (k = 0; cycle[k + 1]; k += 2)
1432     {
1433       te = od->tes +  cycle[k];
1434       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1435         POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1436       else
1437         POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1438     }
1439   POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1440 }
1441
1442 static inline void
1443 dump_tes(struct orderdata *od)
1444 {
1445   Pool *pool = od->trans->pool;
1446   int i, j;
1447   Queue obsq;
1448   struct _TransactionElement *te, *te2;
1449
1450   queue_init(&obsq);
1451   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1452     {
1453       Solvable *s = pool->solvables + te->p;
1454       POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1455       if (s->repo != pool->installed)
1456         {
1457           queue_empty(&obsq);
1458           transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1459           for (j = 0; j < obsq.count; j++)
1460             POOL_DEBUG(SOLV_DEBUG_RESULT, "         -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1461         }
1462       for (j = te->edges; od->edgedata[j]; j += 2)
1463         {
1464           te2 = od->tes + od->edgedata[j];
1465           if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1466             POOL_DEBUG(SOLV_DEBUG_RESULT, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1467           else
1468             POOL_DEBUG(SOLV_DEBUG_RESULT, "       ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1469         }
1470     }
1471 }
1472
1473 #if 1
1474 static void
1475 reachable(struct orderdata *od, Id i)
1476 {
1477   struct _TransactionElement *te = od->tes + i;
1478   int j, k;
1479
1480   if (te->mark != 0)
1481     return;
1482   te->mark = 1;
1483   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1484     {
1485       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1486         continue;
1487       if (!od->tes[k].mark)
1488         reachable(od, k);
1489       if (od->tes[k].mark == 2)
1490         {
1491           te->mark = 2;
1492           return;
1493         }
1494     }
1495   te->mark = -1;
1496 }
1497 #endif
1498
1499 static void
1500 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1501 {
1502 #if 0
1503   Transaction *trans = od->trans;
1504   Pool *pool = trans->pool;
1505 #endif
1506   struct _TransactionElement *te;
1507   int i, j, k, tail;
1508 #if 1
1509   int head;
1510 #endif
1511
1512 #if 0
1513   printf("addcycleedges\n");
1514   for (i = 0; (j = cycle[i]) != 0; i++)
1515     printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1516 #endif
1517
1518   /* first add all the tail cycle edges */
1519
1520   /* see what we can reach from the cycle */
1521   queue_empty(todo);
1522   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1523     te->mark = 0;
1524   for (i = 0; (j = cycle[i]) != 0; i++)
1525     {
1526       od->tes[j].mark = -1;
1527       queue_push(todo, j);
1528     }
1529   while (todo->count)
1530     {
1531       i = queue_pop(todo);
1532       te = od->tes + i;
1533       if (te->mark > 0)
1534         continue;
1535       te->mark = te->mark < 0 ? 2 : 1;
1536       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1537         {
1538           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1539             continue;
1540           if (od->tes[k].mark > 0)
1541             continue;   /* no need to visit again */
1542           queue_push(todo, k);
1543         }
1544     }
1545   /* now all cycle TEs are marked with 2, all TEs reachable
1546    * from the cycle are marked with 1 */
1547   tail = cycle[0];
1548   od->tes[tail].mark = 1;       /* no need to add edges */
1549
1550   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1551     {
1552       if (te->mark)
1553         continue;       /* reachable from cycle */
1554       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1555         {
1556           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1557             continue;
1558           if (od->tes[k].mark != 2)
1559             continue;
1560           /* We found an edge to the cycle. Add an extra edge to the tail */
1561           /* the TE was not reachable, so we're not creating a new cycle! */
1562 #if 0
1563           printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
1564 #endif
1565           j -= te->edges;       /* in case we move */
1566           addteedge(od, i, tail, TYPE_CYCLETAIL);
1567           j += te->edges;
1568           break;        /* one edge is enough */
1569         }
1570     }
1571
1572 #if 1
1573   /* now add all head cycle edges */
1574
1575   /* reset marks */
1576   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1577     te->mark = 0;
1578   head = 0;
1579   for (i = 0; (j = cycle[i]) != 0; i++)
1580     {
1581       head = j;
1582       od->tes[j].mark = 2;
1583     }
1584   /* first the head to save some time */
1585   te = od->tes + head;
1586   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1587     {
1588       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1589         continue;
1590       if (!od->tes[k].mark)
1591         reachable(od, k);
1592       if (od->tes[k].mark == -1)
1593         od->tes[k].mark = -2;   /* no need for another edge */
1594     }
1595   for (i = 0; cycle[i] != 0; i++)
1596     {
1597       if (cycle[i] == head)
1598         break;
1599       te = od->tes + cycle[i];
1600       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1601         {
1602           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1603             continue;
1604           /* see if we can reach a cycle TE from k */
1605           if (!od->tes[k].mark)
1606             reachable(od, k);
1607           if (od->tes[k].mark == -1)
1608             {
1609 #if 0
1610               printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
1611 #endif
1612               addteedge(od, head, k, TYPE_CYCLEHEAD);
1613               od->tes[k].mark = -2;     /* no need to add that one again */
1614             }
1615         }
1616     }
1617 #endif
1618 }
1619
1620 void
1621 transaction_order(Transaction *trans, int flags)
1622 {
1623   Pool *pool = trans->pool;
1624   Queue *tr = &trans->steps;
1625   Repo *installed = pool->installed;
1626   Id p;
1627   Solvable *s;
1628   int i, j, k, numte, numedge;
1629   struct orderdata od;
1630   struct _TransactionElement *te;
1631   Queue todo, obsq, samerepoq, uninstq;
1632   int cycstart, cycel;
1633   Id *cycle;
1634   int oldcount;
1635   int start, now;
1636   Repo *lastrepo;
1637   int lastmedia;
1638   Id *temedianr;
1639
1640   start = now = solv_timems(0);
1641   POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1642   /* free old data if present */
1643   if (trans->orderdata)
1644     {
1645       struct _TransactionOrderdata *od = trans->orderdata;
1646       od->tes = solv_free(od->tes);
1647       od->invedgedata = solv_free(od->invedgedata);
1648       trans->orderdata = solv_free(trans->orderdata);
1649     }
1650
1651   /* create a transaction element for every active component */
1652   numte = 0;
1653   for (i = 0; i < tr->count; i++)
1654     {
1655       p = tr->elements[i];
1656       s = pool->solvables + p;
1657       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1658         continue;
1659       numte++;
1660     }
1661   POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1662   if (!numte)
1663     return;     /* nothing to do... */
1664
1665   numte++;      /* leave first one zero */
1666   memset(&od, 0, sizeof(od));
1667   od.trans = trans;
1668   od.ntes = numte;
1669   od.tes = solv_calloc(numte, sizeof(*od.tes));
1670   od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1671   od.edgedata[0] = 0;
1672   od.nedgedata = 1;
1673   queue_init(&od.cycles);
1674
1675   /* initialize TEs */
1676   for (i = 0, te = od.tes + 1; i < tr->count; i++)
1677     {
1678       p = tr->elements[i];
1679       s = pool->solvables + p;
1680       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1681         continue;
1682       te->p = p;
1683       te++;
1684     }
1685
1686   /* create dependency graph */
1687   for (i = 0; i < tr->count; i++)
1688     addsolvableedges(&od, pool->solvables + tr->elements[i]);
1689
1690   /* count edges */
1691   numedge = 0;
1692   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1693     for (j = te->edges; od.edgedata[j]; j += 2)
1694       numedge++;
1695   POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1696   POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1697
1698 #if 0
1699   dump_tes(&od);
1700 #endif
1701   
1702   now = solv_timems(0);
1703   /* kill all cycles */
1704   queue_init(&todo);
1705   for (i = numte - 1; i > 0; i--)
1706     queue_push(&todo, i);
1707
1708   while (todo.count)
1709     {
1710       i = queue_pop(&todo);
1711       /* printf("- look at TE %d\n", i); */
1712       if (i < 0)
1713         {
1714           i = -i;
1715           od.tes[i].mark = 2;   /* done with that one */
1716           continue;
1717         }
1718       te = od.tes + i;
1719       if (te->mark == 2)
1720         continue;               /* already finished before */
1721       if (te->mark == 0)
1722         {
1723           int edgestovisit = 0;
1724           /* new node, visit edges */
1725           for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1726             {
1727               if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1728                 continue;
1729               if (od.tes[k].mark == 2)
1730                 continue;       /* no need to visit again */
1731               if (!edgestovisit++)
1732                 queue_push(&todo, -i);  /* end of edges marker */
1733               queue_push(&todo, k);
1734             }
1735           if (!edgestovisit)
1736             te->mark = 2;       /* no edges, done with that one */
1737           else
1738             te->mark = 1;       /* under investigation */
1739           continue;
1740         }
1741       /* oh no, we found a cycle */
1742       /* find start of cycle node (<0) */
1743       for (j = todo.count - 1; j >= 0; j--)
1744         if (todo.elements[j] == -i)
1745           break;
1746       assert(j >= 0);
1747       cycstart = j;
1748       /* build te/edge chain */
1749       k = cycstart;
1750       for (j = k; j < todo.count; j++)
1751         if (todo.elements[j] < 0)
1752           todo.elements[k++] = -todo.elements[j];
1753       cycel = k - cycstart;
1754       assert(cycel > 1);
1755       /* make room for edges, two extra element for cycle loop + terminating 0 */
1756       while (todo.count < cycstart + 2 * cycel + 2)
1757         queue_push(&todo, 0);
1758       cycle = todo.elements + cycstart;
1759       cycle[cycel] = i;         /* close the loop */
1760       cycle[2 * cycel + 1] = 0; /* terminator */
1761       for (k = cycel; k > 0; k--)
1762         {
1763           cycle[k * 2] = cycle[k];
1764           te = od.tes + cycle[k - 1];
1765           assert(te->mark == 1);
1766           te->mark = 0; /* reset investigation marker */
1767           /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1768           for (j = te->edges; od.edgedata[j]; j += 2)
1769             if (od.edgedata[j] == cycle[k])
1770               break;
1771           assert(od.edgedata[j]);
1772           cycle[k * 2 - 1] = j;
1773         }
1774       /* now cycle looks like this: */
1775       /* te1 edge te2 edge te3 ... teN edge te1 0 */
1776       breakcycle(&od, cycle);
1777       /* restart with start of cycle */
1778       todo.count = cycstart + 1;
1779     }
1780   POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1781   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1782
1783   now = solv_timems(0);
1784   /* now go through all broken cycles and create cycle edges to help
1785      the ordering */
1786    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1787      {
1788        if (od.cycles.elements[i + 2] >= TYPE_REQ)
1789          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1790      }
1791    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1792      {
1793        if (od.cycles.elements[i + 2] < TYPE_REQ)
1794          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1795      }
1796   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1797
1798 #if 0
1799   dump_tes(&od);
1800 #endif
1801   /* all edges are finally set up and there are no cycles, now the easy part.
1802    * Create an ordered transaction */
1803   now = solv_timems(0);
1804   /* first invert all edges */
1805   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1806     te->mark = 1;       /* term 0 */
1807   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1808     {
1809       for (j = te->edges; od.edgedata[j]; j += 2)
1810         {
1811           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1812             continue;
1813           od.tes[od.edgedata[j]].mark++;
1814         }
1815     }
1816   j = 1;
1817   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1818     {
1819       te->mark += j;
1820       j = te->mark;
1821     }
1822   POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1823   od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1824   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1825     {
1826       for (j = te->edges; od.edgedata[j]; j += 2)
1827         {
1828           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1829             continue;
1830           od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1831         }
1832     }
1833   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1834     te->edges = te->mark;       /* edges now points into invedgedata */
1835   od.edgedata = solv_free(od.edgedata);
1836   od.nedgedata = j + 1;
1837
1838   /* now the final ordering */
1839   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1840     te->mark = 0;
1841   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1842     for (j = te->edges; od.invedgedata[j]; j++)
1843       od.tes[od.invedgedata[j]].mark++;
1844
1845   queue_init(&samerepoq);
1846   queue_init(&uninstq);
1847   queue_empty(&todo);
1848   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1849     if (te->mark == 0)
1850       {
1851         if (installed && pool->solvables[te->p].repo == installed)
1852           queue_push(&uninstq, i);
1853         else
1854           queue_push(&todo, i);
1855       }
1856   assert(todo.count > 0 || uninstq.count > 0);
1857   oldcount = tr->count;
1858   queue_empty(tr);
1859
1860   queue_init(&obsq);
1861   
1862   lastrepo = 0;
1863   lastmedia = 0;
1864   temedianr = solv_calloc(numte, sizeof(Id));
1865   for (i = 1; i < numte; i++)
1866     {
1867       Solvable *s = pool->solvables + od.tes[i].p;
1868       if (installed && s->repo == installed)
1869         j = 1;
1870       else
1871         j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1872       temedianr[i] = j;
1873     }
1874   for (;;)
1875     {
1876       /* select an TE i */
1877       if (uninstq.count)
1878         i = queue_shift(&uninstq);
1879       else if (samerepoq.count)
1880         i = queue_shift(&samerepoq);
1881       else if (todo.count)
1882         {
1883           /* find next repo/media */
1884           for (j = 0; j < todo.count; j++)
1885             {
1886               if (!j || temedianr[todo.elements[j]] < lastmedia)
1887                 {
1888                   i = j;
1889                   lastmedia = temedianr[todo.elements[j]];
1890                 }
1891             }
1892           lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1893
1894           /* move all matching TEs to samerepoq */
1895           for (i = j = 0; j < todo.count; j++)
1896             {
1897               int k = todo.elements[j];
1898               if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1899                 queue_push(&samerepoq, k);
1900               else
1901                 todo.elements[i++] = k;
1902             }
1903           todo.count = i;
1904
1905           assert(samerepoq.count);
1906           i = queue_shift(&samerepoq);
1907         }
1908       else
1909         break;
1910
1911       te = od.tes + i;
1912       queue_push(tr, te->p);
1913 #if 0
1914 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1915 #endif
1916       s = pool->solvables + te->p;
1917       for (j = te->edges; od.invedgedata[j]; j++)
1918         {
1919           struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1920           assert(te2->mark > 0);
1921           if (--te2->mark == 0)
1922             {
1923               Solvable *s = pool->solvables + te2->p;
1924 #if 0
1925 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1926 #endif
1927               if (installed && s->repo == installed)
1928                 queue_push(&uninstq, od.invedgedata[j]);
1929               else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1930                 queue_push(&samerepoq, od.invedgedata[j]);
1931               else
1932                 queue_push(&todo, od.invedgedata[j]);
1933             }
1934         }
1935     }
1936   solv_free(temedianr);
1937   queue_free(&todo);
1938   queue_free(&samerepoq);
1939   queue_free(&uninstq);
1940   queue_free(&obsq);
1941   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1942     assert(te->mark == 0);
1943
1944   /* add back obsoleted packages */
1945   transaction_add_obsoleted(trans);
1946   assert(tr->count == oldcount);
1947
1948   POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1949   POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1950
1951   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1952     {
1953       trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1954       trans->orderdata->tes = od.tes;
1955       trans->orderdata->ntes = numte;
1956       trans->orderdata->invedgedata = od.invedgedata;
1957       trans->orderdata->ninvedgedata = od.nedgedata;
1958     }
1959   else
1960     {
1961       solv_free(od.tes);
1962       solv_free(od.invedgedata);
1963     }
1964   queue_free(&od.cycles);
1965   queue_free(&od.cyclesdata);
1966 }
1967
1968
1969 int
1970 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1971 {
1972   int i, j;
1973   struct _TransactionOrderdata *od = trans->orderdata;
1974   struct _TransactionElement *te;
1975
1976   if (!od)
1977      return choices->count;
1978   if (!chosen)
1979     {
1980       /* initialization step */
1981       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1982         te->mark = 0;
1983       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1984         {
1985           for (j = te->edges; od->invedgedata[j]; j++)
1986             od->tes[od->invedgedata[j]].mark++;
1987         }
1988       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1989         if (!te->mark)
1990           queue_push(choices, te->p);
1991       return choices->count;
1992     }
1993   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1994     if (te->p == chosen)
1995       break;
1996   if (i == od->ntes)
1997     return choices->count;
1998   if (te->mark > 0)
1999     {
2000       /* hey! out-of-order installation! */
2001       te->mark = -1;
2002     }
2003   for (j = te->edges; od->invedgedata[j]; j++)
2004     {
2005       te = od->tes + od->invedgedata[j];
2006       assert(te->mark > 0 || te->mark == -1);
2007       if (te->mark > 0 && --te->mark == 0)
2008         queue_push(choices, te->p);
2009     }
2010   return choices->count;
2011 }
2012
2013 void
2014 transaction_add_obsoleted(Transaction *trans)
2015 {
2016   Pool *pool = trans->pool;
2017   Repo *installed = pool->installed;
2018   Id p;
2019   Solvable *s;
2020   int i, j, k, max;
2021   Map done;
2022   Queue obsq, *steps;
2023
2024   if (!installed || !trans->steps.count)
2025     return;
2026   /* calculate upper bound */
2027   max = 0;
2028   FOR_REPO_SOLVABLES(installed, p, s)
2029     if (MAPTST(&trans->transactsmap, p))
2030       max++;
2031   if (!max)
2032     return;
2033   /* make room */
2034   steps = &trans->steps;
2035   queue_insertn(steps, 0, max, 0);
2036
2037   /* now add em */
2038   map_init(&done, installed->end - installed->start);
2039   queue_init(&obsq);
2040   for (j = 0, i = max; i < steps->count; i++)
2041     {
2042       p = trans->steps.elements[i];
2043       if (pool->solvables[p].repo == installed)
2044         {
2045           if (!trans->transaction_installed[p - pool->installed->start])
2046             trans->steps.elements[j++] = p;
2047           continue;
2048         }
2049       trans->steps.elements[j++] = p;
2050       queue_empty(&obsq);
2051       transaction_all_obs_pkgs(trans, p, &obsq);
2052       for (k = 0; k < obsq.count; k++)
2053         {
2054           p = obsq.elements[k];
2055           assert(p >= installed->start && p < installed->end);
2056           if (!MAPTST(&trans->transactsmap, p)) /* just in case */
2057             continue;
2058           if (MAPTST(&done, p - installed->start))
2059             continue;
2060           MAPSET(&done, p - installed->start);
2061           trans->steps.elements[j++] = p;
2062         }
2063     }
2064
2065   /* free unneeded space */
2066   queue_truncate(steps, j);
2067   map_free(&done);
2068   queue_free(&obsq);
2069 }
2070
2071 static void
2072 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
2073 {
2074   Pool *pool = trans->pool;
2075   Id p, pp;
2076   Solvable *s;
2077   int good;
2078
2079   if (MAPTST(seen, pkg))
2080     return;
2081   MAPSET(seen, pkg);
2082   s = pool->solvables + pkg;
2083 #if 0
2084   printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2085 #endif
2086   if (s->requires)
2087     {
2088       Id req, *reqp;
2089       int inpre = 0;
2090       reqp = s->repo->idarraydata + s->requires;
2091       while ((req = *reqp++) != 0)
2092         {
2093           if (req == SOLVABLE_PREREQMARKER)
2094             {
2095               inpre = 1;
2096               continue;
2097             }
2098           if (onlyprereq && !inpre)
2099             continue;
2100           if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2101             continue;
2102           good = 0;
2103           /* first check kept packages, then freshly installed, then not yet uninstalled */
2104           FOR_PROVIDES(p, pp, req)
2105             {
2106               if (!MAPTST(ins, p))
2107                 continue;
2108               if (MAPTST(&trans->transactsmap, p))
2109                 continue;
2110               good++;
2111               transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2112             }
2113           if (!good)
2114             {
2115               FOR_PROVIDES(p, pp, req)
2116                 {
2117                   if (!MAPTST(ins, p))
2118                     continue;
2119                   if (pool->solvables[p].repo == pool->installed)
2120                     continue;
2121                   good++;
2122                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2123                 }
2124             }
2125           if (!good)
2126             {
2127               FOR_PROVIDES(p, pp, req)
2128                 {
2129                   if (!MAPTST(ins, p))
2130                     continue;
2131                   good++;
2132                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2133                 }
2134             }
2135           if (!good)
2136             {
2137               POOL_DEBUG(SOLV_DEBUG_RESULT, "  %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2138             }
2139         }
2140     }
2141 }
2142
2143 void
2144 transaction_check_order(Transaction *trans)
2145 {
2146   Pool *pool = trans->pool;
2147   Solvable *s;
2148   Id p, lastins;
2149   Map ins, seen;
2150   int i;
2151
2152   POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
2153   map_init(&ins, pool->nsolvables);
2154   map_init(&seen, pool->nsolvables);
2155   if (pool->installed)
2156     FOR_REPO_SOLVABLES(pool->installed, p, s)
2157       MAPSET(&ins, p);
2158   lastins = 0;
2159   for (i = 0; i < trans->steps.count; i++)
2160     {
2161       p = trans->steps.elements[i];
2162       s = pool->solvables + p;
2163       if (s->repo != pool->installed)
2164         lastins = p;
2165       if (s->repo != pool->installed)
2166         MAPSET(&ins, p);
2167       if (havescripts(pool, p))
2168         {
2169           MAPZERO(&seen);
2170           transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2171         }
2172       if (s->repo == pool->installed)
2173         MAPCLR(&ins, p);
2174     }
2175   map_free(&seen);
2176   map_free(&ins);
2177   POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
2178 }