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