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