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