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