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