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 if (oas->name == s->name)
51 if (obs->name == s->name)
53 return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
55 r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
57 return -r; /* highest version first */
62 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
64 Pool *pool = trans->pool;
65 Solvable *s = pool->solvables + p;
66 Queue *ti = &trans->transaction_info;
71 if (p <= 0 || !s->repo)
73 if (s->repo == pool->installed)
75 q = trans->transaction_installed[p - pool->installed->start];
80 /* only a single obsoleting package */
84 /* find which packages obsolete us */
85 for (i = 0; i < ti->count; i += 2)
86 if (ti->elements[i + 1] == p)
87 queue_push2(pkgs, p, ti->elements[i]);
90 solv_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
91 for (i = 0; i < pkgs->count; i += 2)
92 pkgs->elements[i / 2] = pkgs->elements[i + 1];
93 queue_truncate(pkgs, pkgs->count / 2);
97 /* find the packages we obsolete */
98 for (i = 0; i < ti->count; i += 2)
100 if (ti->elements[i] == p)
101 queue_push(pkgs, ti->elements[i + 1]);
102 else if (pkgs->count)
109 transaction_obs_pkg(Transaction *trans, Id p)
111 Pool *pool = trans->pool;
112 Solvable *s = pool->solvables + p;
116 if (p <= 0 || !s->repo)
118 if (s->repo == pool->installed)
120 p = trans->transaction_installed[p - pool->installed->start];
121 return p < 0 ? -p : p;
123 ti = &trans->transaction_info;
124 for (i = 0; i < ti->count; i += 2)
125 if (ti->elements[i] == p)
126 return ti->elements[i + 1];
132 * calculate base type of transaction element
136 transaction_base_type(Transaction *trans, Id p)
138 Pool *pool = trans->pool;
143 if (!MAPTST(&trans->transactsmap, p))
144 return SOLVER_TRANSACTION_IGNORE;
145 p2 = transaction_obs_pkg(trans, p);
146 if (pool->installed && pool->solvables[p].repo == pool->installed)
150 return SOLVER_TRANSACTION_ERASE;
151 s = pool->solvables + p;
152 s2 = pool->solvables + p2;
153 if (s->name == s2->name)
155 if (s->evr == s2->evr && solvable_identical(s, s2))
156 return SOLVER_TRANSACTION_REINSTALLED;
157 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
159 return SOLVER_TRANSACTION_UPGRADED;
161 return SOLVER_TRANSACTION_DOWNGRADED;
162 return SOLVER_TRANSACTION_CHANGED;
164 return SOLVER_TRANSACTION_OBSOLETED;
168 int noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
170 return p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL;
172 return SOLVER_TRANSACTION_INSTALL;
173 s = pool->solvables + p;
174 s2 = pool->solvables + p2;
175 if (s->name == s2->name)
177 if (s->evr == s2->evr && solvable_identical(s, s2))
178 return SOLVER_TRANSACTION_REINSTALL;
179 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
181 return SOLVER_TRANSACTION_UPGRADE;
183 return SOLVER_TRANSACTION_DOWNGRADE;
185 return SOLVER_TRANSACTION_CHANGE;
187 return SOLVER_TRANSACTION_OBSOLETES;
192 * return type of transaction element
194 * filtering is needed if either not all packages are shown
195 * or replaces are not shown, as otherwise parts of the
196 * transaction might not be shown to the user */
199 transaction_type(Transaction *trans, Id p, int mode)
201 Pool *pool = trans->pool;
202 Solvable *s = pool->solvables + p;
208 return SOLVER_TRANSACTION_IGNORE;
210 /* XXX: SUSE only? */
211 if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO))
213 const char *n = pool_id2str(pool, s->name);
214 if (!strncmp(n, "patch:", 6))
215 return SOLVER_TRANSACTION_IGNORE;
216 if (!strncmp(n, "pattern:", 8))
217 return SOLVER_TRANSACTION_IGNORE;
220 type = transaction_base_type(trans, p);
222 if (type == SOLVER_TRANSACTION_IGNORE)
223 return SOLVER_TRANSACTION_IGNORE; /* not part of the transaction */
225 if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
227 /* application wants to know what to feed to rpm */
228 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
230 if (s->repo == pool->installed)
231 return SOLVER_TRANSACTION_IGNORE; /* ignore as we're being obsoleted */
232 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
233 return SOLVER_TRANSACTION_MULTIINSTALL;
234 return SOLVER_TRANSACTION_INSTALL;
237 if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
239 /* application wants to make no difference between install
240 * and multiinstall */
241 if (type == SOLVER_TRANSACTION_MULTIINSTALL)
242 type = SOLVER_TRANSACTION_INSTALL;
243 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
244 type = SOLVER_TRANSACTION_REINSTALL;
247 if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
249 /* application wants to make no difference between change
251 if (type == SOLVER_TRANSACTION_CHANGED)
252 type = SOLVER_TRANSACTION_REINSTALLED;
253 else if (type == SOLVER_TRANSACTION_CHANGE)
254 type = SOLVER_TRANSACTION_REINSTALL;
257 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
260 if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
262 /* erase element and we're showing the passive side */
263 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETED)
264 type = SOLVER_TRANSACTION_ERASE;
267 if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
269 /* install element and we're showing the active side */
270 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETES)
271 type = SOLVER_TRANSACTION_INSTALL;
275 /* the element doesn't match the show mode */
277 /* if we're showing all references, we can ignore this package */
278 if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
279 return SOLVER_TRANSACTION_IGNORE;
281 /* we're not showing all refs. check if some other package
282 * references us. If yes, it's safe to ignore this package,
283 * otherwise we need to map the type */
285 /* most of the time there's only one reference, so check it first */
286 q = transaction_obs_pkg(trans, p);
288 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
290 Solvable *sq = pool->solvables + q;
291 if (sq->name != s->name)
293 /* it's a replace but we're not showing replaces. map type. */
294 if (s->repo == pool->installed)
295 return SOLVER_TRANSACTION_ERASE;
296 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
297 return SOLVER_TRANSACTION_MULTIINSTALL;
299 return SOLVER_TRANSACTION_INSTALL;
303 /* if there's a match, p will be shown when q
305 if (transaction_obs_pkg(trans, q) == p)
306 return SOLVER_TRANSACTION_IGNORE;
308 /* too bad, a miss. check em all */
311 transaction_all_obs_pkgs(trans, p, &oq);
312 for (i = 0; i < oq.count; i++)
315 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
317 Solvable *sq = pool->solvables + q;
318 if (sq->name != s->name)
321 /* check if we are referenced? */
322 if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
324 transaction_all_obs_pkgs(trans, q, &rq);
325 for (j = 0; j < rq.count; j++)
326 if (rq.elements[j] == p)
334 else if (transaction_obs_pkg(trans, q) == p)
345 /* we're not referenced. map type */
346 if (s->repo == pool->installed)
347 return SOLVER_TRANSACTION_ERASE;
348 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
349 return SOLVER_TRANSACTION_MULTIINSTALL;
351 return SOLVER_TRANSACTION_INSTALL;
353 /* there was a ref, so p is shown with some other package */
354 return SOLVER_TRANSACTION_IGNORE;
360 classify_cmp(const void *ap, const void *bp, void *dp)
362 Transaction *trans = dp;
363 Pool *pool = trans->pool;
373 return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
376 return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
381 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
383 Transaction *trans = dp;
384 Pool *pool = trans->pool;
389 sa = pool->solvables + a;
390 sb = pool->solvables + b;
391 if (sa->name != sb->name)
392 return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
393 if (sa->evr != sb->evr)
395 int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
403 transaction_classify(Transaction *trans, int mode, Queue *classes)
405 Pool *pool = trans->pool;
406 int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
408 Id v, vq, type, p, q;
411 queue_empty(classes);
412 memset(ntypes, 0, sizeof(ntypes));
413 /* go through transaction and classify each step */
414 for (i = 0; i < trans->steps.count; i++)
416 p = trans->steps.elements[i];
417 s = pool->solvables + p;
418 type = transaction_type(trans, p, mode);
420 if (!pool->installed || s->repo != pool->installed)
422 /* don't report vendor/arch changes if we were mapped to erase. */
423 if (type == SOLVER_TRANSACTION_ERASE)
425 /* look at arch/vendor changes */
426 q = transaction_obs_pkg(trans, p);
429 sq = pool->solvables + q;
435 if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
437 for (j = 0; j < classes->count; j += 4)
438 if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
440 if (j == classes->count)
442 queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
443 queue_push(classes, 1);
444 queue_push(classes, v);
445 queue_push(classes, vq);
448 classes->elements[j + 1]++;
451 v = s->vendor ? s->vendor : 1;
452 vq = sq->vendor ? sq->vendor : 1;
455 if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
457 for (j = 0; j < classes->count; j += 4)
458 if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
460 if (j == classes->count)
462 queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
463 queue_push(classes, 1);
464 queue_push(classes, v);
465 queue_push(classes, vq);
468 classes->elements[j + 1]++;
471 /* now sort all vendor/arch changes */
472 if (classes->count > 4)
473 solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
474 /* finally add all classes. put erases last */
475 i = SOLVER_TRANSACTION_ERASE;
478 queue_unshift(classes, 0);
479 queue_unshift(classes, 0);
480 queue_unshift(classes, ntypes[i]);
481 queue_unshift(classes, i);
483 for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
487 if (i == SOLVER_TRANSACTION_ERASE)
489 queue_unshift(classes, 0);
490 queue_unshift(classes, 0);
491 queue_unshift(classes, ntypes[i]);
492 queue_unshift(classes, i);
497 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
499 Pool *pool = trans->pool;
505 for (i = 0; i < trans->steps.count; i++)
507 p = trans->steps.elements[i];
508 s = pool->solvables + p;
509 if (class <= SOLVER_TRANSACTION_MAXTYPE)
511 type = transaction_type(trans, p, mode);
516 if (!pool->installed || s->repo != pool->installed)
518 q = transaction_obs_pkg(trans, p);
521 sq = pool->solvables + q;
522 if (class == SOLVER_TRANSACTION_ARCHCHANGE)
524 if ((!from && !to) || (s->arch == from && sq->arch == to))
528 if (class == SOLVER_TRANSACTION_VENDORCHANGE)
530 Id v = s->vendor ? s->vendor : 1;
531 Id vq = sq->vendor ? sq->vendor : 1;
532 if ((!from && !to) || (v == from && vq == to))
538 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
542 create_transaction_info(Transaction *trans, Queue *decisionq)
544 Pool *pool = trans->pool;
545 Queue *ti = &trans->transaction_info;
546 Repo *installed = pool->installed;
552 trans->transaction_installed = solv_free(trans->transaction_installed);
554 return; /* no info needed */
555 for (i = 0; i < decisionq->count; i++)
557 p = decisionq->elements[i];
558 if (p <= 0 || p == SYSTEMSOLVABLE)
560 s = pool->solvables + p;
561 if (!s->repo || s->repo == installed)
563 noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
564 FOR_PROVIDES(p2, pp2, s->name)
566 if (!MAPTST(&trans->transactsmap, p2))
568 s2 = pool->solvables + p2;
569 if (s2->repo != installed)
571 if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
573 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
575 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
577 queue_push2(ti, p, p2);
579 if (s->obsoletes && !noobs)
581 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
582 while ((obs = *obsp++) != 0)
584 FOR_PROVIDES(p2, pp2, obs)
586 s2 = pool->solvables + p2;
587 if (s2->repo != installed)
589 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
591 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
593 queue_push2(ti, p, p2);
601 solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
602 for (i = j = 2; i < ti->count; i += 2)
604 if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
606 ti->elements[j++] = ti->elements[i];
607 ti->elements[j++] = ti->elements[i + 1];
609 queue_truncate(ti, j);
612 /* create transaction_installed helper */
613 /* entry > 0: exactly one obsoleter, entry < 0: multiple obsoleters, -entry is "best" */
614 trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
615 for (i = 0; i < ti->count; i += 2)
617 j = ti->elements[i + 1] - installed->start;
618 if (!trans->transaction_installed[j])
619 trans->transaction_installed[j] = ti->elements[i];
622 /* more than one package obsoletes us. compare to find "best" */
624 if (trans->transaction_installed[j] > 0)
625 trans->transaction_installed[j] = -trans->transaction_installed[j];
626 q[0] = q[2] = ti->elements[i + 1];
627 q[1] = ti->elements[i];
628 q[3] = -trans->transaction_installed[j];
629 if (obsq_sortcmp(q, q + 2, pool) < 0)
630 trans->transaction_installed[j] = -ti->elements[i];
637 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *noobsmap)
639 Repo *installed = pool->installed;
645 trans = transaction_create(pool);
646 if (noobsmap && !noobsmap->size)
647 noobsmap = 0; /* ignore empty map */
648 queue_empty(&trans->steps);
649 map_init(&trans->transactsmap, pool->nsolvables);
651 for (i = 0; i < decisionq->count; i++)
653 p = decisionq->elements[i];
654 s = pool->solvables + (p > 0 ? p : -p);
657 if (installed && s->repo == installed && p < 0)
658 MAPSET(&trans->transactsmap, -p);
659 if ((!installed || s->repo != installed) && p > 0)
662 const char *n = pool_id2str(pool, s->name);
663 if (!strncmp(n, "patch:", 6))
665 if (!strncmp(n, "pattern:", 8))
668 MAPSET(&trans->transactsmap, p);
669 if (noobsmap && MAPTST(noobsmap, p))
673 MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
675 map_init_clone(&trans->noobsmap, noobsmap);
677 create_transaction_info(trans, decisionq);
681 FOR_REPO_SOLVABLES(installed, p, s)
683 if (MAPTST(&trans->transactsmap, p))
684 queue_push(&trans->steps, p);
687 for (i = 0; i < decisionq->count; i++)
689 p = decisionq->elements[i];
690 if (p > 0 && MAPTST(&trans->transactsmap, p))
691 queue_push(&trans->steps, p);
697 transaction_installedresult(Transaction *trans, Queue *installedq)
699 Pool *pool = trans->pool;
700 Repo *installed = pool->installed;
705 queue_empty(installedq);
706 /* first the new installs, than the kept packages */
707 for (i = 0; i < trans->steps.count; i++)
709 p = trans->steps.elements[i];
710 s = pool->solvables + p;
711 if (installed && s->repo == installed)
713 queue_push(installedq, p);
715 cutoff = installedq->count;
718 FOR_REPO_SOLVABLES(installed, p, s)
719 if (!MAPTST(&trans->transactsmap, p))
720 queue_push(installedq, p);
726 transaction_create_installedmap(Transaction *trans, Map *installedmap)
728 Pool *pool = trans->pool;
729 Repo *installed = pool->installed;
734 map_init(installedmap, pool->nsolvables);
735 for (i = 0; i < trans->steps.count; i++)
737 p = trans->steps.elements[i];
738 s = pool->solvables + p;
739 if (!installed || s->repo != installed)
740 MAPSET(installedmap, p);
744 FOR_REPO_SOLVABLES(installed, p, s)
745 if (!MAPTST(&trans->transactsmap, p))
746 MAPSET(installedmap, p);
751 transaction_calc_installsizechange(Transaction *trans)
756 transaction_create_installedmap(trans, &installedmap);
757 change = pool_calc_installsizechange(trans->pool, &installedmap);
758 map_free(&installedmap);
763 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
767 transaction_create_installedmap(trans, &installedmap);
768 pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
769 map_free(&installedmap);
772 struct _TransactionElement {
773 Id p; /* solvable id */
774 Id edges; /* pointer into edges data */
778 struct _TransactionOrderdata {
779 struct _TransactionElement *tes;
785 #define TYPE_BROKEN (1<<0)
786 #define TYPE_CON (1<<1)
788 #define TYPE_REQ_P (1<<2)
789 #define TYPE_PREREQ_P (1<<3)
791 #define TYPE_REQ (1<<4)
792 #define TYPE_PREREQ (1<<5)
794 #define TYPE_CYCLETAIL (1<<16)
795 #define TYPE_CYCLEHEAD (1<<17)
797 #define EDGEDATA_BLOCK 127
800 transaction_create(Pool *pool)
802 Transaction *trans = solv_calloc(1, sizeof(*trans));
808 transaction_create_clone(Transaction *srctrans)
810 Transaction *trans = transaction_create(srctrans->pool);
811 queue_init_clone(&trans->steps, &srctrans->steps);
812 queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
813 if (srctrans->transaction_installed)
815 Repo *installed = srctrans->pool->installed;
816 trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
817 memcpy(trans->transaction_installed, srctrans->transaction_installed, (installed->end - installed->start) * sizeof(Id));
819 map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
820 map_init_clone(&trans->noobsmap, &srctrans->noobsmap);
821 if (srctrans->orderdata)
823 struct _TransactionOrderdata *od = srctrans->orderdata;
824 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
825 trans->orderdata->tes = solv_malloc2(od->ntes, sizeof(*od->tes));
826 memcpy(trans->orderdata->tes, od->tes, od->ntes * sizeof(*od->tes));
827 trans->orderdata->ntes = od->ntes;
828 trans->orderdata->invedgedata = solv_malloc2(od->ninvedgedata, sizeof(Id));
829 memcpy(trans->orderdata->invedgedata, od->invedgedata, od->ninvedgedata * sizeof(Id));
830 trans->orderdata->ninvedgedata = od->ninvedgedata;
836 transaction_free(Transaction *trans)
838 queue_free(&trans->steps);
839 queue_free(&trans->transaction_info);
840 trans->transaction_installed = solv_free(trans->transaction_installed);
841 map_free(&trans->transactsmap);
842 map_free(&trans->noobsmap);
843 transaction_free_orderdata(trans);
848 transaction_free_orderdata(Transaction *trans)
850 if (trans->orderdata)
852 struct _TransactionOrderdata *od = trans->orderdata;
853 od->tes = solv_free(od->tes);
854 od->invedgedata = solv_free(od->invedgedata);
855 trans->orderdata = solv_free(trans->orderdata);
861 struct _TransactionElement *tes;
873 addteedge(struct orderdata *od, int from, int to, int type)
876 struct _TransactionElement *te;
881 /* 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); */
884 for (i = te->edges; od->edgedata[i]; i += 2)
885 if (od->edgedata[i] == to)
887 /* test of brokenness */
888 if (type == TYPE_BROKEN)
889 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
892 od->edgedata[i + 1] |= type;
895 if (i + 1 == od->nedgedata)
897 /* printf("tail add %d\n", i - te->edges); */
900 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
904 /* printf("extend %d\n", i - te->edges); */
905 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
907 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
908 i = od->nedgedata + (i - te->edges);
909 te->edges = od->nedgedata;
911 od->edgedata[i] = to;
912 od->edgedata[i + 1] = type;
913 od->edgedata[i + 2] = 0; /* end marker */
914 od->nedgedata = i + 3;
919 addedge(struct orderdata *od, Id from, Id to, int type)
921 Transaction *trans = od->trans;
922 Pool *pool = trans->pool;
924 struct _TransactionElement *te;
927 /* printf("addedge %d %d type %d\n", from, to, type); */
928 s = pool->solvables + from;
929 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
931 /* obsolete, map to install */
932 if (trans->transaction_installed[from - pool->installed->start] > 0)
933 from = trans->transaction_installed[from - pool->installed->start];
940 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
941 transaction_all_obs_pkgs(trans, from, &ti);
942 for (i = 0; i < ti.count; i++)
943 ret |= addedge(od, ti.elements[i], to, type);
948 s = pool->solvables + to;
949 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
951 /* obsolete, map to install */
952 if (trans->transaction_installed[to - pool->installed->start] > 0)
953 to = trans->transaction_installed[to - pool->installed->start];
960 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
961 transaction_all_obs_pkgs(trans, to, &ti);
962 for (i = 0; i < ti.count; i++)
963 ret |= addedge(od, from, ti.elements[i], type);
969 /* map from/to to te numbers */
970 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
977 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
983 return addteedge(od, i, to, type);
988 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
990 Transaction *trans = od->trans;
991 Pool *pool = trans->pool;
992 Id ti1buf[5], ti2buf[5];
996 /* both q1 and q2 are uninstalls. check if their TEs intersect */
997 /* common case: just one TE for both packages */
999 printf("havechoice %d %d %d\n", p, q1, q2);
1001 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
1003 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
1005 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
1007 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
1009 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
1010 transaction_all_obs_pkgs(trans, q1, &ti1);
1011 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
1012 transaction_all_obs_pkgs(trans, q2, &ti2);
1013 for (i = 0; i < ti1.count; i++)
1014 for (j = 0; j < ti2.count; j++)
1015 if (ti1.elements[i] == ti2.elements[j])
1017 /* found a common edge */
1029 havescripts(Pool *pool, Id solvid)
1031 Solvable *s = pool->solvables + solvid;
1037 reqp = s->repo->idarraydata + s->requires;
1038 while ((req = *reqp++) != 0)
1040 if (req == SOLVABLE_PREREQMARKER)
1047 dep = pool_id2str(pool, req);
1048 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1056 addsolvableedges(struct orderdata *od, Solvable *s)
1058 Transaction *trans = od->trans;
1059 Pool *pool = trans->pool;
1060 Id req, *reqp, con, *conp;
1062 int i, j, pre, numins;
1063 Repo *installed = pool->installed;
1069 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1071 p = s - pool->solvables;
1075 reqp = s->repo->idarraydata + s->requires;
1077 while ((req = *reqp++) != 0)
1079 if (req == SOLVABLE_PREREQMARKER)
1085 if (pre != TYPE_PREREQ && installed && s->repo == installed)
1087 /* ignore normal requires if we're getting obsoleted */
1088 if (trans->transaction_installed[p - pool->installed->start])
1093 numins = 0; /* number of packages to be installed providing it */
1094 provbyinst = 0; /* provided by kept package */
1095 FOR_PROVIDES(p2, pp2, req)
1097 s2 = pool->solvables + p2;
1100 reqq.count = 0; /* self provides */
1103 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1107 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1108 reqq.count = 0; /* provided by package that stays installed */
1114 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1115 continue; /* package stays uninstalled */
1117 if (s->repo == installed)
1119 /* s gets uninstalled */
1120 queue_pushunique(&reqq, p2);
1121 if (s2->repo != installed)
1126 if (s2->repo == installed)
1127 continue; /* s2 gets uninstalled */
1128 queue_pushunique(&reqq, p2);
1133 /* prune to harmless ->inst edges */
1134 for (i = j = 0; i < reqq.count; i++)
1135 if (pool->solvables[reqq.elements[i]].repo != installed)
1136 reqq.elements[j++] = reqq.elements[i];
1140 if (numins && reqq.count)
1142 if (s->repo == installed)
1144 for (i = 0; i < reqq.count; i++)
1146 if (pool->solvables[reqq.elements[i]].repo == installed)
1148 for (j = 0; j < reqq.count; j++)
1150 if (pool->solvables[reqq.elements[j]].repo != installed)
1152 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1153 continue; /* no self edge */
1155 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]));
1157 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1163 /* no mixed types, remove all deps on uninstalls */
1164 for (i = j = 0; i < reqq.count; i++)
1165 if (pool->solvables[reqq.elements[i]].repo != installed)
1166 reqq.elements[j++] = reqq.elements[i];
1171 for (i = 0; i < reqq.count; i++)
1174 p2 = reqq.elements[i];
1175 if (pool->solvables[p2].repo != installed)
1177 /* all elements of reqq are installs, thus have different TEs */
1178 choice = reqq.count - 1;
1179 if (pool->solvables[p].repo != installed)
1182 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1184 addedge(od, p, p2, pre);
1189 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1191 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1196 if (s->repo != installed)
1197 continue; /* no inst->uninst edges, please! */
1199 /* uninst -> uninst edge. Those make trouble. Only add if we must */
1200 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1202 /* p is obsoleted by another package and has no scripts */
1203 /* we assume that the obsoletor is good enough to replace p */
1208 for (j = 0; j < reqq.count; j++)
1212 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1217 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1219 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1226 conp = s->repo->idarraydata + s->conflicts;
1227 while ((con = *conp++) != 0)
1229 FOR_PROVIDES(p2, pp2, con)
1233 s2 = pool->solvables + p2;
1236 if (s->repo == installed)
1238 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1240 /* deinstall p before installing p2 */
1242 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1244 addedge(od, p2, p, TYPE_CON);
1249 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1251 /* deinstall p2 before installing p */
1253 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1255 addedge(od, p, p2, TYPE_CON);
1262 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1264 /* we're getting deinstalled/updated. Try to do this before our
1265 * triggers are hit */
1266 for (i = 0; i < reqq.count; i++)
1268 Id tri = reqq.elements[i];
1269 FOR_PROVIDES(p2, pp2, tri)
1273 s2 = pool->solvables + p2;
1276 if (s2->name == s->name)
1277 continue; /* obsoleted anyway */
1278 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1280 /* deinstall/update p before installing p2 */
1282 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1284 addedge(od, p2, p, TYPE_CON);
1293 /* break an edge in a cycle */
1295 breakcycle(struct orderdata *od, Id *cycle)
1297 Pool *pool = od->trans->pool;
1298 Id ddegmin, ddegmax, ddeg;
1300 struct _TransactionElement *te;
1303 ddegmin = ddegmax = 0;
1304 for (k = 0; cycle[k + 1]; k += 2)
1306 ddeg = od->edgedata[cycle[k + 1] + 1];
1309 if (!k || ddeg < ddegmin)
1315 if (ddeg == ddegmin)
1317 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1319 /* prefer k, as l comes from a package with contains scriptlets */
1324 /* same edge value, check for prereq */
1328 /* record brkoen cycle starting with the tail */
1329 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
1330 queue_push(&od->cycles, k / 2); /* cycle elements */
1331 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
1338 queue_push(&od->cyclesdata, cycle[k]);
1342 queue_push(&od->cyclesdata, 0); /* mark end */
1344 /* break that edge */
1345 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1348 if (ddegmin < TYPE_REQ)
1352 /* cycle recorded, print it */
1353 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1354 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1355 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1356 for (k = 0; cycle[k + 1]; k += 2)
1358 te = od->tes + cycle[k];
1359 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1360 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1362 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1364 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1368 dump_tes(struct orderdata *od)
1370 Pool *pool = od->trans->pool;
1373 struct _TransactionElement *te, *te2;
1376 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1378 Solvable *s = pool->solvables + te->p;
1379 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1380 if (s->repo != pool->installed)
1383 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1384 for (j = 0; j < obsq.count; j++)
1385 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1387 for (j = te->edges; od->edgedata[j]; j += 2)
1389 te2 = od->tes + od->edgedata[j];
1390 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1391 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1393 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1400 reachable(struct orderdata *od, Id i)
1402 struct _TransactionElement *te = od->tes + i;
1408 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1410 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1412 if (!od->tes[k].mark)
1414 if (od->tes[k].mark == 2)
1425 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1428 Transaction *trans = od->trans;
1429 Pool *pool = trans->pool;
1431 struct _TransactionElement *te;
1438 printf("addcycleedges\n");
1439 for (i = 0; (j = cycle[i]) != 0; i++)
1440 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1443 /* first add all the tail cycle edges */
1445 /* see what we can reach from the cycle */
1447 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1449 for (i = 0; (j = cycle[i]) != 0; i++)
1451 od->tes[j].mark = -1;
1452 queue_push(todo, j);
1456 i = queue_pop(todo);
1460 te->mark = te->mark < 0 ? 2 : 1;
1461 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1463 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1465 if (od->tes[k].mark > 0)
1466 continue; /* no need to visit again */
1467 queue_push(todo, k);
1470 /* now all cycle TEs are marked with 2, all TEs reachable
1471 * from the cycle are marked with 1 */
1473 od->tes[tail].mark = 1; /* no need to add edges */
1475 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1478 continue; /* reachable from cycle */
1479 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1481 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1483 if (od->tes[k].mark != 2)
1485 /* We found an edge to the cycle. Add an extra edge to the tail */
1486 /* the TE was not reachable, so we're not creating a new cycle! */
1488 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));
1490 j -= te->edges; /* in case we move */
1491 addteedge(od, i, tail, TYPE_CYCLETAIL);
1493 break; /* one edge is enough */
1498 /* now add all head cycle edges */
1501 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1504 for (i = 0; (j = cycle[i]) != 0; i++)
1507 od->tes[j].mark = 2;
1509 /* first the head to save some time */
1510 te = od->tes + head;
1511 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1513 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1515 if (!od->tes[k].mark)
1517 if (od->tes[k].mark == -1)
1518 od->tes[k].mark = -2; /* no need for another edge */
1520 for (i = 0; cycle[i] != 0; i++)
1522 if (cycle[i] == head)
1524 te = od->tes + cycle[i];
1525 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1527 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1529 /* see if we can reach a cycle TE from k */
1530 if (!od->tes[k].mark)
1532 if (od->tes[k].mark == -1)
1535 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));
1537 addteedge(od, head, k, TYPE_CYCLEHEAD);
1538 od->tes[k].mark = -2; /* no need to add that one again */
1546 transaction_order(Transaction *trans, int flags)
1548 Pool *pool = trans->pool;
1549 Queue *tr = &trans->steps;
1550 Repo *installed = pool->installed;
1553 int i, j, k, numte, numedge;
1554 struct orderdata od;
1555 struct _TransactionElement *te;
1556 Queue todo, obsq, samerepoq, uninstq;
1557 int cycstart, cycel;
1565 start = now = solv_timems(0);
1566 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1567 /* free old data if present */
1568 if (trans->orderdata)
1570 struct _TransactionOrderdata *od = trans->orderdata;
1571 od->tes = solv_free(od->tes);
1572 od->invedgedata = solv_free(od->invedgedata);
1573 trans->orderdata = solv_free(trans->orderdata);
1576 /* create a transaction element for every active component */
1578 for (i = 0; i < tr->count; i++)
1580 p = tr->elements[i];
1581 s = pool->solvables + p;
1582 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1586 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1588 return; /* nothing to do... */
1590 numte++; /* leave first one zero */
1591 memset(&od, 0, sizeof(od));
1594 od.tes = solv_calloc(numte, sizeof(*od.tes));
1595 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1598 queue_init(&od.cycles);
1600 /* initialize TEs */
1601 for (i = 0, te = od.tes + 1; i < tr->count; i++)
1603 p = tr->elements[i];
1604 s = pool->solvables + p;
1605 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1611 /* create dependency graph */
1612 for (i = 0; i < tr->count; i++)
1613 addsolvableedges(&od, pool->solvables + tr->elements[i]);
1617 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1618 for (j = te->edges; od.edgedata[j]; j += 2)
1620 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1621 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1627 now = solv_timems(0);
1628 /* kill all cycles */
1630 for (i = numte - 1; i > 0; i--)
1631 queue_push(&todo, i);
1635 i = queue_pop(&todo);
1636 /* printf("- look at TE %d\n", i); */
1640 od.tes[i].mark = 2; /* done with that one */
1645 continue; /* already finished before */
1648 int edgestovisit = 0;
1649 /* new node, visit edges */
1650 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1652 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1654 if (od.tes[k].mark == 2)
1655 continue; /* no need to visit again */
1656 if (!edgestovisit++)
1657 queue_push(&todo, -i); /* end of edges marker */
1658 queue_push(&todo, k);
1661 te->mark = 2; /* no edges, done with that one */
1663 te->mark = 1; /* under investigation */
1666 /* oh no, we found a cycle */
1667 /* find start of cycle node (<0) */
1668 for (j = todo.count - 1; j >= 0; j--)
1669 if (todo.elements[j] == -i)
1673 /* build te/edge chain */
1675 for (j = k; j < todo.count; j++)
1676 if (todo.elements[j] < 0)
1677 todo.elements[k++] = -todo.elements[j];
1678 cycel = k - cycstart;
1680 /* make room for edges, two extra element for cycle loop + terminating 0 */
1681 while (todo.count < cycstart + 2 * cycel + 2)
1682 queue_push(&todo, 0);
1683 cycle = todo.elements + cycstart;
1684 cycle[cycel] = i; /* close the loop */
1685 cycle[2 * cycel + 1] = 0; /* terminator */
1686 for (k = cycel; k > 0; k--)
1688 cycle[k * 2] = cycle[k];
1689 te = od.tes + cycle[k - 1];
1690 assert(te->mark == 1);
1691 te->mark = 0; /* reset investigation marker */
1692 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1693 for (j = te->edges; od.edgedata[j]; j += 2)
1694 if (od.edgedata[j] == cycle[k])
1696 assert(od.edgedata[j]);
1697 cycle[k * 2 - 1] = j;
1699 /* now cycle looks like this: */
1700 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1701 breakcycle(&od, cycle);
1702 /* restart with start of cycle */
1703 todo.count = cycstart + 1;
1705 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1706 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1708 now = solv_timems(0);
1709 /* now go through all broken cycles and create cycle edges to help
1711 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1713 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1714 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1716 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1718 if (od.cycles.elements[i + 2] < TYPE_REQ)
1719 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1721 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1726 /* all edges are finally set up and there are no cycles, now the easy part.
1727 * Create an ordered transaction */
1728 now = solv_timems(0);
1729 /* first invert all edges */
1730 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1731 te->mark = 1; /* term 0 */
1732 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1734 for (j = te->edges; od.edgedata[j]; j += 2)
1736 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1738 od.tes[od.edgedata[j]].mark++;
1742 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1747 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1748 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1749 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1751 for (j = te->edges; od.edgedata[j]; j += 2)
1753 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1755 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1758 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1759 te->edges = te->mark; /* edges now points into invedgedata */
1760 od.edgedata = solv_free(od.edgedata);
1761 od.nedgedata = j + 1;
1763 /* now the final ordering */
1764 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1766 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1767 for (j = te->edges; od.invedgedata[j]; j++)
1768 od.tes[od.invedgedata[j]].mark++;
1770 queue_init(&samerepoq);
1771 queue_init(&uninstq);
1773 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1776 if (installed && pool->solvables[te->p].repo == installed)
1777 queue_push(&uninstq, i);
1779 queue_push(&todo, i);
1781 assert(todo.count > 0 || uninstq.count > 0);
1782 oldcount = tr->count;
1789 temedianr = solv_calloc(numte, sizeof(Id));
1790 for (i = 1; i < numte; i++)
1792 Solvable *s = pool->solvables + od.tes[i].p;
1793 if (installed && s->repo == installed)
1796 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1801 /* select an TE i */
1803 i = queue_shift(&uninstq);
1804 else if (samerepoq.count)
1805 i = queue_shift(&samerepoq);
1806 else if (todo.count)
1808 /* find next repo/media */
1809 for (j = 0; j < todo.count; j++)
1811 if (!j || temedianr[todo.elements[j]] < lastmedia)
1814 lastmedia = temedianr[todo.elements[j]];
1817 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1819 /* move all matching TEs to samerepoq */
1820 for (i = j = 0; j < todo.count; j++)
1822 int k = todo.elements[j];
1823 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1824 queue_push(&samerepoq, k);
1826 todo.elements[i++] = k;
1830 assert(samerepoq.count);
1831 i = queue_shift(&samerepoq);
1837 queue_push(tr, te->p);
1839 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1841 s = pool->solvables + te->p;
1842 for (j = te->edges; od.invedgedata[j]; j++)
1844 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1845 assert(te2->mark > 0);
1846 if (--te2->mark == 0)
1848 Solvable *s = pool->solvables + te2->p;
1850 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1852 if (installed && s->repo == installed)
1853 queue_push(&uninstq, od.invedgedata[j]);
1854 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1855 queue_push(&samerepoq, od.invedgedata[j]);
1857 queue_push(&todo, od.invedgedata[j]);
1861 solv_free(temedianr);
1863 queue_free(&samerepoq);
1864 queue_free(&uninstq);
1866 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1867 assert(te->mark == 0);
1869 /* add back obsoleted packages */
1870 transaction_add_obsoleted(trans);
1871 assert(tr->count == oldcount);
1873 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1874 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1876 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1878 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1879 trans->orderdata->tes = od.tes;
1880 trans->orderdata->ntes = numte;
1881 trans->orderdata->invedgedata = od.invedgedata;
1882 trans->orderdata->ninvedgedata = od.nedgedata;
1887 solv_free(od.invedgedata);
1889 queue_free(&od.cycles);
1890 queue_free(&od.cyclesdata);
1895 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1898 struct _TransactionOrderdata *od = trans->orderdata;
1899 struct _TransactionElement *te;
1902 return choices->count;
1905 /* initialization step */
1906 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1908 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1910 for (j = te->edges; od->invedgedata[j]; j++)
1911 od->tes[od->invedgedata[j]].mark++;
1913 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1915 queue_push(choices, te->p);
1916 return choices->count;
1918 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1919 if (te->p == chosen)
1922 return choices->count;
1925 /* hey! out-of-order installation! */
1928 for (j = te->edges; od->invedgedata[j]; j++)
1930 te = od->tes + od->invedgedata[j];
1931 assert(te->mark > 0 || te->mark == -1);
1932 if (te->mark > 0 && --te->mark == 0)
1933 queue_push(choices, te->p);
1935 return choices->count;
1939 transaction_add_obsoleted(Transaction *trans)
1941 Pool *pool = trans->pool;
1942 Repo *installed = pool->installed;
1949 if (!installed || !trans->steps.count)
1951 /* calculate upper bound */
1953 FOR_REPO_SOLVABLES(installed, p, s)
1954 if (MAPTST(&trans->transactsmap, p))
1959 steps = &trans->steps;
1960 queue_insertn(steps, 0, max);
1963 map_init(&done, installed->end - installed->start);
1965 for (j = 0, i = max; i < steps->count; i++)
1967 p = trans->steps.elements[i];
1968 if (pool->solvables[p].repo == installed)
1970 if (!trans->transaction_installed[p - pool->installed->start])
1971 trans->steps.elements[j++] = p;
1974 trans->steps.elements[j++] = p;
1976 transaction_all_obs_pkgs(trans, p, &obsq);
1977 for (k = 0; k < obsq.count; k++)
1979 p = obsq.elements[k];
1980 assert(p >= installed->start && p < installed->end);
1981 if (MAPTST(&done, p - installed->start))
1983 MAPSET(&done, p - installed->start);
1984 trans->steps.elements[j++] = p;
1988 /* free unneeded space */
1989 queue_truncate(steps, j);
1995 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1997 Pool *pool = trans->pool;
2002 if (MAPTST(seen, pkg))
2005 s = pool->solvables + pkg;
2007 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2013 reqp = s->repo->idarraydata + s->requires;
2014 while ((req = *reqp++) != 0)
2016 if (req == SOLVABLE_PREREQMARKER)
2021 if (onlyprereq && !inpre)
2023 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2026 /* first check kept packages, then freshly installed, then not yet uninstalled */
2027 FOR_PROVIDES(p, pp, req)
2029 if (!MAPTST(ins, p))
2031 if (MAPTST(&trans->transactsmap, p))
2034 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2038 FOR_PROVIDES(p, pp, req)
2040 if (!MAPTST(ins, p))
2042 if (pool->solvables[p].repo == pool->installed)
2045 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2050 FOR_PROVIDES(p, pp, req)
2052 if (!MAPTST(ins, p))
2055 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2060 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));
2067 transaction_check_order(Transaction *trans)
2069 Pool *pool = trans->pool;
2075 POOL_DEBUG(SOLV_WARN, "\nchecking transaction order...\n");
2076 map_init(&ins, pool->nsolvables);
2077 map_init(&seen, pool->nsolvables);
2078 if (pool->installed)
2079 FOR_REPO_SOLVABLES(pool->installed, p, s)
2082 for (i = 0; i < trans->steps.count; i++)
2084 p = trans->steps.elements[i];
2085 s = pool->solvables + p;
2086 if (s->repo != pool->installed)
2088 if (s->repo != pool->installed)
2090 if (havescripts(pool, p))
2093 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2095 if (s->repo == pool->installed)
2100 POOL_DEBUG(SOLV_WARN, "transaction order check done.\n");