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