cleanup code a bit. remove unused choice calculation in order 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 static inline void
479 queue_push4(Queue *q, Id id1, Id id2, Id id3, Id id4)
480 {
481   queue_push(q, id1);
482   queue_push(q, id2);
483   queue_push(q, id3);
484   queue_push(q, id4);
485 }
486
487 static inline void
488 queue_unshift4(Queue *q, Id id1, Id id2, Id id3, Id id4)
489 {
490   queue_unshift(q, id4);
491   queue_unshift(q, id3);
492   queue_unshift(q, id2);
493   queue_unshift(q, id1);
494 }
495
496 void
497 transaction_classify(Transaction *trans, int mode, Queue *classes)
498 {
499   Pool *pool = trans->pool;
500   int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
501   Solvable *s, *sq;
502   Id v, vq, type, p, q;
503   int i, j;
504
505   queue_empty(classes);
506   memset(ntypes, 0, sizeof(ntypes));
507   /* go through transaction and classify each step */
508   for (i = 0; i < trans->steps.count; i++)
509     {
510       p = trans->steps.elements[i];
511       s = pool->solvables + p;
512       type = transaction_type(trans, p, mode);
513       ntypes[type]++;
514       if (!pool->installed || s->repo != pool->installed)
515         continue;
516       /* don't report vendor/arch changes if we were mapped to erase. */
517       if (type == SOLVER_TRANSACTION_ERASE)
518         continue;
519       /* look at arch/vendor changes */
520       q = transaction_obs_pkg(trans, p);
521       if (!q)
522         continue;
523       sq = pool->solvables + q;
524
525       v = s->arch;
526       vq = sq->arch;
527       if (v != vq)
528         {
529           if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
530             v = vq = 0;
531           for (j = 0; j < classes->count; j += 4)
532             if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
533               break;
534           if (j == classes->count)
535             queue_push4(classes, SOLVER_TRANSACTION_ARCHCHANGE, 1, v, vq);
536           else
537             classes->elements[j + 1]++;
538         }
539
540       v = s->vendor ? s->vendor : 1;
541       vq = sq->vendor ? sq->vendor : 1;
542       if (v != vq)
543         {
544           if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
545             v = vq = 0;
546           for (j = 0; j < classes->count; j += 4)
547             if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
548               break;
549           if (j == classes->count)
550             queue_push4(classes, SOLVER_TRANSACTION_VENDORCHANGE, 1, v, vq);
551           else
552             classes->elements[j + 1]++;
553         }
554     }
555   /* now sort all vendor/arch changes */
556   if (classes->count > 4)
557     solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
558   /* finally add all classes. put erases last */
559   i = SOLVER_TRANSACTION_ERASE;
560   if (ntypes[i])
561     queue_unshift4(classes, i, ntypes[i], 0, 0);
562   for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
563     {
564       if (!ntypes[i])
565         continue;
566       if (i == SOLVER_TRANSACTION_ERASE)
567         continue;
568       queue_unshift4(classes, i, ntypes[i], 0, 0);
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 /* create a transaction from the decisionq */
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           MAPSET(&trans->transactsmap, p);
740           if (multiversionmap && MAPTST(multiversionmap, p))
741             needmulti = 1;
742         }
743     }
744   MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
745   if (needmulti)
746     map_init_clone(&trans->multiversionmap, multiversionmap);
747
748   create_transaction_info(trans, decisionq);
749
750   if (installed)
751     {
752       FOR_REPO_SOLVABLES(installed, p, s)
753         {
754           if (MAPTST(&trans->transactsmap, p))
755             queue_push(&trans->steps, p);
756         }
757     }
758   for (i = 0; i < decisionq->count; i++)
759     {
760       p = decisionq->elements[i];
761       if (p > 0 && MAPTST(&trans->transactsmap, p))
762         queue_push(&trans->steps, p);
763     }
764   return trans;
765 }
766
767 int
768 transaction_installedresult(Transaction *trans, Queue *installedq)
769 {
770   Pool *pool = trans->pool;
771   Repo *installed = pool->installed;
772   Solvable *s;
773   int i, cutoff;
774   Id p;
775
776   queue_empty(installedq);
777   /* first the new installs, than the kept packages */
778   for (i = 0; i < trans->steps.count; i++)
779     {
780       p = trans->steps.elements[i];
781       s = pool->solvables + p;
782       if (installed && s->repo == installed)
783         continue;
784       queue_push(installedq, p);
785     }
786   cutoff = installedq->count;
787   if (installed)
788     {
789       FOR_REPO_SOLVABLES(installed, p, s)
790         if (!MAPTST(&trans->transactsmap, p))
791           queue_push(installedq, p);
792     }
793   return cutoff;
794 }
795
796 static void
797 transaction_make_installedmap(Transaction *trans, Map *installedmap)
798 {
799   Pool *pool = trans->pool;
800   Repo *installed = pool->installed;
801   Solvable *s;
802   Id p;
803   int i;
804
805   map_init(installedmap, pool->nsolvables);
806   for (i = 0; i < trans->steps.count; i++)
807     {
808       p = trans->steps.elements[i];
809       s = pool->solvables + p;
810       if (!installed || s->repo != installed)
811         MAPSET(installedmap, p);
812     }
813   if (installed)
814     {
815       FOR_REPO_SOLVABLES(installed, p, s)
816         if (!MAPTST(&trans->transactsmap, p))
817           MAPSET(installedmap, p);
818     }
819 }
820
821 int
822 transaction_calc_installsizechange(Transaction *trans)
823 {
824   Map installedmap;
825   int change;
826
827   transaction_make_installedmap(trans, &installedmap);
828   change = pool_calc_installsizechange(trans->pool, &installedmap);
829   map_free(&installedmap);
830   return change;
831 }
832
833 void
834 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
835 {
836   Map installedmap;
837
838   transaction_make_installedmap(trans, &installedmap);
839   pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
840   map_free(&installedmap);
841 }
842
843 struct _TransactionElement {
844   Id p;         /* solvable id */
845   Id edges;     /* pointer into edges data */
846   Id mark;
847 };
848
849 struct _TransactionOrderdata {
850   struct _TransactionElement *tes;
851   int ntes;
852   Id *invedgedata;
853   int ninvedgedata;
854 };
855
856 #define TYPE_BROKEN     (1<<0)
857 #define TYPE_CON        (1<<1)
858
859 #define TYPE_REQ_P      (1<<2)
860 #define TYPE_PREREQ_P   (1<<3)
861
862 #define TYPE_REQ        (1<<4)
863 #define TYPE_PREREQ     (1<<5)
864
865 #define TYPE_CYCLETAIL  (1<<16)
866 #define TYPE_CYCLEHEAD  (1<<17)
867
868 #define EDGEDATA_BLOCK  127
869
870 Transaction *
871 transaction_create(Pool *pool)
872 {
873   Transaction *trans = solv_calloc(1, sizeof(*trans));
874   trans->pool = pool;
875   return trans;
876 }
877
878 Transaction *
879 transaction_create_clone(Transaction *srctrans)
880 {
881   Transaction *trans = transaction_create(srctrans->pool);
882   queue_init_clone(&trans->steps, &srctrans->steps);
883   queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
884   if (srctrans->transaction_installed)
885     {
886       Repo *installed = srctrans->pool->installed;
887       trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
888     }
889   map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
890   map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
891   if (srctrans->orderdata)
892     {
893       struct _TransactionOrderdata *od = srctrans->orderdata;
894       trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
895       trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
896       trans->orderdata->ntes = od->ntes;
897       trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
898       trans->orderdata->ninvedgedata = od->ninvedgedata;
899     }
900   return trans;
901 }
902
903 void
904 transaction_free(Transaction *trans)
905 {
906   queue_free(&trans->steps);
907   queue_free(&trans->transaction_info);
908   trans->transaction_installed = solv_free(trans->transaction_installed);
909   map_free(&trans->transactsmap);
910   map_free(&trans->multiversionmap);
911   transaction_free_orderdata(trans);
912   free(trans);
913 }
914
915 void
916 transaction_free_orderdata(Transaction *trans)
917 {
918   if (trans->orderdata)
919     {
920       struct _TransactionOrderdata *od = trans->orderdata;
921       od->tes = solv_free(od->tes);
922       od->invedgedata = solv_free(od->invedgedata);
923       trans->orderdata = solv_free(trans->orderdata);
924     }
925 }
926
927 struct orderdata {
928   Transaction *trans;
929   struct _TransactionElement *tes;
930   int ntes;
931   Id *edgedata;
932   int nedgedata;
933   Id *invedgedata;
934
935   Queue cycles;
936   Queue cyclesdata;
937   int ncycles;
938 };
939
940 static int
941 addteedge(struct orderdata *od, int from, int to, int type)
942 {
943   int i;
944   struct _TransactionElement *te;
945
946   if (from == to)
947     return 0;
948
949   /* 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); */
950
951   te = od->tes + from;
952   for (i = te->edges; od->edgedata[i]; i += 2)
953     if (od->edgedata[i] == to)
954       break;
955   /* test of brokenness */
956   if (type == TYPE_BROKEN)
957     return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
958   if (od->edgedata[i])
959     {
960       od->edgedata[i + 1] |= type;
961       return 0;
962     }
963   if (i + 1 == od->nedgedata)
964     {
965       /* printf("tail add %d\n", i - te->edges); */
966       if (!i)
967         te->edges = ++i;
968       od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
969     }
970   else
971     {
972       /* printf("extend %d\n", i - te->edges); */
973       od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
974       if (i > te->edges)
975         memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
976       i = od->nedgedata + (i - te->edges);
977       te->edges = od->nedgedata;
978     }
979   od->edgedata[i] = to;
980   od->edgedata[i + 1] = type;
981   od->edgedata[i + 2] = 0;      /* end marker */
982   od->nedgedata = i + 3;
983   return 0;
984 }
985
986 static int
987 addedge(struct orderdata *od, Id from, Id to, int type)
988 {
989   Transaction *trans = od->trans;
990   Pool *pool = trans->pool;
991   Solvable *s;
992   struct _TransactionElement *te;
993   int i;
994
995   /* printf("addedge %d %d type %d\n", from, to, type); */
996   s = pool->solvables + from;
997   if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
998     {
999       /* obsolete, map to install */
1000       if (trans->transaction_installed[from - pool->installed->start] > 0)
1001         from = trans->transaction_installed[from - pool->installed->start];
1002       else
1003         {
1004           int ret = 0;
1005           Queue ti;
1006           Id tibuf[5];
1007
1008           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1009           transaction_all_obs_pkgs(trans, from, &ti);
1010           for (i = 0; i < ti.count; i++)
1011             ret |= addedge(od, ti.elements[i], to, type);
1012           queue_free(&ti);
1013           return ret;
1014         }
1015     }
1016   s = pool->solvables + to;
1017   if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
1018     {
1019       /* obsolete, map to install */
1020       if (trans->transaction_installed[to - pool->installed->start] > 0)
1021         to = trans->transaction_installed[to - pool->installed->start];
1022       else
1023         {
1024           int ret = 0;
1025           Queue ti;
1026           Id tibuf[5];
1027
1028           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1029           transaction_all_obs_pkgs(trans, to, &ti);
1030           for (i = 0; i < ti.count; i++)
1031             ret |= addedge(od, from, ti.elements[i], type);
1032           queue_free(&ti);
1033           return ret;
1034         }
1035     }
1036
1037   /* map from/to to te numbers */
1038   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1039     if (te->p == to)
1040       break;
1041   if (i == od->ntes)
1042     return 0;
1043   to = i;
1044
1045   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1046     if (te->p == from)
1047       break;
1048   if (i == od->ntes)
1049     return 0;
1050
1051   return addteedge(od, i, to, type);
1052 }
1053
1054 static inline int
1055 havescripts(Pool *pool, Id solvid)
1056 {
1057   Solvable *s = pool->solvables + solvid;
1058   const char *dep;
1059   if (s->requires)
1060     {
1061       Id req, *reqp;
1062       int inpre = 0;
1063       reqp = s->repo->idarraydata + s->requires;
1064       while ((req = *reqp++) != 0)
1065         {
1066           if (req == SOLVABLE_PREREQMARKER)
1067             {
1068               inpre = 1;
1069               continue;
1070             }
1071           if (!inpre)
1072             continue;
1073           dep = pool_id2str(pool, req);
1074           if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1075             return 1;
1076         }
1077     }
1078   return 0;
1079 }
1080
1081 static void
1082 addsolvableedges(struct orderdata *od, Solvable *s)
1083 {
1084   Transaction *trans = od->trans;
1085   Pool *pool = trans->pool;
1086   Id req, *reqp, con, *conp;
1087   Id p, p2, pp2;
1088   int i, j, pre, numins;
1089   Repo *installed = pool->installed;
1090   Solvable *s2;
1091   Queue reqq;
1092   int provbyinst;
1093
1094 #if 0
1095   printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1096 #endif
1097   p = s - pool->solvables;
1098   queue_init(&reqq);
1099   if (s->requires)
1100     {
1101       reqp = s->repo->idarraydata + s->requires;
1102       pre = TYPE_REQ;
1103       while ((req = *reqp++) != 0)
1104         {
1105           if (req == SOLVABLE_PREREQMARKER)
1106             {
1107               pre = TYPE_PREREQ;
1108               continue;
1109             }
1110 #if 0
1111           if (pre != TYPE_PREREQ && installed && s->repo == installed)
1112             {
1113               /* ignore normal requires if we're getting obsoleted */
1114               if (trans->transaction_installed[p - pool->installed->start])
1115                 continue;
1116             }
1117 #endif
1118           queue_empty(&reqq);
1119           numins = 0;   /* number of packages to be installed providing it */
1120           provbyinst = 0;       /* provided by kept package */
1121           FOR_PROVIDES(p2, pp2, req)
1122             {
1123               s2 = pool->solvables + p2;
1124               if (p2 == p)
1125                 {
1126                   reqq.count = 0;       /* self provides */
1127                   break;
1128                 }
1129               if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1130                 {
1131                   provbyinst = 1;
1132 #if 0
1133                   printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1134                   reqq.count = 0;       /* provided by package that stays installed */
1135                   break;
1136 #else
1137                   continue;
1138 #endif
1139                 }
1140               if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1141                 continue;               /* package stays uninstalled */
1142
1143               if (s->repo == installed)
1144                 {
1145                   /* s gets uninstalled */
1146                   queue_pushunique(&reqq, p2);
1147                   if (s2->repo != installed)
1148                     numins++;
1149                 }
1150               else
1151                 {
1152                   if (s2->repo == installed)
1153                     continue;   /* s2 gets uninstalled */
1154                   queue_pushunique(&reqq, p2);
1155                 }
1156             }
1157           if (provbyinst)
1158             {
1159               /* prune to harmless ->inst edges */
1160               for (i = j = 0; i < reqq.count; i++)
1161                 if (pool->solvables[reqq.elements[i]].repo != installed)
1162                   reqq.elements[j++] = reqq.elements[i];
1163               reqq.count = j;
1164             }
1165
1166           if (numins && reqq.count)
1167             {
1168               if (s->repo == installed)
1169                 {
1170                   for (i = 0; i < reqq.count; i++)
1171                     {
1172                       if (pool->solvables[reqq.elements[i]].repo == installed)
1173                         {
1174                           for (j = 0; j < reqq.count; j++)
1175                             {
1176                               if (pool->solvables[reqq.elements[j]].repo != installed)
1177                                 {
1178                                   if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1179                                     continue;   /* no self edge */
1180 #if 0
1181                                   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]));
1182 #endif
1183                                   addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1184                                 }
1185                             }
1186                         }
1187                     }
1188                 }
1189               /* no mixed types, remove all deps on uninstalls */
1190               for (i = j = 0; i < reqq.count; i++)
1191                 if (pool->solvables[reqq.elements[i]].repo != installed)
1192                   reqq.elements[j++] = reqq.elements[i];
1193               reqq.count = j;
1194             }
1195           if (!reqq.count)
1196             continue;
1197           for (i = 0; i < reqq.count; i++)
1198             {
1199               p2 = reqq.elements[i];
1200               if (pool->solvables[p2].repo != installed)
1201                 {
1202                   /* all elements of reqq are installs, thus have different TEs */
1203                   if (pool->solvables[p].repo != installed)
1204                     {
1205 #if 0
1206                       printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1207 #endif
1208                       addedge(od, p, p2, pre);
1209                     }
1210                   else
1211                     {
1212 #if 0
1213                       printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1214 #endif
1215                       addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1216                     }
1217                 }
1218               else
1219                 {
1220                   if (s->repo != installed)
1221                     continue;   /* no inst->uninst edges, please! */
1222
1223                   /* uninst -> uninst edge. Those make trouble. Only add if we must */
1224                   if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1225                     {
1226                       /* p is obsoleted by another package and has no scripts */
1227                       /* we assume that the obsoletor is good enough to replace p */
1228                       continue;
1229                     }
1230 #if 0
1231                   printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1232 #endif
1233                   addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1234                 }
1235             }
1236         }
1237     }
1238   if (s->conflicts)
1239     {
1240       conp = s->repo->idarraydata + s->conflicts;
1241       while ((con = *conp++) != 0)
1242         {
1243           FOR_PROVIDES(p2, pp2, con)
1244             {
1245               if (p2 == p)
1246                 continue;
1247               s2 = pool->solvables + p2;
1248               if (!s2->repo)
1249                 continue;
1250               if (s->repo == installed)
1251                 {
1252                   if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1253                     {
1254                       /* deinstall p before installing p2 */
1255 #if 0
1256                       printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1257 #endif
1258                       addedge(od, p2, p, TYPE_CON);
1259                     }
1260                 }
1261               else
1262                 {
1263                   if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1264                     {
1265                       /* deinstall p2 before installing p */
1266 #if 0
1267                       printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1268 #endif
1269                       addedge(od, p, p2, TYPE_CON);
1270                     }
1271                 }
1272
1273             }
1274         }
1275     }
1276   if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1277     {
1278       /* we're getting deinstalled/updated. Try to do this before our
1279        * triggers are hit */
1280       for (i = 0; i < reqq.count; i++)
1281         {
1282           Id tri = reqq.elements[i];
1283           FOR_PROVIDES(p2, pp2, tri)
1284             {
1285               if (p2 == p)
1286                 continue;
1287               s2 = pool->solvables + p2;
1288               if (!s2->repo)
1289                 continue;
1290               if (s2->name == s->name)
1291                 continue;       /* obsoleted anyway */
1292               if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1293                 {
1294                   /* deinstall/update p before installing p2 */
1295 #if 0
1296                   printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1297 #endif
1298                   addedge(od, p2, p, TYPE_CON);
1299                 }
1300             }
1301         }
1302     }
1303   queue_free(&reqq);
1304 }
1305
1306
1307 /* break an edge in a cycle */
1308 static void
1309 breakcycle(struct orderdata *od, Id *cycle)
1310 {
1311   Pool *pool = od->trans->pool;
1312   Id ddegmin, ddegmax, ddeg;
1313   int k, l;
1314   struct _TransactionElement *te;
1315
1316   l = 0;
1317   ddegmin = ddegmax = 0;
1318   for (k = 0; cycle[k + 1]; k += 2)
1319     {
1320       ddeg = od->edgedata[cycle[k + 1] + 1];
1321       if (ddeg > ddegmax)
1322         ddegmax = ddeg;
1323       if (!k || ddeg < ddegmin)
1324         {
1325           l = k;
1326           ddegmin = ddeg;
1327           continue;
1328         }
1329       if (ddeg == ddegmin)
1330         {
1331           if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1332             {
1333               /* prefer k, as l comes from a package with contains scriptlets */
1334               l = k;
1335               ddegmin = ddeg;
1336               continue;
1337             }
1338           /* same edge value, check for prereq */
1339         }
1340     }
1341
1342   /* record brkoen cycle starting with the tail */
1343   queue_push(&od->cycles, od->cyclesdata.count);                /* offset into data */
1344   queue_push(&od->cycles, k / 2);                               /* cycle elements */
1345   queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);      /* broken edge */
1346   od->ncycles++;
1347   for (k = l;;)
1348     {
1349       k += 2;
1350       if (!cycle[k + 1])
1351         k = 0;
1352       queue_push(&od->cyclesdata, cycle[k]);
1353       if (k == l)
1354         break;
1355     }
1356   queue_push(&od->cyclesdata, 0);       /* mark end */
1357
1358   /* break that edge */
1359   od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1360
1361 #if 1
1362   if (ddegmin < TYPE_REQ)
1363     return;
1364 #endif
1365
1366   /* cycle recorded, print it */
1367   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1368     POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1369   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1370   for (k = 0; cycle[k + 1]; k += 2)
1371     {
1372       te = od->tes +  cycle[k];
1373       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1374         POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1375       else
1376         POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1377     }
1378   POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1379 }
1380
1381 static inline void
1382 dump_tes(struct orderdata *od)
1383 {
1384   Pool *pool = od->trans->pool;
1385   int i, j;
1386   Queue obsq;
1387   struct _TransactionElement *te, *te2;
1388
1389   queue_init(&obsq);
1390   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1391     {
1392       Solvable *s = pool->solvables + te->p;
1393       POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1394       if (s->repo != pool->installed)
1395         {
1396           queue_empty(&obsq);
1397           transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1398           for (j = 0; j < obsq.count; j++)
1399             POOL_DEBUG(SOLV_DEBUG_RESULT, "         -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1400         }
1401       for (j = te->edges; od->edgedata[j]; j += 2)
1402         {
1403           te2 = od->tes + od->edgedata[j];
1404           if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1405             POOL_DEBUG(SOLV_DEBUG_RESULT, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1406           else
1407             POOL_DEBUG(SOLV_DEBUG_RESULT, "       ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1408         }
1409     }
1410 }
1411
1412 #if 1
1413 static void
1414 reachable(struct orderdata *od, Id i)
1415 {
1416   struct _TransactionElement *te = od->tes + i;
1417   int j, k;
1418
1419   if (te->mark != 0)
1420     return;
1421   te->mark = 1;
1422   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1423     {
1424       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1425         continue;
1426       if (!od->tes[k].mark)
1427         reachable(od, k);
1428       if (od->tes[k].mark == 2)
1429         {
1430           te->mark = 2;
1431           return;
1432         }
1433     }
1434   te->mark = -1;
1435 }
1436 #endif
1437
1438 static void
1439 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1440 {
1441 #if 0
1442   Transaction *trans = od->trans;
1443   Pool *pool = trans->pool;
1444 #endif
1445   struct _TransactionElement *te;
1446   int i, j, k, tail;
1447 #if 1
1448   int head;
1449 #endif
1450
1451 #if 0
1452   printf("addcycleedges\n");
1453   for (i = 0; (j = cycle[i]) != 0; i++)
1454     printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1455 #endif
1456
1457   /* first add all the tail cycle edges */
1458
1459   /* see what we can reach from the cycle */
1460   queue_empty(todo);
1461   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1462     te->mark = 0;
1463   for (i = 0; (j = cycle[i]) != 0; i++)
1464     {
1465       od->tes[j].mark = -1;
1466       queue_push(todo, j);
1467     }
1468   while (todo->count)
1469     {
1470       i = queue_pop(todo);
1471       te = od->tes + i;
1472       if (te->mark > 0)
1473         continue;
1474       te->mark = te->mark < 0 ? 2 : 1;
1475       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1476         {
1477           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1478             continue;
1479           if (od->tes[k].mark > 0)
1480             continue;   /* no need to visit again */
1481           queue_push(todo, k);
1482         }
1483     }
1484   /* now all cycle TEs are marked with 2, all TEs reachable
1485    * from the cycle are marked with 1 */
1486   tail = cycle[0];
1487   od->tes[tail].mark = 1;       /* no need to add edges */
1488
1489   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1490     {
1491       if (te->mark)
1492         continue;       /* reachable from cycle */
1493       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1494         {
1495           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1496             continue;
1497           if (od->tes[k].mark != 2)
1498             continue;
1499           /* We found an edge to the cycle. Add an extra edge to the tail */
1500           /* the TE was not reachable, so we're not creating a new cycle! */
1501 #if 0
1502           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));
1503 #endif
1504           j -= te->edges;       /* in case we move */
1505           addteedge(od, i, tail, TYPE_CYCLETAIL);
1506           j += te->edges;
1507           break;        /* one edge is enough */
1508         }
1509     }
1510
1511 #if 1
1512   /* now add all head cycle edges */
1513
1514   /* reset marks */
1515   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1516     te->mark = 0;
1517   head = 0;
1518   for (i = 0; (j = cycle[i]) != 0; i++)
1519     {
1520       head = j;
1521       od->tes[j].mark = 2;
1522     }
1523   /* first the head to save some time */
1524   te = od->tes + head;
1525   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1526     {
1527       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1528         continue;
1529       if (!od->tes[k].mark)
1530         reachable(od, k);
1531       if (od->tes[k].mark == -1)
1532         od->tes[k].mark = -2;   /* no need for another edge */
1533     }
1534   for (i = 0; cycle[i] != 0; i++)
1535     {
1536       if (cycle[i] == head)
1537         break;
1538       te = od->tes + cycle[i];
1539       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1540         {
1541           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1542             continue;
1543           /* see if we can reach a cycle TE from k */
1544           if (!od->tes[k].mark)
1545             reachable(od, k);
1546           if (od->tes[k].mark == -1)
1547             {
1548 #if 0
1549               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));
1550 #endif
1551               addteedge(od, head, k, TYPE_CYCLEHEAD);
1552               od->tes[k].mark = -2;     /* no need to add that one again */
1553             }
1554         }
1555     }
1556 #endif
1557 }
1558
1559 void
1560 transaction_order(Transaction *trans, int flags)
1561 {
1562   Pool *pool = trans->pool;
1563   Queue *tr = &trans->steps;
1564   Repo *installed = pool->installed;
1565   Id p;
1566   Solvable *s;
1567   int i, j, k, numte, numedge;
1568   struct orderdata od;
1569   struct _TransactionElement *te;
1570   Queue todo, obsq, samerepoq, uninstq;
1571   int cycstart, cycel;
1572   Id *cycle;
1573   int oldcount;
1574   int start, now;
1575   Repo *lastrepo;
1576   int lastmedia;
1577   Id *temedianr;
1578
1579   start = now = solv_timems(0);
1580   POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1581   /* free old data if present */
1582   if (trans->orderdata)
1583     {
1584       struct _TransactionOrderdata *od = trans->orderdata;
1585       od->tes = solv_free(od->tes);
1586       od->invedgedata = solv_free(od->invedgedata);
1587       trans->orderdata = solv_free(trans->orderdata);
1588     }
1589
1590   /* create a transaction element for every active component */
1591   numte = 0;
1592   for (i = 0; i < tr->count; i++)
1593     {
1594       p = tr->elements[i];
1595       s = pool->solvables + p;
1596       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1597         continue;
1598       numte++;
1599     }
1600   POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1601   if (!numte)
1602     return;     /* nothing to do... */
1603
1604   numte++;      /* leave first one zero */
1605   memset(&od, 0, sizeof(od));
1606   od.trans = trans;
1607   od.ntes = numte;
1608   od.tes = solv_calloc(numte, sizeof(*od.tes));
1609   od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1610   od.edgedata[0] = 0;
1611   od.nedgedata = 1;
1612   queue_init(&od.cycles);
1613
1614   /* initialize TEs */
1615   for (i = 0, te = od.tes + 1; i < tr->count; i++)
1616     {
1617       p = tr->elements[i];
1618       s = pool->solvables + p;
1619       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1620         continue;
1621       te->p = p;
1622       te++;
1623     }
1624
1625   /* create dependency graph */
1626   for (i = 0; i < tr->count; i++)
1627     addsolvableedges(&od, pool->solvables + tr->elements[i]);
1628
1629   /* count edges */
1630   numedge = 0;
1631   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1632     for (j = te->edges; od.edgedata[j]; j += 2)
1633       numedge++;
1634   POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1635   POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1636
1637 #if 0
1638   dump_tes(&od);
1639 #endif
1640
1641   now = solv_timems(0);
1642   /* kill all cycles */
1643   queue_init(&todo);
1644   for (i = numte - 1; i > 0; i--)
1645     queue_push(&todo, i);
1646
1647   while (todo.count)
1648     {
1649       i = queue_pop(&todo);
1650       /* printf("- look at TE %d\n", i); */
1651       if (i < 0)
1652         {
1653           i = -i;
1654           od.tes[i].mark = 2;   /* done with that one */
1655           continue;
1656         }
1657       te = od.tes + i;
1658       if (te->mark == 2)
1659         continue;               /* already finished before */
1660       if (te->mark == 0)
1661         {
1662           int edgestovisit = 0;
1663           /* new node, visit edges */
1664           for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1665             {
1666               if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1667                 continue;
1668               if (od.tes[k].mark == 2)
1669                 continue;       /* no need to visit again */
1670               if (!edgestovisit++)
1671                 queue_push(&todo, -i);  /* end of edges marker */
1672               queue_push(&todo, k);
1673             }
1674           if (!edgestovisit)
1675             te->mark = 2;       /* no edges, done with that one */
1676           else
1677             te->mark = 1;       /* under investigation */
1678           continue;
1679         }
1680       /* oh no, we found a cycle */
1681       /* find start of cycle node (<0) */
1682       for (j = todo.count - 1; j >= 0; j--)
1683         if (todo.elements[j] == -i)
1684           break;
1685       assert(j >= 0);
1686       cycstart = j;
1687       /* build te/edge chain */
1688       k = cycstart;
1689       for (j = k; j < todo.count; j++)
1690         if (todo.elements[j] < 0)
1691           todo.elements[k++] = -todo.elements[j];
1692       cycel = k - cycstart;
1693       assert(cycel > 1);
1694       /* make room for edges, two extra element for cycle loop + terminating 0 */
1695       while (todo.count < cycstart + 2 * cycel + 2)
1696         queue_push(&todo, 0);
1697       cycle = todo.elements + cycstart;
1698       cycle[cycel] = i;         /* close the loop */
1699       cycle[2 * cycel + 1] = 0; /* terminator */
1700       for (k = cycel; k > 0; k--)
1701         {
1702           cycle[k * 2] = cycle[k];
1703           te = od.tes + cycle[k - 1];
1704           assert(te->mark == 1);
1705           te->mark = 0; /* reset investigation marker */
1706           /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1707           for (j = te->edges; od.edgedata[j]; j += 2)
1708             if (od.edgedata[j] == cycle[k])
1709               break;
1710           assert(od.edgedata[j]);
1711           cycle[k * 2 - 1] = j;
1712         }
1713       /* now cycle looks like this: */
1714       /* te1 edge te2 edge te3 ... teN edge te1 0 */
1715       breakcycle(&od, cycle);
1716       /* restart with start of cycle */
1717       todo.count = cycstart + 1;
1718     }
1719   POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1720   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1721
1722   now = solv_timems(0);
1723   /* now go through all broken cycles and create cycle edges to help
1724      the ordering */
1725    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1726      {
1727        if (od.cycles.elements[i + 2] >= TYPE_REQ)
1728          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1729      }
1730    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1731      {
1732        if (od.cycles.elements[i + 2] < TYPE_REQ)
1733          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1734      }
1735   POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1736
1737 #if 0
1738   dump_tes(&od);
1739 #endif
1740   /* all edges are finally set up and there are no cycles, now the easy part.
1741    * Create an ordered transaction */
1742   now = solv_timems(0);
1743   /* first invert all edges */
1744   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1745     te->mark = 1;       /* term 0 */
1746   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1747     {
1748       for (j = te->edges; od.edgedata[j]; j += 2)
1749         {
1750           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1751             continue;
1752           od.tes[od.edgedata[j]].mark++;
1753         }
1754     }
1755   j = 1;
1756   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1757     {
1758       te->mark += j;
1759       j = te->mark;
1760     }
1761   POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1762   od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1763   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1764     {
1765       for (j = te->edges; od.edgedata[j]; j += 2)
1766         {
1767           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1768             continue;
1769           od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1770         }
1771     }
1772   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1773     te->edges = te->mark;       /* edges now points into invedgedata */
1774   od.edgedata = solv_free(od.edgedata);
1775   od.nedgedata = j + 1;
1776
1777   /* now the final ordering */
1778   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1779     te->mark = 0;
1780   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1781     for (j = te->edges; od.invedgedata[j]; j++)
1782       od.tes[od.invedgedata[j]].mark++;
1783
1784   queue_init(&samerepoq);
1785   queue_init(&uninstq);
1786   queue_empty(&todo);
1787   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1788     if (te->mark == 0)
1789       {
1790         if (installed && pool->solvables[te->p].repo == installed)
1791           queue_push(&uninstq, i);
1792         else
1793           queue_push(&todo, i);
1794       }
1795   assert(todo.count > 0 || uninstq.count > 0);
1796   oldcount = tr->count;
1797   queue_empty(tr);
1798
1799   queue_init(&obsq);
1800
1801   lastrepo = 0;
1802   lastmedia = 0;
1803   temedianr = solv_calloc(numte, sizeof(Id));
1804   for (i = 1; i < numte; i++)
1805     {
1806       Solvable *s = pool->solvables + od.tes[i].p;
1807       if (installed && s->repo == installed)
1808         j = 1;
1809       else
1810         j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1811       temedianr[i] = j;
1812     }
1813   for (;;)
1814     {
1815       /* select an TE i */
1816       if (uninstq.count)
1817         i = queue_shift(&uninstq);
1818       else if (samerepoq.count)
1819         i = queue_shift(&samerepoq);
1820       else if (todo.count)
1821         {
1822           /* find next repo/media */
1823           for (j = 0; j < todo.count; j++)
1824             {
1825               if (!j || temedianr[todo.elements[j]] < lastmedia)
1826                 {
1827                   i = j;
1828                   lastmedia = temedianr[todo.elements[j]];
1829                 }
1830             }
1831           lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1832
1833           /* move all matching TEs to samerepoq */
1834           for (i = j = 0; j < todo.count; j++)
1835             {
1836               int k = todo.elements[j];
1837               if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1838                 queue_push(&samerepoq, k);
1839               else
1840                 todo.elements[i++] = k;
1841             }
1842           todo.count = i;
1843
1844           assert(samerepoq.count);
1845           i = queue_shift(&samerepoq);
1846         }
1847       else
1848         break;
1849
1850       te = od.tes + i;
1851       queue_push(tr, te->p);
1852 #if 0
1853 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1854 #endif
1855       s = pool->solvables + te->p;
1856       for (j = te->edges; od.invedgedata[j]; j++)
1857         {
1858           struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1859           assert(te2->mark > 0);
1860           if (--te2->mark == 0)
1861             {
1862               Solvable *s = pool->solvables + te2->p;
1863 #if 0
1864 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1865 #endif
1866               if (installed && s->repo == installed)
1867                 queue_push(&uninstq, od.invedgedata[j]);
1868               else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1869                 queue_push(&samerepoq, od.invedgedata[j]);
1870               else
1871                 queue_push(&todo, od.invedgedata[j]);
1872             }
1873         }
1874     }
1875   solv_free(temedianr);
1876   queue_free(&todo);
1877   queue_free(&samerepoq);
1878   queue_free(&uninstq);
1879   queue_free(&obsq);
1880   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1881     assert(te->mark == 0);
1882
1883   /* add back obsoleted packages */
1884   transaction_add_obsoleted(trans);
1885   assert(tr->count == oldcount);
1886
1887   POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1888   POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1889
1890   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1891     {
1892       trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1893       trans->orderdata->tes = od.tes;
1894       trans->orderdata->ntes = numte;
1895       trans->orderdata->invedgedata = od.invedgedata;
1896       trans->orderdata->ninvedgedata = od.nedgedata;
1897     }
1898   else
1899     {
1900       solv_free(od.tes);
1901       solv_free(od.invedgedata);
1902     }
1903   queue_free(&od.cycles);
1904   queue_free(&od.cyclesdata);
1905 }
1906
1907
1908 int
1909 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1910 {
1911   int i, j;
1912   struct _TransactionOrderdata *od = trans->orderdata;
1913   struct _TransactionElement *te;
1914
1915   if (!od)
1916      return choices->count;
1917   if (!chosen)
1918     {
1919       /* initialization step */
1920       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1921         te->mark = 0;
1922       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1923         {
1924           for (j = te->edges; od->invedgedata[j]; j++)
1925             od->tes[od->invedgedata[j]].mark++;
1926         }
1927       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1928         if (!te->mark)
1929           queue_push(choices, te->p);
1930       return choices->count;
1931     }
1932   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1933     if (te->p == chosen)
1934       break;
1935   if (i == od->ntes)
1936     return choices->count;
1937   if (te->mark > 0)
1938     {
1939       /* hey! out-of-order installation! */
1940       te->mark = -1;
1941     }
1942   for (j = te->edges; od->invedgedata[j]; j++)
1943     {
1944       te = od->tes + od->invedgedata[j];
1945       assert(te->mark > 0 || te->mark == -1);
1946       if (te->mark > 0 && --te->mark == 0)
1947         queue_push(choices, te->p);
1948     }
1949   return choices->count;
1950 }
1951
1952 void
1953 transaction_add_obsoleted(Transaction *trans)
1954 {
1955   Pool *pool = trans->pool;
1956   Repo *installed = pool->installed;
1957   Id p;
1958   Solvable *s;
1959   int i, j, k, max;
1960   Map done;
1961   Queue obsq, *steps;
1962
1963   if (!installed || !trans->steps.count)
1964     return;
1965   /* calculate upper bound */
1966   max = 0;
1967   FOR_REPO_SOLVABLES(installed, p, s)
1968     if (MAPTST(&trans->transactsmap, p))
1969       max++;
1970   if (!max)
1971     return;
1972   /* make room */
1973   steps = &trans->steps;
1974   queue_insertn(steps, 0, max, 0);
1975
1976   /* now add em */
1977   map_init(&done, installed->end - installed->start);
1978   queue_init(&obsq);
1979   for (j = 0, i = max; i < steps->count; i++)
1980     {
1981       p = trans->steps.elements[i];
1982       if (pool->solvables[p].repo == installed)
1983         {
1984           if (!trans->transaction_installed[p - pool->installed->start])
1985             trans->steps.elements[j++] = p;
1986           continue;
1987         }
1988       trans->steps.elements[j++] = p;
1989       queue_empty(&obsq);
1990       transaction_all_obs_pkgs(trans, p, &obsq);
1991       for (k = 0; k < obsq.count; k++)
1992         {
1993           p = obsq.elements[k];
1994           assert(p >= installed->start && p < installed->end);
1995           if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1996             continue;
1997           if (MAPTST(&done, p - installed->start))
1998             continue;
1999           MAPSET(&done, p - installed->start);
2000           trans->steps.elements[j++] = p;
2001         }
2002     }
2003
2004   /* free unneeded space */
2005   queue_truncate(steps, j);
2006   map_free(&done);
2007   queue_free(&obsq);
2008 }
2009
2010 static void
2011 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
2012 {
2013   Pool *pool = trans->pool;
2014   Id p, pp;
2015   Solvable *s;
2016   int good;
2017
2018   if (MAPTST(seen, pkg))
2019     return;
2020   MAPSET(seen, pkg);
2021   s = pool->solvables + pkg;
2022 #if 0
2023   printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2024 #endif
2025   if (s->requires)
2026     {
2027       Id req, *reqp;
2028       int inpre = 0;
2029       reqp = s->repo->idarraydata + s->requires;
2030       while ((req = *reqp++) != 0)
2031         {
2032           if (req == SOLVABLE_PREREQMARKER)
2033             {
2034               inpre = 1;
2035               continue;
2036             }
2037           if (onlyprereq && !inpre)
2038             continue;
2039           if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2040             continue;
2041           good = 0;
2042           /* first check kept packages, then freshly installed, then not yet uninstalled */
2043           FOR_PROVIDES(p, pp, req)
2044             {
2045               if (!MAPTST(ins, p))
2046                 continue;
2047               if (MAPTST(&trans->transactsmap, p))
2048                 continue;
2049               good++;
2050               transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2051             }
2052           if (!good)
2053             {
2054               FOR_PROVIDES(p, pp, req)
2055                 {
2056                   if (!MAPTST(ins, p))
2057                     continue;
2058                   if (pool->solvables[p].repo == pool->installed)
2059                     continue;
2060                   good++;
2061                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2062                 }
2063             }
2064           if (!good)
2065             {
2066               FOR_PROVIDES(p, pp, req)
2067                 {
2068                   if (!MAPTST(ins, p))
2069                     continue;
2070                   good++;
2071                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2072                 }
2073             }
2074           if (!good)
2075             {
2076               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));
2077             }
2078         }
2079     }
2080 }
2081
2082 void
2083 transaction_check_order(Transaction *trans)
2084 {
2085   Pool *pool = trans->pool;
2086   Solvable *s;
2087   Id p, lastins;
2088   Map ins, seen;
2089   int i;
2090
2091   POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
2092   map_init(&ins, pool->nsolvables);
2093   map_init(&seen, pool->nsolvables);
2094   if (pool->installed)
2095     FOR_REPO_SOLVABLES(pool->installed, p, s)
2096       MAPSET(&ins, p);
2097   lastins = 0;
2098   for (i = 0; i < trans->steps.count; i++)
2099     {
2100       p = trans->steps.elements[i];
2101       s = pool->solvables + p;
2102       if (s->repo != pool->installed)
2103         lastins = p;
2104       if (s->repo != pool->installed)
2105         MAPSET(&ins, p);
2106       if (havescripts(pool, p))
2107         {
2108           MAPZERO(&seen);
2109           transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2110         }
2111       if (s->repo == pool->installed)
2112         MAPCLR(&ins, p);
2113     }
2114   map_free(&seen);
2115   map_free(&ins);
2116   POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
2117 }