Imported Upstream version 0.6.13
[platform/upstream/libsolv.git] / src / transaction.c
1 /*
2  * Copyright (c) 2007-2015, SUSE LLC
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       /* bring "same name" obsoleters (i.e. upgraders) to front */
50       if (oas->name == s->name)
51         return -1;
52       if (obs->name == s->name)
53         return 1;
54       return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
55     }
56   r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
57   if (r)
58     return -r;  /* highest version first */
59   return oa - ob;
60 }
61
62 void
63 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
64 {
65   Pool *pool = trans->pool;
66   Solvable *s = pool->solvables + p;
67   Queue *ti = &trans->transaction_info;
68   Id q;
69   int i;
70
71   queue_empty(pkgs);
72   if (p <= 0 || !s->repo)
73     return;
74   if (s->repo == pool->installed)
75     {
76       q = trans->transaction_installed[p - pool->installed->start];
77       if (!q)
78         return;
79       if (q > 0)
80         {
81           /* only a single obsoleting package */
82           queue_push(pkgs, q);
83           return;
84         }
85       /* find which packages obsolete us */
86       for (i = 0; i < ti->count; i += 2)
87         if (ti->elements[i + 1] == p)
88           queue_push2(pkgs, p, ti->elements[i]);
89       /* sort obsoleters */
90       if (pkgs->count > 2)
91         solv_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
92       for (i = 0; i < pkgs->count; i += 2)
93         pkgs->elements[i / 2] = pkgs->elements[i + 1];
94       queue_truncate(pkgs, pkgs->count / 2);
95     }
96   else
97     {
98       /* find the packages we obsolete */
99       for (i = 0; i < ti->count; i += 2)
100         {
101           if (ti->elements[i] == p)
102             queue_push(pkgs, ti->elements[i + 1]);
103           else if (pkgs->count)
104             break;
105         }
106     }
107 }
108
109 Id
110 transaction_obs_pkg(Transaction *trans, Id p)
111 {
112   Pool *pool = trans->pool;
113   Solvable *s = pool->solvables + p;
114   Queue *ti;
115   int i;
116
117   if (p <= 0 || !s->repo)
118     return 0;
119   if (s->repo == pool->installed)
120     {
121       p = trans->transaction_installed[p - pool->installed->start];
122       return p < 0 ? -p : p;
123     }
124   ti = &trans->transaction_info;
125   for (i = 0; i < ti->count; i += 2)
126     if (ti->elements[i] == p)
127       return ti->elements[i + 1];
128   return 0;
129 }
130
131
132 /*
133  * calculate base type of transaction element
134  */
135
136 static Id
137 transaction_base_type(Transaction *trans, Id p)
138 {
139   Pool *pool = trans->pool;
140   Solvable *s, *s2;
141   int r;
142   Id p2;
143
144   if (!MAPTST(&trans->transactsmap, p))
145     return SOLVER_TRANSACTION_IGNORE;
146   p2 = transaction_obs_pkg(trans, p);
147   if (pool->installed && pool->solvables[p].repo == pool->installed)
148     {
149       /* erase */
150       if (!p2)
151         return SOLVER_TRANSACTION_ERASE;
152       s = pool->solvables + p;
153       s2 = pool->solvables + p2;
154       if (s->name == s2->name)
155         {
156           if (s->evr == s2->evr && solvable_identical(s, s2))
157             return SOLVER_TRANSACTION_REINSTALLED;
158           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
159           if (r < 0)
160             return SOLVER_TRANSACTION_UPGRADED;
161           else if (r > 0)
162             return SOLVER_TRANSACTION_DOWNGRADED;
163           return SOLVER_TRANSACTION_CHANGED;
164         }
165       return SOLVER_TRANSACTION_OBSOLETED;
166     }
167   else
168     {
169       /* install or multiinstall */
170       int multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
171       if (multi)
172         {
173           if (p2)
174             {
175               s = pool->solvables + p;
176               s2 = pool->solvables + p2;
177               if (s->name == s2->name && s->arch == s2->arch && s->evr == s2->evr)
178                 return SOLVER_TRANSACTION_MULTIREINSTALL;
179             }
180           return SOLVER_TRANSACTION_MULTIINSTALL;
181         }
182       if (!p2)
183         return SOLVER_TRANSACTION_INSTALL;
184       s = pool->solvables + p;
185       s2 = pool->solvables + p2;
186       if (s->name == s2->name)
187         {
188           if (s->evr == s2->evr && solvable_identical(s, s2))
189             return SOLVER_TRANSACTION_REINSTALL;
190           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
191           if (r > 0)
192             return SOLVER_TRANSACTION_UPGRADE;
193           else if (r < 0)
194             return SOLVER_TRANSACTION_DOWNGRADE;
195           else
196             return SOLVER_TRANSACTION_CHANGE;
197         }
198       return SOLVER_TRANSACTION_OBSOLETES;
199     }
200 }
201
202 /* these packages do not get installed by the package manager */
203 static inline int
204 is_pseudo_package(Pool *pool, Solvable *s)
205 {
206   const char *n = pool_id2str(pool, s->name);
207   if (*n == 'p' && !strncmp(n, "patch:", 6))
208     return 1;
209   if (*n == 'p' && !strncmp(n, "pattern:", 8))
210     return 1;
211   if (*n == 'p' && !strncmp(n, "product:", 8))
212     return 1;
213   if (*n == 'a' && !strncmp(n, "application:", 12))
214     return 1;
215   return 0;
216 }
217
218 /* these packages will never show up installed */
219 static inline int
220 is_noinst_pseudo_package(Pool *pool, Solvable *s)
221 {
222   const char *n = pool_id2str(pool, s->name);
223   if (!strncmp(n, "patch:", 6))
224     return 1;
225   if (!strncmp(n, "pattern:", 8))
226     {
227 #if defined(SUSE) && defined(ENABLE_LINKED_PKGS)
228       /* unlike normal patterns, autopatterns *can* be installed (via the package link),
229          so do not filter them */
230       if (s->provides)
231         {
232           Id prv, *prvp = s->repo->idarraydata + s->provides;
233           while ((prv = *prvp++) != 0)
234             if (ISRELDEP(prv) && !strcmp(pool_id2str(pool, prv), "autopattern()"))
235               return 0;
236         }
237 #endif
238       return 1;
239     }
240   return 0;
241 }
242
243 static int
244 obsoleted_by_pseudos_only(Transaction *trans, Id p)
245 {
246   Pool *pool = trans->pool;
247   Queue q;
248   Id op;
249   int i;
250
251   op = transaction_obs_pkg(trans, p);
252   if (op && !is_pseudo_package(pool, pool->solvables + op))
253     return 0;
254   queue_init(&q);
255   transaction_all_obs_pkgs(trans, p, &q);
256   for (i = 0; i < q.count; i++)
257     if (!is_pseudo_package(pool, pool->solvables + q.elements[i]))
258       break;
259   i = !q.count || i < q.count ? 0 : 1;
260   queue_free(&q);
261   return i;
262 }
263
264 /*
265  * return type of transaction element
266  *
267  * filtering is needed if either not all packages are shown
268  * or replaces are not shown, as otherwise parts of the
269  * transaction might not be shown to the user */
270
271 Id
272 transaction_type(Transaction *trans, Id p, int mode)
273 {
274   Pool *pool = trans->pool;
275   Solvable *s = pool->solvables + p;
276   Queue oq, rq;
277   Id type, q;
278   int i, j, ref = 0;
279
280   if (!s->repo)
281     return SOLVER_TRANSACTION_IGNORE;
282
283   /* XXX: SUSE only? */
284   if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_noinst_pseudo_package(pool, s))
285     return SOLVER_TRANSACTION_IGNORE;
286
287   type = transaction_base_type(trans, p);
288
289   if (type == SOLVER_TRANSACTION_IGNORE)
290     return SOLVER_TRANSACTION_IGNORE;   /* not part of the transaction */
291
292   if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
293     {
294       /* application wants to know what to feed to the package manager */
295       if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_pseudo_package(pool, s))
296         return SOLVER_TRANSACTION_IGNORE;
297       if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
298         return type;
299       if (s->repo == pool->installed)
300         {
301           /* check if we're a real package that is obsoleted by pseudos */
302           if (!is_pseudo_package(pool, s) && obsoleted_by_pseudos_only(trans, s - pool->solvables))
303             return SOLVER_TRANSACTION_ERASE;
304           return SOLVER_TRANSACTION_IGNORE;     /* ignore as we're being obsoleted */
305         }
306       if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
307         return SOLVER_TRANSACTION_MULTIINSTALL;
308       return SOLVER_TRANSACTION_INSTALL;
309     }
310
311   if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
312     {
313       /* application wants to make no difference between install
314        * and multiinstall */
315       if (type == SOLVER_TRANSACTION_MULTIINSTALL)
316         type = SOLVER_TRANSACTION_INSTALL;
317       if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
318         type = SOLVER_TRANSACTION_REINSTALL;
319     }
320
321   if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
322     {
323       /* application wants to make no difference between change
324        * and reinstall */
325       if (type == SOLVER_TRANSACTION_CHANGED)
326         type = SOLVER_TRANSACTION_REINSTALLED;
327       else if (type == SOLVER_TRANSACTION_CHANGE)
328         type = SOLVER_TRANSACTION_REINSTALL;
329     }
330
331   if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
332     return type;
333
334   if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
335     {
336       /* erase element and we're showing the passive side */
337       if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
338         type = SOLVER_TRANSACTION_ERASE;
339       if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
340         type = SOLVER_TRANSACTION_UPGRADED;
341       return type;
342     }
343   if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
344     {
345       /* install element and we're showing the active side */
346       if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
347         type = SOLVER_TRANSACTION_INSTALL;
348       if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
349         type = SOLVER_TRANSACTION_UPGRADE;
350       return type;
351     }
352
353   /* the element doesn't match the show mode */
354
355   /* if we're showing all references, we can ignore this package */
356   if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
357     return SOLVER_TRANSACTION_IGNORE;
358
359   /* we're not showing all refs. check if some other package
360    * references us. If yes, it's safe to ignore this package,
361    * otherwise we need to map the type */
362
363   /* most of the time there's only one reference, so check it first */
364   q = transaction_obs_pkg(trans, p);
365
366   if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
367     {
368       Solvable *sq = pool->solvables + q;
369       if (sq->name != s->name)
370         {
371           /* it's a replace but we're not showing replaces. map type. */
372           if (s->repo == pool->installed)
373             return SOLVER_TRANSACTION_ERASE;
374           else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
375             return SOLVER_TRANSACTION_MULTIINSTALL;
376           else
377             return SOLVER_TRANSACTION_INSTALL;
378         }
379     }
380
381   /* if there's a match, p will be shown when q
382    * is processed */
383   if (transaction_obs_pkg(trans, q) == p)
384     return SOLVER_TRANSACTION_IGNORE;
385
386   /* too bad, a miss. check em all */
387   queue_init(&oq);
388   queue_init(&rq);
389   transaction_all_obs_pkgs(trans, p, &oq);
390   for (i = 0; i < oq.count; i++)
391     {
392       q = oq.elements[i];
393       if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
394         {
395           Solvable *sq = pool->solvables + q;
396           if (sq->name != s->name)
397             continue;
398         }
399       /* check if we are referenced? */
400       if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
401         {
402           transaction_all_obs_pkgs(trans, q, &rq);
403           for (j = 0; j < rq.count; j++)
404             if (rq.elements[j] == p)
405               {
406                 ref = 1;
407                 break;
408               }
409           if (ref)
410             break;
411         }
412       else if (transaction_obs_pkg(trans, q) == p)
413         {
414           ref = 1;
415           break;
416         }
417     }
418   queue_free(&oq);
419   queue_free(&rq);
420
421   if (!ref)
422     {
423       /* we're not referenced. map type */
424       if (s->repo == pool->installed)
425         return SOLVER_TRANSACTION_ERASE;
426       else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
427         return SOLVER_TRANSACTION_MULTIINSTALL;
428       else
429         return SOLVER_TRANSACTION_INSTALL;
430     }
431   /* there was a ref, so p is shown with some other package */
432   return SOLVER_TRANSACTION_IGNORE;
433 }
434
435
436
437 static int
438 classify_cmp(const void *ap, const void *bp, void *dp)
439 {
440   Transaction *trans = dp;
441   Pool *pool = trans->pool;
442   const Id *a = ap;
443   const Id *b = bp;
444   int r;
445
446   r = a[0] - b[0];
447   if (r)
448     return r;
449   r = a[2] - b[2];
450   if (r)
451     return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
452   r = a[3] - b[3];
453   if (r)
454     return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
455   return 0;
456 }
457
458 static int
459 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
460 {
461   Transaction *trans = dp;
462   Pool *pool = trans->pool;
463   Id a = *(Id *)ap;
464   Id b = *(Id *)bp;
465   Solvable *sa, *sb;
466
467   sa = pool->solvables + a;
468   sb = pool->solvables + b;
469   if (sa->name != sb->name)
470     return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
471   if (sa->evr != sb->evr)
472     {
473       int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
474       if (r)
475         return r;
476     }
477   return a - b;
478 }
479
480 static inline void
481 queue_push4(Queue *q, Id id1, Id id2, Id id3, Id id4)
482 {
483   queue_push(q, id1);
484   queue_push(q, id2);
485   queue_push(q, id3);
486   queue_push(q, id4);
487 }
488
489 static inline void
490 queue_unshift4(Queue *q, Id id1, Id id2, Id id3, Id id4)
491 {
492   queue_unshift(q, id4);
493   queue_unshift(q, id3);
494   queue_unshift(q, id2);
495   queue_unshift(q, id1);
496 }
497
498 void
499 transaction_classify(Transaction *trans, int mode, Queue *classes)
500 {
501   Pool *pool = trans->pool;
502   int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
503   Solvable *s, *sq;
504   Id v, vq, type, p, q;
505   int i, j;
506
507   queue_empty(classes);
508   memset(ntypes, 0, sizeof(ntypes));
509   /* go through transaction and classify each step */
510   for (i = 0; i < trans->steps.count; i++)
511     {
512       p = trans->steps.elements[i];
513       s = pool->solvables + p;
514       type = transaction_type(trans, p, mode);
515       ntypes[type]++;
516       if (!pool->installed || s->repo != pool->installed)
517         continue;
518       /* don't report vendor/arch changes if we were mapped to erase. */
519       if (type == SOLVER_TRANSACTION_ERASE)
520         continue;
521       /* look at arch/vendor changes */
522       q = transaction_obs_pkg(trans, p);
523       if (!q)
524         continue;
525       sq = pool->solvables + q;
526
527       v = s->arch;
528       vq = sq->arch;
529       if (v != vq)
530         {
531           if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
532             v = vq = 0;
533           for (j = 0; j < classes->count; j += 4)
534             if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
535               break;
536           if (j == classes->count)
537             queue_push4(classes, SOLVER_TRANSACTION_ARCHCHANGE, 1, v, vq);
538           else
539             classes->elements[j + 1]++;
540         }
541
542       v = s->vendor ? s->vendor : 1;
543       vq = sq->vendor ? sq->vendor : 1;
544       if (v != vq)
545         {
546           if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
547             v = vq = 0;
548           for (j = 0; j < classes->count; j += 4)
549             if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
550               break;
551           if (j == classes->count)
552             queue_push4(classes, SOLVER_TRANSACTION_VENDORCHANGE, 1, v, vq);
553           else
554             classes->elements[j + 1]++;
555         }
556     }
557   /* now sort all vendor/arch changes */
558   if (classes->count > 4)
559     solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
560   /* finally add all classes. put erases last */
561   i = SOLVER_TRANSACTION_ERASE;
562   if (ntypes[i])
563     queue_unshift4(classes, i, ntypes[i], 0, 0);
564   for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
565     {
566       if (!ntypes[i])
567         continue;
568       if (i == SOLVER_TRANSACTION_ERASE)
569         continue;
570       queue_unshift4(classes, i, ntypes[i], 0, 0);
571     }
572 }
573
574 void
575 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
576 {
577   Pool *pool = trans->pool;
578   int i;
579   Id type, p, q;
580   Solvable *s, *sq;
581
582   queue_empty(pkgs);
583   for (i = 0; i < trans->steps.count; i++)
584     {
585       p = trans->steps.elements[i];
586       s = pool->solvables + p;
587       if (class <= SOLVER_TRANSACTION_MAXTYPE)
588         {
589           type = transaction_type(trans, p, mode);
590           if (type == class)
591             queue_push(pkgs, p);
592           continue;
593         }
594       if (!pool->installed || s->repo != pool->installed)
595         continue;
596       q = transaction_obs_pkg(trans, p);
597       if (!q)
598         continue;
599       sq = pool->solvables + q;
600       if (class == SOLVER_TRANSACTION_ARCHCHANGE)
601         {
602           if ((!from && !to) || (s->arch == from && sq->arch == to))
603             queue_push(pkgs, p);
604           continue;
605         }
606       if (class == SOLVER_TRANSACTION_VENDORCHANGE)
607         {
608           Id v = s->vendor ? s->vendor : 1;
609           Id vq = sq->vendor ? sq->vendor : 1;
610           if ((!from && !to) || (v == from && vq == to))
611             queue_push(pkgs, p);
612           continue;
613         }
614     }
615   if (pkgs->count > 1)
616     solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
617 }
618
619 static void
620 create_transaction_info(Transaction *trans, Queue *decisionq)
621 {
622   Pool *pool = trans->pool;
623   Queue *ti = &trans->transaction_info;
624   Repo *installed = pool->installed;
625   int i, j, multi;
626   Id p, p2, pp2;
627   Solvable *s, *s2;
628
629   queue_empty(ti);
630   trans->transaction_installed = solv_free(trans->transaction_installed);
631   if (!installed)
632     return;     /* no info needed */
633   for (i = 0; i < decisionq->count; i++)
634     {
635       p = decisionq->elements[i];
636       if (p <= 0 || p == SYSTEMSOLVABLE)
637         continue;
638       s = pool->solvables + p;
639       if (!s->repo || s->repo == installed)
640         continue;
641       multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
642       FOR_PROVIDES(p2, pp2, s->name)
643         {
644           if (!MAPTST(&trans->transactsmap, p2))
645             continue;
646           s2 = pool->solvables + p2;
647           if (s2->repo != installed)
648             continue;
649           if (multi && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
650             continue;
651           if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
652             continue;
653           if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
654             continue;
655           queue_push2(ti, p, p2);
656         }
657       if (s->obsoletes && !multi)
658         {
659           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
660           while ((obs = *obsp++) != 0)
661             {
662               FOR_PROVIDES(p2, pp2, obs)
663                 {
664                   if (!MAPTST(&trans->transactsmap, p2))
665                     continue;
666                   s2 = pool->solvables + p2;
667                   if (s2->repo != installed)
668                     continue;
669                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, s2, obs))
670                     continue;
671                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
672                     continue;
673                   queue_push2(ti, p, p2);
674                 }
675             }
676         }
677     }
678   if (ti->count > 2)
679     {
680       /* sort and unify */
681       solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
682       for (i = j = 2; i < ti->count; i += 2)
683         {
684           if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
685             continue;
686           ti->elements[j++] = ti->elements[i];
687           ti->elements[j++] = ti->elements[i + 1];
688         }
689       queue_truncate(ti, j);
690     }
691
692   /* create transaction_installed helper */
693   /*   entry > 0: exactly one obsoleter, entry < 0: multiple obsoleters, -entry is "best" */
694   trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
695   for (i = 0; i < ti->count; i += 2)
696     {
697       j = ti->elements[i + 1] - installed->start;
698       if (!trans->transaction_installed[j])
699         trans->transaction_installed[j] = ti->elements[i];
700       else
701         {
702           /* more than one package obsoletes us. compare to find "best" */
703           Id q[4];
704           if (trans->transaction_installed[j] > 0)
705             trans->transaction_installed[j] = -trans->transaction_installed[j];
706           q[0] = q[2] = ti->elements[i + 1];
707           q[1] = ti->elements[i];
708           q[3] = -trans->transaction_installed[j];
709           if (obsq_sortcmp(q, q + 2, pool) < 0)
710             trans->transaction_installed[j] = -ti->elements[i];
711         }
712     }
713 }
714
715 /* create a transaction from the decisionq */
716 Transaction *
717 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *multiversionmap)
718 {
719   Repo *installed = pool->installed;
720   int i, needmulti;
721   Id p;
722   Solvable *s;
723   Transaction *trans;
724
725   trans = transaction_create(pool);
726   if (multiversionmap && !multiversionmap->size)
727     multiversionmap = 0;        /* ignore empty map */
728   queue_empty(&trans->steps);
729   map_init(&trans->transactsmap, pool->nsolvables);
730   needmulti = 0;
731   for (i = 0; i < decisionq->count; i++)
732     {
733       p = decisionq->elements[i];
734       s = pool->solvables + (p > 0 ? p : -p);
735       if (!s->repo)
736         continue;
737       if (installed && s->repo == installed && p < 0)
738         MAPSET(&trans->transactsmap, -p);
739       if (!(installed && s->repo == installed) && p > 0)
740         {
741           MAPSET(&trans->transactsmap, p);
742           if (multiversionmap && MAPTST(multiversionmap, p))
743             needmulti = 1;
744         }
745     }
746   MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
747   if (needmulti)
748     map_init_clone(&trans->multiversionmap, multiversionmap);
749
750   create_transaction_info(trans, decisionq);
751
752   if (installed)
753     {
754       FOR_REPO_SOLVABLES(installed, p, s)
755         {
756           if (MAPTST(&trans->transactsmap, p))
757             queue_push(&trans->steps, p);
758         }
759     }
760   for (i = 0; i < decisionq->count; i++)
761     {
762       p = decisionq->elements[i];
763       if (p > 0 && MAPTST(&trans->transactsmap, p))
764         queue_push(&trans->steps, p);
765     }
766   return trans;
767 }
768
769 int
770 transaction_installedresult(Transaction *trans, Queue *installedq)
771 {
772   Pool *pool = trans->pool;
773   Repo *installed = pool->installed;
774   Solvable *s;
775   int i, cutoff;
776   Id p;
777
778   queue_empty(installedq);
779   /* first the new installs, than the kept packages */
780   for (i = 0; i < trans->steps.count; i++)
781     {
782       p = trans->steps.elements[i];
783       s = pool->solvables + p;
784       if (installed && s->repo == installed)
785         continue;
786       queue_push(installedq, p);
787     }
788   cutoff = installedq->count;
789   if (installed)
790     {
791       FOR_REPO_SOLVABLES(installed, p, s)
792         if (!MAPTST(&trans->transactsmap, p))
793           queue_push(installedq, p);
794     }
795   return cutoff;
796 }
797
798 static void
799 transaction_make_installedmap(Transaction *trans, Map *installedmap)
800 {
801   Pool *pool = trans->pool;
802   Repo *installed = pool->installed;
803   Solvable *s;
804   Id p;
805   int i;
806
807   map_init(installedmap, pool->nsolvables);
808   for (i = 0; i < trans->steps.count; i++)
809     {
810       p = trans->steps.elements[i];
811       s = pool->solvables + p;
812       if (!installed || s->repo != installed)
813         MAPSET(installedmap, p);
814     }
815   if (installed)
816     {
817       FOR_REPO_SOLVABLES(installed, p, s)
818         if (!MAPTST(&trans->transactsmap, p))
819           MAPSET(installedmap, p);
820     }
821 }
822
823 int
824 transaction_calc_installsizechange(Transaction *trans)
825 {
826   Map installedmap;
827   int change;
828
829   transaction_make_installedmap(trans, &installedmap);
830   change = pool_calc_installsizechange(trans->pool, &installedmap);
831   map_free(&installedmap);
832   return change;
833 }
834
835 void
836 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
837 {
838   Map installedmap;
839
840   transaction_make_installedmap(trans, &installedmap);
841   pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
842   map_free(&installedmap);
843 }
844
845 Transaction *
846 transaction_create(Pool *pool)
847 {
848   Transaction *trans = solv_calloc(1, sizeof(*trans));
849   trans->pool = pool;
850   return trans;
851 }
852
853 Transaction *
854 transaction_create_clone(Transaction *srctrans)
855 {
856   Transaction *trans = transaction_create(srctrans->pool);
857   queue_init_clone(&trans->steps, &srctrans->steps);
858   queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
859   if (srctrans->transaction_installed)
860     {
861       Repo *installed = srctrans->pool->installed;
862       trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
863     }
864   map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
865   map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
866   if (srctrans->orderdata)
867     transaction_clone_orderdata(trans, srctrans);
868   return trans;
869 }
870
871 void
872 transaction_free(Transaction *trans)
873 {
874   queue_free(&trans->steps);
875   queue_free(&trans->transaction_info);
876   trans->transaction_installed = solv_free(trans->transaction_installed);
877   map_free(&trans->transactsmap);
878   map_free(&trans->multiversionmap);
879   if (trans->orderdata)
880     transaction_free_orderdata(trans);
881   free(trans);
882 }
883