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_REQ (1<<0)
399 #define TYPE_PREREQ (1<<1)
400 #define TYPE_CON (1<<2)
401 #define TYPE_ERASE (1<<3)
403 #define EDGEDATA_BLOCK 127
419 addedge(struct orderdata *od, Id from, Id to, int type)
421 Solver *solv = od->solv;
422 Pool *pool = solv->pool;
427 // printf("addedge %d %d type %d\n", from, to, type);
428 s = pool->solvables + from;
429 if (s->repo == solv->installed && solv->transaction_installed[from - solv->installed->start])
431 /* passive, map to active */
432 if (solv->transaction_installed[from - solv->installed->start] > 0)
433 from = solv->transaction_installed[from - solv->installed->start];
438 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
439 solver_transaction_all_pkgs(solv, from, &ti);
440 for (i = 0; i < ti.count; i++)
441 addedge(od, ti.elements[i], to, type);
446 s = pool->solvables + to;
447 if (s->repo == solv->installed && solv->transaction_installed[to - solv->installed->start])
449 /* passive, map to active */
450 if (solv->transaction_installed[to - solv->installed->start] > 0)
451 to = solv->transaction_installed[to - solv->installed->start];
456 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
457 solver_transaction_all_pkgs(solv, to, &ti);
458 for (i = 0; i < ti.count; i++)
459 addedge(od, from, ti.elements[i], type);
465 /* map target to te num */
466 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
473 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
480 return; /* no edges to ourselfes */
482 // printf("edge %d -> %d type %x\n", i, to, type);
484 for (i = te->edges; od->edgedata[i]; i += 2)
485 if (od->edgedata[i] == to)
489 od->edgedata[i + 1] |= type;
492 if (i + 1 == od->nedgedata)
494 // printf("tail add %d\n", i - te->edges);
497 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
501 // printf("extend %d\n", i - te->edges);
502 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
504 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
505 i = od->nedgedata + (i - te->edges);
506 te->edges = od->nedgedata;
508 od->edgedata[i] = to;
509 od->edgedata[i + 1] = type;
510 od->edgedata[i + 2] = 0;
511 od->nedgedata = i + 3;
515 solver_order_transaction(Solver *solv)
517 Pool *pool = solv->pool;
518 Queue *tr = &solv->transaction;
519 Repo *installed = solv->installed;
522 Id req, *reqp, con, *conp;
524 int i, j, pre, numte, numedge;
528 /* create a transaction element for every active component */
530 for (i = 0; i < tr->count; i += 2)
532 p = tr->elements[i + 1];
533 s = pool->solvables + p;
534 if (s->repo != installed || !solv->transaction_installed[p - solv->installed->start])
538 return; /* nothing to do... */
541 printf("numte = %d\n", numte);
542 numte++; /* leave first one zero */
545 od.tes = sat_calloc(numte, sizeof(*od.tes));
546 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
550 for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
552 p = tr->elements[i + 1];
553 s = pool->solvables + p;
554 if (s->repo == installed && solv->transaction_installed[p - solv->installed->start])
560 /* create dependency graph */
561 for (i = 0; i < tr->count; i += 2)
563 type = tr->elements[i];
564 p = tr->elements[i + 1];
565 s = pool->solvables + p;
568 reqp = s->repo->idarraydata + s->requires;
570 while ((req = *reqp++) != 0)
573 if (req == SOLVABLE_PREREQMARKER)
579 if (s->repo == installed && pre != TYPE_PREREQ)
582 FOR_PROVIDES(p2, pp2, req)
586 s2 = pool->solvables + p2;
589 if (s2->repo == installed && solv->decisionmap[p2] > 0)
591 if (s2->repo != installed && solv->decisionmap[p2] < 0)
592 continue; /* not interesting */
593 if (s->repo == installed)
595 /* we're uninstalling s */
596 if (s2->repo == installed)
601 if (s2->repo != installed)
603 /* update p2 before erasing p */
605 addedge(&od, p, p2, pre);
612 /* install p2 before installing p */
613 if (s2->repo != installed)
614 addedge(&od, p, p2, pre);
619 printf("eraseonlyedge for %s req %s\n", solvable2str(pool, s), dep2str(pool, req));
620 /* need edges to uninstalled pkgs */
622 FOR_PROVIDES(p2, pp2, req)
626 s2 = pool->solvables + p2;
627 if (!s2->repo || s2->repo != installed)
629 if (solv->decisionmap[p2] > 0)
632 addedge(&od, p2, p, pre);
634 addedge(&od, p2, p, TYPE_ERASE);
643 conp = s->repo->idarraydata + s->conflicts;
644 while ((con = *conp++) != 0)
647 FOR_PROVIDES(p2, pp2, con)
651 s2 = pool->solvables + p2;
654 if (s->repo == installed)
656 if (s2->repo != installed && solv->decisionmap[p2] >= 0)
658 /* deinstall p before installing p2 */
659 addedge(&od, p2, p, TYPE_CON);
664 if (s2->repo == installed && solv->decisionmap[p2] < 0)
666 /* deinstall p2 before installing p */
668 addedge(&od, p, p2, TYPE_CON);
679 for (i = 1, te = od.tes + i; i < numte; i++, te++)
681 printf("TE #%d, %d(%s)\n", i, te->p, solvid2str(pool, te->p));
682 for (j = te->edges; od.edgedata[j]; j += 2)
684 struct transel *te2 = od.tes + od.edgedata[j];
685 printf(" depends %x on TE %d, %d(%s)\n", od.edgedata[j + 1], od.edgedata[j], te2->p, solvid2str(pool, te2->p));
689 printf("TEs: %d, Edges: %d, Space: %d\n", numte - 1, numedge, od.nedgedata / 2);