- also look at triggers when ordering packages
[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                       addedge(od, p2, p, TYPE_CON);
1232                     }
1233                 }
1234               else
1235                 {
1236                   if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1237                     {
1238                       /* deinstall p2 before installing p */
1239                       addedge(od, p, p2, TYPE_CON);
1240                     }
1241                 }
1242
1243             }
1244         }
1245     }
1246   if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1247     {
1248       /* we're getting deinstalled/updated. Try to do this before our
1249        * triggers are hit */
1250       for (i = 0; i < reqq.count; i++)
1251         {
1252           Id tri = reqq.elements[i];
1253           FOR_PROVIDES(p2, pp2, tri)
1254             {
1255               if (p2 == p)
1256                 continue;
1257               s2 = pool->solvables + p2;
1258               if (!s2->repo)
1259                 continue;
1260               if (s2->name == s->name)
1261                 continue;       /* obsoleted anyway */
1262               if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1263                 {
1264                   /* deinstall/update p before installing p2 */
1265 #if 0
1266                   printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, p2), dep2str(pool, tri), solvid2str(pool, p));
1267 #endif
1268                   addedge(od, p2, p, TYPE_CON);
1269                 }
1270             }
1271         }
1272     }
1273   queue_free(&reqq);
1274 }
1275
1276
1277 /* break an edge in a cycle */
1278 static void
1279 breakcycle(struct orderdata *od, Id *cycle)
1280 {
1281   Pool *pool = od->trans->pool;
1282   Id ddegmin, ddegmax, ddeg;
1283   int k, l;
1284   struct _TransactionElement *te;
1285
1286   l = 0;
1287   ddegmin = ddegmax = 0;
1288   for (k = 0; cycle[k + 1]; k += 2)
1289     {
1290       ddeg = od->edgedata[cycle[k + 1] + 1];
1291       if (ddeg > ddegmax)
1292         ddegmax = ddeg;
1293       if (!k || ddeg < ddegmin)
1294         {
1295           l = k;
1296           ddegmin = ddeg;
1297           continue;
1298         }
1299       if (ddeg == ddegmin)
1300         {
1301           if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1302             {
1303               /* prefer k, as l comes from a package with contains scriptlets */
1304               l = k;
1305               ddegmin = ddeg;
1306               continue;
1307             }
1308           /* same edge value, check for prereq */
1309         }
1310     }
1311
1312   /* record brkoen cycle starting with the tail */
1313   queue_push(&od->cycles, od->cyclesdata.count);                /* offset into data */
1314   queue_push(&od->cycles, k / 2);                               /* cycle elements */
1315   queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);      /* broken edge */
1316   od->ncycles++;
1317   for (k = l;;)
1318     {
1319       k += 2;
1320       if (!cycle[k + 1])
1321         k = 0;
1322       queue_push(&od->cyclesdata, cycle[k]);
1323       if (k == l)
1324         break;
1325     }
1326   queue_push(&od->cyclesdata, 0);       /* mark end */
1327
1328   /* break that edge */
1329   od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1330
1331 #if 1
1332   if (ddegmin < TYPE_REQ)
1333     return;
1334 #endif
1335
1336   /* cycle recorded, print it */
1337   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1338     POOL_DEBUG(SAT_DEBUG_STATS, "CRITICAL ");
1339   POOL_DEBUG(SAT_DEBUG_STATS, "cycle: --> ");
1340   for (k = 0; cycle[k + 1]; k += 2)
1341     {
1342       te = od->tes +  cycle[k];
1343       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1344         POOL_DEBUG(SAT_DEBUG_STATS, "%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1345       else
1346         POOL_DEBUG(SAT_DEBUG_STATS, "%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1347     }
1348   POOL_DEBUG(SAT_DEBUG_STATS, "\n");
1349 }
1350
1351 static inline void
1352 dump_tes(struct orderdata *od)
1353 {
1354   Pool *pool = od->trans->pool;
1355   int i, j;
1356   Queue obsq;
1357   struct _TransactionElement *te, *te2;
1358
1359   queue_init(&obsq);
1360   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1361     {
1362       Solvable *s = pool->solvables + te->p;
1363       POOL_DEBUG(SAT_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1364       if (s->repo != pool->installed)
1365         {
1366           queue_empty(&obsq);
1367           transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1368           for (j = 0; j < obsq.count; j++)
1369             POOL_DEBUG(SAT_DEBUG_RESULT, "         -%s\n", solvid2str(pool, obsq.elements[j]));
1370         }
1371       for (j = te->edges; od->edgedata[j]; j += 2)
1372         {
1373           te2 = od->tes + od->edgedata[j];
1374           if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1375             POOL_DEBUG(SAT_DEBUG_RESULT, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
1376           else
1377             POOL_DEBUG(SAT_DEBUG_RESULT, "       ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
1378         }
1379     }
1380 }
1381
1382 #if 1
1383 static void
1384 reachable(struct orderdata *od, Id i)
1385 {
1386   struct _TransactionElement *te = od->tes + i;
1387   int j, k;
1388
1389   if (te->mark != 0)
1390     return;
1391   te->mark = 1;
1392   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1393     {
1394       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1395         continue;
1396       if (!od->tes[k].mark)
1397         reachable(od, k);
1398       if (od->tes[k].mark == 2)
1399         {
1400           te->mark = 2;
1401           return;
1402         }
1403     }
1404   te->mark = -1;
1405 }
1406 #endif
1407
1408 static void
1409 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1410 {
1411 #if 0
1412   Transaction *trans = od->trans;
1413   Pool *pool = trans->pool;
1414 #endif
1415   struct _TransactionElement *te;
1416   int i, j, k, tail;
1417 #if 1
1418   int head;
1419 #endif
1420
1421 #if 0
1422   printf("addcycleedges\n");
1423   for (i = 0; (j = cycle[i]) != 0; i++)
1424     printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
1425 #endif
1426
1427   /* first add all the tail cycle edges */
1428
1429   /* see what we can reach from the cycle */
1430   queue_empty(todo);
1431   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1432     te->mark = 0;
1433   for (i = 0; (j = cycle[i]) != 0; i++)
1434     {
1435       od->tes[j].mark = -1;
1436       queue_push(todo, j);
1437     }
1438   while (todo->count)
1439     {
1440       i = queue_pop(todo);
1441       te = od->tes + i;
1442       if (te->mark > 0)
1443         continue;
1444       te->mark = te->mark < 0 ? 2 : 1;
1445       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1446         {
1447           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1448             continue;
1449           if (od->tes[k].mark > 0)
1450             continue;   /* no need to visit again */
1451           queue_push(todo, k);
1452         }
1453     }
1454   /* now all cycle TEs are marked with 2, all TEs reachable
1455    * from the cycle are marked with 1 */
1456   tail = cycle[0];
1457   od->tes[tail].mark = 1;       /* no need to add edges */
1458
1459   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1460     {
1461       if (te->mark)
1462         continue;       /* reachable from cycle */
1463       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1464         {
1465           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1466             continue;
1467           if (od->tes[k].mark != 2)
1468             continue;
1469           /* We found an edge to the cycle. Add an extra edge to the tail */
1470           /* the TE was not reachable, so we're not creating a new cycle! */
1471 #if 0
1472           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));
1473 #endif
1474           j -= te->edges;       /* in case we move */
1475           addteedge(od, i, tail, TYPE_CYCLETAIL);
1476           j += te->edges;
1477           break;        /* one edge is enough */
1478         }
1479     }
1480
1481 #if 1
1482   /* now add all head cycle edges */
1483
1484   /* reset marks */
1485   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1486     te->mark = 0;
1487   head = 0;
1488   for (i = 0; (j = cycle[i]) != 0; i++)
1489     {
1490       head = j;
1491       od->tes[j].mark = 2;
1492     }
1493   /* first the head to save some time */
1494   te = od->tes + head;
1495   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1496     {
1497       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1498         continue;
1499       if (!od->tes[k].mark)
1500         reachable(od, k);
1501       if (od->tes[k].mark == -1)
1502         od->tes[k].mark = -2;   /* no need for another edge */
1503     }
1504   for (i = 0; cycle[i] != 0; i++)
1505     {
1506       if (cycle[i] == head)
1507         break;
1508       te = od->tes + cycle[i];
1509       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1510         {
1511           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1512             continue;
1513           /* see if we can reach a cycle TE from k */
1514           if (!od->tes[k].mark)
1515             reachable(od, k);
1516           if (od->tes[k].mark == -1)
1517             {
1518 #if 0
1519               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));
1520 #endif
1521               addteedge(od, head, k, TYPE_CYCLEHEAD);
1522               od->tes[k].mark = -2;     /* no need to add that one again */
1523             }
1524         }
1525     }
1526 #endif
1527 }
1528
1529 void
1530 transaction_order(Transaction *trans, int flags)
1531 {
1532   Pool *pool = trans->pool;
1533   Queue *tr = &trans->steps;
1534   Repo *installed = pool->installed;
1535   Id p;
1536   Solvable *s;
1537   int i, j, k, numte, numedge;
1538   struct orderdata od;
1539   struct _TransactionElement *te;
1540   Queue todo, obsq, samerepoq, uninstq;
1541   int cycstart, cycel;
1542   Id *cycle;
1543   int oldcount;
1544   int start, now;
1545   Repo *lastrepo;
1546   int lastmedia;
1547   Id *temedianr;
1548
1549   start = now = sat_timems(0);
1550   POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1551   /* free old data if present */
1552   if (trans->orderdata)
1553     {
1554       struct _TransactionOrderdata *od = trans->orderdata;
1555       od->tes = sat_free(od->tes);
1556       od->invedgedata = sat_free(od->invedgedata);
1557       trans->orderdata = sat_free(trans->orderdata);
1558     }
1559
1560   /* create a transaction element for every active component */
1561   numte = 0;
1562   for (i = 0; i < tr->count; i++)
1563     {
1564       p = tr->elements[i];
1565       s = pool->solvables + p;
1566       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1567         continue;
1568       numte++;
1569     }
1570   POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1571   if (!numte)
1572     return;     /* nothing to do... */
1573
1574   numte++;      /* leave first one zero */
1575   memset(&od, 0, sizeof(od));
1576   od.trans = trans;
1577   od.ntes = numte;
1578   od.tes = sat_calloc(numte, sizeof(*od.tes));
1579   od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1580   od.edgedata[0] = 0;
1581   od.nedgedata = 1;
1582   queue_init(&od.cycles);
1583
1584   /* initialize TEs */
1585   for (i = 0, te = od.tes + 1; i < tr->count; i++)
1586     {
1587       p = tr->elements[i];
1588       s = pool->solvables + p;
1589       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1590         continue;
1591       te->p = p;
1592       te++;
1593     }
1594
1595   /* create dependency graph */
1596   for (i = 0; i < tr->count; i++)
1597     addsolvableedges(&od, pool->solvables + tr->elements[i]);
1598
1599   /* count edges */
1600   numedge = 0;
1601   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1602     for (j = te->edges; od.edgedata[j]; j += 2)
1603       numedge++;
1604   POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1605   POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1606
1607 #if 0
1608   dump_tes(&od);
1609 #endif
1610   
1611   now = sat_timems(0);
1612   /* kill all cycles */
1613   queue_init(&todo);
1614   for (i = numte - 1; i > 0; i--)
1615     queue_push(&todo, i);
1616
1617   while (todo.count)
1618     {
1619       i = queue_pop(&todo);
1620       /* printf("- look at TE %d\n", i); */
1621       if (i < 0)
1622         {
1623           i = -i;
1624           od.tes[i].mark = 2;   /* done with that one */
1625           continue;
1626         }
1627       te = od.tes + i;
1628       if (te->mark == 2)
1629         continue;               /* already finished before */
1630       if (te->mark == 0)
1631         {
1632           int edgestovisit = 0;
1633           /* new node, visit edges */
1634           for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1635             {
1636               if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1637                 continue;
1638               if (od.tes[k].mark == 2)
1639                 continue;       /* no need to visit again */
1640               if (!edgestovisit++)
1641                 queue_push(&todo, -i);  /* end of edges marker */
1642               queue_push(&todo, k);
1643             }
1644           if (!edgestovisit)
1645             te->mark = 2;       /* no edges, done with that one */
1646           else
1647             te->mark = 1;       /* under investigation */
1648           continue;
1649         }
1650       /* oh no, we found a cycle */
1651       /* find start of cycle node (<0) */
1652       for (j = todo.count - 1; j >= 0; j--)
1653         if (todo.elements[j] == -i)
1654           break;
1655       assert(j >= 0);
1656       cycstart = j;
1657       /* build te/edge chain */
1658       k = cycstart;
1659       for (j = k; j < todo.count; j++)
1660         if (todo.elements[j] < 0)
1661           todo.elements[k++] = -todo.elements[j];
1662       cycel = k - cycstart;
1663       assert(cycel > 1);
1664       /* make room for edges, two extra element for cycle loop + terminating 0 */
1665       while (todo.count < cycstart + 2 * cycel + 2)
1666         queue_push(&todo, 0);
1667       cycle = todo.elements + cycstart;
1668       cycle[cycel] = i;         /* close the loop */
1669       cycle[2 * cycel + 1] = 0; /* terminator */
1670       for (k = cycel; k > 0; k--)
1671         {
1672           cycle[k * 2] = cycle[k];
1673           te = od.tes + cycle[k - 1];
1674           assert(te->mark == 1);
1675           te->mark = 0; /* reset investigation marker */
1676           /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1677           for (j = te->edges; od.edgedata[j]; j += 2)
1678             if (od.edgedata[j] == cycle[k])
1679               break;
1680           assert(od.edgedata[j]);
1681           cycle[k * 2 - 1] = j;
1682         }
1683       /* now cycle looks like this: */
1684       /* te1 edge te2 edge te3 ... teN edge te1 0 */
1685       breakcycle(&od, cycle);
1686       /* restart with start of cycle */
1687       todo.count = cycstart + 1;
1688     }
1689   POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1690   POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1691
1692   now = sat_timems(0);
1693   /* now go through all broken cycles and create cycle edges to help
1694      the ordering */
1695    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1696      {
1697        if (od.cycles.elements[i + 2] >= TYPE_REQ)
1698          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1699      }
1700    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1701      {
1702        if (od.cycles.elements[i + 2] < TYPE_REQ)
1703          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1704      }
1705   POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1706
1707 #if 0
1708   dump_tes(&od);
1709 #endif
1710   /* all edges are finally set up and there are no cycles, now the easy part.
1711    * Create an ordered transaction */
1712   now = sat_timems(0);
1713   /* first invert all edges */
1714   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1715     te->mark = 1;       /* term 0 */
1716   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1717     {
1718       for (j = te->edges; od.edgedata[j]; j += 2)
1719         {
1720           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1721             continue;
1722           od.tes[od.edgedata[j]].mark++;
1723         }
1724     }
1725   j = 1;
1726   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1727     {
1728       te->mark += j;
1729       j = te->mark;
1730     }
1731   POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1732   od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1733   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1734     {
1735       for (j = te->edges; od.edgedata[j]; j += 2)
1736         {
1737           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1738             continue;
1739           od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1740         }
1741     }
1742   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1743     te->edges = te->mark;       /* edges now points into invedgedata */
1744   od.edgedata = sat_free(od.edgedata);
1745   od.nedgedata = j + 1;
1746
1747   /* now the final ordering */
1748   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1749     te->mark = 0;
1750   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1751     for (j = te->edges; od.invedgedata[j]; j++)
1752       od.tes[od.invedgedata[j]].mark++;
1753
1754   queue_init(&samerepoq);
1755   queue_init(&uninstq);
1756   queue_empty(&todo);
1757   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1758     if (te->mark == 0)
1759       {
1760         if (installed && pool->solvables[te->p].repo == installed)
1761           queue_push(&uninstq, i);
1762         else
1763           queue_push(&todo, i);
1764       }
1765   assert(todo.count > 0 || uninstq.count > 0);
1766   oldcount = tr->count;
1767   queue_empty(tr);
1768
1769   queue_init(&obsq);
1770   
1771   lastrepo = 0;
1772   lastmedia = 0;
1773   temedianr = sat_calloc(numte, sizeof(Id));
1774   for (i = 1; i < numte; i++)
1775     {
1776       Solvable *s = pool->solvables + od.tes[i].p;
1777       if (installed && s->repo == installed)
1778         j = 1;
1779       else
1780         j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1781       temedianr[i] = j;
1782     }
1783   for (;;)
1784     {
1785       /* select an TE i */
1786       if (uninstq.count)
1787         i = queue_shift(&uninstq);
1788       else if (samerepoq.count)
1789         i = queue_shift(&samerepoq);
1790       else if (todo.count)
1791         {
1792           /* find next repo/media */
1793           for (j = 0; j < todo.count; j++)
1794             {
1795               if (!j || temedianr[todo.elements[j]] < lastmedia)
1796                 {
1797                   i = j;
1798                   lastmedia = temedianr[todo.elements[j]];
1799                 }
1800             }
1801           lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1802
1803           /* move all matching TEs to samerepoq */
1804           for (i = j = 0; j < todo.count; j++)
1805             {
1806               int k = todo.elements[j];
1807               if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1808                 queue_push(&samerepoq, k);
1809               else
1810                 todo.elements[i++] = k;
1811             }
1812           todo.count = i;
1813
1814           assert(samerepoq.count);
1815           i = queue_shift(&samerepoq);
1816         }
1817       else
1818         break;
1819
1820       te = od.tes + i;
1821       queue_push(tr, te->p);
1822 #if 0
1823 printf("do %s [%d]\n", solvid2str(pool, te->p), temedianr[i]);
1824 #endif
1825       s = pool->solvables + te->p;
1826       for (j = te->edges; od.invedgedata[j]; j++)
1827         {
1828           struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1829           assert(te2->mark > 0);
1830           if (--te2->mark == 0)
1831             {
1832               Solvable *s = pool->solvables + te2->p;
1833 #if 0
1834 printf("free %s [%d]\n", solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1835 #endif
1836               if (installed && s->repo == installed)
1837                 queue_push(&uninstq, od.invedgedata[j]);
1838               else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1839                 queue_push(&samerepoq, od.invedgedata[j]);
1840               else
1841                 queue_push(&todo, od.invedgedata[j]);
1842             }
1843         }
1844     }
1845   sat_free(temedianr);
1846   queue_free(&todo);
1847   queue_free(&samerepoq);
1848   queue_free(&uninstq);
1849   queue_free(&obsq);
1850   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1851     assert(te->mark == 0);
1852
1853   /* add back obsoleted packages */
1854   transaction_add_obsoleted(trans);
1855   assert(tr->count == oldcount);
1856
1857   POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1858   POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1859
1860   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1861     {
1862       trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1863       trans->orderdata->tes = od.tes;
1864       trans->orderdata->ntes = numte;
1865       trans->orderdata->invedgedata = od.invedgedata;
1866       trans->orderdata->ninvedgedata = od.nedgedata;
1867     }
1868   else
1869     {
1870       sat_free(od.tes);
1871       sat_free(od.invedgedata);
1872     }
1873 }
1874
1875
1876 int
1877 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1878 {
1879   int i, j;
1880   struct _TransactionOrderdata *od = trans->orderdata;
1881   struct _TransactionElement *te;
1882
1883   if (!od)
1884      return choices->count;
1885   if (!chosen)
1886     {
1887       /* initialization step */
1888       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1889         te->mark = 0;
1890       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1891         {
1892           for (j = te->edges; od->invedgedata[j]; j++)
1893             od->tes[od->invedgedata[j]].mark++;
1894         }
1895       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1896         if (!te->mark)
1897           queue_push(choices, te->p);
1898       return choices->count;
1899     }
1900   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1901     if (te->p == chosen)
1902       break;
1903   if (i == od->ntes)
1904     return choices->count;
1905   if (te->mark > 0)
1906     {
1907       /* hey! out-of-order installation! */
1908       te->mark = -1;
1909     }
1910   for (j = te->edges; od->invedgedata[j]; j++)
1911     {
1912       te = od->tes + od->invedgedata[j];
1913       assert(te->mark > 0 || te->mark == -1);
1914       if (te->mark > 0 && --te->mark == 0)
1915         queue_push(choices, te->p);
1916     }
1917   return choices->count;
1918 }
1919
1920 void
1921 transaction_add_obsoleted(Transaction *trans)
1922 {
1923   Pool *pool = trans->pool;
1924   Repo *installed = pool->installed;
1925   Id p;
1926   Solvable *s;
1927   int i, j, k, max, oldcount;
1928   Map done;
1929   Queue obsq, *steps;
1930
1931   if (!installed || !trans->steps.count)
1932     return;
1933   /* calculate upper bound */
1934   max = 0;
1935   FOR_REPO_SOLVABLES(installed, p, s)
1936     if (MAPTST(&trans->transactsmap, p))
1937       max++;
1938   if (!max)
1939     return;
1940   /* make room */
1941   steps = &trans->steps;
1942   oldcount = steps->count;
1943   queue_insertn(steps, 0, max);
1944
1945   /* now add em */
1946   map_init(&done, installed->end - installed->start);
1947   queue_init(&obsq);
1948   for (j = 0, i = max; i < steps->count; i++)
1949     {
1950       p = trans->steps.elements[i];
1951       if (pool->solvables[p].repo == installed)
1952         {
1953           if (!trans->transaction_installed[p - pool->installed->start])
1954             trans->steps.elements[j++] = p;
1955           continue;
1956         }
1957       trans->steps.elements[j++] = p;
1958       queue_empty(&obsq);
1959       transaction_all_obs_pkgs(trans, p, &obsq);
1960       for (k = 0; k < obsq.count; k++)
1961         {
1962           p = obsq.elements[k];
1963           assert(p >= installed->start && p < installed->end);
1964           if (MAPTST(&done, p - installed->start))
1965             continue;
1966           MAPSET(&done, p - installed->start);
1967           trans->steps.elements[j++] = p;
1968         }
1969     }
1970
1971   /* free unneeded space */
1972   queue_truncate(steps, j);
1973   map_free(&done);
1974   queue_free(&obsq);
1975 }
1976
1977 static void
1978 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1979 {
1980   Pool *pool = trans->pool;
1981   Id p, pp;
1982   Solvable *s;
1983   int good;
1984
1985   if (MAPTST(seen, pkg))
1986     return;
1987   MAPSET(seen, pkg);
1988   s = pool->solvables + pkg;
1989 #if 0
1990   printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1991 #endif
1992   if (s->requires)
1993     {
1994       Id req, *reqp;
1995       int inpre = 0;
1996       reqp = s->repo->idarraydata + s->requires;
1997       while ((req = *reqp++) != 0)
1998         {
1999           if (req == SOLVABLE_PREREQMARKER)
2000             {
2001               inpre = 1;
2002               continue;
2003             }
2004           if (onlyprereq && !inpre)
2005             continue;
2006           if (!strncmp(id2str(pool, req), "rpmlib(", 7))
2007             continue;
2008           good = 0;
2009           /* first check kept packages, then freshly installed, then not yet uninstalled */
2010           FOR_PROVIDES(p, pp, req)
2011             {
2012               if (!MAPTST(ins, p))
2013                 continue;
2014               if (MAPTST(&trans->transactsmap, p))
2015                 continue;
2016               good++;
2017               transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2018             }
2019           if (!good)
2020             {
2021               FOR_PROVIDES(p, pp, req)
2022                 {
2023                   if (!MAPTST(ins, p))
2024                     continue;
2025                   if (pool->solvables[p].repo == pool->installed)
2026                     continue;
2027                   good++;
2028                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2029                 }
2030             }
2031           if (!good)
2032             {
2033               FOR_PROVIDES(p, pp, req)
2034                 {
2035                   if (!MAPTST(ins, p))
2036                     continue;
2037                   good++;
2038                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2039                 }
2040             }
2041           if (!good)
2042             {
2043               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));
2044             }
2045         }
2046     }
2047 }
2048
2049 void
2050 transaction_check_order(Transaction *trans)
2051 {
2052   Pool *pool = trans->pool;
2053   Solvable *s;
2054   Id p, lastins;
2055   Map ins, seen;
2056   int i;
2057
2058   POOL_DEBUG(SAT_WARN, "\nchecking transaction order...\n");
2059   map_init(&ins, pool->nsolvables);
2060   map_init(&seen, pool->nsolvables);
2061   if (pool->installed)
2062     FOR_REPO_SOLVABLES(pool->installed, p, s)
2063       MAPSET(&ins, p);
2064   lastins = 0;
2065   for (i = 0; i < trans->steps.count; i++)
2066     {
2067       p = trans->steps.elements[i];
2068       s = pool->solvables + p;
2069       if (s->repo != pool->installed)
2070         lastins = p;
2071       if (s->repo != pool->installed)
2072         MAPSET(&ins, p);
2073       if (havescripts(pool, p))
2074         {
2075           MAPZERO(&seen);
2076           transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2077         }
2078       if (s->repo == pool->installed)
2079         MAPCLR(&ins, p);
2080     }
2081   map_free(&seen);
2082   map_free(&ins);
2083   POOL_DEBUG(SAT_WARN, "transaction order check done.\n");
2084 }