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;
203 is_pseudo_package(Pool *pool, Solvable *s)
205 const char *n = pool_id2str(pool, s->name);
206 if (!strncmp(n, "patch:", 6))
208 if (!strncmp(n, "pattern:", 8))
214 obsoleted_by_pseudos_only(Transaction *trans, Id p)
216 Pool *pool = trans->pool;
221 op = transaction_obs_pkg(trans, p);
222 if (op && !is_pseudo_package(pool, pool->solvables + op))
225 transaction_all_obs_pkgs(trans, p, &q);
226 for (i = 0; i < q.count; i++)
227 if (!is_pseudo_package(pool, pool->solvables + q.elements[i]))
229 i = !q.count || i < q.count ? 0 : 1;
235 * return type of transaction element
237 * filtering is needed if either not all packages are shown
238 * or replaces are not shown, as otherwise parts of the
239 * transaction might not be shown to the user */
242 transaction_type(Transaction *trans, Id p, int mode)
244 Pool *pool = trans->pool;
245 Solvable *s = pool->solvables + p;
251 return SOLVER_TRANSACTION_IGNORE;
253 /* XXX: SUSE only? */
254 if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_pseudo_package(pool, s))
255 return SOLVER_TRANSACTION_IGNORE;
257 type = transaction_base_type(trans, p);
259 if (type == SOLVER_TRANSACTION_IGNORE)
260 return SOLVER_TRANSACTION_IGNORE; /* not part of the transaction */
262 if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
264 /* application wants to know what to feed to rpm */
265 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
267 if (s->repo == pool->installed)
269 /* check if we're obsoleted by pseudos only */
270 if (obsoleted_by_pseudos_only(trans, s - pool->solvables))
271 return SOLVER_TRANSACTION_ERASE;
272 return SOLVER_TRANSACTION_IGNORE; /* ignore as we're being obsoleted */
274 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
275 return SOLVER_TRANSACTION_MULTIINSTALL;
276 return SOLVER_TRANSACTION_INSTALL;
279 if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
281 /* application wants to make no difference between install
282 * and multiinstall */
283 if (type == SOLVER_TRANSACTION_MULTIINSTALL)
284 type = SOLVER_TRANSACTION_INSTALL;
285 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
286 type = SOLVER_TRANSACTION_REINSTALL;
289 if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
291 /* application wants to make no difference between change
293 if (type == SOLVER_TRANSACTION_CHANGED)
294 type = SOLVER_TRANSACTION_REINSTALLED;
295 else if (type == SOLVER_TRANSACTION_CHANGE)
296 type = SOLVER_TRANSACTION_REINSTALL;
299 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
302 if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
304 /* erase element and we're showing the passive side */
305 if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
306 type = SOLVER_TRANSACTION_ERASE;
307 if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
308 type = SOLVER_TRANSACTION_UPGRADED;
311 if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
313 /* install element and we're showing the active side */
314 if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
315 type = SOLVER_TRANSACTION_INSTALL;
316 if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
317 type = SOLVER_TRANSACTION_UPGRADE;
321 /* the element doesn't match the show mode */
323 /* if we're showing all references, we can ignore this package */
324 if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
325 return SOLVER_TRANSACTION_IGNORE;
327 /* we're not showing all refs. check if some other package
328 * references us. If yes, it's safe to ignore this package,
329 * otherwise we need to map the type */
331 /* most of the time there's only one reference, so check it first */
332 q = transaction_obs_pkg(trans, p);
334 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
336 Solvable *sq = pool->solvables + q;
337 if (sq->name != s->name)
339 /* it's a replace but we're not showing replaces. map type. */
340 if (s->repo == pool->installed)
341 return SOLVER_TRANSACTION_ERASE;
342 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
343 return SOLVER_TRANSACTION_MULTIINSTALL;
345 return SOLVER_TRANSACTION_INSTALL;
349 /* if there's a match, p will be shown when q
351 if (transaction_obs_pkg(trans, q) == p)
352 return SOLVER_TRANSACTION_IGNORE;
354 /* too bad, a miss. check em all */
357 transaction_all_obs_pkgs(trans, p, &oq);
358 for (i = 0; i < oq.count; i++)
361 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
363 Solvable *sq = pool->solvables + q;
364 if (sq->name != s->name)
367 /* check if we are referenced? */
368 if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
370 transaction_all_obs_pkgs(trans, q, &rq);
371 for (j = 0; j < rq.count; j++)
372 if (rq.elements[j] == p)
380 else if (transaction_obs_pkg(trans, q) == p)
391 /* we're not referenced. map type */
392 if (s->repo == pool->installed)
393 return SOLVER_TRANSACTION_ERASE;
394 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
395 return SOLVER_TRANSACTION_MULTIINSTALL;
397 return SOLVER_TRANSACTION_INSTALL;
399 /* there was a ref, so p is shown with some other package */
400 return SOLVER_TRANSACTION_IGNORE;
406 classify_cmp(const void *ap, const void *bp, void *dp)
408 Transaction *trans = dp;
409 Pool *pool = trans->pool;
419 return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
422 return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
427 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
429 Transaction *trans = dp;
430 Pool *pool = trans->pool;
435 sa = pool->solvables + a;
436 sb = pool->solvables + b;
437 if (sa->name != sb->name)
438 return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
439 if (sa->evr != sb->evr)
441 int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
449 transaction_classify(Transaction *trans, int mode, Queue *classes)
451 Pool *pool = trans->pool;
452 int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
454 Id v, vq, type, p, q;
457 queue_empty(classes);
458 memset(ntypes, 0, sizeof(ntypes));
459 /* go through transaction and classify each step */
460 for (i = 0; i < trans->steps.count; i++)
462 p = trans->steps.elements[i];
463 s = pool->solvables + p;
464 type = transaction_type(trans, p, mode);
466 if (!pool->installed || s->repo != pool->installed)
468 /* don't report vendor/arch changes if we were mapped to erase. */
469 if (type == SOLVER_TRANSACTION_ERASE)
471 /* look at arch/vendor changes */
472 q = transaction_obs_pkg(trans, p);
475 sq = pool->solvables + q;
481 if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
483 for (j = 0; j < classes->count; j += 4)
484 if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
486 if (j == classes->count)
488 queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
489 queue_push(classes, 1);
490 queue_push(classes, v);
491 queue_push(classes, vq);
494 classes->elements[j + 1]++;
497 v = s->vendor ? s->vendor : 1;
498 vq = sq->vendor ? sq->vendor : 1;
501 if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
503 for (j = 0; j < classes->count; j += 4)
504 if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
506 if (j == classes->count)
508 queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
509 queue_push(classes, 1);
510 queue_push(classes, v);
511 queue_push(classes, vq);
514 classes->elements[j + 1]++;
517 /* now sort all vendor/arch changes */
518 if (classes->count > 4)
519 solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
520 /* finally add all classes. put erases last */
521 i = SOLVER_TRANSACTION_ERASE;
524 queue_unshift(classes, 0);
525 queue_unshift(classes, 0);
526 queue_unshift(classes, ntypes[i]);
527 queue_unshift(classes, i);
529 for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
533 if (i == SOLVER_TRANSACTION_ERASE)
535 queue_unshift(classes, 0);
536 queue_unshift(classes, 0);
537 queue_unshift(classes, ntypes[i]);
538 queue_unshift(classes, i);
543 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
545 Pool *pool = trans->pool;
551 for (i = 0; i < trans->steps.count; i++)
553 p = trans->steps.elements[i];
554 s = pool->solvables + p;
555 if (class <= SOLVER_TRANSACTION_MAXTYPE)
557 type = transaction_type(trans, p, mode);
562 if (!pool->installed || s->repo != pool->installed)
564 q = transaction_obs_pkg(trans, p);
567 sq = pool->solvables + q;
568 if (class == SOLVER_TRANSACTION_ARCHCHANGE)
570 if ((!from && !to) || (s->arch == from && sq->arch == to))
574 if (class == SOLVER_TRANSACTION_VENDORCHANGE)
576 Id v = s->vendor ? s->vendor : 1;
577 Id vq = sq->vendor ? sq->vendor : 1;
578 if ((!from && !to) || (v == from && vq == to))
584 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
588 create_transaction_info(Transaction *trans, Queue *decisionq)
590 Pool *pool = trans->pool;
591 Queue *ti = &trans->transaction_info;
592 Repo *installed = pool->installed;
598 trans->transaction_installed = solv_free(trans->transaction_installed);
600 return; /* no info needed */
601 for (i = 0; i < decisionq->count; i++)
603 p = decisionq->elements[i];
604 if (p <= 0 || p == SYSTEMSOLVABLE)
606 s = pool->solvables + p;
607 if (!s->repo || s->repo == installed)
609 multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
610 FOR_PROVIDES(p2, pp2, s->name)
612 if (!MAPTST(&trans->transactsmap, p2))
614 s2 = pool->solvables + p2;
615 if (s2->repo != installed)
617 if (multi && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
619 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
621 if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
623 queue_push2(ti, p, p2);
625 if (s->obsoletes && (!multi || !pool->noobsoletesmultiversion))
627 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
628 while ((obs = *obsp++) != 0)
630 FOR_PROVIDES(p2, pp2, obs)
632 if (!MAPTST(&trans->transactsmap, p2))
634 s2 = pool->solvables + p2;
635 if (s2->repo != installed)
637 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
639 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
641 queue_push2(ti, p, p2);
649 solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
650 for (i = j = 2; i < ti->count; i += 2)
652 if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
654 ti->elements[j++] = ti->elements[i];
655 ti->elements[j++] = ti->elements[i + 1];
657 queue_truncate(ti, j);
660 /* create transaction_installed helper */
661 /* entry > 0: exactly one obsoleter, entry < 0: multiple obsoleters, -entry is "best" */
662 trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
663 for (i = 0; i < ti->count; i += 2)
665 j = ti->elements[i + 1] - installed->start;
666 if (!trans->transaction_installed[j])
667 trans->transaction_installed[j] = ti->elements[i];
670 /* more than one package obsoletes us. compare to find "best" */
672 if (trans->transaction_installed[j] > 0)
673 trans->transaction_installed[j] = -trans->transaction_installed[j];
674 q[0] = q[2] = ti->elements[i + 1];
675 q[1] = ti->elements[i];
676 q[3] = -trans->transaction_installed[j];
677 if (obsq_sortcmp(q, q + 2, pool) < 0)
678 trans->transaction_installed[j] = -ti->elements[i];
685 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *multiversionmap)
687 Repo *installed = pool->installed;
693 trans = transaction_create(pool);
694 if (multiversionmap && !multiversionmap->size)
695 multiversionmap = 0; /* ignore empty map */
696 queue_empty(&trans->steps);
697 map_init(&trans->transactsmap, pool->nsolvables);
699 for (i = 0; i < decisionq->count; i++)
701 p = decisionq->elements[i];
702 s = pool->solvables + (p > 0 ? p : -p);
705 if (installed && s->repo == installed && p < 0)
706 MAPSET(&trans->transactsmap, -p);
707 if ((!installed || s->repo != installed) && p > 0)
710 const char *n = pool_id2str(pool, s->name);
711 if (!strncmp(n, "patch:", 6))
713 if (!strncmp(n, "pattern:", 8))
716 MAPSET(&trans->transactsmap, p);
717 if (multiversionmap && MAPTST(multiversionmap, p))
721 MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
723 map_init_clone(&trans->multiversionmap, multiversionmap);
725 create_transaction_info(trans, decisionq);
729 FOR_REPO_SOLVABLES(installed, p, s)
731 if (MAPTST(&trans->transactsmap, p))
732 queue_push(&trans->steps, p);
735 for (i = 0; i < decisionq->count; i++)
737 p = decisionq->elements[i];
738 if (p > 0 && MAPTST(&trans->transactsmap, p))
739 queue_push(&trans->steps, p);
745 transaction_installedresult(Transaction *trans, Queue *installedq)
747 Pool *pool = trans->pool;
748 Repo *installed = pool->installed;
753 queue_empty(installedq);
754 /* first the new installs, than the kept packages */
755 for (i = 0; i < trans->steps.count; i++)
757 p = trans->steps.elements[i];
758 s = pool->solvables + p;
759 if (installed && s->repo == installed)
761 queue_push(installedq, p);
763 cutoff = installedq->count;
766 FOR_REPO_SOLVABLES(installed, p, s)
767 if (!MAPTST(&trans->transactsmap, p))
768 queue_push(installedq, p);
774 transaction_create_installedmap(Transaction *trans, Map *installedmap)
776 Pool *pool = trans->pool;
777 Repo *installed = pool->installed;
782 map_init(installedmap, pool->nsolvables);
783 for (i = 0; i < trans->steps.count; i++)
785 p = trans->steps.elements[i];
786 s = pool->solvables + p;
787 if (!installed || s->repo != installed)
788 MAPSET(installedmap, p);
792 FOR_REPO_SOLVABLES(installed, p, s)
793 if (!MAPTST(&trans->transactsmap, p))
794 MAPSET(installedmap, p);
799 transaction_calc_installsizechange(Transaction *trans)
804 transaction_create_installedmap(trans, &installedmap);
805 change = pool_calc_installsizechange(trans->pool, &installedmap);
806 map_free(&installedmap);
811 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
815 transaction_create_installedmap(trans, &installedmap);
816 pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
817 map_free(&installedmap);
820 struct _TransactionElement {
821 Id p; /* solvable id */
822 Id edges; /* pointer into edges data */
826 struct _TransactionOrderdata {
827 struct _TransactionElement *tes;
833 #define TYPE_BROKEN (1<<0)
834 #define TYPE_CON (1<<1)
836 #define TYPE_REQ_P (1<<2)
837 #define TYPE_PREREQ_P (1<<3)
839 #define TYPE_REQ (1<<4)
840 #define TYPE_PREREQ (1<<5)
842 #define TYPE_CYCLETAIL (1<<16)
843 #define TYPE_CYCLEHEAD (1<<17)
845 #define EDGEDATA_BLOCK 127
848 transaction_create(Pool *pool)
850 Transaction *trans = solv_calloc(1, sizeof(*trans));
856 transaction_create_clone(Transaction *srctrans)
858 Transaction *trans = transaction_create(srctrans->pool);
859 queue_init_clone(&trans->steps, &srctrans->steps);
860 queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
861 if (srctrans->transaction_installed)
863 Repo *installed = srctrans->pool->installed;
864 trans->transaction_installed = solv_memdup2(srctrans->transaction_installed, installed->end - installed->start, sizeof(Id));
866 map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
867 map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
868 if (srctrans->orderdata)
870 struct _TransactionOrderdata *od = srctrans->orderdata;
871 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
872 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
873 trans->orderdata->ntes = od->ntes;
874 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
875 trans->orderdata->ninvedgedata = od->ninvedgedata;
881 transaction_free(Transaction *trans)
883 queue_free(&trans->steps);
884 queue_free(&trans->transaction_info);
885 trans->transaction_installed = solv_free(trans->transaction_installed);
886 map_free(&trans->transactsmap);
887 map_free(&trans->multiversionmap);
888 transaction_free_orderdata(trans);
893 transaction_free_orderdata(Transaction *trans)
895 if (trans->orderdata)
897 struct _TransactionOrderdata *od = trans->orderdata;
898 od->tes = solv_free(od->tes);
899 od->invedgedata = solv_free(od->invedgedata);
900 trans->orderdata = solv_free(trans->orderdata);
906 struct _TransactionElement *tes;
918 addteedge(struct orderdata *od, int from, int to, int type)
921 struct _TransactionElement *te;
926 /* 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); */
929 for (i = te->edges; od->edgedata[i]; i += 2)
930 if (od->edgedata[i] == to)
932 /* test of brokenness */
933 if (type == TYPE_BROKEN)
934 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
937 od->edgedata[i + 1] |= type;
940 if (i + 1 == od->nedgedata)
942 /* printf("tail add %d\n", i - te->edges); */
945 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
949 /* printf("extend %d\n", i - te->edges); */
950 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
952 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
953 i = od->nedgedata + (i - te->edges);
954 te->edges = od->nedgedata;
956 od->edgedata[i] = to;
957 od->edgedata[i + 1] = type;
958 od->edgedata[i + 2] = 0; /* end marker */
959 od->nedgedata = i + 3;
964 addedge(struct orderdata *od, Id from, Id to, int type)
966 Transaction *trans = od->trans;
967 Pool *pool = trans->pool;
969 struct _TransactionElement *te;
972 /* printf("addedge %d %d type %d\n", from, to, type); */
973 s = pool->solvables + from;
974 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
976 /* obsolete, map to install */
977 if (trans->transaction_installed[from - pool->installed->start] > 0)
978 from = trans->transaction_installed[from - pool->installed->start];
985 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
986 transaction_all_obs_pkgs(trans, from, &ti);
987 for (i = 0; i < ti.count; i++)
988 ret |= addedge(od, ti.elements[i], to, type);
993 s = pool->solvables + to;
994 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
996 /* obsolete, map to install */
997 if (trans->transaction_installed[to - pool->installed->start] > 0)
998 to = trans->transaction_installed[to - pool->installed->start];
1005 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
1006 transaction_all_obs_pkgs(trans, to, &ti);
1007 for (i = 0; i < ti.count; i++)
1008 ret |= addedge(od, from, ti.elements[i], type);
1014 /* map from/to to te numbers */
1015 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1022 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1028 return addteedge(od, i, to, type);
1033 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
1035 Transaction *trans = od->trans;
1036 Pool *pool = trans->pool;
1037 Id ti1buf[5], ti2buf[5];
1041 /* both q1 and q2 are uninstalls. check if their TEs intersect */
1042 /* common case: just one TE for both packages */
1044 printf("havechoice %d %d %d\n", p, q1, q2);
1046 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
1048 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
1050 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
1052 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
1054 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
1055 transaction_all_obs_pkgs(trans, q1, &ti1);
1056 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
1057 transaction_all_obs_pkgs(trans, q2, &ti2);
1058 for (i = 0; i < ti1.count; i++)
1059 for (j = 0; j < ti2.count; j++)
1060 if (ti1.elements[i] == ti2.elements[j])
1062 /* found a common edge */
1074 havescripts(Pool *pool, Id solvid)
1076 Solvable *s = pool->solvables + solvid;
1082 reqp = s->repo->idarraydata + s->requires;
1083 while ((req = *reqp++) != 0)
1085 if (req == SOLVABLE_PREREQMARKER)
1092 dep = pool_id2str(pool, req);
1093 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1101 addsolvableedges(struct orderdata *od, Solvable *s)
1103 Transaction *trans = od->trans;
1104 Pool *pool = trans->pool;
1105 Id req, *reqp, con, *conp;
1107 int i, j, pre, numins;
1108 Repo *installed = pool->installed;
1114 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1116 p = s - pool->solvables;
1120 reqp = s->repo->idarraydata + s->requires;
1122 while ((req = *reqp++) != 0)
1124 if (req == SOLVABLE_PREREQMARKER)
1130 if (pre != TYPE_PREREQ && installed && s->repo == installed)
1132 /* ignore normal requires if we're getting obsoleted */
1133 if (trans->transaction_installed[p - pool->installed->start])
1138 numins = 0; /* number of packages to be installed providing it */
1139 provbyinst = 0; /* provided by kept package */
1140 FOR_PROVIDES(p2, pp2, req)
1142 s2 = pool->solvables + p2;
1145 reqq.count = 0; /* self provides */
1148 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1152 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1153 reqq.count = 0; /* provided by package that stays installed */
1159 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1160 continue; /* package stays uninstalled */
1162 if (s->repo == installed)
1164 /* s gets uninstalled */
1165 queue_pushunique(&reqq, p2);
1166 if (s2->repo != installed)
1171 if (s2->repo == installed)
1172 continue; /* s2 gets uninstalled */
1173 queue_pushunique(&reqq, p2);
1178 /* prune to harmless ->inst edges */
1179 for (i = j = 0; i < reqq.count; i++)
1180 if (pool->solvables[reqq.elements[i]].repo != installed)
1181 reqq.elements[j++] = reqq.elements[i];
1185 if (numins && reqq.count)
1187 if (s->repo == installed)
1189 for (i = 0; i < reqq.count; i++)
1191 if (pool->solvables[reqq.elements[i]].repo == installed)
1193 for (j = 0; j < reqq.count; j++)
1195 if (pool->solvables[reqq.elements[j]].repo != installed)
1197 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1198 continue; /* no self edge */
1200 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]));
1202 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1208 /* no mixed types, remove all deps on uninstalls */
1209 for (i = j = 0; i < reqq.count; i++)
1210 if (pool->solvables[reqq.elements[i]].repo != installed)
1211 reqq.elements[j++] = reqq.elements[i];
1216 for (i = 0; i < reqq.count; i++)
1219 p2 = reqq.elements[i];
1220 if (pool->solvables[p2].repo != installed)
1222 /* all elements of reqq are installs, thus have different TEs */
1223 choice = reqq.count - 1;
1224 if (pool->solvables[p].repo != installed)
1227 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1229 addedge(od, p, p2, pre);
1234 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1236 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1241 if (s->repo != installed)
1242 continue; /* no inst->uninst edges, please! */
1244 /* uninst -> uninst edge. Those make trouble. Only add if we must */
1245 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1247 /* p is obsoleted by another package and has no scripts */
1248 /* we assume that the obsoletor is good enough to replace p */
1253 for (j = 0; j < reqq.count; j++)
1257 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1262 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1264 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1271 conp = s->repo->idarraydata + s->conflicts;
1272 while ((con = *conp++) != 0)
1274 FOR_PROVIDES(p2, pp2, con)
1278 s2 = pool->solvables + p2;
1281 if (s->repo == installed)
1283 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1285 /* deinstall p before installing p2 */
1287 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1289 addedge(od, p2, p, TYPE_CON);
1294 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1296 /* deinstall p2 before installing p */
1298 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1300 addedge(od, p, p2, TYPE_CON);
1307 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1309 /* we're getting deinstalled/updated. Try to do this before our
1310 * triggers are hit */
1311 for (i = 0; i < reqq.count; i++)
1313 Id tri = reqq.elements[i];
1314 FOR_PROVIDES(p2, pp2, tri)
1318 s2 = pool->solvables + p2;
1321 if (s2->name == s->name)
1322 continue; /* obsoleted anyway */
1323 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1325 /* deinstall/update p before installing p2 */
1327 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1329 addedge(od, p2, p, TYPE_CON);
1338 /* break an edge in a cycle */
1340 breakcycle(struct orderdata *od, Id *cycle)
1342 Pool *pool = od->trans->pool;
1343 Id ddegmin, ddegmax, ddeg;
1345 struct _TransactionElement *te;
1348 ddegmin = ddegmax = 0;
1349 for (k = 0; cycle[k + 1]; k += 2)
1351 ddeg = od->edgedata[cycle[k + 1] + 1];
1354 if (!k || ddeg < ddegmin)
1360 if (ddeg == ddegmin)
1362 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1364 /* prefer k, as l comes from a package with contains scriptlets */
1369 /* same edge value, check for prereq */
1373 /* record brkoen cycle starting with the tail */
1374 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
1375 queue_push(&od->cycles, k / 2); /* cycle elements */
1376 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
1383 queue_push(&od->cyclesdata, cycle[k]);
1387 queue_push(&od->cyclesdata, 0); /* mark end */
1389 /* break that edge */
1390 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1393 if (ddegmin < TYPE_REQ)
1397 /* cycle recorded, print it */
1398 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1399 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1400 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1401 for (k = 0; cycle[k + 1]; k += 2)
1403 te = od->tes + cycle[k];
1404 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1405 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1407 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1409 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1413 dump_tes(struct orderdata *od)
1415 Pool *pool = od->trans->pool;
1418 struct _TransactionElement *te, *te2;
1421 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1423 Solvable *s = pool->solvables + te->p;
1424 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1425 if (s->repo != pool->installed)
1428 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1429 for (j = 0; j < obsq.count; j++)
1430 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1432 for (j = te->edges; od->edgedata[j]; j += 2)
1434 te2 = od->tes + od->edgedata[j];
1435 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1436 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1438 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1445 reachable(struct orderdata *od, Id i)
1447 struct _TransactionElement *te = od->tes + i;
1453 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1455 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1457 if (!od->tes[k].mark)
1459 if (od->tes[k].mark == 2)
1470 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1473 Transaction *trans = od->trans;
1474 Pool *pool = trans->pool;
1476 struct _TransactionElement *te;
1483 printf("addcycleedges\n");
1484 for (i = 0; (j = cycle[i]) != 0; i++)
1485 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1488 /* first add all the tail cycle edges */
1490 /* see what we can reach from the cycle */
1492 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1494 for (i = 0; (j = cycle[i]) != 0; i++)
1496 od->tes[j].mark = -1;
1497 queue_push(todo, j);
1501 i = queue_pop(todo);
1505 te->mark = te->mark < 0 ? 2 : 1;
1506 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1508 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1510 if (od->tes[k].mark > 0)
1511 continue; /* no need to visit again */
1512 queue_push(todo, k);
1515 /* now all cycle TEs are marked with 2, all TEs reachable
1516 * from the cycle are marked with 1 */
1518 od->tes[tail].mark = 1; /* no need to add edges */
1520 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1523 continue; /* reachable from cycle */
1524 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1526 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1528 if (od->tes[k].mark != 2)
1530 /* We found an edge to the cycle. Add an extra edge to the tail */
1531 /* the TE was not reachable, so we're not creating a new cycle! */
1533 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));
1535 j -= te->edges; /* in case we move */
1536 addteedge(od, i, tail, TYPE_CYCLETAIL);
1538 break; /* one edge is enough */
1543 /* now add all head cycle edges */
1546 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1549 for (i = 0; (j = cycle[i]) != 0; i++)
1552 od->tes[j].mark = 2;
1554 /* first the head to save some time */
1555 te = od->tes + head;
1556 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1558 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1560 if (!od->tes[k].mark)
1562 if (od->tes[k].mark == -1)
1563 od->tes[k].mark = -2; /* no need for another edge */
1565 for (i = 0; cycle[i] != 0; i++)
1567 if (cycle[i] == head)
1569 te = od->tes + cycle[i];
1570 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1572 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1574 /* see if we can reach a cycle TE from k */
1575 if (!od->tes[k].mark)
1577 if (od->tes[k].mark == -1)
1580 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));
1582 addteedge(od, head, k, TYPE_CYCLEHEAD);
1583 od->tes[k].mark = -2; /* no need to add that one again */
1591 transaction_order(Transaction *trans, int flags)
1593 Pool *pool = trans->pool;
1594 Queue *tr = &trans->steps;
1595 Repo *installed = pool->installed;
1598 int i, j, k, numte, numedge;
1599 struct orderdata od;
1600 struct _TransactionElement *te;
1601 Queue todo, obsq, samerepoq, uninstq;
1602 int cycstart, cycel;
1610 start = now = solv_timems(0);
1611 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1612 /* free old data if present */
1613 if (trans->orderdata)
1615 struct _TransactionOrderdata *od = trans->orderdata;
1616 od->tes = solv_free(od->tes);
1617 od->invedgedata = solv_free(od->invedgedata);
1618 trans->orderdata = solv_free(trans->orderdata);
1621 /* create a transaction element for every active component */
1623 for (i = 0; i < tr->count; i++)
1625 p = tr->elements[i];
1626 s = pool->solvables + p;
1627 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1631 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1633 return; /* nothing to do... */
1635 numte++; /* leave first one zero */
1636 memset(&od, 0, sizeof(od));
1639 od.tes = solv_calloc(numte, sizeof(*od.tes));
1640 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1643 queue_init(&od.cycles);
1645 /* initialize TEs */
1646 for (i = 0, te = od.tes + 1; i < tr->count; i++)
1648 p = tr->elements[i];
1649 s = pool->solvables + p;
1650 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1656 /* create dependency graph */
1657 for (i = 0; i < tr->count; i++)
1658 addsolvableedges(&od, pool->solvables + tr->elements[i]);
1662 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1663 for (j = te->edges; od.edgedata[j]; j += 2)
1665 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1666 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1672 now = solv_timems(0);
1673 /* kill all cycles */
1675 for (i = numte - 1; i > 0; i--)
1676 queue_push(&todo, i);
1680 i = queue_pop(&todo);
1681 /* printf("- look at TE %d\n", i); */
1685 od.tes[i].mark = 2; /* done with that one */
1690 continue; /* already finished before */
1693 int edgestovisit = 0;
1694 /* new node, visit edges */
1695 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1697 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1699 if (od.tes[k].mark == 2)
1700 continue; /* no need to visit again */
1701 if (!edgestovisit++)
1702 queue_push(&todo, -i); /* end of edges marker */
1703 queue_push(&todo, k);
1706 te->mark = 2; /* no edges, done with that one */
1708 te->mark = 1; /* under investigation */
1711 /* oh no, we found a cycle */
1712 /* find start of cycle node (<0) */
1713 for (j = todo.count - 1; j >= 0; j--)
1714 if (todo.elements[j] == -i)
1718 /* build te/edge chain */
1720 for (j = k; j < todo.count; j++)
1721 if (todo.elements[j] < 0)
1722 todo.elements[k++] = -todo.elements[j];
1723 cycel = k - cycstart;
1725 /* make room for edges, two extra element for cycle loop + terminating 0 */
1726 while (todo.count < cycstart + 2 * cycel + 2)
1727 queue_push(&todo, 0);
1728 cycle = todo.elements + cycstart;
1729 cycle[cycel] = i; /* close the loop */
1730 cycle[2 * cycel + 1] = 0; /* terminator */
1731 for (k = cycel; k > 0; k--)
1733 cycle[k * 2] = cycle[k];
1734 te = od.tes + cycle[k - 1];
1735 assert(te->mark == 1);
1736 te->mark = 0; /* reset investigation marker */
1737 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1738 for (j = te->edges; od.edgedata[j]; j += 2)
1739 if (od.edgedata[j] == cycle[k])
1741 assert(od.edgedata[j]);
1742 cycle[k * 2 - 1] = j;
1744 /* now cycle looks like this: */
1745 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1746 breakcycle(&od, cycle);
1747 /* restart with start of cycle */
1748 todo.count = cycstart + 1;
1750 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1751 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1753 now = solv_timems(0);
1754 /* now go through all broken cycles and create cycle edges to help
1756 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1758 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1759 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1761 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1763 if (od.cycles.elements[i + 2] < TYPE_REQ)
1764 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1766 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1771 /* all edges are finally set up and there are no cycles, now the easy part.
1772 * Create an ordered transaction */
1773 now = solv_timems(0);
1774 /* first invert all edges */
1775 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1776 te->mark = 1; /* term 0 */
1777 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1779 for (j = te->edges; od.edgedata[j]; j += 2)
1781 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1783 od.tes[od.edgedata[j]].mark++;
1787 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1792 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1793 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1794 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1796 for (j = te->edges; od.edgedata[j]; j += 2)
1798 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1800 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1803 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1804 te->edges = te->mark; /* edges now points into invedgedata */
1805 od.edgedata = solv_free(od.edgedata);
1806 od.nedgedata = j + 1;
1808 /* now the final ordering */
1809 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1811 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1812 for (j = te->edges; od.invedgedata[j]; j++)
1813 od.tes[od.invedgedata[j]].mark++;
1815 queue_init(&samerepoq);
1816 queue_init(&uninstq);
1818 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1821 if (installed && pool->solvables[te->p].repo == installed)
1822 queue_push(&uninstq, i);
1824 queue_push(&todo, i);
1826 assert(todo.count > 0 || uninstq.count > 0);
1827 oldcount = tr->count;
1834 temedianr = solv_calloc(numte, sizeof(Id));
1835 for (i = 1; i < numte; i++)
1837 Solvable *s = pool->solvables + od.tes[i].p;
1838 if (installed && s->repo == installed)
1841 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1846 /* select an TE i */
1848 i = queue_shift(&uninstq);
1849 else if (samerepoq.count)
1850 i = queue_shift(&samerepoq);
1851 else if (todo.count)
1853 /* find next repo/media */
1854 for (j = 0; j < todo.count; j++)
1856 if (!j || temedianr[todo.elements[j]] < lastmedia)
1859 lastmedia = temedianr[todo.elements[j]];
1862 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1864 /* move all matching TEs to samerepoq */
1865 for (i = j = 0; j < todo.count; j++)
1867 int k = todo.elements[j];
1868 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1869 queue_push(&samerepoq, k);
1871 todo.elements[i++] = k;
1875 assert(samerepoq.count);
1876 i = queue_shift(&samerepoq);
1882 queue_push(tr, te->p);
1884 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1886 s = pool->solvables + te->p;
1887 for (j = te->edges; od.invedgedata[j]; j++)
1889 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1890 assert(te2->mark > 0);
1891 if (--te2->mark == 0)
1893 Solvable *s = pool->solvables + te2->p;
1895 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1897 if (installed && s->repo == installed)
1898 queue_push(&uninstq, od.invedgedata[j]);
1899 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1900 queue_push(&samerepoq, od.invedgedata[j]);
1902 queue_push(&todo, od.invedgedata[j]);
1906 solv_free(temedianr);
1908 queue_free(&samerepoq);
1909 queue_free(&uninstq);
1911 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1912 assert(te->mark == 0);
1914 /* add back obsoleted packages */
1915 transaction_add_obsoleted(trans);
1916 assert(tr->count == oldcount);
1918 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1919 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1921 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1923 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1924 trans->orderdata->tes = od.tes;
1925 trans->orderdata->ntes = numte;
1926 trans->orderdata->invedgedata = od.invedgedata;
1927 trans->orderdata->ninvedgedata = od.nedgedata;
1932 solv_free(od.invedgedata);
1934 queue_free(&od.cycles);
1935 queue_free(&od.cyclesdata);
1940 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1943 struct _TransactionOrderdata *od = trans->orderdata;
1944 struct _TransactionElement *te;
1947 return choices->count;
1950 /* initialization step */
1951 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1953 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1955 for (j = te->edges; od->invedgedata[j]; j++)
1956 od->tes[od->invedgedata[j]].mark++;
1958 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1960 queue_push(choices, te->p);
1961 return choices->count;
1963 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1964 if (te->p == chosen)
1967 return choices->count;
1970 /* hey! out-of-order installation! */
1973 for (j = te->edges; od->invedgedata[j]; j++)
1975 te = od->tes + od->invedgedata[j];
1976 assert(te->mark > 0 || te->mark == -1);
1977 if (te->mark > 0 && --te->mark == 0)
1978 queue_push(choices, te->p);
1980 return choices->count;
1984 transaction_add_obsoleted(Transaction *trans)
1986 Pool *pool = trans->pool;
1987 Repo *installed = pool->installed;
1994 if (!installed || !trans->steps.count)
1996 /* calculate upper bound */
1998 FOR_REPO_SOLVABLES(installed, p, s)
1999 if (MAPTST(&trans->transactsmap, p))
2004 steps = &trans->steps;
2005 queue_insertn(steps, 0, max, 0);
2008 map_init(&done, installed->end - installed->start);
2010 for (j = 0, i = max; i < steps->count; i++)
2012 p = trans->steps.elements[i];
2013 if (pool->solvables[p].repo == installed)
2015 if (!trans->transaction_installed[p - pool->installed->start])
2016 trans->steps.elements[j++] = p;
2019 trans->steps.elements[j++] = p;
2021 transaction_all_obs_pkgs(trans, p, &obsq);
2022 for (k = 0; k < obsq.count; k++)
2024 p = obsq.elements[k];
2025 assert(p >= installed->start && p < installed->end);
2026 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
2028 if (MAPTST(&done, p - installed->start))
2030 MAPSET(&done, p - installed->start);
2031 trans->steps.elements[j++] = p;
2035 /* free unneeded space */
2036 queue_truncate(steps, j);
2042 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
2044 Pool *pool = trans->pool;
2049 if (MAPTST(seen, pkg))
2052 s = pool->solvables + pkg;
2054 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2060 reqp = s->repo->idarraydata + s->requires;
2061 while ((req = *reqp++) != 0)
2063 if (req == SOLVABLE_PREREQMARKER)
2068 if (onlyprereq && !inpre)
2070 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2073 /* first check kept packages, then freshly installed, then not yet uninstalled */
2074 FOR_PROVIDES(p, pp, req)
2076 if (!MAPTST(ins, p))
2078 if (MAPTST(&trans->transactsmap, p))
2081 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2085 FOR_PROVIDES(p, pp, req)
2087 if (!MAPTST(ins, p))
2089 if (pool->solvables[p].repo == pool->installed)
2092 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2097 FOR_PROVIDES(p, pp, req)
2099 if (!MAPTST(ins, p))
2102 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2107 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));
2114 transaction_check_order(Transaction *trans)
2116 Pool *pool = trans->pool;
2122 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
2123 map_init(&ins, pool->nsolvables);
2124 map_init(&seen, pool->nsolvables);
2125 if (pool->installed)
2126 FOR_REPO_SOLVABLES(pool->installed, p, s)
2129 for (i = 0; i < trans->steps.count; i++)
2131 p = trans->steps.elements[i];
2132 s = pool->solvables + p;
2133 if (s->repo != pool->installed)
2135 if (s->repo != pool->installed)
2137 if (havescripts(pool, p))
2140 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2142 if (s->repo == pool->installed)
2147 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");