2 * Copyright (c) 2007-2015, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction ordering
20 #include "transaction.h"
26 struct _TransactionElement {
27 Id p; /* solvable id */
28 Id edges; /* pointer into edges data */
32 struct _TransactionOrderdata {
33 struct _TransactionElement *tes;
40 #define TYPE_BROKEN (1<<0)
41 #define TYPE_CON (1<<1)
43 #define TYPE_REQ_P (1<<2)
44 #define TYPE_PREREQ_P (1<<3)
46 #define TYPE_REC (1<<4)
48 #define TYPE_REQ (1<<5)
49 #define TYPE_PREREQ (1<<6)
51 #define TYPE_CYCLETAIL (1<<16)
52 #define TYPE_CYCLEHEAD (1<<17)
54 #define EDGEDATA_BLOCK 127
57 transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
59 struct _TransactionOrderdata *od = srctrans->orderdata;
62 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
63 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
64 trans->orderdata->ntes = od->ntes;
65 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
66 trans->orderdata->ninvedgedata = od->ninvedgedata;
69 trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
70 queue_init_clone(trans->orderdata->cycles, od->cycles);
75 transaction_free_orderdata(Transaction *trans)
79 struct _TransactionOrderdata *od = trans->orderdata;
80 od->tes = solv_free(od->tes);
81 od->invedgedata = solv_free(od->invedgedata);
84 queue_free(od->cycles);
85 od->cycles = solv_free(od->cycles);
87 trans->orderdata = solv_free(trans->orderdata);
93 struct _TransactionElement *tes;
105 addteedge(struct orderdata *od, int from, int to, int type)
108 struct _TransactionElement *te;
113 /* 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); */
116 for (i = te->edges; od->edgedata[i]; i += 2)
117 if (od->edgedata[i] == to)
119 /* test of brokenness */
120 if (type == TYPE_BROKEN)
121 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
124 od->edgedata[i + 1] |= type;
127 if (i + 1 == od->nedgedata)
129 /* printf("tail add %d\n", i - te->edges); */
132 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
136 /* printf("extend %d\n", i - te->edges); */
137 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
139 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
140 i = od->nedgedata + (i - te->edges);
141 te->edges = od->nedgedata;
143 od->edgedata[i] = to;
144 od->edgedata[i + 1] = type;
145 od->edgedata[i + 2] = 0; /* end marker */
146 od->nedgedata = i + 3;
151 addedge(struct orderdata *od, Id from, Id to, int type)
153 Transaction *trans = od->trans;
154 Pool *pool = trans->pool;
156 struct _TransactionElement *te;
159 /* printf("addedge %d %d type %d\n", from, to, type); */
160 s = pool->solvables + from;
161 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
163 /* obsolete, map to install */
164 if (trans->transaction_installed[from - pool->installed->start] > 0)
165 from = trans->transaction_installed[from - pool->installed->start];
172 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
173 transaction_all_obs_pkgs(trans, from, &ti);
174 for (i = 0; i < ti.count; i++)
175 ret |= addedge(od, ti.elements[i], to, type);
180 s = pool->solvables + to;
181 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
183 /* obsolete, map to install */
184 if (trans->transaction_installed[to - pool->installed->start] > 0)
185 to = trans->transaction_installed[to - pool->installed->start];
192 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
193 transaction_all_obs_pkgs(trans, to, &ti);
194 for (i = 0; i < ti.count; i++)
195 ret |= addedge(od, from, ti.elements[i], type);
201 /* map from/to to te numbers */
202 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
209 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
215 return addteedge(od, i, to, type);
219 havescripts(Pool *pool, Id solvid)
221 Solvable *s = pool->solvables + solvid;
227 reqp = s->repo->idarraydata + s->requires;
228 while ((req = *reqp++) != 0)
230 if (req == SOLVABLE_PREREQMARKER)
237 dep = pool_id2str(pool, req);
238 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
246 addsolvableedges(struct orderdata *od, Solvable *s)
248 Transaction *trans = od->trans;
249 Pool *pool = trans->pool;
250 Id req, *reqp, con, *conp, rec, *recp;
252 int i, j, pre, numins;
253 Repo *installed = pool->installed;
259 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
261 p = s - pool->solvables;
265 reqp = s->repo->idarraydata + s->requires;
267 while ((req = *reqp++) != 0)
269 if (req == SOLVABLE_PREREQMARKER)
275 numins = 0; /* number of packages to be installed providing it */
276 provbyinst = 0; /* provided by kept package */
277 FOR_PROVIDES(p2, pp2, req)
279 s2 = pool->solvables + p2;
282 depq.count = 0; /* self provides */
285 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
290 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
291 continue; /* package stays uninstalled */
293 if (s->repo == installed)
295 /* s gets uninstalled */
296 queue_pushunique(&depq, p2);
297 if (s2->repo != installed)
302 if (s2->repo == installed)
303 continue; /* s2 gets uninstalled */
304 queue_pushunique(&depq, p2);
309 /* prune to harmless ->inst edges */
310 for (i = j = 0; i < depq.count; i++)
311 if (pool->solvables[depq.elements[i]].repo != installed)
312 depq.elements[j++] = depq.elements[i];
316 if (numins && depq.count)
318 if (s->repo == installed)
320 for (i = 0; i < depq.count; i++)
322 if (pool->solvables[depq.elements[i]].repo == installed)
324 for (j = 0; j < depq.count; j++)
326 if (pool->solvables[depq.elements[j]].repo != installed)
328 if (trans->transaction_installed[depq.elements[i] - pool->installed->start] == depq.elements[j])
329 continue; /* no self edge */
331 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, depq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, depq.elements[j]));
333 addedge(od, depq.elements[i], depq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
339 /* no mixed types, remove all deps on uninstalls */
340 for (i = j = 0; i < depq.count; i++)
341 if (pool->solvables[depq.elements[i]].repo != installed)
342 depq.elements[j++] = depq.elements[i];
345 for (i = 0; i < depq.count; i++)
347 p2 = depq.elements[i];
348 if (pool->solvables[p2].repo != installed)
350 /* all elements of depq are installs, thus have different TEs */
351 if (pool->solvables[p].repo != installed)
354 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
356 addedge(od, p, p2, pre);
361 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
363 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
368 if (s->repo != installed)
369 continue; /* no inst->uninst edges, please! */
371 /* uninst -> uninst edge. Those make trouble. Only add if we must */
372 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
374 /* p is obsoleted by another package and has no scripts */
375 /* we assume that the obsoletor is good enough to replace p */
379 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
381 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
388 conp = s->repo->idarraydata + s->conflicts;
389 while ((con = *conp++) != 0)
391 FOR_PROVIDES(p2, pp2, con)
395 s2 = pool->solvables + p2;
398 if (s->repo == installed)
400 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
402 /* deinstall p before installing p2 */
404 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
406 addedge(od, p2, p, TYPE_CON);
411 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
413 /* deinstall p2 before installing p */
415 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
417 addedge(od, p, p2, TYPE_CON);
424 if (s->recommends && s->repo != installed)
426 recp = s->repo->idarraydata + s->recommends;
427 while ((rec = *recp++) != 0)
430 FOR_PROVIDES(p2, pp2, rec)
432 s2 = pool->solvables + p2;
435 depq.count = 0; /* self provides */
438 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
440 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
441 continue; /* package stays uninstalled */
442 if (s2->repo != installed)
443 queue_pushunique(&depq, p2);
445 for (i = 0; i < depq.count; i++)
447 p2 = depq.elements[i];
448 if (pool->solvables[p2].repo != installed)
451 printf("add recommends inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, rec), pool_solvid2str(pool, p2));
453 addedge(od, p, p2, TYPE_REC);
458 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &depq) && depq.count)
460 /* we're getting deinstalled/updated. Try to do this before our
461 * triggers are hit */
462 for (i = 0; i < depq.count; i++)
464 Id tri = depq.elements[i];
465 FOR_PROVIDES(p2, pp2, tri)
469 s2 = pool->solvables + p2;
472 if (s2->name == s->name)
473 continue; /* obsoleted anyway */
474 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
476 /* deinstall/update p before installing p2 */
478 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
480 addedge(od, p2, p, TYPE_CON);
489 /* break an edge in a cycle */
491 breakcycle(struct orderdata *od, Id *cycle)
493 Pool *pool = od->trans->pool;
494 Id ddegmin, ddegmax, ddeg;
496 struct _TransactionElement *te;
499 ddegmin = ddegmax = 0;
500 for (k = 0; cycle[k + 1]; k += 2)
502 ddeg = od->edgedata[cycle[k + 1] + 1];
505 if (!k || ddeg < ddegmin)
513 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
515 /* prefer k, as l comes from a package with contains scriptlets */
519 /* same edge value, check for prereq */
523 /* record brkoen cycle starting with the tail */
524 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
525 queue_push(&od->cycles, k / 2); /* cycle elements */
526 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
527 queue_push(&od->cycles, (ddegmax << 16) | ddegmin); /* max/min values */
534 queue_push(&od->cyclesdata, cycle[k]);
538 queue_push(&od->cyclesdata, 0); /* mark end */
540 /* break that edge */
541 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
544 if (ddegmin < TYPE_REQ)
548 /* cycle recorded, print it */
549 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
550 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
551 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
552 for (k = 0; cycle[k + 1]; k += 2)
554 te = od->tes + cycle[k];
555 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
556 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
558 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
560 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
564 dump_tes(struct orderdata *od)
566 Pool *pool = od->trans->pool;
569 struct _TransactionElement *te, *te2;
572 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
574 Solvable *s = pool->solvables + te->p;
575 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
576 if (s->repo != pool->installed)
579 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
580 for (j = 0; j < obsq.count; j++)
581 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
583 for (j = te->edges; od->edgedata[j]; j += 2)
585 te2 = od->tes + od->edgedata[j];
586 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
587 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
589 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
595 reachable(struct orderdata *od, Id i)
597 struct _TransactionElement *te = od->tes + i;
603 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
605 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
607 if (!od->tes[k].mark)
609 if (od->tes[k].mark == 2)
619 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
622 Transaction *trans = od->trans;
623 Pool *pool = trans->pool;
625 struct _TransactionElement *te;
630 printf("addcycleedges\n");
631 for (i = 0; (j = cycle[i]) != 0; i++)
632 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
635 /* first add all the tail cycle edges */
637 /* see what we can reach from the cycle */
639 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
641 for (i = 0; (j = cycle[i]) != 0; i++)
643 od->tes[j].mark = -1;
652 te->mark = te->mark < 0 ? 2 : 1;
653 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
655 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
657 if (od->tes[k].mark > 0)
658 continue; /* no need to visit again */
662 /* now all cycle TEs are marked with 2, all TEs reachable
663 * from the cycle are marked with 1 */
665 od->tes[tail].mark = 1; /* no need to add edges */
667 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
670 continue; /* reachable from cycle */
671 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
673 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
675 if (od->tes[k].mark != 2)
677 /* We found an edge to the cycle. Add an extra edge to the tail */
678 /* the TE was not reachable, so we're not creating a new cycle! */
680 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));
682 j -= te->edges; /* in case we move */
683 addteedge(od, i, tail, TYPE_CYCLETAIL);
685 break; /* one edge is enough */
689 /* now add all head cycle edges */
692 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
695 for (i = 0; (j = cycle[i]) != 0; i++)
700 /* first the head to save some time */
702 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
704 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
706 if (!od->tes[k].mark)
708 if (od->tes[k].mark == -1)
709 od->tes[k].mark = -2; /* no need for another edge */
711 for (i = 0; cycle[i] != 0; i++)
713 if (cycle[i] == head)
715 te = od->tes + cycle[i];
716 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
718 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
720 /* see if we can reach a cycle TE from k */
721 if (!od->tes[k].mark)
723 if (od->tes[k].mark == -1)
726 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));
728 addteedge(od, head, k, TYPE_CYCLEHEAD);
729 od->tes[k].mark = -2; /* no need to add that one again */
736 transaction_order(Transaction *trans, int flags)
738 Pool *pool = trans->pool;
739 Queue *tr = &trans->steps;
740 Repo *installed = pool->installed;
743 int i, j, k, numte, numedge;
745 struct _TransactionElement *te;
746 Queue todo, obsq, samerepoq, uninstq;
755 start = now = solv_timems(0);
756 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
757 /* free old data if present */
758 if (trans->orderdata)
760 struct _TransactionOrderdata *od = trans->orderdata;
761 od->tes = solv_free(od->tes);
762 od->invedgedata = solv_free(od->invedgedata);
763 trans->orderdata = solv_free(trans->orderdata);
766 /* create a transaction element for every active component */
768 for (i = 0; i < tr->count; i++)
771 s = pool->solvables + p;
772 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
776 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
778 return; /* nothing to do... */
780 numte++; /* leave first one zero */
781 memset(&od, 0, sizeof(od));
784 od.tes = solv_calloc(numte, sizeof(*od.tes));
785 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
788 queue_init(&od.cycles);
791 for (i = 0, te = od.tes + 1; i < tr->count; i++)
794 s = pool->solvables + p;
795 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
801 /* create dependency graph */
802 for (i = 0; i < tr->count; i++)
803 addsolvableedges(&od, pool->solvables + tr->elements[i]);
807 for (i = 1, te = od.tes + i; i < numte; i++, te++)
808 for (j = te->edges; od.edgedata[j]; j += 2)
810 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
811 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
817 now = solv_timems(0);
818 /* kill all cycles */
820 for (i = numte - 1; i > 0; i--)
821 queue_push(&todo, i);
825 i = queue_pop(&todo);
826 /* printf("- look at TE %d\n", i); */
830 od.tes[i].mark = 2; /* done with that one */
835 continue; /* already finished before */
838 int edgestovisit = 0;
839 /* new node, visit edges */
840 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
842 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
844 if (od.tes[k].mark == 2)
845 continue; /* no need to visit again */
847 queue_push(&todo, -i); /* end of edges marker */
848 queue_push(&todo, k);
851 te->mark = 2; /* no edges, done with that one */
853 te->mark = 1; /* under investigation */
856 /* oh no, we found a cycle */
857 /* find start of cycle node (<0) */
858 for (j = todo.count - 1; j >= 0; j--)
859 if (todo.elements[j] == -i)
863 /* build te/edge chain */
865 for (j = k; j < todo.count; j++)
866 if (todo.elements[j] < 0)
867 todo.elements[k++] = -todo.elements[j];
868 cycel = k - cycstart;
870 /* make room for edges, two extra element for cycle loop + terminating 0 */
871 while (todo.count < cycstart + 2 * cycel + 2)
872 queue_push(&todo, 0);
873 cycle = todo.elements + cycstart;
874 cycle[cycel] = i; /* close the loop */
875 cycle[2 * cycel + 1] = 0; /* terminator */
876 for (k = cycel; k > 0; k--)
878 cycle[k * 2] = cycle[k];
879 te = od.tes + cycle[k - 1];
880 assert(te->mark == 1);
881 te->mark = 0; /* reset investigation marker */
882 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
883 for (j = te->edges; od.edgedata[j]; j += 2)
884 if (od.edgedata[j] == cycle[k])
886 assert(od.edgedata[j]);
887 cycle[k * 2 - 1] = j;
889 /* now cycle looks like this: */
890 /* te1 edge te2 edge te3 ... teN edge te1 0 */
891 breakcycle(&od, cycle);
892 /* restart with start of cycle */
893 todo.count = cycstart + 1;
895 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
896 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
898 now = solv_timems(0);
899 /* now go through all broken cycles and create cycle edges to help
901 for (i = od.cycles.count - 4; i >= 0; i -= 4)
903 if (od.cycles.elements[i + 2] >= TYPE_REQ)
904 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
906 for (i = od.cycles.count - 4; i >= 0; i -= 4)
908 if (od.cycles.elements[i + 2] < TYPE_REQ)
909 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
911 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
916 /* all edges are finally set up and there are no cycles, now the easy part.
917 * Create an ordered transaction */
918 now = solv_timems(0);
919 /* first invert all edges */
920 for (i = 1, te = od.tes + i; i < numte; i++, te++)
921 te->mark = 1; /* term 0 */
922 for (i = 1, te = od.tes + i; i < numte; i++, te++)
924 for (j = te->edges; od.edgedata[j]; j += 2)
926 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
928 od.tes[od.edgedata[j]].mark++;
932 for (i = 1, te = od.tes + i; i < numte; i++, te++)
937 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
938 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
939 for (i = 1, te = od.tes + i; i < numte; i++, te++)
941 for (j = te->edges; od.edgedata[j]; j += 2)
943 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
945 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
948 for (i = 1, te = od.tes + i; i < numte; i++, te++)
949 te->edges = te->mark; /* edges now points into invedgedata */
950 od.edgedata = solv_free(od.edgedata);
951 od.nedgedata = j + 1;
953 /* now the final ordering */
954 for (i = 1, te = od.tes + i; i < numte; i++, te++)
956 for (i = 1, te = od.tes + i; i < numte; i++, te++)
957 for (j = te->edges; od.invedgedata[j]; j++)
958 od.tes[od.invedgedata[j]].mark++;
960 queue_init(&samerepoq);
961 queue_init(&uninstq);
963 for (i = 1, te = od.tes + i; i < numte; i++, te++)
966 if (installed && pool->solvables[te->p].repo == installed)
967 queue_push(&uninstq, i);
969 queue_push(&todo, i);
971 assert(todo.count > 0 || uninstq.count > 0);
972 oldcount = tr->count;
979 temedianr = solv_calloc(numte, sizeof(Id));
980 for (i = 1; i < numte; i++)
982 Solvable *s = pool->solvables + od.tes[i].p;
983 if (installed && s->repo == installed)
986 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
993 i = queue_shift(&uninstq);
994 else if (samerepoq.count)
995 i = queue_shift(&samerepoq);
998 /* find next repo/media */
999 for (j = 0; j < todo.count; j++)
1001 if (!j || temedianr[todo.elements[j]] < lastmedia)
1004 lastmedia = temedianr[todo.elements[j]];
1007 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1009 /* move all matching TEs to samerepoq */
1010 for (i = j = 0; j < todo.count; j++)
1012 int k = todo.elements[j];
1013 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1014 queue_push(&samerepoq, k);
1016 todo.elements[i++] = k;
1020 assert(samerepoq.count);
1021 i = queue_shift(&samerepoq);
1027 queue_push(tr, te->p);
1029 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1031 s = pool->solvables + te->p;
1032 for (j = te->edges; od.invedgedata[j]; j++)
1034 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1035 assert(te2->mark > 0);
1036 if (--te2->mark == 0)
1038 Solvable *s = pool->solvables + te2->p;
1040 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1042 if (installed && s->repo == installed)
1043 queue_push(&uninstq, od.invedgedata[j]);
1044 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1045 queue_push(&samerepoq, od.invedgedata[j]);
1047 queue_push(&todo, od.invedgedata[j]);
1051 solv_free(temedianr);
1053 queue_free(&samerepoq);
1054 queue_free(&uninstq);
1056 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1057 assert(te->mark == 0);
1059 /* add back obsoleted packages */
1060 transaction_add_obsoleted(trans);
1061 assert(tr->count == oldcount);
1063 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1064 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1066 if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
1068 struct _TransactionOrderdata *tod;
1069 trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
1070 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
1072 Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
1073 queue_init_clone(cycles, &od.cyclesdata);
1074 /* map from tes to packages */
1075 for (i = 0; i < cycles->count; i++)
1076 if (cycles->elements[i])
1077 cycles->elements[i] = od.tes[cycles->elements[i]].p;
1078 queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
1079 queue_push(cycles, od.cycles.count / 4);
1081 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1085 tod->invedgedata = od.invedgedata;
1086 tod->ninvedgedata = od.nedgedata;
1092 solv_free(od.invedgedata);
1093 queue_free(&od.cycles);
1094 queue_free(&od.cyclesdata);
1099 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1102 struct _TransactionOrderdata *od = trans->orderdata;
1103 struct _TransactionElement *te;
1106 return choices->count;
1109 /* initialization step */
1110 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1112 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1114 for (j = te->edges; od->invedgedata[j]; j++)
1115 od->tes[od->invedgedata[j]].mark++;
1117 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1119 queue_push(choices, te->p);
1120 return choices->count;
1122 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1123 if (te->p == chosen)
1126 return choices->count;
1129 /* hey! out-of-order installation! */
1132 for (j = te->edges; od->invedgedata[j]; j++)
1134 te = od->tes + od->invedgedata[j];
1135 assert(te->mark > 0 || te->mark == -1);
1136 if (te->mark > 0 && --te->mark == 0)
1137 queue_push(choices, te->p);
1139 return choices->count;
1143 transaction_add_obsoleted(Transaction *trans)
1145 Pool *pool = trans->pool;
1146 Repo *installed = pool->installed;
1153 if (!installed || !trans->steps.count)
1155 /* calculate upper bound */
1157 FOR_REPO_SOLVABLES(installed, p, s)
1158 if (MAPTST(&trans->transactsmap, p))
1163 steps = &trans->steps;
1164 queue_insertn(steps, 0, max, 0);
1167 map_init(&done, installed->end - installed->start);
1169 for (j = 0, i = max; i < steps->count; i++)
1171 p = trans->steps.elements[i];
1172 if (pool->solvables[p].repo == installed)
1174 if (!trans->transaction_installed[p - pool->installed->start])
1175 trans->steps.elements[j++] = p;
1178 trans->steps.elements[j++] = p;
1180 transaction_all_obs_pkgs(trans, p, &obsq);
1181 for (k = 0; k < obsq.count; k++)
1183 p = obsq.elements[k];
1184 assert(p >= installed->start && p < installed->end);
1185 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1187 if (MAPTST(&done, p - installed->start))
1189 MAPSET(&done, p - installed->start);
1190 trans->steps.elements[j++] = p;
1194 /* free unneeded space */
1195 queue_truncate(steps, j);
1201 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1203 Pool *pool = trans->pool;
1208 if (MAPTST(seen, pkg))
1211 s = pool->solvables + pkg;
1213 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1219 reqp = s->repo->idarraydata + s->requires;
1220 while ((req = *reqp++) != 0)
1222 if (req == SOLVABLE_PREREQMARKER)
1227 if (onlyprereq && !inpre)
1229 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
1232 /* first check kept packages, then freshly installed, then not yet uninstalled */
1233 FOR_PROVIDES(p, pp, req)
1235 if (!MAPTST(ins, p))
1237 if (MAPTST(&trans->transactsmap, p))
1240 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1244 FOR_PROVIDES(p, pp, req)
1246 if (!MAPTST(ins, p))
1248 if (pool->solvables[p].repo == pool->installed)
1251 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1256 FOR_PROVIDES(p, pp, req)
1258 if (!MAPTST(ins, p))
1261 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1266 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));
1273 transaction_check_order(Transaction *trans)
1275 Pool *pool = trans->pool;
1281 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
1282 map_init(&ins, pool->nsolvables);
1283 map_init(&seen, pool->nsolvables);
1284 if (pool->installed)
1286 FOR_REPO_SOLVABLES(pool->installed, p, s)
1290 for (i = 0; i < trans->steps.count; i++)
1292 p = trans->steps.elements[i];
1293 s = pool->solvables + p;
1294 if (s->repo != pool->installed)
1296 if (s->repo != pool->installed)
1298 if (havescripts(pool, p))
1301 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1303 if (s->repo == pool->installed)
1308 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
1312 transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
1314 struct _TransactionOrderdata *od = trans->orderdata;
1316 int i, cid, ncycles;
1319 if (!od || !od->cycles || !od->cycles->count)
1322 ncycles = cq->elements[cq->count - 1];
1323 i = cq->count - 1 - ncycles * 4;
1324 for (cid = 1; cid <= ncycles; cid++, i += 4)
1328 int cmin = cq->elements[i + 3] & 0xffff;
1329 int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
1330 if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
1332 if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
1340 transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
1342 struct _TransactionOrderdata *od = trans->orderdata;
1344 int cmin, cmax, severity;
1348 if (!od || !od->cycles || !od->cycles->count)
1349 return SOLVER_ORDERCYCLE_HARMLESS;
1351 ncycles = cq->elements[cq->count - 1];
1352 if (cid < 1 || cid > ncycles)
1353 return SOLVER_ORDERCYCLE_HARMLESS;
1354 cid = cq->count - 1 - 4 * (ncycles - cid + 1);
1355 cmin = cq->elements[cid + 3] & 0xffff;
1356 cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
1357 if (cmin < TYPE_REQ)
1358 severity = SOLVER_ORDERCYCLE_HARMLESS;
1359 else if ((cmax & TYPE_PREREQ) == 0)
1360 severity = SOLVER_ORDERCYCLE_NORMAL;
1362 severity = SOLVER_ORDERCYCLE_CRITICAL;
1364 queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);