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"
29 obsq_sortcmp(const void *ap, const void *bp, void *dp)
33 Solvable *s, *oas, *obs;
44 s = pool->solvables + a;
45 oas = pool->solvables + oa;
46 obs = pool->solvables + ob;
47 if (oas->name != obs->name)
49 if (oas->name == s->name)
51 if (obs->name == s->name)
53 return strcmp(id2str(pool, oas->name), id2str(pool, obs->name));
55 r = evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
57 return -r; /* highest version first */
62 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
64 Pool *pool = trans->pool;
65 Solvable *s = pool->solvables + p;
66 Queue *ti = &trans->transaction_info;
71 if (p <= 0 || !s->repo)
73 if (s->repo == pool->installed)
75 q = trans->transaction_installed[p - pool->installed->start];
83 /* find which packages obsolete us */
84 for (i = 0; i < ti->count; i += 2)
85 if (ti->elements[i + 1] == p)
88 queue_push(pkgs, ti->elements[i]);
92 sat_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
93 for (i = 0; i < pkgs->count; i += 2)
94 pkgs->elements[i / 2] = pkgs->elements[i + 1];
99 /* find the packages we obsolete */
100 for (i = 0; i < ti->count; i += 2)
102 if (ti->elements[i] == p)
103 queue_push(pkgs, ti->elements[i + 1]);
104 else if (pkgs->count)
111 transaction_obs_pkg(Transaction *trans, Id p)
113 Pool *pool = trans->pool;
114 Solvable *s = pool->solvables + p;
118 if (p <= 0 || !s->repo)
120 if (s->repo == pool->installed)
122 p = trans->transaction_installed[p - pool->installed->start];
123 return p < 0 ? -p : p;
125 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
126 transaction_all_obs_pkgs(trans, p, &ti);
127 p = ti.count ? ti.elements[0] : 0;
134 * calculate base type of transaction element
138 transaction_base_type(Transaction *trans, Id p)
140 Pool *pool = trans->pool;
145 if (!MAPTST(&trans->transactsmap, p))
146 return SOLVER_TRANSACTION_IGNORE;
147 p2 = transaction_obs_pkg(trans, p);
148 if (pool->installed && pool->solvables[p].repo == pool->installed)
152 return SOLVER_TRANSACTION_ERASE;
153 s = pool->solvables + p;
154 s2 = pool->solvables + p2;
155 if (s->name == s2->name)
157 if (s->evr == s2->evr && solvable_identical(s, s2))
158 return SOLVER_TRANSACTION_REINSTALLED;
159 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
161 return SOLVER_TRANSACTION_UPGRADED;
163 return SOLVER_TRANSACTION_DOWNGRADED;
164 return SOLVER_TRANSACTION_CHANGED;
166 return SOLVER_TRANSACTION_OBSOLETED;
170 int noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
172 return p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL;
174 return SOLVER_TRANSACTION_INSTALL;
175 s = pool->solvables + p;
176 s2 = pool->solvables + p2;
177 if (s->name == s2->name)
179 if (s->evr == s2->evr && solvable_identical(s, s2))
180 return SOLVER_TRANSACTION_REINSTALL;
181 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
183 return SOLVER_TRANSACTION_UPGRADE;
185 return SOLVER_TRANSACTION_DOWNGRADE;
187 return SOLVER_TRANSACTION_CHANGE;
189 return SOLVER_TRANSACTION_OBSOLETES;
194 * return type of transaction element
196 * filtering is needed if either not all packages are shown
197 * or replaces are not shown, as otherwise parts of the
198 * transaction might not be shown to the user */
201 transaction_type(Transaction *trans, Id p, int mode)
203 Pool *pool = trans->pool;
204 Solvable *s = pool->solvables + p;
211 return SOLVER_TRANSACTION_IGNORE;
213 n = id2str(pool, s->name);
214 if (!strncmp(n, "patch:", 6))
215 return SOLVER_TRANSACTION_IGNORE;
216 if (!strncmp(n, "pattern:", 8))
217 return SOLVER_TRANSACTION_IGNORE;
219 type = transaction_base_type(trans, p);
221 if (type == SOLVER_TRANSACTION_IGNORE)
222 return SOLVER_TRANSACTION_IGNORE; /* not part of the transaction */
224 if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
226 /* application wants to know what to feed to rpm */
227 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
229 if (s->repo == pool->installed)
230 return SOLVER_TRANSACTION_IGNORE; /* ignore as we're being obsoleted */
231 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
232 return SOLVER_TRANSACTION_MULTIINSTALL;
233 return SOLVER_TRANSACTION_INSTALL;
236 if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
238 /* application wants to make no difference between install
239 * and multiinstall */
240 if (type == SOLVER_TRANSACTION_MULTIINSTALL)
241 type = SOLVER_TRANSACTION_INSTALL;
242 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
243 type = SOLVER_TRANSACTION_REINSTALL;
246 if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL))
248 /* application wants to make no difference between change
250 if (type == SOLVER_TRANSACTION_CHANGED)
251 type = SOLVER_TRANSACTION_REINSTALLED;
252 else if (type == SOLVER_TRANSACTION_CHANGE)
253 type = SOLVER_TRANSACTION_REINSTALL;
256 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
259 if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
261 /* erase element and we're showing the passive side */
262 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETED)
263 type = SOLVER_TRANSACTION_ERASE;
266 if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
268 /* install element and we're showing the active side */
269 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETES)
270 type = SOLVER_TRANSACTION_INSTALL;
274 /* the element doesn't match the show mode */
276 /* if we're showing all references, we can ignore this package */
277 if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
278 return SOLVER_TRANSACTION_IGNORE;
280 /* we're not showing all refs. check if some other package
281 * references us. If yes, it's safe to ignore this package,
282 * otherwise we need to map the type */
284 /* most of the time there's only one reference, so check it first */
285 q = transaction_obs_pkg(trans, p);
287 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
289 Solvable *sq = pool->solvables + q;
290 if (sq->name != s->name)
292 /* it's a replace but we're not showing replaces. map type. */
293 if (s->repo == pool->installed)
294 return SOLVER_TRANSACTION_ERASE;
295 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
296 return SOLVER_TRANSACTION_MULTIINSTALL;
298 return SOLVER_TRANSACTION_INSTALL;
302 /* if there's a match, p will be shown when q
304 if (transaction_obs_pkg(trans, q) == p)
305 return SOLVER_TRANSACTION_IGNORE;
307 /* too bad, a miss. check em all */
310 transaction_all_obs_pkgs(trans, p, &oq);
311 for (i = 0; i < oq.count; i++)
314 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
316 Solvable *sq = pool->solvables + q;
317 if (sq->name != s->name)
320 /* check if we are referenced? */
321 if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
323 transaction_all_obs_pkgs(trans, q, &rq);
324 for (j = 0; j < rq.count; j++)
325 if (rq.elements[j] == p)
333 else if (transaction_obs_pkg(trans, q) == p)
344 /* we're not referenced. map type */
345 if (s->repo == pool->installed)
346 return SOLVER_TRANSACTION_ERASE;
347 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
348 return SOLVER_TRANSACTION_MULTIINSTALL;
350 return SOLVER_TRANSACTION_INSTALL;
352 /* there was a ref, so p is shown with some other package */
353 return SOLVER_TRANSACTION_IGNORE;
359 classify_cmp(const void *ap, const void *bp, void *dp)
361 Transaction *trans = dp;
362 Pool *pool = trans->pool;
372 return a[2] && b[2] ? strcmp(id2str(pool, a[2]), id2str(pool, b[2])) : r;
375 return a[3] && b[3] ? strcmp(id2str(pool, a[3]), id2str(pool, b[3])) : r;
380 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
382 Transaction *trans = dp;
383 Pool *pool = trans->pool;
388 sa = pool->solvables + a;
389 sb = pool->solvables + b;
390 if (sa->name != sb->name)
391 return strcmp(id2str(pool, sa->name), id2str(pool, sb->name));
392 if (sa->evr != sb->evr)
394 int r = evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
402 transaction_classify(Transaction *trans, int mode, Queue *classes)
404 Pool *pool = trans->pool;
405 int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
407 Id v, vq, type, p, q;
410 queue_empty(classes);
411 memset(ntypes, 0, sizeof(ntypes));
412 /* go through transaction and classify each step */
413 for (i = 0; i < trans->steps.count; i++)
415 p = trans->steps.elements[i];
416 s = pool->solvables + p;
417 type = transaction_type(trans, p, mode);
419 if (!pool->installed || s->repo != pool->installed)
421 /* look at arch/vendor changes */
422 q = transaction_obs_pkg(trans, p);
425 sq = pool->solvables + q;
431 if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
433 for (j = 0; j < classes->count; j += 4)
434 if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
436 if (j == classes->count)
438 queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
439 queue_push(classes, 1);
440 queue_push(classes, v);
441 queue_push(classes, vq);
444 classes->elements[j + 1]++;
447 v = s->vendor ? s->vendor : 1;
448 vq = sq->vendor ? sq->vendor : 1;
451 if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
453 for (j = 0; j < classes->count; j += 4)
454 if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
456 if (j == classes->count)
458 queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
459 queue_push(classes, 1);
460 queue_push(classes, v);
461 queue_push(classes, vq);
464 classes->elements[j + 1]++;
467 /* now sort all vendor/arch changes */
468 if (classes->count > 4)
469 sat_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
470 /* finally add all classes. put erases last */
471 i = SOLVER_TRANSACTION_ERASE;
474 queue_unshift(classes, 0);
475 queue_unshift(classes, 0);
476 queue_unshift(classes, ntypes[i]);
477 queue_unshift(classes, i);
479 for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
483 if (i == SOLVER_TRANSACTION_ERASE)
485 queue_unshift(classes, 0);
486 queue_unshift(classes, 0);
487 queue_unshift(classes, ntypes[i]);
488 queue_unshift(classes, i);
493 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
495 Pool *pool = trans->pool;
501 for (i = 0; i < trans->steps.count; i++)
503 p = trans->steps.elements[i];
504 s = pool->solvables + p;
505 if (class <= SOLVER_TRANSACTION_MAXTYPE)
507 type = transaction_type(trans, p, mode);
512 if (!pool->installed || s->repo != pool->installed)
514 q = transaction_obs_pkg(trans, p);
517 sq = pool->solvables + q;
518 if (class == SOLVER_TRANSACTION_ARCHCHANGE)
520 if ((!from && !to) || (s->arch == from && sq->arch == to))
524 if (class == SOLVER_TRANSACTION_VENDORCHANGE)
526 Id v = s->vendor ? s->vendor : 1;
527 Id vq = sq->vendor ? sq->vendor : 1;
528 if ((!from && !to) || (v == from && vq == to))
534 sat_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
538 create_transaction_info(Transaction *trans, Queue *decisionq)
540 Pool *pool = trans->pool;
541 Queue *ti = &trans->transaction_info;
542 Repo *installed = pool->installed;
548 trans->transaction_installed = sat_free(trans->transaction_installed);
550 return; /* no info needed */
551 for (i = 0; i < decisionq->count; i++)
553 p = decisionq->elements[i];
554 if (p <= 0 || p == SYSTEMSOLVABLE)
556 s = pool->solvables + p;
557 if (!s->repo || s->repo == installed)
559 noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
560 FOR_PROVIDES(p2, pp2, s->name)
562 if (!MAPTST(&trans->transactsmap, p2))
564 s2 = pool->solvables + p2;
565 if (s2->repo != installed)
567 if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
569 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
571 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
576 if (s->obsoletes && !noobs)
578 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
579 while ((obs = *obsp++) != 0)
581 FOR_PROVIDES(p2, pp2, obs)
583 s2 = pool->solvables + p2;
584 if (s2->repo != installed)
586 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
588 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
596 sat_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
598 for (i = j = 0; i < ti->count; i += 2)
600 if (j && ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
602 ti->elements[j++] = ti->elements[i];
603 ti->elements[j++] = ti->elements[i + 1];
607 /* create transaction_installed helper */
608 trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
609 for (i = 0; i < ti->count; i += 2)
611 j = ti->elements[i + 1] - installed->start;
612 if (!trans->transaction_installed[j])
613 trans->transaction_installed[j] = ti->elements[i];
616 /* more than one package obsoletes us. compare */
618 if (trans->transaction_installed[j] > 0)
619 trans->transaction_installed[j] = -trans->transaction_installed[j];
620 q[0] = q[2] = ti->elements[i + 1];
621 q[1] = ti->elements[i];
622 q[3] = -trans->transaction_installed[j];
623 if (obsq_sortcmp(q, q + 2, pool) < 0)
624 trans->transaction_installed[j] = -ti->elements[i];
631 transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
633 Pool *pool = trans->pool;
634 Repo *installed = pool->installed;
639 if (noobsmap && !noobsmap->size)
640 noobsmap = 0; /* ignore empty map */
641 queue_empty(&trans->steps);
642 map_init(&trans->transactsmap, pool->nsolvables);
644 for (i = 0; i < decisionq->count; i++)
646 p = decisionq->elements[i];
647 s = pool->solvables + (p > 0 ? p : -p);
650 if (installed && s->repo == installed && p < 0)
651 MAPSET(&trans->transactsmap, -p);
652 if ((!installed || s->repo != installed) && p > 0)
655 const char *n = id2str(pool, s->name);
656 if (!strncmp(n, "patch:", 6))
658 if (!strncmp(n, "pattern:", 8))
661 MAPSET(&trans->transactsmap, p);
662 if (noobsmap && MAPTST(noobsmap, p))
666 MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
668 map_init_clone(&trans->noobsmap, noobsmap);
670 create_transaction_info(trans, decisionq);
674 FOR_REPO_SOLVABLES(installed, p, s)
676 if (MAPTST(&trans->transactsmap, p))
677 queue_push(&trans->steps, p);
680 for (i = 0; i < decisionq->count; i++)
682 p = decisionq->elements[i];
683 if (p > 0 && MAPTST(&trans->transactsmap, p))
684 queue_push(&trans->steps, p);
689 transaction_installedresult(Transaction *trans, Queue *installedq)
691 Pool *pool = trans->pool;
692 Repo *installed = pool->installed;
697 queue_empty(installedq);
698 /* first the new installs, than the kept packages */
699 for (i = 0; i < trans->steps.count; i++)
701 p = trans->steps.elements[i];
702 s = pool->solvables + p;
703 if (installed && s->repo == installed)
705 queue_push(installedq, p);
707 cutoff = installedq->count;
710 FOR_REPO_SOLVABLES(installed, p, s)
711 if (!MAPTST(&trans->transactsmap, p))
712 queue_push(installedq, p);
718 transaction_create_installedmap(Transaction *trans, Map *installedmap)
720 Pool *pool = trans->pool;
721 Repo *installed = pool->installed;
726 map_init(installedmap, pool->nsolvables);
727 for (i = 0; i < trans->steps.count; i++)
729 p = trans->steps.elements[i];
730 s = pool->solvables + p;
731 if (!installed || s->repo != installed)
732 MAPSET(installedmap, p);
736 FOR_REPO_SOLVABLES(installed, p, s)
737 if (!MAPTST(&trans->transactsmap, p))
738 MAPSET(installedmap, p);
743 transaction_calc_installsizechange(Transaction *trans)
748 transaction_create_installedmap(trans, &installedmap);
749 change = pool_calc_installsizechange(trans->pool, &installedmap);
750 map_free(&installedmap);
755 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
759 transaction_create_installedmap(trans, &installedmap);
760 pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
761 map_free(&installedmap);
764 struct _TransactionElement {
765 Id p; /* solvable id */
766 Id edges; /* pointer into edges data */
770 struct _TransactionOrderdata {
771 struct _TransactionElement *tes;
777 #define TYPE_BROKEN (1<<0)
778 #define TYPE_CON (1<<1)
780 #define TYPE_REQ_P (1<<2)
781 #define TYPE_PREREQ_P (1<<3)
783 #define TYPE_REQ (1<<4)
784 #define TYPE_PREREQ (1<<5)
786 #define TYPE_CYCLETAIL (1<<16)
787 #define TYPE_CYCLEHEAD (1<<17)
789 #define EDGEDATA_BLOCK 127
792 transaction_init(Transaction *trans, Pool *pool)
794 memset(trans, 0, sizeof(*trans));
799 transaction_init_clone(Transaction *trans, Transaction *srctrans)
801 memset(trans, 0, sizeof(*trans));
802 trans->pool = srctrans->pool;
803 queue_init_clone(&trans->steps, &srctrans->steps);
804 queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
805 if (srctrans->transaction_installed)
807 Repo *installed = srctrans->pool->installed;
808 trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
809 memcpy(trans->transaction_installed, srctrans->transaction_installed, (installed->end - installed->start) * sizeof(Id));
811 map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
812 map_init_clone(&trans->noobsmap, &srctrans->noobsmap);
813 if (srctrans->orderdata)
815 struct _TransactionOrderdata *od = srctrans->orderdata;
816 trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
817 trans->orderdata->tes = sat_malloc2(od->ntes, sizeof(*od->tes));
818 memcpy(trans->orderdata->tes, od->tes, od->ntes * sizeof(*od->tes));
819 trans->orderdata->ntes = od->ntes;
820 trans->orderdata->invedgedata = sat_malloc2(od->ninvedgedata, sizeof(Id));
821 memcpy(trans->orderdata->invedgedata, od->invedgedata, od->ninvedgedata * sizeof(Id));
822 trans->orderdata->ninvedgedata = od->ninvedgedata;
827 transaction_free(Transaction *trans)
829 queue_free(&trans->steps);
830 queue_free(&trans->transaction_info);
831 trans->transaction_installed = sat_free(trans->transaction_installed);
832 map_free(&trans->transactsmap);
833 map_free(&trans->noobsmap);
834 transaction_free_orderdata(trans);
838 transaction_free_orderdata(Transaction *trans)
840 if (trans->orderdata)
842 struct _TransactionOrderdata *od = trans->orderdata;
843 od->tes = sat_free(od->tes);
844 od->invedgedata = sat_free(od->invedgedata);
845 trans->orderdata = sat_free(trans->orderdata);
851 struct _TransactionElement *tes;
863 addteedge(struct orderdata *od, int from, int to, int type)
866 struct _TransactionElement *te;
871 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
874 for (i = te->edges; od->edgedata[i]; i += 2)
875 if (od->edgedata[i] == to)
877 /* test of brokenness */
878 if (type == TYPE_BROKEN)
879 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
882 od->edgedata[i + 1] |= type;
885 if (i + 1 == od->nedgedata)
887 /* printf("tail add %d\n", i - te->edges); */
890 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
894 /* printf("extend %d\n", i - te->edges); */
895 od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
897 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
898 i = od->nedgedata + (i - te->edges);
899 te->edges = od->nedgedata;
901 od->edgedata[i] = to;
902 od->edgedata[i + 1] = type;
903 od->edgedata[i + 2] = 0; /* end marker */
904 od->nedgedata = i + 3;
909 addedge(struct orderdata *od, Id from, Id to, int type)
911 Transaction *trans = od->trans;
912 Pool *pool = trans->pool;
914 struct _TransactionElement *te;
917 // printf("addedge %d %d type %d\n", from, to, type);
918 s = pool->solvables + from;
919 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
921 /* obsolete, map to install */
922 if (trans->transaction_installed[from - pool->installed->start] > 0)
923 from = trans->transaction_installed[from - pool->installed->start];
930 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
931 transaction_all_obs_pkgs(trans, from, &ti);
932 for (i = 0; i < ti.count; i++)
933 ret |= addedge(od, ti.elements[i], to, type);
938 s = pool->solvables + to;
939 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
941 /* obsolete, map to install */
942 if (trans->transaction_installed[to - pool->installed->start] > 0)
943 to = trans->transaction_installed[to - pool->installed->start];
950 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
951 transaction_all_obs_pkgs(trans, to, &ti);
952 for (i = 0; i < ti.count; i++)
953 ret |= addedge(od, from, ti.elements[i], type);
959 /* map from/to to te numbers */
960 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
967 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
973 return addteedge(od, i, to, type);
978 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
980 Transaction *trans = od->trans;
981 Pool *pool = trans->pool;
982 Id ti1buf[5], ti2buf[5];
986 /* both q1 and q2 are uninstalls. check if their TEs intersect */
987 /* common case: just one TE for both packages */
989 printf("havechoice %d %d %d\n", p, q1, q2);
991 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
993 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
995 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
997 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
999 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
1000 transaction_all_obs_pkgs(trans, q1, &ti1);
1001 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
1002 transaction_all_obs_pkgs(trans, q2, &ti2);
1003 for (i = 0; i < ti1.count; i++)
1004 for (j = 0; j < ti2.count; j++)
1005 if (ti1.elements[i] == ti2.elements[j])
1007 /* found a common edge */
1019 havescripts(Pool *pool, Id solvid)
1021 Solvable *s = pool->solvables + solvid;
1027 reqp = s->repo->idarraydata + s->requires;
1028 while ((req = *reqp++) != 0)
1030 if (req == SOLVABLE_PREREQMARKER)
1037 dep = id2str(pool, req);
1038 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1046 addsolvableedges(struct orderdata *od, Solvable *s)
1048 Transaction *trans = od->trans;
1049 Pool *pool = trans->pool;
1050 Id req, *reqp, con, *conp;
1052 int i, j, pre, numins;
1053 Repo *installed = pool->installed;
1059 printf("addsolvableedges %s\n", solvable2str(pool, s));
1061 p = s - pool->solvables;
1065 reqp = s->repo->idarraydata + s->requires;
1067 while ((req = *reqp++) != 0)
1069 if (req == SOLVABLE_PREREQMARKER)
1075 if (pre != TYPE_PREREQ && installed && s->repo == installed)
1077 /* ignore normal requires if we're getting obsoleted */
1078 if (trans->transaction_installed[p - pool->installed->start])
1083 numins = 0; /* number of packages to be installed providing it */
1084 provbyinst = 0; /* provided by kept package */
1085 FOR_PROVIDES(p2, pp2, req)
1087 s2 = pool->solvables + p2;
1090 reqq.count = 0; /* self provides */
1093 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1097 printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
1098 reqq.count = 0; /* provided by package that stays installed */
1104 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1105 continue; /* package stays uninstalled */
1107 if (s->repo == installed)
1109 /* s gets uninstalled */
1110 queue_pushunique(&reqq, p2);
1111 if (s2->repo != installed)
1116 if (s2->repo == installed)
1117 continue; /* s2 gets uninstalled */
1118 queue_pushunique(&reqq, p2);
1123 /* prune to harmless ->inst edges */
1124 for (i = j = 0; i < reqq.count; i++)
1125 if (pool->solvables[reqq.elements[i]].repo != installed)
1126 reqq.elements[j++] = reqq.elements[i];
1130 if (numins && reqq.count)
1132 if (s->repo == installed)
1134 for (i = 0; i < reqq.count; i++)
1136 if (pool->solvables[reqq.elements[i]].repo == installed)
1138 for (j = 0; j < reqq.count; j++)
1140 if (pool->solvables[reqq.elements[j]].repo != installed)
1142 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1143 continue; /* no self edge */
1145 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
1147 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1153 /* no mixed types, remove all deps on uninstalls */
1154 for (i = j = 0; i < reqq.count; i++)
1155 if (pool->solvables[reqq.elements[i]].repo != installed)
1156 reqq.elements[j++] = reqq.elements[i];
1161 for (i = 0; i < reqq.count; i++)
1164 p2 = reqq.elements[i];
1165 if (pool->solvables[p2].repo != installed)
1167 /* all elements of reqq are installs, thus have different TEs */
1168 choice = reqq.count - 1;
1169 if (pool->solvables[p].repo != installed)
1172 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1174 addedge(od, p, p2, pre);
1179 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1181 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1186 if (s->repo != installed)
1187 continue; /* no inst->uninst edges, please! */
1189 /* uninst -> uninst edge. Those make trouble. Only add if we must */
1190 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1192 /* p is obsoleted by another package and has no scripts */
1193 /* we assume that the obsoletor is good enough to replace p */
1198 for (j = 0; j < reqq.count; j++)
1202 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1207 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
1209 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1216 conp = s->repo->idarraydata + s->conflicts;
1217 while ((con = *conp++) != 0)
1219 FOR_PROVIDES(p2, pp2, con)
1223 s2 = pool->solvables + p2;
1226 if (s->repo == installed)
1228 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1230 /* deinstall p before installing p2 */
1231 addedge(od, p2, p, TYPE_CON);
1236 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1238 /* deinstall p2 before installing p */
1239 addedge(od, p, p2, TYPE_CON);
1246 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1248 /* we're getting deinstalled/updated. Try to do this before our
1249 * triggers are hit */
1250 for (i = 0; i < reqq.count; i++)
1252 Id tri = reqq.elements[i];
1253 FOR_PROVIDES(p2, pp2, tri)
1257 s2 = pool->solvables + p2;
1260 if (s2->name == s->name)
1261 continue; /* obsoleted anyway */
1262 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1264 /* deinstall/update p before installing p2 */
1266 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, p2), dep2str(pool, tri), solvid2str(pool, p));
1268 addedge(od, p2, p, TYPE_CON);
1277 /* break an edge in a cycle */
1279 breakcycle(struct orderdata *od, Id *cycle)
1281 Pool *pool = od->trans->pool;
1282 Id ddegmin, ddegmax, ddeg;
1284 struct _TransactionElement *te;
1287 ddegmin = ddegmax = 0;
1288 for (k = 0; cycle[k + 1]; k += 2)
1290 ddeg = od->edgedata[cycle[k + 1] + 1];
1293 if (!k || ddeg < ddegmin)
1299 if (ddeg == ddegmin)
1301 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1303 /* prefer k, as l comes from a package with contains scriptlets */
1308 /* same edge value, check for prereq */
1312 /* record brkoen cycle starting with the tail */
1313 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
1314 queue_push(&od->cycles, k / 2); /* cycle elements */
1315 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
1322 queue_push(&od->cyclesdata, cycle[k]);
1326 queue_push(&od->cyclesdata, 0); /* mark end */
1328 /* break that edge */
1329 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1332 if (ddegmin < TYPE_REQ)
1336 /* cycle recorded, print it */
1337 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1338 POOL_DEBUG(SAT_DEBUG_STATS, "CRITICAL ");
1339 POOL_DEBUG(SAT_DEBUG_STATS, "cycle: --> ");
1340 for (k = 0; cycle[k + 1]; k += 2)
1342 te = od->tes + cycle[k];
1343 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1344 POOL_DEBUG(SAT_DEBUG_STATS, "%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1346 POOL_DEBUG(SAT_DEBUG_STATS, "%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1348 POOL_DEBUG(SAT_DEBUG_STATS, "\n");
1352 dump_tes(struct orderdata *od)
1354 Pool *pool = od->trans->pool;
1357 struct _TransactionElement *te, *te2;
1360 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1362 Solvable *s = pool->solvables + te->p;
1363 POOL_DEBUG(SAT_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1364 if (s->repo != pool->installed)
1367 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1368 for (j = 0; j < obsq.count; j++)
1369 POOL_DEBUG(SAT_DEBUG_RESULT, " -%s\n", solvid2str(pool, obsq.elements[j]));
1371 for (j = te->edges; od->edgedata[j]; j += 2)
1373 te2 = od->tes + od->edgedata[j];
1374 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1375 POOL_DEBUG(SAT_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
1377 POOL_DEBUG(SAT_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
1384 reachable(struct orderdata *od, Id i)
1386 struct _TransactionElement *te = od->tes + i;
1392 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1394 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1396 if (!od->tes[k].mark)
1398 if (od->tes[k].mark == 2)
1409 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1412 Transaction *trans = od->trans;
1413 Pool *pool = trans->pool;
1415 struct _TransactionElement *te;
1422 printf("addcycleedges\n");
1423 for (i = 0; (j = cycle[i]) != 0; i++)
1424 printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
1427 /* first add all the tail cycle edges */
1429 /* see what we can reach from the cycle */
1431 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1433 for (i = 0; (j = cycle[i]) != 0; i++)
1435 od->tes[j].mark = -1;
1436 queue_push(todo, j);
1440 i = queue_pop(todo);
1444 te->mark = te->mark < 0 ? 2 : 1;
1445 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1447 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1449 if (od->tes[k].mark > 0)
1450 continue; /* no need to visit again */
1451 queue_push(todo, k);
1454 /* now all cycle TEs are marked with 2, all TEs reachable
1455 * from the cycle are marked with 1 */
1457 od->tes[tail].mark = 1; /* no need to add edges */
1459 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1462 continue; /* reachable from cycle */
1463 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1465 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1467 if (od->tes[k].mark != 2)
1469 /* We found an edge to the cycle. Add an extra edge to the tail */
1470 /* the TE was not reachable, so we're not creating a new cycle! */
1472 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));
1474 j -= te->edges; /* in case we move */
1475 addteedge(od, i, tail, TYPE_CYCLETAIL);
1477 break; /* one edge is enough */
1482 /* now add all head cycle edges */
1485 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1488 for (i = 0; (j = cycle[i]) != 0; i++)
1491 od->tes[j].mark = 2;
1493 /* first the head to save some time */
1494 te = od->tes + head;
1495 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1497 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1499 if (!od->tes[k].mark)
1501 if (od->tes[k].mark == -1)
1502 od->tes[k].mark = -2; /* no need for another edge */
1504 for (i = 0; cycle[i] != 0; i++)
1506 if (cycle[i] == head)
1508 te = od->tes + cycle[i];
1509 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1511 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1513 /* see if we can reach a cycle TE from k */
1514 if (!od->tes[k].mark)
1516 if (od->tes[k].mark == -1)
1519 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));
1521 addteedge(od, head, k, TYPE_CYCLEHEAD);
1522 od->tes[k].mark = -2; /* no need to add that one again */
1530 transaction_order(Transaction *trans, int flags)
1532 Pool *pool = trans->pool;
1533 Queue *tr = &trans->steps;
1534 Repo *installed = pool->installed;
1537 int i, j, k, numte, numedge;
1538 struct orderdata od;
1539 struct _TransactionElement *te;
1540 Queue todo, obsq, samerepoq, uninstq;
1541 int cycstart, cycel;
1549 start = now = sat_timems(0);
1550 POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
1551 /* free old data if present */
1552 if (trans->orderdata)
1554 struct _TransactionOrderdata *od = trans->orderdata;
1555 od->tes = sat_free(od->tes);
1556 od->invedgedata = sat_free(od->invedgedata);
1557 trans->orderdata = sat_free(trans->orderdata);
1560 /* create a transaction element for every active component */
1562 for (i = 0; i < tr->count; i++)
1564 p = tr->elements[i];
1565 s = pool->solvables + p;
1566 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1570 POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
1572 return; /* nothing to do... */
1574 numte++; /* leave first one zero */
1575 memset(&od, 0, sizeof(od));
1578 od.tes = sat_calloc(numte, sizeof(*od.tes));
1579 od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1582 queue_init(&od.cycles);
1584 /* initialize TEs */
1585 for (i = 0, te = od.tes + 1; i < tr->count; i++)
1587 p = tr->elements[i];
1588 s = pool->solvables + p;
1589 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1595 /* create dependency graph */
1596 for (i = 0; i < tr->count; i++)
1597 addsolvableedges(&od, pool->solvables + tr->elements[i]);
1601 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1602 for (j = te->edges; od.edgedata[j]; j += 2)
1604 POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1605 POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
1611 now = sat_timems(0);
1612 /* kill all cycles */
1614 for (i = numte - 1; i > 0; i--)
1615 queue_push(&todo, i);
1619 i = queue_pop(&todo);
1620 /* printf("- look at TE %d\n", i); */
1624 od.tes[i].mark = 2; /* done with that one */
1629 continue; /* already finished before */
1632 int edgestovisit = 0;
1633 /* new node, visit edges */
1634 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1636 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1638 if (od.tes[k].mark == 2)
1639 continue; /* no need to visit again */
1640 if (!edgestovisit++)
1641 queue_push(&todo, -i); /* end of edges marker */
1642 queue_push(&todo, k);
1645 te->mark = 2; /* no edges, done with that one */
1647 te->mark = 1; /* under investigation */
1650 /* oh no, we found a cycle */
1651 /* find start of cycle node (<0) */
1652 for (j = todo.count - 1; j >= 0; j--)
1653 if (todo.elements[j] == -i)
1657 /* build te/edge chain */
1659 for (j = k; j < todo.count; j++)
1660 if (todo.elements[j] < 0)
1661 todo.elements[k++] = -todo.elements[j];
1662 cycel = k - cycstart;
1664 /* make room for edges, two extra element for cycle loop + terminating 0 */
1665 while (todo.count < cycstart + 2 * cycel + 2)
1666 queue_push(&todo, 0);
1667 cycle = todo.elements + cycstart;
1668 cycle[cycel] = i; /* close the loop */
1669 cycle[2 * cycel + 1] = 0; /* terminator */
1670 for (k = cycel; k > 0; k--)
1672 cycle[k * 2] = cycle[k];
1673 te = od.tes + cycle[k - 1];
1674 assert(te->mark == 1);
1675 te->mark = 0; /* reset investigation marker */
1676 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1677 for (j = te->edges; od.edgedata[j]; j += 2)
1678 if (od.edgedata[j] == cycle[k])
1680 assert(od.edgedata[j]);
1681 cycle[k * 2 - 1] = j;
1683 /* now cycle looks like this: */
1684 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1685 breakcycle(&od, cycle);
1686 /* restart with start of cycle */
1687 todo.count = cycstart + 1;
1689 POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1690 POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
1692 now = sat_timems(0);
1693 /* now go through all broken cycles and create cycle edges to help
1695 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1697 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1698 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1700 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1702 if (od.cycles.elements[i + 2] < TYPE_REQ)
1703 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1705 POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
1710 /* all edges are finally set up and there are no cycles, now the easy part.
1711 * Create an ordered transaction */
1712 now = sat_timems(0);
1713 /* first invert all edges */
1714 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1715 te->mark = 1; /* term 0 */
1716 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1718 for (j = te->edges; od.edgedata[j]; j += 2)
1720 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1722 od.tes[od.edgedata[j]].mark++;
1726 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1731 POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
1732 od.invedgedata = sat_calloc(j + 1, sizeof(Id));
1733 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1735 for (j = te->edges; od.edgedata[j]; j += 2)
1737 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1739 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1742 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1743 te->edges = te->mark; /* edges now points into invedgedata */
1744 od.edgedata = sat_free(od.edgedata);
1745 od.nedgedata = j + 1;
1747 /* now the final ordering */
1748 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1750 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1751 for (j = te->edges; od.invedgedata[j]; j++)
1752 od.tes[od.invedgedata[j]].mark++;
1754 queue_init(&samerepoq);
1755 queue_init(&uninstq);
1757 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1760 if (installed && pool->solvables[te->p].repo == installed)
1761 queue_push(&uninstq, i);
1763 queue_push(&todo, i);
1765 assert(todo.count > 0 || uninstq.count > 0);
1766 oldcount = tr->count;
1773 temedianr = sat_calloc(numte, sizeof(Id));
1774 for (i = 1; i < numte; i++)
1776 Solvable *s = pool->solvables + od.tes[i].p;
1777 if (installed && s->repo == installed)
1780 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1785 /* select an TE i */
1787 i = queue_shift(&uninstq);
1788 else if (samerepoq.count)
1789 i = queue_shift(&samerepoq);
1790 else if (todo.count)
1792 /* find next repo/media */
1793 for (j = 0; j < todo.count; j++)
1795 if (!j || temedianr[todo.elements[j]] < lastmedia)
1798 lastmedia = temedianr[todo.elements[j]];
1801 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1803 /* move all matching TEs to samerepoq */
1804 for (i = j = 0; j < todo.count; j++)
1806 int k = todo.elements[j];
1807 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1808 queue_push(&samerepoq, k);
1810 todo.elements[i++] = k;
1814 assert(samerepoq.count);
1815 i = queue_shift(&samerepoq);
1821 queue_push(tr, te->p);
1823 printf("do %s [%d]\n", solvid2str(pool, te->p), temedianr[i]);
1825 s = pool->solvables + te->p;
1826 for (j = te->edges; od.invedgedata[j]; j++)
1828 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1829 assert(te2->mark > 0);
1830 if (--te2->mark == 0)
1832 Solvable *s = pool->solvables + te2->p;
1834 printf("free %s [%d]\n", solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1836 if (installed && s->repo == installed)
1837 queue_push(&uninstq, od.invedgedata[j]);
1838 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1839 queue_push(&samerepoq, od.invedgedata[j]);
1841 queue_push(&todo, od.invedgedata[j]);
1845 sat_free(temedianr);
1847 queue_free(&samerepoq);
1848 queue_free(&uninstq);
1850 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1851 assert(te->mark == 0);
1853 /* add back obsoleted packages */
1854 transaction_add_obsoleted(trans);
1855 assert(tr->count == oldcount);
1857 POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
1858 POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
1860 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1862 trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
1863 trans->orderdata->tes = od.tes;
1864 trans->orderdata->ntes = numte;
1865 trans->orderdata->invedgedata = od.invedgedata;
1866 trans->orderdata->ninvedgedata = od.nedgedata;
1871 sat_free(od.invedgedata);
1877 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1880 struct _TransactionOrderdata *od = trans->orderdata;
1881 struct _TransactionElement *te;
1884 return choices->count;
1887 /* initialization step */
1888 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1890 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1892 for (j = te->edges; od->invedgedata[j]; j++)
1893 od->tes[od->invedgedata[j]].mark++;
1895 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1897 queue_push(choices, te->p);
1898 return choices->count;
1900 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1901 if (te->p == chosen)
1904 return choices->count;
1907 /* hey! out-of-order installation! */
1910 for (j = te->edges; od->invedgedata[j]; j++)
1912 te = od->tes + od->invedgedata[j];
1913 assert(te->mark > 0 || te->mark == -1);
1914 if (te->mark > 0 && --te->mark == 0)
1915 queue_push(choices, te->p);
1917 return choices->count;
1921 transaction_add_obsoleted(Transaction *trans)
1923 Pool *pool = trans->pool;
1924 Repo *installed = pool->installed;
1927 int i, j, k, max, oldcount;
1931 if (!installed || !trans->steps.count)
1933 /* calculate upper bound */
1935 FOR_REPO_SOLVABLES(installed, p, s)
1936 if (MAPTST(&trans->transactsmap, p))
1941 steps = &trans->steps;
1942 oldcount = steps->count;
1943 queue_insertn(steps, 0, max);
1946 map_init(&done, installed->end - installed->start);
1948 for (j = 0, i = max; i < steps->count; i++)
1950 p = trans->steps.elements[i];
1951 if (pool->solvables[p].repo == installed)
1953 if (!trans->transaction_installed[p - pool->installed->start])
1954 trans->steps.elements[j++] = p;
1957 trans->steps.elements[j++] = p;
1959 transaction_all_obs_pkgs(trans, p, &obsq);
1960 for (k = 0; k < obsq.count; k++)
1962 p = obsq.elements[k];
1963 assert(p >= installed->start && p < installed->end);
1964 if (MAPTST(&done, p - installed->start))
1966 MAPSET(&done, p - installed->start);
1967 trans->steps.elements[j++] = p;
1971 /* free unneeded space */
1972 queue_truncate(steps, j);
1978 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1980 Pool *pool = trans->pool;
1985 if (MAPTST(seen, pkg))
1988 s = pool->solvables + pkg;
1990 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
1996 reqp = s->repo->idarraydata + s->requires;
1997 while ((req = *reqp++) != 0)
1999 if (req == SOLVABLE_PREREQMARKER)
2004 if (onlyprereq && !inpre)
2006 if (!strncmp(id2str(pool, req), "rpmlib(", 7))
2009 /* first check kept packages, then freshly installed, then not yet uninstalled */
2010 FOR_PROVIDES(p, pp, req)
2012 if (!MAPTST(ins, p))
2014 if (MAPTST(&trans->transactsmap, p))
2017 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2021 FOR_PROVIDES(p, pp, req)
2023 if (!MAPTST(ins, p))
2025 if (pool->solvables[p].repo == pool->installed)
2028 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2033 FOR_PROVIDES(p, pp, req)
2035 if (!MAPTST(ins, p))
2038 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2043 POOL_DEBUG(SAT_DEBUG_RESULT, " %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));
2050 transaction_check_order(Transaction *trans)
2052 Pool *pool = trans->pool;
2058 POOL_DEBUG(SAT_WARN, "\nchecking transaction order...\n");
2059 map_init(&ins, pool->nsolvables);
2060 map_init(&seen, pool->nsolvables);
2061 if (pool->installed)
2062 FOR_REPO_SOLVABLES(pool->installed, p, s)
2065 for (i = 0; i < trans->steps.count; i++)
2067 p = trans->steps.elements[i];
2068 s = pool->solvables + p;
2069 if (s->repo != pool->installed)
2071 if (s->repo != pool->installed)
2073 if (havescripts(pool, p))
2076 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2078 if (s->repo == pool->installed)
2083 POOL_DEBUG(SAT_WARN, "transaction order check done.\n");