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"
28 obsq_sortcmp(const void *ap, const void *bp, void *dp)
32 Solvable *s, *oas, *obs;
43 s = pool->solvables + a;
44 oas = pool->solvables + oa;
45 obs = pool->solvables + ob;
46 if (oas->name != obs->name)
48 if (oas->name == s->name)
50 if (obs->name == s->name)
52 return strcmp(id2str(pool, oas->name), id2str(pool, obs->name));
54 r = evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
56 return -r; /* highest version first */
61 solver_transaction_all_pkgs(Transaction *trans, Id p, Queue *pkgs)
63 Pool *pool = trans->pool;
64 Solvable *s = pool->solvables + p;
65 Queue *ti = &trans->transaction_info;
70 if (p <= 0 || !s->repo)
72 if (s->repo == pool->installed)
74 q = trans->transaction_installed[p - pool->installed->start];
82 /* find which packages obsolete us */
83 for (i = 0; i < ti->count; i += 2)
84 if (ti->elements[i + 1] == p)
87 queue_push(pkgs, ti->elements[i]);
91 sat_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
92 for (i = 0; i < pkgs->count; i += 2)
93 pkgs->elements[i / 2] = pkgs->elements[i + 1];
98 /* find the packages we obsolete */
99 for (i = 0; i < ti->count; i += 2)
101 if (ti->elements[i] == p)
102 queue_push(pkgs, ti->elements[i + 1]);
103 else if (pkgs->count)
110 solver_transaction_pkg(Transaction *trans, Id p)
112 Pool *pool = trans->pool;
113 Solvable *s = pool->solvables + p;
117 if (p <= 0 || !s->repo)
119 if (s->repo == pool->installed)
121 p = trans->transaction_installed[p - pool->installed->start];
122 return p < 0 ? -p : p;
124 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
125 solver_transaction_all_pkgs(trans, p, &ti);
126 p = ti.count ? ti.elements[0] : 0;
131 /* type filtering, needed if either not all packages are shown
132 * or replaces are not shown, as otherwise parts of the
133 * transaction might not be shown to the user */
136 solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
138 Pool *pool = trans->pool;
139 Solvable *s = pool->solvables + p;
144 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
147 if (s->repo == pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
150 if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACED)
151 type = SOLVER_TRANSACTION_ERASE;
154 if (s->repo != pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
156 if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACE)
157 type = SOLVER_TRANSACTION_INSTALL;
161 /* most of the time there's only one reference, so check it first */
162 q = solver_transaction_pkg(trans, p);
163 if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
165 Solvable *sq = pool->solvables + q;
166 if (sq->name != s->name)
168 if (s->repo == pool->installed)
169 return SOLVER_TRANSACTION_ERASE;
170 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
171 return SOLVER_TRANSACTION_MULTIINSTALL;
173 return SOLVER_TRANSACTION_INSTALL;
176 if (solver_transaction_pkg(trans, q) == p)
179 /* too bad, a miss. check em all */
182 solver_transaction_all_pkgs(trans, p, &oq);
183 for (i = 0; i < oq.count; i++)
186 if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
188 Solvable *sq = pool->solvables + q;
189 if (sq->name != s->name)
192 /* check if we are referenced? */
193 if ((flags & SOLVER_TRANSACTION_SHOW_ALL) != 0)
195 solver_transaction_all_pkgs(trans, q, &rq);
196 for (j = 0; j < rq.count; j++)
197 if (rq.elements[j] == p)
205 else if (solver_transaction_pkg(trans, q) == p)
216 if (s->repo == pool->installed)
217 type = SOLVER_TRANSACTION_ERASE;
218 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
219 type = SOLVER_TRANSACTION_MULTIINSTALL;
221 type = SOLVER_TRANSACTION_INSTALL;
227 create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
229 Pool *pool = trans->pool;
230 Queue *ti = &trans->transaction_info;
231 Repo *installed = pool->installed;
238 return; /* no info needed */
239 for (i = 0; i < decisionq->count; i++)
241 p = decisionq->elements[i];
242 if (p <= 0 || p == SYSTEMSOLVABLE)
244 s = pool->solvables + p;
245 if (s->repo == installed)
247 noobs = noobsmap && MAPTST(noobsmap, p);
248 FOR_PROVIDES(p2, pp2, s->name)
250 if (!MAPTST(&trans->transactsmap, p2))
252 s2 = pool->solvables + p2;
253 if (s2->repo != installed)
255 if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
257 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
262 if (s->obsoletes && !noobs)
264 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
265 while ((obs = *obsp++) != 0)
267 FOR_PROVIDES(p2, pp2, obs)
269 s2 = pool->solvables + p2;
270 if (s2->repo != installed)
272 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
280 sat_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
282 for (i = j = 0; i < ti->count; i += 2)
284 if (j && ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
286 ti->elements[j++] = ti->elements[i];
287 ti->elements[j++] = ti->elements[i + 1];
291 /* create transaction_installed helper */
292 trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
293 for (i = 0; i < ti->count; i += 2)
295 j = ti->elements[i + 1] - installed->start;
296 if (!trans->transaction_installed[j])
297 trans->transaction_installed[j] = ti->elements[i];
300 /* more than one package obsoletes us. compare */
302 if (trans->transaction_installed[j] > 0)
303 trans->transaction_installed[j] = -trans->transaction_installed[j];
304 q[0] = q[2] = ti->elements[i + 1];
305 q[1] = ti->elements[i];
306 q[3] = -trans->transaction_installed[j];
307 if (obsq_sortcmp(q, q + 2, pool) < 0)
308 trans->transaction_installed[j] = -ti->elements[i];
314 transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
316 Pool *pool = trans->pool;
317 Repo *installed = pool->installed;
322 if (noobsmap && !noobsmap->size)
323 noobsmap = 0; /* ignore empty map */
324 queue_empty(&trans->steps);
325 map_init(&trans->transactsmap, pool->nsolvables);
326 for (i = 0; i < decisionq->count; i++)
328 p = decisionq->elements[i];
329 s = pool->solvables + (p > 0 ? p : -p);
332 if (installed && s->repo == installed && p < 0)
333 MAPSET(&trans->transactsmap, -p);
334 if ((!installed || s->repo != installed) && p > 0)
335 MAPSET(&trans->transactsmap, p);
337 create_transaction_info(trans, decisionq, noobsmap);
341 FOR_REPO_SOLVABLES(installed, p, s)
343 if (!MAPTST(&trans->transactsmap, p))
345 p2 = solver_transaction_pkg(trans, p);
347 queue_push(&trans->steps, SOLVER_TRANSACTION_ERASE);
350 s2 = pool->solvables + p2;
351 if (s->name == s2->name)
353 if (s->evr == s2->evr && solvable_identical(s, s2))
354 queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALLED);
357 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
359 queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADED);
361 queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADED);
363 queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGED);
367 queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACED);
369 queue_push(&trans->steps, p);
372 for (i = 0; i < decisionq->count; i++)
374 p = decisionq->elements[i];
375 if (p < 0 || p == SYSTEMSOLVABLE)
377 s = pool->solvables + p;
378 if (installed && s->repo == installed)
380 noobs = noobsmap && MAPTST(noobsmap, p);
381 p2 = solver_transaction_pkg(trans, p);
383 queue_push(&trans->steps, p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL);
385 queue_push(&trans->steps, SOLVER_TRANSACTION_INSTALL);
388 s2 = pool->solvables + p2;
389 if (s->name == s2->name)
391 if (s->evr == s2->evr && solvable_identical(s, s2))
392 queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALL);
395 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
397 queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADE);
399 queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADE);
401 queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGE);
405 queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACE);
407 queue_push(&trans->steps, p);
412 struct _TransactionElement {
413 Id p; /* solvable id */
414 Id type; /* installation type */
415 Id edges; /* pointer into edges data */
419 struct _TransactionOrderdata {
420 struct _TransactionElement *tes;
425 #define TYPE_BROKEN (1<<0)
426 #define TYPE_CON (1<<1)
428 #define TYPE_REQ_P (1<<2)
429 #define TYPE_PREREQ_P (1<<3)
431 #define TYPE_REQ (1<<4)
432 #define TYPE_PREREQ (1<<5)
434 #define TYPE_CYCLETAIL (1<<16)
435 #define TYPE_CYCLEHEAD (1<<17)
437 #define EDGEDATA_BLOCK 127
440 transaction_init(Transaction *trans, Pool *pool)
442 memset(trans, 0, sizeof(*trans));
447 transaction_free(Transaction *trans)
449 queue_free(&trans->steps);
450 queue_free(&trans->transaction_info);
451 trans->transaction_installed = sat_free(trans->transaction_installed);
452 map_free(&trans->transactsmap);
453 if (trans->orderdata)
455 struct _TransactionOrderdata *od = trans->orderdata;
456 od->tes = sat_free(od->tes);
457 od->invedgedata = sat_free(od->invedgedata);
458 trans->orderdata = sat_free(trans->orderdata);
464 struct _TransactionElement *tes;
476 addteedge(struct orderdata *od, int from, int to, int type)
479 struct _TransactionElement *te;
484 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
487 for (i = te->edges; od->edgedata[i]; i += 2)
488 if (od->edgedata[i] == to)
490 /* test of brokenness */
491 if (type == TYPE_BROKEN)
492 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
495 od->edgedata[i + 1] |= type;
498 if (i + 1 == od->nedgedata)
500 /* printf("tail add %d\n", i - te->edges); */
503 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
507 /* printf("extend %d\n", i - te->edges); */
508 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
510 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
511 i = od->nedgedata + (i - te->edges);
512 te->edges = od->nedgedata;
514 od->edgedata[i] = to;
515 od->edgedata[i + 1] = type;
516 od->edgedata[i + 2] = 0; /* end marker */
517 od->nedgedata = i + 3;
522 addedge(struct orderdata *od, Id from, Id to, int type)
524 Transaction *trans = od->trans;
525 Pool *pool = trans->pool;
527 struct _TransactionElement *te;
530 // printf("addedge %d %d type %d\n", from, to, type);
531 s = pool->solvables + from;
532 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
534 /* obsolete, map to install */
535 if (trans->transaction_installed[from - pool->installed->start] > 0)
536 from = trans->transaction_installed[from - pool->installed->start];
543 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
544 solver_transaction_all_pkgs(trans, from, &ti);
545 for (i = 0; i < ti.count; i++)
546 ret |= addedge(od, ti.elements[i], to, type);
551 s = pool->solvables + to;
552 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
554 /* obsolete, map to install */
555 if (trans->transaction_installed[to - pool->installed->start] > 0)
556 to = trans->transaction_installed[to - pool->installed->start];
563 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
564 solver_transaction_all_pkgs(trans, to, &ti);
565 for (i = 0; i < ti.count; i++)
566 ret |= addedge(od, from, ti.elements[i], type);
572 /* map from/to to te numbers */
573 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
580 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
586 return addteedge(od, i, to, type);
591 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
593 Transaction *trans = od->trans;
594 Pool *pool = trans->pool;
595 Id ti1buf[5], ti2buf[5];
599 /* both q1 and q2 are uninstalls. check if their TEs intersect */
600 /* common case: just one TE for both packages */
602 printf("havechoice %d %d %d\n", p, q1, q2);
604 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
606 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
608 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
610 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
612 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
613 solver_transaction_all_pkgs(trans, q1, &ti1);
614 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
615 solver_transaction_all_pkgs(trans, q2, &ti2);
616 for (i = 0; i < ti1.count; i++)
617 for (j = 0; j < ti2.count; j++)
618 if (ti1.elements[i] == ti2.elements[j])
620 /* found a common edge */
632 havescripts(Pool *pool, Id solvid)
634 Solvable *s = pool->solvables + solvid;
640 reqp = s->repo->idarraydata + s->requires;
641 while ((req = *reqp++) != 0)
643 if (req == SOLVABLE_PREREQMARKER)
650 dep = id2str(pool, req);
651 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
659 addsolvableedges(struct orderdata *od, Solvable *s)
661 Transaction *trans = od->trans;
662 Pool *pool = trans->pool;
663 Id req, *reqp, con, *conp;
665 int i, j, pre, numins;
666 Repo *installed = pool->installed;
672 printf("addsolvableedges %s\n", solvable2str(pool, s));
674 p = s - pool->solvables;
678 reqp = s->repo->idarraydata + s->requires;
680 while ((req = *reqp++) != 0)
682 if (req == SOLVABLE_PREREQMARKER)
688 if (pre != TYPE_PREREQ && installed && s->repo == installed)
690 /* ignore normal requires if we're getting obsoleted */
691 if (trans->transaction_installed[p - pool->installed->start])
696 numins = 0; /* number of packages to be installed providing it */
697 provbyinst = 0; /* provided by kept package */
698 FOR_PROVIDES(p2, pp2, req)
700 s2 = pool->solvables + p2;
703 reqq.count = 0; /* self provides */
706 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
710 printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
711 reqq.count = 0; /* provided by package that stays installed */
717 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
718 continue; /* package stays uninstalled */
720 if (s->repo == installed)
722 /* s gets uninstalled */
723 queue_pushunique(&reqq, p2);
724 if (s2->repo != installed)
729 if (s2->repo == installed)
730 continue; /* s2 gets uninstalled */
731 queue_pushunique(&reqq, p2);
736 /* prune to harmless ->inst edges */
737 for (i = j = 0; i < reqq.count; i++)
738 if (pool->solvables[reqq.elements[i]].repo != installed)
739 reqq.elements[j++] = reqq.elements[i];
743 if (numins && reqq.count)
745 if (s->repo == installed)
747 for (i = 0; i < reqq.count; i++)
749 if (pool->solvables[reqq.elements[i]].repo == installed)
751 for (j = 0; j < reqq.count; j++)
753 if (pool->solvables[reqq.elements[j]].repo != installed)
755 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
756 continue; /* no self edge */
758 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
760 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
766 /* no mixed types, remove all deps on uninstalls */
767 for (i = j = 0; i < reqq.count; i++)
768 if (pool->solvables[reqq.elements[i]].repo != installed)
769 reqq.elements[j++] = reqq.elements[i];
774 for (i = 0; i < reqq.count; i++)
777 p2 = reqq.elements[i];
778 if (pool->solvables[p2].repo != installed)
780 /* all elements of reqq are installs, thus have different TEs */
781 choice = reqq.count - 1;
782 if (pool->solvables[p].repo != installed)
785 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
787 addedge(od, p, p2, pre);
792 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
794 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
799 if (s->repo != installed)
800 continue; /* no inst->uninst edges, please! */
802 /* uninst -> uninst edge. Those make trouble. Only add if we must */
803 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
805 /* p is obsoleted by another package and has no scripts */
806 /* we assume that the obsoletor is good enough to replace p */
811 for (j = 0; j < reqq.count; j++)
815 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
820 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
822 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
829 conp = s->repo->idarraydata + s->conflicts;
830 while ((con = *conp++) != 0)
832 FOR_PROVIDES(p2, pp2, con)
836 s2 = pool->solvables + p2;
839 if (s->repo == installed)
841 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
843 /* deinstall p before installing p2 */
844 addedge(od, p2, p, TYPE_CON);
849 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
851 /* deinstall p2 before installing p */
852 addedge(od, p, p2, TYPE_CON);
863 /* break an edge in a cycle */
865 breakcycle(struct orderdata *od, Id *cycle)
867 Pool *pool = od->trans->pool;
868 Id ddegmin, ddegmax, ddeg;
870 struct _TransactionElement *te;
873 ddegmin = ddegmax = 0;
874 for (k = 0; cycle[k + 1]; k += 2)
876 ddeg = od->edgedata[cycle[k + 1] + 1];
879 if (!k || ddeg < ddegmin)
887 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
889 /* prefer k, as l comes from a package with contains scriptlets */
894 /* same edge value, check for prereq */
898 /* record brkoen cycle starting with the tail */
899 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
900 queue_push(&od->cycles, k / 2); /* cycle elements */
901 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
908 queue_push(&od->cyclesdata, cycle[k]);
912 queue_push(&od->cyclesdata, 0); /* mark end */
914 /* break that edge */
915 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
918 if (ddegmin < TYPE_REQ)
922 /* cycle recorded, print it */
923 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
925 printf("cycle: --> ");
926 for (k = 0; cycle[k + 1]; k += 2)
928 te = od->tes + cycle[k];
929 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
930 printf("%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
932 printf("%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
938 dump_tes(struct orderdata *od)
940 Pool *pool = od->trans->pool;
943 struct _TransactionElement *te, *te2;
946 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
948 Solvable *s = pool->solvables + te->p;
949 printf("TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
950 if (s->repo != pool->installed)
953 solver_transaction_all_pkgs(od->trans, te->p, &obsq);
954 for (j = 0; j < obsq.count; j++)
955 printf(" -%s\n", solvid2str(pool, obsq.elements[j]));
957 for (j = te->edges; od->edgedata[j]; j += 2)
959 te2 = od->tes + od->edgedata[j];
960 printf(" --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
967 reachable(struct orderdata *od, Id i)
969 struct _TransactionElement *te = od->tes + i;
975 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
977 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
979 if (!od->tes[k].mark)
981 if (od->tes[k].mark == 2)
992 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
995 Transaction *trans = od->trans;
996 Pool *pool = trans->pool;
998 struct _TransactionElement *te;
1005 printf("addcycleedges\n");
1006 for (i = 0; (j = cycle[i]) != 0; i++)
1007 printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
1010 /* first add all the tail cycle edges */
1012 /* see what we can reach from the cycle */
1014 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1016 for (i = 0; (j = cycle[i]) != 0; i++)
1018 od->tes[j].mark = -1;
1019 queue_push(todo, j);
1023 i = queue_pop(todo);
1027 te->mark = te->mark < 0 ? 2 : 1;
1028 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1030 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1032 if (od->tes[k].mark > 0)
1033 continue; /* no need to visit again */
1034 queue_push(todo, k);
1037 /* now all cycle TEs are marked with 2, all TEs reachable
1038 * from the cycle are marked with 1 */
1040 od->tes[tail].mark = 1; /* no need to add edges */
1042 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1045 continue; /* reachable from cycle */
1046 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1048 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1050 if (od->tes[k].mark != 2)
1052 /* We found an edge to the cycle. Add an extra edge to the tail */
1053 /* the TE was not reachable, so we're not creating a new cycle! */
1055 printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, solvid2str(pool, od->tes[i].p), solvid2str(pool, od->tes[tail].p));
1057 j -= te->edges; /* in case we move */
1058 addteedge(od, i, tail, TYPE_CYCLETAIL);
1060 break; /* one edge is enough */
1065 /* now add all head cycle edges */
1068 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1071 for (i = 0; (j = cycle[i]) != 0; i++)
1074 od->tes[j].mark = 2;
1076 /* first the head to save some time */
1077 te = od->tes + head;
1078 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1080 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1082 if (!od->tes[k].mark)
1084 if (od->tes[k].mark == -1)
1085 od->tes[k].mark = -2; /* no need for another edge */
1087 for (i = 0; cycle[i] != 0; i++)
1089 if (cycle[i] == head)
1091 te = od->tes + cycle[i];
1092 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1094 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1096 /* see if we can reach a cycle TE from k */
1097 if (!od->tes[k].mark)
1099 if (od->tes[k].mark == -1)
1102 printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, solvid2str(pool, od->tes[head].p), solvid2str(pool, od->tes[k].p), solvid2str(pool, od->tes[cycle[i]].p));
1104 addteedge(od, head, k, TYPE_CYCLEHEAD);
1105 od->tes[k].mark = -2; /* no need to add that one again */
1113 transaction_order(Transaction *trans, int flags)
1115 Pool *pool = trans->pool;
1116 Queue *tr = &trans->steps;
1117 Repo *installed = pool->installed;
1120 int i, j, k, numte, numedge;
1121 struct orderdata od;
1122 struct _TransactionElement *te;
1124 int cycstart, cycel;
1125 Id *cycle, *obstypes;
1129 start = now = sat_timems(0);
1130 POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1131 /* free old data if present */
1132 if (trans->orderdata)
1134 struct _TransactionOrderdata *od = trans->orderdata;
1135 od->tes = sat_free(od->tes);
1136 od->invedgedata = sat_free(od->invedgedata);
1137 trans->orderdata = sat_free(trans->orderdata);
1140 /* create a transaction element for every active component */
1142 for (i = 0; i < tr->count; i += 2)
1144 p = tr->elements[i + 1];
1145 s = pool->solvables + p;
1146 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1150 POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1152 return; /* nothing to do... */
1154 numte++; /* leave first one zero */
1155 memset(&od, 0, sizeof(od));
1158 od.tes = sat_calloc(numte, sizeof(*od.tes));
1159 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1162 queue_init(&od.cycles);
1164 for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
1166 p = tr->elements[i + 1];
1167 s = pool->solvables + p;
1168 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1171 te->type = tr->elements[i];
1175 /* create dependency graph */
1176 for (i = 0; i < tr->count; i += 2)
1178 type = tr->elements[i];
1179 p = tr->elements[i + 1];
1180 addsolvableedges(&od, pool->solvables + p);
1185 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1186 for (j = te->edges; od.edgedata[j]; j += 2)
1188 POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1189 POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1195 now = sat_timems(0);
1196 /* kill all cycles */
1198 for (i = numte - 1; i > 0; i--)
1199 queue_push(&todo, i);
1203 i = queue_pop(&todo);
1204 /* printf("- look at TE %d\n", i); */
1208 od.tes[i].mark = 2; /* done with that one */
1213 continue; /* already finished before */
1216 int edgestovisit = 0;
1217 /* new node, visit edges */
1218 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1220 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1222 if (od.tes[k].mark == 2)
1223 continue; /* no need to visit again */
1224 if (!edgestovisit++)
1225 queue_push(&todo, -i); /* end of edges marker */
1226 queue_push(&todo, k);
1229 te->mark = 2; /* no edges, done with that one */
1231 te->mark = 1; /* under investigation */
1234 /* oh no, we found a cycle */
1235 /* find start of cycle node (<0) */
1236 for (j = todo.count - 1; j >= 0; j--)
1237 if (todo.elements[j] == -i)
1241 /* build te/edge chain */
1243 for (j = k; j < todo.count; j++)
1244 if (todo.elements[j] < 0)
1245 todo.elements[k++] = -todo.elements[j];
1246 cycel = k - cycstart;
1248 /* make room for edges, two extra element for cycle loop + terminating 0 */
1249 while (todo.count < cycstart + 2 * cycel + 2)
1250 queue_push(&todo, 0);
1251 cycle = todo.elements + cycstart;
1252 cycle[cycel] = i; /* close the loop */
1253 cycle[2 * cycel + 1] = 0; /* terminator */
1254 for (k = cycel; k > 0; k--)
1256 cycle[k * 2] = cycle[k];
1257 te = od.tes + cycle[k - 1];
1258 assert(te->mark == 1);
1259 te->mark = 0; /* reset investigation marker */
1260 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1261 for (j = te->edges; od.edgedata[j]; j += 2)
1262 if (od.edgedata[j] == cycle[k])
1264 assert(od.edgedata[j]);
1265 cycle[k * 2 - 1] = j;
1267 /* now cycle looks like this: */
1268 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1269 breakcycle(&od, cycle);
1270 /* restart with start of cycle */
1271 todo.count = cycstart + 1;
1273 POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1274 POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1276 now = sat_timems(0);
1277 /* now go through all broken cycles and create cycle edges to help
1279 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1281 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1282 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1284 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1286 if (od.cycles.elements[i + 2] < TYPE_REQ)
1287 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1289 POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1291 /* all edges are finally set up and there are no cycles, now the easy part.
1292 * Create an ordered transaction */
1293 now = sat_timems(0);
1294 /* first invert all edges */
1295 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1296 te->mark = 1; /* term 0 */
1297 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1299 for (j = te->edges; od.edgedata[j]; j += 2)
1301 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1303 od.tes[od.edgedata[j]].mark++;
1307 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1312 POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1313 od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1314 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1316 for (j = te->edges; od.edgedata[j]; j += 2)
1318 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1320 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1323 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1324 te->edges = te->mark; /* edges now points into invedgedata */
1325 od.edgedata = sat_free(od.edgedata);
1327 /* now the final ordering */
1328 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1330 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1331 for (j = te->edges; od.invedgedata[j]; j++)
1332 od.tes[od.invedgedata[j]].mark++;
1333 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1335 queue_push(&todo, i);
1336 assert(todo.count > 0);
1339 obstypes = sat_calloc(installed->end - installed->start, sizeof(Id));
1340 for (i = 0; i < tr->count; i += 2)
1342 p = tr->elements[i + 1];
1343 s = pool->solvables + p;
1344 if (s->repo == installed && trans->transaction_installed[p - installed->start])
1345 obstypes[p - installed->start] = tr->elements[i];
1350 oldcount = tr->count;
1360 for (i = 0; i < todo.count; i++)
1362 j = todo.elements[i];
1363 if (pool->solvables[od.tes[j].p].repo == installed)
1367 if (i == todo.count)
1369 te = od.tes + todo.elements[i];
1370 queue_push(tr, te->type);
1371 queue_push(tr, te->p);
1373 printf("do %s\n", solvid2str(pool, te->p));
1375 s = pool->solvables + te->p;
1376 if (installed && s->repo != installed)
1379 solver_transaction_all_pkgs(trans, te->p, &obsq);
1380 for (j = 0; j < obsq.count; j++)
1382 p = obsq.elements[j];
1383 assert(p >= installed->start && p < installed->end);
1384 if (obstypes[p - installed->start])
1386 queue_push(tr, obstypes[p - installed->start]);
1388 obstypes[p - installed->start] = 0;
1392 queue_delete(&todo, i);
1393 for (j = te->edges; od.invedgedata[j]; j++)
1395 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1396 assert(te2->mark > 0);
1397 if (--te2->mark == 0)
1399 queue_push(&todo, od.invedgedata[j]);
1401 printf("free %s\n", solvid2str(pool, od.tes[od.invedgedata[j]].p));
1409 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1410 assert(te->mark == 0);
1411 assert(tr->count == oldcount);
1412 POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1413 POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1415 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1417 trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1418 trans->orderdata->tes = od.tes;
1419 trans->orderdata->ntes = numte;
1420 trans->orderdata->invedgedata = od.invedgedata;
1425 sat_free(od.invedgedata);
1430 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1432 Pool *pool = trans->pool;
1437 if (MAPTST(seen, pkg))
1440 s = pool->solvables + pkg;
1442 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1448 reqp = s->repo->idarraydata + s->requires;
1449 while ((req = *reqp++) != 0)
1451 if (req == SOLVABLE_PREREQMARKER)
1456 if (onlyprereq && !inpre)
1458 if (!strncmp(id2str(pool, req), "rpmlib(", 7))
1461 /* first check kept packages, then freshly installed, then not yet uninstalled */
1462 FOR_PROVIDES(p, pp, req)
1464 if (!MAPTST(ins, p))
1466 if (MAPTST(&trans->transactsmap, p))
1469 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1473 FOR_PROVIDES(p, pp, req)
1475 if (!MAPTST(ins, p))
1477 if (pool->solvables[p].repo == pool->installed)
1480 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1485 FOR_PROVIDES(p, pp, req)
1487 if (!MAPTST(ins, p))
1490 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1495 printf(" %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', solvid2str(pool, tepkg), dep2str(pool, req), s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1502 transaction_check(Transaction *trans)
1504 Pool *pool = trans->pool;
1506 Id p, type, lastins;
1510 printf("\nchecking transaction order...\n");
1511 map_init(&ins, pool->nsolvables);
1512 map_init(&seen, pool->nsolvables);
1513 if (pool->installed)
1514 FOR_REPO_SOLVABLES(pool->installed, p, s)
1517 for (i = 0; i < trans->steps.count; i += 2)
1519 type = trans->steps.elements[i];
1520 p = trans->steps.elements[i + 1];
1521 s = pool->solvables + p;
1522 if (s->repo != pool->installed)
1524 if (s->repo != pool->installed)
1526 if (havescripts(pool, p))
1529 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1531 if (s->repo == pool->installed)
1536 printf("transaction order check done.\n");
1540 transaction_add_choices(Transaction *trans, Queue *choices, Id chosen)
1543 struct _TransactionOrderdata *od = trans->orderdata;
1544 struct _TransactionElement *te;
1547 return choices->count;
1550 /* initialization step */
1551 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1553 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1555 for (j = te->edges; od->invedgedata[j]; j++)
1556 od->tes[od->invedgedata[j]].mark++;
1558 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1561 queue_push(choices, te->type);
1562 queue_push(choices, te->p);
1564 return choices->count;
1566 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1567 if (te->p == chosen)
1570 return choices->count;
1573 /* hey! out-of-order installation! */
1576 for (j = te->edges; od->invedgedata[j]; j++)
1578 te = od->tes + od->invedgedata[j];
1579 assert(te->mark > 0 || te->mark == -1);
1580 if (te->mark > 0 && --te->mark == 0)
1582 queue_push(choices, te->type);
1583 queue_push(choices, te->p);
1586 return choices->count;