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_REQ (1<<4)
47 #define TYPE_PREREQ (1<<5)
49 #define TYPE_CYCLETAIL (1<<16)
50 #define TYPE_CYCLEHEAD (1<<17)
52 #define EDGEDATA_BLOCK 127
55 transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
57 struct _TransactionOrderdata *od = srctrans->orderdata;
60 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
61 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
62 trans->orderdata->ntes = od->ntes;
63 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
64 trans->orderdata->ninvedgedata = od->ninvedgedata;
67 trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
68 queue_init_clone(trans->orderdata->cycles, od->cycles);
73 transaction_free_orderdata(Transaction *trans)
77 struct _TransactionOrderdata *od = trans->orderdata;
78 od->tes = solv_free(od->tes);
79 od->invedgedata = solv_free(od->invedgedata);
82 queue_free(od->cycles);
83 od->cycles = solv_free(od->cycles);
85 trans->orderdata = solv_free(trans->orderdata);
91 struct _TransactionElement *tes;
103 addteedge(struct orderdata *od, int from, int to, int type)
106 struct _TransactionElement *te;
111 /* 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); */
114 for (i = te->edges; od->edgedata[i]; i += 2)
115 if (od->edgedata[i] == to)
117 /* test of brokenness */
118 if (type == TYPE_BROKEN)
119 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
122 od->edgedata[i + 1] |= type;
125 if (i + 1 == od->nedgedata)
127 /* printf("tail add %d\n", i - te->edges); */
130 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
134 /* printf("extend %d\n", i - te->edges); */
135 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
137 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
138 i = od->nedgedata + (i - te->edges);
139 te->edges = od->nedgedata;
141 od->edgedata[i] = to;
142 od->edgedata[i + 1] = type;
143 od->edgedata[i + 2] = 0; /* end marker */
144 od->nedgedata = i + 3;
149 addedge(struct orderdata *od, Id from, Id to, int type)
151 Transaction *trans = od->trans;
152 Pool *pool = trans->pool;
154 struct _TransactionElement *te;
157 /* printf("addedge %d %d type %d\n", from, to, type); */
158 s = pool->solvables + from;
159 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
161 /* obsolete, map to install */
162 if (trans->transaction_installed[from - pool->installed->start] > 0)
163 from = trans->transaction_installed[from - pool->installed->start];
170 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
171 transaction_all_obs_pkgs(trans, from, &ti);
172 for (i = 0; i < ti.count; i++)
173 ret |= addedge(od, ti.elements[i], to, type);
178 s = pool->solvables + to;
179 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
181 /* obsolete, map to install */
182 if (trans->transaction_installed[to - pool->installed->start] > 0)
183 to = trans->transaction_installed[to - pool->installed->start];
190 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
191 transaction_all_obs_pkgs(trans, to, &ti);
192 for (i = 0; i < ti.count; i++)
193 ret |= addedge(od, from, ti.elements[i], type);
199 /* map from/to to te numbers */
200 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
207 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
213 return addteedge(od, i, to, type);
217 havescripts(Pool *pool, Id solvid)
219 Solvable *s = pool->solvables + solvid;
225 reqp = s->repo->idarraydata + s->requires;
226 while ((req = *reqp++) != 0)
228 if (req == SOLVABLE_PREREQMARKER)
235 dep = pool_id2str(pool, req);
236 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
244 addsolvableedges(struct orderdata *od, Solvable *s)
246 Transaction *trans = od->trans;
247 Pool *pool = trans->pool;
248 Id req, *reqp, con, *conp;
250 int i, j, pre, numins;
251 Repo *installed = pool->installed;
257 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
259 p = s - pool->solvables;
263 reqp = s->repo->idarraydata + s->requires;
265 while ((req = *reqp++) != 0)
267 if (req == SOLVABLE_PREREQMARKER)
273 if (pre != TYPE_PREREQ && installed && s->repo == installed)
275 /* ignore normal requires if we're getting obsoleted */
276 if (trans->transaction_installed[p - pool->installed->start])
281 numins = 0; /* number of packages to be installed providing it */
282 provbyinst = 0; /* provided by kept package */
283 FOR_PROVIDES(p2, pp2, req)
285 s2 = pool->solvables + p2;
288 reqq.count = 0; /* self provides */
291 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
295 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
296 reqq.count = 0; /* provided by package that stays installed */
302 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
303 continue; /* package stays uninstalled */
305 if (s->repo == installed)
307 /* s gets uninstalled */
308 queue_pushunique(&reqq, p2);
309 if (s2->repo != installed)
314 if (s2->repo == installed)
315 continue; /* s2 gets uninstalled */
316 queue_pushunique(&reqq, p2);
321 /* prune to harmless ->inst edges */
322 for (i = j = 0; i < reqq.count; i++)
323 if (pool->solvables[reqq.elements[i]].repo != installed)
324 reqq.elements[j++] = reqq.elements[i];
328 if (numins && reqq.count)
330 if (s->repo == installed)
332 for (i = 0; i < reqq.count; i++)
334 if (pool->solvables[reqq.elements[i]].repo == installed)
336 for (j = 0; j < reqq.count; j++)
338 if (pool->solvables[reqq.elements[j]].repo != installed)
340 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
341 continue; /* no self edge */
343 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]));
345 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
351 /* no mixed types, remove all deps on uninstalls */
352 for (i = j = 0; i < reqq.count; i++)
353 if (pool->solvables[reqq.elements[i]].repo != installed)
354 reqq.elements[j++] = reqq.elements[i];
359 for (i = 0; i < reqq.count; i++)
361 p2 = reqq.elements[i];
362 if (pool->solvables[p2].repo != installed)
364 /* all elements of reqq are installs, thus have different TEs */
365 if (pool->solvables[p].repo != installed)
368 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
370 addedge(od, p, p2, pre);
375 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
377 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
382 if (s->repo != installed)
383 continue; /* no inst->uninst edges, please! */
385 /* uninst -> uninst edge. Those make trouble. Only add if we must */
386 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
388 /* p is obsoleted by another package and has no scripts */
389 /* we assume that the obsoletor is good enough to replace p */
393 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
395 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
402 conp = s->repo->idarraydata + s->conflicts;
403 while ((con = *conp++) != 0)
405 FOR_PROVIDES(p2, pp2, con)
409 s2 = pool->solvables + p2;
412 if (s->repo == installed)
414 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
416 /* deinstall p before installing p2 */
418 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
420 addedge(od, p2, p, TYPE_CON);
425 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
427 /* deinstall p2 before installing p */
429 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
431 addedge(od, p, p2, TYPE_CON);
438 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
440 /* we're getting deinstalled/updated. Try to do this before our
441 * triggers are hit */
442 for (i = 0; i < reqq.count; i++)
444 Id tri = reqq.elements[i];
445 FOR_PROVIDES(p2, pp2, tri)
449 s2 = pool->solvables + p2;
452 if (s2->name == s->name)
453 continue; /* obsoleted anyway */
454 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
456 /* deinstall/update p before installing p2 */
458 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
460 addedge(od, p2, p, TYPE_CON);
469 /* break an edge in a cycle */
471 breakcycle(struct orderdata *od, Id *cycle)
473 Pool *pool = od->trans->pool;
474 Id ddegmin, ddegmax, ddeg;
476 struct _TransactionElement *te;
479 ddegmin = ddegmax = 0;
480 for (k = 0; cycle[k + 1]; k += 2)
482 ddeg = od->edgedata[cycle[k + 1] + 1];
485 if (!k || ddeg < ddegmin)
493 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
495 /* prefer k, as l comes from a package with contains scriptlets */
499 /* same edge value, check for prereq */
503 /* record brkoen cycle starting with the tail */
504 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
505 queue_push(&od->cycles, k / 2); /* cycle elements */
506 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
507 queue_push(&od->cycles, (ddegmax << 16) | ddegmin); /* max/min values */
514 queue_push(&od->cyclesdata, cycle[k]);
518 queue_push(&od->cyclesdata, 0); /* mark end */
520 /* break that edge */
521 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
524 if (ddegmin < TYPE_REQ)
528 /* cycle recorded, print it */
529 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
530 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
531 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
532 for (k = 0; cycle[k + 1]; k += 2)
534 te = od->tes + cycle[k];
535 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
536 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
538 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
540 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
544 dump_tes(struct orderdata *od)
546 Pool *pool = od->trans->pool;
549 struct _TransactionElement *te, *te2;
552 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
554 Solvable *s = pool->solvables + te->p;
555 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
556 if (s->repo != pool->installed)
559 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
560 for (j = 0; j < obsq.count; j++)
561 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
563 for (j = te->edges; od->edgedata[j]; j += 2)
565 te2 = od->tes + od->edgedata[j];
566 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
567 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
569 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
575 reachable(struct orderdata *od, Id i)
577 struct _TransactionElement *te = od->tes + i;
583 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
585 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
587 if (!od->tes[k].mark)
589 if (od->tes[k].mark == 2)
599 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
602 Transaction *trans = od->trans;
603 Pool *pool = trans->pool;
605 struct _TransactionElement *te;
610 printf("addcycleedges\n");
611 for (i = 0; (j = cycle[i]) != 0; i++)
612 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
615 /* first add all the tail cycle edges */
617 /* see what we can reach from the cycle */
619 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
621 for (i = 0; (j = cycle[i]) != 0; i++)
623 od->tes[j].mark = -1;
632 te->mark = te->mark < 0 ? 2 : 1;
633 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
635 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
637 if (od->tes[k].mark > 0)
638 continue; /* no need to visit again */
642 /* now all cycle TEs are marked with 2, all TEs reachable
643 * from the cycle are marked with 1 */
645 od->tes[tail].mark = 1; /* no need to add edges */
647 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
650 continue; /* reachable from cycle */
651 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
653 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
655 if (od->tes[k].mark != 2)
657 /* We found an edge to the cycle. Add an extra edge to the tail */
658 /* the TE was not reachable, so we're not creating a new cycle! */
660 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));
662 j -= te->edges; /* in case we move */
663 addteedge(od, i, tail, TYPE_CYCLETAIL);
665 break; /* one edge is enough */
669 /* now add all head cycle edges */
672 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
675 for (i = 0; (j = cycle[i]) != 0; i++)
680 /* first the head to save some time */
682 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
684 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
686 if (!od->tes[k].mark)
688 if (od->tes[k].mark == -1)
689 od->tes[k].mark = -2; /* no need for another edge */
691 for (i = 0; cycle[i] != 0; i++)
693 if (cycle[i] == head)
695 te = od->tes + cycle[i];
696 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
698 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
700 /* see if we can reach a cycle TE from k */
701 if (!od->tes[k].mark)
703 if (od->tes[k].mark == -1)
706 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));
708 addteedge(od, head, k, TYPE_CYCLEHEAD);
709 od->tes[k].mark = -2; /* no need to add that one again */
716 transaction_order(Transaction *trans, int flags)
718 Pool *pool = trans->pool;
719 Queue *tr = &trans->steps;
720 Repo *installed = pool->installed;
723 int i, j, k, numte, numedge;
725 struct _TransactionElement *te;
726 Queue todo, obsq, samerepoq, uninstq;
735 start = now = solv_timems(0);
736 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
737 /* free old data if present */
738 if (trans->orderdata)
740 struct _TransactionOrderdata *od = trans->orderdata;
741 od->tes = solv_free(od->tes);
742 od->invedgedata = solv_free(od->invedgedata);
743 trans->orderdata = solv_free(trans->orderdata);
746 /* create a transaction element for every active component */
748 for (i = 0; i < tr->count; i++)
751 s = pool->solvables + p;
752 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
756 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
758 return; /* nothing to do... */
760 numte++; /* leave first one zero */
761 memset(&od, 0, sizeof(od));
764 od.tes = solv_calloc(numte, sizeof(*od.tes));
765 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
768 queue_init(&od.cycles);
771 for (i = 0, te = od.tes + 1; i < tr->count; i++)
774 s = pool->solvables + p;
775 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
781 /* create dependency graph */
782 for (i = 0; i < tr->count; i++)
783 addsolvableedges(&od, pool->solvables + tr->elements[i]);
787 for (i = 1, te = od.tes + i; i < numte; i++, te++)
788 for (j = te->edges; od.edgedata[j]; j += 2)
790 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
791 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
797 now = solv_timems(0);
798 /* kill all cycles */
800 for (i = numte - 1; i > 0; i--)
801 queue_push(&todo, i);
805 i = queue_pop(&todo);
806 /* printf("- look at TE %d\n", i); */
810 od.tes[i].mark = 2; /* done with that one */
815 continue; /* already finished before */
818 int edgestovisit = 0;
819 /* new node, visit edges */
820 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
822 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
824 if (od.tes[k].mark == 2)
825 continue; /* no need to visit again */
827 queue_push(&todo, -i); /* end of edges marker */
828 queue_push(&todo, k);
831 te->mark = 2; /* no edges, done with that one */
833 te->mark = 1; /* under investigation */
836 /* oh no, we found a cycle */
837 /* find start of cycle node (<0) */
838 for (j = todo.count - 1; j >= 0; j--)
839 if (todo.elements[j] == -i)
843 /* build te/edge chain */
845 for (j = k; j < todo.count; j++)
846 if (todo.elements[j] < 0)
847 todo.elements[k++] = -todo.elements[j];
848 cycel = k - cycstart;
850 /* make room for edges, two extra element for cycle loop + terminating 0 */
851 while (todo.count < cycstart + 2 * cycel + 2)
852 queue_push(&todo, 0);
853 cycle = todo.elements + cycstart;
854 cycle[cycel] = i; /* close the loop */
855 cycle[2 * cycel + 1] = 0; /* terminator */
856 for (k = cycel; k > 0; k--)
858 cycle[k * 2] = cycle[k];
859 te = od.tes + cycle[k - 1];
860 assert(te->mark == 1);
861 te->mark = 0; /* reset investigation marker */
862 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
863 for (j = te->edges; od.edgedata[j]; j += 2)
864 if (od.edgedata[j] == cycle[k])
866 assert(od.edgedata[j]);
867 cycle[k * 2 - 1] = j;
869 /* now cycle looks like this: */
870 /* te1 edge te2 edge te3 ... teN edge te1 0 */
871 breakcycle(&od, cycle);
872 /* restart with start of cycle */
873 todo.count = cycstart + 1;
875 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
876 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
878 now = solv_timems(0);
879 /* now go through all broken cycles and create cycle edges to help
881 for (i = od.cycles.count - 4; i >= 0; i -= 4)
883 if (od.cycles.elements[i + 2] >= TYPE_REQ)
884 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
886 for (i = od.cycles.count - 4; i >= 0; i -= 4)
888 if (od.cycles.elements[i + 2] < TYPE_REQ)
889 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
891 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
896 /* all edges are finally set up and there are no cycles, now the easy part.
897 * Create an ordered transaction */
898 now = solv_timems(0);
899 /* first invert all edges */
900 for (i = 1, te = od.tes + i; i < numte; i++, te++)
901 te->mark = 1; /* term 0 */
902 for (i = 1, te = od.tes + i; i < numte; i++, te++)
904 for (j = te->edges; od.edgedata[j]; j += 2)
906 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
908 od.tes[od.edgedata[j]].mark++;
912 for (i = 1, te = od.tes + i; i < numte; i++, te++)
917 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
918 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
919 for (i = 1, te = od.tes + i; i < numte; i++, te++)
921 for (j = te->edges; od.edgedata[j]; j += 2)
923 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
925 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
928 for (i = 1, te = od.tes + i; i < numte; i++, te++)
929 te->edges = te->mark; /* edges now points into invedgedata */
930 od.edgedata = solv_free(od.edgedata);
931 od.nedgedata = j + 1;
933 /* now the final ordering */
934 for (i = 1, te = od.tes + i; i < numte; i++, te++)
936 for (i = 1, te = od.tes + i; i < numte; i++, te++)
937 for (j = te->edges; od.invedgedata[j]; j++)
938 od.tes[od.invedgedata[j]].mark++;
940 queue_init(&samerepoq);
941 queue_init(&uninstq);
943 for (i = 1, te = od.tes + i; i < numte; i++, te++)
946 if (installed && pool->solvables[te->p].repo == installed)
947 queue_push(&uninstq, i);
949 queue_push(&todo, i);
951 assert(todo.count > 0 || uninstq.count > 0);
952 oldcount = tr->count;
959 temedianr = solv_calloc(numte, sizeof(Id));
960 for (i = 1; i < numte; i++)
962 Solvable *s = pool->solvables + od.tes[i].p;
963 if (installed && s->repo == installed)
966 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
973 i = queue_shift(&uninstq);
974 else if (samerepoq.count)
975 i = queue_shift(&samerepoq);
978 /* find next repo/media */
979 for (j = 0; j < todo.count; j++)
981 if (!j || temedianr[todo.elements[j]] < lastmedia)
984 lastmedia = temedianr[todo.elements[j]];
987 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
989 /* move all matching TEs to samerepoq */
990 for (i = j = 0; j < todo.count; j++)
992 int k = todo.elements[j];
993 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
994 queue_push(&samerepoq, k);
996 todo.elements[i++] = k;
1000 assert(samerepoq.count);
1001 i = queue_shift(&samerepoq);
1007 queue_push(tr, te->p);
1009 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1011 s = pool->solvables + te->p;
1012 for (j = te->edges; od.invedgedata[j]; j++)
1014 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1015 assert(te2->mark > 0);
1016 if (--te2->mark == 0)
1018 Solvable *s = pool->solvables + te2->p;
1020 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1022 if (installed && s->repo == installed)
1023 queue_push(&uninstq, od.invedgedata[j]);
1024 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1025 queue_push(&samerepoq, od.invedgedata[j]);
1027 queue_push(&todo, od.invedgedata[j]);
1031 solv_free(temedianr);
1033 queue_free(&samerepoq);
1034 queue_free(&uninstq);
1036 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1037 assert(te->mark == 0);
1039 /* add back obsoleted packages */
1040 transaction_add_obsoleted(trans);
1041 assert(tr->count == oldcount);
1043 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1044 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1046 if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
1048 struct _TransactionOrderdata *tod;
1049 trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
1050 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
1052 Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
1053 queue_init_clone(cycles, &od.cyclesdata);
1054 /* map from tes to packages */
1055 for (i = 0; i < cycles->count; i++)
1056 if (cycles->elements[i])
1057 cycles->elements[i] = od.tes[cycles->elements[i]].p;
1058 queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
1059 queue_push(cycles, od.cycles.count / 4);
1061 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1065 tod->invedgedata = od.invedgedata;
1066 tod->ninvedgedata = od.nedgedata;
1072 solv_free(od.invedgedata);
1073 queue_free(&od.cycles);
1074 queue_free(&od.cyclesdata);
1079 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1082 struct _TransactionOrderdata *od = trans->orderdata;
1083 struct _TransactionElement *te;
1086 return choices->count;
1089 /* initialization step */
1090 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1092 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1094 for (j = te->edges; od->invedgedata[j]; j++)
1095 od->tes[od->invedgedata[j]].mark++;
1097 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1099 queue_push(choices, te->p);
1100 return choices->count;
1102 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1103 if (te->p == chosen)
1106 return choices->count;
1109 /* hey! out-of-order installation! */
1112 for (j = te->edges; od->invedgedata[j]; j++)
1114 te = od->tes + od->invedgedata[j];
1115 assert(te->mark > 0 || te->mark == -1);
1116 if (te->mark > 0 && --te->mark == 0)
1117 queue_push(choices, te->p);
1119 return choices->count;
1123 transaction_add_obsoleted(Transaction *trans)
1125 Pool *pool = trans->pool;
1126 Repo *installed = pool->installed;
1133 if (!installed || !trans->steps.count)
1135 /* calculate upper bound */
1137 FOR_REPO_SOLVABLES(installed, p, s)
1138 if (MAPTST(&trans->transactsmap, p))
1143 steps = &trans->steps;
1144 queue_insertn(steps, 0, max, 0);
1147 map_init(&done, installed->end - installed->start);
1149 for (j = 0, i = max; i < steps->count; i++)
1151 p = trans->steps.elements[i];
1152 if (pool->solvables[p].repo == installed)
1154 if (!trans->transaction_installed[p - pool->installed->start])
1155 trans->steps.elements[j++] = p;
1158 trans->steps.elements[j++] = p;
1160 transaction_all_obs_pkgs(trans, p, &obsq);
1161 for (k = 0; k < obsq.count; k++)
1163 p = obsq.elements[k];
1164 assert(p >= installed->start && p < installed->end);
1165 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1167 if (MAPTST(&done, p - installed->start))
1169 MAPSET(&done, p - installed->start);
1170 trans->steps.elements[j++] = p;
1174 /* free unneeded space */
1175 queue_truncate(steps, j);
1181 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1183 Pool *pool = trans->pool;
1188 if (MAPTST(seen, pkg))
1191 s = pool->solvables + pkg;
1193 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1199 reqp = s->repo->idarraydata + s->requires;
1200 while ((req = *reqp++) != 0)
1202 if (req == SOLVABLE_PREREQMARKER)
1207 if (onlyprereq && !inpre)
1209 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
1212 /* first check kept packages, then freshly installed, then not yet uninstalled */
1213 FOR_PROVIDES(p, pp, req)
1215 if (!MAPTST(ins, p))
1217 if (MAPTST(&trans->transactsmap, p))
1220 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1224 FOR_PROVIDES(p, pp, req)
1226 if (!MAPTST(ins, p))
1228 if (pool->solvables[p].repo == pool->installed)
1231 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1236 FOR_PROVIDES(p, pp, req)
1238 if (!MAPTST(ins, p))
1241 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1246 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));
1253 transaction_check_order(Transaction *trans)
1255 Pool *pool = trans->pool;
1261 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
1262 map_init(&ins, pool->nsolvables);
1263 map_init(&seen, pool->nsolvables);
1264 if (pool->installed)
1266 FOR_REPO_SOLVABLES(pool->installed, p, s)
1270 for (i = 0; i < trans->steps.count; i++)
1272 p = trans->steps.elements[i];
1273 s = pool->solvables + p;
1274 if (s->repo != pool->installed)
1276 if (s->repo != pool->installed)
1278 if (havescripts(pool, p))
1281 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1283 if (s->repo == pool->installed)
1288 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
1292 transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
1294 struct _TransactionOrderdata *od = trans->orderdata;
1296 int i, cid, ncycles;
1299 if (!od || !od->cycles || !od->cycles->count)
1302 ncycles = cq->elements[cq->count - 1];
1303 i = cq->count - 1 - ncycles * 4;
1304 for (cid = 1; cid <= ncycles; cid++, i += 4)
1308 int cmin = cq->elements[i + 3] & 0xffff;
1309 int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
1310 if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
1312 if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
1320 transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
1322 struct _TransactionOrderdata *od = trans->orderdata;
1324 int cmin, cmax, severity;
1328 if (!od || !od->cycles || !od->cycles->count)
1329 return SOLVER_ORDERCYCLE_HARMLESS;
1331 ncycles = cq->elements[cq->count - 1];
1332 if (cid < 1 || cid > ncycles)
1333 return SOLVER_ORDERCYCLE_HARMLESS;
1334 cid = cq->count - 1 - 4 * (ncycles - cid + 1);
1335 cmin = cq->elements[cid + 3] & 0xffff;
1336 cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
1337 if (cmin < TYPE_REQ)
1338 severity = SOLVER_ORDERCYCLE_HARMLESS;
1339 else if ((cmax & TYPE_PREREQ) == 0)
1340 severity = SOLVER_ORDERCYCLE_NORMAL;
1342 severity = SOLVER_ORDERCYCLE_CRITICAL;
1344 queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);