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