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