2 * Copyright (c) 2007-2015, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction handling
20 #include "transaction.h"
29 obsq_sortcmp(const void *ap, const void *bp, void *dp)
33 Solvable *s, *oas, *obs;
44 s = pool->solvables + a;
45 oas = pool->solvables + oa;
46 obs = pool->solvables + ob;
47 if (oas->name != obs->name)
49 /* bring "same name" obsoleters (i.e. upgraders) to front */
50 if (oas->name == s->name)
52 if (obs->name == s->name)
54 return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
56 r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
58 return -r; /* highest version first */
63 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
65 Pool *pool = trans->pool;
66 Solvable *s = pool->solvables + p;
67 Queue *ti = &trans->transaction_info;
72 if (p <= 0 || !s->repo)
74 if (s->repo == pool->installed)
76 q = trans->transaction_installed[p - pool->installed->start];
81 /* only a single obsoleting package */
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]);
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);
98 /* find the packages we obsolete */
99 for (i = 0; i < ti->count; i += 2)
101 if (ti->elements[i] == p)
102 queue_push(pkgs, ti->elements[i + 1]);
103 else if (pkgs->count)
110 transaction_obs_pkg(Transaction *trans, Id p)
112 Pool *pool = trans->pool;
113 Solvable *s = pool->solvables + p;
117 if (p <= 0 || !s->repo)
119 if (s->repo == pool->installed)
121 p = trans->transaction_installed[p - pool->installed->start];
122 return p < 0 ? -p : p;
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];
133 * calculate base type of transaction element
137 transaction_base_type(Transaction *trans, Id p)
139 Pool *pool = trans->pool;
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)
151 return SOLVER_TRANSACTION_ERASE;
152 s = pool->solvables + p;
153 s2 = pool->solvables + p2;
154 if (s->name == s2->name)
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);
160 return SOLVER_TRANSACTION_UPGRADED;
162 return SOLVER_TRANSACTION_DOWNGRADED;
163 return SOLVER_TRANSACTION_CHANGED;
165 return SOLVER_TRANSACTION_OBSOLETED;
169 /* install or multiinstall */
170 int multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
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;
180 return SOLVER_TRANSACTION_MULTIINSTALL;
183 return SOLVER_TRANSACTION_INSTALL;
184 s = pool->solvables + p;
185 s2 = pool->solvables + p2;
186 if (s->name == s2->name)
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);
192 return SOLVER_TRANSACTION_UPGRADE;
194 return SOLVER_TRANSACTION_DOWNGRADE;
196 return SOLVER_TRANSACTION_CHANGE;
198 return SOLVER_TRANSACTION_OBSOLETES;
202 /* these packages do not get installed by the package manager */
204 is_pseudo_package(Pool *pool, Solvable *s)
206 const char *n = pool_id2str(pool, s->name);
207 if (*n == 'p' && !strncmp(n, "patch:", 6))
209 if (*n == 'p' && !strncmp(n, "pattern:", 8))
211 if (*n == 'p' && !strncmp(n, "product:", 8))
213 if (*n == 'a' && !strncmp(n, "application:", 12))
218 /* these packages will never show up installed */
220 is_noinst_pseudo_package(Pool *pool, Solvable *s)
222 const char *n = pool_id2str(pool, s->name);
223 if (!strncmp(n, "patch:", 6))
225 if (!strncmp(n, "pattern:", 8))
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 */
232 Id prv, *prvp = s->repo->idarraydata + s->provides;
233 while ((prv = *prvp++) != 0)
234 if (ISRELDEP(prv) && !strcmp(pool_id2str(pool, prv), "autopattern()"))
244 obsoleted_by_pseudos_only(Transaction *trans, Id p)
246 Pool *pool = trans->pool;
251 op = transaction_obs_pkg(trans, p);
252 if (op && !is_pseudo_package(pool, pool->solvables + op))
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]))
259 i = !q.count || i < q.count ? 0 : 1;
265 * return type of transaction element
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 */
272 transaction_type(Transaction *trans, Id p, int mode)
274 Pool *pool = trans->pool;
275 Solvable *s = pool->solvables + p;
281 return SOLVER_TRANSACTION_IGNORE;
283 /* XXX: SUSE only? */
284 if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_noinst_pseudo_package(pool, s))
285 return SOLVER_TRANSACTION_IGNORE;
287 type = transaction_base_type(trans, p);
289 if (type == SOLVER_TRANSACTION_IGNORE)
290 return SOLVER_TRANSACTION_IGNORE; /* not part of the transaction */
292 if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
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)
299 if (s->repo == pool->installed)
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 */
306 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
307 return SOLVER_TRANSACTION_MULTIINSTALL;
308 return SOLVER_TRANSACTION_INSTALL;
311 if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
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;
321 if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
323 /* application wants to make no difference between change
325 if (type == SOLVER_TRANSACTION_CHANGED)
326 type = SOLVER_TRANSACTION_REINSTALLED;
327 else if (type == SOLVER_TRANSACTION_CHANGE)
328 type = SOLVER_TRANSACTION_REINSTALL;
331 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
334 if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
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;
343 if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
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;
353 /* the element doesn't match the show mode */
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;
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 */
363 /* most of the time there's only one reference, so check it first */
364 q = transaction_obs_pkg(trans, p);
366 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
368 Solvable *sq = pool->solvables + q;
369 if (sq->name != s->name)
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;
377 return SOLVER_TRANSACTION_INSTALL;
381 /* if there's a match, p will be shown when q
383 if (transaction_obs_pkg(trans, q) == p)
384 return SOLVER_TRANSACTION_IGNORE;
386 /* too bad, a miss. check em all */
389 transaction_all_obs_pkgs(trans, p, &oq);
390 for (i = 0; i < oq.count; i++)
393 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
395 Solvable *sq = pool->solvables + q;
396 if (sq->name != s->name)
399 /* check if we are referenced? */
400 if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
402 transaction_all_obs_pkgs(trans, q, &rq);
403 for (j = 0; j < rq.count; j++)
404 if (rq.elements[j] == p)
412 else if (transaction_obs_pkg(trans, q) == p)
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;
429 return SOLVER_TRANSACTION_INSTALL;
431 /* there was a ref, so p is shown with some other package */
432 return SOLVER_TRANSACTION_IGNORE;
438 classify_cmp(const void *ap, const void *bp, void *dp)
440 Transaction *trans = dp;
441 Pool *pool = trans->pool;
451 return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
454 return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
459 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
461 Transaction *trans = dp;
462 Pool *pool = trans->pool;
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)
473 int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
481 queue_push4(Queue *q, Id id1, Id id2, Id id3, Id id4)
490 queue_unshift4(Queue *q, Id id1, Id id2, Id id3, Id id4)
492 queue_unshift(q, id4);
493 queue_unshift(q, id3);
494 queue_unshift(q, id2);
495 queue_unshift(q, id1);
499 transaction_classify(Transaction *trans, int mode, Queue *classes)
501 Pool *pool = trans->pool;
502 int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
504 Id v, vq, type, p, q;
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++)
512 p = trans->steps.elements[i];
513 s = pool->solvables + p;
514 type = transaction_type(trans, p, mode);
516 if (!pool->installed || s->repo != pool->installed)
518 /* don't report vendor/arch changes if we were mapped to erase. */
519 if (type == SOLVER_TRANSACTION_ERASE)
521 /* look at arch/vendor changes */
522 q = transaction_obs_pkg(trans, p);
525 sq = pool->solvables + q;
531 if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 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)
536 if (j == classes->count)
537 queue_push4(classes, SOLVER_TRANSACTION_ARCHCHANGE, 1, v, vq);
539 classes->elements[j + 1]++;
542 v = s->vendor ? s->vendor : 1;
543 vq = sq->vendor ? sq->vendor : 1;
546 if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 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)
551 if (j == classes->count)
552 queue_push4(classes, SOLVER_TRANSACTION_VENDORCHANGE, 1, v, vq);
554 classes->elements[j + 1]++;
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;
563 queue_unshift4(classes, i, ntypes[i], 0, 0);
564 for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
568 if (i == SOLVER_TRANSACTION_ERASE)
570 queue_unshift4(classes, i, ntypes[i], 0, 0);
575 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
577 Pool *pool = trans->pool;
583 for (i = 0; i < trans->steps.count; i++)
585 p = trans->steps.elements[i];
586 s = pool->solvables + p;
587 if (class <= SOLVER_TRANSACTION_MAXTYPE)
589 type = transaction_type(trans, p, mode);
594 if (!pool->installed || s->repo != pool->installed)
596 q = transaction_obs_pkg(trans, p);
599 sq = pool->solvables + q;
600 if (class == SOLVER_TRANSACTION_ARCHCHANGE)
602 if ((!from && !to) || (s->arch == from && sq->arch == to))
606 if (class == SOLVER_TRANSACTION_VENDORCHANGE)
608 Id v = s->vendor ? s->vendor : 1;
609 Id vq = sq->vendor ? sq->vendor : 1;
610 if ((!from && !to) || (v == from && vq == to))
616 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
620 create_transaction_info(Transaction *trans, Queue *decisionq)
622 Pool *pool = trans->pool;
623 Queue *ti = &trans->transaction_info;
624 Repo *installed = pool->installed;
630 trans->transaction_installed = solv_free(trans->transaction_installed);
632 return; /* no info needed */
633 for (i = 0; i < decisionq->count; i++)
635 p = decisionq->elements[i];
636 if (p <= 0 || p == SYSTEMSOLVABLE)
638 s = pool->solvables + p;
639 if (!s->repo || s->repo == installed)
641 multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
642 FOR_PROVIDES(p2, pp2, s->name)
644 if (!MAPTST(&trans->transactsmap, p2))
646 s2 = pool->solvables + p2;
647 if (s2->repo != installed)
649 if (multi && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
651 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
653 if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
655 queue_push2(ti, p, p2);
657 if (s->obsoletes && !multi)
659 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
660 while ((obs = *obsp++) != 0)
662 FOR_PROVIDES(p2, pp2, obs)
664 if (!MAPTST(&trans->transactsmap, p2))
666 s2 = pool->solvables + p2;
667 if (s2->repo != installed)
669 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, s2, obs))
671 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
673 queue_push2(ti, p, p2);
681 solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
682 for (i = j = 2; i < ti->count; i += 2)
684 if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
686 ti->elements[j++] = ti->elements[i];
687 ti->elements[j++] = ti->elements[i + 1];
689 queue_truncate(ti, j);
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)
697 j = ti->elements[i + 1] - installed->start;
698 if (!trans->transaction_installed[j])
699 trans->transaction_installed[j] = ti->elements[i];
702 /* more than one package obsoletes us. compare to find "best" */
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];
715 /* create a transaction from the decisionq */
717 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *multiversionmap)
719 Repo *installed = pool->installed;
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);
731 for (i = 0; i < decisionq->count; i++)
733 p = decisionq->elements[i];
734 s = pool->solvables + (p > 0 ? p : -p);
737 if (installed && s->repo == installed && p < 0)
738 MAPSET(&trans->transactsmap, -p);
739 if (!(installed && s->repo == installed) && p > 0)
741 MAPSET(&trans->transactsmap, p);
742 if (multiversionmap && MAPTST(multiversionmap, p))
746 MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
748 map_init_clone(&trans->multiversionmap, multiversionmap);
750 create_transaction_info(trans, decisionq);
754 FOR_REPO_SOLVABLES(installed, p, s)
756 if (MAPTST(&trans->transactsmap, p))
757 queue_push(&trans->steps, p);
760 for (i = 0; i < decisionq->count; i++)
762 p = decisionq->elements[i];
763 if (p > 0 && MAPTST(&trans->transactsmap, p))
764 queue_push(&trans->steps, p);
770 transaction_installedresult(Transaction *trans, Queue *installedq)
772 Pool *pool = trans->pool;
773 Repo *installed = pool->installed;
778 queue_empty(installedq);
779 /* first the new installs, than the kept packages */
780 for (i = 0; i < trans->steps.count; i++)
782 p = trans->steps.elements[i];
783 s = pool->solvables + p;
784 if (installed && s->repo == installed)
786 queue_push(installedq, p);
788 cutoff = installedq->count;
791 FOR_REPO_SOLVABLES(installed, p, s)
792 if (!MAPTST(&trans->transactsmap, p))
793 queue_push(installedq, p);
799 transaction_make_installedmap(Transaction *trans, Map *installedmap)
801 Pool *pool = trans->pool;
802 Repo *installed = pool->installed;
807 map_init(installedmap, pool->nsolvables);
808 for (i = 0; i < trans->steps.count; i++)
810 p = trans->steps.elements[i];
811 s = pool->solvables + p;
812 if (!installed || s->repo != installed)
813 MAPSET(installedmap, p);
817 FOR_REPO_SOLVABLES(installed, p, s)
818 if (!MAPTST(&trans->transactsmap, p))
819 MAPSET(installedmap, p);
824 transaction_calc_installsizechange(Transaction *trans)
829 transaction_make_installedmap(trans, &installedmap);
830 change = pool_calc_installsizechange(trans->pool, &installedmap);
831 map_free(&installedmap);
836 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
840 transaction_make_installedmap(trans, &installedmap);
841 pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
842 map_free(&installedmap);
846 transaction_create(Pool *pool)
848 Transaction *trans = solv_calloc(1, sizeof(*trans));
854 transaction_create_clone(Transaction *srctrans)
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)
861 Repo *installed = srctrans->pool->installed;
862 trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
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);
872 transaction_free(Transaction *trans)
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);