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_init(Transaction *trans, Pool *pool)
316 memset(trans, 0, sizeof(*trans));
321 transaction_free(Transaction *trans)
323 queue_free(&trans->steps);
324 queue_free(&trans->transaction_info);
325 trans->transaction_installed = sat_free(trans->transaction_installed);
326 map_free(&trans->transactsmap);
327 trans->tes = sat_free(trans->tes);
329 trans->invedgedata = sat_free(trans->invedgedata);
333 transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
335 Pool *pool = trans->pool;
336 Repo *installed = pool->installed;
341 if (noobsmap && !noobsmap->size)
342 noobsmap = 0; /* ignore empty map */
343 queue_empty(&trans->steps);
344 map_init(&trans->transactsmap, pool->nsolvables);
345 for (i = 0; i < decisionq->count; i++)
347 p = decisionq->elements[i];
348 s = pool->solvables + (p > 0 ? p : -p);
351 if (installed && s->repo == installed && p < 0)
352 MAPSET(&trans->transactsmap, -p);
353 if ((!installed || s->repo != installed) && p > 0)
354 MAPSET(&trans->transactsmap, p);
356 create_transaction_info(trans, decisionq, noobsmap);
360 FOR_REPO_SOLVABLES(installed, p, s)
362 if (!MAPTST(&trans->transactsmap, p))
364 p2 = solver_transaction_pkg(trans, p);
366 queue_push(&trans->steps, SOLVER_TRANSACTION_ERASE);
369 s2 = pool->solvables + p2;
370 if (s->name == s2->name)
372 if (s->evr == s2->evr && solvable_identical(s, s2))
373 queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALLED);
376 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
378 queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADED);
380 queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADED);
382 queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGED);
386 queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACED);
388 queue_push(&trans->steps, p);
391 for (i = 0; i < decisionq->count; i++)
393 p = decisionq->elements[i];
394 if (p < 0 || p == SYSTEMSOLVABLE)
396 s = pool->solvables + p;
397 if (installed && s->repo == installed)
399 noobs = noobsmap && MAPTST(noobsmap, p);
400 p2 = solver_transaction_pkg(trans, p);
402 queue_push(&trans->steps, p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL);
404 queue_push(&trans->steps, SOLVER_TRANSACTION_INSTALL);
407 s2 = pool->solvables + p2;
408 if (s->name == s2->name)
410 if (s->evr == s2->evr && solvable_identical(s, s2))
411 queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALL);
414 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
416 queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADE);
418 queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADE);
420 queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGE);
424 queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACE);
426 queue_push(&trans->steps, p);
430 #define TYPE_BROKEN (1<<0)
431 #define TYPE_CON (1<<1)
433 #define TYPE_REQ_P (1<<2)
434 #define TYPE_PREREQ_P (1<<3)
436 #define TYPE_REQ (1<<4)
437 #define TYPE_PREREQ (1<<5)
439 #define TYPE_CYCLETAIL (1<<16)
440 #define TYPE_CYCLEHEAD (1<<17)
442 #define EDGEDATA_BLOCK 127
446 struct _TransactionElement *tes;
458 addteedge(struct orderdata *od, int from, int to, int type)
461 struct _TransactionElement *te;
466 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
469 for (i = te->edges; od->edgedata[i]; i += 2)
470 if (od->edgedata[i] == to)
472 /* test of brokenness */
473 if (type == TYPE_BROKEN)
474 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
477 od->edgedata[i + 1] |= type;
480 if (i + 1 == od->nedgedata)
482 /* printf("tail add %d\n", i - te->edges); */
485 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
489 /* printf("extend %d\n", i - te->edges); */
490 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
492 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
493 i = od->nedgedata + (i - te->edges);
494 te->edges = od->nedgedata;
496 od->edgedata[i] = to;
497 od->edgedata[i + 1] = type;
498 od->edgedata[i + 2] = 0; /* end marker */
499 od->nedgedata = i + 3;
504 addedge(struct orderdata *od, Id from, Id to, int type)
506 Transaction *trans = od->trans;
507 Pool *pool = trans->pool;
509 struct _TransactionElement *te;
512 // printf("addedge %d %d type %d\n", from, to, type);
513 s = pool->solvables + from;
514 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
516 /* obsolete, map to install */
517 if (trans->transaction_installed[from - pool->installed->start] > 0)
518 from = trans->transaction_installed[from - pool->installed->start];
525 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
526 solver_transaction_all_pkgs(trans, from, &ti);
527 for (i = 0; i < ti.count; i++)
528 ret |= addedge(od, ti.elements[i], to, type);
533 s = pool->solvables + to;
534 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
536 /* obsolete, map to install */
537 if (trans->transaction_installed[to - pool->installed->start] > 0)
538 to = trans->transaction_installed[to - pool->installed->start];
545 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
546 solver_transaction_all_pkgs(trans, to, &ti);
547 for (i = 0; i < ti.count; i++)
548 ret |= addedge(od, from, ti.elements[i], type);
554 /* map from/to to te numbers */
555 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
562 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
568 return addteedge(od, i, to, type);
573 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
575 Transaction *trans = od->trans;
576 Pool *pool = trans->pool;
577 Id ti1buf[5], ti2buf[5];
581 /* both q1 and q2 are uninstalls. check if their TEs intersect */
582 /* common case: just one TE for both packages */
584 printf("havechoice %d %d %d\n", p, q1, q2);
586 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
588 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
590 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
592 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
594 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
595 solver_transaction_all_pkgs(trans, q1, &ti1);
596 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
597 solver_transaction_all_pkgs(trans, q2, &ti2);
598 for (i = 0; i < ti1.count; i++)
599 for (j = 0; j < ti2.count; j++)
600 if (ti1.elements[i] == ti2.elements[j])
602 /* found a common edge */
614 havescripts(Pool *pool, Id solvid)
616 Solvable *s = pool->solvables + solvid;
622 reqp = s->repo->idarraydata + s->requires;
623 while ((req = *reqp++) != 0)
625 if (req == SOLVABLE_PREREQMARKER)
632 dep = id2str(pool, req);
633 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
641 addsolvableedges(struct orderdata *od, Solvable *s)
643 Transaction *trans = od->trans;
644 Pool *pool = trans->pool;
645 Id req, *reqp, con, *conp;
647 int i, j, pre, numins;
648 Repo *installed = pool->installed;
654 printf("addsolvableedges %s\n", solvable2str(pool, s));
656 p = s - pool->solvables;
660 reqp = s->repo->idarraydata + s->requires;
662 while ((req = *reqp++) != 0)
664 if (req == SOLVABLE_PREREQMARKER)
670 if (pre != TYPE_PREREQ && installed && s->repo == installed)
672 /* ignore normal requires if we're getting obsoleted */
673 if (trans->transaction_installed[p - pool->installed->start])
678 numins = 0; /* number of packages to be installed providing it */
679 provbyinst = 0; /* provided by kept package */
680 FOR_PROVIDES(p2, pp2, req)
682 s2 = pool->solvables + p2;
685 reqq.count = 0; /* self provides */
688 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
692 printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
693 reqq.count = 0; /* provided by package that stays installed */
699 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
700 continue; /* package stays uninstalled */
702 if (s->repo == installed)
704 /* s gets uninstalled */
705 queue_pushunique(&reqq, p2);
706 if (s2->repo != installed)
711 if (s2->repo == installed)
712 continue; /* s2 gets uninstalled */
713 queue_pushunique(&reqq, p2);
718 /* prune to harmless ->inst edges */
719 for (i = j = 0; i < reqq.count; i++)
720 if (pool->solvables[reqq.elements[i]].repo != installed)
721 reqq.elements[j++] = reqq.elements[i];
725 if (numins && reqq.count)
727 if (s->repo == installed)
729 for (i = 0; i < reqq.count; i++)
731 if (pool->solvables[reqq.elements[i]].repo == installed)
733 for (j = 0; j < reqq.count; j++)
735 if (pool->solvables[reqq.elements[j]].repo != installed)
737 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
738 continue; /* no self edge */
740 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
742 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
748 /* no mixed types, remove all deps on uninstalls */
749 for (i = j = 0; i < reqq.count; i++)
750 if (pool->solvables[reqq.elements[i]].repo != installed)
751 reqq.elements[j++] = reqq.elements[i];
756 for (i = 0; i < reqq.count; i++)
759 p2 = reqq.elements[i];
760 if (pool->solvables[p2].repo != installed)
762 /* all elements of reqq are installs, thus have different TEs */
763 choice = reqq.count - 1;
764 if (pool->solvables[p].repo != installed)
767 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
769 addedge(od, p, p2, pre);
774 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
776 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
781 if (s->repo != installed)
782 continue; /* no inst->uninst edges, please! */
784 /* uninst -> uninst edge. Those make trouble. Only add if we must */
785 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
787 /* p is obsoleted by another package and has no scripts */
788 /* we assume that the obsoletor is good enough to replace p */
793 for (j = 0; j < reqq.count; j++)
797 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
802 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
804 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
811 conp = s->repo->idarraydata + s->conflicts;
812 while ((con = *conp++) != 0)
814 FOR_PROVIDES(p2, pp2, con)
818 s2 = pool->solvables + p2;
821 if (s->repo == installed)
823 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
825 /* deinstall p before installing p2 */
826 addedge(od, p2, p, TYPE_CON);
831 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
833 /* deinstall p2 before installing p */
834 addedge(od, p, p2, TYPE_CON);
845 /* break an edge in a cycle */
847 breakcycle(struct orderdata *od, Id *cycle)
849 Pool *pool = od->trans->pool;
850 Id ddegmin, ddegmax, ddeg;
852 struct _TransactionElement *te;
855 ddegmin = ddegmax = 0;
856 for (k = 0; cycle[k + 1]; k += 2)
858 ddeg = od->edgedata[cycle[k + 1] + 1];
861 if (!k || ddeg < ddegmin)
869 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
871 /* prefer k, as l comes from a package with contains scriptlets */
876 /* same edge value, check for prereq */
880 /* record brkoen cycle starting with the tail */
881 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
882 queue_push(&od->cycles, k / 2); /* cycle elements */
883 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
890 queue_push(&od->cyclesdata, cycle[k]);
894 queue_push(&od->cyclesdata, 0); /* mark end */
896 /* break that edge */
897 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
900 if (ddegmin < TYPE_REQ)
904 /* cycle recorded, print it */
905 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
907 printf("cycle: --> ");
908 for (k = 0; cycle[k + 1]; k += 2)
910 te = od->tes + cycle[k];
911 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
912 printf("%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
914 printf("%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
920 dump_tes(struct orderdata *od)
922 Pool *pool = od->trans->pool;
925 struct _TransactionElement *te, *te2;
928 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
930 Solvable *s = pool->solvables + te->p;
931 printf("TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
932 if (s->repo != pool->installed)
935 solver_transaction_all_pkgs(od->trans, te->p, &obsq);
936 for (j = 0; j < obsq.count; j++)
937 printf(" -%s\n", solvid2str(pool, obsq.elements[j]));
939 for (j = te->edges; od->edgedata[j]; j += 2)
941 te2 = od->tes + od->edgedata[j];
942 printf(" --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
949 reachable(struct orderdata *od, Id i)
951 struct _TransactionElement *te = od->tes + i;
957 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
959 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
961 if (!od->tes[k].mark)
963 if (od->tes[k].mark == 2)
974 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
977 Transaction *trans = od->trans;
978 Pool *pool = trans->pool;
980 struct _TransactionElement *te;
987 printf("addcycleedges\n");
988 for (i = 0; (j = cycle[i]) != 0; i++)
989 printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
992 /* first add all the tail cycle edges */
994 /* see what we can reach from the cycle */
996 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
998 for (i = 0; (j = cycle[i]) != 0; i++)
1000 od->tes[j].mark = -1;
1001 queue_push(todo, j);
1005 i = queue_pop(todo);
1009 te->mark = te->mark < 0 ? 2 : 1;
1010 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1012 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1014 if (od->tes[k].mark > 0)
1015 continue; /* no need to visit again */
1016 queue_push(todo, k);
1019 /* now all cycle TEs are marked with 2, all TEs reachable
1020 * from the cycle are marked with 1 */
1022 od->tes[tail].mark = 1; /* no need to add edges */
1024 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1027 continue; /* reachable from cycle */
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 != 2)
1034 /* We found an edge to the cycle. Add an extra edge to the tail */
1035 /* the TE was not reachable, so we're not creating a new cycle! */
1037 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));
1039 j -= te->edges; /* in case we move */
1040 addteedge(od, i, tail, TYPE_CYCLETAIL);
1042 break; /* one edge is enough */
1047 /* now add all head cycle edges */
1050 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1053 for (i = 0; (j = cycle[i]) != 0; i++)
1056 od->tes[j].mark = 2;
1058 /* first the head to save some time */
1059 te = od->tes + head;
1060 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1062 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1064 if (!od->tes[k].mark)
1066 if (od->tes[k].mark == -1)
1067 od->tes[k].mark = -2; /* no need for another edge */
1069 for (i = 0; cycle[i] != 0; i++)
1071 if (cycle[i] == head)
1073 te = od->tes + cycle[i];
1074 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1076 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1078 /* see if we can reach a cycle TE from k */
1079 if (!od->tes[k].mark)
1081 if (od->tes[k].mark == -1)
1084 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));
1086 addteedge(od, head, k, TYPE_CYCLEHEAD);
1087 od->tes[k].mark = -2; /* no need to add that one again */
1095 transaction_order(Transaction *trans)
1097 Pool *pool = trans->pool;
1098 Queue *tr = &trans->steps;
1099 Repo *installed = pool->installed;
1102 int i, j, k, numte, numedge;
1103 struct orderdata od;
1104 struct _TransactionElement *te;
1106 int cycstart, cycel;
1107 Id *cycle, *obstypes;
1111 POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1112 start = now = sat_timems(0);
1113 /* create a transaction element for every active component */
1115 for (i = 0; i < tr->count; i += 2)
1117 p = tr->elements[i + 1];
1118 s = pool->solvables + p;
1119 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1124 return; /* nothing to do... */
1126 POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1127 numte++; /* leave first one zero */
1128 memset(&od, 0, sizeof(od));
1131 od.tes = sat_calloc(numte, sizeof(*od.tes));
1132 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1135 queue_init(&od.cycles);
1137 for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
1139 p = tr->elements[i + 1];
1140 s = pool->solvables + p;
1141 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1144 te->type = tr->elements[i];
1148 /* create dependency graph */
1149 for (i = 0; i < tr->count; i += 2)
1151 type = tr->elements[i];
1152 p = tr->elements[i + 1];
1153 addsolvableedges(&od, pool->solvables + p);
1158 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1159 for (j = te->edges; od.edgedata[j]; j += 2)
1161 POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1162 POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1168 now = sat_timems(0);
1169 /* kill all cycles */
1171 for (i = numte - 1; i > 0; i--)
1172 queue_push(&todo, i);
1176 i = queue_pop(&todo);
1177 /* printf("- look at TE %d\n", i); */
1181 od.tes[i].mark = 2; /* done with that one */
1186 continue; /* already finished before */
1189 int edgestovisit = 0;
1190 /* new node, visit edges */
1191 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1193 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1195 if (od.tes[k].mark == 2)
1196 continue; /* no need to visit again */
1197 if (!edgestovisit++)
1198 queue_push(&todo, -i); /* end of edges marker */
1199 queue_push(&todo, k);
1202 te->mark = 2; /* no edges, done with that one */
1204 te->mark = 1; /* under investigation */
1207 /* oh no, we found a cycle */
1208 /* find start of cycle node (<0) */
1209 for (j = todo.count - 1; j >= 0; j--)
1210 if (todo.elements[j] == -i)
1214 /* build te/edge chain */
1216 for (j = k; j < todo.count; j++)
1217 if (todo.elements[j] < 0)
1218 todo.elements[k++] = -todo.elements[j];
1219 cycel = k - cycstart;
1221 /* make room for edges, two extra element for cycle loop + terminating 0 */
1222 while (todo.count < cycstart + 2 * cycel + 2)
1223 queue_push(&todo, 0);
1224 cycle = todo.elements + cycstart;
1225 cycle[cycel] = i; /* close the loop */
1226 cycle[2 * cycel + 1] = 0; /* terminator */
1227 for (k = cycel; k > 0; k--)
1229 cycle[k * 2] = cycle[k];
1230 te = od.tes + cycle[k - 1];
1231 assert(te->mark == 1);
1232 te->mark = 0; /* reset investigation marker */
1233 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1234 for (j = te->edges; od.edgedata[j]; j += 2)
1235 if (od.edgedata[j] == cycle[k])
1237 assert(od.edgedata[j]);
1238 cycle[k * 2 - 1] = j;
1240 /* now cycle looks like this: */
1241 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1242 breakcycle(&od, cycle);
1243 /* restart with start of cycle */
1244 todo.count = cycstart + 1;
1246 POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1247 POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1249 now = sat_timems(0);
1250 /* now go through all broken cycles and create cycle edges to help
1252 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1254 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1255 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1257 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1259 if (od.cycles.elements[i + 2] < TYPE_REQ)
1260 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1262 POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1264 /* all edges are finally set up and there are no cycles, now the easy part.
1265 * Create an ordered transaction */
1266 now = sat_timems(0);
1267 /* first invert all edges */
1268 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1269 te->mark = 1; /* term 0 */
1270 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1272 for (j = te->edges; od.edgedata[j]; j += 2)
1274 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1276 od.tes[od.edgedata[j]].mark++;
1280 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1285 POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1286 od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1287 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1289 for (j = te->edges; od.edgedata[j]; j += 2)
1291 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1293 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1296 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1297 te->edges = te->mark; /* edges now points into invedgedata */
1298 od.edgedata = sat_free(od.edgedata);
1300 /* now the final ordering */
1301 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1303 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1304 for (j = te->edges; od.invedgedata[j]; j++)
1305 od.tes[od.invedgedata[j]].mark++;
1306 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1308 queue_push(&todo, i);
1309 assert(todo.count > 0);
1312 obstypes = sat_calloc(installed->end - installed->start, sizeof(Id));
1313 for (i = 0; i < tr->count; i += 2)
1315 p = tr->elements[i + 1];
1316 s = pool->solvables + p;
1317 if (s->repo == installed && trans->transaction_installed[p - installed->start])
1318 obstypes[p - installed->start] = tr->elements[i];
1323 oldcount = tr->count;
1333 for (i = 0; i < todo.count; i++)
1335 j = todo.elements[i];
1336 if (pool->solvables[od.tes[j].p].repo == installed)
1340 if (i == todo.count)
1342 te = od.tes + todo.elements[i];
1343 queue_push(tr, te->type);
1344 queue_push(tr, te->p);
1346 printf("do %s\n", solvid2str(pool, te->p));
1348 s = pool->solvables + te->p;
1349 if (installed && s->repo != installed)
1352 solver_transaction_all_pkgs(trans, te->p, &obsq);
1353 for (j = 0; j < obsq.count; j++)
1355 p = obsq.elements[j];
1356 assert(p >= installed->start && p < installed->end);
1357 if (obstypes[p - installed->start])
1359 queue_push(tr, obstypes[p - installed->start]);
1361 obstypes[p - installed->start] = 0;
1365 queue_delete(&todo, i);
1366 for (j = te->edges; od.invedgedata[j]; j++)
1368 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1369 assert(te2->mark > 0);
1370 if (--te2->mark == 0)
1372 queue_push(&todo, od.invedgedata[j]);
1374 printf("free %s\n", solvid2str(pool, od.tes[od.invedgedata[j]].p));
1382 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1383 assert(te->mark == 0);
1384 assert(tr->count == oldcount);
1385 POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1386 POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1388 trans->tes = od.tes;
1389 trans->ntes = numte;
1390 trans->invedgedata = od.invedgedata;
1394 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1396 Pool *pool = trans->pool;
1401 if (MAPTST(seen, pkg))
1404 s = pool->solvables + pkg;
1406 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1412 reqp = s->repo->idarraydata + s->requires;
1413 while ((req = *reqp++) != 0)
1415 if (req == SOLVABLE_PREREQMARKER)
1420 if (onlyprereq && !inpre)
1422 if (!strncmp(id2str(pool, req), "rpmlib(", 7))
1425 /* first check kept packages, then freshly installed, then not yet uninstalled */
1426 FOR_PROVIDES(p, pp, req)
1428 if (!MAPTST(ins, p))
1430 if (MAPTST(&trans->transactsmap, p))
1433 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1437 FOR_PROVIDES(p, pp, req)
1439 if (!MAPTST(ins, p))
1441 if (pool->solvables[p].repo == pool->installed)
1444 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1449 FOR_PROVIDES(p, pp, req)
1451 if (!MAPTST(ins, p))
1454 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1459 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));
1466 transaction_check(Transaction *trans)
1468 Pool *pool = trans->pool;
1470 Id p, type, lastins;
1474 printf("\nchecking transaction order...\n");
1475 map_init(&ins, pool->nsolvables);
1476 map_init(&seen, pool->nsolvables);
1477 if (pool->installed)
1478 FOR_REPO_SOLVABLES(pool->installed, p, s)
1481 for (i = 0; i < trans->steps.count; i += 2)
1483 type = trans->steps.elements[i];
1484 p = trans->steps.elements[i + 1];
1485 s = pool->solvables + p;
1486 if (s->repo != pool->installed)
1488 if (s->repo != pool->installed)
1490 if (havescripts(pool, p))
1493 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1495 if (s->repo == pool->installed)
1500 printf("transaction order check done.\n");
1504 transaction_add_choices(Transaction *trans, Queue *choices, Id chosen)
1507 struct _TransactionElement *te;
1511 /* initialization step */
1512 for (i = 1, te = trans->tes + i; i < trans->ntes; i++, te++)
1514 for (i = 1, te = trans->tes + i; i < trans->ntes; i++, te++)
1516 for (j = te->edges; trans->invedgedata[j]; j++)
1517 trans->tes[trans->invedgedata[j]].mark++;
1519 for (i = 1, te = trans->tes + i; i < trans->ntes; i++, te++)
1522 queue_push(choices, te->type);
1523 queue_push(choices, te->p);
1525 return choices->count;
1527 for (i = 1, te = trans->tes + i; i < trans->ntes; i++, te++)
1528 if (te->p == chosen)
1530 if (i == trans->ntes)
1531 return choices->count;
1534 /* hey! out-of-order installation! */
1537 for (j = te->edges; trans->invedgedata[j]; j++)
1539 te = trans->tes + trans->invedgedata[j];
1540 assert(te->mark > 0 || te->mark == -1);
1541 if (te->mark > 0 && --te->mark == 0)
1543 queue_push(choices, te->type);
1544 queue_push(choices, te->p);
1547 return choices->count;