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