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