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;
1123 Queue todo, obsq, samerepoq, uninstq;
1124 int cycstart, cycel;
1125 Id *cycle, *obstypes;
1132 start = now = sat_timems(0);
1133 POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1134 /* free old data if present */
1135 if (trans->orderdata)
1137 struct _TransactionOrderdata *od = trans->orderdata;
1138 od->tes = sat_free(od->tes);
1139 od->invedgedata = sat_free(od->invedgedata);
1140 trans->orderdata = sat_free(trans->orderdata);
1143 /* create a transaction element for every active component */
1145 for (i = 0; i < tr->count; i += 2)
1147 p = tr->elements[i + 1];
1148 s = pool->solvables + p;
1149 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1153 POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1155 return; /* nothing to do... */
1157 numte++; /* leave first one zero */
1158 memset(&od, 0, sizeof(od));
1161 od.tes = sat_calloc(numte, sizeof(*od.tes));
1162 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1165 queue_init(&od.cycles);
1167 for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
1169 p = tr->elements[i + 1];
1170 s = pool->solvables + p;
1171 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1174 te->type = tr->elements[i];
1178 /* create dependency graph */
1179 for (i = 0; i < tr->count; i += 2)
1181 type = tr->elements[i];
1182 p = tr->elements[i + 1];
1183 addsolvableedges(&od, pool->solvables + p);
1188 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1189 for (j = te->edges; od.edgedata[j]; j += 2)
1191 POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1192 POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1198 now = sat_timems(0);
1199 /* kill all cycles */
1201 for (i = numte - 1; i > 0; i--)
1202 queue_push(&todo, i);
1206 i = queue_pop(&todo);
1207 /* printf("- look at TE %d\n", i); */
1211 od.tes[i].mark = 2; /* done with that one */
1216 continue; /* already finished before */
1219 int edgestovisit = 0;
1220 /* new node, visit edges */
1221 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1223 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1225 if (od.tes[k].mark == 2)
1226 continue; /* no need to visit again */
1227 if (!edgestovisit++)
1228 queue_push(&todo, -i); /* end of edges marker */
1229 queue_push(&todo, k);
1232 te->mark = 2; /* no edges, done with that one */
1234 te->mark = 1; /* under investigation */
1237 /* oh no, we found a cycle */
1238 /* find start of cycle node (<0) */
1239 for (j = todo.count - 1; j >= 0; j--)
1240 if (todo.elements[j] == -i)
1244 /* build te/edge chain */
1246 for (j = k; j < todo.count; j++)
1247 if (todo.elements[j] < 0)
1248 todo.elements[k++] = -todo.elements[j];
1249 cycel = k - cycstart;
1251 /* make room for edges, two extra element for cycle loop + terminating 0 */
1252 while (todo.count < cycstart + 2 * cycel + 2)
1253 queue_push(&todo, 0);
1254 cycle = todo.elements + cycstart;
1255 cycle[cycel] = i; /* close the loop */
1256 cycle[2 * cycel + 1] = 0; /* terminator */
1257 for (k = cycel; k > 0; k--)
1259 cycle[k * 2] = cycle[k];
1260 te = od.tes + cycle[k - 1];
1261 assert(te->mark == 1);
1262 te->mark = 0; /* reset investigation marker */
1263 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1264 for (j = te->edges; od.edgedata[j]; j += 2)
1265 if (od.edgedata[j] == cycle[k])
1267 assert(od.edgedata[j]);
1268 cycle[k * 2 - 1] = j;
1270 /* now cycle looks like this: */
1271 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1272 breakcycle(&od, cycle);
1273 /* restart with start of cycle */
1274 todo.count = cycstart + 1;
1276 POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1277 POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1279 now = sat_timems(0);
1280 /* now go through all broken cycles and create cycle edges to help
1282 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1284 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1285 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1287 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1289 if (od.cycles.elements[i + 2] < TYPE_REQ)
1290 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1292 POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1294 /* all edges are finally set up and there are no cycles, now the easy part.
1295 * Create an ordered transaction */
1296 now = sat_timems(0);
1297 /* first invert all edges */
1298 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1299 te->mark = 1; /* term 0 */
1300 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1302 for (j = te->edges; od.edgedata[j]; j += 2)
1304 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1306 od.tes[od.edgedata[j]].mark++;
1310 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1315 POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1316 od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1317 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1319 for (j = te->edges; od.edgedata[j]; j += 2)
1321 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1323 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1326 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1327 te->edges = te->mark; /* edges now points into invedgedata */
1328 od.edgedata = sat_free(od.edgedata);
1330 /* now the final ordering */
1331 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1333 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1334 for (j = te->edges; od.invedgedata[j]; j++)
1335 od.tes[od.invedgedata[j]].mark++;
1337 queue_init(&samerepoq);
1338 queue_init(&uninstq);
1340 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1343 if (installed && pool->solvables[te->p].repo == installed)
1344 queue_push(&uninstq, i);
1346 queue_push(&todo, i);
1348 assert(todo.count > 0 || uninstq.count > 0);
1351 obstypes = sat_calloc(installed->end - installed->start, sizeof(Id));
1352 for (i = 0; i < tr->count; i += 2)
1354 p = tr->elements[i + 1];
1355 s = pool->solvables + p;
1356 if (s->repo == installed && trans->transaction_installed[p - installed->start])
1357 obstypes[p - installed->start] = tr->elements[i];
1362 oldcount = tr->count;
1369 temedianr = sat_calloc(numte, sizeof(Id));
1370 for (i = 1; i < numte; i++)
1372 Solvable *s = pool->solvables + od.tes[i].p;
1373 if (installed && s->repo == installed)
1376 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1381 /* select an TE i */
1383 i = queue_shift(&uninstq);
1384 else if (samerepoq.count)
1385 i = queue_shift(&samerepoq);
1386 else if (todo.count)
1388 /* find next repo/media */
1389 for (j = 0; j < todo.count; j++)
1391 if (!j || temedianr[todo.elements[j]] < lastmedia)
1394 lastmedia = temedianr[todo.elements[j]];
1397 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1399 /* move all matching TEs to samerepoq */
1400 for (i = j = 0; j < todo.count; j++)
1402 int k = todo.elements[j];
1403 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1404 queue_push(&samerepoq, k);
1406 todo.elements[i++] = k;
1410 assert(samerepoq.count);
1411 i = queue_shift(&samerepoq);
1417 queue_push(tr, te->type);
1418 queue_push(tr, te->p);
1420 printf("do %s [%d]\n", solvid2str(pool, te->p), temedianr[i]);
1422 s = pool->solvables + te->p;
1423 if (installed && s->repo != installed)
1426 solver_transaction_all_pkgs(trans, te->p, &obsq);
1427 for (j = 0; j < obsq.count; j++)
1429 p = obsq.elements[j];
1430 assert(p >= installed->start && p < installed->end);
1431 if (obstypes[p - installed->start])
1433 queue_push(tr, obstypes[p - installed->start]);
1435 obstypes[p - installed->start] = 0;
1439 for (j = te->edges; od.invedgedata[j]; j++)
1441 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1442 assert(te2->mark > 0);
1443 if (--te2->mark == 0)
1445 Solvable *s = pool->solvables + te2->p;
1447 printf("free %s [%d]\n", solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1449 if (installed && s->repo == installed)
1450 queue_push(&uninstq, od.invedgedata[j]);
1451 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1452 queue_push(&samerepoq, od.invedgedata[j]);
1454 queue_push(&todo, od.invedgedata[j]);
1459 sat_free(temedianr);
1461 queue_free(&samerepoq);
1462 queue_free(&uninstq);
1464 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1465 assert(te->mark == 0);
1466 assert(tr->count == oldcount);
1467 POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1468 POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1470 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1472 trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1473 trans->orderdata->tes = od.tes;
1474 trans->orderdata->ntes = numte;
1475 trans->orderdata->invedgedata = od.invedgedata;
1480 sat_free(od.invedgedata);
1485 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1487 Pool *pool = trans->pool;
1492 if (MAPTST(seen, pkg))
1495 s = pool->solvables + pkg;
1497 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1503 reqp = s->repo->idarraydata + s->requires;
1504 while ((req = *reqp++) != 0)
1506 if (req == SOLVABLE_PREREQMARKER)
1511 if (onlyprereq && !inpre)
1513 if (!strncmp(id2str(pool, req), "rpmlib(", 7))
1516 /* first check kept packages, then freshly installed, then not yet uninstalled */
1517 FOR_PROVIDES(p, pp, req)
1519 if (!MAPTST(ins, p))
1521 if (MAPTST(&trans->transactsmap, p))
1524 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1528 FOR_PROVIDES(p, pp, req)
1530 if (!MAPTST(ins, p))
1532 if (pool->solvables[p].repo == pool->installed)
1535 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1540 FOR_PROVIDES(p, pp, req)
1542 if (!MAPTST(ins, p))
1545 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1550 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));
1557 transaction_check(Transaction *trans)
1559 Pool *pool = trans->pool;
1561 Id p, type, lastins;
1565 printf("\nchecking transaction order...\n");
1566 map_init(&ins, pool->nsolvables);
1567 map_init(&seen, pool->nsolvables);
1568 if (pool->installed)
1569 FOR_REPO_SOLVABLES(pool->installed, p, s)
1572 for (i = 0; i < trans->steps.count; i += 2)
1574 type = trans->steps.elements[i];
1575 p = trans->steps.elements[i + 1];
1576 s = pool->solvables + p;
1577 if (s->repo != pool->installed)
1579 if (s->repo != pool->installed)
1581 if (havescripts(pool, p))
1584 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1586 if (s->repo == pool->installed)
1591 printf("transaction order check done.\n");
1595 transaction_add_choices(Transaction *trans, Queue *choices, Id chosen)
1598 struct _TransactionOrderdata *od = trans->orderdata;
1599 struct _TransactionElement *te;
1602 return choices->count;
1605 /* initialization step */
1606 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1608 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1610 for (j = te->edges; od->invedgedata[j]; j++)
1611 od->tes[od->invedgedata[j]].mark++;
1613 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1616 queue_push(choices, te->type);
1617 queue_push(choices, te->p);
1619 return choices->count;
1621 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1622 if (te->p == chosen)
1625 return choices->count;
1628 /* hey! out-of-order installation! */
1631 for (j = te->edges; od->invedgedata[j]; j++)
1633 te = od->tes + od->invedgedata[j];
1634 assert(te->mark > 0 || te->mark == -1);
1635 if (te->mark > 0 && --te->mark == 0)
1637 queue_push(choices, te->type);
1638 queue_push(choices, te->p);
1641 return choices->count;