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