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