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(Solver *solv, Id p, Queue *pkgs)
63 Pool *pool = solv->pool;
64 Solvable *s = pool->solvables + p;
65 Queue *ti = &solv->transaction_info;
70 if (p <= 0 || !s->repo)
72 if (s->repo == solv->installed)
74 q = solv->transaction_installed[p - solv->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(Solver *solv, Id p)
112 Pool *pool = solv->pool;
113 Solvable *s = pool->solvables + p;
117 if (p <= 0 || !s->repo)
119 if (s->repo == solv->installed)
121 p = solv->transaction_installed[p - solv->installed->start];
122 return p < 0 ? -p : p;
124 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
125 solver_transaction_all_pkgs(solv, 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_filter(Solver *solv, Id type, Id p, int flags)
138 Pool *pool = solv->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(solv, 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(solv, q) == p)
179 /* too bad, a miss. check em all */
182 solver_transaction_all_pkgs(solv, 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(solv, q, &rq);
196 for (j = 0; j < rq.count; j++)
197 if (rq.elements[j] == p)
205 else if (solver_transaction_pkg(solv, 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(Solver *solv)
229 Pool *pool = solv->pool;
230 Queue *ti = &solv->transaction_info;
231 Repo *installed = solv->installed;
238 return; /* no info needed */
239 for (i = 0; i < solv->decisionq.count; i++)
241 p = solv->decisionq.elements[i];
242 if (p <= 0 || p == SYSTEMSOLVABLE)
244 s = pool->solvables + p;
245 if (s->repo == installed)
247 noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p);
248 FOR_PROVIDES(p2, pp2, s->name)
250 if (solv->decisionmap[p2] > 0)
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 (!solv->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 (!solv->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 solv->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 (!solv->transaction_installed[j])
297 solv->transaction_installed[j] = ti->elements[i];
300 /* more than one package obsoletes us. compare */
302 if (solv->transaction_installed[j] > 0)
303 solv->transaction_installed[j] = -solv->transaction_installed[j];
304 q[0] = q[2] = ti->elements[i + 1];
305 q[1] = ti->elements[i];
306 q[3] = -solv->transaction_installed[j];
307 if (obsq_sortcmp(q, q + 2, pool) < 0)
308 solv->transaction_installed[j] = -ti->elements[i];
315 solver_create_transaction(Solver *solv)
317 Pool *pool = solv->pool;
318 Repo *installed = solv->installed;
323 queue_empty(&solv->transaction);
324 create_transaction_info(solv);
328 FOR_REPO_SOLVABLES(installed, p, s)
330 if (solv->decisionmap[p] > 0)
332 p2 = solver_transaction_pkg(solv, p);
334 queue_push(&solv->transaction, SOLVER_TRANSACTION_ERASE);
337 s2 = pool->solvables + p2;
338 if (s->name == s2->name)
340 if (s->evr == s2->evr && solvable_identical(s, s2))
341 queue_push(&solv->transaction, SOLVER_TRANSACTION_REINSTALLED);
344 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
346 queue_push(&solv->transaction, SOLVER_TRANSACTION_UPGRADED);
348 queue_push(&solv->transaction, SOLVER_TRANSACTION_DOWNGRADED);
350 queue_push(&solv->transaction, SOLVER_TRANSACTION_CHANGED);
354 queue_push(&solv->transaction, SOLVER_TRANSACTION_REPLACED);
356 queue_push(&solv->transaction, p);
359 for (i = 0; i < solv->decisionq.count; i++)
361 p = solv->decisionq.elements[i];
362 if (p < 0 || p == SYSTEMSOLVABLE)
364 s = pool->solvables + p;
365 if (solv->installed && s->repo == solv->installed)
367 noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p);
368 p2 = solver_transaction_pkg(solv, p);
370 queue_push(&solv->transaction, p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL);
372 queue_push(&solv->transaction, SOLVER_TRANSACTION_INSTALL);
375 s2 = pool->solvables + p2;
376 if (s->name == s2->name)
378 if (s->evr == s2->evr && solvable_identical(s, s2))
379 queue_push(&solv->transaction, SOLVER_TRANSACTION_REINSTALL);
382 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
384 queue_push(&solv->transaction, SOLVER_TRANSACTION_UPGRADE);
386 queue_push(&solv->transaction, SOLVER_TRANSACTION_DOWNGRADE);
388 queue_push(&solv->transaction, SOLVER_TRANSACTION_CHANGE);
392 queue_push(&solv->transaction, SOLVER_TRANSACTION_REPLACE);
394 queue_push(&solv->transaction, p);
398 #define TYPE_BROKEN (1<<0)
399 #define TYPE_REQ (1<<1)
400 #define TYPE_PREREQ (1<<2)
401 #define TYPE_CON (1<<3)
402 #define TYPE_ERASE (1<<4)
404 #define EDGEDATA_BLOCK 127
422 addedge(struct orderdata *od, Id from, Id to, int type)
424 Solver *solv = od->solv;
425 Pool *pool = solv->pool;
430 // printf("addedge %d %d type %d\n", from, to, type);
431 s = pool->solvables + from;
432 if (s->repo == solv->installed && solv->transaction_installed[from - solv->installed->start])
434 /* passive, map to active */
435 if (solv->transaction_installed[from - solv->installed->start] > 0)
436 from = solv->transaction_installed[from - solv->installed->start];
441 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
442 solver_transaction_all_pkgs(solv, from, &ti);
443 for (i = 0; i < ti.count; i++)
444 addedge(od, ti.elements[i], to, type);
449 s = pool->solvables + to;
450 if (s->repo == solv->installed && solv->transaction_installed[to - solv->installed->start])
452 /* passive, map to active */
453 if (solv->transaction_installed[to - solv->installed->start] > 0)
454 to = solv->transaction_installed[to - solv->installed->start];
459 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
460 solver_transaction_all_pkgs(solv, to, &ti);
461 for (i = 0; i < ti.count; i++)
462 addedge(od, from, ti.elements[i], type);
468 /* map target to te num */
469 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
476 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
483 return; /* no edges to ourselfes */
485 // printf("edge %d -> %d type %x\n", i, to, type);
487 for (i = te->edges; od->edgedata[i]; i += 2)
488 if (od->edgedata[i] == to)
492 od->edgedata[i + 1] |= type;
495 if (i + 1 == od->nedgedata)
497 // printf("tail add %d\n", i - te->edges);
500 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
504 // printf("extend %d\n", i - te->edges);
505 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
507 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
508 i = od->nedgedata + (i - te->edges);
509 te->edges = od->nedgedata;
511 od->edgedata[i] = to;
512 od->edgedata[i + 1] = type;
513 od->edgedata[i + 2] = 0;
514 od->nedgedata = i + 3;
520 addsolvableedges(struct orderdata *od, Solvable *s)
522 Solver *solv = od->solv;
523 Pool *pool = solv->pool;
524 Id req, *reqp, con, *conp;
527 Repo *installed = solv->installed;
530 p = s - pool->solvables;
533 reqp = s->repo->idarraydata + s->requires;
535 while ((req = *reqp++) != 0)
538 if (req == SOLVABLE_PREREQMARKER)
544 if (s->repo == installed && pre != TYPE_PREREQ)
547 FOR_PROVIDES(p2, pp2, req)
551 s2 = pool->solvables + p2;
554 if (s2->repo == installed && solv->decisionmap[p2] > 0)
556 if (s2->repo != installed && solv->decisionmap[p2] < 0)
557 continue; /* not interesting */
558 if (s->repo == installed)
560 /* we're uninstalling s */
561 if (s2->repo == installed)
566 if (s2->repo != installed)
568 /* update p2 before erasing p */
570 addedge(od, p, p2, pre);
577 /* install p2 before installing p */
578 if (s2->repo != installed)
579 addedge(od, p, p2, pre);
584 printf("eraseonlyedge for %s req %s\n", solvable2str(pool, s), dep2str(pool, req));
585 /* need edges to uninstalled pkgs */
587 FOR_PROVIDES(p2, pp2, req)
591 s2 = pool->solvables + p2;
592 if (!s2->repo || s2->repo != installed)
594 if (solv->decisionmap[p2] > 0)
597 addedge(od, p2, p, pre);
599 addedge(od, p2, p, TYPE_ERASE);
608 conp = s->repo->idarraydata + s->conflicts;
609 while ((con = *conp++) != 0)
612 FOR_PROVIDES(p2, pp2, con)
616 s2 = pool->solvables + p2;
619 if (s->repo == installed)
621 if (s2->repo != installed && solv->decisionmap[p2] >= 0)
623 /* deinstall p before installing p2 */
624 addedge(od, p2, p, TYPE_CON);
629 if (s2->repo == installed && solv->decisionmap[p2] < 0)
631 /* deinstall p2 before installing p */
633 addedge(od, p, p2, TYPE_CON);
645 breakcycle(struct orderdata *od, Id *cycle)
647 Pool *pool = od->solv->pool;
654 for (k = 0; cycle[k + 1]; k += 2)
656 ddeg = od->tes[cycle[k]].ddeg - od->tes[cycle[k + 2]].ddeg;
657 if (!k || ddeg < ddegm)
667 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
669 /* cycle recorded, print it */
670 printf("cycle: --> ");
671 for (k = 0; cycle[k + 1]; k += 2)
673 te = od->tes + cycle[k];
674 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
675 printf("%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
677 printf("%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
683 solver_order_transaction(Solver *solv)
685 Pool *pool = solv->pool;
686 Queue *tr = &solv->transaction;
687 Repo *installed = solv->installed;
690 int i, j, k, numte, numedge;
694 int cycstart, cycel, broken;
697 /* create a transaction element for every active component */
699 for (i = 0; i < tr->count; i += 2)
701 p = tr->elements[i + 1];
702 s = pool->solvables + p;
703 if (s->repo != installed || !solv->transaction_installed[p - solv->installed->start])
707 return; /* nothing to do... */
710 printf("numte = %d\n", numte);
711 numte++; /* leave first one zero */
714 od.tes = sat_calloc(numte, sizeof(*od.tes));
715 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
719 for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
721 p = tr->elements[i + 1];
722 s = pool->solvables + p;
723 if (s->repo == installed && solv->transaction_installed[p - solv->installed->start])
729 /* create dependency graph */
730 for (i = 0; i < tr->count; i += 2)
732 type = tr->elements[i];
733 p = tr->elements[i + 1];
734 addsolvableedges(&od, pool->solvables + p);
737 /* kill all cycles */
741 for (i = numte - 1; i > 0; i--)
742 queue_push(&todo, i);
746 i = queue_pop(&todo);
747 /* printf("- look at TE %d\n", i); */
751 od.tes[i].mark = 2; /* done with that one */
756 continue; /* already finished before */
759 int edgestovisit = 0;
760 /* new node, visit edges */
761 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
763 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
765 if (od.tes[k].mark == 2)
766 continue; /* no need to visit again */
768 queue_push(&todo, -i); /* end of edges marker */
769 queue_push(&todo, k);
772 te->mark = 2; /* no edges, done with that one */
774 te->mark = 1; /* under investigation */
777 /* oh no, we found a cycle */
778 /* find start of cycle node (<0) */
779 for (j = todo.count - 1; j >= 0; j--)
780 if (todo.elements[j] == -i)
784 /* build te/edge chain */
786 for (j = k; j < todo.count; j++)
787 if (todo.elements[j] < 0)
788 todo.elements[k++] = -todo.elements[j];
789 cycel = k - cycstart;
791 /* make room for edges, two extra element for cycle loop + terminating 0 */
792 while (todo.count < cycstart + 2 * cycel + 2)
793 queue_push(&todo, 0);
794 cycle = todo.elements + cycstart;
795 cycle[cycel] = i; /* close the loop */
796 cycle[2 * cycel + 1] = 0; /* terminator */
797 for (k = cycel; k > 0; k--)
799 cycle[k * 2] = cycle[k];
800 te = od.tes + cycle[k - 1];
802 te->mark = 0; /* reset investigation marker */
803 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
804 for (j = te->edges; od.edgedata[j]; j += 2)
805 if (od.edgedata[j] == cycle[k])
807 assert(od.edgedata[j]);
808 cycle[k * 2 - 1] = j;
810 /* now cycle looks like this: */
811 /* te1 edge te2 edge te3 ... teN edge te1 0 */
812 breakcycle(&od, cycle);
814 /* restart with start of cycle */
815 todo.count = cycstart + 1;
817 printf("Broken: %d\n", broken);
820 for (i = 1, te = od.tes + i; i < numte; i++, te++)
823 printf("TE #%d, %d(%s)\n", i, te->p, solvid2str(pool, te->p));
825 for (j = te->edges; od.edgedata[j]; j += 2)
828 struct transel *te2 = od.tes + od.edgedata[j];
829 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
831 printf(" depends %x on TE %d, %d(%s)\n", od.edgedata[j + 1], od.edgedata[j], te2->p, solvid2str(pool, te2->p));
836 printf("TEs: %d, Edges: %d, Space: %d\n", numte - 1, numedge, od.nedgedata / 2);