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