2 * Copyright (c) 2007-2009, Novell Inc.
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, pool->solvables + p2, 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);
845 struct _TransactionElement {
846 Id p; /* solvable id */
847 Id edges; /* pointer into edges data */
851 struct _TransactionOrderdata {
852 struct _TransactionElement *tes;
858 #define TYPE_BROKEN (1<<0)
859 #define TYPE_CON (1<<1)
861 #define TYPE_REQ_P (1<<2)
862 #define TYPE_PREREQ_P (1<<3)
864 #define TYPE_REQ (1<<4)
865 #define TYPE_PREREQ (1<<5)
867 #define TYPE_CYCLETAIL (1<<16)
868 #define TYPE_CYCLEHEAD (1<<17)
870 #define EDGEDATA_BLOCK 127
873 transaction_create(Pool *pool)
875 Transaction *trans = solv_calloc(1, sizeof(*trans));
881 transaction_create_clone(Transaction *srctrans)
883 Transaction *trans = transaction_create(srctrans->pool);
884 queue_init_clone(&trans->steps, &srctrans->steps);
885 queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
886 if (srctrans->transaction_installed)
888 Repo *installed = srctrans->pool->installed;
889 trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
891 map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
892 map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
893 if (srctrans->orderdata)
895 struct _TransactionOrderdata *od = srctrans->orderdata;
896 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
897 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
898 trans->orderdata->ntes = od->ntes;
899 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
900 trans->orderdata->ninvedgedata = od->ninvedgedata;
906 transaction_free(Transaction *trans)
908 queue_free(&trans->steps);
909 queue_free(&trans->transaction_info);
910 trans->transaction_installed = solv_free(trans->transaction_installed);
911 map_free(&trans->transactsmap);
912 map_free(&trans->multiversionmap);
913 transaction_free_orderdata(trans);
918 transaction_free_orderdata(Transaction *trans)
920 if (trans->orderdata)
922 struct _TransactionOrderdata *od = trans->orderdata;
923 od->tes = solv_free(od->tes);
924 od->invedgedata = solv_free(od->invedgedata);
925 trans->orderdata = solv_free(trans->orderdata);
931 struct _TransactionElement *tes;
943 addteedge(struct orderdata *od, int from, int to, int type)
946 struct _TransactionElement *te;
951 /* 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); */
954 for (i = te->edges; od->edgedata[i]; i += 2)
955 if (od->edgedata[i] == to)
957 /* test of brokenness */
958 if (type == TYPE_BROKEN)
959 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
962 od->edgedata[i + 1] |= type;
965 if (i + 1 == od->nedgedata)
967 /* printf("tail add %d\n", i - te->edges); */
970 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
974 /* printf("extend %d\n", i - te->edges); */
975 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
977 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
978 i = od->nedgedata + (i - te->edges);
979 te->edges = od->nedgedata;
981 od->edgedata[i] = to;
982 od->edgedata[i + 1] = type;
983 od->edgedata[i + 2] = 0; /* end marker */
984 od->nedgedata = i + 3;
989 addedge(struct orderdata *od, Id from, Id to, int type)
991 Transaction *trans = od->trans;
992 Pool *pool = trans->pool;
994 struct _TransactionElement *te;
997 /* printf("addedge %d %d type %d\n", from, to, type); */
998 s = pool->solvables + from;
999 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
1001 /* obsolete, map to install */
1002 if (trans->transaction_installed[from - pool->installed->start] > 0)
1003 from = trans->transaction_installed[from - pool->installed->start];
1010 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1011 transaction_all_obs_pkgs(trans, from, &ti);
1012 for (i = 0; i < ti.count; i++)
1013 ret |= addedge(od, ti.elements[i], to, type);
1018 s = pool->solvables + to;
1019 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
1021 /* obsolete, map to install */
1022 if (trans->transaction_installed[to - pool->installed->start] > 0)
1023 to = trans->transaction_installed[to - pool->installed->start];
1030 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1031 transaction_all_obs_pkgs(trans, to, &ti);
1032 for (i = 0; i < ti.count; i++)
1033 ret |= addedge(od, from, ti.elements[i], type);
1039 /* map from/to to te numbers */
1040 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1047 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1053 return addteedge(od, i, to, type);
1057 havescripts(Pool *pool, Id solvid)
1059 Solvable *s = pool->solvables + solvid;
1065 reqp = s->repo->idarraydata + s->requires;
1066 while ((req = *reqp++) != 0)
1068 if (req == SOLVABLE_PREREQMARKER)
1075 dep = pool_id2str(pool, req);
1076 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1084 addsolvableedges(struct orderdata *od, Solvable *s)
1086 Transaction *trans = od->trans;
1087 Pool *pool = trans->pool;
1088 Id req, *reqp, con, *conp;
1090 int i, j, pre, numins;
1091 Repo *installed = pool->installed;
1097 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1099 p = s - pool->solvables;
1103 reqp = s->repo->idarraydata + s->requires;
1105 while ((req = *reqp++) != 0)
1107 if (req == SOLVABLE_PREREQMARKER)
1113 if (pre != TYPE_PREREQ && installed && s->repo == installed)
1115 /* ignore normal requires if we're getting obsoleted */
1116 if (trans->transaction_installed[p - pool->installed->start])
1121 numins = 0; /* number of packages to be installed providing it */
1122 provbyinst = 0; /* provided by kept package */
1123 FOR_PROVIDES(p2, pp2, req)
1125 s2 = pool->solvables + p2;
1128 reqq.count = 0; /* self provides */
1131 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1135 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1136 reqq.count = 0; /* provided by package that stays installed */
1142 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1143 continue; /* package stays uninstalled */
1145 if (s->repo == installed)
1147 /* s gets uninstalled */
1148 queue_pushunique(&reqq, p2);
1149 if (s2->repo != installed)
1154 if (s2->repo == installed)
1155 continue; /* s2 gets uninstalled */
1156 queue_pushunique(&reqq, p2);
1161 /* prune to harmless ->inst edges */
1162 for (i = j = 0; i < reqq.count; i++)
1163 if (pool->solvables[reqq.elements[i]].repo != installed)
1164 reqq.elements[j++] = reqq.elements[i];
1168 if (numins && reqq.count)
1170 if (s->repo == installed)
1172 for (i = 0; i < reqq.count; i++)
1174 if (pool->solvables[reqq.elements[i]].repo == installed)
1176 for (j = 0; j < reqq.count; j++)
1178 if (pool->solvables[reqq.elements[j]].repo != installed)
1180 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1181 continue; /* no self edge */
1183 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]));
1185 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1191 /* no mixed types, remove all deps on uninstalls */
1192 for (i = j = 0; i < reqq.count; i++)
1193 if (pool->solvables[reqq.elements[i]].repo != installed)
1194 reqq.elements[j++] = reqq.elements[i];
1199 for (i = 0; i < reqq.count; i++)
1201 p2 = reqq.elements[i];
1202 if (pool->solvables[p2].repo != installed)
1204 /* all elements of reqq are installs, thus have different TEs */
1205 if (pool->solvables[p].repo != installed)
1208 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1210 addedge(od, p, p2, pre);
1215 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1217 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1222 if (s->repo != installed)
1223 continue; /* no inst->uninst edges, please! */
1225 /* uninst -> uninst edge. Those make trouble. Only add if we must */
1226 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1228 /* p is obsoleted by another package and has no scripts */
1229 /* we assume that the obsoletor is good enough to replace p */
1233 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1235 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1242 conp = s->repo->idarraydata + s->conflicts;
1243 while ((con = *conp++) != 0)
1245 FOR_PROVIDES(p2, pp2, con)
1249 s2 = pool->solvables + p2;
1252 if (s->repo == installed)
1254 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1256 /* deinstall p before installing p2 */
1258 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1260 addedge(od, p2, p, TYPE_CON);
1265 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1267 /* deinstall p2 before installing p */
1269 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1271 addedge(od, p, p2, TYPE_CON);
1278 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1280 /* we're getting deinstalled/updated. Try to do this before our
1281 * triggers are hit */
1282 for (i = 0; i < reqq.count; i++)
1284 Id tri = reqq.elements[i];
1285 FOR_PROVIDES(p2, pp2, tri)
1289 s2 = pool->solvables + p2;
1292 if (s2->name == s->name)
1293 continue; /* obsoleted anyway */
1294 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1296 /* deinstall/update p before installing p2 */
1298 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1300 addedge(od, p2, p, TYPE_CON);
1309 /* break an edge in a cycle */
1311 breakcycle(struct orderdata *od, Id *cycle)
1313 Pool *pool = od->trans->pool;
1314 Id ddegmin, ddegmax, ddeg;
1316 struct _TransactionElement *te;
1319 ddegmin = ddegmax = 0;
1320 for (k = 0; cycle[k + 1]; k += 2)
1322 ddeg = od->edgedata[cycle[k + 1] + 1];
1325 if (!k || ddeg < ddegmin)
1331 if (ddeg == ddegmin)
1333 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1335 /* prefer k, as l comes from a package with contains scriptlets */
1340 /* same edge value, check for prereq */
1344 /* record brkoen cycle starting with the tail */
1345 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
1346 queue_push(&od->cycles, k / 2); /* cycle elements */
1347 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
1354 queue_push(&od->cyclesdata, cycle[k]);
1358 queue_push(&od->cyclesdata, 0); /* mark end */
1360 /* break that edge */
1361 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1364 if (ddegmin < TYPE_REQ)
1368 /* cycle recorded, print it */
1369 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1370 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1371 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1372 for (k = 0; cycle[k + 1]; k += 2)
1374 te = od->tes + cycle[k];
1375 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1376 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1378 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1380 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1384 dump_tes(struct orderdata *od)
1386 Pool *pool = od->trans->pool;
1389 struct _TransactionElement *te, *te2;
1392 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1394 Solvable *s = pool->solvables + te->p;
1395 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1396 if (s->repo != pool->installed)
1399 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1400 for (j = 0; j < obsq.count; j++)
1401 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1403 for (j = te->edges; od->edgedata[j]; j += 2)
1405 te2 = od->tes + od->edgedata[j];
1406 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1407 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1409 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1416 reachable(struct orderdata *od, Id i)
1418 struct _TransactionElement *te = od->tes + i;
1424 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1426 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1428 if (!od->tes[k].mark)
1430 if (od->tes[k].mark == 2)
1441 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1444 Transaction *trans = od->trans;
1445 Pool *pool = trans->pool;
1447 struct _TransactionElement *te;
1454 printf("addcycleedges\n");
1455 for (i = 0; (j = cycle[i]) != 0; i++)
1456 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1459 /* first add all the tail cycle edges */
1461 /* see what we can reach from the cycle */
1463 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1465 for (i = 0; (j = cycle[i]) != 0; i++)
1467 od->tes[j].mark = -1;
1468 queue_push(todo, j);
1472 i = queue_pop(todo);
1476 te->mark = te->mark < 0 ? 2 : 1;
1477 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1479 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1481 if (od->tes[k].mark > 0)
1482 continue; /* no need to visit again */
1483 queue_push(todo, k);
1486 /* now all cycle TEs are marked with 2, all TEs reachable
1487 * from the cycle are marked with 1 */
1489 od->tes[tail].mark = 1; /* no need to add edges */
1491 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1494 continue; /* reachable from cycle */
1495 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1497 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1499 if (od->tes[k].mark != 2)
1501 /* We found an edge to the cycle. Add an extra edge to the tail */
1502 /* the TE was not reachable, so we're not creating a new cycle! */
1504 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));
1506 j -= te->edges; /* in case we move */
1507 addteedge(od, i, tail, TYPE_CYCLETAIL);
1509 break; /* one edge is enough */
1514 /* now add all head cycle edges */
1517 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1520 for (i = 0; (j = cycle[i]) != 0; i++)
1523 od->tes[j].mark = 2;
1525 /* first the head to save some time */
1526 te = od->tes + head;
1527 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1529 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1531 if (!od->tes[k].mark)
1533 if (od->tes[k].mark == -1)
1534 od->tes[k].mark = -2; /* no need for another edge */
1536 for (i = 0; cycle[i] != 0; i++)
1538 if (cycle[i] == head)
1540 te = od->tes + cycle[i];
1541 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1543 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1545 /* see if we can reach a cycle TE from k */
1546 if (!od->tes[k].mark)
1548 if (od->tes[k].mark == -1)
1551 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));
1553 addteedge(od, head, k, TYPE_CYCLEHEAD);
1554 od->tes[k].mark = -2; /* no need to add that one again */
1562 transaction_order(Transaction *trans, int flags)
1564 Pool *pool = trans->pool;
1565 Queue *tr = &trans->steps;
1566 Repo *installed = pool->installed;
1569 int i, j, k, numte, numedge;
1570 struct orderdata od;
1571 struct _TransactionElement *te;
1572 Queue todo, obsq, samerepoq, uninstq;
1573 int cycstart, cycel;
1581 start = now = solv_timems(0);
1582 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1583 /* free old data if present */
1584 if (trans->orderdata)
1586 struct _TransactionOrderdata *od = trans->orderdata;
1587 od->tes = solv_free(od->tes);
1588 od->invedgedata = solv_free(od->invedgedata);
1589 trans->orderdata = solv_free(trans->orderdata);
1592 /* create a transaction element for every active component */
1594 for (i = 0; i < tr->count; i++)
1596 p = tr->elements[i];
1597 s = pool->solvables + p;
1598 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1602 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1604 return; /* nothing to do... */
1606 numte++; /* leave first one zero */
1607 memset(&od, 0, sizeof(od));
1610 od.tes = solv_calloc(numte, sizeof(*od.tes));
1611 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1614 queue_init(&od.cycles);
1616 /* initialize TEs */
1617 for (i = 0, te = od.tes + 1; i < tr->count; i++)
1619 p = tr->elements[i];
1620 s = pool->solvables + p;
1621 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1627 /* create dependency graph */
1628 for (i = 0; i < tr->count; i++)
1629 addsolvableedges(&od, pool->solvables + tr->elements[i]);
1633 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1634 for (j = te->edges; od.edgedata[j]; j += 2)
1636 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1637 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1643 now = solv_timems(0);
1644 /* kill all cycles */
1646 for (i = numte - 1; i > 0; i--)
1647 queue_push(&todo, i);
1651 i = queue_pop(&todo);
1652 /* printf("- look at TE %d\n", i); */
1656 od.tes[i].mark = 2; /* done with that one */
1661 continue; /* already finished before */
1664 int edgestovisit = 0;
1665 /* new node, visit edges */
1666 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1668 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1670 if (od.tes[k].mark == 2)
1671 continue; /* no need to visit again */
1672 if (!edgestovisit++)
1673 queue_push(&todo, -i); /* end of edges marker */
1674 queue_push(&todo, k);
1677 te->mark = 2; /* no edges, done with that one */
1679 te->mark = 1; /* under investigation */
1682 /* oh no, we found a cycle */
1683 /* find start of cycle node (<0) */
1684 for (j = todo.count - 1; j >= 0; j--)
1685 if (todo.elements[j] == -i)
1689 /* build te/edge chain */
1691 for (j = k; j < todo.count; j++)
1692 if (todo.elements[j] < 0)
1693 todo.elements[k++] = -todo.elements[j];
1694 cycel = k - cycstart;
1696 /* make room for edges, two extra element for cycle loop + terminating 0 */
1697 while (todo.count < cycstart + 2 * cycel + 2)
1698 queue_push(&todo, 0);
1699 cycle = todo.elements + cycstart;
1700 cycle[cycel] = i; /* close the loop */
1701 cycle[2 * cycel + 1] = 0; /* terminator */
1702 for (k = cycel; k > 0; k--)
1704 cycle[k * 2] = cycle[k];
1705 te = od.tes + cycle[k - 1];
1706 assert(te->mark == 1);
1707 te->mark = 0; /* reset investigation marker */
1708 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1709 for (j = te->edges; od.edgedata[j]; j += 2)
1710 if (od.edgedata[j] == cycle[k])
1712 assert(od.edgedata[j]);
1713 cycle[k * 2 - 1] = j;
1715 /* now cycle looks like this: */
1716 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1717 breakcycle(&od, cycle);
1718 /* restart with start of cycle */
1719 todo.count = cycstart + 1;
1721 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1722 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1724 now = solv_timems(0);
1725 /* now go through all broken cycles and create cycle edges to help
1727 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1729 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1730 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1732 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1734 if (od.cycles.elements[i + 2] < TYPE_REQ)
1735 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1737 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1742 /* all edges are finally set up and there are no cycles, now the easy part.
1743 * Create an ordered transaction */
1744 now = solv_timems(0);
1745 /* first invert all edges */
1746 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1747 te->mark = 1; /* term 0 */
1748 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1750 for (j = te->edges; od.edgedata[j]; j += 2)
1752 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1754 od.tes[od.edgedata[j]].mark++;
1758 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1763 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1764 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1765 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1767 for (j = te->edges; od.edgedata[j]; j += 2)
1769 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1771 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1774 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1775 te->edges = te->mark; /* edges now points into invedgedata */
1776 od.edgedata = solv_free(od.edgedata);
1777 od.nedgedata = j + 1;
1779 /* now the final ordering */
1780 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1782 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1783 for (j = te->edges; od.invedgedata[j]; j++)
1784 od.tes[od.invedgedata[j]].mark++;
1786 queue_init(&samerepoq);
1787 queue_init(&uninstq);
1789 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1792 if (installed && pool->solvables[te->p].repo == installed)
1793 queue_push(&uninstq, i);
1795 queue_push(&todo, i);
1797 assert(todo.count > 0 || uninstq.count > 0);
1798 oldcount = tr->count;
1805 temedianr = solv_calloc(numte, sizeof(Id));
1806 for (i = 1; i < numte; i++)
1808 Solvable *s = pool->solvables + od.tes[i].p;
1809 if (installed && s->repo == installed)
1812 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1817 /* select an TE i */
1819 i = queue_shift(&uninstq);
1820 else if (samerepoq.count)
1821 i = queue_shift(&samerepoq);
1822 else if (todo.count)
1824 /* find next repo/media */
1825 for (j = 0; j < todo.count; j++)
1827 if (!j || temedianr[todo.elements[j]] < lastmedia)
1830 lastmedia = temedianr[todo.elements[j]];
1833 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1835 /* move all matching TEs to samerepoq */
1836 for (i = j = 0; j < todo.count; j++)
1838 int k = todo.elements[j];
1839 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1840 queue_push(&samerepoq, k);
1842 todo.elements[i++] = k;
1846 assert(samerepoq.count);
1847 i = queue_shift(&samerepoq);
1853 queue_push(tr, te->p);
1855 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1857 s = pool->solvables + te->p;
1858 for (j = te->edges; od.invedgedata[j]; j++)
1860 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1861 assert(te2->mark > 0);
1862 if (--te2->mark == 0)
1864 Solvable *s = pool->solvables + te2->p;
1866 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1868 if (installed && s->repo == installed)
1869 queue_push(&uninstq, od.invedgedata[j]);
1870 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1871 queue_push(&samerepoq, od.invedgedata[j]);
1873 queue_push(&todo, od.invedgedata[j]);
1877 solv_free(temedianr);
1879 queue_free(&samerepoq);
1880 queue_free(&uninstq);
1882 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1883 assert(te->mark == 0);
1885 /* add back obsoleted packages */
1886 transaction_add_obsoleted(trans);
1887 assert(tr->count == oldcount);
1889 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1890 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1892 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1894 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1895 trans->orderdata->tes = od.tes;
1896 trans->orderdata->ntes = numte;
1897 trans->orderdata->invedgedata = od.invedgedata;
1898 trans->orderdata->ninvedgedata = od.nedgedata;
1903 solv_free(od.invedgedata);
1905 queue_free(&od.cycles);
1906 queue_free(&od.cyclesdata);
1911 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1914 struct _TransactionOrderdata *od = trans->orderdata;
1915 struct _TransactionElement *te;
1918 return choices->count;
1921 /* initialization step */
1922 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1924 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1926 for (j = te->edges; od->invedgedata[j]; j++)
1927 od->tes[od->invedgedata[j]].mark++;
1929 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1931 queue_push(choices, te->p);
1932 return choices->count;
1934 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1935 if (te->p == chosen)
1938 return choices->count;
1941 /* hey! out-of-order installation! */
1944 for (j = te->edges; od->invedgedata[j]; j++)
1946 te = od->tes + od->invedgedata[j];
1947 assert(te->mark > 0 || te->mark == -1);
1948 if (te->mark > 0 && --te->mark == 0)
1949 queue_push(choices, te->p);
1951 return choices->count;
1955 transaction_add_obsoleted(Transaction *trans)
1957 Pool *pool = trans->pool;
1958 Repo *installed = pool->installed;
1965 if (!installed || !trans->steps.count)
1967 /* calculate upper bound */
1969 FOR_REPO_SOLVABLES(installed, p, s)
1970 if (MAPTST(&trans->transactsmap, p))
1975 steps = &trans->steps;
1976 queue_insertn(steps, 0, max, 0);
1979 map_init(&done, installed->end - installed->start);
1981 for (j = 0, i = max; i < steps->count; i++)
1983 p = trans->steps.elements[i];
1984 if (pool->solvables[p].repo == installed)
1986 if (!trans->transaction_installed[p - pool->installed->start])
1987 trans->steps.elements[j++] = p;
1990 trans->steps.elements[j++] = p;
1992 transaction_all_obs_pkgs(trans, p, &obsq);
1993 for (k = 0; k < obsq.count; k++)
1995 p = obsq.elements[k];
1996 assert(p >= installed->start && p < installed->end);
1997 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1999 if (MAPTST(&done, p - installed->start))
2001 MAPSET(&done, p - installed->start);
2002 trans->steps.elements[j++] = p;
2006 /* free unneeded space */
2007 queue_truncate(steps, j);
2013 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
2015 Pool *pool = trans->pool;
2020 if (MAPTST(seen, pkg))
2023 s = pool->solvables + pkg;
2025 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2031 reqp = s->repo->idarraydata + s->requires;
2032 while ((req = *reqp++) != 0)
2034 if (req == SOLVABLE_PREREQMARKER)
2039 if (onlyprereq && !inpre)
2041 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2044 /* first check kept packages, then freshly installed, then not yet uninstalled */
2045 FOR_PROVIDES(p, pp, req)
2047 if (!MAPTST(ins, p))
2049 if (MAPTST(&trans->transactsmap, p))
2052 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2056 FOR_PROVIDES(p, pp, req)
2058 if (!MAPTST(ins, p))
2060 if (pool->solvables[p].repo == pool->installed)
2063 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2068 FOR_PROVIDES(p, pp, req)
2070 if (!MAPTST(ins, p))
2073 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2078 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));
2085 transaction_check_order(Transaction *trans)
2087 Pool *pool = trans->pool;
2093 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
2094 map_init(&ins, pool->nsolvables);
2095 map_init(&seen, pool->nsolvables);
2096 if (pool->installed)
2097 FOR_REPO_SOLVABLES(pool->installed, p, s)
2100 for (i = 0; i < trans->steps.count; i++)
2102 p = trans->steps.elements[i];
2103 s = pool->solvables + p;
2104 if (s->repo != pool->installed)
2106 if (s->repo != pool->installed)
2108 if (havescripts(pool, p))
2111 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2113 if (s->repo == pool->installed)
2118 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");