- close fds before calling rpm
[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 int
670 transaction_installedresult(Transaction *trans, Queue *installedq)
671 {
672   Pool *pool = trans->pool;
673   Repo *installed = pool->installed;
674   Solvable *s;
675   int i, cutoff;
676   Id p;
677
678   queue_empty(installedq);
679   /* first the new installs, than the kept packages */
680   for (i = 0; i < trans->steps.count; i++)
681     {
682       p = trans->steps.elements[i];
683       s = pool->solvables + p;
684       if (installed && s->repo == installed)
685         continue;
686       queue_push(installedq, p);
687     }
688   cutoff = installedq->count;
689   if (installed)
690     {
691       FOR_REPO_SOLVABLES(installed, p, s)
692         if (!MAPTST(&trans->transactsmap, p))
693           queue_push(installedq, p);
694     }
695   return cutoff;
696 }
697
698 struct _TransactionElement {
699   Id p;         /* solvable id */
700   Id edges;     /* pointer into edges data */
701   Id mark;
702 };
703
704 struct _TransactionOrderdata {
705   struct _TransactionElement *tes;
706   int ntes;
707   Id *invedgedata;
708   int ninvedgedata;
709 };
710
711 #define TYPE_BROKEN     (1<<0)
712 #define TYPE_CON        (1<<1)
713
714 #define TYPE_REQ_P      (1<<2)
715 #define TYPE_PREREQ_P   (1<<3)
716
717 #define TYPE_REQ        (1<<4)
718 #define TYPE_PREREQ     (1<<5)
719
720 #define TYPE_CYCLETAIL  (1<<16)
721 #define TYPE_CYCLEHEAD  (1<<17)
722
723 #define EDGEDATA_BLOCK  127
724
725 void
726 transaction_init(Transaction *trans, Pool *pool)
727 {
728   memset(trans, 0, sizeof(*trans));
729   trans->pool = pool;
730 }
731
732 void
733 transaction_init_clone(Transaction *trans, Transaction *srctrans)
734 {
735   memset(trans, 0, sizeof(*trans));
736   trans->pool = srctrans->pool;
737   queue_init_clone(&trans->steps, &srctrans->steps);
738   queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
739   if (srctrans->transaction_installed)
740     {
741       Repo *installed = srctrans->pool->installed;
742       trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
743       memcpy(trans->transaction_installed, srctrans->transaction_installed, (installed->end - installed->start) * sizeof(Id));
744     }
745   map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
746   map_init_clone(&trans->noobsmap, &srctrans->noobsmap);
747   if (srctrans->orderdata)
748     {
749       struct _TransactionOrderdata *od = srctrans->orderdata;
750       trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
751       trans->orderdata->tes = sat_malloc2(od->ntes, sizeof(*od->tes));
752       memcpy(trans->orderdata->tes, od->tes, od->ntes * sizeof(*od->tes));
753       trans->orderdata->ntes = od->ntes;
754       trans->orderdata->invedgedata = sat_malloc2(od->ninvedgedata, sizeof(Id));
755       memcpy(trans->orderdata->invedgedata, od->invedgedata, od->ninvedgedata * sizeof(Id));
756       trans->orderdata->ninvedgedata = od->ninvedgedata;
757     }
758 }
759
760 void
761 transaction_free(Transaction *trans)
762 {
763   queue_free(&trans->steps);
764   queue_free(&trans->transaction_info);
765   trans->transaction_installed = sat_free(trans->transaction_installed);
766   map_free(&trans->transactsmap);
767   map_free(&trans->noobsmap);
768   transaction_free_orderdata(trans);
769 }
770
771 void
772 transaction_free_orderdata(Transaction *trans)
773 {
774   if (trans->orderdata)
775     {
776       struct _TransactionOrderdata *od = trans->orderdata;
777       od->tes = sat_free(od->tes);
778       od->invedgedata = sat_free(od->invedgedata);
779       trans->orderdata = sat_free(trans->orderdata);
780     }
781 }
782
783 struct orderdata {
784   Transaction *trans;
785   struct _TransactionElement *tes;
786   int ntes;
787   Id *edgedata;
788   int nedgedata;
789   Id *invedgedata;
790
791   Queue cycles;
792   Queue cyclesdata;
793   int ncycles;
794 };
795
796 static int
797 addteedge(struct orderdata *od, int from, int to, int type)
798 {
799   int i;
800   struct _TransactionElement *te;
801
802   if (from == to)
803     return 0;
804
805   /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
806
807   te = od->tes + from;
808   for (i = te->edges; od->edgedata[i]; i += 2)
809     if (od->edgedata[i] == to)
810       break;
811   /* test of brokenness */
812   if (type == TYPE_BROKEN)
813     return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
814   if (od->edgedata[i])
815     {
816       od->edgedata[i + 1] |= type;
817       return 0;
818     }
819   if (i + 1 == od->nedgedata)
820     {
821       /* printf("tail add %d\n", i - te->edges); */
822       if (!i)
823         te->edges = ++i;
824       od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
825     }
826   else
827     {
828       /* printf("extend %d\n", i - te->edges); */
829       od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
830       if (i > te->edges)
831         memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
832       i = od->nedgedata + (i - te->edges);
833       te->edges = od->nedgedata;
834     }
835   od->edgedata[i] = to;
836   od->edgedata[i + 1] = type;
837   od->edgedata[i + 2] = 0;      /* end marker */
838   od->nedgedata = i + 3;
839   return 0;
840 }
841
842 static int
843 addedge(struct orderdata *od, Id from, Id to, int type)
844 {
845   Transaction *trans = od->trans;
846   Pool *pool = trans->pool;
847   Solvable *s;
848   struct _TransactionElement *te;
849   int i;
850
851   // printf("addedge %d %d type %d\n", from, to, type);
852   s = pool->solvables + from;
853   if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
854     {
855       /* obsolete, map to install */
856       if (trans->transaction_installed[from - pool->installed->start] > 0)
857         from = trans->transaction_installed[from - pool->installed->start];
858       else
859         {
860           int ret = 0;
861           Queue ti;
862           Id tibuf[5];
863
864           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
865           transaction_all_obs_pkgs(trans, from, &ti);
866           for (i = 0; i < ti.count; i++)
867             ret |= addedge(od, ti.elements[i], to, type);
868           queue_free(&ti);
869           return ret;
870         }
871     }
872   s = pool->solvables + to;
873   if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
874     {
875       /* obsolete, map to install */
876       if (trans->transaction_installed[to - pool->installed->start] > 0)
877         to = trans->transaction_installed[to - pool->installed->start];
878       else
879         {
880           int ret = 0;
881           Queue ti;
882           Id tibuf[5];
883
884           queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
885           transaction_all_obs_pkgs(trans, to, &ti);
886           for (i = 0; i < ti.count; i++)
887             ret |= addedge(od, from, ti.elements[i], type);
888           queue_free(&ti);
889           return ret;
890         }
891     }
892
893   /* map from/to to te numbers */
894   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
895     if (te->p == to)
896       break;
897   if (i == od->ntes)
898     return 0;
899   to = i;
900
901   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
902     if (te->p == from)
903       break;
904   if (i == od->ntes)
905     return 0;
906
907   return addteedge(od, i, to, type);
908 }
909
910 #if 1
911 static int
912 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
913 {
914   Transaction *trans = od->trans;
915   Pool *pool = trans->pool;
916   Id ti1buf[5], ti2buf[5];
917   Queue ti1, ti2;
918   int i, j;
919
920   /* both q1 and q2 are uninstalls. check if their TEs intersect */
921   /* common case: just one TE for both packages */
922 #if 0
923   printf("havechoice %d %d %d\n", p, q1, q2);
924 #endif
925   if (trans->transaction_installed[q1 - pool->installed->start] == 0)
926     return 1;
927   if (trans->transaction_installed[q2 - pool->installed->start] == 0)
928     return 1;
929   if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
930     return 0;
931   if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
932     return 1;
933   queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
934   transaction_all_obs_pkgs(trans, q1, &ti1);
935   queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
936   transaction_all_obs_pkgs(trans, q2, &ti2);
937   for (i = 0; i < ti1.count; i++)
938     for (j = 0; j < ti2.count; j++)
939       if (ti1.elements[i] == ti2.elements[j])
940         {
941           /* found a common edge */
942           queue_free(&ti1);
943           queue_free(&ti2);
944           return 0;
945         }
946   queue_free(&ti1);
947   queue_free(&ti2);
948   return 1;
949 }
950 #endif
951
952 static inline int
953 havescripts(Pool *pool, Id solvid)
954 {
955   Solvable *s = pool->solvables + solvid;
956   const char *dep;
957   if (s->requires)
958     {
959       Id req, *reqp;
960       int inpre = 0;
961       reqp = s->repo->idarraydata + s->requires;
962       while ((req = *reqp++) != 0)
963         {
964           if (req == SOLVABLE_PREREQMARKER)
965             {
966               inpre = 1;
967               continue;
968             }
969           if (!inpre)
970             continue;
971           dep = id2str(pool, req);
972           if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
973             return 1;
974         }
975     }
976   return 0;
977 }
978
979 static void
980 addsolvableedges(struct orderdata *od, Solvable *s)
981 {
982   Transaction *trans = od->trans;
983   Pool *pool = trans->pool;
984   Id req, *reqp, con, *conp;
985   Id p, p2, pp2;
986   int i, j, pre, numins;
987   Repo *installed = pool->installed;
988   Solvable *s2;
989   Queue reqq;
990   int provbyinst;
991
992 #if 0
993   printf("addsolvableedges %s\n", solvable2str(pool, s));
994 #endif
995   p = s - pool->solvables;
996   queue_init(&reqq);
997   if (s->requires)
998     {
999       reqp = s->repo->idarraydata + s->requires;
1000       pre = TYPE_REQ;
1001       while ((req = *reqp++) != 0)
1002         {
1003           if (req == SOLVABLE_PREREQMARKER)
1004             {
1005               pre = TYPE_PREREQ;
1006               continue;
1007             }
1008 #if 0
1009           if (pre != TYPE_PREREQ && installed && s->repo == installed)
1010             {
1011               /* ignore normal requires if we're getting obsoleted */
1012               if (trans->transaction_installed[p - pool->installed->start])
1013                 continue;
1014             }
1015 #endif
1016           queue_empty(&reqq);
1017           numins = 0;   /* number of packages to be installed providing it */
1018           provbyinst = 0;       /* provided by kept package */
1019           FOR_PROVIDES(p2, pp2, req)
1020             {
1021               s2 = pool->solvables + p2;
1022               if (p2 == p)
1023                 {
1024                   reqq.count = 0;       /* self provides */
1025                   break;
1026                 }
1027               if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1028                 {
1029                   provbyinst = 1;
1030 #if 0
1031                   printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
1032                   reqq.count = 0;       /* provided by package that stays installed */
1033                   break;
1034 #else
1035                   continue;
1036 #endif
1037                 }
1038               if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1039                 continue;               /* package stays uninstalled */
1040               
1041               if (s->repo == installed)
1042                 {
1043                   /* s gets uninstalled */
1044                   queue_pushunique(&reqq, p2);
1045                   if (s2->repo != installed)
1046                     numins++;
1047                 }
1048               else
1049                 {
1050                   if (s2->repo == installed)
1051                     continue;   /* s2 gets uninstalled */
1052                   queue_pushunique(&reqq, p2);
1053                 }
1054             }
1055           if (provbyinst)
1056             {
1057               /* prune to harmless ->inst edges */
1058               for (i = j = 0; i < reqq.count; i++)
1059                 if (pool->solvables[reqq.elements[i]].repo != installed)
1060                   reqq.elements[j++] = reqq.elements[i];
1061               reqq.count = j;
1062             }
1063
1064           if (numins && reqq.count)
1065             {
1066               if (s->repo == installed)
1067                 {
1068                   for (i = 0; i < reqq.count; i++)
1069                     {
1070                       if (pool->solvables[reqq.elements[i]].repo == installed)
1071                         {
1072                           for (j = 0; j < reqq.count; j++)
1073                             {
1074                               if (pool->solvables[reqq.elements[j]].repo != installed)
1075                                 {
1076                                   if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1077                                     continue;   /* no self edge */
1078 #if 0
1079                                   printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
1080 #endif
1081                                   addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1082                                 }
1083                             }
1084                         }
1085                     }
1086                 }
1087               /* no mixed types, remove all deps on uninstalls */
1088               for (i = j = 0; i < reqq.count; i++)
1089                 if (pool->solvables[reqq.elements[i]].repo != installed)
1090                   reqq.elements[j++] = reqq.elements[i];
1091               reqq.count = j;
1092             }
1093           if (!reqq.count)
1094             continue;
1095           for (i = 0; i < reqq.count; i++)
1096             {
1097               int choice = 0;
1098               p2 = reqq.elements[i];
1099               if (pool->solvables[p2].repo != installed)
1100                 {
1101                   /* all elements of reqq are installs, thus have different TEs */
1102                   choice = reqq.count - 1;
1103                   if (pool->solvables[p].repo != installed)
1104                     {
1105 #if 0
1106                       printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1107 #endif
1108                       addedge(od, p, p2, pre);
1109                     }
1110                   else
1111                     {
1112 #if 0
1113                       printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1114 #endif
1115                       addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1116                     }
1117                 }
1118               else
1119                 {
1120                   if (s->repo != installed)
1121                     continue;   /* no inst->uninst edges, please! */
1122
1123                   /* uninst -> uninst edge. Those make trouble. Only add if we must */
1124                   if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1125                     {
1126                       /* p is obsoleted by another package and has no scripts */
1127                       /* we assume that the obsoletor is good enough to replace p */
1128                       continue;
1129                     }
1130 #if 1
1131                   choice = 0;
1132                   for (j = 0; j < reqq.count; j++)
1133                     {
1134                       if (i == j)
1135                         continue;
1136                       if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1137                         choice++;
1138                     }
1139 #endif
1140 #if 0
1141                   printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1142 #endif
1143                   addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1144                 }
1145             }
1146         }
1147     }
1148   if (s->conflicts)
1149     {
1150       conp = s->repo->idarraydata + s->conflicts;
1151       while ((con = *conp++) != 0)
1152         {
1153           FOR_PROVIDES(p2, pp2, con)
1154             {
1155               if (p2 == p)
1156                 continue;
1157               s2 = pool->solvables + p2;
1158               if (!s2->repo)
1159                 continue;
1160               if (s->repo == installed)
1161                 {
1162                   if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1163                     {
1164                       /* deinstall p before installing p2 */
1165                       addedge(od, p2, p, TYPE_CON);
1166                     }
1167                 }
1168               else
1169                 {
1170                   if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1171                     {
1172                       /* deinstall p2 before installing p */
1173                       addedge(od, p, p2, TYPE_CON);
1174                     }
1175                 }
1176
1177             }
1178         }
1179     }
1180   queue_free(&reqq);
1181 }
1182
1183
1184 /* break an edge in a cycle */
1185 static void
1186 breakcycle(struct orderdata *od, Id *cycle)
1187 {
1188   Pool *pool = od->trans->pool;
1189   Id ddegmin, ddegmax, ddeg;
1190   int k, l;
1191   struct _TransactionElement *te;
1192
1193   l = 0;
1194   ddegmin = ddegmax = 0;
1195   for (k = 0; cycle[k + 1]; k += 2)
1196     {
1197       ddeg = od->edgedata[cycle[k + 1] + 1];
1198       if (ddeg > ddegmax)
1199         ddegmax = ddeg;
1200       if (!k || ddeg < ddegmin)
1201         {
1202           l = k;
1203           ddegmin = ddeg;
1204           continue;
1205         }
1206       if (ddeg == ddegmin)
1207         {
1208           if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1209             {
1210               /* prefer k, as l comes from a package with contains scriptlets */
1211               l = k;
1212               ddegmin = ddeg;
1213               continue;
1214             }
1215           /* same edge value, check for prereq */
1216         }
1217     }
1218
1219   /* record brkoen cycle starting with the tail */
1220   queue_push(&od->cycles, od->cyclesdata.count);                /* offset into data */
1221   queue_push(&od->cycles, k / 2);                               /* cycle elements */
1222   queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);      /* broken edge */
1223   od->ncycles++;
1224   for (k = l;;)
1225     {
1226       k += 2;
1227       if (!cycle[k + 1])
1228         k = 0;
1229       queue_push(&od->cyclesdata, cycle[k]);
1230       if (k == l)
1231         break;
1232     }
1233   queue_push(&od->cyclesdata, 0);       /* mark end */
1234
1235   /* break that edge */
1236   od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1237
1238 #if 1
1239   if (ddegmin < TYPE_REQ)
1240     return;
1241 #endif
1242
1243   /* cycle recorded, print it */
1244   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1245     POOL_DEBUG(SAT_DEBUG_STATS, "CRITICAL ");
1246   POOL_DEBUG(SAT_DEBUG_STATS, "cycle: --> ");
1247   for (k = 0; cycle[k + 1]; k += 2)
1248     {
1249       te = od->tes +  cycle[k];
1250       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1251         POOL_DEBUG(SAT_DEBUG_STATS, "%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1252       else
1253         POOL_DEBUG(SAT_DEBUG_STATS, "%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1254     }
1255   POOL_DEBUG(SAT_DEBUG_STATS, "\n");
1256 }
1257
1258 static inline void
1259 dump_tes(struct orderdata *od)
1260 {
1261   Pool *pool = od->trans->pool;
1262   int i, j;
1263   Queue obsq;
1264   struct _TransactionElement *te, *te2;
1265
1266   queue_init(&obsq);
1267   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1268     {
1269       Solvable *s = pool->solvables + te->p;
1270       POOL_DEBUG(SAT_WARN, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1271       if (s->repo != pool->installed)
1272         {
1273           queue_empty(&obsq);
1274           transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1275           for (j = 0; j < obsq.count; j++)
1276             POOL_DEBUG(SAT_WARN, "         -%s\n", solvid2str(pool, obsq.elements[j]));
1277         }
1278       for (j = te->edges; od->edgedata[j]; j += 2)
1279         {
1280           te2 = od->tes + od->edgedata[j];
1281           POOL_DEBUG(SAT_WARN, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
1282         }
1283     }
1284 }
1285
1286 #if 1
1287 static void
1288 reachable(struct orderdata *od, Id i)
1289 {
1290   struct _TransactionElement *te = od->tes + i;
1291   int j, k;
1292
1293   if (te->mark != 0)
1294     return;
1295   te->mark = 1;
1296   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1297     {
1298       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1299         continue;
1300       if (!od->tes[k].mark)
1301         reachable(od, k);
1302       if (od->tes[k].mark == 2)
1303         {
1304           te->mark = 2;
1305           return;
1306         }
1307     }
1308   te->mark = -1;
1309 }
1310 #endif
1311
1312 static void
1313 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1314 {
1315 #if 0
1316   Transaction *trans = od->trans;
1317   Pool *pool = trans->pool;
1318 #endif
1319   struct _TransactionElement *te;
1320   int i, j, k, tail;
1321 #if 1
1322   int head;
1323 #endif
1324
1325 #if 0
1326   printf("addcycleedges\n");
1327   for (i = 0; (j = cycle[i]) != 0; i++)
1328     printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
1329 #endif
1330
1331   /* first add all the tail cycle edges */
1332
1333   /* see what we can reach from the cycle */
1334   queue_empty(todo);
1335   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1336     te->mark = 0;
1337   for (i = 0; (j = cycle[i]) != 0; i++)
1338     {
1339       od->tes[j].mark = -1;
1340       queue_push(todo, j);
1341     }
1342   while (todo->count)
1343     {
1344       i = queue_pop(todo);
1345       te = od->tes + i;
1346       if (te->mark > 0)
1347         continue;
1348       te->mark = te->mark < 0 ? 2 : 1;
1349       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1350         {
1351           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1352             continue;
1353           if (od->tes[k].mark > 0)
1354             continue;   /* no need to visit again */
1355           queue_push(todo, k);
1356         }
1357     }
1358   /* now all cycle TEs are marked with 2, all TEs reachable
1359    * from the cycle are marked with 1 */
1360   tail = cycle[0];
1361   od->tes[tail].mark = 1;       /* no need to add edges */
1362
1363   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1364     {
1365       if (te->mark)
1366         continue;       /* reachable from cycle */
1367       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1368         {
1369           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1370             continue;
1371           if (od->tes[k].mark != 2)
1372             continue;
1373           /* We found an edge to the cycle. Add an extra edge to the tail */
1374           /* the TE was not reachable, so we're not creating a new cycle! */
1375 #if 0
1376           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));
1377 #endif
1378           j -= te->edges;       /* in case we move */
1379           addteedge(od, i, tail, TYPE_CYCLETAIL);
1380           j += te->edges;
1381           break;        /* one edge is enough */
1382         }
1383     }
1384
1385 #if 1
1386   /* now add all head cycle edges */
1387
1388   /* reset marks */
1389   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1390     te->mark = 0;
1391   head = 0;
1392   for (i = 0; (j = cycle[i]) != 0; i++)
1393     {
1394       head = j;
1395       od->tes[j].mark = 2;
1396     }
1397   /* first the head to save some time */
1398   te = od->tes + head;
1399   for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1400     {
1401       if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1402         continue;
1403       if (!od->tes[k].mark)
1404         reachable(od, k);
1405       if (od->tes[k].mark == -1)
1406         od->tes[k].mark = -2;   /* no need for another edge */
1407     }
1408   for (i = 0; cycle[i] != 0; i++)
1409     {
1410       if (cycle[i] == head)
1411         break;
1412       te = od->tes + cycle[i];
1413       for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1414         {
1415           if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1416             continue;
1417           /* see if we can reach a cycle TE from k */
1418           if (!od->tes[k].mark)
1419             reachable(od, k);
1420           if (od->tes[k].mark == -1)
1421             {
1422 #if 0
1423               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));
1424 #endif
1425               addteedge(od, head, k, TYPE_CYCLEHEAD);
1426               od->tes[k].mark = -2;     /* no need to add that one again */
1427             }
1428         }
1429     }
1430 #endif
1431 }
1432
1433 void
1434 transaction_order(Transaction *trans, int flags)
1435 {
1436   Pool *pool = trans->pool;
1437   Queue *tr = &trans->steps;
1438   Repo *installed = pool->installed;
1439   Id p;
1440   Solvable *s;
1441   int i, j, k, numte, numedge;
1442   struct orderdata od;
1443   struct _TransactionElement *te;
1444   Queue todo, obsq, samerepoq, uninstq;
1445   int cycstart, cycel;
1446   Id *cycle;
1447   int oldcount;
1448   int start, now;
1449   Repo *lastrepo;
1450   int lastmedia;
1451   Id *temedianr;
1452
1453   start = now = sat_timems(0);
1454   POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1455   /* free old data if present */
1456   if (trans->orderdata)
1457     {
1458       struct _TransactionOrderdata *od = trans->orderdata;
1459       od->tes = sat_free(od->tes);
1460       od->invedgedata = sat_free(od->invedgedata);
1461       trans->orderdata = sat_free(trans->orderdata);
1462     }
1463
1464   /* create a transaction element for every active component */
1465   numte = 0;
1466   for (i = 0; i < tr->count; i++)
1467     {
1468       p = tr->elements[i];
1469       s = pool->solvables + p;
1470       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1471         continue;
1472       numte++;
1473     }
1474   POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1475   if (!numte)
1476     return;     /* nothing to do... */
1477
1478   numte++;      /* leave first one zero */
1479   memset(&od, 0, sizeof(od));
1480   od.trans = trans;
1481   od.ntes = numte;
1482   od.tes = sat_calloc(numte, sizeof(*od.tes));
1483   od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1484   od.edgedata[0] = 0;
1485   od.nedgedata = 1;
1486   queue_init(&od.cycles);
1487
1488   /* initialize TEs */
1489   for (i = 0, te = od.tes + 1; i < tr->count; i++)
1490     {
1491       p = tr->elements[i];
1492       s = pool->solvables + p;
1493       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1494         continue;
1495       te->p = p;
1496       te++;
1497     }
1498
1499   /* create dependency graph */
1500   for (i = 0; i < tr->count; i++)
1501     addsolvableedges(&od, pool->solvables + tr->elements[i]);
1502
1503   /* count edges */
1504   numedge = 0;
1505   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1506     for (j = te->edges; od.edgedata[j]; j += 2)
1507       numedge++;
1508   POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1509   POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1510
1511 #if 0
1512   dump_tes(&od);
1513 #endif
1514   
1515   now = sat_timems(0);
1516   /* kill all cycles */
1517   queue_init(&todo);
1518   for (i = numte - 1; i > 0; i--)
1519     queue_push(&todo, i);
1520
1521   while (todo.count)
1522     {
1523       i = queue_pop(&todo);
1524       /* printf("- look at TE %d\n", i); */
1525       if (i < 0)
1526         {
1527           i = -i;
1528           od.tes[i].mark = 2;   /* done with that one */
1529           continue;
1530         }
1531       te = od.tes + i;
1532       if (te->mark == 2)
1533         continue;               /* already finished before */
1534       if (te->mark == 0)
1535         {
1536           int edgestovisit = 0;
1537           /* new node, visit edges */
1538           for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1539             {
1540               if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1541                 continue;
1542               if (od.tes[k].mark == 2)
1543                 continue;       /* no need to visit again */
1544               if (!edgestovisit++)
1545                 queue_push(&todo, -i);  /* end of edges marker */
1546               queue_push(&todo, k);
1547             }
1548           if (!edgestovisit)
1549             te->mark = 2;       /* no edges, done with that one */
1550           else
1551             te->mark = 1;       /* under investigation */
1552           continue;
1553         }
1554       /* oh no, we found a cycle */
1555       /* find start of cycle node (<0) */
1556       for (j = todo.count - 1; j >= 0; j--)
1557         if (todo.elements[j] == -i)
1558           break;
1559       assert(j >= 0);
1560       cycstart = j;
1561       /* build te/edge chain */
1562       k = cycstart;
1563       for (j = k; j < todo.count; j++)
1564         if (todo.elements[j] < 0)
1565           todo.elements[k++] = -todo.elements[j];
1566       cycel = k - cycstart;
1567       assert(cycel > 1);
1568       /* make room for edges, two extra element for cycle loop + terminating 0 */
1569       while (todo.count < cycstart + 2 * cycel + 2)
1570         queue_push(&todo, 0);
1571       cycle = todo.elements + cycstart;
1572       cycle[cycel] = i;         /* close the loop */
1573       cycle[2 * cycel + 1] = 0; /* terminator */
1574       for (k = cycel; k > 0; k--)
1575         {
1576           cycle[k * 2] = cycle[k];
1577           te = od.tes + cycle[k - 1];
1578           assert(te->mark == 1);
1579           te->mark = 0; /* reset investigation marker */
1580           /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1581           for (j = te->edges; od.edgedata[j]; j += 2)
1582             if (od.edgedata[j] == cycle[k])
1583               break;
1584           assert(od.edgedata[j]);
1585           cycle[k * 2 - 1] = j;
1586         }
1587       /* now cycle looks like this: */
1588       /* te1 edge te2 edge te3 ... teN edge te1 0 */
1589       breakcycle(&od, cycle);
1590       /* restart with start of cycle */
1591       todo.count = cycstart + 1;
1592     }
1593   POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1594   POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1595
1596   now = sat_timems(0);
1597   /* now go through all broken cycles and create cycle edges to help
1598      the ordering */
1599    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1600      {
1601        if (od.cycles.elements[i + 2] >= TYPE_REQ)
1602          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1603      }
1604    for (i = od.cycles.count - 3; i >= 0; i -= 3)
1605      {
1606        if (od.cycles.elements[i + 2] < TYPE_REQ)
1607          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1608      }
1609   POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1610
1611   /* all edges are finally set up and there are no cycles, now the easy part.
1612    * Create an ordered transaction */
1613   now = sat_timems(0);
1614   /* first invert all edges */
1615   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1616     te->mark = 1;       /* term 0 */
1617   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1618     {
1619       for (j = te->edges; od.edgedata[j]; j += 2)
1620         {
1621           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1622             continue;
1623           od.tes[od.edgedata[j]].mark++;
1624         }
1625     }
1626   j = 1;
1627   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1628     {
1629       te->mark += j;
1630       j = te->mark;
1631     }
1632   POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1633   od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1634   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1635     {
1636       for (j = te->edges; od.edgedata[j]; j += 2)
1637         {
1638           if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1639             continue;
1640           od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1641         }
1642     }
1643   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1644     te->edges = te->mark;       /* edges now points into invedgedata */
1645   od.edgedata = sat_free(od.edgedata);
1646   od.nedgedata = j + 1;
1647
1648   /* now the final ordering */
1649   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1650     te->mark = 0;
1651   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1652     for (j = te->edges; od.invedgedata[j]; j++)
1653       od.tes[od.invedgedata[j]].mark++;
1654
1655   queue_init(&samerepoq);
1656   queue_init(&uninstq);
1657   queue_empty(&todo);
1658   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1659     if (te->mark == 0)
1660       {
1661         if (installed && pool->solvables[te->p].repo == installed)
1662           queue_push(&uninstq, i);
1663         else
1664           queue_push(&todo, i);
1665       }
1666   assert(todo.count > 0 || uninstq.count > 0);
1667   oldcount = tr->count;
1668   queue_empty(tr);
1669
1670   queue_init(&obsq);
1671   
1672   lastrepo = 0;
1673   lastmedia = 0;
1674   temedianr = sat_calloc(numte, sizeof(Id));
1675   for (i = 1; i < numte; i++)
1676     {
1677       Solvable *s = pool->solvables + od.tes[i].p;
1678       if (installed && s->repo == installed)
1679         j = 1;
1680       else
1681         j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1682       temedianr[i] = j;
1683     }
1684   for (;;)
1685     {
1686       /* select an TE i */
1687       if (uninstq.count)
1688         i = queue_shift(&uninstq);
1689       else if (samerepoq.count)
1690         i = queue_shift(&samerepoq);
1691       else if (todo.count)
1692         {
1693           /* find next repo/media */
1694           for (j = 0; j < todo.count; j++)
1695             {
1696               if (!j || temedianr[todo.elements[j]] < lastmedia)
1697                 {
1698                   i = j;
1699                   lastmedia = temedianr[todo.elements[j]];
1700                 }
1701             }
1702           lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1703
1704           /* move all matching TEs to samerepoq */
1705           for (i = j = 0; j < todo.count; j++)
1706             {
1707               int k = todo.elements[j];
1708               if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1709                 queue_push(&samerepoq, k);
1710               else
1711                 todo.elements[i++] = k;
1712             }
1713           todo.count = i;
1714
1715           assert(samerepoq.count);
1716           i = queue_shift(&samerepoq);
1717         }
1718       else
1719         break;
1720
1721       te = od.tes + i;
1722       queue_push(tr, te->p);
1723 #if 0
1724 printf("do %s [%d]\n", solvid2str(pool, te->p), temedianr[i]);
1725 #endif
1726       s = pool->solvables + te->p;
1727       for (j = te->edges; od.invedgedata[j]; j++)
1728         {
1729           struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1730           assert(te2->mark > 0);
1731           if (--te2->mark == 0)
1732             {
1733               Solvable *s = pool->solvables + te2->p;
1734 #if 0
1735 printf("free %s [%d]\n", solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1736 #endif
1737               if (installed && s->repo == installed)
1738                 queue_push(&uninstq, od.invedgedata[j]);
1739               else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1740                 queue_push(&samerepoq, od.invedgedata[j]);
1741               else
1742                 queue_push(&todo, od.invedgedata[j]);
1743             }
1744         }
1745     }
1746   sat_free(temedianr);
1747   queue_free(&todo);
1748   queue_free(&samerepoq);
1749   queue_free(&uninstq);
1750   queue_free(&obsq);
1751   for (i = 1, te = od.tes + i; i < numte; i++, te++)
1752     assert(te->mark == 0);
1753
1754   /* add back obsoleted packages */
1755   transaction_add_obsoleted(trans);
1756   assert(tr->count == oldcount);
1757
1758   POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1759   POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1760
1761   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1762     {
1763       trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1764       trans->orderdata->tes = od.tes;
1765       trans->orderdata->ntes = numte;
1766       trans->orderdata->invedgedata = od.invedgedata;
1767       trans->orderdata->ninvedgedata = od.nedgedata;
1768     }
1769   else
1770     {
1771       sat_free(od.tes);
1772       sat_free(od.invedgedata);
1773     }
1774 }
1775
1776
1777 int
1778 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1779 {
1780   int i, j;
1781   struct _TransactionOrderdata *od = trans->orderdata;
1782   struct _TransactionElement *te;
1783
1784   if (!od)
1785      return choices->count;
1786   if (!chosen)
1787     {
1788       /* initialization step */
1789       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1790         te->mark = 0;
1791       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1792         {
1793           for (j = te->edges; od->invedgedata[j]; j++)
1794             od->tes[od->invedgedata[j]].mark++;
1795         }
1796       for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1797         if (!te->mark)
1798           queue_push(choices, te->p);
1799       return choices->count;
1800     }
1801   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1802     if (te->p == chosen)
1803       break;
1804   if (i == od->ntes)
1805     return choices->count;
1806   if (te->mark > 0)
1807     {
1808       /* hey! out-of-order installation! */
1809       te->mark = -1;
1810     }
1811   for (j = te->edges; od->invedgedata[j]; j++)
1812     {
1813       te = od->tes + od->invedgedata[j];
1814       assert(te->mark > 0 || te->mark == -1);
1815       if (te->mark > 0 && --te->mark == 0)
1816         queue_push(choices, te->p);
1817     }
1818   return choices->count;
1819 }
1820
1821 void
1822 transaction_add_obsoleted(Transaction *trans)
1823 {
1824   Pool *pool = trans->pool;
1825   Repo *installed = pool->installed;
1826   Id p;
1827   Solvable *s;
1828   int i, j, k, max, oldcount;
1829   Map done;
1830   Queue obsq, *steps;
1831
1832   if (!installed || !trans->steps.count)
1833     return;
1834   /* calculate upper bound */
1835   max = 0;
1836   FOR_REPO_SOLVABLES(installed, p, s)
1837     if (MAPTST(&trans->transactsmap, p))
1838       max++;
1839   if (!max)
1840     return;
1841   /* make room */
1842   steps = &trans->steps;
1843   oldcount = steps->count;
1844   queue_insertn(steps, 0, max);
1845
1846   /* now add em */
1847   map_init(&done, installed->end - installed->start);
1848   queue_init(&obsq);
1849   for (j = 0, i = max; i < steps->count; i++)
1850     {
1851       p = trans->steps.elements[i];
1852       if (pool->solvables[p].repo == installed)
1853         {
1854           if (!trans->transaction_installed[p - pool->installed->start])
1855             trans->steps.elements[j++] = p;
1856           continue;
1857         }
1858       trans->steps.elements[j++] = p;
1859       queue_empty(&obsq);
1860       transaction_all_obs_pkgs(trans, p, &obsq);
1861       for (k = 0; k < obsq.count; k++)
1862         {
1863           p = obsq.elements[k];
1864           assert(p >= installed->start && p < installed->end);
1865           if (MAPTST(&done, p - installed->start))
1866             continue;
1867           MAPSET(&done, p - installed->start);
1868           trans->steps.elements[j++] = p;
1869         }
1870     }
1871
1872   /* free unneeded space */
1873   queue_truncate(steps, j);
1874   map_free(&done);
1875   queue_free(&obsq);
1876 }
1877
1878 static void
1879 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1880 {
1881   Pool *pool = trans->pool;
1882   Id p, pp;
1883   Solvable *s;
1884   int good;
1885
1886   if (MAPTST(seen, pkg))
1887     return;
1888   MAPSET(seen, pkg);
1889   s = pool->solvables + pkg;
1890 #if 0
1891   printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1892 #endif
1893   if (s->requires)
1894     {
1895       Id req, *reqp;
1896       int inpre = 0;
1897       reqp = s->repo->idarraydata + s->requires;
1898       while ((req = *reqp++) != 0)
1899         {
1900           if (req == SOLVABLE_PREREQMARKER)
1901             {
1902               inpre = 1;
1903               continue;
1904             }
1905           if (onlyprereq && !inpre)
1906             continue;
1907           if (!strncmp(id2str(pool, req), "rpmlib(", 7))
1908             continue;
1909           good = 0;
1910           /* first check kept packages, then freshly installed, then not yet uninstalled */
1911           FOR_PROVIDES(p, pp, req)
1912             {
1913               if (!MAPTST(ins, p))
1914                 continue;
1915               if (MAPTST(&trans->transactsmap, p))
1916                 continue;
1917               good++;
1918               transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1919             }
1920           if (!good)
1921             {
1922               FOR_PROVIDES(p, pp, req)
1923                 {
1924                   if (!MAPTST(ins, p))
1925                     continue;
1926                   if (pool->solvables[p].repo == pool->installed)
1927                     continue;
1928                   good++;
1929                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1930                 }
1931             }
1932           if (!good)
1933             {
1934               FOR_PROVIDES(p, pp, req)
1935                 {
1936                   if (!MAPTST(ins, p))
1937                     continue;
1938                   good++;
1939                   transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1940                 }
1941             }
1942           if (!good)
1943             {
1944               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));
1945             }
1946         }
1947     }
1948 }
1949
1950 void
1951 transaction_check_order(Transaction *trans)
1952 {
1953   Pool *pool = trans->pool;
1954   Solvable *s;
1955   Id p, lastins;
1956   Map ins, seen;
1957   int i;
1958
1959   POOL_DEBUG(SAT_WARN, "\nchecking transaction order...\n");
1960   map_init(&ins, pool->nsolvables);
1961   map_init(&seen, pool->nsolvables);
1962   if (pool->installed)
1963     FOR_REPO_SOLVABLES(pool->installed, p, s)
1964       MAPSET(&ins, p);
1965   lastins = 0;
1966   for (i = 0; i < trans->steps.count; i++)
1967     {
1968       p = trans->steps.elements[i];
1969       s = pool->solvables + p;
1970       if (s->repo != pool->installed)
1971         lastins = p;
1972       if (s->repo != pool->installed)
1973         MAPSET(&ins, p);
1974       if (havescripts(pool, p))
1975         {
1976           MAPZERO(&seen);
1977           transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1978         }
1979       if (s->repo == pool->installed)
1980         MAPCLR(&ins, p);
1981     }
1982   map_free(&seen);
1983   map_free(&ins);
1984   POOL_DEBUG(SAT_WARN, "transaction order check done.\n");
1985 }