Merge branch 'master' of git@git.opensuse.org:projects/zypp/sat-solver
[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 "evr.h"
25 #include "util.h"
26
27 static int
28 obsq_sortcmp(const void *ap, const void *bp, void *dp)
29 {
30   Id a, b, oa, ob;
31   Pool *pool = dp;
32   Solvable *s, *oas, *obs;
33   int r;
34
35   a = ((Id *)ap)[0];
36   oa = ((Id *)ap)[1];
37   b = ((Id *)bp)[0];
38   ob = ((Id *)bp)[1];
39   if (a != b)
40     return a - b;
41   if (oa == ob)
42     return 0;
43   s = pool->solvables + a;
44   oas = pool->solvables + oa;
45   obs = pool->solvables + ob;
46   if (oas->name != obs->name)
47     {
48       if (oas->name == s->name)
49         return -1;
50       if (obs->name == s->name)
51         return 1;
52       return strcmp(id2str(pool, oas->name), id2str(pool, obs->name));
53     }
54   r = evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
55   if (r)
56     return -r;  /* highest version first */
57   return oa - ob;
58 }
59
60 void
61 solver_transaction_all_pkgs(Transaction *trans, Id p, Queue *pkgs)
62 {
63   Pool *pool = trans->pool;
64   Solvable *s = pool->solvables + p;
65   Queue *ti = &trans->transaction_info;
66   Id q;
67   int i;
68
69   queue_empty(pkgs);
70   if (p <= 0 || !s->repo)
71     return;
72   if (s->repo == pool->installed)
73     {
74       q = trans->transaction_installed[p - pool->installed->start];
75       if (!q)
76         return;
77       if (q > 0)
78         {
79           queue_push(pkgs, q);
80           return;
81         }
82       /* find which packages obsolete us */
83       for (i = 0; i < ti->count; i += 2)
84         if (ti->elements[i + 1] == p)
85           {
86             queue_push(pkgs, p);
87             queue_push(pkgs, ti->elements[i]);
88           }
89       /* sort obsoleters */
90       if (pkgs->count > 2)
91         sat_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
92       for (i = 0; i < pkgs->count; i += 2)
93         pkgs->elements[i / 2] = pkgs->elements[i + 1];
94       pkgs->count /= 2;
95     }
96   else
97     {
98       /* find the packages we obsolete */
99       for (i = 0; i < ti->count; i += 2)
100         {
101           if (ti->elements[i] == p)
102             queue_push(pkgs, ti->elements[i + 1]);
103           else if (pkgs->count)
104             break;
105         }
106     }
107 }
108
109 Id
110 solver_transaction_pkg(Transaction *trans, Id p)
111 {
112   Pool *pool = trans->pool;
113   Solvable *s = pool->solvables + p;
114   Queue ti;
115   Id tibuf[5];
116
117   if (p <= 0 || !s->repo)
118     return 0;
119   if (s->repo == pool->installed)
120     {
121       p = trans->transaction_installed[p - pool->installed->start];
122       return p < 0 ? -p : p;
123     }
124   queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
125   solver_transaction_all_pkgs(trans, p, &ti);
126   p = ti.count ? ti.elements[0] : 0;
127   queue_free(&ti);
128   return p;
129 }
130
131 /* type filtering, needed if either not all packages are shown
132  * or replaces are not shown, as otherwise parts of the
133  * transaction might not be shown to the user */
134
135 Id
136 solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
137 {
138   Pool *pool = trans->pool;
139   Solvable *s = pool->solvables + p;
140   Queue oq, rq;
141   Id q;
142   int i, j, ref = 0;
143
144   if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
145     return type;
146
147   if (s->repo == pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
148     {
149       /* erase element */
150       if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACED)
151         type = SOLVER_TRANSACTION_ERASE;
152       return type;
153     }
154   if (s->repo != pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
155     {
156       if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACE)
157         type = SOLVER_TRANSACTION_INSTALL;
158       return type;
159     }
160
161   /* most of the time there's only one reference, so check it first */
162   q = solver_transaction_pkg(trans, p);
163   if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
164     {
165       Solvable *sq = pool->solvables + q;
166       if (sq->name != s->name)
167         {
168           if (s->repo == pool->installed)
169             return SOLVER_TRANSACTION_ERASE;
170           else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
171             return SOLVER_TRANSACTION_MULTIINSTALL;
172           else
173             return SOLVER_TRANSACTION_INSTALL;
174         }
175     }
176   if (solver_transaction_pkg(trans, q) == p)
177     return type;
178
179   /* too bad, a miss. check em all */
180   queue_init(&oq);
181   queue_init(&rq);
182   solver_transaction_all_pkgs(trans, p, &oq);
183   for (i = 0; i < oq.count; i++)
184     {
185       q = oq.elements[i];
186       if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
187         {
188           Solvable *sq = pool->solvables + q;
189           if (sq->name != s->name)
190             continue;
191         }
192       /* check if we are referenced? */
193       if ((flags & SOLVER_TRANSACTION_SHOW_ALL) != 0)
194         {
195           solver_transaction_all_pkgs(trans, q, &rq);
196           for (j = 0; j < rq.count; j++)
197             if (rq.elements[j] == p)
198               {
199                 ref = 1;
200                 break;
201               }
202           if (ref)
203             break;
204         }
205       else if (solver_transaction_pkg(trans, q) == p)
206         {
207           ref = 1;
208           break;
209         }
210     }
211   queue_free(&oq);
212   queue_free(&rq);
213
214   if (!ref)
215     {
216       if (s->repo == pool->installed)
217         type = SOLVER_TRANSACTION_ERASE;
218       else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
219         type = SOLVER_TRANSACTION_MULTIINSTALL;
220       else
221         type = SOLVER_TRANSACTION_INSTALL;
222     }
223   return type;
224 }
225
226 static void
227 create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
228 {
229   Pool *pool = trans->pool;
230   Queue *ti = &trans->transaction_info;
231   Repo *installed = pool->installed;
232   int i, j, noobs;
233   Id p, p2, pp2;
234   Solvable *s, *s2;
235
236   queue_empty(ti);
237   if (!installed)
238     return;     /* no info needed */
239   for (i = 0; i < decisionq->count; i++)
240     {
241       p = decisionq->elements[i];
242       if (p <= 0 || p == SYSTEMSOLVABLE)
243         continue;
244       s = pool->solvables + p;
245       if (s->repo == installed)
246         continue;
247       noobs = noobsmap && MAPTST(noobsmap, p);
248       FOR_PROVIDES(p2, pp2, s->name)
249         {
250           if (!MAPTST(&trans->transactsmap, p2))
251             continue;
252           s2 = pool->solvables + p2;
253           if (s2->repo != installed)
254             continue;
255           if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
256             continue;
257           if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
258             continue;
259           queue_push(ti, p);
260           queue_push(ti, p2);
261         }
262       if (s->obsoletes && !noobs)
263         {
264           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
265           while ((obs = *obsp++) != 0)
266             {
267               FOR_PROVIDES(p2, pp2, obs)
268                 {
269                   s2 = pool->solvables + p2;
270                   if (s2->repo != installed)
271                     continue;
272                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
273                     continue;
274                   queue_push(ti, p);
275                   queue_push(ti, p2);
276                 }
277             }
278         }
279     }
280   sat_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
281   /* now unify */
282   for (i = j = 0; i < ti->count; i += 2)
283     {
284       if (j && ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
285         continue;
286       ti->elements[j++] = ti->elements[i];
287       ti->elements[j++] = ti->elements[i + 1];
288     }
289   ti->count = j;
290
291   /* create transaction_installed helper */
292   trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
293   for (i = 0; i < ti->count; i += 2)
294     {
295       j = ti->elements[i + 1] - installed->start;
296       if (!trans->transaction_installed[j])
297         trans->transaction_installed[j] = ti->elements[i];
298       else
299         {
300           /* more than one package obsoletes us. compare */
301           Id q[4];
302           if (trans->transaction_installed[j] > 0)
303             trans->transaction_installed[j] = -trans->transaction_installed[j];
304           q[0] = q[2] = ti->elements[i + 1];
305           q[1] = ti->elements[i];
306           q[3] = -trans->transaction_installed[j];
307           if (obsq_sortcmp(q, q + 2, pool) < 0)
308             trans->transaction_installed[j] = -ti->elements[i];
309         }
310     }
311 }
312
313 void
314 transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
315 {
316   Pool *pool = trans->pool;
317   Repo *installed = pool->installed;
318   int i, r, noobs;
319   Id p, p2;
320   Solvable *s, *s2;
321
322   if (noobsmap && !noobsmap->size)
323     noobsmap = 0;       /* ignore empty map */
324   queue_empty(&trans->steps);
325   map_init(&trans->transactsmap, pool->nsolvables);
326   for (i = 0; i < decisionq->count; i++)
327     {
328       p = decisionq->elements[i];
329       s = pool->solvables + (p > 0 ? p : -p);
330       if (!s->repo)
331         continue;
332       if (installed && s->repo == installed && p < 0)
333         MAPSET(&trans->transactsmap, -p);
334       if ((!installed || s->repo != installed) && p > 0)
335         MAPSET(&trans->transactsmap, p);
336     }
337   create_transaction_info(trans, decisionq, noobsmap);
338
339   if (installed)
340     {
341       FOR_REPO_SOLVABLES(installed, p, s)
342         {
343           if (!MAPTST(&trans->transactsmap, p))
344             continue;
345           p2 = solver_transaction_pkg(trans, p);
346           if (!p2)
347             queue_push(&trans->steps, SOLVER_TRANSACTION_ERASE);
348           else
349             {
350               s2 = pool->solvables + p2;
351               if (s->name == s2->name)
352                 {
353                   if (s->evr == s2->evr && solvable_identical(s, s2))
354                     queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALLED);
355                   else
356                     {
357                       r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
358                       if (r < 0)
359                         queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADED);
360                       else if (r > 0)
361                         queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADED);
362                       else
363                         queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGED);
364                     }
365                 }
366               else
367                 queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACED);
368             }
369           queue_push(&trans->steps, p);
370         }
371     }
372   for (i = 0; i < decisionq->count; i++)
373     {
374       p = decisionq->elements[i];
375       if (p < 0 || p == SYSTEMSOLVABLE)
376         continue;
377       s = pool->solvables + p;
378       if (installed && s->repo == installed)
379         continue;
380       noobs = noobsmap && MAPTST(noobsmap, p);
381       p2 = solver_transaction_pkg(trans, p);
382       if (noobs)
383         queue_push(&trans->steps, p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL);
384       else if (!p2)
385         queue_push(&trans->steps, SOLVER_TRANSACTION_INSTALL);
386       else
387         {
388           s2 = pool->solvables + p2;
389           if (s->name == s2->name)
390             {
391               if (s->evr == s2->evr && solvable_identical(s, s2))
392                 queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALL);
393               else
394                 {
395                   r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
396                   if (r > 0)
397                     queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADE);
398                   else if (r < 0)
399                     queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADE);
400                   else
401                     queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGE);
402                 }
403             }
404           else
405             queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACE);
406         }
407       queue_push(&trans->steps, p);
408     }
409 }
410
411
412 struct _TransactionElement {
413   Id p;         /* solvable id */
414   Id type;      /* installation type */
415   Id edges;     /* pointer into edges data */
416   Id mark;
417 };
418
419 struct _TransactionOrderdata {
420   struct _TransactionElement *tes;
421   int ntes;
422   Id *invedgedata;
423 };
424
425 #define TYPE_BROKEN     (1<<0)
426 #define TYPE_CON        (1<<1)
427
428 #define TYPE_REQ_P      (1<<2)
429 #define TYPE_PREREQ_P   (1<<3)
430
431 #define TYPE_REQ        (1<<4)
432 #define TYPE_PREREQ     (1<<5)
433
434 #define TYPE_CYCLETAIL  (1<<16)
435 #define TYPE_CYCLEHEAD  (1<<17)
436
437 #define EDGEDATA_BLOCK  127
438
439 void
440 transaction_init(Transaction *trans, Pool *pool)
441 {
442   memset(trans, 0, sizeof(*trans));
443   trans->pool = pool;
444 }
445
446 void
447 transaction_free(Transaction *trans)
448 {
449   queue_free(&trans->steps);
450   queue_free(&trans->transaction_info);
451   trans->transaction_installed = sat_free(trans->transaction_installed);
452   map_free(&trans->transactsmap);
453   if (trans->orderdata)
454     {
455       struct _TransactionOrderdata *od = trans->orderdata;
456       od->tes = sat_free(od->tes);
457       od->invedgedata = sat_free(od->invedgedata);
458       trans->orderdata = sat_free(trans->orderdata);
459     }
460 }
461
462 struct orderdata {
463   Transaction *trans;
464   struct _TransactionElement *tes;
465   int ntes;
466   Id *edgedata;
467   int nedgedata;
468   Id *invedgedata;
469
470   Queue cycles;
471   Queue cyclesdata;
472   int ncycles;
473 };
474
475 static int
476 addteedge(struct orderdata *od, int from, int to, int type)
477 {
478   int i;
479   struct _TransactionElement *te;
480
481   if (from == to)
482     return 0;
483
484   /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
485
486   te = od->tes + from;
487   for (i = te->edges; od->edgedata[i]; i += 2)
488     if (od->edgedata[i] == to)
489       break;
490   /* test of brokenness */
491   if (type == TYPE_BROKEN)
492     return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
493   if (od->edgedata[i])
494     {
495       od->edgedata[i + 1] |= type;
496       return 0;
497     }
498   if (i + 1 == od->nedgedata)
499     {
500       /* printf("tail add %d\n", i - te->edges); */
501       if (!i)
502         te->edges = ++i;
503       od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
504     }
505   else
506     {
507       /* printf("extend %d\n", i - te->edges); */
508       od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
509       if (i > te->edges)
510         memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
511       i = od->nedgedata + (i - te->edges);
512       te->edges = od->nedgedata;
513     }
514   od->edgedata[i] = to;
515   od->edgedata[i + 1] = type;
516   od->edgedata[i + 2] = 0;      /* end marker */
517   od->nedgedata = i + 3;
518   return 0;
519 }
520
521 static int
522 addedge(struct orderdata *od, Id from, Id to, int type)
523 {
524   Transaction *trans = od->trans;
525   Pool *pool = trans->pool;
526   Solvable *s;
527   struct _TransactionElement *te;
528   int i;
529
530   // printf("addedge %d %d type %d\n", from, to, type);
531   s = pool->solvables + from;
532   if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
533     {
534       /* obsolete, map to install */
535       if (trans->transaction_installed[from - pool->installed->start] > 0)
536         from = trans->transaction_installed[from - pool->installed->start];
537       else
538         {
539           int ret = 0;
540           Queue ti;
541           Id tibuf[5];
542
543           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
544           solver_transaction_all_pkgs(trans, from, &ti);
545           for (i = 0; i < ti.count; i++)
546             ret |= addedge(od, ti.elements[i], to, type);
547           queue_free(&ti);
548           return ret;
549         }
550     }
551   s = pool->solvables + to;
552   if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
553     {
554       /* obsolete, map to install */
555       if (trans->transaction_installed[to - pool->installed->start] > 0)
556         to = trans->transaction_installed[to - pool->installed->start];
557       else
558         {
559           int ret = 0;
560           Queue ti;
561           Id tibuf[5];
562
563           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
564           solver_transaction_all_pkgs(trans, to, &ti);
565           for (i = 0; i < ti.count; i++)
566             ret |= addedge(od, from, ti.elements[i], type);
567           queue_free(&ti);
568           return ret;
569         }
570     }
571
572   /* map from/to to te numbers */
573   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
574     if (te->p == to)
575       break;
576   if (i == od->ntes)
577     return 0;
578   to = i;
579
580   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
581     if (te->p == from)
582       break;
583   if (i == od->ntes)
584     return 0;
585
586   return addteedge(od, i, to, type);
587 }
588
589 #if 1
590 static int
591 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
592 {
593   Transaction *trans = od->trans;
594   Pool *pool = trans->pool;
595   Id ti1buf[5], ti2buf[5];
596   Queue ti1, ti2;
597   int i, j;
598
599   /* both q1 and q2 are uninstalls. check if their TEs intersect */
600   /* common case: just one TE for both packages */
601 #if 0
602   printf("havechoice %d %d %d\n", p, q1, q2);
603 #endif
604   if (trans->transaction_installed[q1 - pool->installed->start] == 0)
605     return 1;
606   if (trans->transaction_installed[q2 - pool->installed->start] == 0)
607     return 1;
608   if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
609     return 0;
610   if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
611     return 1;
612   queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
613   solver_transaction_all_pkgs(trans, q1, &ti1);
614   queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
615   solver_transaction_all_pkgs(trans, q2, &ti2);
616   for (i = 0; i < ti1.count; i++)
617     for (j = 0; j < ti2.count; j++)
618       if (ti1.elements[i] == ti2.elements[j])
619         {
620           /* found a common edge */
621           queue_free(&ti1);
622           queue_free(&ti2);
623           return 0;
624         }
625   queue_free(&ti1);
626   queue_free(&ti2);
627   return 1;
628 }
629 #endif
630
631 static inline int
632 havescripts(Pool *pool, Id solvid)
633 {
634   Solvable *s = pool->solvables + solvid;
635   const char *dep;
636   if (s->requires)
637     {
638       Id req, *reqp;
639       int inpre = 0;
640       reqp = s->repo->idarraydata + s->requires;
641       while ((req = *reqp++) != 0)
642         {
643           if (req == SOLVABLE_PREREQMARKER)
644             {
645               inpre = 1;
646               continue;
647             }
648           if (!inpre)
649             continue;
650           dep = id2str(pool, req);
651           if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
652             return 1;
653         }
654     }
655   return 0;
656 }
657
658 static void
659 addsolvableedges(struct orderdata *od, Solvable *s)
660 {
661   Transaction *trans = od->trans;
662   Pool *pool = trans->pool;
663   Id req, *reqp, con, *conp;
664   Id p, p2, pp2;
665   int i, j, pre, numins;
666   Repo *installed = pool->installed;
667   Solvable *s2;
668   Queue reqq;
669   int provbyinst;
670
671 #if 0
672   printf("addsolvableedges %s\n", solvable2str(pool, s));
673 #endif
674   p = s - pool->solvables;
675   queue_init(&reqq);
676   if (s->requires)
677     {
678       reqp = s->repo->idarraydata + s->requires;
679       pre = TYPE_REQ;
680       while ((req = *reqp++) != 0)
681         {
682           if (req == SOLVABLE_PREREQMARKER)
683             {
684               pre = TYPE_PREREQ;
685               continue;
686             }
687 #if 0
688           if (pre != TYPE_PREREQ && installed && s->repo == installed)
689             {
690               /* ignore normal requires if we're getting obsoleted */
691               if (trans->transaction_installed[p - pool->installed->start])
692                 continue;
693             }
694 #endif
695           queue_empty(&reqq);
696           numins = 0;   /* number of packages to be installed providing it */
697           provbyinst = 0;       /* provided by kept package */
698           FOR_PROVIDES(p2, pp2, req)
699             {
700               s2 = pool->solvables + p2;
701               if (p2 == p)
702                 {
703                   reqq.count = 0;       /* self provides */
704                   break;
705                 }
706               if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
707                 {
708                   provbyinst = 1;
709 #if 0
710                   printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
711                   reqq.count = 0;       /* provided by package that stays installed */
712                   break;
713 #else
714                   continue;
715 #endif
716                 }
717               if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
718                 continue;               /* package stays uninstalled */
719               
720               if (s->repo == installed)
721                 {
722                   /* s gets uninstalled */
723                   queue_pushunique(&reqq, p2);
724                   if (s2->repo != installed)
725                     numins++;
726                 }
727               else
728                 {
729                   if (s2->repo == installed)
730                     continue;   /* s2 gets uninstalled */
731                   queue_pushunique(&reqq, p2);
732                 }
733             }
734           if (provbyinst)
735             {
736               /* prune to harmless ->inst edges */
737               for (i = j = 0; i < reqq.count; i++)
738                 if (pool->solvables[reqq.elements[i]].repo != installed)
739                   reqq.elements[j++] = reqq.elements[i];
740               reqq.count = j;
741             }
742
743           if (numins && reqq.count)
744             {
745               if (s->repo == installed)
746                 {
747                   for (i = 0; i < reqq.count; i++)
748                     {
749                       if (pool->solvables[reqq.elements[i]].repo == installed)
750                         {
751                           for (j = 0; j < reqq.count; j++)
752                             {
753                               if (pool->solvables[reqq.elements[j]].repo != installed)
754                                 {
755                                   if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
756                                     continue;   /* no self edge */
757 #if 0
758                                   printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
759 #endif
760                                   addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
761                                 }
762                             }
763                         }
764                     }
765                 }
766               /* no mixed types, remove all deps on uninstalls */
767               for (i = j = 0; i < reqq.count; i++)
768                 if (pool->solvables[reqq.elements[i]].repo != installed)
769                   reqq.elements[j++] = reqq.elements[i];
770               reqq.count = j;
771             }
772           if (!reqq.count)
773             continue;
774           for (i = 0; i < reqq.count; i++)
775             {
776               int choice = 0;
777               p2 = reqq.elements[i];
778               if (pool->solvables[p2].repo != installed)
779                 {
780                   /* all elements of reqq are installs, thus have different TEs */
781                   choice = reqq.count - 1;
782                   if (pool->solvables[p].repo != installed)
783                     {
784 #if 0
785                       printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
786 #endif
787                       addedge(od, p, p2, pre);
788                     }
789                   else
790                     {
791 #if 0
792                       printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
793 #endif
794                       addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
795                     }
796                 }
797               else
798                 {
799                   if (s->repo != installed)
800                     continue;   /* no inst->uninst edges, please! */
801
802                   /* uninst -> uninst edge. Those make trouble. Only add if we must */
803                   if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
804                     {
805                       /* p is obsoleted by another package and has no scripts */
806                       /* we assume that the obsoletor is good enough to replace p */
807                       continue;
808                     }
809 #if 1
810                   choice = 0;
811                   for (j = 0; j < reqq.count; j++)
812                     {
813                       if (i == j)
814                         continue;
815                       if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
816                         choice++;
817                     }
818 #endif
819 #if 0
820                   printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
821 #endif
822                   addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
823                 }
824             }
825         }
826     }
827   if (s->conflicts)
828     {
829       conp = s->repo->idarraydata + s->conflicts;
830       while ((con = *conp++) != 0)
831         {
832           FOR_PROVIDES(p2, pp2, con)
833             {
834               if (p2 == p)
835                 continue;
836               s2 = pool->solvables + p2;
837               if (!s2->repo)
838                 continue;
839               if (s->repo == installed)
840                 {
841                   if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
842                     {
843                       /* deinstall p before installing p2 */
844                       addedge(od, p2, p, TYPE_CON);
845                     }
846                 }
847               else
848                 {
849                   if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
850                     {
851                       /* deinstall p2 before installing p */
852                       addedge(od, p, p2, TYPE_CON);
853                     }
854                 }
855
856             }
857         }
858     }
859   queue_free(&reqq);
860 }
861
862
863 /* break an edge in a cycle */
864 static void
865 breakcycle(struct orderdata *od, Id *cycle)
866 {
867   Pool *pool = od->trans->pool;
868   Id ddegmin, ddegmax, ddeg;
869   int k, l;
870   struct _TransactionElement *te;
871
872   l = 0;
873   ddegmin = ddegmax = 0;
874   for (k = 0; cycle[k + 1]; k += 2)
875     {
876       ddeg = od->edgedata[cycle[k + 1] + 1];
877       if (ddeg > ddegmax)
878         ddegmax = ddeg;
879       if (!k || ddeg < ddegmin)
880         {
881           l = k;
882           ddegmin = ddeg;
883           continue;
884         }
885       if (ddeg == ddegmin)
886         {
887           if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
888             {
889               /* prefer k, as l comes from a package with contains scriptlets */
890               l = k;
891               ddegmin = ddeg;
892               continue;
893             }
894           /* same edge value, check for prereq */
895         }
896     }
897
898   /* record brkoen cycle starting with the tail */
899   queue_push(&od->cycles, od->cyclesdata.count);                /* offset into data */
900   queue_push(&od->cycles, k / 2);                               /* cycle elements */
901   queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);      /* broken edge */
902   od->ncycles++;
903   for (k = l;;)
904     {
905       k += 2;
906       if (!cycle[k + 1])
907         k = 0;
908       queue_push(&od->cyclesdata, cycle[k]);
909       if (k == l)
910         break;
911     }
912   queue_push(&od->cyclesdata, 0);       /* mark end */
913
914   /* break that edge */
915   od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
916
917 #if 1
918   if (ddegmin < TYPE_REQ)
919     return;
920 #endif
921
922   /* cycle recorded, print it */
923   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
924     printf("CRITICAL ");
925   printf("cycle: --> ");
926   for (k = 0; cycle[k + 1]; k += 2)
927     {
928       te = od->tes +  cycle[k];
929       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
930         printf("%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
931       else
932         printf("%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
933     }
934   printf("\n");
935 }
936
937 static inline void
938 dump_tes(struct orderdata *od)
939 {
940   Pool *pool = od->trans->pool;
941   int i, j;
942   Queue obsq;
943   struct _TransactionElement *te, *te2;
944
945   queue_init(&obsq);
946   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
947     {
948       Solvable *s = pool->solvables + te->p;
949       printf("TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
950       if (s->repo != pool->installed)
951         {
952           queue_empty(&obsq);
953           solver_transaction_all_pkgs(od->trans, te->p, &obsq);
954           for (j = 0; j < obsq.count; j++)
955             printf("         -%s\n", solvid2str(pool, obsq.elements[j]));
956         }
957       for (j = te->edges; od->edgedata[j]; j += 2)
958         {
959           te2 = od->tes + od->edgedata[j];
960           printf("       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
961         }
962     }
963 }
964
965 #if 1
966 static void
967 reachable(struct orderdata *od, Id i)
968 {
969   struct _TransactionElement *te = od->tes + i;
970   int j, k;
971
972   if (te->mark != 0)
973     return;
974   te->mark = 1;
975   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
976     {
977       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
978         continue;
979       if (!od->tes[k].mark)
980         reachable(od, k);
981       if (od->tes[k].mark == 2)
982         {
983           te->mark = 2;
984           return;
985         }
986     }
987   te->mark = -1;
988 }
989 #endif
990
991 static void
992 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
993 {
994 #if 0
995   Transaction *trans = od->trans;
996   Pool *pool = trans->pool;
997 #endif
998   struct _TransactionElement *te;
999   int i, j, k, tail;
1000 #if 1
1001   int head;
1002 #endif
1003
1004 #if 0
1005   printf("addcycleedges\n");
1006   for (i = 0; (j = cycle[i]) != 0; i++)
1007     printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
1008 #endif
1009
1010   /* first add all the tail cycle edges */
1011
1012   /* see what we can reach from the cycle */
1013   queue_empty(todo);
1014   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1015     te->mark = 0;
1016   for (i = 0; (j = cycle[i]) != 0; i++)
1017     {
1018       od->tes[j].mark = -1;
1019       queue_push(todo, j);
1020     }
1021   while (todo->count)
1022     {
1023       i = queue_pop(todo);
1024       te = od->tes + i;
1025       if (te->mark > 0)
1026         continue;
1027       te->mark = te->mark < 0 ? 2 : 1;
1028       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1029         {
1030           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1031             continue;
1032           if (od->tes[k].mark > 0)
1033             continue;   /* no need to visit again */
1034           queue_push(todo, k);
1035         }
1036     }
1037   /* now all cycle TEs are marked with 2, all TEs reachable
1038    * from the cycle are marked with 1 */
1039   tail = cycle[0];
1040   od->tes[tail].mark = 1;       /* no need to add edges */
1041
1042   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1043     {
1044       if (te->mark)
1045         continue;       /* reachable from cycle */
1046       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1047         {
1048           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1049             continue;
1050           if (od->tes[k].mark != 2)
1051             continue;
1052           /* We found an edge to the cycle. Add an extra edge to the tail */
1053           /* the TE was not reachable, so we're not creating a new cycle! */
1054 #if 0
1055           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));
1056 #endif
1057           j -= te->edges;       /* in case we move */
1058           addteedge(od, i, tail, TYPE_CYCLETAIL);
1059           j += te->edges;
1060           break;        /* one edge is enough */
1061         }
1062     }
1063
1064 #if 1
1065   /* now add all head cycle edges */
1066
1067   /* reset marks */
1068   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1069     te->mark = 0;
1070   head = 0;
1071   for (i = 0; (j = cycle[i]) != 0; i++)
1072     {
1073       head = j;
1074       od->tes[j].mark = 2;
1075     }
1076   /* first the head to save some time */
1077   te = od->tes + head;
1078   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1079     {
1080       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1081         continue;
1082       if (!od->tes[k].mark)
1083         reachable(od, k);
1084       if (od->tes[k].mark == -1)
1085         od->tes[k].mark = -2;   /* no need for another edge */
1086     }
1087   for (i = 0; cycle[i] != 0; i++)
1088     {
1089       if (cycle[i] == head)
1090         break;
1091       te = od->tes + cycle[i];
1092       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1093         {
1094           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1095             continue;
1096           /* see if we can reach a cycle TE from k */
1097           if (!od->tes[k].mark)
1098             reachable(od, k);
1099           if (od->tes[k].mark == -1)
1100             {
1101 #if 0
1102               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));
1103 #endif
1104               addteedge(od, head, k, TYPE_CYCLEHEAD);
1105               od->tes[k].mark = -2;     /* no need to add that one again */
1106             }
1107         }
1108     }
1109 #endif
1110 }
1111
1112 void
1113 transaction_order(Transaction *trans, int flags)
1114 {
1115   Pool *pool = trans->pool;
1116   Queue *tr = &trans->steps;
1117   Repo *installed = pool->installed;
1118   Id type, p;
1119   Solvable *s;
1120   int i, j, k, numte, numedge;
1121   struct orderdata od;
1122   struct _TransactionElement *te;
1123   Queue todo, obsq, samerepoq, uninstq;
1124   int cycstart, cycel;
1125   Id *cycle, *obstypes;
1126   int oldcount;
1127   int start, now;
1128   Repo *lastrepo;
1129   int lastmedia;
1130   Id *temedianr;
1131
1132   start = now = sat_timems(0);
1133   POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1134   /* free old data if present */
1135   if (trans->orderdata)
1136     {
1137       struct _TransactionOrderdata *od = trans->orderdata;
1138       od->tes = sat_free(od->tes);
1139       od->invedgedata = sat_free(od->invedgedata);
1140       trans->orderdata = sat_free(trans->orderdata);
1141     }
1142
1143   /* create a transaction element for every active component */
1144   numte = 0;
1145   for (i = 0; i < tr->count; i += 2)
1146     {
1147       p = tr->elements[i + 1];
1148       s = pool->solvables + p;
1149       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1150         continue;
1151       numte++;
1152     }
1153   POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1154   if (!numte)
1155     return;     /* nothing to do... */
1156
1157   numte++;      /* leave first one zero */
1158   memset(&od, 0, sizeof(od));
1159   od.trans = trans;
1160   od.ntes = numte;
1161   od.tes = sat_calloc(numte, sizeof(*od.tes));
1162   od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1163   od.edgedata[0] = 0;
1164   od.nedgedata = 1;
1165   queue_init(&od.cycles);
1166
1167   for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
1168     {
1169       p = tr->elements[i + 1];
1170       s = pool->solvables + p;
1171       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1172         continue;
1173       te->p = p;
1174       te->type = tr->elements[i];
1175       te++;
1176     }
1177
1178   /* create dependency graph */
1179   for (i = 0; i < tr->count; i += 2)
1180     {
1181       type = tr->elements[i];
1182       p = tr->elements[i + 1];
1183       addsolvableedges(&od, pool->solvables + p);
1184     }
1185
1186   /* count edges */
1187   numedge = 0;
1188   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1189     for (j = te->edges; od.edgedata[j]; j += 2)
1190       numedge++;
1191   POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1192   POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1193
1194 #if 0
1195   dump_tes(&od);
1196 #endif
1197   
1198   now = sat_timems(0);
1199   /* kill all cycles */
1200   queue_init(&todo);
1201   for (i = numte - 1; i > 0; i--)
1202     queue_push(&todo, i);
1203
1204   while (todo.count)
1205     {
1206       i = queue_pop(&todo);
1207       /* printf("- look at TE %d\n", i); */
1208       if (i < 0)
1209         {
1210           i = -i;
1211           od.tes[i].mark = 2;   /* done with that one */
1212           continue;
1213         }
1214       te = od.tes + i;
1215       if (te->mark == 2)
1216         continue;               /* already finished before */
1217       if (te->mark == 0)
1218         {
1219           int edgestovisit = 0;
1220           /* new node, visit edges */
1221           for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1222             {
1223               if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1224                 continue;
1225               if (od.tes[k].mark == 2)
1226                 continue;       /* no need to visit again */
1227               if (!edgestovisit++)
1228                 queue_push(&todo, -i);  /* end of edges marker */
1229               queue_push(&todo, k);
1230             }
1231           if (!edgestovisit)
1232             te->mark = 2;       /* no edges, done with that one */
1233           else
1234             te->mark = 1;       /* under investigation */
1235           continue;
1236         }
1237       /* oh no, we found a cycle */
1238       /* find start of cycle node (<0) */
1239       for (j = todo.count - 1; j >= 0; j--)
1240         if (todo.elements[j] == -i)
1241           break;
1242       assert(j >= 0);
1243       cycstart = j;
1244       /* build te/edge chain */
1245       k = cycstart;
1246       for (j = k; j < todo.count; j++)
1247         if (todo.elements[j] < 0)
1248           todo.elements[k++] = -todo.elements[j];
1249       cycel = k - cycstart;
1250       assert(cycel > 1);
1251       /* make room for edges, two extra element for cycle loop + terminating 0 */
1252       while (todo.count < cycstart + 2 * cycel + 2)
1253         queue_push(&todo, 0);
1254       cycle = todo.elements + cycstart;
1255       cycle[cycel] = i;         /* close the loop */
1256       cycle[2 * cycel + 1] = 0; /* terminator */
1257       for (k = cycel; k > 0; k--)
1258         {
1259           cycle[k * 2] = cycle[k];
1260           te = od.tes + cycle[k - 1];
1261           assert(te->mark == 1);
1262           te->mark = 0; /* reset investigation marker */
1263           /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1264           for (j = te->edges; od.edgedata[j]; j += 2)
1265             if (od.edgedata[j] == cycle[k])
1266               break;
1267           assert(od.edgedata[j]);
1268           cycle[k * 2 - 1] = j;
1269         }
1270       /* now cycle looks like this: */
1271       /* te1 edge te2 edge te3 ... teN edge te1 0 */
1272       breakcycle(&od, cycle);
1273       /* restart with start of cycle */
1274       todo.count = cycstart + 1;
1275     }
1276   POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1277   POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1278
1279   now = sat_timems(0);
1280   /* now go through all broken cycles and create cycle edges to help
1281      the ordering */
1282    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1283      {
1284        if (od.cycles.elements[i + 2] >= TYPE_REQ)
1285          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1286      }
1287    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1288      {
1289        if (od.cycles.elements[i + 2] < TYPE_REQ)
1290          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1291      }
1292   POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1293
1294   /* all edges are finally set up and there are no cycles, now the easy part.
1295    * Create an ordered transaction */
1296   now = sat_timems(0);
1297   /* first invert all edges */
1298   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1299     te->mark = 1;       /* term 0 */
1300   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1301     {
1302       for (j = te->edges; od.edgedata[j]; j += 2)
1303         {
1304           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1305             continue;
1306           od.tes[od.edgedata[j]].mark++;
1307         }
1308     }
1309   j = 1;
1310   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1311     {
1312       te->mark += j;
1313       j = te->mark;
1314     }
1315   POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1316   od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1317   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1318     {
1319       for (j = te->edges; od.edgedata[j]; j += 2)
1320         {
1321           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1322             continue;
1323           od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1324         }
1325     }
1326   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1327     te->edges = te->mark;       /* edges now points into invedgedata */
1328   od.edgedata = sat_free(od.edgedata);
1329
1330   /* now the final ordering */
1331   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1332     te->mark = 0;
1333   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1334     for (j = te->edges; od.invedgedata[j]; j++)
1335       od.tes[od.invedgedata[j]].mark++;
1336
1337   queue_init(&samerepoq);
1338   queue_init(&uninstq);
1339   queue_empty(&todo);
1340   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1341     if (te->mark == 0)
1342       {
1343         if (installed && pool->solvables[te->p].repo == installed)
1344           queue_push(&uninstq, i);
1345         else
1346           queue_push(&todo, i);
1347       }
1348   assert(todo.count > 0 || uninstq.count > 0);
1349   if (installed)
1350     {
1351       obstypes = sat_calloc(installed->end - installed->start, sizeof(Id));
1352       for (i = 0; i < tr->count; i += 2)
1353         {
1354           p = tr->elements[i + 1];
1355           s = pool->solvables + p;
1356           if (s->repo == installed && trans->transaction_installed[p - installed->start])
1357             obstypes[p - installed->start] = tr->elements[i];
1358         }
1359     }
1360   else
1361     obstypes = 0;
1362   oldcount = tr->count;
1363   queue_empty(tr);
1364
1365   queue_init(&obsq);
1366   
1367   lastrepo = 0;
1368   lastmedia = 0;
1369   temedianr = sat_calloc(numte, sizeof(Id));
1370   for (i = 1; i < numte; i++)
1371     {
1372       Solvable *s = pool->solvables + od.tes[i].p;
1373       if (installed && s->repo == installed)
1374         j = 1;
1375       else
1376         j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1377       temedianr[i] = j;
1378     }
1379   for (;;)
1380     {
1381       /* select an TE i */
1382       if (uninstq.count)
1383         i = queue_shift(&uninstq);
1384       else if (samerepoq.count)
1385         i = queue_shift(&samerepoq);
1386       else if (todo.count)
1387         {
1388           /* find next repo/media */
1389           for (j = 0; j < todo.count; j++)
1390             {
1391               if (!j || temedianr[todo.elements[j]] < lastmedia)
1392                 {
1393                   i = j;
1394                   lastmedia = temedianr[todo.elements[j]];
1395                 }
1396             }
1397           lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1398
1399           /* move all matching TEs to samerepoq */
1400           for (i = j = 0; j < todo.count; j++)
1401             {
1402               int k = todo.elements[j];
1403               if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1404                 queue_push(&samerepoq, k);
1405               else
1406                 todo.elements[i++] = k;
1407             }
1408           todo.count = i;
1409
1410           assert(samerepoq.count);
1411           i = queue_shift(&samerepoq);
1412         }
1413       else
1414         break;
1415
1416       te = od.tes + i;
1417       queue_push(tr, te->type);
1418       queue_push(tr, te->p);
1419 #if 0
1420 printf("do %s [%d]\n", solvid2str(pool, te->p), temedianr[i]);
1421 #endif
1422       s = pool->solvables + te->p;
1423       if (installed && s->repo != installed)
1424         {
1425           queue_empty(&obsq);
1426           solver_transaction_all_pkgs(trans, te->p, &obsq);
1427           for (j = 0; j < obsq.count; j++)
1428             {
1429               p = obsq.elements[j];
1430               assert(p >= installed->start && p < installed->end);
1431               if (obstypes[p - installed->start])
1432                 {
1433                   queue_push(tr, obstypes[p - installed->start]);
1434                   queue_push(tr, p);
1435                   obstypes[p - installed->start] = 0;
1436                 }
1437             }
1438         }
1439       for (j = te->edges; od.invedgedata[j]; j++)
1440         {
1441           struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1442           assert(te2->mark > 0);
1443           if (--te2->mark == 0)
1444             {
1445               Solvable *s = pool->solvables + te2->p;
1446 #if 0
1447 printf("free %s [%d]\n", solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1448 #endif
1449               if (installed && s->repo == installed)
1450                 queue_push(&uninstq, od.invedgedata[j]);
1451               else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1452                 queue_push(&samerepoq, od.invedgedata[j]);
1453               else
1454                 queue_push(&todo, od.invedgedata[j]);
1455             }
1456         }
1457     }
1458   sat_free(obstypes);
1459   sat_free(temedianr);
1460   queue_free(&todo);
1461   queue_free(&samerepoq);
1462   queue_free(&uninstq);
1463   queue_free(&obsq);
1464   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1465     assert(te->mark == 0);
1466   assert(tr->count == oldcount);
1467   POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1468   POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1469
1470   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1471     {
1472       trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1473       trans->orderdata->tes = od.tes;
1474       trans->orderdata->ntes = numte;
1475       trans->orderdata->invedgedata = od.invedgedata;
1476     }
1477   else
1478     {
1479       sat_free(od.tes);
1480       sat_free(od.invedgedata);
1481     }
1482 }
1483
1484 static void
1485 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1486 {
1487   Pool *pool = trans->pool;
1488   Id p, pp;
1489   Solvable *s;
1490   int good;
1491
1492   if (MAPTST(seen, pkg))
1493     return;
1494   MAPSET(seen, pkg);
1495   s = pool->solvables + pkg;
1496 #if 0
1497   printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1498 #endif
1499   if (s->requires)
1500     {
1501       Id req, *reqp;
1502       int inpre = 0;
1503       reqp = s->repo->idarraydata + s->requires;
1504       while ((req = *reqp++) != 0)
1505         {
1506           if (req == SOLVABLE_PREREQMARKER)
1507             {
1508               inpre = 1;
1509               continue;
1510             }
1511           if (onlyprereq && !inpre)
1512             continue;
1513           if (!strncmp(id2str(pool, req), "rpmlib(", 7))
1514             continue;
1515           good = 0;
1516           /* first check kept packages, then freshly installed, then not yet uninstalled */
1517           FOR_PROVIDES(p, pp, req)
1518             {
1519               if (!MAPTST(ins, p))
1520                 continue;
1521               if (MAPTST(&trans->transactsmap, p))
1522                 continue;
1523               good++;
1524               transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1525             }
1526           if (!good)
1527             {
1528               FOR_PROVIDES(p, pp, req)
1529                 {
1530                   if (!MAPTST(ins, p))
1531                     continue;
1532                   if (pool->solvables[p].repo == pool->installed)
1533                     continue;
1534                   good++;
1535                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1536                 }
1537             }
1538           if (!good)
1539             {
1540               FOR_PROVIDES(p, pp, req)
1541                 {
1542                   if (!MAPTST(ins, p))
1543                     continue;
1544                   good++;
1545                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1546                 }
1547             }
1548           if (!good)
1549             {
1550               printf("  %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));
1551             }
1552         }
1553     }
1554 }
1555
1556 void
1557 transaction_check(Transaction *trans)
1558 {
1559   Pool *pool = trans->pool;
1560   Solvable *s;
1561   Id p, type, lastins;
1562   Map ins, seen;
1563   int i;
1564
1565   printf("\nchecking transaction order...\n");
1566   map_init(&ins, pool->nsolvables);
1567   map_init(&seen, pool->nsolvables);
1568   if (pool->installed)
1569     FOR_REPO_SOLVABLES(pool->installed, p, s)
1570       MAPSET(&ins, p);
1571   lastins = 0;
1572   for (i = 0; i < trans->steps.count; i += 2)
1573     {
1574       type = trans->steps.elements[i];
1575       p = trans->steps.elements[i + 1];
1576       s = pool->solvables + p;
1577       if (s->repo != pool->installed)
1578         lastins = p;
1579       if (s->repo != pool->installed)
1580         MAPSET(&ins, p);
1581       if (havescripts(pool, p))
1582         {
1583           MAPZERO(&seen);
1584           transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1585         }
1586       if (s->repo == pool->installed)
1587         MAPCLR(&ins, p);
1588     }
1589   map_free(&seen);
1590   map_free(&ins);
1591   printf("transaction order check done.\n");
1592 }
1593
1594 int
1595 transaction_add_choices(Transaction *trans, Queue *choices, Id chosen)
1596 {
1597   int i, j;
1598   struct _TransactionOrderdata *od = trans->orderdata;
1599   struct _TransactionElement *te;
1600
1601   if (!od)
1602      return choices->count;
1603   if (!chosen)
1604     {
1605       /* initialization step */
1606       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1607         te->mark = 0;
1608       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1609         {
1610           for (j = te->edges; od->invedgedata[j]; j++)
1611             od->tes[od->invedgedata[j]].mark++;
1612         }
1613       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1614         if (!te->mark)
1615           {
1616             queue_push(choices, te->type);
1617             queue_push(choices, te->p);
1618           }
1619       return choices->count;
1620     }
1621   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1622     if (te->p == chosen)
1623       break;
1624   if (i == od->ntes)
1625     return choices->count;
1626   if (te->mark > 0)
1627     {
1628       /* hey! out-of-order installation! */
1629       te->mark = -1;
1630     }
1631   for (j = te->edges; od->invedgedata[j]; j++)
1632     {
1633       te = od->tes + od->invedgedata[j];
1634       assert(te->mark > 0 || te->mark == -1);
1635       if (te->mark > 0 && --te->mark == 0)
1636         {
1637           queue_push(choices, te->type);
1638           queue_push(choices, te->p);
1639         }
1640     }
1641   return choices->count;
1642 }