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