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